extern "C" AJ_Status AJS_TargetIO_PinEnableTrigger(void* pinCtx, AJS_IO_PinTriggerMode trigger, int32_t* trigId, uint8_t debounce) { GPIO* gpio = (GPIO*)pinCtx; size_t pin; uint16_t pinId; uint16_t physicalPin = AJS_TargetIO_GetInfo(gpio->pinIdx)->physicalPin; for (pin = 0; pin < ArraySize(pinInfo); ++pin) { if (pinInfo[pin].pinNum == physicalPin) { pinId = pinInfo[pin].pinId; break; } } if (!pinInfo[pin].func) { return AJ_ERR_INVALID; } gpio->trigId = AllocTrigId(pin); if (gpio->trigId == AJS_IO_PIN_NO_TRIGGER) { return AJ_ERR_RESOURCES; } gpio->interrupt = new InterruptIn((PinName)pinId); if (trigger == AJS_IO_PIN_TRIGGER_ON_RISE) { gpio->interrupt->rise(pinInfo[pin].func); } else if (trigger == AJS_IO_PIN_TRIGGER_ON_FALL) { gpio->interrupt->fall(pinInfo[pin].func); } *trigId = gpio->trigId; return AJ_OK; }
AJ_Status AJS_TargetIO_PinEnableTrigger(void* pinCtx, AJS_IO_PinTriggerMode trigger, int32_t* trigId, uint8_t debounce) { GPIO* gpio = (GPIO*)pinCtx; uint8_t trig; uint8_t exti_gpio; uint8_t exti_pin; uint8_t exti_irqn; uint8_t enable = ENABLE; EXTI_InitTypeDef interrupt; NVIC_InitTypeDef nvic; GPIO_InitTypeDef gpio_int; if ((trigger != AJS_IO_PIN_TRIGGER_ON_RISE) && (trigger != AJS_IO_PIN_TRIGGER_ON_FALL)) { /* * Disable interrupts on this pin */ enable = DISABLE; return AJ_OK; } switch (gpio->gpioPin) { case (GPIO_Pin_0): exti_pin = EXTI_PinSource0; exti_irqn = EXTI0_IRQn; break; case (GPIO_Pin_1): /* * EXTI1 is already used for WSL so return AJ_ERR_RESOURCES */ //exti_pin = EXTI_PinSource1; //exti_irqn = EXTI1_IRQn; return AJ_ERR_RESOURCES; break; case (GPIO_Pin_2): exti_pin = EXTI_PinSource2; exti_irqn = EXTI2_IRQn; break; case (GPIO_Pin_3): exti_pin = EXTI_PinSource3; exti_irqn = EXTI3_IRQn; break; case (GPIO_Pin_4): exti_pin = EXTI_PinSource4; exti_irqn = EXTI4_IRQn; break; case (GPIO_Pin_5): exti_pin = EXTI_PinSource5; exti_irqn = EXTI9_5_IRQn; break; case (GPIO_Pin_6): exti_pin = EXTI_PinSource6; exti_irqn = EXTI9_5_IRQn; break; case (GPIO_Pin_7): exti_pin = EXTI_PinSource7; exti_irqn = EXTI9_5_IRQn; break; case (GPIO_Pin_8): exti_pin = EXTI_PinSource8; exti_irqn = EXTI9_5_IRQn; break; case (GPIO_Pin_9): exti_pin = EXTI_PinSource9; exti_irqn = EXTI9_5_IRQn; break; case (GPIO_Pin_10): exti_pin = EXTI_PinSource10; exti_irqn = EXTI15_10_IRQn; break; case (GPIO_Pin_11): exti_pin = EXTI_PinSource11; exti_irqn = EXTI15_10_IRQn; break; case (GPIO_Pin_12): exti_pin = EXTI_PinSource12; exti_irqn = EXTI15_10_IRQn; break; case (GPIO_Pin_13): exti_pin = EXTI_PinSource13; exti_irqn = EXTI15_10_IRQn; break; case (GPIO_Pin_14): exti_pin = EXTI_PinSource14; exti_irqn = EXTI15_10_IRQn; break; case (GPIO_Pin_15): exti_pin = EXTI_PinSource15; exti_irqn = EXTI15_10_IRQn; break; } switch ((uint32_t)gpio->GPIOx) { case ((uint32_t)GPIOA): exti_gpio = EXTI_PortSourceGPIOA; break; case ((uint32_t)GPIOB): exti_gpio = EXTI_PortSourceGPIOB; break; case ((uint32_t)GPIOC): exti_gpio = EXTI_PortSourceGPIOC; break; case ((uint32_t)GPIOD): exti_gpio = EXTI_PortSourceGPIOD; break; case ((uint32_t)GPIOE): exti_gpio = EXTI_PortSourceGPIOE; break; case ((uint32_t)GPIOF): exti_gpio = EXTI_PortSourceGPIOF; break; case ((uint32_t)GPIOG): exti_gpio = EXTI_PortSourceGPIOG; break; case ((uint32_t)GPIOH): exti_gpio = EXTI_PortSourceGPIOH; break; case ((uint32_t)GPIOI): exti_gpio = EXTI_PortSourceGPIOI; break; } gpio->trigId = AllocTrigId(exti_pin); if (gpio->trigId == AJS_IO_PIN_NO_TRIGGER) { return AJ_ERR_RESOURCES; } gpio_int.GPIO_Pin = gpio->gpioPin; gpio_int.GPIO_OType = GPIO_OType_PP; gpio_int.GPIO_Mode = GPIO_Mode_IN; gpio_int.GPIO_PuPd = GPIO_PuPd_NOPULL; gpio_int.GPIO_Speed = GPIO_Speed_2MHz; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); SYSCFG_EXTILineConfig(exti_gpio, exti_pin); if (enable == ENABLE) { GPIO_Init(gpio->GPIOx, &gpio_int); } else { GPIO_DeInit(gpio->GPIOx); } trig = (trigger == AJS_IO_PIN_TRIGGER_ON_RISE) ? EXTI_Trigger_Rising : EXTI_Trigger_Falling; interrupt.EXTI_Trigger = trig; interrupt.EXTI_Mode = EXTI_Mode_Interrupt; interrupt.EXTI_LineCmd = enable; if (gpio->gpioPin <= GPIO_Pin_15) { /* * The gpio pin defines match the EXTI line defines so there is not need for a * large switch statement finding the correct match */ interrupt.EXTI_Line = gpio->gpioPin; } EXTI_Init(&interrupt); nvic.NVIC_IRQChannel = exti_irqn; nvic.NVIC_IRQChannelCmd = enable; nvic.NVIC_IRQChannelPreemptionPriority = 7; nvic.NVIC_IRQChannelSubPriority = 7; NVIC_Init(&nvic); *trigId = gpio->trigId; return AJ_OK; }