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