usart_spi.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 "usart_spi.h"
38 #include "sysclk.h"
39 #if SAMG55
40 #include "flexcom.h"
41 #include "conf_board.h"
42 #endif
43 
45 
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49 
50 
57 void usart_spi_init(Usart *p_usart)
58 {
59 #if (!SAMG55)
60 
61  uint8_t uc_id = 0;
62 
63 #ifdef USART0
64  if (p_usart == USART0) {
65  uc_id = ID_USART0;
66  }
67 #endif
68 
69 #ifdef USART1
70  else if(p_usart == USART1) {
71  uc_id = ID_USART1;
72  }
73 #endif
74 
75 #ifdef USART2
76  else if(p_usart == USART2) {
77  uc_id = ID_USART2;
78  }
79 #endif
80 
81 #ifdef USART3
82  else if(p_usart == USART3) {
83  uc_id = ID_USART3;
84  }
85 #endif
86 
87 #endif
88 
89 #if SAM4L
91 #elif SAMG55
92  flexcom_enable(BOARD_FLEXCOM_USART);
93  flexcom_set_opmode(BOARD_FLEXCOM_USART, FLEXCOM_USART);
94 #else
96 #endif
97 }
98 
113 void usart_spi_setup_device(Usart *p_usart, struct usart_spi_device *device,
114  spi_flags_t flags, unsigned long baud_rate,
115  board_spi_select_id_t sel_id)
116 {
117  usart_spi_opt_t opt;
118 
119  /* avoid Cppcheck Warning */
120  UNUSED(device);
121  UNUSED(sel_id);
122 
123  /* Basic usart SPI configuration. */
124  opt.baudrate = baud_rate;
126  opt.spi_mode = flags;
128 
129  /* Initialize the USART module as SPI master. */
130 #if (SAM4L)
131  usart_init_spi_master(p_usart, &opt, sysclk_get_pba_hz());
132 #else
134 #endif
135 
136  usart_enable_rx(p_usart);
137  usart_enable_tx(p_usart);
138 }
139 
146 void usart_spi_write_single(Usart *p_usart, uint8_t data)
147 {
148  usart_putchar(p_usart, data);
149 }
150 
164 uint32_t usart_spi_write_packet(Usart *p_usart, const uint8_t *data, size_t len)
165 {
166  uint32_t dummy_data;
167  size_t i=0;
168  while(len) {
169  usart_putchar(p_usart, *(data+i));
170  usart_getchar(p_usart, &dummy_data);
171  len--;
172  i++;
173  }
174  return 0;
175 }
176 
184 void usart_spi_read_single(Usart *p_usart, uint8_t *data)
185 {
186  uint32_t temp_data = 0;
187  /* Dummy write one data to slave in order to read data. */
189 
190  usart_getchar(p_usart, &temp_data);
191  *data = (uint8_t)temp_data;
192 }
193 
207 uint32_t usart_spi_read_packet(Usart *p_usart, uint8_t *data, size_t len)
208 {
209  uint32_t val;
210  uint32_t i = 0;
211 
212  while(len) {
213  /* Dummy write one data to slave in order to read data. */
215  usart_getchar(p_usart, &val);
216 
217  data[i] = (uint8_t)(val & 0xFF);
218  i++;
219  len--;
220  }
221 
222  return 0;
223 }
224 
232 void usart_spi_select_device(Usart *p_usart, struct usart_spi_device *device)
233 {
234  /* avoid Cppcheck Warning */
235  UNUSED(device);
236 
238 }
239 
246 void usart_spi_deselect_device(Usart *p_usart, struct usart_spi_device *device)
247 {
248  /* avoid Cppcheck Warning */
249  UNUSED(device);
250 
252 }
253 
262 uint32_t usart_spi_is_tx_empty(Usart *p_usart)
263 {
264  return usart_is_tx_empty(p_usart);
265 }
266 
274 uint32_t usart_spi_is_rx_ready(Usart *p_usart)
275 {
276  return usart_is_rx_ready(p_usart);
277 }
278 
286 uint32_t usart_spi_is_tx_ready(Usart *p_usart)
287 {
288  return usart_is_tx_ready(p_usart);
289 }
290 
298 uint32_t usart_spi_is_rx_full(Usart *p_usart)
299 {
300 #if (!SAMV71 && !SAMV70 && !SAME70 && !SAMS70)
301  return usart_is_rx_buf_full(p_usart);
302 #else
303  return 0;
304 #endif
305 }
306 
311 void usart_spi_enable(Usart *p_usart)
312 {
313  usart_enable_tx(p_usart);
314  usart_enable_rx(p_usart);
315 }
316 
324 void usart_spi_disable(Usart *p_usart)
325 {
326  usart_disable_tx(p_usart);
327  usart_disable_rx(p_usart);
328 }
329 
331 
332 #ifdef __cplusplus
333 }
334 #endif
335 
336 
uint32_t usart_spi_read_packet(Usart *p_usart, uint8_t *data, size_t len)
Receive a sequence of bytes from a USART in SPI mode device.
Definition: usart_spi.c:207
void usart_enable_rx(Usart *p_usart)
Enable USART receiver.
Definition: usart.c:1426
uint32_t usart_spi_is_rx_ready(Usart *p_usart)
Check whether the USART in SPI master mode contains a received character.
Definition: usart_spi.c:274
#define ID_USART1
USART 1 (USART1)
Definition: same70j19.h:419
#define UNUSED(v)
Marking v as a unused parameter or value.
Definition: compiler.h:86
uint32_t usart_spi_is_tx_ready(Usart *p_usart)
Check if the USART Transmit Holding Register is empty or not in SPI mode.
Definition: usart_spi.c:286
uint32_t usart_is_rx_buf_full(Usart *p_usart)
Check if both receive buffers are full.
Definition: usart.c:646
Usart hardware registers.
uint32_t usart_spi_write_packet(Usart *p_usart, const uint8_t *data, size_t len)
Send a sequence of bytes to an SPI device using USART in SPI mode.
Definition: usart_spi.c:164
void usart_spi_read_single(Usart *p_usart, uint8_t *data)
Receive one byte from an SPI device using USART in SPI mode.
Definition: usart_spi.c:184
#define US_MR_CHMODE_NORMAL
(US_MR) Normal mode
void usart_spi_write_single(Usart *p_usart, uint8_t data)
Write one byte to an SPI device using USART in SPI mode.
Definition: usart_spi.c:146
#define USART0
(USART0) Base Address
Definition: same70j19.h:505
#define USART1
(USART1) Base Address
Definition: same70j19.h:506
void usart_spi_select_device(Usart *p_usart, struct usart_spi_device *device)
Select the given device on the SPI bus.
Definition: usart_spi.c:232
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
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_spi_disable(Usart *p_usart)
Disable the USART for the specified USART in SPI mode.
Definition: usart_spi.c:324
static uint32_t sysclk_get_peripheral_hz(void)
Retrieves the current rate in Hz of the peripheral clocks.
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_spi_setup_device(Usart *p_usart, struct usart_spi_device *device, spi_flags_t flags, unsigned long baud_rate, board_spi_select_id_t sel_id)
Set up a USART in SPI master mode device.
Definition: usart_spi.c:113
uint32_t usart_spi_is_tx_empty(Usart *p_usart)
Check whether there are data in Transmit Holding Register or Transmit Shift Register in SPI master mo...
Definition: usart_spi.c:262
void usart_spi_init(Usart *p_usart)
Enable the USART system clock in SPI master mode.
Definition: usart_spi.c:57
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
Board configuration.
#define US_MR_CHRL_8_BIT
(US_MR) Character length is 8 bits
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
void usart_disable_rx(Usart *p_usart)
Disable USART receiver.
Definition: usart.c:1436
void usart_spi_enable(Usart *p_usart)
Enable the USART for the specified USART in SPI mode.
Definition: usart_spi.c:311
USBInterfaceDescriptor data
Polled SPI device definition.
uint32_t usart_putchar(Usart *p_usart, uint32_t c)
Write to USART Transmit Holding Register.
Definition: usart.c:1725
#define ID_USART0
USART 0 (USART0)
Definition: same70j19.h:418
uint8_t spi_flags_t
SPI Flags Definition.
uint32_t usart_spi_is_rx_full(Usart *p_usart)
Check if both receive buffers are full.
Definition: usart_spi.c:298
void usart_disable_tx(Usart *p_usart)
Disable USART transmitter.
Definition: usart.c:1394
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
void usart_enable_tx(Usart *p_usart)
Enable USART transmitter.
Definition: usart.c:1384
#define CONFIG_USART_SPI_DUMMY
Default Configuration of SPI Master Dummy Field.
static void sysclk_enable_peripheral_clock(uint32_t ul_id)
Enable a peripheral&#39;s clock.
#define USART2
(USART2) Base Address
Definition: same70n19.h:548
SAM USART in SPI mode driver functions.
uint32_t board_spi_select_id_t
Board SPI Select Id Definition.
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
#define ID_USART2
USART 2 (USART2)
Definition: same70n19.h:445
void usart_spi_deselect_device(Usart *p_usart, struct usart_spi_device *device)
De-select the given device on the SPI bus.
Definition: usart_spi.c:246


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