system.c 8.0 KB

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