shiftIn and shiftOut do not work with SPI_MODE 3 and 4
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 42/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
- 技术栈
- cpp
- 领域
- embedded-iot
调研方向
从 wiring_shift.c 开始,将 shiftIn() 和 shiftOut() 与 SPI library 对 SPI 模式的处理进行比较。在受影响的时钟模式下重现报告的 74HC165 行为,然后验证对于现有模式和位顺序,标准移位行为仍然正确。当软件函数能够处理报告的模式且不遗漏或添加位时,即表示完成。
由索引模型根据 Issue 内容生成。
描述
The solution described here might well fix several other problems in this thread where bits go missing or additional bits appear...
I was trying to use the standard Arduino shiftIn and shiftOut to operate a 74HC165 shift register. I found that it always clocked one too many bits, and therefore shifted all the bits to the left, losing the top one. This did not happen when I used hardware SPI (the SPI library).
I therefore took a close look at the source code for shiftIn and shiftOut in (wiring_shift.c) and soon spotted the problem: shiftIn and shiftOut assume a clock polarity of 0. They will therefore only work with SPI_MODE0 and 1.
This can be fixed by inverting the clock signal if the SPI_MODE is 3 or 4. I give my solution below. The functions require the SPI mode as an additional parameter, using the Arduino standard definitions SPI_MODE0 etc.
While I was at it, I gained a little speed by taking the test for MSBFIRST or LSBFIRST outside the bit loop and by declaring the method ‘inline’.
I have tested NEWshiftIn with the 74HC165 shift register (SPI_MODE3) and it works fine. I have not tested NEWshiftOut yet as I do not have a MOD3 or 4 device output to test it with. I have also not tested the timings to see if there is a real improvement, as I do not have access to the necessary equipment at the moment. I will be happy to hear from anyone who does any of these tests.
Happy shifting in SPI_MODES 3 and 4!
// ===============================================================================================================================
// New version of shiftIn() and shiftOut with the clock polarity bug removed
// Requires SPI_MODE as a parameter
// Also speeded up by taking the bit direction test out of the loop
// Andrew W Symons 9-Aug-2018
//
// SHIFT IN
//
inline uint8_t NEWshiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t spiMode )
{
uint8_t value = 0;
uint8_t i;
uint8_t clock_polarity = spiMode >> 1 ;
switch ( bitOrder )
{
case LSBFIRST:
{
for (i = 0; i < 8; ++i)
{
digitalWrite(clockPin, HIGH ^ clock_polarity );
value |= digitalRead(dataPin) << i;
digitalWrite(clockPin, LOW ^ clock_polarity );
} ;
} ;
break ;
case MSBFIRST:
{
for (i = 0; i < 8; ++i)
{
digitalWrite(clockPin, HIGH ^ clock_polarity );
value |= digitalRead(dataPin) << (7 - i);
digitalWrite(clockPin, LOW ^ clock_polarity );
} ;
} ;
break ;
} ;
return value;
} ;
//
// ===============================================================================================================================
// SHIFT OUT
//
inline void NEWshiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val, uint8_t spiMode )
{
uint8_t i;
uint8_t clock_polarity = spiMode >> 1 ;
switch ( bitOrder )
{
case LSBFIRST:
{
digitalWrite(dataPin, !!(val & (1 << i)));
digitalWrite(clockPin, HIGH ^ clock_polarity );
digitalWrite(clockPin, LOW ^ clock_polarity );
} ;
break ;
case MSBFIRST:
{
digitalWrite(dataPin, !!(val & (1 << (7 - i))));
digitalWrite(clockPin, HIGH ^ clock_polarity );
digitalWrite(clockPin, LOW ^ clock_polarity );
} ;
break ;
} ;
} ;
//
// ===============================================================================================================================
// END OF FILE
//
- 主要语言
- C++
- 星标
- 306
- 派生
- 150
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
我们还没有检查这个项目的环境配置文件。先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
arduino/ArduinoCore-API 的其他 Issue
-
Bit manipulation not working with 64 bit values可能已有人在做 @asumo-1xts 于 29 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 25/100
arduino/ArduinoCore-API#261 ·
-
Missing virtual destructor for `Print` class可能已有人在做 @andreagilardoni 于 1097 天前认领。 未关闭bug
难度 1/5 1 小时以内 新手友好度 30/100
arduino/ArduinoCore-API#256 ·
-
enhancement
难度 3/5 1-2 天 新手友好度 48/100
arduino/ArduinoCore-API#251 · 1 条评论 ·
-
enhancement
难度 4/5 3-5 天 新手友好度 35/100
arduino/ArduinoCore-API#250 ·
-
bug
难度 2/5 1-3 小时 新手友好度 45/100
arduino/ArduinoCore-API#249 ·
查看 arduino/ArduinoCore-API 的全部 Issue
相似的 Issue
-
难度 1/5 1 小时以内 新手友好度 92/100
sandialabs/seacas#945 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
ROCm/FastFlowLM#757 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 92/100
espressif/esp-matter#1867 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100
mltframework/shotcut#1920 ·
-
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 1 天内回复