shiftIn and shiftOut do not work with SPI_MODE 3 and 4
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 42/100
- Tipo de issue
- Error
- Claridad
- Bastante claro
- Estado de actividad
- Estancado
- Stack tecnológico
- cpp
- Área
- embedded-iot
Línea de trabajo
Comienza en wiring_shift.c y compara shiftIn() y shiftOut() con el manejo de los modos SPI por parte de la SPI library. Reproduce el comportamiento reportado de 74HC165 en los modos de reloj afectados y, después, verifica que el comportamiento estándar de desplazamiento siga siendo correcto para los modos y órdenes de bits existentes. Se considera terminado cuando las funciones de software manejan los modos reportados sin omitir ni añadir bits.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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
//
- Lenguaje dominante
- C++
- Estrellas
- 307
- Forks
- 152
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Preparar el entorno
Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de arduino/ArduinoCore-API
-
Bit manipulation not working with 64 bit valuesPosiblemente ocupada @asumo-1xts la tomó hace 31 días. Abierto
Dificultad 3/5 1-2 días Aptitud para principiantes 25/100
arduino/ArduinoCore-API#261 ·
-
Missing virtual destructor for `Print` classPosiblemente ocupada @andreagilardoni la tomó hace 1099 días. Abiertobug
Dificultad 1/5 Menos de una hora Aptitud para principiantes 30/100
arduino/ArduinoCore-API#256 ·
-
enhancement
Dificultad 3/5 1-2 días Aptitud para principiantes 48/100
arduino/ArduinoCore-API#251 · 1 comentario ·
-
enhancement
Dificultad 4/5 3-5 días Aptitud para principiantes 35/100
arduino/ArduinoCore-API#250 ·
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 45/100
arduino/ArduinoCore-API#249 ·
Todos los issues de arduino/ArduinoCore-API
Issues similares
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 92/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
cp-algorithms/cp-algorithms#1715 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Icinga/icinga2#11058 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
status:needs-triage
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
PX4/PX4-Autopilot#28924 ·
Los mantenedores suelen responder en 1 día
-
component: split-view platform: windows
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
zen-browser/desktop#15616 · 1 reacción ·
Los mantenedores suelen responder en 1 día