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

Fix SPI 16-bit transfers (#1882)

Fixes #1879
Fixes #1874
parent 280fc437
...@@ -18,6 +18,7 @@ beginTransaction KEYWORD2 ...@@ -18,6 +18,7 @@ beginTransaction KEYWORD2
endTransaction KEYWORD2 endTransaction KEYWORD2
SPISettings KEYWORD2 SPISettings KEYWORD2
transfer KEYWORD2 transfer KEYWORD2
transfer16 KEYWORD2
setBitOrder KEYWORD2 setBitOrder KEYWORD2
setDataMode KEYWORD2 setDataMode KEYWORD2
setClockDivider KEYWORD2 setClockDivider KEYWORD2
......
...@@ -111,6 +111,7 @@ byte SPIClassRP2040::transfer(uint8_t data) { ...@@ -111,6 +111,7 @@ byte SPIClassRP2040::transfer(uint8_t data) {
} }
data = (_spis.getBitOrder() == MSBFIRST) ? data : reverseByte(data); data = (_spis.getBitOrder() == MSBFIRST) ? data : reverseByte(data);
DEBUGSPI("SPI::transfer(%02x), cpol=%d, cpha=%d\n", data, cpol(), cpha()); DEBUGSPI("SPI::transfer(%02x), cpol=%d, cpha=%d\n", data, cpol(), cpha());
hw_write_masked(&spi_get_hw(_spi)->cr0, (8 - 1) << SPI_SSPCR0_DSS_LSB, SPI_SSPCR0_DSS_BITS); // Fast set to 8-bits
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);
DEBUGSPI("SPI: read back %02x\n", ret); DEBUGSPI("SPI: read back %02x\n", ret);
...@@ -124,11 +125,8 @@ uint16_t SPIClassRP2040::transfer16(uint16_t data) { ...@@ -124,11 +125,8 @@ uint16_t SPIClassRP2040::transfer16(uint16_t data) {
} }
data = (_spis.getBitOrder() == MSBFIRST) ? data : reverse16Bit(data); data = (_spis.getBitOrder() == MSBFIRST) ? data : reverse16Bit(data);
DEBUGSPI("SPI::transfer16(%04x), cpol=%d, cpha=%d\n", data, cpol(), cpha()); DEBUGSPI("SPI::transfer16(%04x), cpol=%d, cpha=%d\n", data, cpol(), cpha());
uint8_t d[2]; hw_write_masked(&spi_get_hw(_spi)->cr0, (16 - 1) << SPI_SSPCR0_DSS_LSB, SPI_SSPCR0_DSS_BITS); // Fast set to 16-bits
d[0] = (data >> 8) & 0xff; spi_write16_read16_blocking(_spi, &data, &ret, 1);
d[1] = data & 0xff;
spi_write_read_blocking(_spi, d, d, 2);
ret = ((d[0] << 8) | (d[1] & 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;
...@@ -149,6 +147,8 @@ void SPIClassRP2040::transfer(const void *txbuf, void *rxbuf, size_t count) { ...@@ -149,6 +147,8 @@ void SPIClassRP2040::transfer(const void *txbuf, void *rxbuf, size_t count) {
return; return;
} }
hw_write_masked(&spi_get_hw(_spi)->cr0, (8 - 1) << SPI_SSPCR0_DSS_LSB, SPI_SSPCR0_DSS_BITS); // Fast set to 8-bits
DEBUGSPI("SPI::transfer(%p, %p, %d)\n", txbuf, rxbuf, count); DEBUGSPI("SPI::transfer(%p, %p, %d)\n", txbuf, rxbuf, count);
const uint8_t *txbuff = reinterpret_cast<const uint8_t *>(txbuf); const uint8_t *txbuff = reinterpret_cast<const uint8_t *>(txbuf);
uint8_t *rxbuff = reinterpret_cast<uint8_t *>(rxbuf); uint8_t *rxbuff = reinterpret_cast<uint8_t *>(rxbuf);
......
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