I saw that the topic was discussed in many other issues, but I can not find the answer for my specific case.
I work with the STM32F0 microcontroller. The top of the FIFO receive / transmit SPI is accessible via memory access. This particular microcontroller allows me to read / write 8 bits or 16 bits from the top of the FIFO. More precisely, when the LDRB / STRB command is executed, 8 bits are pushed / moved from / to the FIFO, and when the LDRH / STRH command is executed, 16 bits are unloaded from / to the FIFO.
The hardware abstraction layer provided by STMicroelectronic offers this syntax for reading SPI FIFO.
return *(volatile uint8_t*)&_handle->Instance->DR;
return *(volatile uint16_t*)&_handle->Instance->DR;
*(volatile uint8_t*)&_handle->Instance->DR = val;
*(volatile uint16_t*)&_handle->Instance->DR = val;
Where DRis it uint32_t*pointing to the top of the SPI FIFO
I built my software using this syntax and it works great. The only problem is that g ++ warns a lot about punning type. More precisely:
Inc / drivers / SPI.h: 70: 50: warning: dereferenced pointer type will violate strict anti-aliasing rules [-Wstrict-aliasing] return *(volatile uint16_t*)&_handle->Instance->DR;
After some reads, it seems like using a connection is not a good idea in C ++. I tried to do it anyway, but I had problems. In fact, accessing the memory using a pointer in the connection makes my microcontroller crash, as well as un-smooth access to the memory.
static_cast and reinterpret_cast issues warning alerts as C-style styles
I cannot use memcpywith void*, since my ultimate goal is to force the compiler to use the LDRB / STRB and LDRH / STRH instructions.
Other suggested solutions that I found in Stack Overflow depended on the use case.Any suggestion?
source
share