Lines Matching +full:uart +full:- +full:dev
5 * SPDX-License-Identifier: Apache-2.0
11 * @brief UART driver for Atmel SAM MCU family.
19 #include <zephyr/drivers/uart.h>
26 Uart *regs;
45 static int uart_sam_poll_in(const struct device *dev, unsigned char *c) in uart_sam_poll_in() argument
47 const struct uart_sam_dev_cfg *const cfg = dev->config; in uart_sam_poll_in()
49 Uart * const uart = cfg->regs; in uart_sam_poll_in() local
51 if (!(uart->UART_SR & UART_SR_RXRDY)) { in uart_sam_poll_in()
52 return -1; in uart_sam_poll_in()
56 *c = (unsigned char)uart->UART_RHR; in uart_sam_poll_in()
61 static void uart_sam_poll_out(const struct device *dev, unsigned char c) in uart_sam_poll_out() argument
63 const struct uart_sam_dev_cfg *const cfg = dev->config; in uart_sam_poll_out()
65 Uart * const uart = cfg->regs; in uart_sam_poll_out() local
68 while (!(uart->UART_SR & UART_SR_TXRDY)) { in uart_sam_poll_out()
72 uart->UART_THR = (uint32_t)c; in uart_sam_poll_out()
75 static int uart_sam_err_check(const struct device *dev) in uart_sam_err_check() argument
77 const struct uart_sam_dev_cfg *const cfg = dev->config; in uart_sam_err_check()
79 volatile Uart * const uart = cfg->regs; in uart_sam_err_check() local
82 if (uart->UART_SR & UART_SR_OVRE) { in uart_sam_err_check()
86 if (uart->UART_SR & UART_SR_PARE) { in uart_sam_err_check()
90 if (uart->UART_SR & UART_SR_FRAME) { in uart_sam_err_check()
94 uart->UART_CR = UART_CR_RSTSTA; in uart_sam_err_check()
99 static int uart_sam_baudrate_set(const struct device *dev, uint32_t baudrate) in uart_sam_baudrate_set() argument
101 struct uart_sam_dev_data *const dev_data = dev->data; in uart_sam_baudrate_set()
103 const struct uart_sam_dev_cfg *const cfg = dev->config; in uart_sam_baudrate_set()
105 volatile Uart * const uart = cfg->regs; in uart_sam_baudrate_set() local
117 return -EINVAL; in uart_sam_baudrate_set()
120 uart->UART_BRGR = UART_BRGR_CD(divisor); in uart_sam_baudrate_set()
121 dev_data->baud_rate = baudrate; in uart_sam_baudrate_set()
143 static uint8_t uart_sam_get_parity(const struct device *dev) in uart_sam_get_parity() argument
145 const struct uart_sam_dev_cfg *const cfg = dev->config; in uart_sam_get_parity()
147 volatile Uart * const uart = cfg->regs; in uart_sam_get_parity() local
149 switch (uart->UART_MR & UART_MR_PAR_Msk) { in uart_sam_get_parity()
164 static int uart_sam_configure(const struct device *dev, in uart_sam_configure() argument
169 const struct uart_sam_dev_cfg *const config = dev->config; in uart_sam_configure()
171 volatile Uart * const uart = config->regs; in uart_sam_configure() local
174 if (cfg->stop_bits != UART_CFG_STOP_BITS_1 || in uart_sam_configure()
175 cfg->data_bits != UART_CFG_DATA_BITS_8 || in uart_sam_configure()
176 cfg->flow_ctrl != UART_CFG_FLOW_CTRL_NONE) { in uart_sam_configure()
177 return -ENOTSUP; in uart_sam_configure()
180 /* Reset and disable UART */ in uart_sam_configure()
181 uart->UART_CR = UART_CR_RSTRX | UART_CR_RSTTX in uart_sam_configure()
185 /* baud rate driven by the peripheral clock, UART does not filter in uart_sam_configure()
188 uart->UART_MR = UART_MR_CHMODE_NORMAL in uart_sam_configure()
189 | uart_sam_cfg2sam_parity(cfg->parity); in uart_sam_configure()
192 retval = uart_sam_baudrate_set(dev, cfg->baudrate); in uart_sam_configure()
198 uart->UART_CR = UART_CR_RXEN | UART_CR_TXEN; in uart_sam_configure()
203 static int uart_sam_config_get(const struct device *dev, in uart_sam_config_get() argument
206 struct uart_sam_dev_data *const dev_data = dev->data; in uart_sam_config_get()
208 cfg->baudrate = dev_data->baud_rate; in uart_sam_config_get()
209 cfg->parity = uart_sam_get_parity(dev); in uart_sam_config_get()
211 cfg->stop_bits = UART_CFG_STOP_BITS_1; in uart_sam_config_get()
212 cfg->data_bits = UART_CFG_DATA_BITS_8; in uart_sam_config_get()
213 cfg->flow_ctrl = UART_CFG_FLOW_CTRL_NONE; in uart_sam_config_get()
220 static int uart_sam_fifo_fill(const struct device *dev, in uart_sam_fifo_fill() argument
224 const struct uart_sam_dev_cfg *const cfg = dev->config; in uart_sam_fifo_fill()
226 volatile Uart * const uart = cfg->regs; in uart_sam_fifo_fill() local
229 while ((uart->UART_SR & UART_SR_TXRDY) == 0) { in uart_sam_fifo_fill()
232 uart->UART_THR = *tx_data; in uart_sam_fifo_fill()
237 static int uart_sam_fifo_read(const struct device *dev, uint8_t *rx_data, in uart_sam_fifo_read() argument
240 const struct uart_sam_dev_cfg *const cfg = dev->config; in uart_sam_fifo_read()
242 volatile Uart * const uart = cfg->regs; in uart_sam_fifo_read() local
248 if (uart->UART_SR & UART_SR_RXRDY) { in uart_sam_fifo_read()
249 rx_data[bytes_read] = uart->UART_RHR; in uart_sam_fifo_read()
259 static void uart_sam_irq_tx_enable(const struct device *dev) in uart_sam_irq_tx_enable() argument
261 const struct uart_sam_dev_cfg *const cfg = dev->config; in uart_sam_irq_tx_enable()
263 volatile Uart * const uart = cfg->regs; in uart_sam_irq_tx_enable() local
265 uart->UART_IER = UART_IER_TXRDY; in uart_sam_irq_tx_enable()
268 static void uart_sam_irq_tx_disable(const struct device *dev) in uart_sam_irq_tx_disable() argument
270 const struct uart_sam_dev_cfg *const cfg = dev->config; in uart_sam_irq_tx_disable()
272 volatile Uart * const uart = cfg->regs; in uart_sam_irq_tx_disable() local
274 uart->UART_IDR = UART_IDR_TXRDY; in uart_sam_irq_tx_disable()
277 static int uart_sam_irq_tx_ready(const struct device *dev) in uart_sam_irq_tx_ready() argument
279 const struct uart_sam_dev_cfg *const cfg = dev->config; in uart_sam_irq_tx_ready()
281 volatile Uart * const uart = cfg->regs; in uart_sam_irq_tx_ready() local
286 return (uart->UART_SR & UART_SR_TXRDY && in uart_sam_irq_tx_ready()
287 uart->UART_IMR & UART_IMR_TXRDY); in uart_sam_irq_tx_ready()
290 static void uart_sam_irq_rx_enable(const struct device *dev) in uart_sam_irq_rx_enable() argument
292 const struct uart_sam_dev_cfg *const cfg = dev->config; in uart_sam_irq_rx_enable()
294 volatile Uart * const uart = cfg->regs; in uart_sam_irq_rx_enable() local
296 uart->UART_IER = UART_IER_RXRDY; in uart_sam_irq_rx_enable()
299 static void uart_sam_irq_rx_disable(const struct device *dev) in uart_sam_irq_rx_disable() argument
301 const struct uart_sam_dev_cfg *const cfg = dev->config; in uart_sam_irq_rx_disable()
303 volatile Uart * const uart = cfg->regs; in uart_sam_irq_rx_disable() local
305 uart->UART_IDR = UART_IDR_RXRDY; in uart_sam_irq_rx_disable()
308 static int uart_sam_irq_tx_complete(const struct device *dev) in uart_sam_irq_tx_complete() argument
310 const struct uart_sam_dev_cfg *const cfg = dev->config; in uart_sam_irq_tx_complete()
312 volatile Uart * const uart = cfg->regs; in uart_sam_irq_tx_complete() local
314 return (uart->UART_SR & UART_SR_TXRDY && in uart_sam_irq_tx_complete()
315 uart->UART_IMR & UART_IMR_TXEMPTY); in uart_sam_irq_tx_complete()
318 static int uart_sam_irq_rx_ready(const struct device *dev) in uart_sam_irq_rx_ready() argument
320 const struct uart_sam_dev_cfg *const cfg = dev->config; in uart_sam_irq_rx_ready()
322 volatile Uart * const uart = cfg->regs; in uart_sam_irq_rx_ready() local
324 return (uart->UART_SR & UART_SR_RXRDY); in uart_sam_irq_rx_ready()
327 static void uart_sam_irq_err_enable(const struct device *dev) in uart_sam_irq_err_enable() argument
329 const struct uart_sam_dev_cfg *const cfg = dev->config; in uart_sam_irq_err_enable()
331 volatile Uart * const uart = cfg->regs; in uart_sam_irq_err_enable() local
333 uart->UART_IER = UART_IER_OVRE | UART_IER_FRAME | UART_IER_PARE; in uart_sam_irq_err_enable()
336 static void uart_sam_irq_err_disable(const struct device *dev) in uart_sam_irq_err_disable() argument
338 const struct uart_sam_dev_cfg *const cfg = dev->config; in uart_sam_irq_err_disable()
340 volatile Uart * const uart = cfg->regs; in uart_sam_irq_err_disable() local
342 uart->UART_IDR = UART_IDR_OVRE | UART_IDR_FRAME | UART_IDR_PARE; in uart_sam_irq_err_disable()
345 static int uart_sam_irq_is_pending(const struct device *dev) in uart_sam_irq_is_pending() argument
347 const struct uart_sam_dev_cfg *const cfg = dev->config; in uart_sam_irq_is_pending()
349 volatile Uart * const uart = cfg->regs; in uart_sam_irq_is_pending() local
351 return (uart->UART_IMR & (UART_IMR_TXRDY | UART_IMR_RXRDY)) & in uart_sam_irq_is_pending()
352 (uart->UART_SR & (UART_SR_TXRDY | UART_SR_RXRDY)); in uart_sam_irq_is_pending()
355 static int uart_sam_irq_update(const struct device *dev) in uart_sam_irq_update() argument
357 ARG_UNUSED(dev); in uart_sam_irq_update()
362 static void uart_sam_irq_callback_set(const struct device *dev, in uart_sam_irq_callback_set() argument
366 struct uart_sam_dev_data *const dev_data = dev->data; in uart_sam_irq_callback_set()
368 dev_data->irq_cb = cb; in uart_sam_irq_callback_set()
369 dev_data->irq_cb_data = cb_data; in uart_sam_irq_callback_set()
372 static void uart_sam_isr(const struct device *dev) in uart_sam_isr() argument
374 struct uart_sam_dev_data *const dev_data = dev->data; in uart_sam_isr()
376 if (dev_data->irq_cb) { in uart_sam_isr()
377 dev_data->irq_cb(dev, dev_data->irq_cb_data); in uart_sam_isr()
383 static int uart_sam_init(const struct device *dev) in uart_sam_init() argument
387 const struct uart_sam_dev_cfg *const cfg = dev->config; in uart_sam_init()
389 struct uart_sam_dev_data *const dev_data = dev->data; in uart_sam_init()
391 Uart * const uart = cfg->regs; in uart_sam_init() local
393 /* Enable UART clock in PMC */ in uart_sam_init()
395 (clock_control_subsys_t)&cfg->clock_cfg); in uart_sam_init()
398 retval = pinctrl_apply_state(cfg->pcfg, PINCTRL_STATE_DEFAULT); in uart_sam_init()
404 uart->UART_IDR = 0xFFFFFFFF; in uart_sam_init()
407 cfg->irq_config_func(dev); in uart_sam_init()
411 .baudrate = dev_data->baud_rate, in uart_sam_init()
417 return uart_sam_configure(dev, &uart_config); in uart_sam_init()
420 static DEVICE_API(uart, uart_sam_driver_api) = {
447 static const struct uart_sam_dev_cfg uart##n##_sam_config = { \
448 .regs = (Uart *)DT_INST_REG_ADDR(n), \
458 static void uart##n##_sam_irq_config_func(const struct device *port) \
467 .irq_config_func = uart##n##_sam_irq_config_func
479 static struct uart_sam_dev_data uart##n##_sam_data = { \
483 static const struct uart_sam_dev_cfg uart##n##_sam_config; \
486 NULL, &uart##n##_sam_data, \
487 &uart##n##_sam_config, PRE_KERNEL_1, \