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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
|
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "hardware/pio.h"
#include "hardware/clocks.h"
#include "pico/multicore.h"
#include "pico/sync.h"
#include "ws2812.pio.h"
#include "bsp/board.h"
#include "tusb.h"
#include "tusb_desc.c"
#define COL_GRN 0x33000000
#define COL_RED 0x00330000
#define COL_BLU 0x00003300
#define COL_ORG 0x05330000
/*
* Needed Pins:
* 8 data 5v
* 1 eeprom ce 5v
* 1 eeprom oe 5v
* 1 eeprom we 5v
* 1 shift oe 5v
* 1 shift clr 5v
* 1 shift sclk 5v
* 1 shift rclk 5v
* 1 shift in 5v
* 1 FET power 3v3
* -----------------
* 16 pins 5v
* 1 pin 3v3
* -----------------
* 17 pins
*/
#define PIN_RGB 16
enum
{
SR_IN = 0,
SR_nOE,
SR_RCLK,
SR_SCLK,
SR_nCLR,
SR_PIN_CNT
};
const static uint8_t sr_pins[SR_PIN_CNT] =
{
[SR_IN] = 9,
[SR_nOE] = 10,
[SR_RCLK] = 11,
[SR_SCLK] = 12,
[SR_nCLR] = 13,
};
const static uint8_t sr_pins_def[SR_PIN_CNT] =
{
[SR_IN] = 0,
[SR_nOE] = 1,
[SR_RCLK] = 0,
[SR_SCLK] = 0,
[SR_nCLR] = 1,
};
enum
{
ROM_nWE = 0,
ROM_nOE,
ROM_nCE,
ROM_PIN_CNT
};
const static uint8_t rom_pins[ROM_PIN_CNT] =
{
[ROM_nWE] = 14,
[ROM_nOE] = 15,
[ROM_nCE] = 26,
};
const static uint8_t rom_pins_def[ROM_PIN_CNT] =
{
[ROM_nWE] = 1,
[ROM_nOE] = 1,
[ROM_nCE] = 0,
};
#define PIN_IO_OE 29
#define PIN_IO_OE_DEF 1
const static uint8_t io_pins[8] =
{
2,
3,
4,
5,
6,
7,
27,
28
};
#define BLKS 64
#define BLK_SZ CFG_TUD_MSC_EP_BUFSIZE
#define EEPROM_BLK_SZ 64
static semaphore_t core1_sem;
static mutex_t busy_lock;
static uint16_t pending = 0;
static uint16_t writeoffset = 0;
static uint8_t writebuf[BLK_SZ] = {0};
static uint64_t access = 0;
// XXX: assumes stock clockspeed of 125MHz
#define sleep_ns(ns) \
do{ for(int i = 0; i < ((ns) / 8); i++) __asm volatile ("nop\n"); }while(0)
static void readmode(void)
{
gpio_put(PIN_IO_OE, 0);
for(int i = 0; i < 8; i++)
{
gpio_init(io_pins[i]);
gpio_set_dir(io_pins[i], GPIO_IN);
gpio_pull_up(io_pins[i]);
}
gpio_put(PIN_IO_OE, 1);
}
static void writemode(void)
{
gpio_put(PIN_IO_OE, 0);
for(int i = 0; i < 8; i++)
{
gpio_init(io_pins[i]);
gpio_set_dir(io_pins[i], GPIO_OUT);
gpio_put(io_pins[i], 0);
}
gpio_put(PIN_IO_OE, 1);
}
static void set_addr(uint16_t addr)
{
gpio_put(sr_pins[SR_nOE], 1);
gpio_put(sr_pins[SR_RCLK], 0);
gpio_put(sr_pins[SR_nCLR], 0);
sleep_ns(24);
gpio_put(sr_pins[SR_nCLR], 1);
for(int i = 0; i < 16; i++)
{
gpio_put(sr_pins[SR_IN], addr & 1);
addr = addr >> 1;
gpio_put(sr_pins[SR_SCLK], 0);
sleep_ns(17);
gpio_put(sr_pins[SR_SCLK], 1);
sleep_ns(17);
}
gpio_put(sr_pins[SR_RCLK], 1);
gpio_put(sr_pins[SR_nOE], 0);
sleep_ns(24);
}
void core1_main(void)
{
while(1)
{
sem_acquire_blocking(&core1_sem);
{
mutex_enter_blocking(&busy_lock);
writemode();
gpio_put(rom_pins[ROM_nOE], 1);
int pages = (pending / EEPROM_BLK_SZ) + ((pending % EEPROM_BLK_SZ) ? 1 : 0);
for(int i = 0; i < pages; i++)
{
for(int j = 0; j < EEPROM_BLK_SZ; j++)
{
int off = i * EEPROM_BLK_SZ + j;
if(off >= pending) break;
set_addr(writeoffset + off);
for(int k = 0; k < 8; k++)
gpio_put(io_pins[k], (writebuf[off] >> k) & 1);
sleep_ns(50);
gpio_put(rom_pins[ROM_nWE], 0);
sleep_ns(150);
gpio_put(rom_pins[ROM_nWE], 1);
sleep_ns(50);
}
busy_wait_ms(10);
}
printf("core1: wrote %d bytes to %04x\n", pending, writeoffset);
pending = 0;
mutex_exit(&busy_lock);
}
}
}
int main(void)
{
board_init();
tusb_init();
stdio_init_all();
gpio_init(PIN_RGB);
gpio_set_dir(PIN_RGB, GPIO_OUT);
gpio_put(PIN_RGB, 0);
gpio_init(PIN_IO_OE);
gpio_set_dir(PIN_IO_OE, GPIO_OUT);
gpio_put(PIN_IO_OE, PIN_IO_OE_DEF);
for(int i = 0; i < SR_PIN_CNT; i++)
{
gpio_init(sr_pins[i]);
gpio_set_dir(sr_pins[i], GPIO_OUT);
gpio_put(sr_pins[i], sr_pins_def[i]);
}
for(int i = 0; i < ROM_PIN_CNT; i++)
{
gpio_init(rom_pins[i]);
gpio_set_dir(rom_pins[i], GPIO_OUT);
gpio_put(rom_pins[i], rom_pins_def[i]);
}
readmode();
int sm = 0; // XXX: ???
uint32_t offset = pio_add_program(pio0, &ws2812_program);
ws2812_program_init(pio0, sm, offset, PIN_RGB, 800000, false);
pio_sm_put_blocking(pio0, 0, 0);
sem_init(&core1_sem, 0, 1);
mutex_init(&busy_lock);
multicore_reset_core1();
multicore_launch_core1(core1_main);
sleep_ms(1000);
printf("Hell, world...\n");
uint32_t prev = access;
uint32_t start_ms = 0;
uint32_t interval = 60;
int state = 1;
int prevs = state;
while(1)
{
tud_task();
uint32_t diff = board_millis() - start_ms;
if(diff >= interval)
{
start_ms += diff;
if(prev != access)
{
prev = access;
prevs = state;
state = !state;
if(state) pio_sm_put_blocking(pio0, 0, COL_GRN);
else pio_sm_put_blocking(pio0, 0, 0);
}
else if(prevs != state)
{
state = 1;
prevs = state;
pio_sm_put_blocking(pio0, 0, COL_GRN);
}
}
}
return 0;
}
uint8_t tud_msc_get_maxlun_cb(void)
{
return 1;
}
void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16],
uint8_t product_rev[4])
{
const char vid[] = "CAB_DIG";
const char pid[] = "EEPROM Progrmmr";
const char rev[] = "0dev";
memcpy(vendor_id , vid, strlen(vid));
memcpy(product_id , pid, strlen(pid));
memcpy(product_rev, rev, strlen(rev));
printf("scsi: inq\n");
}
bool tud_msc_test_unit_ready_cb(uint8_t lun)
{
printf("scsi: rdy\n");
return true;
}
void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_size)
{
*block_count = BLKS;
*block_size = BLK_SZ;
printf("scsi: cap\n");
}
bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject)
{
printf("scsi: stop\n");
if(load_eject)
{
if(start) {} // load disk storage
else {} // unload disk storage
}
return true;
}
int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize) {
access++;
if(lba >= BLKS) return -1;
if(bufsize > BLK_SZ) return -1;
if(!mutex_try_enter(&busy_lock, NULL)) return 0;
int addr = (lba * BLK_SZ) + offset;
printf("scsi: read @ %04X\n", addr);
readmode();
uint8_t* bytes = buffer;
for(int i = 0; i < bufsize; i++)
{
set_addr(addr + i);
gpio_put(rom_pins[ROM_nOE], 0);
sleep_ns(150);
uint8_t byte = 0;
for(int j = 0; j < 8; j++)
byte |= gpio_get(io_pins[j]) << j;
bytes[i] = byte;
gpio_put(rom_pins[ROM_nOE], 1);
sleep_ns(150);
}
mutex_exit(&busy_lock);
return (int32_t)bufsize;
}
bool tud_msc_is_writable_cb(uint8_t lun)
{
printf("scsi: rw?\n");
return true;
}
int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer,
uint32_t bufsize)
{
access++;
if(lba >= BLKS) return -1;
if(bufsize > BLK_SZ) return -1;
if(!mutex_try_enter(&busy_lock, NULL)) return 0;
writeoffset = (lba * BLK_SZ) + offset;
printf("scsi: write req @ %04x\n", writeoffset);
memcpy(writebuf, buffer, bufsize);
pending = bufsize;
sem_release(&core1_sem);
mutex_exit(&busy_lock);
return (int32_t) bufsize;
}
int32_t tud_msc_scsi_cb(uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize)
{
void const* response = NULL;
int32_t resplen = 0;
// most scsi handled is input
bool in_xfer = true;
switch (scsi_cmd[0]) {
default:
tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
return -1;
}
if (resplen > bufsize) resplen = bufsize;
if (response && (resplen > 0)) {
if (in_xfer) {
memcpy(buffer, response, (size_t) resplen);
} else {
// SCSI output
}
}
printf("scsi: generic\n");
return resplen;
}
void tud_mount_cb(void)
{
pio_sm_put_blocking(pio0, 0, COL_GRN);
}
void tud_umount_cb(void)
{
pio_sm_put_blocking(pio0, 0, COL_RED);
}
void tud_suspend_cb(bool remote_wakeup_en)
{
pio_sm_put_blocking(pio0, 0, COL_ORG);
}
void tud_resume_cb(void)
{
pio_sm_put_blocking(pio0, 0, COL_BLU);
}
|