1 #include "trcRecorder.h"
\r
3 #if (TRC_USE_TRACEALYZER_RECORDER == 1)
\r
4 #if(TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
\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
18 uint8_t data[BUFSIZE];
\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
25 /* Received Data over USB are stored in this buffer */
\r
26 uint8_t UserRxBufferFS[APP_RX_DATA_SIZE];
\r
28 /* Send Data over USB CDC are stored in this buffer */
\r
29 uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];
\r
31 extern USBD_HandleTypeDef hUsbDeviceFS;
\r
32 extern PCD_HandleTypeDef hpcd_USB_OTG_FS;
\r
35 recBuf commandBuffer;
\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
42 USBD_CDC_ItfTypeDef USBD_Interface_fops_FS =
\r
50 /* Private functions ---------------------------------------------------------*/
\r
52 * @brief CDC_Init_FS
\r
53 * Initializes the CDC media low layer over the FS USB IP
\r
55 * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
\r
57 static int8_t CDC_Init_FS(void)
\r
59 /* Set Application Buffers */
\r
60 USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBufferFS, 0);
\r
61 USBD_CDC_SetRxBuffer(&hUsbDeviceFS, UserRxBufferFS);
\r
66 * @brief CDC_DeInit_FS
\r
67 * DeInitializes the CDC media low layer
\r
69 * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
\r
71 static int8_t CDC_DeInit_FS(void)
\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
84 static int8_t CDC_Control_FS (uint8_t cmd, uint8_t* pbuf, uint16_t length)
\r
88 case CDC_SEND_ENCAPSULATED_COMMAND:
\r
91 case CDC_GET_ENCAPSULATED_RESPONSE:
\r
94 case CDC_SET_COMM_FEATURE:
\r
97 case CDC_GET_COMM_FEATURE:
\r
100 case CDC_CLEAR_COMM_FEATURE:
\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
118 /* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */
\r
119 /*******************************************************************************/
\r
120 case CDC_SET_LINE_CODING:
\r
123 case CDC_GET_LINE_CODING:
\r
126 case CDC_SET_CONTROL_LINE_STATE:
\r
129 case CDC_SEND_BREAK:
\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
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
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
153 static int8_t CDC_Receive_FS (uint8_t* Buf, uint32_t *Len)
\r
155 for( uint32_t i=0;i<* Len;i++)
\r
157 commandBuffer.data[commandBuffer.idx]=Buf[i];
\r
158 commandBuffer.idx++;
\r
160 USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
\r
161 USBD_CDC_ReceivePacket(&hUsbDeviceFS);
\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
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
177 uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)
\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
184 USBD_CDC_SetTxBuffer(&hUsbDeviceFS, Buf, Len);
\r
185 result = USBD_CDC_TransmitPacket(&hUsbDeviceFS);
\r
189 int32_t trcCDCReceive(void *data, uint32_t size, int32_t* NumBytes)
\r
193 if(commandBuffer.idx>0)
\r
195 if (size >= commandBuffer.idx) // more than what is stored, number of bytes will be .idx
\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
201 else //If some data in the buffer is not read
\r
203 diff = commandBuffer.idx-size;
\r
204 memcpy(data,commandBuffer.data, size);
\r
205 for(i=0;i<diff;i++)
\r
207 commandBuffer.data[i]=commandBuffer.data[i+size];
\r
210 commandBuffer.idx=diff;
\r
220 int32_t trcCDCTransmit(void* data, uint32_t size, int32_t * noOfBytesSent )
\r
223 result=CDC_Transmit_FS(data,size);
\r
224 *noOfBytesSent=size;
\r
230 * @brief This function handles USB On The Go FS global interrupt.
\r
232 void OTG_FS_IRQHandler(void)
\r
234 HAL_PCD_IRQHandler(&hpcd_USB_OTG_FS);
\r
237 #endif /*(TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)*/
\r
238 #endif /*(TRC_USE_TRACEALYZER_RECORDER == 1)*/
\r