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)
29 #include "build/build_config.h"
31 #include "common/utils.h"
32 #include "drivers/io.h"
33 #include "build/atomic.h"
38 #include "cdc_class.h"
42 #include "drivers/time.h"
43 #include "at32f435_437_clock.h"
45 #include "serial_usb_vcp_at32f43x.h"
47 #include "at32f435_437_tmr.h"
50 otg_core_type otg_core_struct
;
51 #define USB_TIMEOUT 50
53 static vcpPort_t vcpPort
;
56 * @brief usb 48M clock select
57 * @param clk_s:USB_CLK_HICK, USB_CLK_HEXT
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 */
74 acc_sof_select(ACC_SOF_OTG1
);
76 acc_sof_select(ACC_SOF_OTG2
);
78 /* open acc calibration */
79 acc_calibration_mode_enable(ACC_CAL_HICKTRIM
, TRUE
);
83 switch(system_core_clock
)
87 crm_usb_clock_div_set(CRM_USB_DIV_1
);
92 crm_usb_clock_div_set(CRM_USB_DIV_1_5
);
97 crm_usb_clock_div_set(CRM_USB_DIV_2
);
102 crm_usb_clock_div_set(CRM_USB_DIV_2_5
);
107 crm_usb_clock_div_set(CRM_USB_DIV_3
);
112 crm_usb_clock_div_set(CRM_USB_DIV_3_5
);
117 crm_usb_clock_div_set(CRM_USB_DIV_4
);
122 crm_usb_clock_div_set(CRM_USB_DIV_4_5
);
127 crm_usb_clock_div_set(CRM_USB_DIV_5
);
132 crm_usb_clock_div_set(CRM_USB_DIV_5_5
);
137 crm_usb_clock_div_set(CRM_USB_DIV_6
);
148 * @brief this function config gpio.
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
;
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
);
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
);
188 #ifdef USB_LOW_POWER_WAKUP
190 * @brief usb low power wakeup interrupt config
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.
215 void OTG_WKUP_HANDLER(void)
217 exint_flag_clear(OTG_WKUP_EXINT_LINE
);
223 /********************************************
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
;
238 * @brief CDC_Send_DATA
239 * CDC received data to be send over USB IN endpoint are managed in
241 * @param ptrBuffer: Buffer of data to be sent
242 * @param sendLength: Number of data to be sent (in bytes)
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();
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
) {
261 uint32_t txed
=usb_vcp_send_data(&otg_core_struct
.dev
,(uint8_t *)(ptrBuffer
+pos
), tosend
);
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
)
285 return usbIsConnected() && usbIsConfigured();
289 * @brief this function handles otgfs interrupt.
293 void OTG_IRQ_HANDLER(void)
295 usbd_irq_handler(&otg_core_struct
);
299 static void usbVcpSetBaudRate(serialPort_t
*instance
, uint32_t baudRate
)
305 static void usbVcpSetMode(serialPort_t
*instance
, portMode_t mode
)
311 static bool isUsbVcpTransmitBufferEmpty(const serialPort_t
*instance
)
317 static uint32_t usbVcpAvailable(const serialPort_t
*instance
)
320 uint32_t available
=0;
322 available
=APP_Rx_ptr_in
-APP_Rx_ptr_out
;
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
;
333 static uint8_t usbVcpRead(serialPort_t
*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
)){
340 APP_Rx_ptr_in
=usb_vcp_get_rxdata(&otg_core_struct
.dev
,APP_Rx_Buffer
);// usb Maximum 64 bytes each time
343 // No data is read, 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
)
355 if (!usbVcpIsConnected(instance
)) {
359 uint32_t start
= millis();
360 const uint8_t *p
= data
;
362 uint32_t txed
= CDC_Send_DATA(p
, count
);
366 if (millis() - start
> USB_TIMEOUT
) {
372 static bool usbVcpFlush(vcpPort_t
*port
)
374 uint32_t count
= port
->txAt
;
381 if (!usbIsConnected() || !usbIsConfigured()) {
385 uint32_t start
= millis();
386 uint8_t *p
= port
->txBuf
;
388 uint32_t txed
= CDC_Send_DATA(p
, count
);
392 if (millis() - start
> USB_TIMEOUT
) {
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
)) {
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
)
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;
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
,
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 */
453 #ifdef USB_LOW_POWER_WAKUP
454 usb_low_power_wakeup_config();
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();
469 usbd_init(&otg_core_struct
,
470 USB_FULL_SPEED_CORE_ID
,
477 serialPort_t
*usbVcpOpen(void)
481 s
->port
.vTable
= usbVTable
;
483 return (serialPort_t
*)s
;
486 uint32_t usbVcpGetBaudRate(serialPort_t
*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();