]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-Trace/streamports/USB_CDC/trcStreamingPort.c
Update trace recorder code.
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-Trace / streamports / USB_CDC / trcStreamingPort.c
1 \r
2 #include "trcRecorder.h"\r
3 \r
4 #if (TRC_USE_TRACEALYZER_RECORDER == 1)\r
5 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)\r
6 \r
7 #include "stdint.h"\r
8 \r
9 /* Include files as needed, in this case it is files from STM32Cube FW_F7 V1.4.1 */\r
10 #include "usb_device.h"\r
11 #include "usbd_cdc.h"\r
12 #include "usbd_CDC_if.h"\r
13 #include "usb_device.h"\r
14 \r
15 #define BUFSIZE 64\r
16         \r
17 typedef struct{\r
18         uint32_t idx;\r
19         uint8_t data[BUFSIZE];\r
20 }recBuf;\r
21 \r
22 /* Define size for the receive and transmit buffer over CDC */\r
23 #define APP_RX_DATA_SIZE  8\r
24 #define APP_TX_DATA_SIZE  64\r
25 \r
26 /* Received Data over USB are stored in this buffer       */\r
27 uint8_t UserRxBufferFS[APP_RX_DATA_SIZE];\r
28 \r
29 /* Send Data over USB CDC are stored in this buffer       */\r
30 uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];\r
31 \r
32 extern USBD_HandleTypeDef hUsbDeviceFS;\r
33 \r
34 extern PCD_HandleTypeDef hpcd_USB_OTG_FS;\r
35 \r
36 recBuf commandBuffer;\r
37 \r
38 static int8_t CDC_Init_FS     (void);\r
39 static int8_t CDC_DeInit_FS   (void);\r
40 static int8_t CDC_Control_FS  (uint8_t cmd, uint8_t* pbuf, uint16_t length);\r
41 static int8_t CDC_Receive_FS  (uint8_t* pbuf, uint32_t *Len);\r
42   \r
43 USBD_CDC_ItfTypeDef USBD_Interface_fops_FS = \r
44 {\r
45   CDC_Init_FS,\r
46   CDC_DeInit_FS,\r
47   CDC_Control_FS,  \r
48   CDC_Receive_FS\r
49 };\r
50 \r
51 /* Private functions ---------------------------------------------------------*/\r
52 /**\r
53   * @brief  CDC_Init_FS\r
54   *         Initializes the CDC media low layer over the FS USB IP\r
55   * @param  None\r
56   * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL\r
57   */\r
58 static int8_t CDC_Init_FS(void)\r
59\r
60   /* Set Application Buffers */\r
61   USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBufferFS, 0);\r
62   USBD_CDC_SetRxBuffer(&hUsbDeviceFS, UserRxBufferFS);\r
63   return (USBD_OK);\r
64 }\r
65 \r
66 /**\r
67   * @brief  CDC_DeInit_FS\r
68   *         DeInitializes the CDC media low layer\r
69   * @param  None\r
70   * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL\r
71   */\r
72 static int8_t CDC_DeInit_FS(void)\r
73\r
74   return (USBD_OK);\r
75 }\r
76 \r
77 /**\r
78   * @brief  CDC_Control_FS\r
79   *         Manage the CDC class requests\r
80   * @param  cmd: Command code            \r
81   * @param  pbuf: Buffer containing command data (request parameters)\r
82   * @param  length: Number of data to be sent (in bytes)\r
83   * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL\r
84   */\r
85 static int8_t CDC_Control_FS  (uint8_t cmd, uint8_t* pbuf, uint16_t length)\r
86\r
87         switch (cmd)\r
88   {\r
89   case CDC_SEND_ENCAPSULATED_COMMAND:\r
90                 break;\r
91 \r
92   case CDC_GET_ENCAPSULATED_RESPONSE:\r
93                 break;\r
94 \r
95   case CDC_SET_COMM_FEATURE:\r
96                 break;\r
97 \r
98   case CDC_GET_COMM_FEATURE:\r
99                 break;\r
100 \r
101   case CDC_CLEAR_COMM_FEATURE:\r
102                 break;\r
103 \r
104   /*******************************************************************************/\r
105   /* Line Coding Structure                                                       */\r
106   /*-----------------------------------------------------------------------------*/\r
107   /* Offset | Field       | Size | Value  | Description                          */\r
108   /* 0      | dwDTERate   |   4  | Number |Data terminal rate, in bits per second*/\r
109   /* 4      | bCharFormat |   1  | Number | Stop bits                            */\r
110   /*                                        0 - 1 Stop bit                       */\r
111   /*                                        1 - 1.5 Stop bits                    */\r
112   /*                                        2 - 2 Stop bits                      */\r
113   /* 5      | bParityType |  1   | Number | Parity                               */\r
114   /*                                        0 - None                             */\r
115   /*                                        1 - Odd                              */ \r
116   /*                                        2 - Even                             */\r
117   /*                                        3 - Mark                             */\r
118   /*                                        4 - Space                            */\r
119   /* 6      | bDataBits  |   1   | Number Data bits (5, 6, 7, 8 or 16).          */\r
120   /*******************************************************************************/\r
121   case CDC_SET_LINE_CODING:\r
122                 break;\r
123 \r
124   case CDC_GET_LINE_CODING:\r
125                 break;\r
126 \r
127   case CDC_SET_CONTROL_LINE_STATE:\r
128                 break;\r
129 \r
130   case CDC_SEND_BREAK:\r
131                 break;    \r
132     \r
133   default:\r
134     break;\r
135   }\r
136   return (USBD_OK);\r
137 }\r
138 \r
139 /**\r
140   * @brief  CDC_Receive_FS\r
141   *         Data received over USB OUT endpoint are sent over CDC interface \r
142   *         through this function.\r
143   *           \r
144   *         @note\r
145   *         This function will block any OUT packet reception on USB endpoint \r
146   *         until exiting this function. If you exit this function before transfer\r
147   *         is complete on CDC interface (i.e. using DMA controller) it will result \r
148   *         in receiving more data while previous ones are still not sent.\r
149   *                 \r
150   * @param  Buf: Buffer of data to be received\r
151   * @param  Len: Number of data received (in bytes)\r
152   * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL\r
153   */\r
154 static int8_t CDC_Receive_FS (uint8_t* Buf, uint32_t *Len)\r
155 {\r
156         for( uint32_t i=0;i<* Len;i++)\r
157         {               \r
158                 commandBuffer.data[commandBuffer.idx]=Buf[i];\r
159                 commandBuffer.idx++;\r
160         }       \r
161   USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);\r
162   USBD_CDC_ReceivePacket(&hUsbDeviceFS);        \r
163 \r
164   return (USBD_OK);\r
165 }\r
166 \r
167 /**\r
168   * @brief  CDC_Transmit_FS\r
169   *         Data send over USB IN endpoint are sent over CDC interface \r
170   *         through this function.           \r
171   *         @note\r
172   *         \r
173   *                 \r
174   * @param  Buf: Buffer of data to be send\r
175   * @param  Len: Number of data to be send (in bytes)\r
176   * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY\r
177   */\r
178 uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)\r
179 {\r
180   uint8_t result = USBD_OK;\r
181   USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData;\r
182   if (hcdc->TxState != 0){\r
183     return USBD_BUSY;\r
184   }\r
185   USBD_CDC_SetTxBuffer(&hUsbDeviceFS, Buf, Len);\r
186   result = USBD_CDC_TransmitPacket(&hUsbDeviceFS);\r
187   return result;\r
188 }\r
189 \r
190 /* The READ function, used in trcStreamingPort.h */\r
191 int32_t trcCDCReceive(void *data, uint32_t size, int32_t* NumBytes)\r
192 {\r
193         uint32_t i,diff;\r
194 \r
195         if(commandBuffer.idx>0)\r
196         {\r
197                 if (size >= commandBuffer.idx) // more than what is stored, number of bytes will be .idx\r
198                 {\r
199                         memcpy(data,commandBuffer.data, commandBuffer.idx);\r
200                         *NumBytes=commandBuffer.idx;\r
201                         commandBuffer.idx=0; // Make the buffer ready for a new command\r
202                 }\r
203                 else  //If some data in the buffer is not read\r
204                 {\r
205                         diff = commandBuffer.idx-size;\r
206                         memcpy(data,commandBuffer.data, size);\r
207                         for(i=0;i<diff;i++)\r
208                         {\r
209                                 commandBuffer.data[i]=commandBuffer.data[i+size];\r
210                         }\r
211                         *NumBytes=size;\r
212                         commandBuffer.idx=diff;\r
213                 }\r
214         }\r
215         else\r
216         {\r
217                 *NumBytes=0;\r
218         }\r
219         return 0;\r
220 }\r
221 \r
222 /* The WRITE function, used in trcStreamingPort.h */\r
223 int32_t trcCDCTransmit(void* data, uint32_t size, int32_t * noOfBytesSent )\r
224 {\r
225         int32_t result;\r
226         result=CDC_Transmit_FS(data, size);\r
227         *noOfBytesSent = size;\r
228         \r
229         /* Return value should be 0 on success (not sure what the value of USBD_OK is) */\r
230         if (result == USBD_OK)\r
231                 return 0;\r
232         else\r
233                 return -1;\r
234 }\r
235 \r
236 /**\r
237 * @brief This function handles USB On The Go FS global interrupt.\r
238 */\r
239 void OTG_FS_IRQHandler(void)\r
240 {\r
241   HAL_PCD_IRQHandler(&hpcd_USB_OTG_FS);\r
242 }\r
243 \r
244 #endif  /*(TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)*/\r
245 #endif  /*(TRC_USE_TRACEALYZER_RECORDER == 1)*/\r
246 \r