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 <util/delay.h>
  21. #include "config.h"
  22. #include "uart.h"
  23. #include "ff.h"
  24. #include "fileops.h"
  25. WCHAR ff_convert(WCHAR w, UINT dir) {
  26. return w;
  27. }
  28. void file_init() {
  29. f_mount(0, &fatfs);
  30. }
  31. void file_open_by_filinfo(FILINFO* fno) {
  32. file_res = l_openfilebycluster(&fatfs, &file_handle, (UCHAR*)"", fno->clust, fno->fsize);
  33. }
  34. void file_open(uint8_t* filename, BYTE flags) {
  35. file_res = f_open(&file_handle, (unsigned char*)filename, flags);
  36. }
  37. void file_close() {
  38. file_res = f_close(&file_handle);
  39. }
  40. UINT file_read() {
  41. UINT bytes_read;
  42. file_res = f_read(&file_handle, file_buf, sizeof(file_buf), &bytes_read);
  43. return bytes_read;
  44. }
  45. UINT file_write() {
  46. UINT bytes_written;
  47. file_res = f_write(&file_handle, file_buf, sizeof(file_buf), &bytes_written);
  48. return bytes_written;
  49. }
  50. UINT file_readblock(void* buf, uint32_t addr, uint16_t size) {
  51. UINT bytes_read;
  52. file_res = f_lseek(&file_handle, addr);
  53. if(file_handle.fptr != addr) {
  54. return 0;
  55. }
  56. file_res = f_read(&file_handle, buf, size, &bytes_read);
  57. return bytes_read;
  58. }
  59. UINT file_writeblock(void* buf, uint32_t addr, uint16_t size) {
  60. UINT bytes_written;
  61. file_res = f_lseek(&file_handle, addr);
  62. if(file_res) return 0;
  63. file_res = f_write(&file_handle, buf, size, &bytes_written);
  64. return bytes_written;
  65. }