Very minor SPI speedup
This commit is contained in:
parent
422dd6666e
commit
758c6c9464
3 changed files with 47 additions and 60 deletions
|
@ -27,8 +27,6 @@
|
|||
|
||||
/**
|
||||
* Description: HAL for AVR - SPI functions
|
||||
*
|
||||
* For __AVR__
|
||||
*/
|
||||
|
||||
#ifdef __AVR__
|
||||
|
@ -68,10 +66,12 @@ void spiBegin (void) {
|
|||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
#if DISABLED(SOFTWARE_SPI)
|
||||
// functions for hardware SPI
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Hardware SPI
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
// make sure SPCR rate is in expected bits
|
||||
#if (SPR0 != 0 || SPR1 != 1)
|
||||
#error "unexpected SPCR bits"
|
||||
|
@ -95,14 +95,13 @@ void spiBegin (void) {
|
|||
SPSR = spiRate & 1 || spiRate == 6 ? 0 : _BV(SPI2X);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/** SPI receive a byte */
|
||||
uint8_t spiRec(void) {
|
||||
SPDR = 0xFF;
|
||||
while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
|
||||
return SPDR;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/** SPI read data */
|
||||
void spiRead(uint8_t* buf, uint16_t nbyte) {
|
||||
if (nbyte-- == 0) return;
|
||||
|
@ -115,13 +114,13 @@ void spiBegin (void) {
|
|||
while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
|
||||
buf[nbyte] = SPDR;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/** SPI send a byte */
|
||||
void spiSend(uint8_t b) {
|
||||
SPDR = b;
|
||||
while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/** SPI send block */
|
||||
void spiSendBlock(uint8_t token, const uint8_t* buf) {
|
||||
SPDR = token;
|
||||
|
@ -191,25 +190,21 @@ void spiBegin (void) {
|
|||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
#else // SOFTWARE_SPI
|
||||
//------------------------------------------------------------------------------
|
||||
#else
|
||||
|
||||
/** nop to tune soft SPI timing */
|
||||
#define nop asm volatile ("\tnop\n")
|
||||
|
||||
/** Set SPI rate */
|
||||
void spiInit(uint8_t spiRate) {
|
||||
// nothing to do
|
||||
UNUSED(spiRate);
|
||||
UNUSED(spiRate); // nothing to do
|
||||
}
|
||||
|
||||
/** Begin SPI transaction, set clock, bit order, data mode */
|
||||
void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) {
|
||||
// nothing to do
|
||||
UNUSED(spiBeginTransaction);
|
||||
UNUSED(spiBeginTransaction); // nothing to do
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/** Soft SPI receive byte */
|
||||
uint8_t spiRec() {
|
||||
uint8_t data = 0;
|
||||
|
@ -221,8 +216,7 @@ void spiBegin (void) {
|
|||
for (uint8_t i = 0; i < 8; i++) {
|
||||
WRITE(SCK_PIN, HIGH);
|
||||
|
||||
// adjust so SCK is nice
|
||||
nop;
|
||||
nop; // adjust so SCK is nice
|
||||
nop;
|
||||
|
||||
data <<= 1;
|
||||
|
@ -231,48 +225,45 @@ void spiBegin (void) {
|
|||
|
||||
WRITE(SCK_PIN, LOW);
|
||||
}
|
||||
// enable interrupts
|
||||
|
||||
sei();
|
||||
return data;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/** Soft SPI read data */
|
||||
void spiRead(uint8_t* buf, uint16_t nbyte) {
|
||||
for (uint16_t i = 0; i < nbyte; i++)
|
||||
buf[i] = spiRec();
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/** Soft SPI send byte */
|
||||
void spiSend(uint8_t data) {
|
||||
// no interrupts during byte send - about 8µs
|
||||
cli();
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
WRITE(SCK_PIN, LOW);
|
||||
|
||||
WRITE(MOSI_PIN, data & 0x80);
|
||||
|
||||
data <<= 1;
|
||||
|
||||
WRITE(SCK_PIN, HIGH);
|
||||
}
|
||||
// hold SCK high for a few ns
|
||||
nop;
|
||||
|
||||
nop; // hold SCK high for a few ns
|
||||
nop;
|
||||
nop;
|
||||
nop;
|
||||
|
||||
WRITE(SCK_PIN, LOW);
|
||||
// enable interrupts
|
||||
|
||||
sei();
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/** Soft SPI send block */
|
||||
void spiSendBlock(uint8_t token, const uint8_t* buf) {
|
||||
spiSend(token);
|
||||
for (uint16_t i = 0; i < 512; i++)
|
||||
spiSend(buf[i]);
|
||||
}
|
||||
|
||||
#endif // SOFTWARE_SPI
|
||||
|
||||
|
||||
#endif // __AVR__
|
||||
|
|
|
@ -656,8 +656,8 @@
|
|||
|
||||
// Read from SPI into buffer
|
||||
void spiRead(uint8_t* buf, uint16_t nbyte) {
|
||||
if (nbyte-- == 0) return;
|
||||
|
||||
if (!nbyte) return;
|
||||
--nbyte;
|
||||
for (int i = 0; i < nbyte; i++) {
|
||||
//WHILE_TX(0);
|
||||
SPI0->SPI_TDR = 0x000000FF | SPI_PCS(SPI_CHAN);
|
||||
|
@ -669,7 +669,7 @@
|
|||
}
|
||||
|
||||
// Write single byte to SPI
|
||||
void spiSend(byte b) {
|
||||
void spiSend(const byte b) {
|
||||
// write byte with address and end transmission flag
|
||||
SPI0->SPI_TDR = (uint32_t)b | SPI_PCS(SPI_CHAN) | SPI_TDR_LASTXFER;
|
||||
WHILE_TX(0);
|
||||
|
@ -678,16 +678,17 @@
|
|||
//DELAY_US(1U);
|
||||
}
|
||||
|
||||
void spiSend(const uint8_t* buf, size_t n) {
|
||||
if (n == 0) return;
|
||||
for (size_t i = 0; i < n - 1; i++) {
|
||||
void spiSend(const uint8_t* buf, size_t nbyte) {
|
||||
if (!nbyte) return;
|
||||
--nbyte;
|
||||
for (size_t i = 0; i < nbyte; i++) {
|
||||
SPI0->SPI_TDR = (uint32_t)buf[i] | SPI_PCS(SPI_CHAN);
|
||||
WHILE_TX(0);
|
||||
WHILE_RX(0);
|
||||
SPI0->SPI_RDR;
|
||||
//DELAY_US(1U);
|
||||
}
|
||||
spiSend(buf[n - 1]);
|
||||
spiSend(buf[nbyte]);
|
||||
}
|
||||
|
||||
void spiSend(uint32_t chan, byte b) {
|
||||
|
@ -698,15 +699,16 @@
|
|||
FLUSH_RX();
|
||||
}
|
||||
|
||||
void spiSend(uint32_t chan, const uint8_t* buf, size_t n) {
|
||||
if (n == 0) return;
|
||||
for (int i = 0; i < (int)n - 1; i++) {
|
||||
void spiSend(uint32_t chan, const uint8_t* buf, size_t nbyte) {
|
||||
if (!nbyte) return;
|
||||
--nbyte;
|
||||
for (size_t i = 0; i < nbyte; i++) {
|
||||
WHILE_TX(0);
|
||||
SPI0->SPI_TDR = (uint32_t)buf[i] | SPI_PCS(chan);
|
||||
WHILE_RX(0);
|
||||
FLUSH_RX();
|
||||
}
|
||||
spiSend(chan, buf[n - 1]);
|
||||
spiSend(chan, buf[nbyte]);
|
||||
}
|
||||
|
||||
// Write from buffer to SPI
|
||||
|
@ -775,7 +777,6 @@
|
|||
uint8_t spiRec() { return (uint8_t)spiTransfer(0xFF); }
|
||||
|
||||
void spiRead(uint8_t* buf, uint16_t nbyte) {
|
||||
if (nbyte)
|
||||
for (int i = 0; i < nbyte; i++)
|
||||
buf[i] = spiTransfer(0xFF);
|
||||
}
|
||||
|
@ -783,7 +784,6 @@
|
|||
void spiSend(uint8_t data) { spiTransfer(data); }
|
||||
|
||||
void spiSend(const uint8_t* buf, size_t nbyte) {
|
||||
if (nbyte)
|
||||
for (uint16_t i = 0; i < nbyte; i++)
|
||||
spiTransfer(buf[i]);
|
||||
}
|
||||
|
|
|
@ -81,16 +81,14 @@
|
|||
uint8_t spiRec() { return spiTransfer(0xFF); }
|
||||
|
||||
void spiRead(uint8_t*buf, uint16_t nbyte) {
|
||||
if (nbyte)
|
||||
for (int i = 0; i < nbyte; i++)
|
||||
buf[i] = spiTransfer(0xFF);
|
||||
}
|
||||
|
||||
void spiSend(uint8_t b) { (void)spiTransfer(b); }
|
||||
|
||||
void spiSend(const uint8_t* buf, size_t n) {
|
||||
if (n)
|
||||
for (uint16_t i = 0; i < n; i++)
|
||||
void spiSend(const uint8_t* buf, size_t nbyte) {
|
||||
for (uint16_t i = 0; i < nbyte; i++)
|
||||
(void)spiTransfer(buf[i]);
|
||||
}
|
||||
|
||||
|
@ -178,15 +176,14 @@
|
|||
|
||||
void spiSend(uint8_t b) { doio(b); }
|
||||
|
||||
void spiSend(const uint8_t* buf, size_t n) {
|
||||
if (n)
|
||||
for (uint16_t i = 0; i < n; i++) doio(buf[i]);
|
||||
void spiSend(const uint8_t* buf, size_t nbyte) {
|
||||
for (uint16_t i = 0; i < nbyte; i++) doio(buf[i]);
|
||||
}
|
||||
|
||||
void spiSend(uint32_t chan, byte b) {
|
||||
}
|
||||
|
||||
void spiSend(uint32_t chan, const uint8_t* buf, size_t n) {
|
||||
void spiSend(uint32_t chan, const uint8_t* buf, size_t nbyte) {
|
||||
}
|
||||
|
||||
// Read single byte from SPI
|
||||
|
@ -195,9 +192,8 @@
|
|||
uint8_t spiRec(uint32_t chan) { return 0; }
|
||||
|
||||
// Read from SPI into buffer
|
||||
void spiRead(uint8_t*buf, uint16_t nbyte) {
|
||||
if (nbyte)
|
||||
for (int i = 0; i < nbyte; i++) buf[i] = doio(0xff);
|
||||
void spiRead(uint8_t *buf, uint16_t nbyte) {
|
||||
for (uint16_t i = 0; i < nbyte; i++) buf[i] = doio(0xFF);
|
||||
}
|
||||
|
||||
static uint8_t spiTransfer(uint8_t b) {
|
||||
|
|
Reference in a new issue