GTL消息和(动态)内存分配

11个帖子/ 0新
最后一篇
abremen
离线
最后一次露面:4 years 2 months ago
加入:2015-02-10 17:44
GTL消息和(动态)内存分配

Dear support,

目前我正在在自定义嵌入式项目中实现GTL接口(OVER UART)。我的第一个目标是获得与我们的外部处理器组合的Prox_Reporter_ext项目(DA1458X_SDK \ 5.0.2.1 \ projects \ target_apps \ ble_examples文件夹)。我使用Host_Proxr_sdk项目作为参考(da1458x_sdk \ 5.0.2.1 \ projects \ host_apps \ windows \ proximity \ reporter \ folder)。

我面临的挑战是内存分配。直到现在我们已经避免使用Malloc /免费在我们的嵌入式代码中使用,只需防止引入灾难性的副作用,例如内存泄漏(也可能与我们的RTO结合引入新的挑战)。在上述项目的BLE_MSG中,我看到MALLOC和免费使用例如BLEMSGALLOC和BLEFREEMSG,同样用于UART.C中的Sendtomain。此外,UARTPROC(UART.C)中的接收缓冲区的大小为1000个字节,MAX_PACKET_LENGTH为350字节(UART.H)。在UARTSEND(UART.C)中分配了另外500个字节。

从我所理解的来源和通过综合进程应用程序的UM-B-017 GTL接口阅读.pdf和UM-B-010_DA14580_581_583 Proximity Application_v1.3.pdf,GTL接口不能被分类为停止 -等待协议。换句话说,DA14580可以在任何给定时刻将DA14580发送到外部处理器的多个事件分组/消息。另一方面,外部处理器可以在应用程序所需时发送命令包/消息。

我完全理解使用动态的原因mory allocation, it makes sense with a variable number of packets and a variable PAR_LEN field value. However I would like to know if static memory allocation is a viable option (and achievable looking at memory requirements). In this case I would like to know what the max value for PAR_LEN is (the maximum number of bytes of Parameters that a message can contain) and how many packets / messages could potentially be sent by the DA14580. If feasible, I could create a circular buffer of X number of packets, each with MAX_PAR_LEN bytes of Parameters (we have 32kB of RAM available in total, so for example 3 packets of each 350 bytes with a separate read buffer of 350 bytes and a write buffer of 350 bytes (for asynchronous reading/writing) is not very realistic).

我很想听到你的想法。如果可能,我宁愿不使用malloc /免费。

Kind regards,
arjan.

Edit 02-11-2015
我添加了有关外部性和数据结构填充的信息,也许其他论坛用户也可能会发现这种有用。

Device:
abremen
离线
最后一次露面:4 years 2 months ago
加入:2015-02-10 17:44
很想听到别人

很乐意听到别人在嵌入式系统中如何在具有有限的嵌入式系统中实现这一点和/或Malloc /免费被认为是不良的做法。

Kind regards,
arjan.

mt_dialog.
离线
最后一次露面:4 days 14 hours ago
职员
加入:2015-06-08 11:34
嗨,ablimen,

嗨,ablimen,

We never considered to do something like this, it can be done though. The maximum of potential packets that could be exchanged is fully depended on the current application, i suppose that you can dynamically count in the current implementation how many packets are being allocated and sent, using somekind of counter, and by that you are going to be able to judge to size of memory that should be pre-allocated.

谢谢mt_dialog.

summer20100514
离线
最后一次露面:4 years 2 months ago
格鲁鲁
加入:2014-12-30 05:01
您可以使用动态内存

您可以使用动态内存分配,并且您可以选择由静态缓冲区实现的,而不是原点。更重要的是,您可以添加一个调试功能,如PrintF到Project DA14580_SDK_3.0.4.0 \ host_Apps \ Windows \ Proximity \ Monitor,看看发送和接收多少数据包。就我而言,32KB的RAM完全充分,也许5KB就足够了。

abremen
离线
最后一次露面:4 years 2 months ago
加入:2015-02-10 17:44
@MT_dialog thank you for your

@MT_dialog thank you for your response, I ended up using dynamic memory allocation for obvious reasons. However it was meaningful experimenting with static memory allocation.

@summer20100514 thank you for your response. Note that the 32kB of RAM I mentioned is for the entire system, not just the BLE related portion of it. So 5kB would still be a rather big chunk ;-).

Perhaps you can help me out with a new question; Are all data structures in the SDK explicitly aligned on the right boundaries? Or is it assumed that data structures will be packed in both DA14580 as well as host code (in order to use the SDK)? I notice sizeof is used quite often to determine the size of the different message parameters, I was just wondering whether or not potential implicit padding was taken into account.

谢谢,
arjan.

summer20100514
离线
最后一次露面:4 years 2 months ago
格鲁鲁
加入:2014-12-30 05:01
您可以打印出地址

您可以打印出地址if you are not quite sure about the alignment.

abremen
离线
最后一次露面:4 years 2 months ago
加入:2015-02-10 17:44
@ summer20100514也许我

@ Summer20100514也许我应该澄清我的问题。

让我们拍摄struct proxr_enable_req:


// sdk \ ble_stack \ profiles \ prox \ proxr \ proxr_task.h

/// @ref proxr_enable_req消息的参数
struct proxr_enable_req
{
///连接句柄
uint16_t conhdl;
/// 安全级别
uint8_t sec_l​​vl;

/// Saved LLS alert level to set in ATT DB
uint8_t lls_alert_lvl;
/// TX Power level
INT8_T TXP_LVL;
};

在创建Proxr_Enable_Req GTL数据包时,SizeOf(struct proxr_enable_req)用于确定参数长度。在这种情况下,结构的编译值,因此是分组参数长度的值为6;在成员TXP_LVL之后,添加了另外的填充字节。换一种说法;最后一个成员用所需的字节数填充,使得结构的总大小是任何结构成员的最大对准的倍数。

When casting the incoming byte array to the message's corresponding parameters struct type we need to be sure that this implicit padding is, or is not taken into account by both sender and receiver. In case of proxr_enable_req we wouldn't have a real issue dereferencing the individual members, as padding is merely added after the last member. However if you take the example below, individual members might not be where you'd expect them to be (depending on whether or not sender and receiver assume different rules regarding packing structures).

以下是具有各种类型的成员的结构,在编译之前总计8个字节:


结构混合数据
{
char data1;
短数据2;
int data3;
char data4;
};

编译后,数据结构将补充填充字节,以确保每个成员的适当对齐:


struct mixeddata / *编译32位x86机器* /
{
char data1;/ * 1字节* /
char padding1 [1];/ * 1字节用于以下“短”以在2字节边界上对齐
假设结构开始的地址是偶数* /
短数据2;/ * 2字节* /
int data3;/* 4 bytes - largest structure member */
char data4;/ * 1字节* /
char padding2 [3];/ * 3个字节,使结构的总大小为12字节* /
};

https://en.wikipedia.org/wiki/data_structure_alignment.

长话短说,我很想从对话本身学习哪些设计选择是用达SDK制作的。是否在字段之间发送或没有填充字节的数据包参数?

summer20100514
离线
最后一次露面:4 years 2 months ago
格鲁鲁
加入:2014-12-30 05:01
I didn't notice the matter

I didn't notice the matter before, but it seemed nothing wrong happened. Maybe the padding byte is always added after the last member ?

abremen
离线
最后一次露面:4 years 2 months ago
加入:2015-02-10 17:44
因为我还没有来

由于我尚未遇到任何明确地指定数据的文档,因为在结构成员之间包含数据结构以及数据结构是否存在,我的猜测是直到现在,SDK仅用于/打算(32位)平台,带有少量内存架构的没有结构(例如,我在SDK代码中找到了任何__Attribute __(packed))。

As long as both systems use sizeof(struct ) for setting the GTL packet parameter length and cast the incoming and outgoing parameter byte array to these struct types, everything will work just fine (as both platforms expect the same struct member padding / member alignment).

Even tough I believe this is the case with how the SDK has been used so far, I would really appreciate a response from Dialog itself regarding this matter.

- 是否假设结构未打包(编译器添加的隐式填充)是正确的?
- 没有添加显式填充,以确保成员正确对齐,结构大小是其最大成员大小的倍数?(即,这是留给编译器)

Please note that I am absolutely fine with this approach (with our current host platform), as our host uses the 32-bit ARM Cortex-M0+ processor as well. Both DA14580 and host processor are configured with little-endian byte order. In other words, sizeof() will return the same result on both platforms and struct members will be aligned exactly the same on both platforms (with the same implicit padding added by the compiler).

我在跨平台解决方案中使用了这些类型的面向字节的面向的基于包装的协议(具有不同的内存架构)。也许对于SDK的未来未来添加可以是添加额外的抽象级别,通过指定字段之间存在没有填充字节,并使用写/读取模块以以endianness独立方式写入/读取多字节数据类型。这样你就可以删除它的平台依赖性方面。

我是SDK的忠实粉丝(特别是5.0.3,爱它!),我只是喜欢尽可能地清理的东西:-)。

Looking forward to your response Dialog.
Kind regards,
arjan.

mt_dialog.
离线
最后一次露面:4 days 14 hours ago
职员
加入:2015-06-08 11:34
嗨,ablimen,

嗨,ablimen,

The alignment is left to the compiler to decide, the reason for that is because windows and ARM have the same endianess (little endian) and sizes. In case of a different architecture the compiler will fail and you will have to declare the structs explictly.

谢谢mt_dialog.

abremen
离线
最后一次露面:4 years 2 months ago
加入:2015-02-10 17:44
Hi MT_dialog,

Hi MT_dialog,

谢谢你的确认。现在可以关闭此主题。

Kind regards,
arjan.

主题锁定