shiftIn and shiftOut do not work with SPI_MODE 3 and 4
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 42/100
- Issue-Typ
- Bug
- Klarheit
- Größtenteils klar
- Aktivitätsstatus
- Veraltet
- Tech-Stack
- cpp
- Bereich
- embedded-iot
Rechercherichtung
Beginne in wiring_shift.c und vergleiche shiftIn() und shiftOut() mit der Handhabung der SPI-Modi durch die SPI library. Reproduziere das gemeldete 74HC165-Verhalten in den betroffenen Taktmodi und überprüfe anschließend, dass das standardmäßige Shift-Verhalten für vorhandene Modi und Bit-Reihenfolgen weiterhin korrekt ist. Als abgeschlossen gilt die Aufgabe, wenn die Softwarefunktionen die gemeldeten Modi ohne fehlende oder zusätzliche Bits verarbeiten.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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
//
- Vorherrschende Sprache
- C++
- Sterne
- 307
- Forks
- 152
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Entwicklungsumgebung
Dieses Projekt bietet weder Dev-Container noch Dockerfile noch Beitragsleitfaden – die Einrichtung liegt bei Ihnen. Beginnen Sie mit der README; die allgemeinen Schritte stehen in unserem Leitfaden für den ersten Beitrag.
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus arduino/ArduinoCore-API
-
Bit manipulation not working with 64 bit valuesEvtl. vergeben @asumo-1xts hat das vor 31 Tagen übernommen. Offen
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 25/100
arduino/ArduinoCore-API#261 ·
-
Missing virtual destructor for `Print` classEvtl. vergeben @andreagilardoni hat das vor 1099 Tagen übernommen. Offenbug
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 30/100
arduino/ArduinoCore-API#256 ·
-
enhancement
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 48/100
arduino/ArduinoCore-API#251 · 1 Kommentar ·
-
enhancement
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 35/100
arduino/ArduinoCore-API#250 ·
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 45/100
arduino/ArduinoCore-API#249 ·
Alle Issues in arduino/ArduinoCore-API
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Icinga/icinga2#11058 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
component: split-view platform: windows
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
zen-browser/desktop#15616 · 1 Reaktion ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
-
area/ysql kind/bug priority/medium status/awaiting-triage
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 84/100
yugabyte/yugabyte-db#34415 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 86/100
WayfireWM/wayfire#3148 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag