-
Notifications
You must be signed in to change notification settings - Fork 8
/
sd.c
319 lines (253 loc) · 7.03 KB
/
sd.c
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
/*
Thymio-II Firmware
Copyright (C) 2011 Philippe Retornaz <philippe dot retornaz at epfl dot ch>,
Mobots group (http://mobots.epfl.ch), Robotics system laboratory (http://lsro.epfl.ch)
EPFL Ecole polytechnique federale de Lausanne (http://www.epfl.ch)
See authors.txt for more details about other contributors.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <types/types.h>
#include "sd/diskio.h"
#include "sd/ff.h"
#include "regulator.h"
#include "sound.h"
#include "behavior.h"
#include "wav.h"
#include "abo.h"
#include "playback.h"
#include "log.h"
static __attribute((far)) FATFS fs; // SD fat
static __attribute((far)) FIL read_file; // Read handle
static __attribute((far)) FIL write_file; // Write handle
static __attribute((far)) FIL user_file; // Read/write file accessible from the VM
static unsigned char l;
#define SD_PRIO 2
// Basic rule: ALL SD ACCESS IS DONE AT IPL == 1 !!
DWORD get_fattime(void) {
return ((2011UL - 1980) << 25)
| ((18UL) << 21)
| ((2UL) << 16)
| (0 << 11)
| (0 << 5)
| (0 >> 1);
}
void sd_init(void) {
va_get(); // For the SD card refcounting not done inside it
f_mount(0,&fs); // Cannot fail (it's just initialisation)
}
void sd_shutdown(void) {
unsigned int flags;
RAISE_IPL(flags, SD_PRIO);
f_close(&read_file);
f_close(&write_file);
f_close(&user_file);
f_mount(0,0);
_TRISB14 = 1;
SPI2STATbits.SPIEN = 0; /* Disable SPI2 */
va_put();
IRQ_ENABLE(flags);
}
static int sd_play_cb(unsigned char * buffer) {
unsigned int read;
unsigned int i;
f_read(&read_file, buffer, SOUND_OBUFSZ, &read);
// Fill the remaining bytes with middle value
for(i = read; i < SOUND_OBUFSZ; i++)
buffer[i] = 127;
// sd buffer size 512, so we want to stay 512 aligned,
// So even if we are at the end, we fill the end of the buffer with 0
// So next read, we stay aligned (loop case only).
if(read != SOUND_OBUFSZ && l) {
// If loop and EOF soon
wav_rewind(&read_file);
read = SOUND_OBUFSZ;
}
if(read == 0) {
behavior_notify_sd(BEHAVIOR_STOP | BEHAVIOR_SD_READ);
playback_notify_eop();
return 0; // End of playback
} else
return 1; // Still some data ...
}
// return 1 if file found
// return 0 if not found
int sd_play_file(const char * file, int loop) {
// start to play a file ...
unsigned int flags;
int ret;
RAISE_IPL(flags, SD_PRIO);
f_close(&read_file); // Close the last read file
if(f_open(&read_file, file, FA_READ) == FR_OK && !wav_init(&read_file)) {
sound_playback_hold();
l = loop;
sound_playback_enable(sd_play_cb);
behavior_notify_sd(BEHAVIOR_START | BEHAVIOR_SD_READ);
log_set_flag(LOG_FLAG_PLAYBACKSD);
ret = 1;
} else {
behavior_notify_sd(BEHAVIOR_STOP | BEHAVIOR_SD_READ);
ret = 0;
}
IRQ_ENABLE(flags);
return ret;
}
static int record;
static unsigned long sample_count;
static void close_write_file(void) {
record = 0;
behavior_notify_sd(BEHAVIOR_STOP | BEHAVIOR_SD_WRITE);
// Check if the file is open
if(!write_file.fs)
return;
// If we have writtend at least one sample
if(sample_count)
wav_finalize_header(&write_file, sample_count);
sample_count = 0;
f_close(&write_file);
}
void sound_mic_buffer(unsigned char *b) {
unsigned int written;
if(record) {
f_write(&write_file, b, SOUND_IBUFSZ, &written);
sample_count += written;
if(written != SOUND_IBUFSZ)
close_write_file();
}
}
void sd_start_record(const char * file) {
unsigned int flags;
RAISE_IPL(flags, SD_PRIO);
// Make sure the file is closed
close_write_file();
if(!f_open(&write_file, file, FA_WRITE | FA_CREATE_ALWAYS)) {
sample_count = 0;
if(!wav_create_header(&write_file)) {
log_set_flag(LOG_FLAG_RECORDSD);
behavior_notify_sd(BEHAVIOR_START | BEHAVIOR_SD_WRITE);
record = 1;
} else
f_close(&write_file);
}
IRQ_ENABLE(flags);
}
void sd_stop_record(void) {
unsigned int flags;
RAISE_IPL(flags, SD_PRIO);
close_write_file();
IRQ_ENABLE(flags);
}
int sd_read_duration(const char * file)
{
unsigned int flags;
unsigned long duration;
RAISE_IPL(flags, SD_PRIO);
// Make sure the file is closed
f_close(&read_file);
if(f_open(&read_file, file, FA_READ) == FR_OK){
duration = wav_header_read_duration(&read_file);
duration = duration * 5 / 3906; // change in 10th of sec 7812byte/s
if ( duration > 32767)
duration = 32767;
}
else
duration = 0;
f_close(&read_file);
IRQ_ENABLE(flags);
return (int) duration;
}
void sd_log_file(void) {
unsigned int flags;
RAISE_IPL(flags,SD_PRIO);
if(f_open(&write_file, "_LOGDUMP.#@!", FA_WRITE | FA_OPEN_EXISTING) == FR_OK){
log_dump(&write_file);
f_close(&write_file);
}
IRQ_ENABLE(flags);
}
int sd_test_file_present(void) {
unsigned int flags;
int ret;
RAISE_IPL(flags,SD_PRIO);
if(f_open(&read_file, "_TESTMOD.#@!", FA_READ) == FR_OK) {
f_close(&read_file);
ret = 1;
} else {
ret = 0;
}
IRQ_ENABLE(flags);
return ret;
}
int sd_load_aseba_code(void) {
unsigned int flags;
int ret;
RAISE_IPL(flags, SD_PRIO);
if(f_open(&read_file, "VMCODE.ABO", FA_READ) == FR_OK) {
ret = abo_load(&read_file);
f_close(&read_file);
} else {
ret = -1;
}
IRQ_ENABLE(flags);
return ret;
}
int sd_user_open(char * name) {
int ret = 0;
unsigned int flags;
RAISE_IPL(flags, SD_PRIO);
f_close(&user_file);
if(name) {
if(f_open(&user_file, name, FA_READ | FA_WRITE | FA_OPEN_ALWAYS) != FR_OK) {
ret = -1;
} else {
behavior_notify_sd(BEHAVIOR_START | BEHAVIOR_SD_FILE_ACCESS);
}
}
IRQ_ENABLE(flags);
return ret;
}
int sd_user_seek(unsigned long offset) {
int ret;
unsigned int flags;
RAISE_IPL(flags, SD_PRIO);
if(f_lseek(&user_file, offset) == FR_OK) {
ret = 0;
behavior_notify_sd(BEHAVIOR_START | BEHAVIOR_SD_FILE_ACCESS);
} else
ret = -1;
IRQ_ENABLE(flags);
return ret;
}
unsigned int sd_user_read(unsigned char * data, unsigned int size) {
unsigned int ret;
unsigned int flags;
unsigned int read;
RAISE_IPL(flags, SD_PRIO);
if(f_read(&user_file, data, size, &read) == FR_OK) {
ret = read;
behavior_notify_sd(BEHAVIOR_START | BEHAVIOR_SD_FILE_ACCESS);
} else
ret = 0;
IRQ_ENABLE(flags);
return ret;
}
unsigned int sd_user_write(unsigned char * data, unsigned int size) {
unsigned int ret;
unsigned int flags;
unsigned int written;
RAISE_IPL(flags, SD_PRIO);
if(f_write(&user_file, data, size, &written) == FR_OK) {
ret = written;
behavior_notify_sd(BEHAVIOR_START | BEHAVIOR_SD_FILE_ACCESS);
} else
ret = 0;
IRQ_ENABLE(flags);
return ret;
}