|
| 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 | + |
| 23 | +#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) |
| 24 | + |
| 25 | +#include "../../inc/MarlinConfig.h" |
| 26 | + |
| 27 | +#if BOTH(USE_OTG_USB_HOST, USBHOST) |
| 28 | + |
| 29 | +#include "usb_host.h" |
| 30 | +#include "../shared/Marduino.h" |
| 31 | +#include "usbh_core.h" |
| 32 | +#include "usbh_msc.h" |
| 33 | + |
| 34 | +USBH_HandleTypeDef hUsbHost; |
| 35 | +USBHost usb; |
| 36 | +BulkStorage bulk(&usb); |
| 37 | + |
| 38 | +static void USBH_UserProcess(USBH_HandleTypeDef *phost, uint8_t id) { |
| 39 | + switch(id) { |
| 40 | + case HOST_USER_SELECT_CONFIGURATION: |
| 41 | + //SERIAL_ECHOLNPGM("APPLICATION_SELECT_CONFIGURATION"); |
| 42 | + break; |
| 43 | + case HOST_USER_DISCONNECTION: |
| 44 | + //SERIAL_ECHOLNPGM("APPLICATION_DISCONNECT"); |
| 45 | + //usb.setUsbTaskState(USB_STATE_RUNNING); |
| 46 | + break; |
| 47 | + case HOST_USER_CLASS_ACTIVE: |
| 48 | + //SERIAL_ECHOLNPGM("APPLICATION_READY"); |
| 49 | + usb.setUsbTaskState(USB_STATE_RUNNING); |
| 50 | + break; |
| 51 | + case HOST_USER_CONNECTION: |
| 52 | + break; |
| 53 | + default: |
| 54 | + break; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +bool USBHost::start() { |
| 59 | + if (USBH_Init(&hUsbHost, USBH_UserProcess, TERN(USE_USB_HS_IN_FS, HOST_HS, HOST_FS)) != USBH_OK) { |
| 60 | + SERIAL_ECHOLNPGM("Error: USBH_Init"); |
| 61 | + return false; |
| 62 | + } |
| 63 | + if (USBH_RegisterClass(&hUsbHost, USBH_MSC_CLASS) != USBH_OK) { |
| 64 | + SERIAL_ECHOLNPGM("Error: USBH_RegisterClass"); |
| 65 | + return false; |
| 66 | + } |
| 67 | + if (USBH_Start(&hUsbHost) != USBH_OK) { |
| 68 | + SERIAL_ECHOLNPGM("Error: USBH_Start"); |
| 69 | + return false; |
| 70 | + } |
| 71 | + return true; |
| 72 | +} |
| 73 | + |
| 74 | +void USBHost::Task() { |
| 75 | + USBH_Process(&hUsbHost); |
| 76 | +} |
| 77 | + |
| 78 | +uint8_t USBHost::getUsbTaskState() { |
| 79 | + return usb_task_state; |
| 80 | +} |
| 81 | + |
| 82 | +void USBHost::setUsbTaskState(uint8_t state) { |
| 83 | + usb_task_state = state; |
| 84 | + if (usb_task_state == USB_STATE_RUNNING) { |
| 85 | + MSC_LUNTypeDef info; |
| 86 | + USBH_MSC_GetLUNInfo(&hUsbHost, usb.lun, &info); |
| 87 | + capacity = info.capacity.block_nbr / 2000; |
| 88 | + block_size = info.capacity.block_size; |
| 89 | + block_count = info.capacity.block_nbr; |
| 90 | + // SERIAL_ECHOLNPAIR("info.capacity.block_nbr : %ld\n", info.capacity.block_nbr); |
| 91 | + // SERIAL_ECHOLNPAIR("info.capacity.block_size: %d\n", info.capacity.block_size); |
| 92 | + // SERIAL_ECHOLNPAIR("capacity : %d MB\n", capacity); |
| 93 | + } |
| 94 | +}; |
| 95 | + |
| 96 | +bool BulkStorage::LUNIsGood(uint8_t t) { |
| 97 | + return USBH_MSC_IsReady(&hUsbHost) && USBH_MSC_UnitIsReady(&hUsbHost, t); |
| 98 | +} |
| 99 | + |
| 100 | +uint32_t BulkStorage::GetCapacity(uint8_t lun) { |
| 101 | + return usb->block_count; |
| 102 | +} |
| 103 | + |
| 104 | +uint16_t BulkStorage::GetSectorSize(uint8_t lun) { |
| 105 | + return usb->block_size; |
| 106 | +} |
| 107 | + |
| 108 | +uint8_t BulkStorage::Read(uint8_t lun, uint32_t addr, uint16_t bsize, uint8_t blocks, uint8_t *buf) { |
| 109 | + return USBH_MSC_Read(&hUsbHost, lun, addr, buf, blocks) != USBH_OK; |
| 110 | +} |
| 111 | + |
| 112 | +uint8_t BulkStorage::Write(uint8_t lun, uint32_t addr, uint16_t bsize, uint8_t blocks, const uint8_t * buf) { |
| 113 | + return USBH_MSC_Write(&hUsbHost, lun, addr, const_cast <uint8_t*>(buf), blocks) != USBH_OK; |
| 114 | +} |
| 115 | + |
| 116 | +#endif // USE_OTG_USB_HOST && USBHOST |
| 117 | +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC |
0 commit comments