spi/spi.cpp

54 lines
985 B
C++
Raw Normal View History

2016-05-26 21:40:24 +02:00
#include "spi.h"
void spiInit()
{
SPCR |= ( 1 << SPE ) | ( 1 << MSTR );
}
void spiEnd()
{
SPCR = 0;
SPSR = 0;
}
void spiSetClockDiv( SPIClock enmClockDiv )
{
bool bSPI2X = ( static_cast<uint8_t>( enmClockDiv ) >> 2 ) & 1;
bool bSPR1 = ( static_cast<uint8_t>( enmClockDiv ) >> 1 ) & 1;
bool bSPR0 = ( static_cast<uint8_t>( 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<uint8_t>( enmMode ) >> 1 ) & 1;
bool bCPHA = ( static_cast<uint8_t>( 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;
}