Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions ports/zephyr/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,37 @@ choice MICROPY_CONFIG_ROM_LEVEL

endchoice

choice MICROPY_GETCHAR
prompt "Getchar driver to use for the console"
default MICROPY_GETCHAR_CONSOLE_DRIVER

config MICROPY_GETCHAR_CONSOLE_DRIVER
bool "Use the console driver getchar"
depends on UART_CONSOLE
depends on UART_CONSOLE_DEBUG_SERVER_HOOKS
help
Use the uart_console driver API directly to access interrupts via a mix
of UART-like drivers and standard I/O. The console buffer is managed by
MicroPython.

config MICROPY_GETCHAR_CONSOLE_SUBSYS
bool "Use the console subsys getchar"
depends on CONSOLE_SUBSYS
depends on CONSOLE_GETCHAR
help
Let Zephyr handle all the console management including the buffers via
the console subsystem.

endchoice

if MICROPY_GETCHAR_CONSOLE_DRIVER

config MICROPY_GETCHAR_CONSOLE_DRIVER_BUF_SIZE
int "Size of the console driver getchar buffer"
default 1024

endif # MICROPY_GETCHAR_CONSOLE_DRIVER

endmenu # MicroPython Options

source "Kconfig.zephyr"
4 changes: 0 additions & 4 deletions ports/zephyr/boards/ai_m61_32s_kit.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
CONFIG_PINCTRL=y
CONFIG_GPIO=y
CONFIG_WATCHDOG=y
CONFIG_CONSOLE_SUBSYS=y
CONFIG_CONSOLE_GETCHAR=y
CONFIG_CONSOLE_GETCHAR_BUFSIZE=1024
CONFIG_CONSOLE_PUTCHAR_BUFSIZE=1024
CONFIG_I2C=y
CONFIG_I2C_TARGET=y
CONFIG_SPI=y
Expand Down
4 changes: 0 additions & 4 deletions ports/zephyr/boards/ai_m62_12f_kit.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
CONFIG_PINCTRL=y
CONFIG_GPIO=y
CONFIG_WATCHDOG=y
CONFIG_CONSOLE_SUBSYS=y
CONFIG_CONSOLE_GETCHAR=y
CONFIG_CONSOLE_GETCHAR_BUFSIZE=1024
CONFIG_CONSOLE_PUTCHAR_BUFSIZE=1024
CONFIG_I2C=y
CONFIG_I2C_TARGET=y
CONFIG_SPI=y
Expand Down
4 changes: 0 additions & 4 deletions ports/zephyr/boards/ai_m64p_32s_kit.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
CONFIG_PINCTRL=y
CONFIG_GPIO=y
CONFIG_WATCHDOG=y
CONFIG_CONSOLE_SUBSYS=y
CONFIG_CONSOLE_GETCHAR=y
CONFIG_CONSOLE_GETCHAR_BUFSIZE=1024
CONFIG_CONSOLE_PUTCHAR_BUFSIZE=1024
CONFIG_I2C=y
CONFIG_I2C_TARGET=y
CONFIG_SPI=y
Expand Down
4 changes: 0 additions & 4 deletions ports/zephyr/boards/ai_wb2_12f_kit.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
CONFIG_PINCTRL=y
CONFIG_GPIO=y
CONFIG_WATCHDOG=y
CONFIG_CONSOLE_SUBSYS=y
CONFIG_CONSOLE_GETCHAR=y
CONFIG_CONSOLE_GETCHAR_BUFSIZE=1024
CONFIG_CONSOLE_PUTCHAR_BUFSIZE=1024
CONFIG_I2C=y
CONFIG_I2C_TARGET=y
CONFIG_SPI=y
Expand Down
8 changes: 5 additions & 3 deletions ports/zephyr/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

#include <zephyr/storage/flash_map.h>

#ifdef CONFIG_CONSOLE_SUBSYS
#ifdef CONFIG_MICROPY_GETCHAR_CONSOLE_SUBSYS
#include <zephyr/console/console.h>

