S&B Volcano vaporizer remote control with Pi Pico W
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 4.2KB

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