Skip to content

Optimize SPI transfers #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 2 additions & 15 deletions cores/arduino/SERCOM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,24 +300,11 @@ void SERCOM::setClockModeSPI(SercomSpiClockMode clockMode)
enableSPI();
}

void SERCOM::writeDataSPI(uint8_t data)
uint8_t SERCOM::transferDataSPI(uint8_t data)
{
while( sercom->SPI.INTFLAG.bit.DRE == 0 )
{
// Waiting Data Registry Empty
}

sercom->SPI.DATA.bit.DATA = data; // Writing data into Data register

while( sercom->SPI.INTFLAG.bit.TXC == 0 || sercom->SPI.INTFLAG.bit.DRE == 0 )
{
// Waiting Complete Transmission
}
}

uint16_t SERCOM::readDataSPI()
{
while( sercom->SPI.INTFLAG.bit.DRE == 0 || sercom->SPI.INTFLAG.bit.RXC == 0 )
while( sercom->SPI.INTFLAG.bit.RXC == 0 )
{
// Waiting Complete Reception
}
Expand Down
3 changes: 1 addition & 2 deletions cores/arduino/SERCOM.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ class SERCOM
SercomDataOrder getDataOrderSPI( void ) ;
void setBaudrateSPI(uint8_t divider) ;
void setClockModeSPI(SercomSpiClockMode clockMode) ;
void writeDataSPI(uint8_t data) ;
uint16_t readDataSPI( void ) ;
uint8_t transferDataSPI(uint8_t data) ;
bool isBufferOverflowErrorSPI( void ) ;
bool isDataRegisterEmptySPI( void ) ;
bool isTransmitCompleteSPI( void ) ;
Expand Down
15 changes: 10 additions & 5 deletions libraries/SPI/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,7 @@ void SPIClass::setClockDivider(uint8_t div)

byte SPIClass::transfer(uint8_t data)
{
// Writing the data
_p_sercom->writeDataSPI(data);

// Read data
return _p_sercom->readDataSPI() & 0xFF;
return _p_sercom->transferDataSPI(data);
}

uint16_t SPIClass::transfer16(uint16_t data) {
Expand All @@ -210,6 +206,15 @@ uint16_t SPIClass::transfer16(uint16_t data) {
return t.val;
}

void SPIClass::transfer(void *buf, size_t count)
{
uint8_t *buffer = reinterpret_cast<uint8_t *>(buf);
for (size_t i=0; i<count; i++) {
*buffer = transfer(*buffer);
buffer++;
}
}

void SPIClass::attachInterrupt() {
// Should be enableInterrupt()
}
Expand Down
10 changes: 1 addition & 9 deletions libraries/SPI/SPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class SPIClass {

byte transfer(uint8_t data);
uint16_t transfer16(uint16_t data);
inline void transfer(void *buf, size_t count);
void transfer(void *buf, size_t count);

// Transaction Functions
void usingInterrupt(int interruptNumber);
Expand Down Expand Up @@ -132,14 +132,6 @@ class SPIClass {
uint32_t interruptMask;
};

void SPIClass::transfer(void *buf, size_t count)
{
// TODO: Optimize for faster block-transfer
uint8_t *buffer = reinterpret_cast<uint8_t *>(buf);
for (size_t i=0; i<count; i++)
buffer[i] = transfer(buffer[i]);
}

#if SPI_INTERFACES_COUNT > 0
extern SPIClass SPI;
#endif
Expand Down