shiftIn and shiftOut do not work with SPI_MODE 3 and 4
Ninguém assumiu esta issue ainda.
Avaliação
- Dificuldade
- 4/5
- Tempo estimado
- 3-5 dias
- Facilidade para iniciantes
- 42/100
- Tipo de issue
- Bug
- Clareza
- Razoavelmente clara
- Status de atividade
- Estagnada
- Stack de tecnologia
- cpp
- Domínio
- embedded-iot
Direção de pesquisa
Comece em wiring_shift.c e compare shiftIn() e shiftOut() com o tratamento dos modos SPI pela SPI library. Reproduza o comportamento relatado do 74HC165 nos modos de clock afetados e, em seguida, verifique se o comportamento padrão de deslocamento continua correto para os modos e as ordens de bits existentes. A tarefa estará concluída quando as funções de software tratarem os modos relatados sem perder nem adicionar bits.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
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
//
- Linguagem predominante
- C++
- Estrelas
- 306
- Forks
- 150
- Métricas de merge de PRs
- Nenhum PR com merge em 30d
Preparar o ambiente
Ainda não verificamos os arquivos de configuração deste projeto. Comece pelo README e veja nosso guia da primeira contribuição para os passos gerais.
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de arduino/ArduinoCore-API
-
Bit manipulation not working with 64 bit valuesTalvez já em andamento @asumo-1xts assumiu há 29 dias. Aberta
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 25/100
arduino/ArduinoCore-API#261 ·
-
Missing virtual destructor for `Print` classTalvez já em andamento @andreagilardoni assumiu há 1097 dias. Abertabug
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 30/100
arduino/ArduinoCore-API#256 ·
-
enhancement
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 48/100
arduino/ArduinoCore-API#251 · 1 comentário ·
-
enhancement
Dificuldade 4/5 3-5 dias Facilidade para iniciantes 35/100
arduino/ArduinoCore-API#250 ·
-
bug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 45/100
arduino/ArduinoCore-API#249 ·
Todas as issues de arduino/ArduinoCore-API
Issues semelhantes
-
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 92/100
espressif/esp-matter#1867 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 82/100
Mantenedores costumam responder em até 1 dia
-
cuAmpcorParameter.cpp (CPU pycuampcor) does not compile with libc++: EXIT_FAILURE is undeclaredAberta
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 92/100
isce-framework/isce3#387 ·
-
[request] opencv/5.0.0Abertaupstream update
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 68/100
conan-io/conan-center-index#31055 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 88/100
Mantenedores costumam responder em até 1 dia