123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764 |
- /* mbed Microcontroller Library
- *******************************************************************************
- * Copyright (c) 2017, STMicroelectronics
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of STMicroelectronics nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *******************************************************************************
- */
- #if DEVICE_SERIAL
- #include "serial_api_hal.h"
- // Possible choices of the LPUART_CLOCK_SOURCE configuration set in json file
- #define USE_LPUART_CLK_LSE 0x01
- #define USE_LPUART_CLK_PCLK1 0x02
- #define USE_LPUART_CLK_HSI 0x04
- int stdio_uart_inited = 0; // used in platform/mbed_board.c and platform/mbed_retarget.cpp
- serial_t stdio_uart;
- extern UART_HandleTypeDef uart_handlers[];
- extern uint32_t serial_irq_ids[];
- // Utility functions
- HAL_StatusTypeDef init_uart(serial_t *obj);
- int8_t get_uart_index(UARTName uart_name);
- void serial_init(serial_t *obj, PinName tx, PinName rx)
- {
- struct serial_s *obj_s = SERIAL_S(obj);
- uint8_t stdio_config = 0;
- // Determine the UART to use (UART_1, UART_2, ...)
- UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
- UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
- // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object
- obj_s->uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
- MBED_ASSERT(obj_s->uart != (UARTName)NC);
- if ((tx == STDIO_UART_TX) || (rx == STDIO_UART_RX)) {
- stdio_config = 1;
- } else {
- if (uart_tx == pinmap_peripheral(STDIO_UART_TX, PinMap_UART_TX)) {
- error("Error: new serial object is using same UART as STDIO");
- }
- }
- // Reset and enable clock
- #if defined(USART1_BASE)
- if (obj_s->uart == UART_1) {
- __HAL_RCC_USART1_CLK_ENABLE();
- }
- #endif
- #if defined (USART2_BASE)
- if (obj_s->uart == UART_2) {
- __HAL_RCC_USART2_CLK_ENABLE();
- }
- #endif
- #if defined(USART3_BASE)
- if (obj_s->uart == UART_3) {
- __HAL_RCC_USART3_CLK_ENABLE();
- }
- #endif
- #if defined(UART4_BASE)
- if (obj_s->uart == UART_4) {
- __HAL_RCC_UART4_CLK_ENABLE();
- }
- #endif
- #if defined(USART4_BASE)
- if (obj_s->uart == UART_4) {
- __HAL_RCC_USART4_CLK_ENABLE();
- }
- #endif
- #if defined(UART5_BASE)
- if (obj_s->uart == UART_5) {
- __HAL_RCC_UART5_CLK_ENABLE();
- }
- #endif
- #if defined(USART5_BASE)
- if (obj_s->uart == UART_5) {
- __HAL_RCC_USART5_CLK_ENABLE();
- }
- #endif
- #if defined(USART6_BASE)
- if (obj_s->uart == UART_6) {
- __HAL_RCC_USART6_CLK_ENABLE();
- }
- #endif
- #if defined(UART7_BASE)
- if (obj_s->uart == UART_7) {
- __HAL_RCC_UART7_CLK_ENABLE();
- }
- #endif
- #if defined(USART7_BASE)
- if (obj_s->uart == UART_7) {
- __HAL_RCC_USART7_CLK_ENABLE();
- }
- #endif
- #if defined(UART8_BASE)
- if (obj_s->uart == UART_8) {
- __HAL_RCC_UART8_CLK_ENABLE();
- }
- #endif
- #if defined(USART8_BASE)
- if (obj_s->uart == UART_8) {
- __HAL_RCC_USART8_CLK_ENABLE();
- }
- #endif
- #if defined(UART9_BASE)
- if (obj_s->uart == UART_9) {
- __HAL_RCC_UART9_CLK_ENABLE();
- }
- #endif
- #if defined(UART10_BASE)
- if (obj_s->uart == UART_10) {
- __HAL_RCC_UART10_CLK_ENABLE();
- }
- #endif
- #if defined(LPUART1_BASE)
- if (obj_s->uart == LPUART_1) {
- __HAL_RCC_LPUART1_CLK_ENABLE();
- }
- #endif
- // Assign serial object index
- obj_s->index = get_uart_index(obj_s->uart);
- MBED_ASSERT(obj_s->index >= 0);
- // Configure UART pins
- pinmap_pinout(tx, PinMap_UART_TX);
- pinmap_pinout(rx, PinMap_UART_RX);
- if (tx != NC) {
- pin_mode(tx, PullUp);
- }
- if (rx != NC) {
- pin_mode(rx, PullUp);
- }
- // Configure UART
- obj_s->baudrate = 9600; // baudrate default value
- if (stdio_config) {
- #if MBED_CONF_PLATFORM_STDIO_BAUD_RATE
- obj_s->baudrate = MBED_CONF_PLATFORM_STDIO_BAUD_RATE; // baudrate takes value from platform/mbed_lib.json
- #endif /* MBED_CONF_PLATFORM_STDIO_BAUD_RATE */
- } else {
- #if MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE
- obj_s->baudrate = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE; // baudrate takes value from platform/mbed_lib.json
- #endif /* MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE */
- }
- obj_s->databits = UART_WORDLENGTH_8B;
- obj_s->stopbits = UART_STOPBITS_1;
- obj_s->parity = UART_PARITY_NONE;
- #if DEVICE_SERIAL_FC
- obj_s->hw_flow_ctl = UART_HWCONTROL_NONE;
- #endif
- obj_s->pin_tx = tx;
- obj_s->pin_rx = rx;
- init_uart(obj); /* init_uart will be called again in serial_baud function, so don't worry if init_uart returns HAL_ERROR */
- // For stdio management in platform/mbed_board.c and platform/mbed_retarget.cpp
- if (stdio_config) {
- stdio_uart_inited = 1;
- memcpy(&stdio_uart, obj, sizeof(serial_t));
- }
- }
- void serial_free(serial_t *obj)
- {
- struct serial_s *obj_s = SERIAL_S(obj);
- // Reset UART and disable clock
- #if defined(USART1_BASE)
- if (obj_s->uart == UART_1) {
- __HAL_RCC_USART1_FORCE_RESET();
- __HAL_RCC_USART1_RELEASE_RESET();
- __HAL_RCC_USART1_CLK_DISABLE();
- }
- #endif
- #if defined(USART2_BASE)
- if (obj_s->uart == UART_2) {
- __HAL_RCC_USART2_FORCE_RESET();
- __HAL_RCC_USART2_RELEASE_RESET();
- __HAL_RCC_USART2_CLK_DISABLE();
- }
- #endif
- #if defined(USART3_BASE)
- if (obj_s->uart == UART_3) {
- __HAL_RCC_USART3_FORCE_RESET();
- __HAL_RCC_USART3_RELEASE_RESET();
- __HAL_RCC_USART3_CLK_DISABLE();
- }
- #endif
- #if defined(UART4_BASE)
- if (obj_s->uart == UART_4) {
- __HAL_RCC_UART4_FORCE_RESET();
- __HAL_RCC_UART4_RELEASE_RESET();
- __HAL_RCC_UART4_CLK_DISABLE();
- }
- #endif
- #if defined(USART4_BASE)
- if (obj_s->uart == UART_4) {
- __HAL_RCC_USART4_FORCE_RESET();
- __HAL_RCC_USART4_RELEASE_RESET();
- __HAL_RCC_USART4_CLK_DISABLE();
- }
- #endif
- #if defined(UART5_BASE)
- if (obj_s->uart == UART_5) {
- __HAL_RCC_UART5_FORCE_RESET();
- __HAL_RCC_UART5_RELEASE_RESET();
- __HAL_RCC_UART5_CLK_DISABLE();
- }
- #endif
- #if defined(USART5_BASE)
- if (obj_s->uart == UART_5) {
- __HAL_RCC_USART5_FORCE_RESET();
- __HAL_RCC_USART5_RELEASE_RESET();
- __HAL_RCC_USART5_CLK_DISABLE();
- }
- #endif
- #if defined(USART6_BASE)
- if (obj_s->uart == UART_6) {
- __HAL_RCC_USART6_FORCE_RESET();
- __HAL_RCC_USART6_RELEASE_RESET();
- __HAL_RCC_USART6_CLK_DISABLE();
- }
- #endif
- #if defined(UART7_BASE)
- if (obj_s->uart == UART_7) {
- __HAL_RCC_UART7_FORCE_RESET();
- __HAL_RCC_UART7_RELEASE_RESET();
- __HAL_RCC_UART7_CLK_DISABLE();
- }
- #endif
- #if defined(USART7_BASE)
- if (obj_s->uart == UART_7) {
- __HAL_RCC_USART7_FORCE_RESET();
- __HAL_RCC_USART7_RELEASE_RESET();
- __HAL_RCC_USART7_CLK_DISABLE();
- }
- #endif
- #if defined(UART8_BASE)
- if (obj_s->uart == UART_8) {
- __HAL_RCC_UART8_FORCE_RESET();
- __HAL_RCC_UART8_RELEASE_RESET();
- __HAL_RCC_UART8_CLK_DISABLE();
- }
- #endif
- #if defined(USART8_BASE)
- if (obj_s->uart == UART_8) {
- __HAL_RCC_USART8_FORCE_RESET();
- __HAL_RCC_USART8_RELEASE_RESET();
- __HAL_RCC_USART8_CLK_DISABLE();
- }
- #endif
- #if defined(UART9_BASE)
- if (obj_s->uart == UART_9) {
- __HAL_RCC_UART9_FORCE_RESET();
- __HAL_RCC_UART9_RELEASE_RESET();
- __HAL_RCC_UART9_CLK_DISABLE();
- }
- #endif
- #if defined(UART10_BASE)
- if (obj_s->uart == UART_10) {
- __HAL_RCC_UART10_FORCE_RESET();
- __HAL_RCC_UART10_RELEASE_RESET();
- __HAL_RCC_UART10_CLK_DISABLE();
- }
- #endif
- #if defined(LPUART1_BASE)
- if (obj_s->uart == LPUART_1) {
- __HAL_RCC_LPUART1_FORCE_RESET();
- __HAL_RCC_LPUART1_RELEASE_RESET();
- __HAL_RCC_LPUART1_CLK_DISABLE();
- }
- #endif
- // Configure GPIOs
- pin_function(obj_s->pin_tx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
- pin_function(obj_s->pin_rx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
- serial_irq_ids[obj_s->index] = 0;
- }
- void serial_baud(serial_t *obj, int baudrate)
- {
- struct serial_s *obj_s = SERIAL_S(obj);
- obj_s->baudrate = baudrate;
- #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_s->uart == LPUART_1) {
- RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
- PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
- #if ((MBED_CONF_TARGET_LPUART_CLOCK_SOURCE) & USE_LPUART_CLK_LSE)
- if (baudrate <= 9600) {
- // Enable LSE in case it is not already done
- if (!__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY)) {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
- RCC_OscInitStruct.HSIState = RCC_LSE_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_OFF;
- HAL_RCC_OscConfig(&RCC_OscInitStruct);
- }
- // Keep it to verify if HAL_RCC_OscConfig didn't exit with a timeout
- if (__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY)) {
- PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_LSE;
- HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
- if (init_uart(obj) == HAL_OK) {
- return;
- }
- }
- }
- #endif
- #if ((MBED_CONF_TARGET_LPUART_CLOCK_SOURCE) & USE_LPUART_CLK_PCLK1)
- PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK1;
- HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
- if (init_uart(obj) == HAL_OK) {
- return;
- }
- #endif
- #if ((MBED_CONF_TARGET_LPUART_CLOCK_SOURCE) & USE_LPUART_CLK_HSI)
- // Enable HSI in case it is not already done
- if (!__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY)) {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_OFF;
- RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
- HAL_RCC_OscConfig(&RCC_OscInitStruct);
- }
- // Keep it to verify if HAL_RCC_OscConfig didn't exit with a timeout
- if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY)) {
- PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_HSI;
- HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
- if (init_uart(obj) == HAL_OK) {
- return;
- }
- }
- #endif
- // Last chance using SYSCLK
- PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_SYSCLK;
- HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
- }
- #endif /* LPUART1_BASE */
- if (init_uart(obj) != HAL_OK) {
- debug("Cannot initialize UART with baud rate %u\n", baudrate);
- }
- }
- void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits)
- {
- struct serial_s *obj_s = SERIAL_S(obj);
- switch (parity) {
- case ParityOdd:
- obj_s->parity = UART_PARITY_ODD;
- break;
- case ParityEven:
- obj_s->parity = UART_PARITY_EVEN;
- break;
- default: // ParityNone
- case ParityForced0: // unsupported!
- case ParityForced1: // unsupported!
- obj_s->parity = UART_PARITY_NONE;
- break;
- }
- switch (data_bits) {
- case 7:
- if (parity != UART_PARITY_NONE) {
- obj_s->databits = UART_WORDLENGTH_8B;
- } else {
- #if defined UART_WORDLENGTH_7B
- obj_s->databits = UART_WORDLENGTH_7B;
- #else
- error("7-bit data format without parity is not supported");
- #endif
- }
- break;
- case 8:
- if (parity != UART_PARITY_NONE) {
- obj_s->databits = UART_WORDLENGTH_9B;
- } else {
- obj_s->databits = UART_WORDLENGTH_8B;
- }
- break;
- case 9:
- if (parity != UART_PARITY_NONE) {
- error("Parity is not supported with 9-bit data format");
- } else {
- obj_s->databits = UART_WORDLENGTH_9B;
- }
- break;
- default:
- error("Only 7, 8 or 9-bit data formats are supported");
- break;
- }
- if (stop_bits == 2) {
- obj_s->stopbits = UART_STOPBITS_2;
- } else {
- obj_s->stopbits = UART_STOPBITS_1;
- }
- init_uart(obj);
- }
- /******************************************************************************
- * READ/WRITE
- ******************************************************************************/
- int serial_readable(serial_t *obj)
- {
- struct serial_s *obj_s = SERIAL_S(obj);
- UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
- /* To avoid a target blocking case, let's check for
- * possible OVERRUN error and discard it
- */
- if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE)) {
- __HAL_UART_CLEAR_OREFLAG(huart);
- }
- // Check if data is received
- return (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) ? 1 : 0;
- }
- int serial_writable(serial_t *obj)
- {
- struct serial_s *obj_s = SERIAL_S(obj);
- UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
- // Check if data is transmitted
- return (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) ? 1 : 0;
- }
- void serial_pinout_tx(PinName tx)
- {
- pinmap_pinout(tx, PinMap_UART_TX);
- }
- void serial_break_clear(serial_t *obj)
- {
- (void)obj;
- }
- /******************************************************************************
- * UTILITY FUNCTIONS
- ******************************************************************************/
- HAL_StatusTypeDef init_uart(serial_t *obj)
- {
- struct serial_s *obj_s = SERIAL_S(obj);
- UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
- huart->Instance = (USART_TypeDef *)(obj_s->uart);
- huart->Init.BaudRate = obj_s->baudrate;
- huart->Init.WordLength = obj_s->databits;
- huart->Init.StopBits = obj_s->stopbits;
- huart->Init.Parity = obj_s->parity;
- #if DEVICE_SERIAL_FC
- huart->Init.HwFlowCtl = obj_s->hw_flow_ctl;
- #else
- huart->Init.HwFlowCtl = UART_HWCONTROL_NONE;
- #endif
- huart->Init.OverSampling = UART_OVERSAMPLING_16;
- huart->TxXferCount = 0;
- huart->TxXferSize = 0;
- huart->RxXferCount = 0;
- huart->RxXferSize = 0;
- if (obj_s->pin_rx == NC) {
- huart->Init.Mode = UART_MODE_TX;
- } else if (obj_s->pin_tx == NC) {
- huart->Init.Mode = UART_MODE_RX;
- } else {
- huart->Init.Mode = UART_MODE_TX_RX;
- }
- #if defined(LPUART1_BASE)
- if (huart->Instance == LPUART1) {
- if (obj_s->baudrate <= 9600) {
- HAL_UARTEx_EnableClockStopMode(huart);
- HAL_UARTEx_EnableStopMode(huart);
- } else {
- HAL_UARTEx_DisableClockStopMode(huart);
- HAL_UARTEx_DisableStopMode(huart);
- }
- }
- #endif
- return HAL_UART_Init(huart);
- }
- int8_t get_uart_index(UARTName uart_name)
- {
- uint8_t index = 0;
- #if defined(USART1_BASE)
- if (uart_name == UART_1) {
- return index;
- }
- index++;
- #endif
- #if defined(USART2_BASE)
- if (uart_name == UART_2) {
- return index;
- }
- index++;
- #endif
- #if defined(USART3_BASE)
- if (uart_name == UART_3) {
- return index;
- }
- index++;
- #endif
- #if defined(UART4_BASE)
- if (uart_name == UART_4) {
- return index;
- }
- index++;
- #endif
- #if defined(USART4_BASE)
- if (uart_name == UART_4) {
- return index;
- }
- index++;
- #endif
- #if defined(UART5_BASE)
- if (uart_name == UART_5) {
- return index;
- }
- index++;
- #endif
- #if defined(USART5_BASE)
- if (uart_name == UART_5) {
- return index;
- }
- index++;
- #endif
- #if defined(USART6_BASE)
- if (uart_name == UART_6) {
- return index;
- }
- index++;
- #endif
- #if defined(UART7_BASE)
- if (uart_name == UART_7) {
- return index;
- }
- index++;
- #endif
- #if defined(USART7_BASE)
- if (uart_name == UART_7) {
- return index;
- }
- index++;
- #endif
- #if defined(UART8_BASE)
- if (uart_name == UART_8) {
- return index;
- }
- index++;
- #endif
- #if defined(USART8_BASE)
- if (uart_name == UART_8) {
- return index;
- }
- index++;
- #endif
- #if defined(UART9_BASE)
- if (uart_name == UART_9) {
- return index;
- }
- index++;
- #endif
- #if defined(UART10_BASE)
- if (uart_name == UART_10) {
- return index;
- }
- index++;
- #endif
- #if defined(LPUART1_BASE)
- if (uart_name == LPUART_1) {
- return index;
- }
- index++;
- #endif
- return -1;
- }
- /* Function to protect deep sleep while a seral Tx is ongoing on not complete
- * yet. Returns 1 if there is at least 1 serial instance with ongoing ransfer
- * 0 otherwise.
- */
- int serial_is_tx_ongoing(void) {
- int TxOngoing = 0;
- #if defined(USART1_BASE)
- if (LL_USART_IsEnabled(USART1) && !LL_USART_IsActiveFlag_TC(USART1)) {
- TxOngoing |= 1;
- }
- #endif
- #if defined(USART2_BASE)
- if (LL_USART_IsEnabled(USART2) && !LL_USART_IsActiveFlag_TC(USART2)) {
- TxOngoing |= 1;
- }
- #endif
- #if defined(USART3_BASE)
- if (LL_USART_IsEnabled(USART3) && !LL_USART_IsActiveFlag_TC(USART3)) {
- TxOngoing |= 1;
- }
- #endif
- #if defined(UART4_BASE)
- if (LL_USART_IsEnabled(UART4) && !LL_USART_IsActiveFlag_TC(UART4)) {
- TxOngoing |= 1;
- }
- #endif
- #if defined(USART4_BASE)
- if (LL_USART_IsEnabled(USART4) && !LL_USART_IsActiveFlag_TC(USART4)) {
- TxOngoing |= 1;
- }
- #endif
- #if defined(UART5_BASE)
- if (LL_USART_IsEnabled(UART5) && !LL_USART_IsActiveFlag_TC(UART5)) {
- TxOngoing |= 1;
- }
- #endif
- #if defined(USART5_BASE)
- if (LL_USART_IsEnabled(USART5) && !LL_USART_IsActiveFlag_TC(USART5)) {
- TxOngoing |= 1;
- }
- #endif
- #if defined(USART6_BASE)
- if (LL_USART_IsEnabled(USART6) && !LL_USART_IsActiveFlag_TC(USART6)) {
- TxOngoing |= 1;
- }
- #endif
- #if defined(UART7_BASE)
- if (LL_USART_IsEnabled(UART7) && !LL_USART_IsActiveFlag_TC(UART7)) {
- TxOngoing |= 1;
- }
- #endif
- #if defined(USART7_BASE)
- if (LL_USART_IsEnabled(USART7) && !LL_USART_IsActiveFlag_TC(USART7)) {
- TxOngoing |= 1;
- }
- #endif
- #if defined(UART8_BASE)
- if (LL_USART_IsEnabled(UART8) && !LL_USART_IsActiveFlag_TC(UART8)) {
- TxOngoing |= 1;
- }
- #endif
- #if defined(USART8_BASE)
- if (LL_USART_IsEnabled(USART8) && !LL_USART_IsActiveFlag_TC(USART8)) {
- TxOngoing |= 1;
- }
- #endif
- #if defined(UART9_BASE)
- if (LL_USART_IsEnabled(UART9) && !LL_USART_IsActiveFlag_TC(UART9)) {
- TxOngoing |= 1;
- }
- #endif
- #if defined(UART10_BASE)
- if (LL_USART_IsEnabled(UART10) && !LL_USART_IsActiveFlag_TC(UART10)) {
- TxOngoing |= 1;
- }
- #endif
- #if defined(LPUART1_BASE)
- if (LL_USART_IsEnabled(LPUART1) && !LL_USART_IsActiveFlag_TC(LPUART1)) {
- TxOngoing |= 1;
- }
- #endif
- /* If Tx is ongoing, then transfer is */
- return TxOngoing;
- }
- #endif /* DEVICE_SERIAL */
|