aboutsummaryrefslogtreecommitdiffstats
path: root/src/lpc176x/usbserial.c
blob: 54c28a31dd310bd9de888248582c4916b04415d6 (plain)
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
118
119
120
121
122
123
124
125
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Hardware interface to USB on lpc176x
//
// Copyright (C) 2018  Kevin O'Connor <kevin@koconnor.net>
//
// This file may be distributed under the terms of the GNU GPLv3 license.

#include <string.h> // memcpy
#include "autoconf.h" // CONFIG_SMOOTHIEWARE_BOOTLOADER
#include "board/armcm_boot.h" // armcm_enable_irq
#include "board/armcm_timer.h" // udelay
#include "board/irq.h" // irq_disable
#include "board/misc.h" // timer_read_time
#include "byteorder.h" // cpu_to_le32
#include "command.h" // DECL_CONSTANT_STR
#include "generic/usb_cdc.h" // usb_notify_ep0
#include "generic/usbstd.h" // usb_string_descriptor
#include "internal.h" // gpio_peripheral
#include "sched.h" // DECL_INIT
#include "usb_cdc_ep.h" // USB_CDC_EP_BULK_IN

// Internal endpoint addresses
#define EP0OUT 0x00
#define EP0IN 0x01
#define EP1IN 0x03
#define EP2OUT 0x04
#define EP5IN 0x0b

// USB device interupt status flags
#define EP_SLOW (1<<2)
#define DEV_STAT (1<<3)
#define CCEMPTY (1<<4)
#define CDFULL (1<<5)
#define EP_RLZED (1<<8)

#define RD_EN (1<<0)
#define WR_EN (1<<1)

// IAP interface
#define IAP_LOCATION        0x1fff1ff1
#define IAP_CMD_READ_UID    58
#define IAP_UID_LEN         16
typedef void (*IAP)(uint32_t *, uint32_t *);

static void
usb_irq_disable(void)
{
    NVIC_DisableIRQ(USB_IRQn);
}

static void
usb_irq_enable(void)
{
    NVIC_EnableIRQ(USB_IRQn);
}

static void
usb_wait(uint32_t flag)
{
    while (!(LPC_USB->USBDevIntSt & flag))
        ;
    LPC_USB->USBDevIntClr = flag;
}


/****************************************************************
 * Serial Interface Engine (SIE) functions
 ****************************************************************/

#define SIE_CMD_SELECT 0x00
#define SIE_CMD_SET_ENDPOINT_STATUS 0x40
#define SIE_CMD_SET_ADDRESS 0xD0
#define SIE_CMD_CONFIGURE 0xD8
#define SIE_CMD_SET_DEVICE_STATUS 0xFE
#define SIE_CMD_CLEAR_BUFFER 0xF2
#define SIE_CMD_VALIDATE_BUFFER 0xFA

static void
sie_cmd(uint32_t cmd)
{
    LPC_USB->USBDevIntClr = CDFULL | CCEMPTY;
    LPC_USB->USBCmdCode = 0x00000500 | (cmd << 16);
    usb_wait(CCEMPTY);
}

static void
sie_cmd_write(uint32_t cmd, uint32_t data)
{
    sie_cmd(cmd);
    LPC_USB->USBCmdCode = 0x00000100 | (data << 16);
    usb_wait(CCEMPTY);
}

static uint32_t
sie_cmd_read(uint32_t cmd)
{
    sie_cmd(cmd);
    LPC_USB->USBCmdCode = 0x00000200 | (cmd << 16);
    usb_wait(CDFULL);
    return LPC_USB->USBCmdData;
}

static uint32_t
sie_select_and_clear(uint32_t idx)
{
    LPC_USB->USBEpIntClr = 1<<idx;
    usb_wait(CDFULL);
    return LPC_USB->USBCmdData;
}


/****************************************************************
 * Interface
 ****************************************************************/
#define USB_STR_SERIAL_CHIPID u"0123456789ABCDEF0123456789ABCDEF"

#define SIZE_cdc_string_serial_chipid \
    (sizeof(cdc_string_serial_chipid) + sizeof(USB_STR_SERIAL_CHIPID) - 2)

static struct usb_string_descriptor cdc_string_serial_chipid = {
    .bLength = SIZE_cdc_string_serial_chipid,
    .bDescriptorType = USB_DT_STRING,
    .data = USB_STR_SERIAL_CHIPID,
};

