gpt4 book ai didi

c - 如果我使用超过 25 个元素的数组,msp430g2211 (launchpad) 在启动时停止

转载 作者:太空宇宙 更新时间:2023-11-03 23:53:27 25 4
gpt4 key购买 nike

我在 Linux 环境(Ubuntu、msp430-gcc 4.6.3)中使用 TI Launchpad (msp430g2211)。

我有一个奇怪的问题:当我尝试分配超过固定数量的内存时,微 Controller 在启动时停止。

更准确地说:我在 main() 中有一个包含 25 个元素的数组,创建如下:

color img0[25] = {


off, cyan, cyan, cyan, off,
cyan, red, violet, orange, cyan,
cyan, red, violet, orange, cyan,
off, cyan, cyan, cyan, off,
off, off, off, off, off,

};

(为了完整性:

typedef struct color {
uint8_t r;
uint8_t g;
uint8_t b;
} color;


static color red = {25, 0, 0}; ...

)

一切正常,但如果我尝试使用 30 个(或任何大于 25 个的数字)元素而不是 25 个元素,uC 似乎已经死了。 (编译和编程时没有错误)

我在 TI Wiki 中找到了这个,这似乎符合我正在经历的事情:

WDT fires during C startup code

Another most common problem if the application is written in C language is the watchdog timeout during startup. Per default watchdog timer on all MSP430 is set to active after startup. Therefore it is necessary to turn off the WDT if not needed right at the beggining of the application code. If the application code is using large variables which are needed to be initialized during startup, this could cause that the watchdog timer already fires during startup and the code will never be run.

The solution for this problem is to use the compiler's low level C initialization function which is called even prior to the initialization of C variables. In CCS compiler, it is called "int _system_pre_init(void)", while in IAR, the function is called int __low_level_init(void). The return value is used to determine whether or not C/C++ global data initialization will be performed (return value of 0 to bypass C/C++ auto-initialization). Refer to the MSP430 Software Coding Techniques Appication Report, chapter 3.6 "Using a Low-Level Initialization Function" for more detailed information regarding this issue.

所以我尝试添加以下函数(以便在分配内存之前停止 WDT):

__attribute__((naked, section(".init5"))) void __low_level_init()
{
WDTCTL = WDTPW + WDTHOLD; //Stop WDT
}

但是问题还是存在...

我做过的其他测试: - 改变了编译器的版本 - 将数组拆分为 15 个元素中的两个或更多个 - 在主函数或“.init3”、“.init1”、“.init7”中停止 WDT

有什么想法吗?谢谢。

编辑:按照建议,我分析了 GCC 输出的 ASM 代码(用“##”评论了不工作的版本):

    .file   "main.c"
.arch msp430g2211
.cpu 430
.mpy none

.text
.Ltext0:
.comm dir,2,2
.comm colors,2,2
.section .init9,"ax",@progbits
.p2align 1,0
.global main
.type main,@function
/***********************
* Function `main'
***********************/
main:
.LFB0:
.file 1 "main.c"
.loc 1 61 0
mov r1, r4
.LCFI0:
add #2, r4
.LCFI1:
add #llo(-100), r1 ## add #llo(-150), r1
.LCFI2:
.loc 1 63 0
mov #23168, &__WDTCTL
.loc 1 66 0
mov.b &__CALBC1_1MHZ, r15
mov.b r15, &__BCSCTL1
.loc 1 67 0
mov.b &__CALDCO_1MHZ, r15
mov.b r15, &__DCOCTL
.loc 1 68 0
mov.b &__P1DIR, r15
bis.b #127, r15
mov.b r15, &__P1DIR
.loc 1 90 0
mov r4, r15
add #llo(-102), r15 ## add #llo(-152), r15
mov #100, r14 ## mov #150, r14
mov r14, r13
mov #0, r14
call #memset
mov.b #llo(-64), -102(r4) ## mov.b #llo(-64), -152(r4)
mov.b #llo(-64), -101(r4) ## mov.b #llo(-64), -151(r4)
...

diff 命令还说:

106c106
< add #llo(-102), r15
---
> add #llo(-152), r15
187c187
< cmp #20, r15
---
> cmp #30, r15
193c193
< add #llo(-100), r15
---
> add #llo(-150), r15
541c541
< .sleb128 -102
---
> .sleb128 -152
548c548
< .byte 0x63
---
> .byte 0x95

我不是 ASM 专家,但 WDT 指令似乎就在正确的位置。我不明白问题出在哪里...谢谢。

最佳答案

听起来像是一个编译器错误,在这种情况下,如果您查看 GCC 生成的 asm,可能很明显出了什么问题。

在您发布后我查看了 asm,我发现您只有 128 字节的 SRAM 可以使用。这个uC的汇编器我不是很熟悉,但是我觉得是超出了。不是单独分配颜色数组,而是结合堆栈?

关于c - 如果我使用超过 25 个元素的数组,msp430g2211 (launchpad) 在启动时停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14242327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com