fileops.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* sd2snes - SD card based universal cartridge for the SNES
  2. Copyright (C) 2009-2010 Maximilian Rehkopf <otakon@gmx.net>
  3. AVR firmware portion
  4. Inspired by and based on code from sd2iec, written by Ingo Korb et al.
  5. See sdcard.c|h, config.h.
  6. FAT file system access based on code by ChaN, Jim Brain, Ingo Korb,
  7. see ff.c|h.
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; version 2 of the License only.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. fileops.c: simple file access functions
  19. */
  20. #include "config.h"
  21. #include "uart.h"
  22. #include "ff.h"
  23. #include "fileops.h"
  24. /*WCHAR ff_convert(WCHAR w, UINT dir) {
  25. return w;
  26. }*/
  27. void file_init() {
  28. file_res=f_mount(0, &fatfs);
  29. }
  30. /*void file_open_by_filinfo(FILINFO* fno) {
  31. file_res = l_openfilebycluster(&fatfs, &file_handle, (UCHAR*)"", fno->clust, fno->fsize);
  32. }*/
  33. void file_open(uint8_t* filename, BYTE flags) {
  34. file_res = f_open(&file_handle, (TCHAR*)filename, flags);
  35. }
  36. void file_close() {
  37. file_res = f_close(&file_handle);
  38. }
  39. UINT file_read() {
  40. UINT bytes_read;
  41. file_res = f_read(&file_handle, file_buf, sizeof(file_buf), &bytes_read);
  42. return bytes_read;
  43. }
  44. UINT file_write() {
  45. UINT bytes_written;
  46. file_res = f_write(&file_handle, file_buf, sizeof(file_buf), &bytes_written);
  47. return bytes_written;
  48. }
  49. UINT file_readblock(void* buf, uint32_t addr, uint16_t size) {
  50. UINT bytes_read;
  51. file_res = f_lseek(&file_handle, addr);
  52. if(file_handle.fptr != addr) {
  53. return 0;
  54. }
  55. file_res = f_read(&file_handle, buf, size, &bytes_read);
  56. return bytes_read;
  57. }
  58. UINT file_writeblock(void* buf, uint32_t addr, uint16_t size) {
  59. UINT bytes_written;
  60. file_res = f_lseek(&file_handle, addr);
  61. if(file_res) return 0;
  62. file_res = f_write(&file_handle, buf, size, &bytes_written);
  63. return bytes_written;
  64. }