cancel
Showing results for 
Search instead for 
Did you mean: 

Hard fault error when cast variable in stm32g070

JCuna.1
Senior

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.

1 ACCEPTED SOLUTION

Accepted Solutions

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

View solution in original post

3 REPLIES 3

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
Senior

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

TDK
Guru

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".