66 Assert(clkper_hz > baudrate);
72 divisor = (clkper_hz + baudrate - 1) / baudrate;
86 divisor_8bit = divisor;
92 if (divisor_8bit > 64) {
93 ctrl = SPI_PRESCALER_DIV128_gc;
95 else if (divisor_8bit > 32) {
96 ctrl = SPI_PRESCALER_DIV64_gc;
98 else if (divisor_8bit > 16) {
99 ctrl = SPI_CLK2X_bm | SPI_PRESCALER_DIV64_gc;
101 else if (divisor_8bit > 8) {
102 ctrl = SPI_PRESCALER_DIV16_gc;
104 else if (divisor_8bit > 4) {
105 ctrl = SPI_CLK2X_bm | SPI_PRESCALER_DIV16_gc;
107 else if (divisor_8bit > 2) {
108 ctrl = SPI_PRESCALER_DIV4_gc;
111 ctrl = SPI_CLK2X_bm | SPI_PRESCALER_DIV4_gc;
115 spi->CTRL = (spi->CTRL & ~(SPI_CLK2X_bm | SPI_PRESCALER_gm)) | ctrl;
129 if ((uint16_t)spi == (uint16_t)&SPIA) {
134 if ((uint16_t)spi == (uint16_t)&SPIB) {
139 if ((uint16_t)spi == (uint16_t)&SPIC) {
144 if ((uint16_t)spi == (uint16_t)&SPID) {
149 if ((uint16_t)spi == (uint16_t)&SPIE) {
154 if ((uint16_t)spi == (uint16_t)&SPIF) {
176 spi_flags_t flags, uint32_t baud_rate,
177 board_spi_select_id_t sel_id)
185 spi->CTRL = (spi->CTRL & ~SPI_MODE_gm) |
186 ((flags << SPI_MODE_gp) & SPI_MODE_gm);
static bool spi_is_rx_full(SPI_t *spi)
Tests if the SPI contains a received character.
static uint32_t sysclk_get_cpu_hz(void)
Return the current rate in Hz of the CPU clock.
static void spi_enable_master_mode(SPI_t *spi)
Activate SPI master mode of a SPI peripheral.
Polled SPI device definition.
#define Assert(expr)
This macro is used to test fatal errors.
static void spi_read_single(SPI_t *spi, uint8_t *data)
Receive one byte from a SPI device.
void spi_master_setup_device(SPI_t *spi, struct spi_device *device, spi_flags_t flags, uint32_t baud_rate, board_spi_select_id_t sel_id)
Setup a SPI device.
void spi_master_init(SPI_t *spi)
Initializes the SPI in master mode.
int8_t spi_xmega_set_baud_div(SPI_t *spi, uint32_t baudrate, uint32_t clkper_hz)
Calculates the SPI baudrate divider.
status_code_t spi_read_packet(SPI_t *spi, uint8_t *data, size_t len)
Receive a sequence of bytes from a SPI device.
static __always_inline void spi_write_single(SPI_t *spi, uint8_t data)
Write one byte to a SPI device.
void spi_select_device(SPI_t *spi, struct spi_device *device)
Select given device on the SPI bus.
void sysclk_enable_module(enum sysclk_port_id port, uint8_t id)
Enable the clock to peripheral id on port port.
#define CONFIG_SPI_MASTER_DUMMY
Default Config Spi Master Dummy Field.
Chip-specific system clock management functions.
SPI Master driver header file.
void spi_deselect_device(SPI_t *spi, struct spi_device *device)
Deselect given device on the SPI bus.
status_code_t spi_write_packet(SPI_t *spi, const uint8_t *data, size_t len)
Send a sequence of bytes to a SPI device.