esp8266 / esp8266/Arduino

BUG: The SPI library does not handle HW CS for other bit lengths than 8.

Open
#2,820 3 comments 0 reactions 0 assignees View on GitHub
waiting for feedback
Dominant language
C++
Stars
16.7k
Forks
13.1k
PR merge metrics
No merged PRs in 30d

Description

BUG: The SPI library does not handle HW SC for other bit lengths than 8.
For example: There is a bug in the routine "transfer16".

Hardware: WEMOS ESP8266

If one uses transfer16 for 16 bit SPI transfer using HW CS, the SS/CS pin goes high after each 8 bit transfer. the 16bit word is therefore transferred as 2 x 8 bit bytes.

For the below to work:
SPI.setHwCs(true); //Use (automatic) HW SS/CS (GPIO 15 , D8)
SPI.setBitOrder(MSBFIRST);
SPI.transfer16(0x1234);

I had to write my own alternative "_transfer16" routine:

uint16_t _bs(uint16_t data) {
return(((data&0xFF)<<8)|((data>>8)&0xFF));
}

void _setDataBits(uint16_t bits) {
const uint32_t mask = ~((SPIMMOSI << SPILMOSI) | (SPIMMISO << SPILMISO));
bits--;
SPI1U1 = ((SPI1U1 & mask) | ((bits << SPILMOSI) | (bits << SPILMISO)));
}

uint16_t _transfer16(uint16_t data) {
if(!(SPI1C & (SPICWBO | SPICRBO))) data=_bs(data);
while(SPI1CMD & SPIBUSY) {}
_setDataBits(16);
SPI1W0 = data;
SPI1CMD |= SPIBUSY;
while(SPI1CMD & SPIBUSY) {}
data=SPI1W0;
if(!(SPI1C & (SPICWBO | SPICRBO))) data=_bs(data);
return (data);
}

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.