Unverified Commit 52d50da5 authored by Earle F. Philhower, III's avatar Earle F. Philhower, III Committed by GitHub

Avoid calling spi_set_format during SPI transfers (#1762)

Avoid potential interaction with Pico SDK 1.5.1 update that causes hiccups in
SPI transmissions.  SPI.transfer16 to use 8-bit transfers.

Fixes #1760
parent 47111a6e
...@@ -110,7 +110,6 @@ byte SPIClassRP2040::transfer(uint8_t data) { ...@@ -110,7 +110,6 @@ byte SPIClassRP2040::transfer(uint8_t data) {
return 0; return 0;
} }
data = (_spis.getBitOrder() == MSBFIRST) ? data : reverseByte(data); data = (_spis.getBitOrder() == MSBFIRST) ? data : reverseByte(data);
spi_set_format(_spi, 8, cpol(), cpha(), SPI_MSB_FIRST);
DEBUGSPI("SPI::transfer(%02x), cpol=%d, cpha=%d\n", data, cpol(), cpha()); DEBUGSPI("SPI::transfer(%02x), cpol=%d, cpha=%d\n", data, cpol(), cpha());
spi_write_read_blocking(_spi, &data, &ret, 1); spi_write_read_blocking(_spi, &data, &ret, 1);
ret = (_spis.getBitOrder() == MSBFIRST) ? ret : reverseByte(ret); ret = (_spis.getBitOrder() == MSBFIRST) ? ret : reverseByte(ret);
...@@ -124,9 +123,13 @@ uint16_t SPIClassRP2040::transfer16(uint16_t data) { ...@@ -124,9 +123,13 @@ uint16_t SPIClassRP2040::transfer16(uint16_t data) {
return 0; return 0;
} }
data = (_spis.getBitOrder() == MSBFIRST) ? data : reverse16Bit(data); data = (_spis.getBitOrder() == MSBFIRST) ? data : reverse16Bit(data);
spi_set_format(_spi, 16, cpol(), cpha(), SPI_MSB_FIRST);
DEBUGSPI("SPI::transfer16(%04x), cpol=%d, cpha=%d\n", data, cpol(), cpha()); DEBUGSPI("SPI::transfer16(%04x), cpol=%d, cpha=%d\n", data, cpol(), cpha());
spi_write16_read16_blocking(_spi, &data, &ret, 1); uint8_t msb, lsb;
msb = (data >> 8) & 0xff;
lsb = data & 0xff;
spi_write_read_blocking(_spi, &msb, &msb, 1);
spi_write_read_blocking(_spi, &lsb, &lsb, 1);
ret = ((msb << 8) | (lsb & 0xff)) & 0xffff;
ret = (_spis.getBitOrder() == MSBFIRST) ? ret : reverse16Bit(ret); ret = (_spis.getBitOrder() == MSBFIRST) ? ret : reverse16Bit(ret);
DEBUGSPI("SPI: read back %02x\n", ret); DEBUGSPI("SPI: read back %02x\n", ret);
return ret; return ret;
...@@ -153,8 +156,6 @@ void SPIClassRP2040::transfer(const void *txbuf, void *rxbuf, size_t count) { ...@@ -153,8 +156,6 @@ void SPIClassRP2040::transfer(const void *txbuf, void *rxbuf, size_t count) {
// MSB version is easy! // MSB version is easy!
if (_spis.getBitOrder() == MSBFIRST) { if (_spis.getBitOrder() == MSBFIRST) {
spi_set_format(_spi, 8, cpol(), cpha(), SPI_MSB_FIRST);
if (rxbuf == nullptr) { // transmit only! if (rxbuf == nullptr) { // transmit only!
spi_write_blocking(_spi, txbuff, count); spi_write_blocking(_spi, txbuff, count);
return; return;
...@@ -191,6 +192,7 @@ void SPIClassRP2040::beginTransaction(SPISettings settings) { ...@@ -191,6 +192,7 @@ void SPIClassRP2040::beginTransaction(SPISettings settings) {
} }
DEBUGSPI("SPI: initting SPI\n"); DEBUGSPI("SPI: initting SPI\n");
spi_init(_spi, _spis.getClockFreq()); spi_init(_spi, _spis.getClockFreq());
spi_set_format(_spi, 8, cpol(), cpha(), SPI_MSB_FIRST);
DEBUGSPI("SPI: actual baudrate=%u\n", spi_get_baudrate(_spi)); DEBUGSPI("SPI: actual baudrate=%u\n", spi_get_baudrate(_spi));
_initted = true; _initted = true;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment