SPI macht interrupt

This commit is contained in:
Tristan Krause 2019-04-03 11:38:15 +02:00
parent 266d90df56
commit fd2cb56ef3
11 changed files with 605 additions and 538 deletions

View file

@ -6,7 +6,6 @@ SPI::SPI(void)
void SPI::init(void) const volatile
{
// Konfiguriere SPI DDRs
DDRB |= _BV(SLSL) | _BV(MOSI) | _BV(SCLK);
DDRB &= ~_BV(MISO);
@ -16,13 +15,60 @@ void SPI::init(void) const volatile
// aktiviere SPI, Master Modus, SPI Modus 0
// F_SPI = F_CPU / 2 (prescaler 2)
SPCR = _BV(SPE) | _BV(MSTR);
SPSR = _BV(SPI2X);
SPCR = _BV(SPE) | _BV(MSTR) | _BV(SPIE);
SPSR = _BV(SPI2X) | _BV(SPIF);
// waehle keinen SPI Slave aus
setAdr(SPIADR::NONE);
}
void SPI::handleTransfer() volatile
{
if(!active)
{
setAdr(SPIADR::NONE);
return;
}
uint8_t next = buffer[index];
buffer[index++] = SPDR;
if(index >= length)
{
index = 0;
active = false;
}
SPDR = next;
}
void SPI::transfer(uint8_t adr) volatile
{
if(!index)
return;
wait();
active = true;
setAdr(adr);
this->length = index;
this->index = 0;
handleTransfer();
}
void SPI::addByte(uint8_t b) volatile
{
wait();
buffer[index++] = b;
}
void SPI::wait() volatile
{
while(active);
}
void SPI::setAdr(uint8_t adr) const volatile
{
PORTD &= ~(_BV(DMUX1) | _BV(DMUX2) | _BV(DMUX3));
@ -30,10 +76,3 @@ void SPI::setAdr(uint8_t adr) const volatile
PORTD |= (adr & 0x02) ? _BV(DMUX2) : 0;
PORTD |= (adr & 0x04) ? _BV(DMUX3) : 0;
}
uint8_t SPI::pushByte(uint8_t b) const volatile
{
SPDR = b;
while(!(SPSR & _BV(SPIF)));
return SPDR;
}