aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal/platforms/STM32/OTGv1
diff options
context:
space:
mode:
authorgdisirio <gdisirio@35acf78f-673a-0410-8e92-d51de3d6d3f4>2012-06-11 16:54:35 +0000
committergdisirio <gdisirio@35acf78f-673a-0410-8e92-d51de3d6d3f4>2012-06-11 16:54:35 +0000
commitd094e348c5d1a3785289c69c5185bbaca1257de8 (patch)
tree982d1606d57103c8ee2d38e9a11d161a65716640 /os/hal/platforms/STM32/OTGv1
parentc762926b68f6a6c7f1e71b8acf9b1dd29d6e481f (diff)
downloadChibiOS-d094e348c5d1a3785289c69c5185bbaca1257de8.tar.gz
ChibiOS-d094e348c5d1a3785289c69c5185bbaca1257de8.tar.bz2
ChibiOS-d094e348c5d1a3785289c69c5185bbaca1257de8.zip
git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@4266 35acf78f-673a-0410-8e92-d51de3d6d3f4
Diffstat (limited to 'os/hal/platforms/STM32/OTGv1')
-rw-r--r--os/hal/platforms/STM32/OTGv1/usb_lld.c163
-rw-r--r--os/hal/platforms/STM32/OTGv1/usb_lld.h44
2 files changed, 177 insertions, 30 deletions
diff --git a/os/hal/platforms/STM32/OTGv1/usb_lld.c b/os/hal/platforms/STM32/OTGv1/usb_lld.c
index 44d292b8b..fae5f500f 100644
--- a/os/hal/platforms/STM32/OTGv1/usb_lld.c
+++ b/os/hal/platforms/STM32/OTGv1/usb_lld.c
@@ -193,11 +193,12 @@ static uint32_t otg_ram_alloc(USBDriver *usbp, size_t size) {
* @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 number of bytes that were effectively written
*
* @notapi
*/
-static void otg_fifo_write(usbep_t ep, const uint8_t *buf, size_t n) {
+static void otg_fifo_write_from_buffer(usbep_t ep,
+ const uint8_t *buf,
+ size_t n) {
volatile uint32_t *fifop;
fifop = OTG_FIFO(ep);
@@ -214,6 +215,55 @@ static void otg_fifo_write(usbep_t ep, const uint8_t *buf, size_t n) {
}
/**
+ * @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) {
+ size_t nw;
+ uint8_t *bp;
+ volatile uint32_t *fifop;
+
+ fifop = OTG_FIFO(ep);
+
+ /* Fetching data from the queue buffer directly.*/
+ bp = oqp->q_rdptr;
+ nw = (n + 3) / 4;
+ do {
+ uint32_t dw;
+ dw = (uint32_t)*bp++;
+ if (bp >= oqp->q_top)
+ bp = oqp->q_buffer;
+ dw |= (uint32_t)*bp++ << 8;
+ if (bp >= oqp->q_top)
+ bp = oqp->q_buffer;
+ dw |= (uint32_t)*bp++ << 16;
+ if (bp >= oqp->q_top)
+ bp = oqp->q_buffer;
+ dw |= (uint32_t)*bp++ << 24;
+ if (bp >= oqp->q_top)
+ bp = oqp->q_buffer;
+ *fifop = dw;
+ } while (--nw > 0);
+
+ /* Updating queue.*/
+ chSysLockFromIsr();
+ oqp->q_rdptr += n;
+ if (oqp->q_rdptr >= oqp->q_top)
+ oqp->q_rdptr = oqp->q_buffer;
+ oqp->q_counter += n;
+ while (notempty(&oqp->q_waiting))
+ chSchReadyI(fifo_remove(&oqp->q_waiting))->p_u.rdymsg = Q_OK;
+ chSysUnlockFromIsr();
+}
+
+/**
* @brief Reads a packet from the RXFIFO.
*
* @param[out] buf buffer where to copy the endpoint data
@@ -222,7 +272,7 @@ static void otg_fifo_write(usbep_t ep, const uint8_t *buf, size_t n) {
*
* @notapi
*/
-static void otg_fifo_read(uint8_t *buf, size_t n, size_t max) {
+static void otg_fifo_read_to_buffer(uint8_t *buf, size_t n, size_t max) {
volatile uint32_t *fifop;
fifop = OTG_FIFO(0);
@@ -242,6 +292,25 @@ static void otg_fifo_read(uint8_t *buf, size_t n, size_t max) {
}
/**
+ * @brief Reads a packet from the RXFIFO.
+ *
+ * @param[in] oqp pointer to an @p InputQueue object
+ * @param[in] n number of bytes to pull from the FIFO
+ *
+ * @notapi
+ */
+static void otg_fifo_read_to_queue(InputQueue *iqp, size_t n) {
+ size_t nw;
+ volatile uint32_t *fifop;
+
+ fifop = OTG_FIFO(0);
+ nw = (n + 3) / 4;
+ do {
+ uint32_t dw = *fifop;
+ } while (--n > 0);
+}
+
+/**
* @brief Incoming packets handler.
*
* @param[in] usbp pointer to the @p USBDriver object
@@ -258,15 +327,21 @@ static void otg_rxfifo_handler(USBDriver *usbp) {
case GRXSTSP_SETUP_DATA:
cnt = (sts & GRXSTSP_BCNT_MASK) >> GRXSTSP_BCNT_OFF;
ep = (sts & GRXSTSP_EPNUM_MASK) >> GRXSTSP_EPNUM_OFF;
- otg_fifo_read(usbp->epc[ep]->setup_buf, cnt, 8);
+ otg_fifo_read_to_buffer(usbp->epc[ep]->setup_buf, cnt, 8);
break;
case GRXSTSP_OUT_DATA:
cnt = (sts & GRXSTSP_BCNT_MASK) >> GRXSTSP_BCNT_OFF;
ep = (sts & GRXSTSP_EPNUM_MASK) >> GRXSTSP_EPNUM_OFF;
- otg_fifo_read(usbp->epc[ep]->out_state->rxbuf, cnt,
- usbp->epc[ep]->out_state->rxsize -
- usbp->epc[ep]->out_state->rxcnt);
- usbp->epc[ep]->out_state->rxbuf += cnt;
+ if (usbp->epc[ep]->out_state->rxqueued) {
+ /* Queue associated.*/
+ }
+ 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;
+ }
usbp->epc[ep]->out_state->rxcnt += cnt;
break;
case GRXSTSP_OUT_GLOBAL_NAK:
@@ -287,12 +362,28 @@ static void otg_rxfifo_handler(USBDriver *usbp) {
static void otg_txfifo_handler(USBDriver *usbp, usbep_t ep) {
uint32_t n;
+ /* 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;
- otg_fifo_write(ep, usbp->epc[ep]->in_state->txbuf, n);
- usbp->epc[ep]->in_state->txbuf += n;
+
+ 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;
+ /* TODO: Potential issue, txcnt can be less than txsize after the planned
+ number of packets have been received, better disable the interrupt
+ at the end of the transaction in otg_epin_handler().*/
if (usbp->epc[ep]->in_state->txcnt >= usbp->epc[ep]->in_state->txsize) {
/* Transfer finished.*/
OTG->DIEPEMPMSK &= ~DIEPEMPMSK_INEPTXFEM(ep);
@@ -313,14 +404,14 @@ static void otg_epin_handler(USBDriver *usbp, usbep_t ep) {
/* Resets all EP IRQ sources.*/
OTG->ie[ep].DIEPINT = 0xFFFFFFFF;
- if (epint & DIEPINT_TXFE) {
- /* TX FIFO empty or emptying.*/
- otg_txfifo_handler(usbp, ep);
- }
if (epint & DIEPINT_XFRC) {
/* Transmit transfer complete.*/
_usb_isr_invoke_in_cb(usbp, ep);
}
+ if (epint & DIEPINT_TXFE) {
+ /* TX FIFO empty or emptying.*/
+ otg_txfifo_handler(usbp, ep);
+ }
if (epint & DIEPINT_TOC) {
/* Timeouts not handled yet, not sure how to handle.*/
}
@@ -730,9 +821,12 @@ void usb_lld_prepare_receive(USBDriver *usbp, usbep_t ep,
uint32_t pcnt;
USBOutEndpointState *osp = usbp->epc[ep]->out_state;
- osp->rxbuf = buf;
- osp->rxsize = n;
- osp->rxcnt = 0;
+ osp->rxqueued = FALSE;
+ osp->rxsize = n;
+ osp->rxcnt = 0;
+ osp->mode.linear.rxbuf = buf;
+
+ /* Transfer initialization.*/
pcnt = (n + 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);
@@ -753,9 +847,10 @@ void usb_lld_prepare_transmit(USBDriver *usbp, usbep_t ep,
uint32_t pcnt;
USBInEndpointState *isp = usbp->epc[ep]->in_state;
- isp->txbuf = buf;
- isp->txsize = n;
- isp->txcnt = 0;
+ isp->txqueued = FALSE;
+ isp->txsize = n;
+ isp->txcnt = 0;
+ isp->mode.linear.txbuf = buf;
if (n == 0) {
/* Special case, sending zero size packet.*/
@@ -782,12 +877,24 @@ void usb_lld_prepare_transmit(USBDriver *usbp, usbep_t ep,
* @param[in] usbp pointer to the @p USBDriver object
* @param[in] ep endpoint number
* @param[in] iq input queue to be filled with incoming data
+ * @param[in] n maximum number of bytes to copy
*
* @special
*/
void usb_lld_prepare_queued_receive(USBDriver *usbp, usbep_t ep,
- InputQueue *iq) {
+ InputQueue *iq, size_t n) {
+ uint32_t pcnt;
+ USBOutEndpointState *osp = usbp->epc[ep]->out_state;
+
+ osp->rxqueued = TRUE;
+ osp->rxsize = n;
+ osp->rxcnt = 0;
+ osp->mode.queue.rxqueue = iq;
+ /* Transfer initialization.*/
+ pcnt = (n + 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);
}
/**
@@ -801,12 +908,24 @@ void usb_lld_prepare_queued_receive(USBDriver *usbp, usbep_t ep,
* @param[in] usbp pointer to the @p USBDriver object
* @param[in] ep endpoint number
* @param[in] oq output queue to be fetched for outgoing data
+ * @param[in] n maximum number of bytes to copy
*
* @special
*/
void usb_lld_prepare_queued_transmit(USBDriver *usbp, usbep_t ep,
- OutputQueue *oq) {
+ OutputQueue *oq, size_t n) {
+ uint32_t pcnt;
+ USBInEndpointState *isp = usbp->epc[ep]->in_state;
+
+ isp->txqueued = TRUE;
+ isp->txsize = n;
+ isp->txcnt = 0;
+ isp->mode.queue.txqueue = oq;
+ /* Transfer initialization.*/
+ pcnt = (n + 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);
}
/**
diff --git a/os/hal/platforms/STM32/OTGv1/usb_lld.h b/os/hal/platforms/STM32/OTGv1/usb_lld.h
index 687d788ff..ea0def924 100644
--- a/os/hal/platforms/STM32/OTGv1/usb_lld.h
+++ b/os/hal/platforms/STM32/OTGv1/usb_lld.h
@@ -108,13 +108,13 @@
/*===========================================================================*/
/**
- * @brief Type of an endpoint state structure.
+ * @brief Type of an IN endpoint state structure.
*/
typedef struct {
/**
- * @brief Pointer to the transmission buffer.
+ * @brief Buffer mode, queue or linear.
*/
- const uint8_t *txbuf;
+ bool_t txqueued;
/**
* @brief Requested transmit transfer size.
*/
@@ -123,16 +123,30 @@ typedef struct {
* @brief Transmitted bytes so far.
*/
size_t txcnt;
+ union {
+ struct {
+ /**
+ * @brief Pointer to the transmission buffer.
+ */
+ const uint8_t *txbuf;
+ } linear;
+ struct {
+ /**
+ * @brief Pointer to the output queue.
+ */
+ OutputQueue *txqueue;
+ } queue;
+ } mode;
} USBInEndpointState;
/**
- * @brief Type of an endpoint state structure.
+ * @brief Type of an OUT endpoint state structure.
*/
typedef struct {
/**
- * @brief Pointer to the receive buffer.
+ * @brief Buffer mode, queue or linear.
*/
- uint8_t *rxbuf;
+ bool_t rxqueued;
/**
* @brief Requested receive transfer size.
*/
@@ -141,6 +155,20 @@ typedef struct {
* @brief Received bytes so far.
*/
size_t rxcnt;
+ union {
+ struct {
+ /**
+ * @brief Pointer to the receive buffer.
+ */
+ uint8_t *rxbuf;
+ } linear;
+ struct {
+ /**
+ * @brief Pointer to the input queue.
+ */
+ InputQueue *rxqueue;
+ } queue;
+ } mode;
} USBOutEndpointState;
/**
@@ -378,9 +406,9 @@ extern "C" {
void usb_lld_prepare_transmit(USBDriver *usbp, usbep_t ep,
const uint8_t *buf, size_t n);
void usb_lld_prepare_queued_receive(USBDriver *usbp, usbep_t ep,
- InputQueue *iq);
+ InputQueue *iq, size_t n);
void usb_lld_prepare_queued_transmit(USBDriver *usbp, usbep_t ep,
- OutputQueue *oq);
+ OutputQueue *oq, size_t n);
void usb_lld_start_out(USBDriver *usbp, usbep_t ep);
void usb_lld_start_in(USBDriver *usbp, usbep_t ep);
void usb_lld_stall_out(USBDriver *usbp, usbep_t ep);