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. |
Abderrahmane Jarmouni | e783aaf | 2024-03-20 15:25:14 +0100 | [diff] [blame] | 4 | * Copyright (c) 2024 STMicroelectronics |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 5 | * |
David B. Kinder | ac74d8b | 2017-01-18 17:01:01 -0800 | [diff] [blame] | 6 | * SPDX-License-Identifier: Apache-2.0 |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
Kumar Gala | 989484b | 2020-03-24 14:28:48 -0500 | [diff] [blame] | 9 | #define DT_DRV_COMPAT st_stm32_uart |
| 10 | |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 11 | /** |
Ilya Tagunov | 84cffc7 | 2018-03-20 20:44:45 +0300 | [diff] [blame] | 12 | * @brief Driver for UART port on STM32 family processor. |
Georgij Cernysiov | 78eed34 | 2019-03-15 19:51:09 +0100 | [diff] [blame] | 13 | * @note LPUART and U(S)ART have the same base and |
| 14 | * majority of operations are performed the same way. |
| 15 | * Please validate for newly added series. |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 16 | */ |
| 17 | |
Gerard Marull-Paretas | fb60aab | 2022-05-06 10:25:46 +0200 | [diff] [blame] | 18 | #include <zephyr/kernel.h> |
| 19 | #include <zephyr/arch/cpu.h> |
| 20 | #include <zephyr/sys/__assert.h> |
Kumar Gala | aa2bdbe | 2018-10-31 12:44:45 -0500 | [diff] [blame] | 21 | #include <soc.h> |
Gerard Marull-Paretas | fb60aab | 2022-05-06 10:25:46 +0200 | [diff] [blame] | 22 | #include <zephyr/init.h> |
Gerard Marull-Paretas | fb60aab | 2022-05-06 10:25:46 +0200 | [diff] [blame] | 23 | #include <zephyr/drivers/clock_control.h> |
| 24 | #include <zephyr/pm/policy.h> |
Erwan Gouriou | a439c04 | 2022-08-03 14:39:59 +0200 | [diff] [blame] | 25 | #include <zephyr/pm/device.h> |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 26 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 27 | #ifdef CONFIG_UART_ASYNC_API |
Gerard Marull-Paretas | fb60aab | 2022-05-06 10:25:46 +0200 | [diff] [blame] | 28 | #include <zephyr/drivers/dma/dma_stm32.h> |
| 29 | #include <zephyr/drivers/dma.h> |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 30 | #endif |
| 31 | |
Gerard Marull-Paretas | fb60aab | 2022-05-06 10:25:46 +0200 | [diff] [blame] | 32 | #include <zephyr/linker/sections.h> |
| 33 | #include <zephyr/drivers/clock_control/stm32_clock_control.h> |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 34 | #include "uart_stm32.h" |
| 35 | |
Gerard Marull-Paretas | e83fab3 | 2020-10-03 23:58:36 +0200 | [diff] [blame] | 36 | #include <stm32_ll_usart.h> |
| 37 | #include <stm32_ll_lpuart.h> |
Francois Ramu | 9926b90 | 2022-09-16 10:41:18 +0200 | [diff] [blame] | 38 | #if defined(CONFIG_PM) && defined(IS_UART_WAKEUP_FROMSTOP_INSTANCE) |
| 39 | #include <stm32_ll_exti.h> |
| 40 | #endif /* CONFIG_PM */ |
Gerard Marull-Paretas | e83fab3 | 2020-10-03 23:58:36 +0200 | [diff] [blame] | 41 | |
Abderrahmane Jarmouni | e783aaf | 2024-03-20 15:25:14 +0100 | [diff] [blame] | 42 | #ifdef CONFIG_DCACHE |
| 43 | #include <zephyr/linker/linker-defs.h> |
| 44 | #include <zephyr/mem_mgmt/mem_attr.h> |
| 45 | #include <zephyr/dt-bindings/memory-attr/memory-attr-arm.h> |
| 46 | #endif /* CONFIG_DCACHE */ |
| 47 | |
Gerard Marull-Paretas | fb60aab | 2022-05-06 10:25:46 +0200 | [diff] [blame] | 48 | #include <zephyr/logging/log.h> |
Gerard Marull-Paretas | 178bdc4 | 2022-10-17 10:24:11 +0200 | [diff] [blame] | 49 | #include <zephyr/irq.h> |
Pete Dietl | 060e39e | 2022-03-04 12:56:05 -0800 | [diff] [blame] | 50 | LOG_MODULE_REGISTER(uart_stm32, CONFIG_UART_LOG_LEVEL); |
Francois Ramu | d3ffa8d | 2019-11-12 16:13:03 +0100 | [diff] [blame] | 51 | |
Artur Lipowski | bb85759 | 2022-05-10 12:18:28 +0200 | [diff] [blame] | 52 | /* This symbol takes the value 1 if one of the device instances */ |
Erwan Gouriou | 1ef9e9e | 2022-08-04 11:08:15 +0200 | [diff] [blame] | 53 | /* is configured in dts with a domain clock */ |
| 54 | #if STM32_DT_INST_DEV_DOMAIN_CLOCK_SUPPORT |
| 55 | #define STM32_UART_DOMAIN_CLOCK_SUPPORT 1 |
Artur Lipowski | bb85759 | 2022-05-10 12:18:28 +0200 | [diff] [blame] | 56 | #else |
Erwan Gouriou | 1ef9e9e | 2022-08-04 11:08:15 +0200 | [diff] [blame] | 57 | #define STM32_UART_DOMAIN_CLOCK_SUPPORT 0 |
Artur Lipowski | bb85759 | 2022-05-10 12:18:28 +0200 | [diff] [blame] | 58 | #endif |
| 59 | |
Nando Galliard | b9748b5 | 2023-10-16 12:20:23 +0200 | [diff] [blame] | 60 | #define HAS_LPUART DT_HAS_COMPAT_STATUS_OKAY(st_stm32_lpuart) |
Erwan Gouriou | 76313f0 | 2020-04-17 12:33:29 +0200 | [diff] [blame] | 61 | |
Armin Brauns | b95cdb2 | 2023-05-22 14:29:23 +0000 | [diff] [blame] | 62 | /* Available everywhere except l1, f1, f2, f4. */ |
| 63 | #ifdef USART_CR3_DEM |
| 64 | #define HAS_DRIVER_ENABLE 1 |
| 65 | #else |
| 66 | #define HAS_DRIVER_ENABLE 0 |
| 67 | #endif |
| 68 | |
Mathieu Choplain | b1b85fc | 2024-08-22 16:50:37 +0200 | [diff] [blame] | 69 | #ifdef CONFIG_PM |
| 70 | /* Placeholder value when wakeup-line DT property is not defined */ |
| 71 | #define STM32_WAKEUP_LINE_NONE 0xFFFFFFFF |
| 72 | #endif |
| 73 | |
Nando Galliard | b9748b5 | 2023-10-16 12:20:23 +0200 | [diff] [blame] | 74 | #if HAS_LPUART |
Yong Cong Sin | a6ebcdd | 2021-10-22 00:02:29 +0800 | [diff] [blame] | 75 | #ifdef USART_PRESC_PRESCALER |
| 76 | uint32_t lpuartdiv_calc(const uint64_t clock_rate, const uint16_t presc_idx, |
| 77 | const uint32_t baud_rate) |
| 78 | { |
| 79 | uint64_t lpuartdiv; |
| 80 | |
| 81 | lpuartdiv = clock_rate / LPUART_PRESCALER_TAB[presc_idx]; |
| 82 | lpuartdiv *= LPUART_LPUARTDIV_FREQ_MUL; |
| 83 | lpuartdiv += baud_rate / 2; |
| 84 | lpuartdiv /= baud_rate; |
| 85 | |
| 86 | return (uint32_t)lpuartdiv; |
| 87 | } |
| 88 | #else |
| 89 | uint32_t lpuartdiv_calc(const uint64_t clock_rate, const uint32_t baud_rate) |
| 90 | { |
| 91 | uint64_t lpuartdiv; |
| 92 | |
| 93 | lpuartdiv = clock_rate * LPUART_LPUARTDIV_FREQ_MUL; |
| 94 | lpuartdiv += baud_rate / 2; |
| 95 | lpuartdiv /= baud_rate; |
| 96 | |
| 97 | return (uint32_t)lpuartdiv; |
| 98 | } |
| 99 | #endif /* USART_PRESC_PRESCALER */ |
Nando Galliard | b9748b5 | 2023-10-16 12:20:23 +0200 | [diff] [blame] | 100 | #endif /* HAS_LPUART */ |
Yong Cong Sin | a6ebcdd | 2021-10-22 00:02:29 +0800 | [diff] [blame] | 101 | |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 102 | #ifdef CONFIG_PM |
Gerard Marull-Paretas | 5a71eeb | 2022-01-20 16:06:28 +0100 | [diff] [blame] | 103 | static void uart_stm32_pm_policy_state_lock_get(const struct device *dev) |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 104 | { |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 105 | struct uart_stm32_data *data = dev->data; |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 106 | |
Gerard Marull-Paretas | 5a71eeb | 2022-01-20 16:06:28 +0100 | [diff] [blame] | 107 | if (!data->pm_policy_state_on) { |
| 108 | data->pm_policy_state_on = true; |
Carlo Caione | 69b28bf | 2022-04-07 13:06:12 +0200 | [diff] [blame] | 109 | pm_policy_state_lock_get(PM_STATE_SUSPEND_TO_IDLE, PM_ALL_SUBSTATES); |
Guillaume Gautier | be26c71 | 2024-01-17 09:02:53 +0100 | [diff] [blame] | 110 | if (IS_ENABLED(CONFIG_PM_S2RAM)) { |
| 111 | pm_policy_state_lock_get(PM_STATE_SUSPEND_TO_RAM, PM_ALL_SUBSTATES); |
| 112 | } |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
Gerard Marull-Paretas | 5a71eeb | 2022-01-20 16:06:28 +0100 | [diff] [blame] | 116 | static void uart_stm32_pm_policy_state_lock_put(const struct device *dev) |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 117 | { |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 118 | struct uart_stm32_data *data = dev->data; |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 119 | |
Gerard Marull-Paretas | 5a71eeb | 2022-01-20 16:06:28 +0100 | [diff] [blame] | 120 | if (data->pm_policy_state_on) { |
| 121 | data->pm_policy_state_on = false; |
Carlo Caione | 69b28bf | 2022-04-07 13:06:12 +0200 | [diff] [blame] | 122 | pm_policy_state_lock_put(PM_STATE_SUSPEND_TO_IDLE, PM_ALL_SUBSTATES); |
Guillaume Gautier | be26c71 | 2024-01-17 09:02:53 +0100 | [diff] [blame] | 123 | if (IS_ENABLED(CONFIG_PM_S2RAM)) { |
| 124 | pm_policy_state_lock_put(PM_STATE_SUSPEND_TO_RAM, PM_ALL_SUBSTATES); |
| 125 | } |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | #endif /* CONFIG_PM */ |
| 129 | |
Artur Lipowski | bb85759 | 2022-05-10 12:18:28 +0200 | [diff] [blame] | 130 | static inline void uart_stm32_set_baudrate(const struct device *dev, uint32_t baud_rate) |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 131 | { |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 132 | const struct uart_stm32_config *config = dev->config; |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 133 | USART_TypeDef *usart = config->usart; |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 134 | struct uart_stm32_data *data = dev->data; |
Erwan Gouriou | 69a2896 | 2019-05-20 17:15:02 +0200 | [diff] [blame] | 135 | |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 136 | uint32_t clock_rate; |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 137 | |
| 138 | /* Get clock rate */ |
Erwan Gouriou | 1ef9e9e | 2022-08-04 11:08:15 +0200 | [diff] [blame] | 139 | if (IS_ENABLED(STM32_UART_DOMAIN_CLOCK_SUPPORT) && (config->pclk_len > 1)) { |
Artur Lipowski | bb85759 | 2022-05-10 12:18:28 +0200 | [diff] [blame] | 140 | if (clock_control_get_rate(data->clock, |
| 141 | (clock_control_subsys_t)&config->pclken[1], |
| 142 | &clock_rate) < 0) { |
| 143 | LOG_ERR("Failed call clock_control_get_rate(pclken[1])"); |
| 144 | return; |
| 145 | } |
| 146 | } else { |
| 147 | if (clock_control_get_rate(data->clock, |
| 148 | (clock_control_subsys_t)&config->pclken[0], |
| 149 | &clock_rate) < 0) { |
| 150 | LOG_ERR("Failed call clock_control_get_rate(pclken[0])"); |
| 151 | return; |
| 152 | } |
Francois Ramu | d3ffa8d | 2019-11-12 16:13:03 +0100 | [diff] [blame] | 153 | } |
Erwan Gouriou | 69a2896 | 2019-05-20 17:15:02 +0200 | [diff] [blame] | 154 | |
Nando Galliard | b9748b5 | 2023-10-16 12:20:23 +0200 | [diff] [blame] | 155 | #if HAS_LPUART |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 156 | if (IS_LPUART_INSTANCE(usart)) { |
Yong Cong Sin | a6ebcdd | 2021-10-22 00:02:29 +0800 | [diff] [blame] | 157 | uint32_t lpuartdiv; |
| 158 | #ifdef USART_PRESC_PRESCALER |
| 159 | uint8_t presc_idx; |
| 160 | uint32_t presc_val; |
| 161 | |
| 162 | for (presc_idx = 0; presc_idx < ARRAY_SIZE(LPUART_PRESCALER_TAB); presc_idx++) { |
| 163 | lpuartdiv = lpuartdiv_calc(clock_rate, presc_idx, baud_rate); |
| 164 | if (lpuartdiv >= LPUART_BRR_MIN_VALUE && lpuartdiv <= LPUART_BRR_MASK) { |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | if (presc_idx == ARRAY_SIZE(LPUART_PRESCALER_TAB)) { |
| 170 | LOG_ERR("Unable to set %s to %d", dev->name, baud_rate); |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | presc_val = presc_idx << USART_PRESC_PRESCALER_Pos; |
| 175 | |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 176 | LL_LPUART_SetPrescaler(usart, presc_val); |
Yong Cong Sin | a6ebcdd | 2021-10-22 00:02:29 +0800 | [diff] [blame] | 177 | #else |
| 178 | lpuartdiv = lpuartdiv_calc(clock_rate, baud_rate); |
| 179 | if (lpuartdiv < LPUART_BRR_MIN_VALUE || lpuartdiv > LPUART_BRR_MASK) { |
| 180 | LOG_ERR("Unable to set %s to %d", dev->name, baud_rate); |
| 181 | return; |
| 182 | } |
| 183 | #endif /* USART_PRESC_PRESCALER */ |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 184 | LL_LPUART_SetBaudRate(usart, |
Erwan Gouriou | 69a2896 | 2019-05-20 17:15:02 +0200 | [diff] [blame] | 185 | clock_rate, |
| 186 | #ifdef USART_PRESC_PRESCALER |
Yong Cong Sin | a6ebcdd | 2021-10-22 00:02:29 +0800 | [diff] [blame] | 187 | presc_val, |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 188 | #endif |
Erwan Gouriou | 69a2896 | 2019-05-20 17:15:02 +0200 | [diff] [blame] | 189 | baud_rate); |
Francois Ramu | 7b95803 | 2022-09-08 17:26:29 +0200 | [diff] [blame] | 190 | /* Check BRR is greater than or equal to 0x300 */ |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 191 | __ASSERT(LL_LPUART_ReadReg(usart, BRR) >= 0x300U, |
Francois Ramu | 7b95803 | 2022-09-08 17:26:29 +0200 | [diff] [blame] | 192 | "BaudRateReg >= 0x300"); |
| 193 | |
| 194 | /* Check BRR is lower than or equal to 0xFFFFF */ |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 195 | __ASSERT(LL_LPUART_ReadReg(usart, BRR) < 0x000FFFFFU, |
Francois Ramu | 7b95803 | 2022-09-08 17:26:29 +0200 | [diff] [blame] | 196 | "BaudRateReg < 0xFFFF"); |
Erwan Gouriou | 69a2896 | 2019-05-20 17:15:02 +0200 | [diff] [blame] | 197 | } else { |
Nando Galliard | b9748b5 | 2023-10-16 12:20:23 +0200 | [diff] [blame] | 198 | #endif /* HAS_LPUART */ |
Erwan Gouriou | 37c7b89 | 2021-01-08 10:54:46 +0100 | [diff] [blame] | 199 | #ifdef USART_CR1_OVER8 |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 200 | LL_USART_SetOverSampling(usart, |
Erwan Gouriou | 37c7b89 | 2021-01-08 10:54:46 +0100 | [diff] [blame] | 201 | LL_USART_OVERSAMPLING_16); |
| 202 | #endif |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 203 | LL_USART_SetBaudRate(usart, |
Erwan Gouriou | 69a2896 | 2019-05-20 17:15:02 +0200 | [diff] [blame] | 204 | clock_rate, |
| 205 | #ifdef USART_PRESC_PRESCALER |
| 206 | LL_USART_PRESCALER_DIV1, |
| 207 | #endif |
| 208 | #ifdef USART_CR1_OVER8 |
| 209 | LL_USART_OVERSAMPLING_16, |
| 210 | #endif |
| 211 | baud_rate); |
Francois Ramu | 7b95803 | 2022-09-08 17:26:29 +0200 | [diff] [blame] | 212 | /* Check BRR is greater than or equal to 16d */ |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 213 | __ASSERT(LL_USART_ReadReg(usart, BRR) >= 16, |
Francois Ramu | 7b95803 | 2022-09-08 17:26:29 +0200 | [diff] [blame] | 214 | "BaudRateReg >= 16"); |
Erwan Gouriou | 69a2896 | 2019-05-20 17:15:02 +0200 | [diff] [blame] | 215 | |
Nando Galliard | b9748b5 | 2023-10-16 12:20:23 +0200 | [diff] [blame] | 216 | #if HAS_LPUART |
Erwan Gouriou | 69a2896 | 2019-05-20 17:15:02 +0200 | [diff] [blame] | 217 | } |
Nando Galliard | b9748b5 | 2023-10-16 12:20:23 +0200 | [diff] [blame] | 218 | #endif /* HAS_LPUART */ |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 221 | static inline void uart_stm32_set_parity(const struct device *dev, |
| 222 | uint32_t parity) |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 223 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 224 | const struct uart_stm32_config *config = dev->config; |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 225 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 226 | LL_USART_SetParity(config->usart, parity); |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 229 | static inline uint32_t uart_stm32_get_parity(const struct device *dev) |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 230 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 231 | const struct uart_stm32_config *config = dev->config; |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 232 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 233 | return LL_USART_GetParity(config->usart); |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 234 | } |
| 235 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 236 | static inline void uart_stm32_set_stopbits(const struct device *dev, |
| 237 | uint32_t stopbits) |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 238 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 239 | const struct uart_stm32_config *config = dev->config; |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 240 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 241 | LL_USART_SetStopBitsLength(config->usart, stopbits); |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 244 | static inline uint32_t uart_stm32_get_stopbits(const struct device *dev) |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 245 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 246 | const struct uart_stm32_config *config = dev->config; |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 247 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 248 | return LL_USART_GetStopBitsLength(config->usart); |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 251 | static inline void uart_stm32_set_databits(const struct device *dev, |
| 252 | uint32_t databits) |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 253 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 254 | const struct uart_stm32_config *config = dev->config; |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 255 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 256 | LL_USART_SetDataWidth(config->usart, databits); |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 257 | } |
| 258 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 259 | static inline uint32_t uart_stm32_get_databits(const struct device *dev) |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 260 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 261 | const struct uart_stm32_config *config = dev->config; |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 262 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 263 | return LL_USART_GetDataWidth(config->usart); |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 264 | } |
| 265 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 266 | static inline void uart_stm32_set_hwctrl(const struct device *dev, |
| 267 | uint32_t hwctrl) |
Georgij Cernysiov | 78eed34 | 2019-03-15 19:51:09 +0100 | [diff] [blame] | 268 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 269 | const struct uart_stm32_config *config = dev->config; |
Georgij Cernysiov | 78eed34 | 2019-03-15 19:51:09 +0100 | [diff] [blame] | 270 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 271 | LL_USART_SetHWFlowCtrl(config->usart, hwctrl); |
Georgij Cernysiov | 78eed34 | 2019-03-15 19:51:09 +0100 | [diff] [blame] | 272 | } |
| 273 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 274 | static inline uint32_t uart_stm32_get_hwctrl(const struct device *dev) |
Georgij Cernysiov | 78eed34 | 2019-03-15 19:51:09 +0100 | [diff] [blame] | 275 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 276 | const struct uart_stm32_config *config = dev->config; |
Georgij Cernysiov | 78eed34 | 2019-03-15 19:51:09 +0100 | [diff] [blame] | 277 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 278 | return LL_USART_GetHWFlowCtrl(config->usart); |
Georgij Cernysiov | 78eed34 | 2019-03-15 19:51:09 +0100 | [diff] [blame] | 279 | } |
| 280 | |
Armin Brauns | b95cdb2 | 2023-05-22 14:29:23 +0000 | [diff] [blame] | 281 | #if HAS_DRIVER_ENABLE |
| 282 | static inline void uart_stm32_set_driver_enable(const struct device *dev, |
| 283 | bool driver_enable) |
| 284 | { |
| 285 | const struct uart_stm32_config *config = dev->config; |
| 286 | |
| 287 | if (driver_enable) { |
| 288 | LL_USART_EnableDEMode(config->usart); |
| 289 | } else { |
| 290 | LL_USART_DisableDEMode(config->usart); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | static inline bool uart_stm32_get_driver_enable(const struct device *dev) |
| 295 | { |
| 296 | const struct uart_stm32_config *config = dev->config; |
| 297 | |
| 298 | return LL_USART_IsEnabledDEMode(config->usart); |
| 299 | } |
| 300 | #endif |
| 301 | |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 302 | 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] | 303 | { |
| 304 | switch (parity) { |
| 305 | case UART_CFG_PARITY_ODD: |
| 306 | return LL_USART_PARITY_ODD; |
| 307 | case UART_CFG_PARITY_EVEN: |
| 308 | return LL_USART_PARITY_EVEN; |
| 309 | case UART_CFG_PARITY_NONE: |
| 310 | default: |
| 311 | return LL_USART_PARITY_NONE; |
| 312 | } |
| 313 | } |
| 314 | |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 315 | 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] | 316 | { |
| 317 | switch (parity) { |
| 318 | case LL_USART_PARITY_ODD: |
| 319 | return UART_CFG_PARITY_ODD; |
| 320 | case LL_USART_PARITY_EVEN: |
| 321 | return UART_CFG_PARITY_EVEN; |
| 322 | case LL_USART_PARITY_NONE: |
| 323 | default: |
| 324 | return UART_CFG_PARITY_NONE; |
| 325 | } |
| 326 | } |
| 327 | |
Jeroen van Dooren | 250e1f9 | 2023-02-06 09:39:37 +0100 | [diff] [blame] | 328 | static inline uint32_t uart_stm32_cfg2ll_stopbits(const struct uart_stm32_config *config, |
| 329 | enum uart_config_stop_bits sb) |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 330 | { |
| 331 | switch (sb) { |
| 332 | /* Some MCU's don't support 0.5 stop bits */ |
| 333 | #ifdef LL_USART_STOPBITS_0_5 |
| 334 | case UART_CFG_STOP_BITS_0_5: |
Nando Galliard | b9748b5 | 2023-10-16 12:20:23 +0200 | [diff] [blame] | 335 | #if HAS_LPUART |
Jeroen van Dooren | 250e1f9 | 2023-02-06 09:39:37 +0100 | [diff] [blame] | 336 | if (IS_LPUART_INSTANCE(config->usart)) { |
| 337 | /* return the default */ |
| 338 | return LL_USART_STOPBITS_1; |
| 339 | } |
Nando Galliard | b9748b5 | 2023-10-16 12:20:23 +0200 | [diff] [blame] | 340 | #endif /* HAS_LPUART */ |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 341 | return LL_USART_STOPBITS_0_5; |
| 342 | #endif /* LL_USART_STOPBITS_0_5 */ |
| 343 | case UART_CFG_STOP_BITS_1: |
| 344 | return LL_USART_STOPBITS_1; |
| 345 | /* Some MCU's don't support 1.5 stop bits */ |
| 346 | #ifdef LL_USART_STOPBITS_1_5 |
| 347 | case UART_CFG_STOP_BITS_1_5: |
Nando Galliard | b9748b5 | 2023-10-16 12:20:23 +0200 | [diff] [blame] | 348 | #if HAS_LPUART |
Jeroen van Dooren | 250e1f9 | 2023-02-06 09:39:37 +0100 | [diff] [blame] | 349 | if (IS_LPUART_INSTANCE(config->usart)) { |
| 350 | /* return the default */ |
| 351 | return LL_USART_STOPBITS_2; |
| 352 | } |
| 353 | #endif |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 354 | return LL_USART_STOPBITS_1_5; |
| 355 | #endif /* LL_USART_STOPBITS_1_5 */ |
| 356 | case UART_CFG_STOP_BITS_2: |
| 357 | default: |
| 358 | return LL_USART_STOPBITS_2; |
| 359 | } |
| 360 | } |
| 361 | |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 362 | 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] | 363 | { |
| 364 | switch (sb) { |
| 365 | /* Some MCU's don't support 0.5 stop bits */ |
| 366 | #ifdef LL_USART_STOPBITS_0_5 |
| 367 | case LL_USART_STOPBITS_0_5: |
| 368 | return UART_CFG_STOP_BITS_0_5; |
| 369 | #endif /* LL_USART_STOPBITS_0_5 */ |
| 370 | case LL_USART_STOPBITS_1: |
| 371 | return UART_CFG_STOP_BITS_1; |
| 372 | /* Some MCU's don't support 1.5 stop bits */ |
| 373 | #ifdef LL_USART_STOPBITS_1_5 |
| 374 | case LL_USART_STOPBITS_1_5: |
| 375 | return UART_CFG_STOP_BITS_1_5; |
| 376 | #endif /* LL_USART_STOPBITS_1_5 */ |
| 377 | case LL_USART_STOPBITS_2: |
| 378 | default: |
| 379 | return UART_CFG_STOP_BITS_2; |
| 380 | } |
| 381 | } |
| 382 | |
Nicolas VINCENT | 573eec1 | 2021-03-10 13:23:00 +0100 | [diff] [blame] | 383 | static inline uint32_t uart_stm32_cfg2ll_databits(enum uart_config_data_bits db, |
| 384 | enum uart_config_parity p) |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 385 | { |
| 386 | switch (db) { |
Benoit Leforestier | 9fee67d | 2019-03-22 14:19:57 +0100 | [diff] [blame] | 387 | /* Some MCU's don't support 7B or 9B datawidth */ |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 388 | #ifdef LL_USART_DATAWIDTH_7B |
| 389 | case UART_CFG_DATA_BITS_7: |
Nicolas VINCENT | 573eec1 | 2021-03-10 13:23:00 +0100 | [diff] [blame] | 390 | if (p == UART_CFG_PARITY_NONE) { |
| 391 | return LL_USART_DATAWIDTH_7B; |
| 392 | } else { |
| 393 | return LL_USART_DATAWIDTH_8B; |
| 394 | } |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 395 | #endif /* LL_USART_DATAWIDTH_7B */ |
Benoit Leforestier | 9fee67d | 2019-03-22 14:19:57 +0100 | [diff] [blame] | 396 | #ifdef LL_USART_DATAWIDTH_9B |
| 397 | case UART_CFG_DATA_BITS_9: |
| 398 | return LL_USART_DATAWIDTH_9B; |
| 399 | #endif /* LL_USART_DATAWIDTH_9B */ |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 400 | case UART_CFG_DATA_BITS_8: |
| 401 | default: |
Nicolas VINCENT | 573eec1 | 2021-03-10 13:23:00 +0100 | [diff] [blame] | 402 | if (p == UART_CFG_PARITY_NONE) { |
| 403 | return LL_USART_DATAWIDTH_8B; |
| 404 | #ifdef LL_USART_DATAWIDTH_9B |
| 405 | } else { |
| 406 | return LL_USART_DATAWIDTH_9B; |
| 407 | #endif |
| 408 | } |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 409 | return LL_USART_DATAWIDTH_8B; |
| 410 | } |
| 411 | } |
| 412 | |
Nicolas VINCENT | 573eec1 | 2021-03-10 13:23:00 +0100 | [diff] [blame] | 413 | static inline enum uart_config_data_bits uart_stm32_ll2cfg_databits(uint32_t db, |
| 414 | uint32_t p) |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 415 | { |
| 416 | switch (db) { |
Benoit Leforestier | 9fee67d | 2019-03-22 14:19:57 +0100 | [diff] [blame] | 417 | /* Some MCU's don't support 7B or 9B datawidth */ |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 418 | #ifdef LL_USART_DATAWIDTH_7B |
| 419 | case LL_USART_DATAWIDTH_7B: |
Nicolas VINCENT | 573eec1 | 2021-03-10 13:23:00 +0100 | [diff] [blame] | 420 | if (p == LL_USART_PARITY_NONE) { |
| 421 | return UART_CFG_DATA_BITS_7; |
| 422 | } else { |
| 423 | return UART_CFG_DATA_BITS_6; |
| 424 | } |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 425 | #endif /* LL_USART_DATAWIDTH_7B */ |
Benoit Leforestier | 9fee67d | 2019-03-22 14:19:57 +0100 | [diff] [blame] | 426 | #ifdef LL_USART_DATAWIDTH_9B |
| 427 | case LL_USART_DATAWIDTH_9B: |
Nicolas VINCENT | 573eec1 | 2021-03-10 13:23:00 +0100 | [diff] [blame] | 428 | if (p == LL_USART_PARITY_NONE) { |
| 429 | return UART_CFG_DATA_BITS_9; |
| 430 | } else { |
| 431 | return UART_CFG_DATA_BITS_8; |
| 432 | } |
Benoit Leforestier | 9fee67d | 2019-03-22 14:19:57 +0100 | [diff] [blame] | 433 | #endif /* LL_USART_DATAWIDTH_9B */ |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 434 | case LL_USART_DATAWIDTH_8B: |
| 435 | default: |
Nicolas VINCENT | 573eec1 | 2021-03-10 13:23:00 +0100 | [diff] [blame] | 436 | if (p == LL_USART_PARITY_NONE) { |
| 437 | return UART_CFG_DATA_BITS_8; |
| 438 | } else { |
| 439 | return UART_CFG_DATA_BITS_7; |
| 440 | } |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
Georgij Cernysiov | 78eed34 | 2019-03-15 19:51:09 +0100 | [diff] [blame] | 444 | /** |
| 445 | * @brief Get LL hardware flow control define from |
| 446 | * Zephyr hardware flow control option. |
Armin Brauns | b95cdb2 | 2023-05-22 14:29:23 +0000 | [diff] [blame] | 447 | * @note Supports only UART_CFG_FLOW_CTRL_RTS_CTS and UART_CFG_FLOW_CTRL_RS485. |
Georgij Cernysiov | 78eed34 | 2019-03-15 19:51:09 +0100 | [diff] [blame] | 448 | * @param fc: Zephyr hardware flow control option. |
| 449 | * @retval LL_USART_HWCONTROL_RTS_CTS, or LL_USART_HWCONTROL_NONE. |
| 450 | */ |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 451 | 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] | 452 | { |
| 453 | if (fc == UART_CFG_FLOW_CTRL_RTS_CTS) { |
| 454 | return LL_USART_HWCONTROL_RTS_CTS; |
Armin Brauns | b95cdb2 | 2023-05-22 14:29:23 +0000 | [diff] [blame] | 455 | } else if (fc == UART_CFG_FLOW_CTRL_RS485) { |
| 456 | /* Driver Enable is handled separately */ |
| 457 | return LL_USART_HWCONTROL_NONE; |
Georgij Cernysiov | 78eed34 | 2019-03-15 19:51:09 +0100 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | return LL_USART_HWCONTROL_NONE; |
| 461 | } |
| 462 | |
| 463 | /** |
Yannis Damigos | a4b448e | 2020-02-06 20:36:03 +0200 | [diff] [blame] | 464 | * @brief Get Zephyr hardware flow control option from |
Georgij Cernysiov | 78eed34 | 2019-03-15 19:51:09 +0100 | [diff] [blame] | 465 | * LL hardware flow control define. |
| 466 | * @note Supports only LL_USART_HWCONTROL_RTS_CTS. |
Yannis Damigos | a4b448e | 2020-02-06 20:36:03 +0200 | [diff] [blame] | 467 | * @param fc: LL hardware flow control definition. |
Kumar Gala | 6115346 | 2020-02-06 11:30:05 -0600 | [diff] [blame] | 468 | * @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] | 469 | */ |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 470 | 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] | 471 | { |
| 472 | if (fc == LL_USART_HWCONTROL_RTS_CTS) { |
| 473 | return UART_CFG_FLOW_CTRL_RTS_CTS; |
| 474 | } |
| 475 | |
Kumar Gala | 6115346 | 2020-02-06 11:30:05 -0600 | [diff] [blame] | 476 | return UART_CFG_FLOW_CTRL_NONE; |
Georgij Cernysiov | 78eed34 | 2019-03-15 19:51:09 +0100 | [diff] [blame] | 477 | } |
| 478 | |
Kenneth J. Miller | 5217a17 | 2023-06-20 01:39:32 +0200 | [diff] [blame] | 479 | static void uart_stm32_parameters_set(const struct device *dev, |
| 480 | const struct uart_config *cfg) |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 481 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 482 | const struct uart_stm32_config *config = dev->config; |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 483 | struct uart_stm32_data *data = dev->data; |
Kenneth J. Miller | c8ffeb4 | 2023-06-20 01:16:42 +0200 | [diff] [blame] | 484 | struct uart_config *uart_cfg = data->uart_cfg; |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 485 | const uint32_t parity = uart_stm32_cfg2ll_parity(cfg->parity); |
Jeroen van Dooren | 250e1f9 | 2023-02-06 09:39:37 +0100 | [diff] [blame] | 486 | const uint32_t stopbits = uart_stm32_cfg2ll_stopbits(config, cfg->stop_bits); |
Nicolas VINCENT | 573eec1 | 2021-03-10 13:23:00 +0100 | [diff] [blame] | 487 | const uint32_t databits = uart_stm32_cfg2ll_databits(cfg->data_bits, |
| 488 | cfg->parity); |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 489 | const uint32_t flowctrl = uart_stm32_cfg2ll_hwctrl(cfg->flow_ctrl); |
Armin Brauns | b95cdb2 | 2023-05-22 14:29:23 +0000 | [diff] [blame] | 490 | #if HAS_DRIVER_ENABLE |
| 491 | bool driver_enable = cfg->flow_ctrl == UART_CFG_FLOW_CTRL_RS485; |
| 492 | #endif |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 493 | |
Kenneth J. Miller | 5217a17 | 2023-06-20 01:39:32 +0200 | [diff] [blame] | 494 | if (cfg == uart_cfg) { |
| 495 | /* Called via (re-)init function, so the SoC either just booted, |
| 496 | * or is returning from a low-power state where it lost register |
| 497 | * contents |
| 498 | */ |
| 499 | LL_USART_ConfigCharacter(config->usart, |
| 500 | databits, |
| 501 | parity, |
| 502 | stopbits); |
| 503 | uart_stm32_set_hwctrl(dev, flowctrl); |
| 504 | uart_stm32_set_baudrate(dev, cfg->baudrate); |
| 505 | } else { |
| 506 | /* Called from application/subsys via uart_configure syscall */ |
| 507 | if (parity != uart_stm32_get_parity(dev)) { |
| 508 | uart_stm32_set_parity(dev, parity); |
| 509 | } |
| 510 | |
| 511 | if (stopbits != uart_stm32_get_stopbits(dev)) { |
| 512 | uart_stm32_set_stopbits(dev, stopbits); |
| 513 | } |
| 514 | |
| 515 | if (databits != uart_stm32_get_databits(dev)) { |
| 516 | uart_stm32_set_databits(dev, databits); |
| 517 | } |
| 518 | |
| 519 | if (flowctrl != uart_stm32_get_hwctrl(dev)) { |
| 520 | uart_stm32_set_hwctrl(dev, flowctrl); |
| 521 | } |
| 522 | |
| 523 | #if HAS_DRIVER_ENABLE |
| 524 | if (driver_enable != uart_stm32_get_driver_enable(dev)) { |
| 525 | uart_stm32_set_driver_enable(dev, driver_enable); |
| 526 | } |
| 527 | #endif |
| 528 | |
| 529 | if (cfg->baudrate != uart_cfg->baudrate) { |
| 530 | uart_stm32_set_baudrate(dev, cfg->baudrate); |
| 531 | uart_cfg->baudrate = cfg->baudrate; |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | #ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE |
| 537 | static int uart_stm32_configure(const struct device *dev, |
| 538 | const struct uart_config *cfg) |
| 539 | { |
| 540 | const struct uart_stm32_config *config = dev->config; |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 541 | USART_TypeDef *usart = config->usart; |
Kenneth J. Miller | 5217a17 | 2023-06-20 01:39:32 +0200 | [diff] [blame] | 542 | struct uart_stm32_data *data = dev->data; |
| 543 | struct uart_config *uart_cfg = data->uart_cfg; |
| 544 | const uint32_t parity = uart_stm32_cfg2ll_parity(cfg->parity); |
| 545 | const uint32_t stopbits = uart_stm32_cfg2ll_stopbits(config, cfg->stop_bits); |
| 546 | const uint32_t databits = uart_stm32_cfg2ll_databits(cfg->data_bits, |
| 547 | cfg->parity); |
| 548 | |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 549 | /* Hardware doesn't support mark or space parity */ |
Erwan Gouriou | 5e56170 | 2021-02-11 09:55:38 +0100 | [diff] [blame] | 550 | if ((cfg->parity == UART_CFG_PARITY_MARK) || |
| 551 | (cfg->parity == UART_CFG_PARITY_SPACE)) { |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 552 | return -ENOTSUP; |
| 553 | } |
| 554 | |
Nicolas VINCENT | 573eec1 | 2021-03-10 13:23:00 +0100 | [diff] [blame] | 555 | /* Driver does not supports parity + 9 databits */ |
| 556 | if ((cfg->parity != UART_CFG_PARITY_NONE) && |
| 557 | (cfg->data_bits == UART_CFG_DATA_BITS_9)) { |
| 558 | return -ENOTSUP; |
| 559 | } |
| 560 | |
Jeroen van Dooren | 250e1f9 | 2023-02-06 09:39:37 +0100 | [diff] [blame] | 561 | /* When the transformed ll stop bits don't match with what was requested, then it's not |
| 562 | * supported |
| 563 | */ |
| 564 | if (uart_stm32_ll2cfg_stopbits(stopbits) != cfg->stop_bits) { |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 565 | return -ENOTSUP; |
| 566 | } |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 567 | |
Jeroen van Dooren | 250e1f9 | 2023-02-06 09:39:37 +0100 | [diff] [blame] | 568 | /* When the transformed ll databits don't match with what was requested, then it's not |
| 569 | * supported |
| 570 | */ |
| 571 | if (uart_stm32_ll2cfg_databits(databits, parity) != cfg->data_bits) { |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 572 | return -ENOTSUP; |
| 573 | } |
| 574 | |
Armin Brauns | b95cdb2 | 2023-05-22 14:29:23 +0000 | [diff] [blame] | 575 | /* Driver supports only RTS/CTS and RS485 flow control */ |
| 576 | if (!(cfg->flow_ctrl == UART_CFG_FLOW_CTRL_NONE |
| 577 | || (cfg->flow_ctrl == UART_CFG_FLOW_CTRL_RTS_CTS && |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 578 | IS_UART_HWFLOW_INSTANCE(usart)) |
Armin Brauns | b95cdb2 | 2023-05-22 14:29:23 +0000 | [diff] [blame] | 579 | #if HAS_DRIVER_ENABLE |
| 580 | || (cfg->flow_ctrl == UART_CFG_FLOW_CTRL_RS485 && |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 581 | IS_UART_DRIVER_ENABLE_INSTANCE(usart)) |
Armin Brauns | b95cdb2 | 2023-05-22 14:29:23 +0000 | [diff] [blame] | 582 | #endif |
| 583 | )) { |
| 584 | return -ENOTSUP; |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 585 | } |
| 586 | |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 587 | LL_USART_Disable(usart); |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 588 | |
Pisit Sawangvonganan | b4567fa | 2024-01-13 00:34:18 +0700 | [diff] [blame] | 589 | /* Set basic parameters, such as data-/stop-bit, parity, and baudrate */ |
Kenneth J. Miller | 5217a17 | 2023-06-20 01:39:32 +0200 | [diff] [blame] | 590 | uart_stm32_parameters_set(dev, cfg); |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 591 | |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 592 | LL_USART_Enable(usart); |
Kenneth J. Miller | 5217a17 | 2023-06-20 01:39:32 +0200 | [diff] [blame] | 593 | |
| 594 | /* Upon successful configuration, persist the syscall-passed |
| 595 | * uart_config. |
| 596 | * This allows restoring it, should the device return from a low-power |
| 597 | * mode in which register contents are lost. |
| 598 | */ |
| 599 | *uart_cfg = *cfg; |
| 600 | |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 601 | return 0; |
| 602 | }; |
| 603 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 604 | static int uart_stm32_config_get(const struct device *dev, |
| 605 | struct uart_config *cfg) |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 606 | { |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 607 | struct uart_stm32_data *data = dev->data; |
Kenneth J. Miller | c8ffeb4 | 2023-06-20 01:16:42 +0200 | [diff] [blame] | 608 | struct uart_config *uart_cfg = data->uart_cfg; |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 609 | |
Kenneth J. Miller | c8ffeb4 | 2023-06-20 01:16:42 +0200 | [diff] [blame] | 610 | cfg->baudrate = uart_cfg->baudrate; |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 611 | cfg->parity = uart_stm32_ll2cfg_parity(uart_stm32_get_parity(dev)); |
| 612 | cfg->stop_bits = uart_stm32_ll2cfg_stopbits( |
| 613 | uart_stm32_get_stopbits(dev)); |
| 614 | cfg->data_bits = uart_stm32_ll2cfg_databits( |
Nicolas VINCENT | 573eec1 | 2021-03-10 13:23:00 +0100 | [diff] [blame] | 615 | uart_stm32_get_databits(dev), uart_stm32_get_parity(dev)); |
Georgij Cernysiov | 78eed34 | 2019-03-15 19:51:09 +0100 | [diff] [blame] | 616 | cfg->flow_ctrl = uart_stm32_ll2cfg_hwctrl( |
| 617 | uart_stm32_get_hwctrl(dev)); |
Armin Brauns | b95cdb2 | 2023-05-22 14:29:23 +0000 | [diff] [blame] | 618 | #if HAS_DRIVER_ENABLE |
| 619 | if (uart_stm32_get_driver_enable(dev)) { |
| 620 | cfg->flow_ctrl = UART_CFG_FLOW_CTRL_RS485; |
| 621 | } |
| 622 | #endif |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 623 | return 0; |
| 624 | } |
Daniel Leung | 4e1692f | 2021-05-26 12:33:37 -0700 | [diff] [blame] | 625 | #endif /* CONFIG_UART_USE_RUNTIME_CONFIGURE */ |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 626 | |
Jeroen van Dooren | 435d5d1 | 2023-02-06 09:49:27 +0100 | [diff] [blame] | 627 | typedef void (*poll_in_fn)( |
Pisit Sawangvonganan | f503b7a | 2024-05-24 13:17:38 +0700 | [diff] [blame] | 628 | USART_TypeDef *usart, |
Jeroen van Dooren | 435d5d1 | 2023-02-06 09:49:27 +0100 | [diff] [blame] | 629 | void *in); |
| 630 | |
| 631 | static int uart_stm32_poll_in_visitor(const struct device *dev, void *in, poll_in_fn get_fn) |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 632 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 633 | const struct uart_stm32_config *config = dev->config; |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 634 | USART_TypeDef *usart = config->usart; |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 635 | |
Kiril Zyapkov | 7a602fc | 2018-11-02 15:34:56 +0200 | [diff] [blame] | 636 | /* Clear overrun error flag */ |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 637 | if (LL_USART_IsActiveFlag_ORE(usart)) { |
| 638 | LL_USART_ClearFlag_ORE(usart); |
Kiril Zyapkov | 7a602fc | 2018-11-02 15:34:56 +0200 | [diff] [blame] | 639 | } |
| 640 | |
Francois Ramu | 860bd87 | 2022-01-28 16:52:28 +0100 | [diff] [blame] | 641 | /* |
| 642 | * On stm32 F4X, F1X, and F2X, the RXNE flag is affected (cleared) by |
| 643 | * the uart_err_check function call (on errors flags clearing) |
| 644 | */ |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 645 | if (!LL_USART_IsActiveFlag_RXNE(usart)) { |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 646 | return -1; |
| 647 | } |
Erwan Gouriou | da210ba | 2017-09-21 15:20:53 +0200 | [diff] [blame] | 648 | |
Pisit Sawangvonganan | f503b7a | 2024-05-24 13:17:38 +0700 | [diff] [blame] | 649 | get_fn(usart, in); |
Erwan Gouriou | da210ba | 2017-09-21 15:20:53 +0200 | [diff] [blame] | 650 | |
| 651 | return 0; |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 652 | } |
| 653 | |
Jeroen van Dooren | 435d5d1 | 2023-02-06 09:49:27 +0100 | [diff] [blame] | 654 | typedef void (*poll_out_fn)( |
Pisit Sawangvonganan | 3a6e36d | 2024-11-10 00:03:03 +0700 | [diff] [blame] | 655 | USART_TypeDef *usart, uint16_t out); |
Jeroen van Dooren | 435d5d1 | 2023-02-06 09:49:27 +0100 | [diff] [blame] | 656 | |
Pisit Sawangvonganan | 3a6e36d | 2024-11-10 00:03:03 +0700 | [diff] [blame] | 657 | static void uart_stm32_poll_out_visitor(const struct device *dev, uint16_t out, poll_out_fn set_fn) |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 658 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 659 | const struct uart_stm32_config *config = dev->config; |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 660 | USART_TypeDef *usart = config->usart; |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 661 | #ifdef CONFIG_PM |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 662 | struct uart_stm32_data *data = dev->data; |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 663 | #endif |
Johann Fischer | 5e5ea9a | 2022-07-13 16:50:25 +0200 | [diff] [blame] | 664 | unsigned int key; |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 665 | |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 666 | /* Wait for TXE flag to be raised |
| 667 | * When TXE flag is raised, we lock interrupts to prevent interrupts (notably that of usart) |
| 668 | * or thread switch. Then, we can safely send our character. The character sent will be |
| 669 | * interlaced with the characters potentially send with interrupt transmission API |
| 670 | */ |
Julien D'ascenzio | d42cef1 | 2021-11-08 12:47:25 +0100 | [diff] [blame] | 671 | while (1) { |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 672 | if (LL_USART_IsActiveFlag_TXE(usart)) { |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 673 | key = irq_lock(); |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 674 | if (LL_USART_IsActiveFlag_TXE(usart)) { |
Julien D'ascenzio | d42cef1 | 2021-11-08 12:47:25 +0100 | [diff] [blame] | 675 | break; |
| 676 | } |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 677 | irq_unlock(key); |
| 678 | } |
Anas Nashif | 4c32258 | 2019-06-04 10:52:23 -0400 | [diff] [blame] | 679 | } |
Erwan Gouriou | da210ba | 2017-09-21 15:20:53 +0200 | [diff] [blame] | 680 | |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 681 | #ifdef CONFIG_PM |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 682 | |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 683 | /* If an interrupt transmission is in progress, the pm constraint is already managed by the |
| 684 | * call of uart_stm32_irq_tx_[en|dis]able |
| 685 | */ |
| 686 | if (!data->tx_poll_stream_on && !data->tx_int_stream_on) { |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 687 | data->tx_poll_stream_on = true; |
| 688 | |
| 689 | /* Don't allow system to suspend until stream |
| 690 | * transmission has completed |
| 691 | */ |
Gerard Marull-Paretas | 5a71eeb | 2022-01-20 16:06:28 +0100 | [diff] [blame] | 692 | uart_stm32_pm_policy_state_lock_get(dev); |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 693 | |
| 694 | /* Enable TC interrupt so we can release suspend |
| 695 | * constraint when done |
| 696 | */ |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 697 | LL_USART_EnableIT_TC(usart); |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 698 | } |
| 699 | #endif /* CONFIG_PM */ |
Gerard Marull-Paretas | e3f4907 | 2021-09-09 22:41:35 +0200 | [diff] [blame] | 700 | |
Pisit Sawangvonganan | f503b7a | 2024-05-24 13:17:38 +0700 | [diff] [blame] | 701 | set_fn(usart, out); |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 702 | irq_unlock(key); |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 703 | } |
| 704 | |
Pisit Sawangvonganan | f503b7a | 2024-05-24 13:17:38 +0700 | [diff] [blame] | 705 | static void poll_in_u8(USART_TypeDef *usart, void *in) |
Jeroen van Dooren | 435d5d1 | 2023-02-06 09:49:27 +0100 | [diff] [blame] | 706 | { |
Pisit Sawangvonganan | f503b7a | 2024-05-24 13:17:38 +0700 | [diff] [blame] | 707 | *((unsigned char *)in) = (unsigned char)LL_USART_ReceiveData8(usart); |
Jeroen van Dooren | 435d5d1 | 2023-02-06 09:49:27 +0100 | [diff] [blame] | 708 | } |
| 709 | |
Pisit Sawangvonganan | 3a6e36d | 2024-11-10 00:03:03 +0700 | [diff] [blame] | 710 | static void poll_out_u8(USART_TypeDef *usart, uint16_t out) |
Jeroen van Dooren | 435d5d1 | 2023-02-06 09:49:27 +0100 | [diff] [blame] | 711 | { |
Pisit Sawangvonganan | 3a6e36d | 2024-11-10 00:03:03 +0700 | [diff] [blame] | 712 | LL_USART_TransmitData8(usart, (uint8_t)out); |
Jeroen van Dooren | 435d5d1 | 2023-02-06 09:49:27 +0100 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | static int uart_stm32_poll_in(const struct device *dev, unsigned char *c) |
| 716 | { |
Jeroen van Dooren | 6ab7069 | 2023-02-06 09:54:20 +0100 | [diff] [blame] | 717 | return uart_stm32_poll_in_visitor(dev, (void *)c, poll_in_u8); |
Jeroen van Dooren | 435d5d1 | 2023-02-06 09:49:27 +0100 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | static void uart_stm32_poll_out(const struct device *dev, unsigned char c) |
| 721 | { |
Pisit Sawangvonganan | 3a6e36d | 2024-11-10 00:03:03 +0700 | [diff] [blame] | 722 | uart_stm32_poll_out_visitor(dev, c, poll_out_u8); |
Jeroen van Dooren | 435d5d1 | 2023-02-06 09:49:27 +0100 | [diff] [blame] | 723 | } |
| 724 | |
Jeroen van Dooren | 6ab7069 | 2023-02-06 09:54:20 +0100 | [diff] [blame] | 725 | #ifdef CONFIG_UART_WIDE_DATA |
| 726 | |
Pisit Sawangvonganan | 3a6e36d | 2024-11-10 00:03:03 +0700 | [diff] [blame] | 727 | static void poll_out_u9(USART_TypeDef *usart, uint16_t out) |
Jeroen van Dooren | 6ab7069 | 2023-02-06 09:54:20 +0100 | [diff] [blame] | 728 | { |
Pisit Sawangvonganan | 3a6e36d | 2024-11-10 00:03:03 +0700 | [diff] [blame] | 729 | LL_USART_TransmitData9(usart, out); |
Jeroen van Dooren | 6ab7069 | 2023-02-06 09:54:20 +0100 | [diff] [blame] | 730 | } |
| 731 | |
Pisit Sawangvonganan | f503b7a | 2024-05-24 13:17:38 +0700 | [diff] [blame] | 732 | static void poll_in_u9(USART_TypeDef *usart, void *in) |
Jeroen van Dooren | 6ab7069 | 2023-02-06 09:54:20 +0100 | [diff] [blame] | 733 | { |
Pisit Sawangvonganan | f503b7a | 2024-05-24 13:17:38 +0700 | [diff] [blame] | 734 | *((uint16_t *)in) = LL_USART_ReceiveData9(usart); |
Jeroen van Dooren | 6ab7069 | 2023-02-06 09:54:20 +0100 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | static int uart_stm32_poll_in_u16(const struct device *dev, uint16_t *in_u16) |
| 738 | { |
| 739 | return uart_stm32_poll_in_visitor(dev, (void *)in_u16, poll_in_u9); |
| 740 | } |
| 741 | |
| 742 | static void uart_stm32_poll_out_u16(const struct device *dev, uint16_t out_u16) |
| 743 | { |
Pisit Sawangvonganan | 3a6e36d | 2024-11-10 00:03:03 +0700 | [diff] [blame] | 744 | uart_stm32_poll_out_visitor(dev, out_u16, poll_out_u9); |
Jeroen van Dooren | 6ab7069 | 2023-02-06 09:54:20 +0100 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | #endif |
| 748 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 749 | static int uart_stm32_err_check(const struct device *dev) |
Georgij Cernysiov | c74c131 | 2019-02-14 10:50:19 +0100 | [diff] [blame] | 750 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 751 | const struct uart_stm32_config *config = dev->config; |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 752 | USART_TypeDef *usart = config->usart; |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 753 | uint32_t err = 0U; |
Georgij Cernysiov | c74c131 | 2019-02-14 10:50:19 +0100 | [diff] [blame] | 754 | |
Francois Ramu | 860bd87 | 2022-01-28 16:52:28 +0100 | [diff] [blame] | 755 | /* Check for errors, then clear them. |
Georgij Cernysiov | c74c131 | 2019-02-14 10:50:19 +0100 | [diff] [blame] | 756 | * Some SoC clear all error flags when at least |
Francois Ramu | 860bd87 | 2022-01-28 16:52:28 +0100 | [diff] [blame] | 757 | * one is cleared. (e.g. F4X, F1X, and F2X). |
| 758 | * The stm32 F4X, F1X, and F2X also reads the usart DR when clearing Errors |
Georgij Cernysiov | c74c131 | 2019-02-14 10:50:19 +0100 | [diff] [blame] | 759 | */ |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 760 | if (LL_USART_IsActiveFlag_ORE(usart)) { |
Georgij Cernysiov | c74c131 | 2019-02-14 10:50:19 +0100 | [diff] [blame] | 761 | err |= UART_ERROR_OVERRUN; |
| 762 | } |
| 763 | |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 764 | if (LL_USART_IsActiveFlag_PE(usart)) { |
Georgij Cernysiov | c74c131 | 2019-02-14 10:50:19 +0100 | [diff] [blame] | 765 | err |= UART_ERROR_PARITY; |
| 766 | } |
| 767 | |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 768 | if (LL_USART_IsActiveFlag_FE(usart)) { |
Georgij Cernysiov | c74c131 | 2019-02-14 10:50:19 +0100 | [diff] [blame] | 769 | err |= UART_ERROR_FRAMING; |
| 770 | } |
| 771 | |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 772 | if (LL_USART_IsActiveFlag_NE(usart)) { |
Dawid Niedzwiecki | 4a2a512 | 2023-01-03 09:38:48 +0100 | [diff] [blame] | 773 | err |= UART_ERROR_NOISE; |
| 774 | } |
| 775 | |
Andrea Campanella | b2190fd | 2021-12-21 16:09:15 +0000 | [diff] [blame] | 776 | #if !defined(CONFIG_SOC_SERIES_STM32F0X) || defined(USART_LIN_SUPPORT) |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 777 | if (LL_USART_IsActiveFlag_LBD(usart)) { |
Andrea Campanella | b2190fd | 2021-12-21 16:09:15 +0000 | [diff] [blame] | 778 | err |= UART_BREAK; |
| 779 | } |
| 780 | |
| 781 | if (err & UART_BREAK) { |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 782 | LL_USART_ClearFlag_LBD(usart); |
Andrea Campanella | b2190fd | 2021-12-21 16:09:15 +0000 | [diff] [blame] | 783 | } |
| 784 | #endif |
Francois Ramu | 860bd87 | 2022-01-28 16:52:28 +0100 | [diff] [blame] | 785 | /* Clearing error : |
| 786 | * the stm32 F4X, F1X, and F2X sw sequence is reading the usart SR |
| 787 | * then the usart DR to clear the Error flags ORE, PE, FE, NE |
| 788 | * --> so is the RXNE flag also cleared ! |
| 789 | */ |
Georgij Cernysiov | c74c131 | 2019-02-14 10:50:19 +0100 | [diff] [blame] | 790 | if (err & UART_ERROR_OVERRUN) { |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 791 | LL_USART_ClearFlag_ORE(usart); |
Georgij Cernysiov | c74c131 | 2019-02-14 10:50:19 +0100 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | if (err & UART_ERROR_PARITY) { |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 795 | LL_USART_ClearFlag_PE(usart); |
Georgij Cernysiov | c74c131 | 2019-02-14 10:50:19 +0100 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | if (err & UART_ERROR_FRAMING) { |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 799 | LL_USART_ClearFlag_FE(usart); |
Georgij Cernysiov | c74c131 | 2019-02-14 10:50:19 +0100 | [diff] [blame] | 800 | } |
Dawid Niedzwiecki | 4a2a512 | 2023-01-03 09:38:48 +0100 | [diff] [blame] | 801 | |
| 802 | if (err & UART_ERROR_NOISE) { |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 803 | LL_USART_ClearFlag_NE(usart); |
Dawid Niedzwiecki | 4a2a512 | 2023-01-03 09:38:48 +0100 | [diff] [blame] | 804 | } |
Georgij Cernysiov | c74c131 | 2019-02-14 10:50:19 +0100 | [diff] [blame] | 805 | |
| 806 | return err; |
| 807 | } |
| 808 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 809 | static inline void __uart_stm32_get_clock(const struct device *dev) |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 810 | { |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 811 | struct uart_stm32_data *data = dev->data; |
Gerard Marull-Paretas | a202341 | 2022-08-22 10:36:10 +0200 | [diff] [blame] | 812 | const struct device *const clk = DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE); |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 813 | |
Erwan Gouriou | 8c079e9 | 2016-11-14 11:53:52 +0100 | [diff] [blame] | 814 | data->clock = clk; |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 815 | } |
| 816 | |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 817 | #ifdef CONFIG_UART_INTERRUPT_DRIVEN |
| 818 | |
Zheng Wu | f59e247 | 2024-10-30 19:34:45 +0800 | [diff] [blame] | 819 | typedef void (*fifo_fill_fn)(USART_TypeDef *usart, const void *tx_data, const int offset); |
Jeroen van Dooren | 435d5d1 | 2023-02-06 09:49:27 +0100 | [diff] [blame] | 820 | |
| 821 | static int uart_stm32_fifo_fill_visitor(const struct device *dev, const void *tx_data, int size, |
| 822 | fifo_fill_fn fill_fn) |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 823 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 824 | const struct uart_stm32_config *config = dev->config; |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 825 | USART_TypeDef *usart = config->usart; |
Zheng Wu | f59e247 | 2024-10-30 19:34:45 +0800 | [diff] [blame] | 826 | int num_tx = 0U; |
Johann Fischer | 5e5ea9a | 2022-07-13 16:50:25 +0200 | [diff] [blame] | 827 | unsigned int key; |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 828 | |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 829 | if (!LL_USART_IsActiveFlag_TXE(usart)) { |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 830 | return num_tx; |
| 831 | } |
| 832 | |
| 833 | /* Lock interrupts to prevent nested interrupts or thread switch */ |
| 834 | key = irq_lock(); |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 835 | |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 836 | while ((size - num_tx > 0) && LL_USART_IsActiveFlag_TXE(usart)) { |
Georgij Cernysiov | 3de55da | 2019-02-06 23:31:24 +0100 | [diff] [blame] | 837 | /* TXE flag will be cleared with byte write to DR|RDR register */ |
Erwan Gouriou | 8c079e9 | 2016-11-14 11:53:52 +0100 | [diff] [blame] | 838 | |
Jeroen van Dooren | 435d5d1 | 2023-02-06 09:49:27 +0100 | [diff] [blame] | 839 | /* Send a character */ |
Pisit Sawangvonganan | f503b7a | 2024-05-24 13:17:38 +0700 | [diff] [blame] | 840 | fill_fn(usart, tx_data, num_tx); |
Jeroen van Dooren | 435d5d1 | 2023-02-06 09:49:27 +0100 | [diff] [blame] | 841 | num_tx++; |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 842 | } |
| 843 | |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 844 | irq_unlock(key); |
| 845 | |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 846 | return num_tx; |
| 847 | } |
| 848 | |
Zheng Wu | f59e247 | 2024-10-30 19:34:45 +0800 | [diff] [blame] | 849 | static void fifo_fill_with_u8(USART_TypeDef *usart, const void *tx_data, const int offset) |
Jeroen van Dooren | 435d5d1 | 2023-02-06 09:49:27 +0100 | [diff] [blame] | 850 | { |
| 851 | const uint8_t *data = (const uint8_t *)tx_data; |
| 852 | /* Send a character (8bit) */ |
Pisit Sawangvonganan | f503b7a | 2024-05-24 13:17:38 +0700 | [diff] [blame] | 853 | LL_USART_TransmitData8(usart, data[offset]); |
Jeroen van Dooren | 435d5d1 | 2023-02-06 09:49:27 +0100 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | static int uart_stm32_fifo_fill(const struct device *dev, const uint8_t *tx_data, int size) |
| 857 | { |
| 858 | if (uart_stm32_ll2cfg_databits(uart_stm32_get_databits(dev), uart_stm32_get_parity(dev)) == |
| 859 | UART_CFG_DATA_BITS_9) { |
| 860 | return -ENOTSUP; |
| 861 | } |
| 862 | return uart_stm32_fifo_fill_visitor(dev, (const void *)tx_data, size, |
| 863 | fifo_fill_with_u8); |
| 864 | } |
| 865 | |
Zheng Wu | f59e247 | 2024-10-30 19:34:45 +0800 | [diff] [blame] | 866 | typedef void (*fifo_read_fn)(USART_TypeDef *usart, void *rx_data, const int offset); |
Jeroen van Dooren | 435d5d1 | 2023-02-06 09:49:27 +0100 | [diff] [blame] | 867 | |
| 868 | static int uart_stm32_fifo_read_visitor(const struct device *dev, void *rx_data, const int size, |
| 869 | fifo_read_fn read_fn) |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 870 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 871 | const struct uart_stm32_config *config = dev->config; |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 872 | USART_TypeDef *usart = config->usart; |
Zheng Wu | f59e247 | 2024-10-30 19:34:45 +0800 | [diff] [blame] | 873 | int num_rx = 0U; |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 874 | |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 875 | while ((size - num_rx > 0) && LL_USART_IsActiveFlag_RXNE(usart)) { |
Georgij Cernysiov | 3de55da | 2019-02-06 23:31:24 +0100 | [diff] [blame] | 876 | /* RXNE flag will be cleared upon read from DR|RDR register */ |
Erwan Gouriou | 8c079e9 | 2016-11-14 11:53:52 +0100 | [diff] [blame] | 877 | |
Pisit Sawangvonganan | f503b7a | 2024-05-24 13:17:38 +0700 | [diff] [blame] | 878 | read_fn(usart, rx_data, num_rx); |
Jeroen van Dooren | 435d5d1 | 2023-02-06 09:49:27 +0100 | [diff] [blame] | 879 | num_rx++; |
Kiril Zyapkov | 7a602fc | 2018-11-02 15:34:56 +0200 | [diff] [blame] | 880 | |
| 881 | /* Clear overrun error flag */ |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 882 | if (LL_USART_IsActiveFlag_ORE(usart)) { |
| 883 | LL_USART_ClearFlag_ORE(usart); |
Jeroen van Dooren | 435d5d1 | 2023-02-06 09:49:27 +0100 | [diff] [blame] | 884 | /* |
| 885 | * On stm32 F4X, F1X, and F2X, the RXNE flag is affected (cleared) by |
| 886 | * the uart_err_check function call (on errors flags clearing) |
| 887 | */ |
Kiril Zyapkov | 7a602fc | 2018-11-02 15:34:56 +0200 | [diff] [blame] | 888 | } |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 889 | } |
Georgij Cernysiov | 3de55da | 2019-02-06 23:31:24 +0100 | [diff] [blame] | 890 | |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 891 | return num_rx; |
| 892 | } |
| 893 | |
Zheng Wu | f59e247 | 2024-10-30 19:34:45 +0800 | [diff] [blame] | 894 | static void fifo_read_with_u8(USART_TypeDef *usart, void *rx_data, const int offset) |
Jeroen van Dooren | 435d5d1 | 2023-02-06 09:49:27 +0100 | [diff] [blame] | 895 | { |
| 896 | uint8_t *data = (uint8_t *)rx_data; |
| 897 | |
Pisit Sawangvonganan | f503b7a | 2024-05-24 13:17:38 +0700 | [diff] [blame] | 898 | data[offset] = LL_USART_ReceiveData8(usart); |
Jeroen van Dooren | 435d5d1 | 2023-02-06 09:49:27 +0100 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | static int uart_stm32_fifo_read(const struct device *dev, uint8_t *rx_data, const int size) |
| 902 | { |
| 903 | if (uart_stm32_ll2cfg_databits(uart_stm32_get_databits(dev), uart_stm32_get_parity(dev)) == |
| 904 | UART_CFG_DATA_BITS_9) { |
| 905 | return -ENOTSUP; |
| 906 | } |
| 907 | return uart_stm32_fifo_read_visitor(dev, (void *)rx_data, size, |
| 908 | fifo_read_with_u8); |
| 909 | } |
| 910 | |
Jeroen van Dooren | 6ab7069 | 2023-02-06 09:54:20 +0100 | [diff] [blame] | 911 | #ifdef CONFIG_UART_WIDE_DATA |
| 912 | |
Zheng Wu | f59e247 | 2024-10-30 19:34:45 +0800 | [diff] [blame] | 913 | static void fifo_fill_with_u16(USART_TypeDef *usart, const void *tx_data, const int offset) |
Jeroen van Dooren | 6ab7069 | 2023-02-06 09:54:20 +0100 | [diff] [blame] | 914 | { |
| 915 | const uint16_t *data = (const uint16_t *)tx_data; |
| 916 | |
| 917 | /* Send a character (9bit) */ |
Pisit Sawangvonganan | f503b7a | 2024-05-24 13:17:38 +0700 | [diff] [blame] | 918 | LL_USART_TransmitData9(usart, data[offset]); |
Jeroen van Dooren | 6ab7069 | 2023-02-06 09:54:20 +0100 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | static int uart_stm32_fifo_fill_u16(const struct device *dev, const uint16_t *tx_data, int size) |
| 922 | { |
| 923 | if (uart_stm32_ll2cfg_databits(uart_stm32_get_databits(dev), uart_stm32_get_parity(dev)) != |
| 924 | UART_CFG_DATA_BITS_9) { |
| 925 | return -ENOTSUP; |
| 926 | } |
| 927 | return uart_stm32_fifo_fill_visitor(dev, (const void *)tx_data, size, |
| 928 | fifo_fill_with_u16); |
| 929 | } |
| 930 | |
Zheng Wu | f59e247 | 2024-10-30 19:34:45 +0800 | [diff] [blame] | 931 | static void fifo_read_with_u16(USART_TypeDef *usart, void *rx_data, const int offset) |
Jeroen van Dooren | 6ab7069 | 2023-02-06 09:54:20 +0100 | [diff] [blame] | 932 | { |
| 933 | uint16_t *data = (uint16_t *)rx_data; |
| 934 | |
Pisit Sawangvonganan | f503b7a | 2024-05-24 13:17:38 +0700 | [diff] [blame] | 935 | data[offset] = LL_USART_ReceiveData9(usart); |
Jeroen van Dooren | 6ab7069 | 2023-02-06 09:54:20 +0100 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | static int uart_stm32_fifo_read_u16(const struct device *dev, uint16_t *rx_data, const int size) |
| 939 | { |
| 940 | if (uart_stm32_ll2cfg_databits(uart_stm32_get_databits(dev), uart_stm32_get_parity(dev)) != |
| 941 | UART_CFG_DATA_BITS_9) { |
| 942 | return -ENOTSUP; |
| 943 | } |
| 944 | return uart_stm32_fifo_read_visitor(dev, (void *)rx_data, size, |
| 945 | fifo_read_with_u16); |
| 946 | } |
| 947 | |
| 948 | #endif |
| 949 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 950 | static void uart_stm32_irq_tx_enable(const struct device *dev) |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 951 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 952 | const struct uart_stm32_config *config = dev->config; |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 953 | #ifdef CONFIG_PM |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 954 | struct uart_stm32_data *data = dev->data; |
Johann Fischer | 5e5ea9a | 2022-07-13 16:50:25 +0200 | [diff] [blame] | 955 | unsigned int key; |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 956 | #endif |
Julien D'ascenzio | d42cef1 | 2021-11-08 12:47:25 +0100 | [diff] [blame] | 957 | |
| 958 | #ifdef CONFIG_PM |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 959 | key = irq_lock(); |
Julien D'ascenzio | 7b21050 | 2021-10-26 18:03:31 +0200 | [diff] [blame] | 960 | data->tx_poll_stream_on = false; |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 961 | data->tx_int_stream_on = true; |
Gerard Marull-Paretas | 5a71eeb | 2022-01-20 16:06:28 +0100 | [diff] [blame] | 962 | uart_stm32_pm_policy_state_lock_get(dev); |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 963 | #endif |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 964 | LL_USART_EnableIT_TC(config->usart); |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 965 | |
| 966 | #ifdef CONFIG_PM |
| 967 | irq_unlock(key); |
| 968 | #endif |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 969 | } |
| 970 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 971 | static void uart_stm32_irq_tx_disable(const struct device *dev) |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 972 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 973 | const struct uart_stm32_config *config = dev->config; |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 974 | #ifdef CONFIG_PM |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 975 | struct uart_stm32_data *data = dev->data; |
Johann Fischer | 5e5ea9a | 2022-07-13 16:50:25 +0200 | [diff] [blame] | 976 | unsigned int key; |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 977 | |
| 978 | key = irq_lock(); |
| 979 | #endif |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 980 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 981 | LL_USART_DisableIT_TC(config->usart); |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 982 | |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 983 | #ifdef CONFIG_PM |
| 984 | data->tx_int_stream_on = false; |
Gerard Marull-Paretas | 5a71eeb | 2022-01-20 16:06:28 +0100 | [diff] [blame] | 985 | uart_stm32_pm_policy_state_lock_put(dev); |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 986 | #endif |
Julien D'ascenzio | d42cef1 | 2021-11-08 12:47:25 +0100 | [diff] [blame] | 987 | |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 988 | #ifdef CONFIG_PM |
Julien D'ascenzio | e4234ae | 2021-12-01 10:19:44 +0100 | [diff] [blame] | 989 | irq_unlock(key); |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 990 | #endif |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 991 | } |
| 992 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 993 | static int uart_stm32_irq_tx_ready(const struct device *dev) |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 994 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 995 | const struct uart_stm32_config *config = dev->config; |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 996 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 997 | return LL_USART_IsActiveFlag_TXE(config->usart) && |
| 998 | LL_USART_IsEnabledIT_TC(config->usart); |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 999 | } |
| 1000 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 1001 | static int uart_stm32_irq_tx_complete(const struct device *dev) |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1002 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1003 | const struct uart_stm32_config *config = dev->config; |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1004 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1005 | return LL_USART_IsActiveFlag_TC(config->usart); |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1006 | } |
| 1007 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 1008 | static void uart_stm32_irq_rx_enable(const struct device *dev) |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1009 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1010 | const struct uart_stm32_config *config = dev->config; |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1011 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1012 | LL_USART_EnableIT_RXNE(config->usart); |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1013 | } |
| 1014 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 1015 | static void uart_stm32_irq_rx_disable(const struct device *dev) |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1016 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1017 | const struct uart_stm32_config *config = dev->config; |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1018 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1019 | LL_USART_DisableIT_RXNE(config->usart); |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1020 | } |
| 1021 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 1022 | static int uart_stm32_irq_rx_ready(const struct device *dev) |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1023 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1024 | const struct uart_stm32_config *config = dev->config; |
Francois Ramu | 860bd87 | 2022-01-28 16:52:28 +0100 | [diff] [blame] | 1025 | /* |
| 1026 | * On stm32 F4X, F1X, and F2X, the RXNE flag is affected (cleared) by |
| 1027 | * the uart_err_check function call (on errors flags clearing) |
| 1028 | */ |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1029 | return LL_USART_IsActiveFlag_RXNE(config->usart); |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1030 | } |
| 1031 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 1032 | static void uart_stm32_irq_err_enable(const struct device *dev) |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1033 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1034 | const struct uart_stm32_config *config = dev->config; |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1035 | USART_TypeDef *usart = config->usart; |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1036 | |
Erwan Gouriou | 8c079e9 | 2016-11-14 11:53:52 +0100 | [diff] [blame] | 1037 | /* Enable FE, ORE interruptions */ |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1038 | LL_USART_EnableIT_ERROR(usart); |
Ilya Tagunov | 967c31b | 2018-03-29 19:40:00 +0300 | [diff] [blame] | 1039 | #if !defined(CONFIG_SOC_SERIES_STM32F0X) || defined(USART_LIN_SUPPORT) |
Erwan Gouriou | 8c079e9 | 2016-11-14 11:53:52 +0100 | [diff] [blame] | 1040 | /* Enable Line break detection */ |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1041 | if (IS_UART_LIN_INSTANCE(usart)) { |
| 1042 | LL_USART_EnableIT_LBD(usart); |
Ilya Tagunov | 967c31b | 2018-03-29 19:40:00 +0300 | [diff] [blame] | 1043 | } |
Maciej Debski | eaff37e | 2017-08-09 11:23:04 +0200 | [diff] [blame] | 1044 | #endif |
Erwan Gouriou | 8c079e9 | 2016-11-14 11:53:52 +0100 | [diff] [blame] | 1045 | /* Enable parity error interruption */ |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1046 | LL_USART_EnableIT_PE(usart); |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1047 | } |
| 1048 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 1049 | static void uart_stm32_irq_err_disable(const struct device *dev) |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1050 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1051 | const struct uart_stm32_config *config = dev->config; |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1052 | USART_TypeDef *usart = config->usart; |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1053 | |
Ilya Tagunov | 967c31b | 2018-03-29 19:40:00 +0300 | [diff] [blame] | 1054 | /* Disable FE, ORE interruptions */ |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1055 | LL_USART_DisableIT_ERROR(usart); |
Ilya Tagunov | 967c31b | 2018-03-29 19:40:00 +0300 | [diff] [blame] | 1056 | #if !defined(CONFIG_SOC_SERIES_STM32F0X) || defined(USART_LIN_SUPPORT) |
| 1057 | /* Disable Line break detection */ |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1058 | if (IS_UART_LIN_INSTANCE(usart)) { |
| 1059 | LL_USART_DisableIT_LBD(usart); |
Ilya Tagunov | 967c31b | 2018-03-29 19:40:00 +0300 | [diff] [blame] | 1060 | } |
Maciej Debski | eaff37e | 2017-08-09 11:23:04 +0200 | [diff] [blame] | 1061 | #endif |
Ilya Tagunov | 967c31b | 2018-03-29 19:40:00 +0300 | [diff] [blame] | 1062 | /* Disable parity error interruption */ |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1063 | LL_USART_DisableIT_PE(usart); |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1064 | } |
| 1065 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 1066 | static int uart_stm32_irq_is_pending(const struct device *dev) |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1067 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1068 | const struct uart_stm32_config *config = dev->config; |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1069 | USART_TypeDef *usart = config->usart; |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1070 | |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1071 | return ((LL_USART_IsActiveFlag_RXNE(usart) && |
| 1072 | LL_USART_IsEnabledIT_RXNE(usart)) || |
| 1073 | (LL_USART_IsActiveFlag_TC(usart) && |
| 1074 | LL_USART_IsEnabledIT_TC(usart))); |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1075 | } |
| 1076 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 1077 | static int uart_stm32_irq_update(const struct device *dev) |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1078 | { |
| 1079 | return 1; |
| 1080 | } |
| 1081 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 1082 | static void uart_stm32_irq_callback_set(const struct device *dev, |
Paul Sokolovsky | 57286af | 2018-07-16 21:12:26 +0300 | [diff] [blame] | 1083 | uart_irq_callback_user_data_t cb, |
| 1084 | void *cb_data) |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1085 | { |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 1086 | struct uart_stm32_data *data = dev->data; |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1087 | |
| 1088 | data->user_cb = cb; |
Paul Sokolovsky | 57286af | 2018-07-16 21:12:26 +0300 | [diff] [blame] | 1089 | data->user_data = cb_data; |
Daniel Leung | 9f02eea | 2022-08-18 11:19:46 -0700 | [diff] [blame] | 1090 | |
| 1091 | #if defined(CONFIG_UART_EXCLUSIVE_API_CALLBACKS) |
| 1092 | data->async_cb = NULL; |
| 1093 | data->async_user_data = NULL; |
| 1094 | #endif |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1095 | } |
| 1096 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1097 | #endif /* CONFIG_UART_INTERRUPT_DRIVEN */ |
| 1098 | |
| 1099 | #ifdef CONFIG_UART_ASYNC_API |
| 1100 | |
| 1101 | static inline void async_user_callback(struct uart_stm32_data *data, |
| 1102 | struct uart_event *event) |
| 1103 | { |
| 1104 | if (data->async_cb) { |
| 1105 | data->async_cb(data->uart_dev, event, data->async_user_data); |
| 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | static inline void async_evt_rx_rdy(struct uart_stm32_data *data) |
| 1110 | { |
| 1111 | LOG_DBG("rx_rdy: (%d %d)", data->dma_rx.offset, data->dma_rx.counter); |
| 1112 | |
| 1113 | struct uart_event event = { |
| 1114 | .type = UART_RX_RDY, |
| 1115 | .data.rx.buf = data->dma_rx.buffer, |
| 1116 | .data.rx.len = data->dma_rx.counter - data->dma_rx.offset, |
| 1117 | .data.rx.offset = data->dma_rx.offset |
| 1118 | }; |
| 1119 | |
Alexander Shuklin | 6831b8b | 2021-02-04 12:00:49 +0300 | [diff] [blame] | 1120 | /* update the current pos for new data */ |
| 1121 | data->dma_rx.offset = data->dma_rx.counter; |
| 1122 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1123 | /* send event only for new data */ |
| 1124 | if (event.data.rx.len > 0) { |
| 1125 | async_user_callback(data, &event); |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | static inline void async_evt_rx_err(struct uart_stm32_data *data, int err_code) |
| 1130 | { |
| 1131 | LOG_DBG("rx error: %d", err_code); |
| 1132 | |
| 1133 | struct uart_event event = { |
| 1134 | .type = UART_RX_STOPPED, |
| 1135 | .data.rx_stop.reason = err_code, |
| 1136 | .data.rx_stop.data.len = data->dma_rx.counter, |
| 1137 | .data.rx_stop.data.offset = 0, |
| 1138 | .data.rx_stop.data.buf = data->dma_rx.buffer |
| 1139 | }; |
| 1140 | |
| 1141 | async_user_callback(data, &event); |
| 1142 | } |
| 1143 | |
| 1144 | static inline void async_evt_tx_done(struct uart_stm32_data *data) |
| 1145 | { |
| 1146 | LOG_DBG("tx done: %d", data->dma_tx.counter); |
| 1147 | |
| 1148 | struct uart_event event = { |
| 1149 | .type = UART_TX_DONE, |
| 1150 | .data.tx.buf = data->dma_tx.buffer, |
| 1151 | .data.tx.len = data->dma_tx.counter |
| 1152 | }; |
| 1153 | |
| 1154 | /* Reset tx buffer */ |
| 1155 | data->dma_tx.buffer_length = 0; |
| 1156 | data->dma_tx.counter = 0; |
| 1157 | |
| 1158 | async_user_callback(data, &event); |
| 1159 | } |
| 1160 | |
| 1161 | static inline void async_evt_tx_abort(struct uart_stm32_data *data) |
| 1162 | { |
| 1163 | LOG_DBG("tx abort: %d", data->dma_tx.counter); |
| 1164 | |
| 1165 | struct uart_event event = { |
| 1166 | .type = UART_TX_ABORTED, |
| 1167 | .data.tx.buf = data->dma_tx.buffer, |
| 1168 | .data.tx.len = data->dma_tx.counter |
| 1169 | }; |
| 1170 | |
| 1171 | /* Reset tx buffer */ |
| 1172 | data->dma_tx.buffer_length = 0; |
| 1173 | data->dma_tx.counter = 0; |
| 1174 | |
| 1175 | async_user_callback(data, &event); |
| 1176 | } |
| 1177 | |
| 1178 | static inline void async_evt_rx_buf_request(struct uart_stm32_data *data) |
| 1179 | { |
| 1180 | struct uart_event evt = { |
| 1181 | .type = UART_RX_BUF_REQUEST, |
| 1182 | }; |
| 1183 | |
| 1184 | async_user_callback(data, &evt); |
| 1185 | } |
| 1186 | |
| 1187 | static inline void async_evt_rx_buf_release(struct uart_stm32_data *data) |
| 1188 | { |
| 1189 | struct uart_event evt = { |
| 1190 | .type = UART_RX_BUF_RELEASED, |
| 1191 | .data.rx_buf.buf = data->dma_rx.buffer, |
| 1192 | }; |
| 1193 | |
| 1194 | async_user_callback(data, &evt); |
| 1195 | } |
| 1196 | |
Francois Ramu | 962d6b1 | 2021-04-08 12:09:58 +0200 | [diff] [blame] | 1197 | static inline void async_timer_start(struct k_work_delayable *work, |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1198 | int32_t timeout) |
| 1199 | { |
Krzysztof Chruscinski | c590b35 | 2021-10-01 15:47:40 +0200 | [diff] [blame] | 1200 | if ((timeout != SYS_FOREVER_US) && (timeout != 0)) { |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1201 | /* start timer */ |
Krzysztof Chruscinski | c590b35 | 2021-10-01 15:47:40 +0200 | [diff] [blame] | 1202 | LOG_DBG("async timer started for %d us", timeout); |
| 1203 | k_work_reschedule(work, K_USEC(timeout)); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | static void uart_stm32_dma_rx_flush(const struct device *dev) |
| 1208 | { |
| 1209 | struct dma_status stat; |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 1210 | struct uart_stm32_data *data = dev->data; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1211 | |
Erwan Gouriou | 13c2351 | 2021-03-01 11:59:57 +0100 | [diff] [blame] | 1212 | if (dma_get_status(data->dma_rx.dma_dev, |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1213 | data->dma_rx.dma_channel, &stat) == 0) { |
| 1214 | size_t rx_rcv_len = data->dma_rx.buffer_length - |
| 1215 | stat.pending_length; |
| 1216 | if (rx_rcv_len > data->dma_rx.offset) { |
| 1217 | data->dma_rx.counter = rx_rcv_len; |
| 1218 | |
| 1219 | async_evt_rx_rdy(data); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1220 | } |
| 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | #endif /* CONFIG_UART_ASYNC_API */ |
| 1225 | |
Erwan Gouriou | 79ff645 | 2021-09-21 09:34:09 +0200 | [diff] [blame] | 1226 | #if defined(CONFIG_UART_INTERRUPT_DRIVEN) || \ |
| 1227 | defined(CONFIG_UART_ASYNC_API) || \ |
| 1228 | defined(CONFIG_PM) |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1229 | |
Tomasz Bursztyka | 4dcfb55 | 2020-06-17 14:58:56 +0200 | [diff] [blame] | 1230 | static void uart_stm32_isr(const struct device *dev) |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1231 | { |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 1232 | struct uart_stm32_data *data = dev->data; |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 1233 | #if defined(CONFIG_PM) || defined(CONFIG_UART_ASYNC_API) |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1234 | const struct uart_stm32_config *config = dev->config; |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1235 | USART_TypeDef *usart = config->usart; |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 1236 | #endif |
| 1237 | |
| 1238 | #ifdef CONFIG_PM |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1239 | if (LL_USART_IsEnabledIT_TC(usart) && |
| 1240 | LL_USART_IsActiveFlag_TC(usart)) { |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 1241 | |
| 1242 | if (data->tx_poll_stream_on) { |
Yegor Yefremov | 1155d46 | 2022-03-07 22:53:16 +0100 | [diff] [blame] | 1243 | /* A poll stream transmission just completed, |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 1244 | * allow system to suspend |
| 1245 | */ |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1246 | LL_USART_DisableIT_TC(usart); |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 1247 | data->tx_poll_stream_on = false; |
Gerard Marull-Paretas | 5a71eeb | 2022-01-20 16:06:28 +0100 | [diff] [blame] | 1248 | uart_stm32_pm_policy_state_lock_put(dev); |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 1249 | } |
Yegor Yefremov | 1155d46 | 2022-03-07 22:53:16 +0100 | [diff] [blame] | 1250 | /* Stream transmission was either async or IRQ based, |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 1251 | * constraint will be released at the same time TC IT |
| 1252 | * is disabled |
| 1253 | */ |
| 1254 | } |
| 1255 | #endif |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1256 | |
Shlomi Vaknin | 1b4f7e5 | 2021-05-17 23:06:51 +0300 | [diff] [blame] | 1257 | #ifdef CONFIG_UART_INTERRUPT_DRIVEN |
| 1258 | if (data->user_cb) { |
| 1259 | data->user_cb(dev, data->user_data); |
| 1260 | } |
| 1261 | #endif /* CONFIG_UART_INTERRUPT_DRIVEN */ |
| 1262 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1263 | #ifdef CONFIG_UART_ASYNC_API |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1264 | if (LL_USART_IsEnabledIT_IDLE(usart) && |
| 1265 | LL_USART_IsActiveFlag_IDLE(usart)) { |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1266 | |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1267 | LL_USART_ClearFlag_IDLE(usart); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1268 | |
| 1269 | LOG_DBG("idle interrupt occurred"); |
| 1270 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1271 | if (data->dma_rx.timeout == 0) { |
| 1272 | uart_stm32_dma_rx_flush(dev); |
Francois Ramu | 0300b34 | 2022-08-31 12:13:41 +0200 | [diff] [blame] | 1273 | } else { |
| 1274 | /* Start the RX timer not null */ |
| 1275 | async_timer_start(&data->dma_rx.timeout_work, |
| 1276 | data->dma_rx.timeout); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1277 | } |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1278 | } else if (LL_USART_IsEnabledIT_TC(usart) && |
| 1279 | LL_USART_IsActiveFlag_TC(usart)) { |
Zisis Adamos | 7235b09 | 2021-03-30 12:20:46 +0200 | [diff] [blame] | 1280 | |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1281 | LL_USART_DisableIT_TC(usart); |
Zisis Adamos | 7235b09 | 2021-03-30 12:20:46 +0200 | [diff] [blame] | 1282 | /* Generate TX_DONE event when transmission is done */ |
| 1283 | async_evt_tx_done(data); |
Gerard Marull-Paretas | e3f4907 | 2021-09-09 22:41:35 +0200 | [diff] [blame] | 1284 | |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 1285 | #ifdef CONFIG_PM |
Gerard Marull-Paretas | 5a71eeb | 2022-01-20 16:06:28 +0100 | [diff] [blame] | 1286 | uart_stm32_pm_policy_state_lock_put(dev); |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 1287 | #endif |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1288 | } else if (LL_USART_IsEnabledIT_RXNE(usart) && |
| 1289 | LL_USART_IsActiveFlag_RXNE(usart)) { |
Francois Ramu | cf60639 | 2021-11-08 11:43:53 +0100 | [diff] [blame] | 1290 | #ifdef USART_SR_RXNE |
| 1291 | /* clear the RXNE flag, because Rx data was not read */ |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1292 | LL_USART_ClearFlag_RXNE(usart); |
Francois Ramu | cf60639 | 2021-11-08 11:43:53 +0100 | [diff] [blame] | 1293 | #else |
Francois Ramu | 95e2c39 | 2021-10-26 11:39:57 +0200 | [diff] [blame] | 1294 | /* clear the RXNE by flushing the fifo, because Rx data was not read */ |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1295 | LL_USART_RequestRxDataFlush(usart); |
Francois Ramu | cf60639 | 2021-11-08 11:43:53 +0100 | [diff] [blame] | 1296 | #endif /* USART_SR_RXNE */ |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | /* Clear errors */ |
| 1300 | uart_stm32_err_check(dev); |
| 1301 | #endif /* CONFIG_UART_ASYNC_API */ |
Erwan Gouriou | e5ab70b | 2023-11-16 12:17:59 +0100 | [diff] [blame] | 1302 | |
Erwan Gouriou | a38c8d2 | 2023-11-21 16:59:27 +0100 | [diff] [blame] | 1303 | #if defined(CONFIG_PM) && defined(IS_UART_WAKEUP_FROMSTOP_INSTANCE) \ |
| 1304 | && defined(USART_CR3_WUFIE) |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1305 | if (LL_USART_IsEnabledIT_WKUP(usart) && |
| 1306 | LL_USART_IsActiveFlag_WKUP(usart)) { |
Erwan Gouriou | e5ab70b | 2023-11-16 12:17:59 +0100 | [diff] [blame] | 1307 | |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1308 | LL_USART_ClearFlag_WKUP(usart); |
Erwan Gouriou | e5ab70b | 2023-11-16 12:17:59 +0100 | [diff] [blame] | 1309 | #ifdef USART_ISR_REACK |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1310 | while (LL_USART_IsActiveFlag_REACK(usart) == 0) { |
Erwan Gouriou | e5ab70b | 2023-11-16 12:17:59 +0100 | [diff] [blame] | 1311 | } |
| 1312 | #endif |
| 1313 | } |
| 1314 | #endif |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1315 | } |
Erwan Gouriou | 79ff645 | 2021-09-21 09:34:09 +0200 | [diff] [blame] | 1316 | #endif /* CONFIG_UART_INTERRUPT_DRIVEN || CONFIG_UART_ASYNC_API || CONFIG_PM */ |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1317 | |
| 1318 | #ifdef CONFIG_UART_ASYNC_API |
| 1319 | |
Abderrahmane Jarmouni | e783aaf | 2024-03-20 15:25:14 +0100 | [diff] [blame] | 1320 | #ifdef CONFIG_DCACHE |
| 1321 | static bool buf_in_nocache(uintptr_t buf, size_t len_bytes) |
| 1322 | { |
| 1323 | bool buf_within_nocache = false; |
| 1324 | |
| 1325 | #ifdef CONFIG_NOCACHE_MEMORY |
| 1326 | buf_within_nocache = (buf >= ((uintptr_t)_nocache_ram_start)) && |
| 1327 | ((buf + len_bytes - 1) <= ((uintptr_t)_nocache_ram_end)); |
| 1328 | if (buf_within_nocache) { |
| 1329 | return true; |
| 1330 | } |
| 1331 | #endif /* CONFIG_NOCACHE_MEMORY */ |
| 1332 | |
| 1333 | buf_within_nocache = mem_attr_check_buf( |
| 1334 | (void *)buf, len_bytes, DT_MEM_ARM_MPU_RAM_NOCACHE) == 0; |
| 1335 | if (buf_within_nocache) { |
| 1336 | return true; |
| 1337 | } |
| 1338 | |
| 1339 | buf_within_nocache = (buf >= ((uintptr_t)__rodata_region_start)) && |
| 1340 | ((buf + len_bytes - 1) <= ((uintptr_t)__rodata_region_end)); |
| 1341 | |
| 1342 | return buf_within_nocache; |
| 1343 | } |
| 1344 | #endif /* CONFIG_DCACHE */ |
| 1345 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1346 | static int uart_stm32_async_callback_set(const struct device *dev, |
| 1347 | uart_callback_t callback, |
| 1348 | void *user_data) |
| 1349 | { |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 1350 | struct uart_stm32_data *data = dev->data; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1351 | |
| 1352 | data->async_cb = callback; |
| 1353 | data->async_user_data = user_data; |
| 1354 | |
Daniel Leung | 9f02eea | 2022-08-18 11:19:46 -0700 | [diff] [blame] | 1355 | #if defined(CONFIG_UART_EXCLUSIVE_API_CALLBACKS) |
| 1356 | data->user_cb = NULL; |
| 1357 | data->user_data = NULL; |
| 1358 | #endif |
| 1359 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1360 | return 0; |
| 1361 | } |
| 1362 | |
| 1363 | static inline void uart_stm32_dma_tx_enable(const struct device *dev) |
| 1364 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1365 | const struct uart_stm32_config *config = dev->config; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1366 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1367 | LL_USART_EnableDMAReq_TX(config->usart); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | static inline void uart_stm32_dma_tx_disable(const struct device *dev) |
| 1371 | { |
Erwan Gouriou | d7513fb | 2023-11-14 16:49:18 +0100 | [diff] [blame] | 1372 | #ifdef CONFIG_UART_STM32U5_ERRATA_DMAT |
Francois Ramu | 1b2942f | 2023-04-05 16:09:27 +0200 | [diff] [blame] | 1373 | ARG_UNUSED(dev); |
| 1374 | |
| 1375 | /* |
| 1376 | * Errata Sheet ES0499 : STM32U575xx and STM32U585xx device errata |
| 1377 | * USART does not generate DMA requests after setting/clearing DMAT bit |
| 1378 | * (also seen on stm32H5 serie) |
| 1379 | */ |
| 1380 | #else |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1381 | const struct uart_stm32_config *config = dev->config; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1382 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1383 | LL_USART_DisableDMAReq_TX(config->usart); |
Erwan Gouriou | d7513fb | 2023-11-14 16:49:18 +0100 | [diff] [blame] | 1384 | #endif |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | static inline void uart_stm32_dma_rx_enable(const struct device *dev) |
| 1388 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1389 | const struct uart_stm32_config *config = dev->config; |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 1390 | struct uart_stm32_data *data = dev->data; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1391 | |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1392 | LL_USART_EnableDMAReq_RX(config->usart); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1393 | |
| 1394 | data->dma_rx.enabled = true; |
| 1395 | } |
| 1396 | |
| 1397 | static inline void uart_stm32_dma_rx_disable(const struct device *dev) |
| 1398 | { |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 1399 | struct uart_stm32_data *data = dev->data; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1400 | |
| 1401 | data->dma_rx.enabled = false; |
| 1402 | } |
| 1403 | |
| 1404 | static int uart_stm32_async_rx_disable(const struct device *dev) |
| 1405 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1406 | const struct uart_stm32_config *config = dev->config; |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1407 | USART_TypeDef *usart = config->usart; |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 1408 | struct uart_stm32_data *data = dev->data; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1409 | struct uart_event disabled_event = { |
| 1410 | .type = UART_RX_DISABLED |
| 1411 | }; |
| 1412 | |
| 1413 | if (!data->dma_rx.enabled) { |
| 1414 | async_user_callback(data, &disabled_event); |
| 1415 | return -EFAULT; |
| 1416 | } |
| 1417 | |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1418 | LL_USART_DisableIT_IDLE(usart); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1419 | |
| 1420 | uart_stm32_dma_rx_flush(dev); |
| 1421 | |
| 1422 | async_evt_rx_buf_release(data); |
| 1423 | |
| 1424 | uart_stm32_dma_rx_disable(dev); |
| 1425 | |
Francois Ramu | 962d6b1 | 2021-04-08 12:09:58 +0200 | [diff] [blame] | 1426 | (void)k_work_cancel_delayable(&data->dma_rx.timeout_work); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1427 | |
Erwan Gouriou | 13c2351 | 2021-03-01 11:59:57 +0100 | [diff] [blame] | 1428 | dma_stop(data->dma_rx.dma_dev, data->dma_rx.dma_channel); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1429 | |
Wouter Cappelle | 984b612 | 2022-10-07 14:49:16 +0200 | [diff] [blame] | 1430 | if (data->rx_next_buffer) { |
| 1431 | struct uart_event rx_next_buf_release_evt = { |
| 1432 | .type = UART_RX_BUF_RELEASED, |
| 1433 | .data.rx_buf.buf = data->rx_next_buffer, |
| 1434 | }; |
| 1435 | async_user_callback(data, &rx_next_buf_release_evt); |
| 1436 | } |
| 1437 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1438 | data->rx_next_buffer = NULL; |
| 1439 | data->rx_next_buffer_len = 0; |
| 1440 | |
Yegor Yefremov | 1155d46 | 2022-03-07 22:53:16 +0100 | [diff] [blame] | 1441 | /* When async rx is disabled, enable interruptible instance of uart to function normally */ |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1442 | LL_USART_EnableIT_RXNE(usart); |
Manojkumar Subramaniam | d79d26f | 2021-08-23 02:04:05 +0800 | [diff] [blame] | 1443 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1444 | LOG_DBG("rx: disabled"); |
| 1445 | |
| 1446 | async_user_callback(data, &disabled_event); |
| 1447 | |
| 1448 | return 0; |
| 1449 | } |
| 1450 | |
| 1451 | void uart_stm32_dma_tx_cb(const struct device *dma_dev, void *user_data, |
| 1452 | uint32_t channel, int status) |
| 1453 | { |
| 1454 | const struct device *uart_dev = user_data; |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 1455 | struct uart_stm32_data *data = uart_dev->data; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1456 | struct dma_status stat; |
| 1457 | unsigned int key = irq_lock(); |
| 1458 | |
| 1459 | /* Disable TX */ |
| 1460 | uart_stm32_dma_tx_disable(uart_dev); |
| 1461 | |
Francois Ramu | 962d6b1 | 2021-04-08 12:09:58 +0200 | [diff] [blame] | 1462 | (void)k_work_cancel_delayable(&data->dma_tx.timeout_work); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1463 | |
Erwan Gouriou | 13c2351 | 2021-03-01 11:59:57 +0100 | [diff] [blame] | 1464 | if (!dma_get_status(data->dma_tx.dma_dev, |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1465 | data->dma_tx.dma_channel, &stat)) { |
| 1466 | data->dma_tx.counter = data->dma_tx.buffer_length - |
| 1467 | stat.pending_length; |
| 1468 | } |
| 1469 | |
Prema Jonathan van Win | 76dee39 | 2021-05-03 11:24:38 +0300 | [diff] [blame] | 1470 | data->dma_tx.buffer_length = 0; |
| 1471 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1472 | irq_unlock(key); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1473 | } |
| 1474 | |
| 1475 | static void uart_stm32_dma_replace_buffer(const struct device *dev) |
| 1476 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1477 | const struct uart_stm32_config *config = dev->config; |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1478 | USART_TypeDef *usart = config->usart; |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 1479 | struct uart_stm32_data *data = dev->data; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1480 | |
Yegor Yefremov | 1155d46 | 2022-03-07 22:53:16 +0100 | [diff] [blame] | 1481 | /* Replace the buffer and reload the DMA */ |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1482 | LOG_DBG("Replacing RX buffer: %d", data->rx_next_buffer_len); |
| 1483 | |
| 1484 | /* reload DMA */ |
| 1485 | data->dma_rx.offset = 0; |
| 1486 | data->dma_rx.counter = 0; |
| 1487 | data->dma_rx.buffer = data->rx_next_buffer; |
| 1488 | data->dma_rx.buffer_length = data->rx_next_buffer_len; |
| 1489 | data->dma_rx.blk_cfg.block_size = data->dma_rx.buffer_length; |
| 1490 | data->dma_rx.blk_cfg.dest_address = (uint32_t)data->dma_rx.buffer; |
| 1491 | data->rx_next_buffer = NULL; |
| 1492 | data->rx_next_buffer_len = 0; |
| 1493 | |
Erwan Gouriou | 13c2351 | 2021-03-01 11:59:57 +0100 | [diff] [blame] | 1494 | dma_reload(data->dma_rx.dma_dev, data->dma_rx.dma_channel, |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1495 | data->dma_rx.blk_cfg.source_address, |
| 1496 | data->dma_rx.blk_cfg.dest_address, |
| 1497 | data->dma_rx.blk_cfg.block_size); |
| 1498 | |
Erwan Gouriou | 13c2351 | 2021-03-01 11:59:57 +0100 | [diff] [blame] | 1499 | dma_start(data->dma_rx.dma_dev, data->dma_rx.dma_channel); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1500 | |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1501 | LL_USART_ClearFlag_IDLE(usart); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1502 | |
| 1503 | /* Request next buffer */ |
| 1504 | async_evt_rx_buf_request(data); |
| 1505 | } |
| 1506 | |
| 1507 | void uart_stm32_dma_rx_cb(const struct device *dma_dev, void *user_data, |
| 1508 | uint32_t channel, int status) |
| 1509 | { |
| 1510 | const struct device *uart_dev = user_data; |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 1511 | struct uart_stm32_data *data = uart_dev->data; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1512 | |
Cyril Fougeray | 1be72d9 | 2023-03-26 17:26:23 +0200 | [diff] [blame] | 1513 | if (status < 0) { |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1514 | async_evt_rx_err(data, status); |
| 1515 | return; |
| 1516 | } |
| 1517 | |
Francois Ramu | 962d6b1 | 2021-04-08 12:09:58 +0200 | [diff] [blame] | 1518 | (void)k_work_cancel_delayable(&data->dma_rx.timeout_work); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1519 | |
| 1520 | /* true since this functions occurs when buffer if full */ |
| 1521 | data->dma_rx.counter = data->dma_rx.buffer_length; |
| 1522 | |
| 1523 | async_evt_rx_rdy(data); |
| 1524 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1525 | if (data->rx_next_buffer != NULL) { |
| 1526 | async_evt_rx_buf_release(data); |
| 1527 | |
| 1528 | /* replace the buffer when the current |
| 1529 | * is full and not the same as the next |
| 1530 | * one. |
| 1531 | */ |
| 1532 | uart_stm32_dma_replace_buffer(uart_dev); |
| 1533 | } else { |
| 1534 | /* Buffer full without valid next buffer, |
| 1535 | * an UART_RX_DISABLED event must be generated, |
| 1536 | * but uart_stm32_async_rx_disable() cannot be |
| 1537 | * called in ISR context. So force the RX timeout |
| 1538 | * to minimum value and let the RX timeout to do the job. |
| 1539 | */ |
Francois Ramu | 962d6b1 | 2021-04-08 12:09:58 +0200 | [diff] [blame] | 1540 | k_work_reschedule(&data->dma_rx.timeout_work, K_TICKS(1)); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1541 | } |
| 1542 | } |
| 1543 | |
| 1544 | static int uart_stm32_async_tx(const struct device *dev, |
| 1545 | const uint8_t *tx_data, size_t buf_size, int32_t timeout) |
| 1546 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1547 | const struct uart_stm32_config *config = dev->config; |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1548 | USART_TypeDef *usart = config->usart; |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 1549 | struct uart_stm32_data *data = dev->data; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1550 | int ret; |
| 1551 | |
Erwan Gouriou | 13c2351 | 2021-03-01 11:59:57 +0100 | [diff] [blame] | 1552 | if (data->dma_tx.dma_dev == NULL) { |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1553 | return -ENODEV; |
| 1554 | } |
| 1555 | |
| 1556 | if (data->dma_tx.buffer_length != 0) { |
| 1557 | return -EBUSY; |
| 1558 | } |
| 1559 | |
Abderrahmane Jarmouni | e783aaf | 2024-03-20 15:25:14 +0100 | [diff] [blame] | 1560 | #ifdef CONFIG_DCACHE |
| 1561 | if (!buf_in_nocache((uintptr_t)tx_data, buf_size)) { |
| 1562 | LOG_ERR("Tx buffer should be placed in a nocache memory region"); |
| 1563 | return -EFAULT; |
| 1564 | } |
| 1565 | #endif /* CONFIG_DCACHE */ |
| 1566 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1567 | data->dma_tx.buffer = (uint8_t *)tx_data; |
| 1568 | data->dma_tx.buffer_length = buf_size; |
| 1569 | data->dma_tx.timeout = timeout; |
| 1570 | |
| 1571 | LOG_DBG("tx: l=%d", data->dma_tx.buffer_length); |
| 1572 | |
Zisis Adamos | 7235b09 | 2021-03-30 12:20:46 +0200 | [diff] [blame] | 1573 | /* Clear TC flag */ |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1574 | LL_USART_ClearFlag_TC(usart); |
Zisis Adamos | 7235b09 | 2021-03-30 12:20:46 +0200 | [diff] [blame] | 1575 | |
| 1576 | /* Enable TC interrupt so we can signal correct TX done */ |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1577 | LL_USART_EnableIT_TC(usart); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1578 | |
| 1579 | /* set source address */ |
| 1580 | data->dma_tx.blk_cfg.source_address = (uint32_t)data->dma_tx.buffer; |
| 1581 | data->dma_tx.blk_cfg.block_size = data->dma_tx.buffer_length; |
| 1582 | |
Erwan Gouriou | 13c2351 | 2021-03-01 11:59:57 +0100 | [diff] [blame] | 1583 | 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] | 1584 | &data->dma_tx.dma_cfg); |
| 1585 | |
| 1586 | if (ret != 0) { |
| 1587 | LOG_ERR("dma tx config error!"); |
| 1588 | return -EINVAL; |
| 1589 | } |
| 1590 | |
Erwan Gouriou | 13c2351 | 2021-03-01 11:59:57 +0100 | [diff] [blame] | 1591 | 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] | 1592 | LOG_ERR("UART err: TX DMA start failed!"); |
| 1593 | return -EFAULT; |
| 1594 | } |
| 1595 | |
| 1596 | /* Start TX timer */ |
| 1597 | async_timer_start(&data->dma_tx.timeout_work, data->dma_tx.timeout); |
| 1598 | |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 1599 | #ifdef CONFIG_PM |
Erwan Gouriou | 79ff645 | 2021-09-21 09:34:09 +0200 | [diff] [blame] | 1600 | |
| 1601 | /* Do not allow system to suspend until transmission has completed */ |
Gerard Marull-Paretas | 5a71eeb | 2022-01-20 16:06:28 +0100 | [diff] [blame] | 1602 | uart_stm32_pm_policy_state_lock_get(dev); |
Erwan Gouriou | a3de3df | 2021-09-14 09:27:56 +0200 | [diff] [blame] | 1603 | #endif |
Gerard Marull-Paretas | e3f4907 | 2021-09-09 22:41:35 +0200 | [diff] [blame] | 1604 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1605 | /* Enable TX DMA requests */ |
| 1606 | uart_stm32_dma_tx_enable(dev); |
| 1607 | |
| 1608 | return 0; |
| 1609 | } |
| 1610 | |
| 1611 | static int uart_stm32_async_rx_enable(const struct device *dev, |
| 1612 | uint8_t *rx_buf, size_t buf_size, int32_t timeout) |
| 1613 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1614 | const struct uart_stm32_config *config = dev->config; |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1615 | USART_TypeDef *usart = config->usart; |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 1616 | struct uart_stm32_data *data = dev->data; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1617 | int ret; |
| 1618 | |
Erwan Gouriou | 13c2351 | 2021-03-01 11:59:57 +0100 | [diff] [blame] | 1619 | if (data->dma_rx.dma_dev == NULL) { |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1620 | return -ENODEV; |
| 1621 | } |
| 1622 | |
| 1623 | if (data->dma_rx.enabled) { |
| 1624 | LOG_WRN("RX was already enabled"); |
| 1625 | return -EBUSY; |
| 1626 | } |
| 1627 | |
Abderrahmane Jarmouni | e783aaf | 2024-03-20 15:25:14 +0100 | [diff] [blame] | 1628 | #ifdef CONFIG_DCACHE |
| 1629 | if (!buf_in_nocache((uintptr_t)rx_buf, buf_size)) { |
| 1630 | LOG_ERR("Rx buffer should be placed in a nocache memory region"); |
| 1631 | return -EFAULT; |
| 1632 | } |
| 1633 | #endif /* CONFIG_DCACHE */ |
| 1634 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1635 | data->dma_rx.offset = 0; |
| 1636 | data->dma_rx.buffer = rx_buf; |
| 1637 | data->dma_rx.buffer_length = buf_size; |
| 1638 | data->dma_rx.counter = 0; |
| 1639 | data->dma_rx.timeout = timeout; |
| 1640 | |
| 1641 | /* Disable RX interrupts to let DMA to handle it */ |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1642 | LL_USART_DisableIT_RXNE(usart); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1643 | |
| 1644 | data->dma_rx.blk_cfg.block_size = buf_size; |
| 1645 | data->dma_rx.blk_cfg.dest_address = (uint32_t)data->dma_rx.buffer; |
| 1646 | |
Erwan Gouriou | 13c2351 | 2021-03-01 11:59:57 +0100 | [diff] [blame] | 1647 | 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] | 1648 | &data->dma_rx.dma_cfg); |
| 1649 | |
| 1650 | if (ret != 0) { |
| 1651 | LOG_ERR("UART ERR: RX DMA config failed!"); |
| 1652 | return -EINVAL; |
| 1653 | } |
| 1654 | |
Erwan Gouriou | 13c2351 | 2021-03-01 11:59:57 +0100 | [diff] [blame] | 1655 | 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] | 1656 | LOG_ERR("UART ERR: RX DMA start failed!"); |
| 1657 | return -EFAULT; |
| 1658 | } |
| 1659 | |
Bjarki Arge Andreasen | f9b42bc | 2023-11-16 20:39:39 +0100 | [diff] [blame] | 1660 | /* Flush RX data buffer */ |
| 1661 | #ifdef USART_SR_RXNE |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1662 | LL_USART_ClearFlag_RXNE(usart); |
Bjarki Arge Andreasen | f9b42bc | 2023-11-16 20:39:39 +0100 | [diff] [blame] | 1663 | #else |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1664 | LL_USART_RequestRxDataFlush(usart); |
Bjarki Arge Andreasen | f9b42bc | 2023-11-16 20:39:39 +0100 | [diff] [blame] | 1665 | #endif /* USART_SR_RXNE */ |
| 1666 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1667 | /* Enable RX DMA requests */ |
| 1668 | uart_stm32_dma_rx_enable(dev); |
| 1669 | |
| 1670 | /* Enable IRQ IDLE to define the end of a |
| 1671 | * RX DMA transaction. |
| 1672 | */ |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1673 | LL_USART_ClearFlag_IDLE(usart); |
| 1674 | LL_USART_EnableIT_IDLE(usart); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1675 | |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1676 | LL_USART_EnableIT_ERROR(usart); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1677 | |
| 1678 | /* Request next buffer */ |
| 1679 | async_evt_rx_buf_request(data); |
| 1680 | |
| 1681 | LOG_DBG("async rx enabled"); |
| 1682 | |
| 1683 | return ret; |
| 1684 | } |
| 1685 | |
| 1686 | static int uart_stm32_async_tx_abort(const struct device *dev) |
| 1687 | { |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 1688 | struct uart_stm32_data *data = dev->data; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1689 | size_t tx_buffer_length = data->dma_tx.buffer_length; |
| 1690 | struct dma_status stat; |
| 1691 | |
| 1692 | if (tx_buffer_length == 0) { |
| 1693 | return -EFAULT; |
| 1694 | } |
| 1695 | |
Francois Ramu | 962d6b1 | 2021-04-08 12:09:58 +0200 | [diff] [blame] | 1696 | (void)k_work_cancel_delayable(&data->dma_tx.timeout_work); |
Erwan Gouriou | 13c2351 | 2021-03-01 11:59:57 +0100 | [diff] [blame] | 1697 | if (!dma_get_status(data->dma_tx.dma_dev, |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1698 | data->dma_tx.dma_channel, &stat)) { |
| 1699 | data->dma_tx.counter = tx_buffer_length - stat.pending_length; |
| 1700 | } |
| 1701 | |
Erwan Gouriou | d2ea9e4 | 2023-11-30 11:35:11 +0100 | [diff] [blame] | 1702 | #if DT_HAS_COMPAT_STATUS_OKAY(st_stm32u5_dma) |
Francois Ramu | e00f91d | 2022-10-04 17:18:42 +0200 | [diff] [blame] | 1703 | dma_suspend(data->dma_tx.dma_dev, data->dma_tx.dma_channel); |
Erwan Gouriou | d7513fb | 2023-11-14 16:49:18 +0100 | [diff] [blame] | 1704 | #endif |
Erwan Gouriou | 13c2351 | 2021-03-01 11:59:57 +0100 | [diff] [blame] | 1705 | dma_stop(data->dma_tx.dma_dev, data->dma_tx.dma_channel); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1706 | async_evt_tx_abort(data); |
| 1707 | |
| 1708 | return 0; |
| 1709 | } |
| 1710 | |
| 1711 | static void uart_stm32_async_rx_timeout(struct k_work *work) |
| 1712 | { |
Yong Cong Sin | 731241f | 2022-01-19 12:07:51 +0800 | [diff] [blame] | 1713 | struct k_work_delayable *dwork = k_work_delayable_from_work(work); |
| 1714 | struct uart_dma_stream *rx_stream = CONTAINER_OF(dwork, |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1715 | struct uart_dma_stream, timeout_work); |
| 1716 | struct uart_stm32_data *data = CONTAINER_OF(rx_stream, |
| 1717 | struct uart_stm32_data, dma_rx); |
| 1718 | const struct device *dev = data->uart_dev; |
| 1719 | |
| 1720 | LOG_DBG("rx timeout"); |
| 1721 | |
| 1722 | if (data->dma_rx.counter == data->dma_rx.buffer_length) { |
| 1723 | uart_stm32_async_rx_disable(dev); |
| 1724 | } else { |
| 1725 | uart_stm32_dma_rx_flush(dev); |
| 1726 | } |
| 1727 | } |
| 1728 | |
| 1729 | static void uart_stm32_async_tx_timeout(struct k_work *work) |
| 1730 | { |
Yong Cong Sin | 731241f | 2022-01-19 12:07:51 +0800 | [diff] [blame] | 1731 | struct k_work_delayable *dwork = k_work_delayable_from_work(work); |
| 1732 | struct uart_dma_stream *tx_stream = CONTAINER_OF(dwork, |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1733 | struct uart_dma_stream, timeout_work); |
| 1734 | struct uart_stm32_data *data = CONTAINER_OF(tx_stream, |
| 1735 | struct uart_stm32_data, dma_tx); |
| 1736 | const struct device *dev = data->uart_dev; |
| 1737 | |
| 1738 | uart_stm32_async_tx_abort(dev); |
| 1739 | |
| 1740 | LOG_DBG("tx: async timeout"); |
| 1741 | } |
| 1742 | |
| 1743 | static int uart_stm32_async_rx_buf_rsp(const struct device *dev, uint8_t *buf, |
| 1744 | size_t len) |
| 1745 | { |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 1746 | struct uart_stm32_data *data = dev->data; |
Abderrahmane Jarmouni | 4affeaa | 2024-02-15 16:59:47 +0100 | [diff] [blame] | 1747 | unsigned int key; |
| 1748 | int err = 0; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1749 | |
| 1750 | LOG_DBG("replace buffer (%d)", len); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1751 | |
Abderrahmane Jarmouni | 4affeaa | 2024-02-15 16:59:47 +0100 | [diff] [blame] | 1752 | key = irq_lock(); |
| 1753 | |
| 1754 | if (data->rx_next_buffer != NULL) { |
| 1755 | err = -EBUSY; |
| 1756 | } else if (!data->dma_rx.enabled) { |
| 1757 | err = -EACCES; |
| 1758 | } else { |
Abderrahmane Jarmouni | e783aaf | 2024-03-20 15:25:14 +0100 | [diff] [blame] | 1759 | #ifdef CONFIG_DCACHE |
| 1760 | if (!buf_in_nocache((uintptr_t)buf, len)) { |
| 1761 | LOG_ERR("Rx buffer should be placed in a nocache memory region"); |
| 1762 | return -EFAULT; |
| 1763 | } |
| 1764 | #endif /* CONFIG_DCACHE */ |
Abderrahmane Jarmouni | 4affeaa | 2024-02-15 16:59:47 +0100 | [diff] [blame] | 1765 | data->rx_next_buffer = buf; |
| 1766 | data->rx_next_buffer_len = len; |
| 1767 | } |
| 1768 | |
| 1769 | irq_unlock(key); |
| 1770 | |
| 1771 | return err; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1772 | } |
| 1773 | |
| 1774 | static int uart_stm32_async_init(const struct device *dev) |
| 1775 | { |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 1776 | const struct uart_stm32_config *config = dev->config; |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1777 | USART_TypeDef *usart = config->usart; |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 1778 | struct uart_stm32_data *data = dev->data; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1779 | |
| 1780 | data->uart_dev = dev; |
| 1781 | |
Erwan Gouriou | 13c2351 | 2021-03-01 11:59:57 +0100 | [diff] [blame] | 1782 | if (data->dma_rx.dma_dev != NULL) { |
| 1783 | if (!device_is_ready(data->dma_rx.dma_dev)) { |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1784 | return -ENODEV; |
| 1785 | } |
| 1786 | } |
| 1787 | |
Erwan Gouriou | 13c2351 | 2021-03-01 11:59:57 +0100 | [diff] [blame] | 1788 | if (data->dma_tx.dma_dev != NULL) { |
Georgij Cernysiov | 64c804a | 2022-05-06 08:47:03 +0200 | [diff] [blame] | 1789 | if (!device_is_ready(data->dma_tx.dma_dev)) { |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1790 | return -ENODEV; |
| 1791 | } |
| 1792 | } |
| 1793 | |
| 1794 | /* Disable both TX and RX DMA requests */ |
| 1795 | uart_stm32_dma_rx_disable(dev); |
| 1796 | uart_stm32_dma_tx_disable(dev); |
| 1797 | |
Francois Ramu | 962d6b1 | 2021-04-08 12:09:58 +0200 | [diff] [blame] | 1798 | k_work_init_delayable(&data->dma_rx.timeout_work, |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1799 | uart_stm32_async_rx_timeout); |
Francois Ramu | 962d6b1 | 2021-04-08 12:09:58 +0200 | [diff] [blame] | 1800 | k_work_init_delayable(&data->dma_tx.timeout_work, |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1801 | uart_stm32_async_tx_timeout); |
| 1802 | |
| 1803 | /* Configure dma rx config */ |
| 1804 | memset(&data->dma_rx.blk_cfg, 0, sizeof(data->dma_rx.blk_cfg)); |
| 1805 | |
| 1806 | #if defined(CONFIG_SOC_SERIES_STM32F1X) || \ |
| 1807 | defined(CONFIG_SOC_SERIES_STM32F2X) || \ |
| 1808 | defined(CONFIG_SOC_SERIES_STM32F4X) || \ |
| 1809 | defined(CONFIG_SOC_SERIES_STM32L1X) |
| 1810 | data->dma_rx.blk_cfg.source_address = |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1811 | LL_USART_DMA_GetRegAddr(usart); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1812 | #else |
| 1813 | data->dma_rx.blk_cfg.source_address = |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1814 | LL_USART_DMA_GetRegAddr(usart, |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1815 | LL_USART_DMA_REG_DATA_RECEIVE); |
| 1816 | #endif |
| 1817 | |
| 1818 | data->dma_rx.blk_cfg.dest_address = 0; /* dest not ready */ |
| 1819 | |
| 1820 | if (data->dma_rx.src_addr_increment) { |
| 1821 | data->dma_rx.blk_cfg.source_addr_adj = DMA_ADDR_ADJ_INCREMENT; |
| 1822 | } else { |
| 1823 | data->dma_rx.blk_cfg.source_addr_adj = DMA_ADDR_ADJ_NO_CHANGE; |
| 1824 | } |
| 1825 | |
| 1826 | if (data->dma_rx.dst_addr_increment) { |
| 1827 | data->dma_rx.blk_cfg.dest_addr_adj = DMA_ADDR_ADJ_INCREMENT; |
| 1828 | } else { |
| 1829 | data->dma_rx.blk_cfg.dest_addr_adj = DMA_ADDR_ADJ_NO_CHANGE; |
| 1830 | } |
| 1831 | |
| 1832 | /* RX disable circular buffer */ |
| 1833 | data->dma_rx.blk_cfg.source_reload_en = 0; |
| 1834 | data->dma_rx.blk_cfg.dest_reload_en = 0; |
| 1835 | data->dma_rx.blk_cfg.fifo_mode_control = data->dma_rx.fifo_threshold; |
| 1836 | |
| 1837 | data->dma_rx.dma_cfg.head_block = &data->dma_rx.blk_cfg; |
| 1838 | data->dma_rx.dma_cfg.user_data = (void *)dev; |
| 1839 | data->rx_next_buffer = NULL; |
| 1840 | data->rx_next_buffer_len = 0; |
| 1841 | |
| 1842 | /* Configure dma tx config */ |
| 1843 | memset(&data->dma_tx.blk_cfg, 0, sizeof(data->dma_tx.blk_cfg)); |
| 1844 | |
| 1845 | #if defined(CONFIG_SOC_SERIES_STM32F1X) || \ |
| 1846 | defined(CONFIG_SOC_SERIES_STM32F2X) || \ |
| 1847 | defined(CONFIG_SOC_SERIES_STM32F4X) || \ |
| 1848 | defined(CONFIG_SOC_SERIES_STM32L1X) |
| 1849 | data->dma_tx.blk_cfg.dest_address = |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1850 | LL_USART_DMA_GetRegAddr(usart); |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1851 | #else |
| 1852 | data->dma_tx.blk_cfg.dest_address = |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1853 | LL_USART_DMA_GetRegAddr(usart, |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1854 | LL_USART_DMA_REG_DATA_TRANSMIT); |
| 1855 | #endif |
| 1856 | |
| 1857 | data->dma_tx.blk_cfg.source_address = 0; /* not ready */ |
| 1858 | |
| 1859 | if (data->dma_tx.src_addr_increment) { |
| 1860 | data->dma_tx.blk_cfg.source_addr_adj = DMA_ADDR_ADJ_INCREMENT; |
| 1861 | } else { |
| 1862 | data->dma_tx.blk_cfg.source_addr_adj = DMA_ADDR_ADJ_NO_CHANGE; |
| 1863 | } |
| 1864 | |
| 1865 | if (data->dma_tx.dst_addr_increment) { |
| 1866 | data->dma_tx.blk_cfg.dest_addr_adj = DMA_ADDR_ADJ_INCREMENT; |
| 1867 | } else { |
| 1868 | data->dma_tx.blk_cfg.dest_addr_adj = DMA_ADDR_ADJ_NO_CHANGE; |
| 1869 | } |
| 1870 | |
| 1871 | data->dma_tx.blk_cfg.fifo_mode_control = data->dma_tx.fifo_threshold; |
| 1872 | |
| 1873 | data->dma_tx.dma_cfg.head_block = &data->dma_tx.blk_cfg; |
| 1874 | data->dma_tx.dma_cfg.user_data = (void *)dev; |
| 1875 | |
| 1876 | return 0; |
| 1877 | } |
| 1878 | |
Jeroen van Dooren | 6ab7069 | 2023-02-06 09:54:20 +0100 | [diff] [blame] | 1879 | #ifdef CONFIG_UART_WIDE_DATA |
| 1880 | |
| 1881 | static int uart_stm32_async_tx_u16(const struct device *dev, const uint16_t *tx_data, |
| 1882 | size_t buf_size, int32_t timeout) |
| 1883 | { |
| 1884 | return uart_stm32_async_tx(dev, (const uint8_t *)tx_data, buf_size * 2, timeout); |
| 1885 | } |
| 1886 | |
| 1887 | static int uart_stm32_async_rx_enable_u16(const struct device *dev, uint16_t *buf, size_t len, |
| 1888 | int32_t timeout) |
| 1889 | { |
| 1890 | return uart_stm32_async_rx_enable(dev, (uint8_t *)buf, len * 2, timeout); |
| 1891 | } |
| 1892 | |
| 1893 | static int uart_stm32_async_rx_buf_rsp_u16(const struct device *dev, uint16_t *buf, size_t len) |
| 1894 | { |
| 1895 | return uart_stm32_async_rx_buf_rsp(dev, (uint8_t *)buf, len * 2); |
| 1896 | } |
| 1897 | |
| 1898 | #endif |
| 1899 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1900 | #endif /* CONFIG_UART_ASYNC_API */ |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1901 | |
Pieter De Gendt | 29c9b91 | 2024-11-28 21:21:48 +0100 | [diff] [blame] | 1902 | static DEVICE_API(uart, uart_stm32_driver_api) = { |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 1903 | .poll_in = uart_stm32_poll_in, |
| 1904 | .poll_out = uart_stm32_poll_out, |
Jeroen van Dooren | 6ab7069 | 2023-02-06 09:54:20 +0100 | [diff] [blame] | 1905 | #ifdef CONFIG_UART_WIDE_DATA |
| 1906 | .poll_in_u16 = uart_stm32_poll_in_u16, |
| 1907 | .poll_out_u16 = uart_stm32_poll_out_u16, |
| 1908 | #endif |
Georgij Cernysiov | c74c131 | 2019-02-14 10:50:19 +0100 | [diff] [blame] | 1909 | .err_check = uart_stm32_err_check, |
Daniel Leung | 4e1692f | 2021-05-26 12:33:37 -0700 | [diff] [blame] | 1910 | #ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE |
Pushpal Sidhu | acd0e25 | 2019-01-07 13:52:24 -0800 | [diff] [blame] | 1911 | .configure = uart_stm32_configure, |
| 1912 | .config_get = uart_stm32_config_get, |
Daniel Leung | 4e1692f | 2021-05-26 12:33:37 -0700 | [diff] [blame] | 1913 | #endif /* CONFIG_UART_USE_RUNTIME_CONFIGURE */ |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1914 | #ifdef CONFIG_UART_INTERRUPT_DRIVEN |
| 1915 | .fifo_fill = uart_stm32_fifo_fill, |
| 1916 | .fifo_read = uart_stm32_fifo_read, |
Jeroen van Dooren | 6ab7069 | 2023-02-06 09:54:20 +0100 | [diff] [blame] | 1917 | #ifdef CONFIG_UART_WIDE_DATA |
| 1918 | .fifo_fill_u16 = uart_stm32_fifo_fill_u16, |
| 1919 | .fifo_read_u16 = uart_stm32_fifo_read_u16, |
| 1920 | #endif |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1921 | .irq_tx_enable = uart_stm32_irq_tx_enable, |
| 1922 | .irq_tx_disable = uart_stm32_irq_tx_disable, |
| 1923 | .irq_tx_ready = uart_stm32_irq_tx_ready, |
Paul Sokolovsky | 0fdc9b5 | 2017-05-11 17:57:29 +0300 | [diff] [blame] | 1924 | .irq_tx_complete = uart_stm32_irq_tx_complete, |
Maciek Borzecki | 0cd7ff8 | 2016-03-13 19:37:25 +0100 | [diff] [blame] | 1925 | .irq_rx_enable = uart_stm32_irq_rx_enable, |
| 1926 | .irq_rx_disable = uart_stm32_irq_rx_disable, |
| 1927 | .irq_rx_ready = uart_stm32_irq_rx_ready, |
| 1928 | .irq_err_enable = uart_stm32_irq_err_enable, |
| 1929 | .irq_err_disable = uart_stm32_irq_err_disable, |
| 1930 | .irq_is_pending = uart_stm32_irq_is_pending, |
| 1931 | .irq_update = uart_stm32_irq_update, |
| 1932 | .irq_callback_set = uart_stm32_irq_callback_set, |
Jeroen van Dooren | 6ab7069 | 2023-02-06 09:54:20 +0100 | [diff] [blame] | 1933 | #endif /* CONFIG_UART_INTERRUPT_DRIVEN */ |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 1934 | #ifdef CONFIG_UART_ASYNC_API |
| 1935 | .callback_set = uart_stm32_async_callback_set, |
| 1936 | .tx = uart_stm32_async_tx, |
| 1937 | .tx_abort = uart_stm32_async_tx_abort, |
| 1938 | .rx_enable = uart_stm32_async_rx_enable, |
| 1939 | .rx_disable = uart_stm32_async_rx_disable, |
| 1940 | .rx_buf_rsp = uart_stm32_async_rx_buf_rsp, |
Jeroen van Dooren | 6ab7069 | 2023-02-06 09:54:20 +0100 | [diff] [blame] | 1941 | #ifdef CONFIG_UART_WIDE_DATA |
| 1942 | .tx_u16 = uart_stm32_async_tx_u16, |
| 1943 | .rx_enable_u16 = uart_stm32_async_rx_enable_u16, |
| 1944 | .rx_buf_rsp_u16 = uart_stm32_async_rx_buf_rsp_u16, |
| 1945 | #endif |
| 1946 | #endif /* CONFIG_UART_ASYNC_API */ |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 1947 | }; |
| 1948 | |
Kenneth J. Miller | 31a6e31 | 2023-06-20 01:28:09 +0200 | [diff] [blame] | 1949 | static int uart_stm32_clocks_enable(const struct device *dev) |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 1950 | { |
Gerard Marull-Paretas | 1674fec | 2022-01-18 16:58:32 +0100 | [diff] [blame] | 1951 | const struct uart_stm32_config *config = dev->config; |
| 1952 | struct uart_stm32_data *data = dev->data; |
Erwan Gouriou | 0b9c584 | 2020-10-16 17:20:00 +0200 | [diff] [blame] | 1953 | int err; |
Erwan Gouriou | da210ba | 2017-09-21 15:20:53 +0200 | [diff] [blame] | 1954 | |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 1955 | __uart_stm32_get_clock(dev); |
Henrik Brix Andersen | 9456eca | 2022-08-08 16:05:16 +0200 | [diff] [blame] | 1956 | |
| 1957 | if (!device_is_ready(data->clock)) { |
| 1958 | LOG_ERR("clock control device not ready"); |
| 1959 | return -ENODEV; |
| 1960 | } |
| 1961 | |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 1962 | /* enable clock */ |
Artur Lipowski | bb85759 | 2022-05-10 12:18:28 +0200 | [diff] [blame] | 1963 | err = clock_control_on(data->clock, (clock_control_subsys_t)&config->pclken[0]); |
| 1964 | if (err != 0) { |
| 1965 | LOG_ERR("Could not enable (LP)UART clock"); |
| 1966 | return err; |
| 1967 | } |
| 1968 | |
Erwan Gouriou | 1ef9e9e | 2022-08-04 11:08:15 +0200 | [diff] [blame] | 1969 | if (IS_ENABLED(STM32_UART_DOMAIN_CLOCK_SUPPORT) && (config->pclk_len > 1)) { |
Artur Lipowski | bb85759 | 2022-05-10 12:18:28 +0200 | [diff] [blame] | 1970 | err = clock_control_configure(DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE), |
| 1971 | (clock_control_subsys_t) &config->pclken[1], |
| 1972 | NULL); |
| 1973 | if (err != 0) { |
Erwan Gouriou | 1ef9e9e | 2022-08-04 11:08:15 +0200 | [diff] [blame] | 1974 | LOG_ERR("Could not select UART domain clock"); |
Artur Lipowski | bb85759 | 2022-05-10 12:18:28 +0200 | [diff] [blame] | 1975 | return err; |
| 1976 | } |
Erwan Gouriou | 9062e97 | 2018-12-07 11:09:28 +0100 | [diff] [blame] | 1977 | } |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 1978 | |
Kenneth J. Miller | 31a6e31 | 2023-06-20 01:28:09 +0200 | [diff] [blame] | 1979 | return 0; |
| 1980 | } |
| 1981 | |
| 1982 | static int uart_stm32_registers_configure(const struct device *dev) |
| 1983 | { |
| 1984 | const struct uart_stm32_config *config = dev->config; |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1985 | USART_TypeDef *usart = config->usart; |
Kenneth J. Miller | 31a6e31 | 2023-06-20 01:28:09 +0200 | [diff] [blame] | 1986 | struct uart_stm32_data *data = dev->data; |
| 1987 | struct uart_config *uart_cfg = data->uart_cfg; |
Erwan Gouriou | 252a623 | 2020-06-05 10:57:52 +0200 | [diff] [blame] | 1988 | |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 1989 | LL_USART_Disable(usart); |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 1990 | |
Kenneth J. Miller | bde1cd8 | 2023-06-23 04:24:49 +0200 | [diff] [blame] | 1991 | if (!device_is_ready(config->reset.dev)) { |
Patryk Duda | 1aebcec | 2022-10-28 14:25:13 +0200 | [diff] [blame] | 1992 | LOG_ERR("reset controller not ready"); |
| 1993 | return -ENODEV; |
| 1994 | } |
| 1995 | |
| 1996 | /* Reset UART to default state using RCC */ |
Kenneth J. Miller | bde1cd8 | 2023-06-23 04:24:49 +0200 | [diff] [blame] | 1997 | (void)reset_line_toggle_dt(&config->reset); |
Patryk Duda | 1aebcec | 2022-10-28 14:25:13 +0200 | [diff] [blame] | 1998 | |
Erwan Gouriou | da210ba | 2017-09-21 15:20:53 +0200 | [diff] [blame] | 1999 | /* TX/RX direction */ |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 2000 | LL_USART_SetTransferDirection(usart, LL_USART_DIRECTION_TX_RX); |
Erwan Gouriou | da210ba | 2017-09-21 15:20:53 +0200 | [diff] [blame] | 2001 | |
Pisit Sawangvonganan | b4567fa | 2024-01-13 00:34:18 +0700 | [diff] [blame] | 2002 | /* Set basic parameters, such as data-/stop-bit, parity, and baudrate */ |
Kenneth J. Miller | 5217a17 | 2023-06-20 01:39:32 +0200 | [diff] [blame] | 2003 | uart_stm32_parameters_set(dev, uart_cfg); |
Erwan Gouriou | da210ba | 2017-09-21 15:20:53 +0200 | [diff] [blame] | 2004 | |
Jonathan Hahn | 32f9dcf | 2022-01-01 23:41:19 +0100 | [diff] [blame] | 2005 | /* Enable the single wire / half-duplex mode */ |
| 2006 | if (config->single_wire) { |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 2007 | LL_USART_EnableHalfDuplex(usart); |
Jonathan Hahn | 32f9dcf | 2022-01-01 23:41:19 +0100 | [diff] [blame] | 2008 | } |
| 2009 | |
Peter Maxwell Warasila | 3c09c21 | 2022-05-09 16:28:51 -0400 | [diff] [blame] | 2010 | #ifdef LL_USART_TXRX_SWAPPED |
| 2011 | if (config->tx_rx_swap) { |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 2012 | LL_USART_SetTXRXSwap(usart, LL_USART_TXRX_SWAPPED); |
Peter Maxwell Warasila | 3c09c21 | 2022-05-09 16:28:51 -0400 | [diff] [blame] | 2013 | } |
| 2014 | #endif |
| 2015 | |
Jonathan Hahn | 05cc2e1 | 2022-04-25 20:12:25 +0200 | [diff] [blame] | 2016 | #ifdef LL_USART_RXPIN_LEVEL_INVERTED |
| 2017 | if (config->rx_invert) { |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 2018 | LL_USART_SetRXPinLevel(usart, LL_USART_RXPIN_LEVEL_INVERTED); |
Jonathan Hahn | 05cc2e1 | 2022-04-25 20:12:25 +0200 | [diff] [blame] | 2019 | } |
| 2020 | #endif |
| 2021 | |
| 2022 | #ifdef LL_USART_TXPIN_LEVEL_INVERTED |
| 2023 | if (config->tx_invert) { |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 2024 | LL_USART_SetTXPinLevel(usart, LL_USART_TXPIN_LEVEL_INVERTED); |
Jonathan Hahn | 05cc2e1 | 2022-04-25 20:12:25 +0200 | [diff] [blame] | 2025 | } |
| 2026 | #endif |
| 2027 | |
Armin Brauns | b95cdb2 | 2023-05-22 14:29:23 +0000 | [diff] [blame] | 2028 | #if HAS_DRIVER_ENABLE |
Abram Early | a59c948 | 2023-05-03 10:10:21 -0600 | [diff] [blame] | 2029 | if (config->de_enable) { |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 2030 | if (!IS_UART_DRIVER_ENABLE_INSTANCE(usart)) { |
Abram Early | a59c948 | 2023-05-03 10:10:21 -0600 | [diff] [blame] | 2031 | LOG_ERR("%s does not support driver enable", dev->name); |
| 2032 | return -EINVAL; |
| 2033 | } |
| 2034 | |
Armin Brauns | b95cdb2 | 2023-05-22 14:29:23 +0000 | [diff] [blame] | 2035 | uart_stm32_set_driver_enable(dev, true); |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 2036 | LL_USART_SetDEAssertionTime(usart, config->de_assert_time); |
| 2037 | LL_USART_SetDEDeassertionTime(usart, config->de_deassert_time); |
Abram Early | a59c948 | 2023-05-03 10:10:21 -0600 | [diff] [blame] | 2038 | |
| 2039 | if (config->de_invert) { |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 2040 | LL_USART_SetDESignalPolarity(usart, LL_USART_DE_POLARITY_LOW); |
Abram Early | a59c948 | 2023-05-03 10:10:21 -0600 | [diff] [blame] | 2041 | } |
| 2042 | } |
| 2043 | #endif |
| 2044 | |
Erwan Gouriou | 0c541d7 | 2023-11-27 11:34:29 +0100 | [diff] [blame] | 2045 | #ifdef USART_CR1_FIFOEN |
| 2046 | if (config->fifo_enable) { |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 2047 | LL_USART_EnableFIFO(usart); |
Erwan Gouriou | 0c541d7 | 2023-11-27 11:34:29 +0100 | [diff] [blame] | 2048 | } |
| 2049 | #endif |
| 2050 | |
Erwan Gouriou | ad1594e | 2023-11-28 14:38:54 +0100 | [diff] [blame] | 2051 | #if defined(CONFIG_PM) && defined(IS_UART_WAKEUP_FROMSTOP_INSTANCE) |
| 2052 | if (config->wakeup_source) { |
| 2053 | /* Enable ability to wakeup device in Stop mode |
| 2054 | * Effect depends on CONFIG_PM_DEVICE status: |
| 2055 | * CONFIG_PM_DEVICE=n : Always active |
| 2056 | * CONFIG_PM_DEVICE=y : Controlled by pm_device_wakeup_enable() |
| 2057 | */ |
| 2058 | #ifdef USART_CR3_WUFIE |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 2059 | LL_USART_SetWKUPType(usart, LL_USART_WAKEUP_ON_RXNE); |
| 2060 | LL_USART_EnableIT_WKUP(usart); |
| 2061 | LL_USART_ClearFlag_WKUP(usart); |
Erwan Gouriou | ad1594e | 2023-11-28 14:38:54 +0100 | [diff] [blame] | 2062 | #endif |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 2063 | LL_USART_EnableInStopMode(usart); |
Erwan Gouriou | ad1594e | 2023-11-28 14:38:54 +0100 | [diff] [blame] | 2064 | |
Mathieu Choplain | b1b85fc | 2024-08-22 16:50:37 +0200 | [diff] [blame] | 2065 | if (config->wakeup_line != STM32_WAKEUP_LINE_NONE) { |
Erwan Gouriou | ad1594e | 2023-11-28 14:38:54 +0100 | [diff] [blame] | 2066 | /* Prepare the WAKEUP with the expected EXTI line */ |
| 2067 | LL_EXTI_EnableIT_0_31(BIT(config->wakeup_line)); |
| 2068 | } |
| 2069 | } |
| 2070 | #endif /* CONFIG_PM */ |
| 2071 | |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 2072 | LL_USART_Enable(usart); |
Erwan Gouriou | da210ba | 2017-09-21 15:20:53 +0200 | [diff] [blame] | 2073 | |
Erwan Gouriou | 80b8c50 | 2018-06-13 11:32:38 +0200 | [diff] [blame] | 2074 | #ifdef USART_ISR_TEACK |
| 2075 | /* Wait until TEACK flag is set */ |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 2076 | while (!(LL_USART_IsActiveFlag_TEACK(usart))) { |
Anas Nashif | 4c32258 | 2019-06-04 10:52:23 -0400 | [diff] [blame] | 2077 | } |
Erwan Gouriou | 80b8c50 | 2018-06-13 11:32:38 +0200 | [diff] [blame] | 2078 | #endif /* !USART_ISR_TEACK */ |
| 2079 | |
| 2080 | #ifdef USART_ISR_REACK |
Erwan Gouriou | 13a9657 | 2018-06-18 18:01:06 +0200 | [diff] [blame] | 2081 | /* Wait until REACK flag is set */ |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 2082 | while (!(LL_USART_IsActiveFlag_REACK(usart))) { |
Anas Nashif | 4c32258 | 2019-06-04 10:52:23 -0400 | [diff] [blame] | 2083 | } |
Erwan Gouriou | 80b8c50 | 2018-06-13 11:32:38 +0200 | [diff] [blame] | 2084 | #endif /* !USART_ISR_REACK */ |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 2085 | |
Kenneth J. Miller | 31a6e31 | 2023-06-20 01:28:09 +0200 | [diff] [blame] | 2086 | return 0; |
| 2087 | } |
| 2088 | |
| 2089 | /** |
| 2090 | * @brief Initialize UART channel |
| 2091 | * |
| 2092 | * This routine is called to reset the chip in a quiescent state. |
| 2093 | * It is assumed that this function is called only once per UART. |
| 2094 | * |
| 2095 | * @param dev UART device struct |
| 2096 | * |
| 2097 | * @return 0 |
| 2098 | */ |
| 2099 | static int uart_stm32_init(const struct device *dev) |
| 2100 | { |
| 2101 | const struct uart_stm32_config *config = dev->config; |
| 2102 | int err; |
| 2103 | |
| 2104 | err = uart_stm32_clocks_enable(dev); |
| 2105 | if (err < 0) { |
| 2106 | return err; |
| 2107 | } |
| 2108 | |
| 2109 | /* Configure dt provided device signals when available */ |
| 2110 | err = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT); |
| 2111 | if (err < 0) { |
| 2112 | return err; |
| 2113 | } |
| 2114 | |
| 2115 | err = uart_stm32_registers_configure(dev); |
| 2116 | if (err < 0) { |
| 2117 | return err; |
| 2118 | } |
| 2119 | |
Gerard Marull-Paretas | e101cc7 | 2022-01-25 16:21:36 +0100 | [diff] [blame] | 2120 | #if defined(CONFIG_PM) || \ |
| 2121 | defined(CONFIG_UART_INTERRUPT_DRIVEN) || \ |
| 2122 | defined(CONFIG_UART_ASYNC_API) |
Gerard Marull-Paretas | e3f4907 | 2021-09-09 22:41:35 +0200 | [diff] [blame] | 2123 | config->irq_config_func(dev); |
Gerard Marull-Paretas | e101cc7 | 2022-01-25 16:21:36 +0100 | [diff] [blame] | 2124 | #endif /* CONFIG_PM || CONFIG_UART_INTERRUPT_DRIVEN || CONFIG_UART_ASYNC_API */ |
Gerard Marull-Paretas | e3f4907 | 2021-09-09 22:41:35 +0200 | [diff] [blame] | 2125 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 2126 | #ifdef CONFIG_UART_ASYNC_API |
| 2127 | return uart_stm32_async_init(dev); |
| 2128 | #else |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 2129 | return 0; |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 2130 | #endif |
Maciek Borzecki | 5a73ca6 | 2016-03-03 15:33:20 +0100 | [diff] [blame] | 2131 | } |
| 2132 | |
Erwan Gouriou | a439c04 | 2022-08-03 14:39:59 +0200 | [diff] [blame] | 2133 | #ifdef CONFIG_PM_DEVICE |
| 2134 | static void uart_stm32_suspend_setup(const struct device *dev) |
| 2135 | { |
| 2136 | const struct uart_stm32_config *config = dev->config; |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 2137 | USART_TypeDef *usart = config->usart; |
Erwan Gouriou | a439c04 | 2022-08-03 14:39:59 +0200 | [diff] [blame] | 2138 | |
| 2139 | #ifdef USART_ISR_BUSY |
| 2140 | /* Make sure that no USART transfer is on-going */ |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 2141 | while (LL_USART_IsActiveFlag_BUSY(usart) == 1) { |
Erwan Gouriou | a439c04 | 2022-08-03 14:39:59 +0200 | [diff] [blame] | 2142 | } |
| 2143 | #endif |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 2144 | while (LL_USART_IsActiveFlag_TC(usart) == 0) { |
Erwan Gouriou | a439c04 | 2022-08-03 14:39:59 +0200 | [diff] [blame] | 2145 | } |
| 2146 | #ifdef USART_ISR_REACK |
| 2147 | /* Make sure that USART is ready for reception */ |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 2148 | while (LL_USART_IsActiveFlag_REACK(usart) == 0) { |
Erwan Gouriou | a439c04 | 2022-08-03 14:39:59 +0200 | [diff] [blame] | 2149 | } |
| 2150 | #endif |
| 2151 | /* Clear OVERRUN flag */ |
Pisit Sawangvonganan | 86e0fe1 | 2024-05-24 13:09:35 +0700 | [diff] [blame] | 2152 | LL_USART_ClearFlag_ORE(usart); |
Erwan Gouriou | a439c04 | 2022-08-03 14:39:59 +0200 | [diff] [blame] | 2153 | } |
| 2154 | |
| 2155 | static int uart_stm32_pm_action(const struct device *dev, |
| 2156 | enum pm_device_action action) |
| 2157 | { |
| 2158 | const struct uart_stm32_config *config = dev->config; |
| 2159 | struct uart_stm32_data *data = dev->data; |
| 2160 | int err; |
| 2161 | |
| 2162 | |
| 2163 | switch (action) { |
| 2164 | case PM_DEVICE_ACTION_RESUME: |
Guillaume Gautier | c1327db | 2024-01-29 16:56:02 +0100 | [diff] [blame] | 2165 | /* Set pins to active state */ |
| 2166 | err = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT); |
| 2167 | if (err < 0) { |
| 2168 | return err; |
| 2169 | } |
Erwan Gouriou | a439c04 | 2022-08-03 14:39:59 +0200 | [diff] [blame] | 2170 | |
Guillaume Gautier | c1327db | 2024-01-29 16:56:02 +0100 | [diff] [blame] | 2171 | /* Enable clock */ |
| 2172 | err = clock_control_on(data->clock, |
| 2173 | (clock_control_subsys_t)&config->pclken[0]); |
| 2174 | if (err < 0) { |
| 2175 | LOG_ERR("Could not enable (LP)UART clock"); |
| 2176 | return err; |
| 2177 | } |
| 2178 | |
| 2179 | if ((IS_ENABLED(CONFIG_PM_S2RAM)) && |
| 2180 | (!LL_USART_IsEnabled(config->usart))) { |
| 2181 | /* When exiting low power mode, check whether UART is enabled. |
| 2182 | * If not, it means we are exiting Suspend to RAM mode (STM32 |
| 2183 | * Standby), and the driver needs to be reinitialized. |
| 2184 | */ |
Guillaume Gautier | 0792a85 | 2023-12-08 14:11:53 +0100 | [diff] [blame] | 2185 | uart_stm32_init(dev); |
Erwan Gouriou | a439c04 | 2022-08-03 14:39:59 +0200 | [diff] [blame] | 2186 | } |
| 2187 | break; |
| 2188 | case PM_DEVICE_ACTION_SUSPEND: |
| 2189 | uart_stm32_suspend_setup(dev); |
Erwan Gouriou | e89e4b5 | 2022-08-03 10:47:34 +0200 | [diff] [blame] | 2190 | /* Stop device clock. Note: fixed clocks are not handled yet. */ |
Erwan Gouriou | a439c04 | 2022-08-03 14:39:59 +0200 | [diff] [blame] | 2191 | err = clock_control_off(data->clock, (clock_control_subsys_t)&config->pclken[0]); |
Guillaume Gautier | c1327db | 2024-01-29 16:56:02 +0100 | [diff] [blame] | 2192 | if (err < 0) { |
Erwan Gouriou | a439c04 | 2022-08-03 14:39:59 +0200 | [diff] [blame] | 2193 | LOG_ERR("Could not enable (LP)UART clock"); |
| 2194 | return err; |
| 2195 | } |
| 2196 | |
| 2197 | /* Move pins to sleep state */ |
| 2198 | err = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_SLEEP); |
Francois Ramu | 7dff172 | 2023-02-03 15:10:56 +0100 | [diff] [blame] | 2199 | if ((err < 0) && (err != -ENOENT)) { |
| 2200 | /* |
| 2201 | * If returning -ENOENT, no pins where defined for sleep mode : |
| 2202 | * Do not output on console (might sleep already) when going to sleep, |
| 2203 | * "(LP)UART pinctrl sleep state not available" |
| 2204 | * and don't block PM suspend. |
| 2205 | * Else return the error. |
| 2206 | */ |
Erwan Gouriou | a439c04 | 2022-08-03 14:39:59 +0200 | [diff] [blame] | 2207 | return err; |
| 2208 | } |
| 2209 | break; |
| 2210 | default: |
| 2211 | return -ENOTSUP; |
| 2212 | } |
| 2213 | |
| 2214 | return 0; |
| 2215 | } |
| 2216 | #endif /* CONFIG_PM_DEVICE */ |
| 2217 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 2218 | #ifdef CONFIG_UART_ASYNC_API |
Florian Vaussard | f27a5a3 | 2017-05-01 15:21:52 +0200 | [diff] [blame] | 2219 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 2220 | /* src_dev and dest_dev should be 'MEMORY' or 'PERIPHERAL'. */ |
| 2221 | #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] | 2222 | .dma_dev = DEVICE_DT_GET(STM32_DMA_CTLR(index, dir)), \ |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 2223 | .dma_channel = DT_INST_DMAS_CELL_BY_NAME(index, dir, channel), \ |
| 2224 | .dma_cfg = { \ |
Francois Ramu | 744e1dc | 2021-08-09 16:32:57 +0200 | [diff] [blame] | 2225 | .dma_slot = STM32_DMA_SLOT(index, dir, slot),\ |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 2226 | .channel_direction = STM32_DMA_CONFIG_DIRECTION( \ |
Francois Ramu | 30fd022 | 2021-06-18 15:52:30 +0200 | [diff] [blame] | 2227 | STM32_DMA_CHANNEL_CONFIG(index, dir)),\ |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 2228 | .channel_priority = STM32_DMA_CONFIG_PRIORITY( \ |
Francois Ramu | 30fd022 | 2021-06-18 15:52:30 +0200 | [diff] [blame] | 2229 | STM32_DMA_CHANNEL_CONFIG(index, dir)), \ |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 2230 | .source_data_size = STM32_DMA_CONFIG_##src_dev##_DATA_SIZE(\ |
Francois Ramu | 30fd022 | 2021-06-18 15:52:30 +0200 | [diff] [blame] | 2231 | STM32_DMA_CHANNEL_CONFIG(index, dir)),\ |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 2232 | .dest_data_size = STM32_DMA_CONFIG_##dest_dev##_DATA_SIZE(\ |
Francois Ramu | 30fd022 | 2021-06-18 15:52:30 +0200 | [diff] [blame] | 2233 | STM32_DMA_CHANNEL_CONFIG(index, dir)),\ |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 2234 | .source_burst_length = 1, /* SINGLE transfer */ \ |
| 2235 | .dest_burst_length = 1, \ |
| 2236 | .block_count = 1, \ |
| 2237 | .dma_callback = uart_stm32_dma_##dir##_cb, \ |
| 2238 | }, \ |
| 2239 | .src_addr_increment = STM32_DMA_CONFIG_##src_dev##_ADDR_INC( \ |
Francois Ramu | 30fd022 | 2021-06-18 15:52:30 +0200 | [diff] [blame] | 2240 | STM32_DMA_CHANNEL_CONFIG(index, dir)), \ |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 2241 | .dst_addr_increment = STM32_DMA_CONFIG_##dest_dev##_ADDR_INC( \ |
Francois Ramu | 30fd022 | 2021-06-18 15:52:30 +0200 | [diff] [blame] | 2242 | STM32_DMA_CHANNEL_CONFIG(index, dir)), \ |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 2243 | .fifo_threshold = STM32_DMA_FEATURES_FIFO_THRESHOLD( \ |
Francois Ramu | 30fd022 | 2021-06-18 15:52:30 +0200 | [diff] [blame] | 2244 | STM32_DMA_FEATURES(index, dir)), \ |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 2245 | |
| 2246 | #endif |
| 2247 | |
Gerard Marull-Paretas | e3f4907 | 2021-09-09 22:41:35 +0200 | [diff] [blame] | 2248 | #if defined(CONFIG_UART_INTERRUPT_DRIVEN) || defined(CONFIG_UART_ASYNC_API) || \ |
| 2249 | defined(CONFIG_PM) |
Erwan Gouriou | 6275513 | 2020-02-28 14:54:37 +0100 | [diff] [blame] | 2250 | #define STM32_UART_IRQ_HANDLER_DECL(index) \ |
Gerard Marull-Paretas | e3f4907 | 2021-09-09 22:41:35 +0200 | [diff] [blame] | 2251 | static void uart_stm32_irq_config_func_##index(const struct device *dev); |
Erwan Gouriou | 6275513 | 2020-02-28 14:54:37 +0100 | [diff] [blame] | 2252 | #define STM32_UART_IRQ_HANDLER(index) \ |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 2253 | static void uart_stm32_irq_config_func_##index(const struct device *dev) \ |
Florian Vaussard | f27a5a3 | 2017-05-01 15:21:52 +0200 | [diff] [blame] | 2254 | { \ |
Erwan Gouriou | ccd6b72 | 2020-06-05 09:48:29 +0200 | [diff] [blame] | 2255 | IRQ_CONNECT(DT_INST_IRQN(index), \ |
| 2256 | DT_INST_IRQ(index, priority), \ |
Kumar Gala | c49b162 | 2020-12-11 10:12:30 -0600 | [diff] [blame] | 2257 | uart_stm32_isr, DEVICE_DT_INST_GET(index), \ |
Florian Vaussard | f27a5a3 | 2017-05-01 15:21:52 +0200 | [diff] [blame] | 2258 | 0); \ |
Erwan Gouriou | ccd6b72 | 2020-06-05 09:48:29 +0200 | [diff] [blame] | 2259 | irq_enable(DT_INST_IRQN(index)); \ |
Florian Vaussard | f27a5a3 | 2017-05-01 15:21:52 +0200 | [diff] [blame] | 2260 | } |
| 2261 | #else |
Gerard Marull-Paretas | e3f4907 | 2021-09-09 22:41:35 +0200 | [diff] [blame] | 2262 | #define STM32_UART_IRQ_HANDLER_DECL(index) /* Not used */ |
| 2263 | #define STM32_UART_IRQ_HANDLER(index) /* Not used */ |
| 2264 | #endif |
| 2265 | |
Gerard Marull-Paretas | e101cc7 | 2022-01-25 16:21:36 +0100 | [diff] [blame] | 2266 | #if defined(CONFIG_UART_INTERRUPT_DRIVEN) || defined(CONFIG_UART_ASYNC_API) || \ |
| 2267 | defined(CONFIG_PM) |
Gerard Marull-Paretas | e3f4907 | 2021-09-09 22:41:35 +0200 | [diff] [blame] | 2268 | #define STM32_UART_IRQ_HANDLER_FUNC(index) \ |
| 2269 | .irq_config_func = uart_stm32_irq_config_func_##index, |
Gerard Marull-Paretas | e3f4907 | 2021-09-09 22:41:35 +0200 | [diff] [blame] | 2270 | #else |
| 2271 | #define STM32_UART_IRQ_HANDLER_FUNC(index) /* Not used */ |
Florian Vaussard | f27a5a3 | 2017-05-01 15:21:52 +0200 | [diff] [blame] | 2272 | #endif |
| 2273 | |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 2274 | #ifdef CONFIG_UART_ASYNC_API |
| 2275 | #define UART_DMA_CHANNEL(index, dir, DIR, src, dest) \ |
Artur Lipowski | bb85759 | 2022-05-10 12:18:28 +0200 | [diff] [blame] | 2276 | .dma_##dir = { \ |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 2277 | COND_CODE_1(DT_INST_DMAS_HAS_NAME(index, dir), \ |
| 2278 | (UART_DMA_CHANNEL_INIT(index, dir, DIR, src, dest)), \ |
Artur Lipowski | bb85759 | 2022-05-10 12:18:28 +0200 | [diff] [blame] | 2279 | (NULL)) \ |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 2280 | }, |
| 2281 | |
| 2282 | #else |
| 2283 | #define UART_DMA_CHANNEL(index, dir, DIR, src, dest) |
| 2284 | #endif |
| 2285 | |
Erwan Gouriou | 4422de5 | 2022-08-03 14:39:48 +0200 | [diff] [blame] | 2286 | #ifdef CONFIG_PM |
Francois Ramu | 0e119b5 | 2022-09-12 16:22:54 +0200 | [diff] [blame] | 2287 | #define STM32_UART_PM_WAKEUP(index) \ |
| 2288 | .wakeup_source = DT_INST_PROP(index, wakeup_source), \ |
| 2289 | .wakeup_line = COND_CODE_1(DT_INST_NODE_HAS_PROP(index, wakeup_line), \ |
| 2290 | (DT_INST_PROP(index, wakeup_line)), \ |
Mathieu Choplain | b1b85fc | 2024-08-22 16:50:37 +0200 | [diff] [blame] | 2291 | (STM32_WAKEUP_LINE_NONE)), |
Erwan Gouriou | 4422de5 | 2022-08-03 14:39:48 +0200 | [diff] [blame] | 2292 | #else |
| 2293 | #define STM32_UART_PM_WAKEUP(index) /* Not used */ |
| 2294 | #endif |
| 2295 | |
Kenneth J. Miller | 044de03 | 2023-06-20 01:45:32 +0200 | [diff] [blame] | 2296 | /* Ensure DTS doesn't present an incompatible parity configuration. |
| 2297 | * Mark/space parity isn't supported on the STM32 family. |
| 2298 | * If 9 data bits are configured, ensure that a parity bit isn't set. |
| 2299 | */ |
| 2300 | #define STM32_UART_CHECK_DT_PARITY(index) \ |
| 2301 | BUILD_ASSERT( \ |
| 2302 | !(DT_INST_ENUM_IDX_OR(index, parity, STM32_UART_DEFAULT_PARITY) \ |
| 2303 | == UART_CFG_PARITY_MARK || \ |
| 2304 | DT_INST_ENUM_IDX_OR(index, parity, STM32_UART_DEFAULT_PARITY) \ |
| 2305 | == UART_CFG_PARITY_SPACE), \ |
| 2306 | "Node " DT_NODE_PATH(DT_DRV_INST(index)) \ |
| 2307 | " has unsupported parity configuration"); \ |
| 2308 | BUILD_ASSERT( \ |
| 2309 | !(DT_INST_ENUM_IDX_OR(index, parity, STM32_UART_DEFAULT_PARITY) \ |
| 2310 | != UART_CFG_PARITY_NONE && \ |
| 2311 | DT_INST_ENUM_IDX_OR(index, data_bits, \ |
| 2312 | STM32_UART_DEFAULT_DATA_BITS) \ |
| 2313 | == UART_CFG_DATA_BITS_9), \ |
| 2314 | "Node " DT_NODE_PATH(DT_DRV_INST(index)) \ |
| 2315 | " has unsupported parity + data bits combination"); |
| 2316 | |
| 2317 | /* Ensure DTS doesn't present an incompatible data bits configuration |
| 2318 | * The STM32 family doesn't support 5 data bits, or 6 data bits without parity. |
| 2319 | * Only some series support 7 data bits. |
| 2320 | */ |
| 2321 | #ifdef LL_USART_DATAWIDTH_7B |
| 2322 | #define STM32_UART_CHECK_DT_DATA_BITS(index) \ |
| 2323 | BUILD_ASSERT( \ |
| 2324 | !(DT_INST_ENUM_IDX_OR(index, data_bits, \ |
| 2325 | STM32_UART_DEFAULT_DATA_BITS) \ |
| 2326 | == UART_CFG_DATA_BITS_5 || \ |
| 2327 | (DT_INST_ENUM_IDX_OR(index, data_bits, \ |
| 2328 | STM32_UART_DEFAULT_DATA_BITS) \ |
| 2329 | == UART_CFG_DATA_BITS_6 && \ |
| 2330 | DT_INST_ENUM_IDX_OR(index, parity, \ |
| 2331 | STM32_UART_DEFAULT_PARITY) \ |
| 2332 | == UART_CFG_PARITY_NONE)), \ |
| 2333 | "Node " DT_NODE_PATH(DT_DRV_INST(index)) \ |
| 2334 | " has unsupported data bits configuration"); |
| 2335 | #else |
| 2336 | #define STM32_UART_CHECK_DT_DATA_BITS(index) \ |
| 2337 | BUILD_ASSERT( \ |
| 2338 | !(DT_INST_ENUM_IDX_OR(index, data_bits, \ |
| 2339 | STM32_UART_DEFAULT_DATA_BITS) \ |
| 2340 | == UART_CFG_DATA_BITS_5 || \ |
| 2341 | DT_INST_ENUM_IDX_OR(index, data_bits, \ |
| 2342 | STM32_UART_DEFAULT_DATA_BITS) \ |
| 2343 | == UART_CFG_DATA_BITS_6 || \ |
| 2344 | (DT_INST_ENUM_IDX_OR(index, data_bits, \ |
| 2345 | STM32_UART_DEFAULT_DATA_BITS) \ |
| 2346 | == UART_CFG_DATA_BITS_7 && \ |
| 2347 | DT_INST_ENUM_IDX_OR(index, parity, \ |
| 2348 | STM32_UART_DEFAULT_PARITY) \ |
| 2349 | == UART_CFG_PARITY_NONE)), \ |
| 2350 | "Node " DT_NODE_PATH(DT_DRV_INST(index)) \ |
| 2351 | " has unsupported data bits configuration"); |
| 2352 | #endif |
| 2353 | |
| 2354 | /* Ensure DTS doesn't present an incompatible stop bits configuration. |
| 2355 | * Some STM32 series USARTs don't support 0.5 stop bits, and it generally isn't |
| 2356 | * supported for LPUART. |
| 2357 | */ |
| 2358 | #ifndef LL_USART_STOPBITS_0_5 |
| 2359 | #define STM32_UART_CHECK_DT_STOP_BITS_0_5(index) \ |
| 2360 | BUILD_ASSERT( \ |
| 2361 | !(DT_INST_ENUM_IDX_OR(index, stop_bits, \ |
| 2362 | STM32_UART_DEFAULT_STOP_BITS) \ |
| 2363 | == UART_CFG_STOP_BITS_0_5), \ |
| 2364 | "Node " DT_NODE_PATH(DT_DRV_INST(index)) \ |
| 2365 | " has unsupported stop bits configuration"); |
| 2366 | /* LPUARTs don't support 0.5 stop bits configurations */ |
| 2367 | #else |
| 2368 | #define STM32_UART_CHECK_DT_STOP_BITS_0_5(index) \ |
| 2369 | BUILD_ASSERT( \ |
| 2370 | !(DT_HAS_COMPAT_STATUS_OKAY(st_stm32_lpuart) && \ |
| 2371 | DT_INST_ENUM_IDX_OR(index, stop_bits, \ |
| 2372 | STM32_UART_DEFAULT_STOP_BITS) \ |
| 2373 | == UART_CFG_STOP_BITS_0_5), \ |
| 2374 | "Node " DT_NODE_PATH(DT_DRV_INST(index)) \ |
| 2375 | " has unsupported stop bits configuration"); |
| 2376 | #endif |
| 2377 | |
| 2378 | /* Ensure DTS doesn't present an incompatible stop bits configuration. |
| 2379 | * Some STM32 series USARTs don't support 1.5 stop bits, and it generally isn't |
| 2380 | * supported for LPUART. |
| 2381 | */ |
| 2382 | #ifndef LL_USART_STOPBITS_1_5 |
| 2383 | #define STM32_UART_CHECK_DT_STOP_BITS_1_5(index) \ |
| 2384 | BUILD_ASSERT( \ |
| 2385 | DT_INST_ENUM_IDX_OR(index, stop_bits, \ |
| 2386 | STM32_UART_DEFAULT_STOP_BITS) \ |
| 2387 | != UART_CFG_STOP_BITS_1_5, \ |
| 2388 | "Node " DT_NODE_PATH(DT_DRV_INST(index)) \ |
| 2389 | " has unsupported stop bits configuration"); |
| 2390 | /* LPUARTs don't support 1.5 stop bits configurations */ |
| 2391 | #else |
| 2392 | #define STM32_UART_CHECK_DT_STOP_BITS_1_5(index) \ |
| 2393 | BUILD_ASSERT( \ |
| 2394 | !(DT_HAS_COMPAT_STATUS_OKAY(st_stm32_lpuart) && \ |
| 2395 | DT_INST_ENUM_IDX_OR(index, stop_bits, \ |
| 2396 | STM32_UART_DEFAULT_STOP_BITS) \ |
| 2397 | == UART_CFG_STOP_BITS_1_5), \ |
| 2398 | "Node " DT_NODE_PATH(DT_DRV_INST(index)) \ |
| 2399 | " has unsupported stop bits configuration"); |
| 2400 | #endif |
| 2401 | |
Erwan Gouriou | 6275513 | 2020-02-28 14:54:37 +0100 | [diff] [blame] | 2402 | #define STM32_UART_INIT(index) \ |
Gerard Marull-Paretas | e3f4907 | 2021-09-09 22:41:35 +0200 | [diff] [blame] | 2403 | STM32_UART_IRQ_HANDLER_DECL(index) \ |
Florian Vaussard | f27a5a3 | 2017-05-01 15:21:52 +0200 | [diff] [blame] | 2404 | \ |
Gerard Marull-Paretas | 5dc6ed3 | 2021-12-23 12:33:03 +0100 | [diff] [blame] | 2405 | PINCTRL_DT_INST_DEFINE(index); \ |
Erwan Gouriou | 252a623 | 2020-06-05 10:57:52 +0200 | [diff] [blame] | 2406 | \ |
Artur Lipowski | bb85759 | 2022-05-10 12:18:28 +0200 | [diff] [blame] | 2407 | static const struct stm32_pclken pclken_##index[] = \ |
| 2408 | STM32_DT_INST_CLOCKS(index);\ |
| 2409 | \ |
Kenneth J. Miller | c8ffeb4 | 2023-06-20 01:16:42 +0200 | [diff] [blame] | 2410 | static struct uart_config uart_cfg_##index = { \ |
| 2411 | .baudrate = DT_INST_PROP_OR(index, current_speed, \ |
| 2412 | STM32_UART_DEFAULT_BAUDRATE), \ |
| 2413 | .parity = DT_INST_ENUM_IDX_OR(index, parity, \ |
| 2414 | STM32_UART_DEFAULT_PARITY), \ |
| 2415 | .stop_bits = DT_INST_ENUM_IDX_OR(index, stop_bits, \ |
| 2416 | STM32_UART_DEFAULT_STOP_BITS), \ |
| 2417 | .data_bits = DT_INST_ENUM_IDX_OR(index, data_bits, \ |
| 2418 | STM32_UART_DEFAULT_DATA_BITS), \ |
| 2419 | .flow_ctrl = DT_INST_PROP(index, hw_flow_control) \ |
| 2420 | ? UART_CFG_FLOW_CTRL_RTS_CTS \ |
| 2421 | : UART_CFG_FLOW_CTRL_NONE, \ |
| 2422 | }; \ |
| 2423 | \ |
Erwan Gouriou | 6275513 | 2020-02-28 14:54:37 +0100 | [diff] [blame] | 2424 | static const struct uart_stm32_config uart_stm32_cfg_##index = { \ |
Gerard Marull-Paretas | 32a3a02 | 2022-01-25 16:14:12 +0100 | [diff] [blame] | 2425 | .usart = (USART_TypeDef *)DT_INST_REG_ADDR(index), \ |
Kenneth J. Miller | bde1cd8 | 2023-06-23 04:24:49 +0200 | [diff] [blame] | 2426 | .reset = RESET_DT_SPEC_GET(DT_DRV_INST(index)), \ |
Artur Lipowski | bb85759 | 2022-05-10 12:18:28 +0200 | [diff] [blame] | 2427 | .pclken = pclken_##index, \ |
| 2428 | .pclk_len = DT_INST_NUM_CLOCKS(index), \ |
Gerard Marull-Paretas | 21a2719 | 2021-09-07 16:39:45 +0200 | [diff] [blame] | 2429 | .pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(index), \ |
Roman Studenikin | 260fc89 | 2024-01-24 16:06:47 +0000 | [diff] [blame] | 2430 | .single_wire = DT_INST_PROP(index, single_wire), \ |
| 2431 | .tx_rx_swap = DT_INST_PROP(index, tx_rx_swap), \ |
Erwan Gouriou | 4422de5 | 2022-08-03 14:39:48 +0200 | [diff] [blame] | 2432 | .rx_invert = DT_INST_PROP(index, rx_invert), \ |
| 2433 | .tx_invert = DT_INST_PROP(index, tx_invert), \ |
Abram Early | a59c948 | 2023-05-03 10:10:21 -0600 | [diff] [blame] | 2434 | .de_enable = DT_INST_PROP(index, de_enable), \ |
| 2435 | .de_assert_time = DT_INST_PROP(index, de_assert_time), \ |
| 2436 | .de_deassert_time = DT_INST_PROP(index, de_deassert_time), \ |
| 2437 | .de_invert = DT_INST_PROP(index, de_invert), \ |
Erwan Gouriou | 0c541d7 | 2023-11-27 11:34:29 +0100 | [diff] [blame] | 2438 | .fifo_enable = DT_INST_PROP(index, fifo_enable), \ |
Gerard Marull-Paretas | e101cc7 | 2022-01-25 16:21:36 +0100 | [diff] [blame] | 2439 | STM32_UART_IRQ_HANDLER_FUNC(index) \ |
Erwan Gouriou | 4422de5 | 2022-08-03 14:39:48 +0200 | [diff] [blame] | 2440 | STM32_UART_PM_WAKEUP(index) \ |
Florian Vaussard | f27a5a3 | 2017-05-01 15:21:52 +0200 | [diff] [blame] | 2441 | }; \ |
| 2442 | \ |
Erwan Gouriou | 6275513 | 2020-02-28 14:54:37 +0100 | [diff] [blame] | 2443 | static struct uart_stm32_data uart_stm32_data_##index = { \ |
Kenneth J. Miller | c8ffeb4 | 2023-06-20 01:16:42 +0200 | [diff] [blame] | 2444 | .uart_cfg = &uart_cfg_##index, \ |
Shlomi Vaknin | b4afd1a | 2021-01-16 18:44:24 +0200 | [diff] [blame] | 2445 | UART_DMA_CHANNEL(index, rx, RX, PERIPHERAL, MEMORY) \ |
| 2446 | UART_DMA_CHANNEL(index, tx, TX, MEMORY, PERIPHERAL) \ |
Florian Vaussard | f27a5a3 | 2017-05-01 15:21:52 +0200 | [diff] [blame] | 2447 | }; \ |
| 2448 | \ |
Pisit Sawangvonganan | a2e0b1d | 2024-07-06 18:43:11 +0700 | [diff] [blame] | 2449 | PM_DEVICE_DT_INST_DEFINE(index, uart_stm32_pm_action); \ |
Erwan Gouriou | a439c04 | 2022-08-03 14:39:59 +0200 | [diff] [blame] | 2450 | \ |
Kumar Gala | c49b162 | 2020-12-11 10:12:30 -0600 | [diff] [blame] | 2451 | DEVICE_DT_INST_DEFINE(index, \ |
Pisit Sawangvonganan | a2e0b1d | 2024-07-06 18:43:11 +0700 | [diff] [blame] | 2452 | uart_stm32_init, \ |
Erwan Gouriou | a439c04 | 2022-08-03 14:39:59 +0200 | [diff] [blame] | 2453 | PM_DEVICE_DT_INST_GET(index), \ |
Erwan Gouriou | 6275513 | 2020-02-28 14:54:37 +0100 | [diff] [blame] | 2454 | &uart_stm32_data_##index, &uart_stm32_cfg_##index, \ |
Maureen Helm | ad14505 | 2021-10-14 09:38:10 -0500 | [diff] [blame] | 2455 | PRE_KERNEL_1, CONFIG_SERIAL_INIT_PRIORITY, \ |
Florian Vaussard | f27a5a3 | 2017-05-01 15:21:52 +0200 | [diff] [blame] | 2456 | &uart_stm32_driver_api); \ |
| 2457 | \ |
Kenneth J. Miller | 044de03 | 2023-06-20 01:45:32 +0200 | [diff] [blame] | 2458 | STM32_UART_IRQ_HANDLER(index) \ |
| 2459 | \ |
| 2460 | STM32_UART_CHECK_DT_PARITY(index) \ |
| 2461 | STM32_UART_CHECK_DT_DATA_BITS(index) \ |
| 2462 | STM32_UART_CHECK_DT_STOP_BITS_0_5(index) \ |
| 2463 | STM32_UART_CHECK_DT_STOP_BITS_1_5(index) |
Erwan Gouriou | 8c079e9 | 2016-11-14 11:53:52 +0100 | [diff] [blame] | 2464 | |
Martí Bolívar | 7e0eed9 | 2020-05-06 11:23:07 -0700 | [diff] [blame] | 2465 | DT_INST_FOREACH_STATUS_OKAY(STM32_UART_INIT) |