uart.c
Go to the documentation of this file.
1 
33 /*
34  * Support and FAQ: visit <a href="https://www.microchip.com/support/">Microchip Support</a>
35  */
36 
37 #include "uart.h"
38 
40 
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
45 
81 uint32_t uart_init(Uart *p_uart, const sam_uart_opt_t *p_uart_opt)
82 {
83  uint32_t cd = 0;
84 
85  /* Reset and disable receiver & transmitter */
88 
89  /* Check and configure baudrate */
90  /* Asynchronous, no oversampling */
91  cd = (p_uart_opt->ul_mck / p_uart_opt->ul_baudrate) / UART_MCK_DIV;
92  if (cd < UART_MCK_DIV_MIN_FACTOR || cd > UART_MCK_DIV_MAX_FACTOR)
93  return 1;
94 
95  p_uart->UART_BRGR = cd;
96  /* Configure mode */
97  p_uart->UART_MR = p_uart_opt->ul_mode;
98 
99 #if (!SAMV71 && !SAMV70 && !SAME70 && !SAMS70)
100  /* Disable PDC channel */
101  p_uart->UART_PTCR = UART_PTCR_RXTDIS | UART_PTCR_TXTDIS;
102 #endif
103 
104  /* Enable receiver and transmitter */
105  p_uart->UART_CR = UART_CR_RXEN | UART_CR_TXEN;
106 
107  return 0;
108 }
109 
115 void uart_enable_tx(Uart *p_uart)
116 {
117  /* Enable transmitter */
118  p_uart->UART_CR = UART_CR_TXEN;
119 }
120 
126 void uart_disable_tx(Uart *p_uart)
127 {
128  /* Disable transmitter */
129  p_uart->UART_CR = UART_CR_TXDIS;
130 }
131 
137 void uart_reset_tx(Uart *p_uart)
138 {
139  /* Reset transmitter */
140  p_uart->UART_CR = UART_CR_RSTTX | UART_CR_TXDIS;
141 }
142 
148 void uart_enable_rx(Uart *p_uart)
149 {
150  /* Enable receiver */
151  p_uart->UART_CR = UART_CR_RXEN;
152 }
153 
159 void uart_disable_rx(Uart *p_uart)
160 {
161  /* Disable receiver */
162  p_uart->UART_CR = UART_CR_RXDIS;
163 }
164 
170 void uart_reset_rx(Uart *p_uart)
171 {
172  /* Reset receiver */
173  p_uart->UART_CR = UART_CR_RSTRX | UART_CR_RXDIS;
174 }
175 
181 void uart_enable(Uart *p_uart)
182 {
183  /* Enable receiver and transmitter */
184  p_uart->UART_CR = UART_CR_RXEN | UART_CR_TXEN;
185 }
186 
192 void uart_disable(Uart *p_uart)
193 {
194  /* Disable receiver and transmitter */
195  p_uart->UART_CR = UART_CR_RXDIS | UART_CR_TXDIS;
196 }
197 
203 void uart_reset(Uart *p_uart)
204 {
205  /* Reset and disable receiver & transmitter */
208 }
209 
215 void uart_enable_interrupt(Uart *p_uart, uint32_t ul_sources)
216 {
217  p_uart->UART_IER = ul_sources;
218 }
219 
225 void uart_disable_interrupt(Uart *p_uart, uint32_t ul_sources)
226 {
227  p_uart->UART_IDR = ul_sources;
228 }
229 
236 uint32_t uart_get_interrupt_mask(Uart *p_uart)
237 {
238  return p_uart->UART_IMR;
239 }
240 
248 uint32_t uart_get_status(Uart *p_uart)
249 {
250  return p_uart->UART_SR;
251 }
252 
258 void uart_reset_status(Uart *p_uart)
259 {
260  p_uart->UART_CR = UART_CR_RSTSTA;
261 }
262 
273 uint32_t uart_is_tx_ready(Uart *p_uart)
274 {
275  return (p_uart->UART_SR & UART_SR_TXRDY) > 0;
276 }
277 
288 uint32_t uart_is_tx_empty(Uart *p_uart)
289 {
290  return (p_uart->UART_SR & UART_SR_TXEMPTY) > 0;
291 }
292 
302 uint32_t uart_is_rx_ready(Uart *p_uart)
303 {
304  return (p_uart->UART_SR & UART_SR_RXRDY) > 0;
305 }
306 
315 uint32_t uart_is_tx_buf_empty(Uart *p_uart)
316 {
317  return (p_uart->UART_SR & UART_SR_TXEMPTY) > 0;
318 }
319 
327 void uart_set_clock_divisor(Uart *p_uart, uint16_t us_divisor)
328 {
329  p_uart->UART_BRGR = us_divisor;
330 }
331 
342 uint32_t uart_write(Uart *p_uart, const uint8_t uc_data)
343 {
344  /* Check if the transmitter is ready */
345  if (!(p_uart->UART_SR & UART_SR_TXRDY))
346  return 1;
347 
348  /* Send character */
349  p_uart->UART_THR = uc_data;
350  return 0;
351 }
352 
362 uint32_t uart_read(Uart *p_uart, uint8_t *puc_data)
363 {
364  /* Check if the receiver is ready */
365  if ((p_uart->UART_SR & UART_SR_RXRDY) == 0)
366  return 1;
367 
368  /* Read character */
369  *puc_data = (uint8_t) p_uart->UART_RHR;
370  return 0;
371 }
372 
373 #if (!SAMV71 && !SAMV70 && !SAME70 && !SAMS70)
374 
382 uint32_t uart_is_rx_buf_end(Uart *p_uart)
383 {
384  return (p_uart->UART_SR & UART_SR_ENDRX) > 0;
385 }
386 
395 uint32_t uart_is_tx_buf_end(Uart *p_uart)
396 {
397  return (p_uart->UART_SR & UART_SR_ENDTX) > 0;
398 }
399 
408 uint32_t uart_is_rx_buf_full(Uart *p_uart)
409 {
410  return (p_uart->UART_SR & UART_SR_RXBUFF) > 0;
411 }
412 
420 Pdc *uart_get_pdc_base(Uart *p_uart)
421 {
422  Pdc *p_pdc_base;
423 
424 #if (SAM3S || SAM3N || SAM4S || SAM4E || SAM4N || SAM4C || SAMG || SAM4CP || SAM4CM)
425  if (p_uart == UART0)
426  p_pdc_base = PDC_UART0;
427 #elif (SAM3XA || SAM3U)
428  if (p_uart == UART)
429  p_pdc_base = PDC_UART;
430 #else
431 #error "Unsupported device"
432 #endif
433 
434 #if (SAM3S || SAM4S || SAM4E || SAM4N || SAM4C || SAMG || SAM4CP || SAM4CM)
435  if (p_uart == UART1)
436  p_pdc_base = PDC_UART1;
437 #endif
438 
439 #if (SAM4N)
440  if (p_uart == UART2)
441  p_pdc_base = PDC_UART2;
442 #endif
443 
444  return p_pdc_base;
445 }
446 #endif
447 
448 #if (SAM4C || SAM4CP || SAM4CM)
449 
454 void uart_enable_optical_interface(Uart *p_uart)
455 {
456  Assert(p_uart == UART1);
457  p_uart->UART_MR |= UART_MR_OPT_EN;
458 }
459 
465 void uart_disable_optical_interface(Uart *p_uart)
466 {
467  Assert(p_uart == UART1);
468  p_uart->UART_MR &= ~UART_MR_OPT_EN;
469 }
470 
477 void uart_config_optical_interface(Uart *p_uart,
478  struct uart_config_optical *cfg)
479 {
480  Assert(p_uart == UART1);
481  uint32_t reg = p_uart->UART_MR;
482 
483  reg &= ~(UART_MR_OPT_RXINV | UART_MR_OPT_MDINV | UART_MR_FILTER
484  | UART_MR_OPT_CLKDIV_Msk | UART_MR_OPT_DUTY_Msk
485  | UART_MR_OPT_CMPTH_Msk);
486  reg |= (cfg->rx_inverted ? UART_MR_OPT_RXINV : 0)
487  | (cfg->tx_inverted ? UART_MR_OPT_MDINV : 0)
488  | (cfg->rx_filter ? UART_MR_FILTER : 0)
489  | UART_MR_OPT_CLKDIV(cfg->clk_div)
490  | cfg->duty | cfg->threshold;
491 
492  p_uart->UART_MR = reg;
493 }
494 #endif
495 
496 #if (SAMG53 || SAMG54 || SAMV71 || SAMV70 || SAME70 || SAMS70)
497 
506 void uart_set_sleepwalking(Uart *p_uart, uint8_t ul_low_value,
507  bool cmpmode, bool cmppar, uint8_t ul_high_value)
508 {
509  Assert(ul_low_value <= ul_high_value);
510 
511  uint32_t temp = 0;
512 
513  if (cmpmode) {
515  }
516 
517  if (cmppar) {
518  temp |= UART_CMPR_CMPPAR;
519  }
520 
521  temp |= UART_CMPR_VAL1(ul_low_value);
522 
523  temp |= UART_CMPR_VAL2(ul_high_value);
524 
525  p_uart->UART_CMPR= temp;
526 }
527 
534 void uart_set_write_protection(Uart *p_uart, bool flag)
535 {
536  if (flag) {
538  } else {
540  }
541 }
542 #endif
543 
545 
547 
548 #ifdef __cplusplus
549 }
550 #endif
551 
552 
Pdc * uart_get_pdc_base(Uart *p_uart)
Get UART PDC base address.
Definition: uart.c:420
void uart_reset_rx(Uart *p_uart)
Reset UART receiver.
Definition: uart.c:170
#define UART_CMPR_VAL1(value)
#define UART_CMPR_CMPPAR
(UART_CMPR) Compare Parity
#define UART_WPMR_WPEN
(UART_WPMR) Write Protection Enable
void uart_disable_tx(Uart *p_uart)
Disable UART transmitter.
Definition: uart.c:126
#define UART_SR_TXEMPTY
(UART_SR) Transmitter Empty
__IO uint32_t UART_CMPR
(Uart Offset: 0x0024) Comparison Register
#define UART_CR_RSTTX
(UART_CR) Reset Transmitter
uint32_t ul_baudrate
__IO uint32_t UART_MR
(Uart Offset: 0x0004) Mode Register
uint32_t uart_is_tx_empty(Uart *p_uart)
Check if Transmit Hold Register is empty. Check if the last data written in UART_THR has been loaded ...
Definition: uart.c:288
Universal Asynchronous Receiver Transceiver (UART) driver for SAM.
void uart_set_clock_divisor(Uart *p_uart, uint16_t us_divisor)
Set UART clock divisor value.
Definition: uart.c:327
uint32_t uart_is_tx_buf_end(Uart *p_uart)
Check if one transmit buffer is sent out.
Definition: uart.c:395
Uart hardware registers.
uint32_t uart_read(Uart *p_uart, uint8_t *puc_data)
Read from UART Receive Holding Register. Before reading user should check if rx is ready...
Definition: uart.c:362
#define UART_WPMR_WPKEY_PASSWD
(UART_WPMR) Writing any other value in this field aborts the write operation.Always reads as 0...
__O uint32_t UART_THR
(Uart Offset: 0x001C) Transmit Holding Register
uint32_t uart_is_tx_buf_empty(Uart *p_uart)
Check if both transmit buffers are sent out.
Definition: uart.c:315
#define UART1
(UART1 ) Base Address
Definition: same70j19.h:527
void uart_disable(Uart *p_uart)
Disable UART receiver and transmitter.
Definition: uart.c:192
void uart_enable(Uart *p_uart)
Enable UART receiver and transmitter.
Definition: uart.c:181
uint32_t uart_init(Uart *p_uart, const sam_uart_opt_t *p_uart_opt)
Configure UART with the specified parameters.
Definition: uart.c:81
__O uint32_t UART_IDR
(Uart Offset: 0x000C) Interrupt Disable Register
#define UART_CR_RXEN
(UART_CR) Receiver Enable
__O uint32_t UART_CR
(Uart Offset: 0x0000) Control Register
#define UART_SR_RXRDY
(UART_SR) Receiver Ready
void uart_enable_rx(Uart *p_uart)
Enable UART receiver.
Definition: uart.c:148
__IO uint32_t UART_BRGR
(Uart Offset: 0x0020) Baud Rate Generator Register
#define UART_MR_FILTER
(UART_MR) Receiver Digital Filter
void uart_disable_rx(Uart *p_uart)
Disable UART receiver.
Definition: uart.c:159
void uart_disable_interrupt(Uart *p_uart, uint32_t ul_sources)
Disable UART interrupts.
Definition: uart.c:225
__I uint32_t UART_RHR
(Uart Offset: 0x0018) Receive Holding Register
uint32_t uart_get_interrupt_mask(Uart *p_uart)
Read UART interrupt mask.
Definition: uart.c:236
#define UART_CMPR_CMPMODE_START_CONDITION
(UART_CMPR) Comparison condition must be met to start reception.
uint32_t uart_is_rx_buf_end(Uart *p_uart)
Check if one receive buffer is filled.
Definition: uart.c:382
#define UART_SR_TXRDY
(UART_SR) Transmitter Ready
uint32_t uart_is_rx_ready(Uart *p_uart)
Check if Received data is ready. Check if data has been received and loaded in UART_RHR.
Definition: uart.c:302
#define UART_CR_TXEN
(UART_CR) Transmitter Enable
Option list for UART peripheral initialization.
void uart_reset_status(Uart *p_uart)
Reset status bits.
Definition: uart.c:258
#define UART_CR_RSTSTA
(UART_CR) Reset Status
#define UART_CR_RXDIS
(UART_CR) Receiver Disable
uint32_t uart_get_status(Uart *p_uart)
Get current status.
Definition: uart.c:248
#define UART_CMPR_VAL2(value)
#define UART_MCK_DIV_MAX_FACTOR
#define UART_MCK_DIV
#define UART_CR_TXDIS
(UART_CR) Transmitter Disable
#define UART_CR_RSTRX
(UART_CR) Reset Receiver
__I uint32_t UART_IMR
(Uart Offset: 0x0010) Interrupt Mask Register
uint32_t uart_write(Uart *p_uart, const uint8_t uc_data)
Write to UART Transmit Holding Register Before writing user should check if tx is ready (or empty)...
Definition: uart.c:342
uint32_t uart_is_rx_buf_full(Uart *p_uart)
Check if both receive buffers are full.
Definition: uart.c:408
void uart_reset_tx(Uart *p_uart)
Reset UART transmitter.
Definition: uart.c:137
void uart_enable_tx(Uart *p_uart)
Enable UART transmitter.
Definition: uart.c:115
__I uint32_t UART_SR
(Uart Offset: 0x0014) Status Register
#define UART0
(UART0 ) Base Address
Definition: same70j19.h:525
__IO uint32_t UART_WPMR
(Uart Offset: 0x00E4) Write Protection Mode Register
uint32_t uart_is_tx_ready(Uart *p_uart)
Check if Transmit is Ready. Check if data has been loaded in UART_THR and is waiting to be loaded in ...
Definition: uart.c:273
#define Assert(expr)
This macro is used to test fatal errors.
Definition: compiler.h:196
__O uint32_t UART_IER
(Uart Offset: 0x0008) Interrupt Enable Register
void uart_reset(Uart *p_uart)
Reset UART receiver and transmitter.
Definition: uart.c:203
#define UART2
(UART2 ) Base Address
Definition: same70j19.h:539
void uart_enable_interrupt(Uart *p_uart, uint32_t ul_sources)
Enable UART interrupts.
Definition: uart.c:215


inertial_sense_ros
Author(s):
autogenerated on Sun Feb 28 2021 03:17:58