|
很少见到关于heap5的使用,特地今天测试了一下,并且分享关于这方面的例子。 软件:CubeMX5.6,Keil5.27,串口助手 硬件:STM32L4R5,片外 双SRAM + |5 i; X, Z5 o) P& { STEP1: 环境搭建,各项配置 并不赘述如何配置芯片,自行搭建。" r# f" F) y$ S, _9 `/ j: S STEP2:修改CubemX代码 生成的FreeRTOS的MX_FREERTOS_Init中添加部分代码,如下 const HeapRegion_t xHeapRegions[] =# I* _: q. O! f- ?8 g { { ( uint8_t * ) 0x64000000UL, 1024*32 },6 z0 ~) ~0 x* E# I9 q! v { ( uint8_t * ) 0x68000000UL, 1024*32 }, { NULL, 0 } /* Terminates the array. */" R+ m( Z% ?# D9 Y! i };: i4 Z! {! K2 @5 q vPortDefineHeapRegions(xHeapRegions); + Q' p; Z, c" z( e" q ( e5 H$ Z p. l6 |5 i STEP3:测试malloc 1 F* J+ J; U7 O- j 本测试仅有一个任务, void StartDefaultTask(void const * argument): N' ^, u! |- m! `1 M9 \ { /* USER CODE BEGIN StartDefaultTask */ uint8_t * p; uint32_t size;$ ]+ Z( h- G9 h+ x( D uint32_t count = 1;. W8 \, u+ c0 X$ j char str[64]; /* Infinite loop */9 j2 R; z& P" U! h a for(;;)) Y' U, k, e! U, B. L. m {! ?6 I7 P! P2 h$ v4 \ if( HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin) == GPIO_PIN_SET )! x; O t1 g3 f! c {4 ]" J @+ I; U HAL_Delay(10);4 I; R0 ]; A7 A6 l# O$ g# g- j2 r if( HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin) == GPIO_PIN_SET ) { p = pvPortMalloc(1024*2); . L, i6 {5 W' N size = xPortGetFreeHeapSize(); 2 k0 N) [* F- v" s A: F sprintf(str, "malloc times %d\r\n", (uint32_t)count);8 D% u; M0 ]# g; S" t+ {3 ` HAL_UART_Transmit(&hlpuart1, (uint8_t *)str, strlen(str), 0xFF); memset(str, 0, strlen(str)); # v# i! S- m |) L( J* e sprintf(str, "mem add %x\r\n", (uint32_t)p); HAL_UART_Transmit(&hlpuart1, (uint8_t *)str, strlen(str), 0xFF); n+ T$ g0 H/ G5 g5 L: _; `3 V memset(str, 0, strlen(str)); 9 }2 L: W9 g% E% V: p sprintf(str, "Free size Bytes %d\r\n", size);5 @. |$ b# W; V. @+ L" J" @, g HAL_UART_Transmit(&hlpuart1, (uint8_t *)str, strlen(str), 0xFF);# A! M( T0 l6 `6 o# e memset(str, 0, strlen(str));5 z1 [# X/ h$ m7 w0 F& r& M7 ~ - ~4 ^& j8 l: p+ U, V" r sprintf(str, "Free size Bytes %d\r\n", size); HAL_UART_Transmit(&hlpuart1, (uint8_t *)str, strlen(str), 0xFF); memset(str, 0, strlen(str)); count++;' Q8 C# [, X2 z. Q0 s [" Y/ O; I; ]- M* x5 f- p; J- E } }$ a/ u% [6 z5 h! f: t4 e& y9 v osDelay(50); }# T/ C* x Q0 ` /* USER CODE END StartDefaultTask */. D% i2 z' |) z2 G! E! W& Y& G }. v2 i5 l% |+ L$ o) f & N0 u3 t/ W* `5 a. G' M! ~ p" w! [& c 测试结果: malloc times 1 mem add 64000270 Free size Bytes 628488 {4 K! ^6 u# A3 R/ K0 f0 N Free size Bytes 62848) }4 V% T1 g% O. \* } malloc times 24 Q; f3 T+ y* A; T9 }. C9 \ mem add 64000a785 |9 q+ c. g, v5 ^4 S/ \- d Free size Bytes 60792 Free size Bytes 60792 malloc times 3 mem add 64001280 Free size Bytes 58736) u& l1 H2 p' R! T5 Y* B4 ? Free size Bytes 58736- R& k: I* C6 W4 K2 Q malloc times 4; R/ T$ J* t% E- E mem add 64001a883 k! T8 t5 `2 Y1 m Free size Bytes 56680# b9 W* ~% {) n* g0 T Free size Bytes 56680 malloc times 5% Q* W) p3 G* O- P mem add 64002290/ V1 A* [+ ?0 n Free size Bytes 54624 Free size Bytes 54624 malloc times 6 mem add 64002a98 Free size Bytes 525682 g5 w# W1 R9 L+ D+ { Free size Bytes 525683 Z- u' C s4 K" | malloc times 7 mem add 640032a0/ J- i! u. F. @ Free size Bytes 505123 Q* r# n, D4 j+ Q, A Free size Bytes 50512 malloc times 8! _/ L& K% _( c" T6 z9 U# w mem add 64003aa8 Free size Bytes 48456 Free size Bytes 484569 J' o) e) v$ j# L malloc times 9 mem add 640042b0 Free size Bytes 46400( v9 p Q/ f2 { h Free size Bytes 46400; G; q' ]6 p( L4 x' a, s4 \6 }$ g1 H malloc times 10% B" m: ?* q8 c! w( s: s5 Q1 k mem add 64004ab8 Free size Bytes 44344, Q/ H& ]7 w3 f) r& Q2 k) \/ E Free size Bytes 44344 malloc times 11 mem add 640052c01 N" @ a! L7 S2 Q Free size Bytes 42288 Free size Bytes 42288 malloc times 12+ n; z5 V$ m3 S R) ^$ B! q+ v) i% K mem add 64005ac8, E* B! d b; Y, [6 J. z Free size Bytes 40232. Q/ \+ [$ L' C" R, K Free size Bytes 40232 malloc times 13 mem add 640062d04 I' l; @4 M% y3 F c+ p5 C* R Free size Bytes 38176( a; j; l4 x5 {7 h4 i% Y Free size Bytes 38176+ X. v1 P% j1 e malloc times 14 mem add 64006ad8 Free size Bytes 36120& ?4 P* [+ |/ V! b7 |$ j# t Free size Bytes 36120 malloc times 155 V* Q4 G9 \' h T; ~ mem add 640072e0 Free size Bytes 340649 P7 V: y, G! P' x; m c8 n9 c Free size Bytes 34064 malloc times 16 mem add 68000008! C+ D! [: \0 ~; N( `: z# N Free size Bytes 32008 Free size Bytes 320088 X9 W, t% b) R% k+ T' y3 E- K malloc times 17 mem add 68000810 Free size Bytes 29952$ h7 X( @! ~2 g' Z* V Free size Bytes 29952$ H- a7 Y; N) W4 F; j1 ? malloc times 18 mem add 680010183 Q* ~2 c Z: i4 f$ B Free size Bytes 27896 Free size Bytes 27896 malloc times 19, E: w2 Q" ]7 W. ^' u8 |- |, y% A mem add 680018202 O, j, m% ~4 Z2 n; t# W" M; h, j) ` Free size Bytes 25840 Free size Bytes 25840 malloc times 208 t. c( u5 f) Z mem add 680020287 R& `2 s+ E6 }) z* l Free size Bytes 23784 Free size Bytes 23784/ e4 R* p1 S& T" y. F: f malloc times 21 mem add 68002830 Free size Bytes 21728 Free size Bytes 21728# ~+ G( i: L W( S malloc times 22 mem add 68003038 Free size Bytes 19672 Free size Bytes 19672 malloc times 23$ u1 Q' V$ c8 ?% w5 v- g- o x( t3 a' n! x mem add 680038409 @: \: q* s" _/ s Free size Bytes 17616" H2 S6 |7 h( |1 i5 q( `* e Free size Bytes 17616% Z3 n' q/ g/ b; K malloc times 24 mem add 68004048 Free size Bytes 15560) x! E; S! j) {4 v+ p! E: h Free size Bytes 15560 malloc times 256 G( e' k) D B; @8 r mem add 68004850 Free size Bytes 13504* T- V9 t2 Q5 Q3 |, C Free size Bytes 13504 malloc times 26 mem add 68005058 Free size Bytes 114489 e" S: s7 T- N5 B Free size Bytes 11448 malloc times 27 mem add 68005860 Free size Bytes 9392 Free size Bytes 93920 R4 c. x8 ~- t6 w, U" n+ ~; Q malloc times 28 L& D3 J0 d3 x y( M# J* | mem add 680060688 e. a- Q( o; Z. x9 x Free size Bytes 7336- j7 o& I' P, g7 E3 } Free size Bytes 7336 malloc times 29 mem add 68006870+ V: }# f: `' B" l- Z% N7 [ Free size Bytes 5280 Free size Bytes 52805 ]0 f3 P5 K- N! }2 J G4 f) S malloc times 30$ t- r' N; I3 {+ {: K mem add 68007078 Free size Bytes 3224 Free size Bytes 3224% t9 e$ |8 O0 V0 O7 r% ` malloc times 31 mem add 0 Free size Bytes 3224 Free size Bytes 32249 _" q; F, X e: |& n 7 |3 s% B. z: e& w5 ^1 v 32KB x 2 SRAM,总共64KB,实际可以使用小于64KB,并且每次malloc大小为2KB,实际1024*2+8 = 2056Byets。- \5 J0 x! a3 J; T1 m+ {& a! ^ 最后明显大于2056Bytes却还是不能malloc的原因: 第一次malloc地址0x64000270,也就是说624Bytes跳过了, 那么3324-624=2700Bytes, 4 \* W2 v* p& V1 J 第一片SRAM的最后一次malloc 地址0x640072e0,第一片剩余3360-2056=518Bytes,总共2700-518=2182Bytes,; N/ ~5 _3 ?5 R3 ?% A& ?6 W6 } 第二片SRAM的最后一次有效malloc 地址0x68007078,第二片最后剩余922Bytes,总共2182-922<2056Bytes, 简单来说,第一片前部分内存,后部分内存,第二片后部分内存都被切开剩余了,虽然剩余3224Bytes,很明显能malloc一次2056Bytes的空间,但是这种碎片应该可以被更小malloc大小用掉。SDRAM使用heap5应该注意这点,而且对于不连续的内存的使用会比较复杂,0x64000000到0x68000000,中间不连续,程序需要考虑,操作内存需要跳到相应地址。我觉得一个if else 就够了。+ M S1 L/ E8 ` |
微信公众号
手机版