|
@@ -11,15 +11,28 @@
|
|
*******************************************************************************/
|
|
*******************************************************************************/
|
|
|
|
|
|
#include "hal/io.h"
|
|
#include "hal/io.h"
|
|
|
|
+#include "hal/lsm6ds3.h"
|
|
|
|
+#include "log/esp_log.h"
|
|
|
|
+
|
|
|
|
+static const char* TAG = "io.c";
|
|
|
|
|
|
|
|
|
|
-static Encoder_Timer_t encoder_timer;
|
|
+static Encoder_t encoder;
|
|
|
|
+
|
|
|
|
+void Encoder_GPIO_Handler(void);
|
|
|
|
+static void switchA_debounce(void);
|
|
|
|
+static void switchB_debounce(void);
|
|
|
|
|
|
-void ICACHE_FLASH_ATTR Encoder_InitIO(uint32_t debounce_delay, switch_function_t
|
|
+void ICACHE_FLASH_ATTR Encoder_InitIO(void)
|
|
- switch_a_cb, switch_function_t switch_b_cb, switch_function_t imu_cb)
|
|
|
|
{
|
|
{
|
|
- ETS_GPIO_INTR_DISABLE();
|
|
+ ETS_GPIO_INTR_DISABLE();
|
|
- gpio_init();
|
|
+
|
|
|
|
+ gpio_init();
|
|
|
|
+
|
|
|
|
+ encoder.debounce_delay = DEBOUNCEDELAY;
|
|
|
|
+ encoder.switch_a_cb = NULL;
|
|
|
|
+ encoder.switch_b_cb = NULL;
|
|
|
|
+ encoder.imu_cb = NULL;
|
|
|
|
|
|
|
|
|
|
PIN_FUNC_SELECT(LDO_SHUTDOWN_MUX, LDO_SHUTDOWN_FUNC);
|
|
PIN_FUNC_SELECT(LDO_SHUTDOWN_MUX, LDO_SHUTDOWN_FUNC);
|
|
@@ -30,29 +43,40 @@ void ICACHE_FLASH_ATTR Encoder_InitIO(uint32_t debounce_delay, switch_function_t
|
|
|
|
|
|
gpio_output_set(BIT(LDO_SHUTDOWN), BIT(STATUS_LED), (BIT(LDO_SHUTDOWN)|BIT(STATUS_LED)), (BIT(SWITCH_A)|BIT(SWITCH_B)));
|
|
gpio_output_set(BIT(LDO_SHUTDOWN), BIT(STATUS_LED), (BIT(LDO_SHUTDOWN)|BIT(STATUS_LED)), (BIT(SWITCH_A)|BIT(SWITCH_B)));
|
|
|
|
|
|
-
|
|
+
|
|
- gpio_pin_intr_state_set(GPIO_ID_PIN(SWTICH_A), GPIO_PIN_INTR_ANYEDGE);
|
|
+ ETS_GPIO_INTR_ATTACH(Encoder_GPIO_Handler, NULL);
|
|
- gpio_pin_intr_state_set(GPIO_ID_PIN(SWITCH_B), GPIO_PIN_INTR_ANYEDGE);
|
|
|
|
|
|
|
|
- os_timer_disarm(&encoder_timer.switch_a);
|
|
+
|
|
- os_timer_disarm(&encoder_timer.switch_b);
|
|
+ os_timer_disarm(&encoder.switch_a_timer);
|
|
|
|
+ os_timer_disarm(&encoder.switch_b_timer);
|
|
|
|
+ os_timer_setfn(&encoder.switch_a_timer, (os_timer_func_t *)switchA_debounce, NULL);
|
|
|
|
+ os_timer_setfn(&encoder.switch_b_timer, (os_timer_func_t *)switchB_debounce, NULL);
|
|
|
|
|
|
- os_timer_setfn(&timerA, (os_timer_func_t *)switchA_debounce, NULL);
|
|
|
|
- os_timer_setfn(&timerB, (os_timer_func_t *)switchB_debounce, NULL);
|
|
|
|
- ETS_GPIO_INTR_ENABLE();
|
|
|
|
- return;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- void ICACHE_FLASH_ATTR Encoder_EnableAdaptiveSwitches(void)
|
|
+void ICACHE_FLASH_ATTR Encoder_SetSwitchCallback(switch_function_t switch_a_cb, switch_function_t switch_b_cb)
|
|
|
|
+{
|
|
|
|
+ encoder.switch_a_cb = switch_a_cb;
|
|
|
|
+ encoder.switch_b_cb = switch_b_cb;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void ICACHE_FLASH_ATTR Encoder_SetIMUCallback(imu_function_t imu_cb)
|
|
|
|
+{
|
|
|
|
+ encoder.imu_cb = imu_cb;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+ void ICACHE_FLASH_ATTR Encoder_EnableIOInterrupts(void)
|
|
{
|
|
{
|
|
- gpio_pin_intr_state_set(GPIO_ID_PIN(SWTICH_A), GPIO_PIN_INTR_ANYEDGE);
|
|
+ ETS_GPIO_INTR_DISABLE();
|
|
|
|
+ gpio_pin_intr_state_set(GPIO_ID_PIN(SWITCH_A), GPIO_PIN_INTR_ANYEDGE);
|
|
gpio_pin_intr_state_set(GPIO_ID_PIN(SWITCH_B), GPIO_PIN_INTR_ANYEDGE);
|
|
gpio_pin_intr_state_set(GPIO_ID_PIN(SWITCH_B), GPIO_PIN_INTR_ANYEDGE);
|
|
-
|
|
+ ETS_GPIO_INTR_ENABLE();
|
|
}
|
|
}
|
|
|
|
|
|
- void ICACHE_FLASH_ATTR Encoder_DisableAdaptiveSwitches(void)
|
|
+ void ICACHE_FLASH_ATTR Encoder_DisableIOInterrupts(void)
|
|
{
|
|
{
|
|
- gpio_pin_intr_state_set(GPIO_ID_PIN(SWTICH_A), GPIO_PIN_INTR_DISABLE);
|
|
+ ETS_GPIO_INTR_DISABLE();
|
|
|
|
+ gpio_pin_intr_state_set(GPIO_ID_PIN(SWITCH_A), GPIO_PIN_INTR_DISABLE);
|
|
gpio_pin_intr_state_set(GPIO_ID_PIN(SWITCH_B), GPIO_PIN_INTR_DISABLE);
|
|
gpio_pin_intr_state_set(GPIO_ID_PIN(SWITCH_B), GPIO_PIN_INTR_DISABLE);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -61,11 +85,6 @@ void ICACHE_FLASH_ATTR Encoder_ShutDown(void)
|
|
GPIO_OUTPUT_SET(LDO_SHUTDOWN, 0);
|
|
GPIO_OUTPUT_SET(LDO_SHUTDOWN, 0);
|
|
}
|
|
}
|
|
|
|
|
|
-void ICACHE_FLASH_ATTR Encoder_Restart(void)
|
|
|
|
-{
|
|
|
|
- return;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
void ICACHE_FLASH_ATTR Encoder_GPIO_Handler(void)
|
|
void ICACHE_FLASH_ATTR Encoder_GPIO_Handler(void)
|
|
{
|
|
{
|
|
uint8_t i;
|
|
uint8_t i;
|
|
@@ -80,119 +99,50 @@ void ICACHE_FLASH_ATTR Encoder_GPIO_Handler(void)
|
|
{
|
|
{
|
|
gpio_pin_intr_state_set(GPIO_ID_PIN(i), GPIO_PIN_INTR_DISABLE);
|
|
gpio_pin_intr_state_set(GPIO_ID_PIN(i), GPIO_PIN_INTR_DISABLE);
|
|
GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, gpio_status & BIT(i));
|
|
GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, gpio_status & BIT(i));
|
|
- #if MODULE_IO_DEBUG
|
|
+ ESP_LOGV(TAG, "Pin Interrupt %d", i);
|
|
- os_printf("GPIO: Pin %d Interrupt\r\n", i);
|
|
|
|
- #endif
|
|
|
|
switch(i)
|
|
switch(i)
|
|
{
|
|
{
|
|
- case PBA: {
|
|
+ case SWITCH_A:
|
|
-
|
|
+ {
|
|
- if (sysCfg.module_state.connected == true)
|
|
+ if (LSM6DS3_Get_Interrupts() != LSM6DS3_NO_INT)
|
|
- {
|
|
+ {
|
|
-
|
|
+ if (encoder.imu_cb != NULL)
|
|
- uint8_t fifoFlags;
|
|
+ encoder.imu_cb();
|
|
- fifoFlags = lsm6ds3_fifo_getFlags();
|
|
+ gpio_pin_intr_state_set(GPIO_ID_PIN(i), GPIO_PIN_INTR_ANYEDGE);
|
|
- os_printf("LSM6DS3: FIFO Flags %x\r\n", fifoFlags);
|
|
+
|
|
- if (fifoFlags & (LSM6DS3_FIFO_OVER_RUN | LSM6DS3_FIFO_FULL ) )
|
|
+ }
|
|
- {
|
|
+ else
|
|
- os_printf("LSM6DS3: FIFO Full / Overrun\r\n");
|
|
+ {
|
|
- lsm6ds3_reset();
|
|
+ if (encoder.switch_a_cb != NULL)
|
|
-
|
|
+ encoder.switch_a_cb();
|
|
- os_printf("LSM6DS3: Reset IMU\r\n");
|
|
+ os_timer_arm(&encoder.switch_a_timer, encoder.debounce_delay, 0);
|
|
- gpio_pin_intr_state_set(GPIO_ID_PIN(i), GPIO_PIN_INTR_ANYEDGE);
|
|
+ }
|
|
-
|
|
+ break;
|
|
-
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
- else if (fifoFlags & LSM6DS3_FIFO_THRESHOLD)
|
|
|
|
- {
|
|
|
|
- os_printf("LSM6DS3: FIFO Threshold\r\n");
|
|
|
|
- publish_fifo_dump();
|
|
|
|
- gpio_pin_intr_state_set(GPIO_ID_PIN(i), GPIO_PIN_INTR_ANYEDGE);
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
- else if (fifoFlags & LSM6DS3_FIFO_EMPTY)
|
|
|
|
- {
|
|
|
|
- os_printf("LSM6DS3: FIFO Empty\r\n");
|
|
|
|
- gpio_pin_intr_state_set(GPIO_ID_PIN(i), GPIO_PIN_INTR_ANYEDGE);
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- os_timer_arm(&pba_timer, DEBOUNCEDELAY, 0);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
- else if (sysCfg.module_state.connected == false && sysCfg.module_state.asleep == true)
|
|
|
|
- {
|
|
|
|
- sysCfg.module_state.asleep = false;
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- * MODULE NEEDS TO WAKE FROM SLEEP HERE IF PLACED TO SLEEP BY WI-FI PROCESS
|
|
|
|
-
|
|
|
|
- */
|
|
|
|
- lsm6ds3_read_motion();
|
|
|
|
- start_wifi_process();
|
|
|
|
- gpio_pin_intr_state_set(GPIO_ID_PIN(i), GPIO_PIN_INTR_ANYEDGE);
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
-
|
|
|
|
- lsm6ds3_read_motion();
|
|
|
|
- gpio_pin_intr_state_set(GPIO_ID_PIN(i), GPIO_PIN_INTR_ANYEDGE);
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
- break;
|
|
|
|
}
|
|
}
|
|
- case PBB: {
|
|
+ case SWITCH_B:
|
|
- if (sysCfg.module_state.connected == true)
|
|
+ {
|
|
- {
|
|
+ if (encoder.switch_b_cb != NULL)
|
|
- os_timer_arm(&pbb_timer, DEBOUNCEDELAY, 0);
|
|
+ encoder.switch_b_cb();
|
|
- }
|
|
+ os_timer_arm(&encoder.switch_b_timer, encoder.debounce_delay, 0);
|
|
- else if (sysCfg.module_state.searching == false && sysCfg.module_state.connected == false && sysCfg.module_state.asleep == true)
|
|
+ break;
|
|
- {
|
|
|
|
- sysCfg.module_state.asleep = false;
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- * MODULE NEEDS TO WAKE FROM SLEEP HERE IF PLACED TO SLEEP BY WI-FI PROCESS
|
|
|
|
-
|
|
|
|
- */
|
|
|
|
- start_wifi_process();
|
|
|
|
- gpio_pin_intr_state_set(GPIO_ID_PIN(i), GPIO_PIN_INTR_ANYEDGE);
|
|
|
|
- }
|
|
|
|
- break;
|
|
|
|
}
|
|
}
|
|
- default: {
|
|
+ default:
|
|
- #if MODULE_IO_DEBUG
|
|
+ {
|
|
- os_printf("Help! Unknown Interrupt: %d\r\n", gpio_status);
|
|
+ ESP_LOGW(TAG, "unexpected interrupt on pin %d", i);
|
|
- #endif
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
+ ETS_GPIO_INTR_ENABLE();
|
|
-
|
|
|
|
- NOTE: This is slightly a hack. When the start_wifi_process function is called, it will disable I/O interrupts to insure
|
|
|
|
- it is not called multiple times and schedule a Wi-Fi scan. The functions returns before the scan is started the following
|
|
|
|
- line is called and therefore the LSM6DS3 is free to trigger the ISR multiple times on any motion. This line prevents this.
|
|
|
|
- This could also be stopped by setting the INTS to INT2 when the first INT is called.
|
|
|
|
- */
|
|
|
|
- if (sysCfg.module_state.searching == false)
|
|
|
|
- {
|
|
|
|
- ETS_GPIO_INTR_ENABLE();
|
|
|
|
- os_printf("MODULE IO: I re-enabled interrupts..bitch.\r\n");
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
|
|
|
|
static void ICACHE_FLASH_ATTR switchA_debounce(void)
|
|
static void ICACHE_FLASH_ATTR switchA_debounce(void)
|
|
{
|
|
{
|
|
ETS_GPIO_INTR_DISABLE();
|
|
ETS_GPIO_INTR_DISABLE();
|
|
|
|
|
|
- uint32_t gpio_states = GPIO_REG_READ(GPIO_IN_ADDRESS);
|
|
|
|
-
|
|
|
|
gpio_pin_intr_state_set(GPIO_ID_PIN(SWITCH_A), GPIO_PIN_INTR_ANYEDGE);
|
|
gpio_pin_intr_state_set(GPIO_ID_PIN(SWITCH_A), GPIO_PIN_INTR_ANYEDGE);
|
|
- os_timer_disarm(&timerA);
|
|
+ os_timer_disarm(&encoder.switch_a_timer);
|
|
|
|
|
|
ETS_GPIO_INTR_ENABLE();
|
|
ETS_GPIO_INTR_ENABLE();
|
|
}
|
|
}
|
|
@@ -202,10 +152,8 @@ static void ICACHE_FLASH_ATTR switchB_debounce(void)
|
|
{
|
|
{
|
|
ETS_GPIO_INTR_DISABLE();
|
|
ETS_GPIO_INTR_DISABLE();
|
|
|
|
|
|
- uint32_t gpio_states = GPIO_REG_READ(GPIO_IN_ADDRESS);
|
|
|
|
-
|
|
|
|
gpio_pin_intr_state_set(GPIO_ID_PIN(SWITCH_B), GPIO_PIN_INTR_ANYEDGE);
|
|
gpio_pin_intr_state_set(GPIO_ID_PIN(SWITCH_B), GPIO_PIN_INTR_ANYEDGE);
|
|
- os_timer_disarm(&timerB);
|
|
+ os_timer_disarm(&encoder.switch_b_timer);
|
|
|
|
|
|
ETS_GPIO_INTR_ENABLE();
|
|
ETS_GPIO_INTR_ENABLE();
|
|
}
|
|
}
|