AVRTools
A Library for the AVR ATmega328 and ATmega2560 Microcontrollers
Public Member Functions | List of all members
SPI::SPISettings Class Reference

A class that binds settings for configuring SPI transmissions. More...

#include <SPI.h>

Public Member Functions

 SPISettings (uint32_t maxSpeed, uint8_t bitOrder, uint8_t dataMode)
 The constructor builds an SPISettings object out of three parameters describing the maximum transmission speed, the data order (most or least significant bit first), and the data mode (phase and polarity). Note that bit order extends to byte order when passing multibyte integers. More...
 
 SPISettings ()
 The constructor builds an SPISettings object with default settings corresponding to a maximum transmission speed of 8 MHz, most significant bit first, and kSpiMode0.
 
uint8_t getSpcr () const
 Return the appropriate configure value for the SPCR register. More...
 
uint8_t getSpsr () const
 Return the appropriate configure value for the SPSR register. More...
 

Detailed Description

A class that binds settings for configuring SPI transmissions.

The SPISettings object is used to configure the SPI hardware. The three parameters are combined into a single SPISettings object, which is passed to SPI::configure(). You need to configure the SPI subsystem in this way before transmitting any data. The configuration remains in effect until explicitly changed by another call to SPI::configure() or the SPI subsystem is disabled by a call to SPI::disable().

This class is taken almost verbatim from the Arduino library SPISettings class created by Matthijs Kooijman and licensed under terms of either the GNU General Public License version 2 or the GNU Lesser General Public License version 2.1.

The implementation makes clever use of GCC intrinsic functions to do essentially all the heavy lifting at compile time whenever the SPI parameters are compile-time constants, producing very small and efficient code in this case. My modifications reformat the code to the AVRTools library conventions and adapt the interface to align with the AVRTools SPI implementation.

Constructor & Destructor Documentation

§ SPISettings()

SPI::SPISettings::SPISettings ( uint32_t  maxSpeed,
uint8_t  bitOrder,
uint8_t  dataMode 
)
inline

The constructor builds an SPISettings object out of three parameters describing the maximum transmission speed, the data order (most or least significant bit first), and the data mode (phase and polarity). Note that bit order extends to byte order when passing multibyte integers.

The code is designed to be exceptionally efficient and small if all three parameters are compile-time constants.

  • maxSpeed the maximum speed of transmission, in herz (Hz). For a SPI chip rated up to 16 MHz, use 16000000.
  • bitOrder whether least significant or most significant bit is first. Pass either kMsbFirst or kLsbFirst.
  • dataMode sets the data mode (phase and polarity) for SPI communications. Pass one of kSpiMode0, kSpiMode1, kSpiMode2, or kSpiMode3.

Member Function Documentation

§ getSpcr()

uint8_t SPI::SPISettings::getSpcr ( ) const
inline

Return the appropriate configure value for the SPCR register.

Returns
a value to load in the SPCR register to configure the SPI hardware.

§ getSpsr()

uint8_t SPI::SPISettings::getSpsr ( ) const
inline

Return the appropriate configure value for the SPSR register.

Returns
a value to load in the SPSR register to configure the SPI hardware.

The documentation for this class was generated from the following file: