简体   繁体   English

C语言的RAM校验和

[英]RAM Checksum in C language

I need to check the RAM of a MCU at startup using a checkerboard-like algorithm. 我需要在启动时使用类似于棋盘的算法检查MCU的RAM。 I do not want to lose whatever data is already in RAM, and also i do not know how not to affect the variables im using to perform this algorithm. 我不想丢失RAM中已经存在的任何数据,而且我也不知道如何不影响im用来执行此算法的变量。 I was thinking something like: 我在想类似的东西:

for (position=0; position< 4096; position++)
 {
     *Temporal = 0x5555;
     if(*Temporal != 0x5555) Error = TRUE;
     *Temporal  = 0xAAAA;
     if(*Temporal != 0xAAAA) Error= TRUE;

    Temporal +=1;

 }

should i modify the linker to know where Temporal and Error are being placed? 我应该修改链接器以了解将“时间”和“错误”放置在何处?

Use the modifier "register" when declaring your pointers (as in "register int *"). 在声明指针时使用修饰符“ register”(如“ register int *”中所示)。 Registers are a special segment of memory inside the processor core that (usually) does not count as part of RAM, so any changes to them don't count as RAM read/writes. 寄存器是处理器内核中内存的一个特殊部分,(通常)不算作RAM的一部分,因此对它们的任何更改都不算作RAM的读/写。 Some exceptions exist; 存在一些例外。 for instance, in AVR microcontroles, the first 32 bytes of RAM are "faked" into registers. 例如,在AVR微控制器中,RAM的前32个字节被“伪造”到寄存器中。

Your question probably got a downvote for the lack of clarity, and the question about retaining whatever was in RAM being easily solved by most beginner C programmers (just copy the contents of the pointer into a temp variable before testing it, and copying back after the end of the test). 您的问题可能由于缺乏清晰性而感到沮丧,大多数初学者C程序员很容易解决关于保留RAM中内容的问题(只需在测试之前将指针的内容复制到temp变量中,然后在执行完之后将其复制回测试结束)。 Also, you're not performing a Checksum: you're just making a Memory Test. 另外,您执行校验和:您只是在进行内存测试。 These are very different, separate things. 这些是截然不同的东西。

You need to ensure the memory that you are testing is in a different location than the memory containing the program you are running. 您需要确保要测试的内存与包含正在运行的程序的内存不在同一位置。 You may be able to do this by running directly out of flash, or whatever other permanent storage you have connected. 您可以通过直接在闪存或已连接的任何其他永久存储中运行来实现此目的。 If not, you need to do something with the link map to ensure proper memory segmentation. 如果不是,则需要对链接映射进行某些操作以确保正确的内存分段。

Inside the test function, ussing register as MVittiS suggests is a good idea. 在测试功能内部,按照MVittiS的建议使用register是个好主意。 Another alternate is to use a global variable mapped to a different segment than the one under test. 另一种选择是使用映射到与被测对象不同的段的全局变量。

You may want to read this article about memory testing to understand the limitations of the test you propose, how memory can fail, and what you should be testing for. 您可能需要阅读有关内存测试的本文 ,以了解您建议的测试的局限性,内存可能如何发生故障以及应该进行的测试。

I don't know (not enough details) but your memory test may be a cache test and might not test any RAM at all. 我不知道(细节不够多),但是您的内存测试可能是缓存测试,可能根本不测试任何RAM。

Your memory test (if it does test memory) is also very badly designed. 您的内存测试(如果确实测试内存)也设计得很糟糕。 For example, you could chop (or short out) all of the address lines and all data lines except for the least significant 2 data lines, and even though there'd be many extremely serious faults the test will still pass. 例如,您可以砍除(或短路)所有地址线和所有数据线(除了最低有效的2条数据线之外),即使存在许多非常严重的错误,测试仍然可以通过。

My advice would be to read something like this web page about memory testing, just to get some ideas and background information: http://www.esacademy.com/en/library/technical-articles-and-documents/miscellaneous/software-based-memory-testing.html 我的建议是阅读类似于此网页的有关内存测试的内容,以获取一些想法和背景信息: http : //www.esacademy.com/en/library/technical-articles-and-documents/miscellaneous/software- based-memory-testing.html

In general, for non-destructive tests you'd copy whatever you want to keep somewhere else, then do the test, then copy the data back. 通常,对于非破坏性测试,您需要复制要保留在其他地方的任何内容,然后进行测试,然后再将数据复制回去。 It's very important that "somewhere else" is tested first (you don't want to copy everything into faulty RAM and then copy it back). 首先测试“其他地方”非常重要(您不想将所有内容复制到有故障的RAM中,然后再复制回去)。

I would also recommend using assembly (instead of C) to ensure no unwanted memory accesses are made; 我还建议使用汇编程序(而不是C)来确保不进行不必要的内存访问。 including your stack. 包括您的堆栈。

If your code is in RAM that needs to be tested then you will probably need 2 copies of your RAM testing code. 如果您的代码位于需要测试的RAM中,那么您可能需要两份RAM测试代码。 You'd use the second copy of your code when you're testing the RAM that contains the first copy of your code. 在测试包含代码的第一个副本的RAM时,将使用代码的第二个副本。 If your code is in ROM then it's much easier (but you still need to worry about your stack). 如果您的代码在ROM中,那么它会容易得多(但是您仍然需要担心堆栈)。

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

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