Minor cleanup of usb. Template driver for STM32F107 added.

This commit is contained in:
Gareth McMullin 2011-02-10 19:58:51 +13:00
parent dfa9145a34
commit e64a9d2bf9
7 changed files with 177 additions and 13 deletions

View File

@ -21,7 +21,7 @@
#define LIBOPENCM3_OTG_FS_H
#include <libopencm3/stm32/memorymap.h>
#include <libopencm3/common.h>
#include <libopencm3/cm3/common.h>
/* Core Global Control and Status Registers */
#define OTG_FS_OTGCTL MMIO32(USB_OTG_FS_BASE + 0x000)

View File

@ -24,6 +24,7 @@
typedef struct _usbd_driver usbd_driver;
extern const usbd_driver stm32f103_usb_driver;
extern const usbd_driver stm32f107_usb_driver;
/* Static buffer for control transactions:
* This is defined as weak in the library, applicaiton

View File

@ -31,7 +31,7 @@ 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 scb.o ethernet.o \
usb_f103.o usb.o usb_control.o usb_standard.o can.o \
timer.o
timer.o usb_f107.o
VPATH += ../usb

View File

@ -46,7 +46,7 @@ int usbd_init(const usbd_driver *driver,
const struct usb_device_descriptor *dev,
const struct usb_config_descriptor *conf, const char **strings)
{
_usbd_device.driver = (usbd_driver *)driver;
_usbd_device.driver = driver;
_usbd_device.desc = dev;
_usbd_device.config = conf;
_usbd_device.strings = strings;

View File

@ -35,10 +35,10 @@ static u16 stm32f103_ep_read_packet(u8 addr, void *buf, u16 len);
static void stm32f103_poll(void);
const struct _usbd_driver stm32f103_usb_driver = {
._init = stm32f103_usbd_init,
._set_address = stm32f103_set_address,
.init = stm32f103_usbd_init,
.set_address = stm32f103_set_address,
.ep_setup = stm32f103_ep_setup,
._ep_reset = stm32f103_endpoints_reset,
.ep_reset = stm32f103_endpoints_reset,
.ep_stall_set = stm32f103_ep_stall_set,
.ep_stall_get = stm32f103_ep_stall_get,
.ep_write_packet = stm32f103_ep_write_packet,

163
lib/usb/usb_f107.c Normal file
View File

@ -0,0 +1,163 @@
/*
* This file is part of the libopencm3 project.
*
* Copyright (C) 2011 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 <libopencm3/cm3/common.h>
#include <libopencm3/stm32/tools.h>
#include <libopencm3/stm32/otg_fs.h>
#include <libopencm3/usb/usbd.h>
#include "usb_private.h"
static void stm32f107_usbd_init(void);
static void stm32f107_set_address(u8 addr);
static void stm32f107_ep_setup(u8 addr, u8 type, u16 max_size,
void (*callback) (u8 ep));
static void stm32f107_endpoints_reset(void);
static void stm32f107_ep_stall_set(u8 addr, u8 stall);
static u8 stm32f107_ep_stall_get(u8 addr);
static u16 stm32f107_ep_write_packet(u8 addr, const void *buf, u16 len);
static u16 stm32f107_ep_read_packet(u8 addr, void *buf, u16 len);
static void stm32f107_poll(void);
const struct _usbd_driver stm32f107_usb_driver = {
.init = stm32f107_usbd_init,
.set_address = stm32f107_set_address,
.ep_setup = stm32f107_ep_setup,
.ep_reset = stm32f107_endpoints_reset,
.ep_stall_set = stm32f107_ep_stall_set,
.ep_stall_get = stm32f107_ep_stall_get,
.ep_write_packet = stm32f107_ep_write_packet,
.ep_read_packet = stm32f107_ep_read_packet,
.poll = stm32f107_poll,
};
/** Initialize the USB device controller hardware of the STM32. */
static void stm32f107_usbd_init(void)
{
/* TODO: Enable interrupts on Reset, Transfer, Suspend and Resume */
}
static void stm32f107_set_address(u8 addr)
{
(void)addr;
/* TODO: Set device address and enable. */
}
static void stm32f107_ep_setup(u8 addr, u8 type, u16 max_size,
void (*callback) (u8 ep))
{
/* TODO: Configure endpoint address and type.
* Allocate FIFO memory for endpoint.
* Install callback funciton.
*/
(void)type; (void)max_size;
u8 dir = addr & 0x80;
addr &= 0x7f;
if (dir || (addr == 0)) {
if (callback) {
_usbd_device.
user_callback_ctr[addr][USB_TRANSACTION_IN] =
(void *)callback;
}
}
if (!dir) {
if (callback) {
_usbd_device.
user_callback_ctr[addr][USB_TRANSACTION_OUT] =
(void *)callback;
}
}
}
static void stm32f107_endpoints_reset(void)
{
/* TODO: Reset all endpoints. */
}
static void stm32f107_ep_stall_set(u8 addr, u8 stall)
{
/* TODO: set or clear stall condition */
(void)addr; (void)stall;
}
static u8 stm32f107_ep_stall_get(u8 addr)
{
/* TODO: return 1 if STALL set. */
(void)addr;
return 0;
}
static u16 stm32f107_ep_write_packet(u8 addr, const void *buf, u16 len)
{
addr &= 0x7F;
/* TODO: Send packet on endpoint */
(void)buf;
return len;
}
static u16 stm32f107_ep_read_packet(u8 addr, void *buf, u16 len)
{
/* TODO: Read packet from endpoint */
(void)addr; (void)buf; (void)len;
return len;
}
static void stm32f107_poll(void)
{
/* TODO: Read interrupt status register */
/* TODO: Handle USB RESET condition */
if (0) {
_usbd_reset();
return;
}
/* TODO: Handle transfer complete condition */
if (0) {
u8 ep;
u8 type;
if (_usbd_device.user_callback_ctr[ep][type])
_usbd_device.user_callback_ctr[ep][type] (ep);
/* TODO: clear any interrupt flag */
}
/* TODO: Handle suspend condition */
if (0) {
/* TODO: Clear suspend interrupt flag */
if (_usbd_device.user_callback_suspend)
_usbd_device.user_callback_suspend();
}
/* TODO: Handle wakeup condition */
if (0) {
/* TODO: Clear wakeup interrupt flag */
if (_usbd_device.user_callback_resume)
_usbd_device.user_callback_resume();
}
/* TODO: Handle SOF condition */
}

