Skip to content

Commit 3b2dd7f

Browse files
committed
【增加】RT-Thread console 输出拦截功能。
Signed-off-by: armink <[email protected]>
1 parent 03f4c29 commit 3b2dd7f

File tree

6 files changed

+122
-22
lines changed

6 files changed

+122
-22
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
#include <rtthread.h>
2929
#include <rtdevice.h>
3030
#include <rthw.h>
31-
#include "rtt_getchar.h"
3231
#include "lib/utils/interrupt_char.h"
32+
#include "mpgetcharport.h"
3333

3434
#define UART_FIFO_SIZE 256
3535

@@ -56,7 +56,7 @@ static rt_err_t getchar_rx_ind(rt_device_t dev, rt_size_t size) {
5656
return RT_EOK;
5757
}
5858

59-
void rtt_getchar_init(void) {
59+
void mp_getchar_init(void) {
6060
rt_base_t int_lvl;
6161
rt_device_t console;
6262

@@ -76,7 +76,7 @@ void rtt_getchar_init(void) {
7676

7777
}
7878

79-
void rtt_getchar_deinit(void) {
79+
void mp_getchar_deinit(void) {
8080
rt_base_t int_lvl;
8181
rt_device_t console;
8282

@@ -91,7 +91,7 @@ void rtt_getchar_deinit(void) {
9191
rt_hw_interrupt_enable(int_lvl);
9292
}
9393

94-
int rtt_getchar(void) {
94+
int mp_getchar(void) {
9595
uint8_t ch;
9696
rt_base_t int_lvl;
9797

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#ifndef _RTT_GETCHAR_H_
28-
#define _RTT_GETCHAR_H_
27+
#ifndef _MPGETCHARPORT_H_
28+
#define _MPGETCHARPORT_H_
2929

30-
void rtt_getchar_init(void);
31-
void rtt_getchar_deinit(void);
32-
int rtt_getchar(void);
30+
void mp_getchar_init(void);
31+
void mp_getchar_deinit(void);
32+
int mp_getchar(void);
3333

34-
#endif /* _RTT_GETCHAR_H_ */
34+
#endif /* _MPGETCHARPORT_H_ */

port/mphalport.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@
3131
#include <py/mpconfig.h>
3232
#include <py/runtime.h>
3333
#include "mphalport.h"
34-
#include "rtt_getchar.h"
34+
#include "mpgetcharport.h"
35+
#include "mpputsnport.h"
3536

3637
int mp_hal_stdin_rx_chr(void) {
3738
char ch;
3839
while (1) {
39-
ch = rtt_getchar();
40+
ch = mp_getchar();
4041
if (ch != (char)0xFF) {
4142
break;
4243
}
@@ -46,18 +47,14 @@ int mp_hal_stdin_rx_chr(void) {
4647
return ch;
4748
}
4849

50+
4951
// Send string of given length
5052
void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
51-
rt_device_t console;
52-
53-
console = rt_console_get_device();
54-
if (console) {
55-
rt_device_write(console, 0, str, len);
56-
}
53+
mp_putsn(str, len);
5754
}
5855

5956
void mp_hal_stdout_tx_strn_stream(const char *str, size_t len) {
60-
rt_kprintf("%.*s", len, str);
57+
mp_putsn(str, len);
6158
}
6259

6360
mp_uint_t mp_hal_ticks_us(void) {

port/mpputsnport.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2019 Armink ([email protected])
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <rtthread.h>
28+
#include <rtdevice.h>
29+
30+
static rt_device_t console_dev = NULL;
31+
static struct rt_device dummy_console = { 0 };
32+
33+
void mp_putsn(const char *str, size_t len) {
34+
if (console_dev) {
35+
rt_device_write(console_dev, 0, str, len);
36+
}
37+
}
38+
39+
void mp_putsn_init(void) {
40+
{/* register dummy console device */
41+
#ifdef RT_USING_DEVICE_OPS
42+
static struct rt_device_ops _ops = {.ops = &_ops};
43+
#endif
44+
45+
dummy_console.type = RT_Device_Class_Char;
46+
47+
rt_device_register(&dummy_console, "dummy", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STREAM);
48+
}
49+
50+
/* backup the console device */
51+
console_dev = rt_console_get_device();
52+
53+
/* set the new console device to dummy console */
54+
rt_console_set_device(dummy_console.parent.name);
55+
/* 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);
57+
}
58+
59+
void mp_putsn_deinit(void) {
60+
/* close the old console, it's already in mp_putsn_init */
61+
rt_device_close(console_dev);
62+
/* restore the old console device */
63+
rt_console_set_device(console_dev->parent.name);
64+
65+
rt_device_unregister(&dummy_console);
66+
}

port/mpputsnport.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2019 Armink ([email protected])
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef _MPPUTCHARPORT_H_
28+
#define _MPPUTCHARPORT_H_
29+
30+
void mp_putsn_init(void);
31+
void mp_putsn_deinit(void);
32+
void mp_putsn(const char *str, size_t len);
33+
34+
#endif /* _MPPUTCHARPORT_H_ */

port/mpy_main.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
#include <py/frozenmod.h>
4444
#include <lib/mp-readline/readline.h>
4545
#include <lib/utils/pyexec.h>
46-
#include "rtt_getchar.h"
46+
#include "mpgetcharport.h"
47+
#include "mpputsnport.h"
4748

4849
#if MICROPY_ENABLE_COMPILER
4950
void do_str(const char *src, mp_parse_input_kind_t input_kind) {
@@ -70,7 +71,8 @@ void mpy_main(const char *filename) {
7071
stack_top = (void *)&stack_dummy;
7172
rt_uint16_t old_flag;
7273

73-
rtt_getchar_init();
74+
mp_getchar_init();
75+
mp_putsn_init();
7476

7577
if (rt_thread_self()->stack_size < 4096) {
7678
rt_kprintf("The stack (%.*s) size for executing MicroPython must be >=4096\n", RT_NAME_MAX, rt_thread_self()->name);
@@ -160,7 +162,8 @@ void mpy_main(const char *filename) {
160162

161163
rt_free(heap);
162164

163-
rtt_getchar_deinit();
165+
mp_putsn_deinit();
166+
mp_getchar_deinit();
164167
}
165168

166169
#if !MICROPY_PY_MODUOS_FILE

0 commit comments

Comments
 (0)