初级会员
- 积分
- 114
- 金钱
- 114
- 注册时间
- 2020-3-12
- 在线时间
- 23 小时
|
1金钱
本帖最后由 liyancao001 于 2023-3-9 17:39 编辑
用的STM32F407ZGT6,然后用CUBE的LWIP,收发500字节数据,结果实测UDP接收没啥问题,发送语句实际只发出去252字节。是需要调整哪个参数呢?今天又用STM32F767的原子开发板测试,先用原子例程,发现最多可以收发1400+字节,而用CUBE的LWIP则只能发送550字节。经过测试发现是PBUF_POOL_BUFSIZE这个宏定义直接影响了结果。
从下面截图可以看到,目前PBUF_POOL_BUFSIZE是592。实际程序中是受TCP_MSS影响,
#define TCP_MSS 536
如果把这个536改为936,则等同于PBUF_POOL_BUFSIZE为992时,测试发现最大能发出950字节了。
看了下别的帖子,发现应该是分配内存的问题,正常应该是数据超过PBUF_POOL_BUFSIZE时,应该分配多页,但是实际上只给分了1页,导致数据丢失了。
那么怎么才能解决这个问题呢?
原子例程中的pbuf文件跟CUBE的应该不一样版本,差异比较大。没看明白为啥原子例程可以,CUBE就不行。。。
相关代码如下:
- void udp_client_send_hex(char *pData,uint16_t len)
- {
- struct pbuf *p;
-
- /* 分配缓冲区空间 */
- p = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_POOL);
-
- if (p != NULL)
- {
- /* 填充缓冲区数据 */
- pbuf_take(p, pData, len);
- /* 发送udp数据 */
- udp_send(upcb, p);
- /* 释放缓冲区空间 */
- pbuf_free(p);
- }
- }
复制代码
‘- struct pbuf *
- pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
- {
- struct pbuf *p;
- u16_t offset = (u16_t)layer;
- LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F")\n", length));
- switch (type) {
- case PBUF_REF: /* fall through */
- case PBUF_ROM:
- p = pbuf_alloc_reference(NULL, length, type);
- break;
- case PBUF_POOL: {
- struct pbuf *q, *last;
- u16_t rem_len; /* remaining length */
- p = NULL;
- last = NULL;
- rem_len = length;
- do {
- u16_t qlen;
- q = (struct pbuf *)memp_malloc(MEMP_PBUF_POOL);
- if (q == NULL) {
- PBUF_POOL_IS_EMPTY();
- /* free chain so far allocated */
- if (p) {
- pbuf_free(p);
- }
- /* bail out unsuccessfully */
- return NULL;
- }
- qlen = LWIP_MIN(rem_len, (u16_t)(PBUF_POOL_BUFSIZE_ALIGNED - LWIP_MEM_ALIGN_SIZE(offset)));
- pbuf_init_alloced_pbuf(q, LWIP_MEM_ALIGN((void *)((u8_t *)q + SIZEOF_STRUCT_PBUF + offset)),
- rem_len, qlen, type, 0);
- LWIP_ASSERT("pbuf_alloc: pbuf q->payload properly aligned",
- ((mem_ptr_t)q->payload % MEM_ALIGNMENT) == 0);
- LWIP_ASSERT("PBUF_POOL_BUFSIZE must be bigger than MEM_ALIGNMENT",
- (PBUF_POOL_BUFSIZE_ALIGNED - LWIP_MEM_ALIGN_SIZE(offset)) > 0 );
- if (p == NULL) {
- /* allocated head of pbuf chain (into p) */
- p = q;
- } else {
- /* make previous pbuf point to this pbuf */
- last->next = q;
- }
- last = q;
- rem_len = (u16_t)(rem_len - qlen);
- offset = 0;
- } while (rem_len > 0);
- break;
- }
- case PBUF_RAM: {
- u16_t payload_len = (u16_t)(LWIP_MEM_ALIGN_SIZE(offset) + LWIP_MEM_ALIGN_SIZE(length));
- mem_size_t alloc_len = (mem_size_t)(LWIP_MEM_ALIGN_SIZE(SIZEOF_STRUCT_PBUF) + payload_len);
- /* bug #50040: Check for integer overflow when calculating alloc_len */
- if ((payload_len < LWIP_MEM_ALIGN_SIZE(length)) ||
- (alloc_len < LWIP_MEM_ALIGN_SIZE(length))) {
- return NULL;
- }
- /* If pbuf is to be allocated in RAM, allocate memory for it. */
- p = (struct pbuf *)mem_malloc(alloc_len);
- if (p == NULL) {
- return NULL;
- }
- pbuf_init_alloced_pbuf(p, LWIP_MEM_ALIGN((void *)((u8_t *)p + SIZEOF_STRUCT_PBUF + offset)),
- length, length, type, 0);
- LWIP_ASSERT("pbuf_alloc: pbuf->payload properly aligned",
- ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0);
- break;
- }
- default:
- LWIP_ASSERT("pbuf_alloc: erroneous type", 0);
- return NULL;
- }
- LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F") == %p\n", length, (void *)p));
- return p;
- }
复制代码
|
|