system.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * Copyright (c) 2006-2020, YICHIP Development Team
  3. * @file yc_system.c
  4. * @brief source file for setting system
  5. *
  6. * Change Logs:
  7. * Date Author Version Notes
  8. * 2020-11-05 wushengyan V1.0.0 the first version
  9. */
  10. #include <stdarg.h>
  11. #include "system.h"
  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. typedef struct _PrintPort_TypeDef_
  22. {
  23. UART_TypeDef *PrintUart;
  24. GPIO_TypeDef PrintRX_Port;
  25. GPIO_Pin_TypeDef PrintRX_Pin;
  26. GPIO_TypeDef PrintTX_Port;
  27. GPIO_Pin_TypeDef PrintTX_Pin;
  28. } PrintPort_TypeDef;
  29. static PrintPort_TypeDef PrintPort_Struct =
  30. {
  31. .PrintUart = PRINTPORT,
  32. .PrintRX_Port = PRINTRX_PORT,
  33. .PrintRX_Pin = PRINTRX_IO_PIN,
  34. .PrintTX_Port = PRINTTX_PORT,
  35. .PrintTX_Pin = PRINTTX_IO_PIN,
  36. };
  37. //#define SIM_PLATFORM
  38. void print_char(int data)
  39. {
  40. volatile int *ptr;
  41. ptr = (volatile int *)0xE0300;
  42. *ptr = data;
  43. }
  44. void printfsend(uint8_t *buf, int len)
  45. {
  46. uint8_t printbuf[256];
  47. for (int i = 0; i < len; i++)
  48. {
  49. printbuf[i] = buf[i];
  50. #ifdef SIM_PLATFORM
  51. print_char(buf[i]);
  52. #endif
  53. }
  54. #ifndef SIM_PLATFORM
  55. //UART_SendBuf(PrintPort_Struct.PrintUart, printbuf, len);
  56. UART_SendBuf(PRINTPORT, printbuf, len);
  57. #endif
  58. }
  59. void MyPrintf(char *format, ...)
  60. {
  61. static const int8_t *const g_pcHex1 = "0123456789abcdef";
  62. static const int8_t *const g_pcHex2 = "0123456789ABCDEF";
  63. uint32_t ulIdx = 0, ulValue = 0, ulPos = 0, ulCount = 0, ulBase = 0, ulNeg = 0;
  64. int8_t *pcStr = NULL, pcBuf[16] = {0}, cFill = 0;
  65. char HexFormat;
  66. va_list vaArgP;
  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 = (int8_t *)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((uint8_t *)" ", 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')
  201. pcBuf[ulPos++] = g_pcHex1[(ulValue / ulIdx) % ulBase];//x
  202. else
  203. pcBuf[ulPos++] = g_pcHex2[(ulValue / ulIdx) % ulBase];//X
  204. }
  205. printfsend((uint8_t *)pcBuf, ulPos);
  206. break;
  207. }
  208. case '%':
  209. {
  210. printfsend((uint8_t *)format - 1, 1);
  211. break;
  212. }
  213. default:
  214. {
  215. printfsend((uint8_t *)"ERROR", 5);
  216. break;
  217. }
  218. }/* switch */
  219. }/* if */
  220. }/* while */
  221. va_end(vaArgP);
  222. }
  223. void printv(uint8_t *buf, uint32_t len, char *s)
  224. {
  225. uint32_t i = 0;
  226. uint32_t n = 0;
  227. MyPrintf("\r\n%s:", s);
  228. for (i = 0; i < len; i++)
  229. {
  230. if (i % 16 == 0)
  231. {
  232. MyPrintf("\r\n%08x:", n);
  233. n += 16;
  234. }
  235. MyPrintf("%02x ", buf[i]);
  236. }
  237. MyPrintf("\r\n");
  238. }
  239. static void PrintPort_Init(void)
  240. {
  241. UART_InitTypeDef UART_InitStruct;
  242. UART_InitStruct.BaudRate = PRINT_BAUD; //Configure serial port baud rate, the baud rate defaults to 128000.
  243. UART_InitStruct.DataBits = DATABITS_8B;
  244. UART_InitStruct.StopBits = STOPBITS_1;
  245. UART_InitStruct.Parity = YC_PARITY_NONE;
  246. UART_InitStruct.FlowCtrl = FLOWCTRL_NONE;
  247. UART_InitStruct.RxMode = MODE_RX_ENABLE;
  248. UART_InitStruct.SmartCard = SMARTCARD_DISABLE;
  249. UART_InitStruct.CommMode = MODE_DUPLEX;
  250. if (PrintPort_Struct.PrintUart == MUART0)
  251. {
  252. GPIO_Config(PrintPort_Struct.PrintRX_Port, PrintPort_Struct.PrintRX_Pin, UART0_RXD);
  253. GPIO_Config(PrintPort_Struct.PrintTX_Port, PrintPort_Struct.PrintTX_Pin, UART0_TXD);
  254. }
  255. else if (PrintPort_Struct.PrintUart == MUART1)
  256. {
  257. GPIO_Config(PrintPort_Struct.PrintRX_Port, PrintPort_Struct.PrintRX_Pin, UART1_RXD);
  258. GPIO_Config(PrintPort_Struct.PrintTX_Port, PrintPort_Struct.PrintTX_Pin, UART1_TXD);
  259. }
  260. else if (PrintPort_Struct.PrintUart == MUART2)
  261. {
  262. GPIO_Config(PrintPort_Struct.PrintRX_Port, PrintPort_Struct.PrintRX_Pin, UART2_RXD);
  263. GPIO_Config(PrintPort_Struct.PrintTX_Port, PrintPort_Struct.PrintTX_Pin, UART2_TXD);
  264. }
  265. else if (PrintPort_Struct.PrintUart == MUART3)
  266. {
  267. GPIO_Config(PrintPort_Struct.PrintRX_Port, PrintPort_Struct.PrintRX_Pin, UART3_RXD);
  268. GPIO_Config(PrintPort_Struct.PrintTX_Port, PrintPort_Struct.PrintTX_Pin, UART3_TXD);
  269. }
  270. UART_Init(PrintPort_Struct.PrintUart, &UART_InitStruct);
  271. uint8_t print_irq = (PrintPort_Struct.PrintUart - MUART0) / (MUART1 - MUART0);
  272. NVIC_EnableIRQ((IRQn_Type)(UART0_IRQn + print_irq));
  273. NVIC_SetPriority((IRQn_Type)(UART0_IRQn + print_irq),1);
  274. }
  275. void PrintPort_Set(UART_TypeDef *UARTx)
  276. {
  277. PrintPort_Struct.PrintUart = UARTx;
  278. // if(UARTx == MUART1)
  279. // {
  280. // PrintPort_Struct.PrintRX_Port = UART1RX_PORT;
  281. // PrintPort_Struct.PrintRX_Pin = UART1RX_IO_PIN;
  282. // PrintPort_Struct.PrintTX_Port = UART1TX_PORT;
  283. // PrintPort_Struct.PrintTX_Pin = UART1TX_IO_PIN;
  284. // }
  285. }
  286. void Board_Init(void)
  287. {
  288. /*fpga io func sel*/
  289. #if (BOARD_TYPE == FPGA_BOARD)
  290. uint8_t fpga_io_func_sel_list[][2] =
  291. {
  292. #ifdef __SPI0_FLASH_FPGA__
  293. {0x02,0x01},
  294. {0x08,0x01},
  295. {0x21,0x40},
  296. #endif
  297. {0x00,0x00},
  298. #ifdef __SPI1_FLASH_FPGA__
  299. {0x05,0x01},
  300. {0x08,0x01},
  301. {0x21,0x80},
  302. #endif
  303. #ifdef __SCANNER_BF3007_BCTC_FPGA__
  304. {0x01,0x01}, //func_sel1: ALT1 psram
  305. {0x08,0x01}, //func_sel8: ALT1 tft
  306. {0x09,0x01}, //func_sel9: ALT1 tft led
  307. {0x02,0x01}, //func_sel9: ALT1 spia
  308. {0x21,0x04}, //spi_sel:tft_spi_sel: SPIy
  309. {0x20,0x02}, //iic_sel:iic0_sel: fingerprint i2c
  310. {0x04,0x01}, //sel iica
  311. {0x06,0x02}, //func_sel6: ALT2 fingerprint DCMI
  312. {0x07,0x01}, //alt1 buzzer
  313. #endif
  314. };
  315. for(uint8_t i = 0; i < (sizeof(fpga_io_func_sel_list)/2); i ++)
  316. {
  317. FPGA_reg_write(fpga_io_func_sel_list[i][0],fpga_io_func_sel_list[i][1]);
  318. }
  319. #endif
  320. /*print init*/
  321. PrintPort_Init();
  322. }
  323. void _assert_handler(const char *file, int line, const char *func)
  324. {
  325. #if defined (SDK_DEBUG)
  326. if(PRINTPORT->CTRL.bit.RX_EN == MODE_RX_ENABLE) /*check printuart is init*/
  327. {
  328. MyPrintf("Assert trigger at file: %s line:%d func: %s\n ", file, line, func);
  329. }
  330. #endif
  331. while (1);
  332. }