2011-01-30 19:18:26 +00:00
|
|
|
/*
|
|
|
|
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio.
|
|
|
|
|
|
|
|
This file is part of ChibiOS/RT.
|
|
|
|
|
|
|
|
ChibiOS/RT 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.
|
|
|
|
|
|
|
|
ChibiOS/RT 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file STM32/usb_lld.c
|
|
|
|
* @brief STM32 USB subsystem low level driver source.
|
|
|
|
*
|
|
|
|
* @addtogroup USB
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2011-02-05 09:27:20 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2011-01-30 19:18:26 +00:00
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
|
|
|
#include "usb.h"
|
|
|
|
|
|
|
|
#if HAL_USE_USB || defined(__DOXYGEN__)
|
|
|
|
|
|
|
|
#define BTABLE_ADDR 0x0000
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver exported variables. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/** @brief USB1 driver identifier.*/
|
|
|
|
#if STM32_USB_USE_USB1 || defined(__DOXYGEN__)
|
|
|
|
USBDriver USBD1;
|
|
|
|
#endif
|
|
|
|
|
2011-02-05 09:27:20 +00:00
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver local variables. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief EP0 state.
|
|
|
|
*/
|
|
|
|
static USBEndpointState ep0state;
|
|
|
|
|
2011-01-30 19:18:26 +00:00
|
|
|
/**
|
|
|
|
* @brief EP0 initialization structure.
|
|
|
|
*/
|
2011-02-05 09:27:20 +00:00
|
|
|
static const USBEndpointConfig ep0config = {
|
2011-01-30 19:18:26 +00:00
|
|
|
_usb_ep0in,
|
|
|
|
_usb_ep0out,
|
|
|
|
0x40,
|
|
|
|
0x40,
|
2011-02-06 09:51:16 +00:00
|
|
|
EPR_EP_TYPE_CONTROL | EPR_STAT_TX_NAK | EPR_STAT_RX_VALID,
|
2011-01-30 19:18:26 +00:00
|
|
|
0x40,
|
|
|
|
0x80
|
|
|
|
};
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver local functions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2011-02-06 09:51:16 +00:00
|
|
|
/**
|
|
|
|
* @brief Copies a packet from memory into a packet buffer.
|
|
|
|
*
|
|
|
|
* @param[in] ep endpoint number
|
|
|
|
* @param[in] buf buffer where to fetch the endpoint data
|
|
|
|
* @param[in] n maximum number of bytes to copy
|
|
|
|
*/
|
|
|
|
static void write_packet(usbep_t ep, const uint8_t *buf, size_t n){
|
|
|
|
uint32_t *pmap;
|
|
|
|
stm32_usb_descriptor_t *udp;
|
|
|
|
size_t count;
|
|
|
|
|
|
|
|
udp = USB_GET_DESCRIPTOR(ep);
|
|
|
|
pmap = USB_ADDR2PTR(udp->TXADDR);
|
|
|
|
udp->TXCOUNT = n;
|
|
|
|
count = (n + 1) / 2;
|
|
|
|
while (count) {
|
|
|
|
*pmap++ = *(uint16_t *)buf;
|
|
|
|
buf += 2;
|
|
|
|
count--;
|
|
|
|
}
|
|
|
|
EPR_SET_STAT_TX(ep, EPR_STAT_TX_VALID);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Copies a packet from a packet buffer into memory.
|
|
|
|
*
|
|
|
|
* @param[in] ep endpoint number
|
|
|
|
* @param[in] buf buffer where to copy the endpoint data
|
|
|
|
* @param[in] n maximum number of bytes to copy
|
|
|
|
* @return The packet size.
|
|
|
|
* @retval 0 Special case, zero sized packet.
|
|
|
|
*/
|
|
|
|
static size_t read_packet(usbep_t ep, uint8_t *buf, size_t n){
|
|
|
|
uint32_t *pmap;
|
|
|
|
stm32_usb_descriptor_t *udp;
|
|
|
|
size_t count;
|
|
|
|
|
|
|
|
udp = USB_GET_DESCRIPTOR(ep);
|
|
|
|
pmap = USB_ADDR2PTR(udp->RXADDR);
|
|
|
|
count = udp->RXCOUNT & RXCOUNT_COUNT_MASK;
|
|
|
|
if (n > count)
|
|
|
|
n = count;
|
|
|
|
count = (n + 1) / 2;
|
|
|
|
while (count) {
|
|
|
|
*(uint16_t *)buf = (uint16_t)*pmap++;
|
|
|
|
buf += 2;
|
|
|
|
count--;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2011-01-30 19:18:26 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver interrupt handlers. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
#if STM32_USB_USE_USB1 || defined(__DOXYGEN__)
|
|
|
|
/**
|
|
|
|
* @brief USB high priority interrupt handler.
|
|
|
|
*
|
|
|
|
* @isr
|
|
|
|
*/
|
|
|
|
CH_IRQ_HANDLER(USB_HP_IRQHandler) {
|
|
|
|
|
|
|
|
CH_IRQ_PROLOGUE();
|
|
|
|
|
|
|
|
CH_IRQ_EPILOGUE();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief USB low priority interrupt handler.
|
|
|
|
*
|
|
|
|
* @isr
|
|
|
|
*/
|
|
|
|
CH_IRQ_HANDLER(USB_LP_IRQHandler) {
|
|
|
|
uint32_t istr;
|
2011-02-06 09:51:16 +00:00
|
|
|
size_t n;
|
2011-01-30 19:18:26 +00:00
|
|
|
USBDriver *usbp = &USBD1;
|
|
|
|
|
|
|
|
CH_IRQ_PROLOGUE();
|
|
|
|
|
|
|
|
istr = STM32_USB->ISTR;
|
|
|
|
|
|
|
|
/* USB bus reset condition handling.*/
|
|
|
|
if (istr & ISTR_RESET) {
|
|
|
|
_usb_reset(usbp);
|
2011-02-06 09:51:16 +00:00
|
|
|
if (usbp->config->event_cb)
|
|
|
|
usbp->config->event_cb(usbp, USB_EVENT_RESET);
|
2011-01-30 19:18:26 +00:00
|
|
|
STM32_USB->ISTR = ~ISTR_RESET;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SOF handling.*/
|
|
|
|
if (istr & ISTR_SOF) {
|
2011-02-06 09:51:16 +00:00
|
|
|
if (usbp->config->sof_cb)
|
|
|
|
usbp->config->sof_cb(usbp);
|
2011-01-30 19:18:26 +00:00
|
|
|
STM32_USB->ISTR = ~ISTR_SOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Endpoint events handling.*/
|
|
|
|
while (istr & ISTR_CTR) {
|
|
|
|
uint32_t ep;
|
|
|
|
uint32_t epr = STM32_USB->EPR[ep = istr & ISTR_EP_ID_MASK];
|
2011-02-06 09:51:16 +00:00
|
|
|
const USBEndpointConfig *epcp = usbp->ep[ep]->config;
|
2011-01-30 19:18:26 +00:00
|
|
|
|
|
|
|
if (epr & EPR_CTR_TX) {
|
|
|
|
/* IN endpoint, transmission.*/
|
|
|
|
EPR_CLEAR_CTR_TX(ep);
|
2011-02-06 09:51:16 +00:00
|
|
|
n = USB_GET_DESCRIPTOR(ep)->TXCOUNT;
|
|
|
|
usbp->ep[ep]->txbuf += n;
|
|
|
|
usbp->ep[ep]->txcnt += n;
|
|
|
|
usbp->ep[ep]->txsize -= n;
|
|
|
|
if (usbp->ep[ep]->txsize > 0) {
|
|
|
|
/* Transfer not completed, there are more packets to send.*/
|
|
|
|
if (usbp->ep[ep]->txsize > epcp->in_maxsize)
|
|
|
|
n = epcp->in_maxsize;
|
|
|
|
else
|
|
|
|
n = usbp->ep[ep]->txsize;
|
|
|
|
write_packet(ep, usbp->ep[ep]->txbuf, n);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Transfer completed, invoking the callback, if defined.*/
|
2011-02-06 10:02:08 +00:00
|
|
|
(usbp)->ep[ep]->transmitting = FALSE;
|
2011-02-06 09:51:16 +00:00
|
|
|
if (epcp->in_cb)
|
|
|
|
epcp->in_cb(usbp, ep);
|
|
|
|
}
|
2011-01-30 19:18:26 +00:00
|
|
|
}
|
|
|
|
if (epr & EPR_CTR_RX) {
|
|
|
|
EPR_CLEAR_CTR_RX(ep);
|
2011-02-06 09:51:16 +00:00
|
|
|
/* OUT endpoint, receive.*/
|
|
|
|
if ((epr & EPR_SETUP) && (ep == 0)) {
|
|
|
|
/* Special case, setup packet for EP0, enforcing a reset of the
|
|
|
|
EP0 state machine for robustness.*/
|
|
|
|
usbp->ep0state = USB_EP0_WAITING_SETUP;
|
|
|
|
read_packet(0, usbp->setup, 8);
|
|
|
|
epcp->out_cb(usbp, ep);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
n = read_packet(ep, usbp->ep[ep]->rxbuf, usbp->ep[ep]->rxsize);
|
|
|
|
usbp->ep[ep]->rxbuf += n;
|
|
|
|
usbp->ep[ep]->rxcnt += n;
|
|
|
|
usbp->ep[ep]->rxsize -= n;
|
|
|
|
usbp->ep[ep]->rxpkts -= 1;
|
|
|
|
if (usbp->ep[ep]->rxpkts > 0) {
|
|
|
|
/* Transfer not completed, there are more packets to receive.*/
|
|
|
|
EPR_SET_STAT_RX(ep, EPR_STAT_RX_VALID);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Transfer completed, invoking the callback, if defined.*/
|
2011-02-06 10:02:08 +00:00
|
|
|
(usbp)->ep[ep]->receiving = FALSE;
|
2011-02-06 09:51:16 +00:00
|
|
|
if (epcp->out_cb)
|
|
|
|
epcp->out_cb(usbp, ep);
|
|
|
|
}
|
|
|
|
}
|
2011-01-30 19:18:26 +00:00
|
|
|
}
|
|
|
|
istr = STM32_USB->ISTR;
|
|
|
|
}
|
|
|
|
|
|
|
|
CH_IRQ_EPILOGUE();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver exported functions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Low level USB driver initialization.
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void usb_lld_init(void) {
|
|
|
|
|
|
|
|
/* USB reset, ensures reset state in order to avoid trouble with JTAGs.*/
|
|
|
|
RCC->APB1RSTR = RCC_APB1RSTR_USBRST;
|
|
|
|
RCC->APB1RSTR = 0;
|
|
|
|
|
|
|
|
/* Driver initialization.*/
|
|
|
|
usbObjectInit(&USBD1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Configures and activates the USB peripheral.
|
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void usb_lld_start(USBDriver *usbp) {
|
|
|
|
|
2011-02-06 09:51:16 +00:00
|
|
|
if (usbp->state == USB_STOP) {
|
2011-01-30 19:18:26 +00:00
|
|
|
/* Clock activation.*/
|
|
|
|
#if STM32_USB_USE_USB1
|
|
|
|
if (&USBD1 == usbp) {
|
|
|
|
/* USB clock enabled.*/
|
|
|
|
RCC->APB1ENR |= RCC_APB1ENR_USBEN;
|
|
|
|
/* Powers up the transceiver while holding the USB in reset state.*/
|
|
|
|
STM32_USB->CNTR = CNTR_FRES;
|
|
|
|
/* Enabling the USB IRQ vectors, this also gives enough time to allow
|
|
|
|
the transceiver power up (1uS).*/
|
|
|
|
NVICEnableVector(USB_HP_CAN1_TX_IRQn,
|
|
|
|
CORTEX_PRIORITY_MASK(STM32_USB_USB1_HP_IRQ_PRIORITY));
|
|
|
|
NVICEnableVector(USB_LP_CAN1_RX0_IRQn,
|
|
|
|
CORTEX_PRIORITY_MASK(STM32_USB_USB1_LP_IRQ_PRIORITY));
|
|
|
|
|
|
|
|
/* Reset procedure enforced on driver start.*/
|
|
|
|
_usb_reset(&USBD1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/* Configuration.*/
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Deactivates the USB peripheral.
|
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void usb_lld_stop(USBDriver *usbp) {
|
|
|
|
|
|
|
|
/* If in ready state then disables the USB clock.*/
|
2011-02-06 09:51:16 +00:00
|
|
|
if (usbp->state == USB_STOP) {
|
2011-01-30 19:18:26 +00:00
|
|
|
#if STM32_ADC_USE_ADC1
|
|
|
|
if (&USBD1 == usbp) {
|
|
|
|
NVICDisableVector(USB_HP_CAN1_TX_IRQn);
|
|
|
|
NVICDisableVector(USB_LP_CAN1_RX0_IRQn);
|
|
|
|
RCC->APB1ENR &= ~RCC_APB1ENR_USBEN;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief USB low level reset routine.
|
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void usb_lld_reset(USBDriver *usbp) {
|
|
|
|
uint32_t cntr;
|
|
|
|
|
|
|
|
/* Powers up the transceiver while holding the USB in reset state.*/
|
|
|
|
STM32_USB->CNTR = CNTR_FRES;
|
|
|
|
|
|
|
|
/* Releases the USB reset, BTABLE is reset to zero.*/
|
|
|
|
STM32_USB->CNTR = 0;
|
|
|
|
STM32_USB->ISTR = 0;
|
|
|
|
STM32_USB->DADDR = DADDR_EF;
|
|
|
|
cntr = /*CNTR_ESOFM | */ CNTR_RESETM | /*CNTR_SUSPM |*/
|
|
|
|
/*CNTR_WKUPM | CNTR_ERRM | CNTR_PMAOVRM |*/ CNTR_CTRM;
|
|
|
|
/* The SOF interrupt is only enabled if a callback is defined for
|
|
|
|
this service because it is an high rate source.*/
|
2011-02-06 09:51:16 +00:00
|
|
|
if (usbp->config->sof_cb != NULL)
|
2011-01-30 19:18:26 +00:00
|
|
|
cntr |= CNTR_SOFM;
|
|
|
|
STM32_USB->CNTR = cntr;
|
2011-02-05 09:27:20 +00:00
|
|
|
|
|
|
|
/* EP0 initialization.*/
|
|
|
|
memset(&ep0state, 0, sizeof ep0state);
|
2011-02-06 09:51:16 +00:00
|
|
|
ep0state.config = &ep0config;
|
|
|
|
usbp->ep[0] = &ep0state;
|
2011-02-05 09:27:20 +00:00
|
|
|
usb_lld_init_endpoint(usbp, 0);
|
2011-01-30 19:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Sets the USB address.
|
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
2011-02-06 09:51:16 +00:00
|
|
|
void usb_lld_set_address(USBDriver *usbp) {
|
2011-01-30 19:18:26 +00:00
|
|
|
|
2011-02-06 09:51:16 +00:00
|
|
|
STM32_USB->DADDR = (uint32_t)(usbp->address) | DADDR_EF;
|
2011-01-30 19:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Enables an endpoint.
|
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
|
|
|
* @param[in] ep endpoint number
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
2011-02-05 09:27:20 +00:00
|
|
|
void usb_lld_init_endpoint(USBDriver *usbp, usbep_t ep) {
|
2011-01-30 19:18:26 +00:00
|
|
|
uint16_t nblocks;
|
|
|
|
stm32_usb_descriptor_t *dp;
|
2011-02-06 09:51:16 +00:00
|
|
|
const USBEndpointConfig *epcp = usbp->ep[ep]->config;
|
2011-01-30 19:18:26 +00:00
|
|
|
|
|
|
|
/* EPxR register setup.*/
|
2011-02-06 09:51:16 +00:00
|
|
|
EPR_SET(ep, epcp->epr | ep);
|
|
|
|
EPR_TOGGLE(ep, epcp->epr);
|
2011-01-30 19:18:26 +00:00
|
|
|
|
|
|
|
/* Endpoint size and address initialization.*/
|
2011-02-06 09:51:16 +00:00
|
|
|
if (epcp->out_maxsize > 62)
|
|
|
|
nblocks = (((((epcp->out_maxsize - 1) | 0x1f) + 1) / 32) << 10) |
|
2011-02-05 09:27:20 +00:00
|
|
|
0x8000;
|
2011-01-30 19:18:26 +00:00
|
|
|
else
|
2011-02-06 09:51:16 +00:00
|
|
|
nblocks = ((((epcp->out_maxsize - 1) | 1) + 1) / 2) << 10;
|
2011-01-30 19:18:26 +00:00
|
|
|
dp = USB_GET_DESCRIPTOR(ep);
|
|
|
|
dp->TXCOUNT = 0;
|
|
|
|
dp->RXCOUNT = nblocks;
|
2011-02-06 09:51:16 +00:00
|
|
|
dp->TXADDR = epcp->inaddr;
|
|
|
|
dp->RXADDR = epcp->outaddr;
|
2011-01-30 19:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Disables all the active endpoints except the endpoint zero.
|
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void usb_lld_disable_endpoints(USBDriver *usbp) {
|
|
|
|
unsigned i;
|
|
|
|
|
2011-02-05 09:27:20 +00:00
|
|
|
(void)usbp;
|
2011-01-30 19:18:26 +00:00
|
|
|
for (i = 1; i <= USB_ENDOPOINTS_NUMBER; i++) {
|
|
|
|
EPR_TOGGLE(i, 0);
|
|
|
|
EPR_SET(i, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-06 09:51:16 +00:00
|
|
|
* @brief Returns the status of an OUT endpoint.
|
2011-01-30 19:18:26 +00:00
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
|
|
|
* @param[in] ep endpoint number
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
2011-02-06 09:51:16 +00:00
|
|
|
usbepstatus_t usb_lld_get_status_out(USBDriver *usbp, usbep_t ep) {
|
2011-01-30 19:18:26 +00:00
|
|
|
|
2011-02-05 09:27:20 +00:00
|
|
|
(void)usbp;
|
2011-02-06 09:51:16 +00:00
|
|
|
switch (STM32_USB->EPR[ep] & EPR_STAT_RX_MASK) {
|
|
|
|
case EPR_STAT_RX_DIS:
|
2011-01-30 19:18:26 +00:00
|
|
|
return EP_STATUS_DISABLED;
|
2011-02-06 09:51:16 +00:00
|
|
|
case EPR_STAT_RX_STALL:
|
2011-01-30 19:18:26 +00:00
|
|
|
return EP_STATUS_STALLED;
|
|
|
|
default:
|
|
|
|
return EP_STATUS_ACTIVE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-06 09:51:16 +00:00
|
|
|
* @brief Returns the status of an IN endpoint.
|
2011-01-30 19:18:26 +00:00
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
|
|
|
* @param[in] ep endpoint number
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
2011-02-06 09:51:16 +00:00
|
|
|
usbepstatus_t usb_lld_get_status_in(USBDriver *usbp, usbep_t ep) {
|
2011-01-30 19:18:26 +00:00
|
|
|
|
2011-02-05 09:27:20 +00:00
|
|
|
(void)usbp;
|
2011-02-06 09:51:16 +00:00
|
|
|
switch (STM32_USB->EPR[ep] & EPR_STAT_TX_MASK) {
|
|
|
|
case EPR_STAT_TX_DIS:
|
2011-01-30 19:18:26 +00:00
|
|
|
return EP_STATUS_DISABLED;
|
2011-02-06 09:51:16 +00:00
|
|
|
case EPR_STAT_TX_STALL:
|
2011-01-30 19:18:26 +00:00
|
|
|
return EP_STATUS_STALLED;
|
|
|
|
default:
|
|
|
|
return EP_STATUS_ACTIVE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-06 09:51:16 +00:00
|
|
|
* @brief Starts a receive operation on an OUT endpoint.
|
2011-01-30 19:18:26 +00:00
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
|
|
|
* @param[in] ep endpoint number
|
2011-02-06 09:51:16 +00:00
|
|
|
* @param[out] buf buffer where to copy the endpoint data
|
|
|
|
* @param[in] n maximum number of bytes to copy in the buffer
|
2011-01-30 19:18:26 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
2011-02-06 09:51:16 +00:00
|
|
|
void usb_lld_start_out(USBDriver *usbp, usbep_t ep,
|
|
|
|
uint8_t *buf, size_t n) {
|
|
|
|
USBEndpointState *uesp = usbp->ep[ep];
|
|
|
|
|
|
|
|
uesp->rxbuf = buf;
|
|
|
|
uesp->rxsize = n;
|
|
|
|
uesp->rxcnt = 0;
|
|
|
|
if (uesp->rxsize == 0) /* Special case for zero sized packets.*/
|
|
|
|
uesp->rxpkts = 1;
|
|
|
|
else
|
|
|
|
uesp->rxpkts = (uint16_t)((n + uesp->config->out_maxsize - 1) /
|
|
|
|
uesp->config->out_maxsize);
|
|
|
|
EPR_SET_STAT_RX(ep, EPR_STAT_RX_VALID);
|
|
|
|
}
|
2011-01-30 19:18:26 +00:00
|
|
|
|
2011-02-06 09:51:16 +00:00
|
|
|
/**
|
|
|
|
* @brief Starts a transmit operation on an IN endpoint.
|
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
|
|
|
* @param[in] ep endpoint number
|
|
|
|
* @param[in] buf buffer where to fetch the endpoint data
|
|
|
|
* @param[in] n maximum number of bytes to copy
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void usb_lld_start_in(USBDriver *usbp, usbep_t ep,
|
|
|
|
const uint8_t *buf, size_t n) {
|
|
|
|
USBEndpointState *uesp = usbp->ep[ep];
|
|
|
|
|
|
|
|
uesp->txbuf = buf;
|
|
|
|
uesp->txsize = n;
|
|
|
|
uesp->txcnt = 0;
|
|
|
|
if (n > (size_t)uesp->config->in_maxsize)
|
|
|
|
n = (size_t)uesp->config->in_maxsize;
|
|
|
|
write_packet(ep, buf, n);
|
2011-01-30 19:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Brings an OUT endpoint in the stalled state.
|
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
|
|
|
* @param[in] ep endpoint number
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void usb_lld_stall_out(USBDriver *usbp, usbep_t ep) {
|
|
|
|
|
2011-02-05 09:27:20 +00:00
|
|
|
(void)usbp;
|
2011-01-30 19:18:26 +00:00
|
|
|
EPR_SET_STAT_RX(ep, EPR_STAT_RX_STALL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-06 09:51:16 +00:00
|
|
|
* @brief Brings an IN endpoint in the stalled state.
|
2011-01-30 19:18:26 +00:00
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
|
|
|
* @param[in] ep endpoint number
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
2011-02-06 09:51:16 +00:00
|
|
|
void usb_lld_stall_in(USBDriver *usbp, usbep_t ep) {
|
2011-01-30 19:18:26 +00:00
|
|
|
|
2011-02-05 09:27:20 +00:00
|
|
|
(void)usbp;
|
2011-02-06 09:51:16 +00:00
|
|
|
EPR_SET_STAT_TX(ep, EPR_STAT_TX_STALL);
|
2011-01-30 19:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Brings an OUT endpoint in the active state.
|
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
|
|
|
* @param[in] ep endpoint number
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void usb_lld_clear_out(USBDriver *usbp, usbep_t ep) {
|
|
|
|
|
2011-02-05 09:27:20 +00:00
|
|
|
(void)usbp;
|
|
|
|
|
2011-01-30 19:18:26 +00:00
|
|
|
/* Makes sure to not put to NAK an endpoint that is already
|
|
|
|
transferring.*/
|
|
|
|
if ((STM32_USB->EPR[ep] & EPR_STAT_RX_MASK) != EPR_STAT_RX_VALID)
|
|
|
|
EPR_SET_STAT_TX(ep, EPR_STAT_RX_NAK);
|
|
|
|
}
|
|
|
|
|
2011-02-06 09:51:16 +00:00
|
|
|
/**
|
|
|
|
* @brief Brings an IN endpoint in the active state.
|
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
|
|
|
* @param[in] ep endpoint number
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void usb_lld_clear_in(USBDriver *usbp, usbep_t ep) {
|
|
|
|
|
|
|
|
(void)usbp;
|
|
|
|
|
|
|
|
/* Makes sure to not put to NAK an endpoint that is already
|
|
|
|
transferring.*/
|
|
|
|
if ((STM32_USB->EPR[ep] & EPR_STAT_TX_MASK) != EPR_STAT_TX_VALID)
|
|
|
|
EPR_SET_STAT_TX(ep, EPR_STAT_TX_NAK);
|
|
|
|
}
|
|
|
|
|
2011-01-30 19:18:26 +00:00
|
|
|
#endif /* HAL_USE_USB */
|
|
|
|
|
|
|
|
/** @} */
|