1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
/* sbc_harness/main.c - Main entry point and event loop for sbc-harness
*
* Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include <string.h> /* libc: for strlen() */
#include <pico/stdio_uart.h> /* pico-sdk:pico_stdio_uart: for stdio_uart_init() */
#include <hardware/flash.h> /* pico-sdk:hardware_flash: for flash_get_unique_id() */
#include <libcr/coroutine.h>
#include <libhw/rp2040_hwspi.h>
#include <libhw/w5500.h>
#include <libmisc/hash.h>
#include <libmisc/vcall.h>
#include <libusb/usb_common.h>
#include <libdhcp/client.h>
#include <lib9p/srv.h>
#define LOG_NAME MAIN
#include <libmisc/log.h>
#include "usb_keyboard.h"
#include "config.h"
static COROUTINE hello_world_cr(void *_chan) {
const char *msg = "Hello world!\n";
usb_keyboard_rpc_t *chan = _chan;
cr_begin();
for (size_t i = 0;; i = (i+1) % strlen(msg)) {
int result = usb_keyboard_rpc_send_req(chan, (uint32_t)msg[i]);
if (result < 1) {
errorf("error sending rune U+%d", (uint32_t)msg[i]);
break;
}
}
cr_end();
}
static COROUTINE dhcp_cr(void *_chip) {
struct w5500 *chip = _chip;
cr_begin();
dhcp_client_main(chip, "harness");
cr_end();
}
struct {
struct rp2040_hwspi dev_spi;
struct w5500 dev_w5500;
usb_keyboard_rpc_t keyboard_chan;
uint16_t usb_serial[sizeof(uint64_t)*2]; /* UTF-16 */
struct lib9p_srv srv;
} globals;
static COROUTINE read9p_cr(void *) {
cr_begin();
lib9p_srv_read_cr(&globals.srv,
VCALL(&globals.dev_w5500, tcp_listen, CONFIG_9P_PORT));
cr_end();
}
const char *hexdig = "0123456789ABCDEF";
static_assert(CONFIG_9P_MAX_REQS*_CONFIG_9P_NUM_SOCKS <= 16);
COROUTINE init_cr(void *) {
cr_begin();
/* NOR flash chips have a (bog-?)standard "RUID" "Read Unique
* ID" instruction; use our flash chip's unique ID as the
* basis for our serial numbers. */
uint64_t flash_id64;
static_assert(sizeof(flash_id64) == FLASH_UNIQUE_ID_SIZE_BYTES);
flash_get_unique_id((uint8_t *)&flash_id64);
uint32_t flash_id32 = hash(&flash_id64, sizeof(flash_id64));
static_assert(sizeof(flash_id32) == sizeof(hash(NULL, 0)));
uint8_t flash_id24[3] = {
(uint8_t)((flash_id32 >> 16) & 0xFF),
(uint8_t)((flash_id32 >> 8) & 0xFF),
(uint8_t)((flash_id32 >> 0) & 0xFF),
};
rp2040_hwspi_init(&globals.dev_spi, "W5500", RP2040_HWSPI_0,
SPI_MODE_0, /* the W5500 supports mode 0 or mode 3 */
60*1000*1000, /* as close to the W5500's max rate of 80MHz as we can without hwspi borking */
16, /* PIN_MISO */
19, /* PIN_MOSI */
18, /* PIN_CLK */
17); /* PIN_CS */
w5500_init(&globals.dev_w5500, "W5500", &globals.dev_spi,
21, /* PIN_INTR */
20, /* PIN_RESET */
((struct net_eth_addr){{
/* vendor ID: "Wiznet" */
0x00, 0x08, 0xDC,
/* serial number */
flash_id24[0], flash_id24[1], flash_id24[2],
}}));
static_assert(sizeof(flash_id64)*2 == LM_ARRAY_LEN(globals.usb_serial));
for (size_t i = 0; i < LM_ARRAY_LEN(globals.usb_serial); i++)
globals.usb_serial[i] = hexdig[(flash_id64 >> ((sizeof(flash_id64)*8)-((i+1)*4))) & 0xF];
usb_common_earlyinit(globals.usb_serial, sizeof(globals.usb_serial));
usb_keyboard_init();
usb_common_lateinit();
globals.keyboard_chan = (usb_keyboard_rpc_t){0};
/* set up coroutines **************************************************/
coroutine_add("usb_common", usb_common_cr, NULL);
coroutine_add("usb_keyboard", usb_keyboard_cr, &globals.keyboard_chan);
coroutine_add("hello_world", hello_world_cr, &globals.keyboard_chan);
coroutine_add_with_stack_size(4*1024, "dhcp", dhcp_cr, &globals.dev_w5500);
for (int i = 0; i < _CONFIG_9P_NUM_SOCKS; i++) {
char name[] = {'r', 'e', 'a', 'd', '-', hexdig[i], '\0'};
coroutine_add(name, read9p_cr, NULL);
}
for (int i = 0; i < CONFIG_9P_MAX_REQS*_CONFIG_9P_NUM_SOCKS; i++) {
char name[] = {'w', 'r', 'i', 't', 'e', '-', hexdig[i], '\0'};
coroutine_add(name, lib9p_srv_write_cr, &globals.srv);
}
cr_exit();
}
int main() {
stdio_uart_init();
/* char *hdr = "=" * (80-strlen("info : MAIN: ")); */
infof("===================================================================");
coroutine_add("init", init_cr, NULL);
coroutine_main();
}
|