#if CONFIG_CONSOLE_GETCHAR_BUFSIZE < 512
Expand Down Expand Up @@ -129,13 +129,15 @@ void init_zephyr(void) {

int main(void) {
/* Initialize terminal device */
#ifdef CONFIG_CONSOLE_SUBSYS
#ifdef CONFIG_MICROPY_GETCHAR_CONSOLE_SUBSYS
console_init();
/* Always immediately hand control back to micropython */
console_set_rx_timeout(K_NO_WAIT);
console_set_tx_timeout(K_NO_WAIT);
#else
#elif CONFIG_MICROPY_GETCHAR_CONSOLE_DRIVER
zephyr_getchar_init();
#else
#error A getchar driver must be chosen
#endif

#if MICROPY_PY_THREAD
Expand Down
13 changes: 8 additions & 5 deletions ports/zephyr/src/zephyr_getchar.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

#ifdef CONFIG_MICROPY_GETCHAR_CONSOLE_DRIVER

#include <zephyr/kernel.h>
#include <zephyr/drivers/uart.h>
#include <zephyr/drivers/console/uart_console.h>
Expand All @@ -24,12 +26,11 @@ extern int mp_interrupt_char;
void mp_sched_keyboard_interrupt(void);
void mp_hal_signal_event(void);

#define UART_BUFSIZE (512)
static uint8_t uart_ringbuf[UART_BUFSIZE];
static uint16_t i_get, i_put;
static uint8_t uart_ringbuf[CONFIG_MICROPY_GETCHAR_CONSOLE_DRIVER_BUF_SIZE];
static uint16_t i_get = 0, i_put = 0;

static int console_irq_input_hook(uint8_t ch) {
int i_next = (i_put + 1) & (UART_BUFSIZE - 1);
int i_next = (i_put + 1) & (CONFIG_MICROPY_GETCHAR_CONSOLE_DRIVER_BUF_SIZE - 1);
if (i_next == i_get) {
printk("UART buffer overflow - char dropped\n");
return 1;
Expand All @@ -54,7 +55,7 @@ int zephyr_getchar(void) {
if (i_get != i_put) {
unsigned int key = irq_lock();
int c = (int)uart_ringbuf[i_get++];
i_get &= UART_BUFSIZE - 1;
i_get &= CONFIG_MICROPY_GETCHAR_CONSOLE_DRIVER_BUF_SIZE - 1;
irq_unlock(key);
return c;
}
Expand All @@ -66,3 +67,5 @@ void zephyr_getchar_init(void) {
// All NULLs because we're interested only in the callback above
uart_register_input(NULL, NULL, NULL);
}

#endif /* CONFIG_MICROPY_GETCHAR_CONSOLE_DRIVER */
9 changes: 8 additions & 1 deletion ports/zephyr/src/zephyr_getchar.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@
* limitations under the License.
*/

#include <stdint.h>
#ifndef MICROPY_INCLUDED_ZEPHYR_SRC_ZEPHYR_GETCHAR_H
#define MICROPY_INCLUDED_ZEPHYR_SRC_ZEPHYR_GETCHAR_H

#ifdef CONFIG_MICROPY_GETCHAR_CONSOLE_DRIVER

void zephyr_getchar_init(void);
int zephyr_getchar_check(void);
int zephyr_getchar(void);

#endif

#endif /* MICROPY_INCLUDED_ZEPHYR_SRC_ZEPHYR_GETCHAR_H */
19 changes: 13 additions & 6 deletions ports/zephyr/uart_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include <unistd.h>
#include "py/mpconfig.h"
#include "py/runtime.h"
Expand All @@ -38,10 +39,14 @@
* Core UART functions to implement for a port
*/

#ifdef CONFIG_MICROPY_GETCHAR_CONSOLE_DRIVER
static const struct device *const uart_console_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_console));
#endif

uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
uintptr_t ret = 0;
if (poll_flags & MP_STREAM_POLL_RD) {
#ifdef CONFIG_CONSOLE_SUBSYS
#ifdef CONFIG_MICROPY_GETCHAR_CONSOLE_SUBSYS
// It's not easy to test if tty is readable, so just unconditionally set it for now.
ret |= MP_STREAM_POLL_RD;
#else
Expand All @@ -60,7 +65,7 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
int mp_hal_stdin_rx_chr(void) {
for (;;) {
int _chr;
#ifdef CONFIG_CONSOLE_SUBSYS
#ifdef CONFIG_MICROPY_GETCHAR_CONSOLE_SUBSYS
_chr = console_getchar();
#else
_chr = zephyr_getchar();
Expand All @@ -75,7 +80,7 @@ int mp_hal_stdin_rx_chr(void) {
// Send string of given length
mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
mp_uint_t ret = len;
#ifdef CONFIG_CONSOLE_SUBSYS
#ifdef CONFIG_MICROPY_GETCHAR_CONSOLE_SUBSYS
while (len--) {
char c = *str++;
/* console_putchar returns -EAGAIN when no free tx is available */
Expand All @@ -84,9 +89,11 @@ mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
}
}
#else
static const struct device *uart_console_dev =
DEVICE_DT_GET(DT_CHOSEN(zephyr_console));

/* Use poll out directly, because printk and other indirect uart_console access will append
* windows line returns to the output.
* This works for UART and UART-like serial drivers (CDC ACM) and is equivalent to the
* uart_console driver's output.
*/
while (len--) {
uart_poll_out(uart_console_dev, *str++);
}
Expand Down
Loading