150 lines
2.9 KiB
C++
150 lines
2.9 KiB
C++
#include "spi.h"
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
void SPI::setCPOL( bool bCPOL )
|
|
{
|
|
if( bCPOL )
|
|
{
|
|
SPCR |= ( 1 << CPOL );
|
|
}
|
|
else
|
|
{
|
|
SPCR &= ~( 1 << CPOL );
|
|
}
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
void SPI::setCPHA( bool bCPHA )
|
|
{
|
|
if( bCPHA )
|
|
{
|
|
SPCR |= ( 1 << CPHA );
|
|
}
|
|
else
|
|
{
|
|
SPCR &= ~( 1 << CPHA );
|
|
}
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
void SPI::init( ClockDiv enmClockDiv /* = ClockDiv::CLKDIV_128 */, Mode enmMode /* = Mode::MODE_0 */, bool bMaster /* = true */, bool bLSBFirst /* = false */ )
|
|
{
|
|
setClockDiv( enmClockDiv );
|
|
setMode( enmMode );
|
|
setMaster( bMaster );
|
|
setBitOrder( bLSBFirst );
|
|
|
|
SPCR |= ( 1 << SPE );
|
|
|
|
if( bMaster )
|
|
{
|
|
InOut::writePin( sm_enmSS, true );
|
|
|
|
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 );
|
|
}
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
void SPI::setClockDiv( ClockDiv enmClockDiv )
|
|
{
|
|
uint8_t ui8ClockDiv = static_cast<uint8_t>( enmClockDiv );
|
|
|
|
if( ui8ClockDiv & 1 )
|
|
{
|
|
SPCR |= ( 1 << SPR0 );
|
|
}
|
|
else
|
|
{
|
|
SPCR &= ~( 1 << SPR0 );
|
|
}
|
|
|
|
if( ui8ClockDiv & ( 1 << 1 ) )
|
|
{
|
|
SPCR |= ( 1 << SPR1 );
|
|
}
|
|
else
|
|
{
|
|
SPCR &= ~( 1 << SPR1 );
|
|
}
|
|
|
|
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 );
|
|
}
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
void SPI::setBitOrder( bool bLSBFirst )
|
|
{
|
|
if( bLSBFirst )
|
|
{
|
|
SPCR |= ( 1 << DORD );
|
|
}
|
|
else
|
|
{
|
|
SPCR &= ~( 1 << DORD );
|
|
}
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
uint8_t SPI::transfer( uint8_t ui8Data )
|
|
{
|
|
SPDR = ui8Data;
|
|
while( !( SPSR & ( 1 << SPIF ) ) );
|
|
return SPDR;
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
void SPI::select( bool bSelect )
|
|
{
|
|
InOut::writePin( sm_enmSS, !bSelect );
|
|
} |