Tuesday 29 October 2013

Arduino library shiftIn function bug

Found a bug in the Arduino library ShiftIn() function whilst testing an ALU board.

In order to test the ALU board they are connected to an Arduino via an IO board which provides 16 input and output pins using 74HC165 shift register ICs.

 
The test program generates the two 4-bit inputs for the ALU and eight control signals to select the mathematical operation to be performed. The test values are then serially written to the two 74HC595 SIPO shift registers on the IO board, the parallel outputs are wired to the ALU board.

The ALU output is then serially read from the 74HC165 PISO shift register and the result displayed in the Arduino IDE.

The test program cycles through a large numbers of tests to test the ALU thoroughly.
 
Initially the ALU did not work and I spent two hours debugging it. It became apparent that the problem was with the IO board as manually probing the ALU board showed it was working fine. Initially, I suspected a floating input on one of the logic pins but after extensive probing could not find the problem.

Eventually my suspicion turned to the Arduino library and the ShiftIn() function and I decided to look at the source code to check what it was doing.

Whilst Googling for the source code I came across a bug report (issue 467) within the source code repository for the Arduino library shiftIn function. The bug report described a problem with the way the clock line is driven causing the 8th bit to be missed. Eureka!

Quickly replacing the ShiftIn() function with my own code and retesting the problem went away.