1
1
/*
2
2
* This is free and unencumbered software released into the public domain.
3
- *
3
+ *
4
4
* Anyone is free to copy, modify, publish, use, compile, sell, or
5
5
* distribute this software, either in source code form or as a compiled
6
6
* binary, for any purpose, commercial or non-commercial, and by any
7
7
* means.
8
- *
8
+ *
9
9
* In jurisdictions that recognize copyright laws, the author or authors
10
10
* of this software dedicate any and all copyright interest in the
11
11
* software to the public domain. We make this dedication for the benefit
12
12
* of the public at large and to the detriment of our heirs and
13
13
* successors. We intend this dedication to be an overt act of
14
14
* relinquishment in perpetuity of all present and future rights to this
15
15
* software under copyright law.
16
- *
16
+ *
17
17
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
18
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
19
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20
20
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21
21
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22
22
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
23
* OTHER DEALINGS IN THE SOFTWARE.
24
- *
24
+ *
25
25
* For more information, please refer to <http://unlicense.org>
26
26
* -------------------------------------------------------------------------
27
- *
28
- * This is example code to 1) format an SPI Flash chip, and 2) copy raw
29
- * audio files (mono channel, 16 bit signed, 44100Hz) to it using the
30
- * SerialFlash library. The audio can then be played back using the
27
+ *
28
+ * This is example code to 1) format an SPI Flash chip, and 2) copy raw
29
+ * audio files (mono channel, 16 bit signed, 44100Hz) to it using the
30
+ * SerialFlash library. The audio can then be played back using the
31
31
* AudioPlaySerialflashRaw object in the Teensy Audio library.
32
- *
32
+ *
33
33
* To convert a .wav file to the proper .RAW format, use sox:
34
34
* sox input.wav -r 44100 -b 16 --norm -e signed-integer -t raw OUTPUT.RAW remix 1,2
35
- *
35
+ *
36
36
* Note that the OUTPUT.RAW filename must be all caps and contain only the following
37
37
* characters: A-Z, 0-9, comma, period, colon, dash, underscore. (The SerialFlash
38
38
* library converts filenames to caps, so to avoid confusion we just enforce it here).
39
- *
39
+ *
40
40
* It is a little difficult to see what is happening; aswe are using the Serial port
41
41
* to upload files, we can't just throw out debug information. Instead, we use the LED
42
42
* (pin 13) to convey state.
43
- *
44
- * While the chip is being formatted, the LED (pin 13) will toggle at 1Hz rate. When
45
- * the formatting is done, it flashes quickly (10Hz) for one second, then stays on
46
- * solid. When nothing has been received for 3 seconds, the upload is assumed to be
43
+ *
44
+ * While the chip is being formatted, the LED (pin 13) will toggle at 1Hz rate. When
45
+ * the formatting is done, it flashes quickly (10Hz) for one second, then stays on
46
+ * solid. When nothing has been received for 3 seconds, the upload is assumed to be
47
47
* completed, and the light goes off.
48
- *
48
+ *
49
49
* Use the 'rawfile-uploader.py' python script (included in the extras folder) to upload
50
50
* the files. You can start the script as soon as the Teensy is turned on, and the
51
51
* USB serial upload will just buffer and wait until the flash is formatted.
52
- *
53
- * This code was written by Wyatt Olson <wyatt@digitalcave.ca> (originally as part
54
- * of Drum Master http://drummaster.digitalcave.ca and later modified into a
52
+ *
53
+ * This code was written by Wyatt Olson <wyatt@digitalcave.ca> (originally as part
54
+ * of Drum Master http://drummaster.digitalcave.ca and later modified into a
55
55
* standalone sample).
56
- *
56
+ *
57
57
* Enjoy!
58
58
*
59
59
* SerialFlash library API: https://github.com/PaulStoffregen/SerialFlash
60
60
*
61
- * This example depens on http://librarymanager/all#SerialFlash&SPI (clickme!)
61
+ * This example depends on Paul Stoffregen's SerialFlash library
62
+ * Download at https://github.com/PaulStoffregen/SerialFlash
62
63
*/
63
64
64
65
#include < CurieSerialFlash.h>
@@ -85,14 +86,14 @@ void setup(){
85
86
Serial.begin (9600 ); // Teensy serial is always at full USB speed and buffered... the baud rate here is required but ignored
86
87
87
88
pinMode (13 , OUTPUT);
88
-
89
+
89
90
SerialFlash.begin (ONBOARD_FLASH_SPI_PORT, ONBOARD_FLASH_CS_PIN);
90
91
91
92
// We start by formatting the flash...
92
93
uint8_t id[5 ];
93
94
SerialFlash.readID (id);
94
95
SerialFlash.eraseAll ();
95
-
96
+
96
97
// Flash LED at 1Hz while formatting
97
98
while (!SerialFlash.ready ()) {
98
99
delay (500 );
@@ -109,26 +110,26 @@ void setup(){
109
110
digitalWrite (13 , LOW);
110
111
}
111
112
digitalWrite (13 , HIGH);
112
-
113
+
113
114
// We are now going to wait for the upload program
114
115
while (!Serial.available ());
115
-
116
+
116
117
SerialFlashFile flashFile;
117
-
118
+
118
119
uint8_t state = STATE_START;
119
120
uint8_t escape = 0 ;
120
121
uint8_t fileSizeIndex = 0 ;
121
122
uint32_t fileSize = 0 ;
122
123
char filename[FILENAME_STRING_SIZE];
123
-
124
+
124
125
char usbBuffer[USB_BUFFER_SIZE];
125
126
uint8_t flashBuffer[FLASH_BUFFER_SIZE];
126
-
127
+
127
128
uint16_t flashBufferIndex = 0 ;
128
129
uint8_t filenameIndex = 0 ;
129
-
130
+
130
131
uint32_t lastReceiveTime = millis ();
131
-
132
+
132
133
// We assume the serial receive part is finished when we have not received something for 3 seconds
133
134
while (Serial.available () || lastReceiveTime + 3000 > millis ()){
134
135
uint16_t available = Serial.readBytes (usbBuffer, USB_BUFFER_SIZE);
@@ -138,7 +139,7 @@ void setup(){
138
139
139
140
for (uint16_t usbBufferIndex = 0 ; usbBufferIndex < available; usbBufferIndex++){
140
141
uint8_t b = usbBuffer[usbBufferIndex];
141
-
142
+
142
143
if (state == STATE_START){
143
144
// Start byte. Repeat start is fine.
144
145
if (b == BYTE_START){
@@ -163,12 +164,12 @@ void setup(){
163
164
flushError ();
164
165
return ;
165
166
}
166
-
167
+
167
168
// Change state
168
169
state = STATE_SIZE;
169
170
fileSizeIndex = 0 ;
170
171
fileSize = 0 ;
171
-
172
+
172
173
}
173
174
// Invalid character
174
175
else {
@@ -187,11 +188,11 @@ void setup(){
187
188
state = STATE_CONTENT;
188
189
flashBufferIndex = 0 ;
189
190
escape = 0 ;
190
-
191
+
191
192
if (SerialFlash.exists (filename)){
192
193
SerialFlash.remove (filename); // It doesn't reclaim the space, but it does let you create a new file with the same name.
193
194
}
194
-
195
+
195
196
// Create a new file and open it for writing
196
197
if (SerialFlash.create (filename, fileSize)) {
197
198
flashFile = SerialFlash.open (filename);
@@ -236,7 +237,7 @@ void setup(){
236
237
else {
237
238
flashBuffer[flashBufferIndex++] = b;
238
239
}
239
-
240
+
240
241
// The buffer is filled; write to SD card
241
242
if (flashBufferIndex >= FLASH_BUFFER_SIZE){
242
243
flashFile.write (flashBuffer, FLASH_BUFFER_SIZE);
0 commit comments