Skip to content

Commit c381d03

Browse files
committed
lpc176x: Initial support for serial over usb
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
1 parent c812a40 commit c381d03

8 files changed

Lines changed: 961 additions & 1 deletion

File tree

src/generic/usb_cdc.c

Lines changed: 453 additions & 0 deletions
Large diffs are not rendered by default.

src/generic/usb_cdc.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef __GENERIC_USB_CDC_H
2+
#define __GENERIC_USB_CDC_H
3+
4+
#include <stdint.h> // uint_fast8_t
5+
6+
enum {
7+
USB_CDC_EP0_SIZE = 16,
8+
9+
// XXX - endpoint ids may need to changed per-board
10+
USB_CDC_EP_ACM = 1,
11+
USB_CDC_EP_ACM_SIZE = 8,
12+
13+
USB_CDC_EP_BULK_OUT = 2,
14+
USB_CDC_EP_BULK_OUT_SIZE = 64,
15+
16+
USB_CDC_EP_BULK_IN = 5,
17+
USB_CDC_EP_BULK_IN_SIZE = 64,
18+
};
19+
20+
// callbacks provided by board specific code
21+
int_fast8_t usb_read_bulk_out(void *data, uint_fast8_t max_len);
22+
int_fast8_t usb_send_bulk_in(void *data, uint_fast8_t len);
23+
int_fast8_t usb_read_setup(void *data, uint_fast8_t max_len);
24+
int_fast8_t usb_send_setup(const void *data, uint_fast8_t len);
25+
void usb_send_pgm_setup(void *data, uint_fast8_t len);
26+
void usb_set_stall(void);
27+
void usb_set_address(uint_fast8_t addr);
28+
void usb_set_configure(void);
29+
30+
// usb_cdc.c
31+
void usb_notify_bulk_in(void);
32+
void usb_notify_bulk_out(void);
33+
void usb_notify_setup(void);
34+
35+
#endif // usb_cdc.h

src/generic/usbstd.h

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// Standard definitions for USB commands and data structures
2+
#ifndef __GENERIC_USBSTD_H
3+
#define __GENERIC_USBSTD_H
4+
5+
#include <stdint.h> // uint8_t
6+
#include "compiler.h" // PACKED
7+
8+
#define USB_DIR_OUT 0 /* to device */
9+
#define USB_DIR_IN 0x80 /* to host */
10+
11+
#define USB_REQ_GET_STATUS 0x00
12+
#define USB_REQ_CLEAR_FEATURE 0x01
13+
#define USB_REQ_SET_FEATURE 0x03
14+
#define USB_REQ_SET_ADDRESS 0x05
15+
#define USB_REQ_GET_DESCRIPTOR 0x06
16+
#define USB_REQ_SET_DESCRIPTOR 0x07
17+
#define USB_REQ_GET_CONFIGURATION 0x08
18+
#define USB_REQ_SET_CONFIGURATION 0x09
19+
#define USB_REQ_GET_INTERFACE 0x0A
20+
#define USB_REQ_SET_INTERFACE 0x0B
21+
#define USB_REQ_SYNCH_FRAME 0x0C
22+
23+
struct usb_ctrlrequest {
24+
uint8_t bRequestType;
25+
uint8_t bRequest;
26+
uint16_t wValue;
27+
uint16_t wIndex;
28+
uint16_t wLength;
29+
} PACKED;
30+
31+
#define USB_DT_DEVICE 0x01
32+
#define USB_DT_CONFIG 0x02
33+
#define USB_DT_STRING 0x03
34+
#define USB_DT_INTERFACE 0x04
35+
#define USB_DT_ENDPOINT 0x05
36+
#define USB_DT_DEVICE_QUALIFIER 0x06
37+
#define USB_DT_OTHER_SPEED_CONFIG 0x07
38+
#define USB_DT_ENDPOINT_COMPANION 0x30
39+
40+
struct usb_device_descriptor {
41+
uint8_t bLength;
42+
uint8_t bDescriptorType;
43+
44+
uint16_t bcdUSB;
45+
uint8_t bDeviceClass;
46+
uint8_t bDeviceSubClass;
47+
uint8_t bDeviceProtocol;
48+
uint8_t bMaxPacketSize0;
49+
uint16_t idVendor;
50+
uint16_t idProduct;
51+
uint16_t bcdDevice;
52+
uint8_t iManufacturer;
53+
uint8_t iProduct;
54+
uint8_t iSerialNumber;
55+
uint8_t bNumConfigurations;
56+
} PACKED;
57+
58+
#define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */
59+
#define USB_CLASS_AUDIO 1
60+
#define USB_CLASS_COMM 2
61+
#define USB_CLASS_HID 3
62+
#define USB_CLASS_PHYSICAL 5
63+
#define USB_CLASS_STILL_IMAGE 6
64+
#define USB_CLASS_PRINTER 7
65+
#define USB_CLASS_MASS_STORAGE 8
66+
#define USB_CLASS_HUB 9
67+
68+
struct usb_config_descriptor {
69+
uint8_t bLength;
70+
uint8_t bDescriptorType;
71+
72+
uint16_t wTotalLength;
73+
uint8_t bNumInterfaces;
74+
uint8_t bConfigurationValue;
75+
uint8_t iConfiguration;
76+
uint8_t bmAttributes;
77+
uint8_t bMaxPower;
78+
} PACKED;
79+
80+
struct usb_interface_descriptor {
81+
uint8_t bLength;
82+
uint8_t bDescriptorType;
83+
84+
uint8_t bInterfaceNumber;
85+
uint8_t bAlternateSetting;
86+
uint8_t bNumEndpoints;
87+
uint8_t bInterfaceClass;
88+
uint8_t bInterfaceSubClass;
89+
uint8_t bInterfaceProtocol;
90+
uint8_t iInterface;
91+
} PACKED;
92+
93+
struct usb_endpoint_descriptor {
94+
uint8_t bLength;
95+
uint8_t bDescriptorType;
96+
97+
uint8_t bEndpointAddress;
98+
uint8_t bmAttributes;
99+
uint16_t wMaxPacketSize;
100+
uint8_t bInterval;
101+
} PACKED;
102+
103+
#define USB_ENDPOINT_NUMBER_MASK 0x0f /* in bEndpointAddress */
104+
#define USB_ENDPOINT_DIR_MASK 0x80
105+
106+
#define USB_ENDPOINT_XFERTYPE_MASK 0x03 /* in bmAttributes */
107+
#define USB_ENDPOINT_XFER_CONTROL 0
108+
#define USB_ENDPOINT_XFER_ISOC 1
109+
#define USB_ENDPOINT_XFER_BULK 2
110+
#define USB_ENDPOINT_XFER_INT 3
111+
#define USB_ENDPOINT_MAX_ADJUSTABLE 0x80
112+
113+
struct usb_string_descriptor {
114+
uint8_t bLength;
115+
uint8_t bDescriptorType;
116+
uint16_t data[];
117+
} PACKED;
118+
119+
#define USB_LANGID_ENGLISH_US 0x0409
120+
121+
#endif // usbstd.h

src/generic/usbstd_cdc.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Standard definitions for USB CDC devices
2+
#ifndef __GENERIC_USBSTD_CDC_H
3+
#define __GENERIC_USBSTD_CDC_H
4+
5+
#define USB_CDC_SUBCLASS_ACM 0x02
6+
7+
#define USB_CDC_ACM_PROTO_AT_V25TER 1
8+
9+
struct usb_cdc_header_descriptor {
10+
uint8_t bLength;
11+
uint8_t bDescriptorType;
12+
uint8_t bDescriptorSubType;
13+
uint16_t bcdCDC;
14+
} PACKED;
15+
16+
#define USB_CDC_HEADER_TYPE 0x00
17+
#define USB_CDC_ACM_TYPE 0x02
18+
#define USB_CDC_UNION_TYPE 0x06
19+
20+
#define USB_CDC_CS_INTERFACE 0x24
21+
#define USB_CDC_CS_ENDPOINT 0x25
22+
23+
struct usb_cdc_acm_descriptor {
24+
uint8_t bLength;
25+
uint8_t bDescriptorType;
26+
uint8_t bDescriptorSubType;
27+
uint8_t bmCapabilities;
28+
} PACKED;
29+
30+
struct usb_cdc_union_descriptor {
31+
uint8_t bLength;
32+
uint8_t bDescriptorType;
33+
uint8_t bDescriptorSubType;
34+
uint8_t bMasterInterface0;
35+
uint8_t bSlaveInterface0;
36+
} PACKED;
37+
38+
#define USB_CDC_REQ_SET_LINE_CODING 0x20
39+
#define USB_CDC_REQ_GET_LINE_CODING 0x21
40+
#define USB_CDC_REQ_SET_CONTROL_LINE_STATE 0x22
41+
42+
struct usb_cdc_line_coding {
43+
uint32_t dwDTERate;
44+
uint8_t bCharFormat;
45+
uint8_t bParityType;
46+
uint8_t bDataBits;
47+
} PACKED;
48+
49+
#endif // usbstd_cdc.h

src/lpc176x/Kconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ config CLOCK_FREQ
2525
default 25000000 if MACH_LPC1768 # 100000000 / 4
2626
default 30000000 if MACH_LPC1769 # 120000000 / 4
2727

28+
config USBSERIAL
29+
bool "Use USB for communication (instead of serial)"
30+
default y
2831
config SERIAL
32+
depends on !USBSERIAL
2933
bool
3034
default y
3135
config SERIAL_BAUD

src/lpc176x/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ src-y += lpc176x/main.c lpc176x/timer.c lpc176x/gpio.c
1717
src-y += generic/crc16_ccitt.c generic/alloc.c
1818
src-y += generic/armcm_irq.c generic/timer_irq.c
1919
src-y += ../lib/lpc176x/device/system_LPC17xx.c
20+
src-$(CONFIG_USBSERIAL) += lpc176x/usbserial.c generic/usb_cdc.c
2021
src-$(CONFIG_SERIAL) += lpc176x/serial.c generic/serial_irq.c
2122

2223
# Add the TOOLCHAIN_GCC_ARM files to the build

src/lpc176x/timer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ timer_init(void)
3838
// Disable timer
3939
LPC_TIM0->TCR = 0x02;
4040
// Enable interrupts
41-
NVIC_SetPriority(TIMER0_IRQn, 1);
41+
NVIC_SetPriority(TIMER0_IRQn, 2);
4242
NVIC_EnableIRQ(TIMER0_IRQn);
4343
LPC_TIM0->MCR = 0x01;
4444
// Clear counter value

0 commit comments

Comments
 (0)