SPI RXNE IT: volatile buffer index effect on buffer type when reading DR register
Hey guys, befor asking my question I want to give you a brief overview of the test case:
TestCase: SPI Full Duplex
Master send [0-3](4) bytes and Slave returns received bytes [0-2](3) immediatly.
(no register preload in slave)
Example:
Master <-> Slave
0x01 <-> unkown
0x02 <-> 0x01
0x03 <-> 0x02
0x04 <-> 0x03
-------Variante 1 working-------------
Variables:
volatile uint32_t spiFrameCounter;
uint8_t spiRxTestData;
uint8_t spiRxBuffer[255];On Chipselect:
spiFrameCounter = 0;RXNE interrupt routine:
spiRxTestData = (uint8_t)(READ_REG(SPI3->DR)); //IMPORTANT: assembler contains uxtb command
if (spiFrameCounter < 2) {
*((__IO uint8_t *)&SPIx->DR) = spiRxTestData;
}
spiRxBuffer[spiFrameCounter] = spiRxTestData;
spiFrameCounter++;-------Variante 2 not working---------
Variables:
volatile uint32_t spiFrameCounter;
uint8_t spiRxBuffer[255];On Chipselect:
spiFrameCounter = 0;RXNE interrupt routine:
spiRxBuffer[spiFrameCounter] = (uint8_t)(READ_REG(SPI3->DR)); //IMPORTANT: assembler contains NO uxtb command
if (spiFrameCounter < 2) {
*((__IO uint8_t *)&SPIx->DR) = (uint8_t)spiRxBuffer[spiFrameCounter]);
}
spiFrameCounter++;---- Variante 2 Solution to Fix Problem 1 -----
change:
volatile uint32_t spiFrameCounter;to:
uint32_t spiFrameCounter;Result: uxtb is used again in assembler and therfor DR register is read with 1 byte
---- Variante 2 Solution to Fix Problem 2 -----
change:
spiRxBuffer[spiFrameCounter] = (uint8_t)(READ_REG(SPI3->DR));to:
*((__IO uint8_t *)&spiRxBuffer[spiFrameCounter]) = (uint8_t)(READ_REG(SPI3->DR));Result: uxtb is used again in assembler and therfor DR register is read with 1 byte
---- Variante 2 Solution to Fix Problem 3 -----
change:
spiRxBuffer[spiFrameCounter] = (uint8_t)(READ_REG(SPI3->DR));to:
spiRxBuffer[(uint8_t)spiFrameCounter] = (uint8_t)(READ_REG(SPI3->DR));Disadvantage: spiRxBuffer max size 255
Question:
Why does a volatile variable used as an array index have effect on the type of the actual array?
Or why is DR register read without uxtb command?
Note:
With "Not Working" i mean the SPI3->DR register is not read as 1 Byte but instead is ready as 4 Byte.
This can also be noticed in the response of the slave to the master. (Response is shifted by 4 Bytes)
Kind regards
Franco
