Change to new timer definitions
[inav.git] / src / main / vcpf4 / usbd_cdc_vcp.c
blob67746fe63cbe3b30fa89c50f0d522062c7728e93
1 /**
2 ******************************************************************************
3 * @file usbd_cdc_vcp.c
4 * @author MCD Application Team
5 * @version V1.0.0
6 * @date 22-July-2011
7 * @brief Generic media access Layer.
8 ******************************************************************************
9 * @attention
11 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
12 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
13 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
14 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
15 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
16 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
18 * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
19 ******************************************************************************
22 #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
23 #pragma data_alignment = 4
24 #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
26 /* Includes ------------------------------------------------------------------*/
27 #include "usbd_cdc_vcp.h"
28 #include "stm32f4xx_conf.h"
29 #include "stdbool.h"
30 #include "drivers/time.h"
32 LINE_CODING g_lc;
34 extern __IO uint8_t USB_Tx_State;
35 __IO uint32_t bDeviceState = UNCONNECTED; /* USB device status */
37 /* These are external variables imported from CDC core to be used for IN transfer management. */
39 /* This is the buffer for data received from the MCU to APP (i.e. MCU TX, APP RX) */
40 extern uint8_t APP_Rx_Buffer[];
41 extern uint32_t APP_Rx_ptr_out;
42 /* Increment this buffer position or roll it back to
43 start address when writing received data
44 in the buffer APP_Rx_Buffer. */
45 extern uint32_t APP_Rx_ptr_in;
48 APP TX is the circular buffer for data that is transmitted from the APP (host)
49 to the USB device (flight controller).
51 static uint8_t APP_Tx_Buffer[APP_TX_DATA_SIZE];
52 static uint32_t APP_Tx_ptr_out = 0;
53 static uint32_t APP_Tx_ptr_in = 0;
55 /* Private function prototypes -----------------------------------------------*/
56 static uint16_t VCP_Init(void);
57 static uint16_t VCP_DeInit(void);
58 static uint16_t VCP_Ctrl(uint32_t Cmd, uint8_t* Buf, uint32_t Len);
59 static uint16_t VCP_DataTx(const uint8_t* Buf, uint32_t Len);
60 static uint16_t VCP_DataRx(uint8_t* Buf, uint32_t Len);
62 CDC_IF_Prop_TypeDef VCP_fops = {VCP_Init, VCP_DeInit, VCP_Ctrl, VCP_DataTx, VCP_DataRx };
64 /* Private functions ---------------------------------------------------------*/
65 /**
66 * @brief VCP_Init
67 * Initializes the Media on the STM32
68 * @param None
69 * @retval Result of the opeartion (USBD_OK in all cases)
71 static uint16_t VCP_Init(void)
73 bDeviceState = CONFIGURED;
74 return USBD_OK;
77 /**
78 * @brief VCP_DeInit
79 * DeInitializes the Media on the STM32
80 * @param None
81 * @retval Result of the opeartion (USBD_OK in all cases)
83 static uint16_t VCP_DeInit(void)
85 bDeviceState = UNCONNECTED;
86 return USBD_OK;
89 void ust_cpy(LINE_CODING* plc2, const LINE_CODING* plc1)
91 plc2->bitrate = plc1->bitrate;
92 plc2->format = plc1->format;
93 plc2->paritytype = plc1->paritytype;
94 plc2->datatype = plc1->datatype;
97 /**
98 * @brief VCP_Ctrl
99 * Manage the CDC class requests
100 * @param Cmd: Command code
101 * @param Buf: Buffer containing command data (request parameters)
102 * @param Len: Number of data to be sent (in bytes)
103 * @retval Result of the opeartion (USBD_OK in all cases)
105 static uint16_t VCP_Ctrl(uint32_t Cmd, uint8_t* Buf, uint32_t Len)
107 (void)Len;
108 LINE_CODING* plc = (LINE_CODING*)Buf;
110 assert_param(Len>=sizeof(LINE_CODING));
112 switch (Cmd) {
113 /* Not needed for this driver, AT modem commands */
114 case SEND_ENCAPSULATED_COMMAND:
115 case GET_ENCAPSULATED_RESPONSE:
116 break;
118 // Not needed for this driver
119 case SET_COMM_FEATURE:
120 case GET_COMM_FEATURE:
121 case CLEAR_COMM_FEATURE:
122 break;
125 //Note - hw flow control on UART 1-3 and 6 only
126 case SET_LINE_CODING:
127 ust_cpy(&g_lc, plc); //Copy into structure to save for later
128 break;
131 case GET_LINE_CODING:
132 ust_cpy(plc, &g_lc);
133 break;
136 case SET_CONTROL_LINE_STATE:
137 /* Not needed for this driver */
138 //RSW - This tells how to set RTS and DTR
139 break;
141 case SEND_BREAK:
142 /* Not needed for this driver */
143 break;
145 default:
146 break;
149 return USBD_OK;
152 /*******************************************************************************
153 * Function Name : Send DATA .
154 * Description : send the data received from the STM32 to the PC through USB
155 * Input : buffer to send, and the length of the buffer.
156 * Output : None.
157 * Return : None.
158 *******************************************************************************/
159 uint32_t CDC_Send_DATA(const uint8_t *ptrBuffer, uint32_t sendLength)
161 VCP_DataTx(ptrBuffer, sendLength);
162 return sendLength;
165 uint32_t CDC_Send_FreeBytes(void)
168 return the bytes free in the circular buffer
170 functionally equivalent to:
171 (APP_Rx_ptr_out > APP_Rx_ptr_in ? APP_Rx_ptr_out - APP_Rx_ptr_in : APP_RX_DATA_SIZE - APP_Rx_ptr_in + APP_Rx_ptr_in)
172 but without the impact of the condition check.
174 return ((APP_Rx_ptr_out - APP_Rx_ptr_in) + (-((int)(APP_Rx_ptr_out <= APP_Rx_ptr_in)) & APP_RX_DATA_SIZE)) - 1;
178 * @brief VCP_DataTx
179 * CDC data to be sent to the Host (app) over USB
180 * @param Buf: Buffer of data to be sent
181 * @param Len: Number of data to be sent (in bytes)
182 * @retval Result of the operation: USBD_OK if all operations are OK else VCP_FAIL
184 static uint16_t VCP_DataTx(const uint8_t* Buf, uint32_t Len)
187 make sure that any paragraph end frame is not in play
188 could just check for: USB_CDC_ZLP, but better to be safe
189 and wait for any existing transmission to complete.
191 while (USB_Tx_State != 0);
193 for (uint32_t i = 0; i < Len; i++) {
194 APP_Rx_Buffer[APP_Rx_ptr_in] = Buf[i];
195 APP_Rx_ptr_in = (APP_Rx_ptr_in + 1) % APP_RX_DATA_SIZE;
197 while (CDC_Send_FreeBytes() == 0) {
198 delay(1);
202 return USBD_OK;
205 /*******************************************************************************
206 * Function Name : Receive DATA .
207 * Description : receive the data from the PC to STM32 and send it through USB
208 * Input : None.
209 * Output : None.
210 * Return : None.
211 *******************************************************************************/
212 uint32_t CDC_Receive_DATA(uint8_t* recvBuf, uint32_t len)
214 uint32_t count = 0;
216 while (APP_Tx_ptr_out != APP_Tx_ptr_in && count < len) {
217 recvBuf[count] = APP_Tx_Buffer[APP_Tx_ptr_out];
218 APP_Tx_ptr_out = (APP_Tx_ptr_out + 1) % APP_TX_DATA_SIZE;
219 count++;
221 return count;
224 uint32_t CDC_Receive_BytesAvailable(void)
226 /* return the bytes available in the receive circular buffer */
227 return APP_Tx_ptr_out > APP_Tx_ptr_in ? APP_TX_DATA_SIZE - APP_Tx_ptr_out + APP_Tx_ptr_in : APP_Tx_ptr_in - APP_Tx_ptr_out;
231 * @brief VCP_DataRx
232 * Data received over USB OUT endpoint are sent over CDC interface
233 * through this function.
235 * @note
236 * This function will block any OUT packet reception on USB endpoint
237 * until exiting this function. If you exit this function before transfer
238 * is complete on CDC interface (ie. using DMA controller) it will result
239 * in receiving more data while previous ones are still not sent.
241 * @param Buf: Buffer of data to be received
242 * @param Len: Number of data received (in bytes)
243 * @retval Result of the opeartion: USBD_OK if all operations are OK else VCP_FAIL
245 static uint16_t VCP_DataRx(uint8_t* Buf, uint32_t Len)
247 if (CDC_Receive_BytesAvailable() + Len > APP_TX_DATA_SIZE) {
248 return USBD_FAIL;
251 for (uint32_t i = 0; i < Len; i++) {
252 APP_Tx_Buffer[APP_Tx_ptr_in] = Buf[i];
253 APP_Tx_ptr_in = (APP_Tx_ptr_in + 1) % APP_TX_DATA_SIZE;
256 return USBD_OK;
259 /*******************************************************************************
260 * Function Name : usbIsConfigured.
261 * Description : Determines if USB VCP is configured or not
262 * Input : None.
263 * Output : None.
264 * Return : True if configured.
265 *******************************************************************************/
266 uint8_t usbIsConfigured(void)
268 return (bDeviceState == CONFIGURED);
271 /*******************************************************************************
272 * Function Name : usbIsConnected.
273 * Description : Determines if USB VCP is connected ot not
274 * Input : None.
275 * Output : None.
276 * Return : True if connected.
277 *******************************************************************************/
278 uint8_t usbIsConnected(void)
280 return (bDeviceState != UNCONNECTED);
283 /*******************************************************************************
284 * Function Name : CDC_BaudRate.
285 * Description : Get the current baud rate
286 * Input : None.
287 * Output : None.
288 * Return : Baud rate in bps
289 *******************************************************************************/
290 uint32_t CDC_BaudRate(void)
292 return g_lc.bitrate;
295 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/