From 83d872ff42c3d4988ffed7295199b22a453c9362 Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Tue, 5 Jun 2018 22:56:14 +0200 Subject: [PATCH 1/8] [HWSerial] Review Serial definition Review Serial menu, 3 options: - Enabled with generic Serial: default, define HAL_UART_MODULE_ENABLED and instanciate Serial using SERIAL_UART_INSTANCE and PIN_SERIAL_RX/PIN_SERIAL_Tx - Enabled without generic Serial: define HAL_UART_MODULE_ENABLED - Disabled (No Serial): HAL_UART_MODULE_ENABLED not defined Allow to build without HAL_UART_MODULE_ENABLED (save memory) Now, HAL_UART_MODULE_ENABLED should not be defined in stm32yyxx_hal_conf.h Signed-off-by: Frederic Pillon --- boards.txt | 82 ++++++++----------- cores/arduino/HardwareSerial.cpp | 6 +- cores/arduino/stm32/low_power.c | 8 +- cores/arduino/stm32/uart.c | 2 + cores/arduino/stm32/uart.h | 27 ++---- platform.txt | 2 +- variants/BLUEPILL_F103C8/stm32f1xx_hal_conf.h | 2 +- variants/DISCO_F030R8/stm32f0xx_hal_conf.h | 2 +- variants/DISCO_F100RB/stm32f1xx_hal_conf.h | 2 +- variants/DISCO_F407VG/stm32f4xx_hal_conf.h | 2 +- variants/DISCO_F746NG/stm32f7xx_hal_conf.h | 2 +- .../DISCO_L072CZ_LRWAN1/stm32l0xx_hal_conf.h | 4 +- .../DISCO_L475VG_IOT/stm32l4xx_hal_conf.h | 2 +- .../MAPLEMINI_F103CB/stm32f1xx_hal_conf.h | 2 +- variants/NUCLEO_F030R8/stm32f0xx_hal_conf.h | 2 +- variants/NUCLEO_F091RC/stm32f0xx_hal_conf.h | 2 +- variants/NUCLEO_F103RB/stm32f1xx_hal_conf.h | 2 +- variants/NUCLEO_F207ZG/stm32f2xx_hal_conf.h | 4 +- variants/NUCLEO_F302R8/stm32f3xx_hal_conf.h | 4 +- variants/NUCLEO_F303K8/stm32f3xx_hal_conf.h | 4 +- variants/NUCLEO_F303RE/stm32f3xx_hal_conf.h | 2 +- variants/NUCLEO_F401RE/stm32f4xx_hal_conf.h | 4 +- variants/NUCLEO_F411RE/stm32f4xx_hal_conf.h | 4 +- variants/NUCLEO_F429ZI/stm32f4xx_hal_conf.h | 2 +- variants/NUCLEO_F446RE/stm32f4xx_hal_conf.h | 4 +- variants/NUCLEO_L031K6/stm32l0xx_hal_conf.h | 2 +- variants/NUCLEO_L053R8/stm32l0xx_hal_conf.h | 2 +- variants/NUCLEO_L152RE/stm32l1xx_hal_conf.h | 4 +- variants/NUCLEO_L432KC/stm32l4xx_hal_conf.h | 4 +- variants/NUCLEO_L476RG/stm32l4xx_hal_conf.h | 4 +- variants/board_template/stm32yyxx_hal_conf.h | 1 + 31 files changed, 88 insertions(+), 108 deletions(-) diff --git a/boards.txt b/boards.txt index 7e42c2efd2..7d5857a935 100644 --- a/boards.txt +++ b/boards.txt @@ -472,53 +472,41 @@ Maple.menu.upload_method.serialMethod.upload.tool=serial_upload ################################################################################ # Serialx activation -Nucleo_144.menu.xserial.generic=Generic Serial -Nucleo_144.menu.xserial.none=No Serial -Nucleo_144.menu.xserial.none.build.xSerial=-DNO_HWSERIAL -Nucleo_144.menu.xserial.all=All (up to 10) -Nucleo_144.menu.xserial.all.build.xSerial=-DALL_HWSERIAL -Nucleo_144.menu.xserial.third=Enable first third (USART1 to 3 if available) -Nucleo_144.menu.xserial.third.build.xSerial=-DFIRST_THIRD_HWSERIAL - -Nucleo_64.menu.xserial.generic=Generic Serial -Nucleo_64.menu.xserial.none=No Serial -Nucleo_64.menu.xserial.none.build.xSerial=-DNO_HWSERIAL -Nucleo_64.menu.xserial.all=All (up to 10) -Nucleo_64.menu.xserial.all.build.xSerial=-DALL_HWSERIAL -Nucleo_64.menu.xserial.third=Enable first third (USART1 to 3 if available) -Nucleo_64.menu.xserial.third.build.xSerial=-DFIRST_THIRD_HWSERIAL - -Nucleo_32.menu.xserial.generic=Generic Serial -Nucleo_32.menu.xserial.none=No Serial -Nucleo_32.menu.xserial.none.build.xSerial=-DNO_HWSERIAL -Nucleo_32.menu.xserial.all=All (up to 10) -Nucleo_32.menu.xserial.all.build.xSerial=-DALL_HWSERIAL -Nucleo_32.menu.xserial.third=Enable first third (USART1 to 3 if available) -Nucleo_32.menu.xserial.third.build.xSerial=-DFIRST_THIRD_HWSERIAL - -Disco.menu.xserial.generic=Generic Serial -Disco.menu.xserial.none=No Serial -Disco.menu.xserial.none.build.xSerial=-DNO_HWSERIAL -Disco.menu.xserial.all=All (up to 10) -Disco.menu.xserial.all.build.xSerial=-DALL_HWSERIAL -Disco.menu.xserial.third=Enable first third (USART1 to 3 if available) -Disco.menu.xserial.third.build.xSerial=-DFIRST_THIRD_HWSERIAL - -GenF103.menu.xserial.generic=Generic Serial -GenF103.menu.xserial.none=No Serial -GenF103.menu.xserial.none.build.xSerial=-DNO_HWSERIAL -GenF103.menu.xserial.all=All (up to 10) -GenF103.menu.xserial.all.build.xSerial=-DALL_HWSERIAL -GenF103.menu.xserial.third=Enable first third (USART1 to 3 if available) -GenF103.menu.xserial.third.build.xSerial=-DFIRST_THIRD_HWSERIAL - -Maple.menu.xserial.generic=Generic Serial -Maple.menu.xserial.none=No Serial -Maple.menu.xserial.none.build.xSerial=-DNO_HWSERIAL -Maple.menu.xserial.all=All (up to 10) -Maple.menu.xserial.all.build.xSerial=-DALL_HWSERIAL -Maple.menu.xserial.third=Enable first third (USART1 to 3 if available) -Maple.menu.xserial.third.build.xSerial=-DFIRST_THIRD_HWSERIAL +Nucleo_144.menu.xserial.generic=Enabled with generic Serial +Nucleo_144.menu.xserial.none=Enabled without generic Serial +Nucleo_144.menu.xserial.none.build.xSerial=-DHAL_UART_MODULE_ENABLED -DHWSERIAL_NONE +Nucleo_144.menu.xserial.disabled=Disabled (No Serial) +Nucleo_144.menu.xserial.disabled.build.xSerial= + +Nucleo_64.menu.xserial.generic=Enabled with generic Serial +Nucleo_64.menu.xserial.none=Enabled without generic Serial +Nucleo_64.menu.xserial.none.build.xSerial=-DHAL_UART_MODULE_ENABLED -DHWSERIAL_NONE +Nucleo_64.menu.xserial.disabled=Disabled (No Serial) +Nucleo_64.menu.xserial.disabled.build.xSerial= + +Nucleo_32.menu.xserial.generic=Enabled with generic Serial +Nucleo_32.menu.xserial.none=Enabled without generic Serial +Nucleo_32.menu.xserial.none.build.xSerial=-DHAL_UART_MODULE_ENABLED -DHWSERIAL_NONE +Nucleo_32.menu.xserial.disabled=Disabled (No Serial) +Nucleo_32.menu.xserial.disabled.build.xSerial= + +Disco.menu.xserial.generic=Enabled with generic Serial +Disco.menu.xserial.none=Enabled without generic Serial +Disco.menu.xserial.none.build.xSerial=-DHAL_UART_MODULE_ENABLED -DHWSERIAL_NONE +Disco.menu.xserial.disabled=Disabled (No Serial) +Disco.menu.xserial.disabled.build.xSerial= + +GenF103.menu.xserial.generic=Enabled with generic Serial +GenF103.menu.xserial.none=Enabled without generic Serial +GenF103.menu.xserial.none.build.xSerial=-DHAL_UART_MODULE_ENABLED -DHWSERIAL_NONE +GenF103.menu.xserial.disabled=Disabled (No Serial) +GenF103.menu.xserial.disabled.build.xSerial= + +Maple.menu.xserial.generic=Enabled with generic Serial +Maple.menu.xserial.none=Enabled without generic Serial +Maple.menu.xserial.none.build.xSerial=-DHAL_UART_MODULE_ENABLED -DHWSERIAL_NONE +Maple.menu.xserial.disabled=Disabled (No Serial) +Maple.menu.xserial.disabled.build.xSerial= # USB connectivity Nucleo_144.menu.usb.none=None diff --git a/cores/arduino/HardwareSerial.cpp b/cores/arduino/HardwareSerial.cpp index 6177980390..055bcbd9f1 100644 --- a/cores/arduino/HardwareSerial.cpp +++ b/cores/arduino/HardwareSerial.cpp @@ -30,7 +30,7 @@ #include "Arduino.h" #include "HardwareSerial.h" -#if !defined(NO_HWSERIAL) +#if defined(HAL_UART_MODULE_ENABLED) #if defined(HAVE_HWSERIAL1) || defined(HAVE_HWSERIAL2) || defined(HAVE_HWSERIAL3) ||\ defined(HAVE_HWSERIAL4) || defined(HAVE_HWSERIAL5) || defined(HAVE_HWSERIAL6) ||\ defined(HAVE_HWSERIAL7) || defined(HAVE_HWSERIAL8) || defined(HAVE_HWSERIAL8) ||\ @@ -102,6 +102,7 @@ HardwareSerial Serial10(UART10); void serialEvent10() __attribute__((weak)); #endif +#endif // HAVE_HWSERIALx void serialEventRun(void) { @@ -391,5 +392,4 @@ void HardwareSerial::setRx(PinName _rx) { void HardwareSerial::setTx(PinName _tx){ _serial.pin_tx = _tx; } -#endif // HAVE_HWSERIALx -#endif // !NO_HWSERIAL +#endif // HAL_UART_MODULE_ENABLED diff --git a/cores/arduino/stm32/low_power.c b/cores/arduino/stm32/low_power.c index 7f09544ab9..b0c6d95e04 100644 --- a/cores/arduino/stm32/low_power.c +++ b/cores/arduino/stm32/low_power.c @@ -45,7 +45,7 @@ extern "C" { #endif -#ifdef UART_IT_WUF +#if defined(UART_IT_WUF) && defined(HAL_UART_MODULE_ENABLED) /* Save UART handler for callback */ static UART_HandleTypeDef* WakeUpUart = NULL; #endif @@ -201,7 +201,7 @@ void LowPower_sleep(uint32_t regulator){ void LowPower_stop(serial_t *obj){ __disable_irq(); -#ifdef UART_IT_WUF +#if defined(UART_IT_WUF) && defined(HAL_UART_MODULE_ENABLED) if (WakeUpUart != NULL) { HAL_UARTEx_EnableStopMode(WakeUpUart); } @@ -224,7 +224,7 @@ void LowPower_stop(serial_t *obj){ /* Exit Stop mode reset clocks */ SystemClock_Config(); -#ifdef UART_IT_WUF +#if defined(UART_IT_WUF) && defined(HAL_UART_MODULE_ENABLED) if (WakeUpUart != NULL) { /* In case of WakeUp from UART, reset its clock source to HSI */ uart_config_lowpower(obj); @@ -290,7 +290,7 @@ void LowPower_shutdown(){ * @retval None */ void LowPower_EnableWakeUpUart(serial_t* serial, void (*FuncPtr)( void ) ) { -#ifdef UART_IT_WUF +#if defined(UART_IT_WUF) && defined(HAL_UART_MODULE_ENABLED) UART_WakeUpTypeDef WakeUpSelection; if(serial == NULL) { return; diff --git a/cores/arduino/stm32/uart.c b/cores/arduino/stm32/uart.c index dc054c758b..4920fff7aa 100644 --- a/cores/arduino/stm32/uart.c +++ b/cores/arduino/stm32/uart.c @@ -53,6 +53,7 @@ #ifdef __cplusplus extern "C" { #endif +#if defined(HAL_UART_MODULE_ENABLED) // if DEBUG_UART is not defined assume this is the one // linked to PIN_SERIAL_TX @@ -946,6 +947,7 @@ void HAL_UARTEx_WakeupCallback(UART_HandleTypeDef *huart) HAL_UART_Receive_IT(huart, &(obj->recv), 1); } +#endif /* HAL_UART_MODULE_ENABLED */ #ifdef __cplusplus } diff --git a/cores/arduino/stm32/uart.h b/cores/arduino/stm32/uart.h index 97707c80fe..a3a555ffdc 100644 --- a/cores/arduino/stm32/uart.h +++ b/cores/arduino/stm32/uart.h @@ -47,22 +47,11 @@ extern "C" { #endif -#if !defined(NO_HWSERIAL) -#if defined(ALL_HWSERIAL) || defined(FIRST_THIRD_HWSERIAL) -#define ENABLE_HWSERIAL1 -#define ENABLE_HWSERIAL2 -#define ENABLE_HWSERIAL3 -#if !defined(FIRST_THIRD_HWSERIAL) -#define ENABLE_HWSERIAL4 -#define ENABLE_HWSERIAL5 -#define ENABLE_HWSERIAL6 -#define ENABLE_HWSERIAL7 -#define ENABLE_HWSERIAL8 -#define ENABLE_HWSERIAL9 -#define ENABLE_HWSERIAL10 -#endif // FIRST_THIRD_HWSERIAL -#endif // ALL_HWSERIAL || FIRST_THIRD_HWSERIAL -#ifdef SERIAL_UART_INSTANCE +#if !defined(HAL_UART_MODULE_ENABLED) +#define serial_t void* +#else +#if !defined(HWSERIAL_NONE) && defined(SERIAL_UART_INSTANCE) + #if SERIAL_UART_INSTANCE == 1 #define ENABLE_HWSERIAL1 #if !defined(Serial) @@ -123,13 +112,12 @@ #define Serial Serial10 #define serialEvent serialEvent10 #endif -#endif // SERIAL_UART_INSTANCE == x #else #if !defined(Serial) #warning "No generic 'Serial' defined!" #endif -#endif // SERIAL_UART_INSTANCE -#endif // NO_HWSERIAL +#endif // SERIAL_UART_INSTANCE == x +#endif // !HWSERIAL_NONE && SERIAL_UART_INSTANCE #if defined(ENABLE_HWSERIAL1) #if defined(USART1_BASE) @@ -292,6 +280,7 @@ uint8_t serial_rx_active(serial_t *obj); size_t uart_debug_write(uint8_t *data, uint32_t size); +#endif /* HAL_UART_MODULE_ENABLED */ #ifdef __cplusplus } #endif diff --git a/platform.txt b/platform.txt index 5425382cae..f44e737cec 100644 --- a/platform.txt +++ b/platform.txt @@ -79,7 +79,7 @@ build.usb_manufacturer="Unknown" build.info.flags=-D{build.series} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} -DBOARD_NAME="{build.board}" # Defaults config -build.xSerial= +build.xSerial=-DHAL_UART_MODULE_ENABLED build.enable_usb= build.flags.optimize= build.flags.ldspecs= diff --git a/variants/BLUEPILL_F103C8/stm32f1xx_hal_conf.h b/variants/BLUEPILL_F103C8/stm32f1xx_hal_conf.h index b466615b2f..50e9693dfa 100644 --- a/variants/BLUEPILL_F103C8/stm32f1xx_hal_conf.h +++ b/variants/BLUEPILL_F103C8/stm32f1xx_hal_conf.h @@ -78,7 +78,7 @@ #define HAL_SPI_MODULE_ENABLED /*#define HAL_SRAM_MODULE_ENABLED*/ #define HAL_TIM_MODULE_ENABLED -#define HAL_UART_MODULE_ENABLED +/*#define HAL_UART_MODULE_ENABLED*/ /*#define HAL_USART_MODULE_ENABLED*/ /*#define HAL_WWDG_MODULE_ENABLED*/ #define HAL_MMC_MODULE_ENABLED diff --git a/variants/DISCO_F030R8/stm32f0xx_hal_conf.h b/variants/DISCO_F030R8/stm32f0xx_hal_conf.h index ec2fa06c6d..96167719ef 100644 --- a/variants/DISCO_F030R8/stm32f0xx_hal_conf.h +++ b/variants/DISCO_F030R8/stm32f0xx_hal_conf.h @@ -72,7 +72,7 @@ #define HAL_SPI_MODULE_ENABLED #define HAL_TIM_MODULE_ENABLED /*#define HAL_TSC_MODULE_ENABLED */ -#define HAL_UART_MODULE_ENABLED +/*#define HAL_UART_MODULE_ENABLED */ /*#define HAL_USART_MODULE_ENABLED */ /*#define HAL_WWDG_MODULE_ENABLED */ diff --git a/variants/DISCO_F100RB/stm32f1xx_hal_conf.h b/variants/DISCO_F100RB/stm32f1xx_hal_conf.h index 36a5689a7f..5ab4ad2780 100644 --- a/variants/DISCO_F100RB/stm32f1xx_hal_conf.h +++ b/variants/DISCO_F100RB/stm32f1xx_hal_conf.h @@ -78,7 +78,7 @@ #define HAL_SPI_MODULE_ENABLED /*#define HAL_SRAM_MODULE_ENABLED*/ #define HAL_TIM_MODULE_ENABLED -#define HAL_UART_MODULE_ENABLED +/* #define HAL_UART_MODULE_ENABLED */ /*#define HAL_USART_MODULE_ENABLED*/ /*#define HAL_WWDG_MODULE_ENABLED*/ #define HAL_MMC_MODULE_ENABLED diff --git a/variants/DISCO_F407VG/stm32f4xx_hal_conf.h b/variants/DISCO_F407VG/stm32f4xx_hal_conf.h index 10a4072b55..39591fd420 100644 --- a/variants/DISCO_F407VG/stm32f4xx_hal_conf.h +++ b/variants/DISCO_F407VG/stm32f4xx_hal_conf.h @@ -81,7 +81,7 @@ /* #define HAL_SD_MODULE_ENABLED */ #define HAL_SPI_MODULE_ENABLED #define HAL_TIM_MODULE_ENABLED -#define HAL_UART_MODULE_ENABLED +/* #define HAL_UART_MODULE_ENABLED */ /* #define HAL_USART_MODULE_ENABLED */ /* #define HAL_IRDA_MODULE_ENABLED */ /* #define HAL_SMARTCARD_MODULE_ENABLED */ diff --git a/variants/DISCO_F746NG/stm32f7xx_hal_conf.h b/variants/DISCO_F746NG/stm32f7xx_hal_conf.h index 8b6a92a455..4240194e4e 100644 --- a/variants/DISCO_F746NG/stm32f7xx_hal_conf.h +++ b/variants/DISCO_F746NG/stm32f7xx_hal_conf.h @@ -84,7 +84,7 @@ /* #define HAL_SPDIFRX_MODULE_ENABLED */ #define HAL_SPI_MODULE_ENABLED #define HAL_TIM_MODULE_ENABLED -#define HAL_UART_MODULE_ENABLED +/* #define HAL_UART_MODULE_ENABLED */ /* #define HAL_USART_MODULE_ENABLED */ /* #define HAL_IRDA_MODULE_ENABLED */ /* #define HAL_SMARTCARD_MODULE_ENABLED */ diff --git a/variants/DISCO_L072CZ_LRWAN1/stm32l0xx_hal_conf.h b/variants/DISCO_L072CZ_LRWAN1/stm32l0xx_hal_conf.h index df2b858ef0..36d3f1bbb4 100644 --- a/variants/DISCO_L072CZ_LRWAN1/stm32l0xx_hal_conf.h +++ b/variants/DISCO_L072CZ_LRWAN1/stm32l0xx_hal_conf.h @@ -63,8 +63,8 @@ #define HAL_SPI_MODULE_ENABLED #define HAL_TIM_MODULE_ENABLED /*#define HAL_TSC_MODULE_ENABLED */ -#define HAL_UART_MODULE_ENABLED -#define HAL_USART_MODULE_ENABLED +/*#define HAL_UART_MODULE_ENABLED */ +/*#define HAL_USART_MODULE_ENABLED */ /*#define HAL_IRDA_MODULE_ENABLED */ /*#define HAL_SMARTCARD_MODULE_ENABLED */ /*#define HAL_SMBUS_MODULE_ENABLED */ diff --git a/variants/DISCO_L475VG_IOT/stm32l4xx_hal_conf.h b/variants/DISCO_L475VG_IOT/stm32l4xx_hal_conf.h index 8d16799cab..9455c7b905 100644 --- a/variants/DISCO_L475VG_IOT/stm32l4xx_hal_conf.h +++ b/variants/DISCO_L475VG_IOT/stm32l4xx_hal_conf.h @@ -95,7 +95,7 @@ /* #define HAL_SWPMI_MODULE_ENABLED */ #define HAL_TIM_MODULE_ENABLED /* #define HAL_TSC_MODULE_ENABLED */ -#define HAL_UART_MODULE_ENABLED +/* #define HAL_UART_MODULE_ENABLED */ /* #define HAL_USART_MODULE_ENABLED */ /* #define HAL_WWDG_MODULE_ENABLED */ diff --git a/variants/MAPLEMINI_F103CB/stm32f1xx_hal_conf.h b/variants/MAPLEMINI_F103CB/stm32f1xx_hal_conf.h index b466615b2f..50e9693dfa 100644 --- a/variants/MAPLEMINI_F103CB/stm32f1xx_hal_conf.h +++ b/variants/MAPLEMINI_F103CB/stm32f1xx_hal_conf.h @@ -78,7 +78,7 @@ #define HAL_SPI_MODULE_ENABLED /*#define HAL_SRAM_MODULE_ENABLED*/ #define HAL_TIM_MODULE_ENABLED -#define HAL_UART_MODULE_ENABLED +/*#define HAL_UART_MODULE_ENABLED*/ /*#define HAL_USART_MODULE_ENABLED*/ /*#define HAL_WWDG_MODULE_ENABLED*/ #define HAL_MMC_MODULE_ENABLED diff --git a/variants/NUCLEO_F030R8/stm32f0xx_hal_conf.h b/variants/NUCLEO_F030R8/stm32f0xx_hal_conf.h index 31cbb5dc00..6e1d1cc964 100644 --- a/variants/NUCLEO_F030R8/stm32f0xx_hal_conf.h +++ b/variants/NUCLEO_F030R8/stm32f0xx_hal_conf.h @@ -74,7 +74,7 @@ #define HAL_SPI_MODULE_ENABLED #define HAL_TIM_MODULE_ENABLED //#define HAL_TSC_MODULE_ENABLED -#define HAL_UART_MODULE_ENABLED +//#define HAL_UART_MODULE_ENABLED //#define HAL_USART_MODULE_ENABLED //#define HAL_WWDG_MODULE_ENABLED diff --git a/variants/NUCLEO_F091RC/stm32f0xx_hal_conf.h b/variants/NUCLEO_F091RC/stm32f0xx_hal_conf.h index 45a08eabe7..86877ea86f 100644 --- a/variants/NUCLEO_F091RC/stm32f0xx_hal_conf.h +++ b/variants/NUCLEO_F091RC/stm32f0xx_hal_conf.h @@ -74,7 +74,7 @@ #define HAL_SPI_MODULE_ENABLED #define HAL_TIM_MODULE_ENABLED //#define HAL_TSC_MODULE_ENABLED -#define HAL_UART_MODULE_ENABLED +//#define HAL_UART_MODULE_ENABLED //#define HAL_USART_MODULE_ENABLED //#define HAL_WWDG_MODULE_ENABLED diff --git a/variants/NUCLEO_F103RB/stm32f1xx_hal_conf.h b/variants/NUCLEO_F103RB/stm32f1xx_hal_conf.h index b466615b2f..50e9693dfa 100644 --- a/variants/NUCLEO_F103RB/stm32f1xx_hal_conf.h +++ b/variants/NUCLEO_F103RB/stm32f1xx_hal_conf.h @@ -78,7 +78,7 @@ #define HAL_SPI_MODULE_ENABLED /*#define HAL_SRAM_MODULE_ENABLED*/ #define HAL_TIM_MODULE_ENABLED -#define HAL_UART_MODULE_ENABLED +/*#define HAL_UART_MODULE_ENABLED*/ /*#define HAL_USART_MODULE_ENABLED*/ /*#define HAL_WWDG_MODULE_ENABLED*/ #define HAL_MMC_MODULE_ENABLED diff --git a/variants/NUCLEO_F207ZG/stm32f2xx_hal_conf.h b/variants/NUCLEO_F207ZG/stm32f2xx_hal_conf.h index 156dd264c6..4d383e0d5c 100644 --- a/variants/NUCLEO_F207ZG/stm32f2xx_hal_conf.h +++ b/variants/NUCLEO_F207ZG/stm32f2xx_hal_conf.h @@ -73,8 +73,8 @@ /*#define HAL_SD_MODULE_ENABLED */ #define HAL_SPI_MODULE_ENABLED #define HAL_TIM_MODULE_ENABLED -#define HAL_UART_MODULE_ENABLED -#define HAL_USART_MODULE_ENABLED +/* #define HAL_UART_MODULE_ENABLED */ +/* #define HAL_USART_MODULE_ENABLED */ /*#define HAL_IRDA_MODULE_ENABLED */ /*#define HAL_SMARTCARD_MODULE_ENABLED */ /*#define HAL_WWDG_MODULE_ENABLED */ diff --git a/variants/NUCLEO_F302R8/stm32f3xx_hal_conf.h b/variants/NUCLEO_F302R8/stm32f3xx_hal_conf.h index 39ab432930..9f0fdee21c 100644 --- a/variants/NUCLEO_F302R8/stm32f3xx_hal_conf.h +++ b/variants/NUCLEO_F302R8/stm32f3xx_hal_conf.h @@ -81,8 +81,8 @@ #define HAL_SPI_MODULE_ENABLED #define HAL_TIM_MODULE_ENABLED // #define HAL_TSC_MODULE_ENABLED -#define HAL_UART_MODULE_ENABLED -#define HAL_USART_MODULE_ENABLED +// #define HAL_UART_MODULE_ENABLED +// #define HAL_USART_MODULE_ENABLED // #define HAL_WWDG_MODULE_ENABLED /* ########################## HSE/HSI Values adaptation ##################### */ diff --git a/variants/NUCLEO_F303K8/stm32f3xx_hal_conf.h b/variants/NUCLEO_F303K8/stm32f3xx_hal_conf.h index 020b13c894..9eee3425dc 100644 --- a/variants/NUCLEO_F303K8/stm32f3xx_hal_conf.h +++ b/variants/NUCLEO_F303K8/stm32f3xx_hal_conf.h @@ -74,8 +74,8 @@ #define HAL_RTC_MODULE_ENABLED #define HAL_SPI_MODULE_ENABLED #define HAL_TIM_MODULE_ENABLED -#define HAL_UART_MODULE_ENABLED -#define HAL_USART_MODULE_ENABLED +/*#define HAL_UART_MODULE_ENABLED */ +/*#define HAL_USART_MODULE_ENABLED */ /*#define HAL_IRDA_MODULE_ENABLED */ /*#define HAL_SMARTCARD_MODULE_ENABLED */ /*#define HAL_SMBUS_MODULE_ENABLED */ diff --git a/variants/NUCLEO_F303RE/stm32f3xx_hal_conf.h b/variants/NUCLEO_F303RE/stm32f3xx_hal_conf.h index ecb7c07a8d..bdc387d9ff 100644 --- a/variants/NUCLEO_F303RE/stm32f3xx_hal_conf.h +++ b/variants/NUCLEO_F303RE/stm32f3xx_hal_conf.h @@ -81,7 +81,7 @@ #define HAL_SPI_MODULE_ENABLED #define HAL_TIM_MODULE_ENABLED // #define HAL_TSC_MODULE_ENABLED -#define HAL_UART_MODULE_ENABLED +// #define HAL_UART_MODULE_ENABLED // #define HAL_USART_MODULE_ENABLED // #define HAL_WWDG_MODULE_ENABLED diff --git a/variants/NUCLEO_F401RE/stm32f4xx_hal_conf.h b/variants/NUCLEO_F401RE/stm32f4xx_hal_conf.h index c7eedbd2a9..7bf7ae0515 100644 --- a/variants/NUCLEO_F401RE/stm32f4xx_hal_conf.h +++ b/variants/NUCLEO_F401RE/stm32f4xx_hal_conf.h @@ -81,8 +81,8 @@ /* #define HAL_SD_MODULE_ENABLED */ #define HAL_SPI_MODULE_ENABLED #define HAL_TIM_MODULE_ENABLED -#define HAL_UART_MODULE_ENABLED -#define HAL_USART_MODULE_ENABLED +/* #define HAL_UART_MODULE_ENABLED */ +/* #define HAL_USART_MODULE_ENABLED */ /* #define HAL_IRDA_MODULE_ENABLED */ /* #define HAL_SMARTCARD_MODULE_ENABLED */ /* #define HAL_WWDG_MODULE_ENABLED */ diff --git a/variants/NUCLEO_F411RE/stm32f4xx_hal_conf.h b/variants/NUCLEO_F411RE/stm32f4xx_hal_conf.h index c7eedbd2a9..7bf7ae0515 100644 --- a/variants/NUCLEO_F411RE/stm32f4xx_hal_conf.h +++ b/variants/NUCLEO_F411RE/stm32f4xx_hal_conf.h @@ -81,8 +81,8 @@ /* #define HAL_SD_MODULE_ENABLED */ #define HAL_SPI_MODULE_ENABLED #define HAL_TIM_MODULE_ENABLED -#define HAL_UART_MODULE_ENABLED -#define HAL_USART_MODULE_ENABLED +/* #define HAL_UART_MODULE_ENABLED */ +/* #define HAL_USART_MODULE_ENABLED */ /* #define HAL_IRDA_MODULE_ENABLED */ /* #define HAL_SMARTCARD_MODULE_ENABLED */ /* #define HAL_WWDG_MODULE_ENABLED */ diff --git a/variants/NUCLEO_F429ZI/stm32f4xx_hal_conf.h b/variants/NUCLEO_F429ZI/stm32f4xx_hal_conf.h index 29a35eaed0..2a0682b5d8 100644 --- a/variants/NUCLEO_F429ZI/stm32f4xx_hal_conf.h +++ b/variants/NUCLEO_F429ZI/stm32f4xx_hal_conf.h @@ -81,7 +81,7 @@ /* #define HAL_SD_MODULE_ENABLED */ #define HAL_SPI_MODULE_ENABLED #define HAL_TIM_MODULE_ENABLED -#define HAL_UART_MODULE_ENABLED +/* #define HAL_UART_MODULE_ENABLED */ /* #define HAL_USART_MODULE_ENABLED */ /* #define HAL_IRDA_MODULE_ENABLED */ /* #define HAL_SMARTCARD_MODULE_ENABLED */ diff --git a/variants/NUCLEO_F446RE/stm32f4xx_hal_conf.h b/variants/NUCLEO_F446RE/stm32f4xx_hal_conf.h index 4a4f6f2bbf..6afe6f2043 100644 --- a/variants/NUCLEO_F446RE/stm32f4xx_hal_conf.h +++ b/variants/NUCLEO_F446RE/stm32f4xx_hal_conf.h @@ -81,8 +81,8 @@ /* #define HAL_SD_MODULE_ENABLED */ #define HAL_SPI_MODULE_ENABLED #define HAL_TIM_MODULE_ENABLED -#define HAL_UART_MODULE_ENABLED -#define HAL_USART_MODULE_ENABLED +/* #define HAL_UART_MODULE_ENABLED */ +/* #define HAL_USART_MODULE_ENABLED */ /* #define HAL_IRDA_MODULE_ENABLED */ /* #define HAL_SMARTCARD_MODULE_ENABLED */ /* #define HAL_WWDG_MODULE_ENABLED */ diff --git a/variants/NUCLEO_L031K6/stm32l0xx_hal_conf.h b/variants/NUCLEO_L031K6/stm32l0xx_hal_conf.h index f5c9bee9fc..8b429689d1 100644 --- a/variants/NUCLEO_L031K6/stm32l0xx_hal_conf.h +++ b/variants/NUCLEO_L031K6/stm32l0xx_hal_conf.h @@ -65,7 +65,7 @@ #define HAL_SPI_MODULE_ENABLED #define HAL_TIM_MODULE_ENABLED /*#define HAL_TSC_MODULE_ENABLED */ -#define HAL_UART_MODULE_ENABLED +/*#define HAL_UART_MODULE_ENABLED */ /*#define HAL_USART_MODULE_ENABLED */ /*#define HAL_IRDA_MODULE_ENABLED */ /*#define HAL_SMARTCARD_MODULE_ENABLED */ diff --git a/variants/NUCLEO_L053R8/stm32l0xx_hal_conf.h b/variants/NUCLEO_L053R8/stm32l0xx_hal_conf.h index 26b1b171f6..65fc9bcacc 100644 --- a/variants/NUCLEO_L053R8/stm32l0xx_hal_conf.h +++ b/variants/NUCLEO_L053R8/stm32l0xx_hal_conf.h @@ -74,7 +74,7 @@ #define HAL_SPI_MODULE_ENABLED #define HAL_TIM_MODULE_ENABLED // #define HAL_TSC_MODULE_ENABLED -#define HAL_UART_MODULE_ENABLED +// #define HAL_UART_MODULE_ENABLED // #define HAL_USART_MODULE_ENABLED // #define HAL_IRDA_MODULE_ENABLED // #define HAL_SMARTCARD_MODULE_ENABLED diff --git a/variants/NUCLEO_L152RE/stm32l1xx_hal_conf.h b/variants/NUCLEO_L152RE/stm32l1xx_hal_conf.h index 1932a2bfab..00a0d5744e 100644 --- a/variants/NUCLEO_L152RE/stm32l1xx_hal_conf.h +++ b/variants/NUCLEO_L152RE/stm32l1xx_hal_conf.h @@ -78,8 +78,8 @@ #define HAL_SPI_MODULE_ENABLED /*#define HAL_SRAM_MODULE_ENABLED*/ #define HAL_TIM_MODULE_ENABLED -#define HAL_UART_MODULE_ENABLED -#define HAL_USART_MODULE_ENABLED +/*#define HAL_UART_MODULE_ENABLED*/ +/*#define HAL_USART_MODULE_ENABLED*/ /*#define HAL_WWDG_MODULE_ENABLED*/ /* ########################## Oscillator Values adaptation ####################*/ diff --git a/variants/NUCLEO_L432KC/stm32l4xx_hal_conf.h b/variants/NUCLEO_L432KC/stm32l4xx_hal_conf.h index 90109e68fa..01413e9af6 100644 --- a/variants/NUCLEO_L432KC/stm32l4xx_hal_conf.h +++ b/variants/NUCLEO_L432KC/stm32l4xx_hal_conf.h @@ -95,8 +95,8 @@ /* #define HAL_SWPMI_MODULE_ENABLED */ #define HAL_TIM_MODULE_ENABLED /* #define HAL_TSC_MODULE_ENABLED */ -#define HAL_UART_MODULE_ENABLED -#define HAL_USART_MODULE_ENABLED +/* #define HAL_UART_MODULE_ENABLED */ +/* #define HAL_USART_MODULE_ENABLED */ /* #define HAL_WWDG_MODULE_ENABLED */ diff --git a/variants/NUCLEO_L476RG/stm32l4xx_hal_conf.h b/variants/NUCLEO_L476RG/stm32l4xx_hal_conf.h index 90109e68fa..01413e9af6 100644 --- a/variants/NUCLEO_L476RG/stm32l4xx_hal_conf.h +++ b/variants/NUCLEO_L476RG/stm32l4xx_hal_conf.h @@ -95,8 +95,8 @@ /* #define HAL_SWPMI_MODULE_ENABLED */ #define HAL_TIM_MODULE_ENABLED /* #define HAL_TSC_MODULE_ENABLED */ -#define HAL_UART_MODULE_ENABLED -#define HAL_USART_MODULE_ENABLED +/* #define HAL_UART_MODULE_ENABLED */ +/* #define HAL_USART_MODULE_ENABLED */ /* #define HAL_WWDG_MODULE_ENABLED */ diff --git a/variants/board_template/stm32yyxx_hal_conf.h b/variants/board_template/stm32yyxx_hal_conf.h index e50097fbe9..9e9bf95b30 100644 --- a/variants/board_template/stm32yyxx_hal_conf.h +++ b/variants/board_template/stm32yyxx_hal_conf.h @@ -15,4 +15,5 @@ * - Adjust HSE/HSI Values adaptation if needed * - Update any other configurations * + * Note: disabled HAL_UART_MODULE_ENABLED, it is handled thanks Arduino menu (enabled by default) */ \ No newline at end of file From 1e8017012ee09520cc6d9e21ee961596f9e1ad6f Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Tue, 5 Jun 2018 23:58:35 +0200 Subject: [PATCH 2/8] [HWSerial] Add LPUART1 management Signed-off-by: Frederic Pillon --- cores/arduino/HardwareSerial.cpp | 10 ++- cores/arduino/HardwareSerial.h | 1 + cores/arduino/stm32/uart.c | 108 ++++++++++++++++++++++++++++++- cores/arduino/stm32/uart.h | 13 +++- 4 files changed, 128 insertions(+), 4 deletions(-) diff --git a/cores/arduino/HardwareSerial.cpp b/cores/arduino/HardwareSerial.cpp index 055bcbd9f1..83e998782a 100644 --- a/cores/arduino/HardwareSerial.cpp +++ b/cores/arduino/HardwareSerial.cpp @@ -34,7 +34,7 @@ #if defined(HAVE_HWSERIAL1) || defined(HAVE_HWSERIAL2) || defined(HAVE_HWSERIAL3) ||\ defined(HAVE_HWSERIAL4) || defined(HAVE_HWSERIAL5) || defined(HAVE_HWSERIAL6) ||\ defined(HAVE_HWSERIAL7) || defined(HAVE_HWSERIAL8) || defined(HAVE_HWSERIAL8) ||\ - defined(HAVE_HWSERIAL10) + defined(HAVE_HWSERIAL10) || defined(HAVE_HWSERIALLP1) // SerialEvent functions are weak, so when the user doesn't define them, // the linker just sets their address to 0 (which is checked below). #if defined(HAVE_HWSERIAL1) @@ -102,6 +102,11 @@ HardwareSerial Serial10(UART10); void serialEvent10() __attribute__((weak)); #endif + +#if defined(HAVE_HWSERIALLP1) + HardwareSerial SerialLP1(LPUART1); + void serialEventLP1() __attribute__((weak)); +#endif #endif // HAVE_HWSERIALx void serialEventRun(void) @@ -136,6 +141,9 @@ void serialEventRun(void) #if defined(HAVE_HWSERIAL10) if (serialEventl10 && Serial10.available()) serialEvent10(); #endif +#if defined(HAVE_HWSERIALLP1) + if (serialEventLP1 && SerialLP1.available()) serialEventLP1(); +#endif } // Constructors //////////////////////////////////////////////////////////////// diff --git a/cores/arduino/HardwareSerial.h b/cores/arduino/HardwareSerial.h index dff7163745..1484d67401 100644 --- a/cores/arduino/HardwareSerial.h +++ b/cores/arduino/HardwareSerial.h @@ -146,6 +146,7 @@ extern HardwareSerial Serial7; extern HardwareSerial Serial8; extern HardwareSerial Serial9; extern HardwareSerial Serial10; +extern HardwareSerial SerialLP1; extern void serialEventRun(void) __attribute__((weak)); diff --git a/cores/arduino/stm32/uart.c b/cores/arduino/stm32/uart.c index 4920fff7aa..cdae402d8c 100644 --- a/cores/arduino/stm32/uart.c +++ b/cores/arduino/stm32/uart.c @@ -76,10 +76,10 @@ #define UART_NUM (10) #elif defined(STM32F0xx) || defined(STM32F7xx) #define UART_NUM (8) -#elif defined(STM32F2xx) +#elif defined(STM32F2xx) || defined(STM32L4xx) #define UART_NUM (6) #elif defined(STM32F1xx) || defined(STM32F3xx) ||\ - defined(STM32L0xx) || defined(STM32L1xx) || defined(STM32L4xx) + defined(STM32L0xx) || defined(STM32L1xx) #define UART_NUM (5) #else #error "Unknown Family - unknown UART_NUM" @@ -198,6 +198,19 @@ void uart_init(serial_t *obj) obj->irq = USART6_IRQn; } #endif +#if defined(LPUART1_BASE) + else if(obj->uart == LPUART1) { + __HAL_RCC_LPUART1_FORCE_RESET(); + __HAL_RCC_LPUART1_RELEASE_RESET(); + __HAL_RCC_LPUART1_CLK_ENABLE(); +#if !defined(USART3_BASE) + obj->index = 2; +#else + obj->index = 5; +#endif + obj->irq = LPUART1_IRQn; + } +#endif #if defined(UART7_BASE) else if(obj->uart == UART7) { __HAL_RCC_UART7_FORCE_RESET(); @@ -280,6 +293,7 @@ void uart_init(serial_t *obj) #endif /* STM32F1xx */ HAL_GPIO_Init(port, &GPIO_InitStruct); + //Configure uart uart_handlers[obj->index] = huart; huart->Instance = (USART_TypeDef *)(obj->uart); @@ -296,6 +310,56 @@ void uart_init(serial_t *obj) #endif // huart->Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; +#if defined(LPUART1_BASE) + /* Note that LPUART clock source must be in the range [3 x baud rate, 4096 x baud rate], check Ref Manual */ + if(obj->uart == LPUART1) { + if (obj->baudrate <= 9600) { + HAL_UARTEx_EnableClockStopMode(huart); + HAL_UARTEx_EnableStopMode(huart); + } else { + HAL_UARTEx_DisableClockStopMode(huart); + HAL_UARTEx_DisableStopMode(huart); + } + /* Trying default LPUART clock source */ + if (HAL_UART_Init(huart) == HAL_OK) { + return; + } + /* Trying to change LPUART clock source */ + /* If baudrate is lower than or equal to 9600 try to change to LSE */ + if(obj->baudrate <= 9600) { + /* Enable the clock if not already set by user */ + if(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET) { +#ifdef __HAL_RCC_LSEDRIVE_CONFIG + __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW); +#endif + RCC_OscInitTypeDef RCC_OscInitStruct = {0}; + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE; + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; + RCC_OscInitStruct.LSEState = RCC_LSE_ON; + if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { + Error_Handler(); + } + } + + __HAL_RCC_LPUART1_CONFIG(RCC_LPUART1CLKSOURCE_LSE); + if (HAL_UART_Init(huart) == HAL_OK) { + return; + } + } + if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY)) { + __HAL_RCC_LPUART1_CONFIG(RCC_LPUART1CLKSOURCE_HSI); + if (HAL_UART_Init(huart) == HAL_OK) { + return; + } + } + __HAL_RCC_LPUART1_CONFIG(RCC_LPUART1CLKSOURCE_PCLK1); + if (HAL_UART_Init(huart) == HAL_OK) { + return; + } + __HAL_RCC_LPUART1_CONFIG(RCC_LPUART1CLKSOURCE_SYSCLK); + } +#endif + if(HAL_UART_Init(huart) != HAL_OK) { return; } @@ -330,6 +394,12 @@ void uart_deinit(serial_t *obj) __HAL_RCC_USART3_RELEASE_RESET(); __HAL_RCC_USART3_CLK_DISABLE(); break; +#elif defined(LPUART1_BASE) + case 2: + __HAL_RCC_LPUART1_FORCE_RESET(); + __HAL_RCC_LPUART1_RELEASE_RESET(); + __HAL_RCC_LPUART1_CLK_DISABLE(); + break; #endif #if defined(UART4_BASE) case 3: @@ -363,6 +433,12 @@ void uart_deinit(serial_t *obj) __HAL_RCC_USART6_RELEASE_RESET(); __HAL_RCC_USART6_CLK_DISABLE(); break; +#elif defined(LPUART1_BASE) + case 5: + __HAL_RCC_LPUART1_FORCE_RESET(); + __HAL_RCC_LPUART1_RELEASE_RESET(); + __HAL_RCC_LPUART1_CLK_DISABLE(); + break; #endif #if defined(UART7_BASE) case 6: @@ -467,6 +543,17 @@ void uart_config_lowpower(serial_t *obj) __HAL_RCC_UART5_CONFIG(RCC_UART5CLKSOURCE_HSI); } break; +#endif +#if defined(LPUART1_BASE) && defined(__HAL_RCC_LPUART1_CONFIG) +#if !defined(USART3_BASE) + case 2: +#else + case 5: +#endif + if (__HAL_RCC_GET_LPUART1_SOURCE() != RCC_LPUART1CLKSOURCE_HSI) { + __HAL_RCC_LPUART1_CONFIG(RCC_LPUART1CLKSOURCE_HSI); + } + break; #endif } } @@ -883,6 +970,23 @@ void USART6_IRQHandler(void) } #endif +/** + * @brief LPUART 1 IRQ handler + * @param None + * @retval None + */ +#if defined(LPUART1_BASE) +void LPUART1_IRQHandler(void) +{ + HAL_NVIC_ClearPendingIRQ(LPUART1_IRQn); +#if !defined(USART3_BASE) + HAL_UART_IRQHandler(uart_handlers[2]); +#else + HAL_UART_IRQHandler(uart_handlers[5]); +#endif +} +#endif + /** * @brief UART 7 IRQ handler * @param None diff --git a/cores/arduino/stm32/uart.h b/cores/arduino/stm32/uart.h index a3a555ffdc..9a3f15eee5 100644 --- a/cores/arduino/stm32/uart.h +++ b/cores/arduino/stm32/uart.h @@ -52,7 +52,13 @@ #else #if !defined(HWSERIAL_NONE) && defined(SERIAL_UART_INSTANCE) -#if SERIAL_UART_INSTANCE == 1 +#if SERIAL_UART_INSTANCE == 0 +#define ENABLE_HWSERIALLP1 +#if !defined(Serial) +#define Serial SerialLP1 +#define serialEvent serialEventLP1 +#endif +#elif SERIAL_UART_INSTANCE == 1 #define ENABLE_HWSERIAL1 #if !defined(Serial) #define Serial Serial1 @@ -119,6 +125,11 @@ #endif // SERIAL_UART_INSTANCE == x #endif // !HWSERIAL_NONE && SERIAL_UART_INSTANCE +#if defined(ENABLE_HWSERIALLP1) +#if defined(LPUART1_BASE) +#define HAVE_HWSERIALLP1 +#endif +#endif #if defined(ENABLE_HWSERIAL1) #if defined(USART1_BASE) #define HAVE_HWSERIAL1 From f9b701b08c6cc5b75411c16007caf07c9bd769fa Mon Sep 17 00:00:00 2001 From: "Frederic.Pillon" Date: Mon, 11 Jun 2018 18:12:03 +0200 Subject: [PATCH 3/8] [RTC] Do not reset RTC Domain Backup domain is handled by HAL_RCCEx_PeriphCLKConfig() when changing RTC clock source. If one other peripheral uses LSE and RTC do not use it, force reset of the backup domain will break this peripheral. Signed-off-by: Frederic.Pillon --- cores/arduino/stm32/rtc.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/cores/arduino/stm32/rtc.c b/cores/arduino/stm32/rtc.c index 1f9398d4ad..ae28027d14 100644 --- a/cores/arduino/stm32/rtc.c +++ b/cores/arduino/stm32/rtc.c @@ -321,9 +321,6 @@ void RTC_init(hourFormat_t format, sourceClock_t source) /* Allow access to Backup domain */ HAL_PWR_EnableBkUpAccess(); #endif - /* Reset RTC Domain */ - __HAL_RCC_BACKUPRESET_FORCE(); - __HAL_RCC_BACKUPRESET_RELEASE(); /* Init RTC clock */ RTC_initClock(source); From 50ccd99c805833fc2d283f22840ecdc8e064ae9f Mon Sep 17 00:00:00 2001 From: "Frederic.Pillon" Date: Tue, 12 Jun 2018 10:23:47 +0200 Subject: [PATCH 4/8] [clock] Add new api enableClock Handle properly clock activation. Signed-off-by: Frederic Pillon --- cores/arduino/stm32/clock.c | 60 ++++++++++++++++++++++++++++++++++++- cores/arduino/stm32/clock.h | 9 ++++++ cores/arduino/stm32/rtc.c | 40 +++---------------------- cores/arduino/stm32/rtc.h | 9 +----- cores/arduino/stm32/uart.c | 25 ++-------------- 5 files changed, 76 insertions(+), 67 deletions(-) diff --git a/cores/arduino/stm32/clock.c b/cores/arduino/stm32/clock.c index 18856903d1..d66b240869 100644 --- a/cores/arduino/stm32/clock.c +++ b/cores/arduino/stm32/clock.c @@ -35,7 +35,7 @@ * ****************************************************************************** */ -#include "stm32_def.h" +#include "clock.h" #ifdef __cplusplus extern "C" { @@ -120,6 +120,64 @@ void delayInsideIT(uint32_t delay_us) #endif } +/** + * @brief Enable the specified clock if not already set + * @param source: clock source: LSE_CLOCK, LSI_CLOCK, HSI_CLOCK or HSE_CLOCK + * @retval None + */ +void enableClock(sourceClock_t source) +{ + RCC_OscInitTypeDef RCC_OscInitStruct = {0}; + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; + + switch(source) { + case LSI_CLOCK: + if(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET) { + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI; + RCC_OscInitStruct.LSIState = RCC_LSI_ON; + } + break; + case HSI_CLOCK: + if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET) { + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; + RCC_OscInitStruct.HSIState = RCC_HSI_ON; + RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; + } + break; + case LSE_CLOCK: + /* Enable Power Clock */ + if(__HAL_RCC_PWR_IS_CLK_DISABLED()) { + __HAL_RCC_PWR_CLK_ENABLE(); + } +#ifdef HAL_PWR_MODULE_ENABLED + /* Allow access to Backup domain */ + HAL_PWR_EnableBkUpAccess(); +#endif + if(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET) { +#ifdef __HAL_RCC_LSEDRIVE_CONFIG + __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW); +#endif + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE; + RCC_OscInitStruct.LSEState = RCC_LSE_ON; + } + break; + case HSE_CLOCK: + if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET) { + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; + RCC_OscInitStruct.HSEState = RCC_HSE_ON; + } + break; + default: + /* No valid clock to enable */ + break; + } + if(RCC_OscInitStruct.OscillatorType != RCC_OSCILLATORTYPE_NONE) { + if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { + Error_Handler(); + } + } +} + #ifdef __cplusplus } #endif diff --git a/cores/arduino/stm32/clock.h b/cores/arduino/stm32/clock.h index 995e61822d..635f7abc25 100644 --- a/cores/arduino/stm32/clock.h +++ b/cores/arduino/stm32/clock.h @@ -47,6 +47,14 @@ #endif /* Exported types ------------------------------------------------------------*/ +/* Clock source selection */ +typedef enum { + LSI_CLOCK, + HSI_CLOCK, + LSE_CLOCK, + HSE_CLOCK +} sourceClock_t; + /* Exported constants --------------------------------------------------------*/ /* Exported macro ------------------------------------------------------------*/ /* Exported functions ------------------------------------------------------- */ @@ -54,6 +62,7 @@ uint32_t GetCurrentMilli(void); uint32_t GetCurrentMicro(void); void delayInsideIT(uint32_t delay_us); +void enableClock(sourceClock_t source); #ifdef __cplusplus } #endif diff --git a/cores/arduino/stm32/rtc.c b/cores/arduino/stm32/rtc.c index ae28027d14..9b36e0da77 100644 --- a/cores/arduino/stm32/rtc.c +++ b/cores/arduino/stm32/rtc.c @@ -100,22 +100,11 @@ void RTC_SetClockSource(sourceClock_t source) */ static void RTC_initClock(sourceClock_t source) { - RCC_OscInitTypeDef RCC_OscInitStruct; RCC_PeriphCLKInitTypeDef PeriphClkInit; if(source == LSE_CLOCK) { /* Enable the clock if not already set by user */ - if(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET) { -#ifdef __HAL_RCC_LSEDRIVE_CONFIG - __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW); -#endif - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; - RCC_OscInitStruct.LSEState = RCC_LSE_ON; - if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { - Error_Handler(); - } - } + enableClock(LSE_CLOCK); PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC; PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE; @@ -125,14 +114,8 @@ static void RTC_initClock(sourceClock_t source) clkSrc = LSE_CLOCK; } else if(source == HSE_CLOCK) { /* Enable the clock if not already set by user */ - if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET) { - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; - RCC_OscInitStruct.HSEState = RCC_HSE_ON; - if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { - Error_Handler(); - } - } + enableClock(HSE_CLOCK); + /* HSE division factor for RTC clock must be set to ensure that * the clock supplied to the RTC is less than or equal to 1 MHz */ @@ -182,14 +165,7 @@ static void RTC_initClock(sourceClock_t source) clkSrc = HSE_CLOCK; } else if(source == LSI_CLOCK) { /* Enable the clock if not already set by user */ - if(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET) { - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; - RCC_OscInitStruct.LSIState = RCC_LSI_ON; - if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { - Error_Handler(); - } - } + enableClock(LSI_CLOCK); PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC; PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; @@ -314,14 +290,6 @@ void RTC_init(hourFormat_t format, sourceClock_t source) { initFormat = format; - /* Enable Power Clock */ - __HAL_RCC_PWR_CLK_ENABLE(); - -#ifdef HAL_PWR_MODULE_ENABLED - /* Allow access to Backup domain */ - HAL_PWR_EnableBkUpAccess(); -#endif - /* Init RTC clock */ RTC_initClock(source); diff --git a/cores/arduino/stm32/rtc.h b/cores/arduino/stm32/rtc.h index 67f2aba72f..729d0344d1 100644 --- a/cores/arduino/stm32/rtc.h +++ b/cores/arduino/stm32/rtc.h @@ -40,7 +40,7 @@ #define __RTC_H /* Includes ------------------------------------------------------------------*/ -#include "stm32_def.h" +#include "clock.h" #ifdef HAL_RTC_MODULE_ENABLED @@ -72,13 +72,6 @@ typedef enum { Y_MSK = 32 } alarmMask_t; -/* Clock source selection */ -typedef enum { - LSI_CLOCK, - LSE_CLOCK, - HSE_CLOCK -} sourceClock_t; - typedef void(*voidCallbackPtr)(void *); /* Exported constants --------------------------------------------------------*/ diff --git a/cores/arduino/stm32/uart.c b/cores/arduino/stm32/uart.c index cdae402d8c..5f24c0d1de 100644 --- a/cores/arduino/stm32/uart.c +++ b/cores/arduino/stm32/uart.c @@ -328,18 +328,7 @@ void uart_init(serial_t *obj) /* If baudrate is lower than or equal to 9600 try to change to LSE */ if(obj->baudrate <= 9600) { /* Enable the clock if not already set by user */ - if(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET) { -#ifdef __HAL_RCC_LSEDRIVE_CONFIG - __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW); -#endif - RCC_OscInitTypeDef RCC_OscInitStruct = {0}; - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; - RCC_OscInitStruct.LSEState = RCC_LSE_ON; - if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { - Error_Handler(); - } - } + enableClock(LSE_CLOCK); __HAL_RCC_LPUART1_CONFIG(RCC_LPUART1CLKSOURCE_LSE); if (HAL_UART_Init(huart) == HAL_OK) { @@ -496,17 +485,9 @@ void uart_config_lowpower(serial_t *obj) if(obj == NULL) { return; } - RCC_OscInitTypeDef RCC_OscInitStruct = {0}; /* Ensure HSI clock is enable */ - if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET) { - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; - RCC_OscInitStruct.HSIState = RCC_HSI_ON; - RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct)!= HAL_OK) { - Error_Handler(); - } - } + enableClock(HSI_CLOCK); + /* Configure HSI as source clock for low power wakeup clock */ switch (obj->index) { #if defined(USART1_BASE) From e02bc35d8844148c5d4157f9b88cae72d71520f2 Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Thu, 14 Jun 2018 15:38:28 +0200 Subject: [PATCH 5/8] [UART] Add some enhancements Avoid duplicated common settings Use GPIO_SPEED_FREQ_VERY_HIGH if exists Init OneBitSampling fields if exists Signed-off-by: Frederic Pillon --- cores/arduino/stm32/uart.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/cores/arduino/stm32/uart.c b/cores/arduino/stm32/uart.c index 5f24c0d1de..9ef0f3582b 100644 --- a/cores/arduino/stm32/uart.c +++ b/cores/arduino/stm32/uart.c @@ -270,13 +270,18 @@ void uart_init(serial_t *obj) function = pinmap_function(obj->pin_rx, PinMap_UART_RX); GPIO_InitStruct.Pin = STM_GPIO_PIN(obj->pin_rx); GPIO_InitStruct.Mode = STM_PIN_MODE(function); - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.Pull = STM_PIN_PUPD(function); + /* Common */ #ifdef STM32F1xx pin_SetF1AFPin(STM_PIN_AFNUM(function)); #else GPIO_InitStruct.Alternate = STM_PIN_AFNUM(function); #endif /* STM32F1xx */ +#ifdef GPIO_SPEED_FREQ_VERY_HIGH + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; +#else + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; +#endif HAL_GPIO_Init(port, &GPIO_InitStruct); //TX @@ -284,13 +289,7 @@ void uart_init(serial_t *obj) function = pinmap_function(obj->pin_tx, PinMap_UART_TX); GPIO_InitStruct.Pin = STM_GPIO_PIN(obj->pin_tx); GPIO_InitStruct.Mode = STM_PIN_MODE(function); - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.Pull = STM_PIN_PUPD(function); -#ifdef STM32F1xx - pin_SetF1AFPin(STM_PIN_AFNUM(function)); -#else - GPIO_InitStruct.Alternate = STM_PIN_AFNUM(function); -#endif /* STM32F1xx */ HAL_GPIO_Init(port, &GPIO_InitStruct); @@ -308,7 +307,9 @@ void uart_init(serial_t *obj) && !defined(STM32L1xx) huart->AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; #endif - // huart->Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; +#ifdef UART_ONE_BIT_SAMPLE_DISABLE + huart->Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; +#endif #if defined(LPUART1_BASE) /* Note that LPUART clock source must be in the range [3 x baud rate, 4096 x baud rate], check Ref Manual */ From 72d8bb6d5dc9649565b15262be0fc0105d92a655 Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Thu, 14 Jun 2018 15:41:18 +0200 Subject: [PATCH 6/8] [UART] Fix serial_[rx|tx]_active functions HAL_UART_GetState() return a combined value. So mask needs to be applied before compare the value. Signed-off-by: Frederic Pillon --- cores/arduino/stm32/uart.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cores/arduino/stm32/uart.c b/cores/arduino/stm32/uart.c index 9ef0f3582b..5fb5f7a451 100644 --- a/cores/arduino/stm32/uart.c +++ b/cores/arduino/stm32/uart.c @@ -633,7 +633,7 @@ size_t uart_debug_write(uint8_t *data, uint32_t size) */ uint8_t serial_rx_active(serial_t *obj) { - return ((obj == NULL) ? 1 : (HAL_UART_GetState(uart_handlers[obj->index]) == HAL_UART_STATE_BUSY_RX)); + return ((HAL_UART_GetState(uart_handlers[obj->index]) & HAL_UART_STATE_BUSY_RX) == HAL_UART_STATE_BUSY_RX); } /** @@ -644,7 +644,7 @@ uint8_t serial_rx_active(serial_t *obj) */ uint8_t serial_tx_active(serial_t *obj) { - return ((obj == NULL) ? 1 : (HAL_UART_GetState(uart_handlers[obj->index]) == HAL_UART_STATE_BUSY_TX)); + return ((HAL_UART_GetState(uart_handlers[obj->index]) & HAL_UART_STATE_BUSY_TX) == HAL_UART_STATE_BUSY_TX); } /** From 1cbe636b60f8e553bf4ca1fb099ded59b3fb267a Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Thu, 14 Jun 2018 15:59:15 +0200 Subject: [PATCH 7/8] [UART] Clean up comments Signed-off-by: Frederic Pillon --- cores/arduino/stm32/uart.c | 85 +++++++++++++++++--------------------- cores/arduino/stm32/uart.h | 34 +++++++-------- 2 files changed, 55 insertions(+), 64 deletions(-) diff --git a/cores/arduino/stm32/uart.c b/cores/arduino/stm32/uart.c index 5fb5f7a451..41a0808b62 100644 --- a/cores/arduino/stm32/uart.c +++ b/cores/arduino/stm32/uart.c @@ -1,9 +1,7 @@ /** ****************************************************************************** * @file uart.c - * @author WI6LABS - * @version V1.0.0 - * @date 01-August-2016 + * @author WI6LABS, fpistm * @brief provide the UART interface * ****************************************************************************** @@ -35,17 +33,6 @@ * ****************************************************************************** */ -/** @addtogroup CMSIS - * @{ - */ - -/** @addtogroup stm32f4xx_system - * @{ - */ - -/** @addtogroup STM32F4xx_System_Private_Includes - * @{ - */ #include "uart.h" #include "Arduino.h" #include "PinAF_STM32F1.h" @@ -55,14 +42,13 @@ #endif #if defined(HAL_UART_MODULE_ENABLED) -// if DEBUG_UART is not defined assume this is the one -// linked to PIN_SERIAL_TX +/* If DEBUG_UART is not defined assume this is the one linked to PIN_SERIAL_TX */ #if !defined(DEBUG_UART) #if defined(PIN_SERIAL_TX) #define DEBUG_UART pinmap_peripheral(digitalPinToPinName(PIN_SERIAL_TX), PinMap_UART_TX) #define DEBUG_PINNAME_TX digitalPinToPinName(PIN_SERIAL_TX) #else -// No debug UART defined +/* No debug UART defined */ #define DEBUG_UART NP #define DEBUG_PINNAME_TX NC #endif @@ -71,7 +57,7 @@ #define DEBUG_UART_BAUDRATE 9600 #endif -// @brief uart caracteristics +/* @brief uart caracteristics */ #if defined(STM32F4xx) #define UART_NUM (10) #elif defined(STM32F0xx) || defined(STM32F7xx) @@ -109,25 +95,28 @@ void uart_init(serial_t *obj) GPIO_TypeDef *port; uint32_t function = (uint32_t)NC; - // Determine the UART to use (UART_1, UART_2, ...) + /* Determine the U(S)ART peripheral to use (USART1, USART2, ...) */ USART_TypeDef *uart_tx = pinmap_peripheral(obj->pin_tx, PinMap_UART_TX); USART_TypeDef *uart_rx = pinmap_peripheral(obj->pin_rx, PinMap_UART_RX); - //Pins Rx/Tx must not be NP + /* Pins Rx/Tx must not be NP */ if(uart_rx == NP || uart_tx == NP) { printf("ERROR: at least one UART pin has no peripheral\n"); return; } - // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object + /* + * Get the peripheral name (USART1, USART2, ...) from the pin + * and assign it to the object + */ obj->uart = pinmap_merge_peripheral(uart_tx, uart_rx); if(obj->uart == NP) { - printf("ERROR: UART pins mismatch\n"); + printf("ERROR: U(S)ART pins mismatch\n"); return; } - // Enable USART clock + /* Enable USART clock */ #if defined(USART1_BASE) else if(obj->uart == USART1) { __HAL_RCC_USART1_FORCE_RESET(); @@ -264,8 +253,8 @@ void uart_init(serial_t *obj) } #endif - //Configure GPIOs - //RX + /* Configure GPIOs */ + /* RX */ port = set_GPIO_Port_Clock(STM_PORT(obj->pin_rx)); function = pinmap_function(obj->pin_rx, PinMap_UART_RX); GPIO_InitStruct.Pin = STM_GPIO_PIN(obj->pin_rx); @@ -284,7 +273,7 @@ void uart_init(serial_t *obj) #endif HAL_GPIO_Init(port, &GPIO_InitStruct); - //TX + /* TX */ port = set_GPIO_Port_Clock(STM_PORT(obj->pin_tx)); function = pinmap_function(obj->pin_tx, PinMap_UART_TX); GPIO_InitStruct.Pin = STM_GPIO_PIN(obj->pin_tx); @@ -293,7 +282,7 @@ void uart_init(serial_t *obj) HAL_GPIO_Init(port, &GPIO_InitStruct); - //Configure uart + /* Configure uart */ uart_handlers[obj->index] = huart; huart->Instance = (USART_TypeDef *)(obj->uart); huart->Init.BaudRate = obj->baudrate; @@ -312,7 +301,11 @@ void uart_init(serial_t *obj) #endif #if defined(LPUART1_BASE) - /* Note that LPUART clock source must be in the range [3 x baud rate, 4096 x baud rate], check Ref Manual */ + /* + * Note that LPUART clock source must be in the range + * [3 x baud rate, 4096 x baud rate] + * check Reference Manual + */ if(obj->uart == LPUART1) { if (obj->baudrate <= 9600) { HAL_UARTEx_EnableClockStopMode(huart); @@ -362,7 +355,7 @@ void uart_init(serial_t *obj) */ void uart_deinit(serial_t *obj) { - // Reset UART and disable clock + /* Reset UART and disable clock */ switch (obj->index) { #if defined(USART1_BASE) case 0: @@ -659,11 +652,11 @@ int uart_getc(serial_t *obj, unsigned char* c) } if (serial_rx_active(obj)) { - return -1; // transaction ongoing + return -1; /* Transaction ongoing */ } *c = (unsigned char)(obj->recv); - // Restart RX irq + /* Restart RX irq */ UART_HandleTypeDef *huart = uart_handlers[obj->index]; HAL_UART_Receive_IT(huart, &(obj->recv), 1); @@ -683,7 +676,7 @@ void uart_attach_rx_callback(serial_t *obj, void (*callback)(serial_t*)) return; } - // Exit if a reception is already on-going + /* Exit if a reception is already on-going */ if (serial_rx_active(obj)) { return; } @@ -715,11 +708,11 @@ void uart_attach_tx_callback(serial_t *obj, int (*callback)(serial_t*)) tx_callback[obj->index] = callback; tx_callback_obj[obj->index] = obj; - // Enable interrupt + /* Enable interrupt */ HAL_NVIC_SetPriority(obj->irq, 0, 2); HAL_NVIC_EnableIRQ(obj->irq); - // the following function will enable UART_IT_TXE and error interrupts + /* The following function will enable UART_IT_TXE and error interrupts */ if (HAL_UART_Transmit_IT(uart_handlers[obj->index], &obj->tx_buff[obj->tx_tail], 1) != HAL_OK) { return; } @@ -788,23 +781,23 @@ void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) volatile uint32_t tmpval; #if defined(STM32F1xx) || defined(STM32F2xx) || defined(STM32F4xx) || defined(STM32L1xx) if (__HAL_UART_GET_FLAG(huart, UART_FLAG_PE) != RESET) { - tmpval = huart->Instance->DR; // Clear PE flag + tmpval = huart->Instance->DR; /* Clear PE flag */ } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_FE) != RESET) { - tmpval = huart->Instance->DR; // Clear FE flag + tmpval = huart->Instance->DR; /* Clear FE flag */ } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_NE) != RESET) { - tmpval = huart->Instance->DR; // Clear NE flag + tmpval = huart->Instance->DR; /* Clear NE flag */ } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { - tmpval = huart->Instance->DR; // Clear ORE flag + tmpval = huart->Instance->DR; /* Clear ORE flag */ } #else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_PE) != RESET) { - tmpval = huart->Instance->RDR; // Clear PE flag + tmpval = huart->Instance->RDR; /* Clear PE flag */ } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_FE) != RESET) { - tmpval = huart->Instance->RDR; // Clear FE flag + tmpval = huart->Instance->RDR; /* Clear FE flag */ } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_NE) != RESET) { - tmpval = huart->Instance->RDR; // Clear NE flag + tmpval = huart->Instance->RDR; /* Clear NE flag */ } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { - tmpval = huart->Instance->RDR; // Clear ORE flag + tmpval = huart->Instance->RDR; /* Clear ORE flag */ } #endif @@ -876,7 +869,7 @@ void USART3_IRQHandler(void) HAL_UART_IRQHandler(uart_handlers[2]); } #if defined(STM32F0xx) -// USART3_4_IRQn +/* USART3_4_IRQn */ if(uart_handlers[3] != NULL) { HAL_UART_IRQHandler(uart_handlers[3]); } @@ -887,9 +880,9 @@ void USART3_IRQHandler(void) if(uart_handlers[5] != NULL) { HAL_UART_IRQHandler(uart_handlers[5]); } -#endif // STM32F030xC -#endif // STM32F0xx -#endif // STM32F091xC || STM32F098xx +#endif /* STM32F030xC */ +#endif /* STM32F0xx */ +#endif /* STM32F091xC || STM32F098xx */ } #endif diff --git a/cores/arduino/stm32/uart.h b/cores/arduino/stm32/uart.h index 9a3f15eee5..4f315eaf6a 100644 --- a/cores/arduino/stm32/uart.h +++ b/cores/arduino/stm32/uart.h @@ -1,9 +1,7 @@ /** ****************************************************************************** * @file uart.h - * @author WI6LABS - * @version V1.0.0 - * @date 01-August-2016 + * @author WI6LABS, fpistm * @brief Header for uart module ****************************************************************************** * @attention @@ -122,8 +120,8 @@ #if !defined(Serial) #warning "No generic 'Serial' defined!" #endif -#endif // SERIAL_UART_INSTANCE == x -#endif // !HWSERIAL_NONE && SERIAL_UART_INSTANCE +#endif /* SERIAL_UART_INSTANCE == x */ +#endif /* !HWSERIAL_NONE && SERIAL_UART_INSTANCE */ #if defined(ENABLE_HWSERIALLP1) #if defined(LPUART1_BASE) @@ -219,60 +217,60 @@ struct serial_s { #define USART3_IRQn USART3_4_IRQn #define USART3_IRQHandler USART3_4_IRQHandler -#endif // STM32F091xC || STM32F098xx -#endif // STM32F0xx +#endif /* STM32F091xC || STM32F098xx */ +#endif /* STM32F0xx */ #endif #if defined(USART4_BASE) && !defined(USART4_IRQn) #if defined(STM32F0xx) -// IRQHandler is mapped on USART3_IRQHandler for STM32F0xx +/* IRQHandler is mapped on USART3_IRQHandler for STM32F0xx */ #if defined(STM32F091xC) || defined (STM32F098xx) #define USART4_IRQn USART3_8_IRQn #elif defined(STM32F030xC) #define USART4_IRQn USART3_6_IRQn #else #define USART4_IRQn USART3_4_IRQn -#endif // STM32F091xC || STM32F098xx +#endif /* STM32F091xC || STM32F098xx */ #elif defined(STM32L0xx) #define USART4_IRQn USART4_5_IRQn -#endif // STM32F0xx +#endif /* STM32F0xx */ #endif #if defined(USART5_BASE) && !defined(USART5_IRQn) #if defined(STM32F0xx) -// IRQHandler is mapped on USART3_IRQHandler for STM32F0xx +/* IRQHandler is mapped on USART3_IRQHandler for STM32F0xx */ #if defined(STM32F091xC) || defined (STM32F098xx) #define USART5_IRQn USART3_8_IRQn #elif defined(STM32F030xC) #define USART5_IRQn USART3_6_IRQn -#endif // STM32F091xC || STM32F098xx +#endif /* STM32F091xC || STM32F098xx */ #elif defined(STM32L0xx) #define USART5_IRQn USART4_5_IRQn -#endif // STM32F0xx +#endif /* STM32F0xx */ #endif #if defined (STM32F0xx) -// IRQHandler is mapped on USART3_IRQHandler for STM32F0xx +/* IRQHandler is mapped on USART3_IRQHandler for STM32F0xx */ #if defined(USART6_BASE) && !defined(USART6_IRQn) #if defined(STM32F091xC) || defined (STM32F098xx) #define USART6_IRQn USART3_8_IRQn #elif defined(STM32F030xC) #define USART6_IRQn USART3_6_IRQn -#endif // STM32F091xC || STM32F098xx +#endif /* STM32F091xC || STM32F098xx */ #endif #if defined(USART7_BASE) && !defined(USART7_IRQn) #if defined(STM32F091xC) || defined (STM32F098xx) #define USART7_IRQn USART3_8_IRQn -#endif // STM32F091xC || STM32F098xx +#endif /* STM32F091xC || STM32F098xx */ #endif #if defined(USART8_BASE) && !defined(USART8_IRQn) #if defined(STM32F091xC) || defined (STM32F098xx) #define USART8_IRQn USART3_8_IRQn -#endif // STM32F091xC || STM32F098xx +#endif /* STM32F091xC || STM32F098xx */ #endif -#endif // STM32F0xx +#endif /* STM32F0xx */ /* Exported macro ------------------------------------------------------------*/ /* Exported functions ------------------------------------------------------- */ From 3adfa85748bddd14290005f5eb40c3ec6c25925d Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Thu, 14 Jun 2018 14:56:51 +0200 Subject: [PATCH 8/8] [STM32F09x] Fix Serial using USART[3-8] __HAL_GET_PENDING_IT() read IT line status in SYSCFG register which required to be clocked. This set of registers is implemented only on STM32F09x to collect all pending interrupt sources associated with each interrupt line into a single register. This allows users to check by single read which peripheral requires service in case more than one source is associated to the interrupt line. Fix #260 Signed-off-by: Frederic Pillon --- cores/arduino/stm32/uart.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cores/arduino/stm32/uart.c b/cores/arduino/stm32/uart.c index 41a0808b62..42bd4bf786 100644 --- a/cores/arduino/stm32/uart.c +++ b/cores/arduino/stm32/uart.c @@ -253,6 +253,12 @@ void uart_init(serial_t *obj) } #endif +#if defined(STM32F091xC) || defined (STM32F098xx) + /* Enable SYSCFG Clock */ + /* Required to get SYSCFG interrupt status register */ + __HAL_RCC_SYSCFG_CLK_ENABLE(); +#endif + /* Configure GPIOs */ /* RX */ port = set_GPIO_Port_Clock(STM_PORT(obj->pin_rx));