kservice.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2006-03-16 Bernard the first version
  9. * 2006-05-25 Bernard rewrite vsprintf
  10. * 2006-08-10 Bernard add rt_show_version
  11. * 2010-03-17 Bernard remove rt_strlcpy function
  12. * fix gcc compiling issue.
  13. * 2010-04-15 Bernard remove weak definition on ICCM16C compiler
  14. * 2012-07-18 Arda add the alignment display for signed integer
  15. * 2012-11-23 Bernard fix IAR compiler error.
  16. * 2012-12-22 Bernard fix rt_kprintf issue, which found by Grissiom.
  17. * 2013-06-24 Bernard remove rt_kprintf if RT_USING_CONSOLE is not defined.
  18. * 2013-09-24 aozima make sure the device is in STREAM mode when used by rt_kprintf.
  19. * 2015-07-06 Bernard Add rt_assert_handler routine.
  20. * 2021-02-28 Meco Man add RT_KSERVICE_USING_STDLIB
  21. * 2021-12-20 Meco Man implement rt_strcpy()
  22. * 2022-01-07 Gabriel add __on_rt_assert_hook
  23. * 2022-06-04 Meco Man remove strnlen
  24. * 2022-08-24 Yunjie make rt_memset word-independent to adapt to ti c28x (16bit word)
  25. * 2022-08-30 Yunjie make rt_vsnprintf adapt to ti c28x (16bit int)
  26. * 2023-02-02 Bernard add Smart ID for logo version show
  27. */
  28. #include <rtthread.h>
  29. #include <rthw.h>
  30. #define DBG_TAG "kernel.device"
  31. #ifdef RT_DEBUG_DEVICE
  32. #define DBG_LVL DBG_LOG
  33. #else
  34. #define DBG_LVL DBG_WARNING
  35. #endif /* defined (RT_DEBUG_DEVICE) */
  36. #include <rtdbg.h>
  37. #ifdef RT_USING_MODULE
  38. #include <dlmodule.h>
  39. #endif /* RT_USING_MODULE */
  40. #ifdef RT_USING_SMART
  41. #include <lwp.h>
  42. #include <lwp_user_mm.h>
  43. #include <console.h>
  44. #endif
  45. /* use precision */
  46. #define RT_PRINTF_PRECISION
  47. #define RT_PRINTF_SPECIAL
  48. /**
  49. * @addtogroup KernelService
  50. * @{
  51. */
  52. /* global errno in RT-Thread */
  53. static volatile int __rt_errno;
  54. #if defined(RT_USING_DEVICE) && defined(RT_USING_CONSOLE)
  55. static rt_device_t _console_device = RT_NULL;
  56. #endif
  57. rt_weak void rt_hw_us_delay(rt_uint32_t us)
  58. {
  59. (void) us;
  60. LOG_W("rt_hw_us_delay() doesn't support for this board."
  61. "Please consider implementing rt_hw_us_delay() in another file.");
  62. }
  63. rt_weak void rt_hw_cpu_reset(void)
  64. {
  65. LOG_W("rt_hw_cpu_reset() doesn't support for this board."
  66. "Please consider implementing rt_hw_cpu_reset() in another file.");
  67. return;
  68. }
  69. rt_weak void rt_hw_cpu_shutdown(void)
  70. {
  71. rt_base_t level;
  72. LOG_I("CPU shutdown...");
  73. LOG_W("Using default rt_hw_cpu_shutdown()."
  74. "Please consider implementing rt_hw_cpu_reset() in another file.");
  75. level = rt_hw_interrupt_disable();
  76. while (level)
  77. {
  78. RT_ASSERT(RT_NULL);
  79. }
  80. return;
  81. }
  82. rt_weak const char *rt_hw_cpu_arch(void)
  83. {
  84. return "unknown";
  85. }
  86. static const char* rt_errno_strs[] =
  87. {
  88. "OK",
  89. "ERROR",
  90. "ETIMOUT",
  91. "ERSFULL",
  92. "ERSEPTY",
  93. "ENOMEM",
  94. "ENOSYS",
  95. "EBUSY",
  96. "EIO",
  97. "EINTRPT",
  98. "EINVAL",
  99. "EUNKNOW"
  100. };
  101. /**
  102. * @brief This function return a pointer to a string that contains the
  103. * message of error.
  104. *
  105. * @param error the errorno code
  106. * @return a point to error message string
  107. */
  108. const char *rt_strerror(rt_err_t error)
  109. {
  110. if (error < 0)
  111. error = -error;
  112. return (error > RT_EINVAL + 1) ?
  113. rt_errno_strs[RT_EINVAL + 1] :
  114. rt_errno_strs[error];
  115. }
  116. RTM_EXPORT(rt_strerror);
  117. /**
  118. * @brief This function gets the global errno for the current thread.
  119. *
  120. * @return errno
  121. */
  122. rt_err_t rt_get_errno(void)
  123. {
  124. rt_thread_t tid = RT_NULL;
  125. if (rt_interrupt_get_nest() != 0)
  126. {
  127. /* it's in interrupt context */
  128. return __rt_errno;
  129. }
  130. tid = rt_thread_self();
  131. if (tid == RT_NULL)
  132. {
  133. return __rt_errno;
  134. }
  135. return tid->error;
  136. }
  137. RTM_EXPORT(rt_get_errno);
  138. /**
  139. * @brief This function sets the global errno for the current thread.
  140. *
  141. * @param error is the errno shall be set.
  142. */
  143. void rt_set_errno(rt_err_t error)
  144. {
  145. rt_thread_t tid = RT_NULL;
  146. if (rt_interrupt_get_nest() != 0)
  147. {
  148. /* it's in interrupt context */
  149. __rt_errno = error;
  150. return;
  151. }
  152. tid = rt_thread_self();
  153. if (tid == RT_NULL)
  154. {
  155. __rt_errno = error;
  156. return;
  157. }
  158. tid->error = error;
  159. }
  160. RTM_EXPORT(rt_set_errno);
  161. /**
  162. * @brief This function returns the address of the current thread errno.
  163. *
  164. * @return The errno address.
  165. */
  166. int *_rt_errno(void)
  167. {
  168. rt_thread_t tid = RT_NULL;
  169. if (rt_interrupt_get_nest() != 0)
  170. {
  171. return (int *)&__rt_errno;
  172. }
  173. tid = rt_thread_self();
  174. if (tid != RT_NULL)
  175. {
  176. return (int *) & (tid->error);
  177. }
  178. return (int *)&__rt_errno;
  179. }
  180. RTM_EXPORT(_rt_errno);
  181. #ifndef RT_KSERVICE_USING_STDLIB_MEMORY
  182. /**
  183. * @brief This function will set the content of memory to specified value.
  184. *
  185. * @param s is the address of source memory, point to the memory block to be filled.
  186. *
  187. * @param c is the value to be set. The value is passed in int form, but the function
  188. * uses the unsigned character form of the value when filling the memory block.
  189. *
  190. * @param count number of bytes to be set.
  191. *
  192. * @return The address of source memory.
  193. */
  194. rt_weak void *rt_memset(void *s, int c, rt_ubase_t count)
  195. {
  196. #ifdef RT_KSERVICE_USING_TINY_SIZE
  197. char *xs = (char *)s;
  198. while (count--)
  199. *xs++ = c;
  200. return s;
  201. #else
  202. #define LBLOCKSIZE (sizeof(rt_ubase_t))
  203. #define UNALIGNED(X) ((long)X & (LBLOCKSIZE - 1))
  204. #define TOO_SMALL(LEN) ((LEN) < LBLOCKSIZE)
  205. unsigned int i = 0;
  206. char *m = (char *)s;
  207. unsigned long buffer = 0;
  208. unsigned long *aligned_addr = RT_NULL;
  209. unsigned char d = (unsigned int)c & (unsigned char)(-1); /* To avoid sign extension, copy C to an
  210. unsigned variable. (unsigned)((char)(-1))=0xFF for 8bit and =0xFFFF for 16bit: word independent */
  211. RT_ASSERT(LBLOCKSIZE == 2 || LBLOCKSIZE == 4 || LBLOCKSIZE == 8);
  212. if (!TOO_SMALL(count) && !UNALIGNED(s))
  213. {
  214. /* If we get this far, we know that count is large and s is word-aligned. */
  215. aligned_addr = (unsigned long *)s;
  216. /* Store d into each char sized location in buffer so that
  217. * we can set large blocks quickly.
  218. */
  219. for (i = 0; i < LBLOCKSIZE; i++)
  220. {
  221. *(((unsigned char *)&buffer)+i) = d;
  222. }
  223. while (count >= LBLOCKSIZE * 4)
  224. {
  225. *aligned_addr++ = buffer;
  226. *aligned_addr++ = buffer;
  227. *aligned_addr++ = buffer;
  228. *aligned_addr++ = buffer;
  229. count -= 4 * LBLOCKSIZE;
  230. }
  231. while (count >= LBLOCKSIZE)
  232. {
  233. *aligned_addr++ = buffer;
  234. count -= LBLOCKSIZE;
  235. }
  236. /* Pick up the remainder with a bytewise loop. */
  237. m = (char *)aligned_addr;
  238. }
  239. while (count--)
  240. {
  241. *m++ = (char)d;
  242. }
  243. return s;
  244. #undef LBLOCKSIZE
  245. #undef UNALIGNED
  246. #undef TOO_SMALL
  247. #endif /* RT_KSERVICE_USING_TINY_SIZE */
  248. }
  249. RTM_EXPORT(rt_memset);
  250. /**
  251. * @brief This function will copy memory content from source address to destination address.
  252. *
  253. * @param dst is the address of destination memory, points to the copied content.
  254. *
  255. * @param src is the address of source memory, pointing to the data source to be copied.
  256. *
  257. * @param count is the copied length.
  258. *
  259. * @return The address of destination memory
  260. */
  261. rt_weak void *rt_memcpy(void *dst, const void *src, rt_ubase_t count)
  262. {
  263. #ifdef RT_KSERVICE_USING_TINY_SIZE
  264. char *tmp = (char *)dst, *s = (char *)src;
  265. rt_ubase_t len = 0;
  266. if (tmp <= s || tmp > (s + count))
  267. {
  268. while (count--)
  269. *tmp ++ = *s ++;
  270. }
  271. else
  272. {
  273. for (len = count; len > 0; len --)
  274. tmp[len - 1] = s[len - 1];
  275. }
  276. return dst;
  277. #else
  278. #define UNALIGNED(X, Y) \
  279. (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))
  280. #define BIGBLOCKSIZE (sizeof (long) << 2)
  281. #define LITTLEBLOCKSIZE (sizeof (long))
  282. #define TOO_SMALL(LEN) ((LEN) < BIGBLOCKSIZE)
  283. char *dst_ptr = (char *)dst;
  284. char *src_ptr = (char *)src;
  285. long *aligned_dst = RT_NULL;
  286. long *aligned_src = RT_NULL;
  287. rt_ubase_t len = count;
  288. /* If the size is small, or either SRC or DST is unaligned,
  289. then punt into the byte copy loop. This should be rare. */
  290. if (!TOO_SMALL(len) && !UNALIGNED(src_ptr, dst_ptr))
  291. {
  292. aligned_dst = (long *)dst_ptr;
  293. aligned_src = (long *)src_ptr;
  294. /* Copy 4X long words at a time if possible. */
  295. while (len >= BIGBLOCKSIZE)
  296. {
  297. *aligned_dst++ = *aligned_src++;
  298. *aligned_dst++ = *aligned_src++;
  299. *aligned_dst++ = *aligned_src++;
  300. *aligned_dst++ = *aligned_src++;
  301. len -= BIGBLOCKSIZE;
  302. }
  303. /* Copy one long word at a time if possible. */
  304. while (len >= LITTLEBLOCKSIZE)
  305. {
  306. *aligned_dst++ = *aligned_src++;
  307. len -= LITTLEBLOCKSIZE;
  308. }
  309. /* Pick up any residual with a byte copier. */
  310. dst_ptr = (char *)aligned_dst;
  311. src_ptr = (char *)aligned_src;
  312. }
  313. while (len--)
  314. *dst_ptr++ = *src_ptr++;
  315. return dst;
  316. #undef UNALIGNED
  317. #undef BIGBLOCKSIZE
  318. #undef LITTLEBLOCKSIZE
  319. #undef TOO_SMALL
  320. #endif /* RT_KSERVICE_USING_TINY_SIZE */
  321. }
  322. RTM_EXPORT(rt_memcpy);
  323. /**
  324. * @brief This function will move memory content from source address to destination
  325. * address. If the destination memory does not overlap with the source memory,
  326. * the function is the same as memcpy().
  327. *
  328. * @param dest is the address of destination memory, points to the copied content.
  329. *
  330. * @param src is the address of source memory, point to the data source to be copied.
  331. *
  332. * @param n is the copied length.
  333. *
  334. * @return The address of destination memory.
  335. */
  336. void *rt_memmove(void *dest, const void *src, rt_size_t n)
  337. {
  338. char *tmp = (char *)dest, *s = (char *)src;
  339. if (s < tmp && tmp < s + n)
  340. {
  341. tmp += n;
  342. s += n;
  343. while (n--)
  344. *(--tmp) = *(--s);
  345. }
  346. else
  347. {
  348. while (n--)
  349. *tmp++ = *s++;
  350. }
  351. return dest;
  352. }
  353. RTM_EXPORT(rt_memmove);
  354. /**
  355. * @brief This function will compare two areas of memory.
  356. *
  357. * @param cs is a block of memory.
  358. *
  359. * @param ct is another block of memory.
  360. *
  361. * @param count is the size of the area.
  362. *
  363. * @return Compare the results:
  364. * If the result < 0, cs is smaller than ct.
  365. * If the result > 0, cs is greater than ct.
  366. * If the result = 0, cs is equal to ct.
  367. */
  368. rt_int32_t rt_memcmp(const void *cs, const void *ct, rt_size_t count)
  369. {
  370. const unsigned char *su1 = RT_NULL, *su2 = RT_NULL;
  371. int res = 0;
  372. for (su1 = (const unsigned char *)cs, su2 = (const unsigned char *)ct; 0 < count; ++su1, ++su2, count--)
  373. if ((res = *su1 - *su2) != 0)
  374. break;
  375. return res;
  376. }
  377. RTM_EXPORT(rt_memcmp);
  378. #endif /* RT_KSERVICE_USING_STDLIB_MEMORY*/
  379. #ifndef RT_KSERVICE_USING_STDLIB
  380. /**
  381. * @brief This function will return the first occurrence of a string, without the
  382. * terminator '\0'.
  383. *
  384. * @param s1 is the source string.
  385. *
  386. * @param s2 is the find string.
  387. *
  388. * @return The first occurrence of a s2 in s1, or RT_NULL if no found.
  389. */
  390. char *rt_strstr(const char *s1, const char *s2)
  391. {
  392. int l1 = 0, l2 = 0;
  393. l2 = rt_strlen(s2);
  394. if (!l2)
  395. {
  396. return (char *)s1;
  397. }
  398. l1 = rt_strlen(s1);
  399. while (l1 >= l2)
  400. {
  401. l1 --;
  402. if (!rt_memcmp(s1, s2, l2))
  403. {
  404. return (char *)s1;
  405. }
  406. s1 ++;
  407. }
  408. return RT_NULL;
  409. }
  410. RTM_EXPORT(rt_strstr);
  411. /**
  412. * @brief This function will compare two strings while ignoring differences in case
  413. *
  414. * @param a is the string to be compared.
  415. *
  416. * @param b is the string to be compared.
  417. *
  418. * @return Compare the results:
  419. * If the result < 0, a is smaller than a.
  420. * If the result > 0, a is greater than a.
  421. * If the result = 0, a is equal to a.
  422. */
  423. rt_int32_t rt_strcasecmp(const char *a, const char *b)
  424. {
  425. int ca = 0, cb = 0;
  426. do
  427. {
  428. ca = *a++ & 0xff;
  429. cb = *b++ & 0xff;
  430. if (ca >= 'A' && ca <= 'Z')
  431. ca += 'a' - 'A';
  432. if (cb >= 'A' && cb <= 'Z')
  433. cb += 'a' - 'A';
  434. }
  435. while (ca == cb && ca != '\0');
  436. return ca - cb;
  437. }
  438. RTM_EXPORT(rt_strcasecmp);
  439. /**
  440. * @brief This function will copy string no more than n bytes.
  441. *
  442. * @param dst points to the address used to store the copied content.
  443. *
  444. * @param src is the string to be copied.
  445. *
  446. * @param n is the maximum copied length.
  447. *
  448. * @return The address where the copied content is stored.
  449. */
  450. char *rt_strncpy(char *dst, const char *src, rt_size_t n)
  451. {
  452. if (n != 0)
  453. {
  454. char *d = dst;
  455. const char *s = src;
  456. do
  457. {
  458. if ((*d++ = *s++) == 0)
  459. {
  460. /* NUL pad the remaining n-1 bytes */
  461. while (--n != 0)
  462. {
  463. *d++ = 0;
  464. }
  465. break;
  466. }
  467. } while (--n != 0);
  468. }
  469. return (dst);
  470. }
  471. RTM_EXPORT(rt_strncpy);
  472. /**
  473. * @brief This function will copy string.
  474. *
  475. * @param dst points to the address used to store the copied content.
  476. *
  477. * @param src is the string to be copied.
  478. *
  479. * @return The address where the copied content is stored.
  480. */
  481. char *rt_strcpy(char *dst, const char *src)
  482. {
  483. char *dest = dst;
  484. while (*src != '\0')
  485. {
  486. *dst = *src;
  487. dst++;
  488. src++;
  489. }
  490. *dst = '\0';
  491. return dest;
  492. }
  493. RTM_EXPORT(rt_strcpy);
  494. /**
  495. * @brief This function will compare two strings with specified maximum length.
  496. *
  497. * @param cs is the string to be compared.
  498. *
  499. * @param ct is the string to be compared.
  500. *
  501. * @param count is the maximum compare length.
  502. *
  503. * @return Compare the results:
  504. * If the result < 0, cs is smaller than ct.
  505. * If the result > 0, cs is greater than ct.
  506. * If the result = 0, cs is equal to ct.
  507. */
  508. rt_int32_t rt_strncmp(const char *cs, const char *ct, rt_size_t count)
  509. {
  510. signed char __res = 0;
  511. while (count)
  512. {
  513. if ((__res = *cs - *ct++) != 0 || !*cs++)
  514. {
  515. break;
  516. }
  517. count --;
  518. }
  519. return __res;
  520. }
  521. RTM_EXPORT(rt_strncmp);
  522. /**
  523. * @brief This function will compare two strings without specified length.
  524. *
  525. * @param cs is the string to be compared.
  526. *
  527. * @param ct is the string to be compared.
  528. *
  529. * @return Compare the results:
  530. * If the result < 0, cs is smaller than ct.
  531. * If the result > 0, cs is greater than ct.
  532. * If the result = 0, cs is equal to ct.
  533. */
  534. rt_int32_t rt_strcmp(const char *cs, const char *ct)
  535. {
  536. while (*cs && *cs == *ct)
  537. {
  538. cs++;
  539. ct++;
  540. }
  541. return (*cs - *ct);
  542. }
  543. RTM_EXPORT(rt_strcmp);
  544. /**
  545. * @brief This function will return the length of a string, which terminate will
  546. * null character.
  547. *
  548. * @param s is the string
  549. *
  550. * @return The length of string.
  551. */
  552. rt_size_t rt_strlen(const char *s)
  553. {
  554. const char *sc = RT_NULL;
  555. for (sc = s; *sc != '\0'; ++sc) /* nothing */
  556. ;
  557. return sc - s;
  558. }
  559. RTM_EXPORT(rt_strlen);
  560. #endif /* RT_KSERVICE_USING_STDLIB */
  561. /**
  562. * @brief The strnlen() function returns the number of characters in the
  563. * string pointed to by s, excluding the terminating null byte ('\0'),
  564. * but at most maxlen. In doing this, strnlen() looks only at the
  565. * first maxlen characters in the string pointed to by s and never
  566. * beyond s+maxlen.
  567. *
  568. * @param s is the string.
  569. *
  570. * @param maxlen is the max size.
  571. *
  572. * @return The length of string.
  573. */
  574. rt_size_t rt_strnlen(const char *s, rt_ubase_t maxlen)
  575. {
  576. const char *sc;
  577. for (sc = s; *sc != '\0' && (rt_ubase_t)(sc - s) < maxlen; ++sc) /* nothing */
  578. ;
  579. return sc - s;
  580. }
  581. RTM_EXPORT(rt_strnlen);
  582. #ifdef RT_USING_HEAP
  583. /**
  584. * @brief This function will duplicate a string.
  585. *
  586. * @param s is the string to be duplicated.
  587. *
  588. * @return The string address of the copy.
  589. */
  590. char *rt_strdup(const char *s)
  591. {
  592. rt_size_t len = rt_strlen(s) + 1;
  593. char *tmp = (char *)rt_malloc(len);
  594. if (!tmp)
  595. {
  596. return RT_NULL;
  597. }
  598. rt_memcpy(tmp, s, len);
  599. return tmp;
  600. }
  601. RTM_EXPORT(rt_strdup);
  602. #endif /* RT_USING_HEAP */
  603. /**
  604. * @brief This function will show the version of rt-thread rtos
  605. */
  606. void rt_show_version(void)
  607. {
  608. rt_kprintf("\n \\ | /\n");
  609. #ifdef RT_USING_SMART
  610. rt_kprintf("- RT - Thread Smart Operating System\n");
  611. #else
  612. rt_kprintf("- RT - Thread Operating System\n");
  613. #endif
  614. rt_kprintf(" / | \\ %d.%d.%d build %s %s\n",
  615. (rt_int32_t)RT_VERSION_MAJOR, (rt_int32_t)RT_VERSION_MINOR, (rt_int32_t)RT_VERSION_PATCH, __DATE__, __TIME__);
  616. rt_kprintf(" 2006 - 2022 Copyright by RT-Thread team\n");
  617. }
  618. RTM_EXPORT(rt_show_version);
  619. /* private function */
  620. #define _ISDIGIT(c) ((unsigned)((c) - '0') < 10)
  621. /**
  622. * @brief This function will duplicate a string.
  623. *
  624. * @param n is the string to be duplicated.
  625. *
  626. * @param base is support divide instructions value.
  627. *
  628. * @return the duplicated string pointer.
  629. */
  630. #ifdef RT_KPRINTF_USING_LONGLONG
  631. rt_inline int divide(unsigned long long *n, int base)
  632. #else
  633. rt_inline int divide(unsigned long *n, int base)
  634. #endif /* RT_KPRINTF_USING_LONGLONG */
  635. {
  636. int res;
  637. /* optimized for processor which does not support divide instructions. */
  638. #ifdef RT_KPRINTF_USING_LONGLONG
  639. res = (int)((*n) % base);
  640. *n = (long long)((*n) / base);
  641. #else
  642. res = (int)((*n) % base);
  643. *n = (long)((*n) / base);
  644. #endif
  645. return res;
  646. }
  647. rt_inline int skip_atoi(const char **s)
  648. {
  649. int i = 0;
  650. while (_ISDIGIT(**s))
  651. i = i * 10 + *((*s)++) - '0';
  652. return i;
  653. }
  654. #define ZEROPAD (1 << 0) /* pad with zero */
  655. #define SIGN (1 << 1) /* unsigned/signed long */
  656. #define PLUS (1 << 2) /* show plus */
  657. #define SPACE (1 << 3) /* space if plus */
  658. #define LEFT (1 << 4) /* left justified */
  659. #define SPECIAL (1 << 5) /* 0x */
  660. #define LARGE (1 << 6) /* use 'ABCDEF' instead of 'abcdef' */
  661. static char *print_number(char *buf,
  662. char *end,
  663. #ifdef RT_KPRINTF_USING_LONGLONG
  664. unsigned long long num,
  665. #else
  666. unsigned long num,
  667. #endif /* RT_KPRINTF_USING_LONGLONG */
  668. int base,
  669. int qualifier,
  670. int s,
  671. #ifdef RT_PRINTF_PRECISION
  672. int precision,
  673. #endif /* RT_PRINTF_PRECISION */
  674. int type)
  675. {
  676. char c = 0, sign = 0;
  677. #ifdef RT_KPRINTF_USING_LONGLONG
  678. char tmp[64] = {0};
  679. #else
  680. char tmp[32] = {0};
  681. #endif /* RT_KPRINTF_USING_LONGLONG */
  682. int precision_bak = precision;
  683. const char *digits = RT_NULL;
  684. static const char small_digits[] = "0123456789abcdef";
  685. static const char large_digits[] = "0123456789ABCDEF";
  686. int i = 0;
  687. int size = 0;
  688. size = s;
  689. digits = (type & LARGE) ? large_digits : small_digits;
  690. if (type & LEFT)
  691. {
  692. type &= ~ZEROPAD;
  693. }
  694. c = (type & ZEROPAD) ? '0' : ' ';
  695. /* get sign */
  696. sign = 0;
  697. if (type & SIGN)
  698. {
  699. switch (qualifier)
  700. {
  701. case 'h':
  702. if ((rt_int16_t)num < 0)
  703. {
  704. sign = '-';
  705. num = (rt_uint16_t)-num;
  706. }
  707. break;
  708. case 'L':
  709. case 'l':
  710. if ((long)num < 0)
  711. {
  712. sign = '-';
  713. num = (unsigned long)-num;
  714. }
  715. break;
  716. case 0:
  717. default:
  718. if ((rt_int32_t)num < 0)
  719. {
  720. sign = '-';
  721. num = (rt_uint32_t)-num;
  722. }
  723. break;
  724. }
  725. if (sign != '-')
  726. {
  727. if (type & PLUS)
  728. {
  729. sign = '+';
  730. }
  731. else if (type & SPACE)
  732. {
  733. sign = ' ';
  734. }
  735. }
  736. }
  737. #ifdef RT_PRINTF_SPECIAL
  738. if (type & SPECIAL)
  739. {
  740. if (base == 2 || base == 16)
  741. {
  742. size -= 2;
  743. }
  744. else if (base == 8)
  745. {
  746. size--;
  747. }
  748. }
  749. #endif /* RT_PRINTF_SPECIAL */
  750. i = 0;
  751. if (num == 0)
  752. {
  753. tmp[i++] = '0';
  754. }
  755. else
  756. {
  757. while (num != 0)
  758. tmp[i++] = digits[divide(&num, base)];
  759. }
  760. #ifdef RT_PRINTF_PRECISION
  761. if (i > precision)
  762. {
  763. precision = i;
  764. }
  765. size -= precision;
  766. #else
  767. size -= i;
  768. #endif /* RT_PRINTF_PRECISION */
  769. if (!(type & (ZEROPAD | LEFT)))
  770. {
  771. if ((sign) && (size > 0))
  772. {
  773. size--;
  774. }
  775. while (size-- > 0)
  776. {
  777. if (buf < end)
  778. {
  779. *buf = ' ';
  780. }
  781. ++ buf;
  782. }
  783. }
  784. if (sign)
  785. {
  786. if (buf < end)
  787. {
  788. *buf = sign;
  789. }
  790. -- size;
  791. ++ buf;
  792. }
  793. #ifdef RT_PRINTF_SPECIAL
  794. if (type & SPECIAL)
  795. {
  796. if (base == 2)
  797. {
  798. if (buf < end)
  799. *buf = '0';
  800. ++ buf;
  801. if (buf < end)
  802. *buf = 'b';
  803. ++ buf;
  804. }
  805. else if (base == 8)
  806. {
  807. if (buf < end)
  808. *buf = '0';
  809. ++ buf;
  810. }
  811. else if (base == 16)
  812. {
  813. if (buf < end)
  814. {
  815. *buf = '0';
  816. }
  817. ++ buf;
  818. if (buf < end)
  819. {
  820. *buf = type & LARGE ? 'X' : 'x';
  821. }
  822. ++ buf;
  823. }
  824. }
  825. #endif /* RT_PRINTF_SPECIAL */
  826. /* no align to the left */
  827. if (!(type & LEFT))
  828. {
  829. while (size-- > 0)
  830. {
  831. if (buf < end)
  832. {
  833. *buf = c;
  834. }
  835. ++ buf;
  836. }
  837. }
  838. #ifdef RT_PRINTF_PRECISION
  839. while (i < precision--)
  840. {
  841. if (buf < end)
  842. {
  843. *buf = '0';
  844. }
  845. ++ buf;
  846. }
  847. #endif /* RT_PRINTF_PRECISION */
  848. /* put number in the temporary buffer */
  849. while (i-- > 0 && (precision_bak != 0))
  850. {
  851. if (buf < end)
  852. {
  853. *buf = tmp[i];
  854. }
  855. ++ buf;
  856. }
  857. while (size-- > 0)
  858. {
  859. if (buf < end)
  860. {
  861. *buf = ' ';
  862. }
  863. ++ buf;
  864. }
  865. return buf;
  866. }
  867. /**
  868. * @brief This function will fill a formatted string to buffer.
  869. *
  870. * @param buf is the buffer to save formatted string.
  871. *
  872. * @param size is the size of buffer.
  873. *
  874. * @param fmt is the format parameters.
  875. *
  876. * @param args is a list of variable parameters.
  877. *
  878. * @return The number of characters actually written to buffer.
  879. */
  880. rt_weak int rt_vsnprintf(char *buf, rt_size_t size, const char *fmt, va_list args)
  881. {
  882. #ifdef RT_KPRINTF_USING_LONGLONG
  883. unsigned long long num = 0;
  884. #else
  885. unsigned long num = 0;
  886. #endif /* RT_KPRINTF_USING_LONGLONG */
  887. int i = 0, len = 0;
  888. char *str = RT_NULL, *end = RT_NULL, c = 0;
  889. const char *s = RT_NULL;
  890. rt_uint8_t base = 0; /* the base of number */
  891. rt_uint8_t flags = 0; /* flags to print number */
  892. rt_uint8_t qualifier = 0; /* 'h', 'l', or 'L' for integer fields */
  893. rt_int32_t field_width = 0; /* width of output field */
  894. #ifdef RT_PRINTF_PRECISION
  895. int precision = 0; /* min. # of digits for integers and max for a string */
  896. #endif /* RT_PRINTF_PRECISION */
  897. str = buf;
  898. end = buf + size;
  899. /* Make sure end is always >= buf */
  900. if (end < buf)
  901. {
  902. end = ((char *) - 1);
  903. size = end - buf;
  904. }
  905. for (; *fmt ; ++fmt)
  906. {
  907. if (*fmt != '%')
  908. {
  909. if (str < end)
  910. {
  911. *str = *fmt;
  912. }
  913. ++ str;
  914. continue;
  915. }
  916. /* process flags */
  917. flags = 0;
  918. while (1)
  919. {
  920. /* skips the first '%' also */
  921. ++ fmt;
  922. if (*fmt == '-') flags |= LEFT;
  923. else if (*fmt == '+') flags |= PLUS;
  924. else if (*fmt == ' ') flags |= SPACE;
  925. else if (*fmt == '#') flags |= SPECIAL;
  926. else if (*fmt == '0') flags |= ZEROPAD;
  927. else break;
  928. }
  929. /* get field width */
  930. field_width = -1;
  931. if (_ISDIGIT(*fmt))
  932. {
  933. field_width = skip_atoi(&fmt);
  934. }
  935. else if (*fmt == '*')
  936. {
  937. ++ fmt;
  938. /* it's the next argument */
  939. field_width = va_arg(args, int);
  940. if (field_width < 0)
  941. {
  942. field_width = -field_width;
  943. flags |= LEFT;
  944. }
  945. }
  946. #ifdef RT_PRINTF_PRECISION
  947. /* get the precision */
  948. precision = -1;
  949. if (*fmt == '.')
  950. {
  951. ++ fmt;
  952. if (_ISDIGIT(*fmt))
  953. {
  954. precision = skip_atoi(&fmt);
  955. }
  956. else if (*fmt == '*')
  957. {
  958. ++ fmt;
  959. /* it's the next argument */
  960. precision = va_arg(args, int);
  961. }
  962. if (precision < 0)
  963. {
  964. precision = 0;
  965. }
  966. }
  967. #endif /* RT_PRINTF_PRECISION */
  968. /* get the conversion qualifier */
  969. qualifier = 0;
  970. #ifdef RT_KPRINTF_USING_LONGLONG
  971. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L')
  972. #else
  973. if (*fmt == 'h' || *fmt == 'l')
  974. #endif /* RT_KPRINTF_USING_LONGLONG */
  975. {
  976. qualifier = *fmt;
  977. ++ fmt;
  978. #ifdef RT_KPRINTF_USING_LONGLONG
  979. if (qualifier == 'l' && *fmt == 'l')
  980. {
  981. qualifier = 'L';
  982. ++ fmt;
  983. }
  984. #endif /* RT_KPRINTF_USING_LONGLONG */
  985. }
  986. /* the default base */
  987. base = 10;
  988. switch (*fmt)
  989. {
  990. case 'c':
  991. if (!(flags & LEFT))
  992. {
  993. while (--field_width > 0)
  994. {
  995. if (str < end) *str = ' ';
  996. ++ str;
  997. }
  998. }
  999. /* get character */
  1000. c = (rt_uint8_t)va_arg(args, int);
  1001. if (str < end)
  1002. {
  1003. *str = c;
  1004. }
  1005. ++ str;
  1006. /* put width */
  1007. while (--field_width > 0)
  1008. {
  1009. if (str < end) *str = ' ';
  1010. ++ str;
  1011. }
  1012. continue;
  1013. case 's':
  1014. s = va_arg(args, char *);
  1015. if (!s)
  1016. {
  1017. s = "(NULL)";
  1018. }
  1019. for (len = 0; (len != field_width) && (s[len] != '\0'); len++);
  1020. #ifdef RT_PRINTF_PRECISION
  1021. if (precision > 0 && len > precision)
  1022. {
  1023. len = precision;
  1024. }
  1025. #endif /* RT_PRINTF_PRECISION */
  1026. if (!(flags & LEFT))
  1027. {
  1028. while (len < field_width--)
  1029. {
  1030. if (str < end) *str = ' ';
  1031. ++ str;
  1032. }
  1033. }
  1034. for (i = 0; i < len; ++i)
  1035. {
  1036. if (str < end) *str = *s;
  1037. ++ str;
  1038. ++ s;
  1039. }
  1040. while (len < field_width--)
  1041. {
  1042. if (str < end) *str = ' ';
  1043. ++ str;
  1044. }
  1045. continue;
  1046. case 'p':
  1047. if (field_width == -1)
  1048. {
  1049. field_width = sizeof(void *) << 1;
  1050. #ifdef RT_PRINTF_SPECIAL
  1051. field_width += 2; /* `0x` prefix */
  1052. flags |= SPECIAL;
  1053. #endif
  1054. flags |= ZEROPAD;
  1055. }
  1056. #ifdef RT_PRINTF_PRECISION
  1057. str = print_number(str, end,
  1058. (unsigned long)va_arg(args, void *),
  1059. 16, qualifier, field_width, precision, flags);
  1060. #else
  1061. str = print_number(str, end,
  1062. (unsigned long)va_arg(args, void *),
  1063. 16, qualifier, field_width, flags);
  1064. #endif
  1065. continue;
  1066. case '%':
  1067. if (str < end)
  1068. {
  1069. *str = '%';
  1070. }
  1071. ++ str;
  1072. continue;
  1073. /* integer number formats - set up the flags and "break" */
  1074. case 'b':
  1075. base = 2;
  1076. break;
  1077. case 'o':
  1078. base = 8;
  1079. break;
  1080. case 'X':
  1081. flags |= LARGE;
  1082. case 'x':
  1083. base = 16;
  1084. break;
  1085. case 'd':
  1086. case 'i':
  1087. flags |= SIGN;
  1088. case 'u':
  1089. break;
  1090. default:
  1091. if (str < end)
  1092. {
  1093. *str = '%';
  1094. }
  1095. ++ str;
  1096. if (*fmt)
  1097. {
  1098. if (str < end)
  1099. {
  1100. *str = *fmt;
  1101. }
  1102. ++ str;
  1103. }
  1104. else
  1105. {
  1106. -- fmt;
  1107. }
  1108. continue;
  1109. }
  1110. #ifdef RT_KPRINTF_USING_LONGLONG
  1111. if (qualifier == 'L')
  1112. {
  1113. num = va_arg(args, unsigned long long);
  1114. }
  1115. else if (qualifier == 'l')
  1116. #else
  1117. if (qualifier == 'l')
  1118. #endif /* RT_KPRINTF_USING_LONGLONG */
  1119. {
  1120. num = va_arg(args, unsigned long);
  1121. }
  1122. else if (qualifier == 'h')
  1123. {
  1124. num = (rt_uint16_t)va_arg(args, rt_int32_t);
  1125. if (flags & SIGN)
  1126. {
  1127. num = (rt_int16_t)num;
  1128. }
  1129. }
  1130. else
  1131. {
  1132. num = (rt_uint32_t)va_arg(args, unsigned long);
  1133. }
  1134. #ifdef RT_PRINTF_PRECISION
  1135. str = print_number(str, end, num, base, qualifier, field_width, precision, flags);
  1136. #else
  1137. str = print_number(str, end, num, base, qualifier, field_width, flags);
  1138. #endif
  1139. }
  1140. if (size > 0)
  1141. {
  1142. if (str < end)
  1143. {
  1144. *str = '\0';
  1145. }
  1146. else
  1147. {
  1148. end[-1] = '\0';
  1149. }
  1150. }
  1151. /* the trailing null byte doesn't count towards the total
  1152. * ++str;
  1153. */
  1154. return str - buf;
  1155. }
  1156. RTM_EXPORT(rt_vsnprintf);
  1157. /**
  1158. * @brief This function will fill a formatted string to buffer.
  1159. *
  1160. * @param buf is the buffer to save formatted string.
  1161. *
  1162. * @param size is the size of buffer.
  1163. *
  1164. * @param fmt is the format parameters.
  1165. *
  1166. * @return The number of characters actually written to buffer.
  1167. */
  1168. int rt_snprintf(char *buf, rt_size_t size, const char *fmt, ...)
  1169. {
  1170. rt_int32_t n = 0;
  1171. va_list args;
  1172. va_start(args, fmt);
  1173. n = rt_vsnprintf(buf, size, fmt, args);
  1174. va_end(args);
  1175. return n;
  1176. }
  1177. RTM_EXPORT(rt_snprintf);
  1178. /**
  1179. * @brief This function will fill a formatted string to buffer.
  1180. *
  1181. * @param buf is the buffer to save formatted string.
  1182. *
  1183. * @param format is the format parameters.
  1184. *
  1185. * @param arg_ptr is a list of variable parameters.
  1186. *
  1187. * @return The number of characters actually written to buffer.
  1188. */
  1189. int rt_vsprintf(char *buf, const char *format, va_list arg_ptr)
  1190. {
  1191. return rt_vsnprintf(buf, (rt_size_t) - 1, format, arg_ptr);
  1192. }
  1193. RTM_EXPORT(rt_vsprintf);
  1194. /**
  1195. * @brief This function will fill a formatted string to buffer
  1196. *
  1197. * @param buf the buffer to save formatted string.
  1198. *
  1199. * @param format is the format parameters.
  1200. *
  1201. * @return The number of characters actually written to buffer.
  1202. */
  1203. int rt_sprintf(char *buf, const char *format, ...)
  1204. {
  1205. rt_int32_t n = 0;
  1206. va_list arg_ptr;
  1207. va_start(arg_ptr, format);
  1208. n = rt_vsprintf(buf, format, arg_ptr);
  1209. va_end(arg_ptr);
  1210. return n;
  1211. }
  1212. RTM_EXPORT(rt_sprintf);
  1213. #ifdef RT_USING_CONSOLE
  1214. #ifdef RT_USING_DEVICE
  1215. /**
  1216. * @brief This function returns the device using in console.
  1217. *
  1218. * @return Returns the console device pointer or RT_NULL.
  1219. */
  1220. rt_device_t rt_console_get_device(void)
  1221. {
  1222. return _console_device;
  1223. }
  1224. RTM_EXPORT(rt_console_get_device);
  1225. /**
  1226. * @brief This function will set a device as console device.
  1227. * After set a device to console, all output of rt_kprintf will be
  1228. * redirected to this new device.
  1229. *
  1230. * @param name is the name of new console device.
  1231. *
  1232. * @return the old console device handler on successful, or RT_NULL on failure.
  1233. */
  1234. rt_device_t rt_console_set_device(const char *name)
  1235. {
  1236. #ifdef RT_USING_SMART
  1237. rt_device_t new_iodev = RT_NULL, old_iodev = RT_NULL;
  1238. /* find new console device */
  1239. new_iodev = rt_device_find(name);
  1240. if (new_iodev != RT_NULL)
  1241. {
  1242. if (_console_device != RT_NULL)
  1243. {
  1244. old_iodev = console_set_iodev(new_iodev);
  1245. }
  1246. else
  1247. {
  1248. console_register("console", new_iodev);
  1249. _console_device = rt_device_find("console");
  1250. rt_device_open(_console_device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_STREAM);
  1251. }
  1252. }
  1253. return old_iodev;
  1254. #else
  1255. rt_device_t new_device, old_device;
  1256. /* save old device */
  1257. old_device = _console_device;
  1258. /* find new console device */
  1259. new_device = rt_device_find(name);
  1260. /* check whether it's a same device */
  1261. if (new_device == old_device) return RT_NULL;
  1262. if (new_device != RT_NULL)
  1263. {
  1264. if (_console_device != RT_NULL)
  1265. {
  1266. /* close old console device */
  1267. rt_device_close(_console_device);
  1268. }
  1269. /* set new console device */
  1270. rt_device_open(new_device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_STREAM);
  1271. _console_device = new_device;
  1272. }
  1273. return old_device;
  1274. #endif
  1275. }
  1276. RTM_EXPORT(rt_console_set_device);
  1277. #endif /* RT_USING_DEVICE */
  1278. rt_weak void rt_hw_console_output(const char *str)
  1279. {
  1280. /* empty console output */
  1281. }
  1282. RTM_EXPORT(rt_hw_console_output);
  1283. /**
  1284. * @brief This function will put string to the console.
  1285. *
  1286. * @param str is the string output to the console.
  1287. */
  1288. void rt_kputs(const char *str)
  1289. {
  1290. if (!str)
  1291. {
  1292. return;
  1293. }
  1294. #ifdef RT_USING_DEVICE
  1295. if (_console_device == RT_NULL)
  1296. {
  1297. rt_hw_console_output(str);
  1298. }
  1299. else
  1300. {
  1301. rt_device_write(_console_device, 0, str, rt_strlen(str));
  1302. }
  1303. #else
  1304. rt_hw_console_output(str);
  1305. #endif /* RT_USING_DEVICE */
  1306. }
  1307. /**
  1308. * @brief This function will print a formatted string on system console.
  1309. *
  1310. * @param fmt is the format parameters.
  1311. *
  1312. * @return The number of characters actually written to buffer.
  1313. */
  1314. rt_weak int rt_kprintf(const char *fmt, ...)
  1315. {
  1316. va_list args;
  1317. rt_size_t length = 0;
  1318. static char rt_log_buf[RT_CONSOLEBUF_SIZE];
  1319. va_start(args, fmt);
  1320. /* the return value of vsnprintf is the number of bytes that would be
  1321. * written to buffer had if the size of the buffer been sufficiently
  1322. * large excluding the terminating null byte. If the output string
  1323. * would be larger than the rt_log_buf, we have to adjust the output
  1324. * length. */
  1325. length = rt_vsnprintf(rt_log_buf, sizeof(rt_log_buf) - 1, fmt, args);
  1326. if (length > RT_CONSOLEBUF_SIZE - 1)
  1327. {
  1328. length = RT_CONSOLEBUF_SIZE - 1;
  1329. }
  1330. #ifdef RT_USING_DEVICE
  1331. if (_console_device == RT_NULL)
  1332. {
  1333. rt_hw_console_output(rt_log_buf);
  1334. }
  1335. else
  1336. {
  1337. rt_device_write(_console_device, 0, rt_log_buf, length);
  1338. }
  1339. #else
  1340. rt_hw_console_output(rt_log_buf);
  1341. #endif /* RT_USING_DEVICE */
  1342. va_end(args);
  1343. return length;
  1344. }
  1345. RTM_EXPORT(rt_kprintf);
  1346. #endif /* RT_USING_CONSOLE */
  1347. #if defined(RT_USING_HEAP) && !defined(RT_USING_USERHEAP)
  1348. #ifdef RT_USING_HOOK
  1349. static void (*rt_malloc_hook)(void *ptr, rt_size_t size);
  1350. static void (*rt_free_hook)(void *ptr);
  1351. /**
  1352. * @addtogroup Hook
  1353. * @{
  1354. */
  1355. /**
  1356. * @brief This function will set a hook function, which will be invoked when a memory
  1357. * block is allocated from heap memory.
  1358. *
  1359. * @param hook the hook function.
  1360. */
  1361. void rt_malloc_sethook(void (*hook)(void *ptr, rt_size_t size))
  1362. {
  1363. rt_malloc_hook = hook;
  1364. }
  1365. /**
  1366. * @brief This function will set a hook function, which will be invoked when a memory
  1367. * block is released to heap memory.
  1368. *
  1369. * @param hook the hook function
  1370. */
  1371. void rt_free_sethook(void (*hook)(void *ptr))
  1372. {
  1373. rt_free_hook = hook;
  1374. }
  1375. /**@}*/
  1376. #endif /* RT_USING_HOOK */
  1377. #if defined(RT_USING_HEAP_ISR)
  1378. #elif defined(RT_USING_MUTEX)
  1379. static struct rt_mutex _lock;
  1380. #endif
  1381. rt_inline void _heap_lock_init(void)
  1382. {
  1383. #if defined(RT_USING_HEAP_ISR)
  1384. #elif defined(RT_USING_MUTEX)
  1385. rt_mutex_init(&_lock, "heap", RT_IPC_FLAG_PRIO);
  1386. #endif
  1387. }
  1388. rt_inline rt_base_t _heap_lock(void)
  1389. {
  1390. #if defined(RT_USING_HEAP_ISR)
  1391. return rt_hw_interrupt_disable();
  1392. #elif defined(RT_USING_MUTEX)
  1393. if (rt_thread_self())
  1394. return rt_mutex_take(&_lock, RT_WAITING_FOREVER);
  1395. else
  1396. return RT_EOK;
  1397. #else
  1398. rt_enter_critical();
  1399. return RT_EOK;
  1400. #endif
  1401. }
  1402. rt_inline void _heap_unlock(rt_base_t level)
  1403. {
  1404. #if defined(RT_USING_HEAP_ISR)
  1405. rt_hw_interrupt_enable(level);
  1406. #elif defined(RT_USING_MUTEX)
  1407. RT_ASSERT(level == RT_EOK);
  1408. if (rt_thread_self())
  1409. rt_mutex_release(&_lock);
  1410. #else
  1411. rt_exit_critical();
  1412. #endif
  1413. }
  1414. #ifdef RT_USING_UTESTCASES
  1415. /* export to utest to observe the inner statements */
  1416. #ifdef _MSC_VER
  1417. #define rt_heap_lock() _heap_lock()
  1418. #define rt_heap_unlock() _heap_unlock()
  1419. #else
  1420. rt_base_t rt_heap_lock(void) __attribute__((alias("_heap_lock")));
  1421. void rt_heap_unlock(rt_base_t level) __attribute__((alias("_heap_unlock")));
  1422. #endif /* _MSC_VER */
  1423. #endif
  1424. #if defined(RT_USING_SMALL_MEM_AS_HEAP)
  1425. static rt_smem_t system_heap;
  1426. rt_inline void _smem_info(rt_size_t *total,
  1427. rt_size_t *used, rt_size_t *max_used)
  1428. {
  1429. if (total)
  1430. *total = system_heap->total;
  1431. if (used)
  1432. *used = system_heap->used;
  1433. if (max_used)
  1434. *max_used = system_heap->max;
  1435. }
  1436. #define _MEM_INIT(_name, _start, _size) \
  1437. system_heap = rt_smem_init(_name, _start, _size)
  1438. #define _MEM_MALLOC(_size) \
  1439. rt_smem_alloc(system_heap, _size)
  1440. #define _MEM_REALLOC(_ptr, _newsize)\
  1441. rt_smem_realloc(system_heap, _ptr, _newsize)
  1442. #define _MEM_FREE(_ptr) \
  1443. rt_smem_free(_ptr)
  1444. #define _MEM_INFO(_total, _used, _max) \
  1445. _smem_info(_total, _used, _max)
  1446. #elif defined(RT_USING_MEMHEAP_AS_HEAP)
  1447. static struct rt_memheap system_heap;
  1448. void *_memheap_alloc(struct rt_memheap *heap, rt_size_t size);
  1449. void _memheap_free(void *rmem);
  1450. void *_memheap_realloc(struct rt_memheap *heap, void *rmem, rt_size_t newsize);
  1451. #define _MEM_INIT(_name, _start, _size) \
  1452. rt_memheap_init(&system_heap, _name, _start, _size)
  1453. #define _MEM_MALLOC(_size) \
  1454. _memheap_alloc(&system_heap, _size)
  1455. #define _MEM_REALLOC(_ptr, _newsize) \
  1456. _memheap_realloc(&system_heap, _ptr, _newsize)
  1457. #define _MEM_FREE(_ptr) \
  1458. _memheap_free(_ptr)
  1459. #define _MEM_INFO(_total, _used, _max) \
  1460. rt_memheap_info(&system_heap, _total, _used, _max)
  1461. #elif defined(RT_USING_SLAB_AS_HEAP)
  1462. static rt_slab_t system_heap;
  1463. rt_inline void _slab_info(rt_size_t *total,
  1464. rt_size_t *used, rt_size_t *max_used)
  1465. {
  1466. if (total)
  1467. *total = system_heap->total;
  1468. if (used)
  1469. *used = system_heap->used;
  1470. if (max_used)
  1471. *max_used = system_heap->max;
  1472. }
  1473. #define _MEM_INIT(_name, _start, _size) \
  1474. system_heap = rt_slab_init(_name, _start, _size)
  1475. #define _MEM_MALLOC(_size) \
  1476. rt_slab_alloc(system_heap, _size)
  1477. #define _MEM_REALLOC(_ptr, _newsize) \
  1478. rt_slab_realloc(system_heap, _ptr, _newsize)
  1479. #define _MEM_FREE(_ptr) \
  1480. rt_slab_free(system_heap, _ptr)
  1481. #define _MEM_INFO _slab_info
  1482. #else
  1483. #define _MEM_INIT(...)
  1484. #define _MEM_MALLOC(...) RT_NULL
  1485. #define _MEM_REALLOC(...) RT_NULL
  1486. #define _MEM_FREE(...)
  1487. #define _MEM_INFO(...)
  1488. #endif
  1489. /**
  1490. * @brief This function will init system heap.
  1491. *
  1492. * @param begin_addr the beginning address of system page.
  1493. *
  1494. * @param end_addr the end address of system page.
  1495. */
  1496. rt_weak void rt_system_heap_init(void *begin_addr, void *end_addr)
  1497. {
  1498. rt_ubase_t begin_align = RT_ALIGN((rt_ubase_t)begin_addr, RT_ALIGN_SIZE);
  1499. rt_ubase_t end_align = RT_ALIGN_DOWN((rt_ubase_t)end_addr, RT_ALIGN_SIZE);
  1500. RT_ASSERT(end_align > begin_align);
  1501. /* Initialize system memory heap */
  1502. _MEM_INIT("heap", begin_addr, end_align - begin_align);
  1503. /* Initialize multi thread contention lock */
  1504. _heap_lock_init();
  1505. }
  1506. /**
  1507. * @brief Allocate a block of memory with a minimum of 'size' bytes.
  1508. *
  1509. * @param size is the minimum size of the requested block in bytes.
  1510. *
  1511. * @return the pointer to allocated memory or NULL if no free memory was found.
  1512. */
  1513. rt_weak void *rt_malloc(rt_size_t size)
  1514. {
  1515. rt_base_t level;
  1516. void *ptr;
  1517. /* Enter critical zone */
  1518. level = _heap_lock();
  1519. /* allocate memory block from system heap */
  1520. ptr = _MEM_MALLOC(size);
  1521. /* Exit critical zone */
  1522. _heap_unlock(level);
  1523. /* call 'rt_malloc' hook */
  1524. RT_OBJECT_HOOK_CALL(rt_malloc_hook, (ptr, size));
  1525. return ptr;
  1526. }
  1527. RTM_EXPORT(rt_malloc);
  1528. /**
  1529. * @brief This function will change the size of previously allocated memory block.
  1530. *
  1531. * @param ptr is the pointer to memory allocated by rt_malloc.
  1532. *
  1533. * @param newsize is the required new size.
  1534. *
  1535. * @return the changed memory block address.
  1536. */
  1537. rt_weak void *rt_realloc(void *ptr, rt_size_t newsize)
  1538. {
  1539. rt_base_t level;
  1540. void *nptr;
  1541. /* Enter critical zone */
  1542. level = _heap_lock();
  1543. /* Change the size of previously allocated memory block */
  1544. nptr = _MEM_REALLOC(ptr, newsize);
  1545. /* Exit critical zone */
  1546. _heap_unlock(level);
  1547. return nptr;
  1548. }
  1549. RTM_EXPORT(rt_realloc);
  1550. /**
  1551. * @brief This function will contiguously allocate enough space for count objects
  1552. * that are size bytes of memory each and returns a pointer to the allocated
  1553. * memory.
  1554. *
  1555. * @note The allocated memory is filled with bytes of value zero.
  1556. *
  1557. * @param count is the number of objects to allocate.
  1558. *
  1559. * @param size is the size of one object to allocate.
  1560. *
  1561. * @return pointer to allocated memory / NULL pointer if there is an error.
  1562. */
  1563. rt_weak void *rt_calloc(rt_size_t count, rt_size_t size)
  1564. {
  1565. void *p;
  1566. /* allocate 'count' objects of size 'size' */
  1567. p = rt_malloc(count * size);
  1568. /* zero the memory */
  1569. if (p)
  1570. {
  1571. rt_memset(p, 0, count * size);
  1572. }
  1573. return p;
  1574. }
  1575. RTM_EXPORT(rt_calloc);
  1576. /**
  1577. * @brief This function will release the previously allocated memory block by
  1578. * rt_malloc. The released memory block is taken back to system heap.
  1579. *
  1580. * @param ptr the address of memory which will be released.
  1581. */
  1582. rt_weak void rt_free(void *ptr)
  1583. {
  1584. rt_base_t level;
  1585. /* call 'rt_free' hook */
  1586. RT_OBJECT_HOOK_CALL(rt_free_hook, (ptr));
  1587. /* NULL check */
  1588. if (ptr == RT_NULL) return;
  1589. /* Enter critical zone */
  1590. level = _heap_lock();
  1591. _MEM_FREE(ptr);
  1592. /* Exit critical zone */
  1593. _heap_unlock(level);
  1594. }
  1595. RTM_EXPORT(rt_free);
  1596. /**
  1597. * @brief This function will caculate the total memory, the used memory, and
  1598. * the max used memory.
  1599. *
  1600. * @param total is a pointer to get the total size of the memory.
  1601. *
  1602. * @param used is a pointer to get the size of memory used.
  1603. *
  1604. * @param max_used is a pointer to get the maximum memory used.
  1605. */
  1606. rt_weak void rt_memory_info(rt_size_t *total,
  1607. rt_size_t *used,
  1608. rt_size_t *max_used)
  1609. {
  1610. rt_base_t level;
  1611. /* Enter critical zone */
  1612. level = _heap_lock();
  1613. _MEM_INFO(total, used, max_used);
  1614. /* Exit critical zone */
  1615. _heap_unlock(level);
  1616. }
  1617. RTM_EXPORT(rt_memory_info);
  1618. #if defined(RT_USING_SLAB) && defined(RT_USING_SLAB_AS_HEAP)
  1619. void *rt_page_alloc(rt_size_t npages)
  1620. {
  1621. rt_base_t level;
  1622. void *ptr;
  1623. /* Enter critical zone */
  1624. level = _heap_lock();
  1625. /* alloc page */
  1626. ptr = rt_slab_page_alloc(system_heap, npages);
  1627. /* Exit critical zone */
  1628. _heap_unlock(level);
  1629. return ptr;
  1630. }
  1631. void rt_page_free(void *addr, rt_size_t npages)
  1632. {
  1633. rt_base_t level;
  1634. /* Enter critical zone */
  1635. level = _heap_lock();
  1636. /* free page */
  1637. rt_slab_page_free(system_heap, addr, npages);
  1638. /* Exit critical zone */
  1639. _heap_unlock(level);
  1640. }
  1641. #endif
  1642. /**
  1643. * @brief This function allocates a memory block, which address is aligned to the
  1644. * specified alignment size.
  1645. *
  1646. * @param size is the allocated memory block size.
  1647. *
  1648. * @param align is the alignment size.
  1649. *
  1650. * @return The memory block address was returned successfully, otherwise it was
  1651. * returned empty RT_NULL.
  1652. */
  1653. rt_weak void *rt_malloc_align(rt_size_t size, rt_size_t align)
  1654. {
  1655. void *ptr = RT_NULL;
  1656. void *align_ptr = RT_NULL;
  1657. int uintptr_size = 0;
  1658. rt_size_t align_size = 0;
  1659. /* sizeof pointer */
  1660. uintptr_size = sizeof(void*);
  1661. uintptr_size -= 1;
  1662. /* align the alignment size to uintptr size byte */
  1663. align = ((align + uintptr_size) & ~uintptr_size);
  1664. /* get total aligned size */
  1665. align_size = ((size + uintptr_size) & ~uintptr_size) + align;
  1666. /* allocate memory block from heap */
  1667. ptr = rt_malloc(align_size);
  1668. if (ptr != RT_NULL)
  1669. {
  1670. /* the allocated memory block is aligned */
  1671. if (((rt_ubase_t)ptr & (align - 1)) == 0)
  1672. {
  1673. align_ptr = (void *)((rt_ubase_t)ptr + align);
  1674. }
  1675. else
  1676. {
  1677. align_ptr = (void *)(((rt_ubase_t)ptr + (align - 1)) & ~(align - 1));
  1678. }
  1679. /* set the pointer before alignment pointer to the real pointer */
  1680. *((rt_ubase_t *)((rt_ubase_t)align_ptr - sizeof(void *))) = (rt_ubase_t)ptr;
  1681. ptr = align_ptr;
  1682. }
  1683. return ptr;
  1684. }
  1685. RTM_EXPORT(rt_malloc_align);
  1686. /**
  1687. * @brief This function release the memory block, which is allocated by
  1688. * rt_malloc_align function and address is aligned.
  1689. *
  1690. * @param ptr is the memory block pointer.
  1691. */
  1692. rt_weak void rt_free_align(void *ptr)
  1693. {
  1694. void *real_ptr = RT_NULL;
  1695. /* NULL check */
  1696. if (ptr == RT_NULL) return;
  1697. real_ptr = (void *) * (rt_ubase_t *)((rt_ubase_t)ptr - sizeof(void *));
  1698. rt_free(real_ptr);
  1699. }
  1700. RTM_EXPORT(rt_free_align);
  1701. #endif /* RT_USING_HEAP */
  1702. #ifndef RT_USING_CPU_FFS
  1703. #ifdef RT_USING_TINY_FFS
  1704. const rt_uint8_t __lowest_bit_bitmap[] =
  1705. {
  1706. /* 0 - 7 */ 0, 1, 2, 27, 3, 24, 28, 32,
  1707. /* 8 - 15 */ 4, 17, 25, 31, 29, 12, 32, 14,
  1708. /* 16 - 23 */ 5, 8, 18, 32, 26, 23, 32, 16,
  1709. /* 24 - 31 */ 30, 11, 13, 7, 32, 22, 15, 10,
  1710. /* 32 - 36 */ 6, 21, 9, 20, 19
  1711. };
  1712. /**
  1713. * @brief This function finds the first bit set (beginning with the least significant bit)
  1714. * in value and return the index of that bit.
  1715. *
  1716. * Bits are numbered starting at 1 (the least significant bit). A return value of
  1717. * zero from any of these functions means that the argument was zero.
  1718. *
  1719. * @param value is the value to find the first bit set in.
  1720. *
  1721. * @return return the index of the first bit set. If value is 0, then this function
  1722. * shall return 0.
  1723. */
  1724. int __rt_ffs(int value)
  1725. {
  1726. return __lowest_bit_bitmap[(rt_uint32_t)(value & (value - 1) ^ value) % 37];
  1727. }
  1728. #else
  1729. const rt_uint8_t __lowest_bit_bitmap[] =
  1730. {
  1731. /* 00 */ 0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1732. /* 10 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1733. /* 20 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1734. /* 30 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1735. /* 40 */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1736. /* 50 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1737. /* 60 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1738. /* 70 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1739. /* 80 */ 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1740. /* 90 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1741. /* A0 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1742. /* B0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1743. /* C0 */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1744. /* D0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1745. /* E0 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1746. /* F0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
  1747. };
  1748. /**
  1749. * @brief This function finds the first bit set (beginning with the least significant bit)
  1750. * in value and return the index of that bit.
  1751. *
  1752. * Bits are numbered starting at 1 (the least significant bit). A return value of
  1753. * zero from any of these functions means that the argument was zero.
  1754. *
  1755. * @param value is the value to find the first bit set in.
  1756. *
  1757. * @return Return the index of the first bit set. If value is 0, then this function
  1758. * shall return 0.
  1759. */
  1760. int __rt_ffs(int value)
  1761. {
  1762. if (value == 0)
  1763. {
  1764. return 0;
  1765. }
  1766. if (value & 0xff)
  1767. {
  1768. return __lowest_bit_bitmap[value & 0xff] + 1;
  1769. }
  1770. if (value & 0xff00)
  1771. {
  1772. return __lowest_bit_bitmap[(value & 0xff00) >> 8] + 9;
  1773. }
  1774. if (value & 0xff0000)
  1775. {
  1776. return __lowest_bit_bitmap[(value & 0xff0000) >> 16] + 17;
  1777. }
  1778. return __lowest_bit_bitmap[(value & 0xff000000) >> 24] + 25;
  1779. }
  1780. #endif /* RT_USING_TINY_FFS */
  1781. #endif /* RT_USING_CPU_FFS */
  1782. #ifndef __on_rt_assert_hook
  1783. #define __on_rt_assert_hook(ex, func, line) __ON_HOOK_ARGS(rt_assert_hook, (ex, func, line))
  1784. #endif
  1785. #ifdef RT_USING_DEBUG
  1786. /* RT_ASSERT(EX)'s hook */
  1787. void (*rt_assert_hook)(const char *ex, const char *func, rt_size_t line);
  1788. /**
  1789. * This function will set a hook function to RT_ASSERT(EX). It will run when the expression is false.
  1790. *
  1791. * @param hook is the hook function.
  1792. */
  1793. void rt_assert_set_hook(void (*hook)(const char *ex, const char *func, rt_size_t line))
  1794. {
  1795. rt_assert_hook = hook;
  1796. }
  1797. /**
  1798. * The RT_ASSERT function.
  1799. *
  1800. * @param ex_string is the assertion condition string.
  1801. *
  1802. * @param func is the function name when assertion.
  1803. *
  1804. * @param line is the file line number when assertion.
  1805. */
  1806. void rt_assert_handler(const char *ex_string, const char *func, rt_size_t line)
  1807. {
  1808. volatile char dummy = 0;
  1809. if (rt_assert_hook == RT_NULL)
  1810. {
  1811. #ifdef RT_USING_MODULE
  1812. if (dlmodule_self())
  1813. {
  1814. /* close assertion module */
  1815. dlmodule_exit(-1);
  1816. }
  1817. else
  1818. #endif /*RT_USING_MODULE*/
  1819. {
  1820. rt_kprintf("(%s) assertion failed at function:%s, line number:%d \n", ex_string, func, line);
  1821. while (dummy == 0);
  1822. }
  1823. }
  1824. else
  1825. {
  1826. rt_assert_hook(ex_string, func, line);
  1827. }
  1828. }
  1829. RTM_EXPORT(rt_assert_handler);
  1830. #endif /* RT_USING_DEBUG */
  1831. /**@}*/