Initial USB device stack for STM32.

Patch provided by Gareth McMullin <gareth@blacksphere.co.nz>,
thanks a lot!
This commit is contained in:
Uwe Hermann
2010-11-02 02:02:21 +01:00
parent 1621fde1f4
commit 6e090ccee1
30 changed files with 2913 additions and 10 deletions

View File

@@ -28,7 +28,10 @@ CFLAGS = -Os -g -Wall -Wextra -I../include -fno-common \
# ARFLAGS = rcsv
ARFLAGS = rcs
OBJS = vector.o rcc.o gpio.o usart.o adc.o spi.o flash.o nvic.o \
rtc.o i2c.o dma.o systick.o exti.o
rtc.o i2c.o dma.o systick.o exti.o scb.o \
usb_f103.o usb.o usb_control.o usb_standard.o
VPATH += usb
# Be silent per default, but 'make V=1' will show all compiler calls.
ifneq ($(V),1)

View File

@@ -287,7 +287,7 @@ void rcc_set_adcpre(u32 adcpre)
u32 reg32;
reg32 = RCC_CFGR;
reg32 &= ((1 << 14) | (1 << 15));
reg32 &= ~((1 << 14) | (1 << 15));
RCC_CFGR = (reg32 | (adcpre << 14));
}
@@ -296,7 +296,7 @@ void rcc_set_ppre2(u32 ppre2)
u32 reg32;
reg32 = RCC_CFGR;
reg32 &= ((1 << 11) | (1 << 12) | (1 << 13));
reg32 &= ~((1 << 11) | (1 << 12) | (1 << 13));
RCC_CFGR = (reg32 | (ppre2 << 11));
}
@@ -305,7 +305,7 @@ void rcc_set_ppre1(u32 ppre1)
u32 reg32;
reg32 = RCC_CFGR;
reg32 &= ((1 << 8) | (1 << 9) | (1 << 10));
reg32 &= ~((1 << 8) | (1 << 9) | (1 << 10));
RCC_CFGR = (reg32 | (ppre1 << 8));
}
@@ -314,10 +314,19 @@ void rcc_set_hpre(u32 hpre)
u32 reg32;
reg32 = RCC_CFGR;
reg32 &= ((1 << 4) | (1 << 5) | (1 << 6) | (1 << 7));
reg32 &= ~((1 << 4) | (1 << 5) | (1 << 6) | (1 << 7));
RCC_CFGR = (reg32 | (hpre << 4));
}
void rcc_set_usbpre(u32 usbpre)
{
u32 reg32;
reg32 = RCC_CFGR;
reg32 &= ~(1 << 22);
RCC_CFGR = (reg32 | (usbpre << 22));
}
u32 rcc_system_clock_source(void)
{
/* Return the clock source which is used as system clock. */
@@ -371,6 +380,50 @@ void rcc_clock_setup_in_hsi_out_64mhz(void)
rcc_set_sysclk_source(RCC_CFGR_SW_SYSCLKSEL_PLLCLK);
}
void rcc_clock_setup_in_hsi_out_48mhz(void)
{
/* Enable internal high-speed oscillator. */
rcc_osc_on(HSI);
rcc_wait_for_osc_ready(HSI);
/* Select HSI as SYSCLK source. */
rcc_set_sysclk_source(RCC_CFGR_SW_SYSCLKSEL_HSICLK);
/*
* Set prescalers for AHB, ADC, ABP1, ABP2.
* Do this before touching the PLL (TODO: why?).
*/
rcc_set_hpre(RCC_CFGR_HPRE_SYSCLK_NODIV); /* Max. 72MHz */
rcc_set_adcpre(RCC_CFGR_ADCPRE_PCLK2_DIV8); /* Max. 14MHz */
rcc_set_ppre1(RCC_CFGR_PPRE1_HCLK_DIV2); /* Max. 36MHz */
rcc_set_ppre2(RCC_CFGR_PPRE2_HCLK_NODIV); /* Max. 72MHz */
rcc_set_usbpre(RCC_CFGR_USBPRE_PLL_CLK_NODIV); /* 48 MHz */
/*
* Sysclk runs with 48MHz -> 1 waitstates.
* 0WS from 0-24MHz
* 1WS from 24-48MHz
* 2WS from 48-72MHz
*/
flash_set_ws(FLASH_LATENCY_1WS);
/*
* Set the PLL multiplication factor to 12.
* 8MHz (internal) * 12 (multiplier) / 2 (PLLSRC_HSI_CLK_DIV2) = 48MHz
*/
rcc_set_pll_multiplication_factor(RCC_CFGR_PLLMUL_PLL_CLK_MUL12);
/* Select HSI/2 as PLL source. */
rcc_set_pll_source(RCC_CFGR_PLLSRC_HSI_CLK_DIV2);
/* Enable PLL oscillator and wait for it to stabilize. */
rcc_osc_on(PLL);
rcc_wait_for_osc_ready(PLL);
/* Select PLL as SYSCLK source. */
rcc_set_sysclk_source(RCC_CFGR_SW_SYSCLKSEL_PLLCLK);
}
void rcc_clock_setup_in_hse_8mhz_out_72mhz(void)
{
/* Enable internal high-speed oscillator. */

31
lib/scb.c Normal file
View File

@@ -0,0 +1,31 @@
/*
* This file is part of the libopenstm32 project.
*
* Copyright (C) 2010 Gareth McMullin <gareth@blacksphere.co.nz>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <libopenstm32/scb.h>
void scb_reset_core(void)
{
SCB_AIRCR = SCB_AIRCR_VECTKEY | SCB_AIRCR_VECTRESET;
}
void scb_reset_system(void)
{
SCB_AIRCR = SCB_AIRCR_VECTKEY | SCB_AIRCR_SYSRESETREQ;
}

95
lib/usb/usb.c Normal file
View File

@@ -0,0 +1,95 @@
/*
* This file is part of the libopenstm32 project.
*
* Copyright (C) 2010 Gareth McMullin <gareth@blacksphere.co.nz>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <usbd.h>
#include <string.h>
#include "usb_private.h"
struct _usbd_device _usbd_device;
uint8_t usbd_control_buffer[128] __attribute__((weak));
/** Main initialization entry point.
*
* Initialize the USB firmware library to implement the USB device described
* by the descriptors provided.
*
* It is required that the 48MHz USB clock is already available.
*
* @param dev Pointer to USB Device descriptor. This must not be changed
* while the device is in use.
* @param conf Pointer to array of USB Configuration descriptors. These
* must not be changed while the device is in use. The length
* of this array is determined by the bNumConfigurations field
* in the device descriptor.
* @return Zero on success (currently cannot fail)
*/
int usbd_init(const struct usb_device_descriptor *dev,
const struct usb_config_descriptor *conf,
const char **strings)
{
_usbd_device.desc = dev;
_usbd_device.config = conf;
_usbd_device.strings = strings;
_usbd_device.ctrl_buf = usbd_control_buffer;
_usbd_device.ctrl_buf_len = sizeof(usbd_control_buffer);
_usbd_hw_init();
_usbd_device.user_callback_ctr[0][USB_TRANSACTION_SETUP] =
_usbd_control_setup;
_usbd_device.user_callback_ctr[0][USB_TRANSACTION_OUT] =
_usbd_control_out;
_usbd_device.user_callback_ctr[0][USB_TRANSACTION_IN] =
_usbd_control_in;
return 0;
}
void usbd_register_reset_callback(void (*callback)(void))
{
_usbd_device.user_callback_reset = callback;
}
void usbd_register_suspend_callback(void (*callback)(void))
{
_usbd_device.user_callback_suspend = callback;
}
void usbd_register_resume_callback(void (*callback)(void))
{
_usbd_device.user_callback_resume = callback;
}
void usbd_set_control_buffer_size(uint16_t size)
{
_usbd_device.ctrl_buf_len = size;
}
void _usbd_reset(void)
{
_usbd_device.current_address = 0;
_usbd_device.current_config = 0;
usbd_ep_setup(0, USB_ENDPOINT_ATTR_CONTROL, 64, NULL);
_usbd_hw_set_address(0);
if(_usbd_device.user_callback_reset)
_usbd_device.user_callback_reset();
}

280
lib/usb/usb_control.c Normal file
View File

@@ -0,0 +1,280 @@
/*
* This file is part of the libopenstm32 project.
*
* Copyright (C) 2010 Gareth McMullin <gareth@blacksphere.co.nz>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <usbd.h>
#include <stdlib.h>
#include "usb_private.h"
static struct usb_control_state {
enum { IDLE, STALLED,
DATA_IN, LAST_DATA_IN, STATUS_IN,
DATA_OUT, LAST_DATA_OUT, STATUS_OUT
} state;
struct usb_setup_data req;
uint8_t *ctrl_buf;
uint16_t ctrl_len;
void (*complete)(struct usb_setup_data *req);
} control_state;
/** Register application callback function for handling of usb control
* request with no data. */
void usbd_register_control_command_callback(
int (*callback)(struct usb_setup_data *req,
void (**complete)(struct usb_setup_data *req)))
{
_usbd_device.user_callback_control_command = callback;
}
/** Register application callback function for handling of usb control
* request to read data. */
void usbd_register_control_read_callback(
int (*callback)(struct usb_setup_data *req, uint8_t **buf,
uint16_t *len, void (**complete)(struct usb_setup_data *req)))
{
_usbd_device.user_callback_control_read = callback;
}
/** Register application callback function for handling of usb control
* request with received data. */
void usbd_register_control_write_callback(
int (*callback)(struct usb_setup_data *req, uint8_t *buf, uint16_t len,
void (**complete)(struct usb_setup_data *req)))
{
_usbd_device.user_callback_control_write = callback;
}
static void usb_control_send_chunk(void)
{
if(_usbd_device.desc->bMaxPacketSize0 < control_state.ctrl_len) {
/* Data stage, normal transmission */
usbd_ep_write_packet(0, control_state.ctrl_buf,
_usbd_device.desc->bMaxPacketSize0);
control_state.state = DATA_IN;
control_state.ctrl_buf += _usbd_device.desc->bMaxPacketSize0;
control_state.ctrl_len -= _usbd_device.desc->bMaxPacketSize0;
} else {
/* Data stage, end of transmission */
usbd_ep_write_packet(0, control_state.ctrl_buf,
control_state.ctrl_len);
control_state.state = LAST_DATA_IN;
control_state.ctrl_len = 0;
control_state.ctrl_buf = NULL;
}
}
static int usb_control_recv_chunk(void)
{
uint16_t packetsize = MIN(_usbd_device.desc->bMaxPacketSize0,
control_state.req.wLength -
control_state.ctrl_len);
uint16_t size = usbd_ep_read_packet(0,
control_state.ctrl_buf + control_state.ctrl_len,
packetsize);
if (size != packetsize) {
usbd_ep_stall(0);
return -1;
}
control_state.ctrl_len += size;
return packetsize;
}
static void usb_control_setup_nodata(struct usb_setup_data *req)
{
int result = 0;
/* Call user command hook function */
if(_usbd_device.user_callback_control_command)
result = _usbd_device.user_callback_control_command(req,
&control_state.complete);
/* Try standard command if not already handled */
if(!result)
result = _usbd_standard_request_command(req);
if(result) {
/* Go to status stage if handled */
usbd_ep_write_packet(0, NULL, 0);
control_state.state = STATUS_IN;
} else {
/* Stall endpoint on failure */
usbd_ep_stall(0);
}
}
static void usb_control_setup_read(struct usb_setup_data *req)
{
int result = 0;
control_state.ctrl_buf = _usbd_device.ctrl_buf;
control_state.ctrl_len = req->wLength;
/* Call user command hook function */
if(_usbd_device.user_callback_control_read)
result = _usbd_device.user_callback_control_read(req,
&control_state.ctrl_buf,
&control_state.ctrl_len,
&control_state.complete);
/* Try standard request if not already handled */
if(!result)
result = _usbd_standard_request_read(req,
&control_state.ctrl_buf,
&control_state.ctrl_len);
if(result) {
/* Go to status stage if handled */
usb_control_send_chunk();
} else {
/* Stall endpoint on failure */
usbd_ep_stall(0);
}
}
static void usb_control_setup_write(struct usb_setup_data *req)
{
if(req->wLength > _usbd_device.ctrl_buf_len) {
usbd_ep_stall(0);
return;
}
/* Buffer into which to write received data */
control_state.ctrl_buf = _usbd_device.ctrl_buf;
control_state.ctrl_len = 0;
/* Wait for DATA OUT Stage */
if(req->wLength > _usbd_device.desc->bMaxPacketSize0)
control_state.state = DATA_OUT;
else control_state.state = LAST_DATA_OUT;
}
void _usbd_control_setup(uint8_t ea)
{
struct usb_setup_data *req = &control_state.req;
(void)ea;
control_state.complete = NULL;
if(usbd_ep_read_packet(0, req, 8) != 8) {
usbd_ep_stall(0);
return;
}
if(req->wLength == 0) {
usb_control_setup_nodata(req);
} else if(req->bmRequestType & 0x80) {
usb_control_setup_read(req);
} else {
usb_control_setup_write(req);
}
}
void _usbd_control_out(uint8_t ea)
{
(void)ea;
switch(control_state.state) {
case DATA_OUT:
if(usb_control_recv_chunk() < 0) break;
if((control_state.req.wLength - control_state.ctrl_len) <=
_usbd_device.desc->bMaxPacketSize0)
control_state.state = LAST_DATA_OUT;
break;
case LAST_DATA_OUT: {
int result = 0;
if(usb_control_recv_chunk() < 0) break;
/* We have now received the full data payload.
* Invoke callback to process.
*/
if(_usbd_device.user_callback_control_write)
result = _usbd_device.user_callback_control_write(
&control_state.req,
control_state.ctrl_buf,
control_state.ctrl_len,
&control_state.complete);
if(!result)
result = _usbd_standard_request_write(
&control_state.req,
control_state.ctrl_buf,
control_state.ctrl_len);
if(result) {
usbd_ep_write_packet(0, NULL, 0);
control_state.state = STATUS_IN;
} else {
usbd_ep_stall(0);
}
break;
}
case STATUS_OUT: {
usbd_ep_read_packet(0, NULL, 0);
control_state.state = IDLE;
if (control_state.complete)
control_state.complete(&control_state.req);
control_state.complete = NULL;
break;
}
default:
usbd_ep_stall(0);
}
}
void _usbd_control_in(uint8_t ea)
{
(void)ea;
switch(control_state.state) {
case DATA_IN:
usb_control_send_chunk();
break;
case LAST_DATA_IN:
control_state.state = STATUS_OUT;
break;
case STATUS_IN: {
struct usb_setup_data *req = &control_state.req;
if (control_state.complete)
control_state.complete(&control_state.req);
/* Exception: Handle SET ADDRESS function here... */
if((req->bmRequestType == 0) &&
(req->bRequest == USB_REQ_SET_ADDRESS))
_usbd_hw_set_address(req->wValue);
control_state.state = IDLE;
break;
}
default:
usbd_ep_stall(0);
}
}

223
lib/usb/usb_f103.c Normal file
View File

@@ -0,0 +1,223 @@
/*
* This file is part of the libopenstm32 project.
*
* Copyright (C) 2010 Gareth McMullin <gareth@blacksphere.co.nz>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <libopenstm32/common.h>
#include <libopenstm32/tools.h>
#include <libopenstm32/usb.h>
#include <usbd.h>
#include "usb_private.h"
/** Initialize USB Device Controller.
*
* Function initializes the USB Device controller hardware
* of the STM32 microcontroller.
*/
void _usbd_hw_init(void)
{
SET_REG(USB_CNTR_REG, 0);
SET_REG(USB_BTABLE_REG, 0);
SET_REG(USB_ISTR_REG, 0);
/* Enable RESET, SUSPEND, RESUME and CTR interrupts */
SET_REG(USB_CNTR_REG, USB_CNTR_RESETM | USB_CNTR_CTRM |
USB_CNTR_SUSPM | USB_CNTR_WKUPM);
}
void _usbd_hw_set_address(u8 addr)
{
/* Set device address and 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
* @param addr Size in bytes of RX buffer
*/
static void usb_set_ep_rx_bufsize(u8 ep, u32 size)
{
if(size > 62) {
if(size & 0x1f) size -= 32;
USB_SET_EP_RX_COUNT(ep, (size << 5) | 0x8000);
} else {
if(size & 1) size++;
USB_SET_EP_RX_COUNT(ep, size << 10);
}
}
void usbd_ep_setup(u8 addr, u8 type, u16 max_size, void (*callback)(u8 ep))
{
/* Translate USB standard type codes to stm32 */
const u16 typelookup[] = {
[USB_ENDPOINT_ATTR_CONTROL] = USB_EP_TYPE_CONTROL,
[USB_ENDPOINT_ATTR_ISOCHRONOUS] = USB_EP_TYPE_ISO,
[USB_ENDPOINT_ATTR_BULK] = USB_EP_TYPE_BULK,
[USB_ENDPOINT_ATTR_INTERRUPT] = USB_EP_TYPE_INTERRUPT,
};
u8 dir = addr & 0x80;
addr &= 0x7f;
/* Assign address */
USB_SET_EP_ADDR(addr, addr);
USB_SET_EP_TYPE(addr, typelookup[type]);
if(dir || (addr == 0)) {
USB_SET_EP_TX_ADDR(addr, _usbd_device.pm_top);
if(callback)
_usbd_device.user_callback_ctr[addr][USB_TRANSACTION_IN] = (void*)callback;
USB_SET_EP_TX_STAT(addr, USB_EP_TX_STAT_NAK);
_usbd_device.pm_top += max_size;
}
if(!dir) {
USB_SET_EP_RX_ADDR(addr, _usbd_device.pm_top);
usb_set_ep_rx_bufsize(addr, max_size);
if(callback)
_usbd_device.user_callback_ctr[addr][USB_TRANSACTION_OUT] = (void*)callback;
USB_SET_EP_RX_STAT(addr, USB_EP_RX_STAT_VALID);
_usbd_device.pm_top += max_size;
}
}
void _usbd_hw_endpoints_reset(void)
{
int i;
/* Reset all endpoints */
for(i = 1; i < 8; i++) {
USB_SET_EP_TX_STAT(i, USB_EP_TX_STAT_DISABLED);
USB_SET_EP_RX_STAT(i, USB_EP_RX_STAT_DISABLED);
}
_usbd_device.pm_top = 0x40 + (2*_usbd_device.desc->bMaxPacketSize0);
}
void usbd_ep_stall(u8 addr)
{
if(addr == 0)
USB_SET_EP_TX_STAT(addr, USB_EP_TX_STAT_STALL);
if(addr & 0x80)
USB_SET_EP_TX_STAT(addr, USB_EP_TX_STAT_STALL);
else
USB_SET_EP_RX_STAT(addr & 0x7F, USB_EP_RX_STAT_STALL);
}
/** Copy a data buffer to Packet Memory.
* @param PM Destination pointer into packet memory.
* @param buf Source pointer to data buffer.
* @param len Number of bytes to copy.
*/
static inline void
usb_copy_to_pm(volatile void *vPM, const void *buf, uint16_t len)
{
const uint16_t *lbuf = buf;
volatile uint16_t *PM = vPM;
for(len = (len + 1) >> 1; len; PM += 2, lbuf++, len--)
*PM = *lbuf;
}
u16 usbd_ep_write_packet(u8 addr, const void *buf, u16 len)
{
addr &= 0x7F;
if((*USB_EP_REG(addr) & USB_EP_TX_STAT) == USB_EP_TX_STAT_VALID)
return 0;
usb_copy_to_pm(USB_GET_EP_TX_BUFF(addr), buf, len);
USB_SET_EP_TX_COUNT(addr, len);
USB_SET_EP_TX_STAT(addr, USB_EP_TX_STAT_VALID);
return len;
}
/** Copy a data buffer from Packet Memory.
* @param buf Source pointer to data buffer.
* @param PM Destination pointer into packet memory.
* @param len Number of bytes to copy.
*/
static inline void
usb_copy_from_pm(void *buf, const volatile void *vPM, uint16_t len)
{
uint16_t *lbuf = buf;
const volatile uint16_t *PM = vPM;
uint8_t odd = len & 1;
for(len >>= 1; len; PM += 2, lbuf++, len--)
*lbuf = *PM;
if(odd) *(uint8_t*)lbuf = *(uint8_t*)PM;
}
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)
return 0;
len = MIN(USB_GET_EP_RX_COUNT(addr) & 0x3ff, len);
usb_copy_from_pm(buf, USB_GET_EP_RX_BUFF(addr), len);
USB_CLR_EP_RX_CTR(addr);
USB_SET_EP_RX_STAT(addr, USB_EP_RX_STAT_VALID);
return len;
}
void usbd_poll(void)
{
u16 istr = *USB_ISTR_REG;
if(istr & USB_ISTR_RESET) {
_usbd_device.pm_top = 0x40;
_usbd_reset();
USB_CLR_ISTR_RESET();
return;
}
if(istr & USB_ISTR_CTR) {
u8 ep = istr & USB_ISTR_EP_ID;
u8 type = (istr & USB_ISTR_DIR) ? 1 : 0;
if(type) { /* OUT or SETUP transaction */
type += (*USB_EP_REG(ep) & USB_EP_SETUP) ? 1 : 0;
} else { /* IN transaction */
USB_CLR_EP_TX_CTR(ep);
}
if(_usbd_device.user_callback_ctr[ep][type])
_usbd_device.user_callback_ctr[ep][type](ep);
}
if(istr & USB_ISTR_SUSP) {
USB_CLR_ISTR_SUSP();
if(_usbd_device.user_callback_suspend)
_usbd_device.user_callback_suspend();
}
if(istr & USB_ISTR_WKUP) {
USB_CLR_ISTR_WKUP();
if(_usbd_device.user_callback_resume)
_usbd_device.user_callback_resume();
}
if(istr & USB_ISTR_SOF)
USB_CLR_ISTR_SOF();
}

