No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fat_disk.c 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * fat_disk.c
  3. */
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include "pico/stdlib.h"
  7. #include "ff.h"
  8. #include "diskio.h"
  9. #include "config.h"
  10. #include "log.h"
  11. #include "fat_disk.h"
  12. static uint8_t disk[DISK_BLOCK_COUNT * DISK_BLOCK_SIZE];
  13. void fat_disk_init(void) {
  14. BYTE work[FF_MAX_SS];
  15. FRESULT res = f_mkfs("", 0, work, sizeof(work));
  16. if (res != FR_OK) {
  17. debug("error: f_mkfs returned %d", res);
  18. return;
  19. }
  20. }
  21. uint8_t *fat_disk_get_sector(uint32_t sector) {
  22. return disk + (sector * DISK_BLOCK_SIZE);
  23. }
  24. /*
  25. * FatFS ffsystem.c
  26. */
  27. void* ff_memalloc(UINT msize) {
  28. return malloc((size_t)msize);
  29. }
  30. void ff_memfree(void* mblock) {
  31. free(mblock);
  32. }
  33. /*
  34. * FatFS diskio.c
  35. */
  36. DSTATUS disk_status(BYTE pdrv) {
  37. if (pdrv != 0) {
  38. debug("invalid drive number %d", pdrv);
  39. return STA_NODISK;
  40. }
  41. return 0;
  42. }
  43. DSTATUS disk_initialize(BYTE pdrv) {
  44. if (pdrv != 0) {
  45. debug("invalid drive number %d", pdrv);
  46. return STA_NODISK;
  47. }
  48. return 0;
  49. }
  50. DRESULT disk_read(BYTE pdrv, BYTE *buff, LBA_t sector, UINT count) {
  51. if (pdrv != 0) {
  52. debug("invalid drive number %d", pdrv);
  53. return RES_PARERR;
  54. }
  55. if ((sector + count) > DISK_BLOCK_COUNT) {
  56. debug("invalid read ((%lu + %u) > %u)", sector, count, DISK_BLOCK_COUNT);
  57. return RES_ERROR;
  58. }
  59. memcpy(buff, disk + (sector * DISK_BLOCK_SIZE), count * DISK_BLOCK_SIZE);
  60. return RES_OK;
  61. }
  62. DRESULT disk_write(BYTE pdrv, const BYTE *buff, LBA_t sector, UINT count) {
  63. if (pdrv != 0) {
  64. debug("invalid drive number %d", pdrv);
  65. return RES_PARERR;
  66. }
  67. if ((sector + count) > DISK_BLOCK_COUNT) {
  68. debug("invalid read ((%lu + %u) > %u)", sector, count, DISK_BLOCK_COUNT);
  69. return RES_ERROR;
  70. }
  71. memcpy(disk + (sector * DISK_BLOCK_SIZE), buff, count * DISK_BLOCK_SIZE);
  72. return RES_OK;
  73. }
  74. DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void *buff) {
  75. if (pdrv != 0) {
  76. debug("invalid drive number %d", pdrv);
  77. return RES_PARERR;
  78. }
  79. switch (cmd) {
  80. case GET_SECTOR_COUNT:
  81. *((LBA_t *)buff) = DISK_BLOCK_COUNT;
  82. break;
  83. case GET_SECTOR_SIZE:
  84. *((WORD *)buff) = DISK_BLOCK_SIZE;
  85. break;
  86. case GET_BLOCK_SIZE:
  87. *((DWORD *)buff) = 1; // non flash memory media
  88. break;
  89. }
  90. return RES_OK;
  91. }