Skip to content

Commit 88e50b8

Browse files
committed
Start working on console
1 parent 1200886 commit 88e50b8

File tree

8 files changed

+142
-31
lines changed

8 files changed

+142
-31
lines changed

main.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,8 @@ static int run_repl(safe_mode_t safe_mode) {
975975
}
976976

977977
#if defined(__ZEPHYR__) && __ZEPHYR__ == 1
978+
#include <zephyr/console/console.h>
979+
978980
int circuitpython_main(void) {
979981
#else
980982
int __attribute__((used)) main(void) {
@@ -997,12 +999,18 @@ int __attribute__((used)) main(void) {
997999

9981000
// Start the debug serial
9991001
serial_early_init();
1002+
console_write(NULL, "serial_early_init\r\n", strlen("serial_early_init\r\n"));
1003+
10001004
mp_hal_stdout_tx_str(line_clear);
10011005

10021006
// Wait briefly to give a reset window where we'll enter safe mode after the reset.
10031007
if (get_safe_mode() == SAFE_MODE_NONE) {
1008+
console_write(NULL, "wait_for_safe_mode_reset\r\n", strlen("wait_for_safe_mode_reset\r\n"));
10041009
set_safe_mode(wait_for_safe_mode_reset());
10051010
}
1011+
printk("safe_mode: %d\r\n", get_safe_mode());
1012+
1013+
console_write(NULL, "stack_init\r\n", strlen("stack_init\r\n"));
10061014

10071015
stack_init();
10081016

ports/nordic/common-hal/microcontroller/Pin.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@ static void reset_speaker_enable_pin(void) {
3434
}
3535

3636
void reset_all_pins(void) {
37-
for (size_t i = 0; i < GPIO_COUNT; i++) {
38-
claimed_pins[i] = never_reset_pins[i];
39-
}
40-
41-
for (uint32_t pin = 0; pin < NUMBER_OF_PINS; ++pin) {
42-
if ((never_reset_pins[nrf_pin_port(pin)] & (1 << nrf_relative_pin_number(pin))) != 0) {
43-
continue;
44-
}
45-
nrf_gpio_cfg_default(pin);
46-
}
47-
48-
// After configuring SWD because it may be shared.
49-
reset_speaker_enable_pin();
37+
// for (size_t i = 0; i < GPIO_COUNT; i++) {
38+
// claimed_pins[i] = never_reset_pins[i];
39+
// }
40+
41+
// for (uint32_t pin = 0; pin < NUMBER_OF_PINS; ++pin) {
42+
// if ((never_reset_pins[nrf_pin_port(pin)] & (1 << nrf_relative_pin_number(pin))) != 0) {
43+
// continue;
44+
// }
45+
// nrf_gpio_cfg_default(pin);
46+
// }
47+
48+
// // After configuring SWD because it may be shared.
49+
// reset_speaker_enable_pin();
5050
}
5151

5252
// Mark pin as free and return it to a quiescent state.

prj.conf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
CONFIG_HEAP_MEM_POOL_SIZE=65536
2+
CONFIG_CONSOLE_SUBSYS=y
3+
CONFIG_CONSOLE_GETCHAR=y
4+
CONFIG_LOG=y
5+
6+
CONFIG_RING_BUFFER=y

supervisor/shared/serial.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -113,28 +113,28 @@ MP_WEAK void board_serial_write_substring(const char *text, uint32_t length) {
113113
(void)length;
114114
}
115115

116-
MP_WEAK void port_serial_early_init(void) {
117-
}
116+
// MP_WEAK void port_serial_early_init(void) {
117+
// }
118118

119-
MP_WEAK void port_serial_init(void) {
120-
}
119+
// MP_WEAK void port_serial_init(void) {
120+
// }
121121

122-
MP_WEAK bool port_serial_connected(void) {
123-
return false;
124-
}
122+
// MP_WEAK bool port_serial_connected(void) {
123+
// return false;
124+
// }
125125

126-
MP_WEAK char port_serial_read(void) {
127-
return -1;
128-
}
126+
// MP_WEAK char port_serial_read(void) {
127+
// return -1;
128+
// }
129129

130-
MP_WEAK uint32_t port_serial_bytes_available(void) {
131-
return 0;
132-
}
130+
// MP_WEAK uint32_t port_serial_bytes_available(void) {
131+
// return 0;
132+
// }
133133

134-
MP_WEAK void port_serial_write_substring(const char *text, uint32_t length) {
135-
(void)text;
136-
(void)length;
137-
}
134+
// MP_WEAK void port_serial_write_substring(const char *text, uint32_t length) {
135+
// (void)text;
136+
// (void)length;
137+
// }
138138

139139
void serial_early_init(void) {
140140
// Set up console UART, if enabled.

supervisor/shared/stack.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,24 @@
1313
#include "supervisor/shared/safe_mode.h"
1414

1515
void stack_init(void) {
16+
// Zephyr has a stack canary of its own.
17+
#ifndef __ZEPHYR__
1618
uint32_t *stack_limit = port_stack_get_limit();
1719
*stack_limit = STACK_CANARY_VALUE;
20+
#endif
1821
}
1922

2023
inline bool stack_ok(void) {
24+
#ifndef __ZEPHYR__
2125
uint32_t *stack_limit = port_stack_get_limit();
2226
return *stack_limit == STACK_CANARY_VALUE;
27+
#endif
2328
}
2429

2530
inline void assert_heap_ok(void) {
31+
#ifndef __ZEPHYR__
2632
if (!stack_ok()) {
2733
reset_into_safe_mode(SAFE_MODE_STACK_OVERFLOW);
2834
}
35+
#endif
2936
}

supervisor/zephyr/port.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "supervisor/port.h"
88

9+
#include <zephyr/autoconf.h>
910
#include <zephyr/kernel.h>
1011

1112
safe_mode_t port_init(void) {
@@ -39,6 +40,7 @@ void port_heap_init(void) {
3940

4041
// Get stack limit address
4142
uint32_t *port_stack_get_limit(void) {
43+
k_tid_t current_thread_id = k_current_get();
4244
return NULL;
4345
}
4446

@@ -66,7 +68,14 @@ uint32_t port_get_saved_word(void) {
6668
}
6769

6870
uint64_t port_get_raw_ticks(uint8_t *subticks) {
69-
return 0;
71+
#if CONFIG_SYS_CLOCK_TICKS_PER_SEC != 32768
72+
#error "This code assumes 32768 Hz system clock"
73+
#endif
74+
int64_t uptime = k_uptime_ticks();
75+
if (subticks != NULL) {
76+
*subticks = uptime % 32;
77+
}
78+
return uptime / 32;
7079
}
7180

7281
// Enable 1/1024 second tick.

supervisor/zephyr/serial.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// This file is part of the CircuitPython project: https://circuitpython.org
2+
//
3+
// SPDX-FileCopyrightText: Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
4+
//
5+
// SPDX-License-Identifier: MIT
6+
7+
#include "supervisor/shared/serial.h"
8+
#include "py/ringbuf.h"
9+
10+
#include <zephyr/autoconf.h>
11+
12+
// #if defined(CONFIG_CONSOLE_SUBSYS)
13+
14+
#include <zephyr/console/console.h>
15+
#include <zephyr/sys/ring_buffer.h>
16+
17+
// init the console ourselves so we can set the timeout to zero.
18+
static struct tty_serial console_serial;
19+
20+
static uint8_t console_rxbuf[CONFIG_CONSOLE_GETCHAR_BUFSIZE];
21+
static uint8_t console_txbuf[CONFIG_CONSOLE_PUTCHAR_BUFSIZE];
22+
23+
void port_serial_early_init(void) {
24+
const struct device *uart_dev;
25+
int ret;
26+
27+
uart_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_console));
28+
if (!device_is_ready(uart_dev)) {
29+
return -ENODEV;
30+
}
31+
32+
ret = tty_init(&console_serial, uart_dev);
33+
34+
if (ret) {
35+
return ret;
36+
}
37+
38+
/* Checks device driver supports for interrupt driven data transfers. */
39+
if (CONFIG_CONSOLE_GETCHAR_BUFSIZE + CONFIG_CONSOLE_PUTCHAR_BUFSIZE) {
40+
const struct uart_driver_api *api =
41+
(const struct uart_driver_api *)uart_dev->api;
42+
if (!api->irq_callback_set) {
43+
return -ENOTSUP;
44+
}
45+
}
46+
47+
tty_set_tx_buf(&console_serial, console_txbuf, sizeof(console_txbuf));
48+
tty_set_rx_buf(&console_serial, console_rxbuf, sizeof(console_rxbuf));
49+
50+
printk("port_serial_early_init done");
51+
}
52+
53+
void port_serial_init(void) {
54+
}
55+
56+
bool port_serial_connected(void) {
57+
return true;
58+
}
59+
60+
char port_serial_read(void) {
61+
char buf[1];
62+
ssize_t read = tty_read(&console_serial, buf, 1);
63+
if (read == 0) {
64+
return -1;
65+
}
66+
return buf[0];
67+
}
68+
69+
uint32_t port_serial_bytes_available(void) {
70+
_fill_ring_buf();
71+
return ring_buf_size_get(&rb);
72+
}
73+
74+
void port_serial_write_substring(const char *text, uint32_t length) {
75+
uint32_t total_written = 0;
76+
while (total_written < length) {
77+
total_written += tty_write(NULL, text + total_written, length - total_written);
78+
}
79+
}
80+
81+
// #endif

tools/cpbuild/build_circuitpython.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ async def build_circuitpython():
296296
# These files don't include unique QSTRs. They just need to be compiled.
297297
source_files.append(srcdir / "supervisor" / "zephyr" / "flash.c")
298298
source_files.append(srcdir / "supervisor" / "zephyr" / "port.c")
299+
source_files.append(srcdir / "supervisor" / "zephyr" / "serial.c")
299300
source_files.append(srcdir / "lib" / "oofatfs" / "ff.c")
300301
source_files.append(srcdir / "lib" / "oofatfs" / "ffunicode.c")
301302
source_files.append(srcdir / "extmod" / "vfs_fat_diskio.c")

0 commit comments

Comments
 (0)