-
Notifications
You must be signed in to change notification settings - Fork 3
/
picoduck.ino
261 lines (199 loc) · 5.41 KB
/
picoduck.ino
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
#include "common.h"
#include "lua_api.h"
#include "SPI.h"
#include "SdFat.h"
#include "Adafruit_SPIFlash.h"
#include "ff.h"
#include "diskio.h"
#define DISK_LABEL "Picoduck"
Adafruit_FlashTransport_RP2040 flashTransport;
bool usb_mass_storage = false;
Adafruit_SPIFlash flash(&flashTransport);
void format_fat12(void) {
// Working buffer for f_mkfs.
uint8_t workbuf[4096];
// Elm Cham's fatfs objects
FATFS elmchamFatfs;
// Make filesystem.
FRESULT r = f_mkfs("", FM_FAT, 0, workbuf, sizeof(workbuf));
if (r != FR_OK) {
Serial.print(F("Error, f_mkfs failed with error code: ")); Serial.println(r, DEC);
while(1) yield();
}
// mount to set disk label
r = f_mount(&elmchamFatfs, "0:", 1);
if (r != FR_OK) {
Serial.print(F("Error, f_mount failed with error code: ")); Serial.println(r, DEC);
while(1) yield();
}
// Setting label
Serial.println(F("Setting disk label to: " DISK_LABEL));
r = f_setlabel(DISK_LABEL);
if (r != FR_OK) {
Serial.print(F("Error, f_setlabel failed with error code: ")); Serial.println(r, DEC);
while(1) yield();
}
// unmount
f_unmount("0:");
// sync to make sure all data is written to flash
flash.syncBlocks();
Serial.println(F("Formatted flash!"));
}
// file system object from SdFat
FatVolume fatfs;
Adafruit_USBD_MSC usb_msc;
// Check if flash is formatted
bool fs_formatted;
byte get_program_index_from_pins() {
byte final = 0;
if (digitalRead(PIN_B0) == HIGH) final += 1;
if (digitalRead(PIN_B1) == HIGH) final += 2;
if (digitalRead(PIN_B2) == HIGH) final += 4;
if (digitalRead(PIN_B3) == HIGH) final += 8;
return final;
}
int32_t msc_read_cb (uint32_t lba, void* buffer, uint32_t bufsize) {
return flash.readBlocks(lba, (uint8_t*) buffer, bufsize/512) ? bufsize : -1;
}
int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize) {
digitalWrite(LED_BUILTIN, HIGH);
return flash.writeBlocks(lba, buffer, bufsize/512) ? bufsize : -1;
}
void msc_flush_cb (void) {
flash.syncBlocks();
fatfs.cacheClear();
digitalWrite(LED_BUILTIN, LOW);
}
void led_loop(int rate=1000) {
for (;;) {
digitalWrite(BUILT_IN_LED_PIN, HIGH);
delay(rate);
digitalWrite(BUILT_IN_LED_PIN, LOW);
delay(rate);
}
}
void setup() {
Serial.begin(9600);
pinMode(BUILT_IN_LED_PIN, OUTPUT);
pinMode(EXTERNAL_LED_PIN, OUTPUT);
// Random seed from ADC noise.
pinMode(26, INPUT);
srand(analogRead(26) * 10000);
pinMode(HOLD_PIN, INPUT);
pinMode(MSD_PIN, INPUT);
pinMode(PIN_B0, INPUT);
pinMode(PIN_B1, INPUT);
pinMode(PIN_B2, INPUT);
pinMode(PIN_B3, INPUT);
byte program_index = get_program_index_from_pins();
#ifdef DONT_USE_MSD_PIN
if (program_index == 0b1111) {
#else
if (digitalRead(MSD_PIN) == HIGH) {
#endif
usb_mass_storage = true;
} else {
Keyboard.begin();
Mouse.begin();
}
flash.begin();
if (usb_mass_storage) {
usb_msc.setID("Picoduck", "Internal Memory", "1.0");
usb_msc.setReadWriteCallback(msc_read_cb, msc_write_cb, msc_flush_cb);
usb_msc.setCapacity(flash.size() / 512, 512);
usb_msc.setUnitReady(true);
usb_msc.begin();
}
fs_formatted = fatfs.begin(&flash);
if (!fs_formatted) {
format_fat12();
fs_formatted = fatfs.begin(&flash);
if (!fs_formatted) {
led_loop();
}
}
if (usb_mass_storage) {
digitalWrite(BUILT_IN_LED_PIN, HIGH);
return;
}
String program_code_path = String(program_index) + ".lua";
File32 file = fatfs.open(program_code_path.c_str(), FILE_READ);
if (!file)
led_loop(500);
char file_contents[file.available()+1] = { '\0' };
file.read(file_contents, file.available());
file.close();
fatfs.end();
flash.end();
lua_State *L = luaL_newstate();
if (L == 0)
return;
luaL_openlibs(L);
lua_settop(L, 0);
l_init(L);
luaL_loadstring(L, file_contents);
if (lua_pcall(L, 0, 0, 0) != 0) {
Serial.print("Lua error: ");
Serial.println(lua_tostring(L,-1));
} else {
Serial.println("Program ran successfully.");
}
}
void loop() { }
void setup1() { }
void loop1() {
hold = digitalRead(HOLD_PIN) == HIGH;
}
// fatfs diskio
extern "C" {
DSTATUS disk_status ( BYTE pdrv ) {
(void) pdrv;
return 0;
}
DSTATUS disk_initialize ( BYTE pdrv ) {
(void) pdrv;
return 0;
}
DRESULT disk_read (
BYTE pdrv, // Physical drive nmuber to identify the drive
BYTE *buff, // Data buffer to store read data
DWORD sector, // Start sector in LBA
UINT count // Number of sectors to read
) {
(void) pdrv;
return flash.readBlocks(sector, buff, count) ? RES_OK : RES_ERROR;
}
DRESULT disk_write (
BYTE pdrv, // Physical drive nmuber to identify the drive
const BYTE *buff, // Data to be written
DWORD sector, // Start sector in LBA
UINT count // Number of sectors to write
){
(void) pdrv;
return flash.writeBlocks(sector, buff, count) ? RES_OK : RES_ERROR;
}
DRESULT disk_ioctl (
BYTE pdrv, // Physical drive nmuber (0..)
BYTE cmd, // Control code
void *buff // Buffer to send/receive control data
) {
(void) pdrv;
switch ( cmd )
{
case CTRL_SYNC:
flash.syncBlocks();
return RES_OK;
case GET_SECTOR_COUNT:
*((DWORD*) buff) = flash.size()/512;
return RES_OK;
case GET_SECTOR_SIZE:
*((WORD*) buff) = 512;
return RES_OK;
case GET_BLOCK_SIZE:
*((DWORD*) buff) = 8; // erase block size in units of sector size
return RES_OK;
default:
return RES_PARERR;
}
}
}