CLEON  Version 1
Cloud-Offloaded GPS Receiver
sys_usb.c
Go to the documentation of this file.
1 
7 #include "cleon_conf.h"
8 #include "app_define.h"
9 #include "sys_define.h"
10 #include "hal_define.h"
11 #include "fs_define.h"
12 
13 // CLEON data structure
17 
18 // Flags
21 
22 // USB frames
25 
26 // Counting received number of USB frame bytes for state transition
27 unsigned char ucUSBFrameByteCount = 0;
28 
29 /*----------------------------------------------------------------------------*/
38 {
39  USB_PORT(SEL) |= BV(USB_TXD_PIN);
40  USB_PORT(SEL) |= BV(USB_RXD_PIN);
41 
45 
48  }
49 }
50 
51 /*----------------------------------------------------------------------------*/
59 void SYS_USB_SendACK(unsigned char ucCommand)
60 {
61  memset(&uniUSBSendFrame, 0 , sizeof(uniUSBSendFrame));
62 
63  uniUSBSendFrame.stUSBFrame.ucHeader[0] = USB_FRAME_HEADER;
64  uniUSBSendFrame.stUSBFrame.ucHeader[1] = USB_FRAME_HEADER;
65 
66  uniUSBSendFrame.stUSBFrame.ucLength = 0x02;
67 
68  uniUSBSendFrame.stUSBFrame.ucCommand = USB_FRAME_COMMAND_ACK;
69 
70  uniUSBSendFrame.stUSBFrame.ucData[0] = ucCommand;
71 
72  uniUSBSendFrame.stUSBFrame.ucCRC = SYS_USB_BuildCRC(&uniUSBSendFrame);
73 
74  uniUSBSendFrame.stUSBFrame.ucFooter[0] = USB_FRAME_FOOTER;
75  uniUSBSendFrame.stUSBFrame.ucFooter[1] = USB_FRAME_FOOTER;
76 
77  for(int i = 0; i < USB_FRAME_LENGTH ; i++){
78  SYS_USB_TransmitSingleByte(uniUSBSendFrame.ucSingleByte[i]);
79  }
80 }
81 
82 /*----------------------------------------------------------------------------*/
91 int SYS_USB_Printf(char *format, ... )
92 {
93  // Maximum length of data is 255
94  char buff[250];
95  int index = 0;
96  unsigned char data = 0;
97  va_list argptr; // Argument list
98 
99  buff[0] = 0x00;
100  va_start( argptr,format); // Initializing the function
101  vsprintf(buff,format,argptr ); // Copying buffer
102  va_end( argptr ); // Finalizing the function
103 
104  data = buff[index++]; // Loading data
105 
106  while(data != 0x00){
107  //USCI_A1 TX buffer ready?
108  while (!(UCA1IFG&UCTXIFG)); // USCI_A1 TX buffer ready?
109  UCA1TXBUF = data;
110  data = buff[index++];
111  }
112  return(index);
113 }
114 
115 /*----------------------------------------------------------------------------*/
123 void SYS_USB_TransmitSingleByte(unsigned char ucData)
124 {
125  //USCI_A1 TX buffer ready?
126  while (!(UCA1IFG&UCTXIFG)); // USCI_A1 TX buffer ready?
127  UCA1TXBUF = ucData;
128 }
129 
130 /*----------------------------------------------------------------------------*/
138 void SYS_USB_ReceiveFrame(unsigned char ucData)
139 {
141  switch (ucUSBFrameByteCount){
142  case 0: case 1:
143  if(ucData == USB_FRAME_HEADER){
144  uniUSBRecvFrame.stUSBFrame.ucHeader[ucUSBFrameByteCount] = ucData;
146  }else{
148  // request retransmission
149  }
150  break;
151  case 2:
152  if((uniUSBRecvFrame.stUSBFrame.ucHeader[0] == USB_FRAME_HEADER)&&(uniUSBRecvFrame.stUSBFrame.ucHeader[1] == USB_FRAME_HEADER) &&
153  (ucData > 0) && (ucData < USB_FRAME_COMMAND_AND_DATA_FIELD_LENGTH)){
154  uniUSBRecvFrame.stUSBFrame.ucLength = ucData;
156  }else{
158  // request retransmission
159  }
160  break;
161  case 3:
162  uniUSBRecvFrame.stUSBFrame.ucCommand = ucData;
164 
165  break;
166  case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11: case 12: case 13:
167  case 14: case 15: case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23:
168  case 24: case 25: case 26: case 27: case 28: case 29: case 30: case 31: case 32:
169  uniUSBRecvFrame.stUSBFrame.ucData[ucUSBFrameByteCount - 4] = ucData;
171  break;
172  case 33:
173  if(ucData == SYS_USB_BuildCRC(&uniUSBRecvFrame)){
174  uniUSBRecvFrame.stUSBFrame.ucCRC = ucData;
176  }else{
178  // request retransmission
179  }
180  break;
181  case 34:
182  if(ucData == USB_FRAME_FOOTER){
183  uniUSBRecvFrame.stUSBFrame.ucFooter[ucUSBFrameByteCount - 34] = ucData;
185  }else{
187  // request retransmission
188  }
189  break;
190  case 35:
191  if(ucData == USB_FRAME_FOOTER){
192  uniUSBRecvFrame.stUSBFrame.ucFooter[ucUSBFrameByteCount - 34] = ucData;
195  }else{
197  // request retransmission
198  }
199  break;
200  default:
201  // request retransmission
202  break;
203  }
204  }
205 }
206 
207 /*----------------------------------------------------------------------------*/
215 unsigned char SYS_USB_BuildCRC(usb_frame_u *uniUSBFrame)
216 {
217  unsigned char ucCheckOctet = 0;
218 
219  for ( int i = 3 ; i < (USB_FRAME_LENGTH - 3) ; i++ ){
220  if( i == 0 ){
221  ucCheckOctet = (*uniUSBFrame).ucSingleByte[i];
222  }
223  if (i >= 1)
224  {
225  ucCheckOctet ^= (*uniUSBFrame).ucSingleByte[i];
226  }
227  }
228 
229  ucCheckOctet = ~ucCheckOctet;
230 
231  return ucCheckOctet;
232 }
233 
234 
235 
236 
237 
238 
239 
240 
241 
242 
243 
244 
245 
246 
247 
248 
249 
250 
251 
252 
253 
254 
255 
256 
257 
258 
259 
260 
261 
262