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 
11 #include <stdarg.h>
12 #include "system.h"
13 
14 
15 //*****************************************************************************
16 //
17 //! A simple  MyPrintf function supporting \%c, \%d, \%p, \%s, \%u,\%x, and \%X.
18 //!
19 //! \param format is the format string.
20 //! \param ... are the optional arguments, which depend on the contents of the
21 //! \return None.
22 //
23 //*****************************************************************************
24 
25 typedef struct _PrintPort_TypeDef_
26 {
27     UART_TypeDef            *PrintUart;
28     GPIO_TypeDef            PrintRX_Port;
29     GPIO_Pin_TypeDef        PrintRX_Pin;
30     GPIO_TypeDef            PrintTX_Port;
31     GPIO_Pin_TypeDef        PrintTX_Pin;
32 } PrintPort_TypeDef;
33 
34 static PrintPort_TypeDef PrintPort_Struct =
35 {
36     .PrintUart    = PRINTPORT,
37     .PrintRX_Port = PRINTRX_PORT,
38     .PrintRX_Pin  = PRINTRX_IO_PIN,
39     .PrintTX_Port = PRINTTX_PORT,
40     .PrintTX_Pin  = PRINTTX_IO_PIN,
41 };
42 
43 
44 //#define SIM_PLATFORM
45 
print_char(int data)46 void print_char(int data)
47 {
48 
49     volatile int *ptr;
50     ptr = (volatile int *)0xE0300;
51     *ptr = data;
52 }
53 
printfsend(uint8_t * buf,int len)54 void printfsend(uint8_t *buf, int len)
55 {
56     uint8_t printbuf[256];
57     for (int i = 0; i < len; i++)
58     {
59         printbuf[i] = buf[i];
60 #ifdef  SIM_PLATFORM
61         print_char(buf[i]);
62 #endif
63     }
64 #ifndef SIM_PLATFORM
65     //UART_SendBuf(PrintPort_Struct.PrintUart, printbuf, len);
66     UART_SendBuf(PRINTPORT, printbuf, len);
67 #endif
68 }
69 
MyPrintf(char * format,...)70 void MyPrintf(char *format, ...)
71 {
72     static const int8_t *const g_pcHex1 = "0123456789abcdef";
73     static const int8_t *const g_pcHex2 = "0123456789ABCDEF";
74 
75     uint32_t ulIdx = 0, ulValue = 0, ulPos = 0, ulCount = 0, ulBase = 0, ulNeg = 0;
76     int8_t *pcStr = NULL, pcBuf[16] = {0}, cFill = 0;
77     char HexFormat;
78     va_list vaArgP;
79 
80     va_start(vaArgP, format);
81 
82     while (*format)
83     {
84         /* Find the first non-% character, or the end of the string. */
85         for (ulIdx = 0; (format[ulIdx] != '%') && (format[ulIdx] != '\0'); ulIdx++)
86         {}
87 
88         /* Write this portion of the string. */
89         if (ulIdx > 0)
90         {
91             printfsend((uint8_t *)format, ulIdx);
92         }
93 
94         format += ulIdx;
95 
96         if (*format == '%')
97         {
98             format++;
99 
100             /* Set the digit count to zero, and the fill character to space */
101             /* (i.e. to the defaults) */
102             ulCount = 0;
103             cFill = ' ';
104 
105 again:
106             switch (*format++)
107             {
108             case '0':
109             case '1':
110             case '2':
111             case '3':
112             case '4':
113             case '5':
114             case '6':
115             case '7':
116             case '8':
117             case '9':
118             {
119                 if ((format[-1] == '0') && (ulCount == 0))
120                 {
121                     cFill = '0';
122                 }
123 
124                 ulCount *= 10;
125                 ulCount += format[-1] - '0';
126 
127                 goto again;
128             }
129 
130             case 'c':
131             {
132                 ulValue = va_arg(vaArgP, unsigned long);
133                 printfsend((uint8_t *)&ulValue, 1);
134                 break;
135             }
136 
137             case 'd':
138             {
139                 ulValue = va_arg(vaArgP, unsigned long);
140                 ulPos = 0;
141 
142                 if ((long)ulValue < 0)
143                 {
144                     ulValue = -(long)ulValue;
145                     ulNeg = 1;
146                 }
147                 else
148                 {
149                     ulNeg = 0;
150                 }
151 
152                 ulBase = 10;
153                 goto convert;
154             }
155 
156             case 's':
157             {
158                 pcStr = (int8_t *)va_arg(vaArgP, char *);
159 
160                 for (ulIdx = 0; pcStr[ulIdx] != '\0'; ulIdx++)
161                 {}
162 
163                 printfsend((uint8_t *)pcStr, ulIdx);
164 
165                 if (ulCount > ulIdx)
166                 {
167                     ulCount -= ulIdx;
168                     while (ulCount--)
169                     {
170                         printfsend((uint8_t *)" ", 1);
171                     }
172                 }
173                 break;
174             }
175 
176             case 'u':
177             {
178                 ulValue = va_arg(vaArgP, unsigned long);
179                 ulPos = 0;
180                 ulBase = 10;
181                 ulNeg = 0;
182                 goto convert;
183             }
184 
185             case 'X':
186             {
187                 ulValue = va_arg(vaArgP, unsigned long);
188                 ulPos = 0;
189                 ulBase = 16;
190                 ulNeg = 0;
191                 HexFormat = 'X';
192                 goto convert;
193             }
194 
195             case 'x':
196 
197             case 'p':
198             {
199                 ulValue = va_arg(vaArgP, unsigned long);
200                 ulPos = 0;
201                 ulBase = 16;
202                 ulNeg = 0;
203                 HexFormat = 'x';
204 
205 convert:
206                 for (ulIdx = 1;
207                         (((ulIdx * ulBase) <= ulValue) &&
208                          (((ulIdx * ulBase) / ulBase) == ulIdx));
209                         ulIdx *= ulBase, ulCount--)
210                 {
211                 }
212 
213                 if (ulNeg)
214                 {
215                     ulCount--;
216                 }
217 
218                 if (ulNeg && (cFill == '0'))
219                 {
220                     pcBuf[ulPos++] = '-';
221                     ulNeg = 0;
222                 }
223 
224                 if ((ulCount > 1) && (ulCount < 16))
225                 {
226                     for (ulCount--; ulCount; ulCount--)
227                     {
228                         pcBuf[ulPos++] = cFill;
229                     }
230                 }
231 
232                 if (ulNeg)
233                 {
234                     pcBuf[ulPos++] = '-';
235                 }
236 
237                 for (; ulIdx; ulIdx /= ulBase)
238                 {
239                     if (HexFormat == 'x')
240                         pcBuf[ulPos++] = g_pcHex1[(ulValue / ulIdx) % ulBase];//x
241                     else
242                         pcBuf[ulPos++] = g_pcHex2[(ulValue / ulIdx) % ulBase];//X
243                 }
244 
245                 printfsend((uint8_t *)pcBuf, ulPos);
246                 break;
247             }
248 
249             case '%':
250             {
251                 printfsend((uint8_t *)format - 1, 1);
252                 break;
253             }
254 
255             default:
256             {
257                 printfsend((uint8_t *)"ERROR", 5);
258                 break;
259             }
260             }/* switch */
261         }/* if */
262     }/* while */
263     va_end(vaArgP);
264 }
265 
printv(uint8_t * buf,uint32_t len,char * s)266 void printv(uint8_t *buf, uint32_t len, char *s)
267 {
268     uint32_t i = 0;
269     uint32_t n = 0;
270     MyPrintf("\r\n%s:", s);
271     for (i = 0; i < len; i++)
272     {
273         if (i % 16 == 0)
274         {
275             MyPrintf("\r\n%08x:", n);
276             n += 16;
277         }
278         MyPrintf("%02x ", buf[i]);
279 
280     }
281     MyPrintf("\r\n");
282 }
283 
284 
285 
286 
PrintPort_Init(void)287 static void PrintPort_Init(void)
288 {
289     UART_InitTypeDef   UART_InitStruct;
290     UART_InitStruct.BaudRate  = PRINT_BAUD;			//Configure serial port baud rate, the baud rate defaults to 128000.
291     UART_InitStruct.DataBits  = DATABITS_8B;
292     UART_InitStruct.StopBits  = STOPBITS_1;
293     UART_InitStruct.Parity    = YC_PARITY_NONE;
294     UART_InitStruct.FlowCtrl  = FLOWCTRL_NONE;
295     UART_InitStruct.RxMode    = MODE_RX_ENABLE;
296     UART_InitStruct.SmartCard = SMARTCARD_DISABLE;
297     UART_InitStruct.CommMode  = MODE_DUPLEX;
298 
299     if (PrintPort_Struct.PrintUart == MUART0)
300     {
301         GPIO_Config(PrintPort_Struct.PrintRX_Port, PrintPort_Struct.PrintRX_Pin, UART0_RXD);
302         GPIO_Config(PrintPort_Struct.PrintTX_Port, PrintPort_Struct.PrintTX_Pin, UART0_TXD);
303     }
304     else if (PrintPort_Struct.PrintUart == MUART1)
305     {
306         GPIO_Config(PrintPort_Struct.PrintRX_Port, PrintPort_Struct.PrintRX_Pin, UART1_RXD);
307         GPIO_Config(PrintPort_Struct.PrintTX_Port, PrintPort_Struct.PrintTX_Pin, UART1_TXD);
308     }
309     else if (PrintPort_Struct.PrintUart == MUART2)
310     {
311         GPIO_Config(PrintPort_Struct.PrintRX_Port, PrintPort_Struct.PrintRX_Pin, UART2_RXD);
312         GPIO_Config(PrintPort_Struct.PrintTX_Port, PrintPort_Struct.PrintTX_Pin, UART2_TXD);
313     }
314     else if (PrintPort_Struct.PrintUart == MUART3)
315     {
316         GPIO_Config(PrintPort_Struct.PrintRX_Port, PrintPort_Struct.PrintRX_Pin, UART3_RXD);
317         GPIO_Config(PrintPort_Struct.PrintTX_Port, PrintPort_Struct.PrintTX_Pin, UART3_TXD);
318     }
319 
320     UART_Init(PrintPort_Struct.PrintUart, &UART_InitStruct);
321     uint8_t print_irq = (PrintPort_Struct.PrintUart - MUART0) / (MUART1 - MUART0);
322     NVIC_EnableIRQ((IRQn_Type)(UART0_IRQn + print_irq));
323     NVIC_SetPriority((IRQn_Type)(UART0_IRQn + print_irq),1);
324 }
325 
PrintPort_Set(UART_TypeDef * UARTx)326 void PrintPort_Set(UART_TypeDef *UARTx)
327 {
328     PrintPort_Struct.PrintUart = UARTx;
329 
330 //    if(UARTx == MUART1)
331 //    {
332 //        PrintPort_Struct.PrintRX_Port = UART1RX_PORT;
333 //        PrintPort_Struct.PrintRX_Pin  = UART1RX_IO_PIN;
334 //        PrintPort_Struct.PrintTX_Port = UART1TX_PORT;
335 //        PrintPort_Struct.PrintTX_Pin  = UART1TX_IO_PIN;
336 //    }
337 }
338 
Board_Init(void)339 void Board_Init(void)
340 {
341     /*fpga io func sel*/
342 #if (BOARD_TYPE == FPGA_BOARD)
343 
344     uint8_t fpga_io_func_sel_list[][2] =
345     {
346 #ifdef __SPI0_FLASH_FPGA__
347         {0x02,0x01},
348         {0x08,0x01},
349         {0x21,0x40},
350 #endif
351 
352         {0x00,0x00},
353 
354 #ifdef __SPI1_FLASH_FPGA__
355         {0x05,0x01},
356         {0x08,0x01},
357         {0x21,0x80},
358 #endif
359 #ifdef __SCANNER_BF3007_BCTC_FPGA__
360         {0x01,0x01}, //func_sel1: ALT1 psram
361         {0x08,0x01}, //func_sel8: ALT1 tft
362         {0x09,0x01}, //func_sel9: ALT1 tft led
363         {0x02,0x01}, //func_sel9: ALT1 spia
364         {0x21,0x04}, //spi_sel:tft_spi_sel: SPIy
365         {0x20,0x02}, //iic_sel:iic0_sel: fingerprint i2c
366         {0x04,0x01}, //sel iica
367         {0x06,0x02}, //func_sel6: ALT2 fingerprint DCMI
368         {0x07,0x01}, //alt1 buzzer
369 #endif
370     };
371     for(uint8_t i = 0; i < (sizeof(fpga_io_func_sel_list)/2); i ++)
372     {
373         FPGA_reg_write(fpga_io_func_sel_list[i][0],fpga_io_func_sel_list[i][1]);
374     }
375 
376 #endif
377 
378     /*print init*/
379     PrintPort_Init();
380 }
381 
_assert_handler(const char * file,int line,const char * func)382 void _assert_handler(const char *file, int line, const char *func)
383 {
384 #if defined (SDK_DEBUG)
385     if(PRINTPORT->CTRL.bit.RX_EN == MODE_RX_ENABLE) /*check printuart is init*/
386     {
387         MyPrintf("Assert trigger at file: %s line:%d func: %s\n ", file, line, func);
388     }
389 #endif
390     while (1);
391 }
392