84
lib/usb/usb_private.h Normal file
View File

@@ -0,0 +1,84 @@
/*
* This file is part of the libopenstm32 project.
*
* Copyright (C) 2010 Gareth McMullin <gareth@blacksphere.co.nz>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __USB_PRIVATE_H
#define __USB_PRIVATE_H
#define MIN(a, b) ((a)<(b) ? (a) : (b))
/** Internal collection of device information. */
extern struct _usbd_device {
const struct usb_device_descriptor *desc;
const struct usb_config_descriptor *config;
const char **strings;
uint8_t *ctrl_buf; /**< Internal buffer used for control transfers */
uint16_t ctrl_buf_len;
uint8_t current_address;
uint8_t current_config;
uint16_t pm_top; /**< Top of allocated endpoint buffer memory */
/* User callback functions for various USB events */
void (*user_callback_reset)(void);
void (*user_callback_suspend)(void);
void (*user_callback_resume)(void);
int (*user_callback_control_command)(struct usb_setup_data *req,
void (**complete)(struct usb_setup_data *req));
int (*user_callback_control_read)(struct usb_setup_data *req,
uint8_t **buf, uint16_t *len,
void (**complete)(struct usb_setup_data *req));
int (*user_callback_control_write)(struct usb_setup_data *req,
uint8_t *buf, uint16_t len,
void (**complete)(struct usb_setup_data *req));
void (*user_callback_ctr[8][3])(uint8_t ea);
/* User callback function for some standard USB function hooks */
void (*user_callback_set_config)(uint16_t wValue);
} _usbd_device;
enum _usbd_transaction {
USB_TRANSACTION_IN,
USB_TRANSACTION_OUT,
USB_TRANSACTION_SETUP,
};
void _usbd_control_in(uint8_t ea);
void _usbd_control_out(uint8_t ea);
void _usbd_control_setup(uint8_t ea);
int _usbd_standard_request_command(struct usb_setup_data *req);
int _usbd_standard_request_read(struct usb_setup_data *req,
uint8_t **buf, uint16_t *len);
int _usbd_standard_request_write(struct usb_setup_data *req,
uint8_t *buf, uint16_t len);
void _usbd_reset(void);
/* Functions provided by the hardware abstraction */
void _usbd_hw_init(void);
void _usbd_hw_set_address(uint8_t addr);
void _usbd_hw_endpoints_reset(void);
#endif

