fileops.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "config.h"
  2. #include "uart.h"
  3. #include "ff.h"
  4. #include "fileops.h"
  5. #include "diskio.h"
  6. /*
  7. WCHAR ff_convert(WCHAR w, UINT dir) {
  8. return w;
  9. }*/
  10. int newcard;
  11. void file_init()
  12. {
  13. file_res = f_mount( 0, &fatfs );
  14. newcard = 0;
  15. }
  16. void file_reinit( void )
  17. {
  18. disk_init();
  19. file_init();
  20. }
  21. void file_open_by_filinfo( FILINFO *fno )
  22. {
  23. file_res = l_openfilebycluster( &fatfs, &file_handle, ( TCHAR * )"", fno->clust, fno->fsize );
  24. }
  25. void file_open( uint8_t *filename, BYTE flags )
  26. {
  27. if ( disk_state == DISK_CHANGED )
  28. {
  29. file_reinit();
  30. newcard = 1;
  31. }
  32. file_res = f_open( &file_handle, ( TCHAR * )filename, flags );
  33. file_block_off = sizeof( file_buf );
  34. file_block_max = sizeof( file_buf );
  35. file_status = file_res ? FILE_ERR : FILE_OK;
  36. }
  37. void file_close()
  38. {
  39. file_res = f_close( &file_handle );
  40. }
  41. UINT file_read()
  42. {
  43. UINT bytes_read;
  44. file_res = f_read( &file_handle, file_buf, sizeof( file_buf ), &bytes_read );
  45. return bytes_read;
  46. }
  47. /*UINT file_write() {
  48. UINT bytes_written;
  49. file_res = f_write(&file_handle, file_buf, sizeof(file_buf), &bytes_written);
  50. if(bytes_written < sizeof(file_buf)) {
  51. printf("wrote less than expected - card full?\n");
  52. }
  53. return bytes_written;
  54. }*/
  55. UINT file_readblock( void *buf, uint32_t addr, uint16_t size )
  56. {
  57. UINT bytes_read;
  58. file_res = f_lseek( &file_handle, addr );
  59. if ( file_handle.fptr != addr )
  60. {
  61. return 0;
  62. }
  63. file_res = f_read( &file_handle, buf, size, &bytes_read );
  64. return bytes_read;
  65. }
  66. /*UINT file_writeblock(void* buf, uint32_t addr, uint16_t size) {
  67. UINT bytes_written;
  68. file_res = f_lseek(&file_handle, addr);
  69. if(file_res) return 0;
  70. file_res = f_write(&file_handle, buf, size, &bytes_written);
  71. return bytes_written;
  72. }*/
  73. uint8_t file_getc()
  74. {
  75. if ( file_block_off == file_block_max )
  76. {
  77. file_block_max = file_read();
  78. if ( file_block_max == 0 )
  79. {
  80. file_status = FILE_EOF;
  81. }
  82. file_block_off = 0;
  83. }
  84. return file_buf[file_block_off++];
  85. }