Skip to content

Commit ef36924

Browse files
author
Daniel Campora
committed
cc3200: Implement support for os.dupterm().
1 parent a7261ae commit ef36924

File tree

14 files changed

+102
-126
lines changed

14 files changed

+102
-126
lines changed

cc3200/hal/cc3200_hal.c

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "inc/hw_nvic.h"
3737
#include "hw_memmap.h"
3838
#include "py/mpstate.h"
39+
#include "py/runtime.h"
3940
#include MICROPY_HAL_H
4041
#include "rom_map.h"
4142
#include "interrupt.h"
@@ -47,6 +48,7 @@
4748
#include "pybuart.h"
4849
#include "utils.h"
4950
#include "irq.h"
51+
#include "moduos.h"
5052

5153
#ifdef USE_FREERTOS
5254
#include "FreeRTOS.h"
@@ -67,11 +69,6 @@ static void hal_TickInit (void);
6769
******************************************************************************/
6870
static volatile uint32_t HAL_tickCount;
6971

70-
/******************************************************************************
71-
DECLARE PUBLIC DATA
72-
******************************************************************************/
73-
struct _pyb_uart_obj_t *pyb_stdio_uart;
74-
7572
/******************************************************************************
7673
DECLARE IMPORTED DATA
7774
******************************************************************************/
@@ -141,34 +138,56 @@ void mp_hal_stdout_tx_str(const char *str) {
141138
}
142139

143140
void mp_hal_stdout_tx_strn(const char *str, uint32_t len) {
144-
// send stdout to UART
145-
if (pyb_stdio_uart != NULL) {
146-
uart_tx_strn(pyb_stdio_uart, str, len);
141+
if (MP_STATE_PORT(os_term_dup_obj)) {
142+
if (MP_OBJ_IS_TYPE(MP_STATE_PORT(os_term_dup_obj)->stream_o, &pyb_uart_type)) {
143+
uart_tx_strn(MP_STATE_PORT(os_term_dup_obj)->stream_o, str, len);
144+
} else {
145+
MP_STATE_PORT(os_term_dup_obj)->write[2] = mp_obj_new_bytes((const byte*)str, len);
146+
mp_call_method_n_kw(1, 0, MP_STATE_PORT(os_term_dup_obj)->write);
147+
}
147148
}
148149
// and also to telnet
149-
if (telnet_is_active()) {
150-
telnet_tx_strn(str, len);
151-
}
150+
telnet_tx_strn(str, len);
152151
}
153152

154-
void mp_hal_stdout_tx_strn_cooked(const char *str, uint32_t len) {
155-
// send stdout to UART
156-
if (pyb_stdio_uart != NULL) {
157-
uart_tx_strn_cooked(pyb_stdio_uart, str, len);
153+
void mp_hal_stdout_tx_strn_cooked (const char *str, uint32_t len) {
154+
int32_t nslen = 0;
155+
const char *_str = str;
156+
157+
for (int i = 0; i < len; i++) {
158+
if (str[i] == '\n') {
159+
mp_hal_stdout_tx_strn(_str, nslen);
160+
mp_hal_stdout_tx_strn("\r\n", 2);
161+
_str += nslen + 1;
162+
nslen = 0;
163+
} else {
164+
nslen++;
165+
}
158166
}
159-
// and also to telnet
160-
if (telnet_is_active()) {
161-
telnet_tx_strn_cooked(str, len);
167+
if (_str < str + len) {
168+
mp_hal_stdout_tx_strn(_str, nslen);
162169
}
163170
}
164171

165172
int mp_hal_stdin_rx_chr(void) {
166173
for ( ;; ) {
174+
// read telnet first
167175
if (telnet_rx_any()) {
168176
return telnet_rx_char();
169-
}
170-
else if (pyb_stdio_uart != NULL && uart_rx_any(pyb_stdio_uart)) {
171-
return uart_rx_char(pyb_stdio_uart);
177+
} else if (MP_STATE_PORT(os_term_dup_obj)) { // then the stdio_dup
178+
if (MP_OBJ_IS_TYPE(MP_STATE_PORT(os_term_dup_obj)->stream_o, &pyb_uart_type)) {
179+
if (uart_rx_any(MP_STATE_PORT(os_term_dup_obj)->stream_o)) {
180+
return uart_rx_char(MP_STATE_PORT(os_term_dup_obj)->stream_o);
181+
}
182+
} else {
183+
MP_STATE_PORT(os_term_dup_obj)->read[2] = mp_obj_new_int(1);
184+
mp_obj_t rbytes = mp_call_method_n_kw(1, 0, MP_STATE_PORT(os_term_dup_obj)->read);
185+
if (rbytes != mp_const_none) {
186+
mp_buffer_info_t bufinfo;
187+
mp_get_buffer_raise(rbytes, &bufinfo, MP_BUFFER_READ);
188+
return ((int *)(bufinfo.buf))[0];
189+
}
190+
}
172191
}
173192
HAL_Delay(1);
174193
}

cc3200/hal/cc3200_hal.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@
5555
" isb \n"); \
5656
}
5757

58-
/******************************************************************************
59-
DECLARE PUBLIC DATA
60-
******************************************************************************/
61-
extern struct _pyb_uart_obj_t *pyb_stdio_uart;
62-
6358
/******************************************************************************
6459
DECLARE PUBLIC FUNCTIONS
6560
******************************************************************************/

cc3200/mods/modpyb.c

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -141,28 +141,6 @@ STATIC mp_obj_t pyb_unique_id(void) {
141141
}
142142
STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_unique_id_obj, pyb_unique_id);
143143

144-
/// \function repl_uart(uart)
145-
/// Get or set the UART object that the REPL is repeated on.
146-
STATIC mp_obj_t pyb_repl_uart(uint n_args, const mp_obj_t *args) {
147-
if (n_args == 0) {
148-
if (pyb_stdio_uart == NULL) {
149-
return mp_const_none;
150-
} else {
151-
return pyb_stdio_uart;
152-
}
153-
} else {
154-
if (args[0] == mp_const_none) {
155-
pyb_stdio_uart = NULL;
156-
} else if (mp_obj_get_type(args[0]) == &pyb_uart_type) {
157-
pyb_stdio_uart = args[0];
158-
} else {
159-
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, mpexception_num_type_invalid_arguments));
160-
}
161-
return mp_const_none;
162-
}
163-
}
164-
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_repl_uart_obj, 0, 1, pyb_repl_uart);
165-
166144
MP_DECLARE_CONST_FUN_OBJ(pyb_main_obj); // defined in main.c
167145

