User Manual

Reference
C++ and Arduino methods are shown in red.
C functions are shown in green.
static void OrangutanSPIMaster::init(unsigned char speed_divider, unsigned char options)
void spi_master_init(unsigned char speed_divider, unsigned char options)
Initializes the AVR’s hardware SPI module in master mode. This command makes the MOSI and SCK pins
outputs so that the AVR can send data to the slave device. This command makes MISO an input so that the
AVR can receive data from the slave device. If SS is an input, this function enables its pull-up resistor so that
it is less likely that SS goes low and knocks the SPI module out of master mode (see note above).
The speed_divider parameter specifies the ratio of the AVR’s clock frequency to the SPI frequency. The library
defines several keywords of the form SPI_SPEED_DIVIDER_xxx for use as the speed_divider argument.
These keywords are shown in the table below:
Valid values for speed_divider
speed_divider SPI Frequency (assuming 20 MHz clock)
SPI_SPEED_DIVIDER_2 10 MHz
SPI_SPEED_DIVIDER_4 5 MHz
SPI_SPEED_DIVIDER_8 2.5 MHz
SPI_SPEED_DIVIDER_16 1.25 MHz
SPI_SPEED_DIVIDER_32 625 kHz
SPI_SPEED_DIVIDER_64 313 kHz
SPI_SPEED_DIVIDER_128 156 kHz
The options argument controls three important configuration options for the SPI module. The options
argument can be 0 to select all the default options. To over-ride the defaults, the options argument should be a
combination of some of the following keywords, combined using the inclusive-or operator “|”.
SPI_SCK_IDLE_LOW (default): The idle state of SCK will be low; the leading edge will be rising
and the trailing edge will be falling.
SPI_SCK_IDLE_HIGH: The idle state of SCK will be high; the leading edge will be falling and the
trailing edge will be rising.
SPI_MSB_FIRST (default): Bytes will be transmitted/received starting with the most-significant bit
first.
SPI_LSB_FIRST: Bytes will be transmitted/received starting with the least-significant bit first.
SPI_EDGE_LEADING (default): The AVR will sample data on MISO on the leading edge of SCK.
SPI_EDGE_TRAILING: The AVR will sample data on MISO on the trailing edge of SCK.
Example
// Initialize the SPI module in master mode at 20/2 = 10 MHz, sample on the trailing edge,
// LSB first, SCK idle state low.
spi_master_init(SPI_SPEED_DIVIDER_2, SPI_EDGE_TRAILING | SPI_LSB_FIRST);
// C++: OrangutanSPIMaster::init(SPI_SPEED_DIVIDER_2, SPI_EDGE_TRAILING | SPI_LSB_FIRST);
Pololu AVR Library Command Reference © 2001–2015 Pololu Corporation
12. Orangutan SPI Master Functions Page 46 of 65