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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 "cache.h"
  25. #include "log.h"
  26. #include "debug_disk.h"
  27. /*
  28. * FatFS ffsystem.c
  29. */
  30. void* ff_memalloc(UINT msize) {
  31. return malloc((size_t)msize);
  32. }
  33. void ff_memfree(void* mblock) {
  34. free(mblock);
  35. }
  36. /*
  37. * FatFS diskio.c
  38. */
  39. DSTATUS disk_status(BYTE pdrv) {
  40. if (pdrv != 0) {
  41. debug("invalid drive number %d", pdrv);
  42. return STA_NODISK;
  43. }
  44. return 0;
  45. }
  46. DSTATUS disk_initialize(BYTE pdrv) {
  47. if (pdrv != 0) {
  48. debug("invalid drive number %d", pdrv);
  49. return STA_NODISK;
  50. }
  51. return 0;
  52. }
  53. DRESULT disk_read(BYTE pdrv, BYTE *buff, LBA_t sector, UINT count) {
  54. if (pdrv != 0) {
  55. debug("invalid drive number %d", pdrv);
  56. return RES_PARERR;
  57. }
  58. if ((sector + count) > DISK_BLOCK_COUNT) {
  59. debug("invalid read ((%lu + %u) > %u)", sector, count, DISK_BLOCK_COUNT);
  60. return RES_ERROR;
  61. }
  62. ssize_t r = cache_read(buff, sector * DISK_BLOCK_SIZE, count * DISK_BLOCK_SIZE);
  63. return (r == (ssize_t)(count * DISK_BLOCK_SIZE)) ? RES_OK : RES_ERROR;
  64. }
  65. DRESULT disk_write(BYTE pdrv, const BYTE *buff, LBA_t sector, UINT count) {
  66. if (pdrv != 0) {
  67. debug("invalid drive number %d", pdrv);
  68. return RES_PARERR;
  69. }
  70. if ((sector + count) > DISK_BLOCK_COUNT) {
  71. debug("invalid read ((%lu + %u) > %u)", sector, count, DISK_BLOCK_COUNT);
  72. return RES_ERROR;
  73. }
  74. ssize_t r = cache_write(buff, sector * DISK_BLOCK_SIZE, count * DISK_BLOCK_SIZE);
  75. return (r == (ssize_t)(count * DISK_BLOCK_SIZE)) ? RES_OK : RES_ERROR;
  76. }
  77. DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void *buff) {
  78. if (pdrv != 0) {
  79. debug("invalid drive number %d", pdrv);
  80. return RES_PARERR;
  81. }
  82. switch (cmd) {
  83. case GET_SECTOR_COUNT:
  84. *((LBA_t *)buff) = DISK_BLOCK_COUNT;
  85. break;
  86. case GET_SECTOR_SIZE:
  87. *((WORD *)buff) = DISK_BLOCK_SIZE;
  88. break;
  89. case GET_BLOCK_SIZE:
  90. *((DWORD *)buff) = 1; // non flash memory media
  91. break;
  92. }
  93. return RES_OK;
  94. }