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 3.0KB

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