Skip to content

Commit 3d45664

Browse files
committed
【完善】stdout stream 模式的对接。
Signed-off-by: armink <[email protected]>
1 parent 3b2dd7f commit 3d45664

File tree

4 files changed

+19
-13
lines changed

4 files changed

+19
-13
lines changed

port/mphalport.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
5454
}
5555

5656
void mp_hal_stdout_tx_strn_stream(const char *str, size_t len) {
57-
mp_putsn(str, len);
57+
mp_putsn_stream(str, len);
5858
}
5959

6060
mp_uint_t mp_hal_ticks_us(void) {

port/mpputsnport.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,38 +29,52 @@
2929

3030
static rt_device_t console_dev = NULL;
3131
static struct rt_device dummy_console = { 0 };
32+
static rt_uint16_t console_open_flag;
3233

3334
void mp_putsn(const char *str, size_t len) {
3435
if (console_dev) {
3536
rt_device_write(console_dev, 0, str, len);
3637
}
3738
}
3839

40+
void mp_putsn_stream(const char *str, size_t len) {
41+
if (console_dev) {
42+
rt_uint16_t old_flag = console_dev->open_flag;
43+
44+
console_dev->open_flag |= RT_DEVICE_FLAG_STREAM;
45+
rt_device_write(console_dev, 0, str, len);
46+
console_dev->open_flag = old_flag;
47+
}
48+
}
49+
3950
void mp_putsn_init(void) {
4051
{/* register dummy console device */
4152
#ifdef RT_USING_DEVICE_OPS
42-
static struct rt_device_ops _ops = {.ops = &_ops};
53+
static struct rt_device_ops _ops = {0};
54+
dummy_console.ops = &_ops
4355
#endif
4456

4557
dummy_console.type = RT_Device_Class_Char;
4658

47-
rt_device_register(&dummy_console, "dummy", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STREAM);
59+
rt_device_register(&dummy_console, "dummy", RT_DEVICE_FLAG_RDWR);
4860
}
4961

5062
/* backup the console device */
5163
console_dev = rt_console_get_device();
64+
console_open_flag = console_dev->open_flag;
5265

5366
/* set the new console device to dummy console */
5467
rt_console_set_device(dummy_console.parent.name);
5568
/* reopen the old console device for mp_putsn */
56-
rt_device_open(console_dev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM);
69+
rt_device_open(console_dev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
5770
}
5871

5972
void mp_putsn_deinit(void) {
6073
/* close the old console, it's already in mp_putsn_init */
6174
rt_device_close(console_dev);
6275
/* restore the old console device */
6376
rt_console_set_device(console_dev->parent.name);
77+
console_dev->open_flag = console_open_flag;
6478

6579
rt_device_unregister(&dummy_console);
6680
}

port/mpputsnport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@
3030
void mp_putsn_init(void);
3131
void mp_putsn_deinit(void);
3232
void mp_putsn(const char *str, size_t len);
33+
void mp_putsn_stream(const char *str, size_t len);
3334

3435
#endif /* _MPPUTCHARPORT_H_ */

port/mpy_main.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ static char *heap = RT_NULL;
6969
void mpy_main(const char *filename) {
7070
int stack_dummy;
7171
stack_top = (void *)&stack_dummy;
72-
rt_uint16_t old_flag;
7372

7473
mp_getchar_init();
7574
mp_putsn_init();
@@ -105,11 +104,6 @@ void mpy_main(const char *filename) {
105104
mp_obj_list_init(mp_sys_argv, 0);
106105
readline_init0();
107106

108-
/* Save the open flag */
109-
old_flag = rt_console_get_device()->open_flag;
110-
/* clean the stream flag. stream flag will automatically append '\r' */
111-
rt_console_get_device()->open_flag &= ~RT_DEVICE_FLAG_STREAM;
112-
113107
if (filename) {
114108
#ifndef MICROPYTHON_USING_UOS
115109
rt_kprintf("Please enable uos module in sys module option first.\n");
@@ -149,9 +143,6 @@ void mpy_main(const char *filename) {
149143
}
150144
}
151145

152-
/* restore the open flag */
153-
rt_console_get_device()->open_flag = old_flag;
154-
155146
gc_sweep_all();
156147

157148
mp_deinit();

0 commit comments

Comments
 (0)