stm32/extint: Add extint_set() function for internal C access to EXTI.
@@ -297,6 +297,53 @@ void extint_register_pin(const pin_obj_t *pin, uint32_t mode, bool hard_irq, mp_
}
}
+void extint_set(const pin_obj_t *pin, uint32_t mode) {
+ uint32_t line = pin->pin;
+
+ mp_obj_t *cb = &MP_STATE_PORT(pyb_extint_callback)[line];
+
+ extint_disable(line);
+
+ *cb = MP_OBJ_SENTINEL;
+
+ pyb_extint_mode[line] = (mode & 0x00010000) ? // GPIO_MODE_IT == 0x00010000
+ EXTI_Mode_Interrupt : EXTI_Mode_Event;
+
+ {
+ // Configure and enable the callback
+
+ pyb_extint_hard_irq[line] = 1;
+ pyb_extint_callback_arg[line] = MP_OBJ_FROM_PTR(pin);
+
+ // Route the GPIO to EXTI
+ __HAL_RCC_SYSCFG_CLK_ENABLE();
+ SYSCFG->EXTICR[line >> 2] =
+ (SYSCFG->EXTICR[line >> 2] & ~(0x0f << (4 * (line & 0x03))))
+ | ((uint32_t)(GPIO_GET_INDEX(pin->gpio)) << (4 * (line & 0x03)));
+
+ // Enable or disable the rising detector
+ if ((mode & GPIO_MODE_IT_RISING) == GPIO_MODE_IT_RISING) {
+ EXTI_RTSR |= 1 << line;
+ } else {
+ EXTI_RTSR &= ~(1 << line);
+ }
+
+ // Enable or disable the falling detector
+ if ((mode & GPIO_MODE_IT_FALLING) == GPIO_MODE_IT_FALLING) {
+ EXTI_FTSR |= 1 << line;
+ } else {
+ EXTI_FTSR &= ~(1 << line);
+ }
+
+ // Configure the NVIC
+ NVIC_SetPriority(IRQn_NONNEG(nvic_irq_channel[line]), IRQ_PRI_EXTINT);
+ HAL_NVIC_EnableIRQ(nvic_irq_channel[line]);
+
+ // Enable the interrupt
+ extint_enable(line);
+ }
+}
+
void extint_enable(uint line) {
if (line >= EXTI_NUM_VECTORS) {
return;
@@ -552,6 +599,9 @@ const mp_obj_type_t extint_type = {
void extint_init0(void) {
for (int i = 0; i < PYB_EXTI_NUM_VECTORS; i++) {
+ if (MP_STATE_PORT(pyb_extint_callback)[i] == MP_OBJ_SENTINEL) {
+ continue;
+ }
MP_STATE_PORT(pyb_extint_callback)[i] = mp_const_none;
pyb_extint_mode[i] = EXTI_Mode_Interrupt;
}
