usbd_cdc_vcp.c
Go to the documentation of this file.
1 
22 #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
23 #pragma data_alignment = 4
24 #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
25 
26 /* Includes ------------------------------------------------------------------*/
27 #include "usbd_cdc_vcp.h"
28 #include "stm32f4xx_conf.h"
29 #include "stdbool.h"
30 #include "system.h"
31 
33 
35 
36 extern __IO uint8_t USB_Tx_State;
37 __IO uint32_t bDeviceState = UNCONNECTED; /* USB device status */
38 
39 /* These are external variables imported from CDC core to be used for IN transfer management. */
40 
41 /* This is the buffer for data received from the MCU to APP (i.e. MCU TX, APP RX) */
42 extern uint8_t APP_Rx_Buffer[];
43 extern uint32_t APP_Rx_ptr_out;
44 /* Increment this buffer position or roll it back to
45  start address when writing received data
46  in the buffer APP_Rx_Buffer. */
47 extern uint32_t APP_Rx_ptr_in;
48 
49 /*
50  APP TX is the circular buffer for data that is transmitted from the APP (host)
51  to the USB device (flight controller).
52 */
54 static uint32_t APP_Tx_ptr_out = 0;
55 static uint32_t APP_Tx_ptr_in = 0;
56 
57 /* Private function prototypes -----------------------------------------------*/
58 static uint16_t VCP_Init(void);
59 static uint16_t VCP_DeInit(void);
60 static uint16_t VCP_Ctrl(uint32_t Cmd, uint8_t* Buf, uint32_t Len);
61 static uint16_t VCP_DataTx(const uint8_t* Buf, uint32_t Len);
62 static uint16_t VCP_DataRx(uint8_t* Buf, uint32_t Len);
63 static void (*ctrlLineStateCb)(void* context, uint16_t ctrlLineState);
65 static void (*baudRateCb)(void *context, uint32_t baud);
66 static void *baudRateCbContext;
67 
68 #pragma GCC diagnostic push
69 #pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
71 #pragma GCC diagnostic pop
72 
73 /* Private functions ---------------------------------------------------------*/
80 static uint16_t VCP_Init(void)
81 {
83 // ctrlLineStateCb = NULL;
84  baudRateCb = NULL;
85  return USBD_OK;
86 }
87 
94 static uint16_t VCP_DeInit(void)
95 {
97  return USBD_OK;
98 }
99 
100 void ust_cpy(LINE_CODING* plc2, const LINE_CODING* plc1)
101 {
102  plc2->bitrate = plc1->bitrate;
103  plc2->format = plc1->format;
104  plc2->paritytype = plc1->paritytype;
105  plc2->datatype = plc1->datatype;
106 }
107 
116 static uint16_t VCP_Ctrl(uint32_t Cmd, uint8_t* Buf, uint32_t Len)
117 {
118  LINE_CODING* plc = (LINE_CODING*)Buf;
119 
120  assert_param(Len>=sizeof(LINE_CODING));
121 
122  switch (Cmd) {
123  /* Not needed for this driver, AT modem commands */
126  break;
127 
128  // Not needed for this driver
129  case SET_COMM_FEATURE:
130  case GET_COMM_FEATURE:
131  case CLEAR_COMM_FEATURE:
132  break;
133 
134 
135  //Note - hw flow control on UART 1-3 and 6 only
136  case SET_LINE_CODING:
137  // If a callback is provided, tell the upper driver of changes in baud rate
138  if (plc && (Len == sizeof (*plc))) {
139  if (baudRateCb) {
140  baudRateCb(baudRateCbContext, plc->bitrate);
141  }
142  ust_cpy(&g_lc, plc); //Copy into structure to save for later
143  }
144  break;
145 
146 
147  case GET_LINE_CODING:
148  if (plc && (Len == sizeof (*plc))) {
149  ust_cpy(plc, &g_lc);
150  }
151  break;
152 
153 
155  // If a callback is provided, tell the upper driver of changes in DTR/RTS state
156  if (plc && (Len == sizeof (uint16_t))) {
157  if (ctrlLineStateCb) {
158  ctrlLineStateCb(ctrlLineStateCbContext, *((uint16_t *)Buf));
159  }
160  }
161  break;
162 
163  case SEND_BREAK:
164  /* Not needed for this driver */
165  break;
166 
167  default:
168  break;
169  }
170 
171  return USBD_OK;
172 }
173 
174 /*******************************************************************************
175  * Function Name : Send DATA .
176  * Description : send the data received from the STM32 to the PC through USB
177  * Input : buffer to send, and the length of the buffer.
178  * Output : None.
179  * Return : None.
180  *******************************************************************************/
181 uint32_t CDC_Send_DATA(const uint8_t *ptrBuffer, uint32_t sendLength)
182 {
183  VCP_DataTx(ptrBuffer, sendLength);
184  return sendLength;
185 }
186 
187 uint32_t CDC_Send_FreeBytes(void)
188 {
189  /*
190  return the bytes free in the circular buffer
191 
192  functionally equivalent to:
193  (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)
194  but without the impact of the condition check.
195  */
196  return ((APP_Rx_ptr_out - APP_Rx_ptr_in) + (-((int)(APP_Rx_ptr_out <= APP_Rx_ptr_in)) & APP_RX_DATA_SIZE)) - 1;
197 }
198 
206 static uint16_t VCP_DataTx(const uint8_t* Buf, uint32_t Len)
207 {
208  /*
209  make sure that any paragraph end frame is not in play
210  could just check for: USB_CDC_ZLP, but better to be safe
211  and wait for any existing transmission to complete.
212  */
213  volatile uint32_t free = CDC_Send_FreeBytes();
214 // while (USB_Tx_State != 0);
215 
216  for (uint32_t i = 0; i < Len; i++) {
217  APP_Rx_Buffer[APP_Rx_ptr_in] = Buf[i];
219 
220 // while (CDC_Send_FreeBytes() == 0) {
221 // delay(1);
222 // }
223  }
224 
225  return USBD_OK;
226 }
227 
228 /*******************************************************************************
229  * Function Name : Receive DATA .
230  * Description : receive the data from the PC to STM32 and send it through USB
231  * Input : None.
232  * Output : None.
233  * Return : None.
234  *******************************************************************************/
235 uint32_t CDC_Receive_DATA(uint8_t* recvBuf, uint32_t len)
236 {
237  uint32_t count = 0;
238 
239  while (APP_Tx_ptr_out != APP_Tx_ptr_in && count < len) {
240  recvBuf[count] = APP_Tx_Buffer[APP_Tx_ptr_out];
242  count++;
243  }
244  return count;
245 }
246 
248 {
249  /* return the bytes available in the receive circular buffer */
251 }
252 
268 static uint16_t VCP_DataRx(uint8_t* Buf, uint32_t Len)
269 {
271  return USBD_FAIL;
272  }
273 
274  for (uint32_t i = 0; i < Len; i++) {
275  APP_Tx_Buffer[APP_Tx_ptr_in] = Buf[i];
277  }
278 
279  return USBD_OK;
280 }
281 
282 /*******************************************************************************
283  * Function Name : usbIsConfigured.
284  * Description : Determines if USB VCP is configured or not
285  * Input : None.
286  * Output : None.
287  * Return : True if configured.
288  *******************************************************************************/
289 uint8_t usbIsConfigured(void)
290 {
291  return (bDeviceState == CONFIGURED);
292 }
293 
294 /*******************************************************************************
295  * Function Name : usbIsConnected.
296  * Description : Determines if USB VCP is connected ot not
297  * Input : None.
298  * Output : None.
299  * Return : True if connected.
300  *******************************************************************************/
301 uint8_t usbIsConnected(void)
302 {
303  return (bDeviceState != UNCONNECTED);
304 }
305 
306 /*******************************************************************************
307  * Function Name : CDC_BaudRate.
308  * Description : Get the current baud rate
309  * Input : None.
310  * Output : None.
311  * Return : Baud rate in bps
312  *******************************************************************************/
313 uint32_t CDC_BaudRate(void)
314 {
315  return g_lc.bitrate;
316 }
317 
318 /*******************************************************************************
319  * Function Name : CDC_SetBaudRateCb
320  * Description : Set a callback to call when baud rate changes
321  * Input : callback function and context.
322  * Output : None.
323  * Return : None.
324  *******************************************************************************/
325 void CDC_SetBaudRateCb(void (*cb)(void *context, uint32_t baud), void *context)
326 {
327  baudRateCbContext = context;
328  baudRateCb = cb;
329 }
330 
331 /*******************************************************************************
332  * Function Name : CDC_SetCtrlLineStateCb
333  * Description : Set a callback to call when control line state changes
334  * Input : callback function and context.
335  * Output : None.
336  * Return : None.
337  *******************************************************************************/
338 void CDC_SetCtrlLineStateCb(void (*cb)(void *context, uint16_t ctrlLineState), void *context)
339 {
340  ctrlLineStateCbContext = context;
342 }
343 
344 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
static uint32_t APP_Tx_ptr_out
Definition: usbd_cdc_vcp.c:54
CDC_IF_Prop_TypeDef VCP_fops
Definition: usbd_cdc_vcp.c:70
void CDC_SetBaudRateCb(void(*cb)(void *context, uint32_t baud), void *context)
Definition: usbd_cdc_vcp.c:325
uint32_t CDC_Send_FreeBytes(void)
Definition: usbd_cdc_vcp.c:187
#define GET_LINE_CODING
Definition: usbd_cdc_core.h:80
#define GET_ENCAPSULATED_RESPONSE
Definition: usbd_cdc_core.h:75
void ust_cpy(LINE_CODING *plc2, const LINE_CODING *plc1)
Definition: usbd_cdc_vcp.c:100
static void * ctrlLineStateCbContext
Definition: usbd_cdc_vcp.c:64
static void * baudRateCbContext
Definition: usbd_cdc_vcp.c:66
void assert_param(int val)
static uint8_t APP_Tx_Buffer[APP_TX_DATA_SIZE]
Definition: usbd_cdc_vcp.c:53
static uint16_t VCP_DataRx(uint8_t *Buf, uint32_t Len)
VCP_DataRx Data received over USB OUT endpoint are sent over CDC interface through this function...
Definition: usbd_cdc_vcp.c:268
static uint16_t VCP_DeInit(void)
VCP_DeInit DeInitializes the Media on the STM32.
Definition: usbd_cdc_vcp.c:94
uint8_t APP_Rx_Buffer[]
#define GET_COMM_FEATURE
Definition: usbd_cdc_core.h:77
static void(* baudRateCb)(void *context, uint32_t baud)
Definition: usbd_cdc_vcp.c:65
static void(* ctrlLineStateCb)(void *context, uint16_t ctrlLineState)
Definition: usbd_cdc_vcp.c:63
#define __IO
Definition: core_cm0.h:198
#define SEND_ENCAPSULATED_COMMAND
Definition: usbd_cdc_core.h:74
LINE_CODING g_lc
Definition: usbd_cdc_vcp.c:32
#define APP_RX_DATA_SIZE
Definition: usbd_conf.h:60
#define SEND_BREAK
Definition: usbd_cdc_core.h:82
void CDC_SetCtrlLineStateCb(void(*cb)(void *context, uint16_t ctrlLineState), void *context)
Definition: usbd_cdc_vcp.c:338
static uint16_t VCP_DataTx(const uint8_t *Buf, uint32_t Len)
VCP_DataTx CDC data to be sent to the Host (app) over USB.
Definition: usbd_cdc_vcp.c:206
uint8_t usbIsConnected(void)
Definition: usbd_cdc_vcp.c:301
uint32_t CDC_Receive_DATA(uint8_t *recvBuf, uint32_t len)
Definition: usbd_cdc_vcp.c:235
#define CLEAR_COMM_FEATURE
Definition: usbd_cdc_core.h:78
__IO uint32_t bDeviceState
Definition: usbd_cdc_vcp.c:37
#define APP_TX_DATA_SIZE
Definition: usbd_conf.h:62
uint32_t CDC_Receive_BytesAvailable(void)
Definition: usbd_cdc_vcp.c:247
uint32_t CDC_BaudRate(void)
Definition: usbd_cdc_vcp.c:313
#define SET_LINE_CODING
Definition: usbd_cdc_core.h:79
Header for usbd_cdc_vcp.c file.
static uint16_t VCP_Ctrl(uint32_t Cmd, uint8_t *Buf, uint32_t Len)
VCP_Ctrl Manage the CDC class requests.
Definition: usbd_cdc_vcp.c:116
static uint16_t VCP_Init(void)
VCP_Init Initializes the Media on the STM32.
Definition: usbd_cdc_vcp.c:80
__IO uint8_t USB_Tx_State
#define SET_CONTROL_LINE_STATE
Definition: usbd_cdc_core.h:81
void cb(uint8_t byte)
Definition: ublox.cpp:8
static uint32_t APP_Tx_ptr_in
Definition: usbd_cdc_vcp.c:55
#define NULL
Definition: usbd_def.h:50
uint32_t CDC_Send_DATA(const uint8_t *ptrBuffer, uint32_t sendLength)
Definition: usbd_cdc_vcp.c:181
LINE_CODING
Definition: usbd_cdc_vcp.h:72
uint32_t APP_Rx_ptr_in
USB_OTG_CORE_HANDLE USB_OTG_dev
Definition: usbd_cdc_vcp.c:34
uint8_t usbIsConfigured(void)
Definition: usbd_cdc_vcp.c:289
#define SET_COMM_FEATURE
Definition: usbd_cdc_core.h:76
uint32_t APP_Rx_ptr_out


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Thu Apr 15 2021 05:07:50