Wifly Driver  V1.0
PrintF.c
Go to the documentation of this file.
1 /*
2  * PrintF.C
3  *
4  * Created on: 10/10/2015
5  * Author: Victor
6  */
7 #include "LPC17xx.h"
8 #include <stdio.h>
9 #include "uart.h"
10 #include "PrintF.h"
11 #include <string.h>
12 #include "Type.h"
13 #include "Config.h"
14 #include <stdarg.h>
15 
16 #include "FreeRTOS.h"
17 #include "semphr.h"
18 
19 #define BUFSIZE (BUFSIZE_WIFLY)
20 
21 /* Es declara la variables de tipus xSemaphoreHandle.*/
24 
25 
26 
27 uint32_t PrintF_start( uint32_t PortNum, uint32_t baudRate )
28 {
29  switch( PortNum )
30  {
31  case UART0PORT:
32  /* Es crea el semàfor si no està creat */
33  if( xMutexUART0 == NULL )
34  {
35  xMutexUART0 = xSemaphoreCreateMutex();
36  }
37  /*S'utilitza el semàfor per inicialitzar el Port */
38  xSemaphoreTake( xMutexUART0, portMAX_DELAY );
39  {
40  UARTInit(PortNum, baudRate); /* baud rate setting */
41  }
42  xSemaphoreGive( xMutexUART0 );
43  return (TRUE);
44 
45  case UART1PORT:
46  /* Es crea el semàfor si no està creat */
47  if( xMutexUART1 == NULL )
48  {
49  xMutexUART1 = xSemaphoreCreateMutex();
50  }
51  /*S'utilitza el semàfor per inicialitzar el Port */
52  xSemaphoreTake( xMutexUART1, portMAX_DELAY );
53  {
54  UARTInit(PortNum, baudRate); /* baud rate setting */
55  }
56  xSemaphoreGive( xMutexUART1 );
57  return (TRUE);
58 
59  case UART2PORT:
60  /* Es crea el semàfor si no està creat */
61  if( xMutexUART2 == NULL )
62  {
63  xMutexUART2 = xSemaphoreCreateMutex();
64  }
65  /*S'utilitza el semàfor per inicialitzar el Port */
66  xSemaphoreTake( xMutexUART2, portMAX_DELAY );
67  {
68  UARTInit(PortNum, baudRate); /* baud rate setting */
69  }
70  xSemaphoreGive( xMutexUART2 );
71  return (TRUE);
72 
73  case UART3PORT:
74  /* Es crea el semàfor si no està creat */
75  if( xMutexUART3 == NULL )
76  {
77  xMutexUART3 = xSemaphoreCreateMutex();
78  }
79  /*S'utilitza el semàfor per inicialitzar el Port */
80  xSemaphoreTake( xMutexUART3, portMAX_DELAY );
81  {
82  UARTInit(PortNum, baudRate); /* baud rate setting */
83  }
84  xSemaphoreGive( xMutexUART3 );
85  return (TRUE);
86  }
87 
88  return (FALSE);
89 }
90 
91 uint32_t PrintF_stop( uint32_t PortNum)
92 {
93  switch( PortNum )
94  {
95  case UART0PORT:
96  /*S'utilitza el semàfor per inicialitzar el Port */
97  xSemaphoreTake( xMutexUART0, portMAX_DELAY );
98  {
99  UARTClose(PortNum); /* baud rate setting */
100  }
101  xSemaphoreGive( xMutexUART0 );
102  return (TRUE);
103 
104  case UART1PORT:
105  /*S'utilitza el semàfor per inicialitzar el Port */
106  xSemaphoreTake( xMutexUART1, portMAX_DELAY );
107  {
108  UARTClose(PortNum); /* baud rate setting */
109  }
110  xSemaphoreGive( xMutexUART1 );
111  return (TRUE);
112 
113  case UART2PORT:
114 
115  /*S'utilitza el semàfor per inicialitzar el Port */
116  xSemaphoreTake( xMutexUART2, portMAX_DELAY );
117  {
118  UARTClose(PortNum); /* baud rate setting */
119  }
120  xSemaphoreGive( xMutexUART2 );
121  return (TRUE);
122 
123  case UART3PORT:
124 
125  /*S'utilitza el semàfor per inicialitzar el Port */
126  xSemaphoreTake( xMutexUART3, portMAX_DELAY );
127  {
128  UARTClose(PortNum); /* baud rate setting */
129  }
130  xSemaphoreGive( xMutexUART3 );
131  return (TRUE);
132  }
133 
134  return (FALSE);
135 }
136 
137 void PrintF_print( uint32_t PortNum, const char * restrict format, ...)
138 {
139  int lenBuff;
140  va_list arg;
141  va_start( arg, format );
142 
143  PrintF_ClBuffUart(PortNum);
144 
145  /* Creació dels semàfors segons la UART a escriure */
146  switch( PortNum )
147  {
148  case UART0PORT:
149  /* Es comprova que el semàfor està creat */
150 
151  if( xMutexUART0 != NULL )
152  {
153  lenBuff = vsprintf(cUART0Buffer, format, arg);
154  xSemaphoreTake( xMutexUART0, portMAX_DELAY );
155  {
156  UARTSend(PortNum, (uint8_t *)cUART0Buffer , lenBuff );
157  }
158  }
159  xSemaphoreGive( xMutexUART0 );
160  break;
161 
162  case UART1PORT:
163  if( xMutexUART1 != NULL )
164  {
165  lenBuff = vsprintf(cUART1Buffer, format, arg);
166  xSemaphoreTake( xMutexUART1, portMAX_DELAY );
167  {
168 
169  UARTSend(PortNum, (uint8_t *)cUART1Buffer, lenBuff);
170  }
171  }
172  xSemaphoreGive( xMutexUART1 );
173  break;
174 
175  case UART2PORT:
176  /* Es comprova que el semàfor està creat */
177  if( xMutexUART2 != NULL )
178  {
179  lenBuff = vsprintf(cUART2Buffer, format, arg);
180  xSemaphoreTake( xMutexUART2, portMAX_DELAY );
181  {
182  UARTSend(PortNum, (uint8_t *)cUART2Buffer , lenBuff );
183  }
184  }
185  xSemaphoreGive( xMutexUART2 );
186  break;
187 
188  case UART3PORT:
189  /* Es comprova que el semàfor està creat */
190  if( xMutexUART3 != NULL )
191  {
192  lenBuff = vsprintf(cUART3Buffer, format, arg);
193  xSemaphoreTake( xMutexUART3, portMAX_DELAY );
194  {
195  UARTSend(PortNum, (uint8_t *)cUART3Buffer , lenBuff );
196  }
197  }
198  xSemaphoreGive( xMutexUART3 );
199  break;
200  }
201  va_end( arg );
202  PrintF_ClBuffUart(PortNum);
203  return;
204 }
205 
206 void PrintF_scan(uint8_t port, char* string, uint32_t timeout)
207 {
208  /* Creació dels semàfors segons la UART a escriure */
209  if(port == UART0PORT)
210  {
211  xSemaphoreTake( xMutexUART0, portMAX_DELAY );
212  {
213  UARTReceive(port, string, timeout);
214  }
215  xSemaphoreGive( xMutexUART0 );
216  }
217  else if(port == UART1PORT)
218  {
219  xSemaphoreTake( xMutexUART1, portMAX_DELAY );
220  {
221  UARTReceive(port, string, timeout);
222  }
223  xSemaphoreGive( xMutexUART1 );
224  }
225  else if(port == UART2PORT)
226  {
227  xSemaphoreTake( xMutexUART2, portMAX_DELAY );
228  {
229  UARTReceive(port, string, timeout);
230  }
231  xSemaphoreGive( xMutexUART2 );
232  }
233  else if(port == UART3PORT)
234  {
235  xSemaphoreTake( xMutexUART3, portMAX_DELAY );
236  {
237  UARTReceive(port, string, timeout);
238  }
239  xSemaphoreGive( xMutexUART3 );
240  }
241  return;
242 }
243 
244 uint8_t PrintF_ChkScan(uint8_t PortNum, char *strScan, char *strExp, uint32_t timeout)
245 {
246  uint8_t rtn = TRUE;
247 
248  PrintF_ClBuffUart(PortNum);
249  PrintF_scan(PortNum, strScan, timeout);
250 
251  if (strstr(strScan, strExp) == NULL)
252  rtn = FALSE;
253  PrintF_ClBuffUart(PortNum);
254 
255  return rtn;
256 }
257 
258 void PrintF_ClBuffUart(uint8_t portNum)
259 {
260  clearBuffUart(portNum);
261  switch (portNum)
262  {
263  case UART0PORT:
264  memset((uint8_t *)cUART0Buffer, 0x00, BUFSIZE);
265  break;
266  case UART1PORT:
267  memset((uint8_t *)cUART1Buffer, 0x00, BUFSIZE);
268  break;
269  case UART2PORT:
270  memset((uint8_t *)cUART2Buffer, 0x00, BUFSIZE);
271  break;
272  case UART3PORT:
273  memset((uint8_t *)cUART3Buffer, 0x00, BUFSIZE);
274  break;
275  }
276 }
277 
278 void PrintF_delayMs( uint32_t ms)
279 {
280  delayMs(ms,0);
281 }
void PrintF_scan(uint8_t port, char *string, uint32_t timeout)
Definition: PrintF.c:206
char cUART3Buffer[BUFSIZE]
Definition: PrintF.c:23
char cUART2Buffer[BUFSIZE]
Definition: PrintF.c:23
void delayMs(uint32_t ms, uint8_t type)
void PrintF_delayMs(uint32_t ms)
Definition: PrintF.c:278
xSemaphoreHandle xMutexUART0
Definition: PrintF.c:22
#define UART0PORT
Definition: PrintF.h:13
void UARTSend(uint32_t portNum, uint8_t *BufferPtr, uint32_t Length)
Definition: uart.c:538
xSemaphoreHandle xMutexUART3
Definition: PrintF.c:22
#define UART2PORT
Definition: PrintF.h:15
#define FALSE
Definition: type.h:22
#define TRUE
Definition: type.h:26
uint32_t PrintF_stop(uint32_t PortNum)
Definition: PrintF.c:91
void PrintF_ClBuffUart(uint8_t portNum)
Definition: PrintF.c:258
char cUART1Buffer[BUFSIZE]
Definition: PrintF.c:23
#define NULL
Definition: type.h:18
xSemaphoreHandle xMutexUART2
Definition: PrintF.c:22
uint32_t UARTInit(uint32_t portNum, uint32_t baudRate)
Definition: uart.c:339
uint32_t PrintF_start(uint32_t PortNum, uint32_t baudRate)
Definition: PrintF.c:27
char cUART0Buffer[BUFSIZE]
Definition: PrintF.c:23
xSemaphoreHandle xMutexUART1
Definition: PrintF.c:22
#define UART1PORT
Definition: PrintF.h:14
#define UART3PORT
Definition: PrintF.h:16
#define BUFSIZE
Definition: PrintF.c:19
int UARTReceive(uint8_t portNum, char *buffer, uint32_t timeout)
Definition: uart.c:592
uint8_t PrintF_ChkScan(uint8_t PortNum, char *strScan, char *strExp, uint32_t timeout)
Definition: PrintF.c:244
void clearBuffUart(uint8_t portNum)
Definition: uart.c:680
void PrintF_print(uint32_t PortNum, const char *restrict format,...)
Definition: PrintF.c:137
uint32_t UARTClose(uint8_t PortNum)
Definition: uart.c:498