kservice.c 30 KB

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