libopencm3/lib/usb/usb_f103.c

285 lines
7.3 KiB
C
Raw Normal View History

/*
* This file is part of the libopencm3 project.
*
2010-12-29 15:43:26 +00:00
* 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 <libopencm3/cm3/common.h>
#include <libopencm3/stm32/tools.h>
#include <libopencm3/stm32/usb.h>
#include <libopencm3/usb/usbd.h>
#include "usb_private.h"
static void stm32f103_usbd_init(void);
static void stm32f103_set_address(u8 addr);
static void stm32f103_ep_setup(u8 addr, u8 type, u16 max_size,
void (*callback) (u8 ep));
static void stm32f103_endpoints_reset(void);
static void stm32f103_ep_stall_set(u8 addr, u8 stall);
static u8 stm32f103_ep_stall_get(u8 addr);
static u16 stm32f103_ep_write_packet(u8 addr, const void *buf, u16 len);
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,
.ep_setup = stm32f103_ep_setup,
.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,
.ep_read_packet = stm32f103_ep_read_packet,
.poll = stm32f103_poll,
};
2010-12-29 17:00:32 +00:00
/** Initialize the USB device controller hardware of the STM32. */
static void stm32f103_usbd_init(void)
{
SET_REG(USB_CNTR_REG, 0);
SET_REG(USB_BTABLE_REG, 0);
SET_REG(USB_ISTR_REG, 0);
2010-12-29 17:00:32 +00:00
/* Enable RESET, SUSPEND, RESUME and CTR interrupts. */
SET_REG(USB_CNTR_REG, USB_CNTR_RESETM | USB_CNTR_CTRM |
2010-12-29 17:00:32 +00:00
USB_CNTR_SUSPM | USB_CNTR_WKUPM);
}
static void stm32f103_set_address(u8 addr)
{
/* Set device address and enable. */
SET_REG(USB_DADDR_REG, (addr & USB_DADDR_ADDR) | USB_DADDR_ENABLE);
}
2010-12-29 17:00:32 +00:00
/**
* Set the receive buffer size for a given USB endpoint.
*
* @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)
{
2010-12-29 17:00:32 +00:00
if (size > 62) {
if (size & 0x1f)
size -= 32;
USB_SET_EP_RX_COUNT(ep, (size << 5) | 0x8000);
} else {
2010-12-29 17:00:32 +00:00
if (size & 1)
size++;
USB_SET_EP_RX_COUNT(ep, size << 10);
}
}
static void stm32f103_ep_setup(u8 addr, u8 type, u16 max_size,
void (*callback) (u8 ep))
{
2010-12-29 17:00:32 +00:00
/* 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;
2010-12-29 17:00:32 +00:00
/* Assign address. */
USB_SET_EP_ADDR(addr, addr);
USB_SET_EP_TYPE(addr, typelookup[type]);
2010-12-29 17:00:32 +00:00
if (dir || (addr == 0)) {
USB_SET_EP_TX_ADDR(addr, _usbd_device.pm_top);
2010-12-29 17:00:32 +00:00
if (callback) {
_usbd_device.
user_callback_ctr[addr][USB_TRANSACTION_IN] =
(void *)callback;
}
USB_CLR_EP_TX_DTOG(addr);
USB_SET_EP_TX_STAT(addr, USB_EP_TX_STAT_NAK);
_usbd_device.pm_top += max_size;
2010-12-29 17:00:32 +00:00
}
if (!dir) {
USB_SET_EP_RX_ADDR(addr, _usbd_device.pm_top);
usb_set_ep_rx_bufsize(addr, max_size);
2010-12-29 17:00:32 +00:00
if (callback) {
_usbd_device.
user_callback_ctr[addr][USB_TRANSACTION_OUT] =
(void *)callback;
}
USB_CLR_EP_RX_DTOG(addr);
USB_SET_EP_RX_STAT(addr, USB_EP_RX_STAT_VALID);
_usbd_device.pm_top += max_size;
}
}
static void stm32f103_endpoints_reset(void)
{
int i;
2010-12-29 17:00:32 +00:00
/* 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);
}
2010-12-29 17:00:32 +00:00
_usbd_device.pm_top = 0x40 + (2 * _usbd_device.desc->bMaxPacketSize0);
}
static void stm32f103_ep_stall_set(u8 addr, u8 stall)
{
2010-12-29 17:00:32 +00:00
if (addr == 0)
USB_SET_EP_TX_STAT(addr, stall ? USB_EP_TX_STAT_STALL :
USB_EP_TX_STAT_NAK);
2010-12-29 17:00:32 +00:00
if (addr & 0x80) {
addr &= 0x7F;
2010-12-29 17:00:32 +00:00
USB_SET_EP_TX_STAT(addr, stall ? USB_EP_TX_STAT_STALL :
USB_EP_TX_STAT_NAK);
2010-12-29 17:00:32 +00:00
/* Reset to DATA0 if clearing stall condition. */
if (!stall)
USB_CLR_EP_TX_DTOG(addr);
} else {
2010-12-29 17:00:32 +00:00
/* Reset to DATA0 if clearing stall condition. */
if (!stall)
USB_CLR_EP_RX_DTOG(addr);
2010-12-29 17:00:32 +00:00
USB_SET_EP_RX_STAT(addr, stall ? USB_EP_RX_STAT_STALL :
USB_EP_RX_STAT_VALID);
}
2010-11-03 23:41:49 +00:00
}
static u8 stm32f103_ep_stall_get(u8 addr)
2010-11-03 23:41:49 +00:00
{
2010-12-29 17:00:32 +00:00
if (addr & 0x80) {
if ((*USB_EP_REG(addr & 0x7F) & USB_EP_TX_STAT) ==
USB_EP_TX_STAT_STALL)
return 1;
2010-12-29 17:00:32 +00:00
} else {
if ((*USB_EP_REG(addr) & USB_EP_RX_STAT) ==
USB_EP_RX_STAT_STALL)
2010-11-03 23:41:49 +00:00
return 1;
}
return 0;
}
2010-12-29 17:00:32 +00:00
/**
* 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.
*/
2010-12-29 17:00:32 +00:00
static void usb_copy_to_pm(volatile void *vPM, const void *buf, u16 len)
{
2010-12-29 17:00:32 +00:00
const u16 *lbuf = buf;
volatile u16 *PM = vPM;
2010-12-29 17:00:32 +00:00
for (len = (len + 1) >> 1; len; PM += 2, lbuf++, len--)
*PM = *lbuf;
}
static u16 stm32f103_ep_write_packet(u8 addr, const void *buf, u16 len)
{
addr &= 0x7F;
2010-12-29 17:00:32 +00:00
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;
}
2010-12-29 17:00:32 +00:00
/**
* 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.
*/
2010-12-29 17:00:32 +00:00
static void usb_copy_from_pm(void *buf, const volatile void *vPM, u16 len)
{
2010-12-29 17:00:32 +00:00
u16 *lbuf = buf;
const volatile u16 *PM = vPM;
2010-12-29 17:00:32 +00:00
u8 odd = len & 1;
2010-12-29 17:00:32 +00:00
for (len >>= 1; len; PM += 2, lbuf++, len--)
*lbuf = *PM;
2010-12-29 17:00:32 +00:00
if (odd)
*(u8 *) lbuf = *(u8 *) PM;
}
static u16 stm32f103_ep_read_packet(u8 addr, void *buf, u16 len)
{
2010-12-29 17:00:32 +00:00
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;
}
static void stm32f103_poll(void)
{
u16 istr = *USB_ISTR_REG;
2010-12-29 17:00:32 +00:00
if (istr & USB_ISTR_RESET) {
_usbd_device.pm_top = 0x40;
_usbd_reset();
USB_CLR_ISTR_RESET();
return;
2010-12-29 17:00:32 +00:00
}
2010-12-29 17:00:32 +00:00
if (istr & USB_ISTR_CTR) {
u8 ep = istr & USB_ISTR_EP_ID;
u8 type = (istr & USB_ISTR_DIR) ? 1 : 0;
2010-12-29 17:00:32 +00:00
if (type) /* OUT or SETUP transaction */
type += (*USB_EP_REG(ep) & USB_EP_SETUP) ? 1 : 0;
2010-12-29 17:00:32 +00:00
else /* IN transaction */
USB_CLR_EP_TX_CTR(ep);
2010-12-29 17:00:32 +00:00
if (_usbd_device.user_callback_ctr[ep][type])
_usbd_device.user_callback_ctr[ep][type] (ep);
else
USB_CLR_EP_RX_CTR(ep);
}
2010-12-29 17:00:32 +00:00
if (istr & USB_ISTR_SUSP) {
USB_CLR_ISTR_SUSP();
2010-12-29 17:00:32 +00:00
if (_usbd_device.user_callback_suspend)
_usbd_device.user_callback_suspend();
}
2010-12-29 17:00:32 +00:00
if (istr & USB_ISTR_WKUP) {
USB_CLR_ISTR_WKUP();
2010-12-29 17:00:32 +00:00
if (_usbd_device.user_callback_resume)
_usbd_device.user_callback_resume();
}
2010-12-29 17:00:32 +00:00
if (istr & USB_ISTR_SOF)
USB_CLR_ISTR_SOF();
}