37
37
#include "py/runtime.h"
38
38
#include "shared/runtime/interrupt_char.h"
39
39
#include "shared/tinyusb/mp_usbd.h"
40
+ #include "shared/tinyusb/mp_usbd_cdc.h"
41
+ #include "extmod/misc.h"
42
+ #include <hal/nrf_ficr.h>
40
43
41
44
#ifdef BLUETOOTH_SD
42
45
#include "nrf_sdm.h"
46
49
47
50
extern void tusb_hal_nrf_power_event (uint32_t event );
48
51
49
- static void cdc_task (bool tx );
52
+ // static void cdc_task(bool tx);
50
53
51
- static uint8_t rx_ringbuf_array [1024 ];
52
- static uint8_t tx_ringbuf_array [1024 ];
53
- static volatile ringbuf_t rx_ringbuf ;
54
- static volatile ringbuf_t tx_ringbuf ;
54
+ static uint8_t stdin_ringbuf_array [1024 ];
55
+ ringbuf_t stdin_ringbuf ;
55
56
56
57
static void board_init (void ) {
57
58
// Config clock source.
@@ -105,62 +106,47 @@ static void board_init(void) {
105
106
#endif
106
107
}
107
108
108
- static bool cdc_rx_any (void ) {
109
- return rx_ringbuf .iput != rx_ringbuf .iget ;
110
- }
111
-
112
- static int cdc_rx_char (void ) {
113
- return ringbuf_get ((ringbuf_t *)& rx_ringbuf );
114
- }
115
-
116
- static bool cdc_tx_any (void ) {
117
- return tx_ringbuf .iput != tx_ringbuf .iget ;
118
- }
119
-
120
- static int cdc_tx_char (void ) {
121
- return ringbuf_get ((ringbuf_t * )& tx_ringbuf );
109
+ void mp_usbd_port_get_serial_number (char * serial_buf ) {
110
+ uint32_t deviceid [2 ];
111
+ deviceid [0 ] = nrf_ficr_deviceid_get (NRF_FICR , 0 );
112
+ deviceid [1 ] = nrf_ficr_deviceid_get (NRF_FICR , 1 );
113
+ MP_STATIC_ASSERT (sizeof (deviceid ) * 2 <= MICROPY_HW_USB_DESC_STR_MAX );
114
+ mp_usbd_hex_str (serial_buf , (uint8_t * )deviceid , sizeof (deviceid ));
122
115
}
123
116
124
- static void cdc_task (bool tx )
125
- {
126
- if ( tud_cdc_connected () ) {
127
- // connected and there are data available
128
- while (tud_cdc_available ()) {
129
- int c = tud_cdc_read_char ();
130
- if (c == mp_interrupt_char ) {
131
- rx_ringbuf .iget = 0 ;
132
- rx_ringbuf .iput = 0 ;
133
- mp_sched_keyboard_interrupt ();
134
- } else {
135
- ringbuf_put ((ringbuf_t * )& rx_ringbuf , c );
136
- }
137
- }
138
-
139
- if (tx ) {
140
- int chars = 0 ;
141
- while (cdc_tx_any ()) {
142
- if (chars < 64 ) {
143
- tud_cdc_write_char (cdc_tx_char ());
144
- chars ++ ;
145
- } else {
146
- chars = 0 ;
147
- tud_cdc_write_flush ();
148
- }
149
- }
150
-
151
- tud_cdc_write_flush ();
152
- }
153
- }
117
+ static bool cdc_rx_any (void ) {
118
+ return stdin_ringbuf .iput != stdin_ringbuf .iget ;
154
119
}
155
120
156
- static void usb_cdc_loop (void ) {
157
- tud_task ();
158
- cdc_task (true);
121
+ static int cdc_rx_char (void ) {
122
+ return ringbuf_get ((ringbuf_t * )& stdin_ringbuf );
159
123
}
160
124
161
- void tud_cdc_rx_cb (uint8_t itf ) {
162
- cdc_task (false);
163
- }
125
+ // static void cdc_task(bool tx)
126
+ // {
127
+ // if ( tud_cdc_connected() ) {
128
+ // // connected and there are data available
129
+ // while (tud_cdc_available()) {
130
+ // int c = tud_cdc_read_char();
131
+ // if (c == mp_interrupt_char) {
132
+ // stdin_ringbuf.iget = 0;
133
+ // stdin_ringbuf.iput = 0;
134
+ // mp_sched_keyboard_interrupt();
135
+ // } else {
136
+ // ringbuf_put((ringbuf_t*)&stdin_ringbuf, c);
137
+ // }
138
+ // }
139
+ // }
140
+ // }
141
+
142
+ // static void mp_usbd_task(void) {
143
+ // tud_task();
144
+ // // cdc_task(true);
145
+ // }
146
+
147
+ // void tud_cdc_rx_cb(uint8_t itf) {
148
+ // cdc_task(false);
149
+ // }
164
150
165
151
int usb_cdc_init (void )
166
152
{
@@ -176,15 +162,10 @@ int usb_cdc_init(void)
176
162
initialized = true;
177
163
}
178
164
179
- rx_ringbuf .buf = rx_ringbuf_array ;
180
- rx_ringbuf .size = sizeof (rx_ringbuf_array );
181
- rx_ringbuf .iget = 0 ;
182
- rx_ringbuf .iput = 0 ;
183
-
184
- tx_ringbuf .buf = tx_ringbuf_array ;
185
- tx_ringbuf .size = sizeof (tx_ringbuf_array );
186
- tx_ringbuf .iget = 0 ;
187
- tx_ringbuf .iput = 0 ;
165
+ stdin_ringbuf .buf = stdin_ringbuf_array ;
166
+ stdin_ringbuf .size = sizeof (stdin_ringbuf_array );
167
+ stdin_ringbuf .iget = 0 ;
168
+ stdin_ringbuf .iput = 0 ;
188
169
189
170
mp_usbd_init ();
190
171
@@ -208,7 +189,7 @@ void usb_cdc_sd_event_handler(uint32_t soc_evt) {
208
189
uintptr_t mp_hal_stdio_poll (uintptr_t poll_flags ) {
209
190
uintptr_t ret = 0 ;
210
191
if (poll_flags & MP_STREAM_POLL_RD ) {
211
- usb_cdc_loop ();
192
+ mp_usbd_task ();
212
193
if (cdc_rx_any ()) {
213
194
ret |= MP_STREAM_POLL_RD ;
214
195
}
@@ -221,7 +202,7 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
221
202
222
203
int mp_hal_stdin_rx_chr (void ) {
223
204
for (;;) {
224
- usb_cdc_loop ();
205
+ mp_usbd_task ();
225
206
if (cdc_rx_any ()) {
226
207
return cdc_rx_char ();
227
208
}
@@ -231,24 +212,31 @@ int mp_hal_stdin_rx_chr(void) {
231
212
return 0 ;
232
213
}
233
214
215
+ // Send string of given length
234
216
mp_uint_t mp_hal_stdout_tx_strn (const char * str , mp_uint_t len ) {
235
- for (const char * top = str + len ; str < top ; str ++ ) {
236
- ringbuf_put ((ringbuf_t * )& tx_ringbuf , * str );
237
- usb_cdc_loop ();
217
+ mp_uint_t ret
106A4
= len ;
218
+ bool did_write = false;
219
+ #if MICROPY_HW_ENABLE_UART_REPL
220
+ mp_uart_write_strn (str , len );
221
+ did_write = true;
222
+ #endif
223
+
224
+ #if MICROPY_HW_USB_CDC
225
+ mp_uint_t cdc_res = mp_usbd_cdc_tx_strn (str , len );
226
+ if (cdc_res > 0 ) {
227
+ did_write = true;
228
+ ret = MIN (cdc_res , ret );
238
229
}
239
- return len ;
240
- }
230
+ #endif
241
231
242
- void mp_hal_stdout_tx_strn_cooked (const char * str , mp_uint_t len ) {
243
-
244
- for (const char * top = str + len ; str < top ; str ++ ) {
245
- if (* str == '\n' ) {
246
- ringbuf_put ((ringbuf_t * )& tx_ringbuf , '\r' );
247
- usb_cdc_loop ();
248
- }
249
- ringbuf_put ((ringbuf_t * )& tx_ringbuf , * str );
250
- usb_cdc_loop ();
232
+ #if MICROPY_PY_OS_DUPTERM
233
+ int dupterm_res = mp_os_dupterm_tx_strn (str , len );
234
+ if (dupterm_res >= 0 ) {
235
+ did_write = true;
236
+ ret = MIN ((mp_uint_t )dupterm_res , ret );
251
237
}
238
+ #endif
239
+ return did_write ? ret : 0 ;
252
240
}
253
241
254
242
void USBD_IRQHandler (void ) {
0 commit comments