在嵌入式系统中存储大整数/值
我正在开发一个嵌入式系统,可以测试大量电线(最多 360 条)——本质上是一个连续性检查系统。该系统的工作原理是输入测试向量并读取另一端的输出。然后将输出与存储的结果(存储在 SD 卡上)进行比较,该结果告诉输出应该是什么。测试向量只是步行向量,因此无需将它们存储在任何地方。该过程有点像如下:
- 输出测试向量(步行向量)
- 读入输出测试向量。
- 从 SD 卡读取相应的输出测试向量,它告诉输出向量应该是什么。
- 比较步骤 2 和 3 中的测试向量
- 。记下单独数组中的错误/故障。
- 继续返回步骤 1,除非检查了所有电线。
- 将错误/故障输出到 LCD。
我的硬件由一个大型移位寄存器组成,该寄存器计时到 AVR 微控制器中。对于每个测试向量(也是 360 位),我需要读入 360 位。因此,对于 360 条电线,数据总量约为 360*360 = 16kB 左右。我已经知道我无法一次性完成此操作(即读取整个数据然后进行比较),因此必须逐个测试向量进行测试。
由于没有固有类型可以容纳如此大的数字,因此我打算使用长度为 360 位的位数组。 现在,我的问题是,我应该如何在txt文件中存储这个位数组?
一种方法是存储原始值,即在每一行存储我从移位寄存器读入的原始二进制数据。因此,对于 8 根电线,它将是 0b10011010。但对于最多 360 条线来说,这可能会变得很糟糕 - 每条线将包含 360 个字节。
另一种方法是存储十六进制值 - 8 位只有两个字符(上面的 9A),360 位大约有 90 个字符。然而,这需要我逐行读取文本,并以某种方式将十六进制值转换为位数组中表示的值。
那么解决此类问题的最佳方案是什么?我需要完全“确定性”的解决方案 - 我不能调用 malloc 等。从我读到的内容来看,它们在嵌入式系统中有点禁忌。
摘要
我需要存储无法用任何传统变量类型表示的大值。目前我打算将这些值存储在位数组中。将这些值存储在 SD 卡上的文本文件中的最佳方法是什么?
I'm developing a embedded system that can test a large numbers of wires (upto 360) - essentially a continuity checking system. The system works by clocking in a test vector and reading the output from the other end. The output is then compared with a stored result (which would be on an SD Card) that tells what the output should have been. The test-vectors are just a walking ones so there's no need to store them anywhere. The process would be a bit like follows:
- Clock out test-vector (walking ones)
- Read in output test-vector.
- Read corresponding output test-vector from SD Card which tells what the output vector should be.
- Compare the test-vectors from step 2 and 3.
- Note down the errors/faults in a separate array.
- Continue back to step 1 unless all wires are checked.
- Output the errors/faults to the LCD.
My hardware consists of a large shift register thats clocked into the AVR microcontroller. For every test vector (which would also be 360 bits), I will need to read in 360 bits. So, for 360 wires the total amount of data would be 360*360 = 16kB or so. I already know I cannot do this in one pass (i.e. read the entire data and then compare), so it will have to be test-vector by test-vector.
As there are no inherent types that can hold such large numbers, I intend to use a bit-array of length 360 bit. Now, my question is, how should I store this bit array in a txt file?
One way is to store raw values i.e. on each line store the raw binary data that I read in from the shift register. So, for 8 wires, it would be 0b10011010. But this can get ugly for upto 360 wires - each line would contain 360 bytes.
Another way is to store hex values - this would just be two characters for 8 bits (9A for the above) and about 90 characters for 360 bits. This would, however, require me to read in the text - line by line - and convert the hex value to be represented in the bit-array, somehow.
So whats the best solution for this sort of problem? I need the solution to be completely "deterministic" - I can't have calls to malloc or such. They are a bit of a no-no in embedded systems from what I've read.
SUMMARY
I need to store large values that can't be represented by any traditional variable types. Currently I intend to store these values in a bitarray. What's the best way to store these values in a text file on an SD Card?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些不是整数值而是位图;它们没有算术意义。您所建议的只是一个长度为 360/8 的字节数组,与“大整数”根本无关。然而,一些更合适的数据结构或表示可能是可能的。
如果测试向量是 360 个比特,那么为每个向量存储 360 个比特既低效又不必要,0 到 359 的值足以明确定义每个向量。如果正确的输出也是单个位,那么它也可以存储为位索引,如果不是,那么您可以将其存储为应设置的每个位的索引列表,其中一些哨兵值 >=360 或<0 表示列表末尾。在大多数向量包含少于 22 个设置位的情况下,此结构将比存储 45 字节数组更有效。
根据任何位索引值,您可以通过以下方式确定各个线路的地址和掩码:
您可以迭代测试每个 360 位,也可以从位列表动态生成 360 位向量。
我认为不需要动态内存分配,但在嵌入式系统中是否建议这样做很大程度上取决于应用程序和目标资源。典型的 AVR 系统的内存非常少,动态内存分配会带来堆管理和块对齐的开销,而您可能无法承受。动态内存分配不适合需要硬实时确定性定时的情况。在所有情况下,您都应该有一个明确定义的策略或架构来避免内存泄漏问题(重复分配永远不会释放的内存)。
These are not integer values but rather bit maps; they have no arithmetic meaning. What you are suggesting is simply a byte array of length 360/8, and not related to "large integers" at all. However some more appropriate data structure or representation may be possible.
If the test vector is a single bit in 360, then it is both inefficient and unnecessary to store 360 bits for each vector, a value 0 to 359 is sufficient to unambiguously define each vector. If the correct output is also a single bit, then that could also be stored as a bit index, if not then you could store it as a list of indices for each bit that should be set, with some sentinel value >=360 or <0 to indicate the end of the list. Where most vectors contain less than fewer than 22 set bits, this structure will be more efficient that storing a 45 byte array.
From any bit index value, you can determine the address and mask of the individual wire by:
You could either test each of the 360 bits iteratively or generate a 360 bit vector on the fly from the list of bits.
I can see no need for dynamic memory allocation in this, but whether or not it is advisable in an embedded system is largely dependent on the application and target resources. A typical AVR system has very little memory, and dynamic memory allocation carries an overhead for heap management and block alignment that you may not be able to afford. Dynamic memory allocation is not suited in situations where hard real-time deterministic timing is required. And in all cases you should have a well defined strategy or architecture for avoiding memory leak issues (repeatedly allocating memory that never gets released).