8000 Avoid calling spi_set_format during SPI transfers (#1762) · rondlh/arduino-pico@52d50da · GitHub
[go: up one dir, main page]

Skip to content

Commit 52d50da

Browse files
Avoid calling spi_set_format during SPI transfers (earlephilhower#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 earlephilhower#1760
1 parent 47111a6 commit 52d50da

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

libraries/SPI/src/SPI.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ byte SPIClassRP2040::transfer(uint8_t data) {
110110
return 0;
111111
}
112112
data = (_spis.getBitOrder() == MSBFIRST) ? data : reverseByte(data);
113-
spi_set_format(_spi, 8, cpol(), cpha(), SPI_MSB_FIRST);
114113
DEBUGSPI("SPI::transfer(%02x), cpol=%d, cpha=%d\n", data, cpol(), cpha());
115114
spi_write_read_blocking(_spi, &data, &ret, 1);
116115
ret = (_spis.getBitOrder() == MSBFIRST) ? ret : reverseByte(ret);
@@ -124,9 +123,13 @@ uint16_t SPIClassRP2040::transfer16(uint16_t data) {
124123
return 0;
125124
}
126125
data = (_spis.getBitOrder() == MSBFIRST) ? data : reverse16Bit(data);
127-
spi_set_format(_spi, 16, cpol(), cpha(), SPI_MSB_FIRST);
128126
DEBUGSPI("SPI::transfer16(%04x), cpol=%d, cpha=%d\n", data, cpol(), cpha());
129-
spi_write16_read16_blocking(_spi, &data, &ret, 1);
127+
uint8_t msb, lsb;
128+
msb = (data >> 8) & 0xff;
129+
lsb = data & 0xff;
130+
spi_write_read_blocking(_spi, &msb, &msb, 1);
131+
spi_write_read_blocking(_spi, &lsb, &lsb, 1);
132+
ret = ((msb << 8) | (lsb & 0xff)) & 0xffff;
130133
ret = (_spis.getBitOrder() == MSBFIRST) ? ret : reverse16Bit(ret);
131134
DEBUGSPI("SPI: read back %02x\n", ret);
132135
return ret;
@@ -153,8 +156,6 @@ void SPIClassRP2040::transfer(const void *txbuf, void *rxbuf, size_t count) {
153156

154157
// MSB version is easy!
155158
if (_spis.getBitOrder() == MSBFIRST) {
156-
spi_set_format(_spi, 8, cpol(), cpha(), SPI_MSB_FIRST);
157-
158159
if (rxbuf == nullptr) { // transmit only!
159160
spi_write_blocking(_spi, txbuff, count);
160161
return;
@@ -191,6 +192,7 @@ void SPIClassRP2040::beginTransaction(SPISettings settings) {
191192
}
192193
DEBUGSPI("SPI: initting SPI\n");
193194
spi_init(_spi, _spis.getClockFreq());
195+
spi_set_format(_spi, 8, cpol(), cpha(), SPI_MSB_FIRST);
194196
DEBUGSPI("SPI: actual baudrate=%u\n", spi_get_baudrate(_spi));
195197
_initted = true;
196198
}

0 commit comments

Comments
 (0)
0