Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * fat_disk.c
  3. */
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include "pico/stdlib.h"
  8. #include "ff.h"
  9. #include "diskio.h"
  10. #include "config.h"
  11. #include "log.h"
  12. #include "debug.h"
  13. #include "fat_disk.h"
  14. static uint8_t disk[DISK_BLOCK_COUNT * DISK_BLOCK_SIZE];
  15. void fat_disk_init(void) {
  16. BYTE work[FF_MAX_SS];
  17. FRESULT res = f_mkfs("", 0, work, sizeof(work));
  18. if (res != FR_OK) {
  19. debug("error: f_mkfs returned %d", res);
  20. return;
  21. }
  22. if (debug_msc_mount() != 0) {
  23. debug("error mounting disk");
  24. return;
  25. }
  26. // maximum length: 11 bytes
  27. f_setlabel("DEBUG DISK");
  28. FIL file;
  29. res = f_open(&file, "README.md", FA_CREATE_ALWAYS | FA_WRITE);
  30. if (res != FR_OK) {
  31. debug("error: f_open returned %d", res);
  32. } else {
  33. char readme[1024];
  34. size_t pos = 0;
  35. pos += snprintf(readme + pos, 1024 - pos, "# Trackball\r\n");
  36. pos += snprintf(readme + pos, 1024 - pos, "\r\n");
  37. pos += snprintf(readme + pos, 1024 - pos, "Project by Thomas Buck <thomas@xythobuz.de>\r\n");
  38. pos += snprintf(readme + pos, 1024 - pos, "Licensed under GPLv3.\r\n");
  39. pos += snprintf(readme + pos, 1024 - pos, "Source at https://git.xythobuz.de/thomas/Trackball\r\n");
  40. size_t len = strlen(readme);
  41. UINT bw;
  42. res = f_write(&file, readme, len, &bw);
  43. if ((res != FR_OK) || (bw != len)) {
  44. debug("error: f_write returned %d", res);
  45. }
  46. res = f_close(&file);
  47. if (res != FR_OK) {
  48. debug("error: f_close returned %d", res);
  49. }
  50. }
  51. if (debug_msc_unmount() != 0) {
  52. debug("error unmounting disk");
  53. }
  54. }
  55. uint8_t *fat_disk_get_sector(uint32_t sector) {
  56. return disk + (sector * DISK_BLOCK_SIZE);
  57. }
  58. /*
  59. * FatFS ffsystem.c
  60. */
  61. void* ff_memalloc(UINT msize) {
  62. return malloc((size_t)msize);
  63. }
  64. void ff_memfree(void* mblock) {
  65. free(mblock);
  66. }
  67. /*
  68. * FatFS diskio.c
  69. */
  70. DSTATUS disk_status(BYTE pdrv) {
  71. if (pdrv != 0) {
  72. debug("invalid drive number %d", pdrv);
  73. return STA_NODISK;
  74. }
  75. return 0;
  76. }
  77. DSTATUS disk_initialize(BYTE pdrv) {
  78. if (pdrv != 0) {
  79. debug("invalid drive number %d", pdrv);
  80. return STA_NODISK;
  81. }
  82. return 0;
  83. }
  84. DRESULT disk_read(BYTE pdrv, BYTE *buff, LBA_t sector, UINT count) {
  85. if (pdrv != 0) {
  86. debug("invalid drive number %d", pdrv);
  87. return RES_PARERR;
  88. }
  89. if ((sector + count) > DISK_BLOCK_COUNT) {
  90. debug("invalid read ((%lu + %u) > %u)", sector, count, DISK_BLOCK_COUNT);
  91. return RES_ERROR;
  92. }
  93. memcpy(buff, disk + (sector * DISK_BLOCK_SIZE), count * DISK_BLOCK_SIZE);
  94. return RES_OK;
  95. }
  96. DRESULT disk_write(BYTE pdrv, const BYTE *buff, LBA_t sector, UINT count) {
  97. if (pdrv != 0) {
  98. debug("invalid drive number %d", pdrv);
  99. return RES_PARERR;
  100. }
  101. if ((sector + count) > DISK_BLOCK_COUNT) {
  102. debug("invalid read ((%lu + %u) > %u)", sector, count, DISK_BLOCK_COUNT);
  103. return RES_ERROR;
  104. }
  105. memcpy(disk + (sector * DISK_BLOCK_SIZE), buff, count * DISK_BLOCK_SIZE);
  106. return RES_OK;
  107. }
  108. DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void *buff) {
  109. if (pdrv != 0) {
  110. debug("invalid drive number %d", pdrv);
  111. return RES_PARERR;
  112. }
  113. switch (cmd) {
  114. case GET_SECTOR_COUNT:
  115. *((LBA_t *)buff) = DISK_BLOCK_COUNT;
  116. break;
  117. case GET_SECTOR_SIZE:
  118. *((WORD *)buff) = DISK_BLOCK_SIZE;
  119. break;
  120. case GET_BLOCK_SIZE:
  121. *((DWORD *)buff) = 1; // non flash memory media
  122. break;
  123. }
  124. return RES_OK;
  125. }