fileops.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // insert cool lengthy disclaimer here
  2. // fileops.c: convenience
  3. #include <util/delay.h>
  4. #include "config.h"
  5. #include "uart.h"
  6. #include "ff.h"
  7. #include "fileops.h"
  8. WCHAR ff_convert(WCHAR w, UINT dir) {
  9. return w;
  10. }
  11. void file_init() {
  12. f_mount(0, &fatfs);
  13. }
  14. void file_open_by_filinfo(FILINFO* fno) {
  15. file_res = l_openfilebycluster(&fatfs, &file_handle, (UCHAR*)"", fno->clust, fno->fsize);
  16. }
  17. void file_open(uint8_t* filename, BYTE flags) {
  18. file_res = f_open(&file_handle, (unsigned char*)filename, flags);
  19. }
  20. void file_close() {
  21. file_res = f_close(&file_handle);
  22. }
  23. UINT file_read() {
  24. UINT bytes_read;
  25. file_res = f_read(&file_handle, file_buf, sizeof(file_buf), &bytes_read);
  26. return bytes_read;
  27. }
  28. UINT file_write() {
  29. UINT bytes_written;
  30. file_res = f_write(&file_handle, file_buf, sizeof(file_buf), &bytes_written);
  31. return bytes_written;
  32. }
  33. UINT file_readblock(void* buf, uint32_t addr, uint16_t size) {
  34. UINT bytes_read;
  35. file_res = f_lseek(&file_handle, addr);
  36. if(file_handle.fptr != addr) {
  37. return 0;
  38. }
  39. if(file_res) { dprintf("no lseek %d\n", file_res); _delay_ms(30); return 0;}
  40. file_res = f_read(&file_handle, buf, size, &bytes_read);
  41. if(file_res) { dprintf("no read %d\n", file_res); _delay_ms(30); }
  42. return bytes_read;
  43. }
  44. UINT file_writeblock(void* buf, uint32_t addr, uint16_t size) {
  45. UINT bytes_written;
  46. file_res = f_lseek(&file_handle, addr);
  47. if(file_res) return 0;
  48. file_res = f_write(&file_handle, buf, size, &bytes_written);
  49. return bytes_written;
  50. }