Skip to content

Missing SPI Transaction API methods on Due (SAM3X) #2299

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 4 commits into from
Sep 12, 2014
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
82 changes: 67 additions & 15 deletions hardware/arduino/sam/libraries/SPI/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ void SPIClass::init() {
#define interruptsStatus() __interruptsStatus()
static inline unsigned char __interruptsStatus(void) __attribute__((always_inline, unused));
static inline unsigned char __interruptsStatus(void) {
unsigned int primask, faultmask;
asm volatile ("mrs %0, primask" : "=r" (primask));
if (primask) return 0;
asm volatile ("mrs %0, faultmask" : "=r" (faultmask));
if (faultmask) return 0;
return 1;
unsigned int primask, faultmask;
asm volatile ("mrs %0, primask" : "=r" (primask));
if (primask) return 0;
asm volatile ("mrs %0, faultmask" : "=r" (faultmask));
if (faultmask) return 0;
return 1;
}
#endif

Expand Down Expand Up @@ -182,18 +182,70 @@ byte SPIClass::transfer(byte _pin, uint8_t _data, SPITransferMode _mode) {
d |= SPI_TDR_LASTXFER;

// SPI_Write(spi, _channel, _data);
while ((spi->SPI_SR & SPI_SR_TDRE) == 0)
;
spi->SPI_TDR = d;

// return SPI_Read(spi);
while ((spi->SPI_SR & SPI_SR_RDRF) == 0)
;
d = spi->SPI_RDR;
while ((spi->SPI_SR & SPI_SR_TDRE) == 0)
;
spi->SPI_TDR = d;

// return SPI_Read(spi);
while ((spi->SPI_SR & SPI_SR_RDRF) == 0)
;
d = spi->SPI_RDR;
// Reverse bit order
if (bitOrder[ch] == LSBFIRST)
d = __REV(__RBIT(d));
return d & 0xFF;
return d & 0xFF;
}

void SPIClass::transfer(byte _pin, void *_buf, size_t _count, SPITransferMode _mode) {
if (_count == 0)
return;

uint8_t *buffer = (uint8_t *)_buf;
if (_count == 1) {
*buffer = transfer(_pin, *buffer, _mode);
return;
}

uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
bool reverse = (bitOrder[ch] == LSBFIRST);

// Send the first byte
uint32_t d = *buffer;
if (reverse)
d = __REV(__RBIT(d));
while ((spi->SPI_SR & SPI_SR_TDRE) == 0)
;
spi->SPI_TDR = d | SPI_PCS(ch);

while (_count > 1) {
// Prepare next byte
d = *(buffer+1);
if (reverse)
d = __REV(__RBIT(d));
if (_count == 2 && _mode == SPI_LAST)
d |= SPI_TDR_LASTXFER;

// Read transferred byte and send next one straight away
while ((spi->SPI_SR & SPI_SR_RDRF) == 0)
;
uint8_t r = spi->SPI_RDR;
spi->SPI_TDR = d | SPI_PCS(ch);

// Save read byte
if (reverse)
r = __REV(__RBIT(r));
*buffer = r;
buffer++;
_count--;
}

// Receive the last transferred byte
while ((spi->SPI_SR & SPI_SR_RDRF) == 0)
;
uint8_t r = spi->SPI_RDR;
if (reverse)
r = __REV(__RBIT(r));
*buffer = r;
}

void SPIClass::attachInterrupt(void) {
Expand Down
Loading