kservice.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  1. /*
  2. * File : kservice.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2006-03-16 Bernard the first version
  23. * 2006-05-25 Bernard rewrite vsprintf
  24. * 2006-08-10 Bernard add rt_show_version
  25. * 2010-03-17 Bernard remove rt_strlcpy function
  26. * fix gcc compiling issue.
  27. * 2010-04-15 Bernard remove weak definition on ICCM16C compiler
  28. * 2012-07-18 Arda add the alignment display for signed integer
  29. * 2012-11-23 Bernard fix IAR compiler error.
  30. * 2012-12-22 Bernard fix rt_kprintf issue, which found by Grissiom.
  31. * 2013-06-24 Bernard remove rt_kprintf if RT_USING_CONSOLE is not defined.
  32. * 2013-09-24 aozima make sure the device is in STREAM mode when used by rt_kprintf.
  33. * 2015-07-06 Bernard Add rt_assert_handler routine.
  34. */
  35. #include <rtthread.h>
  36. #include <rthw.h>
  37. /* use precision */
  38. #define RT_PRINTF_PRECISION
  39. /**
  40. * @addtogroup KernelService
  41. */
  42. /**@{*/
  43. /* global errno in RT-Thread */
  44. static volatile int _errno;
  45. #if defined(RT_USING_DEVICE) && defined(RT_USING_CONSOLE)
  46. static rt_device_t _console_device = RT_NULL;
  47. #endif
  48. /*
  49. * This function will get errno
  50. *
  51. * @return errno
  52. */
  53. rt_err_t rt_get_errno(void)
  54. {
  55. rt_thread_t tid;
  56. if (rt_interrupt_get_nest() != 0)
  57. {
  58. /* it's in interrupt context */
  59. return _errno;
  60. }
  61. tid = rt_thread_self();
  62. if (tid == RT_NULL)
  63. return _errno;
  64. return tid->error;
  65. }
  66. RTM_EXPORT(rt_get_errno);
  67. /*
  68. * This function will set errno
  69. *
  70. * @param error the errno shall be set
  71. */
  72. void rt_set_errno(rt_err_t error)
  73. {
  74. rt_thread_t tid;
  75. if (rt_interrupt_get_nest() != 0)
  76. {
  77. /* it's in interrupt context */
  78. _errno = error;
  79. return;
  80. }
  81. tid = rt_thread_self();
  82. if (tid == RT_NULL)
  83. {
  84. _errno = error;
  85. return;
  86. }
  87. tid->error = error;
  88. }
  89. RTM_EXPORT(rt_set_errno);
  90. /**
  91. * This function returns errno.
  92. *
  93. * @return the errno in the system
  94. */
  95. int *_rt_errno(void)
  96. {
  97. rt_thread_t tid;
  98. if (rt_interrupt_get_nest() != 0)
  99. return (int *)&_errno;
  100. tid = rt_thread_self();
  101. if (tid != RT_NULL)
  102. return (int *) & (tid->error);
  103. return (int *)&_errno;
  104. }
  105. RTM_EXPORT(_rt_errno);
  106. /**
  107. * This function will set the content of memory to specified value
  108. *
  109. * @param s the address of source memory
  110. * @param c the value shall be set in content
  111. * @param count the copied length
  112. *
  113. * @return the address of source memory
  114. */
  115. void *rt_memset(void *s, int c, rt_ubase_t count)
  116. {
  117. #ifdef RT_USING_TINY_SIZE
  118. char *xs = (char *)s;
  119. while (count--)
  120. *xs++ = c;
  121. return s;
  122. #else
  123. #define LBLOCKSIZE (sizeof(rt_int32_t))
  124. #define UNALIGNED(X) ((rt_int32_t)X & (LBLOCKSIZE - 1))
  125. #define TOO_SMALL(LEN) ((LEN) < LBLOCKSIZE)
  126. int i;
  127. char *m = (char *)s;
  128. rt_uint32_t buffer;
  129. rt_uint32_t *aligned_addr;
  130. rt_uint32_t d = c & 0xff;
  131. if (!TOO_SMALL(count) && !UNALIGNED(s))
  132. {
  133. /* If we get this far, we know that n is large and m is word-aligned. */
  134. aligned_addr = (rt_uint32_t *)s;
  135. /* Store D into each char sized location in BUFFER so that
  136. * we can set large blocks quickly.
  137. */
  138. if (LBLOCKSIZE == 4)
  139. {
  140. buffer = (d << 8) | d;
  141. buffer |= (buffer << 16);
  142. }
  143. else
  144. {
  145. buffer = 0;
  146. for (i = 0; i < LBLOCKSIZE; i ++)
  147. buffer = (buffer << 8) | d;
  148. }
  149. while (count >= LBLOCKSIZE * 4)
  150. {
  151. *aligned_addr++ = buffer;
  152. *aligned_addr++ = buffer;
  153. *aligned_addr++ = buffer;
  154. *aligned_addr++ = buffer;
  155. count -= 4 * LBLOCKSIZE;
  156. }
  157. while (count >= LBLOCKSIZE)
  158. {
  159. *aligned_addr++ = buffer;
  160. count -= LBLOCKSIZE;
  161. }
  162. /* Pick up the remainder with a bytewise loop. */
  163. m = (char *)aligned_addr;
  164. }
  165. while (count--)
  166. {
  167. *m++ = (char)d;
  168. }
  169. return s;
  170. #undef LBLOCKSIZE
  171. #undef UNALIGNED
  172. #undef TOO_SMALL
  173. #endif
  174. }
  175. RTM_EXPORT(rt_memset);
  176. /**
  177. * This function will copy memory content from source address to destination
  178. * address.
  179. *
  180. * @param dst the address of destination memory
  181. * @param src the address of source memory
  182. * @param count the copied length
  183. *
  184. * @return the address of destination memory
  185. */
  186. void *rt_memcpy(void *dst, const void *src, rt_ubase_t count)
  187. {
  188. #ifdef RT_USING_TINY_SIZE
  189. char *tmp = (char *)dst, *s = (char *)src;
  190. while (count--)
  191. *tmp++ = *s++;
  192. return dst;
  193. #else
  194. #define UNALIGNED(X, Y) \
  195. (((rt_int32_t)X & (sizeof(rt_int32_t) - 1)) | \
  196. ((rt_int32_t)Y & (sizeof(rt_int32_t) - 1)))
  197. #define BIGBLOCKSIZE (sizeof(rt_int32_t) << 2)
  198. #define LITTLEBLOCKSIZE (sizeof(rt_int32_t))
  199. #define TOO_SMALL(LEN) ((LEN) < BIGBLOCKSIZE)
  200. char *dst_ptr = (char *)dst;
  201. char *src_ptr = (char *)src;
  202. rt_int32_t *aligned_dst;
  203. rt_int32_t *aligned_src;
  204. int len = count;
  205. /* If the size is small, or either SRC or DST is unaligned,
  206. then punt into the byte copy loop. This should be rare. */
  207. if (!TOO_SMALL(len) && !UNALIGNED(src_ptr, dst_ptr))
  208. {
  209. aligned_dst = (rt_int32_t *)dst_ptr;
  210. aligned_src = (rt_int32_t *)src_ptr;
  211. /* Copy 4X long words at a time if possible. */
  212. while (len >= BIGBLOCKSIZE)
  213. {
  214. *aligned_dst++ = *aligned_src++;
  215. *aligned_dst++ = *aligned_src++;
  216. *aligned_dst++ = *aligned_src++;
  217. *aligned_dst++ = *aligned_src++;
  218. len -= BIGBLOCKSIZE;
  219. }
  220. /* Copy one long word at a time if possible. */
  221. while (len >= LITTLEBLOCKSIZE)
  222. {
  223. *aligned_dst++ = *aligned_src++;
  224. len -= LITTLEBLOCKSIZE;
  225. }
  226. /* Pick up any residual with a byte copier. */
  227. dst_ptr = (char *)aligned_dst;
  228. src_ptr = (char *)aligned_src;
  229. }
  230. while (len--)
  231. *dst_ptr++ = *src_ptr++;
  232. return dst;
  233. #undef UNALIGNED
  234. #undef BIGBLOCKSIZE
  235. #undef LITTLEBLOCKSIZE
  236. #undef TOO_SMALL
  237. #endif
  238. }
  239. RTM_EXPORT(rt_memcpy);
  240. /**
  241. * This function will move memory content from source address to destination
  242. * address.
  243. *
  244. * @param dest the address of destination memory
  245. * @param src the address of source memory
  246. * @param n the copied length
  247. *
  248. * @return the address of destination memory
  249. */
  250. void *rt_memmove(void *dest, const void *src, rt_ubase_t n)
  251. {
  252. char *tmp = (char *)dest, *s = (char *)src;
  253. if (s < tmp && tmp < s + n)
  254. {
  255. tmp += n;
  256. s += n;
  257. while (n--)
  258. *(--tmp) = *(--s);
  259. }
  260. else
  261. {
  262. while (n--)
  263. *tmp++ = *s++;
  264. }
  265. return dest;
  266. }
  267. RTM_EXPORT(rt_memmove);
  268. /**
  269. * This function will compare two areas of memory
  270. *
  271. * @param cs one area of memory
  272. * @param ct znother area of memory
  273. * @param count the size of the area
  274. *
  275. * @return the result
  276. */
  277. rt_int32_t rt_memcmp(const void *cs, const void *ct, rt_ubase_t count)
  278. {
  279. const unsigned char *su1, *su2;
  280. int res = 0;
  281. for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
  282. if ((res = *su1 - *su2) != 0)
  283. break;
  284. return res;
  285. }
  286. RTM_EXPORT(rt_memcmp);
  287. /**
  288. * This function will return the first occurrence of a string.
  289. *
  290. * @param s1 the source string
  291. * @param s2 the find string
  292. *
  293. * @return the first occurrence of a s2 in s1, or RT_NULL if no found.
  294. */
  295. char *rt_strstr(const char *s1, const char *s2)
  296. {
  297. int l1, l2;
  298. l2 = rt_strlen(s2);
  299. if (!l2)
  300. return (char *)s1;
  301. l1 = rt_strlen(s1);
  302. while (l1 >= l2)
  303. {
  304. l1 --;
  305. if (!rt_memcmp(s1, s2, l2))
  306. return (char *)s1;
  307. s1 ++;
  308. }
  309. return RT_NULL;
  310. }
  311. RTM_EXPORT(rt_strstr);
  312. /**
  313. * This function will compare two strings while ignoring differences in case
  314. *
  315. * @param a the string to be compared
  316. * @param b the string to be compared
  317. *
  318. * @return the result
  319. */
  320. rt_uint32_t rt_strcasecmp(const char *a, const char *b)
  321. {
  322. int ca, cb;
  323. do
  324. {
  325. ca = *a++ & 0xff;
  326. cb = *b++ & 0xff;
  327. if (ca >= 'A' && ca <= 'Z')
  328. ca += 'a' - 'A';
  329. if (cb >= 'A' && cb <= 'Z')
  330. cb += 'a' - 'A';
  331. }
  332. while (ca == cb && ca != '\0');
  333. return ca - cb;
  334. }
  335. RTM_EXPORT(rt_strcasecmp);
  336. /**
  337. * This function will copy string no more than n bytes.
  338. *
  339. * @param dst the string to copy
  340. * @param src the string to be copied
  341. * @param n the maximum copied length
  342. *
  343. * @return the result
  344. */
  345. char *rt_strncpy(char *dst, const char *src, rt_ubase_t n)
  346. {
  347. if (n != 0)
  348. {
  349. char *d = dst;
  350. const char *s = src;
  351. do
  352. {
  353. if ((*d++ = *s++) == 0)
  354. {
  355. /* NUL pad the remaining n-1 bytes */
  356. while (--n != 0)
  357. *d++ = 0;
  358. break;
  359. }
  360. } while (--n != 0);
  361. }
  362. return (dst);
  363. }
  364. RTM_EXPORT(rt_strncpy);
  365. /**
  366. * This function will compare two strings with specified maximum length
  367. *
  368. * @param cs the string to be compared
  369. * @param ct the string to be compared
  370. * @param count the maximum compare length
  371. *
  372. * @return the result
  373. */
  374. rt_int32_t rt_strncmp(const char *cs, const char *ct, rt_ubase_t count)
  375. {
  376. register signed char __res = 0;
  377. while (count)
  378. {
  379. if ((__res = *cs - *ct++) != 0 || !*cs++)
  380. break;
  381. count --;
  382. }
  383. return __res;
  384. }
  385. RTM_EXPORT(rt_strncmp);
  386. /**
  387. * This function will compare two strings without specified length
  388. *
  389. * @param cs the string to be compared
  390. * @param ct the string to be compared
  391. *
  392. * @return the result
  393. */
  394. rt_int32_t rt_strcmp(const char *cs, const char *ct)
  395. {
  396. while (*cs && *cs == *ct)
  397. cs++, ct++;
  398. return (*cs - *ct);
  399. }
  400. RTM_EXPORT(rt_strcmp);
  401. /**
  402. * The strnlen() function returns the number of characters in the
  403. * string pointed to by s, excluding the terminating null byte ('\0'),
  404. * but at most maxlen. In doing this, strnlen() looks only at the
  405. * first maxlen characters in the string pointed to by s and never
  406. * beyond s+maxlen.
  407. *
  408. * @param s the string
  409. * @param maxlen the max size
  410. * @return the length of string
  411. */
  412. rt_size_t rt_strnlen(const char *s, rt_ubase_t maxlen)
  413. {
  414. const char *sc;
  415. for (sc = s; *sc != '\0' && sc - s < maxlen; ++sc) /* nothing */
  416. ;
  417. return sc - s;
  418. }
  419. /**
  420. * This function will return the length of a string, which terminate will
  421. * null character.
  422. *
  423. * @param s the string
  424. *
  425. * @return the length of string
  426. */
  427. rt_size_t rt_strlen(const char *s)
  428. {
  429. const char *sc;
  430. for (sc = s; *sc != '\0'; ++sc) /* nothing */
  431. ;
  432. return sc - s;
  433. }
  434. RTM_EXPORT(rt_strlen);
  435. #ifdef RT_USING_HEAP
  436. /**
  437. * This function will duplicate a string.
  438. *
  439. * @param s the string to be duplicated
  440. *
  441. * @return the duplicated string pointer
  442. */
  443. char *rt_strdup(const char *s)
  444. {
  445. rt_size_t len = rt_strlen(s) + 1;
  446. char *tmp = (char *)rt_malloc(len);
  447. if (!tmp)
  448. return RT_NULL;
  449. rt_memcpy(tmp, s, len);
  450. return tmp;
  451. }
  452. RTM_EXPORT(rt_strdup);
  453. #endif
  454. /**
  455. * This function will show the version of rt-thread rtos
  456. */
  457. void rt_show_version(void)
  458. {
  459. rt_kprintf("\n \\ | /\n");
  460. rt_kprintf("- RT - Thread Operating System\n");
  461. rt_kprintf(" / | \\ %d.%d.%d build %s\n",
  462. RT_VERSION, RT_SUBVERSION, RT_REVISION, __DATE__);
  463. rt_kprintf(" 2006 - 2017 Copyright by rt-thread team\n");
  464. }
  465. RTM_EXPORT(rt_show_version);
  466. /* private function */
  467. #define isdigit(c) ((unsigned)((c) - '0') < 10)
  468. rt_inline rt_int32_t divide(rt_int32_t *n, rt_int32_t base)
  469. {
  470. rt_int32_t res;
  471. /* optimized for processor which does not support divide instructions. */
  472. if (base == 10)
  473. {
  474. res = ((rt_uint32_t) * n) % 10U;
  475. *n = ((rt_uint32_t) * n) / 10U;
  476. }
  477. else
  478. {
  479. res = ((rt_uint32_t) * n) % 16U;
  480. *n = ((rt_uint32_t) * n) / 16U;
  481. }
  482. return res;
  483. }
  484. rt_inline int skip_atoi(const char **s)
  485. {
  486. register int i = 0;
  487. while (isdigit(**s))
  488. i = i * 10 + *((*s)++) - '0';
  489. return i;
  490. }
  491. #define ZEROPAD (1 << 0) /* pad with zero */
  492. #define SIGN (1 << 1) /* unsigned/signed long */
  493. #define PLUS (1 << 2) /* show plus */
  494. #define SPACE (1 << 3) /* space if plus */
  495. #define LEFT (1 << 4) /* left justified */
  496. #define SPECIAL (1 << 5) /* 0x */
  497. #define LARGE (1 << 6) /* use 'ABCDEF' instead of 'abcdef' */
  498. #ifdef RT_PRINTF_PRECISION
  499. static char *print_number(char *buf,
  500. char *end,
  501. long num,
  502. int base,
  503. int s,
  504. int precision,
  505. int type)
  506. #else
  507. static char *print_number(char *buf,
  508. char *end,
  509. long num,
  510. int base,
  511. int s,
  512. int type)
  513. #endif
  514. {
  515. char c, sign;
  516. #ifdef RT_PRINTF_LONGLONG
  517. char tmp[32];
  518. #else
  519. char tmp[16];
  520. #endif
  521. const char *digits;
  522. static const char small_digits[] = "0123456789abcdef";
  523. static const char large_digits[] = "0123456789ABCDEF";
  524. register int i;
  525. register int size;
  526. size = s;
  527. digits = (type & LARGE) ? large_digits : small_digits;
  528. if (type & LEFT)
  529. type &= ~ZEROPAD;
  530. c = (type & ZEROPAD) ? '0' : ' ';
  531. /* get sign */
  532. sign = 0;
  533. if (type & SIGN)
  534. {
  535. if (num < 0)
  536. {
  537. sign = '-';
  538. num = -num;
  539. }
  540. else if (type & PLUS)
  541. sign = '+';
  542. else if (type & SPACE)
  543. sign = ' ';
  544. }
  545. #ifdef RT_PRINTF_SPECIAL
  546. if (type & SPECIAL)
  547. {
  548. if (base == 16)
  549. size -= 2;
  550. else if (base == 8)
  551. size--;
  552. }
  553. #endif
  554. i = 0;
  555. if (num == 0)
  556. tmp[i++] = '0';
  557. else
  558. {
  559. while (num != 0)
  560. tmp[i++] = digits[divide(&num, base)];
  561. }
  562. #ifdef RT_PRINTF_PRECISION
  563. if (i > precision)
  564. precision = i;
  565. size -= precision;
  566. #else
  567. size -= i;
  568. #endif
  569. if (!(type & (ZEROPAD | LEFT)))
  570. {
  571. if ((sign) && (size > 0))
  572. size--;
  573. while (size-- > 0)
  574. {
  575. if (buf <= end)
  576. *buf = ' ';
  577. ++ buf;
  578. }
  579. }
  580. if (sign)
  581. {
  582. if (buf <= end)
  583. {
  584. *buf = sign;
  585. -- size;
  586. }
  587. ++ buf;
  588. }
  589. #ifdef RT_PRINTF_SPECIAL
  590. if (type & SPECIAL)
  591. {
  592. if (base == 8)
  593. {
  594. if (buf <= end)
  595. *buf = '0';
  596. ++ buf;
  597. }
  598. else if (base == 16)
  599. {
  600. if (buf <= end)
  601. *buf = '0';
  602. ++ buf;
  603. if (buf <= end)
  604. {
  605. *buf = type & LARGE ? 'X' : 'x';
  606. }
  607. ++ buf;
  608. }
  609. }
  610. #endif
  611. /* no align to the left */
  612. if (!(type & LEFT))
  613. {
  614. while (size-- > 0)
  615. {
  616. if (buf <= end)
  617. *buf = c;
  618. ++ buf;
  619. }
  620. }
  621. #ifdef RT_PRINTF_PRECISION
  622. while (i < precision--)
  623. {
  624. if (buf <= end)
  625. *buf = '0';
  626. ++ buf;
  627. }
  628. #endif
  629. /* put number in the temporary buffer */
  630. while (i-- > 0)
  631. {
  632. if (buf <= end)
  633. *buf = tmp[i];
  634. ++ buf;
  635. }
  636. while (size-- > 0)
  637. {
  638. if (buf <= end)
  639. *buf = ' ';
  640. ++ buf;
  641. }
  642. return buf;
  643. }
  644. rt_int32_t rt_vsnprintf(char *buf,
  645. rt_size_t size,
  646. const char *fmt,
  647. va_list args)
  648. {
  649. #ifdef RT_PRINTF_LONGLONG
  650. unsigned long long num;
  651. #else
  652. rt_uint32_t num;
  653. #endif
  654. int i, len;
  655. char *str, *end, c;
  656. const char *s;
  657. rt_uint8_t base; /* the base of number */
  658. rt_uint8_t flags; /* flags to print number */
  659. rt_uint8_t qualifier; /* 'h', 'l', or 'L' for integer fields */
  660. rt_int32_t field_width; /* width of output field */
  661. #ifdef RT_PRINTF_PRECISION
  662. int precision; /* min. # of digits for integers and max for a string */
  663. #endif
  664. str = buf;
  665. end = buf + size - 1;
  666. /* Make sure end is always >= buf */
  667. if (end < buf)
  668. {
  669. end = ((char *) - 1);
  670. size = end - buf;
  671. }
  672. for (; *fmt ; ++fmt)
  673. {
  674. if (*fmt != '%')
  675. {
  676. if (str <= end)
  677. *str = *fmt;
  678. ++ str;
  679. continue;
  680. }
  681. /* process flags */
  682. flags = 0;
  683. while (1)
  684. {
  685. /* skips the first '%' also */
  686. ++ fmt;
  687. if (*fmt == '-') flags |= LEFT;
  688. else if (*fmt == '+') flags |= PLUS;
  689. else if (*fmt == ' ') flags |= SPACE;
  690. else if (*fmt == '#') flags |= SPECIAL;
  691. else if (*fmt == '0') flags |= ZEROPAD;
  692. else break;
  693. }
  694. /* get field width */
  695. field_width = -1;
  696. if (isdigit(*fmt)) field_width = skip_atoi(&fmt);
  697. else if (*fmt == '*')
  698. {
  699. ++ fmt;
  700. /* it's the next argument */
  701. field_width = va_arg(args, int);
  702. if (field_width < 0)
  703. {
  704. field_width = -field_width;
  705. flags |= LEFT;
  706. }
  707. }
  708. #ifdef RT_PRINTF_PRECISION
  709. /* get the precision */
  710. precision = -1;
  711. if (*fmt == '.')
  712. {
  713. ++ fmt;
  714. if (isdigit(*fmt)) precision = skip_atoi(&fmt);
  715. else if (*fmt == '*')
  716. {
  717. ++ fmt;
  718. /* it's the next argument */
  719. precision = va_arg(args, int);
  720. }
  721. if (precision < 0) precision = 0;
  722. }
  723. #endif
  724. /* get the conversion qualifier */
  725. qualifier = 0;
  726. #ifdef RT_PRINTF_LONGLONG
  727. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L')
  728. #else
  729. if (*fmt == 'h' || *fmt == 'l')
  730. #endif
  731. {
  732. qualifier = *fmt;
  733. ++ fmt;
  734. #ifdef RT_PRINTF_LONGLONG
  735. if (qualifier == 'l' && *fmt == 'l')
  736. {
  737. qualifier = 'L';
  738. ++ fmt;
  739. }
  740. #endif
  741. }
  742. /* the default base */
  743. base = 10;
  744. switch (*fmt)
  745. {
  746. case 'c':
  747. if (!(flags & LEFT))
  748. {
  749. while (--field_width > 0)
  750. {
  751. if (str <= end) *str = ' ';
  752. ++ str;
  753. }
  754. }
  755. /* get character */
  756. c = (rt_uint8_t)va_arg(args, int);
  757. if (str <= end) *str = c;
  758. ++ str;
  759. /* put width */
  760. while (--field_width > 0)
  761. {
  762. if (str <= end) *str = ' ';
  763. ++ str;
  764. }
  765. continue;
  766. case 's':
  767. s = va_arg(args, char *);
  768. if (!s) s = "(NULL)";
  769. len = rt_strlen(s);
  770. #ifdef RT_PRINTF_PRECISION
  771. if (precision > 0 && len > precision) len = precision;
  772. #endif
  773. if (!(flags & LEFT))
  774. {
  775. while (len < field_width--)
  776. {
  777. if (str <= end) *str = ' ';
  778. ++ str;
  779. }
  780. }
  781. for (i = 0; i < len; ++i)
  782. {
  783. if (str <= end) *str = *s;
  784. ++ str;
  785. ++ s;
  786. }
  787. while (len < field_width--)
  788. {
  789. if (str <= end) *str = ' ';
  790. ++ str;
  791. }
  792. continue;
  793. case 'p':
  794. if (field_width == -1)
  795. {
  796. field_width = sizeof(void *) << 1;
  797. flags |= ZEROPAD;
  798. }
  799. #ifdef RT_PRINTF_PRECISION
  800. str = print_number(str, end,
  801. (long)va_arg(args, void *),
  802. 16, field_width, precision, flags);
  803. #else
  804. str = print_number(str, end,
  805. (long)va_arg(args, void *),
  806. 16, field_width, flags);
  807. #endif
  808. continue;
  809. case '%':
  810. if (str <= end) *str = '%';
  811. ++ str;
  812. continue;
  813. /* integer number formats - set up the flags and "break" */
  814. case 'o':
  815. base = 8;
  816. break;
  817. case 'X':
  818. flags |= LARGE;
  819. case 'x':
  820. base = 16;
  821. break;
  822. case 'd':
  823. case 'i':
  824. flags |= SIGN;
  825. case 'u':
  826. break;
  827. default:
  828. if (str <= end) *str = '%';
  829. ++ str;
  830. if (*fmt)
  831. {
  832. if (str <= end) *str = *fmt;
  833. ++ str;
  834. }
  835. else
  836. {
  837. -- fmt;
  838. }
  839. continue;
  840. }
  841. #ifdef RT_PRINTF_LONGLONG
  842. if (qualifier == 'L') num = va_arg(args, long long);
  843. else if (qualifier == 'l')
  844. #else
  845. if (qualifier == 'l')
  846. #endif
  847. {
  848. num = va_arg(args, rt_uint32_t);
  849. if (flags & SIGN) num = (rt_int32_t)num;
  850. }
  851. else if (qualifier == 'h')
  852. {
  853. num = (rt_uint16_t)va_arg(args, rt_int32_t);
  854. if (flags & SIGN) num = (rt_int16_t)num;
  855. }
  856. else
  857. {
  858. num = va_arg(args, rt_uint32_t);
  859. if (flags & SIGN) num = (rt_int32_t)num;
  860. }
  861. #ifdef RT_PRINTF_PRECISION
  862. str = print_number(str, end, num, base, field_width, precision, flags);
  863. #else
  864. str = print_number(str, end, num, base, field_width, flags);
  865. #endif
  866. }
  867. if (str <= end) *str = '\0';
  868. else *end = '\0';
  869. /* the trailing null byte doesn't count towards the total
  870. * ++str;
  871. */
  872. return str - buf;
  873. }
  874. RTM_EXPORT(rt_vsnprintf);
  875. /**
  876. * This function will fill a formatted string to buffer
  877. *
  878. * @param buf the buffer to save formatted string
  879. * @param size the size of buffer
  880. * @param fmt the format
  881. */
  882. rt_int32_t rt_snprintf(char *buf, rt_size_t size, const char *fmt, ...)
  883. {
  884. rt_int32_t n;
  885. va_list args;
  886. va_start(args, fmt);
  887. n = rt_vsnprintf(buf, size, fmt, args);
  888. va_end(args);
  889. return n;
  890. }
  891. RTM_EXPORT(rt_snprintf);
  892. /**
  893. * This function will fill a formatted string to buffer
  894. *
  895. * @param buf the buffer to save formatted string
  896. * @param arg_ptr the arg_ptr
  897. * @param format the format
  898. */
  899. rt_int32_t rt_vsprintf(char *buf, const char *format, va_list arg_ptr)
  900. {
  901. return rt_vsnprintf(buf, (rt_size_t) - 1, format, arg_ptr);
  902. }
  903. RTM_EXPORT(rt_vsprintf);
  904. /**
  905. * This function will fill a formatted string to buffer
  906. *
  907. * @param buf the buffer to save formatted string
  908. * @param format the format
  909. */
  910. rt_int32_t rt_sprintf(char *buf, const char *format, ...)
  911. {
  912. rt_int32_t n;
  913. va_list arg_ptr;
  914. va_start(arg_ptr, format);
  915. n = rt_vsprintf(buf, format, arg_ptr);
  916. va_end(arg_ptr);
  917. return n;
  918. }
  919. RTM_EXPORT(rt_sprintf);
  920. #ifdef RT_USING_CONSOLE
  921. #ifdef RT_USING_DEVICE
  922. /**
  923. * This function returns the device using in console.
  924. *
  925. * @return the device using in console or RT_NULL
  926. */
  927. rt_device_t rt_console_get_device(void)
  928. {
  929. return _console_device;
  930. }
  931. RTM_EXPORT(rt_console_get_device);
  932. /**
  933. * This function will set a device as console device.
  934. * After set a device to console, all output of rt_kprintf will be
  935. * redirected to this new device.
  936. *
  937. * @param name the name of new console device
  938. *
  939. * @return the old console device handler
  940. */
  941. rt_device_t rt_console_set_device(const char *name)
  942. {
  943. rt_device_t new, old;
  944. /* save old device */
  945. old = _console_device;
  946. /* find new console device */
  947. new = rt_device_find(name);
  948. if (new != RT_NULL)
  949. {
  950. if (_console_device != RT_NULL)
  951. {
  952. /* close old console device */
  953. rt_device_close(_console_device);
  954. }
  955. /* set new console device */
  956. rt_device_open(new, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_STREAM);
  957. _console_device = new;
  958. }
  959. return old;
  960. }
  961. RTM_EXPORT(rt_console_set_device);
  962. #endif
  963. RT_WEAK void rt_hw_console_output(const char *str)
  964. {
  965. /* empty console output */
  966. }
  967. RTM_EXPORT(rt_hw_console_output);
  968. /**
  969. * This function will put string to the console.
  970. *
  971. * @param str the string output to the console.
  972. */
  973. void rt_kputs(const char *str)
  974. {
  975. if (!str) return;
  976. #ifdef RT_USING_DEVICE
  977. if (_console_device == RT_NULL)
  978. {
  979. rt_hw_console_output(str);
  980. }
  981. else
  982. {
  983. rt_uint16_t old_flag = _console_device->open_flag;
  984. _console_device->open_flag |= RT_DEVICE_FLAG_STREAM;
  985. rt_device_write(_console_device, 0, str, rt_strlen(str));
  986. _console_device->open_flag = old_flag;
  987. }
  988. #else
  989. rt_hw_console_output(str);
  990. #endif
  991. }
  992. /**
  993. * This function will print a formatted string on system console
  994. *
  995. * @param fmt the format
  996. */
  997. void rt_kprintf(const char *fmt, ...)
  998. {
  999. va_list args;
  1000. rt_size_t length;
  1001. static char rt_log_buf[RT_CONSOLEBUF_SIZE];
  1002. va_start(args, fmt);
  1003. /* the return value of vsnprintf is the number of bytes that would be
  1004. * written to buffer had if the size of the buffer been sufficiently
  1005. * large excluding the terminating null byte. If the output string
  1006. * would be larger than the rt_log_buf, we have to adjust the output
  1007. * length. */
  1008. length = rt_vsnprintf(rt_log_buf, sizeof(rt_log_buf) - 1, fmt, args);
  1009. if (length > RT_CONSOLEBUF_SIZE - 1)
  1010. length = RT_CONSOLEBUF_SIZE - 1;
  1011. #ifdef RT_USING_DEVICE
  1012. if (_console_device == RT_NULL)
  1013. {
  1014. rt_hw_console_output(rt_log_buf);
  1015. }
  1016. else
  1017. {
  1018. rt_uint16_t old_flag = _console_device->open_flag;
  1019. _console_device->open_flag |= RT_DEVICE_FLAG_STREAM;
  1020. rt_device_write(_console_device, 0, rt_log_buf, length);
  1021. _console_device->open_flag = old_flag;
  1022. }
  1023. #else
  1024. rt_hw_console_output(rt_log_buf);
  1025. #endif
  1026. va_end(args);
  1027. }
  1028. RTM_EXPORT(rt_kprintf);
  1029. #endif
  1030. #ifdef RT_USING_HEAP
  1031. /**
  1032. * This function allocates a memory block, which address is aligned to the
  1033. * specified alignment size.
  1034. *
  1035. * @param size the allocated memory block size
  1036. * @param align the alignment size
  1037. *
  1038. * @return the allocated memory block on successful, otherwise returns RT_NULL
  1039. */
  1040. void *rt_malloc_align(rt_size_t size, rt_size_t align)
  1041. {
  1042. void *align_ptr;
  1043. void *ptr;
  1044. rt_size_t align_size;
  1045. /* align the alignment size to 4 byte */
  1046. align = ((align + 0x03) & ~0x03);
  1047. /* get total aligned size */
  1048. align_size = ((size + 0x03) & ~0x03) + align;
  1049. /* allocate memory block from heap */
  1050. ptr = rt_malloc(align_size);
  1051. if (ptr != RT_NULL)
  1052. {
  1053. /* the allocated memory block is aligned */
  1054. if (((rt_uint32_t)ptr & (align - 1)) == 0)
  1055. {
  1056. align_ptr = (void *)((rt_uint32_t)ptr + align);
  1057. }
  1058. else
  1059. {
  1060. align_ptr = (void *)(((rt_uint32_t)ptr + (align - 1)) & ~(align - 1));
  1061. }
  1062. /* set the pointer before alignment pointer to the real pointer */
  1063. *((rt_uint32_t *)((rt_uint32_t)align_ptr - sizeof(void *))) = (rt_uint32_t)ptr;
  1064. ptr = align_ptr;
  1065. }
  1066. return ptr;
  1067. }
  1068. RTM_EXPORT(rt_malloc_align);
  1069. /**
  1070. * This function release the memory block, which is allocated by
  1071. * rt_malloc_align function and address is aligned.
  1072. *
  1073. * @param ptr the memory block pointer
  1074. */
  1075. void rt_free_align(void *ptr)
  1076. {
  1077. void *real_ptr;
  1078. real_ptr = (void *) * (rt_uint32_t *)((rt_uint32_t)ptr - sizeof(void *));
  1079. rt_free(real_ptr);
  1080. }
  1081. RTM_EXPORT(rt_free_align);
  1082. #endif
  1083. #ifndef RT_USING_CPU_FFS
  1084. const rt_uint8_t __lowest_bit_bitmap[] =
  1085. {
  1086. /* 00 */ 0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1087. /* 10 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1088. /* 20 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1089. /* 30 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1090. /* 40 */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1091. /* 50 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1092. /* 60 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1093. /* 70 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1094. /* 80 */ 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1095. /* 90 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1096. /* A0 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1097. /* B0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1098. /* C0 */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1099. /* D0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1100. /* E0 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1101. /* F0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
  1102. };
  1103. /**
  1104. * This function finds the first bit set (beginning with the least significant bit)
  1105. * in value and return the index of that bit.
  1106. *
  1107. * Bits are numbered starting at 1 (the least significant bit). A return value of
  1108. * zero from any of these functions means that the argument was zero.
  1109. *
  1110. * @return return the index of the first bit set. If value is 0, then this function
  1111. * shall return 0.
  1112. */
  1113. int __rt_ffs(int value)
  1114. {
  1115. if (value == 0) return 0;
  1116. if (value & 0xff)
  1117. return __lowest_bit_bitmap[value & 0xff] + 1;
  1118. if (value & 0xff00)
  1119. return __lowest_bit_bitmap[(value & 0xff00) >> 8] + 9;
  1120. if (value & 0xff0000)
  1121. return __lowest_bit_bitmap[(value & 0xff0000) >> 16] + 17;
  1122. return __lowest_bit_bitmap[(value & 0xff000000) >> 24] + 25;
  1123. }
  1124. #endif
  1125. #ifdef RT_DEBUG
  1126. /* RT_ASSERT(EX)'s hook */
  1127. void (*rt_assert_hook)(const char *ex, const char *func, rt_size_t line);
  1128. /**
  1129. * This function will set a hook function to RT_ASSERT(EX). It will run when the expression is false.
  1130. *
  1131. * @param hook the hook function
  1132. */
  1133. void rt_assert_set_hook(void (*hook)(const char *ex, const char *func, rt_size_t line))
  1134. {
  1135. rt_assert_hook = hook;
  1136. }
  1137. /**
  1138. * The RT_ASSERT function.
  1139. *
  1140. * @param ex the assertion condition string
  1141. * @param func the function name when assertion.
  1142. * @param line the file line number when assertion.
  1143. */
  1144. void rt_assert_handler(const char *ex_string, const char *func, rt_size_t line)
  1145. {
  1146. volatile char dummy = 0;
  1147. if (rt_assert_hook == RT_NULL)
  1148. {
  1149. #ifdef RT_USING_MODULE
  1150. if (rt_module_self() != RT_NULL)
  1151. {
  1152. /* unload assertion module */
  1153. rt_module_unload(rt_module_self());
  1154. /* re-schedule */
  1155. rt_schedule();
  1156. }
  1157. else
  1158. #endif
  1159. {
  1160. rt_kprintf("(%s) assertion failed at function:%s, line number:%d \n", ex_string, func, line);
  1161. while (dummy == 0);
  1162. }
  1163. }
  1164. else
  1165. {
  1166. rt_assert_hook(ex_string, func, line);
  1167. }
  1168. }
  1169. RTM_EXPORT(rt_assert_handler);
  1170. #endif /* RT_DEBUG */
  1171. #if !defined (RT_USING_NEWLIB) && defined (RT_USING_MINILIBC) && defined (__GNUC__)
  1172. #include <sys/types.h>
  1173. void *memcpy(void *dest, const void *src, size_t n) __attribute__((weak, alias("rt_memcpy")));
  1174. void *memset(void *s, int c, size_t n) __attribute__((weak, alias("rt_memset")));
  1175. void *memmove(void *dest, const void *src, size_t n) __attribute__((weak, alias("rt_memmove")));
  1176. int memcmp(const void *s1, const void *s2, size_t n) __attribute__((weak, alias("rt_memcmp")));
  1177. size_t strlen(const char *s) __attribute__((weak, alias("rt_strlen")));
  1178. char *strstr(const char *s1, const char *s2) __attribute__((weak, alias("rt_strstr")));
  1179. int strcasecmp(const char *a, const char *b) __attribute__((weak, alias("rt_strcasecmp")));
  1180. char *strncpy(char *dest, const char *src, size_t n) __attribute__((weak, alias("rt_strncpy")));
  1181. int strncmp(const char *cs, const char *ct, size_t count) __attribute__((weak, alias("rt_strncmp")));
  1182. #ifdef RT_USING_HEAP
  1183. char *strdup(const char *s) __attribute__((weak, alias("rt_strdup")));
  1184. #endif
  1185. int sprintf(char *buf, const char *format, ...) __attribute__((weak, alias("rt_sprintf")));
  1186. int snprintf(char *buf, rt_size_t size, const char *fmt, ...) __attribute__((weak, alias("rt_snprintf")));
  1187. int vsprintf(char *buf, const char *format, va_list arg_ptr) __attribute__((weak, alias("rt_vsprintf")));
  1188. #endif
  1189. /**@}*/