1
0

kservice.c 29 KB

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