简体   繁体   English

使用 Atollic TrueSTUDIO for STM32 IDE 的 C 中的指针转换问题

[英]Pointer casting problem in C with Atollic TrueSTUDIO for STM32 IDE

Working on STM32H7 in Atollic TrueSTUDIO for STM32 IDE.在 Atollic TrueSTUDIO 中为 STM32 IDE 开发 STM32H7。 Only C coding.只有 C 编码。 Using FreeRTOS.使用 FreeRTOS。

Ui08 *pointerSomething;
Ui64 localVariable;

pointerSomething=&addressOfSomething;
localVariable = *(Ui64*)(pointerSomething);

These code is generally working.这些代码通常可以正常工作。

But one of my usage in a case in a thread in something like that;但是我在类似情况下的一个线程中的用法;

thread begin //

Ui08 *pointerSomething;
Ui64 localVariable;

case 3: 

   pointerSomething=&addressOfSomething;
   localVariable = *(Ui64*)(pointerSomething);

break;

thread end //

And I am getting a hardfault when the second sequence in these case.在这种情况下,当第二个序列时,我遇到了一个硬故障。 I mean first time in case working properly but second time in case getting hardfault exactly the line of localVariable = *(Ui64*)(pointerSomething);我的意思是第一次以防万一正常工作,但第二次以防万一发生硬故障 localVariable localVariable = *(Ui64*)(pointerSomething);

thread begin //

Ui08 *pointerSomething;
Ui64 localVariable;

case 3: 

   pointerSomething=&addressOfSomething;
   memcpy( &localVariable, pointerSomething, sizeof(localVariable) );

break;

thread end //

If I change these line as you can see above, the problem is fixing for all time of case.如果我如您在上面看到的那样更改这些行,则问题一直在解决。 But my question is why this problem is occuring, casting type of line?但我的问题是为什么会出现这个问题,铸造类型的线?

There is nothing to guess here.这里没什么好猜的。

gcc is compiling for Cortex-M7 (thumb) 64-bit pointer pun to the LDRD instruction. gcc 正在为LDRD指令的 Cortex-M7(拇指)64 位指针进行编译。 LDRD instruction requires aligned addresses. LDRD指令需要对齐的地址。 That is the reason why you are getting hardFaults from time to time when the address is not word aligned.这就是为什么当地址不是字对齐的时候你会不时收到硬故障的原因。

https://godbolt.org/z/o9sPvfaon https://godbolt.org/z/o9sPvfaon

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM