正在移植ucosii.
移植前的环境是STM32F103VE + lwip2.1.2
在ucosii_v2.92.07文件迁移进工程后, 可以编译过。
这时要修改启动文件 startup_stm32f10x_hd.s
修改点一共4处, 在中断向量表中替换PendSV_Handler => OS_CPU_PendSVHandler和SysTick_Handler => OS_CPU_SysTickHandler
; Vector Table Mapped to Address 0 at Reset
AREA RESET, DATA, READONLY
EXPORT __Vectors
EXPORT __Vectors_End
EXPORT __Vectors_Size
IMPORT OS_CPU_PendSVHandler
IMPORT OS_CPU_SysTickHandler
__Vectors DCD __initial_sp ; Top of Stack
DCD Reset_Handler ; Reset Handler
DCD NMI_Handler ; NMI Handler
DCD HardFault_Handler ; Hard Fault Handler
DCD MemManage_Handler ; MPU Fault Handler
DCD BusFault_Handler ; Bus Fault Handler
DCD UsageFault_Handler ; Usage Fault Handler
DCD 0 ; Reserved
DCD 0 ; Reserved
DCD 0 ; Reserved
DCD 0 ; Reserved
DCD SVC_Handler ; SVCall Handler
DCD DebugMon_Handler ; Debug Monitor Handler
DCD 0 ; Reserved
DCD OS_CPU_PendSVHandler ; PendSV_Handler, PendSV Handler => OS_CPU_PendSVHandler
DCD OS_CPU_SysTickHandler ; SysTick_Handler, SysTick Handler => OS_CPU_SysTickHandler
; External Interrupts
这时出现编译报错
compiling sockets.c...
linking...
.\Objects\my_stm32f1_SPL_template.axf: Error: L6406E: No space in execution regions with .ANY selector matching os_core.o(.bss).
.\Objects\my_stm32f1_SPL_template.axf: Error: L6406E: No space in execution regions with .ANY selector matching my_nic.o(.bss).
.\Objects\my_stm32f1_SPL_template.axf: Error: L6406E: No space in execution regions with .ANY selector matching dns.o(.bss).
.\Objects\my_stm32f1_SPL_template.axf: Error: L6406E: No space in execution regions with .ANY selector matching startup_stm32f10x_hd.o(STACK).
.\Objects\my_stm32f1_SPL_template.axf: Error: L6406E: No space in execution regions with .ANY selector matching stats.o(.bss).
.\Objects\my_stm32f1_SPL_template.axf: Error: L6406E: No space in execution regions with .ANY selector matching tcpip.o(.bss).
.\Objects\my_stm32f1_SPL_template.axf: Error: L6406E: No space in execution regions with .ANY selector matching etharp.o(.bss).
.\Objects\my_stm32f1_SPL_template.axf: Error: L6406E: No space in execution regions with .ANY selector matching netif.o(.bss).
.\Objects\my_stm32f1_SPL_template.axf: Error: L6406E: No space in execution regions with .ANY selector matching os_core.o(.data).
.\Objects\my_stm32f1_SPL_template.axf: Error: L6406E: No space in execution regions with .ANY selector matching memp.o(.data).
.\Objects\my_stm32f1_SPL_template.axf: Error: L6406E: No space in execution regions with .ANY selector matching err.o(.data).
.\Objects\my_stm32f1_SPL_template.axf: Error: L6406E: No space in execution regions with .ANY selector matching dhcp.o(.bss).
.\Objects\my_stm32f1_SPL_template.axf: Error: L6406E: No space in execution regions with .ANY selector matching tcp_in.o(.data).
.\Objects\my_stm32f1_SPL_template.axf: Error: L6406E: No space in execution regions with .ANY selector matching tcp.o(.data).
.\Objects\my_stm32f1_SPL_template.axf: Error: L6406E: No space in execution regions with .ANY selector matching ip.o(.bss).
.\Objects\my_stm32f1_SPL_template.axf: Error: L6406E: No space in execution regions with .ANY selector matching tcp_in.o(.bss).
.\Objects\my_stm32f1_SPL_template.axf: Error: L6406E: No space in execution regions with .ANY selector matching mem.o(.data).
.\Objects\my_stm32f1_SPL_template.axf: Error: L6406E: No space in execution regions with .ANY selector matching dhcp.o(.data).
.\Objects\my_stm32f1_SPL_template.axf: Error: L6406E: No space in execution regions with .ANY selector matching netif.o(.data).
.\Objects\my_stm32f1_SPL_template.axf: Error: L6406E: No space in execution regions with .ANY selector matching dns.o(.data).
.\Objects\my_stm32f1_SPL_template.axf: Error: L6406E: No space in execution regions with .ANY selector matching timeouts.o(.data).
.\Objects\my_stm32f1_SPL_template.axf: Error: L6406E: No space in execution regions with .ANY selector matching enc28j60.o(.data).
.\Objects\my_stm32f1_SPL_template.axf: Error: L6406E: No space in execution regions with .ANY selector matching udp.o(.data).
.\Objects\my_stm32f1_SPL_template.axf: Error: L6406E: No space in execution regions with .ANY selector matching igmp.o(.data).
.\Objects\my_stm32f1_SPL_template.axf: Error: L6406E: No space in execution regions with .ANY selector matching ip4.o(.data).
.\Objects\my_stm32f1_SPL_template.axf: Error: L6406E: No space in execution regions with .ANY selector matching ip4_frag.o(.data).
.\Objects\my_stm32f1_SPL_template.axf: Error: L6406E: No space in execution regions with .ANY selector matching os_cpu_c.o(.data).
.\Objects\my_stm32f1_SPL_template.axf: Error: L6406E: No space in execution regions with .ANY selector matching raw.o(.data).
.\Objects\my_stm32f1_SPL_template.axf: Error: L6406E: No space in execution regions with .ANY selector matching stdout.o(.data).
.\Objects\my_stm32f1_SPL_template.axf: Error: L6406E: No space in execution regions with .ANY selector matching pbuf.o(.data).
.\Objects\my_stm32f1_SPL_template.axf: Error: L6406E: No space in execution regions with .ANY selector matching etharp.o(.data).
.\Objects\my_stm32f1_SPL_template.axf: Error: L6407E: Sections of aggregate size 0x3ecc bytes could not fit into .ANY selector(s).
Not enough information to list image symbols.
Not enough information to list load addresses in the image map.
Finished: 2 information, 0 warning and 32 error messages.
".\Objects\my_stm32f1_SPL_template.axf" - 32 Error(s), 0 Warning(s).
Target not created.
Build Time Elapsed: 00:00:08
报错信息说代码摆不下了, 那不是代码区不够了,就是内存区不够了。
这时,突显svn的重要性(有了可靠的进展,就存档)。
回到修改之前.
; Vector Table Mapped to Address 0 at Reset
AREA RESET, DATA, READONLY
EXPORT __Vectors
EXPORT __Vectors_End
EXPORT __Vectors_Size
; IMPORT OS_CPU_PendSVHandler
; IMPORT OS_CPU_SysTickHandler
__Vectors DCD __initial_sp ; Top of Stack
DCD Reset_Handler ; Reset Handler
DCD NMI_Handler ; NMI Handler
DCD HardFault_Handler ; Hard Fault Handler
DCD MemManage_Handler ; MPU Fault Handler
DCD BusFault_Handler ; Bus Fault Handler
DCD UsageFault_Handler ; Usage Fault Handler
DCD 0 ; Reserved
DCD 0 ; Reserved
DCD 0 ; Reserved
DCD 0 ; Reserved
DCD SVC_Handler ; SVCall Handler
DCD DebugMon_Handler ; Debug Monitor Handler
DCD 0 ; Reserved
DCD PendSV_Handler ; PendSV_Handler, PendSV Handler => OS_CPU_PendSVHandler
DCD SysTick_Handler ; SysTick_Handler, SysTick Handler => OS_CPU_SysTickHandler
compiling app_hooks.c...
linking...
Program Size: Code=94472 RO-data=3872 RW-data=436 ZI-data=64508
".\Objects\my_stm32f1_SPL_template.axf" - 0 Error(s), 0 Warning(s).
Build Time Elapsed: 00:00:07
用掉的代码区size = Code=94472 + RO-data=3872 = 98344 = 96KB
用到的片内RAM区size = RW-data=436 + ZI-data=64508 = 64944 = 63KB
STM32F103的资源在STMCUFinder中可以找到
STM32F103的可用代码区 = 512KB
STM32F103的可用RAM区 = 64KB
结合能编译过的工程来看,代码区是够的,内存区接近64KB. 那可能是内存区不够了(再用一点内存,内存区的物理size就超过了)。
工程编译过时,会生成map文件,里面有内存用量和代码用量的分布统计。
去看map文件,查看内存区在哪里用了大块的内存区.
Component: ARM Compiler 5.06 update 6 (build 750) Tool: armlink [4d35ed]
==============================================================================
Section Cross References
stm32f10x_it.o(i.EXTI1_IRQHandler) refers to stm32f10x_exti.o(i.EXTI_GetITStatus) for EXTI_GetITStatus
...
depilogue.o(.text) refers to llushr.o(.text) for __aeabi_llsr
==============================================================================
Removing Unused input sections from the image.
Removing system_stm32f10x.o(i.SystemCoreClockUpdate), (164 bytes).
...
Removing app_hooks.o(i.App_TimeTickHook), (2 bytes).
1025 unused section(s) (total 116499 bytes) removed from the image.
==============================================================================
Image Symbol Table
Local Symbols
Symbol Name Value Ov Type Size Object(Section)
../clib/../cmprslib/zerorunl2.c 0x00000000 Number 0 __dczerorl2.o ABSOLUTE
...
.data 0x20000108 Section 68 err.o(.data)
err_strerr 0x20000108 Data 68 err.o(.data)
.data 0x2000014c Section 100 os_core.o(.data)
.data 0x200001b0 Section 4 stdout.o(.data)
.bss 0x200001b4 Section 3140 my_nic.o(.bss)
MySendbuf 0x20000240 Data 1500 my_nic.o(.bss)
MyRecvbuf 0x2000081c Data 1500 my_nic.o(.bss)
.bss 0x20000df8 Section 1152 dns.o(.bss)
dns_pcbs 0x20000df8 Data 16 dns.o(.bss)
dns_table 0x20000e08 Data 1088 dns.o(.bss)
dns_requests 0x20001248 Data 48 dns.o(.bss)
.bss 0x20001278 Section 24 ip.o(.bss)
.bss 0x20001290 Section 10259 mem.o(.bss)
.bss 0x20003aa4 Section 47544 memp.o(.bss) // !!! 这里用的太多了
memp_stats_RAW_PCB 0x20003afc Data 16 memp.o(.bss)
memp_stats_UDP_PCB 0x20003bc0 Data 16 memp.o(.bss)
memp_stats_TCP_PCB 0x20003ee0 Data 16 memp.o(.bss)
memp_stats_TCP_PCB_LISTEN 0x20003ff4 Data 16 memp.o(.bss)
memp_stats_TCP_SEG 0x20004148 Data 16 memp.o(.bss)
memp_stats_ALTCP_PCB 0x20004238 Data 16 memp.o(.bss)
memp_stats_REASSDATA 0x200049cc Data 16 memp.o(.bss)
memp_stats_FRAG_PBUF 0x20004b48 Data 16 memp.o(.bss)
memp_stats_NETBUF 0x20004b7c Data 16 memp.o(.bss)
memp_stats_NETCONN 0x20006d78 Data 16 memp.o(.bss)
memp_stats_TCPIP_MSG_API 0x20006e8c Data 16 memp.o(.bss)
memp_stats_TCPIP_MSG_INPKT 0x20006fa0 Data 16 memp.o(.bss)
memp_stats_ARP_QUEUE 0x200070a4 Data 16 memp.o(.bss)
memp_stats_IGMP_GROUP 0x20007138 Data 16 memp.o(.bss)
memp_stats_SYS_TIMEOUT 0x2000725c Data 16 memp.o(.bss)
memp_stats_NETDB 0x200073a4 Data 16 memp.o(.bss)
memp_stats_PBUF 0x200074b8 Data 16 memp.o(.bss)
memp_stats_PBUF_POOL 0x2000f44c Data 16 memp.o(.bss)
.bss 0x2000f45c Section 140 netif.o(.bss)
loop_netif 0x2000f45c Data 140 netif.o(.bss)
.bss 0x2000f4e8 Section 496 stats.o(.bss)
.bss 0x2000f6d8 Section 20 tcp_in.o(.bss)
inseg 0x2000f6d8 Data 20 tcp_in.o(.bss)
.bss 0x2000f6ec Section 50 dhcp.o(.bss)
.bss 0x2000f720 Section 240 etharp.o(.bss)
arp_table 0x2000f720 Data 240 etharp.o(.bss)
.bss 0x2000f810 Section 412 tcpip.o(.bss)
tcpip_mbox 0x2000f810 Data 412 tcpip.o(.bss)
STACK 0x2000f9b0 Section 1024 startup_stm32f10x_hd.o(STACK)
Global Symbols
Symbol Name Value Ov Type Size Object(Section)
BuildAttributes$$THM_ISAv4$P$D$K$B$S$PE$A:L22UL41UL21$X:L11$S22US41US21$IEEE1$IW$USESV6$~STKCKD$USESV7$~SHL$OSPACE$ROPI$EBA8$MICROLIB$REQ8$PRES8$EABIv2 0x00000000 Number 0 anon$$obj.o ABSOLUTE
__ARM_use_no_argv 0x00000000 Number 0 main.o ABSOLUTE
...
ram_heap 0x20001290 Data 10259 mem.o(.bss)
memp_memory_RAW_PCB_base 0x20003aa4 Data 87 memp.o(.bss)
memp_memory_UDP_PCB_base 0x20003b0c Data 179 memp.o(.bss)
memp_memory_TCP_PCB_base 0x20003bd0 Data 783 memp.o(.bss)
memp_memory_TCP_PCB_LISTEN_base 0x20003ef0 Data 259 memp.o(.bss)
memp_memory_TCP_SEG_base 0x20004004 Data 323 memp.o(.bss)
memp_memory_ALTCP_PCB_base 0x20004158 Data 223 memp.o(.bss)
memp_memory_REASSDATA_base 0x20004248 Data 1923 memp.o(.bss)
memp_memory_FRAG_PBUF_base 0x200049dc Data 363 memp.o(.bss)
memp_memory_NETBUF_base 0x20004b58 Data 35 memp.o(.bss)
memp_memory_NETCONN_base 0x20004b8c Data 8683 memp.o(.bss)
memp_memory_TCPIP_MSG_API_base 0x20006d88 Data 259 memp.o(.bss)
memp_memory_TCPIP_MSG_INPKT_base 0x20006e9c Data 259 memp.o(.bss)
memp_memory_ARP_QUEUE_base 0x20006fb0 Data 243 memp.o(.bss)
memp_memory_IGMP_GROUP_base 0x200070b4 Data 131 memp.o(.bss)
memp_memory_SYS_TIMEOUT_base 0x20007148 Data 275 memp.o(.bss)
memp_memory_NETDB_base 0x2000726c Data 311 memp.o(.bss)
memp_memory_PBUF_base 0x200073b4 Data 259 memp.o(.bss)
memp_memory_PBUF_POOL_base 0x200074c8 Data 32643 memp.o(.bss) // 可以进一步看到 memp_memory_PBUF_POOL_base 这个变量(数组)用了30KB
lwip_stats 0x2000f4e8 Data 496 stats.o(.bss)
dhcp_rx_options_val 0x2000f6ec Data 40 dhcp.o(.bss)
dhcp_rx_options_given 0x2000f714 Data 10 dhcp.o(.bss)
__initial_sp 0x2000fdb0 Data 0 startup_stm32f10x_hd.o(STACK)
==============================================================================
Memory Map of the image
Image Entry point : 0x08000131
Load Region LR_IROM1 (Base: 0x08000000, Size: 0x000181dc, Max: 0x00080000, ABSOLUTE, COMPRESSED[0x00018070])
Execution Region ER_IROM1 (Exec base: 0x08000000, Load base: 0x08000000, Size: 0x00018028, Max: 0x00080000, ABSOLUTE)
Exec Addr Load Addr Size Type Attr Idx E Section Name Object
0x08000000 0x08000000 0x00000130 Data RO 3836 RESET startup_stm32f10x_hd.o
0x08000130 0x08000130 0x00000000 Code RO 9413 * .ARM.Collect$$$$00000000 mc_w.l(entry.o)
...
0x08018008 0x08018008 0x00000020 Data RO 9784 Region$$Table anon$$obj.o
Execution Region RW_IRAM1 (Exec base: 0x20000000, Load base: 0x08018028, Size: 0x0000fdb0, Max: 0x00010000, ABSOLUTE, COMPRESSED[0x00000048])
Exec Addr Load Addr Size Type Attr Idx E Section Name Object
0x20000000 COMPRESSED 0x00000008 Data RW 694 .data enc28j60.o
...
0x2000f9ac COMPRESSED 0x00000004 PAD
0x2000f9b0 - 0x00000400 Zero RW 3834 STACK startup_stm32f10x_hd.o
==============================================================================
Image component sizes
Code (inc. data) RO Data RW Data ZI Data Debug Object Name
0 0 0 0 0 5271 altcp.o
0 0 0 0 0 7832 altcp_alloc.o
2094 964 84 0 0 13456 autoip.o
0 0 0 0 0 4599 bridgeif.o
0 0 0 0 0 32 core_cm3.o
38 0 0 0 0 3030 def.o
11490 4898 286 16 50 28572 dhcp.o
1594 496 0 12 1152 11817 dns.o
1198 86 0 8 0 8167 enc28j60.o
48 22 313 68 0 1150 err.o
4132 1088 65 1 240 12280 etharp.o
560 130 12 0 0 2578 ethernet.o
502 230 6 0 0 13080 ethernetif.o
2376 1026 0 0 0 4722 icmp.o
2804 1178 257 8 0 10299 igmp.o
828 78 0 0 0 7328 inet_chksum.o
140 64 65 0 0 16118 init.o
0 0 0 0 24 8317 ip.o
4680 1558 303 8 0 10250 ip4.o
78 0 8 0 0 9134 ip4_addr.o
3918 1340 0 6 0 11746 ip4_frag.o
746 92 184 0 0 19003 main.o
3148 1270 0 16 10259 10698 mem.o
820 426 636 72 47544 11984 memp.o
132 22 0 0 0 1619 misc.o
456 178 0 0 3140 4765 my_nic.o
4026 1624 0 14 140 22550 netif.o
0 0 0 100 0 9496 os_core.o
5100 2260 141 1 0 20119 pbuf.o
352 80 73 4 0 7571 raw.o
148 12 0 0 0 8097 spi.o
36 8 304 0 1024 816 startup_stm32f10x_hd.o
20 10 0 0 496 5370 stats.o
200 18 0 0 0 2220 stm32f10x_exti.o
350 4 0 0 0 3786 stm32f10x_gpio.o
54 4 65 0 0 256538 stm32f10x_it.o
32 6 0 0 0 597 stm32f10x_rcc.o
112 0 0 0 0 3998 stm32f10x_spi.o
260 34 0 0 0 16637 sys_arch.o
328 28 0 0 0 1957 system_stm32f10x.o
10276 4826 189 32 0 35213 tcp.o
12076 3908 152 40 20 31967 tcp_in.o
8494 3614 521 0 0 27934 tcp_out.o
396 170 0 0 412 5015 tcpip.o
704 308 68 12 0 5505 timeouts.o
5710 2390 73 8 0 14144 udp.o
----------------------------------------------------------------------
90502 34450 3872 432 64508 717377 Object Totals
0 0 32 0 0 0 (incl. Generated)
46 0 35 6 7 0 (incl. Padding)
----------------------------------------------------------------------
Code (inc. data) RO Data RW Data ZI Data Debug Library Member Name
86 0 0 0 0 0 __dczerorl2.o
0 0 0 0 0 0 entry.o
0 0 0 0 0 0 entry10a.o
0 0 0 0 0 0 entry11a.o
8 4 0 0 0 0 entry2.o
4 0 0 0 0 0 entry5.o
0 0 0 0 0 0 entry7b.o
0 0 0 0 0 0 entry8b.o
8 4 0 0 0 0 entry9a.o
30 0 0 0 0 0 handlers.o
36 8 0 0 0 68 init.o
0 0 0 0 0 0 iusefp.o
30 0 0 0 0 68 llshl.o
36 0 0 0 0 68 llsshr.o
32 0 0 0 0 68 llushr.o
26 0 0 0 0 80 memcmp.o
64 0 0 0 0 76 memmovea.o
36 0 0 0 0 108 memseta.o
2324 94 0 0 0 600 printfa.o
0 0 0 4 0 0 stdout.o
14 0 0 0 0 68 strlen.o
24 0 0 0 0 76 strncpy.o
44 0 0 0 0 80 uidiv.o
98 0 0 0 0 92 uldiv.o
48 0 0 0 0 68 cdrcmple.o
334 0 0 0 0 148 dadd.o
222 0 0 0 0 100 ddiv.o
186 0 0 0 0 176 depilogue.o
48 0 0 0 0 68 dfixul.o
228 0 0 0 0 96 dmul.o
----------------------------------------------------------------------
3970 110 0 4 0 2108 Library Totals
4 0 0 0 0 0 (incl. Padding)
----------------------------------------------------------------------
Code (inc. data) RO Data RW Data ZI Data Debug Library Name
2900 110 0 4 0 1452 mc_w.l
1066 0 0 0 0 656 mf_w.l
----------------------------------------------------------------------
3970 110 0 4 0 2108 Library Totals
----------------------------------------------------------------------
==============================================================================
Code (inc. data) RO Data RW Data ZI Data Debug
94472 34560 3872 436 64508 700997 Grand Totals
94472 34560 3872 72 64508 700997 ELF Image Totals (compressed)
94472 34560 3872 72 0 0 ROM Totals
==============================================================================
Total RO Size (Code + RO Data) 98344 ( 96.04kB)
Total RW Size (RW Data + ZI Data) 64944 ( 63.42kB)
Total ROM Size (Code + RO Data + RW Data) 98416 ( 96.11kB)
==============================================================================
找到了内存用量的瓶颈
.bss 0x20003aa4 Section 47544 memp.o(.bss) // !!! 这里用的太多了
memp.o用了40KB内存.
ram_heap 0x20001290 Data 10259 mem.o(.bss)
memp_memory_NETCONN_base 0x20004b8c Data 8683 memp.o(.bss)
memp_memory_PBUF_POOL_base 0x200074c8 Data 32643 memp.o(.bss) // 可以进一步看到 memp_memory_PBUF_POOL_base 这个变量(数组)用了30KB
3148 1270 0 16 10259 10698 mem.o
820 426 636 72 47544 11984 memp.o
看map文件, 用掉了内存的地方都在lwip中。
memp_memory_PBUF_POOL_base 用了30KB
ram_heap 用了10KB
memp_memory_NETCONN_base 用了8KB
看代码,看看这3个变量怎么定义的,要用这些RAM空间.
// ram_heap
#define SIZEOF_STRUCT_MEM LWIP_MEM_ALIGN_SIZE(sizeof(struct mem))
#define MEM_SIZE_ALIGNED LWIP_MEM_ALIGN_SIZE(MEM_SIZE)
#define MEM_SIZE 10240
LWIP_DECLARE_MEMORY_ALIGNED(ram_heap, MEM_SIZE_ALIGNED + (2U * SIZEOF_STRUCT_MEM));
// memp_memory_NETCONN_base
#define MEMP_NUM_NETCONN 10
LWIP_MEMPOOL(NETCONN, MEMP_NUM_NETCONN, sizeof(struct netconn), "NETCONN")
#define LWIP_MEMPOOL(name,num,size,desc) LWIP_MEMPOOL_DECLARE(name,num,size,desc)
#define LWIP_MEMPOOL_DECLARE(name,num,size,desc) \
LWIP_DECLARE_MEMORY_ALIGNED(memp_memory_ ## name ## _base, ((num) * (MEMP_SIZE + MEMP_ALIGN_SIZE(size)))); \
\
LWIP_MEMPOOL_DECLARE_STATS_INSTANCE(memp_stats_ ## name) \
\
static struct memp *memp_tab_ ## name; \
\
const struct memp_desc memp_ ## name = { \
DECLARE_LWIP_MEMPOOL_DESC(desc) \
LWIP_MEMPOOL_DECLARE_STATS_REFERENCE(memp_stats_ ## name) \
LWIP_MEM_ALIGN_SIZE(size), \
(num), \
memp_memory_ ## name ## _base, \
&memp_tab_ ## name \
};
// memp_memory_PBUF_POOL_base
LWIP_PBUF_MEMPOOL(PBUF_POOL, PBUF_POOL_SIZE, PBUF_POOL_BUFSIZE, "PBUF_POOL")
/* PBUF_POOL_SIZE: the number of buffers in the pbuf pool. */
#define PBUF_POOL_SIZE 120
/* PBUF_POOL_BUFSIZE: the size of each pbuf in the pbuf pool. */
#define PBUF_POOL_BUFSIZE 256
可以看出,这些都是lwip用宏来控制的性能参数。
#define MEM_SIZE 10240
#define MEMP_NUM_NETCONN 10
#define PBUF_POOL_SIZE 120
#define PBUF_POOL_BUFSIZE 256
不怎么敢改,如果将这些宏改小,lwip的性能就降低了(能连接的连接数量)
但是如果资源不够,能采取的方法只有2个:
换资源多的兼容管脚的MCU, e.g. STM32F103VE => STM32F103VG, 如果是在产品上是可以的,也就是再焊上一块新型号,完全兼容的MCU. 已经试过了,换成STM32F103VG, 可以编译过。
改lwip配置参数,将需要的内存用量降下来,不耽误作试验即可。
**如果是在家作试验,啥也没有,如果只有一块开发板,那就只能改小LWIP配置参数。
未修改前:
.\Objects\my_stm32f1_SPL_template.axf: Error: L6407E: Sections of aggregate size 0x3ecc bytes could not fit into .ANY selector(s).
大约有16KB装不下。
// 将 MEMP_NUM_NETCONN 从10给成5
#define MEMP_NUM_NETCONN 5
.\Objects\my_stm32f1_SPL_template.axf: Error: L6407E: Sections of aggregate size 0xfa8 bytes could not fit into .ANY selector(s).
大约还有4KB装不下,内存用量已经降下来了。
// PBUF_POOL_SIZE 从120 改成96(8 * 12)
#define PBUF_POOL_SIZE 96
有如下编译报错
#if !MEMP_MEM_MALLOC && PBUF_POOL_SIZE && (TCP_WND > (PBUF_POOL_SIZE * (PBUF_POOL_BUFSIZE - (PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN + PBUF_IP_HLEN + PBUF_TRANSPORT_HLEN))))
#error "lwip_sanity_check: WARNING: TCP_WND is larger than space provided by PBUF_POOL_SIZE * (PBUF_POOL_BUFSIZE - protocol headers). If you know what you are doing, define LWIP_DISABLE_TCP_SANITY_CHECKS to 1 to disable this error."
#endif
/* TCP receive window. */
#define TCP_WND (20 * 1024)
尝试改小TCP_WND
#define TCP_WND (18 * 1024)
这回编译过了。
因为移植完ucos, 程序逻辑还要写空间,尝试改小MEM_SIZE
看到opt.h 中MEM_SIZE的默认定义为1600.
将MEM_SIZE减掉4KB
#define MEM_SIZE (6 * 1024)
现在全部编译完,看看内存占用和剩余情况
Total RW Size (RW Data + ZI Data) 58912 ( 57.53kB)
现在还剩6.47KB(64KB - 57.53KB)给主程序写逻辑用。
现在看看在全局符号中的内存变量占用的RAM情况
下表中的第4列是size(我们只看200B以上的变量), 第3列是数据的类型(我们只看下内存类型的变量)
Global Symbols
Symbol Name Value Ov Type Size Object(Section)
OSUnMapTbl 0x08017920 Data 256 os_core.o(.constdata)
ram_heap 0x20001298 Data 6163 mem.o(.bss)
memp_memory_TCP_PCB_base 0x20002bd8 Data 783 memp.o(.bss)
memp_memory_TCP_PCB_LISTEN_base 0x20002ef8 Data 259 memp.o(.bss)
memp_memory_TCP_SEG_base 0x2000300c Data 323 memp.o(.bss)
memp_memory_ALTCP_PCB_base 0x20003160 Data 223 memp.o(.bss)
memp_memory_REASSDATA_base 0x20003250 Data 1923 memp.o(.bss)
memp_memory_FRAG_PBUF_base 0x200039e4 Data 363 memp.o(.bss)
memp_memory_NETCONN_base 0x20003b94 Data 4343 memp.o(.bss)
memp_memory_TCPIP_MSG_API_base 0x20004c9c Data 259 memp.o(.bss)
memp_memory_TCPIP_MSG_INPKT_base 0x20004db0 Data 259 memp.o(.bss)
memp_memory_ARP_QUEUE_base 0x20004ec4 Data 243 memp.o(.bss)
memp_memory_SYS_TIMEOUT_base 0x2000505c Data 275 memp.o(.bss)
memp_memory_NETDB_base 0x20005180 Data 311 memp.o(.bss)
memp_memory_PBUF_base 0x200052c8 Data 259 memp.o(.bss)
memp_memory_PBUF_POOL_base 0x200053dc Data 26115 memp.o(.bss)
lwip_stats 0x2000ba7c Data 496 stats.o(.bss)
OSEventTbl 0x2000bf40 Data 4200 os_core.o(.bss)
OSTaskStatStk 0x2000cff8 Data 512 os_core.o(.bss)
OSTaskIdleStk 0x2000d1f8 Data 512 os_core.o(.bss)
OSTCBPrioTbl 0x2000d3f8 Data 256 os_core.o(.bss)
OSTCBTbl 0x2000d4f8 Data 1936 os_core.o(.bss)
OSTmrTbl 0x2000dd60 Data 640 os_core.o(.bss)
OSTmrTaskStk 0x2000dfe0 Data 512 os_core.o(.bss)
可以看到最大头的内存用量已经下来了。
ram_heap => 6163B
memp_memory_NETCONN_base => 4343B
memp_memory_PBUF_POOL_base => 26115B
如果是正式程序,还是不能改这些参数,否则性能会下降,e.g. PBUF_POOL的size变小了,那么通讯时,超出处理能力后,要不会卡住,要不会丢包。
**如果家里还有其他开发板(e.g. 有高配的MCU), 可以换块开发板作试验。但是这试验是lwip试验,和网卡相关的,如果换了板子,网卡不同了,那网卡驱动也要重新适配lwip. 挺费时间的。我还是用改参数的方法。