Skip to content

Commit

Permalink
lpc176x: Initial support for serial over usb
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
  • Loading branch information
KevinOConnor committed May 25, 2018
1 parent c812a40 commit c381d03
Show file tree
Hide file tree
Showing 8 changed files with 961 additions and 1 deletion.
453 changes: 453 additions & 0 deletions src/generic/usb_cdc.c

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions src/generic/usb_cdc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef __GENERIC_USB_CDC_H
#define __GENERIC_USB_CDC_H

#include <stdint.h> // uint_fast8_t

enum {
USB_CDC_EP0_SIZE = 16,

// XXX - endpoint ids may need to changed per-board
USB_CDC_EP_ACM = 1,
USB_CDC_EP_ACM_SIZE = 8,

USB_CDC_EP_BULK_OUT = 2,
USB_CDC_EP_BULK_OUT_SIZE = 64,

USB_CDC_EP_BULK_IN = 5,
USB_CDC_EP_BULK_IN_SIZE = 64,
};

// callbacks provided by board specific code
int_fast8_t usb_read_bulk_out(void *data, uint_fast8_t max_len);
int_fast8_t usb_send_bulk_in(void *data, uint_fast8_t len);
int_fast8_t usb_read_setup(void *data, uint_fast8_t max_len);
int_fast8_t usb_send_setup(const void *data, uint_fast8_t len);
void usb_send_pgm_setup(void *data, uint_fast8_t len);
void usb_set_stall(void);
void usb_set_address(uint_fast8_t addr);
void usb_set_configure(void);

// usb_cdc.c
void usb_notify_bulk_in(void);
void usb_notify_bulk_out(void);
void usb_notify_setup(void);

#endif // usb_cdc.h
121 changes: 121 additions & 0 deletions src/generic/usbstd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Standard definitions for USB commands and data structures
#ifndef __GENERIC_USBSTD_H
#define __GENERIC_USBSTD_H

#include <stdint.h> // uint8_t
#include "compiler.h" // PACKED

#define USB_DIR_OUT 0 /* to device */
#define USB_DIR_IN 0x80 /* to host */

#define USB_REQ_GET_STATUS 0x00
#define USB_REQ_CLEAR_FEATURE 0x01
#define USB_REQ_SET_FEATURE 0x03
#define USB_REQ_SET_ADDRESS 0x05
#define USB_REQ_GET_DESCRIPTOR 0x06
#define USB_REQ_SET_DESCRIPTOR 0x07
#define USB_REQ_GET_CONFIGURATION 0x08
#define USB_REQ_SET_CONFIGURATION 0x09
#define USB_REQ_GET_INTERFACE 0x0A
#define USB_REQ_SET_INTERFACE 0x0B
#define USB_REQ_SYNCH_FRAME 0x0C

struct usb_ctrlrequest {
uint8_t bRequestType;
uint8_t bRequest;
uint16_t wValue;
uint16_t wIndex;
uint16_t wLength;
} PACKED;

#define USB_DT_DEVICE 0x01
#define USB_DT_CONFIG 0x02
#define USB_DT_STRING 0x03
#define USB_DT_INTERFACE 0x04
#define USB_DT_ENDPOINT 0x05
#define USB_DT_DEVICE_QUALIFIER 0x06
#define USB_DT_OTHER_SPEED_CONFIG 0x07
#define USB_DT_ENDPOINT_COMPANION 0x30

struct usb_device_descriptor {
uint8_t bLength;
uint8_t bDescriptorType;

uint16_t bcdUSB;
uint8_t bDeviceClass;
uint8_t bDeviceSubClass;
uint8_t bDeviceProtocol;
uint8_t bMaxPacketSize0;
uint16_t idVendor;
uint16_t idProduct;
uint16_t bcdDevice;
uint8_t iManufacturer;
uint8_t iProduct;
uint8_t iSerialNumber;
uint8_t bNumConfigurations;
} PACKED;

#define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */
#define USB_CLASS_AUDIO 1
#define USB_CLASS_COMM 2
#define USB_CLASS_HID 3
#define USB_CLASS_PHYSICAL 5
#define USB_CLASS_STILL_IMAGE 6
#define USB_CLASS_PRINTER 7
#define USB_CLASS_MASS_STORAGE 8
#define USB_CLASS_HUB 9

struct usb_config_descriptor {
uint8_t bLength;
uint8_t bDescriptorType;

uint16_t wTotalLength;
uint8_t bNumInterfaces;
uint8_t bConfigurationValue;
uint8_t iConfiguration;
uint8_t bmAttributes;
uint8_t bMaxPower;
} PACKED;

struct usb_interface_descriptor {
uint8_t bLength;
uint8_t bDescriptorType;

uint8_t bInterfaceNumber;
uint8_t bAlternateSetting;
uint8_t bNumEndpoints;
uint8_t bInterfaceClass;
uint8_t bInterfaceSubClass;
uint8_t bInterfaceProtocol;
uint8_t iInterface;
} PACKED;

struct usb_endpoint_descriptor {
uint8_t bLength;
uint8_t bDescriptorType;

uint8_t bEndpointAddress;
uint8_t bmAttributes;
uint16_t wMaxPacketSize;
uint8_t bInterval;
} PACKED;

#define USB_ENDPOINT_NUMBER_MASK 0x0f /* in bEndpointAddress */
#define USB_ENDPOINT_DIR_MASK 0x80

#define USB_ENDPOINT_XFERTYPE_MASK 0x03 /* in bmAttributes */
#define USB_ENDPOINT_XFER_CONTROL 0
#define USB_ENDPOINT_XFER_ISOC 1
#define USB_ENDPOINT_XFER_BULK 2
#define USB_ENDPOINT_XFER_INT 3
#define USB_ENDPOINT_MAX_ADJUSTABLE 0x80

struct usb_string_descriptor {
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t data[];
} PACKED;

#define USB_LANGID_ENGLISH_US 0x0409

#endif // usbstd.h
49 changes: 49 additions & 0 deletions src/generic/usbstd_cdc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Standard definitions for USB CDC devices
#ifndef __GENERIC_USBSTD_CDC_H
#define __GENERIC_USBSTD_CDC_H

#define USB_CDC_SUBCLASS_ACM 0x02

#define USB_CDC_ACM_PROTO_AT_V25TER 1

struct usb_cdc_header_descriptor {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bDescriptorSubType;
uint16_t bcdCDC;
} PACKED;

#define USB_CDC_HEADER_TYPE 0x00
#define USB_CDC_ACM_TYPE 0x02
#define USB_CDC_UNION_TYPE 0x06

#define USB_CDC_CS_INTERFACE 0x24
#define USB_CDC_CS_ENDPOINT 0x25

struct usb_cdc_acm_descriptor {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bDescriptorSubType;
uint8_t bmCapabilities;
} PACKED;

struct usb_cdc_union_descriptor {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bDescriptorSubType;
uint8_t bMasterInterface0;
uint8_t bSlaveInterface0;
} PACKED;

#define USB_CDC_REQ_SET_LINE_CODING 0x20
#define USB_CDC_REQ_GET_LINE_CODING 0x21
#define USB_CDC_REQ_SET_CONTROL_LINE_STATE 0x22

struct usb_cdc_line_coding {
uint32_t dwDTERate;
uint8_t bCharFormat;
uint8_t bParityType;
uint8_t bDataBits;
} PACKED;

#endif // usbstd_cdc.h
4 changes: 4 additions & 0 deletions src/lpc176x/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ config CLOCK_FREQ
default 25000000 if MACH_LPC1768 # 100000000 / 4
default 30000000 if MACH_LPC1769 # 120000000 / 4

config USBSERIAL
bool "Use USB for communication (instead of serial)"
default y
config SERIAL
depends on !USBSERIAL
bool
default y
config SERIAL_BAUD
Expand Down
1 change: 1 addition & 0 deletions src/lpc176x/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ src-y += lpc176x/main.c lpc176x/timer.c lpc176x/gpio.c
src-y += generic/crc16_ccitt.c generic/alloc.c
src-y += generic/armcm_irq.c generic/timer_irq.c
src-y += ../lib/lpc176x/device/system_LPC17xx.c
src-$(CONFIG_USBSERIAL) += lpc176x/usbserial.c generic/usb_cdc.c
src-$(CONFIG_SERIAL) += lpc176x/serial.c generic/serial_irq.c

# Add the TOOLCHAIN_GCC_ARM files to the build
Expand Down
2 changes: 1 addition & 1 deletion src/lpc176x/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ timer_init(void)
// Disable timer
LPC_TIM0->TCR = 0x02;
// Enable interrupts
NVIC_SetPriority(TIMER0_IRQn, 1);
NVIC_SetPriority(TIMER0_IRQn, 2);
NVIC_EnableIRQ(TIMER0_IRQn);
LPC_TIM0->MCR = 0x01;
// Clear counter value
Expand Down
Loading

0 comments on commit c381d03

Please sign in to comment.