-
Notifications
You must be signed in to change notification settings - Fork 4
/
usb_mass.h
117 lines (92 loc) · 3.46 KB
/
usb_mass.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#ifndef _LIBMAPLE_USB_MASS_H_
#define _LIBMAPLE_USB_MASS_H_
#include <libmaple/libmaple_types.h>
#include <libmaple/gpio.h>
#include <libmaple/usb.h>
#include <inttypes.h>
#ifdef __cplusplus
extern "C" {
#endif
#define N_STRING_DESCRIPTORS 4
#define MAX_PACKET_SIZE 0x40 /* 64B, maximum for USB FS Devices */
#define MAX_BULK_PACKET_SIZE 0x40 /* 64B, max bulk Can't use 512 because the internal buffers for USB is only 512B */
/* buffer table base address */
#define BTABLE_ADDRESS 0x00
/* EP0 */
/* rx/tx buffer base address */
#define ENDP0_RXADDR (0x18)
#define ENDP0_TXADDR (ENDP0_RXADDR + MAX_PACKET_SIZE)
/* EP1 */
/* tx buffer base address */
#define ENDP1_TXADDR (ENDP0_TXADDR + MAX_PACKET_SIZE)
/* EP2 */
/* Rx buffer base address */
#define ENDP2_RXADDR (ENDP0_TXADDR + MAX_BULK_PACKET_SIZE)
/* MASS Storage Requests */
#define REQUEST_GET_MAX_LUN 0xFE
#define REQUEST_MASS_STORAGE_RESET 0xFF
/* USB device state */
typedef enum _DEVICE_STATE {
DEVICE_STATE_UNCONNECTED,
DEVICE_STATE_ATTACHED,
DEVICE_STATE_POWERED,
DEVICE_STATE_SUSPENDED,
DEVICE_STATE_ADDRESSED,
DEVICE_STATE_CONFIGURED
} DEVICE_STATE;
#define BOT_DIR_IN 0
#define BOT_DIR_OUT 1
#define BOT_DIR_BOTH 2
/*****************************************************************************/
/*********************** Bulk-Only Transfer State machine ********************/
/*****************************************************************************/
#define BOT_STATE_IDLE 0 /* Idle state */
#define BOT_STATE_DATA_OUT 1 /* Data Out state */
#define BOT_STATE_DATA_IN 2 /* Data In state */
#define BOT_STATE_DATA_IN_LAST 3 /* Last Data In Last */
#define BOT_STATE_CSW_Send 4 /* Command Status Wrapper */
#define BOT_STATE_ERROR 5 /* error state */
#define BOT_CBW_SIGNATURE 0x43425355
#define BOT_CSW_SIGNATURE 0x53425355
#define BOT_CBW_PACKET_LENGTH 31
#define BOT_CSW_DATA_LENGTH 0x000D
/* CSW Status Definitions */
#define BOT_CSW_CMD_PASSED 0x00
#define BOT_CSW_CMD_FAILED 0x01
#define BOT_CSW_PHASE_ERROR 0x02
#define BOT_SEND_CSW_DISABLE 0
#define BOT_SEND_CSW_ENABLE 1
#define USB_EP1_IN 0x81
/* Bulk-only Command Block Wrapper */
typedef struct _BulkOnlyCBW {
uint32_t dSignature;
uint32_t dTag;
uint32_t dDataLength;
uint8_t bmFlags;
uint8_t bLUN;
uint8_t bCBLength;
uint8_t CB[16];
} BulkOnlyCBW;
/* Bulk-only Command Status Wrapper */
typedef struct _BulkOnlyCSW {
uint32_t dSignature;
uint32_t dTag;
uint32_t dDataResidue;
uint8_t bStatus;
} BulkOnlyCSW;
typedef struct _usb_descriptor_config {
usb_descriptor_config_header Config_Header;
usb_descriptor_interface MASS_Interface;
usb_descriptor_endpoint DataInEndpoint;
usb_descriptor_endpoint DataOutEndpoint;
} __packed usb_descriptor_config;
void usb_mass_enable(gpio_dev *disc_dev, uint8 disc_bit);
void usb_mass_disable(gpio_dev *disc_dev, uint8 disc_bit);
void usb_mass_loop();
void usb_mass_bot_set_csw(uint8_t cswStatus, uint8_t sendPermission);
void usb_mass_transfer_data_request(uint8_t* dataPointer, uint16_t dataLen);
void usb_mass_bot_abort(uint8_t direction);
#ifdef __cplusplus
}
#endif
#endif