Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 Open-RnD Sp. z o.o. |
Erwan Gouriou | 8c079e9 | 2016-11-14 11:53:52 +0100 | [diff] [blame] | 3 | * Copyright (c) 2016 Linaro Limited. |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 4 | * |
David B. Kinder | ac74d8b | 2017-01-18 17:01:01 -0800 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Kumar Gala | 989484b | 2020-03-24 14:28:48 -0500 | [diff] [blame] | 8 | #define DT_DRV_COMPAT st_stm32_uart |
| 9 | |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 10 | /** |
Ilya Tagunov | 84cffc7 | 2018-03-20 20:44:45 +0300 | [diff] [blame] | 11 | * @brief Driver for UART port on STM32 family processor. |
Georgij Cernysiov | 78eed34 | 2019-03-15 19:51:09 +0100 | [diff] [blame] | 12 | * @note LPUART and U(S)ART have the same base and |
| 13 | * majority of operations are performed the same way. |
| 14 | * Please validate for newly added series. |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 15 | */ |
| 16 | |
Flavio Santes | b04cdcd | 2016-12-04 14:59:37 -0600 | [diff] [blame] | 17 | #include <kernel.h> |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 18 | #include <arch/cpu.h> |
Anas Nashif | 5eb90ec | 2019-06-26 10:33:39 -0400 | [diff] [blame] | 19 | #include <sys/__assert.h> |
Kumar Gala | aa2bdbe | 2018-10-31 12:44:45 -0500 | [diff] [blame] | 20 | #include <soc.h> |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 21 | #include <init.h> |
Anas Nashif | d1b2718 | 2019-06-25 15:54:01 -0400 | [diff] [blame] | 22 | #include <drivers/uart.h> |
Anas Nashif | 17ddd17 | 2019-06-25 15:53:47 -0400 | [diff] [blame] | 23 | #include <drivers/clock_control.h> |
Gerard Marull-Paretas | e3f4907 | 2021-09-09 22:41:35 +0200 | [diff] [blame] | 24 | #include <pm/pm.h> |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 25 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 26 | #ifdef CONFIG_UART_ASYNC_API |
Francois Ramu | 30fd022 | 2021-06-18 15:52:30 +0200 | [diff] [blame] | 27 | #include <drivers/dma/dma_stm32.h> |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 28 | #include <drivers/dma.h> |
| 29 | #endif |
| 30 | |
Anas Nashif | 397d29d | 2017-06-17 11:30:47 -0400 | [diff] [blame] | 31 | #include <linker/sections.h> |
Peter Bigot | 0b0d2e64 | 2020-01-25 05:34:53 -0600 | [diff] [blame] | 32 | #include <drivers/clock_control/stm32_clock_control.h> |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 33 | #include "uart_stm32.h" |
| 34 | |
Gerard Marull-Paretas | e83fab3 | 2020-10-03 23:58:36 +0200 | [diff] [blame] | 35 | #include <stm32_ll_usart.h> |
| 36 | #include <stm32_ll_lpuart.h> |
| 37 | |
Francois Ramu | d3ffa8d | 2019-11-12 16:13:03 +0100 | [diff] [blame] | 38 | #include <logging/log.h> |
Pete Dietl | 060e39e | 2022-03-04 12:56:05 -0800 | [diff] [blame^] | 39 | LOG_MODULE_REGISTER(uart_stm32, CONFIG_UART_LOG_LEVEL); |
Francois Ramu | d3ffa8d | 2019-11-12 16:13:03 +0100 | [diff] [blame] | 40 | |
Kumar Gala | c2135f8 | 2020-05-07 12:22:26 -0500 | [diff] [blame] | 41 | #define HAS_LPUART_1 (DT_NODE_HAS_COMPAT_STATUS(DT_NODELABEL(lpuart1), \ |
| 42 | st_stm32_lpuart, okay)) |
Erwan Gouriou | 76313f0 | 2020-04-17 12:33:29 +0200 | [diff] [blame] | 43 | |
Yong Cong Sin | a6ebcdd | 2021-10-22 00:02:29 +0800 | [diff] [blame] | 44 | #if HAS_LPUART_1 |
| 45 | #ifdef USART_PRESC_PRESCALER |
| 46 | uint32_t lpuartdiv_calc(const uint64_t clock_rate, const uint16_t presc_idx, |
| 47 | const uint32_t baud_rate) |
| 48 | { |
| 49 | uint64_t lpuartdiv; |
| 50 | |
| 51 | lpuartdiv = clock_rate / LPUART_PRESCALER_TAB[presc_idx]; |
| 52 | lpuartdiv *= LPUART_LPUARTDIV_FREQ_MUL; |
| 53 | lpuartdiv += baud_rate / 2; |
| 54 | lpuartdiv /= baud_rate; |
| 55 | |
| 56 | return (uint32_t)lpuartdiv; |
| 57 | } |
| 58 | #else |
| 59 | uint32_t lpuartdiv_calc(const uint64_t clock_rate, const uint32_t baud_rate) |
| 60 | { |
| 61 | uint64_t lpuartdiv; |
| 62 | |
| 63 | lpuartdiv = clock_rate * LPUART_LPUARTDIV_FREQ_MUL; |
| 64 | lpuartdiv += baud_rate / 2; |
| 65 | lpuartdiv /= baud_rate; |
| 66 | |
| 67 | return (uint32_t)lpuartdiv; |
| 68 | } |
| 69 | #endif /* USART_PRESC_PRESCALER */ |
| 70 | #endif /* HAS_LPUART_1 */ |
| 71 | |
Erwan Gouriou | 8c079e9 | 2016-11-14 11:53:52 +0100 | [diff] [blame] | 72 | #define TIMEOUT 1000 |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 73 | |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 74 | #ifdef CONFIG_PM |
| 75 | static void uart_stm32_pm_constraint_set(const struct device *dev) |
| 76 | { |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 77 | struct uart_stm32_data *data = dev->data; |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 78 | |
| 79 | if (!data->pm_constraint_on) { |
| 80 | data->pm_constraint_on = true; |
| 81 | pm_constraint_set(PM_STATE_SUSPEND_TO_IDLE); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | static void uart_stm32_pm_constraint_release(const struct device *dev) |
| 86 | { |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 87 | struct uart_stm32_data *data = dev->data; |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 88 | |
| 89 | if (data->pm_constraint_on) { |
| 90 | data->pm_constraint_on = false; |
| 91 | pm_constraint_release(PM_STATE_SUSPEND_TO_IDLE); |
| 92 | } |
| 93 | } |
| 94 | #endif /* CONFIG_PM */ |
| 95 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 96 | static inline void uart_stm32_set_baudrate(const struct device *dev, |
| 97 | uint32_t baud_rate) |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 98 | { |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 99 | const struct uart_stm32_config *config = dev->config; |
| 100 | struct uart_stm32_data *data = dev->data; |
Erwan Gouriou | 69a2896 | 2019-05-20 17:15:02 +0200 | [diff] [blame] | 101 | |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 102 | uint32_t clock_rate; |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 103 | |
| 104 | /* Get clock rate */ |
Francois Ramu | d3ffa8d | 2019-11-12 16:13:03 +0100 | [diff] [blame] | 105 | if (clock_control_get_rate(data->clock, |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 106 | (clock_control_subsys_t *)&config->pclken, |
Francois Ramu | d3ffa8d | 2019-11-12 16:13:03 +0100 | [diff] [blame] | 107 | &clock_rate) < 0) { |
| 108 | LOG_ERR("Failed call clock_control_get_rate"); |
| 109 | return; |
| 110 | } |
Erwan Gouriou | 69a2896 | 2019-05-20 17:15:02 +0200 | [diff] [blame] | 111 | |
Erwan Gouriou | 76313f0 | 2020-04-17 12:33:29 +0200 | [diff] [blame] | 112 | #if HAS_LPUART_1 |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 113 | if (IS_LPUART_INSTANCE(config->usart)) { |
Yong Cong Sin | a6ebcdd | 2021-10-22 00:02:29 +0800 | [diff] [blame] | 114 | uint32_t lpuartdiv; |
| 115 | #ifdef USART_PRESC_PRESCALER |
| 116 | uint8_t presc_idx; |
| 117 | uint32_t presc_val; |
| 118 | |
| 119 | for (presc_idx = 0; presc_idx < ARRAY_SIZE(LPUART_PRESCALER_TAB); presc_idx++) { |
| 120 | lpuartdiv = lpuartdiv_calc(clock_rate, presc_idx, baud_rate); |
| 121 | if (lpuartdiv >= LPUART_BRR_MIN_VALUE && lpuartdiv <= LPUART_BRR_MASK) { |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | if (presc_idx == ARRAY_SIZE(LPUART_PRESCALER_TAB)) { |
| 127 | LOG_ERR("Unable to set %s to %d", dev->name, baud_rate); |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | presc_val = presc_idx << USART_PRESC_PRESCALER_Pos; |
| 132 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 133 | LL_LPUART_SetPrescaler(config->usart, presc_val); |
Yong Cong Sin | a6ebcdd | 2021-10-22 00:02:29 +0800 | [diff] [blame] | 134 | #else |
| 135 | lpuartdiv = lpuartdiv_calc(clock_rate, baud_rate); |
| 136 | if (lpuartdiv < LPUART_BRR_MIN_VALUE || lpuartdiv > LPUART_BRR_MASK) { |
| 137 | LOG_ERR("Unable to set %s to %d", dev->name, baud_rate); |
| 138 | return; |
| 139 | } |
| 140 | #endif /* USART_PRESC_PRESCALER */ |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 141 | LL_LPUART_SetBaudRate(config->usart, |
Erwan Gouriou | 69a2896 | 2019-05-20 17:15:02 +0200 | [diff] [blame] | 142 | clock_rate, |
| 143 | #ifdef USART_PRESC_PRESCALER |
Yong Cong Sin | a6ebcdd | 2021-10-22 00:02:29 +0800 | [diff] [blame] | 144 | presc_val, |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 145 | #endif |
Erwan Gouriou | 69a2896 | 2019-05-20 17:15:02 +0200 | [diff] [blame] | 146 | baud_rate); |
| 147 | } else { |
Erwan Gouriou | 76313f0 | 2020-04-17 12:33:29 +0200 | [diff] [blame] | 148 | #endif /* HAS_LPUART_1 */ |
Erwan Gouriou | 37c7b89 | 2021-01-08 10:54:46 +0100 | [diff] [blame] | 149 | #ifdef USART_CR1_OVER8 |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 150 | LL_USART_SetOverSampling(config->usart, |
Erwan Gouriou | 37c7b89 | 2021-01-08 10:54:46 +0100 | [diff] [blame] | 151 | LL_USART_OVERSAMPLING_16); |
| 152 | #endif |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 153 | LL_USART_SetBaudRate(config->usart, |
Erwan Gouriou | 69a2896 | 2019-05-20 17:15:02 +0200 | [diff] [blame] | 154 | clock_rate, |
| 155 | #ifdef USART_PRESC_PRESCALER |
| 156 | LL_USART_PRESCALER_DIV1, |
| 157 | #endif |
| 158 | #ifdef USART_CR1_OVER8 |
| 159 | LL_USART_OVERSAMPLING_16, |
| 160 | #endif |
| 161 | baud_rate); |
| 162 | |
Erwan Gouriou | 76313f0 | 2020-04-17 12:33:29 +0200 | [diff] [blame] | 163 | #if HAS_LPUART_1 |
Erwan Gouriou | 69a2896 | 2019-05-20 17:15:02 +0200 | [diff] [blame] | 164 | } |
Erwan Gouriou | 76313f0 | 2020-04-17 12:33:29 +0200 | [diff] [blame] | 165 | #endif /* HAS_LPUART_1 */ |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 168 | static inline void uart_stm32_set_parity(const struct device *dev, |
| 169 | uint32_t parity) |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 170 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 171 | const struct uart_stm32_config *config = dev->config; |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 172 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 173 | LL_USART_SetParity(config->usart, parity); |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 176 | static inline uint32_t uart_stm32_get_parity(const struct device *dev) |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 177 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 178 | const struct uart_stm32_config *config = dev->config; |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 179 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 180 | return LL_USART_GetParity(config->usart); |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 183 | static inline void uart_stm32_set_stopbits(const struct device *dev, |
| 184 | uint32_t stopbits) |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 185 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 186 | const struct uart_stm32_config *config = dev->config; |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 187 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 188 | LL_USART_SetStopBitsLength(config->usart, stopbits); |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 191 | static inline uint32_t uart_stm32_get_stopbits(const struct device *dev) |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 192 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 193 | const struct uart_stm32_config *config = dev->config; |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 194 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 195 | return LL_USART_GetStopBitsLength(config->usart); |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 198 | static inline void uart_stm32_set_databits(const struct device *dev, |
| 199 | uint32_t databits) |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 200 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 201 | const struct uart_stm32_config *config = dev->config; |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 202 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 203 | LL_USART_SetDataWidth(config->usart, databits); |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 206 | static inline uint32_t uart_stm32_get_databits(const struct device *dev) |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 207 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 208 | const struct uart_stm32_config *config = dev->config; |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 209 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 210 | return LL_USART_GetDataWidth(config->usart); |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 213 | static inline void uart_stm32_set_hwctrl(const struct device *dev, |
| 214 | uint32_t hwctrl) |
Georgij Cernysiov | 78eed34 | 2019-03-15 19:51:09 +0100 | [diff] [blame] | 215 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 216 | const struct uart_stm32_config *config = dev->config; |
Georgij Cernysiov | 78eed34 | 2019-03-15 19:51:09 +0100 | [diff] [blame] | 217 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 218 | LL_USART_SetHWFlowCtrl(config->usart, hwctrl); |
Georgij Cernysiov | 78eed34 | 2019-03-15 19:51:09 +0100 | [diff] [blame] | 219 | } |
| 220 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 221 | static inline uint32_t uart_stm32_get_hwctrl(const struct device *dev) |
Georgij Cernysiov | 78eed34 | 2019-03-15 19:51:09 +0100 | [diff] [blame] | 222 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 223 | const struct uart_stm32_config *config = dev->config; |
Georgij Cernysiov | 78eed34 | 2019-03-15 19:51:09 +0100 | [diff] [blame] | 224 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 225 | return LL_USART_GetHWFlowCtrl(config->usart); |
Georgij Cernysiov | 78eed34 | 2019-03-15 19:51:09 +0100 | [diff] [blame] | 226 | } |
| 227 | |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 228 | static inline uint32_t uart_stm32_cfg2ll_parity(enum uart_config_parity parity) |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 229 | { |
| 230 | switch (parity) { |
| 231 | case UART_CFG_PARITY_ODD: |
| 232 | return LL_USART_PARITY_ODD; |
| 233 | case UART_CFG_PARITY_EVEN: |
| 234 | return LL_USART_PARITY_EVEN; |
| 235 | case UART_CFG_PARITY_NONE: |
| 236 | default: |
| 237 | return LL_USART_PARITY_NONE; |
| 238 | } |
| 239 | } |
| 240 | |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 241 | static inline enum uart_config_parity uart_stm32_ll2cfg_parity(uint32_t parity) |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 242 | { |
| 243 | switch (parity) { |
| 244 | case LL_USART_PARITY_ODD: |
| 245 | return UART_CFG_PARITY_ODD; |
| 246 | case LL_USART_PARITY_EVEN: |
| 247 | return UART_CFG_PARITY_EVEN; |
| 248 | case LL_USART_PARITY_NONE: |
| 249 | default: |
| 250 | return UART_CFG_PARITY_NONE; |
| 251 | } |
| 252 | } |
| 253 | |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 254 | static inline uint32_t uart_stm32_cfg2ll_stopbits(enum uart_config_stop_bits sb) |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 255 | { |
| 256 | switch (sb) { |
| 257 | /* Some MCU's don't support 0.5 stop bits */ |
| 258 | #ifdef LL_USART_STOPBITS_0_5 |
| 259 | case UART_CFG_STOP_BITS_0_5: |
| 260 | return LL_USART_STOPBITS_0_5; |
| 261 | #endif /* LL_USART_STOPBITS_0_5 */ |
| 262 | case UART_CFG_STOP_BITS_1: |
| 263 | return LL_USART_STOPBITS_1; |
| 264 | /* Some MCU's don't support 1.5 stop bits */ |
| 265 | #ifdef LL_USART_STOPBITS_1_5 |
| 266 | case UART_CFG_STOP_BITS_1_5: |
| 267 | return LL_USART_STOPBITS_1_5; |
| 268 | #endif /* LL_USART_STOPBITS_1_5 */ |
| 269 | case UART_CFG_STOP_BITS_2: |
| 270 | default: |
| 271 | return LL_USART_STOPBITS_2; |
| 272 | } |
| 273 | } |
| 274 | |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 275 | static inline enum uart_config_stop_bits uart_stm32_ll2cfg_stopbits(uint32_t sb) |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 276 | { |
| 277 | switch (sb) { |
| 278 | /* Some MCU's don't support 0.5 stop bits */ |
| 279 | #ifdef LL_USART_STOPBITS_0_5 |
| 280 | case LL_USART_STOPBITS_0_5: |
| 281 | return UART_CFG_STOP_BITS_0_5; |
| 282 | #endif /* LL_USART_STOPBITS_0_5 */ |
| 283 | case LL_USART_STOPBITS_1: |
| 284 | return UART_CFG_STOP_BITS_1; |
| 285 | /* Some MCU's don't support 1.5 stop bits */ |
| 286 | #ifdef LL_USART_STOPBITS_1_5 |
| 287 | case LL_USART_STOPBITS_1_5: |
| 288 | return UART_CFG_STOP_BITS_1_5; |
| 289 | #endif /* LL_USART_STOPBITS_1_5 */ |
| 290 | case LL_USART_STOPBITS_2: |
| 291 | default: |
| 292 | return UART_CFG_STOP_BITS_2; |
| 293 | } |
| 294 | } |
| 295 | |
Nicolas VINCENT | 573eec1 | 2021-03-10 13:23:00 +0100 | [diff] [blame] | 296 | static inline uint32_t uart_stm32_cfg2ll_databits(enum uart_config_data_bits db, |
| 297 | enum uart_config_parity p) |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 298 | { |
| 299 | switch (db) { |
Benoit Leforestier | 9fee67d | 2019-03-22 14:19:57 +0100 | [diff] [blame] | 300 | /* Some MCU's don't support 7B or 9B datawidth */ |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 301 | #ifdef LL_USART_DATAWIDTH_7B |
| 302 | case UART_CFG_DATA_BITS_7: |
Nicolas VINCENT | 573eec1 | 2021-03-10 13:23:00 +0100 | [diff] [blame] | 303 | if (p == UART_CFG_PARITY_NONE) { |
| 304 | return LL_USART_DATAWIDTH_7B; |
| 305 | } else { |
| 306 | return LL_USART_DATAWIDTH_8B; |
| 307 | } |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 308 | #endif /* LL_USART_DATAWIDTH_7B */ |
Benoit Leforestier | 9fee67d | 2019-03-22 14:19:57 +0100 | [diff] [blame] | 309 | #ifdef LL_USART_DATAWIDTH_9B |
| 310 | case UART_CFG_DATA_BITS_9: |
| 311 | return LL_USART_DATAWIDTH_9B; |
| 312 | #endif /* LL_USART_DATAWIDTH_9B */ |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 313 | case UART_CFG_DATA_BITS_8: |
| 314 | default: |
Nicolas VINCENT | 573eec1 | 2021-03-10 13:23:00 +0100 | [diff] [blame] | 315 | if (p == UART_CFG_PARITY_NONE) { |
| 316 | return LL_USART_DATAWIDTH_8B; |
| 317 | #ifdef LL_USART_DATAWIDTH_9B |
| 318 | } else { |
| 319 | return LL_USART_DATAWIDTH_9B; |
| 320 | #endif |
| 321 | } |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 322 | return LL_USART_DATAWIDTH_8B; |
| 323 | } |
| 324 | } |
| 325 | |
Nicolas VINCENT | 573eec1 | 2021-03-10 13:23:00 +0100 | [diff] [blame] | 326 | static inline enum uart_config_data_bits uart_stm32_ll2cfg_databits(uint32_t db, |
| 327 | uint32_t p) |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 328 | { |
| 329 | switch (db) { |
Benoit Leforestier | 9fee67d | 2019-03-22 14:19:57 +0100 | [diff] [blame] | 330 | /* Some MCU's don't support 7B or 9B datawidth */ |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 331 | #ifdef LL_USART_DATAWIDTH_7B |
| 332 | case LL_USART_DATAWIDTH_7B: |
Nicolas VINCENT | 573eec1 | 2021-03-10 13:23:00 +0100 | [diff] [blame] | 333 | if (p == LL_USART_PARITY_NONE) { |
| 334 | return UART_CFG_DATA_BITS_7; |
| 335 | } else { |
| 336 | return UART_CFG_DATA_BITS_6; |
| 337 | } |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 338 | #endif /* LL_USART_DATAWIDTH_7B */ |
Benoit Leforestier | 9fee67d | 2019-03-22 14:19:57 +0100 | [diff] [blame] | 339 | #ifdef LL_USART_DATAWIDTH_9B |
| 340 | case LL_USART_DATAWIDTH_9B: |
Nicolas VINCENT | 573eec1 | 2021-03-10 13:23:00 +0100 | [diff] [blame] | 341 | if (p == LL_USART_PARITY_NONE) { |
| 342 | return UART_CFG_DATA_BITS_9; |
| 343 | } else { |
| 344 | return UART_CFG_DATA_BITS_8; |
| 345 | } |
Benoit Leforestier | 9fee67d | 2019-03-22 14:19:57 +0100 | [diff] [blame] | 346 | #endif /* LL_USART_DATAWIDTH_9B */ |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 347 | case LL_USART_DATAWIDTH_8B: |
| 348 | default: |
Nicolas VINCENT | 573eec1 | 2021-03-10 13:23:00 +0100 | [diff] [blame] | 349 | if (p == LL_USART_PARITY_NONE) { |
| 350 | return UART_CFG_DATA_BITS_8; |
| 351 | } else { |
| 352 | return UART_CFG_DATA_BITS_7; |
| 353 | } |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
Georgij Cernysiov | 78eed34 | 2019-03-15 19:51:09 +0100 | [diff] [blame] | 357 | /** |
| 358 | * @brief Get LL hardware flow control define from |
| 359 | * Zephyr hardware flow control option. |
| 360 | * @note Supports only UART_CFG_FLOW_CTRL_RTS_CTS. |
| 361 | * @param fc: Zephyr hardware flow control option. |
| 362 | * @retval LL_USART_HWCONTROL_RTS_CTS, or LL_USART_HWCONTROL_NONE. |
| 363 | */ |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 364 | static inline uint32_t uart_stm32_cfg2ll_hwctrl(enum uart_config_flow_control fc) |
Georgij Cernysiov | 78eed34 | 2019-03-15 19:51:09 +0100 | [diff] [blame] | 365 | { |
| 366 | if (fc == UART_CFG_FLOW_CTRL_RTS_CTS) { |
| 367 | return LL_USART_HWCONTROL_RTS_CTS; |
| 368 | } |
| 369 | |
| 370 | return LL_USART_HWCONTROL_NONE; |
| 371 | } |
| 372 | |
| 373 | /** |
Yannis Damigos | a4b448e | 2020-02-06 20:36:03 +0200 | [diff] [blame] | 374 | * @brief Get Zephyr hardware flow control option from |
Georgij Cernysiov | 78eed34 | 2019-03-15 19:51:09 +0100 | [diff] [blame] | 375 | * LL hardware flow control define. |
| 376 | * @note Supports only LL_USART_HWCONTROL_RTS_CTS. |
Yannis Damigos | a4b448e | 2020-02-06 20:36:03 +0200 | [diff] [blame] | 377 | * @param fc: LL hardware flow control definition. |
Kumar Gala | 6115346 | 2020-02-06 11:30:05 -0600 | [diff] [blame] | 378 | * @retval UART_CFG_FLOW_CTRL_RTS_CTS, or UART_CFG_FLOW_CTRL_NONE. |
Georgij Cernysiov | 78eed34 | 2019-03-15 19:51:09 +0100 | [diff] [blame] | 379 | */ |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 380 | static inline enum uart_config_flow_control uart_stm32_ll2cfg_hwctrl(uint32_t fc) |
Georgij Cernysiov | 78eed34 | 2019-03-15 19:51:09 +0100 | [diff] [blame] | 381 | { |
| 382 | if (fc == LL_USART_HWCONTROL_RTS_CTS) { |
| 383 | return UART_CFG_FLOW_CTRL_RTS_CTS; |
| 384 | } |
| 385 | |
Kumar Gala | 6115346 | 2020-02-06 11:30:05 -0600 | [diff] [blame] | 386 | return UART_CFG_FLOW_CTRL_NONE; |
Georgij Cernysiov | 78eed34 | 2019-03-15 19:51:09 +0100 | [diff] [blame] | 387 | } |
| 388 | |
Daniel Leung | 4e1692f | 2021-05-26 12:33:37 -0700 | [diff] [blame] | 389 | #ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 390 | static int uart_stm32_configure(const struct device *dev, |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 391 | const struct uart_config *cfg) |
| 392 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 393 | const struct uart_stm32_config *config = dev->config; |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 394 | struct uart_stm32_data *data = dev->data; |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 395 | const uint32_t parity = uart_stm32_cfg2ll_parity(cfg->parity); |
| 396 | const uint32_t stopbits = uart_stm32_cfg2ll_stopbits(cfg->stop_bits); |
Nicolas VINCENT | 573eec1 | 2021-03-10 13:23:00 +0100 | [diff] [blame] | 397 | const uint32_t databits = uart_stm32_cfg2ll_databits(cfg->data_bits, |
| 398 | cfg->parity); |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 399 | const uint32_t flowctrl = uart_stm32_cfg2ll_hwctrl(cfg->flow_ctrl); |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 400 | |
| 401 | /* Hardware doesn't support mark or space parity */ |
Erwan Gouriou | 5e56170 | 2021-02-11 09:55:38 +0100 | [diff] [blame] | 402 | if ((cfg->parity == UART_CFG_PARITY_MARK) || |
| 403 | (cfg->parity == UART_CFG_PARITY_SPACE)) { |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 404 | return -ENOTSUP; |
| 405 | } |
| 406 | |
Nicolas VINCENT | 573eec1 | 2021-03-10 13:23:00 +0100 | [diff] [blame] | 407 | /* Driver does not supports parity + 9 databits */ |
| 408 | if ((cfg->parity != UART_CFG_PARITY_NONE) && |
| 409 | (cfg->data_bits == UART_CFG_DATA_BITS_9)) { |
| 410 | return -ENOTSUP; |
| 411 | } |
| 412 | |
Erwan Gouriou | 76313f0 | 2020-04-17 12:33:29 +0200 | [diff] [blame] | 413 | #if defined(LL_USART_STOPBITS_0_5) && HAS_LPUART_1 |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 414 | if (IS_LPUART_INSTANCE(config->usart) && |
Erwan Gouriou | 5e56170 | 2021-02-11 09:55:38 +0100 | [diff] [blame] | 415 | (cfg->stop_bits == UART_CFG_STOP_BITS_0_5)) { |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 416 | return -ENOTSUP; |
| 417 | } |
| 418 | #else |
Erwan Gouriou | 5e56170 | 2021-02-11 09:55:38 +0100 | [diff] [blame] | 419 | if (cfg->stop_bits == UART_CFG_STOP_BITS_0_5) { |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 420 | return -ENOTSUP; |
| 421 | } |
| 422 | #endif |
| 423 | |
Erwan Gouriou | 76313f0 | 2020-04-17 12:33:29 +0200 | [diff] [blame] | 424 | #if defined(LL_USART_STOPBITS_1_5) && HAS_LPUART_1 |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 425 | if (IS_LPUART_INSTANCE(config->usart) && |
Erwan Gouriou | 5e56170 | 2021-02-11 09:55:38 +0100 | [diff] [blame] | 426 | (cfg->stop_bits == UART_CFG_STOP_BITS_1_5)) { |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 427 | return -ENOTSUP; |
| 428 | } |
| 429 | #else |
Erwan Gouriou | 5e56170 | 2021-02-11 09:55:38 +0100 | [diff] [blame] | 430 | if (cfg->stop_bits == UART_CFG_STOP_BITS_1_5) { |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 431 | return -ENOTSUP; |
| 432 | } |
| 433 | #endif |
| 434 | |
Benoit Leforestier | 9fee67d | 2019-03-22 14:19:57 +0100 | [diff] [blame] | 435 | /* Driver doesn't support 5 or 6 databits and potentially 7 or 9 */ |
Erwan Gouriou | 5e56170 | 2021-02-11 09:55:38 +0100 | [diff] [blame] | 436 | if ((cfg->data_bits == UART_CFG_DATA_BITS_5) || |
| 437 | (cfg->data_bits == UART_CFG_DATA_BITS_6) |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 438 | #ifndef LL_USART_DATAWIDTH_7B |
Erwan Gouriou | 5e56170 | 2021-02-11 09:55:38 +0100 | [diff] [blame] | 439 | || (cfg->data_bits == UART_CFG_DATA_BITS_7) |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 440 | #endif /* LL_USART_DATAWIDTH_7B */ |
Erwan Gouriou | 5e56170 | 2021-02-11 09:55:38 +0100 | [diff] [blame] | 441 | || (cfg->data_bits == UART_CFG_DATA_BITS_9)) { |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 442 | return -ENOTSUP; |
| 443 | } |
| 444 | |
Georgij Cernysiov | 78eed34 | 2019-03-15 19:51:09 +0100 | [diff] [blame] | 445 | /* Driver supports only RTS CTS flow control */ |
Erwan Gouriou | 5e56170 | 2021-02-11 09:55:38 +0100 | [diff] [blame] | 446 | if (cfg->flow_ctrl != UART_CFG_FLOW_CTRL_NONE) { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 447 | if (!IS_UART_HWFLOW_INSTANCE(config->usart) || |
Georgij Cernysiov | 78eed34 | 2019-03-15 19:51:09 +0100 | [diff] [blame] | 448 | UART_CFG_FLOW_CTRL_RTS_CTS != cfg->flow_ctrl) { |
| 449 | return -ENOTSUP; |
| 450 | } |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 451 | } |
| 452 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 453 | LL_USART_Disable(config->usart); |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 454 | |
| 455 | if (parity != uart_stm32_get_parity(dev)) { |
| 456 | uart_stm32_set_parity(dev, parity); |
| 457 | } |
| 458 | |
| 459 | if (stopbits != uart_stm32_get_stopbits(dev)) { |
| 460 | uart_stm32_set_stopbits(dev, stopbits); |
| 461 | } |
| 462 | |
| 463 | if (databits != uart_stm32_get_databits(dev)) { |
| 464 | uart_stm32_set_databits(dev, databits); |
| 465 | } |
| 466 | |
Georgij Cernysiov | 78eed34 | 2019-03-15 19:51:09 +0100 | [diff] [blame] | 467 | if (flowctrl != uart_stm32_get_hwctrl(dev)) { |
| 468 | uart_stm32_set_hwctrl(dev, flowctrl); |
| 469 | } |
| 470 | |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 471 | if (cfg->baudrate != data->baud_rate) { |
| 472 | uart_stm32_set_baudrate(dev, cfg->baudrate); |
| 473 | data->baud_rate = cfg->baudrate; |
| 474 | } |
| 475 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 476 | LL_USART_Enable(config->usart); |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 477 | return 0; |
| 478 | }; |
| 479 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 480 | static int uart_stm32_config_get(const struct device *dev, |
| 481 | struct uart_config *cfg) |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 482 | { |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 483 | struct uart_stm32_data *data = dev->data; |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 484 | |
| 485 | cfg->baudrate = data->baud_rate; |
| 486 | cfg->parity = uart_stm32_ll2cfg_parity(uart_stm32_get_parity(dev)); |
| 487 | cfg->stop_bits = uart_stm32_ll2cfg_stopbits( |
| 488 | uart_stm32_get_stopbits(dev)); |
| 489 | cfg->data_bits = uart_stm32_ll2cfg_databits( |
Nicolas VINCENT | 573eec1 | 2021-03-10 13:23:00 +0100 | [diff] [blame] | 490 | uart_stm32_get_databits(dev), uart_stm32_get_parity(dev)); |
Georgij Cernysiov | 78eed34 | 2019-03-15 19:51:09 +0100 | [diff] [blame] | 491 | cfg->flow_ctrl = uart_stm32_ll2cfg_hwctrl( |
| 492 | uart_stm32_get_hwctrl(dev)); |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 493 | return 0; |
| 494 | } |
Daniel Leung | 4e1692f | 2021-05-26 12:33:37 -0700 | [diff] [blame] | 495 | #endif /* CONFIG_UART_USE_RUNTIME_CONFIGURE */ |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 496 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 497 | static int uart_stm32_poll_in(const struct device *dev, unsigned char *c) |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 498 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 499 | const struct uart_stm32_config *config = dev->config; |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 500 | |
Kiril Zyapkov | 7a602fc | 2018-11-02 15:34:56 +0200 | [diff] [blame] | 501 | /* Clear overrun error flag */ |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 502 | if (LL_USART_IsActiveFlag_ORE(config->usart)) { |
| 503 | LL_USART_ClearFlag_ORE(config->usart); |
Kiril Zyapkov | 7a602fc | 2018-11-02 15:34:56 +0200 | [diff] [blame] | 504 | } |
| 505 | |
Francois Ramu | 860bd87 | 2022-01-28 16:52:28 +0100 | [diff] [blame] | 506 | /* |
| 507 | * On stm32 F4X, F1X, and F2X, the RXNE flag is affected (cleared) by |
| 508 | * the uart_err_check function call (on errors flags clearing) |
| 509 | */ |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 510 | if (!LL_USART_IsActiveFlag_RXNE(config->usart)) { |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 511 | return -1; |
| 512 | } |
Erwan Gouriou | da210ba | 2017-09-21 15:20:53 +0200 | [diff] [blame] | 513 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 514 | *c = (unsigned char)LL_USART_ReceiveData8(config->usart); |
Erwan Gouriou | da210ba | 2017-09-21 15:20:53 +0200 | [diff] [blame] | 515 | |
| 516 | return 0; |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 517 | } |
| 518 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 519 | static void uart_stm32_poll_out(const struct device *dev, |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 520 | unsigned char c) |
| 521 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 522 | const struct uart_stm32_config *config = dev->config; |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 523 | #ifdef CONFIG_PM |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 524 | struct uart_stm32_data *data = dev->data; |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 525 | #endif |
| 526 | int key; |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 527 | |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 528 | /* Wait for TXE flag to be raised |
| 529 | * When TXE flag is raised, we lock interrupts to prevent interrupts (notably that of usart) |
| 530 | * or thread switch. Then, we can safely send our character. The character sent will be |
| 531 | * interlaced with the characters potentially send with interrupt transmission API |
| 532 | */ |
Julien D'ascenzio | d42cef1 | 2021-11-08 12:47:25 +0100 | [diff] [blame] | 533 | while (1) { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 534 | if (LL_USART_IsActiveFlag_TXE(config->usart)) { |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 535 | key = irq_lock(); |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 536 | if (LL_USART_IsActiveFlag_TXE(config->usart)) { |
Julien D'ascenzio | d42cef1 | 2021-11-08 12:47:25 +0100 | [diff] [blame] | 537 | break; |
| 538 | } |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 539 | irq_unlock(key); |
| 540 | } |
Anas Nashif | 4c32258 | 2019-06-04 10:52:23 -0400 | [diff] [blame] | 541 | } |
Erwan Gouriou | da210ba | 2017-09-21 15:20:53 +0200 | [diff] [blame] | 542 | |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 543 | #ifdef CONFIG_PM |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 544 | |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 545 | /* If an interrupt transmission is in progress, the pm constraint is already managed by the |
| 546 | * call of uart_stm32_irq_tx_[en|dis]able |
| 547 | */ |
| 548 | if (!data->tx_poll_stream_on && !data->tx_int_stream_on) { |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 549 | data->tx_poll_stream_on = true; |
| 550 | |
| 551 | /* Don't allow system to suspend until stream |
| 552 | * transmission has completed |
| 553 | */ |
| 554 | uart_stm32_pm_constraint_set(dev); |
| 555 | |
| 556 | /* Enable TC interrupt so we can release suspend |
| 557 | * constraint when done |
| 558 | */ |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 559 | LL_USART_EnableIT_TC(config->usart); |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 560 | } |
| 561 | #endif /* CONFIG_PM */ |
Gerard Marull-Paretas | e3f4907 | 2021-09-09 22:41:35 +0200 | [diff] [blame] | 562 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 563 | LL_USART_TransmitData8(config->usart, (uint8_t)c); |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 564 | irq_unlock(key); |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 565 | } |
| 566 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 567 | static int uart_stm32_err_check(const struct device *dev) |
Georgij Cernysiov | c74c131 | 2019-02-14 10:50:19 +0100 | [diff] [blame] | 568 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 569 | const struct uart_stm32_config *config = dev->config; |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 570 | uint32_t err = 0U; |
Georgij Cernysiov | c74c131 | 2019-02-14 10:50:19 +0100 | [diff] [blame] | 571 | |
Francois Ramu | 860bd87 | 2022-01-28 16:52:28 +0100 | [diff] [blame] | 572 | /* Check for errors, then clear them. |
Georgij Cernysiov | c74c131 | 2019-02-14 10:50:19 +0100 | [diff] [blame] | 573 | * Some SoC clear all error flags when at least |
Francois Ramu | 860bd87 | 2022-01-28 16:52:28 +0100 | [diff] [blame] | 574 | * one is cleared. (e.g. F4X, F1X, and F2X). |
| 575 | * The stm32 F4X, F1X, and F2X also reads the usart DR when clearing Errors |
Georgij Cernysiov | c74c131 | 2019-02-14 10:50:19 +0100 | [diff] [blame] | 576 | */ |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 577 | if (LL_USART_IsActiveFlag_ORE(config->usart)) { |
Georgij Cernysiov | c74c131 | 2019-02-14 10:50:19 +0100 | [diff] [blame] | 578 | err |= UART_ERROR_OVERRUN; |
| 579 | } |
| 580 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 581 | if (LL_USART_IsActiveFlag_PE(config->usart)) { |
Georgij Cernysiov | c74c131 | 2019-02-14 10:50:19 +0100 | [diff] [blame] | 582 | err |= UART_ERROR_PARITY; |
| 583 | } |
| 584 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 585 | if (LL_USART_IsActiveFlag_FE(config->usart)) { |
Georgij Cernysiov | c74c131 | 2019-02-14 10:50:19 +0100 | [diff] [blame] | 586 | err |= UART_ERROR_FRAMING; |
| 587 | } |
| 588 | |
Andrea Campanella | b2190fd | 2021-12-21 16:09:15 +0000 | [diff] [blame] | 589 | #if !defined(CONFIG_SOC_SERIES_STM32F0X) || defined(USART_LIN_SUPPORT) |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 590 | if (LL_USART_IsActiveFlag_LBD(config->usart)) { |
Andrea Campanella | b2190fd | 2021-12-21 16:09:15 +0000 | [diff] [blame] | 591 | err |= UART_BREAK; |
| 592 | } |
| 593 | |
| 594 | if (err & UART_BREAK) { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 595 | LL_USART_ClearFlag_LBD(config->usart); |
Andrea Campanella | b2190fd | 2021-12-21 16:09:15 +0000 | [diff] [blame] | 596 | } |
| 597 | #endif |
Francois Ramu | 860bd87 | 2022-01-28 16:52:28 +0100 | [diff] [blame] | 598 | /* Clearing error : |
| 599 | * the stm32 F4X, F1X, and F2X sw sequence is reading the usart SR |
| 600 | * then the usart DR to clear the Error flags ORE, PE, FE, NE |
| 601 | * --> so is the RXNE flag also cleared ! |
| 602 | */ |
Georgij Cernysiov | c74c131 | 2019-02-14 10:50:19 +0100 | [diff] [blame] | 603 | if (err & UART_ERROR_OVERRUN) { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 604 | LL_USART_ClearFlag_ORE(config->usart); |
Georgij Cernysiov | c74c131 | 2019-02-14 10:50:19 +0100 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | if (err & UART_ERROR_PARITY) { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 608 | LL_USART_ClearFlag_PE(config->usart); |
Georgij Cernysiov | c74c131 | 2019-02-14 10:50:19 +0100 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | if (err & UART_ERROR_FRAMING) { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 612 | LL_USART_ClearFlag_FE(config->usart); |
Georgij Cernysiov | c74c131 | 2019-02-14 10:50:19 +0100 | [diff] [blame] | 613 | } |
Georgij Cernysiov | c74c131 | 2019-02-14 10:50:19 +0100 | [diff] [blame] | 614 | /* Clear noise error as well, |
| 615 | * it is not represented by the errors enum |
| 616 | */ |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 617 | LL_USART_ClearFlag_NE(config->usart); |
Georgij Cernysiov | c74c131 | 2019-02-14 10:50:19 +0100 | [diff] [blame] | 618 | |
| 619 | return err; |
| 620 | } |
| 621 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 622 | static inline void __uart_stm32_get_clock(const struct device *dev) |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 623 | { |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 624 | struct uart_stm32_data *data = dev->data; |
Kumar Gala | b275fec | 2021-02-11 11:49:24 -0600 | [diff] [blame] | 625 | const struct device *clk = DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE); |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 626 | |
Erwan Gouriou | 8c079e9 | 2016-11-14 11:53:52 +0100 | [diff] [blame] | 627 | data->clock = clk; |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 628 | } |
| 629 | |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 630 | #ifdef CONFIG_UART_INTERRUPT_DRIVEN |
| 631 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 632 | static int uart_stm32_fifo_fill(const struct device *dev, |
| 633 | const uint8_t *tx_data, |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 634 | int size) |
| 635 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 636 | const struct uart_stm32_config *config = dev->config; |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 637 | uint8_t num_tx = 0U; |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 638 | int key; |
| 639 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 640 | if (!LL_USART_IsActiveFlag_TXE(config->usart)) { |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 641 | return num_tx; |
| 642 | } |
| 643 | |
| 644 | /* Lock interrupts to prevent nested interrupts or thread switch */ |
| 645 | key = irq_lock(); |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 646 | |
Erwan Gouriou | da210ba | 2017-09-21 15:20:53 +0200 | [diff] [blame] | 647 | while ((size - num_tx > 0) && |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 648 | LL_USART_IsActiveFlag_TXE(config->usart)) { |
Georgij Cernysiov | 3de55da | 2019-02-06 23:31:24 +0100 | [diff] [blame] | 649 | /* TXE flag will be cleared with byte write to DR|RDR register */ |
Erwan Gouriou | 8c079e9 | 2016-11-14 11:53:52 +0100 | [diff] [blame] | 650 | |
| 651 | /* Send a character (8bit , parity none) */ |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 652 | LL_USART_TransmitData8(config->usart, tx_data[num_tx++]); |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 653 | } |
| 654 | |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 655 | irq_unlock(key); |
| 656 | |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 657 | return num_tx; |
| 658 | } |
| 659 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 660 | static int uart_stm32_fifo_read(const struct device *dev, uint8_t *rx_data, |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 661 | const int size) |
| 662 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 663 | const struct uart_stm32_config *config = dev->config; |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 664 | uint8_t num_rx = 0U; |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 665 | |
Erwan Gouriou | da210ba | 2017-09-21 15:20:53 +0200 | [diff] [blame] | 666 | while ((size - num_rx > 0) && |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 667 | LL_USART_IsActiveFlag_RXNE(config->usart)) { |
Georgij Cernysiov | 3de55da | 2019-02-06 23:31:24 +0100 | [diff] [blame] | 668 | /* RXNE flag will be cleared upon read from DR|RDR register */ |
Erwan Gouriou | 8c079e9 | 2016-11-14 11:53:52 +0100 | [diff] [blame] | 669 | |
| 670 | /* Receive a character (8bit , parity none) */ |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 671 | rx_data[num_rx++] = LL_USART_ReceiveData8(config->usart); |
Kiril Zyapkov | 7a602fc | 2018-11-02 15:34:56 +0200 | [diff] [blame] | 672 | |
| 673 | /* Clear overrun error flag */ |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 674 | if (LL_USART_IsActiveFlag_ORE(config->usart)) { |
| 675 | LL_USART_ClearFlag_ORE(config->usart); |
Francois Ramu | 860bd87 | 2022-01-28 16:52:28 +0100 | [diff] [blame] | 676 | /* |
| 677 | * On stm32 F4X, F1X, and F2X, the RXNE flag is affected (cleared) by |
| 678 | * the uart_err_check function call (on errors flags clearing) |
| 679 | */ |
Kiril Zyapkov | 7a602fc | 2018-11-02 15:34:56 +0200 | [diff] [blame] | 680 | } |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 681 | } |
Georgij Cernysiov | 3de55da | 2019-02-06 23:31:24 +0100 | [diff] [blame] | 682 | |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 683 | return num_rx; |
| 684 | } |
| 685 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 686 | static void uart_stm32_irq_tx_enable(const struct device *dev) |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 687 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 688 | const struct uart_stm32_config *config = dev->config; |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 689 | #ifdef CONFIG_PM |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 690 | struct uart_stm32_data *data = dev->data; |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 691 | int key; |
| 692 | #endif |
Julien D'ascenzio | d42cef1 | 2021-11-08 12:47:25 +0100 | [diff] [blame] | 693 | |
| 694 | #ifdef CONFIG_PM |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 695 | key = irq_lock(); |
Julien D'ascenzio | 7b21050 | 2021-10-26 18:03:31 +0200 | [diff] [blame] | 696 | data->tx_poll_stream_on = false; |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 697 | data->tx_int_stream_on = true; |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 698 | uart_stm32_pm_constraint_set(dev); |
| 699 | #endif |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 700 | LL_USART_EnableIT_TC(config->usart); |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 701 | |
| 702 | #ifdef CONFIG_PM |
| 703 | irq_unlock(key); |
| 704 | #endif |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 705 | } |
| 706 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 707 | static void uart_stm32_irq_tx_disable(const struct device *dev) |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 708 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 709 | const struct uart_stm32_config *config = dev->config; |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 710 | #ifdef CONFIG_PM |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 711 | struct uart_stm32_data *data = dev->data; |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 712 | int key; |
| 713 | |
| 714 | key = irq_lock(); |
| 715 | #endif |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 716 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 717 | LL_USART_DisableIT_TC(config->usart); |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 718 | |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 719 | #ifdef CONFIG_PM |
| 720 | data->tx_int_stream_on = false; |
| 721 | uart_stm32_pm_constraint_release(dev); |
| 722 | #endif |
Julien D'ascenzio | d42cef1 | 2021-11-08 12:47:25 +0100 | [diff] [blame] | 723 | |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 724 | #ifdef CONFIG_PM |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 725 | irq_unlock(key); |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 726 | #endif |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 727 | } |
| 728 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 729 | static int uart_stm32_irq_tx_ready(const struct device *dev) |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 730 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 731 | const struct uart_stm32_config *config = dev->config; |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 732 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 733 | return LL_USART_IsActiveFlag_TXE(config->usart) && |
| 734 | LL_USART_IsEnabledIT_TC(config->usart); |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 735 | } |
| 736 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 737 | static int uart_stm32_irq_tx_complete(const struct device *dev) |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 738 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 739 | const struct uart_stm32_config *config = dev->config; |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 740 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 741 | return LL_USART_IsActiveFlag_TC(config->usart); |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 742 | } |
| 743 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 744 | static void uart_stm32_irq_rx_enable(const struct device *dev) |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 745 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 746 | const struct uart_stm32_config *config = dev->config; |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 747 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 748 | LL_USART_EnableIT_RXNE(config->usart); |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 749 | } |
| 750 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 751 | static void uart_stm32_irq_rx_disable(const struct device *dev) |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 752 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 753 | const struct uart_stm32_config *config = dev->config; |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 754 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 755 | LL_USART_DisableIT_RXNE(config->usart); |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 756 | } |
| 757 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 758 | static int uart_stm32_irq_rx_ready(const struct device *dev) |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 759 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 760 | const struct uart_stm32_config *config = dev->config; |
Francois Ramu | 860bd87 | 2022-01-28 16:52:28 +0100 | [diff] [blame] | 761 | /* |
| 762 | * On stm32 F4X, F1X, and F2X, the RXNE flag is affected (cleared) by |
| 763 | * the uart_err_check function call (on errors flags clearing) |
| 764 | */ |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 765 | return LL_USART_IsActiveFlag_RXNE(config->usart); |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 766 | } |
| 767 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 768 | static void uart_stm32_irq_err_enable(const struct device *dev) |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 769 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 770 | const struct uart_stm32_config *config = dev->config; |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 771 | |
Erwan Gouriou | 8c079e9 | 2016-11-14 11:53:52 +0100 | [diff] [blame] | 772 | /* Enable FE, ORE interruptions */ |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 773 | LL_USART_EnableIT_ERROR(config->usart); |
Ilya Tagunov | 967c31b | 2018-03-29 19:40:00 +0300 | [diff] [blame] | 774 | #if !defined(CONFIG_SOC_SERIES_STM32F0X) || defined(USART_LIN_SUPPORT) |
Erwan Gouriou | 8c079e9 | 2016-11-14 11:53:52 +0100 | [diff] [blame] | 775 | /* Enable Line break detection */ |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 776 | if (IS_UART_LIN_INSTANCE(config->usart)) { |
| 777 | LL_USART_EnableIT_LBD(config->usart); |
Ilya Tagunov | 967c31b | 2018-03-29 19:40:00 +0300 | [diff] [blame] | 778 | } |
Maciej Debski | eaff37e | 2017-08-09 11:23:04 +0200 | [diff] [blame] | 779 | #endif |
Erwan Gouriou | 8c079e9 | 2016-11-14 11:53:52 +0100 | [diff] [blame] | 780 | /* Enable parity error interruption */ |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 781 | LL_USART_EnableIT_PE(config->usart); |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 782 | } |
| 783 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 784 | static void uart_stm32_irq_err_disable(const struct device *dev) |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 785 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 786 | const struct uart_stm32_config *config = dev->config; |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 787 | |
Ilya Tagunov | 967c31b | 2018-03-29 19:40:00 +0300 | [diff] [blame] | 788 | /* Disable FE, ORE interruptions */ |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 789 | LL_USART_DisableIT_ERROR(config->usart); |
Ilya Tagunov | 967c31b | 2018-03-29 19:40:00 +0300 | [diff] [blame] | 790 | #if !defined(CONFIG_SOC_SERIES_STM32F0X) || defined(USART_LIN_SUPPORT) |
| 791 | /* Disable Line break detection */ |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 792 | if (IS_UART_LIN_INSTANCE(config->usart)) { |
| 793 | LL_USART_DisableIT_LBD(config->usart); |
Ilya Tagunov | 967c31b | 2018-03-29 19:40:00 +0300 | [diff] [blame] | 794 | } |
Maciej Debski | eaff37e | 2017-08-09 11:23:04 +0200 | [diff] [blame] | 795 | #endif |
Ilya Tagunov | 967c31b | 2018-03-29 19:40:00 +0300 | [diff] [blame] | 796 | /* Disable parity error interruption */ |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 797 | LL_USART_DisableIT_PE(config->usart); |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 798 | } |
| 799 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 800 | static int uart_stm32_irq_is_pending(const struct device *dev) |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 801 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 802 | const struct uart_stm32_config *config = dev->config; |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 803 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 804 | return ((LL_USART_IsActiveFlag_RXNE(config->usart) && |
| 805 | LL_USART_IsEnabledIT_RXNE(config->usart)) || |
| 806 | (LL_USART_IsActiveFlag_TC(config->usart) && |
| 807 | LL_USART_IsEnabledIT_TC(config->usart))); |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 808 | } |
| 809 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 810 | static int uart_stm32_irq_update(const struct device *dev) |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 811 | { |
| 812 | return 1; |
| 813 | } |
| 814 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 815 | static void uart_stm32_irq_callback_set(const struct device *dev, |
Paul Sokolovsky | 57286af | 2018-07-16 21:12:26 +0300 | [diff] [blame] | 816 | uart_irq_callback_user_data_t cb, |
| 817 | void *cb_data) |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 818 | { |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 819 | struct uart_stm32_data *data = dev->data; |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 820 | |
| 821 | data->user_cb = cb; |
Paul Sokolovsky | 57286af | 2018-07-16 21:12:26 +0300 | [diff] [blame] | 822 | data->user_data = cb_data; |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 823 | } |
| 824 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 825 | #endif /* CONFIG_UART_INTERRUPT_DRIVEN */ |
| 826 | |
| 827 | #ifdef CONFIG_UART_ASYNC_API |
| 828 | |
| 829 | static inline void async_user_callback(struct uart_stm32_data *data, |
| 830 | struct uart_event *event) |
| 831 | { |
| 832 | if (data->async_cb) { |
| 833 | data->async_cb(data->uart_dev, event, data->async_user_data); |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | static inline void async_evt_rx_rdy(struct uart_stm32_data *data) |
| 838 | { |
| 839 | LOG_DBG("rx_rdy: (%d %d)", data->dma_rx.offset, data->dma_rx.counter); |
| 840 | |
| 841 | struct uart_event event = { |
| 842 | .type = UART_RX_RDY, |
| 843 | .data.rx.buf = data->dma_rx.buffer, |
| 844 | .data.rx.len = data->dma_rx.counter - data->dma_rx.offset, |
| 845 | .data.rx.offset = data->dma_rx.offset |
| 846 | }; |
| 847 | |
Alexander Shuklin | 6831b8b | 2021-02-04 12:00:49 +0300 | [diff] [blame] | 848 | /* update the current pos for new data */ |
| 849 | data->dma_rx.offset = data->dma_rx.counter; |
| 850 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 851 | /* send event only for new data */ |
| 852 | if (event.data.rx.len > 0) { |
| 853 | async_user_callback(data, &event); |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | static inline void async_evt_rx_err(struct uart_stm32_data *data, int err_code) |
| 858 | { |
| 859 | LOG_DBG("rx error: %d", err_code); |
| 860 | |
| 861 | struct uart_event event = { |
| 862 | .type = UART_RX_STOPPED, |
| 863 | .data.rx_stop.reason = err_code, |
| 864 | .data.rx_stop.data.len = data->dma_rx.counter, |
| 865 | .data.rx_stop.data.offset = 0, |
| 866 | .data.rx_stop.data.buf = data->dma_rx.buffer |
| 867 | }; |
| 868 | |
| 869 | async_user_callback(data, &event); |
| 870 | } |
| 871 | |
| 872 | static inline void async_evt_tx_done(struct uart_stm32_data *data) |
| 873 | { |
| 874 | LOG_DBG("tx done: %d", data->dma_tx.counter); |
| 875 | |
| 876 | struct uart_event event = { |
| 877 | .type = UART_TX_DONE, |
| 878 | .data.tx.buf = data->dma_tx.buffer, |
| 879 | .data.tx.len = data->dma_tx.counter |
| 880 | }; |
| 881 | |
| 882 | /* Reset tx buffer */ |
| 883 | data->dma_tx.buffer_length = 0; |
| 884 | data->dma_tx.counter = 0; |
| 885 | |
| 886 | async_user_callback(data, &event); |
| 887 | } |
| 888 | |
| 889 | static inline void async_evt_tx_abort(struct uart_stm32_data *data) |
| 890 | { |
| 891 | LOG_DBG("tx abort: %d", data->dma_tx.counter); |
| 892 | |
| 893 | struct uart_event event = { |
| 894 | .type = UART_TX_ABORTED, |
| 895 | .data.tx.buf = data->dma_tx.buffer, |
| 896 | .data.tx.len = data->dma_tx.counter |
| 897 | }; |
| 898 | |
| 899 | /* Reset tx buffer */ |
| 900 | data->dma_tx.buffer_length = 0; |
| 901 | data->dma_tx.counter = 0; |
| 902 | |
| 903 | async_user_callback(data, &event); |
| 904 | } |
| 905 | |
| 906 | static inline void async_evt_rx_buf_request(struct uart_stm32_data *data) |
| 907 | { |
| 908 | struct uart_event evt = { |
| 909 | .type = UART_RX_BUF_REQUEST, |
| 910 | }; |
| 911 | |
| 912 | async_user_callback(data, &evt); |
| 913 | } |
| 914 | |
| 915 | static inline void async_evt_rx_buf_release(struct uart_stm32_data *data) |
| 916 | { |
| 917 | struct uart_event evt = { |
| 918 | .type = UART_RX_BUF_RELEASED, |
| 919 | .data.rx_buf.buf = data->dma_rx.buffer, |
| 920 | }; |
| 921 | |
| 922 | async_user_callback(data, &evt); |
| 923 | } |
| 924 | |
Francois Ramu | 962d6b1 | 2021-04-08 12:09:58 +0200 | [diff] [blame] | 925 | static inline void async_timer_start(struct k_work_delayable *work, |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 926 | int32_t timeout) |
| 927 | { |
Krzysztof Chruscinski | c590b35 | 2021-10-01 15:47:40 +0200 | [diff] [blame] | 928 | if ((timeout != SYS_FOREVER_US) && (timeout != 0)) { |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 929 | /* start timer */ |
Krzysztof Chruscinski | c590b35 | 2021-10-01 15:47:40 +0200 | [diff] [blame] | 930 | LOG_DBG("async timer started for %d us", timeout); |
| 931 | k_work_reschedule(work, K_USEC(timeout)); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 932 | } |
| 933 | } |
| 934 | |
| 935 | static void uart_stm32_dma_rx_flush(const struct device *dev) |
| 936 | { |
| 937 | struct dma_status stat; |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 938 | struct uart_stm32_data *data = dev->data; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 939 | |
Erwan Gouriou | 13c2351 | 2021-03-01 11:59:57 +0100 | [diff] [blame] | 940 | if (dma_get_status(data->dma_rx.dma_dev, |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 941 | data->dma_rx.dma_channel, &stat) == 0) { |
| 942 | size_t rx_rcv_len = data->dma_rx.buffer_length - |
| 943 | stat.pending_length; |
| 944 | if (rx_rcv_len > data->dma_rx.offset) { |
| 945 | data->dma_rx.counter = rx_rcv_len; |
| 946 | |
| 947 | async_evt_rx_rdy(data); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 948 | } |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | #endif /* CONFIG_UART_ASYNC_API */ |
| 953 | |
Erwan Gouriou | 79ff645 | 2021-09-21 09:34:09 +0200 | [diff] [blame] | 954 | #if defined(CONFIG_UART_INTERRUPT_DRIVEN) || \ |
| 955 | defined(CONFIG_UART_ASYNC_API) || \ |
| 956 | defined(CONFIG_PM) |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 957 | |
Tomasz Bursztyka | 4dcfb55 | 2020-06-17 14:58:56 +0200 | [diff] [blame] | 958 | static void uart_stm32_isr(const struct device *dev) |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 959 | { |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 960 | struct uart_stm32_data *data = dev->data; |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 961 | #if defined(CONFIG_PM) || defined(CONFIG_UART_ASYNC_API) |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 962 | const struct uart_stm32_config *config = dev->config; |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 963 | #endif |
| 964 | |
| 965 | #ifdef CONFIG_PM |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 966 | if (LL_USART_IsEnabledIT_TC(config->usart) && |
| 967 | LL_USART_IsActiveFlag_TC(config->usart)) { |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 968 | |
| 969 | if (data->tx_poll_stream_on) { |
Yegor Yefremov | 1155d46 | 2022-03-07 22:53:16 +0100 | [diff] [blame] | 970 | /* A poll stream transmission just completed, |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 971 | * allow system to suspend |
| 972 | */ |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 973 | LL_USART_DisableIT_TC(config->usart); |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 974 | data->tx_poll_stream_on = false; |
| 975 | uart_stm32_pm_constraint_release(dev); |
| 976 | } |
Yegor Yefremov | 1155d46 | 2022-03-07 22:53:16 +0100 | [diff] [blame] | 977 | /* Stream transmission was either async or IRQ based, |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 978 | * constraint will be released at the same time TC IT |
| 979 | * is disabled |
| 980 | */ |
| 981 | } |
| 982 | #endif |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 983 | |
Shlomi Vaknin | 1b4f7e5 | 2021-05-17 23:06:51 +0300 | [diff] [blame] | 984 | #ifdef CONFIG_UART_INTERRUPT_DRIVEN |
| 985 | if (data->user_cb) { |
| 986 | data->user_cb(dev, data->user_data); |
| 987 | } |
| 988 | #endif /* CONFIG_UART_INTERRUPT_DRIVEN */ |
| 989 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 990 | #ifdef CONFIG_UART_ASYNC_API |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 991 | if (LL_USART_IsEnabledIT_IDLE(config->usart) && |
| 992 | LL_USART_IsActiveFlag_IDLE(config->usart)) { |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 993 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 994 | LL_USART_ClearFlag_IDLE(config->usart); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 995 | |
| 996 | LOG_DBG("idle interrupt occurred"); |
| 997 | |
| 998 | /* Start the RX timer */ |
| 999 | async_timer_start(&data->dma_rx.timeout_work, |
| 1000 | data->dma_rx.timeout); |
| 1001 | |
| 1002 | if (data->dma_rx.timeout == 0) { |
| 1003 | uart_stm32_dma_rx_flush(dev); |
| 1004 | } |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1005 | } else if (LL_USART_IsEnabledIT_TC(config->usart) && |
| 1006 | LL_USART_IsActiveFlag_TC(config->usart)) { |
Zisis Adamos | 7235b09 | 2021-03-30 12:20:46 +0200 | [diff] [blame] | 1007 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1008 | LL_USART_DisableIT_TC(config->usart); |
| 1009 | LL_USART_ClearFlag_TC(config->usart); |
Zisis Adamos | 7235b09 | 2021-03-30 12:20:46 +0200 | [diff] [blame] | 1010 | /* Generate TX_DONE event when transmission is done */ |
| 1011 | async_evt_tx_done(data); |
Gerard Marull-Paretas | e3f4907 | 2021-09-09 22:41:35 +0200 | [diff] [blame] | 1012 | |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 1013 | #ifdef CONFIG_PM |
| 1014 | uart_stm32_pm_constraint_release(dev); |
| 1015 | #endif |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1016 | } else if (LL_USART_IsEnabledIT_RXNE(config->usart) && |
| 1017 | LL_USART_IsActiveFlag_RXNE(config->usart)) { |
Francois Ramu | cf60639 | 2021-11-08 11:43:53 +0100 | [diff] [blame] | 1018 | #ifdef USART_SR_RXNE |
| 1019 | /* clear the RXNE flag, because Rx data was not read */ |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1020 | LL_USART_ClearFlag_RXNE(config->usart); |
Francois Ramu | cf60639 | 2021-11-08 11:43:53 +0100 | [diff] [blame] | 1021 | #else |
Francois Ramu | 95e2c39 | 2021-10-26 11:39:57 +0200 | [diff] [blame] | 1022 | /* clear the RXNE by flushing the fifo, because Rx data was not read */ |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1023 | LL_USART_RequestRxDataFlush(config->usart); |
Francois Ramu | cf60639 | 2021-11-08 11:43:53 +0100 | [diff] [blame] | 1024 | #endif /* USART_SR_RXNE */ |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | /* Clear errors */ |
| 1028 | uart_stm32_err_check(dev); |
| 1029 | #endif /* CONFIG_UART_ASYNC_API */ |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1030 | } |
Erwan Gouriou | 79ff645 | 2021-09-21 09:34:09 +0200 | [diff] [blame] | 1031 | #endif /* CONFIG_UART_INTERRUPT_DRIVEN || CONFIG_UART_ASYNC_API || CONFIG_PM */ |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1032 | |
| 1033 | #ifdef CONFIG_UART_ASYNC_API |
| 1034 | |
| 1035 | static int uart_stm32_async_callback_set(const struct device *dev, |
| 1036 | uart_callback_t callback, |
| 1037 | void *user_data) |
| 1038 | { |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 1039 | struct uart_stm32_data *data = dev->data; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1040 | |
| 1041 | data->async_cb = callback; |
| 1042 | data->async_user_data = user_data; |
| 1043 | |
| 1044 | return 0; |
| 1045 | } |
| 1046 | |
| 1047 | static inline void uart_stm32_dma_tx_enable(const struct device *dev) |
| 1048 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1049 | const struct uart_stm32_config *config = dev->config; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1050 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1051 | LL_USART_EnableDMAReq_TX(config->usart); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | static inline void uart_stm32_dma_tx_disable(const struct device *dev) |
| 1055 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1056 | const struct uart_stm32_config *config = dev->config; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1057 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1058 | LL_USART_DisableDMAReq_TX(config->usart); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | static inline void uart_stm32_dma_rx_enable(const struct device *dev) |
| 1062 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1063 | const struct uart_stm32_config *config = dev->config; |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 1064 | struct uart_stm32_data *data = dev->data; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1065 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1066 | LL_USART_EnableDMAReq_RX(config->usart); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1067 | |
| 1068 | data->dma_rx.enabled = true; |
| 1069 | } |
| 1070 | |
| 1071 | static inline void uart_stm32_dma_rx_disable(const struct device *dev) |
| 1072 | { |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 1073 | struct uart_stm32_data *data = dev->data; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1074 | |
| 1075 | data->dma_rx.enabled = false; |
| 1076 | } |
| 1077 | |
| 1078 | static int uart_stm32_async_rx_disable(const struct device *dev) |
| 1079 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1080 | const struct uart_stm32_config *config = dev->config; |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 1081 | struct uart_stm32_data *data = dev->data; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1082 | struct uart_event disabled_event = { |
| 1083 | .type = UART_RX_DISABLED |
| 1084 | }; |
| 1085 | |
| 1086 | if (!data->dma_rx.enabled) { |
| 1087 | async_user_callback(data, &disabled_event); |
| 1088 | return -EFAULT; |
| 1089 | } |
| 1090 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1091 | LL_USART_DisableIT_IDLE(config->usart); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1092 | |
| 1093 | uart_stm32_dma_rx_flush(dev); |
| 1094 | |
| 1095 | async_evt_rx_buf_release(data); |
| 1096 | |
| 1097 | uart_stm32_dma_rx_disable(dev); |
| 1098 | |
Francois Ramu | 962d6b1 | 2021-04-08 12:09:58 +0200 | [diff] [blame] | 1099 | (void)k_work_cancel_delayable(&data->dma_rx.timeout_work); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1100 | |
Erwan Gouriou | 13c2351 | 2021-03-01 11:59:57 +0100 | [diff] [blame] | 1101 | dma_stop(data->dma_rx.dma_dev, data->dma_rx.dma_channel); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1102 | |
| 1103 | data->rx_next_buffer = NULL; |
| 1104 | data->rx_next_buffer_len = 0; |
| 1105 | |
Yegor Yefremov | 1155d46 | 2022-03-07 22:53:16 +0100 | [diff] [blame] | 1106 | /* When async rx is disabled, enable interruptible instance of uart to function normally */ |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1107 | LL_USART_EnableIT_RXNE(config->usart); |
Manojkumar Subramaniam | d79d26f | 2021-08-23 02:04:05 +0800 | [diff] [blame] | 1108 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1109 | LOG_DBG("rx: disabled"); |
| 1110 | |
| 1111 | async_user_callback(data, &disabled_event); |
| 1112 | |
| 1113 | return 0; |
| 1114 | } |
| 1115 | |
| 1116 | void uart_stm32_dma_tx_cb(const struct device *dma_dev, void *user_data, |
| 1117 | uint32_t channel, int status) |
| 1118 | { |
| 1119 | const struct device *uart_dev = user_data; |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 1120 | struct uart_stm32_data *data = uart_dev->data; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1121 | struct dma_status stat; |
| 1122 | unsigned int key = irq_lock(); |
| 1123 | |
| 1124 | /* Disable TX */ |
| 1125 | uart_stm32_dma_tx_disable(uart_dev); |
| 1126 | |
Francois Ramu | 962d6b1 | 2021-04-08 12:09:58 +0200 | [diff] [blame] | 1127 | (void)k_work_cancel_delayable(&data->dma_tx.timeout_work); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1128 | |
Erwan Gouriou | 13c2351 | 2021-03-01 11:59:57 +0100 | [diff] [blame] | 1129 | if (!dma_get_status(data->dma_tx.dma_dev, |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1130 | data->dma_tx.dma_channel, &stat)) { |
| 1131 | data->dma_tx.counter = data->dma_tx.buffer_length - |
| 1132 | stat.pending_length; |
| 1133 | } |
| 1134 | |
Prema Jonathan van Win | 76dee39 | 2021-05-03 11:24:38 +0300 | [diff] [blame] | 1135 | data->dma_tx.buffer_length = 0; |
| 1136 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1137 | irq_unlock(key); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | static void uart_stm32_dma_replace_buffer(const struct device *dev) |
| 1141 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1142 | const struct uart_stm32_config *config = dev->config; |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 1143 | struct uart_stm32_data *data = dev->data; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1144 | |
Yegor Yefremov | 1155d46 | 2022-03-07 22:53:16 +0100 | [diff] [blame] | 1145 | /* Replace the buffer and reload the DMA */ |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1146 | LOG_DBG("Replacing RX buffer: %d", data->rx_next_buffer_len); |
| 1147 | |
| 1148 | /* reload DMA */ |
| 1149 | data->dma_rx.offset = 0; |
| 1150 | data->dma_rx.counter = 0; |
| 1151 | data->dma_rx.buffer = data->rx_next_buffer; |
| 1152 | data->dma_rx.buffer_length = data->rx_next_buffer_len; |
| 1153 | data->dma_rx.blk_cfg.block_size = data->dma_rx.buffer_length; |
| 1154 | data->dma_rx.blk_cfg.dest_address = (uint32_t)data->dma_rx.buffer; |
| 1155 | data->rx_next_buffer = NULL; |
| 1156 | data->rx_next_buffer_len = 0; |
| 1157 | |
Erwan Gouriou | 13c2351 | 2021-03-01 11:59:57 +0100 | [diff] [blame] | 1158 | dma_reload(data->dma_rx.dma_dev, data->dma_rx.dma_channel, |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1159 | data->dma_rx.blk_cfg.source_address, |
| 1160 | data->dma_rx.blk_cfg.dest_address, |
| 1161 | data->dma_rx.blk_cfg.block_size); |
| 1162 | |
Erwan Gouriou | 13c2351 | 2021-03-01 11:59:57 +0100 | [diff] [blame] | 1163 | dma_start(data->dma_rx.dma_dev, data->dma_rx.dma_channel); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1164 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1165 | LL_USART_ClearFlag_IDLE(config->usart); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1166 | |
| 1167 | /* Request next buffer */ |
| 1168 | async_evt_rx_buf_request(data); |
| 1169 | } |
| 1170 | |
| 1171 | void uart_stm32_dma_rx_cb(const struct device *dma_dev, void *user_data, |
| 1172 | uint32_t channel, int status) |
| 1173 | { |
| 1174 | const struct device *uart_dev = user_data; |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 1175 | struct uart_stm32_data *data = uart_dev->data; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1176 | |
| 1177 | if (status != 0) { |
| 1178 | async_evt_rx_err(data, status); |
| 1179 | return; |
| 1180 | } |
| 1181 | |
Francois Ramu | 962d6b1 | 2021-04-08 12:09:58 +0200 | [diff] [blame] | 1182 | (void)k_work_cancel_delayable(&data->dma_rx.timeout_work); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1183 | |
| 1184 | /* true since this functions occurs when buffer if full */ |
| 1185 | data->dma_rx.counter = data->dma_rx.buffer_length; |
| 1186 | |
| 1187 | async_evt_rx_rdy(data); |
| 1188 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1189 | if (data->rx_next_buffer != NULL) { |
| 1190 | async_evt_rx_buf_release(data); |
| 1191 | |
| 1192 | /* replace the buffer when the current |
| 1193 | * is full and not the same as the next |
| 1194 | * one. |
| 1195 | */ |
| 1196 | uart_stm32_dma_replace_buffer(uart_dev); |
| 1197 | } else { |
| 1198 | /* Buffer full without valid next buffer, |
| 1199 | * an UART_RX_DISABLED event must be generated, |
| 1200 | * but uart_stm32_async_rx_disable() cannot be |
| 1201 | * called in ISR context. So force the RX timeout |
| 1202 | * to minimum value and let the RX timeout to do the job. |
| 1203 | */ |
Francois Ramu | 962d6b1 | 2021-04-08 12:09:58 +0200 | [diff] [blame] | 1204 | k_work_reschedule(&data->dma_rx.timeout_work, K_TICKS(1)); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | static int uart_stm32_async_tx(const struct device *dev, |
| 1209 | const uint8_t *tx_data, size_t buf_size, int32_t timeout) |
| 1210 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1211 | const struct uart_stm32_config *config = dev->config; |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 1212 | struct uart_stm32_data *data = dev->data; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1213 | int ret; |
| 1214 | |
Erwan Gouriou | 13c2351 | 2021-03-01 11:59:57 +0100 | [diff] [blame] | 1215 | if (data->dma_tx.dma_dev == NULL) { |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1216 | return -ENODEV; |
| 1217 | } |
| 1218 | |
| 1219 | if (data->dma_tx.buffer_length != 0) { |
| 1220 | return -EBUSY; |
| 1221 | } |
| 1222 | |
| 1223 | data->dma_tx.buffer = (uint8_t *)tx_data; |
| 1224 | data->dma_tx.buffer_length = buf_size; |
| 1225 | data->dma_tx.timeout = timeout; |
| 1226 | |
| 1227 | LOG_DBG("tx: l=%d", data->dma_tx.buffer_length); |
| 1228 | |
Zisis Adamos | 7235b09 | 2021-03-30 12:20:46 +0200 | [diff] [blame] | 1229 | /* Clear TC flag */ |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1230 | LL_USART_ClearFlag_TC(config->usart); |
Zisis Adamos | 7235b09 | 2021-03-30 12:20:46 +0200 | [diff] [blame] | 1231 | |
| 1232 | /* Enable TC interrupt so we can signal correct TX done */ |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1233 | LL_USART_EnableIT_TC(config->usart); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1234 | |
| 1235 | /* set source address */ |
| 1236 | data->dma_tx.blk_cfg.source_address = (uint32_t)data->dma_tx.buffer; |
| 1237 | data->dma_tx.blk_cfg.block_size = data->dma_tx.buffer_length; |
| 1238 | |
Erwan Gouriou | 13c2351 | 2021-03-01 11:59:57 +0100 | [diff] [blame] | 1239 | ret = dma_config(data->dma_tx.dma_dev, data->dma_tx.dma_channel, |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1240 | &data->dma_tx.dma_cfg); |
| 1241 | |
| 1242 | if (ret != 0) { |
| 1243 | LOG_ERR("dma tx config error!"); |
| 1244 | return -EINVAL; |
| 1245 | } |
| 1246 | |
Erwan Gouriou | 13c2351 | 2021-03-01 11:59:57 +0100 | [diff] [blame] | 1247 | if (dma_start(data->dma_tx.dma_dev, data->dma_tx.dma_channel)) { |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1248 | LOG_ERR("UART err: TX DMA start failed!"); |
| 1249 | return -EFAULT; |
| 1250 | } |
| 1251 | |
| 1252 | /* Start TX timer */ |
| 1253 | async_timer_start(&data->dma_tx.timeout_work, data->dma_tx.timeout); |
| 1254 | |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 1255 | #ifdef CONFIG_PM |
Erwan Gouriou | 79ff645 | 2021-09-21 09:34:09 +0200 | [diff] [blame] | 1256 | |
| 1257 | /* Do not allow system to suspend until transmission has completed */ |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 1258 | uart_stm32_pm_constraint_set(dev); |
| 1259 | #endif |
Gerard Marull-Paretas | e3f4907 | 2021-09-09 22:41:35 +0200 | [diff] [blame] | 1260 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1261 | /* Enable TX DMA requests */ |
| 1262 | uart_stm32_dma_tx_enable(dev); |
| 1263 | |
| 1264 | return 0; |
| 1265 | } |
| 1266 | |
| 1267 | static int uart_stm32_async_rx_enable(const struct device *dev, |
| 1268 | uint8_t *rx_buf, size_t buf_size, int32_t timeout) |
| 1269 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1270 | const struct uart_stm32_config *config = dev->config; |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 1271 | struct uart_stm32_data *data = dev->data; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1272 | int ret; |
| 1273 | |
Erwan Gouriou | 13c2351 | 2021-03-01 11:59:57 +0100 | [diff] [blame] | 1274 | if (data->dma_rx.dma_dev == NULL) { |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1275 | return -ENODEV; |
| 1276 | } |
| 1277 | |
| 1278 | if (data->dma_rx.enabled) { |
| 1279 | LOG_WRN("RX was already enabled"); |
| 1280 | return -EBUSY; |
| 1281 | } |
| 1282 | |
| 1283 | data->dma_rx.offset = 0; |
| 1284 | data->dma_rx.buffer = rx_buf; |
| 1285 | data->dma_rx.buffer_length = buf_size; |
| 1286 | data->dma_rx.counter = 0; |
| 1287 | data->dma_rx.timeout = timeout; |
| 1288 | |
| 1289 | /* Disable RX interrupts to let DMA to handle it */ |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1290 | LL_USART_DisableIT_RXNE(config->usart); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1291 | |
| 1292 | data->dma_rx.blk_cfg.block_size = buf_size; |
| 1293 | data->dma_rx.blk_cfg.dest_address = (uint32_t)data->dma_rx.buffer; |
| 1294 | |
Erwan Gouriou | 13c2351 | 2021-03-01 11:59:57 +0100 | [diff] [blame] | 1295 | ret = dma_config(data->dma_rx.dma_dev, data->dma_rx.dma_channel, |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1296 | &data->dma_rx.dma_cfg); |
| 1297 | |
| 1298 | if (ret != 0) { |
| 1299 | LOG_ERR("UART ERR: RX DMA config failed!"); |
| 1300 | return -EINVAL; |
| 1301 | } |
| 1302 | |
Erwan Gouriou | 13c2351 | 2021-03-01 11:59:57 +0100 | [diff] [blame] | 1303 | if (dma_start(data->dma_rx.dma_dev, data->dma_rx.dma_channel)) { |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1304 | LOG_ERR("UART ERR: RX DMA start failed!"); |
| 1305 | return -EFAULT; |
| 1306 | } |
| 1307 | |
| 1308 | /* Enable RX DMA requests */ |
| 1309 | uart_stm32_dma_rx_enable(dev); |
| 1310 | |
| 1311 | /* Enable IRQ IDLE to define the end of a |
| 1312 | * RX DMA transaction. |
| 1313 | */ |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1314 | LL_USART_ClearFlag_IDLE(config->usart); |
| 1315 | LL_USART_EnableIT_IDLE(config->usart); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1316 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1317 | LL_USART_EnableIT_ERROR(config->usart); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1318 | |
| 1319 | /* Request next buffer */ |
| 1320 | async_evt_rx_buf_request(data); |
| 1321 | |
| 1322 | LOG_DBG("async rx enabled"); |
| 1323 | |
| 1324 | return ret; |
| 1325 | } |
| 1326 | |
| 1327 | static int uart_stm32_async_tx_abort(const struct device *dev) |
| 1328 | { |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 1329 | struct uart_stm32_data *data = dev->data; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1330 | size_t tx_buffer_length = data->dma_tx.buffer_length; |
| 1331 | struct dma_status stat; |
| 1332 | |
| 1333 | if (tx_buffer_length == 0) { |
| 1334 | return -EFAULT; |
| 1335 | } |
| 1336 | |
Francois Ramu | 962d6b1 | 2021-04-08 12:09:58 +0200 | [diff] [blame] | 1337 | (void)k_work_cancel_delayable(&data->dma_tx.timeout_work); |
Erwan Gouriou | 13c2351 | 2021-03-01 11:59:57 +0100 | [diff] [blame] | 1338 | if (!dma_get_status(data->dma_tx.dma_dev, |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1339 | data->dma_tx.dma_channel, &stat)) { |
| 1340 | data->dma_tx.counter = tx_buffer_length - stat.pending_length; |
| 1341 | } |
| 1342 | |
Erwan Gouriou | 13c2351 | 2021-03-01 11:59:57 +0100 | [diff] [blame] | 1343 | dma_stop(data->dma_tx.dma_dev, data->dma_tx.dma_channel); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1344 | async_evt_tx_abort(data); |
| 1345 | |
| 1346 | return 0; |
| 1347 | } |
| 1348 | |
| 1349 | static void uart_stm32_async_rx_timeout(struct k_work *work) |
| 1350 | { |
Yong Cong Sin | 731241f | 2022-01-19 12:07:51 +0800 | [diff] [blame] | 1351 | struct k_work_delayable *dwork = k_work_delayable_from_work(work); |
| 1352 | struct uart_dma_stream *rx_stream = CONTAINER_OF(dwork, |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1353 | struct uart_dma_stream, timeout_work); |
| 1354 | struct uart_stm32_data *data = CONTAINER_OF(rx_stream, |
| 1355 | struct uart_stm32_data, dma_rx); |
| 1356 | const struct device *dev = data->uart_dev; |
| 1357 | |
| 1358 | LOG_DBG("rx timeout"); |
| 1359 | |
| 1360 | if (data->dma_rx.counter == data->dma_rx.buffer_length) { |
| 1361 | uart_stm32_async_rx_disable(dev); |
| 1362 | } else { |
| 1363 | uart_stm32_dma_rx_flush(dev); |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | static void uart_stm32_async_tx_timeout(struct k_work *work) |
| 1368 | { |
Yong Cong Sin | 731241f | 2022-01-19 12:07:51 +0800 | [diff] [blame] | 1369 | struct k_work_delayable *dwork = k_work_delayable_from_work(work); |
| 1370 | struct uart_dma_stream *tx_stream = CONTAINER_OF(dwork, |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1371 | struct uart_dma_stream, timeout_work); |
| 1372 | struct uart_stm32_data *data = CONTAINER_OF(tx_stream, |
| 1373 | struct uart_stm32_data, dma_tx); |
| 1374 | const struct device *dev = data->uart_dev; |
| 1375 | |
| 1376 | uart_stm32_async_tx_abort(dev); |
| 1377 | |
| 1378 | LOG_DBG("tx: async timeout"); |
| 1379 | } |
| 1380 | |
| 1381 | static int uart_stm32_async_rx_buf_rsp(const struct device *dev, uint8_t *buf, |
| 1382 | size_t len) |
| 1383 | { |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 1384 | struct uart_stm32_data *data = dev->data; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1385 | |
| 1386 | LOG_DBG("replace buffer (%d)", len); |
| 1387 | data->rx_next_buffer = buf; |
| 1388 | data->rx_next_buffer_len = len; |
| 1389 | |
| 1390 | return 0; |
| 1391 | } |
| 1392 | |
| 1393 | static int uart_stm32_async_init(const struct device *dev) |
| 1394 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1395 | const struct uart_stm32_config *config = dev->config; |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 1396 | struct uart_stm32_data *data = dev->data; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1397 | |
| 1398 | data->uart_dev = dev; |
| 1399 | |
Erwan Gouriou | 13c2351 | 2021-03-01 11:59:57 +0100 | [diff] [blame] | 1400 | if (data->dma_rx.dma_dev != NULL) { |
| 1401 | if (!device_is_ready(data->dma_rx.dma_dev)) { |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1402 | return -ENODEV; |
| 1403 | } |
| 1404 | } |
| 1405 | |
Erwan Gouriou | 13c2351 | 2021-03-01 11:59:57 +0100 | [diff] [blame] | 1406 | if (data->dma_tx.dma_dev != NULL) { |
| 1407 | if (!device_is_ready(data->dma_rx.dma_dev)) { |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1408 | return -ENODEV; |
| 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | /* Disable both TX and RX DMA requests */ |
| 1413 | uart_stm32_dma_rx_disable(dev); |
| 1414 | uart_stm32_dma_tx_disable(dev); |
| 1415 | |
Francois Ramu | 962d6b1 | 2021-04-08 12:09:58 +0200 | [diff] [blame] | 1416 | k_work_init_delayable(&data->dma_rx.timeout_work, |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1417 | uart_stm32_async_rx_timeout); |
Francois Ramu | 962d6b1 | 2021-04-08 12:09:58 +0200 | [diff] [blame] | 1418 | k_work_init_delayable(&data->dma_tx.timeout_work, |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1419 | uart_stm32_async_tx_timeout); |
| 1420 | |
| 1421 | /* Configure dma rx config */ |
| 1422 | memset(&data->dma_rx.blk_cfg, 0, sizeof(data->dma_rx.blk_cfg)); |
| 1423 | |
| 1424 | #if defined(CONFIG_SOC_SERIES_STM32F1X) || \ |
| 1425 | defined(CONFIG_SOC_SERIES_STM32F2X) || \ |
| 1426 | defined(CONFIG_SOC_SERIES_STM32F4X) || \ |
| 1427 | defined(CONFIG_SOC_SERIES_STM32L1X) |
| 1428 | data->dma_rx.blk_cfg.source_address = |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1429 | LL_USART_DMA_GetRegAddr(config->usart); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1430 | #else |
| 1431 | data->dma_rx.blk_cfg.source_address = |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1432 | LL_USART_DMA_GetRegAddr(config->usart, |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1433 | LL_USART_DMA_REG_DATA_RECEIVE); |
| 1434 | #endif |
| 1435 | |
| 1436 | data->dma_rx.blk_cfg.dest_address = 0; /* dest not ready */ |
| 1437 | |
| 1438 | if (data->dma_rx.src_addr_increment) { |
| 1439 | data->dma_rx.blk_cfg.source_addr_adj = DMA_ADDR_ADJ_INCREMENT; |
| 1440 | } else { |
| 1441 | data->dma_rx.blk_cfg.source_addr_adj = DMA_ADDR_ADJ_NO_CHANGE; |
| 1442 | } |
| 1443 | |
| 1444 | if (data->dma_rx.dst_addr_increment) { |
| 1445 | data->dma_rx.blk_cfg.dest_addr_adj = DMA_ADDR_ADJ_INCREMENT; |
| 1446 | } else { |
| 1447 | data->dma_rx.blk_cfg.dest_addr_adj = DMA_ADDR_ADJ_NO_CHANGE; |
| 1448 | } |
| 1449 | |
| 1450 | /* RX disable circular buffer */ |
| 1451 | data->dma_rx.blk_cfg.source_reload_en = 0; |
| 1452 | data->dma_rx.blk_cfg.dest_reload_en = 0; |
| 1453 | data->dma_rx.blk_cfg.fifo_mode_control = data->dma_rx.fifo_threshold; |
| 1454 | |
| 1455 | data->dma_rx.dma_cfg.head_block = &data->dma_rx.blk_cfg; |
| 1456 | data->dma_rx.dma_cfg.user_data = (void *)dev; |
| 1457 | data->rx_next_buffer = NULL; |
| 1458 | data->rx_next_buffer_len = 0; |
| 1459 | |
| 1460 | /* Configure dma tx config */ |
| 1461 | memset(&data->dma_tx.blk_cfg, 0, sizeof(data->dma_tx.blk_cfg)); |
| 1462 | |
| 1463 | #if defined(CONFIG_SOC_SERIES_STM32F1X) || \ |
| 1464 | defined(CONFIG_SOC_SERIES_STM32F2X) || \ |
| 1465 | defined(CONFIG_SOC_SERIES_STM32F4X) || \ |
| 1466 | defined(CONFIG_SOC_SERIES_STM32L1X) |
| 1467 | data->dma_tx.blk_cfg.dest_address = |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1468 | LL_USART_DMA_GetRegAddr(config->usart); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1469 | #else |
| 1470 | data->dma_tx.blk_cfg.dest_address = |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1471 | LL_USART_DMA_GetRegAddr(config->usart, |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1472 | LL_USART_DMA_REG_DATA_TRANSMIT); |
| 1473 | #endif |
| 1474 | |
| 1475 | data->dma_tx.blk_cfg.source_address = 0; /* not ready */ |
| 1476 | |
| 1477 | if (data->dma_tx.src_addr_increment) { |
| 1478 | data->dma_tx.blk_cfg.source_addr_adj = DMA_ADDR_ADJ_INCREMENT; |
| 1479 | } else { |
| 1480 | data->dma_tx.blk_cfg.source_addr_adj = DMA_ADDR_ADJ_NO_CHANGE; |
| 1481 | } |
| 1482 | |
| 1483 | if (data->dma_tx.dst_addr_increment) { |
| 1484 | data->dma_tx.blk_cfg.dest_addr_adj = DMA_ADDR_ADJ_INCREMENT; |
| 1485 | } else { |
| 1486 | data->dma_tx.blk_cfg.dest_addr_adj = DMA_ADDR_ADJ_NO_CHANGE; |
| 1487 | } |
| 1488 | |
| 1489 | data->dma_tx.blk_cfg.fifo_mode_control = data->dma_tx.fifo_threshold; |
| 1490 | |
| 1491 | data->dma_tx.dma_cfg.head_block = &data->dma_tx.blk_cfg; |
| 1492 | data->dma_tx.dma_cfg.user_data = (void *)dev; |
| 1493 | |
| 1494 | return 0; |
| 1495 | } |
| 1496 | |
| 1497 | #endif /* CONFIG_UART_ASYNC_API */ |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1498 | |
Marcus Shawcroft | d3ea539 | 2016-10-24 08:38:49 +0100 | [diff] [blame] | 1499 | static const struct uart_driver_api uart_stm32_driver_api = { |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 1500 | .poll_in = uart_stm32_poll_in, |
| 1501 | .poll_out = uart_stm32_poll_out, |
Georgij Cernysiov | c74c131 | 2019-02-14 10:50:19 +0100 | [diff] [blame] | 1502 | .err_check = uart_stm32_err_check, |
Daniel Leung | 4e1692f | 2021-05-26 12:33:37 -0700 | [diff] [blame] | 1503 | #ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 1504 | .configure = uart_stm32_configure, |
| 1505 | .config_get = uart_stm32_config_get, |
Daniel Leung | 4e1692f | 2021-05-26 12:33:37 -0700 | [diff] [blame] | 1506 | #endif /* CONFIG_UART_USE_RUNTIME_CONFIGURE */ |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1507 | #ifdef CONFIG_UART_INTERRUPT_DRIVEN |
| 1508 | .fifo_fill = uart_stm32_fifo_fill, |
| 1509 | .fifo_read = uart_stm32_fifo_read, |
| 1510 | .irq_tx_enable = uart_stm32_irq_tx_enable, |
| 1511 | .irq_tx_disable = uart_stm32_irq_tx_disable, |
| 1512 | .irq_tx_ready = uart_stm32_irq_tx_ready, |
Paul Sokolovsky | 0fdc9b5 | 2017-05-11 17:57:29 +0300 | [diff] [blame] | 1513 | .irq_tx_complete = uart_stm32_irq_tx_complete, |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1514 | .irq_rx_enable = uart_stm32_irq_rx_enable, |
| 1515 | .irq_rx_disable = uart_stm32_irq_rx_disable, |
| 1516 | .irq_rx_ready = uart_stm32_irq_rx_ready, |
| 1517 | .irq_err_enable = uart_stm32_irq_err_enable, |
| 1518 | .irq_err_disable = uart_stm32_irq_err_disable, |
| 1519 | .irq_is_pending = uart_stm32_irq_is_pending, |
| 1520 | .irq_update = uart_stm32_irq_update, |
| 1521 | .irq_callback_set = uart_stm32_irq_callback_set, |
| 1522 | #endif /* CONFIG_UART_INTERRUPT_DRIVEN */ |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1523 | #ifdef CONFIG_UART_ASYNC_API |
| 1524 | .callback_set = uart_stm32_async_callback_set, |
| 1525 | .tx = uart_stm32_async_tx, |
| 1526 | .tx_abort = uart_stm32_async_tx_abort, |
| 1527 | .rx_enable = uart_stm32_async_rx_enable, |
| 1528 | .rx_disable = uart_stm32_async_rx_disable, |
| 1529 | .rx_buf_rsp = uart_stm32_async_rx_buf_rsp, |
| 1530 | #endif /* CONFIG_UART_ASYNC_API */ |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 1531 | }; |
| 1532 | |
| 1533 | /** |
| 1534 | * @brief Initialize UART channel |
| 1535 | * |
| 1536 | * This routine is called to reset the chip in a quiescent state. |
| 1537 | * It is assumed that this function is called only once per UART. |
| 1538 | * |
| 1539 | * @param dev UART device struct |
| 1540 | * |
| 1541 | * @return 0 |
| 1542 | */ |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 1543 | static int uart_stm32_init(const struct device *dev) |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 1544 | { |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 1545 | const struct uart_stm32_config *config = dev->config; |
| 1546 | struct uart_stm32_data *data = dev->data; |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 1547 | uint32_t ll_parity; |
| 1548 | uint32_t ll_datawidth; |
Erwan Gouriou | 0b9c584 | 2020-10-16 17:20:00 +0200 | [diff] [blame] | 1549 | int err; |
Erwan Gouriou | da210ba | 2017-09-21 15:20:53 +0200 | [diff] [blame] | 1550 | |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 1551 | __uart_stm32_get_clock(dev); |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 1552 | /* enable clock */ |
Erwan Gouriou | 9062e97 | 2018-12-07 11:09:28 +0100 | [diff] [blame] | 1553 | if (clock_control_on(data->clock, |
| 1554 | (clock_control_subsys_t *)&config->pclken) != 0) { |
| 1555 | return -EIO; |
| 1556 | } |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 1557 | |
Erwan Gouriou | 252a623 | 2020-06-05 10:57:52 +0200 | [diff] [blame] | 1558 | /* Configure dt provided device signals when available */ |
Gerard Marull-Paretas | 21a2719 | 2021-09-07 16:39:45 +0200 | [diff] [blame] | 1559 | err = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT); |
Erwan Gouriou | 0b9c584 | 2020-10-16 17:20:00 +0200 | [diff] [blame] | 1560 | if (err < 0) { |
| 1561 | return err; |
Erwan Gouriou | 252a623 | 2020-06-05 10:57:52 +0200 | [diff] [blame] | 1562 | } |
| 1563 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1564 | LL_USART_Disable(config->usart); |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 1565 | |
Erwan Gouriou | da210ba | 2017-09-21 15:20:53 +0200 | [diff] [blame] | 1566 | /* TX/RX direction */ |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1567 | LL_USART_SetTransferDirection(config->usart, |
Erwan Gouriou | da210ba | 2017-09-21 15:20:53 +0200 | [diff] [blame] | 1568 | LL_USART_DIRECTION_TX_RX); |
| 1569 | |
Pauli Salmenrinne | 30003ff | 2020-03-18 13:40:21 +0200 | [diff] [blame] | 1570 | /* Determine the datawidth and parity. If we use other parity than |
| 1571 | * 'none' we must use datawidth = 9 (to get 8 databit + 1 parity bit). |
| 1572 | */ |
| 1573 | if (config->parity == 2) { |
| 1574 | /* 8 databit, 1 parity bit, parity even */ |
| 1575 | ll_parity = LL_USART_PARITY_EVEN; |
| 1576 | ll_datawidth = LL_USART_DATAWIDTH_9B; |
| 1577 | } else if (config->parity == 1) { |
| 1578 | /* 8 databit, 1 parity bit, parity odd */ |
| 1579 | ll_parity = LL_USART_PARITY_ODD; |
| 1580 | ll_datawidth = LL_USART_DATAWIDTH_9B; |
| 1581 | } else { /* Default to 8N0, but show warning if invalid value */ |
| 1582 | if (config->parity != 0) { |
| 1583 | LOG_WRN("Invalid parity setting '%d'." |
| 1584 | "Defaulting to 'none'.", config->parity); |
| 1585 | } |
| 1586 | /* 8 databit, parity none */ |
| 1587 | ll_parity = LL_USART_PARITY_NONE; |
| 1588 | ll_datawidth = LL_USART_DATAWIDTH_8B; |
| 1589 | } |
| 1590 | |
| 1591 | /* Set datawidth and parity, 1 start bit, 1 stop bit */ |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1592 | LL_USART_ConfigCharacter(config->usart, |
Pauli Salmenrinne | 30003ff | 2020-03-18 13:40:21 +0200 | [diff] [blame] | 1593 | ll_datawidth, |
| 1594 | ll_parity, |
Erwan Gouriou | da210ba | 2017-09-21 15:20:53 +0200 | [diff] [blame] | 1595 | LL_USART_STOPBITS_1); |
| 1596 | |
Georgij Cernysiov | 04da64d | 2019-02-08 18:39:35 +0100 | [diff] [blame] | 1597 | if (config->hw_flow_control) { |
| 1598 | uart_stm32_set_hwctrl(dev, LL_USART_HWCONTROL_RTS_CTS); |
| 1599 | } |
| 1600 | |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 1601 | /* Set the default baudrate */ |
| 1602 | uart_stm32_set_baudrate(dev, data->baud_rate); |
Erwan Gouriou | da210ba | 2017-09-21 15:20:53 +0200 | [diff] [blame] | 1603 | |
Jonathan Hahn | 32f9dcf | 2022-01-01 23:41:19 +0100 | [diff] [blame] | 1604 | /* Enable the single wire / half-duplex mode */ |
| 1605 | if (config->single_wire) { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1606 | LL_USART_EnableHalfDuplex(config->usart); |
Jonathan Hahn | 32f9dcf | 2022-01-01 23:41:19 +0100 | [diff] [blame] | 1607 | } |
| 1608 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1609 | LL_USART_Enable(config->usart); |
Erwan Gouriou | da210ba | 2017-09-21 15:20:53 +0200 | [diff] [blame] | 1610 | |
Erwan Gouriou | 80b8c50 | 2018-06-13 11:32:38 +0200 | [diff] [blame] | 1611 | #ifdef USART_ISR_TEACK |
| 1612 | /* Wait until TEACK flag is set */ |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1613 | while (!(LL_USART_IsActiveFlag_TEACK(config->usart))) { |
Anas Nashif | 4c32258 | 2019-06-04 10:52:23 -0400 | [diff] [blame] | 1614 | } |
Erwan Gouriou | 80b8c50 | 2018-06-13 11:32:38 +0200 | [diff] [blame] | 1615 | #endif /* !USART_ISR_TEACK */ |
| 1616 | |
| 1617 | #ifdef USART_ISR_REACK |
Erwan Gouriou | 13a9657 | 2018-06-18 18:01:06 +0200 | [diff] [blame] | 1618 | /* Wait until REACK flag is set */ |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1619 | while (!(LL_USART_IsActiveFlag_REACK(config->usart))) { |
Anas Nashif | 4c32258 | 2019-06-04 10:52:23 -0400 | [diff] [blame] | 1620 | } |
Erwan Gouriou | 80b8c50 | 2018-06-13 11:32:38 +0200 | [diff] [blame] | 1621 | #endif /* !USART_ISR_REACK */ |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 1622 | |
Gerard Marull-Paretas | e101cc7 | 2022-01-25 16:21:36 +0100 | [diff] [blame] | 1623 | #if defined(CONFIG_PM) || \ |
| 1624 | defined(CONFIG_UART_INTERRUPT_DRIVEN) || \ |
| 1625 | defined(CONFIG_UART_ASYNC_API) |
Gerard Marull-Paretas | e3f4907 | 2021-09-09 22:41:35 +0200 | [diff] [blame] | 1626 | config->irq_config_func(dev); |
Gerard Marull-Paretas | e101cc7 | 2022-01-25 16:21:36 +0100 | [diff] [blame] | 1627 | #endif /* CONFIG_PM || CONFIG_UART_INTERRUPT_DRIVEN || CONFIG_UART_ASYNC_API */ |
Gerard Marull-Paretas | e3f4907 | 2021-09-09 22:41:35 +0200 | [diff] [blame] | 1628 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1629 | #ifdef CONFIG_UART_ASYNC_API |
| 1630 | return uart_stm32_async_init(dev); |
| 1631 | #else |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 1632 | return 0; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1633 | #endif |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 1634 | } |
| 1635 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1636 | #ifdef CONFIG_UART_ASYNC_API |
Florian Vaussard | f27a5a3 | 2017-05-01 15:21:52 +0200 | [diff] [blame] | 1637 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1638 | /* src_dev and dest_dev should be 'MEMORY' or 'PERIPHERAL'. */ |
| 1639 | #define UART_DMA_CHANNEL_INIT(index, dir, dir_cap, src_dev, dest_dev) \ |
Francois Ramu | 30fd022 | 2021-06-18 15:52:30 +0200 | [diff] [blame] | 1640 | .dma_dev = DEVICE_DT_GET(STM32_DMA_CTLR(index, dir)), \ |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1641 | .dma_channel = DT_INST_DMAS_CELL_BY_NAME(index, dir, channel), \ |
| 1642 | .dma_cfg = { \ |
Francois Ramu | 744e1dc | 2021-08-09 16:32:57 +0200 | [diff] [blame] | 1643 | .dma_slot = STM32_DMA_SLOT(index, dir, slot),\ |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1644 | .channel_direction = STM32_DMA_CONFIG_DIRECTION( \ |
Francois Ramu | 30fd022 | 2021-06-18 15:52:30 +0200 | [diff] [blame] | 1645 | STM32_DMA_CHANNEL_CONFIG(index, dir)),\ |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1646 | .channel_priority = STM32_DMA_CONFIG_PRIORITY( \ |
Francois Ramu | 30fd022 | 2021-06-18 15:52:30 +0200 | [diff] [blame] | 1647 | STM32_DMA_CHANNEL_CONFIG(index, dir)), \ |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1648 | .source_data_size = STM32_DMA_CONFIG_##src_dev##_DATA_SIZE(\ |
Francois Ramu | 30fd022 | 2021-06-18 15:52:30 +0200 | [diff] [blame] | 1649 | STM32_DMA_CHANNEL_CONFIG(index, dir)),\ |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1650 | .dest_data_size = STM32_DMA_CONFIG_##dest_dev##_DATA_SIZE(\ |
Francois Ramu | 30fd022 | 2021-06-18 15:52:30 +0200 | [diff] [blame] | 1651 | STM32_DMA_CHANNEL_CONFIG(index, dir)),\ |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1652 | .source_burst_length = 1, /* SINGLE transfer */ \ |
| 1653 | .dest_burst_length = 1, \ |
| 1654 | .block_count = 1, \ |
| 1655 | .dma_callback = uart_stm32_dma_##dir##_cb, \ |
| 1656 | }, \ |
| 1657 | .src_addr_increment = STM32_DMA_CONFIG_##src_dev##_ADDR_INC( \ |
Francois Ramu | 30fd022 | 2021-06-18 15:52:30 +0200 | [diff] [blame] | 1658 | STM32_DMA_CHANNEL_CONFIG(index, dir)), \ |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1659 | .dst_addr_increment = STM32_DMA_CONFIG_##dest_dev##_ADDR_INC( \ |
Francois Ramu | 30fd022 | 2021-06-18 15:52:30 +0200 | [diff] [blame] | 1660 | STM32_DMA_CHANNEL_CONFIG(index, dir)), \ |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1661 | .fifo_threshold = STM32_DMA_FEATURES_FIFO_THRESHOLD( \ |
Francois Ramu | 30fd022 | 2021-06-18 15:52:30 +0200 | [diff] [blame] | 1662 | STM32_DMA_FEATURES(index, dir)), \ |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1663 | |
| 1664 | #endif |
| 1665 | |
Gerard Marull-Paretas | e3f4907 | 2021-09-09 22:41:35 +0200 | [diff] [blame] | 1666 | #if defined(CONFIG_UART_INTERRUPT_DRIVEN) || defined(CONFIG_UART_ASYNC_API) || \ |
| 1667 | defined(CONFIG_PM) |
Erwan Gouriou | 6275513 | 2020-02-28 14:54:37 +0100 | [diff] [blame] | 1668 | #define STM32_UART_IRQ_HANDLER_DECL(index) \ |
Gerard Marull-Paretas | e3f4907 | 2021-09-09 22:41:35 +0200 | [diff] [blame] | 1669 | static void uart_stm32_irq_config_func_##index(const struct device *dev); |
Erwan Gouriou | 6275513 | 2020-02-28 14:54:37 +0100 | [diff] [blame] | 1670 | #define STM32_UART_IRQ_HANDLER(index) \ |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 1671 | static void uart_stm32_irq_config_func_##index(const struct device *dev) \ |
Florian Vaussard | f27a5a3 | 2017-05-01 15:21:52 +0200 | [diff] [blame] | 1672 | { \ |
Erwan Gouriou | ccd6b72 | 2020-06-05 09:48:29 +0200 | [diff] [blame] | 1673 | IRQ_CONNECT(DT_INST_IRQN(index), \ |
| 1674 | DT_INST_IRQ(index, priority), \ |
Kumar Gala | c49b162 | 2020-12-11 10:12:30 -0600 | [diff] [blame] | 1675 | uart_stm32_isr, DEVICE_DT_INST_GET(index), \ |
Florian Vaussard | f27a5a3 | 2017-05-01 15:21:52 +0200 | [diff] [blame] | 1676 | 0); \ |
Erwan Gouriou | ccd6b72 | 2020-06-05 09:48:29 +0200 | [diff] [blame] | 1677 | irq_enable(DT_INST_IRQN(index)); \ |
Florian Vaussard | f27a5a3 | 2017-05-01 15:21:52 +0200 | [diff] [blame] | 1678 | } |
| 1679 | #else |
Gerard Marull-Paretas | e3f4907 | 2021-09-09 22:41:35 +0200 | [diff] [blame] | 1680 | #define STM32_UART_IRQ_HANDLER_DECL(index) /* Not used */ |
| 1681 | #define STM32_UART_IRQ_HANDLER(index) /* Not used */ |
| 1682 | #endif |
| 1683 | |
Gerard Marull-Paretas | e101cc7 | 2022-01-25 16:21:36 +0100 | [diff] [blame] | 1684 | #if defined(CONFIG_UART_INTERRUPT_DRIVEN) || defined(CONFIG_UART_ASYNC_API) || \ |
| 1685 | defined(CONFIG_PM) |
Gerard Marull-Paretas | e3f4907 | 2021-09-09 22:41:35 +0200 | [diff] [blame] | 1686 | #define STM32_UART_IRQ_HANDLER_FUNC(index) \ |
| 1687 | .irq_config_func = uart_stm32_irq_config_func_##index, |
Gerard Marull-Paretas | e3f4907 | 2021-09-09 22:41:35 +0200 | [diff] [blame] | 1688 | #else |
| 1689 | #define STM32_UART_IRQ_HANDLER_FUNC(index) /* Not used */ |
Florian Vaussard | f27a5a3 | 2017-05-01 15:21:52 +0200 | [diff] [blame] | 1690 | #endif |
| 1691 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1692 | #ifdef CONFIG_UART_ASYNC_API |
| 1693 | #define UART_DMA_CHANNEL(index, dir, DIR, src, dest) \ |
| 1694 | .dma_##dir = { \ |
| 1695 | COND_CODE_1(DT_INST_DMAS_HAS_NAME(index, dir), \ |
| 1696 | (UART_DMA_CHANNEL_INIT(index, dir, DIR, src, dest)), \ |
| 1697 | (NULL)) \ |
| 1698 | }, |
| 1699 | |
| 1700 | #else |
| 1701 | #define UART_DMA_CHANNEL(index, dir, DIR, src, dest) |
| 1702 | #endif |
| 1703 | |
Erwan Gouriou | 6275513 | 2020-02-28 14:54:37 +0100 | [diff] [blame] | 1704 | #define STM32_UART_INIT(index) \ |
Gerard Marull-Paretas | e3f4907 | 2021-09-09 22:41:35 +0200 | [diff] [blame] | 1705 | STM32_UART_IRQ_HANDLER_DECL(index) \ |
Florian Vaussard | f27a5a3 | 2017-05-01 15:21:52 +0200 | [diff] [blame] | 1706 | \ |
Gerard Marull-Paretas | 5dc6ed3 | 2021-12-23 12:33:03 +0100 | [diff] [blame] | 1707 | PINCTRL_DT_INST_DEFINE(index); \ |
Erwan Gouriou | 252a623 | 2020-06-05 10:57:52 +0200 | [diff] [blame] | 1708 | \ |
Erwan Gouriou | 6275513 | 2020-02-28 14:54:37 +0100 | [diff] [blame] | 1709 | static const struct uart_stm32_config uart_stm32_cfg_##index = { \ |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1710 | .usart = (USART_TypeDef *)DT_INST_REG_ADDR(index), \ |
Erwan Gouriou | ccd6b72 | 2020-06-05 09:48:29 +0200 | [diff] [blame] | 1711 | .pclken = { .bus = DT_INST_CLOCKS_CELL(index, bus), \ |
| 1712 | .enr = DT_INST_CLOCKS_CELL(index, bits) \ |
Erwan Gouriou | d76a559 | 2018-11-08 14:38:48 +0100 | [diff] [blame] | 1713 | }, \ |
Erwan Gouriou | ccd6b72 | 2020-06-05 09:48:29 +0200 | [diff] [blame] | 1714 | .hw_flow_control = DT_INST_PROP(index, hw_flow_control), \ |
Gerard Marull-Paretas | c759a35 | 2021-11-17 14:01:42 +0100 | [diff] [blame] | 1715 | .parity = DT_INST_ENUM_IDX_OR(index, parity, UART_CFG_PARITY_NONE), \ |
Gerard Marull-Paretas | 21a2719 | 2021-09-07 16:39:45 +0200 | [diff] [blame] | 1716 | .pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(index), \ |
Jonathan Hahn | 32f9dcf | 2022-01-01 23:41:19 +0100 | [diff] [blame] | 1717 | .single_wire = DT_INST_PROP_OR(index, single_wire, false), \ |
Gerard Marull-Paretas | e101cc7 | 2022-01-25 16:21:36 +0100 | [diff] [blame] | 1718 | STM32_UART_IRQ_HANDLER_FUNC(index) \ |
Florian Vaussard | f27a5a3 | 2017-05-01 15:21:52 +0200 | [diff] [blame] | 1719 | }; \ |
| 1720 | \ |
Erwan Gouriou | 6275513 | 2020-02-28 14:54:37 +0100 | [diff] [blame] | 1721 | static struct uart_stm32_data uart_stm32_data_##index = { \ |
Erwan Gouriou | ccd6b72 | 2020-06-05 09:48:29 +0200 | [diff] [blame] | 1722 | .baud_rate = DT_INST_PROP(index, current_speed), \ |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1723 | UART_DMA_CHANNEL(index, rx, RX, PERIPHERAL, MEMORY) \ |
| 1724 | UART_DMA_CHANNEL(index, tx, TX, MEMORY, PERIPHERAL) \ |
Florian Vaussard | f27a5a3 | 2017-05-01 15:21:52 +0200 | [diff] [blame] | 1725 | }; \ |
| 1726 | \ |
Kumar Gala | c49b162 | 2020-12-11 10:12:30 -0600 | [diff] [blame] | 1727 | DEVICE_DT_INST_DEFINE(index, \ |
Florian Vaussard | f27a5a3 | 2017-05-01 15:21:52 +0200 | [diff] [blame] | 1728 | &uart_stm32_init, \ |
Gerard Marull-Paretas | e3f4907 | 2021-09-09 22:41:35 +0200 | [diff] [blame] | 1729 | NULL, \ |
Erwan Gouriou | 6275513 | 2020-02-28 14:54:37 +0100 | [diff] [blame] | 1730 | &uart_stm32_data_##index, &uart_stm32_cfg_##index, \ |
Maureen Helm | ad14505 | 2021-10-14 09:38:10 -0500 | [diff] [blame] | 1731 | PRE_KERNEL_1, CONFIG_SERIAL_INIT_PRIORITY, \ |
Florian Vaussard | f27a5a3 | 2017-05-01 15:21:52 +0200 | [diff] [blame] | 1732 | &uart_stm32_driver_api); \ |
| 1733 | \ |
Erwan Gouriou | 6275513 | 2020-02-28 14:54:37 +0100 | [diff] [blame] | 1734 | STM32_UART_IRQ_HANDLER(index) |
Erwan Gouriou | 8c079e9 | 2016-11-14 11:53:52 +0100 | [diff] [blame] | 1735 | |
Martí Bolívar | 7e0eed9 | 2020-05-06 11:23:07 -0700 | [diff] [blame] | 1736 | DT_INST_FOREACH_STATUS_OKAY(STM32_UART_INIT) |