Home › Forums › Sparkfun MP3 Shield Library Support Forum › Bug's and Feature Fixes › Reply To: Bug's and Feature Fixes
Mp3ReadRegister had issue with FF’ing the upper byte when sign of lower byte was set, due to a carry from shift. This is caused by the following line:
char response1 = SPI.transfer(0xFF); //Read the first byte
while(!digitalRead(MP3_DREQ)) ; //Wait for DREQ to go high indicating command is complete
char response2 = SPI.transfer(0xFF); //Read the second byte
while(!digitalRead(MP3_DREQ)) ; //Wait for DREQ to go high indicating command is complete
Where char is not unsigned and its use on response2 causes shift to carry the sign of the previous shift. Changing this to the following fixes this instance:
unsigned char response2 = SPI.transfer(0xFF); //Read the second byte
This is why i prefer to use unions in place of shifts such as
union twobyte {
uint16_t word;
uint8_t byte[2];
} ;
such as below:
…
union twobyte resultvalue;
…
resultvalue.byte[1] = SPI.transfer(0xFF);
while(!digitalRead(MP3_DREQ)) ;
resultvalue.byte[0] = SPI.transfer(0xFF);
while(!digitalRead(MP3_DREQ)) ;
…
return resultvalue.word;
this way you don’t have to relay on the compiler and op-codes to due an assumed shift as they are not all the same.
And is in a pending release.