My Marlin configs for Fabrikator Mini and CTC i3 Pro B
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.

usb_host.cpp 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../platforms.h"
  23. #ifdef HAL_STM32
  24. #include "../../inc/MarlinConfig.h"
  25. #if BOTH(USE_OTG_USB_HOST, USBHOST)
  26. #include "usb_host.h"
  27. #include "../shared/Marduino.h"
  28. #include "usbh_core.h"
  29. #include "usbh_msc.h"
  30. USBH_HandleTypeDef hUsbHost;
  31. USBHost usb;
  32. BulkStorage bulk(&usb);
  33. static void USBH_UserProcess(USBH_HandleTypeDef *phost, uint8_t id) {
  34. switch(id) {
  35. case HOST_USER_SELECT_CONFIGURATION:
  36. //SERIAL_ECHOLNPGM("APPLICATION_SELECT_CONFIGURATION");
  37. break;
  38. case HOST_USER_DISCONNECTION:
  39. //SERIAL_ECHOLNPGM("APPLICATION_DISCONNECT");
  40. //usb.setUsbTaskState(USB_STATE_RUNNING);
  41. break;
  42. case HOST_USER_CLASS_ACTIVE:
  43. //SERIAL_ECHOLNPGM("APPLICATION_READY");
  44. usb.setUsbTaskState(USB_STATE_RUNNING);
  45. break;
  46. case HOST_USER_CONNECTION:
  47. break;
  48. default:
  49. break;
  50. }
  51. }
  52. bool USBHost::start() {
  53. if (USBH_Init(&hUsbHost, USBH_UserProcess, TERN(USE_USB_HS_IN_FS, HOST_HS, HOST_FS)) != USBH_OK) {
  54. SERIAL_ECHOLNPGM("Error: USBH_Init");
  55. return false;
  56. }
  57. if (USBH_RegisterClass(&hUsbHost, USBH_MSC_CLASS) != USBH_OK) {
  58. SERIAL_ECHOLNPGM("Error: USBH_RegisterClass");
  59. return false;
  60. }
  61. if (USBH_Start(&hUsbHost) != USBH_OK) {
  62. SERIAL_ECHOLNPGM("Error: USBH_Start");
  63. return false;
  64. }
  65. return true;
  66. }
  67. void USBHost::Task() {
  68. USBH_Process(&hUsbHost);
  69. }
  70. uint8_t USBHost::getUsbTaskState() {
  71. return usb_task_state;
  72. }
  73. void USBHost::setUsbTaskState(uint8_t state) {
  74. usb_task_state = state;
  75. if (usb_task_state == USB_STATE_RUNNING) {
  76. MSC_LUNTypeDef info;
  77. USBH_MSC_GetLUNInfo(&hUsbHost, usb.lun, &info);
  78. capacity = info.capacity.block_nbr / 2000;
  79. block_size = info.capacity.block_size;
  80. block_count = info.capacity.block_nbr;
  81. //SERIAL_ECHOLNPGM("info.capacity.block_nbr : %ld\n", info.capacity.block_nbr);
  82. //SERIAL_ECHOLNPGM("info.capacity.block_size: %d\n", info.capacity.block_size);
  83. //SERIAL_ECHOLNPGM("capacity : %d MB\n", capacity);
  84. }
  85. };
  86. bool BulkStorage::LUNIsGood(uint8_t t) {
  87. return USBH_MSC_IsReady(&hUsbHost) && USBH_MSC_UnitIsReady(&hUsbHost, t);
  88. }
  89. uint32_t BulkStorage::GetCapacity(uint8_t lun) {
  90. return usb->block_count;
  91. }
  92. uint16_t BulkStorage::GetSectorSize(uint8_t lun) {
  93. return usb->block_size;
  94. }
  95. uint8_t BulkStorage::Read(uint8_t lun, uint32_t addr, uint16_t bsize, uint8_t blocks, uint8_t *buf) {
  96. return USBH_MSC_Read(&hUsbHost, lun, addr, buf, blocks) != USBH_OK;
  97. }
  98. uint8_t BulkStorage::Write(uint8_t lun, uint32_t addr, uint16_t bsize, uint8_t blocks, const uint8_t * buf) {
  99. return USBH_MSC_Write(&hUsbHost, lun, addr, const_cast<uint8_t*>(buf), blocks) != USBH_OK;
  100. }
  101. #endif // USE_OTG_USB_HOST && USBHOST
  102. #endif // HAL_STM32