diff --git a/src/driver/drv_bl0937.c b/src/driver/drv_bl0937.c index 6731ac29d..68d935aec 100644 --- a/src/driver/drv_bl0937.c +++ b/src/driver/drv_bl0937.c @@ -1,370 +1,9 @@ // HLW8012 aka BL0937 #include "drv_bl0937.h" #include "../obk_config.h" - - - #include "../hal/hal_pins.h" #include "../new_pins.h" -OBKInterruptHandler g_handlers[PLATFORM_GPIO_MAX]; -OBKInterruptType g_modes[PLATFORM_GPIO_MAX]; - -#if PLATFORM_BEKEN - -#include "BkDriverTimer.h" -#include "BkDriverGpio.h" -#include "sys_timer.h" -#include "gw_intf.h" - -void Beken_Interrupt(unsigned char pinNum) { - if (g_handlers[pinNum]) { - g_handlers[pinNum](pinNum); - } -} - -void HAL_AttachInterrupt(int pinIndex, OBKInterruptType mode, OBKInterruptHandler function) { - g_handlers[pinIndex] = function; - int bk_mode; - if (mode == INTERRUPT_RISING) { - bk_mode = IRQ_TRIGGER_RISING_EDGE; - } - else { - bk_mode = IRQ_TRIGGER_FALLING_EDGE; - } - gpio_int_enable(pinIndex, bk_mode, Beken_Interrupt); -} -void HAL_DetachInterrupt(int pinIndex) { - if (g_handlers[pinIndex] == 0) { - return; // already removed; - } - gpio_int_disable(pinIndex); - g_handlers[pinIndex] = 0; -} - -#elif PLATFORM_W600 || PLATFORM_W800 - -void W600_Interrupt(void* context) { - int obkPinNum = (int)(intptr_t)context; - int w600Pin = HAL_GetGPIOPin(obkPinNum); - tls_clr_gpio_irq_status(w600Pin); - if (g_handlers[obkPinNum]) { - g_handlers[obkPinNum](obkPinNum); - } -} - -void HAL_AttachInterrupt(int pinIndex, OBKInterruptType mode, OBKInterruptHandler function) { - g_handlers[pinIndex] = function; - int w600Pin = HAL_GetGPIOPin(pinIndex); - tls_gpio_isr_register(w600Pin, W600_Interrupt, (void*)(intptr_t)pinIndex); - int w_mode; - if (mode == INTERRUPT_RISING) { - w_mode = WM_GPIO_IRQ_TRIG_RISING_EDGE; - } - else { - w_mode = WM_GPIO_IRQ_TRIG_FALLING_EDGE; - } - tls_gpio_irq_enable(w600Pin, w_mode); -} -void HAL_DetachInterrupt(int pinIndex) { - if (g_handlers[pinIndex] == 0) { - return; // already removed; - } - int w600Pin = HAL_GetGPIOPin(pinIndex); - tls_gpio_irq_disable(w600Pin); - g_handlers[pinIndex] = 0; -} - - - -#elif PLATFORM_BL602 - -#include "hal_gpio.h" -#include "bl_gpio.h" - -void BL602_Interrupt(void* context) { - int obkPinNum = (int)context; - if (g_handlers[obkPinNum]) { - g_handlers[obkPinNum](obkPinNum); - } - bl_gpio_intmask(obkPinNum, 0); -} - -void HAL_AttachInterrupt(int pinIndex, OBKInterruptType mode, OBKInterruptHandler function) { - g_handlers[pinIndex] = function; - int bl_mode; - if (mode == INTERRUPT_RISING) { - bl_mode = GPIO_INT_TRIG_POS_PULSE; - } - else { - bl_mode = GPIO_INT_TRIG_NEG_PULSE; - } - hal_gpio_register_handler(BL602_Interrupt, pinIndex, - GPIO_INT_CONTROL_ASYNC, bl_mode, (void*)pinIndex); -} -void HAL_DetachInterrupt(int pinIndex) { - if (g_handlers[pinIndex] == 0) { - return; // already removed; - } - g_handlers[pinIndex] = 0; -} - -#elif PLATFORM_LN882H - -#include "../../sdk/OpenLN882H/mcu/driver_ln882h/hal/hal_common.h" -#include "../../sdk/OpenLN882H/mcu/driver_ln882h/hal/hal_gpio.h" - -uint32_t GetBaseForPin(int pinIndex) -{ - return pinIndex < 16 ? GPIOA_BASE : GPIOB_BASE; -} - -int GetIRQForPin(int pinIndex) -{ - return pinIndex < 16 ? GPIOA_IRQn : GPIOB_IRQn; -} - -uint16_t GetGPIOForPin(int pinIndex) -{ - return (uint16_t)1 << (uint16_t)(pinIndex % 16); -} -void Shared_Handler() { - for (int i = 0; i < PLATFORM_GPIO_MAX; i++) { - if (g_handlers[i]) { - uint32_t base = GetBaseForPin(i); - uint16_t gpio_pin = GetGPIOForPin(i); - if (hal_gpio_pin_get_it_flag(base, gpio_pin) == HAL_SET) { - hal_gpio_pin_clr_it_flag(base, gpio_pin); - g_handlers[i](i); - } - } - } -} -void GPIOA_IRQHandler() -{ - Shared_Handler(); -} - -void GPIOB_IRQHandler() -{ - Shared_Handler(); -} - -void HAL_AttachInterrupt(int pinIndex, OBKInterruptType mode, OBKInterruptHandler function) { - g_handlers[pinIndex] = function; - - int ln_mode; - if (mode == INTERRUPT_RISING) { - ln_mode = GPIO_INT_RISING; - } - else { - ln_mode = GPIO_INT_FALLING; - } - hal_gpio_pin_it_cfg(GetBaseForPin(pinIndex), GetGPIOForPin(pinIndex), ln_mode); - hal_gpio_pin_it_en(GetBaseForPin(pinIndex), GetGPIOForPin(pinIndex), HAL_ENABLE); - NVIC_SetPriority(GetIRQForPin(pinIndex), 1); - NVIC_EnableIRQ(GetIRQForPin(pinIndex)); - -} - -void HAL_DetachInterrupt(int pinIndex) { - if (g_handlers[pinIndex] == 0) { - return; // already removed; - } - g_handlers[pinIndex] = 0; -} - -#elif PLATFORM_REALTEK - -#include "gpio_irq_api.h" -#include "../hal/realtek/hal_pinmap_realtek.h" - - -void Realtek_Interrupt(uint32_t obkPinNum, gpio_irq_event event) -{ - if (g_handlers[obkPinNum]) { - g_handlers[obkPinNum](obkPinNum); - } -} - -void HAL_AttachInterrupt(int pinIndex, OBKInterruptType mode, OBKInterruptHandler function) { - g_handlers[pinIndex] = function; - - rtlPinMapping_t *rtl_cf = g_pins + pinIndex; -#if PLATFORM_RTL87X0C - if (rtl_cf->gpio != NULL) - { - hal_pinmux_unregister(rtl_cf->pin, PID_GPIO); - os_free(rtl_cf->gpio); - rtl_cf->gpio = NULL; - } -#endif - rtl_cf->irq = os_malloc(sizeof(gpio_irq_t)); - memset(rtl_cf->irq, 0, sizeof(gpio_irq_t)); - - int rtl_mode; - if (mode == INTERRUPT_RISING) { - rtl_mode = IRQ_RISE; - } - else { - rtl_mode = IRQ_FALL; - } - gpio_irq_init(rtl_cf->irq, rtl_cf->pin, Realtek_Interrupt, pinIndex); - gpio_irq_set(rtl_cf->irq, rtl_mode, 1); - gpio_irq_enable(rtl_cf->irq); - -} -void HAL_DetachInterrupt(int pinIndex) { - if (g_handlers[pinIndex] == 0) { - return; // already removed; - } - rtlPinMapping_t *rtl_cf = g_pins + pinIndex; - gpio_irq_free(rtl_cf->irq); - os_free(rtl_cf->irq); - rtl_cf->irq = NULL; - g_handlers[pinIndex] = 0; -} - - -#elif PLATFORM_ECR6600 - -#include "gpio.h" - -void ECR6600_Interrupt(unsigned char obkPinNum) { - if (g_handlers[obkPinNum]) { - g_handlers[obkPinNum](obkPinNum); - } -} - -void HAL_AttachInterrupt(int pinIndex, OBKInterruptType mode, OBKInterruptHandler function) { - g_handlers[pinIndex] = function; - - T_GPIO_ISR_CALLBACK cf1isr; - cf1isr.gpio_callback = (&ECR6600_Interrupt); - cf1isr.gpio_data = pinIndex; - - int ecr_mode; - if (mode == INTERRUPT_RISING) { - ecr_mode = DRV_GPIO_ARG_INTR_MODE_P_EDGE; - } - else { - ecr_mode = DRV_GPIO_ARG_INTR_MODE_N_EDGE; - } - drv_gpio_ioctrl(pinIndex, DRV_GPIO_CTRL_INTR_MODE, ecr_mode); - drv_gpio_ioctrl(pinIndex, DRV_GPIO_CTRL_REGISTER_ISR, (int)&cf1isr); - drv_gpio_ioctrl(pinIndex, DRV_GPIO_CTRL_INTR_ENABLE, 0); - -} -void HAL_DetachInterrupt(int pinIndex) { - if (g_handlers[pinIndex] == 0) { - return; // already removed; - } - - drv_gpio_ioctrl(pinIndex, DRV_GPIO_CTRL_INTR_DISABLE, 0); - g_handlers[pinIndex] = 0; -} - - -#elif PLATFORM_XRADIO - -#include "../hal/xradio/hal_pinmap_xradio.h" -extern void HAL_XR_ConfigurePin(GPIO_Port port, GPIO_Pin pin, GPIO_WorkMode mode, GPIO_PullType pull); - - -void XRadio_Interrupt(void* context) { - int obkPinNum = (int)context; - if (g_handlers[obkPinNum]) { - g_handlers[obkPinNum](obkPinNum); - } -} - -void HAL_AttachInterrupt(int pinIndex, OBKInterruptType mode, OBKInterruptHandler function) { - g_handlers[pinIndex] = function; - - xrpin_t *xr_cf = g_pins + pinIndex; - HAL_XR_ConfigurePin(xr_cf->port, xr_cf->pin, GPIOx_Pn_F6_EINT, GPIO_PULL_UP); - GPIO_IrqParam cfparam; - int xr_mode; - if (mode == INTERRUPT_RISING) { - xr_mode = GPIO_IRQ_EVT_RISING_EDGE; - } - else { - xr_mode = GPIO_IRQ_EVT_FALLING_EDGE; - } - cfparam.event = xr_mode; - cfparam.callback = XRadio_Interrupt; - cfparam.arg = (void*)pinIndex; - HAL_GPIO_EnableIRQ(xr_cf->port, xr_cf->pin, &cfparam); - -} -void HAL_DetachInterrupt(int pinIndex) { - if (g_handlers[pinIndex] == 0) { - return; // already removed; - } - xrpin_t* xr_cf; - xr_cf = g_pins + pinIndex; - HAL_GPIO_DeInit(xr_cf->port, xr_cf->pin); - HAL_GPIO_DisableIRQ(xr_cf->port, xr_cf->pin); - g_handlers[pinIndex] = 0; -} - -#elif PLATFORM_ESP8266 || PLATFORM_ESPIDF - -#include "../hal/espidf/hal_pinmap_espidf.h" - - -void ESP_Interrupt(void* context) { - int obkPinNum = (int)context; - if (g_handlers[obkPinNum]) { - g_handlers[obkPinNum](obkPinNum); - } -} - -bool b_esp_ready = false; -void HAL_AttachInterrupt(int pinIndex, OBKInterruptType mode, OBKInterruptHandler function) { - g_handlers[pinIndex] = function; - - if (b_esp_ready == false) { - gpio_install_isr_service(0); - b_esp_ready = true; - } - espPinMapping_t* esp_cf = g_pins + pinIndex; - int esp_mode; - if (mode == INTERRUPT_RISING) { - esp_mode = GPIO_INTR_POSEDGE; - } - else { - esp_mode = GPIO_INTR_NEGEDGE; - } - ESP_ConfigurePin(esp_cf->pin, GPIO_MODE_INPUT, true, false, esp_mode); - gpio_isr_handler_add(esp_cf->pin, ESP_Interrupt, (void*)pinIndex); -} -void HAL_DetachInterrupt(int pinIndex) { - if (g_handlers[pinIndex] == 0) { - return; // already removed; - } - - espPinMapping_t* esp_cf; - esp_cf = g_pins + pinIndex; - gpio_isr_handler_remove(esp_cf->pin); - ///gpio_uninstall_isr_service(); - g_handlers[pinIndex] = 0; -} - - -#else -void HAL_AttachInterrupt(int pinIndex, OBKInterruptType mode, OBKInterruptHandler function) { - -} -void HAL_DetachInterrupt(int pinIndex) { - -} - - -#endif - - - #if ENABLE_DRIVER_BL0937 //dummy @@ -379,10 +18,6 @@ void HAL_DetachInterrupt(int pinIndex) { #include "drv_pwrCal.h" #include "drv_uart.h" - - - - #define DEFAULT_VOLTAGE_CAL 0.13253012048f #define DEFAULT_CURRENT_CAL 0.0118577075f #define DEFAULT_POWER_CAL 1.5f diff --git a/src/hal/bk7231/hal_pins_bk7231.c b/src/hal/bk7231/hal_pins_bk7231.c index 568ce4718..6e9ad4966 100644 --- a/src/hal/bk7231/hal_pins_bk7231.c +++ b/src/hal/bk7231/hal_pins_bk7231.c @@ -3,6 +3,7 @@ #include "../../logging/logging.h" #include "../../new_cfg.h" #include "../../new_pins.h" +#include "../hal_pins.h" //#include "../../new_pins.h" #include @@ -151,3 +152,37 @@ void HAL_PIN_PWM_Update(int index, float value) { unsigned int HAL_GetGPIOPin(int index) { return index; } + +OBKInterruptHandler g_handlers[PLATFORM_GPIO_MAX]; +OBKInterruptType g_modes[PLATFORM_GPIO_MAX]; + +#include "BkDriverTimer.h" +#include "BkDriverGpio.h" +#include "sys_timer.h" +#include "gw_intf.h" + +void Beken_Interrupt(unsigned char pinNum) { + if (g_handlers[pinNum]) { + g_handlers[pinNum](pinNum); + } +} + +void HAL_AttachInterrupt(int pinIndex, OBKInterruptType mode, OBKInterruptHandler function) { + g_handlers[pinIndex] = function; + int bk_mode; + if (mode == INTERRUPT_RISING) { + bk_mode = IRQ_TRIGGER_RISING_EDGE; + } + else { + bk_mode = IRQ_TRIGGER_FALLING_EDGE; + } + gpio_int_enable(pinIndex, bk_mode, Beken_Interrupt); +} +void HAL_DetachInterrupt(int pinIndex) { + if (g_handlers[pinIndex] == 0) { + return; // already removed; + } + gpio_int_disable(pinIndex); + g_handlers[pinIndex] = 0; +} + diff --git a/src/hal/bl602/hal_pins_bl602.c b/src/hal/bl602/hal_pins_bl602.c index 5fe3654ed..b82e0cadf 100644 --- a/src/hal/bl602/hal_pins_bl602.c +++ b/src/hal/bl602/hal_pins_bl602.c @@ -3,6 +3,7 @@ #include "../../new_pins.h" #include "../../new_common.h" #include "../../logging/logging.h" +#include "../hal_pins.h" #include "bl_gpio.h" @@ -98,4 +99,36 @@ unsigned int HAL_GetGPIOPin(int index) { return index; } +OBKInterruptHandler g_handlers[PLATFORM_GPIO_MAX]; +OBKInterruptType g_modes[PLATFORM_GPIO_MAX]; + +#include "hal_gpio.h" + +void BL602_Interrupt(void* context) { + int obkPinNum = (int)context; + if (g_handlers[obkPinNum]) { + g_handlers[obkPinNum](obkPinNum); + } + bl_gpio_intmask(obkPinNum, 0); +} + +void HAL_AttachInterrupt(int pinIndex, OBKInterruptType mode, OBKInterruptHandler function) { + g_handlers[pinIndex] = function; + int bl_mode; + if (mode == INTERRUPT_RISING) { + bl_mode = GPIO_INT_TRIG_POS_PULSE; + } + else { + bl_mode = GPIO_INT_TRIG_NEG_PULSE; + } + hal_gpio_register_handler(BL602_Interrupt, pinIndex, + GPIO_INT_CONTROL_ASYNC, bl_mode, (void*)pinIndex); +} +void HAL_DetachInterrupt(int pinIndex) { + if (g_handlers[pinIndex] == 0) { + return; // already removed; + } + g_handlers[pinIndex] = 0; +} + #endif diff --git a/src/hal/ecr6600/hal_pins_ecr6600.c b/src/hal/ecr6600/hal_pins_ecr6600.c index b8111da62..e259e2106 100644 --- a/src/hal/ecr6600/hal_pins_ecr6600.c +++ b/src/hal/ecr6600/hal_pins_ecr6600.c @@ -4,6 +4,7 @@ #include "../../logging/logging.h" #include "../../new_cfg.h" #include "../../new_pins.h" +#include "../hal_pins.h" #include "hal_gpio.h" #include "gpio.h" #include "pwm.h" @@ -306,4 +307,42 @@ unsigned int HAL_GetGPIOPin(int index) return index; } +OBKInterruptHandler g_handlers[PLATFORM_GPIO_MAX]; +OBKInterruptType g_modes[PLATFORM_GPIO_MAX]; + +void ECR6600_Interrupt(unsigned char obkPinNum) { + if (g_handlers[obkPinNum]) { + g_handlers[obkPinNum](obkPinNum); + } +} + +void HAL_AttachInterrupt(int pinIndex, OBKInterruptType mode, OBKInterruptHandler function) { + g_handlers[pinIndex] = function; + + T_GPIO_ISR_CALLBACK cf1isr; + cf1isr.gpio_callback = (&ECR6600_Interrupt); + cf1isr.gpio_data = pinIndex; + + int ecr_mode; + if (mode == INTERRUPT_RISING) { + ecr_mode = DRV_GPIO_ARG_INTR_MODE_P_EDGE; + } + else { + ecr_mode = DRV_GPIO_ARG_INTR_MODE_N_EDGE; + } + drv_gpio_ioctrl(pinIndex, DRV_GPIO_CTRL_INTR_MODE, ecr_mode); + drv_gpio_ioctrl(pinIndex, DRV_GPIO_CTRL_REGISTER_ISR, (int)&cf1isr); + drv_gpio_ioctrl(pinIndex, DRV_GPIO_CTRL_INTR_ENABLE, 0); + +} +void HAL_DetachInterrupt(int pinIndex) { + if (g_handlers[pinIndex] == 0) { + return; // already removed; + } + + drv_gpio_ioctrl(pinIndex, DRV_GPIO_CTRL_INTR_DISABLE, 0); + g_handlers[pinIndex] = 0; +} + + #endif // PLATFORM_ECR6600 diff --git a/src/hal/espidf/hal_pins_espidf.c b/src/hal/espidf/hal_pins_espidf.c index 2f4b1ad6b..028f440d3 100644 --- a/src/hal/espidf/hal_pins_espidf.c +++ b/src/hal/espidf/hal_pins_espidf.c @@ -6,6 +6,7 @@ #include "../../new_pins.h" #include "hal_pinmap_espidf.h" #include "driver/ledc.h" +#include "../hal_pins.h" #ifdef CONFIG_IDF_TARGET_ESP32C3 @@ -606,4 +607,46 @@ unsigned int HAL_GetGPIOPin(int index) return index; } + +OBKInterruptHandler g_handlers[PLATFORM_GPIO_MAX]; +OBKInterruptType g_modes[PLATFORM_GPIO_MAX]; + +void ESP_Interrupt(void* context) { + int obkPinNum = (int)context; + if (g_handlers[obkPinNum]) { + g_handlers[obkPinNum](obkPinNum); + } +} + +bool b_esp_ready = false; +void HAL_AttachInterrupt(int pinIndex, OBKInterruptType mode, OBKInterruptHandler function) { + g_handlers[pinIndex] = function; + + if (b_esp_ready == false) { + gpio_install_isr_service(0); + b_esp_ready = true; + } + espPinMapping_t* esp_cf = g_pins + pinIndex; + int esp_mode; + if (mode == INTERRUPT_RISING) { + esp_mode = GPIO_INTR_POSEDGE; + } + else { + esp_mode = GPIO_INTR_NEGEDGE; + } + ESP_ConfigurePin(esp_cf->pin, GPIO_MODE_INPUT, true, false, esp_mode); + gpio_isr_handler_add(esp_cf->pin, ESP_Interrupt, (void*)pinIndex); +} +void HAL_DetachInterrupt(int pinIndex) { + if (g_handlers[pinIndex] == 0) { + return; // already removed; + } + + espPinMapping_t* esp_cf; + esp_cf = g_pins + pinIndex; + gpio_isr_handler_remove(esp_cf->pin); + ///gpio_uninstall_isr_service(); + g_handlers[pinIndex] = 0; +} + #endif // PLATFORM_ESPIDF diff --git a/src/hal/ln882h/hal_pins_ln882h.c b/src/hal/ln882h/hal_pins_ln882h.c index 44bf1638e..0ae9a3fcc 100644 --- a/src/hal/ln882h/hal_pins_ln882h.c +++ b/src/hal/ln882h/hal_pins_ln882h.c @@ -4,10 +4,13 @@ #include "../../logging/logging.h" #include "../../new_cfg.h" #include "../../new_pins.h" +#include "../hal_pins.h" #include "hal_pinmap_ln882h.h" // LN882H header #include "hal/hal_adv_timer.h" #include "hal/hal_clock.h" +#include "hal/hal_common.h" +#include "hal/hal_gpio.h" #define IS_QSPI_PIN(index) (index > 12 && index < 19) @@ -239,4 +242,68 @@ unsigned int HAL_GetGPIOPin(int index) { return index; } +OBKInterruptHandler g_handlers[PLATFORM_GPIO_MAX]; +OBKInterruptType g_modes[PLATFORM_GPIO_MAX]; + + +uint32_t GetBaseForPin(int pinIndex) +{ + return pinIndex < 16 ? GPIOA_BASE : GPIOB_BASE; +} + +int GetIRQForPin(int pinIndex) +{ + return pinIndex < 16 ? GPIOA_IRQn : GPIOB_IRQn; +} + +uint16_t GetGPIOForPin(int pinIndex) +{ + return (uint16_t)1 << (uint16_t)(pinIndex % 16); +} +void Shared_Handler() { + for (int i = 0; i < PLATFORM_GPIO_MAX; i++) { + if (g_handlers[i]) { + uint32_t base = GetBaseForPin(i); + uint16_t gpio_pin = GetGPIOForPin(i); + if (hal_gpio_pin_get_it_flag(base, gpio_pin) == HAL_SET) { + hal_gpio_pin_clr_it_flag(base, gpio_pin); + g_handlers[i](i); + } + } + } +} +void GPIOA_IRQHandler() +{ + Shared_Handler(); +} + +void GPIOB_IRQHandler() +{ + Shared_Handler(); +} + +void HAL_AttachInterrupt(int pinIndex, OBKInterruptType mode, OBKInterruptHandler function) { + g_handlers[pinIndex] = function; + + int ln_mode; + if (mode == INTERRUPT_RISING) { + ln_mode = GPIO_INT_RISING; + } + else { + ln_mode = GPIO_INT_FALLING; + } + hal_gpio_pin_it_cfg(GetBaseForPin(pinIndex), GetGPIOForPin(pinIndex), ln_mode); + hal_gpio_pin_it_en(GetBaseForPin(pinIndex), GetGPIOForPin(pinIndex), HAL_ENABLE); + NVIC_SetPriority(GetIRQForPin(pinIndex), 1); + NVIC_EnableIRQ(GetIRQForPin(pinIndex)); + +} + +void HAL_DetachInterrupt(int pinIndex) { + if (g_handlers[pinIndex] == 0) { + return; // already removed; + } + g_handlers[pinIndex] = 0; +} + #endif // PLATFORM_LN882H diff --git a/src/hal/realtek/hal_pins_realtek.c b/src/hal/realtek/hal_pins_realtek.c index 562072ac0..ccef7ec8e 100644 --- a/src/hal/realtek/hal_pins_realtek.c +++ b/src/hal/realtek/hal_pins_realtek.c @@ -193,4 +193,54 @@ unsigned int HAL_GetGPIOPin(int index) return index; } +OBKInterruptHandler g_handlers[PLATFORM_GPIO_MAX]; +OBKInterruptType g_modes[PLATFORM_GPIO_MAX]; + +#include "gpio_irq_api.h" + +void Realtek_Interrupt(uint32_t obkPinNum, gpio_irq_event event) +{ + if (g_handlers[obkPinNum]) { + g_handlers[obkPinNum](obkPinNum); + } +} + +void HAL_AttachInterrupt(int pinIndex, OBKInterruptType mode, OBKInterruptHandler function) { + g_handlers[pinIndex] = function; + + rtlPinMapping_t *rtl_cf = g_pins + pinIndex; +#if PLATFORM_RTL87X0C + if (rtl_cf->gpio != NULL) + { + hal_pinmux_unregister(rtl_cf->pin, PID_GPIO); + os_free(rtl_cf->gpio); + rtl_cf->gpio = NULL; + } +#endif + rtl_cf->irq = os_malloc(sizeof(gpio_irq_t)); + memset(rtl_cf->irq, 0, sizeof(gpio_irq_t)); + + int rtl_mode; + if (mode == INTERRUPT_RISING) { + rtl_mode = IRQ_RISE; + } + else { + rtl_mode = IRQ_FALL; + } + gpio_irq_init(rtl_cf->irq, rtl_cf->pin, Realtek_Interrupt, pinIndex); + gpio_irq_set(rtl_cf->irq, rtl_mode, 1); + gpio_irq_enable(rtl_cf->irq); + +} +void HAL_DetachInterrupt(int pinIndex) { + if (g_handlers[pinIndex] == 0) { + return; // already removed; + } + rtlPinMapping_t *rtl_cf = g_pins + pinIndex; + gpio_irq_free(rtl_cf->irq); + os_free(rtl_cf->irq); + rtl_cf->irq = NULL; + g_handlers[pinIndex] = 0; +} + #endif // PLATFORM_REALTEK diff --git a/src/hal/tr6260/hal_pins_tr6260.c b/src/hal/tr6260/hal_pins_tr6260.c index 2f69c0414..20fd32c49 100644 --- a/src/hal/tr6260/hal_pins_tr6260.c +++ b/src/hal/tr6260/hal_pins_tr6260.c @@ -4,6 +4,7 @@ #include "../../logging/logging.h" #include "../../new_cfg.h" #include "../../new_pins.h" +#include "../hal_pins.h" #include "drv_gpio.h" #include "drv_pwm.h" #include "soc_pin_mux.h" @@ -285,4 +286,13 @@ unsigned int HAL_GetGPIOPin(int index) return index; } +void HAL_AttachInterrupt(int pinIndex, OBKInterruptType mode, OBKInterruptHandler function) { + +} +void HAL_DetachInterrupt(int pinIndex) { + +} + + + #endif // PLATFORM_TR6260 diff --git a/src/hal/w800/hal_pins_w800.c b/src/hal/w800/hal_pins_w800.c index 727941686..9810794d5 100644 --- a/src/hal/w800/hal_pins_w800.c +++ b/src/hal/w800/hal_pins_w800.c @@ -2,6 +2,8 @@ #include "../../new_common.h" #include "../../logging/logging.h" +#include "../hal_pins.h" +#include "../../new_pins.h" #include "wm_include.h" @@ -114,7 +116,7 @@ static int IsPinIndexOk(int index) { return 0; return 1; } -static int PIN_GetPWMIndexForPinIndex(int index) { +int PIN_GetPWMIndexForPinIndex(int index) { return g_pins[index].pwm_channel; } const char* HAL_PIN_GetPinNameAlias(int index) { @@ -248,4 +250,39 @@ void HAL_PIN_PWM_Update(int index, float value) { unsigned int HAL_GetGPIOPin(int index) { return g_pins[index].code; } + +OBKInterruptHandler g_handlers[PLATFORM_GPIO_MAX]; +OBKInterruptType g_modes[PLATFORM_GPIO_MAX]; + +void W600_Interrupt(void* context) { + int obkPinNum = (int)(intptr_t)context; + int w600Pin = HAL_GetGPIOPin(obkPinNum); + tls_clr_gpio_irq_status(w600Pin); + if (g_handlers[obkPinNum]) { + g_handlers[obkPinNum](obkPinNum); + } +} + +void HAL_AttachInterrupt(int pinIndex, OBKInterruptType mode, OBKInterruptHandler function) { + g_handlers[pinIndex] = function; + int w600Pin = HAL_GetGPIOPin(pinIndex); + tls_gpio_isr_register(w600Pin, W600_Interrupt, (void*)(intptr_t)pinIndex); + int w_mode; + if (mode == INTERRUPT_RISING) { + w_mode = WM_GPIO_IRQ_TRIG_RISING_EDGE; + } + else { + w_mode = WM_GPIO_IRQ_TRIG_FALLING_EDGE; + } + tls_gpio_irq_enable(w600Pin, w_mode); +} +void HAL_DetachInterrupt(int pinIndex) { + if (g_handlers[pinIndex] == 0) { + return; // already removed; + } + int w600Pin = HAL_GetGPIOPin(pinIndex); + tls_gpio_irq_disable(w600Pin); + g_handlers[pinIndex] = 0; +} + #endif diff --git a/src/hal/win32/hal_pins_win32.c b/src/hal/win32/hal_pins_win32.c index e789d73ea..fb2d7f4e6 100644 --- a/src/hal/win32/hal_pins_win32.c +++ b/src/hal/win32/hal_pins_win32.c @@ -226,5 +226,13 @@ unsigned int HAL_GetGPIOPin(int index) { return index; } +void HAL_AttachInterrupt(int pinIndex, OBKInterruptType mode, OBKInterruptHandler function) { + +} +void HAL_DetachInterrupt(int pinIndex) { + +} + + #endif diff --git a/src/hal/xradio/hal_pins_xradio.c b/src/hal/xradio/hal_pins_xradio.c index 3639dea54..5114d0c3a 100644 --- a/src/hal/xradio/hal_pins_xradio.c +++ b/src/hal/xradio/hal_pins_xradio.c @@ -2,6 +2,7 @@ #include "../../new_common.h" #include "../../logging/logging.h" +#include "../hal_pins.h" #include "hal_pinmap_xradio.h" @@ -123,4 +124,45 @@ void HAL_PIN_PWM_Update(int index, float value) HAL_PWM_ChSetDutyRatio(g_pins[index].pwm, (g_pins[index].max_duty / 100) * value); } +OBKInterruptHandler g_handlers[PLATFORM_GPIO_MAX]; +OBKInterruptType g_modes[PLATFORM_GPIO_MAX]; + +void XRadio_Interrupt(void* context) { + int obkPinNum = (int)context; + if (g_handlers[obkPinNum]) { + g_handlers[obkPinNum](obkPinNum); + } +} + +void HAL_AttachInterrupt(int pinIndex, OBKInterruptType mode, OBKInterruptHandler function) { + g_handlers[pinIndex] = function; + + xrpin_t *xr_cf = g_pins + pinIndex; + HAL_XR_ConfigurePin(xr_cf->port, xr_cf->pin, GPIOx_Pn_F6_EINT, GPIO_PULL_UP); + GPIO_IrqParam cfparam; + int xr_mode; + if (mode == INTERRUPT_RISING) { + xr_mode = GPIO_IRQ_EVT_RISING_EDGE; + } + else { + xr_mode = GPIO_IRQ_EVT_FALLING_EDGE; + } + cfparam.event = xr_mode; + cfparam.callback = XRadio_Interrupt; + cfparam.arg = (void*)pinIndex; + HAL_GPIO_EnableIRQ(xr_cf->port, xr_cf->pin, &cfparam); + +} +void HAL_DetachInterrupt(int pinIndex) { + if (g_handlers[pinIndex] == 0) { + return; // already removed; + } + xrpin_t* xr_cf; + xr_cf = g_pins + pinIndex; + HAL_GPIO_DeInit(xr_cf->port, xr_cf->pin); + HAL_GPIO_DisableIRQ(xr_cf->port, xr_cf->pin); + g_handlers[pinIndex] = 0; +} + + #endif