cancel
Showing results for 
Search instead for 
Did you mean: 

Communicate NUCLEO-G070RB USART with SPI as MSB

yurim
Associate

Thank you always for your hard work,

I needed 4 SPIs, so while exploring the board, I found a text that could make USART work like SPI, so I tried this with the NUCLEO-G070RB.
The example I referenced is "USART_SyncCommunication_FullDuplex_DMA_Init" on that board.
Referring to the setting of the board, a program was written to check whether it was the same as the buffer sent by the buffer that received a specific message from USART (master) to SPI (slave).

The current code works well.
However, the standard setting of SPI is MSB first, but the example seems to be possible only with LSB first.
I want USART and SPI to communicate by MSB.
Attached is the setup of USART and SPI.

 

yurim_1-1745200797854.png

yurim_0-1745201521951.png

 

 

 

5 REPLIES 5
TDK
Guru

The setting appears to be missing in CubeMX.

Try setting the MSBFIRST bit in USART_CR2 after it's initialized.

SET_BIT(USART_CR2, USART_CR2_MSBFIRST);

 

If you feel a post has answered your question, please click "Accept as Solution".

@TDK wrote:

The setting appears to be missing in CubeMX.


I only had a quick look, but I also didn't see it in the STM32G0 HAL documentation:
https://www.st.com/resource/en/user_manual/um2319-description-of-stm32g0-hal-and-lowlayer-drivers-stmicroelectronics.pdf

via: https://www.st.com/en/embedded-software/stm32cubeg0.html#documentation

 

PS:

maybe this is it?

AndrewNeil_0-1745400113695.png

https://www.st.com/resource/en/user_manual/um2319-description-of-stm32g0-hal-and-lowlayer-drivers-stmicroelectronics.pdf#page=825

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

@TDK 

I've enabled MSB_FIRST BIT since the initialization of USART, but the output results remain unchanged. (It still seems to be operating as LSB first.)
Is there any way I can try it now?

Below are the buffers I sent to USART(UsartTx) and the buffers I received with SPI(SpiRx). In CUBE MX, I changed SPI to MSB First.

yurim_0-1745455579000.png

yurim_1-1745455586615.png

 


Thank you.


@yurim wrote:

I've enabled MSB_FIRST BIT since the initialization of USART


Please show your code - ideally, a minimum but complete example which demonstrates the issue.

Both Rx & Tx

see: How to insert source code.

 

Have you used an oscilloscope and/or logic analyser to see what actually happens on the wires?

You would need to configure both ends for MSB-first

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.
Pavel A.
Evangelist III

From the RM, about the MSBFIRST: "This bitfield can only be written when the USART is disabled (UE=0)."

So don't set this bit after init(), rather set the AdvFeatureInit struct members as usual.