#include "spi.h" void spiInit() { SPCR |= ( 1 << SPE ) | ( 1 << MSTR ); } void spiEnd() { SPCR = 0; SPSR = 0; } void spiSetClockDiv( SPIClock enmClockDiv ) { bool bSPI2X = ( static_cast( enmClockDiv ) >> 2 ) & 1; bool bSPR1 = ( static_cast( enmClockDiv ) >> 1 ) & 1; bool bSPR0 = ( static_cast( enmClockDiv ) >> 0 ) & 1; SPCR &= ~( 1 << SPR1 | 1 << SPR0 ); SPSR &= ~( 1 << SPI2X ); SPCR |= ( bSPR1 << SPR1 | bSPR0 << SPR0 ); SPSR |= ( bSPI2X << SPI2X ); } void spiSetDataMode( SPIMode enmMode ) { bool bCPOL = ( static_cast( enmMode ) >> 1 ) & 1; bool bCPHA = ( static_cast( enmMode ) >> 0 ) & 1; SPCR &= ~( 1 << CPOL | 1 << CPHA ); SPCR |= ( bCPOL << CPOL | bCPHA << CPHA ); } void spiSetBitOrder( bool bLSBFirst ) { if( bLSBFirst ) { SPCR |= ( 1 << DORD ); } else { SPCR &= ~( 1 << DORD ); } } uint8_t spiTransfer( uint8_t ui8Data ) { SPDR = ui8Data; while( !( SPSR & ( 1 << SPIF ) ) ); return SPDR; }