2012-03-26 13:45:36 +00:00
|
|
|
/*
|
|
|
|
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
|
|
|
|
2011,2012 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/OTGv1/usb_lld.c
|
|
|
|
* @brief STM32 USB subsystem low level driver source.
|
|
|
|
*
|
|
|
|
* @addtogroup USB
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
|
|
|
|
|
|
|
#if HAL_USE_USB || defined(__DOXYGEN__)
|
|
|
|
|
2012-03-27 15:37:06 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver local definitions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
#define TRDT_VALUE 5
|
|
|
|
|
2012-03-26 13:45:36 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver exported variables. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/** @brief OTG1 driver identifier.*/
|
|
|
|
#if STM32_USB_USE_OTG1 || defined(__DOXYGEN__)
|
|
|
|
USBDriver USBD1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver local variables. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief EP0 state.
|
|
|
|
* @note It is an union because IN and OUT endpoints are never used at the
|
|
|
|
* same time for EP0.
|
|
|
|
*/
|
|
|
|
static union {
|
|
|
|
/**
|
|
|
|
* @brief IN EP0 state.
|
|
|
|
*/
|
|
|
|
USBInEndpointState in;
|
|
|
|
/**
|
|
|
|
* @brief OUT EP0 state.
|
|
|
|
*/
|
|
|
|
USBOutEndpointState out;
|
|
|
|
} ep0_state;
|
|
|
|
|
2012-04-10 17:15:46 +00:00
|
|
|
/**
|
|
|
|
* @brief Buffer for the EP0 setup packets.
|
|
|
|
*/
|
2012-06-03 09:18:38 +00:00
|
|
|
static uint8_t ep0setup_buffer[8];
|
2012-04-10 17:15:46 +00:00
|
|
|
|
2012-03-26 13:45:36 +00:00
|
|
|
/**
|
|
|
|
* @brief EP0 initialization structure.
|
|
|
|
*/
|
|
|
|
static const USBEndpointConfig ep0config = {
|
2012-06-10 16:31:03 +00:00
|
|
|
USB_EP_MODE_TYPE_CTRL,
|
2012-03-26 13:45:36 +00:00
|
|
|
_usb_ep0setup,
|
|
|
|
_usb_ep0in,
|
|
|
|
_usb_ep0out,
|
|
|
|
0x40,
|
|
|
|
0x40,
|
|
|
|
&ep0_state.in,
|
2012-04-10 17:15:46 +00:00
|
|
|
&ep0_state.out,
|
2012-08-13 10:14:45 +00:00
|
|
|
1,
|
2012-06-03 09:18:38 +00:00
|
|
|
ep0setup_buffer
|
2012-03-26 13:45:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver local functions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2012-03-29 17:01:20 +00:00
|
|
|
static void otg_core_reset(void) {
|
|
|
|
|
|
|
|
/* Wait AHB idle condition.*/
|
|
|
|
while ((OTG->GRSTCTL & GRSTCTL_AHBIDL) == 0)
|
|
|
|
;
|
|
|
|
/* Core reset and delay of at least 3 PHY cycles.*/
|
|
|
|
OTG->GRSTCTL = GRSTCTL_CSRST;
|
|
|
|
while ((OTG->GRSTCTL & GRSTCTL_CSRST) != 0)
|
|
|
|
;
|
|
|
|
halPolledDelay(12);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void otg_disable_ep(void) {
|
2012-03-28 14:19:40 +00:00
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 0; i <= USB_MAX_ENDPOINTS; i++) {
|
|
|
|
/* Disable only if enabled because this sentence in the manual:
|
|
|
|
"The application must set this bit only if Endpoint Enable is
|
|
|
|
already set for this endpoint".*/
|
|
|
|
if ((OTG->ie[i].DIEPCTL & DIEPCTL_EPENA) != 0) {
|
|
|
|
OTG->ie[i].DIEPCTL = DIEPCTL_EPDIS;
|
|
|
|
/* Wait for endpoint disable.*/
|
|
|
|
while (!(OTG->ie[i].DIEPINT & DIEPINT_EPDISD))
|
|
|
|
;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
OTG->ie[i].DIEPCTL = 0;
|
|
|
|
OTG->ie[i].DIEPTSIZ = 0;
|
2012-04-10 17:15:46 +00:00
|
|
|
OTG->ie[i].DIEPINT = 0xFFFFFFFF;
|
2012-03-28 14:19:40 +00:00
|
|
|
/* Disable only if enabled because this sentence in the manual:
|
|
|
|
"The application must set this bit only if Endpoint Enable is
|
|
|
|
already set for this endpoint".
|
|
|
|
Note that the attempt to disable the OUT EP0 is ignored by the
|
|
|
|
hardware but the code is simpler this way.*/
|
|
|
|
if ((OTG->oe[i].DOEPCTL & DOEPCTL_EPENA) != 0) {
|
|
|
|
OTG->oe[i].DOEPCTL = DOEPCTL_EPDIS;
|
|
|
|
/* Wait for endpoint disable.*/
|
|
|
|
while (!(OTG->oe[i].DOEPINT & DOEPINT_OTEPDIS))
|
|
|
|
;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
OTG->oe[i].DOEPCTL = 0;
|
|
|
|
OTG->oe[i].DOEPTSIZ = 0;
|
2012-04-10 17:15:46 +00:00
|
|
|
OTG->oe[i].DOEPINT = 0xFFFFFFFF;
|
2012-03-28 14:19:40 +00:00
|
|
|
}
|
2012-08-14 06:58:24 +00:00
|
|
|
OTG->DAINTMSK = DAINTMSK_OEPM(0) | DAINTMSK_IEPM(0);
|
2012-03-28 14:19:40 +00:00
|
|
|
}
|
|
|
|
|
2012-04-10 17:15:46 +00:00
|
|
|
static void otg_rxfifo_flush(void) {
|
|
|
|
|
|
|
|
OTG->GRSTCTL = GRSTCTL_RXFFLSH;
|
|
|
|
while ((OTG->GRSTCTL & GRSTCTL_RXFFLSH) != 0)
|
|
|
|
;
|
2012-06-13 16:13:00 +00:00
|
|
|
/* Wait for 3 PHY Clocks.*/
|
2012-06-09 09:08:05 +00:00
|
|
|
halPolledDelay(12);
|
2012-04-10 17:15:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void otg_txfifo_flush(uint32_t fifo) {
|
|
|
|
|
|
|
|
OTG->GRSTCTL = GRSTCTL_TXFNUM(fifo) | GRSTCTL_TXFFLSH;
|
|
|
|
while ((OTG->GRSTCTL & GRSTCTL_TXFFLSH) != 0)
|
|
|
|
;
|
2012-06-13 16:13:00 +00:00
|
|
|
/* Wait for 3 PHY Clocks.*/
|
2012-06-09 09:08:05 +00:00
|
|
|
halPolledDelay(12);
|
2012-04-10 17:15:46 +00:00
|
|
|
}
|
|
|
|
|
2012-03-26 13:45:36 +00:00
|
|
|
/**
|
2012-04-10 17:15:46 +00:00
|
|
|
* @brief Resets the FIFO RAM memory allocator.
|
2012-03-26 13:45:36 +00:00
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
2012-03-31 06:41:19 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
2012-03-26 13:45:36 +00:00
|
|
|
*/
|
2012-04-10 17:15:46 +00:00
|
|
|
static void otg_ram_reset(USBDriver *usbp) {
|
2012-03-26 13:45:36 +00:00
|
|
|
|
2012-04-10 17:15:46 +00:00
|
|
|
usbp->pmnext = STM32_USB_OTG1_RX_FIFO_SIZE / 4;
|
2012-03-26 13:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-04-10 17:15:46 +00:00
|
|
|
* @brief Allocates a block from the FIFO RAM memory.
|
2012-03-26 13:45:36 +00:00
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
2012-04-10 17:15:46 +00:00
|
|
|
* @param[in] size size of the packet buffer to allocate in words
|
2012-03-31 06:41:19 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
2012-03-26 13:45:36 +00:00
|
|
|
*/
|
2012-04-10 17:15:46 +00:00
|
|
|
static uint32_t otg_ram_alloc(USBDriver *usbp, size_t size) {
|
|
|
|
uint32_t next;
|
|
|
|
|
|
|
|
next = usbp->pmnext;
|
|
|
|
usbp->pmnext += size;
|
|
|
|
chDbgAssert(usbp->pmnext <= STM32_OTG_FIFO_MEM_SIZE,
|
|
|
|
"otg_fifo_alloc(), #1", "FIFO memory overflow");
|
|
|
|
return next;
|
2012-03-26 13:45:36 +00:00
|
|
|
}
|
2012-06-16 06:22:39 +00:00
|
|
|
|
2012-06-13 16:13:00 +00:00
|
|
|
/**
|
|
|
|
* @brief Pushes a series of words into a FIFO.
|
|
|
|
*
|
|
|
|
* @param[in] fifop pointer to the FIFO register
|
|
|
|
* @param[in] buf pointer to the words buffer, not necessarily word
|
|
|
|
* aligned
|
|
|
|
* @param[in] n number of words to push
|
|
|
|
*
|
|
|
|
* @return A pointer after the last word pushed.
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
static uint8_t *otg_do_push(volatile uint32_t *fifop, uint8_t *buf, size_t n) {
|
|
|
|
|
|
|
|
while (n > 0) {
|
|
|
|
/* Note, this line relies on the Cortex-M3/M4 ability to perform
|
|
|
|
unaligned word accesses and on the LSB-first memory organization.*/
|
|
|
|
*fifop = *((uint32_t *)buf);
|
|
|
|
buf += 4;
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
2012-03-26 13:45:36 +00:00
|
|
|
|
2012-03-31 06:41:19 +00:00
|
|
|
/**
|
|
|
|
* @brief Writes to a TX FIFO.
|
|
|
|
*
|
|
|
|
* @param[in] ep endpoint number
|
|
|
|
* @param[in] buf buffer where to copy the endpoint data
|
|
|
|
* @param[in] n maximum number of bytes to copy
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
2012-06-11 16:54:35 +00:00
|
|
|
static void otg_fifo_write_from_buffer(usbep_t ep,
|
|
|
|
const uint8_t *buf,
|
|
|
|
size_t n) {
|
2012-03-31 06:41:19 +00:00
|
|
|
|
2012-06-13 16:13:00 +00:00
|
|
|
otg_do_push(OTG_FIFO(ep), (uint8_t *)buf, (n + 3) / 4);
|
2012-03-31 06:41:19 +00:00
|
|
|
}
|
|
|
|
|
2012-06-11 16:54:35 +00:00
|
|
|
/**
|
|
|
|
* @brief Writes to a TX FIFO fetching data from a queue.
|
|
|
|
*
|
|
|
|
* @param[in] ep endpoint number
|
|
|
|
* @param[in] oqp pointer to an @p OutputQueue object
|
|
|
|
* @param[in] n maximum number of bytes to copy
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
static void otg_fifo_write_from_queue(usbep_t ep,
|
|
|
|
OutputQueue *oqp,
|
|
|
|
size_t n) {
|
2012-06-13 16:13:00 +00:00
|
|
|
size_t ntogo;
|
2012-06-11 16:54:35 +00:00
|
|
|
volatile uint32_t *fifop;
|
|
|
|
|
|
|
|
fifop = OTG_FIFO(ep);
|
|
|
|
|
2012-06-13 16:13:00 +00:00
|
|
|
ntogo = n;
|
|
|
|
while (ntogo > 0) {
|
2012-06-16 15:04:12 +00:00
|
|
|
uint32_t w, i;
|
2012-06-13 18:32:03 +00:00
|
|
|
size_t nw = ntogo / 4;
|
|
|
|
|
|
|
|
if (nw > 0) {
|
|
|
|
size_t streak;
|
|
|
|
uint32_t nw2end = (oqp->q_top - oqp->q_rdptr) / 4;
|
|
|
|
|
|
|
|
ntogo -= (streak = nw <= nw2end ? nw : nw2end) * 4;
|
|
|
|
oqp->q_rdptr = otg_do_push(fifop, oqp->q_rdptr, streak);
|
|
|
|
if (oqp->q_rdptr >= oqp->q_top) {
|
|
|
|
oqp->q_rdptr = oqp->q_buffer;
|
|
|
|
continue;
|
|
|
|
}
|
2012-06-13 16:13:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If this condition is not satisfied then there is a word lying across
|
2012-06-17 05:03:56 +00:00
|
|
|
queue circular buffer boundary or there are some remaining bytes.*/
|
2012-06-13 16:13:00 +00:00
|
|
|
if (ntogo <= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* One byte at time.*/
|
2012-06-16 15:04:12 +00:00
|
|
|
w = 0;
|
2012-06-13 16:13:00 +00:00
|
|
|
i = 0;
|
|
|
|
while ((ntogo > 0) && (i < 4)) {
|
2012-06-16 15:04:12 +00:00
|
|
|
w |= (uint32_t)*oqp->q_rdptr++ << (i * 8);
|
2012-06-13 16:13:00 +00:00
|
|
|
if (oqp->q_rdptr >= oqp->q_top)
|
|
|
|
oqp->q_rdptr = oqp->q_buffer;
|
|
|
|
ntogo--;
|
|
|
|
i++;
|
|
|
|
}
|
2012-06-17 05:03:56 +00:00
|
|
|
*fifop = w;
|
2012-06-13 16:13:00 +00:00
|
|
|
}
|
2012-06-11 16:54:35 +00:00
|
|
|
|
|
|
|
/* Updating queue.*/
|
|
|
|
chSysLockFromIsr();
|
|
|
|
oqp->q_counter += n;
|
|
|
|
while (notempty(&oqp->q_waiting))
|
|
|
|
chSchReadyI(fifo_remove(&oqp->q_waiting))->p_u.rdymsg = Q_OK;
|
|
|
|
chSysUnlockFromIsr();
|
|
|
|
}
|
|
|
|
|
2012-06-13 16:13:00 +00:00
|
|
|
/**
|
|
|
|
* @brief Pops a series of words from a FIFO.
|
|
|
|
*
|
|
|
|
* @param[in] fifop pointer to the FIFO register
|
|
|
|
* @param[in] buf pointer to the words buffer, not necessarily word
|
|
|
|
* aligned
|
|
|
|
* @param[in] n number of words to push
|
|
|
|
*
|
|
|
|
* @return A pointer after the last word pushed.
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
static uint8_t *otg_do_pop(volatile uint32_t *fifop, uint8_t *buf, size_t n) {
|
|
|
|
|
|
|
|
while (n > 0) {
|
2012-06-16 15:04:12 +00:00
|
|
|
uint32_t w = *fifop;
|
2012-06-13 16:13:00 +00:00
|
|
|
/* Note, this line relies on the Cortex-M3/M4 ability to perform
|
|
|
|
unaligned word accesses and on the LSB-first memory organization.*/
|
2012-06-16 15:04:12 +00:00
|
|
|
*((uint32_t *)buf) = w;
|
2012-06-13 16:13:00 +00:00
|
|
|
buf += 4;
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2012-03-31 06:41:19 +00:00
|
|
|
/**
|
|
|
|
* @brief Reads a packet from the RXFIFO.
|
|
|
|
*
|
|
|
|
* @param[out] buf buffer where to copy the endpoint data
|
|
|
|
* @param[in] n number of bytes to pull from the FIFO
|
|
|
|
* @param[in] max number of bytes to copy into the buffer
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
2012-06-11 16:54:35 +00:00
|
|
|
static void otg_fifo_read_to_buffer(uint8_t *buf, size_t n, size_t max) {
|
2012-03-31 06:41:19 +00:00
|
|
|
volatile uint32_t *fifop;
|
|
|
|
|
|
|
|
fifop = OTG_FIFO(0);
|
|
|
|
n = (n + 3) / 4;
|
|
|
|
max = (max + 3) / 4;
|
|
|
|
while (n) {
|
2012-06-16 15:04:12 +00:00
|
|
|
uint32_t w = *fifop;
|
2012-03-31 06:41:19 +00:00
|
|
|
if (max) {
|
2012-06-13 16:13:00 +00:00
|
|
|
/* Note, this line relies on the Cortex-M3/M4 ability to perform
|
|
|
|
unaligned word accesses and on the LSB-first memory organization.*/
|
2012-06-16 15:04:12 +00:00
|
|
|
*((uint32_t *)buf) = w;
|
2012-06-13 16:13:00 +00:00
|
|
|
buf += 4;
|
2012-03-31 06:41:19 +00:00
|
|
|
max--;
|
|
|
|
}
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-11 16:54:35 +00:00
|
|
|
/**
|
|
|
|
* @brief Reads a packet from the RXFIFO.
|
|
|
|
*
|
2012-06-16 14:08:20 +00:00
|
|
|
* @param[in] iqp pointer to an @p InputQueue object
|
2012-06-11 16:54:35 +00:00
|
|
|
* @param[in] n number of bytes to pull from the FIFO
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
static void otg_fifo_read_to_queue(InputQueue *iqp, size_t n) {
|
2012-06-13 16:13:00 +00:00
|
|
|
size_t ntogo;
|
2012-06-11 16:54:35 +00:00
|
|
|
volatile uint32_t *fifop;
|
|
|
|
|
|
|
|
fifop = OTG_FIFO(0);
|
2012-06-11 19:23:28 +00:00
|
|
|
|
2012-06-13 16:13:00 +00:00
|
|
|
ntogo = n;
|
|
|
|
while (ntogo > 0) {
|
2012-06-16 15:04:12 +00:00
|
|
|
uint32_t w, i;
|
2012-06-13 18:32:03 +00:00
|
|
|
size_t nw = ntogo / 4;
|
|
|
|
|
|
|
|
if (nw > 0) {
|
|
|
|
size_t streak;
|
|
|
|
uint32_t nw2end = (iqp->q_wrptr - iqp->q_wrptr) / 4;
|
|
|
|
|
|
|
|
ntogo -= (streak = nw <= nw2end ? nw : nw2end) * 4;
|
|
|
|
iqp->q_wrptr = otg_do_pop(fifop, iqp->q_wrptr, streak);
|
|
|
|
if (iqp->q_wrptr >= iqp->q_top) {
|
|
|
|
iqp->q_wrptr = iqp->q_buffer;
|
|
|
|
continue;
|
|
|
|
}
|
2012-06-13 16:13:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If this condition is not satisfied then there is a word lying across
|
2012-06-17 05:03:56 +00:00
|
|
|
queue circular buffer boundary or there are some remaining bytes.*/
|
2012-06-13 16:13:00 +00:00
|
|
|
if (ntogo <= 0)
|
|
|
|
break;
|
2012-06-11 19:23:28 +00:00
|
|
|
|
2012-06-13 16:13:00 +00:00
|
|
|
/* One byte at time.*/
|
2012-06-16 15:04:12 +00:00
|
|
|
w = *fifop;
|
2012-06-13 16:13:00 +00:00
|
|
|
i = 0;
|
|
|
|
while ((ntogo > 0) && (i < 4)) {
|
2012-06-16 15:04:12 +00:00
|
|
|
*iqp->q_wrptr++ = (uint8_t)(w >> (i * 8));
|
2012-06-13 16:13:00 +00:00
|
|
|
if (iqp->q_wrptr >= iqp->q_top)
|
|
|
|
iqp->q_wrptr = iqp->q_buffer;
|
|
|
|
ntogo--;
|
|
|
|
i++;
|
2012-06-11 19:21:25 +00:00
|
|
|
}
|
2012-06-13 16:13:00 +00:00
|
|
|
}
|
2012-06-11 19:21:25 +00:00
|
|
|
|
|
|
|
/* Updating queue.*/
|
|
|
|
chSysLockFromIsr();
|
|
|
|
iqp->q_counter += n;
|
|
|
|
while (notempty(&iqp->q_waiting))
|
|
|
|
chSchReadyI(fifo_remove(&iqp->q_waiting))->p_u.rdymsg = Q_OK;
|
|
|
|
chSysUnlockFromIsr();
|
2012-06-11 16:54:35 +00:00
|
|
|
}
|
|
|
|
|
2012-03-31 06:41:19 +00:00
|
|
|
/**
|
|
|
|
* @brief Incoming packets handler.
|
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
static void otg_rxfifo_handler(USBDriver *usbp) {
|
|
|
|
uint32_t sts, cnt, ep;
|
|
|
|
|
|
|
|
sts = OTG->GRXSTSP;
|
|
|
|
switch (sts & GRXSTSP_PKTSTS_MASK) {
|
|
|
|
case GRXSTSP_SETUP_COMP:
|
|
|
|
break;
|
|
|
|
case GRXSTSP_SETUP_DATA:
|
|
|
|
cnt = (sts & GRXSTSP_BCNT_MASK) >> GRXSTSP_BCNT_OFF;
|
|
|
|
ep = (sts & GRXSTSP_EPNUM_MASK) >> GRXSTSP_EPNUM_OFF;
|
2012-06-11 16:54:35 +00:00
|
|
|
otg_fifo_read_to_buffer(usbp->epc[ep]->setup_buf, cnt, 8);
|
2012-03-31 06:41:19 +00:00
|
|
|
break;
|
|
|
|
case GRXSTSP_OUT_DATA:
|
|
|
|
cnt = (sts & GRXSTSP_BCNT_MASK) >> GRXSTSP_BCNT_OFF;
|
|
|
|
ep = (sts & GRXSTSP_EPNUM_MASK) >> GRXSTSP_EPNUM_OFF;
|
2012-06-11 16:54:35 +00:00
|
|
|
if (usbp->epc[ep]->out_state->rxqueued) {
|
|
|
|
/* Queue associated.*/
|
2012-06-11 19:21:25 +00:00
|
|
|
otg_fifo_read_to_queue(usbp->epc[ep]->out_state->mode.queue.rxqueue,
|
|
|
|
cnt);
|
2012-06-11 16:54:35 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
otg_fifo_read_to_buffer(usbp->epc[ep]->out_state->mode.linear.rxbuf,
|
|
|
|
cnt,
|
|
|
|
usbp->epc[ep]->out_state->rxsize -
|
|
|
|
usbp->epc[ep]->out_state->rxcnt);
|
|
|
|
usbp->epc[ep]->out_state->mode.linear.rxbuf += cnt;
|
|
|
|
}
|
2012-03-31 06:41:19 +00:00
|
|
|
usbp->epc[ep]->out_state->rxcnt += cnt;
|
|
|
|
break;
|
|
|
|
case GRXSTSP_OUT_GLOBAL_NAK:
|
|
|
|
case GRXSTSP_OUT_COMP:
|
|
|
|
default:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Outgoing packets handler.
|
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
|
|
|
* @param[in] ep endpoint number
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
static void otg_txfifo_handler(USBDriver *usbp, usbep_t ep) {
|
2012-06-12 14:03:33 +00:00
|
|
|
|
2012-08-13 10:14:45 +00:00
|
|
|
/* The TXFIFO is filled until there is space and data to be transmitted.*/
|
|
|
|
while (TRUE) {
|
|
|
|
uint32_t n;
|
|
|
|
|
2012-08-13 13:21:42 +00:00
|
|
|
/* Interrupt disabled on transaction end.*/
|
|
|
|
if (usbp->epc[ep]->in_state->txcnt >= usbp->epc[ep]->in_state->txsize) {
|
|
|
|
OTG->DIEPEMPMSK &= ~DIEPEMPMSK_INEPTXFEM(ep);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-13 10:14:45 +00:00
|
|
|
/* Number of bytes remaining in current transaction.*/
|
|
|
|
n = usbp->epc[ep]->in_state->txsize - usbp->epc[ep]->in_state->txcnt;
|
|
|
|
if (n > usbp->epc[ep]->in_maxsize)
|
|
|
|
n = usbp->epc[ep]->in_maxsize;
|
|
|
|
|
|
|
|
/* Checks if in the TXFIFO there is enough space to accommodate the
|
|
|
|
next packet.*/
|
|
|
|
if (((OTG->ie[ep].DTXFSTS & DTXFSTS_INEPTFSAV_MASK) * 4) < n)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Handles the two cases: linear buffer or queue.*/
|
|
|
|
if (usbp->epc[ep]->in_state->txqueued) {
|
|
|
|
/* Queue associated.*/
|
|
|
|
otg_fifo_write_from_queue(ep,
|
|
|
|
usbp->epc[ep]->in_state->mode.queue.txqueue,
|
|
|
|
n);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Linear buffer associated.*/
|
|
|
|
otg_fifo_write_from_buffer(ep,
|
|
|
|
usbp->epc[ep]->in_state->mode.linear.txbuf,
|
|
|
|
n);
|
|
|
|
usbp->epc[ep]->in_state->mode.linear.txbuf += n;
|
|
|
|
}
|
|
|
|
usbp->epc[ep]->in_state->txcnt += n;
|
|
|
|
}
|
2012-03-31 06:41:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Generic endpoint IN handler.
|
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
|
|
|
* @param[in] ep endpoint number
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
static void otg_epin_handler(USBDriver *usbp, usbep_t ep) {
|
|
|
|
uint32_t epint = OTG->ie[ep].DIEPINT;
|
|
|
|
|
2012-06-12 14:03:33 +00:00
|
|
|
OTG->ie[ep].DIEPINT = 0xFFFFFFFF;
|
|
|
|
|
2012-06-11 17:35:59 +00:00
|
|
|
if (epint & DIEPINT_TOC) {
|
|
|
|
/* Timeouts not handled yet, not sure how to handle.*/
|
|
|
|
}
|
2012-03-31 06:41:19 +00:00
|
|
|
if (epint & DIEPINT_XFRC) {
|
|
|
|
/* Transmit transfer complete.*/
|
|
|
|
_usb_isr_invoke_in_cb(usbp, ep);
|
|
|
|
}
|
2012-06-12 14:03:33 +00:00
|
|
|
if ((epint & DIEPINT_TXFE) && (OTG->DIEPEMPMSK & DIEPEMPMSK_INEPTXFEM(ep))) {
|
2012-06-11 16:54:35 +00:00
|
|
|
/* TX FIFO empty or emptying.*/
|
|
|
|
otg_txfifo_handler(usbp, ep);
|
|
|
|
}
|
2012-03-31 06:41:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Generic endpoint OUT handler.
|
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
|
|
|
* @param[in] ep endpoint number
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
static void otg_epout_handler(USBDriver *usbp, usbep_t ep) {
|
|
|
|
uint32_t epint = OTG->oe[ep].DOEPINT;
|
|
|
|
|
2012-06-09 09:08:05 +00:00
|
|
|
/* Resets all EP IRQ sources.*/
|
|
|
|
OTG->oe[ep].DOEPINT = 0xFFFFFFFF;
|
|
|
|
|
2012-03-31 06:41:19 +00:00
|
|
|
if (epint & DOEPINT_STUP) {
|
|
|
|
/* Setup packets handling, setup packets are handled using a
|
|
|
|
specific callback.*/
|
|
|
|
_usb_isr_invoke_setup_cb(usbp, ep);
|
2012-06-09 09:08:05 +00:00
|
|
|
|
2012-03-31 06:41:19 +00:00
|
|
|
}
|
|
|
|
if (epint & DOEPINT_XFRC) {
|
|
|
|
/* Receive transfer complete.*/
|
|
|
|
_usb_isr_invoke_out_cb(usbp, ep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-26 13:45:36 +00:00
|
|
|
/*===========================================================================*/
|
2012-08-14 15:13:41 +00:00
|
|
|
/* Driver interrupt handlers and threads. */
|
2012-03-26 13:45:36 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
#if STM32_USB_USE_OTG1 || defined(__DOXYGEN__)
|
2012-06-24 07:41:07 +00:00
|
|
|
#if !defined(STM32_OTG1_HANDLER)
|
|
|
|
#error "STM32_OTG1_HANDLER not defined"
|
|
|
|
#endif
|
2012-03-26 13:45:36 +00:00
|
|
|
/**
|
|
|
|
* @brief OTG1 interrupt handler.
|
|
|
|
*
|
|
|
|
* @isr
|
|
|
|
*/
|
2012-06-24 07:41:07 +00:00
|
|
|
CH_IRQ_HANDLER(STM32_OTG1_HANDLER) {
|
2012-03-29 17:01:20 +00:00
|
|
|
USBDriver *usbp = &USBD1;
|
|
|
|
uint32_t sts;
|
2012-03-26 13:45:36 +00:00
|
|
|
|
|
|
|
CH_IRQ_PROLOGUE();
|
|
|
|
|
2012-03-29 17:01:20 +00:00
|
|
|
sts = OTG->GINTSTS & OTG->GINTMSK;
|
2012-08-14 15:13:41 +00:00
|
|
|
OTG->GINTSTS = sts;
|
2012-03-29 17:01:20 +00:00
|
|
|
|
|
|
|
/* Reset interrupt handling.*/
|
|
|
|
if (sts & GINTSTS_USBRST) {
|
|
|
|
_usb_reset(usbp);
|
|
|
|
_usb_isr_invoke_event_cb(usbp, USB_EVENT_RESET);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enumeration done.*/
|
|
|
|
if (sts & GINTSTS_ENUMDNE) {
|
|
|
|
(void)OTG->DSTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SOF interrupt handling.*/
|
|
|
|
if (sts & GINTSTS_SOF) {
|
|
|
|
_usb_isr_invoke_sof_cb(usbp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* RX FIFO not empty handling.*/
|
2012-08-14 15:13:41 +00:00
|
|
|
if (sts & GINTSTS_RXFLVL) {
|
2012-03-31 06:41:19 +00:00
|
|
|
otg_rxfifo_handler(usbp);
|
2012-03-29 17:01:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* IN/OUT endpoints event handling, timeout and transfer complete events
|
|
|
|
are handled.*/
|
|
|
|
if (sts & (GINTSTS_IEPINT | GINTSTS_OEPINT)) {
|
|
|
|
uint32_t src = OTG->DAINT;
|
|
|
|
if (src & (1 << 0))
|
|
|
|
otg_epin_handler(usbp, 0);
|
|
|
|
if (src & (1 << 1))
|
|
|
|
otg_epin_handler(usbp, 1);
|
|
|
|
if (src & (1 << 2))
|
|
|
|
otg_epin_handler(usbp, 2);
|
|
|
|
if (src & (1 << 3))
|
|
|
|
otg_epin_handler(usbp, 3);
|
|
|
|
if (src & (1 << 16))
|
|
|
|
otg_epout_handler(usbp, 0);
|
|
|
|
if (src & (1 << 17))
|
|
|
|
otg_epout_handler(usbp, 1);
|
|
|
|
if (src & (1 << 18))
|
|
|
|
otg_epout_handler(usbp, 2);
|
|
|
|
if (src & (1 << 19))
|
|
|
|
otg_epout_handler(usbp, 3);
|
|
|
|
}
|
|
|
|
|
2012-03-26 13:45:36 +00:00
|
|
|
CH_IRQ_EPILOGUE();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver exported functions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Low level USB driver initialization.
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void usb_lld_init(void) {
|
|
|
|
|
|
|
|
/* Driver initialization.*/
|
|
|
|
usbObjectInit(&USBD1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Configures and activates the USB peripheral.
|
2012-06-16 06:22:39 +00:00
|
|
|
* @note Starting the OTG cell can be a slow operation carried out with
|
2012-03-28 14:19:40 +00:00
|
|
|
* interrupts disabled, perform it before starting time-critical
|
|
|
|
* operations.
|
2012-03-26 13:45:36 +00:00
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void usb_lld_start(USBDriver *usbp) {
|
|
|
|
|
|
|
|
if (usbp->state == USB_STOP) {
|
|
|
|
/* Clock activation.*/
|
|
|
|
#if STM32_USB_USE_OTG1
|
|
|
|
if (&USBD1 == usbp) {
|
2012-03-27 15:37:06 +00:00
|
|
|
/* OTG FS clock enable and reset.*/
|
2012-03-28 14:19:40 +00:00
|
|
|
rccEnableOTG_FS(FALSE);
|
|
|
|
rccResetOTG_FS();
|
2012-03-27 15:37:06 +00:00
|
|
|
|
2012-03-26 13:45:36 +00:00
|
|
|
/* Enables IRQ vector.*/
|
2012-06-24 07:41:07 +00:00
|
|
|
nvicEnableVector(STM32_OTG1_NUMBER,
|
2012-03-26 13:45:36 +00:00
|
|
|
CORTEX_PRIORITY_MASK(STM32_USB_OTG1_IRQ_PRIORITY));
|
|
|
|
}
|
|
|
|
#endif
|
2012-03-27 15:37:06 +00:00
|
|
|
|
2012-03-29 17:01:20 +00:00
|
|
|
/* Soft core reset.*/
|
|
|
|
otg_core_reset();
|
2012-03-27 15:37:06 +00:00
|
|
|
|
2012-04-15 09:53:25 +00:00
|
|
|
/* Internal FS PHY activation.*/
|
|
|
|
OTG->GCCFG = GCCFG_PWRDWN;
|
|
|
|
|
2012-03-27 15:37:06 +00:00
|
|
|
/* - Forced device mode.
|
2012-03-28 14:19:40 +00:00
|
|
|
- USB turn-around time = TRDT_VALUE.
|
2012-03-27 15:37:06 +00:00
|
|
|
- Full Speed 1.1 PHY.*/
|
|
|
|
OTG->GUSBCFG = GUSBCFG_FDMOD | GUSBCFG_TRDT(TRDT_VALUE) | GUSBCFG_PHYSEL;
|
|
|
|
|
2012-08-13 10:14:45 +00:00
|
|
|
/* Interrupts on TXFIFOs half empty.*/
|
|
|
|
OTG->GAHBCFG = 0;
|
2012-03-27 15:37:06 +00:00
|
|
|
|
|
|
|
/* 48MHz 1.1 PHY.*/
|
|
|
|
OTG->DCFG = 0x02200000 | DCFG_PFIVL(0) | DCFG_DSPD_FS11;
|
|
|
|
|
|
|
|
/* PHY enabled.*/
|
|
|
|
OTG->PCGCCTL = 0;
|
|
|
|
|
2012-03-28 14:19:40 +00:00
|
|
|
/* Endpoints re-initialization.*/
|
2012-03-29 17:01:20 +00:00
|
|
|
otg_disable_ep();
|
2012-03-27 15:37:06 +00:00
|
|
|
|
|
|
|
/* Clear all pending Device Interrupts, only the USB Reset interrupt
|
|
|
|
is required initially.*/
|
|
|
|
OTG->DIEPMSK = 0;
|
|
|
|
OTG->DOEPMSK = 0;
|
|
|
|
OTG->DAINTMSK = 0;
|
2012-08-14 14:24:06 +00:00
|
|
|
if (usbp->config->sof_cb == NULL)
|
|
|
|
OTG->GINTMSK = GINTMSK_ENUMDNEM | GINTMSK_USBRSTM /*| GINTMSK_USBSUSPM |
|
|
|
|
GINTMSK_ESUSPM |*/;
|
|
|
|
else
|
|
|
|
OTG->GINTMSK = GINTMSK_ENUMDNEM | GINTMSK_USBRSTM /*| GINTMSK_USBSUSPM |
|
|
|
|
GINTMSK_ESUSPM */ | GINTMSK_SOFM;
|
2012-03-28 14:19:40 +00:00
|
|
|
OTG->GINTSTS = 0xFFFFFFFF; /* Clears all pending IRQs, if any. */
|
2012-03-27 15:37:06 +00:00
|
|
|
|
|
|
|
/* Global interrupts enable.*/
|
|
|
|
OTG->GAHBCFG |= GAHBCFG_GINTMSK;
|
2012-03-26 13:45:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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.*/
|
2012-08-14 06:58:24 +00:00
|
|
|
if (usbp->state != USB_STOP) {
|
|
|
|
OTG->DAINTMSK = 0;
|
|
|
|
OTG->GCCFG = 0;
|
|
|
|
|
2012-03-26 13:45:36 +00:00
|
|
|
#if STM32_USB_USE_USB1
|
|
|
|
if (&USBD1 == usbp) {
|
2012-06-24 07:41:07 +00:00
|
|
|
nvicDisableVector(STM32_OTG1_NUMBER);
|
2012-03-26 13:45:36 +00:00
|
|
|
rccDisableOTG1(FALSE);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief USB low level reset routine.
|
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void usb_lld_reset(USBDriver *usbp) {
|
2012-03-29 17:01:20 +00:00
|
|
|
unsigned i;
|
|
|
|
|
2012-06-09 09:08:05 +00:00
|
|
|
/* Clear the Remote Wake-up Signaling */
|
|
|
|
OTG->DCTL &= ~DCTL_RWUSIG;
|
|
|
|
|
|
|
|
/* Flush the Tx FIFO */
|
|
|
|
otg_txfifo_flush(0);
|
2012-03-26 13:45:36 +00:00
|
|
|
|
2012-03-29 17:01:20 +00:00
|
|
|
/* All endpoints in NAK mode, interrupts cleared.*/
|
|
|
|
for (i = 0; i <= USB_MAX_ENDPOINTS; i++) {
|
|
|
|
OTG->ie[i].DIEPCTL = DIEPCTL_SNAK;
|
|
|
|
OTG->oe[i].DOEPCTL = DOEPCTL_SNAK;
|
2012-06-09 09:08:05 +00:00
|
|
|
OTG->ie[i].DIEPINT = 0xFF;
|
|
|
|
OTG->oe[i].DOEPINT = 0xFF;
|
2012-03-29 17:01:20 +00:00
|
|
|
}
|
2012-03-26 13:45:36 +00:00
|
|
|
|
2012-06-09 09:08:05 +00:00
|
|
|
/* Endpoint interrupts all disabled and cleared.*/
|
|
|
|
OTG->DAINT = 0xFFFFFFFF;
|
|
|
|
OTG->DAINTMSK = DAINTMSK_OEPM(0) | DAINTMSK_IEPM(0);
|
|
|
|
|
2012-04-10 17:15:46 +00:00
|
|
|
/* Resets the FIFO memory allocator.*/
|
|
|
|
otg_ram_reset(usbp);
|
|
|
|
|
|
|
|
/* Receive FIFO size initialization, the address is always zero.*/
|
|
|
|
OTG->GRXFSIZ = STM32_USB_OTG1_RX_FIFO_SIZE / 4;
|
|
|
|
otg_rxfifo_flush();
|
2012-03-29 17:01:20 +00:00
|
|
|
|
2012-06-13 17:07:30 +00:00
|
|
|
/* Resets the device address to zero.*/
|
|
|
|
OTG->DCFG = (OTG->DCFG & ~DCFG_DAD_MASK) | DCFG_DAD(0);
|
|
|
|
|
2012-03-29 17:01:20 +00:00
|
|
|
/* Enables also EP-related interrupt sources.*/
|
2012-06-09 09:08:05 +00:00
|
|
|
OTG->GINTMSK |= GINTMSK_RXFLVLM | GINTMSK_OEPM | GINTMSK_IEPM;
|
2012-03-31 06:41:19 +00:00
|
|
|
OTG->DIEPMSK = DIEPMSK_TOCM | DIEPMSK_XFRCM;
|
2012-03-29 17:01:20 +00:00
|
|
|
OTG->DOEPMSK = DOEPMSK_STUPM | DOEPMSK_XFRCM;
|
2012-03-26 13:45:36 +00:00
|
|
|
|
2012-04-10 17:15:46 +00:00
|
|
|
/* EP0 initialization, it is a special case.*/
|
2012-03-26 13:45:36 +00:00
|
|
|
usbp->epc[0] = &ep0config;
|
2012-04-10 17:15:46 +00:00
|
|
|
OTG->oe[0].DOEPTSIZ = 0;
|
|
|
|
OTG->oe[0].DOEPCTL = DIEPCTL_SD0PID | DIEPCTL_USBAEP | DIEPCTL_EPTYP_CTRL |
|
|
|
|
DOEPCTL_MPSIZ(ep0config.out_maxsize);
|
|
|
|
OTG->ie[0].DIEPTSIZ = 0;
|
|
|
|
OTG->ie[0].DIEPCTL = DIEPCTL_SD0PID | DIEPCTL_USBAEP | DIEPCTL_EPTYP_CTRL |
|
|
|
|
DIEPCTL_TXFNUM(0) | DIEPCTL_MPSIZ(ep0config.in_maxsize);
|
|
|
|
OTG->DIEPTXF0 = DIEPTXF_INEPTXFD(ep0config.in_maxsize / 4) |
|
|
|
|
DIEPTXF_INEPTXSA(otg_ram_alloc(usbp,
|
|
|
|
ep0config.in_maxsize / 4));
|
2012-03-26 13:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Sets the USB address.
|
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void usb_lld_set_address(USBDriver *usbp) {
|
|
|
|
|
2012-03-29 17:01:20 +00:00
|
|
|
OTG->DCFG = (OTG->DCFG & ~DCFG_DAD_MASK) | DCFG_DAD(usbp->address);
|
2012-03-26 13:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Enables an endpoint.
|
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
|
|
|
* @param[in] ep endpoint number
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void usb_lld_init_endpoint(USBDriver *usbp, usbep_t ep) {
|
2012-04-10 17:15:46 +00:00
|
|
|
uint32_t ctl, fsize;
|
|
|
|
|
|
|
|
/* IN and OUT common parameters.*/
|
|
|
|
switch (usbp->epc[ep]->ep_mode & USB_EP_MODE_TYPE) {
|
|
|
|
case USB_EP_MODE_TYPE_CTRL:
|
|
|
|
ctl = DIEPCTL_SD0PID | DIEPCTL_USBAEP | DIEPCTL_EPTYP_CTRL;
|
|
|
|
break;
|
|
|
|
case USB_EP_MODE_TYPE_ISOC:
|
|
|
|
ctl = DIEPCTL_SD0PID | DIEPCTL_USBAEP | DIEPCTL_EPTYP_ISO;
|
|
|
|
break;
|
|
|
|
case USB_EP_MODE_TYPE_BULK:
|
|
|
|
ctl = DIEPCTL_SD0PID | DIEPCTL_USBAEP | DIEPCTL_EPTYP_BULK;
|
|
|
|
break;
|
|
|
|
case USB_EP_MODE_TYPE_INTR:
|
|
|
|
ctl = DIEPCTL_SD0PID | DIEPCTL_USBAEP | DIEPCTL_EPTYP_INTR;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2012-03-26 13:45:36 +00:00
|
|
|
|
2012-04-10 17:15:46 +00:00
|
|
|
/* OUT endpoint activation or deactivation.*/
|
|
|
|
OTG->oe[ep].DOEPTSIZ = 0;
|
2012-08-14 06:58:24 +00:00
|
|
|
if (usbp->epc[ep]->out_cb != NULL) {
|
2012-04-10 17:15:46 +00:00
|
|
|
OTG->oe[ep].DOEPCTL = ctl | DOEPCTL_MPSIZ(usbp->epc[ep]->out_maxsize);
|
2012-08-14 06:58:24 +00:00
|
|
|
OTG->DAINTMSK |= DAINTMSK_OEPM(ep);
|
|
|
|
}
|
|
|
|
else {
|
2012-04-10 17:15:46 +00:00
|
|
|
OTG->oe[ep].DOEPCTL &= ~DOEPCTL_USBAEP;
|
2012-08-14 06:58:24 +00:00
|
|
|
OTG->DAINTMSK &= ~DAINTMSK_OEPM(ep);
|
|
|
|
}
|
2012-04-10 17:15:46 +00:00
|
|
|
|
|
|
|
/* IN endpoint activation or deactivation.*/
|
|
|
|
OTG->ie[ep].DIEPTSIZ = 0;
|
|
|
|
if (usbp->epc[ep]->in_cb != NULL) {
|
|
|
|
/* FIFO allocation for the IN endpoint.*/
|
|
|
|
fsize = usbp->epc[ep]->in_maxsize / 4;
|
2012-08-13 10:14:45 +00:00
|
|
|
if (usbp->epc[ep]->in_multiplier > 1)
|
|
|
|
fsize *= usbp->epc[ep]->in_multiplier;
|
2012-04-10 17:15:46 +00:00
|
|
|
OTG->DIEPTXF[ep - 1] = DIEPTXF_INEPTXFD(fsize) |
|
|
|
|
DIEPTXF_INEPTXSA(otg_ram_alloc(usbp, fsize));
|
|
|
|
otg_txfifo_flush(ep);
|
|
|
|
|
|
|
|
OTG->ie[ep].DIEPCTL = ctl |
|
|
|
|
DIEPCTL_TXFNUM(ep) |
|
|
|
|
DIEPCTL_MPSIZ(usbp->epc[ep]->in_maxsize);
|
2012-08-14 06:58:24 +00:00
|
|
|
OTG->DAINTMSK |= DAINTMSK_IEPM(ep);
|
2012-04-10 17:15:46 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
OTG->DIEPTXF[ep - 1] = 0x02000400; /* Reset value.*/
|
|
|
|
otg_txfifo_flush(ep);
|
|
|
|
OTG->ie[ep].DIEPCTL &= ~DIEPCTL_USBAEP;
|
2012-08-14 06:58:24 +00:00
|
|
|
OTG->DAINTMSK &= ~DAINTMSK_IEPM(ep);
|
2012-04-10 17:15:46 +00:00
|
|
|
}
|
2012-03-26 13:45:36 +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) {
|
|
|
|
|
2012-04-10 17:15:46 +00:00
|
|
|
/* Resets the FIFO memory allocator.*/
|
|
|
|
otg_ram_reset(usbp);
|
|
|
|
|
|
|
|
/* Disabling all endpoints.*/
|
|
|
|
otg_disable_ep();
|
2012-03-26 13:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the status of an OUT endpoint.
|
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
|
|
|
* @param[in] ep endpoint number
|
|
|
|
* @return The endpoint status.
|
|
|
|
* @retval EP_STATUS_DISABLED The endpoint is not active.
|
|
|
|
* @retval EP_STATUS_STALLED The endpoint is stalled.
|
|
|
|
* @retval EP_STATUS_ACTIVE The endpoint is active.
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
usbepstatus_t usb_lld_get_status_out(USBDriver *usbp, usbep_t ep) {
|
2012-06-16 06:22:39 +00:00
|
|
|
uint32_t ctl;
|
2012-03-26 13:45:36 +00:00
|
|
|
|
2012-03-28 19:57:12 +00:00
|
|
|
(void)usbp;
|
|
|
|
|
2012-06-16 06:22:39 +00:00
|
|
|
ctl = OTG->oe[ep].DOEPCTL;
|
|
|
|
if (!(ctl & DOEPCTL_USBAEP))
|
|
|
|
return EP_STATUS_DISABLED;
|
|
|
|
if (ctl & DOEPCTL_STALL)
|
|
|
|
return EP_STATUS_STALLED;
|
|
|
|
return EP_STATUS_ACTIVE;
|
2012-03-26 13:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the status of an IN endpoint.
|
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
|
|
|
* @param[in] ep endpoint number
|
|
|
|
* @return The endpoint status.
|
|
|
|
* @retval EP_STATUS_DISABLED The endpoint is not active.
|
|
|
|
* @retval EP_STATUS_STALLED The endpoint is stalled.
|
|
|
|
* @retval EP_STATUS_ACTIVE The endpoint is active.
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
usbepstatus_t usb_lld_get_status_in(USBDriver *usbp, usbep_t ep) {
|
2012-06-16 06:22:39 +00:00
|
|
|
uint32_t ctl;
|
2012-03-26 13:45:36 +00:00
|
|
|
|
2012-03-28 19:57:12 +00:00
|
|
|
(void)usbp;
|
|
|
|
|
2012-06-16 06:22:39 +00:00
|
|
|
ctl = OTG->ie[ep].DIEPCTL;
|
|
|
|
if (!(ctl & DIEPCTL_USBAEP))
|
|
|
|
return EP_STATUS_DISABLED;
|
|
|
|
if (ctl & DIEPCTL_STALL)
|
|
|
|
return EP_STATUS_STALLED;
|
|
|
|
return EP_STATUS_ACTIVE;
|
2012-03-26 13:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Reads a setup packet from the dedicated packet buffer.
|
|
|
|
* @details This function must be invoked in the context of the @p setup_cb
|
|
|
|
* callback in order to read the received setup packet.
|
|
|
|
* @pre In order to use this function the endpoint must have been
|
|
|
|
* initialized as a control endpoint.
|
|
|
|
* @post The endpoint is ready to accept another packet.
|
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
|
|
|
* @param[in] ep endpoint number
|
|
|
|
* @param[out] buf buffer where to copy the packet data
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void usb_lld_read_setup(USBDriver *usbp, usbep_t ep, uint8_t *buf) {
|
|
|
|
|
2012-04-15 09:53:25 +00:00
|
|
|
memcpy(buf, usbp->epc[ep]->setup_buf, 8);
|
2012-03-26 13:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Prepares for a receive operation.
|
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
|
|
|
* @param[in] ep endpoint number
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
2012-06-17 07:49:08 +00:00
|
|
|
void usb_lld_prepare_receive(USBDriver *usbp, usbep_t ep) {
|
|
|
|
uint32_t pcnt;
|
2012-06-03 08:12:54 +00:00
|
|
|
USBOutEndpointState *osp = usbp->epc[ep]->out_state;
|
|
|
|
|
2012-06-17 07:49:08 +00:00
|
|
|
/* Transfer initialization.*/
|
|
|
|
pcnt = (osp->rxsize + usbp->epc[ep]->out_maxsize - 1) /
|
|
|
|
usbp->epc[ep]->out_maxsize;
|
|
|
|
OTG->oe[ep].DOEPTSIZ = DOEPTSIZ_STUPCNT(3) | DOEPTSIZ_PKTCNT(pcnt) |
|
|
|
|
DOEPTSIZ_XFRSIZ(usbp->epc[ep]->out_maxsize);
|
|
|
|
|
2012-03-26 13:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Prepares for a transmit operation.
|
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
|
|
|
* @param[in] ep endpoint number
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
2012-06-17 07:49:08 +00:00
|
|
|
void usb_lld_prepare_transmit(USBDriver *usbp, usbep_t ep) {
|
2012-04-15 14:53:56 +00:00
|
|
|
USBInEndpointState *isp = usbp->epc[ep]->in_state;
|
2012-03-26 13:45:36 +00:00
|
|
|
|
2012-06-17 07:49:08 +00:00
|
|
|
/* Transfer initialization.*/
|
|
|
|
if (isp->txsize == 0) {
|
|
|
|
/* Special case, sending zero size packet.*/
|
|
|
|
OTG->ie[ep].DIEPTSIZ = DIEPTSIZ_PKTCNT(1) | DIEPTSIZ_XFRSIZ(0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Normal case.*/
|
|
|
|
uint32_t pcnt = (isp->txsize + usbp->epc[ep]->in_maxsize - 1) /
|
|
|
|
usbp->epc[ep]->in_maxsize;
|
|
|
|
OTG->ie[ep].DIEPTSIZ = DIEPTSIZ_PKTCNT(pcnt) |
|
|
|
|
DIEPTSIZ_XFRSIZ(usbp->epc[ep]->in_state->txsize);
|
|
|
|
}
|
2012-06-11 16:54:35 +00:00
|
|
|
|
2012-06-10 16:31:03 +00:00
|
|
|
}
|
|
|
|
|
2012-03-26 13:45:36 +00:00
|
|
|
/**
|
|
|
|
* @brief Starts a receive operation on an OUT endpoint.
|
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
|
|
|
* @param[in] ep endpoint number
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void usb_lld_start_out(USBDriver *usbp, usbep_t ep) {
|
|
|
|
|
|
|
|
(void)usbp;
|
2012-06-03 08:12:54 +00:00
|
|
|
|
|
|
|
OTG->oe[ep].DOEPCTL |= DOEPCTL_CNAK;
|
2012-03-26 13:45:36 +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
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void usb_lld_start_in(USBDriver *usbp, usbep_t ep) {
|
|
|
|
|
|
|
|
(void)usbp;
|
2012-06-03 07:44:25 +00:00
|
|
|
|
2012-06-09 09:08:05 +00:00
|
|
|
OTG->ie[ep].DIEPCTL |= DIEPCTL_EPENA | DIEPCTL_CNAK;
|
2012-06-03 07:44:25 +00:00
|
|
|
OTG->DIEPEMPMSK |= DIEPEMPMSK_INEPTXFEM(ep);
|
2012-03-26 13:45:36 +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) {
|
|
|
|
|
|
|
|
(void)usbp;
|
2012-03-31 06:41:19 +00:00
|
|
|
|
|
|
|
OTG->oe[ep].DOEPCTL |= DOEPCTL_STALL;
|
2012-03-26 13:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Brings an IN endpoint in the stalled state.
|
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
|
|
|
* @param[in] ep endpoint number
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void usb_lld_stall_in(USBDriver *usbp, usbep_t ep) {
|
|
|
|
|
|
|
|
(void)usbp;
|
2012-03-31 06:41:19 +00:00
|
|
|
|
|
|
|
OTG->ie[ep].DIEPCTL |= DIEPCTL_STALL;
|
2012-03-26 13:45:36 +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) {
|
|
|
|
|
|
|
|
(void)usbp;
|
2012-03-31 06:41:19 +00:00
|
|
|
|
|
|
|
OTG->oe[ep].DOEPCTL &= ~DOEPCTL_STALL;
|
2012-03-26 13:45:36 +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;
|
2012-03-31 06:41:19 +00:00
|
|
|
|
|
|
|
OTG->ie[ep].DIEPCTL &= ~DIEPCTL_STALL;
|
2012-03-26 13:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAL_USE_USB */
|
|
|
|
|
|
|
|
/** @} */
|