Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

281 rinda
9.0KB

  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); // uncomment these if using the alternate pins
  85. //SPI.setMISO(MISO);
  86. //SPI.setSCK(SCK);
  87. if (!SerialFlash.begin(CSPIN)) {
  88. while (1) {
  89. Serial.println("Unable to access SPI Flash chip");
  90. delay(1000);
  91. }
  92. }
  93. //We start by formatting the flash...
  94. uint8_t id[5];
  95. SerialFlash.readID(id);
  96. SerialFlash.eraseAll();
  97. //Flash LED at 1Hz while formatting
  98. while (!SerialFlash.ready()) {
  99. delay(500);
  100. digitalWrite(13, HIGH);
  101. delay(500);
  102. digitalWrite(13, LOW);
  103. }
  104. //Quickly flash LED a few times when completed, then leave the light on solid
  105. for(uint8_t i = 0; i < 10; i++){
  106. delay(100);
  107. digitalWrite(13, HIGH);
  108. delay(100);
  109. digitalWrite(13, LOW);
  110. }
  111. digitalWrite(13, HIGH);
  112. //We are now going to wait for the upload program
  113. while(!Serial.available());
  114. SerialFlashFile flashFile;
  115. uint8_t state = STATE_START;
  116. uint8_t escape = 0;
  117. uint8_t fileSizeIndex = 0;
  118. uint32_t fileSize = 0;
  119. char filename[FILENAME_STRING_SIZE];
  120. char usbBuffer[USB_BUFFER_SIZE];
  121. uint8_t flashBuffer[FLASH_BUFFER_SIZE];
  122. uint16_t flashBufferIndex = 0;
  123. uint8_t filenameIndex = 0;
  124. uint32_t lastReceiveTime = millis();
  125. //We assume the serial receive part is finished when we have not received something for 3 seconds
  126. while(Serial.available() || lastReceiveTime + 3000 > millis()){
  127. uint16_t available = Serial.readBytes(usbBuffer, USB_BUFFER_SIZE);
  128. if (available){
  129. lastReceiveTime = millis();
  130. }
  131. for (uint16_t usbBufferIndex = 0; usbBufferIndex < available; usbBufferIndex++){
  132. uint8_t b = usbBuffer[usbBufferIndex];
  133. if (state == STATE_START){
  134. //Start byte. Repeat start is fine.
  135. if (b == BYTE_START){
  136. for (uint8_t i = 0; i < FILENAME_STRING_SIZE; i++){
  137. filename[i] = 0x00;
  138. }
  139. filenameIndex = 0;
  140. }
  141. //Valid characters are A-Z, 0-9, comma, period, colon, dash, underscore
  142. else if ((b >= 'A' && b <= 'Z') || (b >= '0' && b <= '9') || b == '.' || b == ',' || b == ':' || b == '-' || b == '_'){
  143. filename[filenameIndex++] = b;
  144. if (filenameIndex >= FILENAME_STRING_SIZE){
  145. //Error name too long
  146. flushError();
  147. return;
  148. }
  149. }
  150. //Filename end character
  151. else if (b == BYTE_SEPARATOR){
  152. if (filenameIndex == 0){
  153. //Error empty filename
  154. flushError();
  155. return;
  156. }
  157. //Change state
  158. state = STATE_SIZE;
  159. fileSizeIndex = 0;
  160. fileSize = 0;
  161. }
  162. //Invalid character
  163. else {
  164. //Error bad filename
  165. flushError();
  166. return;
  167. }
  168. }
  169. //We read 4 bytes as a uint32_t for file size
  170. else if (state == STATE_SIZE){
  171. if (fileSizeIndex < 4){
  172. fileSize = (fileSize << 8) + b;
  173. fileSizeIndex++;
  174. }
  175. else if (b == BYTE_SEPARATOR){
  176. state = STATE_CONTENT;
  177. flashBufferIndex = 0;
  178. escape = 0;
  179. if (SerialFlash.exists(filename)){
  180. SerialFlash.remove(filename); //It doesn't reclaim the space, but it does let you create a new file with the same name.
  181. }
  182. //Create a new file and open it for writing
  183. if (SerialFlash.create(filename, fileSize)) {
  184. flashFile = SerialFlash.open(filename);
  185. if (!flashFile) {
  186. //Error flash file open
  187. flushError();
  188. return;
  189. }
  190. }
  191. else {
  192. //Error flash create (no room left?)
  193. flushError();
  194. return;
  195. }
  196. }
  197. else {
  198. //Error invalid length requested
  199. flushError();
  200. return;
  201. }
  202. }
  203. else if (state == STATE_CONTENT){
  204. //Previous byte was escaped; unescape and add to buffer
  205. if (escape){
  206. escape = 0;
  207. flashBuffer[flashBufferIndex++] = b ^ 0x20;
  208. }
  209. //Escape the next byte
  210. else if (b == BYTE_ESCAPE){
  211. //Serial.println("esc");
  212. escape = 1;
  213. }
  214. //End of file
  215. else if (b == BYTE_START){
  216. //Serial.println("End of file");
  217. state = STATE_START;
  218. flashFile.write(flashBuffer, flashBufferIndex);
  219. flashFile.close();
  220. flashBufferIndex = 0;
  221. }
  222. //Normal byte; add to buffer
  223. else {
  224. flashBuffer[flashBufferIndex++] = b;
  225. }
  226. //The buffer is filled; write to SD card
  227. if (flashBufferIndex >= FLASH_BUFFER_SIZE){
  228. flashFile.write(flashBuffer, FLASH_BUFFER_SIZE);
  229. flashBufferIndex = 0;
  230. }
  231. }
  232. }
  233. }
  234. //Success! Turn the light off.
  235. digitalWrite(13, LOW);
  236. }
  237. void loop(){
  238. //Do nothing.
  239. }
  240. void flushError(){
  241. uint32_t lastReceiveTime = millis();
  242. char usbBuffer[USB_BUFFER_SIZE];
  243. //We assume the serial receive part is finished when we have not received something for 3 seconds
  244. while(Serial.available() || lastReceiveTime + 3000 > millis()){
  245. if (Serial.readBytes(usbBuffer, USB_BUFFER_SIZE)){
  246. lastReceiveTime = millis();
  247. }
  248. }
  249. }