37#include <avr/interrupt.h>
60 SPI_DDR |= (1 << SPI_MOSI);
61 SPI_DDR |= (1 << SPI_SCK);
62 SPI_DDR &= ~(1 << SPI_MISO);
63 SPI_DDR &= ~(1 << SPI_SS);
64 SPI_PORT |= (1 << SPI_MISO);
65 SPI_PORT |= (1 << SPI_SS);
68 SPCR |= (transfer_mode << DORD);
69 SPCR |= (control_mode << MSTR);
70 SPCR |= (polarity_mode << CPOL);
71 SPCR |= (phase_mode << CPHA);
74 SPSR |= (dbl_clock << SPI2X);
88 loop_until_bit_is_set(SPSR, SPIF);
107 loop_until_bit_is_set(SPSR, SPIF);
112 loop_until_bit_is_set(SPSR, SPIF);
116 uint16_t received_data = 0;
117 received_data = (msb << 8U);
118 received_data |= (lsb);
120 return received_data;
void init_spi(spi_transfer_mode_t transfer_mode, spi_control_mode_t control_mode, spi_polarity_mode_t polarity_mode, spi_phase_mode_t phase_mode, spi_clk_rate_t clk_rate, spi_dbl_clk_mode_t dbl_clock)
Initialisation routine to set up SPI comms.
uint8_t spi_trade_byte(uint8_t data)
Sends out a byte of data over SPI and returns the byte it receives.
uint16_t spi_trade_word(uint16_t data)
Sends out a 16bit word of data over spi (in two bytes) and returns the byte it receives.
spi_dbl_clk_mode_t
Enumerated constants for selecting whether the SPI clock speed is doubled.
spi_transfer_mode_t
Enumerated constants for selecting the transfer mode, most significant bit first of least significant...
spi_phase_mode_t
Enumerated constants for selecting the SPI phase mode, on leading clock edge.
spi_polarity_mode_t
Enumerated constants for selecting the SPI polarity mode, rising or falling on leading clock edge.
spi_clk_rate_t
Enumerated constants for selecting the SPI clock rate.
spi_control_mode_t
Enumerated constants for selecting whether the host MCU is the controller or peripheral (formerly kno...
Driver for SPI communication between the ATmega328P and other SPI compatible devices.
Definitions for pin mapping (for CCS81 gas sensor)