ioLibrary for Cortex M series
wizplatform.c
Go to the documentation of this file.
1 
12 //#define FILE_LOG_SILENCE
13 #include "common/common.h"
14 
15 #define USART1_RX_INTERRUPT VAL_DISABLE//VAL_DISABLE//VAL_ENABLE
16 #define USART2_RX_INTERRUPT VAL_DISABLE//VAL_DISABLE//VAL_ENABLE
17 #define SYSTICK_HZ 1000
18 
19 
20 uint32_t msTicks = 0; // Max: about 50 days
21 void SysTickIntHandler(void) // SysTick ISR
22 {
23  msTicks++;
24 
25 }
26 
27 
28 #if (USART1_RX_INTERRUPT == VAL_ENABLE)
29 #define U0RX_BUF_SIZE 16
31 uint16_t u0rx_wr=0, u0rx_rd=0;
32 void USART0_IRQHandler(void) // USART1 ISR
33 {
34  if(UARTIntStatus(xUART0_BASE, xUART_INT_RX) != RESET) {
35  UARTIntClear(xUART0_BASE, xUART_INT_RX);
36  if( (u0rx_wr > u0rx_rd && u0rx_wr-u0rx_rd >= U0RX_BUF_SIZE-1) ||
37  (u0rx_wr < u0rx_rd && u0rx_rd == u0rx_wr+1) ) // Buffer Overflow
38  {
39 
40  UARTCharPut(xUART0_BASE, (uint8_t)'@');
41  return;
42  }
43  u0rx_buf[u0rx_wr] = (uint8_t)UARTCharGet(xUART0_BASE);
44  if(u0rx_wr < U0RX_BUF_SIZE-1) u0rx_wr++;
45  else u0rx_wr = 0;
46  }
47 }
48 #endif
49 
50 #if (USART2_RX_INTERRUPT == VAL_ENABLE)
51 #define U1RX_BUF_SIZE 16
53 uint16_t u1rx_wr=0, u1rx_rd=0;
54 void USART1_IRQHandler(void) // USART2 ISR
55 {
56  if(UARTIntStatus(xUART1_BASE, xUART_INT_RX) != RESET) {
57  UARTIntClear(xUART1_BASE, xUART_INT_RX);
58  if( (u1rx_wr > u1rx_rd && u1rx_wr-u1rx_rd >= U1RX_BUF_SIZE-1) ||
59  (u1rx_wr < u1rx_rd && u1rx_rd == u1rx_wr+1) ) // Buffer Overflow
60  {
61 
62  UARTCharPut(xUART1_BASE, (uint8_t)'@');
63  return;
64  }
65  u1rx_buf[u1rx_wr] = (uint8_t)UARTCharGet(xUART1_BASE);
66  if(u1rx_wr < U1RX_BUF_SIZE-1) u1rx_wr++;
67  else u1rx_wr = 0;
68  }
69 }
70 #endif
72 {
73  int8_t ret8;
74 
75  uint8_t tx_size[8]={2, 2, 2, 2, 2, 2, 2, 2}; // Device default memory setting
76  uint8_t rx_size[8]={2, 2, 2, 2, 2, 2, 2, 2};
77 
78  xSysCtlClockSet(12000000, xSYSCTL_OSC_MAIN | xSYSCTL_XTAL_12MHZ);
79 
80  xSysCtlDelay(44000000);
81  xSysTickPeriodSet(xSysCtlClockGet()/1000);
82 
83  // GPIOA enable
84  xSysCtlPeripheralEnable(xSYSCTL_PERIPH_GPIOA );
85  xSysCtlPeripheralEnable(xSYSCTL_PERIPH_GPIOB );
86 
87  xSysCtlPeripheralReset(xSYSCTL_PERIPH_UART0);
88  xSysCtlPeripheralEnable(xSYSCTL_PERIPH_UART0);
89 
90  xGPIODirModeSet(LED1_PORT, LED1_PIN,xGPIO_DIR_MODE_OUT);// xGPIO_DIR_MODE_QB);//xGPIO_DIR_MODE_OUT);
91  xGPIODirModeSet(LED2_PORT, LED2_PIN,xGPIO_DIR_MODE_OUT);// xGPIO_DIR_MODE_QB);//xGPIO_DIR_MODE_OUT);
92  //xGPIOSPinTypeGPIOOutputQB(LED1_GPIO);
93  //xGPIOSPinTypeGPIOOutputQB(LED2_GPIO);
94  xSPinTypeUART(UART0TX, PA2);
95  xSPinTypeUART(UART0RX, PA3);
96 
97  if(up == NULL) {
98  xUARTConfigSet(xUART0_BASE, 115200, (UART_CONFIG_WLEN_8 |
99  UART_CONFIG_STOP_ONE |
100  UART_CONFIG_PAR_NONE));
101 
102  }
103  else
104  {
105  xUARTConfigSet(xUART0_BASE, up->baudrate, (UART_CONFIG_WLEN_8 |
106  UART_CONFIG_STOP_ONE |
107  UART_CONFIG_PAR_NONE));
108  }
109 #if (USART1_RX_INTERRUPT == VAL_ENABLE)
110  xUARTIntCallbackInit(xUART0_BASE,USART0_IRQHandler );
111 #endif
112 #if (USART2_RX_INTERRUPT == VAL_ENABLE)
113  xUARTIntCallbackInit(xUART1_BASE,USART1_IRQHandler );
114 #endif
115 
116  xUARTEnable(xUART0_BASE, (UART_BLOCK_UART | UART_BLOCK_TX | UART_BLOCK_RX));
117 
118  ret8 = wizpf_spi_init(WIZ_SPI1);
119  if(ret8 != RET_OK) {
120  ERR("ERR: wizpf_spi_init fail");
121  return RET_NOK;
122  }
123 
125  // reg_wizchip_cris_cbfunc(wizpf_wizchip_cris_enter,wizpf_wizchip_cris_exit); // If fail to the critical section process, comment this statement and test again.
127 #if (_WIZCHIP_IO_MODE_== _WIZCHIP_IO_MODE_SPI_FDM_)
128  reg_wizchip_cs_cbfunc(wizpf_wizchip_select, wizpf_wizchip_select); // For being tied to the low.
129 #else
131 #endif
134 
135  if(wizchip_init(tx_size,rx_size) < 0)
136  {
137  ERR("ERR: WIZCHIP fail to initialized!!!");
138  while(1);
139  }
140 
143 
144 
145 #if (USART1_RX_INTERRUPT == VAL_ENABLE)
146  xUARTIntEnable(xUART0_BASE,xUART_INT_RX);
147 #endif
148 
149  xSysTickEnable();
150  xSysTickIntEnable();
151 
152  return RET_OK;
153 }
154 
155 
156 
158 {
159 #ifdef WIZ_RESET_PIN
160  xGPIOPinWrite( WIZ_RESET_PORT, WIZ_RESET_PIN, 0);
161  Delay_us(8);
162  xGPIOPinWrite( WIZ_RESET_PORT, WIZ_RESET_PIN, 1);
163  Delay_ms(50);
164 #endif
165 }
166 
167 void Delay_us(uint8_t time_us)
168 {
169  register uint8_t i, j;
170 
171  for(i=0; i<time_us; i++) {
172  for(j=0; j<5; j++) { // 25CLK
173  asm("nop"); //1CLK
174  asm("nop"); //1CLK
175  asm("nop"); //1CLK
176  asm("nop"); //1CLK
177  asm("nop"); //1CLK
178  }
179  } // 25CLK*0.04us=1us
180 }
181 
182 void Delay_ms(uint16_t time_ms)
183 {
184  register uint16_t i;
185 
186  for(i=0; i<time_ms; i++) {
187  Delay_us(250);
188  Delay_us(250);
189  Delay_us(250);
190  Delay_us(250);
191  }
192 }
193 
194 
195 void Delay_tick(uint32_t tick)
196 {
197 
198  uint32_t curTicks;
199  curTicks = msTicks;
200  while(msTicks - curTicks < tick);
201 }
202 
203 uint32_t wizpf_get_systick(void)
204 {
205  return msTicks;
206 }
207 
208 uint32_t wizpf_tick_conv(uint8_t istick2sec, uint32_t tickorsec)
209 {
210  if(istick2sec) return tickorsec / SYSTICK_HZ; // tick to seconds
211  else return tickorsec * SYSTICK_HZ; // seconds to tick
212 }
213 
214 int32_t wizpf_tick_elapse(uint32_t tick) // + Elapsed time, - Remaining time
215 {
216  uint32_t cur = wizpf_get_systick();
217 
218  return cur - tick;
219 }
220 
221 
234 {
235  switch(spi) {
236  case WIZ_SPI1:
237  xSysCtlPeripheralReset(xSYSCTL_PERIPH_SPI0);
238  xSysCtlPeripheralEnable(xSYSCTL_PERIPH_SPI0);
239  xGPIODirModeSet(xGPIO_PORTA_BASE, xGPIO_PIN_4, xGPIO_DIR_MODE_OUT);
240 
241  xSPinTypeSPI(SPI0CLK, SPI0_SCLK_GPIO);
242  xSPinTypeSPI(SPI0MOSI,SPI0_MOSI_GPIO);
243  xSPinTypeSPI(SPI0MISO,SPI0_MISO_GPIO);
244  xSPIConfigSet(xSPI0_BASE, 18000000, xSPI_MOTO_FORMAT_MODE_0 | xSPI_MODE_MASTER | xSPI_MSB_FIRST | xSPI_DATA_WIDTH8);
245 
246  xSPISSSet(xSPI0_BASE, SPI_SS_SOFTWARE, xSPI_SS_NONE);
247 
248  xSPIEnable(xSPI0_BASE);
249  break;
250  case WIZ_SPI2:
251  xSysCtlPeripheralReset(xSYSCTL_PERIPH_SPI1);
252  xSysCtlPeripheralEnable(xSYSCTL_PERIPH_SPI1);
253  xGPIODirModeSet(xGPIO_PORTA_BASE, xGPIO_PIN_4, xGPIO_DIR_MODE_OUT);
254 
255 
256  xSPinTypeSPI(SPI1CLK, SPI1_SCLK_GPIO);
257  xSPinTypeSPI(SPI1MOSI,SPI1_MOSI_GPIO);
258  xSPinTypeSPI(SPI1MISO,SPI1_MISO_GPIO);
259 
260  xSPIConfigSet(xSPI1_BASE, 18000000, xSPI_MOTO_FORMAT_MODE_0 | xSPI_MODE_MASTER | xSPI_MSB_FIRST | xSPI_DATA_WIDTH8);
261  xSPISSSet(xSPI1_BASE, SPI_SS_SOFTWARE, xSPI_SS_NONE);
262 
263  xSPIEnable(xSPI1_BASE);
264 
265  break;
266 
267 
268  default:
269  return RET_NOK;
270  }
271  return RET_OK;
272 }
273 
274 
275 int8_t wizpf_led_set(wizpf_led led, uint8_t action)
276 {
277  uint32_t GPIO_Port = 0;
278  uint32_t GPIO_Pin = 0;
279 
280  switch(led) {
281  case WIZ_LED1:
282  GPIO_Port = LED1_PORT;
283  GPIO_Pin = LED1_PIN;
284  break;
285 
286  case WIZ_LED2:
287  GPIO_Port = LED2_PORT;
288  GPIO_Pin = LED2_PIN;
289  break;
290 
291  default:
292  action = VAL_TOG + 1;
293  break;
294  }
295 
296  if(action == VAL_OFF) {
297  xGPIOPinWrite( GPIO_Port, GPIO_Pin, 0);// LED off
298  } else if(action == VAL_ON) {
299  xGPIOPinWrite( GPIO_Port, GPIO_Pin, 1);// LED on
300  } else if(action == VAL_TOG) {
301  if(wizpf_led_get(led)!=0)// if(xGPIOPinRead(GPIO_Port, GPIO_Pin)!=0)
302  xGPIOPinWrite( GPIO_Port, GPIO_Pin, 0);
303  else
304  xGPIOPinWrite( GPIO_Port, GPIO_Pin, 1);
305  } else return RET_NOK;
306 
307  return RET_OK;
308 }
309 
311 {
312  uint32_t GPIO_Port = 0;
313  uint32_t GPIO_Pin = 0;
314 
315  switch(led) {
316 
317  case WIZ_LED1:
318  GPIO_Port = LED1_PORT;
319  GPIO_Pin = LED1_PIN;
320  break;
321  case WIZ_LED2:
322  GPIO_Port = LED2_PORT;
323  GPIO_Pin = LED2_PIN;
324  break;
325  default:
326  return RET_NOK;
327  }
328  return (int8_t)xGPIOPinRead(GPIO_Port,GPIO_Pin);
329 }
330 
331 void wizpf_led_trap(uint8_t repeat)
332 {
333  uint32_t i=0;
334 #ifdef LED1_PIN
336  while(1) {
337  Delay_ms(1500);
338  for(i=0; i<repeat; i++) {
340  Delay_ms(200);
342  Delay_ms(200);
343  }
344  }
345 #endif
346 }
347 
348 
350 {
351  if(!xIntMasterEnable())
352  {
353  ERR("Fail to enter critical section for WIZCHIP\r\n");
354  while(1);
355  }
356 }
358 {
359  if(!xIntMasterDisable())
360  {
361  ERR("Fail to exit critical section for WIZCHIP\r\n");
362  while(1);
363  }
364 }
365 
367 {
368  xGPIOPinWrite( SPI0_SCS_PORT, SPI0_SCS_PIN, 0);
369 }
370 
372 {
373  xGPIOPinWrite( SPI0_SCS_PORT, SPI0_SCS_PIN, 1);
374 }
375 
377 {
378  xSPISingleDataReadWrite(xSPI0_BASE,wb);
379 }
380 
382 {
383  return xSPISingleDataReadWrite(xSPI0_BASE,0xFF);
384 }
385 
386 
387 int32_t wizpf_putchar(int32_t ch, wizpf_usart usart)
388 {
389  uint32_t USARTx;
390 
391  if(usart == WIZ_USART1) USARTx = xUART0_BASE;
392  else if(usart == WIZ_USART2) USARTx = xUART1_BASE;
393  else return RET_NOK;
394 
395  UARTCharPut(USARTx, (uint8_t)ch);
396  return ch;
397 }
398 
400 {
401 #if (USART1_RX_INTERRUPT == VAL_ENABLE)
402  if(usart == WIZ_USART1) {
403  while(u0rx_rd == u0rx_wr);
404  if(u0rx_rd < U0RX_BUF_SIZE-1) {
405  u0rx_rd++;
406  return u0rx_buf[u0rx_rd-1];
407  } else {
408  u0rx_rd = 0;
409  return u0rx_buf[U0RX_BUF_SIZE-1];
410  }
411  }
412 #endif
413 
414 #if (USART2_RX_INTERRUPT == VAL_ENABLE)
415  if(usart == WIZ_USART2) {
416  while(u1rx_rd == u1rx_wr);
417  if(u1rx_rd < U1RX_BUF_SIZE-1) {
418  u1rx_rd++;
419  return u1rx_buf[u1rx_rd-1];
420  } else {
421  u1rx_rd = 0;
422  return u1rx_buf[U1RX_BUF_SIZE-1];
423  }
424  }
425 #endif
426 
427 #if (USART1_RX_INTERRUPT == VAL_DISABLE) || (USART2_RX_INTERRUPT == VAL_DISABLE)
428  {
429  uint32_t USARTx;
430 
431  if(usart == WIZ_USART1) USARTx = xUART0_BASE;
432  else if(usart == WIZ_USART2) USARTx = xUART1_BASE;
433  else return RET_NOK;
434  return UARTCharGet(USARTx);
435  }
436 #else
437  return RET_NOK;
438 #endif
439 }
440 
442 {
443 #if (USART1_RX_INTERRUPT == VAL_ENABLE)
444  if(usart == WIZ_USART1) {
445  if(u0rx_rd == u0rx_wr) return RET_NOK;
446  if(u0rx_rd < U0RX_BUF_SIZE-1) {
447  u0rx_rd++;
448  return u0rx_buf[u0rx_rd-1];
449  } else {
450  u0rx_rd = 0;
451  return u0rx_buf[U0RX_BUF_SIZE-1];
452  }
453  }
454 #endif
455 
456 #if (USART2_RX_INTERRUPT == VAL_ENABLE)
457  if(usart == WIZ_USART2) {
458  if(u1rx_rd == u1rx_wr) return RET_NOK;
459  if(u1rx_rd < U1RX_BUF_SIZE-1) {
460  u1rx_rd++;
461  return u1rx_buf[u1rx_rd-1];
462  } else {
463  u1rx_rd = 0;
464  return u1rx_buf[U1RX_BUF_SIZE-1];
465  }
466  }
467 #endif
468 
469 #if (USART1_RX_INTERRUPT == VAL_DISABLE) || (USART2_RX_INTERRUPT == VAL_DISABLE)
470  {
471  uint32_t USARTx;
472 
473  if(usart == WIZ_USART1) USARTx = xUART0_BASE;
474  else if(usart == WIZ_USART2) USARTx = xUART1_BASE;
475  else return RET_NOK;
476  return UARTCharGetNonBlocking(USARTx);
477  }
478 #else
479  return RET_NOK;
480 #endif
481 }