system.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. File Name : system.c
  3. Author : Yichip
  4. Version : V1.0
  5. Date : 2019/12/4
  6. Description : none.
  7. */
  8. #include <stdarg.h>
  9. #include "system.h"
  10. //*****************************************************************************
  11. //
  12. //! A simple MyPrintf function supporting \%c, \%d, \%p, \%s, \%u,\%x, and \%X.
  13. //!
  14. //! \param format is the format string.
  15. //! \param ... are the optional arguments, which depend on the contents of the
  16. //! \return None.
  17. //
  18. //*****************************************************************************
  19. static const int8_t *const g_pcHex1 = "0123456789abcdef";
  20. static const int8_t *const g_pcHex2 = "0123456789ABCDEF";
  21. void printfsend(UART_TypeDef UARTx, uint8_t *buf, int len)
  22. {
  23. uint8_t printbuf[256];
  24. for (int i = 0; i < len; i++)
  25. {
  26. printbuf[i] = buf[i];
  27. }
  28. UART_SendBuf(UARTx, printbuf, len);
  29. }
  30. void MyPrintf(char *format, ...)
  31. {
  32. uint32_t ulIdx, ulValue, ulPos, ulCount, ulBase, ulNeg;
  33. int8_t *pcStr, pcBuf[16], cFill;
  34. char HexFormat;
  35. va_list vaArgP;
  36. va_start(vaArgP, format);
  37. while (*format)
  38. {
  39. // Find the first non-% character, or the end of the string.
  40. for (ulIdx = 0; (format[ulIdx] != '%') && (format[ulIdx] != '\0'); ulIdx++)
  41. {
  42. }
  43. // Write this portion of the string.
  44. if (ulIdx > 0)
  45. {
  46. printfsend(UART0, (uint8_t *)format, ulIdx);
  47. }
  48. format += ulIdx;
  49. if (*format == '%')
  50. {
  51. format++;
  52. // Set the digit count to zero, and the fill character to space
  53. // (i.e. to the defaults).
  54. ulCount = 0;
  55. cFill = ' ';
  56. again:
  57. switch (*format++)
  58. {
  59. case '0':
  60. case '1':
  61. case '2':
  62. case '3':
  63. case '4':
  64. case '5':
  65. case '6':
  66. case '7':
  67. case '8':
  68. case '9':
  69. {
  70. if ((format[-1] == '0') && (ulCount == 0))
  71. {
  72. cFill = '0';
  73. }
  74. ulCount *= 10;
  75. ulCount += format[-1] - '0';
  76. goto again;
  77. }
  78. case 'c':
  79. {
  80. ulValue = va_arg(vaArgP, unsigned long);
  81. printfsend(UART0, (uint8_t *)&ulValue, 1);
  82. break;
  83. }
  84. case 'd':
  85. {
  86. ulValue = va_arg(vaArgP, unsigned long);
  87. ulPos = 0;
  88. if ((long)ulValue < 0)
  89. {
  90. ulValue = -(long)ulValue;
  91. ulNeg = 1;
  92. }
  93. else
  94. {
  95. ulNeg = 0;
  96. }
  97. ulBase = 10;
  98. goto convert;
  99. }
  100. case 's':
  101. {
  102. pcStr = (int8_t *)va_arg(vaArgP, char *);
  103. for (ulIdx = 0; pcStr[ulIdx] != '\0'; ulIdx++)
  104. {
  105. }
  106. printfsend(UART0, (uint8_t *)pcStr, ulIdx);
  107. if (ulCount > ulIdx)
  108. {
  109. ulCount -= ulIdx;
  110. while (ulCount--)
  111. {
  112. printfsend(UART0, (uint8_t *)" ", 1);
  113. }
  114. }
  115. break;
  116. }
  117. case 'u':
  118. {
  119. ulValue = va_arg(vaArgP, unsigned long);
  120. ulPos = 0;
  121. ulBase = 10;
  122. ulNeg = 0;
  123. goto convert;
  124. }
  125. case 'X':
  126. {
  127. ulValue = va_arg(vaArgP, unsigned long);
  128. ulPos = 0;
  129. ulBase = 16;
  130. ulNeg = 0;
  131. HexFormat = 'X';
  132. goto convert;
  133. }
  134. case 'x':
  135. case 'p':
  136. {
  137. ulValue = va_arg(vaArgP, unsigned long);
  138. ulPos = 0;
  139. ulBase = 16;
  140. ulNeg = 0;
  141. HexFormat = 'x';
  142. convert:
  143. for (ulIdx = 1;
  144. (((ulIdx * ulBase) <= ulValue) &&
  145. (((ulIdx * ulBase) / ulBase) == ulIdx));
  146. ulIdx *= ulBase, ulCount--)
  147. {
  148. }
  149. if (ulNeg)
  150. {
  151. ulCount--;
  152. }
  153. if (ulNeg && (cFill == '0'))
  154. {
  155. pcBuf[ulPos++] = '-';
  156. ulNeg = 0;
  157. }
  158. if ((ulCount > 1) && (ulCount < 16))
  159. {
  160. for (ulCount--; ulCount; ulCount--)
  161. {
  162. pcBuf[ulPos++] = cFill;
  163. }
  164. }
  165. if (ulNeg)
  166. {
  167. pcBuf[ulPos++] = '-';
  168. }
  169. for (; ulIdx; ulIdx /= ulBase)
  170. {
  171. if (HexFormat == 'x')
  172. pcBuf[ulPos++] = g_pcHex1[(ulValue / ulIdx) % ulBase]; //x
  173. else
  174. pcBuf[ulPos++] = g_pcHex2[(ulValue / ulIdx) % ulBase]; //X
  175. }
  176. printfsend(UART0, (uint8_t *)pcBuf, ulPos);
  177. break;
  178. }
  179. case '%':
  180. {
  181. printfsend(UART0, (uint8_t *)format - 1, 1);
  182. break;
  183. }
  184. default:
  185. {
  186. printfsend(UART0, (uint8_t *)"ERROR", 5);
  187. break;
  188. }
  189. } //switch
  190. } //if
  191. } //while
  192. va_end(vaArgP);
  193. }
  194. void printv(uint8_t *buf, uint32_t len, uint8_t *s)
  195. {
  196. uint32_t i = 0;
  197. uint32_t n = 0;
  198. MyPrintf("\r\n %s:", s);
  199. for (i = 0; i < len; i++)
  200. {
  201. if (i % 16 == 0)
  202. {
  203. MyPrintf("\r\n%08x:", n);
  204. n += 16;
  205. }
  206. MyPrintf("%02x ", buf[i]);
  207. }
  208. }
  209. void _assert_handler(const char *file, int line, const char *func)
  210. {
  211. #if defined(SDK_DEBUG)
  212. MyPrintf("Assert trigger at file: %s line:%d func: %s\n ", file, line, func);
  213. #endif
  214. while (1);
  215. }