Skip to main content
JCuna.1
Senior
November 24, 2021
Solved

Hard fault error when cast variable in stm32g070

  • November 24, 2021
  • 3 replies
  • 1405 views

I have migrated code from stm32f4 to stm32g0 suscesfully in almost all my aplication. However there is a part of code that rise a hard fault error.

This is the code:

crc_received = *((uint32_t*)&e22_uart_rx_buffer[(uint16_t)(e22_uart_rx_buffer_length - 4)]);

This is the assembler compiled:

113 	crc_received = *((uint32_t*)&e22_uart_rx_buffer[(uint16_t)(e22_uart_rx_buffer_length - 4)]);
08003c6a: uxth r3, r1
08003c6c: ldr r3, [r5, r3]
08003c6e: uxtb r1, r1
08003c70: movs r0, #0
08003c72: str r3, [r7, #0]

The error rise after execute: ldr r3, [r5, r3]

And this is the fault analizer viewer from stm32cube ide:

0693W00000GYmY3QAL.pngThe Register content is this one:

0693W00000GYmYmQAL.png 

I can't realize where is the trouble, even fault analizer didn't show a partircular issue. This code operation is a simple cast from 4 bytes uint8_t buffer[] to uint32_t.

This topic has been closed for replies.
Best answer by waclawek.jan

The Cortex-M0+ processor, which is core in 'G0, does not support unaligned accesses, i.e. you must not read an uint32_t from an address which is not multiple of 4.

Read 4 bytes and assemble them into the uint32_t "manually".

JW

3 replies

waclawek.jan
waclawek.janBest answer
Super User
November 24, 2021

The Cortex-M0+ processor, which is core in 'G0, does not support unaligned accesses, i.e. you must not read an uint32_t from an address which is not multiple of 4.

Read 4 bytes and assemble them into the uint32_t "manually".

JW

JCuna.1
JCuna.1Author
Senior
November 25, 2021

Is there any gcc flag compilation in order to make this automatically?

TDK
November 25, 2021

You can try the -mno-unaligned-access flag in GCC to see if it works. Unsure how it will handle the casting.

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