From 09f56eb7280b3f3cad7c83361c0796aaac324e51 Mon Sep 17 00:00:00 2001 From: BjoernSch Date: Wed, 10 Aug 2016 20:50:00 +0200 Subject: [PATCH] Fixed byte order in bbSPIXfer Got default endianess wrong and additionally the byte order has to be changed according to bit order on the SPI-Line --- pigpio.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/pigpio.c b/pigpio.c index 9a37053..2ce81da 100644 --- a/pigpio.c +++ b/pigpio.c @@ -9968,6 +9968,7 @@ static uint8_t bbSPIXferByte(wfRx_t *w, char txByte) myGpioWrite(w->S.MOSI, txByte & 0x80); txByte <<= 1; } + set_SCLK(w); SPI_delay(w); @@ -9997,6 +9998,7 @@ static uint8_t bbSPIXferByte(wfRx_t *w, char txByte) myGpioWrite(w->S.MOSI, txByte & 0x80); txByte <<= 1; } + if (PI_SPI_FLAGS_GET_RX_LSB(w->S.spiFlags)) { rxByte = (rxByte >> 1) | myGpioRead(w->S.MISO) << 7; @@ -10204,9 +10206,10 @@ int bbSPIXfer( unsigned len) { int pos, status; + char txByte, rxByte; wfRx_t *w; - DBG(DBG_USER, "gpio=%d inBuf=%s outBuf=%08X len=%d", + DBG(DBG_USER, "CS=%d inBuf=%s outBuf=%08X len=%d", CS, myBuf2Str(len, (char *)inBuf), (int)outBuf, len); CHECK_INITED; @@ -10228,12 +10231,27 @@ int bbSPIXfer( status = 0; bbSPIStart(w); + for (pos=0; pos < len; pos++) { - DBG(DBG_INTERNAL, "pos=%d len=%d sent=%d", - pos, len, inBuf[pos]); - outBuf[pos] = bbSPIXferByte(w, inBuf[pos]); - DBG(DBG_INTERNAL, "recvd=%d", outBuf[pos]); + if (PI_SPI_FLAGS_GET_TX_LSB(w->S.spiFlags)) + { + txByte = inBuf[pos]; + } + else + { + txByte = inBuf[len - pos - 1]; + } + rxByte = bbSPIXferByte(w, txByte); + if (PI_SPI_FLAGS_GET_RX_LSB(w->S.spiFlags)) + { + outBuf[pos] = rxByte; + } + else + { + outBuf[len - pos - 1] = rxByte; + } + DBG(DBG_INTERNAL, "pos=%d len=%d sent=%d recvd=%d", pos, len, txByte, rxByte); } bbSPIStop(w);