1515#include "mpconfig.h"
1616#include "qstr.h"
1717#include "obj.h"
18- #include "pendsv.h"
18+ // #include "pendsv.h"
1919#include "usb.h"
2020
2121#ifdef USE_DEVICE_MODE
22- //extern CDC_IF_Prop_TypeDef VCP_fops;
2322USBD_HandleTypeDef hUSBDDevice ;
2423#endif
2524
2625static int dev_is_enabled = 0 ;
2726uint32_t APP_dev_is_connected = 0 ; /* used by usbd_cdc_vcp */
28- static char rx_buf [64 ];
29- static int rx_buf_in ;
30- static int rx_buf_out ;
31- static int interrupt_char = VCP_CHAR_NONE ;
3227mp_obj_t mp_const_vcp_interrupt = MP_OBJ_NULL ;
3328
3429void pyb_usb_dev_init (int usb_dev_type ) {
@@ -52,9 +47,6 @@ void pyb_usb_dev_init(int usb_dev_type) {
5247 break ;
5348 }
5449 }
55- rx_buf_in = 0 ;
56- rx_buf_out = 0 ;
57- interrupt_char = VCP_CHAR_NONE ;
5850 dev_is_enabled = 1 ;
5951
6052 // create an exception object for interrupting by VCP
@@ -72,54 +64,19 @@ bool usb_vcp_is_connected(void) {
7264
7365void usb_vcp_set_interrupt_char (int c ) {
7466 if (dev_is_enabled ) {
75- interrupt_char = c ;
76- }
77- }
78-
79- void usb_vcp_receive (const char * buf , uint32_t len ) {
80- if (dev_is_enabled ) {
81- for (int i = 0 ; i < len ; i ++ ) {
82-
83- // catch special interrupt character
84- if (buf [i ] == interrupt_char ) {
85- // raise exception when interrupts are finished
86- mp_obj_exception_clear_traceback (mp_const_vcp_interrupt );
87- pendsv_nlr_jump (mp_const_vcp_interrupt );
88- interrupt_char = VCP_CHAR_NONE ;
89- continue ;
90- }
91-
92- rx_buf [rx_buf_in ++ ] = buf [i ];
93- if (rx_buf_in >= sizeof (rx_buf )) {
94- rx_buf_in = 0 ;
95- }
96- if (rx_buf_in == rx_buf_out ) {
97- rx_buf_out = rx_buf_in + 1 ;
98- if (rx_buf_out >= sizeof (rx_buf )) {
99- rx_buf_out = 0 ;
100- }
101- }
67+ if (c != VCP_CHAR_NONE ) {
68+ mp_obj_exception_clear_traceback (mp_const_vcp_interrupt );
10269 }
70+ USBD_CDC_SetInterrupt (c , mp_const_vcp_interrupt );
10371 }
10472}
10573
10674int usb_vcp_rx_any (void ) {
107- if (rx_buf_in >= rx_buf_out ) {
108- return rx_buf_in - rx_buf_out ;
109- } else {
110- return rx_buf_in + sizeof (rx_buf ) - rx_buf_out ;
111- }
75+ return USBD_CDC_RxAny ();
11276}
11377
11478char usb_vcp_rx_get (void ) {
115- while (rx_buf_out == rx_buf_in ) {
116- }
117- char c = rx_buf [rx_buf_out ];
118- rx_buf_out += 1 ;
119- if (rx_buf_out >= sizeof (rx_buf )) {
120- rx_buf_out = 0 ;
121- }
122- return c ;
79+ return USBD_CDC_RxGet ();
12380}
12481
12582void usb_vcp_send_str (const char * str ) {
@@ -129,39 +86,22 @@ void usb_vcp_send_str(const char *str) {
12986void usb_vcp_send_strn (const char * str , int len ) {
13087#ifdef USE_DEVICE_MODE
13188 if (dev_is_enabled ) {
132- #if 0
133- USBD_CDC_fops .pIf_DataTx ((const uint8_t * )str , len );
134- #endif
89+ USBD_CDC_Tx (str , len );
13590 }
13691#endif
13792}
13893
139- #include "usbd_conf.h"
140-
141- /* These are external variables imported from CDC core to be used for IN
142- transfer management. */
143- #ifdef USE_DEVICE_MODE
144- extern uint8_t UserRxBuffer [];/* Received Data over USB are stored in this buffer */
145- extern uint8_t UserTxBuffer [];/* Received Data over UART (CDC interface) are stored in this buffer */
146- extern uint32_t BuffLength ;
147- extern uint32_t UserTxBufPtrIn ;/* Increment this pointer or roll it back to
148- start address when data are received over USART */
149- extern uint32_t UserTxBufPtrOut ; /* Increment this pointer or roll it back to
150- start address when data are sent over USB */
151- #endif
152-
15394void usb_vcp_send_strn_cooked (const char * str , int len ) {
15495#ifdef USE_DEVICE_MODE
155- #if 0
156- for (const char * top = str + len ; str < top ; str ++ ) {
157- if (* str == '\n' ) {
158- APP_Rx_Buffer [APP_Rx_ptr_in ] = '\r' ;
159- APP_Rx_ptr_in = (APP_Rx_ptr_in + 1 ) & (APP_RX_DATA_SIZE - 1 );
96+ if (dev_is_enabled ) {
97+ for (const char * top = str + len ; str < top ; str ++ ) {
98+ if (* str == '\n' ) {
99+ USBD_CDC_Tx ("\r\n" , 2 );
100+ } else {
101+ USBD_CDC_Tx (str , 1 );
102+ }
160103 }
161- APP_Rx_Buffer [APP_Rx_ptr_in ] = * str ;
162- APP_Rx_ptr_in = (APP_Rx_ptr_in + 1 ) & (APP_RX_DATA_SIZE - 1 );
163104 }
164- #endif
165105#endif
166106}
167107
0 commit comments