268
lib/usb/usb_standard.c Normal file
View File

@@ -0,0 +1,268 @@
/*
* This file is part of the libopenstm32 project.
*
* Copyright (C) 2010 Gareth McMullin <gareth@blacksphere.co.nz>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <usbd.h>
#include "usb_private.h"
void usbd_register_set_config_callback(void (*callback)(uint16_t wValue))
{
_usbd_device.user_callback_set_config = callback;
}
static uint16_t
build_config_descriptor(uint8_t index, uint8_t *buf, uint16_t len)
{
uint8_t *tmpbuf = buf;
const struct usb_config_descriptor *cfg = &_usbd_device.config[index];
uint16_t count, total = 0, totallen = 0;
uint16_t i, j, k;
memcpy(buf, cfg, count = MIN(len, cfg->bLength));
buf += count; len -= count; total += count; totallen += cfg->bLength;
/* For each interface... */
for(i = 0; i < cfg->bNumInterfaces; i++) {
/* For each alternate setting... */
for(j = 0; j < cfg->interface[i].num_altsetting; j++) {
const struct usb_interface_descriptor *iface =
&cfg->interface[i].altsetting[j];
/* Copy interface descriptor */
memcpy(buf, iface, count = MIN(len, iface->bLength));
buf += count; len -= count;
total += count; totallen += iface->bLength;
/* Copy extra bytes (function descriptors) */
memcpy(buf, iface->extra,
count = MIN(len, iface->extralen));
buf += count; len -= count;
total += count; totallen += iface->extralen;
/* For each endpoint... */
for(k = 0; k < iface->bNumEndpoints; k++) {
const struct usb_endpoint_descriptor *ep =
&iface->endpoint[k];
memcpy(buf, ep, count = MIN(len, ep->bLength));
buf += count; len -= count;
total += count; totallen += ep->bLength;
}
}
}
/* Fill in wTotalLength */
*(uint16_t*)(tmpbuf+2) = totallen;
return total;
}
static int usb_standard_get_descriptor(struct usb_setup_data *req,
uint8_t **buf, uint16_t *len)
{
int i;
switch(req->wValue >> 8) {
case USB_DT_DEVICE:
*buf = (uint8_t *)_usbd_device.desc;
*len = MIN(*len, _usbd_device.desc->bLength);
return 1;
case USB_DT_CONFIGURATION: {
*buf = _usbd_device.ctrl_buf;
*len = build_config_descriptor(req->wValue & 0xff, *buf, *len);
return 1;
}
case USB_DT_STRING: {
struct usb_string_descriptor *sd =
(struct usb_string_descriptor *)_usbd_device.ctrl_buf;
if(!_usbd_device.strings)
return 0; /* Device doesn't support strings */
sd->bLength = strlen(_usbd_device.strings[req->wValue & 0xff])
* 2 + 2;
sd->bDescriptorType = USB_DT_STRING;
*buf = (uint8_t *)sd;
*len = MIN(*len, sd->bLength);
for(i = 0; i < (*len / 2) - 1; i++)
sd->wData[i] =
_usbd_device.strings[req->wValue & 0xff][i];
/* Send sane Language ID descriptor... */
if((req->wValue & 0xff) == 0)
sd->wData[0] = 0x409;
return 1;
}
}
return 0;
}
static int usb_standard_set_address(struct usb_setup_data *req)
{
/* The actual address is only latched at the STATUS IN stage */
if((req->bmRequestType != 0) || (req->wValue >= 128)) return 0;
_usbd_device.current_address = req->wValue;
return 1;
}
static int usb_standard_set_configuration(struct usb_setup_data *req)
{
/* Is this correct, or should we reset alternate settings */
if(req->wValue == _usbd_device.current_config) return 1;
_usbd_device.current_config = req->wValue;
/* Reset all endpoints */
_usbd_hw_endpoints_reset();
if(_usbd_device.user_callback_set_config)
_usbd_device.user_callback_set_config(req->wValue);
return 1;
}
static int usb_standard_get_configuration(struct usb_setup_data *req,
uint8_t **buf, uint16_t *len)
{
(void)req;
if(*len > 1) *len = 1;
(*buf)[0] = _usbd_device.current_config;
return 1;
}
static int usb_standard_set_interface(struct usb_setup_data *req)
{
(void)req;
/* FIXME: Do something meaningful here: call app */
return 1;
}
static int usb_standard_get_status(struct usb_setup_data *req, uint8_t **buf,
uint16_t *len)
{
(void)req;
/* FIXME: Return some meaningful status */
if(*len > 2) *len = 2;
(*buf)[0] = 0;
(*buf)[1] = 0;
return 1;
}
int _usbd_standard_request_command(struct usb_setup_data *req)
{
int (*command)(struct usb_setup_data *req) = NULL;
if((req->bmRequestType & 0x60) != USB_REQ_TYPE_STANDARD)
return 0;
switch(req->bRequest) {
case USB_REQ_CLEAR_FEATURE:
/* FIXME: Implement CLEAR_FEATURE */
/* TODO: Check what standard features are.
* Maybe this is the application's responsibility. */
break;
case USB_REQ_SET_ADDRESS:
/* SET ADDRESS is an exception.
* It is only processed at STATUS stage */
command = usb_standard_set_address;
break;
case USB_REQ_SET_CONFIGURATION:
command = usb_standard_set_configuration;
break;
case USB_REQ_SET_FEATURE:
/* FIXME: Implement SET_FEATURE */
/* TODO: Check what standard features are.
* Maybe this is the application's responsibility. */
break;
case USB_REQ_SET_INTERFACE:
command = usb_standard_set_interface;
break;
}
if(!command) return 0;
return command(req);
}
int _usbd_standard_request_read(struct usb_setup_data *req, uint8_t **buf,
uint16_t *len)
{
int (*command)(struct usb_setup_data *req, uint8_t **buf,
uint16_t *len) = NULL;
/* Handle standard requests */
if((req->bmRequestType & 0x60) != USB_REQ_TYPE_STANDARD)
return 0;
switch(req->bRequest) {
case USB_REQ_GET_CONFIGURATION:
command = usb_standard_get_configuration;
break;
case USB_REQ_GET_DESCRIPTOR:
command = usb_standard_get_descriptor;
break;
case USB_REQ_GET_INTERFACE:
/* FIXME: Implement GET_INTERFACE */
break;
case USB_REQ_GET_STATUS:
/* GET_STATUS always responds with zero reply.
* The application may override this behaviour. */
command = usb_standard_get_status;
break;
}
if(!command) return 0;
return command(req, buf, len);
}
int _usbd_standard_request_write(struct usb_setup_data *req, uint8_t *buf,
uint16_t len)
{
int (*command)(struct usb_setup_data *req, uint8_t *buf, uint16_t len)
= NULL;
/* Handle standard requests */
if((req->bmRequestType & 0x60) != USB_REQ_TYPE_STANDARD)
return 0;
switch(req->bRequest) {
case USB_REQ_SET_DESCRIPTOR:
/* SET_DESCRIPTOR is optional and not implemented. */
break;
case USB_REQ_SET_SYNCH_FRAME:
/* FIXME: SYNCH_FRAME is not implemented. */
/* SYNCH_FRAME is used for synchronization of isochronous
* endpoints which are not yet implemented. */
break;
}
if(!command) return 0;
return command(req, buf, len);
}