diff options
| author | HaiMianBBao <[email protected]> | 2021-11-22 22:39:57 +0800 |
|---|---|---|
| committer | HaiMianBBao <[email protected]> | 2021-11-22 22:39:57 +0800 |
| commit | 89f8d74fb82c3af2a67938941421077f499ed5c5 (patch) | |
| tree | 2984959a8886567383131cb4cfe458673b6ab6fb | |
| parent | 936684c055bc01edcd9d046429fca36665aefba4 (diff) | |
add stm32 msc ram demo
| -rw-r--r-- | demo/stm32/stm32f103c8t6/Core/Src/main.c | 3 | ||||
| -rw-r--r-- | demo/stm32/stm32f103c8t6/example/example_select.h | 5 | ||||
| -rw-r--r-- | demo/stm32/stm32f103c8t6/example/msc_ram/msc_ram_init.c | 153 |
3 files changed, 160 insertions, 1 deletions
diff --git a/demo/stm32/stm32f103c8t6/Core/Src/main.c b/demo/stm32/stm32f103c8t6/Core/Src/main.c index d2f353d6..d534000c 100644 --- a/demo/stm32/stm32f103c8t6/Core/Src/main.c +++ b/demo/stm32/stm32f103c8t6/Core/Src/main.c @@ -115,6 +115,9 @@ int main(void) #elif defined USB_HID_CUSTOM extern void hid_custom_init(void); hid_custom_init(); +#elif defined USB_MSC_RAM + extern void msc_ram_init(void); + msc_ram_init(); #endif /*!< init */ diff --git a/demo/stm32/stm32f103c8t6/example/example_select.h b/demo/stm32/stm32f103c8t6/example/example_select.h index 873a2c49..325ea792 100644 --- a/demo/stm32/stm32f103c8t6/example/example_select.h +++ b/demo/stm32/stm32f103c8t6/example/example_select.h @@ -2,7 +2,7 @@ /*!< Use macros to select the examples to test */ /*!< cdc */ -#define USB_CDC +//#define USB_CDC /*!< hid keyboard */ //#define USB_HID_KEYBOARD @@ -12,3 +12,6 @@ /*!< hid custom */ //#define USB_HID_CUSTOM + +/*!< msc ram */ +#define USB_MSC_RAM
\ No newline at end of file diff --git a/demo/stm32/stm32f103c8t6/example/msc_ram/msc_ram_init.c b/demo/stm32/stm32f103c8t6/example/msc_ram/msc_ram_init.c new file mode 100644 index 00000000..026b7e04 --- /dev/null +++ b/demo/stm32/stm32f103c8t6/example/msc_ram/msc_ram_init.c @@ -0,0 +1,153 @@ +/** @mainpage msc_ram_demo_init + * <table> + * <tr><th>Project <td>msc_ram_demo + * <tr><th>Author <td>LiGuo [email protected] + * </table> + * @section msc ram init demo + * + * + * @section �汾������ʷ + * + * �汾|����|ʱ��|���� + * ----|----|----|---- + * 1.0|LiGuo|2021.11.19|creat project + * + * <h2><center>© Copyright 2021 LiGuo. + * All rights reserved.</center></h2> + ********************************************************************************* + */ + +/* include ------------------------------------------------------------------*/ +#include "main.h" +#include "usb_def.h" +#include "usbd_core.h" +#include "usbd_msc.h" + + +#define MSC_IN_EP 0x85 +#define MSC_OUT_EP 0x04 + +#define USBD_VID 0xFFFF +#define USBD_PID 0xFFFF +#define USBD_MAX_POWER 100 +#define USBD_LANGID_STRING 1033 + +#define USB_CONFIG_SIZE (9 + MSC_DESCRIPTOR_LEN) + +const uint8_t msc_ram_descriptor[] = { + USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0200, 0x01), + USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER), + MSC_DESCRIPTOR_INIT(0x00, MSC_OUT_EP, MSC_IN_EP, 0x02), + /////////////////////////////////////// + /// string0 descriptor + /////////////////////////////////////// + USB_LANGID_INIT(USBD_LANGID_STRING), + /////////////////////////////////////// + /// string1 descriptor + /////////////////////////////////////// + 0x12, /* bLength */ + USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */ + 'B', 0x00, /* wcChar0 */ + 'o', 0x00, /* wcChar1 */ + 'u', 0x00, /* wcChar2 */ + 'f', 0x00, /* wcChar3 */ + 'f', 0x00, /* wcChar4 */ + 'a', 0x00, /* wcChar5 */ + 'l', 0x00, /* wcChar6 */ + 'o', 0x00, /* wcChar7 */ + /////////////////////////////////////// + /// string2 descriptor + /////////////////////////////////////// + 0x24, /* bLength */ + USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */ + 'B', 0x00, /* wcChar0 */ + 'o', 0x00, /* wcChar1 */ + 'u', 0x00, /* wcChar2 */ + 'f', 0x00, /* wcChar3 */ + 'f', 0x00, /* wcChar4 */ + 'a', 0x00, /* wcChar5 */ + 'l', 0x00, /* wcChar6 */ + 'o', 0x00, /* wcChar7 */ + ' ', 0x00, /* wcChar8 */ + 'M', 0x00, /* wcChar9 */ + 'S', 0x00, /* wcChar10 */ + 'C', 0x00, /* wcChar11 */ + ' ', 0x00, /* wcChar12 */ + 'D', 0x00, /* wcChar13 */ + 'E', 0x00, /* wcChar14 */ + 'M', 0x00, /* wcChar15 */ + 'O', 0x00, /* wcChar16 */ + /////////////////////////////////////// + /// string3 descriptor + /////////////////////////////////////// + 0x16, /* bLength */ + USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */ + '2', 0x00, /* wcChar0 */ + '0', 0x00, /* wcChar1 */ + '2', 0x00, /* wcChar2 */ + '1', 0x00, /* wcChar3 */ + '0', 0x00, /* wcChar4 */ + '3', 0x00, /* wcChar5 */ + '1', 0x00, /* wcChar6 */ + '0', 0x00, /* wcChar7 */ + '0', 0x00, /* wcChar8 */ + '0', 0x00, /* wcChar9 */ +#ifdef CONFIG_USB_HS + /////////////////////////////////////// + /// device qualifier descriptor + /////////////////////////////////////// + 0x0a, + USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER, + 0x00, + 0x02, + 0x00, + 0x00, + 0x00, + 0x40, + 0x01, + 0x00, +#endif + 0x00 +}; + +#define BLOCK_SIZE 512 +#define BLOCK_COUNT 2 + +typedef struct +{ + uint8_t BlockSpace[BLOCK_SIZE]; +} BLOCK_TYPE; + +// The CDC recv buffer size should equal to the out endpoint size +// or we will need a timeout to flush the recv buffer +BLOCK_TYPE mass_block[BLOCK_COUNT]; + +void usbd_msc_get_cap(uint8_t lun, uint32_t *block_num, uint16_t *block_size) +{ + *block_num = BLOCK_COUNT; + *block_size = BLOCK_SIZE; +} +int usbd_msc_sector_read(uint32_t sector, uint8_t *buffer, uint32_t length) +{ + memcpy(buffer, mass_block[sector].BlockSpace, length); + return 0; +} + +int usbd_msc_sector_write(uint32_t sector, uint8_t *buffer, uint32_t length) +{ + memcpy(mass_block[sector].BlockSpace, buffer, length); + return 0; +} + +/* function ------------------------------------------------------------------*/ +/** + * @brief msc ram init + * @pre none + * @param[in] none + * @retval none + */ +void msc_ram_init(void) +{ + usbd_desc_register(msc_ram_descriptor); + usbd_msc_class_init(MSC_OUT_EP, MSC_IN_EP); +}
\ No newline at end of file |
