usb.c 5.26 KB
Newer Older
1
2
#include <string.h>

3
4
#include "usb_core.h"
#include "usbd_core.h"
5
#include "usbd_cdc_core.h"
6
#include "usbd_pyb_core.h"
Damien's avatar
Damien committed
7
8
#include "usbd_usr.h"
#include "usbd_desc.h"
9
10

#include "misc.h"
11
12
13
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
14
#include "pendsv.h"
Damien's avatar
Damien committed
15
#include "usb.h"
Damien's avatar
Damien committed
16

17
#ifdef USE_DEVICE_MODE
18
extern CDC_IF_Prop_TypeDef VCP_fops;
19
#endif
Damien's avatar
Damien committed
20

21
USB_OTG_CORE_HANDLE USB_OTG_Core;
Damien's avatar
Damien committed
22

23
static int dev_is_enabled = 0;
Damien's avatar
Damien committed
24
25
26
static char rx_buf[64];
static int rx_buf_in;
static int rx_buf_out;
27
28
static int interrupt_char = VCP_CHAR_NONE;
mp_obj_t mp_const_vcp_interrupt = MP_OBJ_NULL;
Damien's avatar
Damien committed
29

30
void pyb_usb_dev_init(void) {
31
#ifdef USE_DEVICE_MODE
32
33
34
35
    if (!dev_is_enabled) {
        // only init USB once in the device's power-lifetime
        USBD_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_PYB_cb, &USR_cb);
        //USBD_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_PYB_HID_cb, &USR_cb);
36
    }
37
38
    rx_buf_in = 0;
    rx_buf_out = 0;
39
    interrupt_char = VCP_CHAR_NONE;
40
    dev_is_enabled = 1;
41
42
43

    // create an exception object for interrupting by VCP
    mp_const_vcp_interrupt = mp_obj_new_exception(qstr_from_str("VCPInterrupt"));
44
#endif
Damien's avatar
Damien committed
45
46
}

47
bool usb_vcp_is_enabled(void) {
48
    return dev_is_enabled;
Damien's avatar
Damien committed
49
50
}

51
52
53
54
55
56
void usb_vcp_set_interrupt_char(int c) {
    if (dev_is_enabled) {
        interrupt_char = c;
    }
}

57
void usb_vcp_receive(const char *buf, uint32_t len) {
58
    if (dev_is_enabled) {
59
        for (int i = 0; i < len; i++) {
60
61
62
63
64
65
66
67
68

            // catch special interrupt character
            if (buf[i] == interrupt_char) {
                // raise exception when interrupts are finished
                pendsv_nlr_jump(mp_const_vcp_interrupt);
                interrupt_char = VCP_CHAR_NONE;
                continue;
            }

69
70
71
72
73
74
75
76
77
            rx_buf[rx_buf_in++] = buf[i];
            if (rx_buf_in >= sizeof(rx_buf)) {
                rx_buf_in = 0;
            }
            if (rx_buf_in == rx_buf_out) {
                rx_buf_out = rx_buf_in + 1;
                if (rx_buf_out >= sizeof(rx_buf)) {
                    rx_buf_out = 0;
                }
78
79
80
81
82
            }
        }
    }
}

83
int usb_vcp_rx_any(void) {
84
85
86
87
88
89
90
    if (rx_buf_in >= rx_buf_out) {
        return rx_buf_in - rx_buf_out;
    } else {
        return rx_buf_in + sizeof(rx_buf) - rx_buf_out;
    }
}

91
char usb_vcp_rx_get(void) {
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    while (rx_buf_out == rx_buf_in) {
    }
    char c = rx_buf[rx_buf_out];
    rx_buf_out += 1;
    if (rx_buf_out >= sizeof(rx_buf)) {
        rx_buf_out = 0;
    }
    return c;
}

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) {
107
#ifdef USE_DEVICE_MODE
108
    if (dev_is_enabled) {
109
        VCP_fops.pIf_DataTx((const uint8_t*)str, len);
110
    }
111
#endif
Damien's avatar
Damien committed
112
}
113

114
#include "usbd_conf.h"
115
116
117

/* These are external variables imported from CDC core to be used for IN 
   transfer management. */
118
#ifdef USE_DEVICE_MODE
119
120
121
122
123
124
extern uint8_t  APP_Rx_Buffer []; /* Write CDC received data in this buffer.
                                     These data will be sent over USB IN endpoint
                                     in the CDC core functions. */
extern uint32_t APP_Rx_ptr_in;    /* Increment this pointer or roll it back to
                                     start address when writing received data
                                     in the buffer APP_Rx_Buffer. */
125
#endif
126
127

void usb_vcp_send_strn_cooked(const char *str, int len) {
128
#ifdef USE_DEVICE_MODE
129
130
131
132
133
134
135
136
    for (const char *top = str + len; str < top; str++) {
        if (*str == '\n') {
            APP_Rx_Buffer[APP_Rx_ptr_in] = '\r';
            APP_Rx_ptr_in = (APP_Rx_ptr_in + 1) & (APP_RX_DATA_SIZE - 1);
        }
        APP_Rx_Buffer[APP_Rx_ptr_in] = *str;
        APP_Rx_ptr_in = (APP_Rx_ptr_in + 1) & (APP_RX_DATA_SIZE - 1);
    }
137
#endif
138
}
139
140

void usb_hid_send_report(uint8_t *buf) {
141
#ifdef USE_DEVICE_MODE
142
    USBD_HID_SendReport(&USB_OTG_Core, buf, 4);
143
#endif
144
}
145
146
147
148
149
150

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

#ifdef USE_HOST_MODE

151
152
153
154
155
156
#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"
157
158
159
160

__ALIGN_BEGIN USBH_HOST USB_Host __ALIGN_END ;

static int host_is_enabled = 0;
161
162

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

171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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);
194
195
}

196
197
198
199
void USR_KEYBRD_Init(void) {
    led_state(4, 1);
    USB_OTG_BSP_mDelay(100);
    led_state(4, 0);
200
201
}

202
203
204
205
206
207
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;
208
209
}

210
#endif // USE_HOST_MODE