static int_fast8_t
usb_write_packet(uint32_t ep, const void *data, uint_fast8_t len)
{
    usb_irq_disable();
    uint32_t sts = sie_cmd_read(SIE_CMD_SELECT | ep);
    if (sts & 0x01) {
        // Output buffers full
        usb_irq_enable();
        return -1;
    }

    LPC_USB->USBCtrl = WR_EN | ((ep/2) << 2);
    LPC_USB->USBTxPLen = len;
    if (!len)
        LPC_USB->USBTxData = 0;
    int i;
    for (i = 0; i<DIV_ROUND_UP(len, 4); i++) {
        uint32_t d;
        memcpy(&d, data, sizeof(d));
        data += sizeof(d);
        LPC_USB->USBTxData = cpu_to_le32(d);
    }
    sie_cmd(SIE_CMD_VALIDATE_BUFFER);
    usb_irq_enable();

    return len;
}

static int_fast8_t
usb_read_packet(uint32_t ep, void *data, uint_fast8_t max_len)
{
    usb_irq_disable();
    uint32_t sts = sie_cmd_read(SIE_CMD_SELECT | ep);
    if (!(sts & 0x01)) {
        // No data available
        usb_irq_enable();
        return -1;
    }

    // Determine packet size
    LPC_USB->USBCtrl = RD_EN | ((ep/2) << 2);
    uint32_t plen = LPC_USB->USBRxPLen;
    while (!(plen & (1<<11)))
        plen = LPC_USB->USBRxPLen;
    plen &= 0x3FF;
    if (plen > max_len)
        // XXX - return error code?  must keep reading?
        plen = max_len;
    // Copy data
    uint32_t xfer = plen;
    for (;;) {
        uint32_t d = le32_to_cpu(LPC_USB->USBRxData);
        if (xfer <= sizeof(d)) {
            memcpy(data, &d, xfer);
            break;
        }
        memcpy(data, &d, sizeof(d));
        data += sizeof(d);
        xfer -= sizeof(d);
    }
    // Clear space for next packet
    sts = sie_cmd_read(SIE_CMD_CLEAR_BUFFER);
    usb_irq_enable();
    if (sts & 0x01)
        // Packet overwritten
        return -1;

    return plen;
}

int_fast8_t
usb_read_bulk_out(void *data, uint_fast8_t max_len)
{
    return usb_read_packet(EP2OUT, data, max_len);
}

int_fast8_t
usb_send_bulk_in(void *data, uint_fast8_t len)
{
    return usb_write_packet(EP5IN, data, len);
}

int_fast8_t
usb_read_ep0(void *data, uint_fast8_t max_len)
{
    return usb_read_packet(EP0OUT, data, max_len);
}

int_fast8_t
usb_read_ep0_setup(void *data, uint_fast8_t max_len)
{
    return usb_read_ep0(data, max_len);
}

int_fast8_t
usb_send_ep0(const void *data, uint_fast8_t len)
{
    return usb_write_packet(EP0IN, data, len);
}

void
usb_stall_ep0(void)
{
    usb_irq_disable();
    sie_cmd_write(SIE_CMD_SET_ENDPOINT_STATUS | 0, (1<<7));
    usb_irq_enable();
}

void
usb_set_address(uint_fast8_t addr)
{
    usb_irq_disable();
    sie_cmd_write(SIE_CMD_SET_ADDRESS, addr | (1<<7));
    usb_irq_enable();
    usb_send_ep0(NULL, 0);
}

static void
realize_endpoint(uint32_t idx, uint32_t packet_size)
{
    LPC_USB->USBDevIntClr = EP_RLZED;
    LPC_USB->USBReEp |= 1<<idx;
    LPC_USB->USBEpInd = idx;
    LPC_USB->USBMaxPSize = packet_size;
    usb_wait(EP_RLZED);
    LPC_USB->USBEpIntEn |= 1<<idx;
    sie_cmd_write(SIE_CMD_SET_ENDPOINT_STATUS | idx, 0);
}

