fileops.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #include "diskio.h"
  25. /*
  26. WCHAR ff_convert(WCHAR w, UINT dir) {
  27. return w;
  28. }*/
  29. int newcard;
  30. void file_init() {
  31. file_res=f_mount(0, &fatfs);
  32. newcard = 0;
  33. }
  34. void file_reinit(void) {
  35. disk_init();
  36. file_init();
  37. }
  38. void file_open_by_filinfo(FILINFO* fno) {
  39. file_res = l_openfilebycluster(&fatfs, &file_handle, (TCHAR*)"", fno->clust, fno->fsize);
  40. }
  41. void file_open(uint8_t* filename, BYTE flags) {
  42. if (disk_state == DISK_CHANGED) {
  43. file_reinit();
  44. newcard = 1;
  45. }
  46. file_res = f_open(&file_handle, (TCHAR*)filename, flags);
  47. file_block_off = sizeof(file_buf);
  48. file_block_max = sizeof(file_buf);
  49. file_status = file_res ? FILE_ERR : FILE_OK;
  50. }
  51. void file_close() {
  52. file_res = f_close(&file_handle);
  53. }
  54. void file_seek(uint32_t offset) {
  55. file_res = f_lseek(&file_handle, (DWORD)offset);
  56. }
  57. UINT file_read() {
  58. UINT bytes_read;
  59. file_res = f_read(&file_handle, file_buf, sizeof(file_buf), &bytes_read);
  60. return bytes_read;
  61. }
  62. UINT file_write() {
  63. UINT bytes_written;
  64. file_res = f_write(&file_handle, file_buf, sizeof(file_buf), &bytes_written);
  65. if(bytes_written < sizeof(file_buf)) {
  66. printf("wrote less than expected - card full?\n");
  67. }
  68. return bytes_written;
  69. }
  70. UINT file_readblock(void* buf, uint32_t addr, uint16_t size) {
  71. UINT bytes_read;
  72. file_res = f_lseek(&file_handle, addr);
  73. if(file_handle.fptr != addr) {
  74. return 0;
  75. }
  76. file_res = f_read(&file_handle, buf, size, &bytes_read);
  77. return bytes_read;
  78. }
  79. UINT file_writeblock(void* buf, uint32_t addr, uint16_t size) {
  80. UINT bytes_written;
  81. file_res = f_lseek(&file_handle, addr);
  82. if(file_res) return 0;
  83. file_res = f_write(&file_handle, buf, size, &bytes_written);
  84. return bytes_written;
  85. }
  86. uint8_t file_getc() {
  87. if(file_block_off == file_block_max) {
  88. file_block_max = file_read();
  89. if(file_block_max == 0) file_status = FILE_EOF;
  90. file_block_off = 0;
  91. }
  92. return file_buf[file_block_off++];
  93. }