diff --git a/CI/update/patch/api/0001-feat-implement-weak-_write-hook-for-Print.patch b/CI/update/patch/api/0001-feat-implement-weak-_write-hook-for-Print.patch deleted file mode 100644 index 9563d94cf3..0000000000 --- a/CI/update/patch/api/0001-feat-implement-weak-_write-hook-for-Print.patch +++ /dev/null @@ -1,65 +0,0 @@ -From ed7ffc8cc475b05bf50fed6ef91007c3d75f0393 Mon Sep 17 00:00:00 2001 -From: Aymane Bahssain -Date: Fri, 23 Jan 2026 17:05:29 +0100 -Subject: [PATCH] feat: implement weak _write() hook for Print - -Signed-off-by: Aymane Bahssain ---- - api/Print.cpp | 34 ++++++++++++++++++++++++++++++++++ - 1 file changed, 34 insertions(+) - -diff --git a/api/Print.cpp b/api/Print.cpp -index d827f2e..54319f1 100644 ---- a/api/Print.cpp -+++ b/api/Print.cpp -@@ -24,6 +24,14 @@ - - #include "Print.h" - -+#ifdef ARDUINO_ARCH_STM32 -+#include -+#include "uart.h" -+#if defined (VIRTIO_LOG) -+ #include "virtio_log.h" -+#endif -+#endif -+ - using namespace arduino; - - // Public Methods ////////////////////////////////////////////////////////////// -@@ -248,6 +256,32 @@ size_t Print::println(const Printable& x) - return n; - } - -+#ifdef ARDUINO_ARCH_STM32 -+extern "C" { -+ __attribute__((weak)) -+ int _write(int file, char *ptr, int len) -+ { -+ switch (file) { -+ case STDOUT_FILENO: -+ case STDERR_FILENO: -+ /* Used for core_debug() */ -+#if defined (VIRTIO_LOG) -+ virtio_log((uint8_t *)ptr, (uint32_t)len); -+#elif defined(HAL_UART_MODULE_ENABLED) && !defined(HAL_UART_MODULE_ONLY) -+ uart_debug_write((uint8_t *)ptr, (uint32_t)len); -+#endif -+ break; -+ case STDIN_FILENO: -+ break; -+ default: -+ ((class Print *)file)->write((uint8_t *)ptr, len); -+ break; -+ } -+ return len; -+ } -+} -+#endif -+ - int Print::printf(const char *format, ...) - { - va_list ap; --- -2.51.2.windows.1 - diff --git a/License.md b/License.md index f8167a817a..5ac7d6469f 100644 --- a/License.md +++ b/License.md @@ -8,7 +8,8 @@ This files includes licensing information for parts of this repository. * `cores/arduino/*.*` mainly contains modified files from Arduino, except the files listed under [BSD 3-Clause License](#bsd-3-clause-license) and [MIT License](#mit-license) -* `cores/arduino/stm32` +* `cores/arduino/stm32` - except the files listed under + [BSD 3-Clause License](#bsd-3-clause-license) and [MIT License](#mit-license) * `cores/arduino/api` (own copy of the license in `cores/arduino/api/LICENSE`), except the files listed under [MIT License](#mit-license) * `libraries/EEPROM`, `libraries/IWatchdog`, `libraries/Keyboard`, @@ -31,6 +32,8 @@ This files includes licensing information for parts of this repository. * `libraries/I3C`, `libraries/RGB_LED_TLC59731` and `libraries/SubGhz` * `libraries/SrcWrapper` - files with a BSD 3-Clause header, mainly `libraries/SrcWrapper/src/stm32` and `libraries/SrcWrapper/inc` +* `cores/arduino/stm32/analog.cpp`, `cores/arduino/stm32/interrupt.cpp` and + `cores/arduino/stm32/uart.c` * `variants` - files with a BSD 3-Clause header, such as `PeripheralPins.c`, `variant_generic.*` and `generic_clock.c` @@ -47,7 +50,7 @@ This files includes licensing information for parts of this repository. [MIT License](#mit-license) is used for: * `libraries/VirtIO/*/virtio*` and `libraries/VirtIO/*/VirtIOSerial.*` -* `libraries/SrcWrapper/*/HardwareTimer.*` +* `libraries/SrcWrapper/inc/HardwareTimer.h` and `cores/arduino/stm32/HardwareTimer.cpp` * `libraries/SrcWrapper/src/new.cpp` * `cores/arduino/api/Udp.h` and `cores/arduino/api/deprecated-avr-comp/avr/pgmspace.h` diff --git a/cmake/overall_settings.cmake b/cmake/overall_settings.cmake index ec74e7221e..e20b24202b 100644 --- a/cmake/overall_settings.cmake +++ b/cmake/overall_settings.cmake @@ -50,6 +50,21 @@ function(overall_settings) endif() endif() + # The C library needs symbols defined in the core, but it is scanned after it + # (e.g. _write() requested by vdprintf()): scan the core again together with + # the C library, like the --start-group ... -lc --end-group of platform.txt. + # The file of the library is used, not the target, to not create a dependency cycle. + if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.24) + if(${WANT_STANDARD_LIBC}) + set(CORE_LIBC c) + else() + set(CORE_LIBC c_nano) + endif() + target_link_libraries(user_settings INTERFACE + "$,${CORE_LIBC}>" + ) + endif() + if(NOT DEFINED WANT_OPTIMIZATION) set(WANT_OPTIMIZATION "s") endif() diff --git a/cores/arduino/CMakeLists.txt b/cores/arduino/CMakeLists.txt index e45b024207..5dccf416ec 100644 --- a/cores/arduino/CMakeLists.txt +++ b/cores/arduino/CMakeLists.txt @@ -36,8 +36,14 @@ add_library(core_bin STATIC EXCLUDE_FROM_ALL main.cpp pins_arduino.c Serial.cpp + stm32/analog.cpp + stm32/HardwareTimer.cpp + stm32/interrupt.cpp stm32/startup_stm32yyxx.c stm32/startup_stm32yyxx.S + stm32/uart.c + stm32/uart_serial.c + stm32/write.cpp Tone.cpp WInterrupts.cpp wiring_analog.c diff --git a/cores/arduino/api b/cores/arduino/api index bce6000b5b..ed8a91e44a 160000 --- a/cores/arduino/api +++ b/cores/arduino/api @@ -1 +1 @@ -Subproject commit bce6000b5b1aaa6cc15374ecdc0ba5c80136e93c +Subproject commit ed8a91e44a43e67603a66c4f33338919c093a136 diff --git a/cores/arduino/main.cpp b/cores/arduino/main.cpp index 643dbd95ac..9096fde524 100644 --- a/cores/arduino/main.cpp +++ b/cores/arduino/main.cpp @@ -23,6 +23,7 @@ */ #define ARDUINO_MAIN +#include #include "Arduino.h" /** @@ -54,6 +55,25 @@ __attribute__((constructor(101))) void premain() init(); } +extern "C" { + /** + * @brief Halt on exit(). + * + * Replaces the C library exit(), which pulls in the stdio cleanup. That + * links _write() and, through it, the whole UART driver in every sketch, + * even when main() never returns. Weak, so a sketch can provide its own. + * + * @note The atexit() handlers and static destructors are not run and stdio + * buffers are not flushed. + * + * @param status Exit status, unused. + */ + WEAK __attribute__((noreturn)) void exit(int status) + { + _exit(status); + } +} + /** * @brief Main entry point of the Arduino application. * diff --git a/libraries/SrcWrapper/src/HardwareTimer.cpp b/cores/arduino/stm32/HardwareTimer.cpp similarity index 97% rename from libraries/SrcWrapper/src/HardwareTimer.cpp rename to cores/arduino/stm32/HardwareTimer.cpp index 147f082816..504bc350fb 100644 --- a/libraries/SrcWrapper/src/HardwareTimer.cpp +++ b/cores/arduino/stm32/HardwareTimer.cpp @@ -50,6 +50,44 @@ timerObj_t *HardwareTimer_Handle[TIMER_NUM] = {NULL}; #endif #endif +#if !defined(USE_HALV2_DRIVER) +/** + * @brief Timer MSP initialization: enable the timer clock, then set priority + * of and enable the update interrupt and, if different, the capture + * compare interrupt. + * Called before HAL_TIM_Base_Init() as the registers are accessed by the HAL. + * @param obj timer object + */ +static void timer_msp_init(timerObj_t *obj) +{ + TIM_TypeDef *instance = obj->handle.Instance; + enableTimerClock(instance); + + // configure Update interrupt + HAL_NVIC_SetPriority(getTimerUpIrq(instance), obj->preemptPriority, obj->subPriority); + HAL_NVIC_EnableIRQ(getTimerUpIrq(instance)); + + if (getTimerCCIrq(instance) != getTimerUpIrq(instance)) { + // configure Capture Compare interrupt + HAL_NVIC_SetPriority(getTimerCCIrq(instance), obj->preemptPriority, obj->subPriority); + HAL_NVIC_EnableIRQ(getTimerCCIrq(instance)); + } +} + +/** + * @brief Timer MSP de-initialization: disable the timer clock and interrupts. + * Called after HAL_TIM_Base_DeInit(). + * @param obj timer object + */ +static void timer_msp_deinit(timerObj_t *obj) +{ + TIM_TypeDef *instance = obj->handle.Instance; + disableTimerClock(instance); + HAL_NVIC_DisableIRQ(getTimerUpIrq(instance)); + HAL_NVIC_DisableIRQ(getTimerCCIrq(instance)); +} +#endif /* !USE_HALV2_DRIVER */ + HardwareTimer::HardwareTimer() { _timerObj.instance = nullptr; @@ -76,8 +114,7 @@ void HardwareTimer::setup(TIM_TypeDef *instance) _timerObj.preemptPriority = TIM_IRQ_PRIO; _timerObj.subPriority = TIM_IRQ_SUBPRIO; - /* Enable timer clock. Even if it is also done in HAL_TIM_Base_MspInit(), - it is done there so that it is possible to write registers right now */ + /* Enable timer clock so that it is possible to write registers right now */ enableTimerClock(instance); // Initialize NULL callbacks @@ -106,7 +143,7 @@ void HardwareTimer::setup(TIM_TypeDef *instance) // HAL_TIM_SetUpdateSource(&_timerObj.handle, HAL_TIM_UPDATE_REGULAR); // HAL_TIM_EnableUpdateGeneration(&_timerObj.handle); - // configure Update interrupt done in HAL_TIM_Base_MspInit for V1 + // configure Update interrupt done in timer_msp_init for V1 HAL_CORTEX_NVIC_SetPriority(getTimerUpIrq(_timerObj.instance), (hal_cortex_nvic_preemp_priority_t)_timerObj.preemptPriority, (hal_cortex_nvic_sub_priority_t)_timerObj.subPriority); @@ -140,6 +177,7 @@ void HardwareTimer::setup(TIM_TypeDef *instance) _timerObj.handle.Init.RepetitionCounter = 0; #endif _timerObj.handle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; + timer_msp_init(&_timerObj); HAL_TIM_Base_Init(&(_timerObj.handle)); #endif } @@ -1591,6 +1629,7 @@ void HardwareTimer::timerHandleDeinit() #else HAL_TIM_Base_Stop_IT(&(_timerObj.handle)); HAL_TIM_Base_DeInit(&(_timerObj.handle)); + timer_msp_deinit(&_timerObj); #endif } diff --git a/libraries/SrcWrapper/src/stm32/analog.cpp b/cores/arduino/stm32/analog.cpp similarity index 98% rename from libraries/SrcWrapper/src/stm32/analog.cpp rename to cores/arduino/stm32/analog.cpp index ffe9ec22b9..aaae3037e3 100644 --- a/libraries/SrcWrapper/src/stm32/analog.cpp +++ b/cores/arduino/stm32/analog.cpp @@ -548,7 +548,7 @@ uint32_t get_adc_internal_channel(PinName pin) * the GPIO pins. * @param hadc ADC handle pointer. */ -void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc) +static void adc_msp_init(ADC_HandleTypeDef *hadc) { /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* ADC Periph clock enable */ @@ -634,7 +634,7 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc) * @brief ADC MSP de-initialization: disable the peripheral clock. * @param hadc ADC handle pointer. */ -void HAL_ADC_MspDeInit(ADC_HandleTypeDef *hadc) +static void adc_msp_deinit(ADC_HandleTypeDef *hadc) { #ifdef __HAL_RCC_ADC_FORCE_RESET __HAL_RCC_ADC_FORCE_RESET(); @@ -978,8 +978,9 @@ uint16_t adc_read_value(PinName pin, uint32_t resolution) AdcHandle.Lock = HAL_UNLOCKED; /* Some other ADC_HandleTypeDef fields exists but not required */ - g_current_pin = pin; /* Needed for HAL_ADC_MspInit*/ + g_current_pin = pin; /* Needed for adc_msp_init */ + adc_msp_init(&AdcHandle); if (HAL_ADC_Init(&AdcHandle) != HAL_OK) { return 0; } @@ -1115,7 +1116,9 @@ uint16_t adc_read_value(PinName pin, uint32_t resolution) return 0; } - if (HAL_ADC_DeInit(&AdcHandle) != HAL_OK) { + HAL_StatusTypeDef deinit_status = HAL_ADC_DeInit(&AdcHandle); + adc_msp_deinit(&AdcHandle); + if (deinit_status != HAL_OK) { return 0; } @@ -1293,7 +1296,7 @@ uint32_t get_dac_channel(PinName pin) * the GPIO pins. * @param hdac DAC handle pointer. */ -void HAL_DAC_MspInit(DAC_HandleTypeDef *hdac) +static void dac_msp_init(DAC_HandleTypeDef *hdac) { /* DAC Periph clock enable */ if (hdac->Instance == DAC1) { @@ -1358,6 +1361,7 @@ void dac_write_value(PinName pin, uint32_t value, bool do_init) if (do_init) { /*##-1- Configure the DAC peripheral #######################################*/ g_current_pin = pin; + dac_msp_init(&DacHandle); if (HAL_DAC_Init(&DacHandle) != HAL_OK) { /* Initialization Error */ return; @@ -1415,7 +1419,7 @@ void dac_write_value(PinName pin, uint32_t value, bool do_init) * @brief DAC MSP de-initialization: disable the peripheral clock. * @param hdac DAC handle pointer. */ -void HAL_DAC_MspDeInit(DAC_HandleTypeDef *hdac) +static void dac_msp_deinit(DAC_HandleTypeDef *hdac) { /* DAC Periph clock disable */ if (hdac->Instance == DAC1) { @@ -1517,7 +1521,9 @@ void dac_stop(PinName pin) HAL_DAC_Stop(&DacHandle, dacChannel); - if (HAL_DAC_DeInit(&DacHandle) != HAL_OK) { + HAL_StatusTypeDef deinit_status = HAL_DAC_DeInit(&DacHandle); + dac_msp_deinit(&DacHandle); + if (deinit_status != HAL_OK) { /* DeInitialization Error */ return; } diff --git a/libraries/SrcWrapper/src/stm32/interrupt.cpp b/cores/arduino/stm32/interrupt.cpp similarity index 100% rename from libraries/SrcWrapper/src/stm32/interrupt.cpp rename to cores/arduino/stm32/interrupt.cpp diff --git a/libraries/SrcWrapper/src/stm32/uart.c b/cores/arduino/stm32/uart.c similarity index 72% rename from libraries/SrcWrapper/src/stm32/uart.c rename to cores/arduino/stm32/uart.c index a2d1289342..97429714c8 100644 --- a/libraries/SrcWrapper/src/stm32/uart.c +++ b/cores/arduino/stm32/uart.c @@ -13,6 +13,7 @@ #include "core_debug.h" #include "lock_resource.h" #include "uart.h" +#include "uart_private.h" #include "Arduino.h" #include "PinAF_STM32F1.h" #include "stm32yyxx_ll_rcc.h" @@ -37,60 +38,10 @@ extern "C" { #define DEBUG_UART_BAUDRATE 9600 #endif -/* @brief uart characteristics */ -typedef enum { -#if defined(USART1_BASE) - UART1_INDEX, -#endif -#if defined(USART2_BASE) - UART2_INDEX, -#endif -#if defined(USART3_BASE) - UART3_INDEX, -#endif -#if defined(UART4_BASE) || defined(USART4_BASE) - UART4_INDEX, -#endif -#if defined(UART5_BASE) || defined(USART5_BASE) - UART5_INDEX, -#endif -#if defined(USART6_BASE) - UART6_INDEX, -#endif -#if defined(UART7_BASE) || defined(USART7_BASE) - UART7_INDEX, -#endif -#if defined(UART8_BASE) || defined(USART8_BASE) - UART8_INDEX, -#endif -#if defined(UART9_BASE) - UART9_INDEX, -#endif -#if defined(UART10_BASE) || defined(USART10_BASE) - UART10_INDEX, -#endif -#if defined(USART11_BASE) - UART11_INDEX, -#endif -#if defined(UART12_BASE) - UART12_INDEX, -#endif -#if defined(LPUART1_BASE) - LPUART1_INDEX, -#endif -#if defined(LPUART2_BASE) - LPUART2_INDEX, -#endif -#if defined(LPUART3_BASE) - LPUART3_INDEX, -#endif - UART_NUM -} uart_index_t; - #if defined(USE_HALV2_DRIVER) -static hal_uart_handle_t *uart_handlers[UART_NUM] = {NULL}; +hal_uart_handle_t *uart_handlers[UART_NUM] = {NULL}; #else -static UART_HandleTypeDef *uart_handlers[UART_NUM] = {NULL}; +UART_HandleTypeDef *uart_handlers[UART_NUM] = {NULL}; #endif static serial_t serial_debug = { @@ -1009,15 +960,6 @@ size_t uart_debug_write(uint8_t *data, uint32_t size) return size; } -uint8_t serial_rx_active(serial_t *obj) -{ -#if defined(USE_HALV2_DRIVER) - return ((HAL_UART_GetRxState(uart_handlers[obj->index]) & HAL_UART_RX_STATE_ACTIVE) == HAL_UART_RX_STATE_ACTIVE); -#else - return ((HAL_UART_GetState(uart_handlers[obj->index]) & HAL_UART_STATE_BUSY_RX) == HAL_UART_STATE_BUSY_RX); -#endif -} - uint8_t serial_tx_active(serial_t *obj) { #if defined(USE_HALV2_DRIVER) @@ -1027,99 +969,6 @@ uint8_t serial_tx_active(serial_t *obj) #endif } -int uart_getc(serial_t *obj, unsigned char *c) -{ - if (obj == NULL) { - return -1; - } - - if (serial_rx_active(obj)) { - return -1; /* Transaction ongoing */ - } - - *c = (unsigned char)(obj->recv); - /* Restart RX irq */ - HAL_UART_Receive_IT(uart_handlers[obj->index], &(obj->recv), 1); - - return 0; -} - -void uart_attach_rx_callback(serial_t *obj, void (*callback)(serial_t *)) -{ - if (obj == NULL) { - return; - } - - /* Exit if a reception is already on-going */ - if (serial_rx_active(obj)) { - return; - } - obj->rx_callback = callback; - - /* Must disable interrupt to prevent handle lock contention */ -#if defined(USE_HALV2_DRIVER) - HAL_CORTEX_NVIC_DisableIRQ(obj->irq); -#else - HAL_NVIC_DisableIRQ(obj->irq); -#endif - - HAL_UART_Receive_IT(uart_handlers[obj->index], &(obj->recv), 1); - - /* Enable interrupt */ -#if defined(USE_HALV2_DRIVER) - HAL_CORTEX_NVIC_EnableIRQ(obj->irq); -#else - HAL_NVIC_EnableIRQ(obj->irq); -#endif -} - -void uart_attach_tx_callback(serial_t *obj, int (*callback)(serial_t *), size_t size) -{ - if (obj == NULL) { - return; - } - obj->tx_callback = callback; - - /* Must disable interrupt to prevent handle lock contention */ -#if defined(USE_HALV2_DRIVER) - HAL_CORTEX_NVIC_DisableIRQ(obj->irq); -#else - HAL_NVIC_DisableIRQ(obj->irq); -#endif - - /* The following function will enable UART_IT_TXE and error interrupts */ - HAL_UART_Transmit_IT(uart_handlers[obj->index], &obj->tx_buff[obj->tx_tail], size); - - /* Enable interrupt */ -#if defined(USE_HALV2_DRIVER) - HAL_CORTEX_NVIC_EnableIRQ(obj->irq); -#else - HAL_NVIC_EnableIRQ(obj->irq); -#endif -} - -void uart_enable_tx(serial_t *obj) -{ - if (obj != NULL && obj->pin_rx == NC) { -#if defined(USE_HALV2_DRIVER) - HAL_UART_EnableTransmitter(uart_handlers[obj->index]); -#else - HAL_HalfDuplex_EnableTransmitter(uart_handlers[obj->index]); -#endif - } -} - -void uart_enable_rx(serial_t *obj) -{ - if (obj != NULL && obj->pin_rx == NC) { -#if defined(USE_HALV2_DRIVER) - HAL_UART_EnableReceiver(uart_handlers[obj->index]); -#else - HAL_HalfDuplex_EnableReceiver(uart_handlers[obj->index]); -#endif - } -} - /* * Unused: return the index of the serial handler (UART_NUM if not found) * @@ -1140,382 +989,6 @@ uint8_t uart_index(UART_HandleTypeDef *huart) } */ -/** - * @brief Rx Transfer completed callback: call the RX callback of the serial object - * @param huart Pointer on the uart reference - * @param size_byte Number of received bytes (HALv2 driver only, unused) - * @param rx_event Reception event type (HALv2 driver only, unused) - */ -#if defined(USE_HALV2_DRIVER) -void HAL_UART_RxCpltCallback(hal_uart_handle_t *huart, uint32_t size_byte, hal_uart_rx_event_types_t rx_event) -#else -void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) -#endif -{ - serial_t *obj = get_serial_obj(huart); - if (obj) { -#if defined(USE_HALV2_DRIVER) - (void)size_byte; - (void)rx_event; -#endif - obj->rx_callback(obj); - } -} - -/** - * @brief Tx Transfer completed callback: call the TX callback of the serial object - * @param huart Pointer on the uart reference - */ -#if defined(USE_HALV2_DRIVER) -void HAL_UART_TxCpltCallback(hal_uart_handle_t *huart) -#else -void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) -#endif -{ - serial_t *obj = get_serial_obj(huart); - if (obj) { - obj->tx_callback(obj); - } -} - -/** - * @brief Error callback from UART: clear the error flag and restart the reception - * @param huart Pointer on the uart reference - */ -#if defined(USE_HALV2_DRIVER) -void HAL_UART_ErrorCallback(hal_uart_handle_t *huart) -#else -void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) -#endif -{ -#if defined(USE_HALV2_DRIVER) - USART_TypeDef *p_uartx = ((USART_TypeDef *)((uint32_t)(huart)->instance)); - if (LL_USART_IsActiveFlag_PE(p_uartx)) { - LL_USART_ClearFlag_PE(p_uartx); /* Clear PE flag */ - } else if (LL_USART_IsActiveFlag_FE(p_uartx)) { - LL_USART_ClearFlag_FE(p_uartx); /* Clear FE flag */ - } else if (LL_USART_IsActiveFlag_NE(p_uartx)) { - LL_USART_ClearFlag_NE(p_uartx); /* Clear NE flag */ - } else if (LL_USART_IsActiveFlag_ORE(p_uartx)) { - LL_USART_ClearFlag_ORE(p_uartx); /* Clear ORE flag */ - } -#else -#if defined(STM32F1xx) || defined(STM32F2xx) || defined(STM32F4xx) || defined(STM32L1xx) - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_PE) != RESET) { - __HAL_UART_CLEAR_PEFLAG(huart); /* Clear PE flag */ - } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_FE) != RESET) { - __HAL_UART_CLEAR_FEFLAG(huart); /* Clear FE flag */ - } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_NE) != RESET) { - __HAL_UART_CLEAR_NEFLAG(huart); /* Clear NE flag */ - } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { - __HAL_UART_CLEAR_OREFLAG(huart); /* Clear ORE flag */ - } -#else - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_PE) != RESET) { - __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_PEF); /* Clear PE flag */ - } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_FE) != RESET) { - __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_FEF); /* Clear FE flag */ - } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_NE) != RESET) { - __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_NEF); /* Clear NE flag */ - } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { - __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF); /* Clear ORE flag */ - } -#endif -#endif /* USE_HALV2_DRIVER */ - /* Restart receive interrupt after any error */ - serial_t *obj = get_serial_obj(huart); - if (obj && !serial_rx_active(obj)) { - HAL_UART_Receive_IT(huart, &(obj->recv), 1); - } -} - -/** - * @brief USART 1 IRQ handler - */ -#if defined(USART1_BASE) -void USART1_IRQHandler(void) -{ -#if defined(USE_HALV2_DRIVER) - HAL_CORTEX_NVIC_ClearPendingIRQ(USART1_IRQn); -#else - HAL_NVIC_ClearPendingIRQ(USART1_IRQn); -#endif - HAL_UART_IRQHandler(uart_handlers[UART1_INDEX]); -} -#endif - -/** - * @brief USART 2 IRQ handler - */ -#if defined(USART2_BASE) -void USART2_IRQHandler(void) -{ -#if defined(USE_HALV2_DRIVER) - HAL_CORTEX_NVIC_ClearPendingIRQ(USART2_IRQn); -#else - HAL_NVIC_ClearPendingIRQ(USART2_IRQn); -#endif - if (uart_handlers[UART2_INDEX] != NULL) { - HAL_UART_IRQHandler(uart_handlers[UART2_INDEX]); - } -#if (defined(STM32G0xx) || defined(STM32U0xx)) && defined(LPUART2_BASE) - if (uart_handlers[LPUART2_INDEX] != NULL) { - HAL_UART_IRQHandler(uart_handlers[LPUART2_INDEX]); - } -#endif -} -#endif - -/** - * @brief USART 3 IRQ handler - */ -#if defined(USART3_BASE) -void USART3_IRQHandler(void) -{ -#if defined(USE_HALV2_DRIVER) - HAL_CORTEX_NVIC_ClearPendingIRQ(USART3_IRQn); -#else - HAL_NVIC_ClearPendingIRQ(USART3_IRQn); -#endif -#if defined(STM32F091xC) || defined (STM32F098xx) - if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART3) != RESET) { - HAL_UART_IRQHandler(uart_handlers[UART3_INDEX]); - } - if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART4) != RESET) { - HAL_UART_IRQHandler(uart_handlers[UART4_INDEX]); - } - if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART5) != RESET) { - HAL_UART_IRQHandler(uart_handlers[UART5_INDEX]); - } - if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART6) != RESET) { - HAL_UART_IRQHandler(uart_handlers[UART6_INDEX]); - } - if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART7) != RESET) { - HAL_UART_IRQHandler(uart_handlers[UART7_INDEX]); - } - if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART8) != RESET) { - HAL_UART_IRQHandler(uart_handlers[UART8_INDEX]); - } -#else - if (uart_handlers[UART3_INDEX] != NULL) { - HAL_UART_IRQHandler(uart_handlers[UART3_INDEX]); - } -#if defined(STM32F0xx) || defined(STM32G0xx) || defined(STM32U0xx) - /* USART3_4_IRQn */ - if (uart_handlers[UART4_INDEX] != NULL) { - HAL_UART_IRQHandler(uart_handlers[UART4_INDEX]); - } -#if defined(STM32F030xC) || defined(STM32G0xx) && (defined(LPUART2_BASE) || defined(USART5_BASE)) - if (uart_handlers[UART5_INDEX] != NULL) { - HAL_UART_IRQHandler(uart_handlers[UART5_INDEX]); - } - if (uart_handlers[UART6_INDEX] != NULL) { - HAL_UART_IRQHandler(uart_handlers[UART6_INDEX]); - } -#endif /* STM32F030xC */ -#if (defined(STM32G0xx) || STM32U0xx) && defined(LPUART1_BASE) - if (uart_handlers[LPUART1_INDEX] != NULL) { - HAL_UART_IRQHandler(uart_handlers[LPUART1_INDEX]); - } -#endif /* (STM32G0xx || STM32U0xx) && LPUART1_BASE */ -#endif /* STM32F0xx || STM32G0xx || STM32U0xx */ -#endif /* STM32F091xC || STM32F098xx */ -} -#endif - -/** - * @brief UART 4 IRQ handler - */ -#if defined(UART4_BASE) -void UART4_IRQHandler(void) -{ -#if defined(USE_HALV2_DRIVER) - HAL_CORTEX_NVIC_ClearPendingIRQ(UART4_IRQn); -#else - HAL_NVIC_ClearPendingIRQ(UART4_IRQn); -#endif - HAL_UART_IRQHandler(uart_handlers[UART4_INDEX]); -} -#endif - -/** - * @brief USART 4/5 IRQ handler - */ -#if defined(STM32L0xx) -#if defined(USART4_BASE) || defined(USART5_BASE) -void USART4_5_IRQHandler(void) -{ - HAL_NVIC_ClearPendingIRQ(USART4_IRQn); - if (uart_handlers[UART4_INDEX] != NULL) { - HAL_UART_IRQHandler(uart_handlers[UART4_INDEX]); - } - if (uart_handlers[UART5_INDEX] != NULL) { - HAL_UART_IRQHandler(uart_handlers[UART5_INDEX]); - } -} -#endif -#endif - -/** - * @brief USART 4 IRQ handler - */ -#if defined(STM32U0xx) -#if defined(USART4_BASE) -void USART4_IRQHandler(void) -{ - HAL_NVIC_ClearPendingIRQ(USART4_IRQn); - if (uart_handlers[UART4_INDEX] != NULL) { - HAL_UART_IRQHandler(uart_handlers[UART4_INDEX]); - } -#if defined(LPUART3_BASE) - if (uart_handlers[LPUART3_INDEX] != NULL) { - HAL_UART_IRQHandler(uart_handlers[LPUART3_INDEX]); - } -#endif -} -#endif -#endif - -/** - * @brief USART 5 IRQ handler - */ -#if defined(UART5_BASE) -void UART5_IRQHandler(void) -{ -#if defined(USE_HALV2_DRIVER) - HAL_CORTEX_NVIC_ClearPendingIRQ(UART5_IRQn); -#else - HAL_NVIC_ClearPendingIRQ(UART5_IRQn); -#endif - HAL_UART_IRQHandler(uart_handlers[UART5_INDEX]); -} -#endif - -/** - * @brief USART 6 IRQ handler - */ -#if defined(USART6_BASE) && !defined(STM32F0xx) && !defined(STM32G0xx) -void USART6_IRQHandler(void) -{ -#if defined(USE_HALV2_DRIVER) - HAL_CORTEX_NVIC_ClearPendingIRQ(USART6_IRQn); -#else - HAL_NVIC_ClearPendingIRQ(USART6_IRQn); -#endif - HAL_UART_IRQHandler(uart_handlers[UART6_INDEX]); -} -#endif - -/** - * @brief LPUART 1 IRQ handler - */ -#if defined(LPUART1_BASE) -void LPUART1_IRQHandler(void) -{ -#if defined(USE_HALV2_DRIVER) - HAL_CORTEX_NVIC_ClearPendingIRQ(LPUART1_IRQn); -#else - HAL_NVIC_ClearPendingIRQ(LPUART1_IRQn); -#endif - HAL_UART_IRQHandler(uart_handlers[LPUART1_INDEX]); -} -#endif - -/** - * @brief UART 7 IRQ handler - */ -#if defined(UART7_BASE) -void UART7_IRQHandler(void) -{ -#if defined(USE_HALV2_DRIVER) - HAL_CORTEX_NVIC_ClearPendingIRQ(UART7_IRQn); -#else - HAL_NVIC_ClearPendingIRQ(UART7_IRQn); -#endif - HAL_UART_IRQHandler(uart_handlers[UART7_INDEX]); -} -#endif - -/** - * @brief UART 8 IRQ handler - */ -#if defined(UART8_BASE) -void UART8_IRQHandler(void) -{ - HAL_NVIC_ClearPendingIRQ(UART8_IRQn); - HAL_UART_IRQHandler(uart_handlers[UART8_INDEX]); -} -#endif - -/** - * @brief UART 9 IRQ handler - */ -#if defined(UART9_BASE) -void UART9_IRQHandler(void) -{ - HAL_NVIC_ClearPendingIRQ(UART9_IRQn); - HAL_UART_IRQHandler(uart_handlers[UART9_INDEX]); -} -#endif - -/** - * @brief UART 10 IRQ handler - */ -#if defined(UART10_BASE) -void UART10_IRQHandler(void) -{ - HAL_NVIC_ClearPendingIRQ(UART10_IRQn); - HAL_UART_IRQHandler(uart_handlers[UART10_INDEX]); -} -#endif - -/** - * @brief USART 10 IRQ handler - */ -#if defined(USART10_BASE) -void USART10_IRQHandler(void) -{ - HAL_NVIC_ClearPendingIRQ(USART10_IRQn); - HAL_UART_IRQHandler(uart_handlers[UART10_INDEX]); -} -#endif - -/** - * @brief USART 11 IRQ handler - */ -#if defined(USART11_BASE) -void USART11_IRQHandler(void) -{ - HAL_NVIC_ClearPendingIRQ(USART11_IRQn); - HAL_UART_IRQHandler(uart_handlers[UART11_INDEX]); -} -#endif - -/** - * @brief UART 12 IRQ handler - */ -#if defined(UART12_BASE) -void UART12_IRQHandler(void) -{ - HAL_NVIC_ClearPendingIRQ(UART12_IRQn); - HAL_UART_IRQHandler(uart_handlers[UART12_INDEX]); -} -#endif - -/** - * @brief HAL UART wakeup callback: restart the reception - * @param huart UART handle - */ -#if defined(USE_HALV2_DRIVER) -void HAL_UARTEx_WakeupCallback(hal_uart_handle_t *huart) -#else -void HAL_UARTEx_WakeupCallback(UART_HandleTypeDef *huart) -#endif -{ - serial_t *obj = get_serial_obj(huart); - HAL_UART_Receive_IT(huart, &(obj->recv), 1); -} - #if defined(USE_HALV2_DRIVER) hal_uart_prescaler_t uart_compute_prescaler(hal_uart_handle_t *huart, hal_uart_config_t *config) { diff --git a/cores/arduino/stm32/uart_private.h b/cores/arduino/stm32/uart_private.h new file mode 100644 index 0000000000..2d07248048 --- /dev/null +++ b/cores/arduino/stm32/uart_private.h @@ -0,0 +1,98 @@ +/* + ******************************************************************************* + * Copyright (c) 2016-2021, STMicroelectronics + * All rights reserved. + * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ******************************************************************************* + */ +/** + * @file uart_private.h + * @brief Definitions shared by uart.c and uart_serial.c, not part of the API. + */ +#ifndef _UART_PRIVATE_H_ +#define _UART_PRIVATE_H_ + +#include "uart.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#if defined(HAL_UART_MODULE_ENABLED) && !defined(HAL_UART_MODULE_ONLY) + +/* @brief uart characteristics */ +typedef enum { +#if defined(USART1_BASE) + UART1_INDEX, +#endif +#if defined(USART2_BASE) + UART2_INDEX, +#endif +#if defined(USART3_BASE) + UART3_INDEX, +#endif +#if defined(UART4_BASE) || defined(USART4_BASE) + UART4_INDEX, +#endif +#if defined(UART5_BASE) || defined(USART5_BASE) + UART5_INDEX, +#endif +#if defined(USART6_BASE) + UART6_INDEX, +#endif +#if defined(UART7_BASE) || defined(USART7_BASE) + UART7_INDEX, +#endif +#if defined(UART8_BASE) || defined(USART8_BASE) + UART8_INDEX, +#endif +#if defined(UART9_BASE) + UART9_INDEX, +#endif +#if defined(UART10_BASE) || defined(USART10_BASE) + UART10_INDEX, +#endif +#if defined(USART11_BASE) + UART11_INDEX, +#endif +#if defined(UART12_BASE) + UART12_INDEX, +#endif +#if defined(LPUART1_BASE) + LPUART1_INDEX, +#endif +#if defined(LPUART2_BASE) + LPUART2_INDEX, +#endif +#if defined(LPUART3_BASE) + LPUART3_INDEX, +#endif + UART_NUM +} uart_index_t; + +/* HAL handles of the initialized U(S)ART, indexed by uart_index_t */ +#if defined(USE_HALV2_DRIVER) +extern hal_uart_handle_t *uart_handlers[UART_NUM]; +#else +extern UART_HandleTypeDef *uart_handlers[UART_NUM]; +#endif + +/* Get the serial_t object from its HAL UART handle, defined in uart.c */ +#if defined(USE_HALV2_DRIVER) +serial_t *get_serial_obj(hal_uart_handle_t *huart); +#else +serial_t *get_serial_obj(UART_HandleTypeDef *huart); +#endif + +#endif /* HAL_UART_MODULE_ENABLED && !HAL_UART_MODULE_ONLY */ + +#ifdef __cplusplus +} +#endif + +#endif /* _UART_PRIVATE_H_ */ diff --git a/cores/arduino/stm32/uart_serial.c b/cores/arduino/stm32/uart_serial.c new file mode 100644 index 0000000000..59ac4fd4f6 --- /dev/null +++ b/cores/arduino/stm32/uart_serial.c @@ -0,0 +1,516 @@ +/* + ******************************************************************************* + * Copyright (c) 2016-2021, STMicroelectronics + * All rights reserved. + * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ******************************************************************************* + */ +/** + * @file uart_serial.c + * @brief Interrupt driven part of the U(S)ART driver, used by HardwareSerial: + * reception and transmission, HAL callbacks and IRQ handlers. + * + * Kept apart from uart.c, so a sketch which only uses the debug UART + * (core logs, printf()) does not link the strong USARTx_IRQHandler() and can + * define its own. This file is a member of core.a, it is extracted when + * HardwareSerial references one of its functions. + */ +#include "Arduino.h" +#include "uart.h" +#include "uart_private.h" + +#ifdef __cplusplus +extern "C" { +#endif +#if defined(HAL_UART_MODULE_ENABLED) && !defined(HAL_UART_MODULE_ONLY) + +uint8_t serial_rx_active(serial_t *obj) +{ +#if defined(USE_HALV2_DRIVER) + return ((HAL_UART_GetRxState(uart_handlers[obj->index]) & HAL_UART_RX_STATE_ACTIVE) == HAL_UART_RX_STATE_ACTIVE); +#else + return ((HAL_UART_GetState(uart_handlers[obj->index]) & HAL_UART_STATE_BUSY_RX) == HAL_UART_STATE_BUSY_RX); +#endif +} + +int uart_getc(serial_t *obj, unsigned char *c) +{ + if (obj == NULL) { + return -1; + } + + if (serial_rx_active(obj)) { + return -1; /* Transaction ongoing */ + } + + *c = (unsigned char)(obj->recv); + /* Restart RX irq */ + HAL_UART_Receive_IT(uart_handlers[obj->index], &(obj->recv), 1); + + return 0; +} + +void uart_attach_rx_callback(serial_t *obj, void (*callback)(serial_t *)) +{ + if (obj == NULL) { + return; + } + + /* Exit if a reception is already on-going */ + if (serial_rx_active(obj)) { + return; + } + obj->rx_callback = callback; + + /* Must disable interrupt to prevent handle lock contention */ +#if defined(USE_HALV2_DRIVER) + HAL_CORTEX_NVIC_DisableIRQ(obj->irq); +#else + HAL_NVIC_DisableIRQ(obj->irq); +#endif + + HAL_UART_Receive_IT(uart_handlers[obj->index], &(obj->recv), 1); + + /* Enable interrupt */ +#if defined(USE_HALV2_DRIVER) + HAL_CORTEX_NVIC_EnableIRQ(obj->irq); +#else + HAL_NVIC_EnableIRQ(obj->irq); +#endif +} + +void uart_attach_tx_callback(serial_t *obj, int (*callback)(serial_t *), size_t size) +{ + if (obj == NULL) { + return; + } + obj->tx_callback = callback; + + /* Must disable interrupt to prevent handle lock contention */ +#if defined(USE_HALV2_DRIVER) + HAL_CORTEX_NVIC_DisableIRQ(obj->irq); +#else + HAL_NVIC_DisableIRQ(obj->irq); +#endif + + /* The following function will enable UART_IT_TXE and error interrupts */ + HAL_UART_Transmit_IT(uart_handlers[obj->index], &obj->tx_buff[obj->tx_tail], size); + + /* Enable interrupt */ +#if defined(USE_HALV2_DRIVER) + HAL_CORTEX_NVIC_EnableIRQ(obj->irq); +#else + HAL_NVIC_EnableIRQ(obj->irq); +#endif +} + +void uart_enable_tx(serial_t *obj) +{ + if (obj != NULL && obj->pin_rx == NC) { +#if defined(USE_HALV2_DRIVER) + HAL_UART_EnableTransmitter(uart_handlers[obj->index]); +#else + HAL_HalfDuplex_EnableTransmitter(uart_handlers[obj->index]); +#endif + } +} + +void uart_enable_rx(serial_t *obj) +{ + if (obj != NULL && obj->pin_rx == NC) { +#if defined(USE_HALV2_DRIVER) + HAL_UART_EnableReceiver(uart_handlers[obj->index]); +#else + HAL_HalfDuplex_EnableReceiver(uart_handlers[obj->index]); +#endif + } +} + +/** + * @brief Rx Transfer completed callback: call the RX callback of the serial object + * @param huart Pointer on the uart reference + * @param size_byte Number of received bytes (HALv2 driver only, unused) + * @param rx_event Reception event type (HALv2 driver only, unused) + */ +#if defined(USE_HALV2_DRIVER) +void HAL_UART_RxCpltCallback(hal_uart_handle_t *huart, uint32_t size_byte, hal_uart_rx_event_types_t rx_event) +#else +void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) +#endif +{ + serial_t *obj = get_serial_obj(huart); + if (obj) { +#if defined(USE_HALV2_DRIVER) + (void)size_byte; + (void)rx_event; +#endif + obj->rx_callback(obj); + } +} + +/** + * @brief Tx Transfer completed callback: call the TX callback of the serial object + * @param huart Pointer on the uart reference + */ +#if defined(USE_HALV2_DRIVER) +void HAL_UART_TxCpltCallback(hal_uart_handle_t *huart) +#else +void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) +#endif +{ + serial_t *obj = get_serial_obj(huart); + if (obj) { + obj->tx_callback(obj); + } +} + +/** + * @brief Error callback from UART: clear the error flag and restart the reception + * @param huart Pointer on the uart reference + */ +#if defined(USE_HALV2_DRIVER) +void HAL_UART_ErrorCallback(hal_uart_handle_t *huart) +#else +void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) +#endif +{ +#if defined(USE_HALV2_DRIVER) + USART_TypeDef *p_uartx = ((USART_TypeDef *)((uint32_t)(huart)->instance)); + if (LL_USART_IsActiveFlag_PE(p_uartx)) { + LL_USART_ClearFlag_PE(p_uartx); /* Clear PE flag */ + } else if (LL_USART_IsActiveFlag_FE(p_uartx)) { + LL_USART_ClearFlag_FE(p_uartx); /* Clear FE flag */ + } else if (LL_USART_IsActiveFlag_NE(p_uartx)) { + LL_USART_ClearFlag_NE(p_uartx); /* Clear NE flag */ + } else if (LL_USART_IsActiveFlag_ORE(p_uartx)) { + LL_USART_ClearFlag_ORE(p_uartx); /* Clear ORE flag */ + } +#else +#if defined(STM32F1xx) || defined(STM32F2xx) || defined(STM32F4xx) || defined(STM32L1xx) + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_PE) != RESET) { + __HAL_UART_CLEAR_PEFLAG(huart); /* Clear PE flag */ + } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_FE) != RESET) { + __HAL_UART_CLEAR_FEFLAG(huart); /* Clear FE flag */ + } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_NE) != RESET) { + __HAL_UART_CLEAR_NEFLAG(huart); /* Clear NE flag */ + } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { + __HAL_UART_CLEAR_OREFLAG(huart); /* Clear ORE flag */ + } +#else + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_PE) != RESET) { + __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_PEF); /* Clear PE flag */ + } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_FE) != RESET) { + __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_FEF); /* Clear FE flag */ + } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_NE) != RESET) { + __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_NEF); /* Clear NE flag */ + } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { + __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF); /* Clear ORE flag */ + } +#endif +#endif /* USE_HALV2_DRIVER */ + /* Restart receive interrupt after any error */ + serial_t *obj = get_serial_obj(huart); + if (obj && !serial_rx_active(obj)) { + HAL_UART_Receive_IT(huart, &(obj->recv), 1); + } +} + +/** + * @brief HAL UART wakeup callback: restart the reception + * @param huart UART handle + */ +#if defined(USE_HALV2_DRIVER) +void HAL_UARTEx_WakeupCallback(hal_uart_handle_t *huart) +#else +void HAL_UARTEx_WakeupCallback(UART_HandleTypeDef *huart) +#endif +{ + serial_t *obj = get_serial_obj(huart); + HAL_UART_Receive_IT(huart, &(obj->recv), 1); +} + +/** + * @brief USART 1 IRQ handler + */ +#if defined(USART1_BASE) +void USART1_IRQHandler(void) +{ +#if defined(USE_HALV2_DRIVER) + HAL_CORTEX_NVIC_ClearPendingIRQ(USART1_IRQn); +#else + HAL_NVIC_ClearPendingIRQ(USART1_IRQn); +#endif + HAL_UART_IRQHandler(uart_handlers[UART1_INDEX]); +} +#endif + +/** + * @brief USART 2 IRQ handler + */ +#if defined(USART2_BASE) +void USART2_IRQHandler(void) +{ +#if defined(USE_HALV2_DRIVER) + HAL_CORTEX_NVIC_ClearPendingIRQ(USART2_IRQn); +#else + HAL_NVIC_ClearPendingIRQ(USART2_IRQn); +#endif + if (uart_handlers[UART2_INDEX] != NULL) { + HAL_UART_IRQHandler(uart_handlers[UART2_INDEX]); + } +#if (defined(STM32G0xx) || defined(STM32U0xx)) && defined(LPUART2_BASE) + if (uart_handlers[LPUART2_INDEX] != NULL) { + HAL_UART_IRQHandler(uart_handlers[LPUART2_INDEX]); + } +#endif +} +#endif + +/** + * @brief USART 3 IRQ handler + */ +#if defined(USART3_BASE) +void USART3_IRQHandler(void) +{ +#if defined(USE_HALV2_DRIVER) + HAL_CORTEX_NVIC_ClearPendingIRQ(USART3_IRQn); +#else + HAL_NVIC_ClearPendingIRQ(USART3_IRQn); +#endif +#if defined(STM32F091xC) || defined (STM32F098xx) + if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART3) != RESET) { + HAL_UART_IRQHandler(uart_handlers[UART3_INDEX]); + } + if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART4) != RESET) { + HAL_UART_IRQHandler(uart_handlers[UART4_INDEX]); + } + if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART5) != RESET) { + HAL_UART_IRQHandler(uart_handlers[UART5_INDEX]); + } + if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART6) != RESET) { + HAL_UART_IRQHandler(uart_handlers[UART6_INDEX]); + } + if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART7) != RESET) { + HAL_UART_IRQHandler(uart_handlers[UART7_INDEX]); + } + if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART8) != RESET) { + HAL_UART_IRQHandler(uart_handlers[UART8_INDEX]); + } +#else + if (uart_handlers[UART3_INDEX] != NULL) { + HAL_UART_IRQHandler(uart_handlers[UART3_INDEX]); + } +#if defined(STM32F0xx) || defined(STM32G0xx) || defined(STM32U0xx) + /* USART3_4_IRQn */ + if (uart_handlers[UART4_INDEX] != NULL) { + HAL_UART_IRQHandler(uart_handlers[UART4_INDEX]); + } +#if defined(STM32F030xC) || defined(STM32G0xx) && (defined(LPUART2_BASE) || defined(USART5_BASE)) + if (uart_handlers[UART5_INDEX] != NULL) { + HAL_UART_IRQHandler(uart_handlers[UART5_INDEX]); + } + if (uart_handlers[UART6_INDEX] != NULL) { + HAL_UART_IRQHandler(uart_handlers[UART6_INDEX]); + } +#endif /* STM32F030xC */ +#if (defined(STM32G0xx) || STM32U0xx) && defined(LPUART1_BASE) + if (uart_handlers[LPUART1_INDEX] != NULL) { + HAL_UART_IRQHandler(uart_handlers[LPUART1_INDEX]); + } +#endif /* (STM32G0xx || STM32U0xx) && LPUART1_BASE */ +#endif /* STM32F0xx || STM32G0xx || STM32U0xx */ +#endif /* STM32F091xC || STM32F098xx */ +} +#endif + +/** + * @brief UART 4 IRQ handler + */ +#if defined(UART4_BASE) +void UART4_IRQHandler(void) +{ +#if defined(USE_HALV2_DRIVER) + HAL_CORTEX_NVIC_ClearPendingIRQ(UART4_IRQn); +#else + HAL_NVIC_ClearPendingIRQ(UART4_IRQn); +#endif + HAL_UART_IRQHandler(uart_handlers[UART4_INDEX]); +} +#endif + +/** + * @brief USART 4/5 IRQ handler + */ +#if defined(STM32L0xx) +#if defined(USART4_BASE) || defined(USART5_BASE) +void USART4_5_IRQHandler(void) +{ + HAL_NVIC_ClearPendingIRQ(USART4_IRQn); + if (uart_handlers[UART4_INDEX] != NULL) { + HAL_UART_IRQHandler(uart_handlers[UART4_INDEX]); + } + if (uart_handlers[UART5_INDEX] != NULL) { + HAL_UART_IRQHandler(uart_handlers[UART5_INDEX]); + } +} +#endif +#endif + +/** + * @brief USART 4 IRQ handler + */ +#if defined(STM32U0xx) +#if defined(USART4_BASE) +void USART4_IRQHandler(void) +{ + HAL_NVIC_ClearPendingIRQ(USART4_IRQn); + if (uart_handlers[UART4_INDEX] != NULL) { + HAL_UART_IRQHandler(uart_handlers[UART4_INDEX]); + } +#if defined(LPUART3_BASE) + if (uart_handlers[LPUART3_INDEX] != NULL) { + HAL_UART_IRQHandler(uart_handlers[LPUART3_INDEX]); + } +#endif +} +#endif +#endif + +/** + * @brief USART 5 IRQ handler + */ +#if defined(UART5_BASE) +void UART5_IRQHandler(void) +{ +#if defined(USE_HALV2_DRIVER) + HAL_CORTEX_NVIC_ClearPendingIRQ(UART5_IRQn); +#else + HAL_NVIC_ClearPendingIRQ(UART5_IRQn); +#endif + HAL_UART_IRQHandler(uart_handlers[UART5_INDEX]); +} +#endif + +/** + * @brief USART 6 IRQ handler + */ +#if defined(USART6_BASE) && !defined(STM32F0xx) && !defined(STM32G0xx) +void USART6_IRQHandler(void) +{ +#if defined(USE_HALV2_DRIVER) + HAL_CORTEX_NVIC_ClearPendingIRQ(USART6_IRQn); +#else + HAL_NVIC_ClearPendingIRQ(USART6_IRQn); +#endif + HAL_UART_IRQHandler(uart_handlers[UART6_INDEX]); +} +#endif + +/** + * @brief LPUART 1 IRQ handler + */ +#if defined(LPUART1_BASE) +void LPUART1_IRQHandler(void) +{ +#if defined(USE_HALV2_DRIVER) + HAL_CORTEX_NVIC_ClearPendingIRQ(LPUART1_IRQn); +#else + HAL_NVIC_ClearPendingIRQ(LPUART1_IRQn); +#endif + HAL_UART_IRQHandler(uart_handlers[LPUART1_INDEX]); +} +#endif + +/** + * @brief UART 7 IRQ handler + */ +#if defined(UART7_BASE) +void UART7_IRQHandler(void) +{ +#if defined(USE_HALV2_DRIVER) + HAL_CORTEX_NVIC_ClearPendingIRQ(UART7_IRQn); +#else + HAL_NVIC_ClearPendingIRQ(UART7_IRQn); +#endif + HAL_UART_IRQHandler(uart_handlers[UART7_INDEX]); +} +#endif + +/** + * @brief UART 8 IRQ handler + */ +#if defined(UART8_BASE) +void UART8_IRQHandler(void) +{ + HAL_NVIC_ClearPendingIRQ(UART8_IRQn); + HAL_UART_IRQHandler(uart_handlers[UART8_INDEX]); +} +#endif + +/** + * @brief UART 9 IRQ handler + */ +#if defined(UART9_BASE) +void UART9_IRQHandler(void) +{ + HAL_NVIC_ClearPendingIRQ(UART9_IRQn); + HAL_UART_IRQHandler(uart_handlers[UART9_INDEX]); +} +#endif + +/** + * @brief UART 10 IRQ handler + */ +#if defined(UART10_BASE) +void UART10_IRQHandler(void) +{ + HAL_NVIC_ClearPendingIRQ(UART10_IRQn); + HAL_UART_IRQHandler(uart_handlers[UART10_INDEX]); +} +#endif + +/** + * @brief USART 10 IRQ handler + */ +#if defined(USART10_BASE) +void USART10_IRQHandler(void) +{ + HAL_NVIC_ClearPendingIRQ(USART10_IRQn); + HAL_UART_IRQHandler(uart_handlers[UART10_INDEX]); +} +#endif + +/** + * @brief USART 11 IRQ handler + */ +#if defined(USART11_BASE) +void USART11_IRQHandler(void) +{ + HAL_NVIC_ClearPendingIRQ(USART11_IRQn); + HAL_UART_IRQHandler(uart_handlers[UART11_INDEX]); +} +#endif + +/** + * @brief UART 12 IRQ handler + */ +#if defined(UART12_BASE) +void UART12_IRQHandler(void) +{ + HAL_NVIC_ClearPendingIRQ(UART12_IRQn); + HAL_UART_IRQHandler(uart_handlers[UART12_INDEX]); +} +#endif + +#endif /* HAL_UART_MODULE_ENABLED && !HAL_UART_MODULE_ONLY */ + +#ifdef __cplusplus +} +#endif + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/cores/arduino/stm32/write.cpp b/cores/arduino/stm32/write.cpp new file mode 100644 index 0000000000..367dbd7bc6 --- /dev/null +++ b/cores/arduino/stm32/write.cpp @@ -0,0 +1,55 @@ +/** + * \file write.cpp + * + * Implementation of the newlib _write() syscall, used by the printf() family, + * by Print::printf() and by core_debug(). + * + */ + +#include +#include "Arduino.h" +#include "uart.h" +#if defined(VIRTIO_LOG) + #include "virtio_log.h" +#endif + +extern "C" { + /** + * @brief Write to a file descriptor, called by the C library. + * + * The output of stdout and stderr (printf(), core_debug()...) is sent to the + * debug UART by uart_debug_write(), which initializes it when it is not + * already used by a Serial instance. + * + * @note The UART driver is linked as soon as this function is requested + * by the C library: using printf() or enabling the core logs (NDEBUG not + * defined) links it, with its strong USARTx_IRQHandler(). + * @note Weak, a sketch can provide its own. + * + * @param file STDOUT_FILENO and STDERR_FILENO for the debug output, STDIN_FILENO + * is ignored, otherwise a pointer to a Print object (Print::printf()). + * @param ptr Data to write. + * @param len Number of bytes to write. + * @return len. + */ + __attribute__((weak)) + int _write(int file, char *ptr, int len) + { + switch (file) { + case STDOUT_FILENO: + case STDERR_FILENO: +#if defined(VIRTIO_LOG) + virtio_log((uint8_t *)ptr, (uint32_t)len); +#elif defined(HAL_UART_MODULE_ENABLED) && !defined(HAL_UART_MODULE_ONLY) + uart_debug_write((uint8_t *)ptr, (uint32_t)len); +#endif + break; + case STDIN_FILENO: + break; + default: + ((class Print *)file)->write((uint8_t *)ptr, len); + break; + } + return len; + } +} diff --git a/cores/arduino/wiring_analog.c b/cores/arduino/wiring_analog.c index 96b34f3c79..9502a5154c 100644 --- a/cores/arduino/wiring_analog.c +++ b/cores/arduino/wiring_analog.c @@ -33,9 +33,29 @@ extern "C" { (defined(HAL_DAC_MODULE_ENABLED) || (defined(USE_HAL_DAC_MODULE) && (USE_HAL_DAC_MODULE == 1))) ||\ (!defined(HAL_TIM_MODULE_ONLY) &&\ (defined(HAL_TIM_MODULE_ENABLED) || (defined(USE_HAL_TIM_MODULE) && (USE_HAL_TIM_MODULE == 1)))) -//This is the list of the IOs configured -uint32_t g_anOutputPinConfigured[MAX_NB_PORT] = {0}; +/** + * @brief Stop the analog output (PWM or DAC) of a pin. Called by pinMode(). + * @param pin Pin name. + */ +void analog_output_stop(PinName pin) +{ +#if !defined(HAL_DAC_MODULE_ONLY) &&\ + (defined(HAL_DAC_MODULE_ENABLED) || (defined(USE_HAL_DAC_MODULE) && (USE_HAL_DAC_MODULE == 1))) + if (pin_in_pinmap(pin, PinMap_DAC)) { + dac_stop(pin); + } else +#endif + { +#if !defined(HAL_TIM_MODULE_ONLY) &&\ + (defined(HAL_TIM_MODULE_ENABLED) || (defined(USE_HAL_TIM_MODULE) && (USE_HAL_TIM_MODULE == 1))) + if (pin_in_pinmap(pin, PinMap_TIM)) { + pwm_stop(pin); + } +#endif /* !HAL_TIM_MODULE_ONLY && (HAL_TIM_MODULE_ENABLED || (USE_HAL_TIM_MODULE && (USE_HAL_TIM_MODULE == 1))) */ + } +} #endif + #define MAX_PWM_RESOLUTION 16 static int _writeResolution = PWM_RESOLUTION; static int _internalWriteResolution = diff --git a/cores/arduino/wiring_digital.c b/cores/arduino/wiring_digital.c index b366891715..78ee8d5e6f 100644 --- a/cores/arduino/wiring_digital.c +++ b/cores/arduino/wiring_digital.c @@ -29,7 +29,16 @@ extern "C" { #endif -extern pin_size_t g_anOutputPinConfigured[MAX_NB_PORT]; +#if !defined(HAL_DAC_MODULE_ONLY) &&\ + (defined(HAL_DAC_MODULE_ENABLED) || (defined(USE_HAL_DAC_MODULE) && (USE_HAL_DAC_MODULE == 1))) ||\ + (!defined(HAL_TIM_MODULE_ONLY) &&\ + (defined(HAL_TIM_MODULE_ENABLED) || (defined(USE_HAL_TIM_MODULE) && (USE_HAL_TIM_MODULE == 1)))) +//This is the list of the IOs configured as analog output +uint32_t g_anOutputPinConfigured[MAX_NB_PORT] = {0}; +#endif + +// Weak: only defined (wiring_analog.c) when the sketch uses analogWrite() +void analog_output_stop(PinName pin) __attribute__((weak)); /** * @brief Set the mode of a pin. @@ -48,20 +57,11 @@ void pinMode(pin_size_t pinNumber, PinMode pinMode) // If the pin that support PWM or DAC output, we need to turn it off #if !defined(HAL_DAC_MODULE_ONLY) &&\ (defined(HAL_DAC_MODULE_ENABLED) || (defined(USE_HAL_DAC_MODULE) && (USE_HAL_DAC_MODULE == 1))) ||\ - (defined(HAL_TIM_MODULE_ENABLED) && !defined(HAL_TIM_MODULE_ONLY)) + (!defined(HAL_TIM_MODULE_ONLY) &&\ + (defined(HAL_TIM_MODULE_ENABLED) || (defined(USE_HAL_TIM_MODULE) && (USE_HAL_TIM_MODULE == 1)))) if (is_pin_configured(p, g_anOutputPinConfigured)) { -#if !defined(HAL_DAC_MODULE_ONLY) &&\ - (defined(HAL_DAC_MODULE_ENABLED) || (defined(USE_HAL_DAC_MODULE) && (USE_HAL_DAC_MODULE == 1))) - if (pin_in_pinmap(p, PinMap_DAC)) { - dac_stop(p); - } else -#endif - { -#if defined(HAL_TIM_MODULE_ENABLED) && !defined(HAL_TIM_MODULE_ONLY) - if (pin_in_pinmap(p, PinMap_TIM)) { - pwm_stop(p); - } -#endif //HAL_TIM_MODULE_ENABLED && !HAL_TIM_MODULE_ONLY + if (analog_output_stop != NULL) { + analog_output_stop(p); } /* Unconditionally reset the pin configuration */ reset_pin_configured(p, g_anOutputPinConfigured); diff --git a/docs/Tools-menu.md b/docs/Tools-menu.md index 06e289c65a..188ac06fb4 100644 --- a/docs/Tools-menu.md +++ b/docs/Tools-menu.md @@ -77,6 +77,10 @@ Menu: "**Tools > Debug symbols and core logs**" | Core logs Enabled | _(`NDEBUG` not defined)_ | Core logs are enabled. No debug symbols. | | Core Logs and Symbols Enabled (-g) | `-g` | Debug symbols are generated and core logs are enabled. | +When the core logs are enabled (`NDEBUG` not defined), including the `arduino-cli compile --optimize-for-debug` build (`-Og -g`), the logs and the `printf()` family output (`stdout`, `stderr`) are sent to the debug UART by `_write()`, which initializes the debug UART if it is not already used by a `Serial` instance. + +The debug UART is driven by polling: it links the UART driver but not its interrupt handlers. The `USARTx_IRQHandler()` are only linked when the sketch uses `Serial` or a `HardwareSerial`, so a sketch which does not use them can define its own `USARTx_IRQHandler()` in a debug build. In a build with `NDEBUG` defined, the UART driver is linked only if `printf()` is used. Provide your own `_write()` to send the output elsewhere. + ## C Runtime Library Menu: "**Tools > C Runtime Library**" diff --git a/docs/customization/HAL-configuration.md b/docs/customization/HAL-configuration.md index 34ab5747c7..12e402a2bd 100644 --- a/docs/customization/HAL-configuration.md +++ b/docs/customization/HAL-configuration.md @@ -180,7 +180,7 @@ The following HAL system configuration values can be redefined: * `USE_SD_TRANSCEIVER`: Enable the microSD transceiver ## Use HAL modules without Arduino ownership -STM32 peripherals have many powerful features. Some of them are used by default by the Arduino API: I2C, SPI, TIM, U(S)ART, ... and take over IRQ Handlers (ex: `TIMx_IRQHandler`) and other HAL weaked functions (ex: `HAL_XXX_MspInit()`). +STM32 peripherals have many powerful features. Some of them are used by default by the Arduino API: I2C, SPI, TIM, U(S)ART, ... and take over IRQ Handlers (ex: `TIMx_IRQHandler`) and other HAL weaked functions (ex: `HAL_UART_RxCpltCallback()`). For advanced user applications, it could be useful to take control of those IRQ handlers. Since core version 1.8.0, it is possible to disable the use of selected HAL modules by the Arduino API. diff --git a/libraries/SrcWrapper/CMakeLists.txt b/libraries/SrcWrapper/CMakeLists.txt index 0a7b1665a6..b62388678c 100644 --- a/libraries/SrcWrapper/CMakeLists.txt +++ b/libraries/SrcWrapper/CMakeLists.txt @@ -156,7 +156,6 @@ add_library(SrcWrapper_bin OBJECT EXCLUDE_FROM_ALL src/HAL/stm32yyxx_utils_fdcan.c src/HAL/stm32yyxx_utils_i2c.c src/HAL/stm32yyxx_utils_i3c.c - src/HardwareTimer.cpp src/LL/stm32yyxx_ll_adc.c src/LL/stm32yyxx_ll_bdma.c src/LL/stm32yyxx_ll_comp.c @@ -200,20 +199,17 @@ add_library(SrcWrapper_bin OBJECT EXCLUDE_FROM_ALL src/LL/stm32yyxx_ll_usb.c src/LL/stm32yyxx_ll_utils.c src/new.cpp - src/stm32/analog.cpp src/stm32/bootloader.c src/stm32/clock.c src/stm32/core_callback.c src/stm32/dwt.c src/stm32/hw_config.c - src/stm32/interrupt.cpp src/stm32/otp.c src/stm32/pinmap.c src/stm32/PortNames.c src/stm32/stm32_def.c src/stm32/system_stm32yyxx.c src/stm32/timer.c - src/stm32/uart.c src/syscalls.c ) target_link_libraries(SrcWrapper_bin PUBLIC SrcWrapper_usage) diff --git a/libraries/SrcWrapper/inc/PinConfigured.h b/libraries/SrcWrapper/inc/PinConfigured.h index 1aeb8abc9b..c07a961f93 100644 --- a/libraries/SrcWrapper/inc/PinConfigured.h +++ b/libraries/SrcWrapper/inc/PinConfigured.h @@ -69,6 +69,23 @@ extern "C" { #define reset_pin_configured(pin, map) \ (map[PINCONF_INDEX(pin)] = map[PINCONF_INDEX(pin)] & (~PINCONF_BIT(pin))) +/** + * @brief Map of the pins configured as analog output (PWM or DAC) by analogWrite(). + * + * Defined in wiring_digital.c so pinMode() does not depend on wiring_analog.c. + */ +extern uint32_t g_anOutputPinConfigured[]; + +/** + * @brief Stop the analog output (PWM or DAC) of a pin. + * + * Defined in wiring_analog.c and referenced weakly by pinMode(): the PWM and + * DAC code is only linked when the sketch uses analogWrite(). + * + * @param pin Pin name. + */ +void analog_output_stop(PinName pin); + #ifdef __cplusplus } #endif diff --git a/libraries/SrcWrapper/src/stm32/timer.c b/libraries/SrcWrapper/src/stm32/timer.c index aa475576ea..f76a07ad79 100644 --- a/libraries/SrcWrapper/src/stm32/timer.c +++ b/libraries/SrcWrapper/src/stm32/timer.c @@ -34,88 +34,6 @@ timerObj_t *get_timer_obj(TIM_HandleTypeDef *htim) return (obj); } -#if !defined(USE_HALV2_DRIVER) -/** - * @brief TIMER Initialization - enable the timer clock, then set priority of - * and enable the update interrupt and, if different, the capture compare interrupt - * @param htim_base TIM handle - */ -void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim_base) -{ - timerObj_t *obj = get_timer_obj(htim_base); - enableTimerClock(htim_base->Instance); - - // configure Update interrupt - HAL_NVIC_SetPriority(getTimerUpIrq(htim_base->Instance), obj->preemptPriority, obj->subPriority); - HAL_NVIC_EnableIRQ(getTimerUpIrq(htim_base->Instance)); - - if (getTimerCCIrq(htim_base->Instance) != getTimerUpIrq(htim_base->Instance)) { - // configure Capture Compare interrupt - HAL_NVIC_SetPriority(getTimerCCIrq(htim_base->Instance), obj->preemptPriority, obj->subPriority); - HAL_NVIC_EnableIRQ(getTimerCCIrq(htim_base->Instance)); - } -} - -/** - * @brief TIMER Deinitialization - clock and nvic - * @param htim_base TIM handle - */ -void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef *htim_base) -{ - disableTimerClock(htim_base->Instance); - HAL_NVIC_DisableIRQ(getTimerUpIrq(htim_base->Instance)); - HAL_NVIC_DisableIRQ(getTimerCCIrq(htim_base->Instance)); -} - -/** - * @brief Initializes the TIM Output Compare MSP. - * @param htim TIM handle - */ -void HAL_TIM_OC_MspInit(TIM_HandleTypeDef *htim) -{ - timerObj_t *obj = get_timer_obj(htim); - enableTimerClock(htim->Instance); - - // configure Update interrupt - HAL_NVIC_SetPriority(getTimerUpIrq(htim->Instance), obj->preemptPriority, obj->subPriority); - HAL_NVIC_EnableIRQ(getTimerUpIrq(htim->Instance)); - - if (getTimerCCIrq(htim->Instance) != getTimerUpIrq(htim->Instance)) { - // configure Capture Compare interrupt - HAL_NVIC_SetPriority(getTimerCCIrq(htim->Instance), obj->preemptPriority, obj->subPriority); - HAL_NVIC_EnableIRQ(getTimerCCIrq(htim->Instance)); - } -} - -/** - * @brief DeInitialize TIM Output Compare MSP. - * @param htim TIM handle - */ -void HAL_TIM_OC_MspDeInit(TIM_HandleTypeDef *htim) -{ - disableTimerClock(htim->Instance); - HAL_NVIC_DisableIRQ(getTimerUpIrq(htim->Instance)); - HAL_NVIC_DisableIRQ(getTimerCCIrq(htim->Instance)); -} - -/** - * @brief Initializes the TIM Input Capture MSP. - * @param htim TIM handle - */ -void HAL_TIM_IC_MspInit(TIM_HandleTypeDef *htim) -{ - enableTimerClock(htim->Instance); -} - -/** - * @brief DeInitialize TIM Input Capture MSP. - * @param htim TIM handle - */ -void HAL_TIM_IC_MspDeInit(TIM_HandleTypeDef *htim) -{ - disableTimerClock(htim->Instance); -} -#endif /* !defined(USE_HALV2_DRIVER) */ /* Exported functions */ void enableTimerClock(TIM_TypeDef *instance) { diff --git a/libraries/SrcWrapper/src/syscalls.c b/libraries/SrcWrapper/src/syscalls.c index b9ff7d5289..ce6f11b7da 100644 --- a/libraries/SrcWrapper/src/syscalls.c +++ b/libraries/SrcWrapper/src/syscalls.c @@ -112,7 +112,7 @@ int _read(int file, char *ptr, int len) return 0; } -/* Moved to Print.cpp to support Print::printf() +/* Moved to cores/arduino/stm32/write.cpp to support Print::printf() __attribute__((weak)) int _write(int file, char *ptr, int len) { diff --git a/libraries/USBDevice/src/usbd_conf.c b/libraries/USBDevice/src/usbd_conf.c index b778741ca0..0c20fd3179 100644 --- a/libraries/USBDevice/src/usbd_conf.c +++ b/libraries/USBDevice/src/usbd_conf.c @@ -45,9 +45,10 @@ PCD_HandleTypeDef g_hpcd; /** * @brief Initializes the PCD MSP. + * Called before HAL_PCD_Init(). * @param hpcd PCD handle */ -void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd) +static void pcd_msp_init(PCD_HandleTypeDef *hpcd) { const PinMap *map = NULL; @@ -222,9 +223,10 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd) /** * @brief De-Initializes the PCD MSP. + * Called after HAL_PCD_DeInit(). * @param hpcd PCD handle */ -void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd) +static void pcd_msp_deinit(PCD_HandleTypeDef *hpcd) { /* Disable USB FS Clock */ #if defined (USB) @@ -507,6 +509,7 @@ USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev) pdev->pData = &g_hpcd; /* Initialize LL Driver */ + pcd_msp_init(&g_hpcd); if (HAL_PCD_Init(&g_hpcd) != HAL_OK) { Error_Handler(); } @@ -534,6 +537,7 @@ USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev) USBD_StatusTypeDef USBD_LL_DeInit(USBD_HandleTypeDef *pdev) { HAL_PCD_DeInit(pdev->pData); + pcd_msp_deinit(pdev->pData); return USBD_OK; }