USB: Cosmetics and coding-style fixes.

This commit is contained in:
Uwe Hermann 2010-12-29 18:00:32 +01:00
parent c39eb69e4d
commit b5727a6c73
10 changed files with 298 additions and 303 deletions

View File

@ -373,4 +373,3 @@ void adc_set_injected_sequence(u32 adc, u8 length, u8 channel[])
ADC_JSQR(adc) = reg32; ADC_JSQR(adc) = reg32;
} }

View File

@ -541,5 +541,3 @@ void dma_set_number_of_data(u32 dma, u8 channel, u16 number)
DMA_CNDTR7(dma) = number; DMA_CNDTR7(dma) = number;
} }
} }

View File

@ -28,4 +28,3 @@ void scb_reset_system(void)
{ {
SCB_AIRCR = SCB_AIRCR_VECTKEY | SCB_AIRCR_SYSRESETREQ; SCB_AIRCR = SCB_AIRCR_VECTKEY | SCB_AIRCR_SYSRESETREQ;
} }

View File

@ -288,4 +288,3 @@ void spi_disable_rx_dma(u32 spi)
{ {
SPI_CR2(spi) &= ~SPI_CR2_RXDMAEN; SPI_CR2(spi) &= ~SPI_CR2_RXDMAEN;
} }

View File

@ -62,5 +62,3 @@ u8 systick_get_countflag(void)
else else
return 0; return 0;
} }

View File

@ -25,24 +25,25 @@ struct _usbd_device _usbd_device;
u8 usbd_control_buffer[128] __attribute__((weak)); u8 usbd_control_buffer[128] __attribute__((weak));
/** Main initialization entry point. /**
* Main initialization entry point.
* *
* Initialize the USB firmware library to implement the USB device described * Initialize the USB firmware library to implement the USB device described
* by the descriptors provided. * by the descriptors provided.
* *
* It is required that the 48MHz USB clock is already available. * It is required that the 48MHz USB clock is already available.
* *
* @param dev Pointer to USB Device descriptor. This must not be changed * @param dev Pointer to USB device descriptor. This must not be changed while
* while the device is in use. * the device is in use.
* @param conf Pointer to array of USB Configuration descriptors. These * @param conf Pointer to array of USB configuration descriptors. These must
* must not be changed while the device is in use. The length * not be changed while the device is in use. The length of this
* of this array is determined by the bNumConfigurations field * array is determined by the bNumConfigurations field in the
* in the device descriptor. * device descriptor.
* @return Zero on success (currently cannot fail) * @param strings TODO
* @return Zero on success (currently cannot fail).
*/ */
int usbd_init(const struct usb_device_descriptor *dev, int usbd_init(const struct usb_device_descriptor *dev,
const struct usb_config_descriptor *conf, const struct usb_config_descriptor *conf, const char **strings)
const char **strings)
{ {
_usbd_device.desc = dev; _usbd_device.desc = dev;
_usbd_device.config = conf; _usbd_device.config = conf;
@ -52,12 +53,12 @@ int usbd_init(const struct usb_device_descriptor *dev,
_usbd_hw_init(); _usbd_hw_init();
_usbd_device.user_callback_ctr[0][USB_TRANSACTION_SETUP] = _usbd_device.user_callback_ctr[0][USB_TRANSACTION_SETUP] =
_usbd_control_setup; _usbd_control_setup;
_usbd_device.user_callback_ctr[0][USB_TRANSACTION_OUT] = _usbd_device.user_callback_ctr[0][USB_TRANSACTION_OUT] =
_usbd_control_out; _usbd_control_out;
_usbd_device.user_callback_ctr[0][USB_TRANSACTION_IN] = _usbd_device.user_callback_ctr[0][USB_TRANSACTION_IN] =
_usbd_control_in; _usbd_control_in;
return 0; return 0;
} }
@ -89,6 +90,6 @@ void _usbd_reset(void)
usbd_ep_setup(0, USB_ENDPOINT_ATTR_CONTROL, 64, NULL); usbd_ep_setup(0, USB_ENDPOINT_ATTR_CONTROL, 64, NULL);
_usbd_hw_set_address(0); _usbd_hw_set_address(0);
if(_usbd_device.user_callback_reset) if (_usbd_device.user_callback_reset)
_usbd_device.user_callback_reset(); _usbd_device.user_callback_reset();
} }

View File

