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