common.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. /**************************************************************************************************
  2. Phyplus Microelectronics Limited confidential and proprietary.
  3. All rights reserved.
  4. IMPORTANT: All rights of this software belong to Phyplus Microelectronics
  5. Limited ("Phyplus"). Your use of this Software is limited to those
  6. specific rights granted under the terms of the business contract, the
  7. confidential agreement, the non-disclosure agreement and any other forms
  8. of agreements as a customer or a partner of Phyplus. You may not use this
  9. Software unless you agree to abide by the terms of these agreements.
  10. You acknowledge that the Software may not be modified, copied,
  11. distributed or disclosed unless embedded on a Phyplus Bluetooth Low Energy
  12. (BLE) integrated circuit, either as a product or is integrated into your
  13. products. Other than for the aforementioned purposes, you may not use,
  14. reproduce, copy, prepare derivative works of, modify, distribute, perform,
  15. display or sell this Software and/or its documentation for any purposes.
  16. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  17. PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  18. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  19. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  20. PHYPLUS OR ITS SUBSIDIARIES BE LIABLE OR OBLIGATED UNDER CONTRACT,
  21. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  22. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  23. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  24. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  25. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  26. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  27. **************************************************************************************************/
  28. /*******************************************************************************
  29. * @file common.c
  30. * @brief Contains all functions support for common function driver,such as string function,you can use this driver for spi,adc,uart and so on
  31. * @version 0.0
  32. * @date 18. Oct. 2017
  33. * @author qing.han
  34. *
  35. * Copyright(C) 2016, PhyPlus Semiconductor
  36. * All rights reserved.
  37. *
  38. *******************************************************************************/
  39. #include "types.h"
  40. #include "common.h"
  41. #include "ap_cp.h"
  42. #include "OSAL_Memory.h"
  43. #include "hal_mcu.h"
  44. //#include "uart.h"
  45. extern uint32_t hclk,pclk;
  46. extern uint8 g_largeHeap[];
  47. /**************************************************************************************
  48. * @fn hal_setMem
  49. *
  50. * @brief This function process for set some memory addr with a value
  51. *
  52. * input parameters
  53. *
  54. * @param uint8_t *buf: set memory buffer
  55. * uint8_t value: memory value
  56. * uint32_t length: set memory length
  57. *
  58. * output parameters
  59. *
  60. * @param uint8_t *buf: set memory buffer
  61. *
  62. * @return None.
  63. **************************************************************************************/
  64. void hal_setMem(uint8_t *buf, uint8_t value, uint32_t length)
  65. {
  66. while ( length-- )
  67. {
  68. *buf++ = value;
  69. }
  70. }
  71. /**************************************************************************************
  72. * @fn hal_cpyMem
  73. *
  74. * @brief This function process for copying data from source addr to dest addr,once copy one byte
  75. *
  76. * input parameters
  77. *
  78. * @param uint8_t *dst: copy destnation buffer
  79. * uint8_t *src: copy source buffer
  80. * uint32_t length: copy length
  81. *
  82. * output parameters
  83. *
  84. * @param uint8_t *dst: copy destnation buffer
  85. *
  86. * @return None.
  87. **************************************************************************************/
  88. void hal_cpyMem(uint8_t *dst, uint8_t *src, uint32_t length)
  89. {
  90. uint8_t *p_dst= (uint8_t *) dst;
  91. uint8_t *p_src= (uint8_t *) src;
  92. while ( length-- )
  93. {
  94. *p_dst++ = *p_src++;
  95. }
  96. }
  97. /**************************************************************************************
  98. * @fn hal_cpyMem32
  99. *
  100. * @brief This function process for copying data from source addr to dest addr,once copy 4 bytes
  101. *
  102. * input parameters
  103. *
  104. * @param uint32_t *dst: copy destnation buffer
  105. * uint32_t *src: copy source buffer
  106. * uint32_t length: copy length
  107. *
  108. * output parameters
  109. *
  110. * @param uint32_t *dst: copy destnation buffer
  111. *
  112. * @return None.
  113. **************************************************************************************/
  114. void hal_cpyMem32(uint32_t *dst, uint32_t *src, uint32_t length)
  115. {
  116. uint32_t *p1=(uint32_t *)dst;
  117. uint32_t *p2=(uint32_t *)src;
  118. while ( length )
  119. {
  120. *p1++ = *p2++;
  121. if(length>=4)
  122. length-=4;
  123. else
  124. length=0;
  125. }
  126. }
  127. /**************************************************************************************
  128. * @fn hal_my_strcmp
  129. *
  130. * @brief This function process for compare two strings, return 1 means same, 0 means different
  131. *
  132. * input parameters
  133. *
  134. * @param const uint8_t *str: the first string
  135. * const uint8_t *ptr: the second string
  136. *
  137. * output parameters
  138. *
  139. * @param None.
  140. *
  141. * @return 1:the same
  142. * 0:the different
  143. **************************************************************************************/
  144. int hal_my_strcmp( const uint8_t *str, const uint8_t *ptr)
  145. {
  146. while ( *ptr!='\0')
  147. {
  148. if(*str!= *ptr) return 0;
  149. ++str;
  150. ++ptr;
  151. }
  152. if(*str=='\0')
  153. return 1;
  154. else
  155. return 0;
  156. }
  157. uint8 char_array_cmp(uint8 *s1, uint8 *s2, uint8 len)
  158. {
  159. uint8 i;
  160. for(i = 0; i < len; i ++)
  161. if(s1[i] > s2[i]) return 1;
  162. else if(s1[i] < s2[i]) return 2;
  163. return 0;//equal
  164. }
  165. /**************************************************************************************
  166. * @fn hal_copy_bin_from_flash_to_sram
  167. *
  168. * @brief This function process for copy bin from flash to sram
  169. *
  170. * input parameters
  171. *
  172. * @param int toAddr: destnation address
  173. * int fromAddr: source address
  174. * int length: copy length
  175. *
  176. * output parameters
  177. *
  178. * @param (uint8_t *) toAddr: destnation buffer
  179. *
  180. * @return None.
  181. **************************************************************************************/
  182. void hal_copy_bin_from_flash_to_sram( int toAddr, int fromAddr, int length)
  183. {
  184. hal_cpyMem((uint8_t *) toAddr, (uint8_t *) fromAddr, length);
  185. }
  186. /**************************************************************************************
  187. * @fn hal_my_sizeof
  188. *
  189. * @brief This function process for calculate the string length
  190. *
  191. * input parameters
  192. *
  193. * @param const uint8_t *str: the source string
  194. *
  195. * output parameters
  196. *
  197. * @param None.
  198. *
  199. * @return the string length(int)
  200. **************************************************************************************/
  201. int hal_my_sizeof(const uint8_t *str)
  202. {
  203. int size=0;
  204. while(*str != '\0')
  205. {
  206. size++;
  207. str++;
  208. }
  209. return size;
  210. }
  211. /**************************************************************************************
  212. * @fn hal_my_strlen
  213. *
  214. * @brief This function process for calculate the string length,PS:the char[] must give the '\0'
  215. *
  216. * input parameters
  217. *
  218. * @param const uint8_t *str: the source string
  219. *
  220. * output parameters
  221. *
  222. * @param None.
  223. *
  224. * @return the string length(int)
  225. **************************************************************************************/
  226. int hal_my_strlen(const uint8_t *str)
  227. {
  228. int len = 0;
  229. while(str[len] != '\0') ++len;
  230. return len;
  231. }
  232. /**************************************************************************************
  233. * @fn hal_is_an_valid_number
  234. *
  235. * @brief This function process for judge if a char is hex number or not, return 1 means yes, 0 means no
  236. *
  237. * input parameters
  238. *
  239. * @param uint8_t ch: the source data
  240. *
  241. * output parameters
  242. *
  243. * @param None.
  244. *
  245. * @return 1: yes
  246. * 0: no
  247. **************************************************************************************/
  248. int hal_is_an_valid_number(uint8_t ch)
  249. {
  250. if((ch>='0' && ch<='9') || (ch>='a' && ch<='f') || (ch>='A' && ch<='F'))
  251. return 1;
  252. else
  253. return 0;
  254. }
  255. /**************************************************************************************
  256. * @fn hal_convert_hex_to_char
  257. *
  258. * @brief This function process for convert a hex data to ASCII code type,include the 0x symbol
  259. *
  260. * input parameters
  261. *
  262. * @param unsigned char *ch: the char string buffer
  263. * unsigned int data: the source hex data
  264. *
  265. * output parameters
  266. *
  267. * @param unsigned char *ch: the char string buffer
  268. *
  269. * @return None.
  270. **************************************************************************************/
  271. void hal_convert_hex_to_char(unsigned char *ch, unsigned int data)
  272. {
  273. int i;
  274. ch[0]='0';
  275. ch[1]='x';
  276. for(i=7; i>=0; i--)
  277. {
  278. switch((data & (0xF<<i*4))>>i*4)
  279. {
  280. case 0:
  281. ch[2+7-i]='0';
  282. break;
  283. case 1:
  284. ch[2+7-i]='1';
  285. break;
  286. case 2:
  287. ch[2+7-i]='2';
  288. break;
  289. case 3:
  290. ch[2+7-i]='3';
  291. break;
  292. case 4:
  293. ch[2+7-i]='4';
  294. break;
  295. case 5:
  296. ch[2+7-i]='5';
  297. break;
  298. case 6:
  299. ch[2+7-i]='6';
  300. break;
  301. case 7:
  302. ch[2+7-i]='7';
  303. break;
  304. case 8:
  305. ch[2+7-i]='8';
  306. break;
  307. case 9:
  308. ch[2+7-i]='9';
  309. break;
  310. case 10:
  311. ch[2+7-i]='a';
  312. break;
  313. case 11:
  314. ch[2+7-i]='b';
  315. break;
  316. case 12:
  317. ch[2+7-i]='c';
  318. break;
  319. case 13:
  320. ch[2+7-i]='d';
  321. break;
  322. case 14:
  323. ch[2+7-i]='e';
  324. break;
  325. case 15:
  326. ch[2+7-i]='f';
  327. break;
  328. default:
  329. break;
  330. }
  331. }
  332. ch[10]='\0';
  333. }
  334. /**************************************************************************************
  335. * @fn hal_convert_hex_to_char_wo_x
  336. *
  337. * @brief This function process for convert a hex data to ASCII code type,without the 0x symbol
  338. *
  339. * input parameters
  340. *
  341. * @param unsigned char *ch: the char string buffer
  342. * unsigned int data: the source hex data
  343. *
  344. * output parameters
  345. *
  346. * @param unsigned char *ch: the char string buffer
  347. *
  348. * @return None.
  349. **************************************************************************************/
  350. void hal_convert_hex_to_char_wo_x(unsigned char *ch, unsigned int data)
  351. {
  352. int i;
  353. for(i=7; i>=0; i--)
  354. {
  355. switch((data & (0xF<<i*4))>>i*4)
  356. {
  357. case 0:
  358. ch[0+7-i]='0';
  359. break;
  360. case 1:
  361. ch[0+7-i]='1';
  362. break;
  363. case 2:
  364. ch[0+7-i]='2';
  365. break;
  366. case 3:
  367. ch[0+7-i]='3';
  368. break;
  369. case 4:
  370. ch[0+7-i]='4';
  371. break;
  372. case 5:
  373. ch[0+7-i]='5';
  374. break;
  375. case 6:
  376. ch[0+7-i]='6';
  377. break;
  378. case 7:
  379. ch[0+7-i]='7';
  380. break;
  381. case 8:
  382. ch[0+7-i]='8';
  383. break;
  384. case 9:
  385. ch[0+7-i]='9';
  386. break;
  387. case 10:
  388. ch[0+7-i]='a';
  389. break;
  390. case 11:
  391. ch[0+7-i]='b';
  392. break;
  393. case 12:
  394. ch[0+7-i]='c';
  395. break;
  396. case 13:
  397. ch[0+7-i]='d';
  398. break;
  399. case 14:
  400. ch[0+7-i]='e';
  401. break;
  402. case 15:
  403. ch[0+7-i]='f';
  404. break;
  405. default:
  406. break;
  407. }
  408. }
  409. ch[8]='\0';
  410. }
  411. /**************************************************************************************
  412. * @fn hal_convert_char_to_hex
  413. *
  414. * @brief This function process for convert a data from ASCII code to hex type
  415. *
  416. * input parameters
  417. *
  418. * @param const unsigned char *ch: the source char string
  419. *
  420. * output parameters
  421. *
  422. * @param None.
  423. *
  424. * @return hex data
  425. **************************************************************************************/
  426. uint32_t hal_convert_char_to_hex(const unsigned char *ch)
  427. {
  428. uint32_t HexData=0;
  429. const uint8_t *p=ch;
  430. int i=0, size;
  431. size=hal_my_sizeof (ch);
  432. if(size>8) return 0;
  433. while(i<size && hal_is_an_valid_number(*p))
  434. {
  435. switch(*p++)
  436. {
  437. case 'f':
  438. case 'F':
  439. HexData += 15 << (size-i-1)*4;
  440. break;
  441. case 'e':
  442. case 'E':
  443. HexData += 14 << (size-i-1)*4;
  444. break;
  445. case 'd':
  446. case 'D':
  447. HexData += 13<< (size-i-1)*4;
  448. break;
  449. case 'c':
  450. case 'C':
  451. HexData += 12<< (size-i-1)*4;
  452. break;
  453. case 'b':
  454. case 'B':
  455. HexData += 11<< (size-i-1)*4;
  456. break;
  457. case 'a':
  458. case 'A':
  459. HexData += 10<< (size-i-1)*4;
  460. break;
  461. case '9':
  462. HexData += 9<< (size-i-1)*4;
  463. break;
  464. case '8':
  465. HexData += 8<< (size-i-1)*4;
  466. break;
  467. case '7':
  468. HexData += 7<< (size-i-1)*4;
  469. break;
  470. case '6':
  471. HexData += 6<< (size-i-1)*4;
  472. break;
  473. case '5':
  474. HexData += 5<< (size-i-1)*4;
  475. break;
  476. case '4':
  477. HexData += 4<< (size-i-1)*4;
  478. break;
  479. case '3':
  480. HexData += 3<< (size-i-1)*4;
  481. break;
  482. case '2':
  483. HexData += 2<< (size-i-1)*4;
  484. break;
  485. case '1':
  486. HexData += 1<< (size-i-1)*4;
  487. break;
  488. case '0':
  489. HexData += 0<< (size-i-1)*4;
  490. break;
  491. }
  492. i++;
  493. }
  494. return HexData;
  495. }
  496. /**************************************************************************************
  497. * @fn hal_convert_char_to_dec
  498. *
  499. * @brief This function process for convert a data from ASCII code to decimal type
  500. *
  501. * input parameters
  502. *
  503. * @param const unsigned char *ch: the source char string
  504. *
  505. * output parameters
  506. *
  507. * @param None.
  508. *
  509. * @return decimal data
  510. **************************************************************************************/
  511. uint32_t hal_convert_char_to_dec(const unsigned char *ch)
  512. {
  513. unsigned int num = 0;
  514. while(*ch != '\0'){
  515. if(*ch >= '0' && *ch <='9'){
  516. num = num*10+(*ch - '0');
  517. ch++;
  518. }
  519. else{
  520. num=0;
  521. break;
  522. }
  523. }
  524. return num;
  525. }
  526. /**************************************************************************************
  527. * @fn WaitMs
  528. *
  529. * @brief This function process for wait program msecond,use RTC
  530. *
  531. * input parameters
  532. *
  533. * @param uint32_t msecond: the msecond value
  534. *
  535. * output parameters
  536. *
  537. * @param None.
  538. *
  539. * @return None.
  540. **************************************************************************************/
  541. void WaitMs(uint32_t msecond){
  542. // uint8_t tick_per_ms = 32;
  543. // //CLAER_RTC_COUNT;
  544. // RUN_RTC;
  545. // uint32_t now_clock_tick = clock_time_rtc();
  546. // while((clock_time_rtc()-now_clock_tick) < (tick_per_ms * msecond)){
  547. // if(clock_time_rtc() == 0xffffff){
  548. // break;
  549. // }
  550. // }
  551. //
  552. WaitRTCCount((msecond<<15)/1000);
  553. }
  554. /**************************************************************************************
  555. * @fn subWriteReg
  556. *
  557. * @brief This function process for write register with sub bit
  558. *
  559. * input parameters
  560. *
  561. * @param uint32_t addr: register address
  562. * uint8_t hOff: high bit offset
  563. * uint8_t lOff: low bit offset
  564. * uint32_t value: write value
  565. *
  566. * output parameters
  567. *
  568. * @param None.
  569. *
  570. * @return None.
  571. **************************************************************************************/
  572. void subWriteReg(uint32_t addr,uint8_t hOff,uint8_t lOff,uint32_t value){
  573. uint32_t temp=read_reg(addr);
  574. uint32_t temp2=0xffffffff;
  575. uint32_t temp3=value << lOff;
  576. for(uint8_t i=lOff;i<=hOff;i++){
  577. temp2 &= ~BIT(i);
  578. }
  579. temp = temp & temp2;
  580. temp = temp |temp3;
  581. write_reg(addr,temp);
  582. }
  583. /**************************************************************************************
  584. * @fn hal_system_init
  585. *
  586. * @brief This function process for system initial,you can select diff source,such as RC_32M XTAL_16M and so on
  587. *
  588. * input parameters
  589. *
  590. * @param enum H_SYSCLK_SEL h_system_clk_sel: system clock select SYS_CLK_RC_32M rc32M
  591. * SYS_CLK_DLL_32M dll32M
  592. * SYS_CLK_XTAL_16M xtal16M
  593. * SYS_CLK_DLL_48M dll48M
  594. * SYS_CLK_DLL_64M dll64M
  595. * SYS_CLK_DLL_96M dll96M
  596. *
  597. * output parameters
  598. *
  599. * @param None.
  600. *
  601. * @return None.
  602. **************************************************************************************/
  603. void hal_system_init(uint8_t h_system_clk_sel){
  604. uint16_t delay=300;
  605. if(h_system_clk_sel == SYS_CLK_RC_32M){
  606. hclk = 32000000;
  607. }else if(h_system_clk_sel == SYS_CLK_XTAL_16M){
  608. // ENABLE_XTAL_TRIGGER;
  609. ENABLE_XTAL_OUTPUT;
  610. hclk = 16000000;
  611. }else if(h_system_clk_sel == SYS_CLK_DLL_32M){
  612. // ENABLE_XTAL_TRIGGER;
  613. ENABLE_XTAL_OUTPUT;
  614. DBLE_CLOCK_DISABLE; //disable double 32M clock,we are now use 32M clock,should enable bit<13>
  615. DLL32M_CLOCK_ENABLE;
  616. ENABLE_DLL;
  617. hclk = 32000000;
  618. }else{
  619. // ENABLE_XTAL_TRIGGER;
  620. ENABLE_XTAL_OUTPUT;
  621. DLLn_CLOCK_ENABLE(h_system_clk_sel);
  622. ENABLE_DLL;
  623. if(h_system_clk_sel == SYS_CLK_DLL_48M){
  624. hclk = 48000000;
  625. }else if(h_system_clk_sel == SYS_CLK_DLL_64M){
  626. hclk = 64000000;
  627. }else{
  628. hclk = 96000000;
  629. }
  630. }
  631. while (delay > 0)
  632. delay --;
  633. subWriteReg(0x4000f03c,3,0,h_system_clk_sel);
  634. if(PCLK_DIV_ENABLE){
  635. uint32_t divider=(read_reg(0x40000018)>>4)&0xff;
  636. pclk = hclk/(divider+1);
  637. }else{
  638. pclk = hclk;
  639. }
  640. }
  641. uint32_t hal_read_current_time(void)
  642. {
  643. // return ((4000000-get_timer3_count())/4+2);
  644. //return (TIME_BASE - (get_timer3_count() >> 2) ) ;
  645. return(TIME_BASE - ((CP_TIM3->CurrentCount)>>2) );
  646. }
  647. /**************************************************************************************
  648. * @fn WaitUs
  649. *
  650. * @brief This function process for wait program usecond,use 4M crystal
  651. *
  652. * input parameters
  653. *
  654. * @param uint32_t usecond: the usecond value
  655. *
  656. * output parameters
  657. *
  658. * @param None.
  659. *
  660. * @return None.
  661. **************************************************************************************/
  662. void WaitUs(uint32_t wtTime){
  663. uint32_t T0,currTick,deltTick;
  664. T0 = hal_read_current_time();
  665. while(1){
  666. currTick = hal_read_current_time();
  667. deltTick =TIME_DELTA(currTick,T0);
  668. if(deltTick>wtTime)
  669. break;
  670. }
  671. }
  672. void WaitRTCCount(uint32_t rtcDelyCnt)
  673. {
  674. uint32 cnt0,cnt1;
  675. uint32 delt =0;
  676. cnt0 = clock_time_rtc();
  677. while(delt<rtcDelyCnt)
  678. {
  679. cnt1 = clock_time_rtc();
  680. delt = (cnt1>=cnt0) ? cnt1-cnt0 : (0x00ffffff-cnt0+cnt1);
  681. }
  682. }
  683. void hal_system_soft_reset(void)
  684. {
  685. *(volatile uint32_t *) 0x40000010 = 0x00;
  686. }
  687. /**************************************************************************************
  688. * @fn osal_memory_statics
  689. *
  690. * @brief This function process for osal memory analize
  691. *
  692. * input parameters
  693. *
  694. * @param None.
  695. *
  696. * output parameters
  697. *
  698. * @param None.
  699. *
  700. * @return Memmory free size.
  701. **************************************************************************************/
  702. uint32_t osal_memory_statics(void)
  703. {
  704. osalMemHdr_t *header, *current;
  705. void *ptr;
  706. uint32_t sum_alloc = 0;
  707. uint32_t sum_free = 0;
  708. ptr = (void *)g_largeHeap;
  709. header = (osalMemHdr_t *)ptr;
  710. current = (osalMemHdr_t *)ptr;
  711. HAL_ENTER_CRITICAL_SECTION();
  712. do
  713. {
  714. if ((uint32)ptr > (uint32)header + 4096)
  715. break;
  716. // seek to the last block, return
  717. if ( current->val == 0 ) /// val = 0, so len = 0
  718. break;
  719. if (current->hdr.inUse)
  720. sum_alloc += current->hdr.len;
  721. else
  722. sum_free += current->hdr.len;
  723. current = (osalMemHdr_t *)((uint8 *)current + current->hdr.len);
  724. } while (1);
  725. HAL_EXIT_CRITICAL_SECTION();
  726. return sum_free;
  727. }