Hart Fault due to the type conversion from unigned long to foat on i.mxrt 1170

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Hart Fault due to the type conversion from unigned long to foat on i.mxrt 1170

跳至解决方案
816 次查看
ricklei_bakerhughes
Contributor I

I am porting some old code from MPC855T to i.mxrt 1170. The code uses unsigned long for value calculation. Before presenting the value on screen, it's converted to float type and use sprintf to generate the string.
The following is piece of my code:

typedef struct {
unsigned long ulValue;
} typeRegs;
typeRegs * ptrRegs = (typeRegs *)memory address;
float fValue = (float)(ptrRegs->ulValue); // this line will trigger a hart fault error
unsigned long ulTemp = ptrRegs->ulValue;
fValue = (float)ulTemp; // this line works well

I am using Keil, and the ArmClang compiler version is 6.16. I need to understand why I can't convert an unsigned long in point parameter to float directly.

0 项奖励
回复
1 解答
734 次查看
ricklei_bakerhughes
Contributor I

Yes, the exact code works well. But there is a small difference between the code and my implementation. I am not using a memory address directly. I am using an array of unsigned char to allocate the memory space. And this gave me a Hard Fault. 

And I figured out the root cause finally. It's due to the alignment error. The conversion from unsigned long to float must be byte aligned. And it seems the array of unsigned char is not aligned with a float value. I changed array defintion to float type. Then it works.

I tried to add "__attribute__ ((aligned(4)))" after the structure defintion. But it didn't work too. I don't know the reason. 

Anyway this can be closed.

Thanks!

Rick

在原帖中查看解决方案

0 项奖励
回复
2 回复数
788 次查看
Kan_Li
NXP TechSupport
NXP TechSupport

Hi @ricklei_bakerhughes ,

 

I have pasted your code in some demo like led_blinky , it works well with -O0 optimization option ,  Please kindly refer to the attachment for details.

 

Have a great day,
Kan


-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!
- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 项奖励
回复
735 次查看
ricklei_bakerhughes
Contributor I

Yes, the exact code works well. But there is a small difference between the code and my implementation. I am not using a memory address directly. I am using an array of unsigned char to allocate the memory space. And this gave me a Hard Fault. 

And I figured out the root cause finally. It's due to the alignment error. The conversion from unsigned long to float must be byte aligned. And it seems the array of unsigned char is not aligned with a float value. I changed array defintion to float type. Then it works.

I tried to add "__attribute__ ((aligned(4)))" after the structure defintion. But it didn't work too. I don't know the reason. 

Anyway this can be closed.

Thanks!

Rick

