Skip to content

arduino API's SPI::transfer(void*, size) (optimized) #4925

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 6 commits into from
Jul 27, 2018
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
38 changes: 33 additions & 5 deletions libraries/SPI/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,25 @@ uint16_t SPIClass::transfer16(uint16_t data) {
return out.val;
}

void SPIClass::transfer(void *buf, uint16_t count) {
uint8_t *cbuf = reinterpret_cast<uint8_t*>(buf);

// cbuf may not be 32bits-aligned
for (; (((unsigned long)cbuf) & 3) && count; cbuf++, count--)
*cbuf = transfer(*cbuf);

// cbuf is now aligned
// count may not be a multiple of 4
uint16_t count4 = count & ~3;
transferBytes(cbuf, cbuf, count4);

// finish the last <4 bytes
cbuf += count4;
count -= count4;
for (; count; cbuf++, count--)
*cbuf = transfer(*cbuf);
}

void SPIClass::write(uint8_t data) {
while(SPI1CMD & SPIBUSY) {}
// reset to 8Bit mode
Expand Down Expand Up @@ -511,6 +530,14 @@ void SPIClass::transferBytes(const uint8_t * out, uint8_t * in, uint32_t size) {
}
}

/**
* Note:
* in and out need to be aligned to 32Bit
* or you get an Fatal exception (9)
* @param out uint8_t *
* @param in uint8_t *
* @param size uint8_t (max 64)
*/
void SPIClass::transferBytes_(const uint8_t * out, uint8_t * in, uint8_t size) {
while(SPI1CMD & SPIBUSY) {}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest an ASSERT() here checking the alignment of the inputs would be handy here and print a useful error when debugging was enabled...

// Set in/out Bits to transfer
Expand Down Expand Up @@ -539,12 +566,13 @@ void SPIClass::transferBytes_(const uint8_t * out, uint8_t * in, uint8_t size) {
while(SPI1CMD & SPIBUSY) {}

if(in) {
volatile uint8_t * fifoPtr8 = (volatile uint8_t *) &SPI1W0;
dataSize = size;
uint32_t * dataPtr = (uint32_t*) in;
fifoPtr = &SPI1W0;
dataSize = ((size + 3) / 4);
while(dataSize--) {
*in = *fifoPtr8;
in++;
fifoPtr8++;
*dataPtr = *fifoPtr;
dataPtr++;
fifoPtr++;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions libraries/SPI/SPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class SPIClass {
void beginTransaction(SPISettings settings);
uint8_t transfer(uint8_t data);
uint16_t transfer16(uint16_t data);
void transfer(void *buf, uint16_t count);
void write(uint8_t data);
void write16(uint16_t data);
void write16(uint16_t data, bool msb);
Expand Down