2222#define UART_TX_PIN 0
2323#define UART_RX_PIN 1
2424
25- #define BUFFER_SIZE 64
25+ #define BUFFER_SIZE 64
2626
2727#define DEF_BIT_RATE 115200
2828#define DEF_STOP_BITS 1
@@ -96,11 +96,11 @@ int update_uart_cfg(void)
9696 }
9797
9898 if ((last_cdc_lc .stop_bits != CDC_LC .stop_bits ) ||
99- (last_cdc_lc .parity != CDC_LC .parity ) ||
100- (last_cdc_lc .data_bits != CDC_LC .data_bits )) {
99+ (last_cdc_lc .parity != CDC_LC .parity ) ||
100+ (last_cdc_lc .data_bits != CDC_LC .data_bits )) {
101101 uart_set_format (UART_ID , databits_usb2uart (CDC_LC .data_bits ),
102- stopbits_usb2uart (CDC_LC .stop_bits ),
103- parity_usb2uart (CDC_LC .parity ));
102+ stopbits_usb2uart (CDC_LC .stop_bits ),
103+ parity_usb2uart (CDC_LC .parity ));
104104 updated = 1 ;
105105 }
106106
@@ -110,46 +110,49 @@ int update_uart_cfg(void)
110110 return updated ;
111111}
112112
113- void core1_entry (void )
113+ void usb_cdc_process (void )
114114{
115- tusb_init ();
116-
117- while (1 ) {
118- tud_task ();
115+ uint32_t count ;
116+ uint32_t len ;
119117
120- if (tud_cdc_connected ()) {
121- uint32_t count ;
118+ tud_cdc_get_line_coding (& CDC_LC );
122119
123- tud_cdc_get_line_coding (& CDC_LC );
120+ /* Read bytes from USB */
121+ if (tud_cdc_available ()) {
122+ mutex_enter_blocking (& USB_MTX );
124123
125- /* Read bytes from USB */
126- if (tud_cdc_available ()) {
127- uint32_t len ;
124+ len = MIN (tud_cdc_available (), BUFFER_SIZE - USB_POS );
125+ if (len ) {
126+ count = tud_cdc_read (USB_BUFFER , len );
127+ USB_POS += count ;
128+ }
128129
129- mutex_enter_blocking (& USB_MTX );
130+ mutex_exit (& USB_MTX );
131+ }
130132
131- len = MIN (tud_cdc_available (), BUFFER_SIZE - USB_POS );
132- if (len ) {
133- count = tud_cdc_read (USB_BUFFER , len );
134- USB_POS += count ;
135- }
133+ /* Write bytes to USB */
134+ if (UART_POS ) {
135+ mutex_enter_blocking (& UART_MTX );
136136
137- mutex_exit (& USB_MTX );
138- }
137+ count = tud_cdc_write (UART_BUFFER , UART_POS );
138+ if (count ) {
139+ UART_POS -= count ;
140+ tud_cdc_write_flush ();
141+ }
139142
140- /* Write bytes to USB */
141- if ( UART_POS ) {
142- mutex_enter_blocking ( & UART_MTX );
143+ mutex_exit ( & UART_MTX );
144+ }
145+ }
143146
144- count = tud_cdc_write (UART_BUFFER , UART_POS );
145- if (count ) {
146- UART_POS -= count ;
147- tud_cdc_write_flush ();
148- }
147+ void core1_entry (void )
148+ {
149+ tusb_init ();
149150
150- mutex_exit ( & UART_MTX );
151- }
151+ while ( 1 ) {
152+ tud_task ();
152153
154+ if (tud_cdc_connected ()) {
155+ usb_cdc_process ();
153156 gpio_put (LED_PIN , 1 );
154157 } else {
155158 gpio_put (LED_PIN , 0 );
@@ -175,8 +178,8 @@ int main(void)
175178
176179 uart_set_hw_flow (UART_ID , false, false);
177180 uart_set_format (UART_ID , databits_usb2uart (CDC_LC .data_bits ),
178- stopbits_usb2uart (CDC_LC .stop_bits ),
179- parity_usb2uart (CDC_LC .parity ));
181+ stopbits_usb2uart (CDC_LC .stop_bits ),
182+ parity_usb2uart (CDC_LC .parity ));
180183
181184 multicore_launch_core1 (core1_entry );
182185
@@ -187,7 +190,8 @@ int main(void)
187190 if (uart_is_readable (UART_ID )) {
188191 mutex_enter_blocking (& UART_MTX );
189192
190- while (uart_is_readable (UART_ID ) && UART_POS < BUFFER_SIZE ) {
193+ while (uart_is_readable (UART_ID ) &&
194+ UART_POS < BUFFER_SIZE ) {
191195 UART_BUFFER [UART_POS ] = uart_getc (UART_ID );
192196 UART_POS ++ ;
193197 }
0 commit comments