@ -25,10 +25,28 @@
*/
# include <unistd.h>
# include "py/mpconfig.h"
# include "py/runtime.h"
# include "src/zephyr_getchar.h"
// Zephyr headers
# include <zephyr/drivers/uart.h>
# include <zephyr/zephyr.h>
# include <zephyr/device.h>
# include <zephyr/console/console.h>
# include <zephyr/console/tty.h>
# include <zephyr/drivers/uart.h>
# ifdef CONFIG_CONSOLE_SUBSYS
static int mp_console_putchar ( char c ) ;
static int mp_console_getchar ( void ) ;
static struct tty_serial mp_console_serial ;
static uint8_t mp_console_rxbuf [ CONFIG_CONSOLE_GETCHAR_BUFSIZE ] ;
static uint8_t mp_console_txbuf [ CONFIG_CONSOLE_PUTCHAR_BUFSIZE ] ;
# endif // CONFIG_CONSOLE_SUBSYS
/*
* Core UART functions to implement for a port
@ -36,11 +54,18 @@
// Receive single character
int mp_hal_stdin_rx_chr ( void ) {
# ifdef CONFIG_CONSOLE_SUBSYS
return console_getchar ( ) ;
# else
return zephyr_getchar ( ) ;
# endif
for ( ; ; ) {
int _chr ;
# ifdef CONFIG_CONSOLE_SUBSYS
_chr = mp_console_getchar ( ) ;
# else
_chr = zephyr_getchar ( ) ;
# endif
if ( _chr > = 0 ) {
return _chr ;
}
MICROPY_EVENT_POLL_HOOK
}
}
// Send string of given length
@ -49,8 +74,8 @@ mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
# ifdef CONFIG_CONSOLE_SUBSYS
while ( len - - ) {
char c = * str + + ;
while ( console_putchar ( c ) = = - 1 ) {
k_msleep ( 1 ) ;
while ( mp_ console_putchar( c ) = = - 1 ) {
MICROPY_EVENT_POLL_HOOK
}
}
# else
@ -63,3 +88,58 @@ mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
# endif
return ret ;
}
# ifdef CONFIG_CONSOLE_SUBSYS
int mp_console_init ( void ) {
const struct device * uart_dev ;
int ret ;
uart_dev = DEVICE_DT_GET ( DT_CHOSEN ( zephyr_console ) ) ;
if ( ! device_is_ready ( uart_dev ) ) {
return - ENODEV ;
}
ret = tty_init ( & mp_console_serial , uart_dev ) ;
if ( ret ) {
return ret ;
}
/* Checks device driver supports for interrupt driven data transfers. */
if ( CONFIG_CONSOLE_GETCHAR_BUFSIZE + CONFIG_CONSOLE_PUTCHAR_BUFSIZE ) {
const struct uart_driver_api * api =
( const struct uart_driver_api * ) uart_dev - > api ;
if ( ! api - > irq_callback_set ) {
return - ENOTSUP ;
}
}
tty_set_tx_buf ( & mp_console_serial , mp_console_txbuf , sizeof ( mp_console_txbuf ) ) ;
tty_set_rx_buf ( & mp_console_serial , mp_console_rxbuf , sizeof ( mp_console_rxbuf ) ) ;
tty_set_rx_timeout ( & mp_console_serial , 0 ) ;
tty_set_tx_timeout ( & mp_console_serial , 1 ) ;
return 0 ;
}
static int mp_console_putchar ( char c ) {
return tty_write ( & mp_console_serial , & c , 1 ) ;
}
static int mp_console_getchar ( void ) {
uint8_t c ;
int res ;
res = tty_read ( & mp_console_serial , & c , 1 ) ;
if ( res < 0 ) {
return res ;
}
return c ;
}
# endif // CONFIG_CONSOLE_SUBSYS