You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

275 line
8.8KB

  1. /*
  2. * This is free and unencumbered software released into the public domain.
  3. *
  4. * Anyone is free to copy, modify, publish, use, compile, sell, or
  5. * distribute this software, either in source code form or as a compiled
  6. * binary, for any purpose, commercial or non-commercial, and by any
  7. * means.
  8. *
  9. * In jurisdictions that recognize copyright laws, the author or authors
  10. * of this software dedicate any and all copyright interest in the
  11. * software to the public domain. We make this dedication for the benefit
  12. * of the public at large and to the detriment of our heirs and
  13. * successors. We intend this dedication to be an overt act of
  14. * relinquishment in perpetuity of all present and future rights to this
  15. * software under copyright law.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  21. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  22. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. * OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. * For more information, please refer to <http://unlicense.org>
  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
  31. * AudioPlaySerialflashRaw object in the Teensy Audio library.
  32. *
  33. * To convert a .wav file to the proper .RAW format, use sox:
  34. * sox input.wav -r 44100 -b 16 --norm -e signed-integer -t raw OUTPUT.RAW remix 1,2
  35. *
  36. * Note that the OUTPUT.RAW filename must be all caps and contain only the following
  37. * characters: A-Z, 0-9, comma, period, colon, dash, underscore. (The SerialFlash
  38. * library converts filenames to caps, so to avoid confusion we just enforce it here).
  39. *
  40. * It is a little difficult to see what is happening; aswe are using the Serial port
  41. * to upload files, we can't just throw out debug information. Instead, we use the LED
  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
  47. * completed, and the light goes off.
  48. *
  49. * Use the 'rawfile-uploader.py' python script (included in the extras folder) to upload
  50. * the files. You can start the script as soon as the Teensy is turned on, and the
  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
  55. * standalone sample).
  56. *
  57. * Enjoy!
  58. */
  59. #include <SerialFlash.h>
  60. #include <SPI.h>
  61. //Buffer sizes
  62. #define USB_BUFFER_SIZE 128
  63. #define FLASH_BUFFER_SIZE 4096
  64. //Max filename length (8.3 plus a null char terminator)
  65. #define FILENAME_STRING_SIZE 13
  66. //State machine
  67. #define STATE_START 0
  68. #define STATE_SIZE 1
  69. #define STATE_CONTENT 2
  70. //Special bytes in the communication protocol
  71. #define BYTE_START 0x7e
  72. #define BYTE_ESCAPE 0x7d
  73. #define BYTE_SEPARATOR 0x7c
  74. //SPI Pins (these are the values on the Audio board; change them if you have different ones)
  75. #define MOSI 7
  76. #define MISO 12
  77. #define SCK 14
  78. #define CSPIN 6
  79. //#define CSPIN 21 // Arduino 101 built-in SPI Flash
  80. void setup(){
  81. Serial.begin(9600); //Teensy serial is always at full USB speed and buffered... the baud rate here is required but ignored
  82. pinMode(13, OUTPUT);
  83. //Set up SPI
  84. SPI.setMOSI(MOSI);
  85. SPI.setMISO(MISO);
  86. SPI.setSCK(SCK);
  87. SerialFlash.begin(CSPIN);
  88. //We start by formatting the flash...
  89. uint8_t id[5];
  90. SerialFlash.readID(id);
  91. SerialFlash.eraseAll();
  92. //Flash LED at 1Hz while formatting
  93. while (!SerialFlash.ready()) {
  94. delay(500);
  95. digitalWrite(13, HIGH);
  96. delay(500);
  97. digitalWrite(13, LOW);
  98. }
  99. //Quickly flash LED a few times when completed, then leave the light on solid
  100. for(uint8_t i = 0; i < 10; i++){
  101. delay(100);
  102. digitalWrite(13, HIGH);
  103. delay(100);
  104. digitalWrite(13, LOW);
  105. }
  106. digitalWrite(13, HIGH);
  107. //We are now going to wait for the upload program
  108. while(!Serial.available());
  109. SerialFlashFile flashFile;
  110. uint8_t state = STATE_START;
  111. uint8_t escape = 0;
  112. uint8_t fileSizeIndex = 0;
  113. uint32_t fileSize = 0;
  114. char filename[FILENAME_STRING_SIZE];
  115. char usbBuffer[USB_BUFFER_SIZE];
  116. uint8_t flashBuffer[FLASH_BUFFER_SIZE];
  117. uint16_t flashBufferIndex = 0;
  118. uint8_t filenameIndex = 0;
  119. uint32_t lastReceiveTime = millis();
  120. //We assume the serial receive part is finished when we have not received something for 3 seconds
  121. while(Serial.available() || lastReceiveTime + 3000 > millis()){
  122. uint16_t available = Serial.readBytes(usbBuffer, USB_BUFFER_SIZE);
  123. if (available){
  124. lastReceiveTime = millis();
  125. }
  126. for (uint16_t usbBufferIndex = 0; usbBufferIndex < available; usbBufferIndex++){
  127. uint8_t b = usbBuffer[usbBufferIndex];
  128. if (state == STATE_START){
  129. //Start byte. Repeat start is fine.
  130. if (b == BYTE_START){
  131. for (uint8_t i = 0; i < FILENAME_STRING_SIZE; i++){
  132. filename[i] = 0x00;
  133. }
  134. filenameIndex = 0;
  135. }
  136. //Valid characters are A-Z, 0-9, comma, period, colon, dash, underscore
  137. else if ((b >= 'A' && b <= 'Z') || (b >= '0' && b <= '9') || b == '.' || b == ',' || b == ':' || b == '-' || b == '_'){
  138. filename[filenameIndex++] = b;
  139. if (filenameIndex >= FILENAME_STRING_SIZE){
  140. //Error name too long
  141. flushError();
  142. return;
  143. }
  144. }
  145. //Filename end character
  146. else if (b == BYTE_SEPARATOR){
  147. if (filenameIndex == 0){
  148. //Error empty filename
  149. flushError();
  150. return;
  151. }
  152. //Change state
  153. state = STATE_SIZE;
  154. fileSizeIndex = 0;
  155. fileSize = 0;
  156. }
  157. //Invalid character
  158. else {
  159. //Error bad filename
  160. flushError();
  161. return;
  162. }
  163. }
  164. //We read 4 bytes as a uint32_t for file size
  165. else if (state == STATE_SIZE){
  166. if (fileSizeIndex < 4){
  167. fileSize = (fileSize << 8) + b;
  168. fileSizeIndex++;
  169. }
  170. else if (b == BYTE_SEPARATOR){
  171. state = STATE_CONTENT;
  172. flashBufferIndex = 0;
  173. escape = 0;
  174. if (SerialFlash.exists(filename)){
  175. SerialFlash.remove(filename); //It doesn't reclaim the space, but it does let you create a new file with the same name.
  176. }
  177. //Create a new file and open it for writing
  178. if (SerialFlash.create(filename, fileSize)) {
  179. flashFile = SerialFlash.open(filename);
  180. if (!flashFile) {
  181. //Error flash file open
  182. flushError();
  183. return;
  184. }
  185. }
  186. else {
  187. //Error flash create (no room left?)
  188. flushError();
  189. return;
  190. }
  191. }
  192. else {
  193. //Error invalid length requested
  194. flushError();
  195. return;
  196. }
  197. }
  198. else if (state == STATE_CONTENT){
  199. //Previous byte was escaped; unescape and add to buffer
  200. if (escape){
  201. escape = 0;
  202. flashBuffer[flashBufferIndex++] = b ^ 0x20;
  203. }
  204. //Escape the next byte
  205. else if (b == BYTE_ESCAPE){
  206. //Serial.println("esc");
  207. escape = 1;
  208. }
  209. //End of file
  210. else if (b == BYTE_START){
  211. //Serial.println("End of file");
  212. state = STATE_START;
  213. flashFile.write(flashBuffer, flashBufferIndex);
  214. flashFile.close();
  215. flashBufferIndex = 0;
  216. }
  217. //Normal byte; add to buffer
  218. else {
  219. flashBuffer[flashBufferIndex++] = b;
  220. }
  221. //The buffer is filled; write to SD card
  222. if (flashBufferIndex >= FLASH_BUFFER_SIZE){
  223. flashFile.write(flashBuffer, FLASH_BUFFER_SIZE);
  224. flashBufferIndex = 0;
  225. }
  226. }
  227. }
  228. }
  229. //Success! Turn the light off.
  230. digitalWrite(13, LOW);
  231. }
  232. void loop(){
  233. //Do nothing.
  234. }
  235. void flushError(){
  236. uint32_t lastReceiveTime = millis();
  237. char usbBuffer[USB_BUFFER_SIZE];
  238. //We assume the serial receive part is finished when we have not received something for 3 seconds
  239. while(Serial.available() || lastReceiveTime + 3000 > millis()){
  240. if (Serial.readBytes(usbBuffer, USB_BUFFER_SIZE)){
  241. lastReceiveTime = millis();
  242. }
  243. }
  244. }