View File

@ -54,7 +54,7 @@ extern struct _usbd_device {
/* User callback function for some standard USB function hooks */
void (*user_callback_set_config)(u16 wValue);
struct _usbd_driver *driver;
const struct _usbd_driver *driver;
} _usbd_device;
enum _usbd_transaction {
@ -73,10 +73,10 @@ void _usbd_reset(void);
/* Functions provided by the hardware abstraction. */
struct _usbd_driver {
void (*_init)(void);
void (*_set_address)(u8 addr);
void (*init)(void);
void (*set_address)(u8 addr);
void (*ep_setup)(u8 addr, u8 type, u16 max_size, void (*cb)(u8 ep));
void (*_ep_reset)(void);
void (*ep_reset)(void);
void (*ep_stall_set)(u8 addr, u8 stall);
u8 (*ep_stall_get)(u8 addr);
u16 (*ep_write_packet)(u8 addr, const void *buf, u16 len);
@ -84,8 +84,8 @@ struct _usbd_driver {
void (*poll)(void);
};
#define _usbd_hw_init() _usbd_device.driver->_init()
#define _usbd_hw_set_address(addr) _usbd_device.driver->_set_address(addr)
#define _usbd_hw_endpoints_reset() _usbd_device.driver->_ep_reset()
#define _usbd_hw_init() _usbd_device.driver->init()
#define _usbd_hw_set_address(addr) _usbd_device.driver->set_address(addr)
#define _usbd_hw_endpoints_reset() _usbd_device.driver->ep_reset()
#endif