drivers/usart/usart.h
Go to the documentation of this file.
1 
34 /*
35  * Support and FAQ: visit <a href="https://www.microchip.com/support/">Microchip Support</a>
36  */
37 
38 #ifndef USART_H_INCLUDED
39 #define USART_H_INCLUDED
40 
41 #include "compiler.h"
42 
55 
57 #ifdef __cplusplus
58 extern "C" {
59 #endif
60 
61 
64 #define SPI_CPHA (1 << 0)
65 
67 #define SPI_CPOL (1 << 1)
68 
70 #define SPI_MODE_0 0
71 #define SPI_MODE_1 (SPI_CPHA)
72 #define SPI_MODE_2 (SPI_CPOL)
73 #define SPI_MODE_3 (SPI_CPOL | SPI_CPHA)
74 
76 #if (SAMV71 || SAMV70 || SAME70 || SAMS70)
77 #ifndef US_MR_USART_MODE_LIN_MASTER
78 #define US_MR_USART_MODE_LIN_MASTER 0x0A
79 #endif
80 #ifndef US_MR_USART_MODE_LIN_SLAVE
81 #define US_MR_USART_MODE_LIN_SLAVE 0x0B
82 #endif
83 #endif
84 /* Input parameters when initializing RS232 and similar modes. */
85 typedef struct {
86  /* Set baud rate of the USART (unused in slave modes). */
87  uint32_t baudrate;
88 
89  /*
90  * Number of bits, which should be one of the following: US_MR_CHRL_5_BIT,
91  * US_MR_CHRL_6_BIT, US_MR_CHRL_7_BIT, US_MR_CHRL_8_BIT or
92  * US_MR_MODE9.
93  */
94  uint32_t char_length;
95 
96  /*
97  * Parity type, which should be one of the following: US_MR_PAR_EVEN,
98  * US_MR_PAR_ODD, US_MR_PAR_SPACE, US_MR_PAR_MARK, US_MR_PAR_NO
99  * or US_MR_PAR_MULTIDROP.
100  */
101  uint32_t parity_type;
102 
103  /*
104  * Number of stop bits between two characters: US_MR_NBSTOP_1_BIT,
105  * US_MR_NBSTOP_1_5_BIT, US_MR_NBSTOP_2_BIT.
106  * \note US_MR_NBSTOP_1_5_BIT is supported in asynchronous modes only.
107  */
108  uint32_t stop_bits;
109 
110  /*
111  * Run the channel in test mode, which should be one of following:
112  * US_MR_CHMODE_NORMAL, US_MR_CHMODE_AUTOMATIC,
113  * US_MR_CHMODE_LOCAL_LOOPBACK, US_MR_CHMODE_REMOTE_LOOPBACK.
114  */
115  uint32_t channel_mode;
116 
117  /* Filter of IrDA mode, useless in other modes. */
118  uint32_t irda_filter;
120 
121 /* Input parameters when initializing ISO7816 mode. */
122 typedef struct {
123  /* Set the frequency of the ISO7816 clock. */
124  uint32_t iso7816_hz;
125 
126  /*
127  * The number of ISO7816 clock ticks in every bit period (1 to 2047,
128  * 0 = disable clock). Baudrate rate = iso7816_hz / fidi_ratio.
129  */
130  uint32_t fidi_ratio;
131 
132  /*
133  * How to calculate the parity bit: US_MR_PAR_EVEN for normal mode or
134  * US_MR_PAR_ODD for inverse mode.
135  */
136  uint32_t parity_type;
137 
138  /*
139  * Inhibit Non Acknowledge:
140  * - 0: the NACK is generated;
141  * - 1: the NACK is not generated.
142  *
143  * \note This bit will be used only in ISO7816 mode, protocol T = 0
144  * receiver.
145  */
146  uint32_t inhibit_nack;
147 
148  /*
149  * Disable successive NACKs.
150  * - 0: NACK is sent on the ISO line as soon as a parity error occurs
151  * in the received character. Successive parity errors are counted up to
152  * the value in the max_iterations field. These parity errors generate
153  * a NACK on the ISO line. As soon as this value is reached, no additional
154  * NACK is sent on the ISO line. The ITERATION flag is asserted.
155  */
156  uint32_t dis_suc_nack;
157 
158  /* Max number of repetitions (0 to 7). */
159  uint32_t max_iterations;
160 
161  /*
162  * Bit order in transmitted characters:
163  * - 0: LSB first;
164  * - 1: MSB first.
165  */
166  uint32_t bit_order;
167 
168  /*
169  * Which protocol is used:
170  * - 0: T = 0;
171  * - 1: T = 1.
172  */
173  uint32_t protocol_type;
175 
176 /* Input parameters when initializing SPI mode. */
177 typedef struct {
178  /* Set the frequency of the SPI clock (unused in slave mode). */
179  uint32_t baudrate;
180 
181  /*
182  * Number of bits, which should be one of the following: US_MR_CHRL_5_BIT,
183  * US_MR_CHRL_6_BIT, US_MR_CHRL_7_BIT, US_MR_CHRL_8_BIT or
184  * US_MR_MODE9.
185  */
186  uint32_t char_length;
187 
188  /*
189  * Which SPI mode to use, which should be one of the following:
190  * SPI_MODE_0, SPI_MODE_1, SPI_MODE_2, SPI_MODE_3.
191  */
192  uint32_t spi_mode;
193 
194  /*
195  * Run the channel in test mode, which should be one of following:
196  * US_MR_CHMODE_NORMAL, US_MR_CHMODE_AUTOMATIC,
197  * US_MR_CHMODE_LOCAL_LOOPBACK, US_MR_CHMODE_REMOTE_LOOPBACK.
198  */
199  uint32_t channel_mode;
201 
202 void usart_reset(Usart *p_usart);
203 uint32_t usart_set_async_baudrate(Usart *p_usart,
204  uint32_t baudrate, uint32_t ul_mck);
205 uint32_t usart_init_rs232(Usart *p_usart,
206  const sam_usart_opt_t *p_usart_opt, uint32_t ul_mck);
207 uint32_t usart_init_hw_handshaking(Usart *p_usart,
208  const sam_usart_opt_t *p_usart_opt, uint32_t ul_mck);
209 #if (SAM3S || SAM4S || SAM3U || SAM4L || SAM4E)
210 uint32_t usart_init_modem(Usart *p_usart,
211  const sam_usart_opt_t *p_usart_opt, uint32_t ul_mck);
212 #endif
213 uint32_t usart_init_sync_master(Usart *p_usart,
214  const sam_usart_opt_t *p_usart_opt, uint32_t ul_mck);
215 uint32_t usart_init_sync_slave(Usart *p_usart,
216  const sam_usart_opt_t *p_usart_opt);
217 uint32_t usart_init_rs485(Usart *p_usart,
218  const sam_usart_opt_t *p_usart_opt, uint32_t ul_mck);
219 #if (!SAMG55 && !SAMV71 && !SAMV70 && !SAME70 && !SAMS70)
220 uint32_t usart_init_irda(Usart *p_usart,
221  const sam_usart_opt_t *p_usart_opt, uint32_t ul_mck);
222 #endif
223 #if (!SAMV71 && !SAMV70 && !SAME70 && !SAMS70)
224 uint32_t usart_init_iso7816(Usart *p_usart,
225  const usart_iso7816_opt_t *p_usart_opt, uint32_t ul_mck);
226 void usart_reset_iterations(Usart *p_usart);
227 void usart_reset_nack(Usart *p_usart);
228 uint32_t usart_is_rx_buf_end(Usart *p_usart);
229 uint32_t usart_is_tx_buf_end(Usart *p_usart);
230 uint32_t usart_is_rx_buf_full(Usart *p_usart);
231 uint32_t usart_is_tx_buf_empty(Usart *p_usart);
232 uint8_t usart_get_error_number(Usart *p_usart);
233 #endif
234 uint32_t usart_init_spi_master(Usart *p_usart,
235  const usart_spi_opt_t *p_usart_opt, uint32_t ul_mck);
236 uint32_t usart_init_spi_slave(Usart *p_usart,
237  const usart_spi_opt_t *p_usart_opt);
238 #if (SAM3XA || SAM4L || SAMG55 || SAMV71 || SAMV70 || SAME70 || SAMS70)
239 uint32_t usart_init_lin_master(Usart *p_usart, uint32_t ul_baudrate,
240  uint32_t ul_mck);
241 uint32_t usart_init_lin_slave(Usart *p_usart, uint32_t ul_baudrate,
242  uint32_t ul_mck);
243 void usart_lin_abort_tx(Usart *p_usart);
244 void usart_lin_send_wakeup_signal(Usart *p_usart);
245 void usart_lin_set_node_action(Usart *p_usart, uint8_t uc_action);
246 void usart_lin_disable_parity(Usart *p_usart);
247 void usart_lin_enable_parity(Usart *p_usart);
248 void usart_lin_disable_checksum(Usart *p_usart);
249 void usart_lin_enable_checksum(Usart *p_usart);
250 void usart_lin_set_checksum_type(Usart *p_usart, uint8_t uc_type);
251 void usart_lin_set_data_len_mode(Usart *p_usart, uint8_t uc_mode);
252 void usart_lin_disable_frame_slot(Usart *p_usart);
253 void usart_lin_enable_frame_slot(Usart *p_usart);
254 void usart_lin_set_wakeup_signal_type(Usart *p_usart, uint8_t uc_type);
255 void usart_lin_set_response_data_len(Usart *p_usart, uint8_t uc_len);
256 void usart_lin_disable_pdc_mode(Usart *p_usart);
257 void usart_lin_enable_pdc_mode(Usart *p_usart);
258 void usart_lin_set_tx_identifier(Usart *p_usart, uint8_t uc_id);
259 uint8_t usart_lin_read_identifier(Usart *p_usart);
260 uint8_t usart_lin_get_data_length(Usart *usart);
261 #endif
262 #if (SAMV71 || SAMV70 || SAME70 || SAMS70)
263 uint8_t usart_lin_identifier_send_complete(Usart *usart);
264 uint8_t usart_lin_identifier_reception_complete(Usart *usart);
265 uint8_t usart_lin_tx_complete(Usart *usart);
266 uint32_t usart_init_lon(Usart *p_usart, uint32_t ul_baudrate, uint32_t ul_mck);
267 void usart_lon_set_comm_type(Usart *p_usart, uint8_t uc_type);
268 void usart_lon_disable_coll_detection(Usart *p_usart);
269 void usart_lon_enable_coll_detection(Usart *p_usart);
270 void usart_lon_set_tcol(Usart *p_usart, uint8_t uc_type);
271 void usart_lon_set_cdtail(Usart *p_usart, uint8_t uc_type);
272 void usart_lon_set_dmam(Usart *p_usart, uint8_t uc_type);
273 void usart_lon_set_beta1_tx_len(Usart *p_usart, uint32_t ul_len);
274 void usart_lon_set_beta1_rx_len(Usart *p_usart, uint32_t ul_len);
275 void usart_lon_set_priority(Usart *p_usart, uint8_t uc_psnb, uint8_t uc_nps);
276 void usart_lon_set_tx_idt(Usart *p_usart, uint32_t ul_time);
277 void usart_lon_set_rx_idt(Usart *p_usart, uint32_t ul_time);
278 void usart_lon_set_pre_len(Usart *p_usart, uint32_t ul_len);
279 void usart_lon_set_data_len(Usart *p_usart, uint8_t uc_len);
280 void usart_lon_set_l2hdr(Usart *p_usart, uint8_t uc_bli, uint8_t uc_altp, uint8_t uc_pb);
281 uint32_t usart_lon_is_tx_end(Usart *p_usart);
282 uint32_t usart_lon_is_rx_end(Usart *p_usart);
283 #endif
284 void usart_enable_tx(Usart *p_usart);
285 void usart_disable_tx(Usart *p_usart);
286 void usart_reset_tx(Usart *p_usart);
287 void usart_set_tx_timeguard(Usart *p_usart, uint32_t timeguard);
288 void usart_enable_rx(Usart *p_usart);
289 void usart_disable_rx(Usart *p_usart);
290 void usart_reset_rx(Usart *p_usart);
291 void usart_set_rx_timeout(Usart *p_usart, uint32_t timeout);
292 void usart_enable_interrupt(Usart *p_usart, uint32_t ul_sources);
293 void usart_disable_interrupt(Usart *p_usart, uint32_t ul_sources);
294 uint32_t usart_get_interrupt_mask(Usart *p_usart);
295 uint32_t usart_get_status(Usart *p_usart);
296 void usart_reset_status(Usart *p_usart);
297 void usart_start_tx_break(Usart *p_usart);
298 void usart_stop_tx_break(Usart *p_usart);
299 void usart_start_rx_timeout(Usart *p_usart);
300 uint32_t usart_send_address(Usart *p_usart, uint32_t ul_addr);
301 void usart_restart_rx_timeout(Usart *p_usart);
302 #if (SAM3S || SAM4S || SAM3U || SAM4L || SAM4E)
303 void usart_drive_DTR_pin_low(Usart *p_usart);
304 void usart_drive_DTR_pin_high(Usart *p_usart);
305 #endif
306 void usart_drive_RTS_pin_low(Usart *p_usart);
307 void usart_drive_RTS_pin_high(Usart *p_usart);
308 void usart_spi_force_chip_select(Usart *p_usart);
309 void usart_spi_release_chip_select(Usart *p_usart);
310 uint32_t usart_is_tx_ready(Usart *p_usart);
311 uint32_t usart_is_tx_empty(Usart *p_usart);
312 uint32_t usart_is_rx_ready(Usart *p_usart);
313 uint32_t usart_write(Usart *p_usart, uint32_t c);
314 uint32_t usart_putchar(Usart *p_usart, uint32_t c);
315 void usart_write_line(Usart *p_usart, const char *string);
316 uint32_t usart_read(Usart *p_usart, uint32_t *c);
317 uint32_t usart_getchar(Usart *p_usart, uint32_t *c);
318 #if (SAM3XA || SAM3U)
319 uint32_t *usart_get_tx_access(Usart *p_usart);
320 uint32_t *usart_get_rx_access(Usart *p_usart);
321 #endif
322 #if (!SAM4L && !SAMV71 && !SAMV70 && !SAME70 && !SAMS70)
323 Pdc *usart_get_pdc_base(Usart *p_usart);
324 #endif
325 void usart_enable_writeprotect(Usart *p_usart);
326 void usart_disable_writeprotect(Usart *p_usart);
327 uint32_t usart_get_writeprotect_status(Usart *p_usart);
328 #if (SAM3S || SAM4S || SAM3U || SAM3XA || SAM4L || SAM4E || SAM4C || SAM4CP || SAM4CM || SAMV70 || SAMV71 || SAMS70 || SAME70)
329 void usart_man_set_tx_pre_len(Usart *p_usart, uint8_t uc_len);
330 void usart_man_set_tx_pre_pattern(Usart *p_usart, uint8_t uc_pattern);
331 void usart_man_set_tx_polarity(Usart *p_usart, uint8_t uc_polarity);
332 void usart_man_set_rx_pre_len(Usart *p_usart, uint8_t uc_len);
333 void usart_man_set_rx_pre_pattern(Usart *p_usart, uint8_t uc_pattern);
334 void usart_man_set_rx_polarity(Usart *p_usart, uint8_t uc_polarity);
335 void usart_man_enable_drift_compensation(Usart *p_usart);
336 void usart_man_disable_drift_compensation(Usart *p_usart);
337 #endif
338 
339 #if SAM4L
340 uint32_t usart_get_version(Usart *p_usart);
341 #endif
342 
343 #if SAMG55
344 void usart_set_sleepwalking(Usart *p_uart, uint8_t ul_low_value,
345  bool cmpmode, bool cmppar, uint8_t ul_high_value);
346 #endif
347 
349 
350 #ifdef __cplusplus
351 }
352 #endif
353 
354 
357 
773 #endif /* USART_H_INCLUDED */
void usart_disable_writeprotect(Usart *p_usart)
Disable write protect of USART registers.
Definition: usart.c:1907
uint8_t usart_get_error_number(Usart *p_usart)
Get the total number of errors that occur during an ISO7816 transfer.
Definition: usart.c:699
void usart_restart_rx_timeout(Usart *p_usart)
Restart the receive timeout.
Definition: usart.c:1580
uint32_t usart_read(Usart *p_usart, uint32_t *c)
Read from USART Receive Holding Register.
Definition: usart.c:1759
uint32_t usart_is_rx_buf_full(Usart *p_usart)
Check if both receive buffers are full.
Definition: usart.c:646
void usart_reset_iterations(Usart *p_usart)
Reset the ITERATION in US_CSR when the ISO7816 mode is enabled.
Definition: usart.c:623
uint32_t usart_is_tx_buf_end(Usart *p_usart)
Check if one transmit buffer is empty.
Definition: usart.c:673
uint32_t usart_write(Usart *p_usart, uint32_t c)
Write to USART Transmit Holding Register.
Definition: usart.c:1704
void usart_disable_rx(Usart *p_usart)
Disable USART receiver.
Definition: usart.c:1436
Usart hardware registers.
uint32_t usart_send_address(Usart *p_usart, uint32_t ul_addr)
In Multidrop mode only, the next character written to the US_THR is sent with the address bit set...
Definition: usart.c:1560
void usart_disable_interrupt(Usart *p_usart, uint32_t ul_sources)
Disable USART interrupts.
Definition: usart.c:1480
void usart_drive_RTS_pin_high(Usart *p_usart)
Drive the pin RTS to 1.
Definition: usart.c:1624
void usart_write_line(Usart *p_usart, const char *string)
Write one-line string through USART.
Definition: usart.c:1741
void usart_drive_RTS_pin_low(Usart *p_usart)
Drive the pin RTS to 0.
Definition: usart.c:1614
void usart_start_rx_timeout(Usart *p_usart)
Start waiting for a character before clocking the timeout count. Reset the status bit TIMEOUT in US_C...
Definition: usart.c:1545
Commonly used includes, types and macros.
void usart_set_tx_timeguard(Usart *p_usart, uint32_t timeguard)
Configure the transmit timeguard register.
Definition: usart.c:1416
void usart_spi_force_chip_select(Usart *p_usart)
Drive the slave select line NSS (RTS pin) to 0 in SPI master mode.
Definition: usart.c:1634
uint32_t usart_get_status(Usart *p_usart)
Get current status.
Definition: usart.c:1504
void usart_enable_writeprotect(Usart *p_usart)
Enable write protect of USART registers.
Definition: usart.c:1897
void usart_reset_rx(Usart *p_usart)
Immediately stop and disable USART receiver.
Definition: usart.c:1446
void usart_enable_tx(Usart *p_usart)
Enable USART transmitter.
Definition: usart.c:1384
void usart_reset_tx(Usart *p_usart)
Immediately stop and disable USART transmitter.
Definition: usart.c:1404
uint32_t usart_getchar(Usart *p_usart, uint32_t *c)
Read from USART Receive Holding Register. Before reading user should check if rx is ready...
Definition: usart.c:1781
uint32_t usart_init_sync_slave(Usart *p_usart, const sam_usart_opt_t *p_usart_opt)
Configure USART to work in SYNC mode and act as a slave.
Definition: usart.c:423
void usart_start_tx_break(Usart *p_usart)
Start transmission of a break.
Definition: usart.c:1524
void usart_enable_interrupt(Usart *p_usart, uint32_t ul_sources)
Enable USART interrupts.
Definition: usart.c:1469
uint32_t usart_init_rs232(Usart *p_usart, const sam_usart_opt_t *p_usart_opt, uint32_t ul_mck)
Configure USART to work in RS232 mode.
Definition: usart.c:274
void usart_spi_release_chip_select(Usart *p_usart)
Drive the slave select line NSS (RTS pin) to 1 in SPI master mode.
Definition: usart.c:1644
void usart_reset_nack(Usart *p_usart)
Reset NACK in US_CSR.
Definition: usart.c:633
uint32_t usart_init_rs485(Usart *p_usart, const sam_usart_opt_t *p_usart_opt, uint32_t ul_mck)
Configure USART to work in RS485 mode.
Definition: usart.c:462
uint32_t usart_init_spi_slave(Usart *p_usart, const usart_spi_opt_t *p_usart_opt)
Configure USART to work in SPI mode and act as a slave.
Definition: usart.c:782
uint32_t usart_is_tx_buf_empty(Usart *p_usart)
Check if both transmit buffers are empty.
Definition: usart.c:686
uint32_t usart_putchar(Usart *p_usart, uint32_t c)
Write to USART Transmit Holding Register.
Definition: usart.c:1725
uint32_t usart_set_async_baudrate(Usart *p_usart, uint32_t baudrate, uint32_t ul_mck)
Calculate a clock divider(CD) and a fractional part (FP) for the USART asynchronous modes to generate...
Definition: usart.c:111
uint32_t usart_init_irda(Usart *p_usart, const sam_usart_opt_t *p_usart_opt, uint32_t ul_mck)
Configure USART to work in IrDA mode.
Definition: usart.c:490
uint32_t usart_is_rx_buf_end(Usart *p_usart)
Check if one receive buffer is filled.
Definition: usart.c:660
uint32_t usart_is_tx_empty(Usart *p_usart)
Check if Transmit Holding Register is empty. Check if the last data written in USART_THR have been lo...
Definition: usart.c:1674
uint32_t usart_init_spi_master(Usart *p_usart, const usart_spi_opt_t *p_usart_opt, uint32_t ul_mck)
Configure USART to work in SPI mode and act as a master.
Definition: usart.c:718
uint32_t usart_init_sync_master(Usart *p_usart, const sam_usart_opt_t *p_usart_opt, uint32_t ul_mck)
Configure USART to work in SYNC mode and act as a master.
Definition: usart.c:386
void usart_disable_tx(Usart *p_usart)
Disable USART transmitter.
Definition: usart.c:1394
void usart_set_rx_timeout(Usart *p_usart, uint32_t timeout)
Configure the receive timeout register.
Definition: usart.c:1458
uint32_t usart_get_interrupt_mask(Usart *p_usart)
Read USART interrupt mask.
Definition: usart.c:1492
uint32_t usart_init_hw_handshaking(Usart *p_usart, const sam_usart_opt_t *p_usart_opt, uint32_t ul_mck)
Configure USART to work in hardware handshaking mode.
Definition: usart.c:313
uint32_t usart_init_iso7816(Usart *p_usart, const usart_iso7816_opt_t *p_usart_opt, uint32_t ul_mck)
Configure USART to work in ISO7816 mode.
Definition: usart.c:557
uint32_t usart_is_tx_ready(Usart *p_usart)
Check if Transmit is Ready. Check if data have been loaded in USART_THR and are waiting to be loaded ...
Definition: usart.c:1659
void usart_reset(Usart *p_usart)
Reset the USART and disable TX and RX.
Definition: usart.c:240
Pdc * usart_get_pdc_base(Usart *p_usart)
Get USART PDC base address.
Definition: usart.c:1827
uint32_t usart_get_writeprotect_status(Usart *p_usart)
Get write protect status.
Definition: usart.c:1920
void usart_stop_tx_break(Usart *p_usart)
Stop transmission of a break.
Definition: usart.c:1534
uint32_t usart_is_rx_ready(Usart *p_usart)
Check if the received data are ready. Check if Data have been received and loaded into USART_RHR...
Definition: usart.c:1688
void usart_enable_rx(Usart *p_usart)
Enable USART receiver.
Definition: usart.c:1426
void usart_reset_status(Usart *p_usart)
Reset status bits (PARE, OVER, MANERR, UNRE and PXBRK in US_CSR).
Definition: usart.c:1514


inertial_sense_ros
Author(s):
autogenerated on Sat Sep 19 2020 03:19:05