Skip to content

Commit 821bf5d

Browse files
committed
Merge branch 'zephyr'
2 parents 81234ed + e4efc33 commit 821bf5d

File tree

176 files changed

+15722
-1867
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

176 files changed

+15722
-1867
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
set(CMAKE_OBJECT_PATH_MAX 1024)
6+
7+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
8+
project(moddable)
9+
10+
# export all the flags used by zephyr
11+
zephyr_get_include_directories_for_lang_as_string( C includes)
12+
zephyr_get_system_include_directories_for_lang_as_string(C system_includes)
13+
zephyr_get_compile_definitions_for_lang_as_string( C definitions)
14+
zephyr_get_compile_options_for_lang_as_string( C options)
15+
16+
if(DEFINED CMAKE_C_COMPILER_TARGET)
17+
set(target_flag "--target=${CMAKE_C_COMPILER_TARGET}")
18+
endif()
19+
20+
set(external_project_cflags
21+
"${target_flag} ${includes} ${definitions} ${options} ${system_includes}"
22+
)
23+
24+
add_subdirectory("${MODDABLE_BUILD_DIR}" "${MODDABLE_BUILD_DIR}/app")
25+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
26+
27+
# message(STATUS "*-*-*-*-*-*-*-*-*")
28+
# get_cmake_property(_variableNames VARIABLES)
29+
# list (SORT _variableNames)
30+
# foreach (_variableName ${_variableNames})
31+
# message(STATUS "${_variableName}=${${_variableName}}")
32+
# endforeach()
33+
34+
35+
target_sources(app PRIVATE
36+
src/main.c
37+
src/debugger.c
38+
src/serial_fifo.c
39+
)
40+

