Drv_debug.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <stdarg.h>
  4. #include "yc11xx_uart.h"
  5. #include "Drv_debug.h"
  6. #include "yc11xx_gpio.h"
  7. #ifdef DEF_PRINTHEX
  8. unsigned char uarttxbuf[DEBUG_PROTOCL_TOTAL_ALLOW_LENGTH], uartrxbuf[2];
  9. unsigned char *txptr;
  10. void uart_txBuffer(uint8_t* send_buf, uint32_t length)
  11. {
  12. uint32_t i;
  13. for (i = 0; i < length; i++)
  14. {
  15. uart_txChar(*(send_buf+i));
  16. }
  17. }
  18. void uart_txChar(unsigned char c)
  19. {
  20. while(HREAD(CORE_UARTB_TX_ITEMS) >= sizeof(uarttxbuf) - 10);
  21. *txptr = c;
  22. if(++txptr > uarttxbuf + sizeof(uarttxbuf) - 1) txptr = uarttxbuf;
  23. HWRITEW(CORE_UARTB_TX_WPTR, txptr);
  24. }
  25. void init_debugUart(void)
  26. {
  27. HWRITE(CORE_CONFIG, 1);
  28. HWCOR(CORE_CLKOFF + 1, 0x80);
  29. //gpio config
  30. HWRITE(CORE_GPIO_CONF + 6, GPCFG_UARTB_TXD); //TX:GPIO6
  31. HWRITEW(CORE_UARTB_BAUD, 0x8034);// 921600
  32. //HWRITEW(CORE_UARTB_BAUD, 0x81bf); //115200
  33. HWRITEW(CORE_UARTB_TX_SADDR, &uarttxbuf);//write start addr
  34. HWRITEW(CORE_UARTB_TX_WPTR, &uarttxbuf); //init write pointer
  35. HWRITEW(CORE_UARTB_TX_EADDR, (int)&uarttxbuf + sizeof(uarttxbuf) - 1);//write end addr
  36. HWRITEW(CORE_UARTB_RX_SADDR, &uartrxbuf); //read start addr
  37. HWRITEW(CORE_UARTB_RX_RPTR, &uartrxbuf); //init read pointer
  38. HWRITEW(CORE_UARTB_RX_EADDR, (int)&uartrxbuf + sizeof(uartrxbuf) - 1); //read end addr
  39. HWRITE(CORE_UARTB_CTRL, 0x81);
  40. txptr = uarttxbuf;
  41. // Init other function
  42. init_print_log();
  43. return;
  44. }
  45. void debugUart_pollTx(void)
  46. {
  47. // Type_Log print
  48. print_log_to_uart();
  49. return;
  50. }
  51. void write_long_value(uint8_t* addr, uint32_t value)
  52. {
  53. *(addr) = (value >> 0);
  54. *(addr + 1) = (value >> 8);
  55. *(addr + 2) = (value >> 16);
  56. *(addr + 3) = (value >> 24);
  57. }
  58. void print_round_buffer(uint8_t index, uint32_t start_addr, uint32_t end_addr, uint32_t write_addr, uint32_t read_addr)
  59. {
  60. uint8_t uart_send_buf[DEBUG_PROTOCL_TOTAL_ALLOW_LENGTH];
  61. uint8_t uart_send_length = 0;
  62. uint8_t offset = 0;
  63. *(uart_send_buf + offset + DEBUG_PROTOCL_PAYLOAD_START_OFFSET) = index;
  64. offset = offset + 1;
  65. write_long_value((uart_send_buf + offset + DEBUG_PROTOCL_PAYLOAD_START_OFFSET), start_addr);
  66. offset = offset + 4;
  67. write_long_value((uart_send_buf + offset + DEBUG_PROTOCL_PAYLOAD_START_OFFSET), end_addr);
  68. offset = offset + 4;
  69. write_long_value((uart_send_buf + offset + DEBUG_PROTOCL_PAYLOAD_START_OFFSET), write_addr);
  70. offset = offset + 4;
  71. write_long_value((uart_send_buf + offset + DEBUG_PROTOCL_PAYLOAD_START_OFFSET), read_addr);
  72. offset = offset + 4;
  73. // Prepare packet
  74. yichip_debug_uart_protocol(DEBUG_PROTOCL_TYPE_ROUND_BUFFER, offset, uart_send_buf, &uart_send_length);
  75. uart_txBuffer(uart_send_buf, uart_send_length);
  76. }
  77. /**
  78. * To avoid uart lost info, we need add protocol to protect it, The input length must limit DEBUG_PROTOCL_PAYLOAD_ALLOW_LENGTH
  79. * To reduce timing, you should prepare payload into <uart_send_buf> in right point.
  80. * 1byte 1byte 1byte nbytes 1byte
  81. * | 0xAA | Type | length | Payload | 0xBB |
  82. */
  83. void yichip_debug_uart_protocol(uint8_t type, uint8_t length
  84. , uint8_t* uart_send_buf, uint8_t* uart_send_length_ptr)
  85. {
  86. uint16_t offset = 0;
  87. *(uart_send_buf + offset) = 0xAA;
  88. offset ++;
  89. *(uart_send_buf + offset) = type;
  90. offset ++;
  91. *(uart_send_buf + offset) = length;
  92. offset ++;
  93. length = length > DEBUG_PROTOCL_PAYLOAD_ALLOW_LENGTH?DEBUG_PROTOCL_PAYLOAD_ALLOW_LENGTH:length;
  94. // DO NOTHING, to reduce timing
  95. //memcpy(uart_send_buf+offset, payload, length);
  96. offset += length;
  97. *(uart_send_buf + offset) = 0xBB;
  98. offset ++;
  99. *uart_send_length_ptr = offset;
  100. }
  101. // use share memory
  102. //uint32_t log_bug[LOG_BUFFER_SIZE];
  103. //uint16_t log_read_index;
  104. //uint16_t log_write_index;
  105. //uint8_t log_lock_flag;
  106. uint32_t* log_bug_ptr;
  107. volatile uint8_t* log_initial_flag_ptr;
  108. // use Peterson's Algorithm
  109. volatile uint8_t* log_lock_flag_cm0_ptr;
  110. volatile uint8_t* log_lock_flag_respin_ptr;
  111. volatile uint8_t* log_lock_victim_ptr;
  112. volatile uint16_t* log_read_index_ptr;
  113. volatile uint16_t* log_write_index_ptr;
  114. void init_print_log(void)
  115. {
  116. // Set ptr
  117. log_bug_ptr = (uint32_t *)DEBUG_LOG_BUF_START_ADDR;
  118. log_initial_flag_ptr = (uint8_t *)DEBUG_LOG_BUF_INITIAL_FLAG_ADDR;
  119. log_lock_flag_cm0_ptr = (uint8_t *)DEBUG_LOG_BUF_LOCK_FLAG_CM0_ADDR;
  120. log_lock_flag_respin_ptr = (uint8_t *)DEBUG_LOG_BUF_LOCK_FLAG_RESPIN_ADDR;
  121. log_lock_victim_ptr = (uint8_t *)DEBUG_LOG_BUF_LOCK_VICTIM_ADDR;
  122. log_read_index_ptr = (uint16_t *)DEBUG_LOG_BUF_READ_INDEX_ADDR;
  123. log_write_index_ptr = (uint16_t *)DEBUG_LOG_BUF_WRITE_INDEX_ADDR;
  124. if(*(log_initial_flag_ptr) != DEBUG_LOG_INITIAL_SUCCESS_FLAG)
  125. {
  126. memset((void *)log_bug_ptr, 0, LOG_BUFFER_SIZE*4);
  127. *(log_lock_flag_cm0_ptr) = 0;
  128. *(log_lock_flag_respin_ptr) = 0;
  129. *(log_lock_victim_ptr) = 0;
  130. *(log_read_index_ptr) = 0;
  131. *(log_write_index_ptr) = 0;
  132. *(log_initial_flag_ptr) = DEBUG_LOG_INITIAL_SUCCESS_FLAG;
  133. }
  134. }
  135. bool print_log_buffer_is_empty(void)
  136. {
  137. volatile uint16_t* read_index_ptr = log_read_index_ptr;
  138. volatile uint16_t* write_index_ptr = log_write_index_ptr;
  139. uint16_t write_index;
  140. uint16_t read_index;
  141. OS_ENTER_CRITICAL();
  142. // get write lock
  143. print_log_get_lock();
  144. write_index = (*write_index_ptr);
  145. read_index = (*read_index_ptr);
  146. print_log_free_lock();
  147. OS_EXIT_CRITICAL();
  148. return (write_index == read_index);
  149. }
  150. void print_log_to_uart(void)
  151. {
  152. volatile uint16_t* read_index_ptr = log_read_index_ptr;
  153. volatile uint16_t* write_index_ptr = log_write_index_ptr;
  154. uint16_t write_index;
  155. uint16_t read_index;
  156. uint16_t offset = 0;
  157. uint8_t uart_send_buf[DEBUG_PROTOCL_TOTAL_ALLOW_LENGTH];
  158. uint8_t uart_send_length = 0;
  159. int i;
  160. OS_ENTER_CRITICAL();
  161. // get write lock
  162. print_log_get_lock();
  163. write_index = (*write_index_ptr);
  164. read_index = (*read_index_ptr);
  165. // To avoid protocol header lost timing
  166. while((write_index != read_index)
  167. && ((offset + 4) < DEBUG_PROTOCL_PAYLOAD_ALLOW_LENGTH))// Here offset must add 4
  168. {
  169. memcpy((void *)(uart_send_buf + DEBUG_PROTOCL_PAYLOAD_START_OFFSET + offset)
  170. , log_bug_ptr + read_index, 4);
  171. read_index = (read_index + 1)%LOG_BUFFER_SIZE;
  172. *read_index_ptr = read_index;
  173. offset += 4;
  174. }
  175. print_log_free_lock();
  176. OS_EXIT_CRITICAL();
  177. // Real Send To Uart
  178. if(offset != 0)
  179. {
  180. // Prepare packet
  181. yichip_debug_uart_protocol(DEBUG_PROTOCL_TYPE_LOG, offset, uart_send_buf, &uart_send_length);
  182. uart_txBuffer(uart_send_buf, uart_send_length);
  183. }
  184. }
  185. void print_log(uint16_t flag, uint16_t value)
  186. {
  187. volatile uint16_t* read_index_ptr = log_read_index_ptr;
  188. volatile uint16_t* write_index_ptr = log_write_index_ptr;
  189. uint16_t write_index;
  190. uint16_t read_index;
  191. OS_ENTER_CRITICAL();
  192. // get lock
  193. print_log_get_lock();
  194. write_index = (*write_index_ptr);
  195. *(log_bug_ptr + write_index) = (flag << 16) | value;
  196. write_index = (write_index + 1)%LOG_BUFFER_SIZE;
  197. #ifdef LOG_PROTECT_OVERFLOW
  198. // TODO: Here we should do something to resolve overflow
  199. read_index = (*read_index_ptr);
  200. if(write_index == read_index)
  201. {
  202. //to drop one log
  203. if(write_index == 0)
  204. {
  205. write_index = LOG_BUFFER_SIZE - 1;
  206. }
  207. else
  208. {
  209. write_index --;
  210. }
  211. }
  212. *write_index_ptr = write_index;
  213. #endif
  214. print_log_free_lock();
  215. OS_EXIT_CRITICAL();
  216. // Try to send log right now
  217. print_log_to_uart();
  218. return;
  219. }
  220. void print_log_get_lock(void)
  221. {
  222. *(log_lock_flag_cm0_ptr) = 1;// I'm interested
  223. *(log_lock_victim_ptr) = DEBUG_LOG_LOCK_VICTIM_CM0;// you go first
  224. while(((*log_lock_flag_respin_ptr) != 0)
  225. && (*(log_lock_victim_ptr) == DEBUG_LOG_LOCK_VICTIM_CM0));// wait
  226. return;
  227. }
  228. void print_log_free_lock(void)
  229. {
  230. *log_lock_flag_cm0_ptr = 0;
  231. return;
  232. }
  233. //*****************************************************************************
  234. //
  235. //! A simple MyPrintf function supporting \%c, \%d, \%p, \%s, \%u,\%x, and \%X.
  236. //!
  237. //! \param format is the format string.
  238. //! \param ... are the optional arguments, which depend on the contents of the
  239. //! \return None.
  240. //
  241. //*****************************************************************************
  242. void print_string_to_uart(uint8_t* buf, int len)
  243. {
  244. uint8_t uart_send_buf[3];
  245. uint8_t offset = 0;
  246. if(len == 0)
  247. {
  248. return;
  249. }
  250. if(len > 255 - DEBUG_PROTOCL_HEADER_SIZE)
  251. {
  252. len = 255 - DEBUG_PROTOCL_HEADER_SIZE;
  253. }
  254. *(uart_send_buf + offset) = 0xAA;
  255. offset ++;
  256. *(uart_send_buf + offset) = DEBUG_PROTOCL_TYPE_STRING;
  257. offset ++;
  258. *(uart_send_buf + offset) = len;
  259. // send header
  260. uart_txBuffer(uart_send_buf, DEBUG_PROTOCL_PAYLOAD_START_OFFSET);
  261. uart_txBuffer(buf, len);
  262. // send trail
  263. *(uart_send_buf + 0) = 0xBB;
  264. uart_txBuffer(uart_send_buf, 1);
  265. }
  266. void print_string(uint8_t* uartSndBuf, uint8_t* uartSndBufUseLen, uint8_t* buf, int len)
  267. {
  268. for(uint8_t i = 0; i < len; i ++)
  269. {
  270. if(*uartSndBufUseLen >= DEBUG_STRING_LOG_SINGLE_LINE_MAX_SIZE)
  271. {
  272. return;
  273. }
  274. *(uartSndBuf + *uartSndBufUseLen) = buf[i];
  275. *uartSndBufUseLen = *uartSndBufUseLen+1;
  276. }
  277. }
  278. void pringt_string_log(char *format, ...)
  279. {
  280. uint32_t ulIdx, ulValue, ulPos, ulCount, ulBase, ulNeg;
  281. char *pcStr, pcBuf[16], cFill;
  282. char HexFormat;
  283. va_list vaArgP;
  284. static const int8_t* const g_pcHex1 = "0123456789abcdef";
  285. static const int8_t* const g_pcHex2 = "0123456789ABCDEF";
  286. va_start(vaArgP, format);
  287. uint8_t uart_tx_buff[DEBUG_STRING_LOG_SINGLE_LINE_MAX_SIZE];
  288. uint8_t uart_tx_local_ptr = 0;
  289. while(*format)
  290. {
  291. // Find the first non-% character, or the end of the string.
  292. for(ulIdx = 0; (format[ulIdx] != '%') && (format[ulIdx] != '\0');ulIdx++)
  293. {}
  294. // Write this portion of the string.
  295. if(ulIdx>0)
  296. {
  297. print_string(uart_tx_buff, &uart_tx_local_ptr, (uint8_t*)format, ulIdx);
  298. }
  299. format += ulIdx;
  300. if(*format == '%')
  301. {
  302. format++;
  303. // Set the digit count to zero, and the fill character to space
  304. // (i.e. to the defaults).
  305. ulCount = 0;
  306. cFill = ' ';
  307. again:
  308. switch(*format++)
  309. {
  310. case '0':
  311. case '1':
  312. case '2':
  313. case '3':
  314. case '4':
  315. case '5':
  316. case '6':
  317. case '7':
  318. case '8':
  319. case '9':
  320. {
  321. if((format[-1] == '0') && (ulCount == 0))
  322. {
  323. cFill = '0';
  324. }
  325. ulCount *= 10;
  326. ulCount += format[-1] - '0';
  327. goto again;
  328. }
  329. case 'c':
  330. {
  331. ulValue = va_arg(vaArgP, unsigned long);
  332. print_string(uart_tx_buff, &uart_tx_local_ptr, (uint8_t*)&ulValue, 1);
  333. break;
  334. }
  335. case 'd':
  336. {
  337. ulValue = va_arg(vaArgP, unsigned long);
  338. ulPos = 0;
  339. if((long)ulValue < 0)
  340. {
  341. ulValue = -(long)ulValue;
  342. ulNeg = 1;
  343. }
  344. else
  345. {
  346. ulNeg = 0;
  347. }
  348. ulBase = 10;
  349. goto convert;
  350. }
  351. case 's':
  352. {
  353. pcStr = va_arg(vaArgP, char *);
  354. for(ulIdx = 0; pcStr[ulIdx] != '\0'; ulIdx++)
  355. {}
  356. print_string(uart_tx_buff, &uart_tx_local_ptr, (uint8_t*)pcStr, ulIdx);
  357. if(ulCount > ulIdx)
  358. {
  359. ulCount -= ulIdx;
  360. while(ulCount--)
  361. {
  362. print_string(uart_tx_buff, &uart_tx_local_ptr, " ", 1);
  363. }
  364. }
  365. break;
  366. }
  367. case 'u':
  368. {
  369. ulValue = va_arg(vaArgP, unsigned long);
  370. ulPos = 0;
  371. ulBase = 10;
  372. ulNeg = 0;
  373. goto convert;
  374. }
  375. case 'X':
  376. {
  377. ulValue = va_arg(vaArgP, unsigned long);
  378. ulPos = 0;
  379. ulBase = 16;
  380. ulNeg = 0;
  381. HexFormat='X';
  382. goto convert;
  383. }
  384. case 'x':
  385. case 'p':
  386. {
  387. ulValue = va_arg(vaArgP, unsigned long);
  388. ulPos = 0;
  389. ulBase = 16;
  390. ulNeg = 0;
  391. HexFormat='x';
  392. convert:
  393. for(ulIdx = 1;
  394. (((ulIdx * ulBase) <= ulValue) &&
  395. (((ulIdx * ulBase) / ulBase) == ulIdx));
  396. ulIdx *= ulBase, ulCount--)
  397. {
  398. }
  399. if(ulNeg)
  400. {
  401. ulCount--;
  402. }
  403. if(ulNeg && (cFill == '0'))
  404. {
  405. pcBuf[ulPos++] = '-';
  406. ulNeg = 0;
  407. }
  408. if((ulCount > 1) && (ulCount < 16))
  409. {
  410. for(ulCount--; ulCount; ulCount--)
  411. {
  412. pcBuf[ulPos++] = cFill;
  413. }
  414. }
  415. if(ulNeg)
  416. {
  417. pcBuf[ulPos++] = '-';
  418. }
  419. for(; ulIdx; ulIdx /= ulBase)
  420. {
  421. if(HexFormat=='x') pcBuf[ulPos++] = g_pcHex1[(ulValue / ulIdx) % ulBase];//x
  422. else pcBuf[ulPos++] = g_pcHex2[(ulValue / ulIdx) % ulBase];//X
  423. }
  424. print_string(uart_tx_buff, &uart_tx_local_ptr, (uint8_t*)pcBuf, ulPos);
  425. break;
  426. }
  427. case '%':
  428. {
  429. print_string(uart_tx_buff, &uart_tx_local_ptr, (uint8_t*)format - 1, 1);
  430. break;
  431. }
  432. default:
  433. {
  434. print_string(uart_tx_buff, &uart_tx_local_ptr, "ERROR", 5);
  435. break;
  436. }
  437. }//switch
  438. }//if
  439. }//while
  440. va_end(vaArgP);
  441. print_string_to_uart(uart_tx_buff, uart_tx_local_ptr);
  442. }
  443. #endif
  444. void print_string_empty_fuction(char *format, ...)
  445. {
  446. }