spi/spi.cpp

150 lines
2.9 KiB
C++
Raw Normal View History

2016-05-26 21:40:24 +02:00
#include "spi.h"
//////////////////////////////////////////////////////////////////////////
void SPI::setCPOL( bool bCPOL )
2016-05-26 21:40:24 +02:00
{
if( bCPOL )
{
SPCR |= ( 1 << CPOL );
}
else
{
SPCR &= ~( 1 << CPOL );
}
2016-05-26 21:40:24 +02:00
}
//////////////////////////////////////////////////////////////////////////
void SPI::setCPHA( bool bCPHA )
2016-05-26 21:40:24 +02:00
{
if( bCPHA )
{
SPCR |= ( 1 << CPHA );
}
else
{
SPCR &= ~( 1 << CPHA );
}
2016-05-26 21:40:24 +02:00
}
//////////////////////////////////////////////////////////////////////////
void SPI::init( ClockDiv enmClockDiv /* = ClockDiv::CLKDIV_128 */, Mode enmMode /* = Mode::MODE_0 */, bool bMaster /* = true */, bool bLSBFirst /* = false */ )
2016-05-26 21:40:24 +02:00
{
setClockDiv( enmClockDiv );
setMode( enmMode );
setMaster( bMaster );
setBitOrder( bLSBFirst );
2016-05-26 21:40:24 +02:00
SPCR |= ( 1 << SPE );
if( bMaster )
{
InOut::writePin( sm_enmSS, true );
2016-05-26 21:40:24 +02:00
InOut::setPinDirection( sm_enmSCK, InOut::Dir::D_OUT, false );
InOut::setPinDirection( sm_enmMISO, InOut::Dir::D_IN, false );
InOut::setPinDirection( sm_enmMOSI, InOut::Dir::D_OUT, false );
InOut::setPinDirection( sm_enmSS, InOut::Dir::D_OUT, false );
}
else
{
InOut::setPinDirection( sm_enmSCK, InOut::Dir::D_IN, false );
InOut::setPinDirection( sm_enmMISO, InOut::Dir::D_OUT, false );
InOut::setPinDirection( sm_enmMOSI, InOut::Dir::D_IN, false );
InOut::setPinDirection( sm_enmSS, InOut::Dir::D_IN, true );
}
2016-05-26 21:40:24 +02:00
}
//////////////////////////////////////////////////////////////////////////
void SPI::setClockDiv( ClockDiv enmClockDiv )
2016-05-26 21:40:24 +02:00
{
uint8_t ui8ClockDiv = static_cast<uint8_t>( enmClockDiv );
if( ui8ClockDiv & 1 )
{
SPCR |= ( 1 << SPR0 );
}
else
{
SPCR &= ~( 1 << SPR0 );
}
2016-05-26 21:40:24 +02:00
if( ui8ClockDiv & ( 1 << 1 ) )
{
SPCR |= ( 1 << SPR1 );
}
else
{
SPCR &= ~( 1 << SPR1 );
}
2016-05-26 21:40:24 +02:00
if( ui8ClockDiv & ( 1 << 2 ) )
{
SPSR |= ( 1 << SPI2X );
}
else
{
SPSR &= ~( 1 << SPI2X );
}
}
//////////////////////////////////////////////////////////////////////////
void SPI::setMode( Mode enmMode )
{
if( enmMode == Mode::MODE_0 || enmMode == Mode::MODE_1 )
{
setCPOL( false );
}
else
{
setCPOL( true );
}
if( enmMode == Mode::MODE_0 || enmMode == Mode::MODE_2 )
{
setCPHA( false );
}
else
{
setCPHA( true );
}
}
//////////////////////////////////////////////////////////////////////////
void SPI::setMaster( bool bMaster )
{
if( bMaster )
{
SPCR |= ( 1 << MSTR );
}
else
{
SPCR &= ~( 1 << MSTR );
}
2016-05-26 21:40:24 +02:00
}
//////////////////////////////////////////////////////////////////////////
void SPI::setBitOrder( bool bLSBFirst )
2016-05-26 21:40:24 +02:00
{
if( bLSBFirst )
{
SPCR |= ( 1 << DORD );
}
else
{
SPCR &= ~( 1 << DORD );
}
}
//////////////////////////////////////////////////////////////////////////
uint8_t SPI::transfer( uint8_t ui8Data )
2016-05-26 21:40:24 +02:00
{
SPDR = ui8Data;
while( !( SPSR & ( 1 << SPIF ) ) );
return SPDR;
}
//////////////////////////////////////////////////////////////////////////
void SPI::select( bool bSelect )
{
InOut::writePin( sm_enmSS, !bSelect );
2016-05-26 21:40:24 +02:00
}