S&B Volcano vaporizer remote control with Pi Pico W
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Extended from TinyUSB example code.
  3. *
  4. * Copyright (c) 2022 - 2023 Thomas Buck (thomas@xythobuz.de)
  5. *
  6. * The MIT License (MIT)
  7. *
  8. * Copyright (c) 2019 Ha Thach (tinyusb.org)
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in
  18. * all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. * THE SOFTWARE.
  27. */
  28. #include "bsp/board.h"
  29. #include "tusb.h"
  30. #include "config.h"
  31. #include "fat_disk.h"
  32. #include "log.h"
  33. static bool medium_available = false;
  34. static bool medium_locked = false;
  35. bool msc_is_medium_available(void) {
  36. return medium_available;
  37. }
  38. bool msc_is_medium_locked(void) {
  39. return medium_locked;
  40. }
  41. void msc_set_medium_available(bool state) {
  42. medium_available = state;
  43. }
  44. // Invoked when received SCSI_CMD_INQUIRY
  45. // Application fill vendor id, product id and revision
  46. // with string up to 8, 16, 4 characters respectively
  47. void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8],
  48. uint8_t product_id[16], uint8_t product_rev[4]) {
  49. (void) lun;
  50. const char vid[] = "xythobuz";
  51. const char pid[] = "Debug Storage";
  52. const char rev[] = "0.01";
  53. memcpy(vendor_id , vid, strlen(vid));
  54. memcpy(product_id , pid, strlen(pid));
  55. memcpy(product_rev, rev, strlen(rev));
  56. }
  57. // Invoked when received Test Unit Ready command.
  58. // return true allowing host to read/write this LUN e.g SD card inserted
  59. bool tud_msc_test_unit_ready_cb(uint8_t lun) {
  60. if (!medium_available) {
  61. // Additional Sense 3A-00 is NOT_FOUND
  62. tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x3a, 0x00);
  63. }
  64. return medium_available;
  65. }
  66. // Invoked when received SCSI_CMD_READ_CAPACITY_10 and
  67. // SCSI_CMD_READ_FORMAT_CAPACITY to determine the disk size
  68. // Application update block count and block size
  69. void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_size) {
  70. (void) lun;
  71. if (!medium_available) {
  72. *block_count = 0;
  73. *block_size = 0;
  74. } else {
  75. *block_count = DISK_BLOCK_COUNT;
  76. *block_size = DISK_BLOCK_SIZE;
  77. }
  78. }
  79. // Invoked when received Start Stop Unit command
  80. // - Start = 0 : stopped power mode, if load_eject = 1 : unload disk storage
  81. // - Start = 1 : active mode, if load_eject = 1 : load disk storage
  82. bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition,
  83. bool start, bool load_eject) {
  84. (void) lun;
  85. (void) power_condition;
  86. if (start) {
  87. // load disk storage
  88. debug("load disk storage %d", load_eject);
  89. } else {
  90. // unload disk storage
  91. debug("unload disk storage %d", load_eject);
  92. if (load_eject) {
  93. debug("Host ejected medium. Unplugging disk.");
  94. medium_available = false;
  95. }
  96. }
  97. return true;
  98. }
  99. // Callback invoked when received READ10 command.
  100. // Copy disk's data to buffer (up to bufsize) and return number of copied bytes.
  101. int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba,
  102. uint32_t offset, void* buffer, uint32_t bufsize) {
  103. (void) lun;
  104. // out of ramdisk
  105. if (lba >= DISK_BLOCK_COUNT) {
  106. return -1;
  107. }
  108. // TODO better range checking and length calculation?
  109. uint8_t const* addr = fat_disk_get_sector(lba) + offset;
  110. memcpy(buffer, addr, bufsize);
  111. return (int32_t) bufsize;
  112. }
  113. bool tud_msc_is_writable_cb (uint8_t lun) {
  114. (void) lun;
  115. return true;
  116. }
  117. // Callback invoked when received WRITE10 command.
  118. // Process data in buffer to disk's storage and return number of written bytes
  119. int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba,
  120. uint32_t offset, uint8_t* buffer, uint32_t bufsize) {
  121. (void) lun;
  122. // out of ramdisk
  123. if (lba >= DISK_BLOCK_COUNT) {
  124. return -1;
  125. }
  126. // TODO better range checking and length calculation?
  127. uint8_t* addr = fat_disk_get_sector(lba) + offset;
  128. memcpy(addr, buffer, bufsize);
  129. return (int32_t) bufsize;
  130. }
  131. // Callback invoked when received an SCSI command not in built-in list below
  132. // - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, MODE_SENSE6, REQUEST_SENSE
  133. // - READ10 and WRITE10 has their own callbacks and MUST not be handled here
  134. int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16],
  135. void* buffer, uint16_t bufsize) {
  136. void const* response = NULL;
  137. int32_t resplen = 0;
  138. // most scsi handled is input
  139. bool in_xfer = true;
  140. switch (scsi_cmd[0]) {
  141. case 0x1E:
  142. // Prevent/Allow Medium Removal
  143. if (scsi_cmd[4] & 0x01) {
  144. // Prevent medium removal
  145. if (!medium_available) {
  146. debug("Host wants to lock non-existing medium. Not supported.");
  147. resplen = -1;
  148. } else {
  149. debug("Host wants to lock medium.");
  150. medium_locked = true;
  151. }
  152. } else {
  153. // Allow medium removal
  154. if (medium_available) {
  155. debug("Host unlocked medium.");
  156. medium_locked = false;
  157. } else {
  158. debug("host unlocked non-existing medium. Not supported.");
  159. resplen = -1;
  160. }
  161. }
  162. break;
  163. default:
  164. // Set Sense = Invalid Command Operation
  165. tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
  166. // negative means error -> tinyusb could stall and/or response with failed status
  167. resplen = -1;
  168. break;
  169. }
  170. // return resplen must not larger than bufsize
  171. if (resplen > bufsize) {
  172. resplen = bufsize;
  173. }
  174. if (response && (resplen > 0)) {
  175. if (in_xfer) {
  176. memcpy(buffer, response, (size_t) resplen);
  177. } else {
  178. // SCSI output
  179. }
  180. }
  181. return (int32_t) resplen;
  182. }