serial_api.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. /* mbed Microcontroller Library
  2. *******************************************************************************
  3. * Copyright (c) 2017, STMicroelectronics
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  24. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *******************************************************************************
  29. */
  30. #if DEVICE_SERIAL
  31. #include "serial_api_hal.h"
  32. // Possible choices of the LPUART_CLOCK_SOURCE configuration set in json file
  33. #define USE_LPUART_CLK_LSE 0x01
  34. #define USE_LPUART_CLK_PCLK1 0x02
  35. #define USE_LPUART_CLK_HSI 0x04
  36. int stdio_uart_inited = 0; // used in platform/mbed_board.c and platform/mbed_retarget.cpp
  37. serial_t stdio_uart;
  38. extern UART_HandleTypeDef uart_handlers[];
  39. extern uint32_t serial_irq_ids[];
  40. // Utility functions
  41. HAL_StatusTypeDef init_uart(serial_t *obj);
  42. int8_t get_uart_index(UARTName uart_name);
  43. void serial_init(serial_t *obj, PinName tx, PinName rx)
  44. {
  45. struct serial_s *obj_s = SERIAL_S(obj);
  46. uint8_t stdio_config = 0;
  47. // Determine the UART to use (UART_1, UART_2, ...)
  48. UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
  49. UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
  50. // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object
  51. obj_s->uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
  52. MBED_ASSERT(obj_s->uart != (UARTName)NC);
  53. if ((tx == STDIO_UART_TX) || (rx == STDIO_UART_RX)) {
  54. stdio_config = 1;
  55. } else {
  56. if (uart_tx == pinmap_peripheral(STDIO_UART_TX, PinMap_UART_TX)) {
  57. error("Error: new serial object is using same UART as STDIO");
  58. }
  59. }
  60. // Reset and enable clock
  61. #if defined(USART1_BASE)
  62. if (obj_s->uart == UART_1) {
  63. __HAL_RCC_USART1_CLK_ENABLE();
  64. }
  65. #endif
  66. #if defined (USART2_BASE)
  67. if (obj_s->uart == UART_2) {
  68. __HAL_RCC_USART2_CLK_ENABLE();
  69. }
  70. #endif
  71. #if defined(USART3_BASE)
  72. if (obj_s->uart == UART_3) {
  73. __HAL_RCC_USART3_CLK_ENABLE();
  74. }
  75. #endif
  76. #if defined(UART4_BASE)
  77. if (obj_s->uart == UART_4) {
  78. __HAL_RCC_UART4_CLK_ENABLE();
  79. }
  80. #endif
  81. #if defined(USART4_BASE)
  82. if (obj_s->uart == UART_4) {
  83. __HAL_RCC_USART4_CLK_ENABLE();
  84. }
  85. #endif
  86. #if defined(UART5_BASE)
  87. if (obj_s->uart == UART_5) {
  88. __HAL_RCC_UART5_CLK_ENABLE();
  89. }
  90. #endif
  91. #if defined(USART5_BASE)
  92. if (obj_s->uart == UART_5) {
  93. __HAL_RCC_USART5_CLK_ENABLE();
  94. }
  95. #endif
  96. #if defined(USART6_BASE)
  97. if (obj_s->uart == UART_6) {
  98. __HAL_RCC_USART6_CLK_ENABLE();
  99. }
  100. #endif
  101. #if defined(UART7_BASE)
  102. if (obj_s->uart == UART_7) {
  103. __HAL_RCC_UART7_CLK_ENABLE();
  104. }
  105. #endif
  106. #if defined(USART7_BASE)
  107. if (obj_s->uart == UART_7) {
  108. __HAL_RCC_USART7_CLK_ENABLE();
  109. }
  110. #endif
  111. #if defined(UART8_BASE)
  112. if (obj_s->uart == UART_8) {
  113. __HAL_RCC_UART8_CLK_ENABLE();
  114. }
  115. #endif
  116. #if defined(USART8_BASE)
  117. if (obj_s->uart == UART_8) {
  118. __HAL_RCC_USART8_CLK_ENABLE();
  119. }
  120. #endif
  121. #if defined(UART9_BASE)
  122. if (obj_s->uart == UART_9) {
  123. __HAL_RCC_UART9_CLK_ENABLE();
  124. }
  125. #endif
  126. #if defined(UART10_BASE)
  127. if (obj_s->uart == UART_10) {
  128. __HAL_RCC_UART10_CLK_ENABLE();
  129. }
  130. #endif
  131. #if defined(LPUART1_BASE)
  132. if (obj_s->uart == LPUART_1) {
  133. __HAL_RCC_LPUART1_CLK_ENABLE();
  134. }
  135. #endif
  136. // Assign serial object index
  137. obj_s->index = get_uart_index(obj_s->uart);
  138. MBED_ASSERT(obj_s->index >= 0);
  139. // Configure UART pins
  140. pinmap_pinout(tx, PinMap_UART_TX);
  141. pinmap_pinout(rx, PinMap_UART_RX);
  142. if (tx != NC) {
  143. pin_mode(tx, PullUp);
  144. }
  145. if (rx != NC) {
  146. pin_mode(rx, PullUp);
  147. }
  148. // Configure UART
  149. obj_s->baudrate = 9600; // baudrate default value
  150. if (stdio_config) {
  151. #if MBED_CONF_PLATFORM_STDIO_BAUD_RATE
  152. obj_s->baudrate = MBED_CONF_PLATFORM_STDIO_BAUD_RATE; // baudrate takes value from platform/mbed_lib.json
  153. #endif /* MBED_CONF_PLATFORM_STDIO_BAUD_RATE */
  154. } else {
  155. #if MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE
  156. obj_s->baudrate = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE; // baudrate takes value from platform/mbed_lib.json
  157. #endif /* MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE */
  158. }
  159. obj_s->databits = UART_WORDLENGTH_8B;
  160. obj_s->stopbits = UART_STOPBITS_1;
  161. obj_s->parity = UART_PARITY_NONE;
  162. #if DEVICE_SERIAL_FC
  163. obj_s->hw_flow_ctl = UART_HWCONTROL_NONE;
  164. #endif
  165. obj_s->pin_tx = tx;
  166. obj_s->pin_rx = rx;
  167. init_uart(obj); /* init_uart will be called again in serial_baud function, so don't worry if init_uart returns HAL_ERROR */
  168. // For stdio management in platform/mbed_board.c and platform/mbed_retarget.cpp
  169. if (stdio_config) {
  170. stdio_uart_inited = 1;
  171. memcpy(&stdio_uart, obj, sizeof(serial_t));
  172. }
  173. }
  174. void serial_free(serial_t *obj)
  175. {
  176. struct serial_s *obj_s = SERIAL_S(obj);
  177. // Reset UART and disable clock
  178. #if defined(USART1_BASE)
  179. if (obj_s->uart == UART_1) {
  180. __HAL_RCC_USART1_FORCE_RESET();
  181. __HAL_RCC_USART1_RELEASE_RESET();
  182. __HAL_RCC_USART1_CLK_DISABLE();
  183. }
  184. #endif
  185. #if defined(USART2_BASE)
  186. if (obj_s->uart == UART_2) {
  187. __HAL_RCC_USART2_FORCE_RESET();
  188. __HAL_RCC_USART2_RELEASE_RESET();
  189. __HAL_RCC_USART2_CLK_DISABLE();
  190. }
  191. #endif
  192. #if defined(USART3_BASE)
  193. if (obj_s->uart == UART_3) {
  194. __HAL_RCC_USART3_FORCE_RESET();
  195. __HAL_RCC_USART3_RELEASE_RESET();
  196. __HAL_RCC_USART3_CLK_DISABLE();
  197. }
  198. #endif
  199. #if defined(UART4_BASE)
  200. if (obj_s->uart == UART_4) {
  201. __HAL_RCC_UART4_FORCE_RESET();
  202. __HAL_RCC_UART4_RELEASE_RESET();
  203. __HAL_RCC_UART4_CLK_DISABLE();
  204. }
  205. #endif
  206. #if defined(USART4_BASE)
  207. if (obj_s->uart == UART_4) {
  208. __HAL_RCC_USART4_FORCE_RESET();
  209. __HAL_RCC_USART4_RELEASE_RESET();
  210. __HAL_RCC_USART4_CLK_DISABLE();
  211. }
  212. #endif
  213. #if defined(UART5_BASE)
  214. if (obj_s->uart == UART_5) {
  215. __HAL_RCC_UART5_FORCE_RESET();
  216. __HAL_RCC_UART5_RELEASE_RESET();
  217. __HAL_RCC_UART5_CLK_DISABLE();
  218. }
  219. #endif
  220. #if defined(USART5_BASE)
  221. if (obj_s->uart == UART_5) {
  222. __HAL_RCC_USART5_FORCE_RESET();
  223. __HAL_RCC_USART5_RELEASE_RESET();
  224. __HAL_RCC_USART5_CLK_DISABLE();
  225. }
  226. #endif
  227. #if defined(USART6_BASE)
  228. if (obj_s->uart == UART_6) {
  229. __HAL_RCC_USART6_FORCE_RESET();
  230. __HAL_RCC_USART6_RELEASE_RESET();
  231. __HAL_RCC_USART6_CLK_DISABLE();
  232. }
  233. #endif
  234. #if defined(UART7_BASE)
  235. if (obj_s->uart == UART_7) {
  236. __HAL_RCC_UART7_FORCE_RESET();
  237. __HAL_RCC_UART7_RELEASE_RESET();
  238. __HAL_RCC_UART7_CLK_DISABLE();
  239. }
  240. #endif
  241. #if defined(USART7_BASE)
  242. if (obj_s->uart == UART_7) {
  243. __HAL_RCC_USART7_FORCE_RESET();
  244. __HAL_RCC_USART7_RELEASE_RESET();
  245. __HAL_RCC_USART7_CLK_DISABLE();
  246. }
  247. #endif
  248. #if defined(UART8_BASE)
  249. if (obj_s->uart == UART_8) {
  250. __HAL_RCC_UART8_FORCE_RESET();
  251. __HAL_RCC_UART8_RELEASE_RESET();
  252. __HAL_RCC_UART8_CLK_DISABLE();
  253. }
  254. #endif
  255. #if defined(USART8_BASE)
  256. if (obj_s->uart == UART_8) {
  257. __HAL_RCC_USART8_FORCE_RESET();
  258. __HAL_RCC_USART8_RELEASE_RESET();
  259. __HAL_RCC_USART8_CLK_DISABLE();
  260. }
  261. #endif
  262. #if defined(UART9_BASE)
  263. if (obj_s->uart == UART_9) {
  264. __HAL_RCC_UART9_FORCE_RESET();
  265. __HAL_RCC_UART9_RELEASE_RESET();
  266. __HAL_RCC_UART9_CLK_DISABLE();
  267. }
  268. #endif
  269. #if defined(UART10_BASE)
  270. if (obj_s->uart == UART_10) {
  271. __HAL_RCC_UART10_FORCE_RESET();
  272. __HAL_RCC_UART10_RELEASE_RESET();
  273. __HAL_RCC_UART10_CLK_DISABLE();
  274. }
  275. #endif
  276. #if defined(LPUART1_BASE)
  277. if (obj_s->uart == LPUART_1) {
  278. __HAL_RCC_LPUART1_FORCE_RESET();
  279. __HAL_RCC_LPUART1_RELEASE_RESET();
  280. __HAL_RCC_LPUART1_CLK_DISABLE();
  281. }
  282. #endif
  283. // Configure GPIOs
  284. pin_function(obj_s->pin_tx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
  285. pin_function(obj_s->pin_rx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
  286. serial_irq_ids[obj_s->index] = 0;
  287. }
  288. void serial_baud(serial_t *obj, int baudrate)
  289. {
  290. struct serial_s *obj_s = SERIAL_S(obj);
  291. obj_s->baudrate = baudrate;
  292. #if defined(LPUART1_BASE)
  293. /* Note that LPUART clock source must be in the range [3 x baud rate, 4096 x baud rate], check Ref Manual */
  294. if (obj_s->uart == LPUART_1) {
  295. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  296. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
  297. #if ((MBED_CONF_TARGET_LPUART_CLOCK_SOURCE) & USE_LPUART_CLK_LSE)
  298. if (baudrate <= 9600) {
  299. // Enable LSE in case it is not already done
  300. if (!__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY)) {
  301. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  302. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
  303. RCC_OscInitStruct.HSIState = RCC_LSE_ON;
  304. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_OFF;
  305. HAL_RCC_OscConfig(&RCC_OscInitStruct);
  306. }
  307. // Keep it to verify if HAL_RCC_OscConfig didn't exit with a timeout
  308. if (__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY)) {
  309. PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_LSE;
  310. HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
  311. if (init_uart(obj) == HAL_OK) {
  312. return;
  313. }
  314. }
  315. }
  316. #endif
  317. #if ((MBED_CONF_TARGET_LPUART_CLOCK_SOURCE) & USE_LPUART_CLK_PCLK1)
  318. PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK1;
  319. HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
  320. if (init_uart(obj) == HAL_OK) {
  321. return;
  322. }
  323. #endif
  324. #if ((MBED_CONF_TARGET_LPUART_CLOCK_SOURCE) & USE_LPUART_CLK_HSI)
  325. // Enable HSI in case it is not already done
  326. if (!__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY)) {
  327. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  328. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  329. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  330. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_OFF;
  331. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  332. HAL_RCC_OscConfig(&RCC_OscInitStruct);
  333. }
  334. // Keep it to verify if HAL_RCC_OscConfig didn't exit with a timeout
  335. if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY)) {
  336. PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_HSI;
  337. HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
  338. if (init_uart(obj) == HAL_OK) {
  339. return;
  340. }
  341. }
  342. #endif
  343. // Last chance using SYSCLK
  344. PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_SYSCLK;
  345. HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
  346. }
  347. #endif /* LPUART1_BASE */
  348. if (init_uart(obj) != HAL_OK) {
  349. debug("Cannot initialize UART with baud rate %u\n", baudrate);
  350. }
  351. }
  352. void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits)
  353. {
  354. struct serial_s *obj_s = SERIAL_S(obj);
  355. switch (parity) {
  356. case ParityOdd:
  357. obj_s->parity = UART_PARITY_ODD;
  358. break;
  359. case ParityEven:
  360. obj_s->parity = UART_PARITY_EVEN;
  361. break;
  362. default: // ParityNone
  363. case ParityForced0: // unsupported!
  364. case ParityForced1: // unsupported!
  365. obj_s->parity = UART_PARITY_NONE;
  366. break;
  367. }
  368. switch (data_bits) {
  369. case 7:
  370. if (parity != UART_PARITY_NONE) {
  371. obj_s->databits = UART_WORDLENGTH_8B;
  372. } else {
  373. #if defined UART_WORDLENGTH_7B
  374. obj_s->databits = UART_WORDLENGTH_7B;
  375. #else
  376. error("7-bit data format without parity is not supported");
  377. #endif
  378. }
  379. break;
  380. case 8:
  381. if (parity != UART_PARITY_NONE) {
  382. obj_s->databits = UART_WORDLENGTH_9B;
  383. } else {
  384. obj_s->databits = UART_WORDLENGTH_8B;
  385. }
  386. break;
  387. case 9:
  388. if (parity != UART_PARITY_NONE) {
  389. error("Parity is not supported with 9-bit data format");
  390. } else {
  391. obj_s->databits = UART_WORDLENGTH_9B;
  392. }
  393. break;
  394. default:
  395. error("Only 7, 8 or 9-bit data formats are supported");
  396. break;
  397. }
  398. if (stop_bits == 2) {
  399. obj_s->stopbits = UART_STOPBITS_2;
  400. } else {
  401. obj_s->stopbits = UART_STOPBITS_1;
  402. }
  403. init_uart(obj);
  404. }
  405. /******************************************************************************
  406. * READ/WRITE
  407. ******************************************************************************/
  408. int serial_readable(serial_t *obj)
  409. {
  410. struct serial_s *obj_s = SERIAL_S(obj);
  411. UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
  412. /* To avoid a target blocking case, let's check for
  413. * possible OVERRUN error and discard it
  414. */
  415. if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE)) {
  416. __HAL_UART_CLEAR_OREFLAG(huart);
  417. }
  418. // Check if data is received
  419. return (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) ? 1 : 0;
  420. }
  421. int serial_writable(serial_t *obj)
  422. {
  423. struct serial_s *obj_s = SERIAL_S(obj);
  424. UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
  425. // Check if data is transmitted
  426. return (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) ? 1 : 0;
  427. }
  428. void serial_pinout_tx(PinName tx)
  429. {
  430. pinmap_pinout(tx, PinMap_UART_TX);
  431. }
  432. void serial_break_clear(serial_t *obj)
  433. {
  434. (void)obj;
  435. }
  436. /******************************************************************************
  437. * UTILITY FUNCTIONS
  438. ******************************************************************************/
  439. HAL_StatusTypeDef init_uart(serial_t *obj)
  440. {
  441. struct serial_s *obj_s = SERIAL_S(obj);
  442. UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
  443. huart->Instance = (USART_TypeDef *)(obj_s->uart);
  444. huart->Init.BaudRate = obj_s->baudrate;
  445. huart->Init.WordLength = obj_s->databits;
  446. huart->Init.StopBits = obj_s->stopbits;
  447. huart->Init.Parity = obj_s->parity;
  448. #if DEVICE_SERIAL_FC
  449. huart->Init.HwFlowCtl = obj_s->hw_flow_ctl;
  450. #else
  451. huart->Init.HwFlowCtl = UART_HWCONTROL_NONE;
  452. #endif
  453. huart->Init.OverSampling = UART_OVERSAMPLING_16;
  454. huart->TxXferCount = 0;
  455. huart->TxXferSize = 0;
  456. huart->RxXferCount = 0;
  457. huart->RxXferSize = 0;
  458. if (obj_s->pin_rx == NC) {
  459. huart->Init.Mode = UART_MODE_TX;
  460. } else if (obj_s->pin_tx == NC) {
  461. huart->Init.Mode = UART_MODE_RX;
  462. } else {
  463. huart->Init.Mode = UART_MODE_TX_RX;
  464. }
  465. #if defined(LPUART1_BASE)
  466. if (huart->Instance == LPUART1) {
  467. if (obj_s->baudrate <= 9600) {
  468. HAL_UARTEx_EnableClockStopMode(huart);
  469. HAL_UARTEx_EnableStopMode(huart);
  470. } else {
  471. HAL_UARTEx_DisableClockStopMode(huart);
  472. HAL_UARTEx_DisableStopMode(huart);
  473. }
  474. }
  475. #endif
  476. return HAL_UART_Init(huart);
  477. }
  478. int8_t get_uart_index(UARTName uart_name)
  479. {
  480. uint8_t index = 0;
  481. #if defined(USART1_BASE)
  482. if (uart_name == UART_1) {
  483. return index;
  484. }
  485. index++;
  486. #endif
  487. #if defined(USART2_BASE)
  488. if (uart_name == UART_2) {
  489. return index;
  490. }
  491. index++;
  492. #endif
  493. #if defined(USART3_BASE)
  494. if (uart_name == UART_3) {
  495. return index;
  496. }
  497. index++;
  498. #endif
  499. #if defined(UART4_BASE)
  500. if (uart_name == UART_4) {
  501. return index;
  502. }
  503. index++;
  504. #endif
  505. #if defined(USART4_BASE)
  506. if (uart_name == UART_4) {
  507. return index;
  508. }
  509. index++;
  510. #endif
  511. #if defined(UART5_BASE)
  512. if (uart_name == UART_5) {
  513. return index;
  514. }
  515. index++;
  516. #endif
  517. #if defined(USART5_BASE)
  518. if (uart_name == UART_5) {
  519. return index;
  520. }
  521. index++;
  522. #endif
  523. #if defined(USART6_BASE)
  524. if (uart_name == UART_6) {
  525. return index;
  526. }
  527. index++;
  528. #endif
  529. #if defined(UART7_BASE)
  530. if (uart_name == UART_7) {
  531. return index;
  532. }
  533. index++;
  534. #endif
  535. #if defined(USART7_BASE)
  536. if (uart_name == UART_7) {
  537. return index;
  538. }
  539. index++;
  540. #endif
  541. #if defined(UART8_BASE)
  542. if (uart_name == UART_8) {
  543. return index;
  544. }
  545. index++;
  546. #endif
  547. #if defined(USART8_BASE)
  548. if (uart_name == UART_8) {
  549. return index;
  550. }
  551. index++;
  552. #endif
  553. #if defined(UART9_BASE)
  554. if (uart_name == UART_9) {
  555. return index;
  556. }
  557. index++;
  558. #endif
  559. #if defined(UART10_BASE)
  560. if (uart_name == UART_10) {
  561. return index;
  562. }
  563. index++;
  564. #endif
  565. #if defined(LPUART1_BASE)
  566. if (uart_name == LPUART_1) {
  567. return index;
  568. }
  569. index++;
  570. #endif
  571. return -1;
  572. }
  573. /* Function to protect deep sleep while a seral Tx is ongoing on not complete
  574. * yet. Returns 1 if there is at least 1 serial instance with ongoing ransfer
  575. * 0 otherwise.
  576. */
  577. int serial_is_tx_ongoing(void) {
  578. int TxOngoing = 0;
  579. #if defined(USART1_BASE)
  580. if (LL_USART_IsEnabled(USART1) && !LL_USART_IsActiveFlag_TC(USART1)) {
  581. TxOngoing |= 1;
  582. }
  583. #endif
  584. #if defined(USART2_BASE)
  585. if (LL_USART_IsEnabled(USART2) && !LL_USART_IsActiveFlag_TC(USART2)) {
  586. TxOngoing |= 1;
  587. }
  588. #endif
  589. #if defined(USART3_BASE)
  590. if (LL_USART_IsEnabled(USART3) && !LL_USART_IsActiveFlag_TC(USART3)) {
  591. TxOngoing |= 1;
  592. }
  593. #endif
  594. #if defined(UART4_BASE)
  595. if (LL_USART_IsEnabled(UART4) && !LL_USART_IsActiveFlag_TC(UART4)) {
  596. TxOngoing |= 1;
  597. }
  598. #endif
  599. #if defined(USART4_BASE)
  600. if (LL_USART_IsEnabled(USART4) && !LL_USART_IsActiveFlag_TC(USART4)) {
  601. TxOngoing |= 1;
  602. }
  603. #endif
  604. #if defined(UART5_BASE)
  605. if (LL_USART_IsEnabled(UART5) && !LL_USART_IsActiveFlag_TC(UART5)) {
  606. TxOngoing |= 1;
  607. }
  608. #endif
  609. #if defined(USART5_BASE)
  610. if (LL_USART_IsEnabled(USART5) && !LL_USART_IsActiveFlag_TC(USART5)) {
  611. TxOngoing |= 1;
  612. }
  613. #endif
  614. #if defined(USART6_BASE)
  615. if (LL_USART_IsEnabled(USART6) && !LL_USART_IsActiveFlag_TC(USART6)) {
  616. TxOngoing |= 1;
  617. }
  618. #endif
  619. #if defined(UART7_BASE)
  620. if (LL_USART_IsEnabled(UART7) && !LL_USART_IsActiveFlag_TC(UART7)) {
  621. TxOngoing |= 1;
  622. }
  623. #endif
  624. #if defined(USART7_BASE)
  625. if (LL_USART_IsEnabled(USART7) && !LL_USART_IsActiveFlag_TC(USART7)) {
  626. TxOngoing |= 1;
  627. }
  628. #endif
  629. #if defined(UART8_BASE)
  630. if (LL_USART_IsEnabled(UART8) && !LL_USART_IsActiveFlag_TC(UART8)) {
  631. TxOngoing |= 1;
  632. }
  633. #endif
  634. #if defined(USART8_BASE)
  635. if (LL_USART_IsEnabled(USART8) && !LL_USART_IsActiveFlag_TC(USART8)) {
  636. TxOngoing |= 1;
  637. }
  638. #endif
  639. #if defined(UART9_BASE)
  640. if (LL_USART_IsEnabled(UART9) && !LL_USART_IsActiveFlag_TC(UART9)) {
  641. TxOngoing |= 1;
  642. }
  643. #endif
  644. #if defined(UART10_BASE)
  645. if (LL_USART_IsEnabled(UART10) && !LL_USART_IsActiveFlag_TC(UART10)) {
  646. TxOngoing |= 1;
  647. }
  648. #endif
  649. #if defined(LPUART1_BASE)
  650. if (LL_USART_IsEnabled(LPUART1) && !LL_USART_IsActiveFlag_TC(LPUART1)) {
  651. TxOngoing |= 1;
  652. }
  653. #endif
  654. /* If Tx is ongoing, then transfer is */
  655. return TxOngoing;
  656. }
  657. #endif /* DEVICE_SERIAL */