void
usb_set_configure(void)
{
    usb_irq_disable();
    realize_endpoint(EP1IN, USB_CDC_EP_ACM_SIZE);
    realize_endpoint(EP2OUT, USB_CDC_EP_BULK_OUT_SIZE);
    realize_endpoint(EP5IN, USB_CDC_EP_BULK_IN_SIZE);
    sie_cmd_write(SIE_CMD_CONFIGURE, 1);
    usb_irq_enable();
}

void
usb_request_bootloader(void)
{
    if (!CONFIG_SMOOTHIEWARE_BOOTLOADER)
        return;
    // Disable USB and pause for 5ms so host recognizes a disconnect
    irq_disable();
    sie_cmd_write(SIE_CMD_SET_DEVICE_STATUS, 0);
    udelay(5000);
    // The "LPC17xx-DFU-Bootloader" will enter the bootloader if the
    // watchdog timeout flag is set.
    LPC_WDT->WDMOD = 0x07;
    NVIC_SystemReset();
}

struct usb_string_descriptor *
usbserial_get_serialid(void)
{
   return &cdc_string_serial_chipid;
}

/****************************************************************
 * Setup and interrupts
 ****************************************************************/
static void
usb_set_serial(void)
{
    uint32_t iap_cmd_uid[5] = {IAP_CMD_READ_UID, 0, 0, 0, 0};
    uint32_t iap_resp[5];

    IAP iap_entry = (IAP)IAP_LOCATION;
    __disable_irq();
    iap_entry(iap_cmd_uid, iap_resp);
    __enable_irq();

    uint8_t *chipid = (uint8_t *)&iap_resp[1];
    uint8_t i, j, c;
    for (i = 0; i < IAP_UID_LEN; i++) {
        for (j = 0; j < 2; j++) {
            c = (chipid[i] >> 4*j) & 0xF;
            c = (c < 10) ? '0'+c : 'A'-10+c;
            cdc_string_serial_chipid.data[2*i+((j)?0:1)] = c;
        }
    }
}

void
USB_IRQHandler(void)
{
    uint32_t udis = LPC_USB->USBDevIntSt;
    if (udis & DEV_STAT) {
        LPC_USB->USBDevIntClr = DEV_STAT;
        // XXX - should handle reset and other states
    }
    if (udis & EP_SLOW) {
        uint32_t ueis = LPC_USB->USBEpIntSt;
        if (ueis & (1<<EP0OUT)) {
            sie_select_and_clear(EP0OUT);
            usb_notify_ep0();
        }
        if (ueis & (1<<EP0IN)) {
            sie_select_and_clear(EP0IN);
            usb_notify_ep0();
        }
        if (ueis & (1<<EP2OUT)) {
            sie_select_and_clear(EP2OUT);
            usb_notify_bulk_out();
        }
        if (ueis & (1<<EP5IN)) {
            sie_select_and_clear(EP5IN);
            usb_notify_bulk_in();
        }
        LPC_USB->USBDevIntClr = EP_SLOW;
    }
}

DECL_CONSTANT_STR("RESERVE_PINS_USB", "P0.30,P0.29,P2.9");

void
usbserial_init(void)
{
    if (CONFIG_USB_SERIAL_NUMBER_CHIPID)
        usb_set_serial();

    usb_irq_disable();
    // enable power
    enable_pclock(PCLK_USB);
    // enable clock
    LPC_USB->USBClkCtrl = 0x12;
    while (LPC_USB->USBClkSt != 0x12)
        ;
    // configure USBD-, USBD+, and USB Connect pins
    gpio_peripheral(GPIO(0, 30), 1, 0);
    gpio_peripheral(GPIO(0, 29), 1, 0);
    gpio_peripheral(GPIO(2, 9), 1, 0);
    // enforce a minimum time bus is disconnected before connecting
    udelay(5000);
    // setup endpoints
    realize_endpoint(EP0OUT, USB_CDC_EP0_SIZE);
    realize_endpoint(EP0IN, USB_CDC_EP0_SIZE);
    sie_cmd_write(SIE_CMD_SET_DEVICE_STATUS, 1);
    // enable irqs
    LPC_USB->USBDevIntEn = DEV_STAT | EP_SLOW;
    armcm_enable_irq(USB_IRQHandler, USB_IRQn, 1);
}
DECL_INIT(usbserial_init);

void
usbserial_shutdown(void)
{
    usb_irq_enable();
}
DECL_SHUTDOWN(usbserial_shutdown);