@ -22,27 +22,25 @@
#include "usb_private.h" #include "usb_private.h"
static struct usb_control_state { static struct usb_control_state {
enum { IDLE, STALLED, enum {
DATA_IN, LAST_DATA_IN, STATUS_IN, IDLE, STALLED,
DATA_OUT, LAST_DATA_OUT, STATUS_OUT DATA_IN, LAST_DATA_IN, STATUS_IN,
DATA_OUT, LAST_DATA_OUT, STATUS_OUT,
} state; } state;
struct usb_setup_data req; struct usb_setup_data req;
u8 *ctrl_buf; u8 *ctrl_buf;
u16 ctrl_len; u16 ctrl_len;
void (*complete)(struct usb_setup_data *req); void (*complete)(struct usb_setup_data *req);
} control_state; } control_state;
/** Register application callback function for handling of usb control /* Register application callback function for handling USB control requests. */
* request. */
int usbd_register_control_callback(u8 type, u8 type_mask, int usbd_register_control_callback(u8 type, u8 type_mask,
usbd_control_callback callback) usbd_control_callback callback)
{ {
int i; int i;
for(i = 0; i < MAX_USER_CONTROL_CALLBACK; i++) {
if(_usbd_device.user_control_callback[i].cb) for (i = 0; i < MAX_USER_CONTROL_CALLBACK; i++) {
if (_usbd_device.user_control_callback[i].cb)
continue; continue;
_usbd_device.user_control_callback[i].type = type; _usbd_device.user_control_callback[i].type = type;
@ -50,25 +48,24 @@ int usbd_register_control_callback(u8 type, u8 type_mask,
_usbd_device.user_control_callback[i].cb = callback; _usbd_device.user_control_callback[i].cb = callback;
return 0; return 0;
} }
return -1; return -1;
} }
static void usb_control_send_chunk(void) static void usb_control_send_chunk(void)
{ {
if(_usbd_device.desc->bMaxPacketSize0 < control_state.ctrl_len) { if (_usbd_device.desc->bMaxPacketSize0 < control_state.ctrl_len) {
/* Data stage, normal transmission */ /* Data stage, normal transmission */
usbd_ep_write_packet(0, control_state.ctrl_buf, usbd_ep_write_packet(0, control_state.ctrl_buf,
_usbd_device.desc->bMaxPacketSize0); _usbd_device.desc->bMaxPacketSize0);
control_state.state = DATA_IN; control_state.state = DATA_IN;
control_state.ctrl_buf += _usbd_device.desc->bMaxPacketSize0; control_state.ctrl_buf += _usbd_device.desc->bMaxPacketSize0;
control_state.ctrl_len -= _usbd_device.desc->bMaxPacketSize0; control_state.ctrl_len -= _usbd_device.desc->bMaxPacketSize0;
} else { } else {
/* Data stage, end of transmission */ /* Data stage, end of transmission */
usbd_ep_write_packet(0, control_state.ctrl_buf, usbd_ep_write_packet(0, control_state.ctrl_buf,
control_state.ctrl_len); control_state.ctrl_len);
control_state.state = LAST_DATA_IN; control_state.state = LAST_DATA_IN;
control_state.ctrl_len = 0; control_state.ctrl_len = 0;
control_state.ctrl_buf = NULL; control_state.ctrl_buf = NULL;
} }
@ -76,12 +73,10 @@ static void usb_control_send_chunk(void)
static int usb_control_recv_chunk(void) static int usb_control_recv_chunk(void)
{ {
u16 packetsize = MIN(_usbd_device.desc->bMaxPacketSize0, u16 packetsize = MIN(_usbd_device.desc->bMaxPacketSize0,
control_state.req.wLength - control_state.req.wLength - control_state.ctrl_len);
control_state.ctrl_len); u16 size = usbd_ep_read_packet(0, control_state.ctrl_buf +
u16 size = usbd_ep_read_packet(0, control_state.ctrl_len, packetsize);
control_state.ctrl_buf + control_state.ctrl_len,
packetsize);
if (size != packetsize) { if (size != packetsize) {
usbd_ep_stall_set(0, 1); usbd_ep_stall_set(0, 1);
@ -95,65 +90,64 @@ static int usb_control_recv_chunk(void)
static int usb_control_request_dispatch(struct usb_setup_data *req) static int usb_control_request_dispatch(struct usb_setup_data *req)
{ {
int result = 0; int i, result = 0;
int i;
struct user_control_callback *cb = _usbd_device.user_control_callback; struct user_control_callback *cb = _usbd_device.user_control_callback;
/* Call user command hook function */ /* Call user command hook function. */
for(i = 0; i < MAX_USER_CONTROL_CALLBACK; i++) { for (i = 0; i < MAX_USER_CONTROL_CALLBACK; i++) {
if(cb[i].cb == NULL) if (cb[i].cb == NULL)
break; break;
if((req->bmRequestType & cb[i].type_mask) == cb[i].type) { if ((req->bmRequestType & cb[i].type_mask) == cb[i].type) {
result = cb[i].cb(req, &control_state.ctrl_buf, result = cb[i].cb(req, &control_state.ctrl_buf,
&control_state.ctrl_len, &control_state.ctrl_len,
&control_state.complete); &control_state.complete);
if(result) if (result)
return result; return result;
} }
} }
/* Try standard request if not already handled */ /* Try standard request if not already handled. */
return _usbd_standard_request(req, &control_state.ctrl_buf, return _usbd_standard_request(req, &control_state.ctrl_buf,
&control_state.ctrl_len); &control_state.ctrl_len);
} }
/* Handle commands and read requests. */ /* Handle commands and read requests. */
static void usb_control_setup_read(struct usb_setup_data *req) static void usb_control_setup_read(struct usb_setup_data *req)
{ {
control_state.ctrl_buf = _usbd_device.ctrl_buf; control_state.ctrl_buf = _usbd_device.ctrl_buf;
control_state.ctrl_len = req->wLength; control_state.ctrl_len = req->wLength;
if(usb_control_request_dispatch(req)) { if (usb_control_request_dispatch(req)) {
if(control_state.ctrl_len) { if (control_state.ctrl_len) {
/* Go to data out stage if handled */ /* Go to data out stage if handled. */
usb_control_send_chunk(); usb_control_send_chunk();
} else { } else {
/* Go to status stage if handled */ /* Go to status stage if handled. */
usbd_ep_write_packet(0, NULL, 0); usbd_ep_write_packet(0, NULL, 0);
control_state.state = STATUS_IN; control_state.state = STATUS_IN;
} }
} else { } else {
/* Stall endpoint on failure */ /* Stall endpoint on failure. */
usbd_ep_stall_set(0, 1); usbd_ep_stall_set(0, 1);
} }
} }
static void usb_control_setup_write(struct usb_setup_data *req) static void usb_control_setup_write(struct usb_setup_data *req)
{ {
if(req->wLength > _usbd_device.ctrl_buf_len) { if (req->wLength > _usbd_device.ctrl_buf_len) {
usbd_ep_stall_set(0, 1); usbd_ep_stall_set(0, 1);
return; return;
} }
/* Buffer into which to write received data */ /* Buffer into which to write received data. */
control_state.ctrl_buf = _usbd_device.ctrl_buf; control_state.ctrl_buf = _usbd_device.ctrl_buf;
control_state.ctrl_len = 0; control_state.ctrl_len = 0;
/* Wait for DATA OUT Stage */ /* Wait for DATA OUT stage. */
if(req->wLength > _usbd_device.desc->bMaxPacketSize0) if (req->wLength > _usbd_device.desc->bMaxPacketSize0)
control_state.state = DATA_OUT; control_state.state = DATA_OUT;
else control_state.state = LAST_DATA_OUT; else
control_state.state = LAST_DATA_OUT;
} }
void _usbd_control_setup(u8 ea) void _usbd_control_setup(u8 ea)
@ -163,91 +157,81 @@ void _usbd_control_setup(u8 ea)
control_state.complete = NULL; control_state.complete = NULL;
if(usbd_ep_read_packet(0, req, 8) != 8) { if (usbd_ep_read_packet(0, req, 8) != 8) {
usbd_ep_stall_set(0, 1); usbd_ep_stall_set(0, 1);
return; return;
} }
if(req->wLength == 0) { if (req->wLength == 0) {
usb_control_setup_read(req); usb_control_setup_read(req);
} else if(req->bmRequestType & 0x80) { } else if (req->bmRequestType & 0x80) {
usb_control_setup_read(req); usb_control_setup_read(req);
} else { } else {
usb_control_setup_write(req); usb_control_setup_write(req);
} }
} }
void _usbd_control_out(u8 ea) void _usbd_control_out(u8 ea)
{ {
(void)ea; (void)ea;
switch(control_state.state) { switch (control_state.state) {
case DATA_OUT: case DATA_OUT:
if(usb_control_recv_chunk() < 0) break; if (usb_control_recv_chunk() < 0)
if((control_state.req.wLength - control_state.ctrl_len) <= break;
_usbd_device.desc->bMaxPacketSize0) if ((control_state.req.wLength - control_state.ctrl_len) <=
_usbd_device.desc->bMaxPacketSize0)
control_state.state = LAST_DATA_OUT; control_state.state = LAST_DATA_OUT;
break; break;
case LAST_DATA_OUT:
case LAST_DATA_OUT: { if (usb_control_recv_chunk() < 0)
int result = 0; break;
if(usb_control_recv_chunk() < 0) break;
/* We have now received the full data payload. /* We have now received the full data payload.
* Invoke callback to process. * Invoke callback to process.
*/ */
if(usb_control_request_dispatch(&control_state.req)) { if (usb_control_request_dispatch(&control_state.req)) {
/* Got to status stage on success */ /* Got to status stage on success. */
usbd_ep_write_packet(0, NULL, 0); usbd_ep_write_packet(0, NULL, 0);
control_state.state = STATUS_IN; control_state.state = STATUS_IN;
} else { } else {
usbd_ep_stall_set(0, 1); usbd_ep_stall_set(0, 1);
} }
break; break;
} case STATUS_OUT:
case STATUS_OUT: {
usbd_ep_read_packet(0, NULL, 0); usbd_ep_read_packet(0, NULL, 0);
control_state.state = IDLE; control_state.state = IDLE;
if (control_state.complete) if (control_state.complete)
control_state.complete(&control_state.req); control_state.complete(&control_state.req);
control_state.complete = NULL; control_state.complete = NULL;
break; break;
} default:
usbd_ep_stall_set(0, 1);
default:
usbd_ep_stall_set(0, 1);
} }
} }
void _usbd_control_in(u8 ea) void _usbd_control_in(u8 ea)
{ {
(void)ea; (void)ea;
switch(control_state.state) { struct usb_setup_data *req = &control_state.req;
case DATA_IN:
switch (control_state.state) {
case DATA_IN:
usb_control_send_chunk(); usb_control_send_chunk();
break; break;
case LAST_DATA_IN:
case LAST_DATA_IN:
control_state.state = STATUS_OUT; control_state.state = STATUS_OUT;
break; break;
case STATUS_IN:
case STATUS_IN: { if (control_state.complete)
struct usb_setup_data *req = &control_state.req;
if (control_state.complete)
control_state.complete(&control_state.req); control_state.complete(&control_state.req);
/* Exception: Handle SET ADDRESS function here... */ /* Exception: Handle SET ADDRESS function here... */
if((req->bmRequestType == 0) && if ((req->bmRequestType == 0) &&
(req->bRequest == USB_REQ_SET_ADDRESS)) (req->bRequest == USB_REQ_SET_ADDRESS))
_usbd_hw_set_address(req->wValue); _usbd_hw_set_address(req->wValue);
control_state.state = IDLE; control_state.state = IDLE;
break; break;
} default:
usbd_ep_stall_set(0, 1);
default:
usbd_ep_stall_set(0, 1);
} }
} }

View File

@ -23,21 +23,16 @@
#include <usb/usbd.h> #include <usb/usbd.h>
#include "usb_private.h" #include "usb_private.h"
/** Initialize USB Device Controller. /** Initialize the USB device controller hardware of the STM32. */
*
* Function initializes the USB Device controller hardware
* of the STM32 microcontroller.
*/
void _usbd_hw_init(void) void _usbd_hw_init(void)
{ {
SET_REG(USB_CNTR_REG, 0); SET_REG(USB_CNTR_REG, 0);
SET_REG(USB_BTABLE_REG, 0); SET_REG(USB_BTABLE_REG, 0);
SET_REG(USB_ISTR_REG, 0); SET_REG(USB_ISTR_REG, 0);
/* Enable RESET, SUSPEND, RESUME and CTR interrupts */ /* Enable RESET, SUSPEND, RESUME and CTR interrupts. */
SET_REG(USB_CNTR_REG, USB_CNTR_RESETM | USB_CNTR_CTRM | SET_REG(USB_CNTR_REG, USB_CNTR_RESETM | USB_CNTR_CTRM |
USB_CNTR_SUSPM | USB_CNTR_WKUPM); USB_CNTR_SUSPM | USB_CNTR_WKUPM);
} }
void _usbd_hw_set_address(u8 addr) void _usbd_hw_set_address(u8 addr)
@ -46,24 +41,28 @@ void _usbd_hw_set_address(u8 addr)
SET_REG(USB_DADDR_REG, (addr & USB_DADDR_ADDR) | USB_DADDR_ENABLE); SET_REG(USB_DADDR_REG, (addr & USB_DADDR_ADDR) | USB_DADDR_ENABLE);
} }
/** Set the receive buffer size for a given USB endpoint /**
* @param ep Index of Endpoint to configure * Set the receive buffer size for a given USB endpoint.
* @param addr Size in bytes of RX buffer *
* @param ep Index of endpoint to configure.
* @param addr Size in bytes of the RX buffer.
*/ */
static void usb_set_ep_rx_bufsize(u8 ep, u32 size) static void usb_set_ep_rx_bufsize(u8 ep, u32 size)
{ {
if(size > 62) { if (size > 62) {
if(size & 0x1f) size -= 32; if (size & 0x1f)
size -= 32;
USB_SET_EP_RX_COUNT(ep, (size << 5) | 0x8000); USB_SET_EP_RX_COUNT(ep, (size << 5) | 0x8000);
} else { } else {
if(size & 1) size++; if (size & 1)
size++;
USB_SET_EP_RX_COUNT(ep, size << 10); USB_SET_EP_RX_COUNT(ep, size << 10);
} }
} }
void usbd_ep_setup(u8 addr, u8 type, u16 max_size, void (*callback)(u8 ep)) void usbd_ep_setup(u8 addr, u8 type, u16 max_size, void (*callback) (u8 ep))
{ {
/* Translate USB standard type codes to stm32 */ /* Translate USB standard type codes to STM32. */
const u16 typelookup[] = { const u16 typelookup[] = {
[USB_ENDPOINT_ATTR_CONTROL] = USB_EP_TYPE_CONTROL, [USB_ENDPOINT_ATTR_CONTROL] = USB_EP_TYPE_CONTROL,
[USB_ENDPOINT_ATTR_ISOCHRONOUS] = USB_EP_TYPE_ISO, [USB_ENDPOINT_ATTR_ISOCHRONOUS] = USB_EP_TYPE_ISO,
@ -73,23 +72,30 @@ void usbd_ep_setup(u8 addr, u8 type, u16 max_size, void (*callback)(u8 ep))
u8 dir = addr & 0x80; u8 dir = addr & 0x80;
addr &= 0x7f; addr &= 0x7f;
/* Assign address */ /* Assign address. */
USB_SET_EP_ADDR(addr, addr); USB_SET_EP_ADDR(addr, addr);
USB_SET_EP_TYPE(addr, typelookup[type]); USB_SET_EP_TYPE(addr, typelookup[type]);
if(dir || (addr == 0)) { if (dir || (addr == 0)) {
USB_SET_EP_TX_ADDR(addr, _usbd_device.pm_top); USB_SET_EP_TX_ADDR(addr, _usbd_device.pm_top);
if(callback) if (callback) {
_usbd_device.user_callback_ctr[addr][USB_TRANSACTION_IN] = (void*)callback; _usbd_device.
user_callback_ctr[addr][USB_TRANSACTION_IN] =
(void *)callback;
}
USB_CLR_EP_TX_DTOG(addr); USB_CLR_EP_TX_DTOG(addr);
USB_SET_EP_TX_STAT(addr, USB_EP_TX_STAT_NAK); USB_SET_EP_TX_STAT(addr, USB_EP_TX_STAT_NAK);
_usbd_device.pm_top += max_size; _usbd_device.pm_top += max_size;
} }
if(!dir) {
if (!dir) {
USB_SET_EP_RX_ADDR(addr, _usbd_device.pm_top); USB_SET_EP_RX_ADDR(addr, _usbd_device.pm_top);
usb_set_ep_rx_bufsize(addr, max_size); usb_set_ep_rx_bufsize(addr, max_size);
if(callback) if (callback) {
_usbd_device.user_callback_ctr[addr][USB_TRANSACTION_OUT] = (void*)callback; _usbd_device.
user_callback_ctr[addr][USB_TRANSACTION_OUT] =
(void *)callback;
}
USB_CLR_EP_RX_DTOG(addr); USB_CLR_EP_RX_DTOG(addr);
USB_SET_EP_RX_STAT(addr, USB_EP_RX_STAT_VALID); USB_SET_EP_RX_STAT(addr, USB_EP_RX_STAT_VALID);
_usbd_device.pm_top += max_size; _usbd_device.pm_top += max_size;
@ -100,73 +106,74 @@ void _usbd_hw_endpoints_reset(void)
{ {
int i; int i;
/* Reset all endpoints */ /* Reset all endpoints. */
for(i = 1; i < 8; i++) { for (i = 1; i < 8; i++) {
USB_SET_EP_TX_STAT(i, USB_EP_TX_STAT_DISABLED); USB_SET_EP_TX_STAT(i, USB_EP_TX_STAT_DISABLED);
USB_SET_EP_RX_STAT(i, USB_EP_RX_STAT_DISABLED); USB_SET_EP_RX_STAT(i, USB_EP_RX_STAT_DISABLED);
} }
_usbd_device.pm_top = 0x40 + (2*_usbd_device.desc->bMaxPacketSize0); _usbd_device.pm_top = 0x40 + (2 * _usbd_device.desc->bMaxPacketSize0);
} }
void usbd_ep_stall_set(u8 addr, u8 stall) void usbd_ep_stall_set(u8 addr, u8 stall)
{ {
if(addr == 0) if (addr == 0)
USB_SET_EP_TX_STAT(addr, USB_SET_EP_TX_STAT(addr, stall ? USB_EP_TX_STAT_STALL :
stall ? USB_EP_TX_STAT_STALL : USB_EP_TX_STAT_NAK); USB_EP_TX_STAT_NAK);
if(addr & 0x80) { if (addr & 0x80) {
addr &= 0x7F; addr &= 0x7F;
USB_SET_EP_TX_STAT(addr, USB_SET_EP_TX_STAT(addr, stall ? USB_EP_TX_STAT_STALL :
stall ? USB_EP_TX_STAT_STALL : USB_EP_TX_STAT_NAK); USB_EP_TX_STAT_NAK);
/* Reset to DATA0 if clearing stall condition */ /* Reset to DATA0 if clearing stall condition. */
if(!stall) if (!stall)
USB_CLR_EP_TX_DTOG(addr); USB_CLR_EP_TX_DTOG(addr);
} else { } else {
/* Reset to DATA0 if clearing stall condition */ /* Reset to DATA0 if clearing stall condition. */
if(!stall) if (!stall)
USB_CLR_EP_RX_DTOG(addr); USB_CLR_EP_RX_DTOG(addr);
USB_SET_EP_RX_STAT(addr, USB_SET_EP_RX_STAT(addr, stall ? USB_EP_RX_STAT_STALL :
stall ? USB_EP_RX_STAT_STALL : USB_EP_RX_STAT_VALID); USB_EP_RX_STAT_VALID);
} }
} }
u8 usbd_ep_stall_get(u8 addr) u8 usbd_ep_stall_get(u8 addr)
{ {
if(addr & 0x80) { if (addr & 0x80) {
if ((*USB_EP_REG(addr & 0x7F) & USB_EP_TX_STAT) == if ((*USB_EP_REG(addr & 0x7F) & USB_EP_TX_STAT) ==
USB_EP_TX_STAT_STALL) USB_EP_TX_STAT_STALL)
return 1; return 1;
} else { } else {
if ((*USB_EP_REG(addr) & USB_EP_RX_STAT) == if ((*USB_EP_REG(addr) & USB_EP_RX_STAT) ==
USB_EP_RX_STAT_STALL) USB_EP_RX_STAT_STALL)
return 1; return 1;
} }
return 0; return 0;
} }
/** Copy a data buffer to Packet Memory. /**
* @param PM Destination pointer into packet memory. * Copy a data buffer to packet memory.
* @param buf Source pointer to data buffer. *
* @param len Number of bytes to copy. * @param PM Destination pointer into packet memory.
* @param buf Source pointer to data buffer.
* @param len Number of bytes to copy.
*/ */
static inline void static void usb_copy_to_pm(volatile void *vPM, const void *buf, u16 len)
usb_copy_to_pm(volatile void *vPM, const void *buf, u16 len)
{ {
const u16 *lbuf = buf; const u16 *lbuf = buf;
volatile u16 *PM = vPM; volatile u16 *PM = vPM;
for(len = (len + 1) >> 1; len; PM += 2, lbuf++, len--) for (len = (len + 1) >> 1; len; PM += 2, lbuf++, len--)
*PM = *lbuf; *PM = *lbuf;
} }
u16 usbd_ep_write_packet(u8 addr, const void *buf, u16 len) u16 usbd_ep_write_packet(u8 addr, const void *buf, u16 len)
{ {
addr &= 0x7F; addr &= 0x7F;
if((*USB_EP_REG(addr) & USB_EP_TX_STAT) == USB_EP_TX_STAT_VALID) if ((*USB_EP_REG(addr) & USB_EP_TX_STAT) == USB_EP_TX_STAT_VALID)
return 0; return 0;
usb_copy_to_pm(USB_GET_EP_TX_BUFF(addr), buf, len); usb_copy_to_pm(USB_GET_EP_TX_BUFF(addr), buf, len);
@ -176,27 +183,29 @@ u16 usbd_ep_write_packet(u8 addr, const void *buf, u16 len)
return len; return len;
} }
/** Copy a data buffer from Packet Memory. /**
* @param buf Source pointer to data buffer. * Copy a data buffer from Packet Memory.
* @param PM Destination pointer into packet memory. *
* @param len Number of bytes to copy. * @param buf Source pointer to data buffer.
* @param PM Destination pointer into packet memory.
* @param len Number of bytes to copy.
*/ */
static inline void static void usb_copy_from_pm(void *buf, const volatile void *vPM, u16 len)
usb_copy_from_pm(void *buf, const volatile void *vPM, u16 len)
{ {
u16 *lbuf = buf; u16 *lbuf = buf;
const volatile u16 *PM = vPM; const volatile u16 *PM = vPM;
u8 odd = len & 1; u8 odd = len & 1;
for(len >>= 1; len; PM += 2, lbuf++, len--) for (len >>= 1; len; PM += 2, lbuf++, len--)
*lbuf = *PM; *lbuf = *PM;
if(odd) *(u8*)lbuf = *(u8*)PM; if (odd)
*(u8 *) lbuf = *(u8 *) PM;
} }
u16 usbd_ep_read_packet(u8 addr, void *buf, u16 len) u16 usbd_ep_read_packet(u8 addr, void *buf, u16 len)
{ {
if((*USB_EP_REG(addr) & USB_EP_RX_STAT) == USB_EP_RX_STAT_VALID) if ((*USB_EP_REG(addr) & USB_EP_RX_STAT) == USB_EP_RX_STAT_VALID)
return 0; return 0;
len = MIN(USB_GET_EP_RX_COUNT(addr) & 0x3ff, len); len = MIN(USB_GET_EP_RX_COUNT(addr) & 0x3ff, len);
@ -212,39 +221,38 @@ void usbd_poll(void)
{ {
u16 istr = *USB_ISTR_REG; u16 istr = *USB_ISTR_REG;
if(istr & USB_ISTR_RESET) { if (istr & USB_ISTR_RESET) {
_usbd_device.pm_top = 0x40; _usbd_device.pm_top = 0x40;
_usbd_reset(); _usbd_reset();
USB_CLR_ISTR_RESET(); USB_CLR_ISTR_RESET();
return; return;
} }
if(istr & USB_ISTR_CTR) { if (istr & USB_ISTR_CTR) {
u8 ep = istr & USB_ISTR_EP_ID; u8 ep = istr & USB_ISTR_EP_ID;
u8 type = (istr & USB_ISTR_DIR) ? 1 : 0; u8 type = (istr & USB_ISTR_DIR) ? 1 : 0;
if(type) { /* OUT or SETUP transaction */ if (type) /* OUT or SETUP transaction */
type += (*USB_EP_REG(ep) & USB_EP_SETUP) ? 1 : 0; type += (*USB_EP_REG(ep) & USB_EP_SETUP) ? 1 : 0;
} else { /* IN transaction */ else /* IN transaction */
USB_CLR_EP_TX_CTR(ep); USB_CLR_EP_TX_CTR(ep);
}
if(_usbd_device.user_callback_ctr[ep][type]) if (_usbd_device.user_callback_ctr[ep][type])
_usbd_device.user_callback_ctr[ep][type](ep); _usbd_device.user_callback_ctr[ep][type] (ep);
} }
if(istr & USB_ISTR_SUSP) { if (istr & USB_ISTR_SUSP) {
USB_CLR_ISTR_SUSP(); USB_CLR_ISTR_SUSP();
if(_usbd_device.user_callback_suspend) if (_usbd_device.user_callback_suspend)
_usbd_device.user_callback_suspend(); _usbd_device.user_callback_suspend();
} }
if(istr & USB_ISTR_WKUP) { if (istr & USB_ISTR_WKUP) {
USB_CLR_ISTR_WKUP(); USB_CLR_ISTR_WKUP();
if(_usbd_device.user_callback_resume) if (_usbd_device.user_callback_resume)
_usbd_device.user_callback_resume(); _usbd_device.user_callback_resume();
} }
if(istr & USB_ISTR_SOF) if (istr & USB_ISTR_SOF)
USB_CLR_ISTR_SOF(); USB_CLR_ISTR_SOF();
} }

View File

@ -65,12 +65,11 @@ void _usbd_control_in(u8 ea);
void _usbd_control_out(u8 ea); void _usbd_control_out(u8 ea);
void _usbd_control_setup(u8 ea); void _usbd_control_setup(u8 ea);
int _usbd_standard_request(struct usb_setup_data *req, int _usbd_standard_request(struct usb_setup_data *req, u8 **buf, u16 *len);
u8 **buf, u16 *len);
void _usbd_reset(void); void _usbd_reset(void);
/* Functions provided by the hardware abstraction */ /* Functions provided by the hardware abstraction. */
void _usbd_hw_init(void); void _usbd_hw_init(void);
void _usbd_hw_set_address(u8 addr); void _usbd_hw_set_address(u8 addr);
void _usbd_hw_endpoints_reset(void); void _usbd_hw_endpoints_reset(void);

View File

@ -26,8 +26,7 @@ void usbd_register_set_config_callback(void (*callback)(u16 wValue))
_usbd_device.user_callback_set_config = callback; _usbd_device.user_callback_set_config = callback;
} }
static u16 static u16 build_config_descriptor(u8 index, u8 *buf, u16 len)
build_config_descriptor(u8 index, u8 *buf, u16 len)
{ {
u8 *tmpbuf = buf; u8 *tmpbuf = buf;
const struct usb_config_descriptor *cfg = &_usbd_device.config[index]; const struct usb_config_descriptor *cfg = &_usbd_device.config[index];
@ -35,152 +34,161 @@ build_config_descriptor(u8 index, u8 *buf, u16 len)
u16 i, j, k; u16 i, j, k;
memcpy(buf, cfg, count = MIN(len, cfg->bLength)); memcpy(buf, cfg, count = MIN(len, cfg->bLength));
buf += count; len -= count; total += count; totallen += cfg->bLength; buf += count;
len -= count;
total += count;
totallen += cfg->bLength;
/* For each interface... */ /* For each interface... */
for(i = 0; i < cfg->bNumInterfaces; i++) { for (i = 0; i < cfg->bNumInterfaces; i++) {
/* For each alternate setting... */ /* For each alternate setting... */
for(j = 0; j < cfg->interface[i].num_altsetting; j++) { for (j = 0; j < cfg->interface[i].num_altsetting; j++) {
const struct usb_interface_descriptor *iface = const struct usb_interface_descriptor *iface =
&cfg->interface[i].altsetting[j]; &cfg->interface[i].altsetting[j];
/* Copy interface descriptor */ /* Copy interface descriptor. */
memcpy(buf, iface, count = MIN(len, iface->bLength)); memcpy(buf, iface, count = MIN(len, iface->bLength));
buf += count; len -= count; buf += count;
total += count; totallen += iface->bLength; len -= count;
/* Copy extra bytes (function descriptors) */ total += count;
memcpy(buf, iface->extra, totallen += iface->bLength;
count = MIN(len, iface->extralen)); /* Copy extra bytes (function descriptors). */
buf += count; len -= count; memcpy(buf, iface->extra,
total += count; totallen += iface->extralen; count = MIN(len, iface->extralen));
buf += count;
len -= count;
total += count;
totallen += iface->extralen;
/* For each endpoint... */ /* For each endpoint... */
for(k = 0; k < iface->bNumEndpoints; k++) { for (k = 0; k < iface->bNumEndpoints; k++) {
const struct usb_endpoint_descriptor *ep = const struct usb_endpoint_descriptor *ep =
&iface->endpoint[k]; &iface->endpoint[k];
memcpy(buf, ep, count = MIN(len, ep->bLength)); memcpy(buf, ep, count = MIN(len, ep->bLength));
buf += count; len -= count; buf += count;
total += count; totallen += ep->bLength; len -= count;
total += count;
totallen += ep->bLength;
} }
} }
} }
/* Fill in wTotalLength */ /* Fill in wTotalLength. */
*(u16*)(tmpbuf+2) = totallen; *(u16 *)(tmpbuf + 2) = totallen;
return total; return total;
} }
static int usb_standard_get_descriptor(struct usb_setup_data *req, static int usb_standard_get_descriptor(struct usb_setup_data *req,
u8 **buf, u16 *len) u8 **buf, u16 *len)
{ {
int i; int i;
struct usb_string_descriptor *sd;
switch(req->wValue >> 8) { switch (req->wValue >> 8) {
case USB_DT_DEVICE: case USB_DT_DEVICE:
*buf = (u8 *)_usbd_device.desc; *buf = (u8 *) _usbd_device.desc;
*len = MIN(*len, _usbd_device.desc->bLength); *len = MIN(*len, _usbd_device.desc->bLength);
return 1; return 1;
case USB_DT_CONFIGURATION:
case USB_DT_CONFIGURATION: {
*buf = _usbd_device.ctrl_buf; *buf = _usbd_device.ctrl_buf;
*len = build_config_descriptor(req->wValue & 0xff, *buf, *len); *len = build_config_descriptor(req->wValue & 0xff, *buf, *len);
return 1; return 1;
} case USB_DT_STRING:
sd = (struct usb_string_descriptor *)_usbd_device.ctrl_buf;
case USB_DT_STRING: { if (!_usbd_device.strings)
struct usb_string_descriptor *sd = return 0; /* Device doesn't support strings. */
(struct usb_string_descriptor *)_usbd_device.ctrl_buf;
if(!_usbd_device.strings) sd->bLength = strlen(_usbd_device.strings[req->wValue & 0xff])
return 0; /* Device doesn't support strings */
sd->bLength = strlen(_usbd_device.strings[req->wValue & 0xff])
* 2 + 2; * 2 + 2;
sd->bDescriptorType = USB_DT_STRING; sd->bDescriptorType = USB_DT_STRING;
*buf = (u8 *)sd; *buf = (u8 *)sd;
*len = MIN(*len, sd->bLength); *len = MIN(*len, sd->bLength);
for(i = 0; i < (*len / 2) - 1; i++) for (i = 0; i < (*len / 2) - 1; i++)
sd->wData[i] = sd->wData[i] =
_usbd_device.strings[req->wValue & 0xff][i]; _usbd_device.strings[req->wValue & 0xff][i];
/* Send sane Language ID descriptor... */ /* Send sane Language ID descriptor... */
if((req->wValue & 0xff) == 0) if ((req->wValue & 0xff) == 0)
sd->wData[0] = 0x409; sd->wData[0] = 0x409;
return 1; return 1;
}
} }
return 0; return 0;
} }
static int usb_standard_set_address(struct usb_setup_data *req, static int usb_standard_set_address(struct usb_setup_data *req, u8 **buf,
u8 **buf, u16 *len) u16 *len)
{ {
(void)req; (void)req;
(void)buf; (void)buf;
(void)len; (void)len;
/* The actual address is only latched at the STATUS IN stage */ /* The actual address is only latched at the STATUS IN stage. */
if((req->bmRequestType != 0) || (req->wValue >= 128)) return 0; if ((req->bmRequestType != 0) || (req->wValue >= 128))
return 0;
_usbd_device.current_address = req->wValue; _usbd_device.current_address = req->wValue;
return 1; return 1;
} }
static int usb_standard_set_configuration(struct usb_setup_data *req, static int usb_standard_set_configuration(struct usb_setup_data *req,
u8 **buf, u16 *len) u8 **buf, u16 *len)
{ {
(void)req; (void)req;
(void)buf; (void)buf;
(void)len; (void)len;
/* Is this correct, or should we reset alternate settings */ /* Is this correct, or should we reset alternate settings. */
if(req->wValue == _usbd_device.current_config) return 1; if (req->wValue == _usbd_device.current_config)
return 1;
_usbd_device.current_config = req->wValue; _usbd_device.current_config = req->wValue;
/* Reset all endpoints */ /* Reset all endpoints. */
_usbd_hw_endpoints_reset(); _usbd_hw_endpoints_reset();
if(_usbd_device.user_callback_set_config) if (_usbd_device.user_callback_set_config)
_usbd_device.user_callback_set_config(req->wValue); _usbd_device.user_callback_set_config(req->wValue);
return 1; return 1;
} }
static int usb_standard_get_configuration(struct usb_setup_data *req, static int usb_standard_get_configuration(struct usb_setup_data *req,
u8 **buf, u16 *len) u8 **buf, u16 *len)
{ {
(void)req; (void)req;
if(*len > 1) *len = 1; if (*len > 1)
*len = 1;
(*buf)[0] = _usbd_device.current_config; (*buf)[0] = _usbd_device.current_config;
return 1; return 1;
} }
static int usb_standard_set_interface(struct usb_setup_data *req, static int usb_standard_set_interface(struct usb_setup_data *req,
u8 **buf, u16 *len) u8 **buf, u16 *len)
{ {
(void)req; (void)req;
(void)buf; (void)buf;
/* FIXME: adapt if we have more than one interface */ /* FIXME: Adapt if we have more than one interface. */
if(req->wValue != 0) return 0; if (req->wValue != 0)
return 0;
*len = 0; *len = 0;
return 1; return 1;
} }
static int usb_standard_get_interface(struct usb_setup_data *req, static int usb_standard_get_interface(struct usb_setup_data *req,
u8 **buf, u16 *len) u8 **buf, u16 *len)
{ {
(void)req; (void)req;
(void)buf; (void)buf;
/* FIXME: adapt if we have more than one interface */ /* FIXME: Adapt if we have more than one interface. */
*len = 1; *len = 1;
(*buf)[0] = 0; (*buf)[0] = 0;
@ -188,37 +196,40 @@ static int usb_standard_get_interface(struct usb_setup_data *req,
} }
static int usb_standard_device_get_status(struct usb_setup_data *req, static int usb_standard_device_get_status(struct usb_setup_data *req,
u8 **buf, u16 *len) u8 **buf, u16 *len)
{ {
(void)req; (void)req;
/* bit 0: self powered */ /* bit 0: self powered */
/* bit 1: remote wakeup */ /* bit 1: remote wakeup */
if(*len > 2) *len = 2; if (*len > 2)
*len = 2;
(*buf)[0] = 0; (*buf)[0] = 0;
(*buf)[1] = 0; (*buf)[1] = 0;
return 1; return 1;
} }
static int usb_standard_interface_get_status(struct usb_setup_data *req, static int usb_standard_interface_get_status(struct usb_setup_data *req,
u8 **buf, u16 *len) u8 **buf, u16 *len)
{ {
(void)req; (void)req;
/* not defined */ /* not defined */
if(*len > 2) *len = 2; if (*len > 2)
*len = 2;
(*buf)[0] = 0; (*buf)[0] = 0;
(*buf)[1] = 0; (*buf)[1] = 0;
return 1; return 1;
} }
static int usb_standard_endpoint_get_status(struct usb_setup_data *req, static int usb_standard_endpoint_get_status(struct usb_setup_data *req,
u8 **buf, u16 *len) u8 **buf, u16 *len)
{ {
(void)req; (void)req;
if(*len > 2) *len = 2; if (*len > 2)
*len = 2;
(*buf)[0] = usbd_ep_stall_get(req->wIndex) ? 1 : 0; (*buf)[0] = usbd_ep_stall_get(req->wIndex) ? 1 : 0;
(*buf)[1] = 0; (*buf)[1] = 0;
@ -226,7 +237,7 @@ static int usb_standard_endpoint_get_status(struct usb_setup_data *req,
} }
static int usb_standard_endpoint_stall(struct usb_setup_data *req, static int usb_standard_endpoint_stall(struct usb_setup_data *req,
u8 **buf, u16 *len) u8 **buf, u16 *len)
{ {
(void)buf; (void)buf;
(void)len; (void)len;
@ -237,7 +248,7 @@ static int usb_standard_endpoint_stall(struct usb_setup_data *req,
} }
static int usb_standard_endpoint_unstall(struct usb_setup_data *req, static int usb_standard_endpoint_unstall(struct usb_setup_data *req,
u8 **buf, u16 *len) u8 **buf, u16 *len)
{ {
(void)buf; (void)buf;
(void)len; (void)len;
@ -247,25 +258,26 @@ static int usb_standard_endpoint_unstall(struct usb_setup_data *req,
return 1; return 1;
} }
int _usbd_standard_request_device(struct usb_setup_data *req, u8 **buf, int _usbd_standard_request_device(struct usb_setup_data *req, u8 **buf,
u16 *len) u16 *len)
{ {
int (*command)(struct usb_setup_data *req, u8 **buf, int (*command)(struct usb_setup_data *req, u8 **buf, u16 *len) = NULL;
u16 *len) = NULL;
switch(req->bRequest) { switch (req->bRequest) {
case USB_REQ_CLEAR_FEATURE: case USB_REQ_CLEAR_FEATURE:
case USB_REQ_SET_FEATURE: case USB_REQ_SET_FEATURE:
if (req->wValue == USB_FEAT_DEVICE_REMOTE_WAKEUP) { if (req->wValue == USB_FEAT_DEVICE_REMOTE_WAKEUP) {
/* device wakeup code goes here */ /* Device wakeup code goes here. */
} }
if (req->wValue == USB_FEAT_TEST_MODE) { if (req->wValue == USB_FEAT_TEST_MODE) {
/* test mode code goes here */ /* Test mode code goes here. */
} }
break; break;
case USB_REQ_SET_ADDRESS: case USB_REQ_SET_ADDRESS:
/* SET ADDRESS is an exception. /*
* It is only processed at STATUS stage */ * SET ADDRESS is an exception.
* It is only processed at STATUS stage.
*/
command = usb_standard_set_address; command = usb_standard_set_address;
break; break;
case USB_REQ_SET_CONFIGURATION: case USB_REQ_SET_CONFIGURATION:
@ -287,20 +299,20 @@ int _usbd_standard_request_device(struct usb_setup_data *req, u8 **buf,
break; break;
} }
if(!command) return 0; if (!command)
return 0;
return command(req, buf, len); return command(req, buf, len);
} }
int _usbd_standard_request_interface(struct usb_setup_data *req, u8 **buf, int _usbd_standard_request_interface(struct usb_setup_data *req, u8 **buf,
u16 *len) u16 *len)
{ {
int (*command)(struct usb_setup_data *req, u8 **buf, int (*command)(struct usb_setup_data *req, u8 **buf, u16 *len) = NULL;
u16 *len) = NULL;
switch(req->bRequest) { switch (req->bRequest) {
case USB_REQ_CLEAR_FEATURE: case USB_REQ_CLEAR_FEATURE:
case USB_REQ_SET_FEATURE: case USB_REQ_SET_FEATURE:
/* not defined */ /* not defined */
break; break;
case USB_REQ_GET_INTERFACE: case USB_REQ_GET_INTERFACE:
@ -314,27 +326,25 @@ int _usbd_standard_request_interface(struct usb_setup_data *req, u8 **buf,
break; break;
} }
if(!command) return 0; if (!command)
return 0;
return command(req, buf, len); return command(req, buf, len);
} }
int _usbd_standard_request_endpoint(struct usb_setup_data *req, u8 **buf, int _usbd_standard_request_endpoint(struct usb_setup_data *req, u8 **buf,
u16 *len) u16 *len)
{ {
int (*command)(struct usb_setup_data *req, u8 **buf, int (*command) (struct usb_setup_data *req, u8 **buf, u16 *len) = NULL;
u16 *len) = NULL;
switch(req->bRequest) { switch (req->bRequest) {
case USB_REQ_CLEAR_FEATURE: case USB_REQ_CLEAR_FEATURE:
if (req->wValue == USB_FEAT_ENDPOINT_HALT) { if (req->wValue == USB_FEAT_ENDPOINT_HALT)
command = usb_standard_endpoint_unstall; command = usb_standard_endpoint_unstall;
}
break; break;
case USB_REQ_SET_FEATURE: case USB_REQ_SET_FEATURE:
if (req->wValue == USB_FEAT_ENDPOINT_HALT) { if (req->wValue == USB_FEAT_ENDPOINT_HALT)
command = usb_standard_endpoint_stall; command = usb_standard_endpoint_stall;
}
break; break;
case USB_REQ_GET_STATUS: case USB_REQ_GET_STATUS:
command = usb_standard_endpoint_get_status; command = usb_standard_endpoint_get_status;
@ -346,16 +356,16 @@ int _usbd_standard_request_endpoint(struct usb_setup_data *req, u8 **buf,
break; break;
} }
if(!command) return 0; if (!command)
return 0;
return command(req, buf, len); return command(req, buf, len);
} }
int _usbd_standard_request(struct usb_setup_data *req, u8 **buf, int _usbd_standard_request(struct usb_setup_data *req, u8 **buf, u16 *len)
u16 *len)
{ {
/* FIXME: have class/vendor requests as well */ /* FIXME: Have class/vendor requests as well. */
if((req->bmRequestType & USB_REQ_TYPE_TYPE) != USB_REQ_TYPE_STANDARD) if ((req->bmRequestType & USB_REQ_TYPE_TYPE) != USB_REQ_TYPE_STANDARD)
return 0; return 0;
switch (req->bmRequestType & USB_REQ_TYPE_RECIPIENT) { switch (req->bmRequestType & USB_REQ_TYPE_RECIPIENT) {
@ -363,7 +373,7 @@ int _usbd_standard_request(struct usb_setup_data *req, u8 **buf,
return _usbd_standard_request_device(req, buf, len); return _usbd_standard_request_device(req, buf, len);
case USB_REQ_TYPE_INTERFACE: case USB_REQ_TYPE_INTERFACE:
return _usbd_standard_request_interface(req, buf, len); return _usbd_standard_request_interface(req, buf, len);
case USB_REQ_TYPE_ENDPOINT: case USB_REQ_TYPE_ENDPOINT:
return _usbd_standard_request_endpoint(req, buf, len); return _usbd_standard_request_endpoint(req, buf, len);
default: default:
return 0; return 0;