Skip to content

Commit e77ae8f

Browse files
committed
Optimize SPI transfers
1 parent 166df7c commit e77ae8f

File tree

4 files changed

+14
-31
lines changed

4 files changed

+14
-31
lines changed

cores/arduino/SERCOM.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -300,24 +300,11 @@ void SERCOM::setClockModeSPI(SercomSpiClockMode clockMode)
300300
enableSPI();
301301
}
302302

303-
void SERCOM::writeDataSPI(uint8_t data)
303+
uint8_t SERCOM::transferDataSPI(uint8_t data)
304304
{
305-
while( sercom->SPI.INTFLAG.bit.DRE == 0 )
306-
{
307-
// Waiting Data Registry Empty
308-
}
309-
310305
sercom->SPI.DATA.bit.DATA = data; // Writing data into Data register
311306

312-
while( sercom->SPI.INTFLAG.bit.TXC == 0 || sercom->SPI.INTFLAG.bit.DRE == 0 )
313-
{
314-
// Waiting Complete Transmission
315-
}
316-
}
317-
318-
uint16_t SERCOM::readDataSPI()
319-
{
320-
while( sercom->SPI.INTFLAG.bit.DRE == 0 || sercom->SPI.INTFLAG.bit.RXC == 0 )
307+
while( sercom->SPI.INTFLAG.bit.RXC == 0 )
321308
{
322309
// Waiting Complete Reception
323310
}

cores/arduino/SERCOM.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,7 @@ class SERCOM
175175
SercomDataOrder getDataOrderSPI( void ) ;
176176
void setBaudrateSPI(uint8_t divider) ;
177177
void setClockModeSPI(SercomSpiClockMode clockMode) ;
178-
void writeDataSPI(uint8_t data) ;
179-
uint16_t readDataSPI( void ) ;
178+
uint8_t transferDataSPI(uint8_t data) ;
180179
bool isBufferOverflowErrorSPI( void ) ;
181180
bool isDataRegisterEmptySPI( void ) ;
182181
bool isTransmitCompleteSPI( void ) ;

libraries/SPI/SPI.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,7 @@ void SPIClass::setClockDivider(uint8_t div)
187187

188188
byte SPIClass::transfer(uint8_t data)
189189
{
190-
// Writing the data
191-
_p_sercom->writeDataSPI(data);
192-
193-
// Read data
194-
return _p_sercom->readDataSPI() & 0xFF;
190+
return _p_sercom->transferDataSPI(data);
195191
}
196192

197193
uint16_t SPIClass::transfer16(uint16_t data) {
@@ -210,6 +206,15 @@ uint16_t SPIClass::transfer16(uint16_t data) {
210206
return t.val;
211207
}
212208

209+
void SPIClass::transfer(void *buf, size_t count)
210+
{
211+
uint8_t *buffer = reinterpret_cast<uint8_t *>(buf);
212+
for (size_t i=0; i<count; i++) {
213+
*buffer = transfer(*buffer);
214+
buffer++;
215+
}
216+
}
217+
213218
void SPIClass::attachInterrupt() {
214219
// Should be enableInterrupt()
215220
}

libraries/SPI/SPI.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class SPIClass {
9696

9797
byte transfer(uint8_t data);
9898
uint16_t transfer16(uint16_t data);
99-
inline void transfer(void *buf, size_t count);
99+
void transfer(void *buf, size_t count);
100100

101101
// Transaction Functions
102102
void usingInterrupt(int interruptNumber);
@@ -132,14 +132,6 @@ class SPIClass {
132132
uint32_t interruptMask;
133133
};
134134

135-
void SPIClass::transfer(void *buf, size_t count)
136-
{
137-
// TODO: Optimize for faster block-transfer
138-
uint8_t *buffer = reinterpret_cast<uint8_t *>(buf);
139-
for (size_t i=0; i<count; i++)
140-
buffer[i] = transfer(buffer[i]);
141-
}
142-
143135
#if SPI_INTERFACES_COUNT > 0
144136
extern SPIClass SPI;
145137
#endif

0 commit comments

Comments
 (0)