build/devices/zephyr/app/prj.conf

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
CONFIG_STDOUT_CONSOLE=y
2+
CONFIG_GPIO=y
3+
CONFIG_I2C=y
4+
CONFIG_EVENTS=y
5+
CONFIG_NEWLIB_LIBC=y
6+
CONFIG_REBOOT=y
7+
8+
CONFIG_ASSERT=y
9+
CONFIG_MAIN_STACK_SIZE=8192
10+
CONFIG_HEAP_MEM_POOL_SIZE=65536
11+
CONFIG_MULTITHREADING=y
12+
13+
CONFIG_HWINFO=y
14+
CONFIG_UART_INTERRUPT_DRIVEN=y
15+
16+
# future
17+
# CONFIG_NETWORKING
18+
19+
CONFIG_SYS_HEAP_RUNTIME_STATS=y
20+
# CONFIG_SYS_HEAP_ARRAY_SIZE=4
21+
# CONFIG_HAVE_CUSTOM_LINKER_SCRIPT=y
22+
# CONFIG_CUSTOM_LINKER_SCRIPT="linker_arm_nocopy.ld"
23+
# CONFIG_XIP=y
24+
25+
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
2+
#include "xs.h"
3+
#include "xsHost.h"
4+
5+
#include <zephyr/kernel.h>
6+
#include <zephyr/device.h>
7+
#include <zephyr/drivers/uart.h>
8+
#include <zephyr/sys/ring_buffer.h>
9+
10+
#include "mc.defines.h"
11+
12+
#ifndef DEBUGGER_SPEED
13+
#define DEBUGGER_SPEED 115200 // in .dts?
14+
#endif
15+
16+
#define DEBUGGER_STACK 2048
17+
#define DEBUGGER_PRIORITY 3
18+
19+
k_tid_t gDebugTID;
20+
struct k_thread gDebugThread;
21+
K_THREAD_STACK_DEFINE(gDebugStack, DEBUGGER_STACK);
22+
23+
#define DEBUG_QUEUE_LEN 2
24+
25+
#define RING_BUF_SIZE 1024
26+
static struct ring_buf m_rx_ringbuf;
27+
static uint8_t rx_ringbuf_buffer[RING_BUF_SIZE];
28+
29+
#define UART_DEVICE_NODE DT_CHOSEN(zephyr_shell_uart)
30+
static const struct device *const uart_dev = DEVICE_DT_GET(UART_DEVICE_NODE);
31+
32+
static uint8_t gNotifyOutstanding = 0;
33+
struct k_msgq dbgServiceQueue;
34+
35+
static void die(char *x) {
36+
while (1)
37+
;
38+
}
39+
40+
void serial_cb(const struct device *dev, void *user_data)
41+
{
42+
if (!uart_irq_update(uart_dev))
43+
return;
44+
45+
while (uart_irq_rx_ready(uart_dev)) {
46+
uint8_t buf[32];
47+
48+
uint32_t space = ring_buf_space_get(&m_rx_ringbuf);
49+
if (space == 0)
50+
die("rx ring buffer full");
51+
52+
uint32_t use = (space < sizeof(buf)) ? space : sizeof(buf);
53+
uint32_t read = uart_fifo_read(uart_dev, buf, use);
54+
if (read > 0) {
55+
uint32_t written = ring_buf_put(&m_rx_ringbuf, buf, read);
56+
if (written != read)
57+
die("rx ring buffer full");
58+
if (!gNotifyOutstanding) {
59+
gNotifyOutstanding = 1;
60+
k_msgq_put(&dbgServiceQueue, &gNotifyOutstanding, K_NO_WAIT);
61+
}
62+
}
63+
}
64+
}
65+
66+
static void debugLoop(void *a, void *b, void *c)
67+
{
68+
uint32_t msg;
69+
uint32_t *running = a;
70+
71+
int ret = uart_irq_callback_user_data_set(uart_dev, serial_cb, NULL);
72+
if (ret < 0) {
73+
if ((ret == -ENOTSUP) || (ret == -ENOSYS))
74+
printk("Interrupt-driven UART not enabled.\n");
75+
else
76+
printk("error setting UART callback: %d\n", ret);
77+
*running = -1;
78+
return;
79+
}
80+
81+
uart_irq_rx_enable(uart_dev);
82+
*running = 1;
83+
84+
while (true) {
85+
int err = k_msgq_get(&dbgServiceQueue, &msg, K_MSEC(10000));
86+
if (err) continue;
87+
88+
gNotifyOutstanding = 0;
89+
#ifdef mxDebug
90+
if (ring_buf_size_get(&m_rx_ringbuf) > 0)
91+
fxReceiveLoop();
92+
#endif
93+
}
94+
}
95+
96+
97+
void setupDebugger(uint32_t *running)
98+
{
99+
if (!device_is_ready(uart_dev)) {
100+
printk("UART device not found!\n");
101+
*running = -1;
102+
return;
103+
}
104+
105+
k_msgq_alloc_init(&dbgServiceQueue, sizeof(uint8_t), DEBUG_QUEUE_LEN);
106+
107+
// initial ringbuf
108+
ring_buf_init(&m_rx_ringbuf, sizeof(rx_ringbuf_buffer), rx_ringbuf_buffer);
109+
110+
// make debugger thread
111+
gDebugTID = k_thread_create(&gDebugThread, gDebugStack, K_THREAD_STACK_SIZEOF(gDebugStack), debugLoop, running, NULL, NULL, DEBUGGER_PRIORITY, 0, K_NO_WAIT);
112+
}
113+
114+
void ESP_put(uint8_t *c, int count)
115+
{
116+
while (count) {
117+
ESP_putc(*c++);
118+
count--;
119+
}
120+
}
121+
122+
void ESP_putc(int c)
123+
{
124+
uint8_t ch = c;
125+
126+
uart_poll_out(uart_dev, ch);
127+
}
128+
129+
int ESP_getc(void)
130+
{
131+
uint8_t c;
132+
uint32_t len;
133+
134+
len = ring_buf_get(&m_rx_ringbuf, &c, 1);
135+
if (1 == len)
136+
return c;
137+
138+
return -1;
139+
}
140+
141+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (c) 2016 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/drivers/gpio.h>
9+
10+
#include "xsmc.h"
11+
#include "xsHost.h"
12+
#include "xsHosts.h"
13+
#include "mc.defines.h"
14+
15+
#include "modInstrumentation.h"
16+
17+
xsMachine *gThe = NULL;
18+
19+
#if MODDEF_XS_TEST
20+
uint8_t gSoftReset;
21+
#endif
22+
23+
static void runLoop(void *p1, void *p2, void *p3)
24+
{
25+
uint32_t running = 0;
26+
setupDebugger(&running);
27+
28+
while (running == 0) {
29+
modDelayMilliseconds(10);
30+
}
31+
32+
while(1) {
33+
gThe = modCloneMachine(NULL, NULL);
34+
modRunMachineSetup(gThe);
35+
36+
#if MODDEF_XS_TEST
37+
xsMachine *the = gThe;
38+
gSoftReset = 0;
39+
while (!gSoftReset) {
40+
modTimersExecute();
41+
modMessageService(gThe, modTimersNext());
42+
modInstrumentationAdjust(Turns, +1);
43+
}
44+
xsDeleteMachine(the);
45+
#else
46+
while (true) {
47+
modTimersExecute();
48+
modMessageService(gThe, modTimersNext());
49+
modInstrumentationAdjust(Turns, +1);
50+
}
51+
#endif
52+
}
53+
}
54+
55+
#define PRIORITY 3
56+
K_THREAD_DEFINE(main_thread, 4096, runLoop, NULL, NULL, NULL, PRIORITY, 0, 0);
57+
58+
int main(void)
59+
{
60+
// nuthin.
61+
62+
return 0;
63+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include "xsPlatform.h"
2+
#include <stdint.h>
3+
#include "serial_fifo.h"
4+
5+
#define __INLINE inline
6+
7+
static __INLINE uint32_t F_length(fifo_t *fifo) {
8+
uint32_t tmp = fifo->read;
9+
return fifo->write - tmp;
10+
}
11+
12+
static __INLINE void F_put(fifo_t *fifo, uint8_t c) {
13+
fifo->buf[fifo->write & fifo->size_mask] = c;
14+
fifo->write++;
15+
}
16+
17+
static __INLINE void F_get(fifo_t *fifo, uint8_t *c) {
18+
*c = fifo->buf[fifo->read & fifo->size_mask];
19+
fifo->read++;
20+
}
21+
22+
void fifo_flush(fifo_t *fifo) {
23+
fifo->read = fifo->write;
24+
}
25+
26+
uint32_t fifo_length(fifo_t *fifo) {
27+
return F_length(fifo);
28+
}
29+
30+
uint32_t fifo_full(fifo_t *fifo) {
31+
if (0 == (fifo->size_mask - F_length(fifo) + 1))
32+
return 1;
33+
return 0;
34+
}
35+
36+
int fifo_get(fifo_t *fifo, uint8_t *c) {
37+
if (F_length(fifo) == 0)
38+
return -1;
39+
F_get(fifo, c);
40+
return 0;
41+
}
42+
43+
int fifo_put(fifo_t *fifo, uint8_t c) {
44+
if (0 == (fifo->size_mask - F_length(fifo) + 1))
45+
return -1;
46+
F_put(fifo, c);
47+
return 0;
48+
}
49+
50+
51+
int fifo_init(fifo_t *fifo, uint8_t *buf, uint32_t size) {
52+
if (0 == buf)
53+
return -1;
54+
55+
if (! ((0 != size) && (0 == ((size - 1) & size))))
56+
return -2; // bad size - needs to be base 2
57+
58+
fifo->buf = buf;
59+
fifo->size_mask = size - 1;
60+
fifo->read = 0;
61+
fifo->write = 0;
62+
63+
return 0;
64+
}
65+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
#ifndef __SERIAL_FIFO__
3+
#define __SERIAL_FIFO__
4+
5+
typedef struct {
6+
uint8_t *buf;
7+
uint32_t size_mask;
8+
volatile uint32_t read;
9+
volatile uint32_t write;
10+
} fifo_t;
11+
12+
uint32_t fifo_length(fifo_t *fifo);
13+
uint32_t fifo_full(fifo_t *fifo);
14+
int fifo_put(fifo_t *fifo, uint8_t c);
15+
int fifo_get(fifo_t *fifo, uint8_t *c);
16+
void fifo_flush(fifo_t *fifo);
17+
int fifo_init(fifo_t *fifo, uint8_t *buf, uint32_t size);
18+
19+
#endif // __SERIAL_FIFO__

0 commit comments

Comments
 (0)