FS.C 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. /*
  2. * Filesystem library
  3. * Handles all filesystem related work for userBDOS programs using CH376.
  4. * Uses only bottom USB port (SPI1 CH376),
  5. * allowing top USB top to be used for other USB devices (like HID).
  6. * Uses blocking delays for now.
  7. */
  8. // uses stdlib.c
  9. #define FS_INTERRUPT_ADDR 0xC0272D
  10. //CH376 Codes
  11. #define FS_CMD_GET_IC_VER 0x01
  12. #define FS_CMD_SET_BAUDRATE 0x02
  13. #define FS_CMD_ENTER_SLEEP 0x03
  14. #define FS_CMD_SET_USB_SPEED 0x04
  15. #define FS_CMD_RESET_ALL 0x05
  16. #define FS_CMD_CHECK_EXIST 0x06
  17. #define FS_CMD_SET_SD0_INT 0x0b
  18. #define FS_CMD_SET_RETRY 0x0b
  19. #define FS_CMD_GET_FILE_SIZE 0x0c
  20. #define FS_CMD_SET_FILE_SIZE 0x0d
  21. #define FS_CMD_SET_USB_ADDRESS 0x13
  22. #define FS_CMD_SET_USB_MODE 0x15
  23. #define FS_MODE_HOST_0 0x05
  24. #define FS_MODE_HOST_1 0x07
  25. #define FS_MODE_HOST_2 0x06
  26. #define FS_CMD_GET_STATUS 0x22
  27. #define FS_CMD_RD_USB_DATA0 0x27
  28. #define FS_CMD_WR_USB_DATA 0x2c
  29. #define FS_CMD_WR_REQ_DATA 0x2d
  30. #define FS_CMD_WR_OFS_DATA 0x2e
  31. #define FS_CMD_SET_FILE_NAME 0x2f
  32. #define FS_CMD_DISK_CONNECT 0x30
  33. #define FS_CMD_DISK_MOUNT 0x31
  34. #define FS_CMD_FILE_OPEN 0x32
  35. #define FS_CMD_FILE_ENUM_GO 0x33
  36. #define FS_CMD_FILE_CREATE 0x34
  37. #define FS_CMD_FILE_ERASE 0x35
  38. #define FS_CMD_FILE_CLOSE 0x36
  39. #define FS_CMD_DIR_INFO_READ 0x37
  40. #define FS_CMD_DIR_INFO_SAVE 0x38
  41. #define FS_CMD_BYTE_LOCATE 0x39
  42. #define FS_CMD_BYTE_READ 0x3a
  43. #define FS_CMD_BYTE_RD_GO 0x3b
  44. #define FS_CMD_BYTE_WRITE 0x3c
  45. #define FS_CMD_BYTE_WR_GO 0x3d
  46. #define FS_CMD_DISK_CAPACITY 0x3e
  47. #define FS_CMD_DISK_QUERY 0x3f
  48. #define FS_CMD_DIR_CREATE 0x40
  49. #define FS_CMD_SET_ADDRESS 0x45
  50. #define FS_CMD_GET_DESCR 0x46
  51. #define FS_CMD_SET_CONFIG 0x49
  52. #define FS_CMD_AUTO_CONFIG 0x4D
  53. #define FS_CMD_ISSUE_TKN_X 0x4E
  54. #define FS_ANSW_RET_SUCCESS 0x51
  55. #define FS_ANSW_USB_INT_SUCCESS 0x14
  56. #define FS_ANSW_USB_INT_CONNECT 0x15
  57. #define FS_ANSW_USB_INT_DISCONNECT 0x16
  58. #define FS_ANSW_USB_INT_USB_READY 0x18
  59. #define FS_ANSW_USB_INT_DISK_READ 0x1d
  60. #define FS_ANSW_USB_INT_DISK_WRITE 0x1e
  61. #define FS_ANSW_RET_ABORT 0x5F
  62. #define FS_ANSW_USB_INT_DISK_ERR 0x1f
  63. #define FS_ANSW_USB_INT_BUF_OVER 0x17
  64. #define FS_ANSW_ERR_OPEN_DIR 0x41
  65. #define FS_ANSW_ERR_MISS_FILE 0x42
  66. #define FS_ANSW_ERR_FOUND_NAME 0x43
  67. #define FS_ANSW_ERR_DISK_DISCON 0x82
  68. #define FS_ANSW_ERR_LARGE_SECTOR 0x84
  69. #define FS_ANSW_ERR_TYPE_ERROR 0x92
  70. #define FS_ANSW_ERR_BPB_ERROR 0xa1
  71. #define FS_ANSW_ERR_DISK_FULL 0xb1
  72. #define FS_ANSW_ERR_FDT_OVER 0xb2
  73. #define FS_ANSW_ERR_FILE_CLOSE 0xb4
  74. #define FS_ERR_LONGFILENAME 0x01
  75. #define FS_ATTR_READ_ONLY 0x01
  76. #define FS_ATTR_HIDDEN 0x02
  77. #define FS_ATTR_SYSTEM 0x04
  78. #define FS_ATTR_VOLUME_ID 0x08
  79. #define FS_ATTR_DIRECTORY 0x10
  80. #define FS_ATTR_ARCHIVE 0x20
  81. // Sets SPI1_CS low
  82. void FS_spiBeginTransfer()
  83. {
  84. asm(
  85. "; backup regs\n"
  86. "push r1\n"
  87. "push r2\n"
  88. "load32 0xC0272C r2 ; r2 = 0xC0272C\n"
  89. "load 0 r1 ; r1 = 0 (enable)\n"
  90. "write 0 r2 r1 ; write to SPI1_CS\n"
  91. "; restore regs\n"
  92. "pop r2\n"
  93. "pop r1\n"
  94. );
  95. }
  96. // Sets SPI1_CS high
  97. void FS_spiEndTransfer()
  98. {
  99. asm(
  100. "; backup regs\n"
  101. "push r1\n"
  102. "push r2\n"
  103. "load32 0xC0272C r2 ; r2 = 0xC0272C\n"
  104. "load 1 r1 ; r1 = 1 (disable)\n"
  105. "write 0 r2 r1 ; write to SPI1_CS\n"
  106. "; restore regs\n"
  107. "pop r2\n"
  108. "pop r1\n"
  109. );
  110. }
  111. // write dataByte and return read value
  112. // write 0x00 for a read
  113. // Writes byte over SPI1 to CH376
  114. word FS_spiTransfer(word dataByte)
  115. {
  116. word retval = 0;
  117. asm(
  118. "load32 0xC0272B r2 ; r2 = 0xC0272B\n"
  119. "write 0 r2 r4 ; write r4 over SPI1\n"
  120. "read 0 r2 r2 ; read return value\n"
  121. "write -4 r14 r2 ; write to stack to return\n"
  122. );
  123. return retval;
  124. }
  125. // Get status after waiting for an interrupt
  126. word FS_WaitGetStatus()
  127. {
  128. word intValue = 1;
  129. while(intValue)
  130. {
  131. word *i = (word *) FS_INTERRUPT_ADDR;
  132. intValue = *i;
  133. }
  134. FS_spiBeginTransfer();
  135. FS_spiTransfer(FS_CMD_GET_STATUS);
  136. FS_spiEndTransfer();
  137. //delay(1);
  138. FS_spiBeginTransfer();
  139. word retval = FS_spiTransfer(0x00);
  140. FS_spiEndTransfer();
  141. return retval;
  142. }
  143. // Get status without using interrupts
  144. word FS_noWaitGetStatus()
  145. {
  146. FS_spiBeginTransfer();
  147. FS_spiTransfer(FS_CMD_GET_STATUS);
  148. word retval = FS_spiTransfer(0x00);
  149. FS_spiEndTransfer();
  150. return retval;
  151. }
  152. // Function to send a string (without terminating 0)
  153. void FS_sendString(char* str)
  154. {
  155. char chr = *str; // first character of str
  156. while (chr != 0) // continue until null value
  157. {
  158. FS_spiTransfer(chr);
  159. str++; // go to next character address
  160. chr = *str; // get character from address
  161. }
  162. }
  163. // Function to send data d of size s
  164. void FS_sendData(char* d, word s)
  165. {
  166. char chr = *d; // first byte of data
  167. word i;
  168. for(i = 0; (unsigned int) i < (unsigned int)s; i++) // write s bytes
  169. {
  170. FS_spiTransfer(chr);
  171. d++; // go to next data address
  172. chr = *d; // get data from address
  173. }
  174. }
  175. // Returns IC version of CH376 chip
  176. // Good test to know if the communication with chip works
  177. word FS_getICver()
  178. {
  179. FS_spiBeginTransfer();
  180. FS_spiTransfer(FS_CMD_GET_IC_VER);
  181. delay(1);
  182. word icVer = FS_spiTransfer(0x00);
  183. FS_spiEndTransfer();
  184. return icVer;
  185. }
  186. // Sets USB mode to mode, returns status code
  187. // Which should be FS_ANSW_RET_SUCCESS when successful
  188. word FS_setUSBmode(word mode)
  189. {
  190. FS_spiBeginTransfer();
  191. FS_spiTransfer(FS_CMD_SET_USB_MODE);
  192. FS_spiEndTransfer();
  193. delay(1);
  194. FS_spiBeginTransfer();
  195. FS_spiTransfer(mode);
  196. FS_spiEndTransfer();
  197. delay(1);
  198. FS_spiBeginTransfer();
  199. word status = FS_spiTransfer(0x00);
  200. FS_spiEndTransfer();
  201. delay(1);
  202. return status;
  203. }
  204. // resets and intitializes CH376
  205. // returns FS_ANSW_RET_SUCCESS on success
  206. word FS_init()
  207. {
  208. FS_spiEndTransfer(); // start with cs high
  209. delay(10);
  210. // Reset
  211. FS_spiBeginTransfer();
  212. FS_spiTransfer(FS_CMD_RESET_ALL);
  213. FS_spiEndTransfer();
  214. delay(100); // wait after reset
  215. // USB mode 0
  216. return FS_setUSBmode(FS_MODE_HOST_0);
  217. }
  218. // waits for drive connection,
  219. // sets usb host mode
  220. // waits for drive to be ready
  221. // mounts drive
  222. // also initializes current path to /
  223. // returns FS_ANSW_USB_INT_SUCCESS on success
  224. word FS_connectDrive()
  225. {
  226. // Wait forever until an USB device is connected
  227. while(FS_WaitGetStatus() != FS_ANSW_USB_INT_CONNECT);
  228. // USB mode 1
  229. word retval = FS_setUSBmode(FS_MODE_HOST_1);
  230. // Return on error
  231. if (retval != FS_ANSW_RET_SUCCESS)
  232. return retval;
  233. // USB mode 2
  234. retval = FS_setUSBmode(FS_MODE_HOST_2);
  235. // Return on error
  236. if (retval != FS_ANSW_RET_SUCCESS)
  237. return retval;
  238. // Need to check again for device connection after changing USB mode
  239. while(FS_WaitGetStatus() != FS_ANSW_USB_INT_CONNECT);
  240. // Connect to drive
  241. FS_spiBeginTransfer();
  242. FS_spiTransfer(FS_CMD_DISK_CONNECT);
  243. FS_spiEndTransfer();
  244. retval = FS_WaitGetStatus();
  245. // Return on error
  246. if (retval != FS_ANSW_USB_INT_SUCCESS)
  247. return retval;
  248. FS_spiBeginTransfer();
  249. FS_spiTransfer(FS_CMD_DISK_MOUNT);
  250. FS_spiEndTransfer();
  251. return FS_WaitGetStatus();
  252. }
  253. // Returns file size of currently opened file (32 bits)
  254. word FS_getFileSize()
  255. {
  256. FS_spiBeginTransfer();
  257. FS_spiTransfer(FS_CMD_GET_FILE_SIZE);
  258. FS_spiTransfer(0x68);
  259. word retval = FS_spiTransfer(0);
  260. retval = retval + (FS_spiTransfer(0) << 8);
  261. retval = retval + (FS_spiTransfer(0) << 16);
  262. retval = retval + (FS_spiTransfer(0) << 24);
  263. FS_spiEndTransfer();
  264. return retval;
  265. }
  266. // Sets cursor to position s
  267. // Returns FS_ANSW_USB_INT_SUCCESS on success
  268. word FS_setCursor(word s)
  269. {
  270. FS_spiBeginTransfer();
  271. FS_spiTransfer(FS_CMD_BYTE_LOCATE);
  272. FS_spiTransfer(s);
  273. FS_spiTransfer((unsigned) s >> 8);
  274. FS_spiTransfer((unsigned) s >> 16);
  275. FS_spiTransfer((unsigned) s >> 24);
  276. FS_spiEndTransfer();
  277. return FS_WaitGetStatus();
  278. }
  279. // Reads s bytes into buf
  280. // If bytesToWord is true, four bytes will be stored in one address/word
  281. // Can read 65536 bytes per call
  282. // Returns FS_ANSW_USB_INT_SUCCESS on success
  283. // TODO: this surely can be optimized for speed in some way!
  284. word FS_readFile(char* buf, word s, word bytesToWord)
  285. {
  286. if (s == 0)
  287. return FS_ANSW_USB_INT_SUCCESS;
  288. FS_spiBeginTransfer();
  289. FS_spiTransfer(FS_CMD_BYTE_READ);
  290. FS_spiTransfer(s);
  291. FS_spiTransfer((unsigned) s >> 8);
  292. FS_spiEndTransfer();
  293. word retval = FS_WaitGetStatus();
  294. // Return on error or EOF
  295. if (retval == FS_ANSW_USB_INT_SUCCESS) // eof
  296. return 0;
  297. if (retval != FS_ANSW_USB_INT_DISK_READ)
  298. return retval;
  299. word bytesRead = 0;
  300. word wordsRead = 0;
  301. word doneReading = 0;
  302. // clear first address
  303. buf[wordsRead] = 0;
  304. // used for shifting bytes to word
  305. word currentByteShift = 24;
  306. while (doneReading == 0)
  307. {
  308. // Read set of bytes (max 255)
  309. FS_spiBeginTransfer();
  310. FS_spiTransfer(FS_CMD_RD_USB_DATA0);
  311. word readLen = FS_spiTransfer(0x00);
  312. word readByte;
  313. word i;
  314. for (i = 0; i < readLen; i++)
  315. {
  316. readByte = FS_spiTransfer(0x00);
  317. // Read 4 bytes into one word, from left to right
  318. if (bytesToWord)
  319. {
  320. readByte = readByte << currentByteShift;
  321. buf[wordsRead] = buf[wordsRead] + readByte;
  322. if (currentByteShift == 0)
  323. {
  324. currentByteShift = 24;
  325. wordsRead++;
  326. buf[wordsRead] = 0;
  327. }
  328. else
  329. {
  330. currentByteShift -= 8;
  331. }
  332. }
  333. else
  334. {
  335. buf[bytesRead] = (char)readByte;
  336. bytesRead = bytesRead + 1;
  337. }
  338. }
  339. FS_spiEndTransfer();
  340. // requesting another set of data
  341. FS_spiBeginTransfer();
  342. FS_spiTransfer(FS_CMD_BYTE_RD_GO);
  343. FS_spiEndTransfer();
  344. retval = FS_WaitGetStatus();
  345. if (retval == FS_ANSW_USB_INT_SUCCESS)
  346. doneReading = 1;
  347. else if (retval == FS_ANSW_USB_INT_DISK_READ)
  348. {
  349. // read another block
  350. }
  351. else
  352. {
  353. uprintln("E: Error while reading data");
  354. return retval;
  355. }
  356. }
  357. return retval;
  358. }
  359. // Writes data d of size s
  360. // Can only write 65536 bytes at a time
  361. // returns FS_ANSW_USB_INT_SUCCESS on success
  362. // TODO: optimize for speed
  363. word FS_writeFile(char* d, word s)
  364. {
  365. if (s == 0)
  366. return FS_ANSW_USB_INT_SUCCESS;
  367. FS_spiBeginTransfer();
  368. FS_spiTransfer(FS_CMD_BYTE_WRITE);
  369. FS_spiTransfer(s);
  370. FS_spiTransfer((unsigned) s >> 8);
  371. FS_spiEndTransfer();
  372. word retval = FS_WaitGetStatus();
  373. // Return on error
  374. if (retval != FS_ANSW_USB_INT_DISK_WRITE)
  375. return retval;
  376. word bytesWritten = 0;
  377. word doneWriting = 0;
  378. while (doneWriting == 0)
  379. {
  380. // Write set of bytes (max 255)
  381. FS_spiBeginTransfer();
  382. FS_spiTransfer(FS_CMD_WR_REQ_DATA);
  383. word wrLen = FS_spiTransfer(0x00);
  384. FS_sendData(d + bytesWritten, wrLen);
  385. bytesWritten = bytesWritten + wrLen;
  386. FS_spiEndTransfer();
  387. // update file size
  388. FS_spiBeginTransfer();
  389. FS_spiTransfer(FS_CMD_BYTE_WR_GO);
  390. FS_spiEndTransfer();
  391. retval = FS_WaitGetStatus();
  392. if (retval == FS_ANSW_USB_INT_SUCCESS)
  393. doneWriting = 1;
  394. else if (retval == FS_ANSW_USB_INT_DISK_WRITE)
  395. {
  396. // write another block
  397. }
  398. else
  399. {
  400. uprintln("E: Error while writing data");
  401. return retval;
  402. }
  403. }
  404. return retval;
  405. }
  406. // returns status of opening a file/directory
  407. // Usually the successful status codes are:
  408. // FS_ANSW_USB_INT_SUCCESS || FS_ANSW_ERR_OPEN_DIR || FS_ANSW_USB_INT_DISK_READ
  409. word FS_open()
  410. {
  411. FS_spiBeginTransfer();
  412. FS_spiTransfer(FS_CMD_FILE_OPEN);
  413. FS_spiEndTransfer();
  414. return FS_WaitGetStatus();
  415. }
  416. // returns FS_ANSW_USB_INT_SUCCESS on success
  417. word FS_delete()
  418. {
  419. FS_spiBeginTransfer();
  420. FS_spiTransfer(FS_CMD_FILE_ERASE);
  421. FS_spiEndTransfer();
  422. return FS_WaitGetStatus();
  423. }
  424. // returns FS_ANSW_USB_INT_SUCCESS on success
  425. word FS_close()
  426. {
  427. FS_spiBeginTransfer();
  428. FS_spiTransfer(FS_CMD_FILE_CLOSE);
  429. FS_spiTransfer(0x01); //0x01 if update filesize, else 0x00
  430. FS_spiEndTransfer();
  431. return FS_WaitGetStatus();
  432. }
  433. // returns FS_ANSW_USB_INT_SUCCESS on success
  434. word FS_createDir()
  435. {
  436. FS_spiBeginTransfer();
  437. FS_spiTransfer(FS_CMD_DIR_CREATE);
  438. FS_spiEndTransfer();
  439. return FS_WaitGetStatus();
  440. }
  441. // Creates file (recreates if exists)
  442. // New files are 1 byte long
  443. // Have not found a way to set it to 0 bytes,
  444. // since FS_CMD_SET_FILE_SIZE does not work
  445. // Automatically closes file
  446. // returns FS_ANSW_USB_INT_SUCCESS on success
  447. word FS_createFile()
  448. {
  449. FS_spiBeginTransfer();
  450. FS_spiTransfer(FS_CMD_FILE_CREATE);
  451. FS_spiEndTransfer();
  452. word retval = FS_WaitGetStatus();
  453. // Return on error
  454. if (retval != FS_ANSW_USB_INT_SUCCESS)
  455. return retval;
  456. // open and close file
  457. FS_open();
  458. FS_close();
  459. return FS_ANSW_USB_INT_SUCCESS;
  460. }
  461. // Sends single path f
  462. // No processing of f is done, it is directly sent
  463. // This means no error checking on file length!
  464. void FS_sendSinglePath(char* f)
  465. {
  466. // send buf
  467. FS_spiBeginTransfer();
  468. FS_spiTransfer(FS_CMD_SET_FILE_NAME);
  469. FS_sendString(f); // send file name
  470. FS_spiTransfer(0); // close with null
  471. FS_spiEndTransfer();
  472. }
  473. // Sends path f
  474. // Can be relative or absolute file or directory
  475. // REQUIRES CAPITAL LETTERS and must conform the 8.3 filename standard
  476. // Function can handle folders with forward slashes
  477. // returns FS_ANSW_USB_INT_SUCCESS on success
  478. word FS_sendFullPath(char* f)
  479. {
  480. // path to uppercase and replace backslash by slash
  481. word i = 0;
  482. while (f[i] != 0)
  483. {
  484. f[i] = toUpper(f[i]);
  485. if (f[i] == '\\')
  486. f[i] = '/';
  487. i++;
  488. }
  489. word removedSlash = 0; // to restore removed slash
  490. // remove (single) trailing slash if exists
  491. // we reuse i here since it points to the end of the path
  492. if (i > 1) // ignore the root path
  493. {
  494. if (f[i-1] == '/')
  495. {
  496. f[i-1] = 0;
  497. removedSlash = 1;
  498. }
  499. }
  500. // if first char is a /, then we go back to root
  501. if (f[0] == '/')
  502. {
  503. FS_sendSinglePath("/");
  504. FS_open();
  505. }
  506. i = 0;
  507. char buf[16]; // buffer for single folder/file name
  508. word bufi = 0;
  509. while (f[i] != 0)
  510. {
  511. // handle all directories
  512. if (f[i] == 47) // forward slash
  513. {
  514. // return error on opening folders containing a single or double dot
  515. if ((bufi == 1 && buf[0] == '.') || (bufi == 2 && buf[0] == '.' && buf[1] == '.'))
  516. return FS_ANSW_ERR_OPEN_DIR;
  517. // add null to end of buf
  518. buf[bufi] = 0;
  519. // send buf
  520. FS_spiBeginTransfer();
  521. FS_spiTransfer(FS_CMD_SET_FILE_NAME);
  522. FS_sendString(buf); // send folder name
  523. FS_spiTransfer(0); // close with null
  524. FS_spiEndTransfer();
  525. // reset bufi
  526. bufi = 0;
  527. // open folder
  528. word retval = FS_open();
  529. // Return on if failure / folder not found
  530. if (retval != FS_ANSW_USB_INT_SUCCESS && retval != FS_ANSW_ERR_OPEN_DIR)
  531. return retval;
  532. }
  533. else
  534. {
  535. buf[bufi] = f[i];
  536. bufi++;
  537. if (bufi > 13)
  538. return FS_ERR_LONGFILENAME; // exit if folder/file name is too long
  539. }
  540. i++;
  541. }
  542. if (removedSlash) // restore removed slash if there
  543. {
  544. f[i] = '/';
  545. f[i+1] = 0;
  546. }
  547. // handle filename itself (if any)
  548. // add null to end of buf
  549. buf[bufi] = 0;
  550. if (bufi == 0)
  551. return FS_ANSW_USB_INT_SUCCESS; // exit if there is no filename after the folder
  552. // return error on opening folders containing a single or double dot
  553. if ((bufi == 1 && buf[0] == '.') || (bufi == 2 && buf[0] == '.' && buf[1] == '.'))
  554. return FS_ANSW_ERR_OPEN_DIR;
  555. // send buf (last part of string)
  556. FS_spiBeginTransfer();
  557. FS_spiTransfer(FS_CMD_SET_FILE_NAME);
  558. FS_sendString(buf); // send file name
  559. FS_spiTransfer(0); // close with null
  560. FS_spiEndTransfer();
  561. return FS_ANSW_USB_INT_SUCCESS;
  562. }
  563. // Parses and prints a string (useful for name and extension) after removing trailing spaces
  564. // Len should be <= 8 chars
  565. // Does not add a new line at the end.
  566. // Returns length of written string
  567. word FS_parseFATstring(char* fatBuffer, word len, char* b, word* bufLen)
  568. {
  569. //uprintlnDec(bufLen);
  570. //uprintlnDec(*bufLen);
  571. if (len > 8)
  572. {
  573. uprintln("FATstring: Len argument > 8");
  574. return 0;
  575. }
  576. word retval = 0;
  577. // buffer of parsed string
  578. char nameBuf[9];
  579. nameBuf[len] = 0;
  580. // loop backwards until a non-space character is found
  581. // then, write string to nameBuf from there, keeping spaces in filename if any
  582. word foundChar = 0;
  583. word i;
  584. for (i = 0; i < len; i++)
  585. {
  586. if (!foundChar)
  587. {
  588. if (fatBuffer[len-1-i] == ' ')
  589. nameBuf[len-1-i] = 0; // set null until a non-space char is found
  590. else
  591. {
  592. foundChar = 1;
  593. retval = len-i;
  594. nameBuf[len-1-i] = fatBuffer[len-1-i]; // write the non-space char
  595. }
  596. }
  597. else
  598. nameBuf[len-1-i] = fatBuffer[len-1-i]; // copy char
  599. }
  600. // write to buffer
  601. i = 0;
  602. while (nameBuf[i] != 0)
  603. {
  604. b[*bufLen] = nameBuf[i];
  605. (*bufLen)++;
  606. i++;
  607. }
  608. return retval;
  609. }
  610. // Parses and writes name.extension and filesize on one line
  611. void FS_parseFATdata(word datalen, char* fatBuffer, char* b, word* bufLen)
  612. {
  613. if (datalen != 32)
  614. {
  615. uprintln("Unexpected FAT table length");
  616. return;
  617. }
  618. /* // Ignore lines with '.' and ".." as filename
  619. // ignore '.'
  620. if (memcmp(fatBuffer, ". ", 11))
  621. return;
  622. // ignore ".."
  623. if (memcmp(fatBuffer, ".. ", 11))
  624. return;
  625. */
  626. // parse filename
  627. word printLen = FS_parseFATstring(fatBuffer, 8, b, bufLen);
  628. // add '.' and parse extension
  629. if (fatBuffer[8] != ' ' || fatBuffer[9] != ' ' || fatBuffer[10] != ' ')
  630. {
  631. b[*bufLen] = '.';
  632. (*bufLen)++;
  633. printLen += FS_parseFATstring(fatBuffer+8, 3, b, bufLen) + 1;
  634. }
  635. // append with spaces until 16th char
  636. while (printLen < 16)
  637. {
  638. b[*bufLen] = ' ';
  639. (*bufLen)++;
  640. printLen++;
  641. }
  642. // filesize
  643. word fileSize = 0;
  644. fileSize += fatBuffer[28];
  645. fileSize += (fatBuffer[29] << 8);
  646. fileSize += (fatBuffer[30] << 16);
  647. fileSize += (fatBuffer[31] << 24);
  648. // filesize to integer string
  649. char buffer[10];
  650. itoa(fileSize, buffer);
  651. // write to buffer
  652. word i = 0;
  653. while (buffer[i] != 0)
  654. {
  655. b[*bufLen] = buffer[i];
  656. (*bufLen)++;
  657. i++;
  658. }
  659. b[*bufLen] = '\n';
  660. (*bufLen)++;
  661. //uprintlnDec(*bufLen);
  662. }
  663. // Reads FAT data for single entry
  664. // FAT data is parsed by FS_parseFatData()
  665. void FS_readFATdata(char* b, word* bufLen)
  666. {
  667. FS_spiBeginTransfer();
  668. FS_spiTransfer(FS_CMD_RD_USB_DATA0);
  669. word datalen = FS_spiTransfer(0x0);
  670. char fatbuf[32];
  671. word i;
  672. for (i = 0; i < datalen; i++)
  673. {
  674. fatbuf[i] = FS_spiTransfer(0x00);
  675. }
  676. FS_parseFATdata(datalen, fatbuf, b, bufLen);
  677. FS_spiEndTransfer();
  678. }
  679. // Lists directory of full path f
  680. // f needs to start with / and not end with /
  681. // Returns FS_ANSW_USB_INT_SUCCESS if successful
  682. // Writes parsed result to address b
  683. // Result is terminated with a \0
  684. word FS_listDir(char* f, char* b)
  685. {
  686. word bufLen = 0;
  687. word retval = FS_sendFullPath(f);
  688. // Return on failure
  689. if (retval != FS_ANSW_USB_INT_SUCCESS)
  690. return retval;
  691. retval = FS_open();
  692. // Return on failure
  693. if (retval != FS_ANSW_USB_INT_SUCCESS && retval != FS_ANSW_ERR_OPEN_DIR)
  694. return retval;
  695. FS_sendSinglePath("*");
  696. retval = FS_open();
  697. // Return on failure
  698. if (retval != FS_ANSW_USB_INT_DISK_READ)
  699. return retval;
  700. // Init length of output buffer
  701. bufLen = 0;
  702. while (retval == FS_ANSW_USB_INT_DISK_READ)
  703. {
  704. FS_readFATdata(b, &bufLen);
  705. FS_spiBeginTransfer();
  706. FS_spiTransfer(FS_CMD_FILE_ENUM_GO);
  707. FS_spiEndTransfer();
  708. retval = FS_WaitGetStatus();
  709. }
  710. // Terminate buffer
  711. b[bufLen] = 0;
  712. bufLen++;
  713. return FS_ANSW_USB_INT_SUCCESS;
  714. }
  715. // Returns FS_ANSW_USB_INT_SUCCESS on successful change of dir
  716. // Will return error FS_ANSW_ERR_FILE_CLOSE if dir is a file
  717. word FS_changeDir(char* f)
  718. {
  719. // Special case for root, since FS_open() returns as if it opened a file
  720. if (f[0] == '/' && f[1] == 0)
  721. {
  722. FS_sendSinglePath("/");
  723. FS_open();
  724. return FS_ANSW_USB_INT_SUCCESS;
  725. }
  726. word retval = FS_sendFullPath(f);
  727. // Return on failure
  728. if (retval != FS_ANSW_USB_INT_SUCCESS)
  729. return retval;
  730. retval = FS_open();
  731. // Return sucess on open dir
  732. if (retval == FS_ANSW_ERR_OPEN_DIR)
  733. return FS_ANSW_USB_INT_SUCCESS;
  734. // Close and return on opening file
  735. if (retval == FS_ANSW_USB_INT_SUCCESS)
  736. {
  737. FS_close();
  738. return FS_ANSW_ERR_FILE_CLOSE;
  739. }
  740. else // otherwise return error code
  741. return retval;
  742. }