APM:Libraries
mass_storage.cpp
Go to the documentation of this file.
1 /*
2 (c) 2017 night_ghost@ykoctpa.ru
3 
4 
5 */
6 
8 
9 #include "mass_storage.h"
10 #include "../sd/SD.h"
11 #include "../sd/Sd2Card.h"
12 #include <usb.h>
13 
14 #include "usbMassStorage.h"
15 
16 #ifdef USB_MASSSTORAGE
17 
18 /*
19  also see https://habrahabr.ru/post/335018/ and https://habrahabr.ru/post/336968/
20 
21 */
22 
23 
26 
27 
29 
30 
31 void MassStorage::setup() const {
33 
34  usb_open();
35 
36  usb_default_attr(&usb_attr);
37  usb_attr.preempt_prio = USB_INT_PRIORITY;
38  usb_attr.sub_prio = 0;
39  usb_attr.use_present_pin = 1;
42 
43  usb_setParams(&usb_attr);
44 
45 
46  SdFatFs *fs = SD.getVolume();
47 
48  MAL_massBlockSize[0] = fs->sectorSize();
49  MAL_massBlockCount[0] = fs->sectorCount(); // in blocks
50 
51  SD.sync(); // consistent state
52 
53  SCSI_Init(); // start USB IO task
54 
56 }
57 
58 
59 // The following methods are used by the USBMassStorage driver to read and write to the SDCard.
60 
61 extern "C" {
62 
63 uint16_t usb_mass_mal_get_status(uint8_t lun){
64  return Sd2Card::errorCode();
65 }
66 
67 
68 int8_t usb_mass_mal_write_memory(uint8_t lun,uint8_t *writebuff, uint32_t lba, uint16_t transferLength) {
69  if (lun != 0) {
70  return USB_MASS_MAL_FAIL;
71  }
72  if (Sd2Card::writeBlock(writebuff, lba, transferLength)) {
73  return USB_MASS_MAL_SUCCESS;
74  }
75  return USB_MASS_MAL_FAIL;
76 }
77 
78 int8_t usb_mass_mal_read_memory(uint8_t lun, uint8_t *readbuff, uint32_t lba, uint16_t transferLength) {
79  if (lun != 0) {
80  return USB_MASS_MAL_FAIL;
81  }
82  if (Sd2Card::readBlock(readbuff, lba, transferLength)) {
83  return USB_MASS_MAL_SUCCESS;
84  }
85  return USB_MASS_MAL_FAIL;
86 }
87 
88 
89 } // extern C
90 
91 #endif
const USBD_DEVICE USR_MSC_desc
Definition: device_desc.c:105
void SCSI_Init()
uint16_t present_pin
Definition: usb.h:79
int usb_open(void)
Definition: usb.c:674
const stm32_pin_info PIN_MAP[BOARD_NR_GPIO_PINS]
Definition: usb.h:74
#define BOARD_USB_SENSE
Definition: board.h:75
#define USB_MASS_MAL_SUCCESS
uint16_t usb_mass_mal_get_status(uint8_t lun)
const USBD_Usr_cb_TypeDef USR_cb
Definition: usb.c:103
void USBD_Init(USB_OTG_CORE_HANDLE *pdev, const USB_OTG_CORE_ID_TypeDef coreID, const USBD_DEVICE *pDevice, const USBD_Class_cb_TypeDef *class_cb, const USBD_Usr_cb_TypeDef *usr_cb)
USBD_Init Initailizes the device stack and load the class driver.
Definition: usbd_core.c:134
static usb_attr_t * usb_attr
Definition: usb.c:38
uint32_t MAL_massBlockCount[STORAGE_LUN_NBR]
#define USB_MASS_MAL_FAIL
#define USB_INT_PRIORITY
Definition: Config.h:50
const USBD_Class_cb_TypeDef USBD_MSC_cb
USB_OTG_CORE_HANDLE USB_OTG_dev
int8_t usb_mass_mal_write_memory(uint8_t lun, uint8_t *writebuff, uint32_t memoryOffset, uint16_t transferLength)
uint8_t preempt_prio
Definition: usb.h:75
const gpio_dev * present_port
Definition: usb.h:78
uint8_t sub_prio
Definition: usb.h:76
#define STORAGE_LUN_NBR
void setup()
Definition: AC_PID_test.cpp:26
uint32_t MAL_massBlockSize[STORAGE_LUN_NBR]
uint8_t gpio_bit
Definition: boards.h:92
void usb_default_attr(usb_attr_t *attr)
Definition: usb.c:296
int8_t usb_mass_mal_read_memory(uint8_t lun, uint8_t *readbuff, uint32_t memoryOffset, uint16_t transferLength)
const gpio_dev *const gpio_device
Definition: boards.h:89
uint8_t use_present_pin
Definition: usb.h:77
void usb_setParams(usb_attr_t *attr)
Definition: usb.c:365