168146
STATIC const mp_map_elem_t pyb_module_globals_table[] = {
@@ -174,7 +152,6 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
174152
#endif
175153
{ MP_OBJ_NEW_QSTR(MP_QSTR_freq), (mp_obj_t)&pyb_freq_obj },
176154
{ MP_OBJ_NEW_QSTR(MP_QSTR_unique_id), (mp_obj_t)&pyb_unique_id_obj },
177-
{ MP_OBJ_NEW_QSTR(MP_QSTR_repl_uart), (mp_obj_t)&pyb_repl_uart_obj },
178155

179156
{ MP_OBJ_NEW_QSTR(MP_QSTR_disable_irq), (mp_obj_t)&pyb_disable_irq_obj },
180157
{ MP_OBJ_NEW_QSTR(MP_QSTR_enable_irq), (mp_obj_t)&pyb_enable_irq_obj },

cc3200/mods/moduos.c

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@
3434
#include "py/objstr.h"
3535
#include "py/runtime.h"
3636
#include "genhdr/mpversion.h"
37-
#include "ff.h"
37+
#include "moduos.h"
3838
#include "diskio.h"
3939
#include "sflash_diskio.h"
4040
#include "file.h"
4141
#include "random.h"
4242
#include "mpexception.h"
4343
#include "version.h"
4444
#include "timeutils.h"
45-
#include "moduos.h"
4645
#include "pybsd.h"
46+
#include "pybuart.h"
4747

4848
/// \module os - basic "operating system" services
4949
///
@@ -60,6 +60,7 @@
6060
DECLARE PRIVATE DATA
6161
******************************************************************************/
6262
STATIC uint32_t os_num_mounted_devices;
63+
STATIC os_term_dup_obj_t os_term_dup_obj;
6364

6465
/******************************************************************************
6566
DEFINE PUBLIC FUNCTIONS
@@ -536,6 +537,31 @@ STATIC mp_obj_t os_mkfs(mp_obj_t device) {
536537
}
537538
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_mkfs_obj, os_mkfs);
538539

540+
STATIC mp_obj_t os_dupterm(uint n_args, const mp_obj_t *args) {
541+
if (n_args == 0) {
542+
if (MP_STATE_PORT(os_term_dup_obj) == MP_OBJ_NULL) {
543+
return mp_const_none;
544+
} else {
545+
return MP_STATE_PORT(os_term_dup_obj)->stream_o;
546+
}
547+
} else {
548+
mp_obj_t stream_o = args[0];
549+
if (stream_o == mp_const_none) {
550+
MP_STATE_PORT(os_term_dup_obj) = MP_OBJ_NULL;
551+
} else {
552+
if (!MP_OBJ_IS_TYPE(stream_o, &pyb_uart_type)) {
553+
// must be a stream-like object providing at least read and write methods
554+
mp_load_method(stream_o, MP_QSTR_read, os_term_dup_obj.read);
555+
mp_load_method(stream_o, MP_QSTR_write, os_term_dup_obj.write);
556+
}
557+
os_term_dup_obj.stream_o = stream_o;
558+
MP_STATE_PORT(os_term_dup_obj) = &os_term_dup_obj;
559+
}
560+
return mp_const_none;
561+
}
562+
}
563+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(os_dupterm_obj, 0, 1, os_dupterm);
564+
539565
STATIC const mp_map_elem_t os_module_globals_table[] = {
540566
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_uos) },
541567

@@ -554,6 +580,7 @@ STATIC const mp_map_elem_t os_module_globals_table[] = {
554580
{ MP_OBJ_NEW_QSTR(MP_QSTR_mount), (mp_obj_t)&os_mount_obj },
555581
{ MP_OBJ_NEW_QSTR(MP_QSTR_unmount), (mp_obj_t)&os_unmount_obj },
556582
{ MP_OBJ_NEW_QSTR(MP_QSTR_mkfs), (mp_obj_t)&os_mkfs_obj },
583+
{ MP_OBJ_NEW_QSTR(MP_QSTR_dupterm), (mp_obj_t)&os_dupterm_obj },
557584

558585
/// \constant sep - separation character used in paths
559586
{ MP_OBJ_NEW_QSTR(MP_QSTR_sep), MP_OBJ_NEW_QSTR(MP_QSTR__slash_) },

cc3200/mods/moduos.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
#ifndef MODUOS_H_
2929
#define MODUOS_H_
3030

31+
#include "ff.h"
32+
33+
/******************************************************************************
34+
DEFINE PUBLIC TYPES
35+
******************************************************************************/
3136
typedef struct _os_fs_mount_t {
3237
mp_obj_t device;
3338
const char *path;
@@ -40,6 +45,15 @@ typedef struct _os_fs_mount_t {
4045
uint8_t vol;
4146
} os_fs_mount_t;
4247

48+
typedef struct _os_term_dup_obj_t {
49+
mp_obj_t stream_o;
50+
mp_obj_t read[3];
51+
mp_obj_t write[3];
52+
} os_term_dup_obj_t;
53+
54+
/******************************************************************************
55+
DECLARE PUBLIC FUNCTIONS
56+
******************************************************************************/
4357
void moduos_init0 (void);
4458
os_fs_mount_t *osmount_find_by_path (const char *path);
4559
os_fs_mount_t *osmount_find_by_volume (uint8_t vol);

cc3200/mods/pybuart.c

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#include "pin.h"
5656
#include "pybpin.h"
5757
#include "pins.h"
58+
#include "moduos.h"
5859

5960
/// \moduleref pyb
6061
/// \class UART - duplex serial communication bus
@@ -168,15 +169,6 @@ bool uart_tx_strn(pyb_uart_obj_t *self, const char *str, uint len) {
168169
return true;
169170
}
170171

171-
void uart_tx_strn_cooked(pyb_uart_obj_t *self, const char *str, uint len) {
172-
for (const char *top = str + len; str < top; str++) {
173-
if (*str == '\n') {
174-
uart_tx_char(self, '\r');
175-
}
176-
uart_tx_char(self, *str);
177-
}
178-
}
179-
180172
/******************************************************************************
181173
DEFINE PRIVATE FUNCTIONS
182174
******************************************************************************/
@@ -261,12 +253,10 @@ STATIC void UARTGenericIntHandler(uint32_t uart_id) {
261253
MAP_UARTIntClear(self->reg, UART_INT_RX | UART_INT_RT);
262254
while (UARTCharsAvail(self->reg)) {
263255
int data = MAP_UARTCharGetNonBlocking(self->reg);
264-
if (pyb_stdio_uart == self && data == user_interrupt_char) {
256+
if (MP_STATE_PORT(os_term_dup_obj) && MP_STATE_PORT(os_term_dup_obj)->stream_o == self && data == user_interrupt_char) {
265257
// raise an exception when interrupts are finished
266258
mpexception_keyboard_nlr_jump();
267-
}
268-
// there's always a read buffer available
269-
else {
259+
} else { // there's always a read buffer available
270260
uint16_t next_head = (self->read_buf_head + 1) % PYBUART_RX_BUFFER_LEN;
271261
if (next_head != self->read_buf_tail) {
272262
// only store data if room in buf

cc3200/mods/pybuart.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,5 @@ uint32_t uart_rx_any(pyb_uart_obj_t *uart_obj);
4242
int uart_rx_char(pyb_uart_obj_t *uart_obj);
4343
bool uart_tx_char(pyb_uart_obj_t *self, int c);
4444
bool uart_tx_strn(pyb_uart_obj_t *uart_obj, const char *str, uint len);
45-
void uart_tx_strn_cooked(pyb_uart_obj_t *uart_obj, const char *str, uint len);
4645

4746
#endif // PYBUART_H_

cc3200/mpconfigport.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,11 @@ extern const struct _mp_obj_module_t mp_module_ussl;
160160
mp_obj_t mp_const_user_interrupt; \
161161
mp_obj_t pyb_config_main; \
162162
mp_obj_list_t pybsleep_obj_list; \
163-
mp_obj_list_t mp_irq_obj_list; \
163+
mp_obj_list_t mp_irq_obj_list; \
164164
mp_obj_list_t pyb_timer_channel_obj_list; \
165165
mp_obj_list_t mount_obj_list; \
166166
struct _pyb_uart_obj_t *pyb_uart_objs[2]; \
167+
struct _os_term_dup_obj_t *os_term_dup_obj; \
167168

168169

169170
// type definitions for the specific machine

cc3200/mptask.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,6 @@ void TASK_Micropython (void *pvParameters) {
138138
moduos_init0();
139139
rng_init0();
140140

141-
#ifdef LAUNCHXL
142-
// instantiate the stdio uart on the default pins
143-
mp_obj_t args[2] = {
144-
mp_obj_new_int(MICROPY_STDIO_UART),
145-
mp_obj_new_int(MICROPY_STDIO_UART_BAUD),
146-
};
147-
pyb_stdio_uart = pyb_uart_type.make_new((mp_obj_t)&pyb_uart_type, MP_ARRAY_SIZE(args), 0, args);
148-
#else
149-
pyb_stdio_uart = MP_OBJ_NULL;
150-
#endif
151-
152141
pybsleep_reset_cause_t rstcause = pybsleep_get_reset_cause();
153142
if (rstcause < PYB_SLP_SOFT_RESET) {
154143
if (rstcause == PYB_SLP_HIB_RESET) {

cc3200/qstrdefsport.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,21 @@
2525
* THE SOFTWARE.
2626
*/
2727

28-
// qstrs specific to this port
29-
Q(__name__)
30-
Q(help)
28+
// for pyb module
3129
Q(pyb)
30+
Q(help)
31+
#ifdef DEBUG
3232
Q(info)
33+
#endif
3334
Q(reset)
3435
Q(main)
3536
Q(sync)
36-
Q(gc)
3737
Q(rng)
38-
Q(toggle)
39-
Q(write)
40-
Q(input)
4138
Q(freq)
4239
Q(unique_id)
4340
Q(disable_irq)
4441
Q(enable_irq)
45-
Q(flush)
46-
Q(repl_uart)
42+
4743
// entries for sys.path
4844
Q(/flash)
4945
Q(/flash/lib)
@@ -81,6 +77,7 @@ Q(urandom)
8177
Q(mkfs)
8278
Q(mount)
8379
Q(unmount)
80+
Q(dupterm)
8481
Q(readonly)
8582
Q(readblocks)
8683
Q(writeblocks)
@@ -90,6 +87,8 @@ Q(count)
9087
// for file class
9188
Q(seek)
9289
Q(tell)
90+
Q(input)
91+
Q(flush)
9392

9493
// for Pin class
9594
Q(Pin)
@@ -348,7 +347,6 @@ Q(read)
348347
Q(readinto)
349348
Q(write_readinto)
350349
Q(nbytes)
351-
Q(write)
352350
Q(buf)
353351
Q(MASTER)
354352
Q(MSB)

0 commit comments

Comments
 (0)