Skip to content

Add SPI.transfer16(...) API's #59

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 2 commits into from
Nov 4, 2015
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
5 changes: 5 additions & 0 deletions cores/arduino/SERCOM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ void SERCOM::setDataOrderSPI(SercomDataOrder dataOrder)
enableSPI();
}

SercomDataOrder SERCOM::getDataOrderSPI()
{
return (sercom->SPI.CTRLA.bit.DORD ? LSB_FIRST : MSB_FIRST);
}

void SERCOM::setBaudrateSPI(uint8_t divider)
{
//Can't divide by 0
Expand Down
1 change: 1 addition & 0 deletions cores/arduino/SERCOM.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ class SERCOM
void enableSPI( void ) ;
void disableSPI( void ) ;
void setDataOrderSPI(SercomDataOrder dataOrder) ;
SercomDataOrder getDataOrderSPI( void ) ;
void setBaudrateSPI(uint8_t divider) ;
void setClockModeSPI(SercomSpiClockMode clockMode) ;
void writeDataSPI(uint8_t data) ;
Expand Down
16 changes: 16 additions & 0 deletions libraries/SPI/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,22 @@ byte SPIClass::transfer(uint8_t data)
return _p_sercom->readDataSPI() & 0xFF;
}

uint16_t SPIClass::transfer16(uint16_t data) {
union { uint16_t val; struct { uint8_t lsb; uint8_t msb; }; } t;

t.val = data;

if (_p_sercom->getDataOrderSPI() == LSB_FIRST) {
t.lsb = transfer(t.lsb);
t.msb = transfer(t.msb);
} else {
t.msb = transfer(t.msb);
t.lsb = transfer(t.lsb);
}

return t.val;
}

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


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

// Transaction Functions
Expand Down