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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <chopstx.h>
#include "usb_lld.h"
#include "stream.h"
#include "board.h"
#include "crc32.h"
#include "adc.h"
struct GPIO {
volatile uint32_t PDOR; /* Port Data Output Register */
volatile uint32_t PSOR; /* Port Set Output Register */
volatile uint32_t PCOR; /* Port Clear Output Register */
volatile uint32_t PTOR; /* Port Toggle Output Register */
volatile uint32_t PDIR; /* Port Data Input Register */
volatile uint32_t PDDR; /* Port Data Direction Register */
};
static struct GPIO *const GPIOB = (struct GPIO *const)0x400FF040;
static struct GPIO *const GPIOD = (struct GPIO *const)0x400FF0C0;
static struct GPIO *const GPIOE = (struct GPIO *const)0x400FF100;
static void
set_led (int on)
{
if (on)
GPIOB->PCOR = (1 << 0); /* PTB0: Clear: Light on */
else
GPIOB->PSOR = (1 << 0); /* PTB0: Set : Light off */
}
static chopstx_mutex_t mtx;
static chopstx_cond_t cnd0;
static chopstx_cond_t cnd1;
uint8_t u;
static uint8_t v;
static uint8_t m; /* 0..100 */
static void
wait_for (uint32_t usec)
{
#if defined(BUSY_LOOP)
uint32_t count = usec * 6;
uint32_t i;
for (i = 0; i < count; i++)
asm volatile ("" : : "r" (i) : "memory");
#else
chopstx_usec_wait (usec);
#endif
}
static void *
pwm (void *arg)
{
(void)arg;
chopstx_mutex_lock (&mtx);
chopstx_cond_wait (&cnd0, &mtx);
chopstx_mutex_unlock (&mtx);
while (1)
{
set_led (u&v);
wait_for (m);
set_led (0);
wait_for (100-m);
}
return NULL;
}
static void *
blk (void *arg)
{
(void)arg;
chopstx_mutex_lock (&mtx);
chopstx_cond_wait (&cnd1, &mtx);
chopstx_mutex_unlock (&mtx);
while (1)
{
v = 0;
wait_for (200*1000);
v = 1;
wait_for (200*1000);
}
return NULL;
}
#define INTR_REQ_USB 24
static void *
usb_intr (void *arg)
{
extern void usb_lld_init (uint8_t feature);
extern void usb_interrupt_handler (void);
chopstx_intr_t interrupt;
(void)arg;
chopstx_claim_irq (&interrupt, INTR_REQ_USB);
usb_lld_init (0x80); /* Bus powered. */
while (1)
{
chopstx_intr_wait (&interrupt);
/* Process interrupt. */
usb_interrupt_handler ();
}
chopstx_release_irq (&interrupt);
return NULL;
}
#if defined(BUSY_LOOP)
#define PRIO_PWM (CHOPSTX_SCHED_RR|1)
#define PRIO_BLK (CHOPSTX_SCHED_RR|1)
#else
#define PRIO_PWM 3
#define PRIO_BLK 2
#endif
#define PRIO_INTR 4
extern uint8_t __process1_stack_base__, __process1_stack_size__;
extern uint8_t __process2_stack_base__, __process2_stack_size__;
extern uint8_t __process3_stack_base__, __process3_stack_size__;
const uint32_t __stackaddr_pwm = (uint32_t)&__process1_stack_base__;
const size_t __stacksize_pwm = (size_t)&__process1_stack_size__;
const uint32_t __stackaddr_blk = (uint32_t)&__process2_stack_base__;
const size_t __stacksize_blk = (size_t)&__process2_stack_size__;
const uint32_t __stackaddr_intr = (uint32_t)&__process3_stack_base__;
const size_t __stacksize_intr = (size_t)&__process3_stack_size__;
static char hexchar (uint8_t x)
{
x &= 0x0f;
if (x <= 0x09)
return '0' + x;
else if (x <= 0x0f)
return 'a' + x - 10;
else
return '?';
}
static int
check_recv (void *arg)
{
struct stream *s = arg;
if ((s->flags & FLAG_CONNECTED) == 0)
return 1;
if ((s->flags & FLAG_RECV_AVAIL))
return 1;
return 0;
}
int
main (int argc, const char *argv[])
{
struct stream *st;
uint8_t count;
extern uint32_t bDeviceState;
(void)argc;
(void)argv;
adc_init ();
adc_start ();
chopstx_mutex_init (&mtx);
chopstx_cond_init (&cnd0);
chopstx_cond_init (&cnd1);
st = stream_open ();
m = 10;
chopstx_create (PRIO_PWM, __stackaddr_pwm, __stacksize_pwm, pwm, NULL);
chopstx_create (PRIO_BLK, __stackaddr_blk, __stacksize_blk, blk, NULL);
chopstx_create (PRIO_INTR, __stackaddr_intr, __stacksize_intr,
usb_intr, NULL);
chopstx_usec_wait (200*1000);
chopstx_mutex_lock (&mtx);
chopstx_cond_signal (&cnd0);
chopstx_cond_signal (&cnd1);
chopstx_mutex_unlock (&mtx);
u = 1;
while (bDeviceState != CONFIGURED)
chopstx_usec_wait (500*1000);
count = 0;
while (1)
{
uint8_t s[64];
u = 1;
if (stream_wait_connection (st) < 0)
{
chopstx_usec_wait (1000*1000);
continue;
}
chopstx_usec_wait (500*1000);
/* Send ZLP at the beginning. */
stream_send (st, s, 0);
memcpy (s, "xx: Hello, World with Chopstx!\r\n", 32);
s[0] = hexchar (count >> 4);
s[1] = hexchar (count & 0x0f);
count++;
if (stream_send (st, s, 32) < 0)
continue;
while (1)
{
int size;
uint32_t usec;
struct chx_poll_desc poll_desc;
poll_desc.type = CHOPSTX_POLL_COND;
poll_desc.c.cond = &st->cnd;
poll_desc.c.mutex = &st->mtx;
poll_desc.c.check = check_recv;
poll_desc.c.arg = st;
/* With chopstx_poll, we can do timed cond_wait */
usec = 3000000;
if (chopstx_poll (&usec, 1, &poll_desc))
{
size = stream_recv (st, s + 4);
if (size < 0)
break;
if (size >= 0)
{
unsigned int value;
if (s[4] == 't')
{
s[0] = 'T';
s[1] = 'M';
adc_start_conversion (0, 1);
adc_wait_completion (NULL);
value = adc_buf[0];
}
else
{
int i;
crc32_init ();
s[0] = hexchar (size >> 4);
s[1] = hexchar (size & 0x0f);
for (i = 0; i < size; i++)
crc32_u8 (s[4 + i]);
value = crc32_value () ^ 0xffffffff;
}
s[4] = hexchar (value >> 28);
s[5] = hexchar (value >> 24);
s[6] = hexchar (value >> 20);
s[7] = hexchar (value >> 16);
s[8] = hexchar (value >> 12);
s[9] = hexchar (value >> 8);
s[10] = hexchar (value >> 4);
s[11] = hexchar (value);
s[12] = '\r';
s[13] = '\n';
if (stream_send (st, s, 14) < 0)
break;
}
}
u ^= 1;
}
}
return 0;
}
|