老麦Mac 发表于 2016-5-13 18:42:17

stm32m0 单片机按地址写长整数,地址必须是4的整数倍?

一段以前用的很多的程序,以前在别的工程用过很多了(stm32 M3系列, 51单片机)
今天拿到m0单片机的工程里一运行就hard fault
找来找去似乎是这么个问题: 用指针往一个地址里写长整数数据, 这个地址必须是4的整数倍?
类似这样:
        {
                int* a;
                int* b;
                a = (int*)(0x20000410);
                *a = 0;                        //okay
                b = (int*)(0x20000421);
                *b = 0;                        //hard fault
        }
我用的是mdk5, 感觉应该是cpu什么模式没设置对吧? 应该怎么解决这个问题?
thx

老麦Mac 发表于 2016-5-13 19:46:30

晕, 还真是这样?

Important
It is important to make sure the memory address accessed is aligned. For example, a word size
access can only be carried out on address locations when address bits are set to zero, and
a half word size access can only be carried out on address locations when an address bit is
set to zero. The Cortex-M0 processor does not support unaligned transfers. Any attempt at
unaligned memory access results in a hard fault exception. Byte-size transfers are always aligned
on the Cortex-M0 processor.

潇潇雨歇pku 发表于 2016-5-14 09:28:49

32位的机器,你不按4来操作,CPU得多累啊

潇潇雨歇pku 发表于 2016-5-14 09:29:31

推荐看这篇文章:结构体和类的内存字节对齐详解
http://www.jizhuomi.com/software/567.html

潇潇雨歇pku 发表于 2016-11-23 18:19:38

我回复了啥?居然被屏蔽了?

潇潇雨歇pku 发表于 2016-11-23 18:21:01

潇潇雨歇pku 发表于 2016-5-14 09:29
**** 该帖被屏蔽 ****

好吧,推荐了一个我觉得比较好的个人网站的文章,可能管理员觉得是广告?呵呵

peter001 发表于 2016-11-24 00:27:17

最好4位对齐,好习惯

zero99 发表于 2016-12-29 10:10:09

潇潇雨歇pku 发表于 2016-11-23 18:21
好吧,推荐了一个我觉得比较好的个人网站的文章,可能管理员觉得是广告?呵呵 ...

恢复了,可能是以为发的广告

jtc_88 发表于 2016-12-29 12:35:58

2的n次方,都行,我试过使用内部flash 存储数据,2字节或4字节都可以。例如6字节就不行!!!

海迹天涯 发表于 2016-12-30 08:33:31

不错学习了
页: [1]
查看完整版本: stm32m0 单片机按地址写长整数,地址必须是4的整数倍?