2022-07-21 08:00 AM - edited 2023-11-20 08:18 AM
Hi,
I am trying to init SPI1 (to just receive data qwith MISO) on nucleo G474RE without using HAL libraries.
Can you help me to assess why my code does not work ?
I can't see the clock eventhough SPI1 is enabled
void SPI1_init(){
RCC-> APB2ENR|=(1 AHB2ENR |= (1MODER &= ~(1 AFR[0] |= (1 AFR[0] |= (1 AFR[0] |= (1 CR1 |= (1 CR1 |= (1 CR1 |= (1 CR2 |=(1CR2 |= (1CR1|= (1CR1|= (1CR1&= ~(1
Solved! Go to Solution.
2022-07-22 02:25 AM
So have you set the RXONLY bit?
2022-07-22 02:40 AM
Thanks @Andrew Neil
It was probably the RXONLY bit causing the issue. Now it is fine.
2022-07-22 03:06 AM
@Tommino "Now it is fine"
Good to hear - now please mark the solution:
2022-07-22 03:19 AM
> Again, I would suggest starting with HAL to see how that does it, then base your bare-metal on that ...
I would recommend completely forget about existence of Cube/HAL, first. Then read the fine manuals, thoroughly.
The Rx-only mode is being matter of grief, especially for those who click, as it requires to know what one is doing.
JW
2022-07-22 03:27 AM
Hi @Community member ,
Thanks for your post in the blog.
What I understood is that you recommend to not connect the MOSI pin through AF if not needed. But then who controls when the clock is output on SCK? the SPI enable?
2022-07-22 03:40 AM
SCK does not depend on MOSI.
SPI in its most basic form are two shift registers - one for output one for input - and some control logic to generate clocks.
If SPI is set as master and enabled, and then you write to DR, the control logic generates 8 or 16 (or whatever you set) clocks on SPI, and using those clocks it shifts out data from output shift register to MOSI and shifts in data into input shift register from MISO.
If you don't assign any pin to MOSI in GPIO, well simply the internal MOSI output from SPI module is connected nowhere and the shifted out data are lost. If you don't assign any pin to MISO in GPIO, the internal MISO input to SPI module is set to 0, so you will simply receive all zeros.
The mode which is set by RXONLY bit is already an extension to this basic model - if you set that bit, as soon as SPI is enabled, the control logic starts to generate clocks and it won't stop until SPI is disabled.
2022-07-22 05:01 AM
Ok I see
But basically in this way you have to write to DR eventhough you have just to receive data. Is this what you recommend?
Can you write the code?
2022-07-22 07:55 AM
> But basically in this way you have to write to DR eventhough you have just to receive data. Is this what you recommend?
Yes. What's wrong with this?
> Can you write the code?
Read N Bytes:
for N bytes
write byte to SPI data register
wait until RXNE is set in status register
read byte from SPI data register
end for
If you want to work with bytes rather than halfwords, you may need to think about data packing (depends on STM32 model, read RM).
JW
2022-07-22 08:20 AM
@Tommino "in this way you have to write to DR even though you have just to receive data"
That's the way you'd do it "in general" - for microcontrollers that don't have this special 'read only' mode.
2022-07-25 03:00 AM
ok thanks. I tried it and it works.
I was afraid of writing to SPI DR before reading it beacuse in some way I thought I could have readback what I wrote. How is this prevented?