before merging master
[inav.git] / src / main / drivers / serial_usb_vcp_at32f43x.c
blob96b283ec3630bf64decc4d044fef4e5fbf80b7a5
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 void usbVcpSetOptions(serialPort_t *instance, portOptions_t options)
313 UNUSED(instance);
314 UNUSED(options);
317 static bool isUsbVcpTransmitBufferEmpty(const serialPort_t *instance)
319 UNUSED(instance);
320 return true;
323 static uint32_t usbVcpAvailable(const serialPort_t *instance)
325 UNUSED(instance);
326 uint32_t available=0;
328 available=APP_Rx_ptr_in-APP_Rx_ptr_out;
329 if(available ==0){
330 // check anything that hasn't been copied into the cache
331 cdc_struct_type *pcdc = (cdc_struct_type *)otg_core_struct.dev.class_handler->pdata;
332 if(pcdc->g_rx_completed == 1){
333 available=pcdc->g_rxlen;
336 return available;
339 static uint8_t usbVcpRead(serialPort_t *instance)
341 UNUSED(instance);
343 // Check the cache is empty. If empty, add a read
344 if ((APP_Rx_ptr_in==0)||(APP_Rx_ptr_out == APP_Rx_ptr_in)){
345 APP_Rx_ptr_out=0;
346 APP_Rx_ptr_in=usb_vcp_get_rxdata(&otg_core_struct.dev,APP_Rx_Buffer);// usb Maximum 64 bytes each time
347 if(APP_Rx_ptr_in==0)
349 // No data is read, return 0
350 return 0;
353 return APP_Rx_Buffer[APP_Rx_ptr_out++];
356 // Write buffer data to vpc
357 static void usbVcpWriteBuf(serialPort_t *instance, const void *data, int count)
359 UNUSED(instance);
361 if (!usbVcpIsConnected(instance)) {
362 return;
365 uint32_t start = millis();
366 const uint8_t *p = data;
367 while (count > 0) {
368 uint32_t txed = CDC_Send_DATA(p, count);
369 count -= txed;
370 p += txed;
372 if (millis() - start > USB_TIMEOUT) {
373 break;
378 static bool usbVcpFlush(vcpPort_t *port)
380 uint32_t count = port->txAt;
381 port->txAt = 0;
383 if (count == 0) {
384 return true;
387 if (!usbIsConnected() || !usbIsConfigured()) {
388 return false;
391 uint32_t start = millis();
392 uint8_t *p = port->txBuf;
393 while (count > 0) {
394 uint32_t txed = CDC_Send_DATA(p, count);
395 count -= txed;
396 p += txed;
398 if (millis() - start > USB_TIMEOUT) {
399 break;
402 return count == 0;
405 static void usbVcpWrite(serialPort_t *instance, uint8_t c)
407 vcpPort_t *port = container_of(instance, vcpPort_t, port);
409 port->txBuf[port->txAt++] = c;
410 if (!port->buffering || port->txAt >= ARRAYLEN(port->txBuf)) {
411 usbVcpFlush(port);
415 static void usbVcpBeginWrite(serialPort_t *instance)
417 vcpPort_t *port = container_of(instance, vcpPort_t, port);
418 port->buffering = true;
421 static uint32_t usbTxBytesFree(const serialPort_t *instance)
423 UNUSED(instance);
424 return CDC_Send_FreeBytes();
427 static void usbVcpEndWrite(serialPort_t *instance)
429 vcpPort_t *port = container_of(instance, vcpPort_t, port);
430 port->buffering = false;
431 usbVcpFlush(port);
434 static const struct serialPortVTable usbVTable[] = {
436 .serialWrite = usbVcpWrite,
437 .serialTotalRxWaiting = usbVcpAvailable,
438 .serialTotalTxFree = usbTxBytesFree,
439 .serialRead = usbVcpRead,
440 .serialSetBaudRate = usbVcpSetBaudRate,
441 .isSerialTransmitBufferEmpty = isUsbVcpTransmitBufferEmpty,
442 .setMode = usbVcpSetMode,
443 .setOptions = usbVcpSetOptions,
444 .isConnected = usbVcpIsConnected,
445 .writeBuf = usbVcpWriteBuf,
446 .beginWrite = usbVcpBeginWrite,
447 .endWrite = usbVcpEndWrite,
448 .isIdle = NULL,
452 void usbVcpInitHardware(void)
454 IOInit(IOGetByTag(IO_TAG(PA11)), OWNER_USB, RESOURCE_INPUT, 0);
455 IOInit(IOGetByTag(IO_TAG(PA12)), OWNER_USB, RESOURCE_OUTPUT, 0);
457 /* usb gpio config */
458 usb_gpio_config();
460 #ifdef USB_LOW_POWER_WAKUP
461 usb_low_power_wakeup_config();
462 #endif
464 /* enable otgfs clock */
465 crm_periph_clock_enable(OTG_CLOCK, TRUE);
467 /* select usb 48m clcok source */
468 usb_clock48m_select(USB_CLK_HEXT);
470 /* enable otgfs irq,cannot set too high priority */
471 nvic_irq_enable(OTG_IRQ,NVIC_PRIO_USB,0);
473 usbGenerateDisconnectPulse();
475 /* init usb */
476 usbd_init(&otg_core_struct,
477 USB_FULL_SPEED_CORE_ID,
478 USB_ID,
479 &cdc_class_handler,
480 &cdc_desc_handler);
484 serialPort_t *usbVcpOpen(void)
486 vcpPort_t *s;
487 s = &vcpPort;
488 s->port.vTable = usbVTable;
490 return (serialPort_t *)s;
493 uint32_t usbVcpGetBaudRate(serialPort_t *instance)
495 UNUSED(instance);
496 cdc_struct_type *pcdc = (cdc_struct_type *)otg_core_struct.dev.class_handler->pdata;
497 return pcdc->linecoding.bitrate;
498 // return CDC_BaudRate();
501 #endif