system.c 8.1 KB

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