From 2dba691ca66c64d77e677852789ae89f71cbeeab Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Sun, 25 Jun 2017 19:05:56 +0200 Subject: [PATCH] stm32-l053r8-em9304: enable interrupt on raising edge of SPI1_RDY, use IRQ instead of polling --- .../cubemx-l053r8-em9304/Inc/main.h | 2 ++ .../cubemx-l053r8-em9304/Inc/stm32l0xx_it.h | 1 + .../cubemx-l053r8-em9304/Makefile | 2 +- .../cubemx-l053r8-em9304/Src/main.c | 6 +++- .../cubemx-l053r8-em9304/Src/stm32l0xx_it.c | 15 ++++++++++ .../cubemx-l053r8-em9304.ioc | 5 +++- port/stm32-l053r8-em9304/port.c | 30 ++++++++++++++++--- 7 files changed, 54 insertions(+), 7 deletions(-) diff --git a/port/stm32-l053r8-em9304/cubemx-l053r8-em9304/Inc/main.h b/port/stm32-l053r8-em9304/cubemx-l053r8-em9304/Inc/main.h index 29b0e9196..901fe6949 100644 --- a/port/stm32-l053r8-em9304/cubemx-l053r8-em9304/Inc/main.h +++ b/port/stm32-l053r8-em9304/cubemx-l053r8-em9304/Inc/main.h @@ -48,6 +48,7 @@ #define B1_Pin GPIO_PIN_13 #define B1_GPIO_Port GPIOC +#define B1_EXTI_IRQn EXTI4_15_IRQn #define DEBUG_0_Pin GPIO_PIN_0 #define DEBUG_0_GPIO_Port GPIOC #define USART_TX_Pin GPIO_PIN_2 @@ -58,6 +59,7 @@ #define EN_GPIO_Port GPIOB #define SPI1_RDY_Pin GPIO_PIN_9 #define SPI1_RDY_GPIO_Port GPIOA +#define SPI1_RDY_EXTI_IRQn EXTI4_15_IRQn #define TMS_Pin GPIO_PIN_13 #define TMS_GPIO_Port GPIOA #define TCK_Pin GPIO_PIN_14 diff --git a/port/stm32-l053r8-em9304/cubemx-l053r8-em9304/Inc/stm32l0xx_it.h b/port/stm32-l053r8-em9304/cubemx-l053r8-em9304/Inc/stm32l0xx_it.h index 49de4beb0..dc2683966 100644 --- a/port/stm32-l053r8-em9304/cubemx-l053r8-em9304/Inc/stm32l0xx_it.h +++ b/port/stm32-l053r8-em9304/cubemx-l053r8-em9304/Inc/stm32l0xx_it.h @@ -46,6 +46,7 @@ /* Exported functions ------------------------------------------------------- */ void SysTick_Handler(void); +void EXTI4_15_IRQHandler(void); void DMA1_Channel2_3_IRQHandler(void); void SPI1_IRQHandler(void); diff --git a/port/stm32-l053r8-em9304/cubemx-l053r8-em9304/Makefile b/port/stm32-l053r8-em9304/cubemx-l053r8-em9304/Makefile index 0304834c8..a40f0d4cf 100644 --- a/port/stm32-l053r8-em9304/cubemx-l053r8-em9304/Makefile +++ b/port/stm32-l053r8-em9304/cubemx-l053r8-em9304/Makefile @@ -1,5 +1,5 @@ ########################################################################################################################## -# File automatically-generated by tool: [projectgenerator] version: [2.23.0] date: [Sun Jun 25 10:44:49 CEST 2017] +# File automatically-generated by tool: [projectgenerator] version: [2.23.0] date: [Sun Jun 25 18:34:23 CEST 2017] ########################################################################################################################## # ------------------------------------------------ diff --git a/port/stm32-l053r8-em9304/cubemx-l053r8-em9304/Src/main.c b/port/stm32-l053r8-em9304/cubemx-l053r8-em9304/Src/main.c index 00ddc332c..0bdcb5406 100644 --- a/port/stm32-l053r8-em9304/cubemx-l053r8-em9304/Src/main.c +++ b/port/stm32-l053r8-em9304/cubemx-l053r8-em9304/Src/main.c @@ -283,10 +283,14 @@ static void MX_GPIO_Init(void) /*Configure GPIO pin : SPI1_RDY_Pin */ GPIO_InitStruct.Pin = SPI1_RDY_Pin; - GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(SPI1_RDY_GPIO_Port, &GPIO_InitStruct); + /* EXTI interrupt init*/ + HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(EXTI4_15_IRQn); + } /* USER CODE BEGIN 4 */ diff --git a/port/stm32-l053r8-em9304/cubemx-l053r8-em9304/Src/stm32l0xx_it.c b/port/stm32-l053r8-em9304/cubemx-l053r8-em9304/Src/stm32l0xx_it.c index bc2e54ae3..f5d83c849 100644 --- a/port/stm32-l053r8-em9304/cubemx-l053r8-em9304/Src/stm32l0xx_it.c +++ b/port/stm32-l053r8-em9304/cubemx-l053r8-em9304/Src/stm32l0xx_it.c @@ -70,6 +70,21 @@ void SysTick_Handler(void) /* please refer to the startup file (startup_stm32l0xx.s). */ /******************************************************************************/ +/** +* @brief This function handles EXTI line 4 to 15 interrupts. +*/ +void EXTI4_15_IRQHandler(void) +{ + /* USER CODE BEGIN EXTI4_15_IRQn 0 */ + + /* USER CODE END EXTI4_15_IRQn 0 */ + HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_9); + HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_13); + /* USER CODE BEGIN EXTI4_15_IRQn 1 */ + + /* USER CODE END EXTI4_15_IRQn 1 */ +} + /** * @brief This function handles DMA1 channel 2 and channel 3 interrupts. */ diff --git a/port/stm32-l053r8-em9304/cubemx-l053r8-em9304/cubemx-l053r8-em9304.ioc b/port/stm32-l053r8-em9304/cubemx-l053r8-em9304/cubemx-l053r8-em9304.ioc index 3491e02f7..16f8a54a2 100644 --- a/port/stm32-l053r8-em9304/cubemx-l053r8-em9304/cubemx-l053r8-em9304.ioc +++ b/port/stm32-l053r8-em9304/cubemx-l053r8-em9304/cubemx-l053r8-em9304.ioc @@ -55,6 +55,7 @@ Mcu.UserName=STM32L053R8Tx MxCube.Version=4.21.0 MxDb.Version=DB.4.0.210 NVIC.DMA1_Channel2_3_IRQn=true\:0\:0\:false\:false\:true +NVIC.EXTI4_15_IRQn=true\:0\:0\:false\:false\:true NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:false NVIC.NonMaskableInt_IRQn=true\:0\:0\:false\:false\:false NVIC.PendSV_IRQn=true\:0\:0\:false\:false\:false @@ -90,7 +91,7 @@ PA7.Signal=SPI1_MOSI PA9.GPIOParameters=GPIO_Label PA9.GPIO_Label=SPI1_RDY PA9.Locked=true -PA9.Signal=GPIO_Input +PA9.Signal=GPXTI9 PB10.GPIOParameters=GPIO_Label PB10.GPIO_Label=EN PB10.Locked=true @@ -187,6 +188,8 @@ RCC.VCOOutputFreq_Value=48000000 RCC.WatchDogFreq_Value=37000 SH.GPXTI13.0=GPIO_EXTI13 SH.GPXTI13.ConfNb=1 +SH.GPXTI9.0=GPIO_EXTI9 +SH.GPXTI9.ConfNb=1 SPI1.CalculateBaudRate=2.097 MBits/s SPI1.Direction=SPI_DIRECTION_2LINES SPI1.IPParameters=VirtualType,Mode,Direction,CalculateBaudRate diff --git a/port/stm32-l053r8-em9304/port.c b/port/stm32-l053r8-em9304/port.c index eb0e8f6fd..15269864c 100644 --- a/port/stm32-l053r8-em9304/port.c +++ b/port/stm32-l053r8-em9304/port.c @@ -96,6 +96,11 @@ static uint16_t hal_spi_em9304_tx_size; static int test_done; static int event_received; static int command_sent; +static volatile int run_loop_triggered; + +static inline void hal_spi_em9304_trigger_run_loop(void){ + run_loop_triggered = 1; +} static inline int hal_spi_em9304_rdy(void){ return HAL_GPIO_ReadPin(SPI1_RDY_GPIO_Port, SPI1_RDY_Pin) == GPIO_PIN_SET; @@ -113,11 +118,11 @@ void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi){ switch (hal_spi_em9304_state){ case SPI_EM9304_RX_W4_READ_COMMAND_SENT: hal_spi_em9304_state = SPI_EM9304_RX_READ_COMMAND_SENT; - // trigger run loop; + hal_spi_em9304_trigger_run_loop(); break; case SPI_EM9304_TX_W4_WRITE_COMMAND_SENT: hal_spi_em9304_state = SPI_EM9304_TX_WRITE_COMMAND_SENT; - // trigger run loop; + hal_spi_em9304_trigger_run_loop(); break; default: break; @@ -128,7 +133,7 @@ void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi){ switch (hal_spi_em9304_state){ case SPI_EM9304_RX_W4_DATA_RECEIVED: hal_spi_em9304_state = SPI_EM9304_RX_DATA_RECEIVED; - // trigger run loop; + hal_spi_em9304_trigger_run_loop(); break; default: break; @@ -139,13 +144,19 @@ void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi){ switch (hal_spi_em9304_state){ case SPI_EM9304_TX_W4_DATA_SENT: hal_spi_em9304_state = SPI_EM9304_TX_DATA_SENT; - // trigger run loop; + hal_spi_em9304_trigger_run_loop(); break; default: break; } } +void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){ + if (hal_spi_em9304_rdy()){ + hal_spi_em9304_trigger_run_loop(); + } +} + static void hal_spi_em9304_process(void){ uint16_t bytes_to_read; uint16_t bytes_ready; @@ -265,7 +276,17 @@ void port_main(void){ hal_spi_em9304_reset(); int step = 0; while (!test_done){ + + // wait for event / sleep + HAL_GPIO_WritePin(DEBUG_0_GPIO_Port, DEBUG_0_Pin, GPIO_PIN_SET); + while (!run_loop_triggered){}; + HAL_GPIO_WritePin(DEBUG_0_GPIO_Port, DEBUG_0_Pin, GPIO_PIN_RESET); + run_loop_triggered = 0; + + // handle event hal_spi_em9304_process(); + + // simulate stack switch (step){ case 0: if (!event_received) continue; @@ -275,6 +296,7 @@ void port_main(void){ // hal_spi_em9304_tx_data = hci_reset; hal_spi_em9304_tx_size = sizeof(hci_reset); + hal_spi_em9304_trigger_run_loop(); step++; break; case 1: