[feat] add usb driver and example

This commit is contained in:
zhji 2025-03-29 22:24:07 +08:00
parent ad3d3db97b
commit 6f891c902f
9 changed files with 982 additions and 0 deletions

150
driver/inc/reg/usb_reg.h Normal file
View File

@ -0,0 +1,150 @@
#ifndef __HARDWARE_USB_REG_H__
#define __HARDWARE_USB_REG_H__
#include "reg.h"
// Endpoint buffer control bits
#define USB_BUF_CTRL_FULL 0x00008000u
#define USB_BUF_CTRL_LAST 0x00004000u
#define USB_BUF_CTRL_DATA0_PID 0x00000000u
#define USB_BUF_CTRL_DATA1_PID 0x00002000u
#define USB_BUF_CTRL_SEL 0x00001000u
#define USB_BUF_CTRL_STALL 0x00000800u
#define USB_BUF_CTRL_AVAIL 0x00000400u
#define USB_BUF_CTRL_LEN_MASK 0x000003FFu
#define USB_BUF_CTRL_LEN_LSB 0
// ep_inout_ctrl bits
#define EP_CTRL_ENABLE_BITS (1u << 31u)
#define EP_CTRL_DOUBLE_BUFFERED_BITS (1u << 30)
#define EP_CTRL_INTERRUPT_PER_BUFFER (1u << 29)
#define EP_CTRL_INTERRUPT_PER_DOUBLE_BUFFER (1u << 28)
#define EP_CTRL_INTERRUPT_ON_NAK (1u << 16)
#define EP_CTRL_INTERRUPT_ON_STALL (1u << 17)
#define EP_CTRL_BUFFER_TYPE_LSB 26
#define EP_CTRL_HOST_INTERRUPT_INTERVAL_LSB 16
/* MAIN_CTRL Register @0x040 */
#define USB_MAIN_CTRL_CONTROLLER_EN_BITS 0x00000001
/* SIE_CTRL Register @0x04C */
#define USB_SIE_CTRL_PULLUP_EN_BITS 0x00010000
#define USB_SIE_CTRL_EP0_INT_1BUF_BITS 0x20000000
/* SIE_STATUS Register @0x050 */
#define USB_SIE_STATUS_SETUP_REC_BITS 0x00020000
#define USB_SIE_STATUS_BUS_RESET_BITS 0x00080000
/* USB_MUXING Register @0x074 */
#define USB_USB_MUXING_TO_PHY_BITS 0x00000001
#define USB_USB_MUXING_SOFTCON_BITS 0x00000008
/* USB_PWR Register @0x078 */
#define USB_USB_PWR_VBUS_DETECT_BITS 0x00000004
#define USB_USB_PWR_VBUS_DETECT_OVERRIDE_EN_BITS 0x00000008
/* INTR Register @0x08C, INTE Register @0x090, INTF Register @0x094, INTS Register @0x098 */
#define USB_INTS_BUFF_STATUS_BITS 0x00000010
#define USB_INTS_BUS_RESET_BITS 0x00001000
#define USB_INTS_SETUP_REQ_BITS 0x00010000
#define USB_NUM_ENDPOINTS (16)
#define USB_DPRAM_MAX (4096)
#define USB_HOST_INTERRUPT_ENDPOINTS (USB_NUM_ENDPOINTS - 1)
typedef struct {
// 4K of DPSRAM at beginning. Note this supports 8, 16, and 32 bit accesses
volatile uint8_t setup_packet[8]; // First 8 bytes are always for setup packets
// Starts at ep1
struct usb_device_dpram_ep_ctrl {
io_rw_32 in;
io_rw_32 out;
} ep_ctrl[USB_NUM_ENDPOINTS - 1];
// Starts at ep0
struct usb_device_dpram_ep_buf_ctrl {
io_rw_32 in;
io_rw_32 out;
} ep_buf_ctrl[USB_NUM_ENDPOINTS];
// EP0 buffers are fixed. Assumes single buffered mode for EP0
uint8_t ep0_buf_a[0x40];
uint8_t ep0_buf_b[0x40];
// Rest of DPRAM can be carved up as needed
uint8_t epx_data[USB_DPRAM_MAX - 0x180];
} usb_device_dpram_t;
typedef struct {
// 4K of DPSRAM at beginning. Note this supports 8, 16, and 32 bit accesses
volatile uint8_t setup_packet[8]; // First 8 bytes are always for setup packets
// Interrupt endpoint control 1 -> 15
struct usb_host_dpram_ep_ctrl {
io_rw_32 ctrl;
io_rw_32 spare;
} int_ep_ctrl[USB_HOST_INTERRUPT_ENDPOINTS];
io_rw_32 epx_buf_ctrl;
io_rw_32 _spare0;
// Interrupt endpoint buffer control
struct usb_host_dpram_ep_buf_ctrl {
io_rw_32 ctrl;
io_rw_32 spare;
} int_ep_buffer_ctrl[USB_HOST_INTERRUPT_ENDPOINTS];
io_rw_32 epx_ctrl;
uint8_t _spare1[124];
// Should start at 0x180
uint8_t epx_data[USB_DPRAM_MAX - 0x180];
} usb_host_dpram_t;
typedef struct {
io_rw_32 dev_addr_ctrl;
io_rw_32 int_ep_addr_ctrl[USB_HOST_INTERRUPT_ENDPOINTS];
io_rw_32 main_ctrl;
io_rw_32 sof_rw;
io_ro_32 sof_rd;
io_rw_32 sie_ctrl;
io_rw_32 sie_status;
io_rw_32 int_ep_ctrl;
io_rw_32 buf_status;
io_rw_32 buf_cpu_should_handle; // for double buff
io_rw_32 abort;
io_rw_32 abort_done;
io_rw_32 ep_stall_arm;
io_rw_32 nak_poll;
io_rw_32 ep_nak_stall_status;
io_rw_32 muxing;
io_rw_32 pwr;
io_rw_32 phy_direct;
io_rw_32 phy_direct_override;
io_rw_32 phy_trim;
io_rw_32 linestate_tuning;
io_rw_32 intr;
io_rw_32 inte;
io_rw_32 intf;
io_rw_32 ints;
} usb_hw_t;
#define usb_hw ((usb_hw_t *const)USBCTRL_REGS_BASE)
#define usb_hw_set ((usb_hw_t *const)hw_set_alias_untyped(usb_hw))
#define usb_hw_clear ((usb_hw_t *const)hw_clear_alias_untyped(usb_hw))
#define usb_dpram ((usb_device_dpram_t *)USBCTRL_DPRAM_BASE)
#define usbh_dpram ((usb_host_dpram_t *)USBCTRL_DPRAM_BASE)
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif /* __HARDWARE_USB_REG_H__ */

66
driver/inc/usb.h Normal file
View File

@ -0,0 +1,66 @@
#ifndef __HARDWARE_USB_H__
#define __HARDWARE_USB_H__
#include "usb_reg.h"
#include "usb_common.h"
typedef void (*usb_ep_handler)(uint8_t *buf, uint16_t len);
// Struct in which we keep the endpoint configuration
struct usb_endpoint_configuration {
const struct usb_endpoint_descriptor *descriptor;
usb_ep_handler handler;
// Pointers to endpoint + buffer control registers
// in the USB controller DPSRAM
volatile uint32_t *endpoint_control;
volatile uint32_t *buffer_control;
volatile uint8_t *data_buffer;
// Toggle after each packet (unless replying to a SETUP)
uint8_t next_pid;
};
// Struct in which we keep the device configuration
struct usb_device_configuration {
const struct usb_device_descriptor *device_descriptor;
const struct usb_interface_descriptor *interface_descriptor;
const struct usb_configuration_descriptor *config_descriptor;
const unsigned char *lang_descriptor;
const unsigned char **descriptor_strings;
// USB num endpoints is 16
struct usb_endpoint_configuration endpoints[USB_NUM_ENDPOINTS];
};
#ifdef __cplusplus
extern "C" {
#endif
#ifndef MIN
#define MIN(a,b) ((a)<(b)?(a):(b))
#endif
#ifndef MAX
#define MAX(a,b) ((a)>(b)?(a):(b))
#endif
/**
* @brief Given an endpoint configuration, returns true if the endpoint
* is transmitting data to the host (i.e. is an IN endpoint)
*
* @param ep, the endpoint configuration
* @return true
* @return false
*/
static inline int ep_is_tx(struct usb_endpoint_configuration *ep) {
return ep->descriptor->bEndpointAddress & USB_DIR_IN;
}
void usb_start_transfer(struct usb_endpoint_configuration *ep, uint8_t *buf, uint16_t len);
void usb_device_init(struct usb_device_configuration *dev_config);
struct usb_endpoint_configuration *usb_get_endpoint_configuration(uint8_t addr);
#ifdef __cplusplus
}
#endif
#endif /* __HARDWARE_USB_H__ */

120
driver/inc/usb_common.h Normal file
View File

@ -0,0 +1,120 @@
#ifndef __HARDWARE_USB_COMMON_H__
#define __HARDWARE_USB_COMMON_H__
#include "stdint.h"
#define USB_DIR_OUT (0x00u)
#define USB_DIR_IN (0x80u)
#define EP0_IN_ADDR (USB_DIR_IN | 0)
#define EP0_OUT_ADDR (USB_DIR_OUT | 0)
#define EP1_OUT_ADDR (USB_DIR_OUT | 1)
#define EP2_IN_ADDR (USB_DIR_IN | 2)
#define USB_TRANSFER_TYPE_CONTROL (0x0)
#define USB_TRANSFER_TYPE_ISOCHRONOUS (0x1)
#define USB_TRANSFER_TYPE_BULK (0x2)
#define USB_TRANSFER_TYPE_INTERRUPT (0x3)
#define USB_TRANSFER_TYPE_BITS (0x3)
// Descriptor types
#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_REQUEST_GET_STATUS 0x0
#define USB_REQUEST_CLEAR_FEATURE 0x01
#define USB_REQUEST_SET_FEATURE 0x03
#define USB_REQUEST_SET_ADDRESS 0x05
#define USB_REQUEST_GET_DESCRIPTOR 0x06
#define USB_REQUEST_SET_DESCRIPTOR 0x07
#define USB_REQUEST_GET_CONFIGURATION 0x08
#define USB_REQUEST_SET_CONFIGURATION 0x09
#define USB_REQUEST_GET_INTERFACE 0x0a
#define USB_REQUEST_SET_INTERFACE 0x0b
#define USB_REQUEST_SYNC_FRAME 0x0c
struct usb_setup_packet {
uint8_t bmRequestType;
uint8_t bRequest;
uint16_t wValue;
uint16_t wIndex;
uint16_t wLength;
} __attribute__((packed));
struct usb_descriptor {
uint8_t bLength;
uint8_t bDescriptorType;
};
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;
} __attribute__((packed));
struct usb_configuration_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;
} __attribute__((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;
} __attribute__((packed));
struct usb_endpoint_descriptor {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bEndpointAddress;
uint8_t bmAttributes;
uint16_t wMaxPacketSize;
uint8_t bInterval;
} __attribute__((packed));
struct usb_endpoint_descriptor_long {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bEndpointAddress;
uint8_t bmAttributes;
uint16_t wMaxPacketSize;
uint8_t bInterval;
uint8_t bRefresh;
uint8_t bSyncAddr;
} __attribute__((packed));
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif /* __HARDWARE_USB_COMMON_H__ */

434
driver/src/usb.c Normal file
View File

@ -0,0 +1,434 @@
#include "usb.h"
#include "resets.h"
#include "irq.h"
#include "string.h"
#include "stdio.h"
static struct usb_device_configuration *dev_config;
static int should_set_address = 0;
static uint8_t dev_addr = 0;
static volatile int configured = 0;
// Global data buffer for EP0
static uint8_t ep0_buf[64];
/**
* @brief Take a buffer pointer located in the USB RAM and return as an offset of the RAM.
*
* @param buf
* @return uint32_t
*/
static inline uint32_t usb_buffer_offset(volatile uint8_t *buf) {
return (uint32_t) buf ^ (uint32_t) usb_dpram;
}
/**
* @brief Set up the endpoint control register for an endpoint (if applicable. Not valid for EP0).
*
* @param ep
*/
void usb_setup_endpoint(const struct usb_endpoint_configuration *ep)
{
printf("Set up endpoint 0x%x with buffer address 0x%p\n", ep->descriptor->bEndpointAddress, ep->data_buffer);
// EP0 doesn't have one so return if that is the case
if (!ep->endpoint_control) {
return;
}
// Get the data buffer as an offset of the USB controller's DPRAM
uint32_t dpram_offset = usb_buffer_offset(ep->data_buffer);
uint32_t reg = EP_CTRL_ENABLE_BITS
| EP_CTRL_INTERRUPT_PER_BUFFER
| (ep->descriptor->bmAttributes << EP_CTRL_BUFFER_TYPE_LSB)
| dpram_offset;
*ep->endpoint_control = reg;
}
/**
* @brief Set up the endpoint control register for each endpoint.
*
*/
void usb_setup_endpoints(void)
{
const struct usb_endpoint_configuration *endpoints = dev_config->endpoints;
for (int i = 0; i < USB_NUM_ENDPOINTS; i++) {
if (endpoints[i].descriptor && endpoints[i].handler) {
usb_setup_endpoint(&endpoints[i]);
}
}
}
struct usb_endpoint_configuration *usb_get_endpoint_configuration(uint8_t addr)
{
struct usb_endpoint_configuration *endpoints = dev_config->endpoints;
for (int i = 0; i < USB_NUM_ENDPOINTS; i++) {
if (endpoints[i].descriptor && (endpoints[i].descriptor->bEndpointAddress == addr)) {
return &endpoints[i];
}
}
return NULL;
}
/**
* @brief Given a C string, fill the EP0 data buf with a USB string descriptor for that string.
*
* @param C string you would like to send to the USB host
* @return the length of the string descriptor in EP0 buf
*/
uint8_t usb_prepare_string_descriptor(const unsigned char *str)
{
// 2 for bLength + bDescriptorType + strlen * 2 because string is unicode. i.e. other byte will be 0
uint8_t bLength = 2 + (strlen((const char *)str) * 2);
static const uint8_t bDescriptorType = 0x03;
volatile uint8_t *buf = &ep0_buf[0];
*buf++ = bLength;
*buf++ = bDescriptorType;
uint8_t c;
do {
c = *str++;
*buf++ = c;
*buf++ = 0;
} while (c != '\0');
return bLength;
}
/**
* @brief Starts a transfer on a given endpoint.
*
* @param ep, the endpoint configuration.
* @param buf, the data buffer to send. Only applicable if the endpoint is TX
* @param len, the length of the data in buf (this example limits max len to one packet - 64 bytes)
*/
void usb_start_transfer(struct usb_endpoint_configuration *ep, uint8_t *buf, uint16_t len)
{
// We are asserting that the length is <= 64 bytes for simplicity of the example.
// For multi packet transfers see the tinyusb port.
while(!(len <= 64));
printf("Start transfer of len %d on ep addr 0x%x\n", len, ep->descriptor->bEndpointAddress);
// Prepare buffer control register value
uint32_t val = len | USB_BUF_CTRL_AVAIL;
if (ep_is_tx(ep)) {
// Need to copy the data from the user buffer to the usb memory
memcpy((void *) ep->data_buffer, (void *) buf, len);
// Mark as full
val |= USB_BUF_CTRL_FULL;
}
// Set pid and flip for next transfer
val |= ep->next_pid ? USB_BUF_CTRL_DATA1_PID : USB_BUF_CTRL_DATA0_PID;
ep->next_pid ^= 1u;
*ep->buffer_control = val;
}
/**
* @brief Send device descriptor to host
*
*/
void usb_handle_device_descriptor(volatile struct usb_setup_packet *pkt) {
const struct usb_device_descriptor *d = dev_config->device_descriptor;
// EP0 in
struct usb_endpoint_configuration *ep = usb_get_endpoint_configuration(EP0_IN_ADDR);
// Always respond with pid 1
ep->next_pid = 1;
usb_start_transfer(ep, (uint8_t *) d, MIN(sizeof(struct usb_device_descriptor), pkt->wLength));
}
/**
* @brief Send the configuration descriptor (and potentially the configuration and endpoint descriptors) to the host.
*
* @param pkt, the setup packet received from the host.
*/
void usb_handle_config_descriptor(volatile struct usb_setup_packet *pkt) {
uint8_t *buf = &ep0_buf[0];
// First request will want just the config descriptor
const struct usb_configuration_descriptor *d = dev_config->config_descriptor;
memcpy((void *) buf, d, sizeof(struct usb_configuration_descriptor));
buf += sizeof(struct usb_configuration_descriptor);
// If we more than just the config descriptor copy it all
if (pkt->wLength >= d->wTotalLength) {
memcpy((void *) buf, dev_config->interface_descriptor, sizeof(struct usb_interface_descriptor));
buf += sizeof(struct usb_interface_descriptor);
const struct usb_endpoint_configuration *ep = dev_config->endpoints;
// Copy all the endpoint descriptors starting from EP1
for (int i = 2; i < USB_NUM_ENDPOINTS; i++) {
if (ep[i].descriptor) {
memcpy((void *) buf, ep[i].descriptor, sizeof(struct usb_endpoint_descriptor));
buf += sizeof(struct usb_endpoint_descriptor);
}
}
}
// Send data
// Get len by working out end of buffer subtract start of buffer
uint32_t len = (uint32_t) buf - (uint32_t) &ep0_buf[0];
usb_start_transfer(usb_get_endpoint_configuration(EP0_IN_ADDR), &ep0_buf[0], MIN(len, pkt->wLength));
}
/**
* @brief Handle a BUS RESET from the host by setting the device address back to 0.
*
*/
void usb_bus_reset(void) {
// Set address back to 0
dev_addr = 0;
should_set_address = 0;
usb_hw->dev_addr_ctrl = 0;
configured = 0;
}
/**
* @brief Send the requested string descriptor to the host.
*
* @param pkt, the setup packet from the host.
*/
void usb_handle_string_descriptor(volatile struct usb_setup_packet *pkt) {
uint8_t i = pkt->wValue & 0xff;
uint8_t len = 0;
if (i == 0) {
len = 4;
memcpy(&ep0_buf[0], dev_config->lang_descriptor, len);
} else {
// Prepare fills in ep0_buf
len = usb_prepare_string_descriptor(dev_config->descriptor_strings[i - 1]);
}
usb_start_transfer(usb_get_endpoint_configuration(EP0_IN_ADDR), &ep0_buf[0], MIN(len, pkt->wLength));
}
/**
* @brief Sends a zero length status packet back to the host.
*/
void usb_acknowledge_out_request(void)
{
usb_start_transfer(usb_get_endpoint_configuration(EP0_IN_ADDR), NULL, 0);
}
/**
* @brief Handles a SET_ADDR request from the host. The actual setting of the device address in
* hardware is done in ep0_in_handler. This is because we have to acknowledge the request first
* as a device with address zero.
*
* @param pkt, the setup packet from the host.
*/
void usb_set_device_address(volatile struct usb_setup_packet *pkt)
{
// Set address is a bit of a strange case because we have to send a 0 length status packet first with
// address 0
dev_addr = (pkt->wValue & 0xff);
printf("Set address %d\r\n", dev_addr);
// Will set address in the callback phase
should_set_address = 1;
usb_acknowledge_out_request();
}
/**
* @brief Handles a SET_CONFIGRUATION request from the host. Assumes one configuration so simply
* sends a zero length status packet back to the host.
*
* @param pkt, the setup packet from the host.
*/
void usb_set_device_configuration(volatile struct usb_setup_packet *pkt)
{
// Only one configuration so just acknowledge the request
printf("Device Enumerated\r\n");
usb_acknowledge_out_request();
configured = 1;
}
/**
* @brief Respond to a setup packet from the host.
*
*/
void usb_handle_setup_packet(void)
{
volatile struct usb_setup_packet *pkt = (volatile struct usb_setup_packet *) &usb_dpram->setup_packet;
uint8_t req_direction = pkt->bmRequestType;
uint8_t req = pkt->bRequest;
// Reset PID to 1 for EP0 IN
usb_get_endpoint_configuration(EP0_IN_ADDR)->next_pid = 1u;
if (req_direction == USB_DIR_OUT) {
if (req == USB_REQUEST_SET_ADDRESS) {
usb_set_device_address(pkt);
} else if (req == USB_REQUEST_SET_CONFIGURATION) {
usb_set_device_configuration(pkt);
} else {
usb_acknowledge_out_request();
printf("Other OUT request (0x%x)\r\n", pkt->bRequest);
}
} else if (req_direction == USB_DIR_IN) {
if (req == USB_REQUEST_GET_DESCRIPTOR) {
uint16_t descriptor_type = pkt->wValue >> 8;
switch (descriptor_type) {
case USB_DT_DEVICE:
usb_handle_device_descriptor(pkt);
printf("GET DEVICE DESCRIPTOR\r\n");
break;
case USB_DT_CONFIG:
usb_handle_config_descriptor(pkt);
printf("GET CONFIG DESCRIPTOR\r\n");
break;
case USB_DT_STRING:
usb_handle_string_descriptor(pkt);
printf("GET STRING DESCRIPTOR\r\n");
break;
default:
printf("Unhandled GET_DESCRIPTOR type 0x%x\r\n", descriptor_type);
}
} else {
printf("Other IN request (0x%x)\r\n", pkt->bRequest);
}
}
}
/**
* @brief Notify an endpoint that a transfer has completed.
*
* @param ep, the endpoint to notify.
*/
static void usb_handle_ep_buff_done(struct usb_endpoint_configuration *ep) {
uint32_t buffer_control = *ep->buffer_control;
// Get the transfer length for this endpoint
uint16_t len = buffer_control & USB_BUF_CTRL_LEN_MASK;
// Call that endpoints buffer done handler
ep->handler((uint8_t *) ep->data_buffer, len);
}
/**
* @brief Find the endpoint configuration for a specified endpoint number and
* direction and notify it that a transfer has completed.
*
* @param ep_num
* @param in
*/
static void usb_handle_buff_done(uint8_t ep_num, int in)
{
uint8_t ep_addr = ep_num | (in ? USB_DIR_IN : 0);
printf("EP %d (in = %d) done\n", ep_num, in);
for (int i = 0; i < USB_NUM_ENDPOINTS; i++) {
struct usb_endpoint_configuration *ep = &dev_config->endpoints[i];
if (ep->descriptor && ep->handler) {
if (ep->descriptor->bEndpointAddress == ep_addr) {
usb_handle_ep_buff_done(ep);
return;
}
}
}
}
/**
* @brief Handle a "buffer status" irq. This means that one or more
* buffers have been sent / received. Notify each endpoint where this
* is the case.
*/
static void usb_handle_buff_status()
{
uint32_t buffers = usb_hw->buf_status;
uint32_t remaining_buffers = buffers;
uint32_t bit = 1u;
for (uint32_t i = 0; remaining_buffers && i < USB_NUM_ENDPOINTS * 2; i++) {
if (remaining_buffers & bit) {
// clear this in advance
usb_hw_clear->buf_status = bit;
// IN transfer for even i, OUT transfer for odd i
usb_handle_buff_done(i >> 1u, !(i & 1u));
remaining_buffers &= ~bit;
}
bit <<= 1u;
}
}
void isr_usbctrl(void)
{
// USB interrupt handler
uint32_t status = usb_hw->ints;
uint32_t handled = 0;
// Setup packet received
if (status & USB_INTS_SETUP_REQ_BITS) {
handled |= USB_INTS_SETUP_REQ_BITS;
usb_hw_clear->sie_status = USB_SIE_STATUS_SETUP_REC_BITS;
usb_handle_setup_packet();
}
// Buffer status, one or more buffers have completed
if (status & USB_INTS_BUFF_STATUS_BITS) {
handled |= USB_INTS_BUFF_STATUS_BITS;
usb_handle_buff_status();
}
// Bus is reset
if (status & USB_INTS_BUS_RESET_BITS) {
printf("BUS RESET\n");
handled |= USB_INTS_BUS_RESET_BITS;
usb_hw_clear->sie_status = USB_SIE_STATUS_BUS_RESET_BITS;
usb_bus_reset();
}
if (status ^ handled) {
printf("Unhandled IRQ 0x%lx\n", (uint32_t) (status ^ handled));
}
}
void usb_device_init(struct usb_device_configuration *config)
{
/* reset usb controller */
reset_unreset_blocks_wait(RESETS_BLOCK_USBCTRL);
/* clear any previous state in dpram just in case */
memset(usb_dpram, 0, sizeof(*usb_dpram));
/* enable USB interrupt at processor */
irq_attach(USBCTRL_IRQ, isr_usbctrl);
irq_enable(USBCTRL_IRQ);
/* mux the controller to the onboard usb phy */
usb_hw->muxing = USB_USB_MUXING_TO_PHY_BITS | USB_USB_MUXING_SOFTCON_BITS;
/* force VBUS detect so the device thinks it is plugged into a host */
usb_hw->pwr = USB_USB_PWR_VBUS_DETECT_BITS | USB_USB_PWR_VBUS_DETECT_OVERRIDE_EN_BITS;
/* enable the USB controller in device mode */
usb_hw->main_ctrl = USB_MAIN_CTRL_CONTROLLER_EN_BITS;
/* enable an interrupt per EP0 transaction */
usb_hw->sie_ctrl = USB_SIE_CTRL_EP0_INT_1BUF_BITS;
/* enable interrupts for when a buffer is done, when the bus is reset, and when a setup packet is received */
usb_hw->inte = USB_INTS_BUFF_STATUS_BITS | USB_INTS_BUS_RESET_BITS | USB_INTS_SETUP_REQ_BITS;
/* set up endpoints (endpoint control registers) described by device configuration */
dev_config = config;
usb_setup_endpoints();
/* present full speed device by enabling pull up on DP */
usb_hw_set->sie_ctrl = USB_SIE_CTRL_PULLUP_EN_BITS;
}
void ep0_in_handler(uint8_t *buf, uint16_t len)
{
if (should_set_address) {
// Set actual device address in hardware
usb_hw->dev_addr_ctrl = dev_addr;
should_set_address = 0;
} else {
// Receive a zero length status packet from the host on EP0 OUT
struct usb_endpoint_configuration *ep = usb_get_endpoint_configuration(EP0_OUT_ADDR);
usb_start_transfer(ep, NULL, 0);
}
}
void ep0_out_handler(uint8_t *buf, uint16_t len)
{
}

View File

@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.10)
include(proj.conf)
project(${EXAMPLE_NAME} VERSION 0.1)
add_executable(${EXAMPLE_NAME}.elf main.c)
target_sources(${EXAMPLE_NAME}.elf PUBLIC dev_lowlevel/dev_lowlevel.c)
add_subdirectory(${SDK_BASE_DIR} sdk)
target_link_libraries(${EXAMPLE_NAME}.elf sdk)

View File

@ -0,0 +1,18 @@
EXAMPLE_BASE_DIR ?= $(shell realpath .)
EXAMPLE_NAME := $(notdir $(patsubst %/,%,$(CURDIR)))
SDK_BASE_DIR ?= $(shell realpath ./../../../..)
export SDK_BASE_DIR
export EXAMPLE_NAME
export EXAMPLE_BASE_DIR
GCC_PATH := $(shell which arm-none-eabi-gcc)
CROSS_COMPILE := $(patsubst %gcc,%,$(GCC_PATH))
ifeq ($(GCC_PATH),)
$(error arm-none-eabi-gcc not found in PATH. Please install the ARM toolchain.)
endif
# add custom cmake definition
#cmake_definition+=-Dxxx=sss
include $(SDK_BASE_DIR)/project.build

View File

@ -0,0 +1,181 @@
#include "system.h"
#include "resets.h"
#include "gpio.h"
#include "timer.h"
#include "usb.h"
#include "stdio.h"
extern struct usb_device_configuration dev_config;
extern void ep0_in_handler(uint8_t *buf, uint16_t len);
extern void ep0_out_handler(uint8_t *buf, uint16_t len);
void ep1_out_handler(uint8_t *buf, uint16_t len)
{
printf("RX %d bytes from host\n", len);
// Send data back to host
struct usb_endpoint_configuration *ep = usb_get_endpoint_configuration(EP2_IN_ADDR);
usb_start_transfer(ep, buf, len);
}
void ep2_in_handler(uint8_t *buf, uint16_t len)
{
printf("Sent %d bytes to host\n", len);
// Get ready to rx again from host
usb_start_transfer(usb_get_endpoint_configuration(EP1_OUT_ADDR), NULL, 64);
}
static const struct usb_endpoint_descriptor ep0_out = {
.bLength = sizeof(struct usb_endpoint_descriptor),
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = EP0_OUT_ADDR, // EP number 0, OUT from host (rx to device)
.bmAttributes = USB_TRANSFER_TYPE_CONTROL,
.wMaxPacketSize = 64,
.bInterval = 0
};
static const struct usb_endpoint_descriptor ep0_in = {
.bLength = sizeof(struct usb_endpoint_descriptor),
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = EP0_IN_ADDR, // EP number 0, OUT from host (rx to device)
.bmAttributes = USB_TRANSFER_TYPE_CONTROL,
.wMaxPacketSize = 64,
.bInterval = 0
};
static const struct usb_endpoint_descriptor ep1_out = {
.bLength = sizeof(struct usb_endpoint_descriptor),
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = EP1_OUT_ADDR, // EP number 1, OUT from host (rx to device)
.bmAttributes = USB_TRANSFER_TYPE_BULK,
.wMaxPacketSize = 64,
.bInterval = 0
};
static const struct usb_endpoint_descriptor ep2_in = {
.bLength = sizeof(struct usb_endpoint_descriptor),
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = EP2_IN_ADDR, // EP number 2, IN from host (tx from device)
.bmAttributes = USB_TRANSFER_TYPE_BULK,
.wMaxPacketSize = 64,
.bInterval = 0
};
// Descriptors
static const struct usb_device_descriptor device_descriptor = {
.bLength = sizeof(struct usb_device_descriptor),
.bDescriptorType = USB_DT_DEVICE,
.bcdUSB = 0x0110, // USB 1.1 device
.bDeviceClass = 0, // Specified in interface descriptor
.bDeviceSubClass = 0, // No subclass
.bDeviceProtocol = 0, // No protocol
.bMaxPacketSize0 = 64, // Max packet size for ep0
.idVendor = 0x0000, // Your vendor id
.idProduct = 0x0001, // Your product ID
.bcdDevice = 0, // No device revision number
.iManufacturer = 1, // Manufacturer string index
.iProduct = 2, // Product string index
.iSerialNumber = 0, // No serial number
.bNumConfigurations = 1, // One configuration
};
static const struct usb_interface_descriptor interface_descriptor = {
.bLength = sizeof(struct usb_interface_descriptor),
.bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 0,
.bAlternateSetting = 0,
.bNumEndpoints = 2, // Interface has 2 endpoints
.bInterfaceClass = 0xff, // Vendor specific endpoint
.bInterfaceSubClass = 0,
.bInterfaceProtocol = 0,
.iInterface = 0,
};
static const struct usb_configuration_descriptor config_descriptor = {
.bLength = sizeof(struct usb_configuration_descriptor),
.bDescriptorType = USB_DT_CONFIG,
.wTotalLength = (sizeof(config_descriptor) +
sizeof(interface_descriptor) +
sizeof(ep1_out) +
sizeof(ep2_in)),
.bNumInterfaces = 1,
.bConfigurationValue = 1, // Configuration 1
.iConfiguration = 0, // No string
.bmAttributes = 0xc0, // attributes: self powered, no remote wakeup
.bMaxPower = 0x32 // 100ma
};
static const unsigned char lang_descriptor[] = {
4, // bLength
0x03, // bDescriptorType == String Descriptor
0x09, 0x04 // language id = us english
};
static const unsigned char *descriptor_strings[] = {
(unsigned char *) "Raspberry Pi", // Vendor
(unsigned char *) "Pico Test Device" // Product
};
// Struct defining the device configuration
struct usb_device_configuration dev_config = {
.device_descriptor = &device_descriptor,
.interface_descriptor = &interface_descriptor,
.config_descriptor = &config_descriptor,
.lang_descriptor = lang_descriptor,
.descriptor_strings = descriptor_strings,
.endpoints = {
{
.descriptor = &ep0_out,
.handler = &ep0_out_handler,
.endpoint_control = NULL, // NA for EP0
.buffer_control = &usb_dpram->ep_buf_ctrl[0].out,
// EP0 in and out share a data buffer
.data_buffer = &usb_dpram->ep0_buf_a[0],
},
{
.descriptor = &ep0_in,
.handler = &ep0_in_handler,
.endpoint_control = NULL, // NA for EP0,
.buffer_control = &usb_dpram->ep_buf_ctrl[0].in,
// EP0 in and out share a data buffer
.data_buffer = &usb_dpram->ep0_buf_a[0],
},
{
.descriptor = &ep1_out,
.handler = &ep1_out_handler,
// EP1 starts at offset 0 for endpoint control
.endpoint_control = &usb_dpram->ep_ctrl[0].out,
.buffer_control = &usb_dpram->ep_buf_ctrl[1].out,
// First free EPX buffer
.data_buffer = &usb_dpram->epx_data[0 * 64],
},
{
.descriptor = &ep2_in,
.handler = &ep2_in_handler,
.endpoint_control = &usb_dpram->ep_ctrl[1].in,
.buffer_control = &usb_dpram->ep_buf_ctrl[2].in,
// Second free EPX buffer
.data_buffer = &usb_dpram->epx_data[1 * 64],
}
}
};
int main(void)
{
uint64_t time_start, time_stop;
system_init();
usb_device_init(&dev_config);
time_start = timer_count_read();
while (1) {
time_stop = timer_count_read();
if (time_stop - time_start > 3000 * 1000) {
usb_hw_clear->sie_ctrl = USB_SIE_CTRL_PULLUP_EN_BITS;
reset_unreset_blocks_wait(RESETS_BLOCK_USBCTRL);
printf("time 1s, stop USB\r\n");
break;
}
}
return 0;
}

View File

@ -0,0 +1 @@
# set(CONFIG_COMPONENT1 1)