2424 * THE SOFTWARE.
2525 */
2626
27- // This file will only be used when CIRCUITPY_USB is 0. See
28- // supervisor/supervisor.mk for the rule that applies.
29-
30- #include <stdarg.h>
31- #include <string.h>
32-
3327#include "py/mpconfig.h"
34- #include "supervisor/shared/cpu.h"
35- #include "supervisor/shared/display.h"
36- #include "shared-bindings/terminalio/Terminal.h"
3728#include "supervisor/serial.h"
38- #include "shared-bindings/microcontroller/Pin.h"
39-
40- #if CIRCUITPY_SERIAL_BLE
41- #include "supervisor/shared/bluetooth/serial.h"
42- #endif
43-
44- #if defined(CIRCUITPY_DEBUG_UART_TX ) || defined(CIRCUITPY_DEBUG_UART_RX )
45- #include "py/mpprint.h"
46- #include "shared-bindings/busio/UART.h"
47- busio_uart_obj_t debug_uart ;
48- byte buf_array [64 ];
49- #endif
5029
5130#if CIRCUITPY_ESP_USB_SERIAL_JTAG
5231#include "supervisor/usb_serial_jtag.h"
5332#endif
5433
55- #if defined(CIRCUITPY_DEBUG_UART_TX )
56- STATIC void debug_uart_print_strn (void * env , const char * str , size_t len ) {
57- (void )env ;
58- int uart_errcode ;
59- common_hal_busio_uart_write (& debug_uart , (const uint8_t * )str , len , & uart_errcode );
60- }
61-
62- const mp_print_t debug_uart_print = {NULL , debug_uart_print_strn };
63- #endif
64-
65- int debug_uart_printf (const char * fmt , ...) {
66- #if defined(CIRCUITPY_DEBUG_UART_TX )
67- // Skip prints that occur before debug serial is started. It's better than
68- // crashing.
69- if (common_hal_busio_uart_deinited (& debug_uart )) {
70- return 0 ;
71- }
72- va_list ap ;
73- va_start (ap , fmt );
74- int ret = mp_vprintf (& debug_uart_print , fmt , ap );
75- va_end (ap );
76- return ret ;
77- #else
78- return 0 ;
79- #endif
80- }
81-
82- void serial_early_init (void ) {
83- #if defined(CIRCUITPY_DEBUG_UART_TX ) || defined(CIRCUITPY_DEBUG_UART_RX )
84- debug_uart .base .type = & busio_uart_type ;
85-
86- #if defined(CIRCUITPY_DEBUG_UART_RX )
87- const mcu_pin_obj_t * rx = MP_OBJ_TO_PTR (CIRCUITPY_DEBUG_UART_RX );
88- #else
89- const mcu_pin_obj_t * rx = NULL ;
90- #endif
91-
92- #if defined(CIRCUITPY_DEBUG_UART_TX )
93- const mcu_pin_obj_t * tx = MP_OBJ_TO_PTR (CIRCUITPY_DEBUG_UART_TX );
94- #else
95- const mcu_pin_obj_t * tx = NULL ;
96- #endif
97-
98- common_hal_busio_uart_construct (& debug_uart , tx , rx , NULL , NULL , NULL ,
99- false, 115200 , 8 , BUSIO_UART_PARITY_NONE , 1 , 1.0f , 64 ,
100- buf_array , true);
101- common_hal_busio_uart_never_reset (& debug_uart );
102-
103- // Do an initial print so that we can confirm the serial output is working.
104- debug_uart_printf ("Serial debug setup\r\n" );
105- #endif
106- }
107-
108- void serial_init (void ) {
34+ void port_serial_init (void ) {
10935 #if CIRCUITPY_ESP_USB_SERIAL_JTAG
11036 usb_serial_jtag_init ();
11137 #endif
11238}
11339
114- bool serial_connected (void ) {
115- #if defined(CIRCUITPY_DEBUG_UART_TX ) && defined(CIRCUITPY_DEBUG_UART_RX )
116- return true;
117- #endif
118-
119- #if CIRCUITPY_SERIAL_BLE
120- if (ble_serial_connected ()) {
121- return true;
122- }
123- #endif
124-
40+ bool port_serial_connected (void ) {
12541 #if CIRCUITPY_ESP_USB_SERIAL_JTAG
12642 if (usb_serial_jtag_connected ()) {
12743 return true;
@@ -131,23 +47,7 @@ bool serial_connected(void) {
13147 return false;
13248}
13349
134- char serial_read (void ) {
135-
136- #if defined(CIRCUITPY_DEBUG_UART_RX )
137- if (common_hal_busio_uart_rx_characters_available (& debug_uart )) {
138- int uart_errcode ;
139- char text ;
140- common_hal_busio_uart_read (& debug_uart , (uint8_t * )& text , 1 , & uart_errcode );
141- return text ;
142- }
143- #endif
144-
145- #if CIRCUITPY_SERIAL_BLE
146- if (ble_serial_available () > 0 ) {
147- return ble_serial_read_char ();
148- }
149- #endif
150-
50+ char port_serial_read (void ) {
15151 #if CIRCUITPY_ESP_USB_SERIAL_JTAG
15252 if (usb_serial_jtag_bytes_available () > 0 ) {
15353 return usb_serial_jtag_read_char ();
@@ -156,19 +56,7 @@ char serial_read(void) {
15656 return -1 ;
15757}
15858
159- bool serial_bytes_available (void ) {
160- #if defined(CIRCUITPY_DEBUG_UART_RX )
161- if (common_hal_busio_uart_rx_characters_available (& debug_uart )) {
162- return true;
163- }
164- #endif
165-
166- #if CIRCUITPY_SERIAL_BLE
167- if (ble_serial_available ()) {
168- return true;
169- }
170- #endif
171-
59+ bool port_serial_bytes_available (void ) {
17260 #if CIRCUITPY_ESP_USB_SERIAL_JTAG
17361 if (usb_serial_jtag_bytes_available ()) {
17462 return true;
@@ -178,30 +66,8 @@ bool serial_bytes_available(void) {
17866 return false;
17967}
18068
181- void serial_write_substring (const char * text , uint32_t length ) {
182- if (length == 0 ) {
183- return ;
184- }
185- #if CIRCUITPY_TERMINALIO
186- int errcode ;
187- common_hal_terminalio_terminal_write (& supervisor_terminal , (const uint8_t * )text , length , & errcode );
188- #endif
189-
190- #if defined(CIRCUITPY_DEBUG_UART_TX )
191- int uart_errcode ;
192-
193- common_hal_busio_uart_write (& debug_uart , (const uint8_t * )text , length , & uart_errcode );
194- #endif
195-
196- #if CIRCUITPY_SERIAL_BLE
197- ble_serial_write (text , length );
198- #endif
199-
69+ void port_serial_write_substring (const char * text , uint32_t length ) {
20070 #if CIRCUITPY_ESP_USB_SERIAL_JTAG
20171 usb_serial_jtag_write (text , length );
20272 #endif
20373}
204-
205- void serial_write (const char * text ) {
206- serial_write_substring (text , strlen (text ));
207- }
0 commit comments