-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
210 lines (168 loc) · 4.29 KB
/
main.c
File metadata and controls
210 lines (168 loc) · 4.29 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* -*- mode: c; tab-width: 8 -*-
*/
#include <alloca.h>
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/cm3/systick.h>
#include <libopencm3/stm32/crs.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/rcc.h>
#include <lwip/api.h>
#include <lwip/inet.h>
#include <lwip/tcp.h>
#include <lwip/tcpip.h>
#include "common.h"
extern void ecm_init(void *);
extern void usb_init(void);
#define LISTEN_PORT 7
#define WORKERS_MAX 2
typedef struct {
struct netconn *conn;
sys_thread_t thread;
sys_sem_t *sem;
void *ctx;
} worker_t;
/*
*
*/
static worker_t workers[WORKERS_MAX];
static void worker_proc(void *arg)
{
worker_t *w = arg;
struct netbuf *buf;
ip_addr_t addr;
uint16_t port;
if (w->conn == NULL)
goto out;
netconn_peer(w->conn, &addr, &port);
debugf("client: %s %d\n", ip4addr_ntoa(&addr), port);
tcp_nagle_disable(w->conn->pcb.tcp);
while (netconn_recv(w->conn, &buf) == ERR_OK) {
uint16_t len;
void *p;
do {
netbuf_data(buf, &p, &len);
netconn_write(w->conn, p, len, NETCONN_COPY);
} while (netbuf_next(buf) >= 0);
netbuf_delete(buf);
}
debugf("client: bye\n");
netconn_close(w->conn);
netconn_delete(w->conn);
w->conn = NULL;
out:
sys_sem_signal(w->sem);
}
static void start_workers(void *ctx, sys_sem_t *sem)
{
worker_t *w = workers;
for (; w < &workers[WORKERS_MAX]; w++) {
w->ctx = ctx;
w->sem = sem;
w->thread = sys_thread_new("worker", worker_proc, w,
DEFAULT_THREAD_STACKSIZE,
DEFAULT_THREAD_PRIO);
}
}
static void wake_worker(struct netconn *conn)
{
int i = 0;
while (i < WORKERS_MAX && workers[i].conn != NULL) i++;
LWIP_ASSERT("WORKERS_MAX", i < WORKERS_MAX);
workers[i].conn = conn;
sys_thread_restart(workers[i].thread);
}
static void app_proc(void *arg)
{
struct netconn *conn, *listen;
sys_sem_t sem;
err_t err;
err = sys_sem_new(&sem, 0);
LWIP_ASSERT("sem_new", err == ERR_OK);
start_workers(arg, &sem);
listen = netconn_new(NETCONN_TCP);
netconn_bind(listen, IP_ADDR_ANY, LISTEN_PORT);
netconn_listen(listen);
loop:
if (netconn_accept(listen, &conn) != ERR_OK)
goto loop;
if ((sys_arch_sem_wait(&sem, 500)) == 0) {
wake_worker(conn);
} else { /* out of free workers */
netconn_close(conn);
netconn_delete(conn);
}
goto loop;
}
static void app_init(void *app_arg)
{
sys_thread_new("listener", app_proc, app_arg,
DEFAULT_THREAD_STACKSIZE,
DEFAULT_THREAD_PRIO);
debugf("app_init: %p\n", app_arg);
}
/*
*
*/
static void idle_cb(void *ctx) { (void)ctx; }
static unsigned idle_arg, late_arg;
static void late_init(void *arg)
{
ecm_init(arg);
app_init(arg);
}
static void idle(void *ctx)
{
unsigned wake;
(void)ctx;
tcpip_init(late_init, &late_arg);
debugf("init done\n");
loop: wake = systicks + 500ul;
sleep: __asm__ __volatile__ ("wfe":::"memory");
if (wake > systicks) goto sleep;
gpio_toggle(GPIOC, GPIO13);
goto loop;
}
/*
*
*/
int main()
{
bgrt_proc_t idle_proc;
bgrt_stack_t *idle_stack;
bgrt_init();
rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSE_240MHZ]);
rcc_periph_clock_enable(RCC_AFIO);
rcc_periph_clock_enable(RCC_CRS);
rcc_periph_clock_enable(RCC_GPIOA);
rcc_periph_clock_enable(RCC_GPIOB);
rcc_periph_clock_enable(RCC_GPIOC);
rcc_set_hsi_div(RCC_CFGR3_HSIDIV_NODIV);
rcc_set_hsi_sclk(RCC_CFGR5_HSI_SCLK_HSIDIV);
rcc_set_usb_clock_source(RCC_HSI);
rcc_periph_clock_enable(RCC_USB);
rcc_usb_alt_pma_enable();
rcc_usb_alt_isr_enable();
crs_autotrim_usb_enable();
systick_set_clocksource(STK_CSR_CLKSOURCE_AHB);
systick_set_reload(rcc_ahb_frequency / 1000);
systick_interrupt_enable();
systick_counter_enable();
gpio_set_mux(AFIO_GMUX_SWJ_NO_JTAG);
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_10_MHZ,
GPIO_CNF_OUTPUT_PUSHPULL,
GPIO0|GPIO1); /* DEBUG */
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL,
GPIO11|GPIO12); /* USBFS */
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ,
GPIO_CNF_OUTPUT_PUSHPULL,
GPIO13); /* PC13 LED */
usb_init();
idle_stack = alloca(BGRT_PROC_STACK_SIZE * sizeof(bgrt_stack_t));
bgrt_proc_init_cs(&idle_proc, &idle, &idle_cb, &idle_cb,
&idle_arg, &idle_stack[BGRT_PROC_STACK_SIZE - 1],
BGRT_PRIO_LOWEST, 1, 0);
bgrt_proc_run_cs(&idle_proc);
bgrt_start();
return 0;
}