system.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. File Name : system.c
  3. Author : Yichip
  4. Version : V1.0
  5. Date : 2019/11/13
  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 printport_CB printportcb =
  20. {
  21. .print_port = PRINTPORT,
  22. .print_rxio = PRINTRXIO,
  23. .print_txio = PRINTTXIO
  24. };
  25. void printport_init(void)
  26. {
  27. USART_InitTypeDef USART_InitStruct ;
  28. USART_InitStruct.USART_BaudRate = UARTE_BAUDRATE_BAUDRATE_Baud921600;
  29. USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  30. USART_InitStruct.USART_WordLength = USART_WordLength_8b;
  31. USART_InitStruct.USART_StopBits = USART_StopBits_1;
  32. USART_InitStruct.USART_Mode = USART_Mode_duplex;
  33. USART_InitStruct.USART_Parity = USART_Parity_Even ;
  34. USART_InitStruct.USART_TXLen = uart_DMA_buf_len;
  35. USART_InitStruct.USART_RXLen = uart_DMA_buf_len;
  36. if(UARTA == printportcb.print_port){
  37. GPIO_SetGpioMultFunction((GPIO_NUM)printportcb.print_txio,GPCFG_UART_TXD);
  38. GPIO_SetGpioMultFunction((GPIO_NUM)printportcb.print_rxio,GPCFG_UART_RXD);
  39. }else if (UARTB == printportcb.print_port){
  40. GPIO_SetGpioMultFunction((GPIO_NUM)printportcb.print_txio,GPCFG_UARTB_TXD);
  41. GPIO_SetGpioMultFunction((GPIO_NUM)printportcb.print_rxio,GPCFG_UARTB_RXD);
  42. }
  43. USART_Init(printportcb.print_port,&USART_InitStruct);
  44. }
  45. void setprintport(USART_TypeDef UARTx)
  46. {
  47. printportcb.print_port = UARTx;
  48. }
  49. void setprintportcb(printport_CB *printportx)
  50. {
  51. printportcb.print_port = printportx->print_port;
  52. printportcb.print_txio = printportx->print_txio;
  53. printportcb.print_rxio = printportx->print_rxio;
  54. }
  55. void printfsend(uint8_t* buf, int len)
  56. {
  57. USART_SendDataFromBuff(printportcb.print_port,buf,len);
  58. }
  59. void MyPrintf(char *format, ...)
  60. {
  61. uint32_t ulIdx, ulValue, ulPos, ulCount, ulBase, ulNeg;
  62. char *pcStr, pcBuf[16], cFill;
  63. char HexFormat;
  64. va_list vaArgP;
  65. static const int8_t* const g_pcHex1 = "0123456789abcdef";
  66. static const int8_t* const g_pcHex2 = "0123456789ABCDEF";
  67. va_start(vaArgP, format);
  68. while(*format)
  69. {
  70. // Find the first non-% character, or the end of the string.
  71. for(ulIdx = 0; (format[ulIdx] != '%') && (format[ulIdx] != '\0');ulIdx++)
  72. {}
  73. // Write this portion of the string.
  74. if(ulIdx>0)
  75. {
  76. printfsend((uint8_t*)format, ulIdx);
  77. }
  78. format += ulIdx;
  79. if(*format == '%')
  80. {
  81. format++;
  82. // Set the digit count to zero, and the fill character to space
  83. // (i.e. to the defaults).
  84. ulCount = 0;
  85. cFill = ' ';
  86. again:
  87. switch(*format++)
  88. {
  89. case '0':
  90. case '1':
  91. case '2':
  92. case '3':
  93. case '4':
  94. case '5':
  95. case '6':
  96. case '7':
  97. case '8':
  98. case '9':
  99. {
  100. if((format[-1] == '0') && (ulCount == 0))
  101. {
  102. cFill = '0';
  103. }
  104. ulCount *= 10;
  105. ulCount += format[-1] - '0';
  106. goto again;
  107. }
  108. case 'c':
  109. {
  110. ulValue = va_arg(vaArgP, unsigned long);
  111. printfsend((uint8_t*)&ulValue, 1);
  112. break;
  113. }
  114. case 'd':
  115. {
  116. ulValue = va_arg(vaArgP, unsigned long);
  117. ulPos = 0;
  118. if((long)ulValue < 0)
  119. {
  120. ulValue = -(long)ulValue;
  121. ulNeg = 1;
  122. }
  123. else
  124. {
  125. ulNeg = 0;
  126. }
  127. ulBase = 10;
  128. goto convert;
  129. }
  130. case 's':
  131. {
  132. pcStr = va_arg(vaArgP, char *);
  133. for(ulIdx = 0; pcStr[ulIdx] != '\0'; ulIdx++)
  134. {}
  135. printfsend((uint8_t*)pcStr, ulIdx);
  136. if(ulCount > ulIdx)
  137. {
  138. ulCount -= ulIdx;
  139. while(ulCount--)
  140. {
  141. printfsend(" ", 1);
  142. }
  143. }
  144. break;
  145. }
  146. case 'u':
  147. {
  148. ulValue = va_arg(vaArgP, unsigned long);
  149. ulPos = 0;
  150. ulBase = 10;
  151. ulNeg = 0;
  152. goto convert;
  153. }
  154. case 'X':
  155. {
  156. ulValue = va_arg(vaArgP, unsigned long);
  157. ulPos = 0;
  158. ulBase = 16;
  159. ulNeg = 0;
  160. HexFormat='X';
  161. goto convert;
  162. }
  163. case 'x':
  164. case 'p':
  165. {
  166. ulValue = va_arg(vaArgP, unsigned long);
  167. ulPos = 0;
  168. ulBase = 16;
  169. ulNeg = 0;
  170. HexFormat='x';
  171. convert:
  172. for(ulIdx = 1;
  173. (((ulIdx * ulBase) <= ulValue) &&
  174. (((ulIdx * ulBase) / ulBase) == ulIdx));
  175. ulIdx *= ulBase, ulCount--)
  176. {
  177. }
  178. if(ulNeg)
  179. {
  180. ulCount--;
  181. }
  182. if(ulNeg && (cFill == '0'))
  183. {
  184. pcBuf[ulPos++] = '-';
  185. ulNeg = 0;
  186. }
  187. if((ulCount > 1) && (ulCount < 16))
  188. {
  189. for(ulCount--; ulCount; ulCount--)
  190. {
  191. pcBuf[ulPos++] = cFill;
  192. }
  193. }
  194. if(ulNeg)
  195. {
  196. pcBuf[ulPos++] = '-';
  197. }
  198. for(; ulIdx; ulIdx /= ulBase)
  199. {
  200. if(HexFormat=='x') pcBuf[ulPos++] = g_pcHex1[(ulValue / ulIdx) % ulBase];//x
  201. else pcBuf[ulPos++] = g_pcHex2[(ulValue / ulIdx) % ulBase];//X
  202. }
  203. printfsend((uint8_t*)pcBuf, ulPos);
  204. break;
  205. }
  206. case '%':
  207. {
  208. printfsend((uint8_t*)format - 1, 1);
  209. break;
  210. }
  211. default:
  212. {
  213. printfsend("ERROR", 5);
  214. break;
  215. }
  216. }//switch
  217. }//if
  218. }//while
  219. va_end(vaArgP);
  220. }
  221. static uint8_t is_open_log_print = 0;
  222. void open_log_print(uint8_t isopen)
  223. {
  224. is_open_log_print = isopen;
  225. }
  226. void log_print(char *format, ...)
  227. {
  228. if(is_open_log_print)
  229. {
  230. MyPrintf(format);
  231. }
  232. }
  233. void log2_print(char *format, ...)
  234. {
  235. if(is_open_log_print>1)
  236. {
  237. MyPrintf(format);
  238. }
  239. }
  240. void _assert_handler(const char* file, int line,const char* func)
  241. {
  242. while(1);
  243. }