diff options
| author | Damien George <damien.p.george@gmail.com> | 2015-02-13 15:04:53 +0000 |
|---|---|---|
| committer | Damien George <damien.p.george@gmail.com> | 2015-02-13 15:04:53 +0000 |
| commit | 0b32e50365b6e36474e183a7240ccfc8fa56830a (patch) | |
| tree | 101cf35f6844c50672af4f15b5866e226bcb47d9 /teensy/teensy_hal.c | |
| parent | c385a639e6316cba4ff0e7a859325807857d8f00 (diff) | |
stmhal: Make pybstdio usable by other ports, and use it.
Now all ports can use pybstdio.c to provide sys.stdin/stdout/stderr, so
long as they implement mp_hal_stdin_* and mp_hal_stdout_* functions.
Diffstat (limited to 'teensy/teensy_hal.c')
| -rw-r--r-- | teensy/teensy_hal.c | 42 |
1 files changed, 39 insertions, 3 deletions
diff --git a/teensy/teensy_hal.c b/teensy/teensy_hal.c index c28dd9182..312aca058 100644 --- a/teensy/teensy_hal.c +++ b/teensy/teensy_hal.c @@ -1,9 +1,10 @@ #include <stdio.h> +#include <string.h> -#include "py/mpconfig.h" - +#include "py/mpstate.h" +#include "usb.h" +#include "uart.h" #include "Arduino.h" - #include MICROPY_HAL_H uint32_t HAL_GetTick(void) { @@ -19,3 +20,38 @@ void mp_hal_set_interrupt_char(int c) { // an exception when a certain character is received. That just means that // you can't press Control-C and get your python script to stop. } + +int mp_hal_stdin_rx_chr(void) { + for (;;) { + byte c; + if (usb_vcp_recv_byte(&c) != 0) { + return c; + } else if (MP_STATE_PORT(pyb_stdio_uart) != NULL && uart_rx_any(MP_STATE_PORT(pyb_stdio_uart))) { + return uart_rx_char(MP_STATE_PORT(pyb_stdio_uart)); + } + __WFI(); + } +} + +void mp_hal_stdout_tx_str(const char *str) { + mp_hal_stdout_tx_strn(str, strlen(str)); +} + +void mp_hal_stdout_tx_strn(const char *str, uint32_t len) { + if (MP_STATE_PORT(pyb_stdio_uart) != NULL) { + uart_tx_strn(MP_STATE_PORT(pyb_stdio_uart), str, len); + } + if (usb_vcp_is_enabled()) { + usb_vcp_send_strn(str, len); + } +} + +void mp_hal_stdout_tx_strn_cooked(const char *str, uint32_t len) { + // send stdout to UART and USB CDC VCP + if (MP_STATE_PORT(pyb_stdio_uart) != NULL) { + uart_tx_strn_cooked(MP_STATE_PORT(pyb_stdio_uart), str, len); + } + if (usb_vcp_is_enabled()) { + usb_vcp_send_strn_cooked(str, len); + } +} |
