Blackbox device type 'file' (SITL) considered working when file handler is available
[inav.git] / src / main / drivers / serial_usb_vcp_at32f43x.c
blob29b96d1a2b6fd7acd783c1d2fdbef7453553eeba
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
16 * author : emsr (shanggl@wo.cn)
17 * hw_config is more modified and is implemented using emsr's VCP code
18 * (implemented using timers)
21 #include <stdint.h>
23 #include <stdbool.h>
25 #include "platform.h"
27 #ifdef USE_VCP
29 #include "build/build_config.h"
31 #include "common/utils.h"
32 #include "drivers/io.h"
33 #include "build/atomic.h"
35 #include "usb_conf.h"
36 #include "usb_core.h"
37 #include "usbd_int.h"
38 #include "cdc_class.h"
39 #include "cdc_desc.h"
40 #include "usb_io.h"
42 #include "drivers/time.h"
43 #include "at32f435_437_clock.h"
44 #include "serial.h"
45 #include "serial_usb_vcp_at32f43x.h"
46 #include "nvic.h"
47 #include "at32f435_437_tmr.h"
48 #include "stddef.h"
50 otg_core_type otg_core_struct;
51 #define USB_TIMEOUT 50
53 static vcpPort_t vcpPort;
55 /**
56 * @brief usb 48M clock select
57 * @param clk_s:USB_CLK_HICK, USB_CLK_HEXT
58 * @retval none
60 void usb_clock48m_select(usb_clk48_s clk_s)
62 if(clk_s == USB_CLK_HICK)
64 crm_usb_clock_source_select(CRM_USB_CLOCK_SOURCE_HICK);
66 /* enable the acc calibration ready interrupt */
67 crm_periph_clock_enable(CRM_ACC_PERIPH_CLOCK, TRUE);
69 /* update the c1\c2\c3 value */
70 acc_write_c1(7980);
71 acc_write_c2(8000);
72 acc_write_c3(8020);
73 #if (USB_ID == 0)
74 acc_sof_select(ACC_SOF_OTG1);
75 #else
76 acc_sof_select(ACC_SOF_OTG2);
77 #endif
78 /* open acc calibration */
79 acc_calibration_mode_enable(ACC_CAL_HICKTRIM, TRUE);
81 else
83 switch(system_core_clock)
85 /* 48MHz */
86 case 48000000:
87 crm_usb_clock_div_set(CRM_USB_DIV_1);
88 break;
90 /* 72MHz */
91 case 72000000:
92 crm_usb_clock_div_set(CRM_USB_DIV_1_5);
93 break;
95 /* 96MHz */
96 case 96000000:
97 crm_usb_clock_div_set(CRM_USB_DIV_2);
98 break;
100 /* 120MHz */
101 case 120000000:
102 crm_usb_clock_div_set(CRM_USB_DIV_2_5);
103 break;
105 /* 144MHz */
106 case 144000000:
107 crm_usb_clock_div_set(CRM_USB_DIV_3);
108 break;
110 /* 168MHz */
111 case 168000000:
112 crm_usb_clock_div_set(CRM_USB_DIV_3_5);
113 break;
115 /* 192MHz */
116 case 192000000:
117 crm_usb_clock_div_set(CRM_USB_DIV_4);
118 break;
120 /* 216MHz */
121 case 216000000:
122 crm_usb_clock_div_set(CRM_USB_DIV_4_5);
123 break;
125 /* 240MHz */
126 case 240000000:
127 crm_usb_clock_div_set(CRM_USB_DIV_5);
128 break;
130 /* 264MHz */
131 case 264000000:
132 crm_usb_clock_div_set(CRM_USB_DIV_5_5);
133 break;
135 /* 288MHz */
136 case 288000000:
137 crm_usb_clock_div_set(CRM_USB_DIV_6);
138 break;
140 default:
141 break;
148 * @brief this function config gpio.
149 * @param none
150 * @retval none
152 void usb_gpio_config(void)
154 gpio_init_type gpio_init_struct;
156 crm_periph_clock_enable(OTG_PIN_GPIO_CLOCK, TRUE);
157 gpio_default_para_init(&gpio_init_struct);
159 gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
160 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
161 gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
162 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
164 /* dp and dm */
165 gpio_init_struct.gpio_pins = OTG_PIN_DP | OTG_PIN_DM;
166 gpio_init(OTG_PIN_GPIO, &gpio_init_struct);
168 gpio_pin_mux_config(OTG_PIN_GPIO, OTG_PIN_DP_SOURCE, OTG_PIN_MUX);
169 gpio_pin_mux_config(OTG_PIN_GPIO, OTG_PIN_DM_SOURCE, OTG_PIN_MUX);
171 #ifdef USB_SOF_OUTPUT_ENABLE
172 crm_periph_clock_enable(OTG_PIN_SOF_GPIO_CLOCK, TRUE);
173 gpio_init_struct.gpio_pins = OTG_PIN_SOF;
174 gpio_init(OTG_PIN_SOF_GPIO, &gpio_init_struct);
175 gpio_pin_mux_config(OTG_PIN_GPIO, OTG_PIN_SOF_SOURCE, OTG_PIN_MUX);
176 #endif
178 /* otgfs use vbus pin */
179 #ifndef USB_VBUS_IGNORE
180 gpio_init_struct.gpio_pins = OTG_PIN_VBUS;
181 gpio_init_struct.gpio_pull = GPIO_PULL_DOWN;
182 gpio_pin_mux_config(OTG_PIN_GPIO, OTG_PIN_VBUS_SOURCE, OTG_PIN_MUX);
183 gpio_init(OTG_PIN_GPIO, &gpio_init_struct);
184 #endif
188 #ifdef USB_LOW_POWER_WAKUP
190 * @brief usb low power wakeup interrupt config
191 * @param none
192 * @retval none
194 void usb_low_power_wakeup_config(void)
196 exint_init_type exint_init_struct;
198 crm_periph_clock_enable(CRM_SCFG_PERIPH_CLOCK, TRUE);
199 exint_default_para_init(&exint_init_struct);
201 exint_init_struct.line_enable = TRUE;
202 exint_init_struct.line_mode = EXINT_LINE_INTERRUPUT;
203 exint_init_struct.line_select = OTG_WKUP_EXINT_LINE;
204 exint_init_struct.line_polarity = EXINT_TRIGGER_RISING_EDGE;
205 exint_init(&exint_init_struct);
207 nvic_irq_enable(OTG_WKUP_IRQ, NVIC_PRIO_USB_WUP,0);
211 * @brief this function handles otgfs wakup interrupt.
212 * @param none
213 * @retval none
215 void OTG_WKUP_HANDLER(void)
217 exint_flag_clear(OTG_WKUP_EXINT_LINE);
220 #endif
223 /********************************************
224 * copy from cdc part
227 uint32_t CDC_Send_FreeBytes(void)
229 cdc_struct_type *pcdc = (cdc_struct_type *)otg_core_struct.dev.class_handler->pdata;
230 if(pcdc->g_tx_completed){
231 return APP_TX_BLOCK_SIZE;
232 }else{
233 return 0;
238 * @brief CDC_Send_DATA
239 * CDC received data to be send over USB IN endpoint are managed in
240 * this function.
241 * @param ptrBuffer: Buffer of data to be sent
242 * @param sendLength: Number of data to be sent (in bytes)
243 * @retval Bytes sent
245 uint32_t CDC_Send_DATA(const uint8_t *ptrBuffer, uint32_t sendLength)
247 cdc_struct_type *pcdc = (cdc_struct_type *)otg_core_struct.dev.class_handler->pdata;
248 uint32_t start = millis();
250 uint32_t pos=0;
251 while(pos < sendLength || (pos==sendLength && sendLength%64 == 0) ){//`==` is intended for sending 0 length packet
252 int tosend=sendLength-pos;
253 if(tosend>APP_TX_BLOCK_SIZE){
254 tosend=APP_TX_BLOCK_SIZE;
256 while(pcdc->g_tx_completed != 1) {
257 if (millis() - start > USB_TIMEOUT) {
258 return pos;
261 uint32_t txed=usb_vcp_send_data(&otg_core_struct.dev,(uint8_t *)(ptrBuffer+pos), tosend);
262 if(pos==sendLength){
263 break;
265 if (txed==SUCCESS) {
266 pos+=tosend;
269 return pos;
272 /************************************************************/
274 uint8_t usbIsConnected(void){
275 return (USB_CONN_STATE_DEFAULT !=otg_core_struct.dev.conn_state);
278 uint8_t usbIsConfigured(void){
279 return (USB_CONN_STATE_CONFIGURED ==otg_core_struct.dev.conn_state);
282 bool usbVcpIsConnected(const serialPort_t *instance)
284 (void)instance;
285 return usbIsConnected() && usbIsConfigured();
289 * @brief this function handles otgfs interrupt.
290 * @param none
291 * @retval none
293 void OTG_IRQ_HANDLER(void)
295 usbd_irq_handler(&otg_core_struct);
299 static void usbVcpSetBaudRate(serialPort_t *instance, uint32_t baudRate)
301 UNUSED(instance);
302 UNUSED(baudRate);
305 static void usbVcpSetMode(serialPort_t *instance, portMode_t mode)
307 UNUSED(instance);
308 UNUSED(mode);
311 static bool isUsbVcpTransmitBufferEmpty(const serialPort_t *instance)
313 UNUSED(instance);
314 return true;
317 static uint32_t usbVcpAvailable(const serialPort_t *instance)
319 UNUSED(instance);
320 uint32_t available=0;
322 available=APP_Rx_ptr_in-APP_Rx_ptr_out;
323 if(available ==0){
324 // check anything that hasn't been copied into the cache
325 cdc_struct_type *pcdc = (cdc_struct_type *)otg_core_struct.dev.class_handler->pdata;
326 if(pcdc->g_rx_completed == 1){
327 available=pcdc->g_rxlen;
330 return available;
333 static uint8_t usbVcpRead(serialPort_t *instance)
335 UNUSED(instance);
337 // Check the cache is empty. If empty, add a read
338 if ((APP_Rx_ptr_in==0)||(APP_Rx_ptr_out == APP_Rx_ptr_in)){
339 APP_Rx_ptr_out=0;
340 APP_Rx_ptr_in=usb_vcp_get_rxdata(&otg_core_struct.dev,APP_Rx_Buffer);// usb Maximum 64 bytes each time
341 if(APP_Rx_ptr_in==0)
343 // No data is read, return 0
344 return 0;
347 return APP_Rx_Buffer[APP_Rx_ptr_out++];
350 // Write buffer data to vpc
351 static void usbVcpWriteBuf(serialPort_t *instance, const void *data, int count)
353 UNUSED(instance);
355 if (!usbVcpIsConnected(instance)) {
356 return;
359 uint32_t start = millis();
360 const uint8_t *p = data;
361 while (count > 0) {
362 uint32_t txed = CDC_Send_DATA(p, count);
363 count -= txed;
364 p += txed;
366 if (millis() - start > USB_TIMEOUT) {
367 break;
372 static bool usbVcpFlush(vcpPort_t *port)
374 uint32_t count = port->txAt;
375 port->txAt = 0;
377 if (count == 0) {
378 return true;
381 if (!usbIsConnected() || !usbIsConfigured()) {
382 return false;
385 uint32_t start = millis();
386 uint8_t *p = port->txBuf;
387 while (count > 0) {
388 uint32_t txed = CDC_Send_DATA(p, count);
389 count -= txed;
390 p += txed;
392 if (millis() - start > USB_TIMEOUT) {
393 break;
396 return count == 0;
399 static void usbVcpWrite(serialPort_t *instance, uint8_t c)
401 vcpPort_t *port = container_of(instance, vcpPort_t, port);
403 port->txBuf[port->txAt++] = c;
404 if (!port->buffering || port->txAt >= ARRAYLEN(port->txBuf)) {
405 usbVcpFlush(port);
409 static void usbVcpBeginWrite(serialPort_t *instance)
411 vcpPort_t *port = container_of(instance, vcpPort_t, port);
412 port->buffering = true;
415 static uint32_t usbTxBytesFree(const serialPort_t *instance)
417 UNUSED(instance);
418 return CDC_Send_FreeBytes();
421 static void usbVcpEndWrite(serialPort_t *instance)
423 vcpPort_t *port = container_of(instance, vcpPort_t, port);
424 port->buffering = false;
425 usbVcpFlush(port);
428 static const struct serialPortVTable usbVTable[] = {
430 .serialWrite = usbVcpWrite,
431 .serialTotalRxWaiting = usbVcpAvailable,
432 .serialTotalTxFree = usbTxBytesFree,
433 .serialRead = usbVcpRead,
434 .serialSetBaudRate = usbVcpSetBaudRate,
435 .isSerialTransmitBufferEmpty = isUsbVcpTransmitBufferEmpty,
436 .setMode = usbVcpSetMode,
437 .isConnected = usbVcpIsConnected,
438 .writeBuf = usbVcpWriteBuf,
439 .beginWrite = usbVcpBeginWrite,
440 .endWrite = usbVcpEndWrite,
441 .isIdle = NULL,
445 void usbVcpInitHardware(void)
447 IOInit(IOGetByTag(IO_TAG(PA11)), OWNER_USB, RESOURCE_INPUT, 0);
448 IOInit(IOGetByTag(IO_TAG(PA12)), OWNER_USB, RESOURCE_OUTPUT, 0);
450 /* usb gpio config */
451 usb_gpio_config();
453 #ifdef USB_LOW_POWER_WAKUP
454 usb_low_power_wakeup_config();
455 #endif
457 /* enable otgfs clock */
458 crm_periph_clock_enable(OTG_CLOCK, TRUE);
460 /* select usb 48m clcok source */
461 usb_clock48m_select(USB_CLK_HEXT);
463 /* enable otgfs irq,cannot set too high priority */
464 nvic_irq_enable(OTG_IRQ,NVIC_PRIO_USB,0);
466 usbGenerateDisconnectPulse();
468 /* init usb */
469 usbd_init(&otg_core_struct,
470 USB_FULL_SPEED_CORE_ID,
471 USB_ID,
472 &cdc_class_handler,
473 &cdc_desc_handler);
477 serialPort_t *usbVcpOpen(void)
479 vcpPort_t *s;
480 s = &vcpPort;
481 s->port.vTable = usbVTable;
483 return (serialPort_t *)s;
486 uint32_t usbVcpGetBaudRate(serialPort_t *instance)
488 UNUSED(instance);
489 cdc_struct_type *pcdc = (cdc_struct_type *)otg_core_struct.dev.class_handler->pdata;
490 return pcdc->linecoding.bitrate;
491 // return CDC_BaudRate();
494 #endif