0 项奖励
回复
%3CLINGO-SUB%20id%3D%22lingo-sub-2326670%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%E7%94%B1%E4%BA%8E%20i.mxrt%20%E4%B8%8A%E4%BB%8E%E6%97%A0%E7%AC%A6%E5%8F%B7%20long%20%E5%88%B0%20foat%20%E7%9A%84%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%E5%AF%BC%E8%87%B4%E5%93%88%E7%89%B9%E6%95%85%E9%9A%9C%201170%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2326670%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3E%E6%88%91%E6%AD%A3%E5%9C%A8%E5%B0%86%E4%B8%80%E4%BA%9B%E6%97%A7%E4%BB%A3%E7%A0%81%E4%BB%8E%20MPC855T%20%E7%A7%BB%E6%A4%8D%E5%88%B0%20i.mxrt%201170%E3%80%82%E4%BB%A3%E7%A0%81%E4%BD%BF%E7%94%A8%E6%97%A0%E7%AC%A6%E5%8F%B7%20long%20%E8%AE%A1%E7%AE%97%E5%80%BC%E3%80%82%E5%9C%A8%E5%B1%8F%E5%B9%95%E4%B8%8A%E6%98%BE%E7%A4%BA%E6%95%B0%E5%80%BC%E4%B9%8B%E5%89%8D%EF%BC%8C%E5%85%88%E5%B0%86%E5%85%B6%E8%BD%AC%E6%8D%A2%E4%B8%BA%E6%B5%AE%E7%82%B9%E7%B1%BB%E5%9E%8B%EF%BC%8C%E7%84%B6%E5%90%8E%E4%BD%BF%E7%94%A8%20sprintf%20%E7%94%9F%E6%88%90%E5%AD%97%E7%AC%A6%E4%B8%B2%E3%80%82%3CBR%20%2F%3E%E4%B8%8B%E9%9D%A2%E6%98%AF%E6%88%91%E7%9A%84%E4%B8%80%E6%AE%B5%E4%BB%A3%E7%A0%81%EF%BC%9A%3C%2FP%3E%3CP%3Etypedef%20struct%20%7B%3CBR%20%2F%3Eunsigned%20long%20ULValu%20%3CBR%20%2F%3E%20e%3B%7D%20TypeRegs%3B%20t%20%3CBR%20%2F%3E%20ypeRegs%20*%20ptrRegs%20%3D%20(typeRegs%20*)%20%E5%86%85%E5%AD%98%E5%9C%B0%E5%9D%80%EF%BC%9Bfloat%20fValue%20%3D%20(f%20%3CBR%20%2F%3E%20loat)%20(ptrRegs-%26gt%3B%20ULValue)%3B%2F%2F%E6%AD%A4%E8%A1%8C%E4%BC%9A%E8%A7%A6%E5%8F%91%E5%BF%83%E8%84%8F%E6%95%85%E9%9A%9C%E9%94%99%E8%AF%AF%20%3CBR%20%2F%3E%20unsigned%20long%20%3D%20ptrRegs-%26gt%3B%20ULValue%3B%2F%2F%E6%AD%A4%E8%A1%8C%E8%BF%90%E8%A1%8C%E8%89%AF%E5%A5%BD%20%3CBR%20%2F%3E%3C%2FP%3E%3CP%3E%E6%88%91%E4%BD%BF%E7%94%A8%E7%9A%84%E6%98%AF%20Keil%EF%BC%8CArmClang%20%E7%BC%96%E8%AF%91%E5%99%A8%E7%9A%84%E7%89%88%E6%9C%AC%E6%98%AF%206.16%E3%80%82%E6%88%91%E9%9C%80%E8%A6%81%E4%BA%86%E8%A7%A3%E4%B8%BA%E4%BB%80%E4%B9%88%E4%B8%8D%E8%83%BD%E5%B0%86%E6%97%A0%E7%AC%A6%E5%8F%B7%20long%20in%20point%20%E5%8F%82%E6%95%B0%E7%9B%B4%E6%8E%A5%E8%BD%AC%E6%8D%A2%E4%B8%BA%20float%E3%80%82%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2326799%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20Hart%20Fault%20due%20to%20the%20type%20conversion%20from%20unigned%20long%20to%20foat%20on%20i.mxrt%201170%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2326799%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3E%E4%BD%A0%E5%A5%BD%EF%BC%8C%E6%88%91%E6%98%AF%3CA%20href%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F246904%22%20target%3D%22_blank%22%3E%40ricklei_bakerhughes%3C%2FA%3E%E3%80%81%3C%2FP%3E%0A%3CBR%20%2F%3E%0A%3CP%3E%E6%88%91%E5%B7%B2%E5%B0%86%E6%82%A8%E7%9A%84%E4%BB%A3%E7%A0%81%E7%B2%98%E8%B4%B4%E5%88%B0%20led_blinky%20%E7%AD%89%E6%BC%94%E7%A4%BA%E7%A8%8B%E5%BA%8F%E4%B8%AD%EF%BC%8C%E5%9C%A8%E4%BD%BF%E7%94%A8%20-O0%20%E4%BC%98%E5%8C%96%E9%80%89%E9%A1%B9%E6%97%B6%E8%BF%90%E8%A1%8C%E8%89%AF%E5%A5%BD%EF%BC%8C%E8%AF%A6%E6%83%85%E8%AF%B7%E5%8F%82%E8%A7%81%E9%99%84%E4%BB%B6%E3%80%82%3C%2FP%3E%0A%3CBR%20%2F%3E%0A%3CP%3E%E7%A5%9D%E6%82%A8%E6%84%89%E5%BF%AB%EF%BC%8C%3CBR%20%2F%3EKan%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E-------------------------------------------------------------------------------%3CBR%20%2F%3E%E6%B3%A8%EF%BC%9A%3CBR%20%2F%3E-%20%E5%A6%82%E6%9E%9C%E6%9C%AC%E5%B8%96%E5%9B%9E%E7%AD%94%E4%BA%86%E6%82%A8%E7%9A%84%E9%97%AE%E9%A2%98%EF%BC%8C%E8%AF%B7%E7%82%B9%E5%87%BB%22%E6%A0%87%E8%AE%B0%E6%AD%A3%E7%A1%AE%22%20%E6%8C%89%E9%92%AE%E3%80%82%E8%B0%A2%E8%B0%A2%EF%BC%81%3CBR%20%2F%3E-%20%E6%88%91%E4%BB%AC%E4%BC%9A%E5%9C%A8%E6%9C%80%E5%90%8E%E4%B8%80%E6%AC%A1%E5%8F%91%E5%B8%96%E5%90%8E%E7%9A%84%207%20%E5%91%A8%E5%86%85%E8%B7%9F%E8%B8%AA%E4%B8%BB%E9%A2%98%EF%BC%8C%E4%B9%8B%E5%90%8E%E7%9A%84%E5%9B%9E%E5%A4%8D%E5%B0%86%E8%A2%AB%E5%BF%BD%E7%95%A5%3CBR%20%2F%3E%E5%A6%82%E6%9E%9C%E6%82%A8%E4%BB%A5%E5%90%8E%E6%9C%89%E7%9B%B8%E5%85%B3%E9%97%AE%E9%A2%98%EF%BC%8C%E8%AF%B7%E6%89%93%E5%BC%80%E4%B8%80%E4%B8%AA%E6%96%B0%E4%B8%BB%E9%A2%98%E5%B9%B6%E5%8F%82%E8%80%83%E5%B7%B2%E5%85%B3%E9%97%AD%E7%9A%84%E4%B8%BB%E9%A2%98%E3%80%82%3CBR%20%2F%3E-------------------------------------------------------------------------------%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2328162%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20Hart%20Fault%20due%20to%20the%20type%20conversion%20from%20unigned%20long%20to%20foat%20on%20i.mxrt%201170%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2328162%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3E%E6%98%AF%E7%9A%84%EF%BC%8C%E7%A1%AE%E5%88%87%E7%9A%84%E4%BB%A3%E7%A0%81%E6%95%88%E6%9E%9C%E5%BE%88%E5%A5%BD%E3%80%82%E4%BD%86%E4%BB%A3%E7%A0%81%E5%92%8C%E6%88%91%E7%9A%84%E5%AE%9E%E7%8E%B0%E4%B9%8B%E9%97%B4%E6%9C%89%E4%B8%80%E7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