usb.c 5.65 KB
Newer Older
1
2
3
4
#include <string.h>

#include "usbd_core.h"
#include "usbd_desc.h"
5
#include "usbd_cdc_msc.h"
6
#include "usbd_cdc_interface.h"
7
#include "usbd_msc_storage.h"
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#include "misc.h"
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "usb.h"

#ifdef USE_DEVICE_MODE
USBD_HandleTypeDef hUSBDDevice;
#endif

static int dev_is_enabled = 0;
uint32_t APP_dev_is_connected = 0; /* used by usbd_cdc_vcp */
mp_obj_t mp_const_vcp_interrupt = MP_OBJ_NULL;

23
void pyb_usb_dev_init(usbd_device_kind_t device_kind, usbd_storage_medium_kind_t medium_kind) {
24
25
26
#ifdef USE_DEVICE_MODE
    if (!dev_is_enabled) {
        // only init USB once in the device's power-lifetime
27
        switch (device_kind) {
28
                #if 0
29
            case USBD_DEVICE_CDC:
30
31
32
33
                // XXX USBD_CDC_Init (called by one of these functions below) uses malloc,
                // so the memory is invalid after a soft reset (which resets the GC).
                USBD_Init(&hUSBDDevice, &VCP_Desc, 0);
                USBD_RegisterClass(&hUSBDDevice, &USBD_CDC);
Damien George's avatar
Damien George committed
34
                USBD_CDC_RegisterInterface(&hUSBDDevice, (USBD_CDC_ItfTypeDef*)&USBD_CDC_fops);
35
36
37
38
                USBD_Start(&hUSBDDevice);
                //USBD_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_PYB_cb, &USR_cb);
                break;

39
40
41
42
43
44
45
46
47
48
49
50
            case USBD_DEVICE_MSC:
                // XXX USBD_CDC_Init (called by one of these functions below) uses malloc,
                // so the memory is invalid after a soft reset (which resets the GC).
                USBD_Init(&hUSBDDevice, &MSC_Desc, 0);
                USBD_RegisterClass(&hUSBDDevice, &USBD_MSC);
                if (medium_kind == USBD_STORAGE_MEDIUM_FLASH) {
                    USBD_MSC_RegisterStorage(&hUSBDDevice, (USBD_StorageTypeDef*)&USBD_FLASH_STORAGE_fops);
                } else {
                    USBD_MSC_RegisterStorage(&hUSBDDevice, (USBD_StorageTypeDef*)&USBD_SDCARD_STORAGE_fops);
                }
                USBD_Start(&hUSBDDevice);
                break;
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
                #endif

            case USBD_DEVICE_CDC:
            case USBD_DEVICE_MSC:
                USBD_Init(&hUSBDDevice, &VCP_Desc, 0);
                USBD_RegisterClass(&hUSBDDevice, &USBD_CDC_MSC);
                USBD_CDC_RegisterInterface(&hUSBDDevice, (USBD_CDC_ItfTypeDef*)&USBD_CDC_fops);
                if (medium_kind == USBD_STORAGE_MEDIUM_FLASH) {
                    USBD_MSC_RegisterStorage(&hUSBDDevice, (USBD_StorageTypeDef*)&USBD_FLASH_STORAGE_fops);
                } else {
                    USBD_MSC_RegisterStorage(&hUSBDDevice, (USBD_StorageTypeDef*)&USBD_SDCARD_STORAGE_fops);
                }
                USBD_Start(&hUSBDDevice);
                break;

66
67

            case USBD_DEVICE_HID:
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
                //USBD_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_PYB_HID_cb, &USR_cb);
                // TODO
                break;
        }
    }
    dev_is_enabled = 1;

    // create an exception object for interrupting by VCP
    mp_const_vcp_interrupt = mp_obj_new_exception_msg(&mp_type_OSError, "VCPInterrupt");
#endif
}

bool usb_vcp_is_enabled(void) {
    return dev_is_enabled;
}

bool usb_vcp_is_connected(void) {
    return APP_dev_is_connected;
}

void usb_vcp_set_interrupt_char(int c) {
    if (dev_is_enabled) {
90
91
        if (c != VCP_CHAR_NONE) {
            mp_obj_exception_clear_traceback(mp_const_vcp_interrupt);
92
        }
93
        USBD_CDC_SetInterrupt(c, mp_const_vcp_interrupt);
94
95
96
    }
}

97
98
int usb_vcp_rx_num(void) {
    return USBD_CDC_RxNum();
99
100
101
}

char usb_vcp_rx_get(void) {
102
    return USBD_CDC_RxGet();
103
104
105
106
107
108
109
110
111
}

void usb_vcp_send_str(const char *str) {
    usb_vcp_send_strn(str, strlen(str));
}

void usb_vcp_send_strn(const char *str, int len) {
#ifdef USE_DEVICE_MODE
    if (dev_is_enabled) {
112
        USBD_CDC_Tx(str, len);
113
114
115
116
117
118
    }
#endif
}

void usb_vcp_send_strn_cooked(const char *str, int len) {
#ifdef USE_DEVICE_MODE
119
120
121
122
123
124
125
    if (dev_is_enabled) {
        for (const char *top = str + len; str < top; str++) {
            if (*str == '\n') {
                USBD_CDC_Tx("\r\n", 2);
            } else {
                USBD_CDC_Tx(str, 1);
            }
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
        }
    }
#endif
}

void usb_hid_send_report(uint8_t *buf) {
#ifdef USE_DEVICE_MODE
    #if 0
    USBD_HID_SendReport(&USB_OTG_Core, buf, 4);
    #endif
#endif
}

/******************************************************************************/
// code for experimental USB OTG support

#ifdef USE_HOST_MODE

#include "led.h"
#include "usbh_core.h"
#include "usbh_usr.h"
#include "usbh_hid_core.h"
#include "usbh_hid_keybd.h"
#include "usbh_hid_mouse.h"

__ALIGN_BEGIN USBH_HOST USB_Host __ALIGN_END ;

static int host_is_enabled = 0;

void pyb_usb_host_init(void) {
    if (!host_is_enabled) {
        // only init USBH once in the device's power-lifetime
        /* Init Host Library */
        USBH_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USB_Host, &HID_cb, &USR_Callbacks);
    }
    host_is_enabled = 1;
}

void pyb_usb_host_process(void) {
    USBH_Process(&USB_OTG_Core, &USB_Host);
}

uint8_t usb_keyboard_key = 0;

// TODO this is an ugly hack to get key presses
uint pyb_usb_host_get_keyboard(void) {
    uint key = usb_keyboard_key;
    usb_keyboard_key = 0;
    return key;
}

void USR_MOUSE_Init(void) {
    led_state(4, 1);
    USB_OTG_BSP_mDelay(100);
    led_state(4, 0);
}

void USR_MOUSE_ProcessData(HID_MOUSE_Data_TypeDef *data) {
    led_state(4, 1);
    USB_OTG_BSP_mDelay(50);
    led_state(4, 0);
}

void USR_KEYBRD_Init(void) {
    led_state(4, 1);
    USB_OTG_BSP_mDelay(100);
    led_state(4, 0);
}

void USR_KEYBRD_ProcessData(uint8_t pbuf) {
    led_state(4, 1);
    USB_OTG_BSP_mDelay(50);
    led_state(4, 0);
    //lcd_print_strn((char*)&pbuf, 1);
    usb_keyboard_key = pbuf;
}

#endif // USE_HOST_MODE