2 #include "trcRecorder.h"
\r
4 #if (TRC_USE_TRACEALYZER_RECORDER == 1)
\r
5 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
\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
19 uint8_t data[BUFSIZE];
\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
26 /* Received Data over USB are stored in this buffer */
\r
27 uint8_t UserRxBufferFS[APP_RX_DATA_SIZE];
\r
29 /* Send Data over USB CDC are stored in this buffer */
\r
30 uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];
\r
32 extern USBD_HandleTypeDef hUsbDeviceFS;
\r
34 extern PCD_HandleTypeDef hpcd_USB_OTG_FS;
\r
36 recBuf commandBuffer;
\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
43 USBD_CDC_ItfTypeDef USBD_Interface_fops_FS =
\r
51 /* Private functions ---------------------------------------------------------*/
\r
53 * @brief CDC_Init_FS
\r
54 * Initializes the CDC media low layer over the FS USB IP
\r
56 * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
\r
58 static int8_t CDC_Init_FS(void)
\r
60 /* Set Application Buffers */
\r
61 USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBufferFS, 0);
\r
62 USBD_CDC_SetRxBuffer(&hUsbDeviceFS, UserRxBufferFS);
\r
67 * @brief CDC_DeInit_FS
\r
68 * DeInitializes the CDC media low layer
\r
70 * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
\r
72 static int8_t CDC_DeInit_FS(void)
\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
85 static int8_t CDC_Control_FS (uint8_t cmd, uint8_t* pbuf, uint16_t length)
\r
89 case CDC_SEND_ENCAPSULATED_COMMAND:
\r
92 case CDC_GET_ENCAPSULATED_RESPONSE:
\r
95 case CDC_SET_COMM_FEATURE:
\r
98 case CDC_GET_COMM_FEATURE:
\r
101 case CDC_CLEAR_COMM_FEATURE:
\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
119 /* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */
\r
120 /*******************************************************************************/
\r
121 case CDC_SET_LINE_CODING:
\r
124 case CDC_GET_LINE_CODING:
\r
127 case CDC_SET_CONTROL_LINE_STATE:
\r
130 case CDC_SEND_BREAK:
\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
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
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
154 static int8_t CDC_Receive_FS (uint8_t* Buf, uint32_t *Len)
\r
156 for( uint32_t i=0;i<* Len;i++)
\r
158 commandBuffer.data[commandBuffer.idx]=Buf[i];
\r
159 commandBuffer.idx++;
\r
161 USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
\r
162 USBD_CDC_ReceivePacket(&hUsbDeviceFS);
\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
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
178 uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)
\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
185 USBD_CDC_SetTxBuffer(&hUsbDeviceFS, Buf, Len);
\r
186 result = USBD_CDC_TransmitPacket(&hUsbDeviceFS);
\r
190 /* The READ function, used in trcStreamingPort.h */
\r
191 int32_t trcCDCReceive(void *data, uint32_t size, int32_t* NumBytes)
\r
195 if(commandBuffer.idx>0)
\r
197 if (size >= commandBuffer.idx) // more than what is stored, number of bytes will be .idx
\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
203 else //If some data in the buffer is not read
\r
205 diff = commandBuffer.idx-size;
\r
206 memcpy(data,commandBuffer.data, size);
\r
207 for(i=0;i<diff;i++)
\r
209 commandBuffer.data[i]=commandBuffer.data[i+size];
\r
212 commandBuffer.idx=diff;
\r
222 /* The WRITE function, used in trcStreamingPort.h */
\r
223 int32_t trcCDCTransmit(void* data, uint32_t size, int32_t * noOfBytesSent )
\r
226 result=CDC_Transmit_FS(data, size);
\r
227 *noOfBytesSent = size;
\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
237 * @brief This function handles USB On The Go FS global interrupt.
\r
239 void OTG_FS_IRQHandler(void)
\r
241 HAL_PCD_IRQHandler(&hpcd_USB_OTG_FS);
\r
244 #endif /*(TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)*/
\r
245 #endif /*(TRC_USE_TRACEALYZER_RECORDER == 1)*/
\r