Initial commit
This commit is contained in:
commit
7105380682
54
spi.cpp
Normal file
54
spi.cpp
Normal file
@ -0,0 +1,54 @@
|
||||
#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;
|
||||
}
|
34
spi.h
Normal file
34
spi.h
Normal file
@ -0,0 +1,34 @@
|
||||
#ifndef SPI_H
|
||||
#define SPI_H
|
||||
|
||||
#include <avr/io.h>
|
||||
|
||||
enum class SPIClock
|
||||
{
|
||||
CLOCKDIV_4 = 0,
|
||||
CLOCKDIV_16 = 1,
|
||||
CLOCKDIV_64 = 2,
|
||||
CLOCKDIV_128 = 3,
|
||||
|
||||
CLOCKDIV2X_2 = 4,
|
||||
CLOCKDIV2X_8 = 5,
|
||||
CLOCKDIV2X_32 = 6,
|
||||
CLOCKDIV2X_64 = 7
|
||||
};
|
||||
|
||||
enum class SPIMode
|
||||
{
|
||||
MODE0 = 0,
|
||||
MODE1 = 1,
|
||||
MODE2 = 2,
|
||||
MODE3 = 3
|
||||
};
|
||||
|
||||
void spiInit();
|
||||
void spiEnd();
|
||||
void spiSetClockDiv( SPIClock enmClockDiv );
|
||||
void spiSetDataMode( SPIMode enmMode );
|
||||
void spiSetBitOrder( bool bLSBFirst );
|
||||
uint8_t spiTransfer( uint8_t ui8Data );
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user