USB rework, step 2.
git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@2714 35acf78f-673a-0410-8e92-d51de3d6d3f4
This commit is contained in:
parent
200f020df9
commit
4e68b68d5a
|
@ -73,6 +73,9 @@
|
||||||
#define USB_FEATURE_DEVICE_REMOTE_WAKEUP 1
|
#define USB_FEATURE_DEVICE_REMOTE_WAKEUP 1
|
||||||
#define USB_FEATURE_TEST_MODE 2
|
#define USB_FEATURE_TEST_MODE 2
|
||||||
|
|
||||||
|
#define USB_EARLY_SET_ADDRESS 0
|
||||||
|
#define USB_LATE_SET_ADDRESS 1
|
||||||
|
|
||||||
/*===========================================================================*/
|
/*===========================================================================*/
|
||||||
/* Driver pre-compile time settings. */
|
/* Driver pre-compile time settings. */
|
||||||
/*===========================================================================*/
|
/*===========================================================================*/
|
||||||
|
@ -296,10 +299,9 @@ typedef const USBDescriptor * (*usbgetdescriptor_t)(USBDriver *usbp,
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
#define usbSetupTransfer(usbp, buf, n, endcb) { \
|
#define usbSetupTransfer(usbp, buf, n) { \
|
||||||
(usbp)->usb_ep0next = (buf); \
|
(usbp)->ep0next = (buf); \
|
||||||
(usbp)->usb_ep0n = (n); \
|
(usbp)->ep0n = (n); \
|
||||||
(usbp)->usb_ep0endcb = (endcb); \
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*===========================================================================*/
|
/*===========================================================================*/
|
||||||
|
@ -315,6 +317,13 @@ extern "C" {
|
||||||
void usbStop(USBDriver *usbp);
|
void usbStop(USBDriver *usbp);
|
||||||
void usbInitEndpointI(USBDriver *usbp, usbep_t ep, USBEndpointState *epp,
|
void usbInitEndpointI(USBDriver *usbp, usbep_t ep, USBEndpointState *epp,
|
||||||
const USBEndpointConfig *epcp);
|
const USBEndpointConfig *epcp);
|
||||||
|
void usbDisableEndpointsI(USBDriver *usbp);
|
||||||
|
bool_t usbStartReceiveI(USBDriver *usbp, usbep_t ep,
|
||||||
|
uint8_t *buf, size_t n);
|
||||||
|
bool_t usbStartTransmitI(USBDriver *usbp, usbep_t ep,
|
||||||
|
const uint8_t *buf, size_t n);
|
||||||
|
bool_t usbStallReceiveI(USBDriver *usbp, usbep_t ep);
|
||||||
|
bool_t usbStallTransmitI(USBDriver *usbp, usbep_t ep);
|
||||||
void _usb_reset(USBDriver *usbp);
|
void _usb_reset(USBDriver *usbp);
|
||||||
void _usb_ep0in(USBDriver *usbp, usbep_t ep);
|
void _usb_ep0in(USBDriver *usbp, usbep_t ep);
|
||||||
void _usb_ep0out(USBDriver *usbp, usbep_t ep);
|
void _usb_ep0out(USBDriver *usbp, usbep_t ep);
|
||||||
|
|
|
@ -62,7 +62,7 @@ static const USBEndpointConfig ep0config = {
|
||||||
_usb_ep0out,
|
_usb_ep0out,
|
||||||
0x40,
|
0x40,
|
||||||
0x40,
|
0x40,
|
||||||
EPR_EP_TYPE_CONTROL | EPR_STAT_TX_STALL | EPR_STAT_RX_VALID,
|
EPR_EP_TYPE_CONTROL | EPR_STAT_TX_NAK | EPR_STAT_RX_VALID,
|
||||||
0x40,
|
0x40,
|
||||||
0x80
|
0x80
|
||||||
};
|
};
|
||||||
|
@ -71,6 +71,58 @@ static const USBEndpointConfig ep0config = {
|
||||||
/* Driver local functions. */
|
/* Driver local functions. */
|
||||||
/*===========================================================================*/
|
/*===========================================================================*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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;
|
||||||
|
}
|
||||||
|
|
||||||
/*===========================================================================*/
|
/*===========================================================================*/
|
||||||
/* Driver interrupt handlers. */
|
/* Driver interrupt handlers. */
|
||||||
/*===========================================================================*/
|
/*===========================================================================*/
|
||||||
|
@ -95,6 +147,7 @@ CH_IRQ_HANDLER(USB_HP_IRQHandler) {
|
||||||
*/
|
*/
|
||||||
CH_IRQ_HANDLER(USB_LP_IRQHandler) {
|
CH_IRQ_HANDLER(USB_LP_IRQHandler) {
|
||||||
uint32_t istr;
|
uint32_t istr;
|
||||||
|
size_t n;
|
||||||
USBDriver *usbp = &USBD1;
|
USBDriver *usbp = &USBD1;
|
||||||
|
|
||||||
CH_IRQ_PROLOGUE();
|
CH_IRQ_PROLOGUE();
|
||||||
|
@ -104,15 +157,15 @@ CH_IRQ_HANDLER(USB_LP_IRQHandler) {
|
||||||
/* USB bus reset condition handling.*/
|
/* USB bus reset condition handling.*/
|
||||||
if (istr & ISTR_RESET) {
|
if (istr & ISTR_RESET) {
|
||||||
_usb_reset(usbp);
|
_usb_reset(usbp);
|
||||||
if (usbp->usb_config->uc_event_cb)
|
if (usbp->config->event_cb)
|
||||||
usbp->usb_config->uc_event_cb(usbp, USB_EVENT_RESET);
|
usbp->config->event_cb(usbp, USB_EVENT_RESET);
|
||||||
STM32_USB->ISTR = ~ISTR_RESET;
|
STM32_USB->ISTR = ~ISTR_RESET;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* SOF handling.*/
|
/* SOF handling.*/
|
||||||
if (istr & ISTR_SOF) {
|
if (istr & ISTR_SOF) {
|
||||||
if (usbp->usb_config->uc_sof_cb)
|
if (usbp->config->sof_cb)
|
||||||
usbp->usb_config->uc_sof_cb(usbp);
|
usbp->config->sof_cb(usbp);
|
||||||
STM32_USB->ISTR = ~ISTR_SOF;
|
STM32_USB->ISTR = ~ISTR_SOF;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,19 +173,55 @@ CH_IRQ_HANDLER(USB_LP_IRQHandler) {
|
||||||
while (istr & ISTR_CTR) {
|
while (istr & ISTR_CTR) {
|
||||||
uint32_t ep;
|
uint32_t ep;
|
||||||
uint32_t epr = STM32_USB->EPR[ep = istr & ISTR_EP_ID_MASK];
|
uint32_t epr = STM32_USB->EPR[ep = istr & ISTR_EP_ID_MASK];
|
||||||
const USBEndpointConfig *epcp = usbp->usb_ep[ep]->uep_config;
|
const USBEndpointConfig *epcp = usbp->ep[ep]->config;
|
||||||
|
|
||||||
if (epr & EPR_CTR_TX) {
|
if (epr & EPR_CTR_TX) {
|
||||||
/* IN endpoint, transmission.*/
|
/* IN endpoint, transmission.*/
|
||||||
EPR_CLEAR_CTR_TX(ep);
|
EPR_CLEAR_CTR_TX(ep);
|
||||||
if (epcp->uepc_in_cb)
|
n = USB_GET_DESCRIPTOR(ep)->TXCOUNT;
|
||||||
epcp->uepc_in_cb(usbp, ep);
|
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.*/
|
||||||
|
if (epcp->in_cb)
|
||||||
|
epcp->in_cb(usbp, ep);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (epr & EPR_CTR_RX) {
|
if (epr & EPR_CTR_RX) {
|
||||||
/* OUT endpoint, receive.*/
|
|
||||||
EPR_CLEAR_CTR_RX(ep);
|
EPR_CLEAR_CTR_RX(ep);
|
||||||
if (epcp->uepc_out_cb)
|
/* OUT endpoint, receive.*/
|
||||||
epcp->uepc_out_cb(usbp, ep);
|
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.*/
|
||||||
|
if (epcp->out_cb)
|
||||||
|
epcp->out_cb(usbp, ep);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
istr = STM32_USB->ISTR;
|
istr = STM32_USB->ISTR;
|
||||||
}
|
}
|
||||||
|
@ -169,7 +258,7 @@ void usb_lld_init(void) {
|
||||||
*/
|
*/
|
||||||
void usb_lld_start(USBDriver *usbp) {
|
void usb_lld_start(USBDriver *usbp) {
|
||||||
|
|
||||||
if (usbp->usb_state == USB_STOP) {
|
if (usbp->state == USB_STOP) {
|
||||||
/* Clock activation.*/
|
/* Clock activation.*/
|
||||||
#if STM32_USB_USE_USB1
|
#if STM32_USB_USE_USB1
|
||||||
if (&USBD1 == usbp) {
|
if (&USBD1 == usbp) {
|
||||||
|
@ -202,7 +291,7 @@ void usb_lld_start(USBDriver *usbp) {
|
||||||
void usb_lld_stop(USBDriver *usbp) {
|
void usb_lld_stop(USBDriver *usbp) {
|
||||||
|
|
||||||
/* If in ready state then disables the USB clock.*/
|
/* If in ready state then disables the USB clock.*/
|
||||||
if (usbp->usb_state == USB_STOP) {
|
if (usbp->state == USB_STOP) {
|
||||||
#if STM32_ADC_USE_ADC1
|
#if STM32_ADC_USE_ADC1
|
||||||
if (&USBD1 == usbp) {
|
if (&USBD1 == usbp) {
|
||||||
NVICDisableVector(USB_HP_CAN1_TX_IRQn);
|
NVICDisableVector(USB_HP_CAN1_TX_IRQn);
|
||||||
|
@ -234,14 +323,14 @@ void usb_lld_reset(USBDriver *usbp) {
|
||||||
/*CNTR_WKUPM | CNTR_ERRM | CNTR_PMAOVRM |*/ CNTR_CTRM;
|
/*CNTR_WKUPM | CNTR_ERRM | CNTR_PMAOVRM |*/ CNTR_CTRM;
|
||||||
/* The SOF interrupt is only enabled if a callback is defined for
|
/* The SOF interrupt is only enabled if a callback is defined for
|
||||||
this service because it is an high rate source.*/
|
this service because it is an high rate source.*/
|
||||||
if (usbp->usb_config->uc_sof_cb != NULL)
|
if (usbp->config->sof_cb != NULL)
|
||||||
cntr |= CNTR_SOFM;
|
cntr |= CNTR_SOFM;
|
||||||
STM32_USB->CNTR = cntr;
|
STM32_USB->CNTR = cntr;
|
||||||
|
|
||||||
/* EP0 initialization.*/
|
/* EP0 initialization.*/
|
||||||
memset(&ep0state, 0, sizeof ep0state);
|
memset(&ep0state, 0, sizeof ep0state);
|
||||||
ep0state.uep_config = &ep0config;
|
ep0state.config = &ep0config;
|
||||||
usbp->usb_ep[0] = &ep0state;
|
usbp->ep[0] = &ep0state;
|
||||||
usb_lld_init_endpoint(usbp, 0);
|
usb_lld_init_endpoint(usbp, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -249,14 +338,12 @@ void usb_lld_reset(USBDriver *usbp) {
|
||||||
* @brief Sets the USB address.
|
* @brief Sets the USB address.
|
||||||
*
|
*
|
||||||
* @param[in] usbp pointer to the @p USBDriver object
|
* @param[in] usbp pointer to the @p USBDriver object
|
||||||
* @param[in] addr the USB address
|
|
||||||
*
|
*
|
||||||
* @notapi
|
* @notapi
|
||||||
*/
|
*/
|
||||||
void usb_lld_set_address(USBDriver *usbp, uint8_t addr) {
|
void usb_lld_set_address(USBDriver *usbp) {
|
||||||
|
|
||||||
(void)usbp;
|
STM32_USB->DADDR = (uint32_t)(usbp->address) | DADDR_EF;
|
||||||
STM32_USB->DADDR = (uint32_t)addr | DADDR_EF;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -270,23 +357,23 @@ void usb_lld_set_address(USBDriver *usbp, uint8_t addr) {
|
||||||
void usb_lld_init_endpoint(USBDriver *usbp, usbep_t ep) {
|
void usb_lld_init_endpoint(USBDriver *usbp, usbep_t ep) {
|
||||||
uint16_t nblocks;
|
uint16_t nblocks;
|
||||||
stm32_usb_descriptor_t *dp;
|
stm32_usb_descriptor_t *dp;
|
||||||
const USBEndpointConfig *epcp = usbp->usb_ep[ep]->uep_config;
|
const USBEndpointConfig *epcp = usbp->ep[ep]->config;
|
||||||
|
|
||||||
/* EPxR register setup.*/
|
/* EPxR register setup.*/
|
||||||
EPR_SET(ep, epcp->uepc_epr | ep);
|
EPR_SET(ep, epcp->epr | ep);
|
||||||
EPR_TOGGLE(ep, epcp->uepc_epr);
|
EPR_TOGGLE(ep, epcp->epr);
|
||||||
|
|
||||||
/* Endpoint size and address initialization.*/
|
/* Endpoint size and address initialization.*/
|
||||||
if (epcp->uepc_out_maxsize > 62)
|
if (epcp->out_maxsize > 62)
|
||||||
nblocks = (((((epcp->uepc_out_maxsize - 1) | 0x1f) + 1) / 32) << 10) |
|
nblocks = (((((epcp->out_maxsize - 1) | 0x1f) + 1) / 32) << 10) |
|
||||||
0x8000;
|
0x8000;
|
||||||
else
|
else
|
||||||
nblocks = ((((epcp->uepc_out_maxsize - 1) | 1) + 1) / 2) << 10;
|
nblocks = ((((epcp->out_maxsize - 1) | 1) + 1) / 2) << 10;
|
||||||
dp = USB_GET_DESCRIPTOR(ep);
|
dp = USB_GET_DESCRIPTOR(ep);
|
||||||
dp->TXCOUNT = 0;
|
dp->TXCOUNT = 0;
|
||||||
dp->RXCOUNT = nblocks;
|
dp->RXCOUNT = nblocks;
|
||||||
dp->TXADDR = epcp->uepc_inaddr;
|
dp->TXADDR = epcp->inaddr;
|
||||||
dp->RXADDR = epcp->uepc_outaddr;
|
dp->RXADDR = epcp->outaddr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -379,7 +466,7 @@ size_t usb_lld_get_writeable(USBDriver *usbp, usbep_t ep) {
|
||||||
|
|
||||||
if ((STM32_USB->EPR[ep] & EPR_STAT_TX_MASK) != EPR_STAT_TX_NAK)
|
if ((STM32_USB->EPR[ep] & EPR_STAT_TX_MASK) != EPR_STAT_TX_NAK)
|
||||||
return 0;
|
return 0;
|
||||||
return (size_t)usbp->usb_ep[ep]->uep_config->uepc_in_maxsize;
|
return (size_t)usbp->ep[ep]->config->in_maxsize;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -421,27 +508,6 @@ size_t usb_lld_write(USBDriver *usbp, usbep_t ep,
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Returns the status of an IN endpoint.
|
|
||||||
*
|
|
||||||
* @param[in] usbp pointer to the @p USBDriver object
|
|
||||||
* @param[in] ep endpoint number
|
|
||||||
*
|
|
||||||
* @notapi
|
|
||||||
*/
|
|
||||||
usbepstatus_t usb_lld_get_status_in(USBDriver *usbp, usbep_t ep) {
|
|
||||||
|
|
||||||
(void)usbp;
|
|
||||||
switch (STM32_USB->EPR[ep] & EPR_STAT_TX_MASK) {
|
|
||||||
case EPR_STAT_TX_DIS:
|
|
||||||
return EP_STATUS_DISABLED;
|
|
||||||
case EPR_STAT_TX_STALL:
|
|
||||||
return EP_STATUS_STALLED;
|
|
||||||
default:
|
|
||||||
return EP_STATUS_ACTIVE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Returns the status of an OUT endpoint.
|
* @brief Returns the status of an OUT endpoint.
|
||||||
*
|
*
|
||||||
|
@ -464,17 +530,71 @@ usbepstatus_t usb_lld_get_status_out(USBDriver *usbp, usbep_t ep) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Brings an IN endpoint in the stalled state.
|
* @brief Returns the status of an IN endpoint.
|
||||||
*
|
*
|
||||||
* @param[in] usbp pointer to the @p USBDriver object
|
* @param[in] usbp pointer to the @p USBDriver object
|
||||||
* @param[in] ep endpoint number
|
* @param[in] ep endpoint number
|
||||||
*
|
*
|
||||||
* @notapi
|
* @notapi
|
||||||
*/
|
*/
|
||||||
void usb_lld_stall_in(USBDriver *usbp, usbep_t ep) {
|
usbepstatus_t usb_lld_get_status_in(USBDriver *usbp, usbep_t ep) {
|
||||||
|
|
||||||
(void)usbp;
|
(void)usbp;
|
||||||
EPR_SET_STAT_TX(ep, EPR_STAT_TX_STALL);
|
switch (STM32_USB->EPR[ep] & EPR_STAT_TX_MASK) {
|
||||||
|
case EPR_STAT_TX_DIS:
|
||||||
|
return EP_STATUS_DISABLED;
|
||||||
|
case EPR_STAT_TX_STALL:
|
||||||
|
return EP_STATUS_STALLED;
|
||||||
|
default:
|
||||||
|
return EP_STATUS_ACTIVE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Starts a receive operation on an OUT endpoint.
|
||||||
|
*
|
||||||
|
* @param[in] usbp pointer to the @p USBDriver object
|
||||||
|
* @param[in] ep endpoint number
|
||||||
|
* @param[out] buf buffer where to copy the endpoint data
|
||||||
|
* @param[in] n maximum number of bytes to copy in the buffer
|
||||||
|
*
|
||||||
|
* @notapi
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -492,21 +612,17 @@ void usb_lld_stall_out(USBDriver *usbp, usbep_t ep) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Brings an IN endpoint in the active state.
|
* @brief Brings an IN endpoint in the stalled state.
|
||||||
*
|
*
|
||||||
* @param[in] usbp pointer to the @p USBDriver object
|
* @param[in] usbp pointer to the @p USBDriver object
|
||||||
* @param[in] ep endpoint number
|
* @param[in] ep endpoint number
|
||||||
*
|
*
|
||||||
* @notapi
|
* @notapi
|
||||||
*/
|
*/
|
||||||
void usb_lld_clear_in(USBDriver *usbp, usbep_t ep) {
|
void usb_lld_stall_in(USBDriver *usbp, usbep_t ep) {
|
||||||
|
|
||||||
(void)usbp;
|
(void)usbp;
|
||||||
|
EPR_SET_STAT_TX(ep, EPR_STAT_TX_STALL);
|
||||||
/* 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -527,6 +643,24 @@ void usb_lld_clear_out(USBDriver *usbp, usbep_t ep) {
|
||||||
EPR_SET_STAT_TX(ep, EPR_STAT_RX_NAK);
|
EPR_SET_STAT_TX(ep, EPR_STAT_RX_NAK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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);
|
||||||
|
}
|
||||||
|
|
||||||
#endif /* HAL_USE_USB */
|
#endif /* HAL_USE_USB */
|
||||||
|
|
||||||
/** @} */
|
/** @} */
|
||||||
|
|
|
@ -41,6 +41,11 @@
|
||||||
*/
|
*/
|
||||||
#define USB_MAX_ENDPOINTS USB_ENDOPOINTS_NUMBER
|
#define USB_MAX_ENDPOINTS USB_ENDOPOINTS_NUMBER
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief This device requires the address change after the status packet.
|
||||||
|
*/
|
||||||
|
#define USB_SET_ADDRESS_MODE USB_LATE_SET_ADDRESS
|
||||||
|
|
||||||
/*===========================================================================*/
|
/*===========================================================================*/
|
||||||
/* Driver pre-compile time settings. */
|
/* Driver pre-compile time settings. */
|
||||||
/*===========================================================================*/
|
/*===========================================================================*/
|
||||||
|
@ -89,40 +94,40 @@
|
||||||
/*===========================================================================*/
|
/*===========================================================================*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Type of an USB Endpoint configuration structure.
|
* @brief Type of an USB endpoint configuration structure.
|
||||||
* @note Platform specific restrictions may apply to endpoints.
|
* @note Platform specific restrictions may apply to endpoints.
|
||||||
*/
|
*/
|
||||||
typedef struct {
|
typedef struct {
|
||||||
/**
|
/**
|
||||||
* @brief IN endpoint notification callback.
|
* @brief IN endpoint notification callback.
|
||||||
*/
|
*/
|
||||||
usbepcallback_t uepc_in_cb;
|
usbepcallback_t in_cb;
|
||||||
/**
|
/**
|
||||||
* @brief OUT endpoint notification callback.
|
* @brief OUT endpoint notification callback.
|
||||||
*/
|
*/
|
||||||
usbepcallback_t uepc_out_cb;
|
usbepcallback_t out_cb;
|
||||||
/**
|
/**
|
||||||
* @brief IN endpoint maximum packet size.
|
* @brief IN endpoint maximum packet size.
|
||||||
*/
|
*/
|
||||||
uint16_t uepc_in_maxsize;
|
uint16_t in_maxsize;
|
||||||
/**
|
/**
|
||||||
* @brief OUT endpoint maximum packet size.
|
* @brief OUT endpoint maximum packet size.
|
||||||
*/
|
*/
|
||||||
uint16_t uepc_out_maxsize;
|
uint16_t out_maxsize;
|
||||||
/* End of the mandatory fields.*/
|
/* End of the mandatory fields.*/
|
||||||
/**
|
/**
|
||||||
* @brief EPxR register initialization value.
|
* @brief EPxR register initialization value.
|
||||||
* @note Do not specify the EA field, leave it to zero.
|
* @note Do not specify the EA field, leave it to zero.
|
||||||
*/
|
*/
|
||||||
uint16_t uepc_epr;
|
uint16_t epr;
|
||||||
/**
|
/**
|
||||||
* @brief Endpoint IN buffer address as offset in the PMA.
|
* @brief Endpoint IN buffer address as offset in the PMA.
|
||||||
*/
|
*/
|
||||||
uint16_t uepc_inaddr;
|
uint16_t inaddr;
|
||||||
/**
|
/**
|
||||||
* @brief Endpoint OUT buffer address as offset in the PMA.
|
* @brief Endpoint OUT buffer address as offset in the PMA.
|
||||||
*/
|
*/
|
||||||
uint16_t uepc_outaddr;
|
uint16_t outaddr;
|
||||||
} USBEndpointConfig;
|
} USBEndpointConfig;
|
||||||
|
|
||||||
|
|
||||||
|
@ -133,39 +138,44 @@ typedef struct {
|
||||||
/**
|
/**
|
||||||
* @brief Configuration associated to the endpoint.
|
* @brief Configuration associated to the endpoint.
|
||||||
*/
|
*/
|
||||||
const USBEndpointConfig *uep_config;
|
const USBEndpointConfig *config;
|
||||||
/**
|
|
||||||
* @brief Pointer to the transmission buffer.
|
|
||||||
*/
|
|
||||||
const uint8_t *uep_txbuf;
|
|
||||||
/**
|
|
||||||
* @brief Pointer to the receive buffer.
|
|
||||||
*/
|
|
||||||
uint8_t *uep_rxbuf;
|
|
||||||
/**
|
|
||||||
* @brief Requested transmit transfer size.
|
|
||||||
*/
|
|
||||||
size_t uep_txsize;
|
|
||||||
/**
|
|
||||||
* @brief Requested receive transfer size.
|
|
||||||
*/
|
|
||||||
size_t uep_rxsize;
|
|
||||||
/**
|
|
||||||
* @brief Transmitted bytes so far.
|
|
||||||
*/
|
|
||||||
size_t uep_txcnt;
|
|
||||||
/**
|
|
||||||
* @brief Received bytes so far.
|
|
||||||
*/
|
|
||||||
size_t uep_rxcnt;
|
|
||||||
/**
|
/**
|
||||||
* @brief @p TRUE if transmitting else @p FALSE.
|
* @brief @p TRUE if transmitting else @p FALSE.
|
||||||
*/
|
*/
|
||||||
uint8_t uep_transmitting;
|
uint8_t transmitting;
|
||||||
/**
|
/**
|
||||||
* @brief @p TRUE if receiving else @p FALSE.
|
* @brief @p TRUE if receiving else @p FALSE.
|
||||||
*/
|
*/
|
||||||
uint8_t uep_receiving;
|
uint8_t receiving;
|
||||||
|
/* End of the mandatory fields.*/
|
||||||
|
/**
|
||||||
|
* @brief Number of packets to receive.
|
||||||
|
*/
|
||||||
|
uint16_t rxpkts;
|
||||||
|
/**
|
||||||
|
* @brief Pointer to the transmission buffer.
|
||||||
|
*/
|
||||||
|
const uint8_t *txbuf;
|
||||||
|
/**
|
||||||
|
* @brief Pointer to the receive buffer.
|
||||||
|
*/
|
||||||
|
uint8_t *rxbuf;
|
||||||
|
/**
|
||||||
|
* @brief Requested transmit transfer size.
|
||||||
|
*/
|
||||||
|
size_t txsize;
|
||||||
|
/**
|
||||||
|
* @brief Requested receive transfer size.
|
||||||
|
*/
|
||||||
|
size_t rxsize;
|
||||||
|
/**
|
||||||
|
* @brief Transmitted bytes so far.
|
||||||
|
*/
|
||||||
|
size_t txcnt;
|
||||||
|
/**
|
||||||
|
* @brief Received bytes so far.
|
||||||
|
*/
|
||||||
|
size_t rxcnt;
|
||||||
} USBEndpointState;
|
} USBEndpointState;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -176,22 +186,22 @@ typedef struct {
|
||||||
* @brief USB events callback.
|
* @brief USB events callback.
|
||||||
* @details This callback is invoked when an USB driver event is registered.
|
* @details This callback is invoked when an USB driver event is registered.
|
||||||
*/
|
*/
|
||||||
usbeventcb_t uc_event_cb;
|
usbeventcb_t event_cb;
|
||||||
/**
|
/**
|
||||||
* @brief Device GET_DESCRIPTOR request callback.
|
* @brief Device GET_DESCRIPTOR request callback.
|
||||||
* @note This callback is mandatory and cannot be set to @p NULL.
|
* @note This callback is mandatory and cannot be set to @p NULL.
|
||||||
*/
|
*/
|
||||||
usbgetdescriptor_t uc_get_descriptor_cb;
|
usbgetdescriptor_t get_descriptor_cb;
|
||||||
/**
|
/**
|
||||||
* @brief Requests hook callback.
|
* @brief Requests hook callback.
|
||||||
* @details This hook allows to be notified of standard requests or to
|
* @details This hook allows to be notified of standard requests or to
|
||||||
* handle non standard requests.
|
* handle non standard requests.
|
||||||
*/
|
*/
|
||||||
usbreqhandler_t uc_requests_hook_cb;
|
usbreqhandler_t requests_hook_cb;
|
||||||
/**
|
/**
|
||||||
* @brief Start Of Frame callback.
|
* @brief Start Of Frame callback.
|
||||||
*/
|
*/
|
||||||
usbcallback_t uc_sof_cb;
|
usbcallback_t sof_cb;
|
||||||
/* End of the mandatory fields.*/
|
/* End of the mandatory fields.*/
|
||||||
} USBConfig;
|
} USBConfig;
|
||||||
|
|
||||||
|
@ -202,60 +212,52 @@ struct USBDriver {
|
||||||
/**
|
/**
|
||||||
* @brief Driver state.
|
* @brief Driver state.
|
||||||
*/
|
*/
|
||||||
usbstate_t usb_state;
|
usbstate_t state;
|
||||||
/**
|
/**
|
||||||
* @brief Current configuration data.
|
* @brief Current configuration data.
|
||||||
*/
|
*/
|
||||||
const USBConfig *usb_config;
|
const USBConfig *config;
|
||||||
/**
|
/**
|
||||||
* @brief Field available to user, it can be used to associate an
|
* @brief Field available to user, it can be used to associate an
|
||||||
* application-defined handler to the USB driver.
|
* application-defined handler to the USB driver.
|
||||||
*/
|
*/
|
||||||
void *usb_param;
|
void *param;
|
||||||
/**
|
/**
|
||||||
* @brief Active endpoints configurations.
|
* @brief Active endpoints configurations.
|
||||||
*/
|
*/
|
||||||
USBEndpointState *usb_ep[USB_MAX_ENDPOINTS + 1];
|
USBEndpointState *ep[USB_MAX_ENDPOINTS + 1];
|
||||||
/**
|
/**
|
||||||
* @brief Endpoint 0 state.
|
* @brief Endpoint 0 state.
|
||||||
*/
|
*/
|
||||||
usbep0state_t usb_ep0state;
|
usbep0state_t ep0state;
|
||||||
/**
|
/**
|
||||||
* @brief Next position in the buffer to be transferred through endpoint 0.
|
* @brief Next position in the buffer to be transferred through endpoint 0.
|
||||||
*/
|
*/
|
||||||
uint8_t *usb_ep0next;
|
uint8_t *ep0next;
|
||||||
/**
|
/**
|
||||||
* @brief Maximum number of bytes to be tranferred through endpoint 0.
|
* @brief Maximum number of bytes to be transferred through endpoint 0.
|
||||||
*/
|
*/
|
||||||
size_t usb_ep0max;
|
size_t ep0max;
|
||||||
/**
|
/**
|
||||||
* @brief Number of bytes yet to be tranferred through endpoint 0.
|
* @brief Number of bytes yet to be transferred through endpoint 0.
|
||||||
*/
|
*/
|
||||||
size_t usb_ep0n;
|
size_t ep0n;
|
||||||
/**
|
|
||||||
* @brief Size of the last packet transferred through endpoint 0.
|
|
||||||
*/
|
|
||||||
size_t usb_ep0lastsize;
|
|
||||||
/**
|
|
||||||
* @brief Endpoint 0 end transaction callback.
|
|
||||||
*/
|
|
||||||
usbcallback_t usb_ep0endcb;
|
|
||||||
/**
|
/**
|
||||||
* @brief Setup packet buffer.
|
* @brief Setup packet buffer.
|
||||||
*/
|
*/
|
||||||
uint8_t usb_setup[8];
|
uint8_t setup[8];
|
||||||
/**
|
/**
|
||||||
* @brief Current USB device status.
|
* @brief Current USB device status.
|
||||||
*/
|
*/
|
||||||
uint16_t usb_status;
|
uint16_t status;
|
||||||
/**
|
/**
|
||||||
* @brief Assigned USB address.
|
* @brief Assigned USB address.
|
||||||
*/
|
*/
|
||||||
uint8_t usb_address;
|
uint8_t address;
|
||||||
/**
|
/**
|
||||||
* @brief Current USB device configuration.
|
* @brief Current USB device configuration.
|
||||||
*/
|
*/
|
||||||
uint8_t usb_configuration;
|
uint8_t configuration;
|
||||||
/* End of the mandatory fields.*/
|
/* End of the mandatory fields.*/
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -305,7 +307,7 @@ extern "C" {
|
||||||
void usb_lld_start(USBDriver *usbp);
|
void usb_lld_start(USBDriver *usbp);
|
||||||
void usb_lld_stop(USBDriver *usbp);
|
void usb_lld_stop(USBDriver *usbp);
|
||||||
void usb_lld_reset(USBDriver *usbp);
|
void usb_lld_reset(USBDriver *usbp);
|
||||||
void usb_lld_set_address(USBDriver *usbp, uint8_t addr);
|
void usb_lld_set_address(USBDriver *usbp);
|
||||||
void usb_lld_init_endpoint(USBDriver *usbp, usbep_t ep);
|
void usb_lld_init_endpoint(USBDriver *usbp, usbep_t ep);
|
||||||
void usb_lld_disable_endpoints(USBDriver *usbp);
|
void usb_lld_disable_endpoints(USBDriver *usbp);
|
||||||
size_t usb_lld_get_readable(USBDriver *usbp, usbep_t ep);
|
size_t usb_lld_get_readable(USBDriver *usbp, usbep_t ep);
|
||||||
|
@ -315,10 +317,14 @@ extern "C" {
|
||||||
const uint8_t *buf, size_t n);
|
const uint8_t *buf, size_t n);
|
||||||
usbepstatus_t usb_lld_get_status_in(USBDriver *usbp, usbep_t ep);
|
usbepstatus_t usb_lld_get_status_in(USBDriver *usbp, usbep_t ep);
|
||||||
usbepstatus_t usb_lld_get_status_out(USBDriver *usbp, usbep_t ep);
|
usbepstatus_t usb_lld_get_status_out(USBDriver *usbp, usbep_t ep);
|
||||||
|
void usb_lld_start_out(USBDriver *usbp, usbep_t ep,
|
||||||
|
uint8_t *buf, size_t n);
|
||||||
|
void usb_lld_start_in(USBDriver *usbp, usbep_t ep,
|
||||||
|
const uint8_t *buf, size_t n);
|
||||||
void usb_lld_stall_in(USBDriver *usbp, usbep_t ep);
|
void usb_lld_stall_in(USBDriver *usbp, usbep_t ep);
|
||||||
void usb_lld_stall_out(USBDriver *usbp, usbep_t ep);
|
void usb_lld_stall_out(USBDriver *usbp, usbep_t ep);
|
||||||
void usb_lld_clear_in(USBDriver *usbp, usbep_t ep);
|
|
||||||
void usb_lld_clear_out(USBDriver *usbp, usbep_t ep);
|
void usb_lld_clear_out(USBDriver *usbp, usbep_t ep);
|
||||||
|
void usb_lld_clear_in(USBDriver *usbp, usbep_t ep);
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
427
os/hal/src/usb.c
427
os/hal/src/usb.c
|
@ -56,72 +56,18 @@ static const uint8_t halted_status[] = {0x01, 0x00};
|
||||||
*/
|
*/
|
||||||
void set_address(USBDriver *usbp) {
|
void set_address(USBDriver *usbp) {
|
||||||
|
|
||||||
usbp->usb_address = usbp->usb_setup[2];
|
usbp->address = usbp->setup[2];
|
||||||
usb_lld_set_address(usbp, usbp->usb_address);
|
usb_lld_set_address(usbp);
|
||||||
if (usbp->usb_config->uc_event_cb)
|
if (usbp->config->event_cb)
|
||||||
usbp->usb_config->uc_event_cb(usbp, USB_EVENT_ADDRESS);
|
usbp->config->event_cb(usbp, USB_EVENT_ADDRESS);
|
||||||
usbp->usb_state = USB_SELECTED;
|
usbp->state = USB_SELECTED;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Starts a receive phase on the endpoint zero.
|
|
||||||
*
|
|
||||||
* @param[in] usbp pointer to the @p USBDriver object
|
|
||||||
*/
|
|
||||||
static void start_rx_ep0(USBDriver *usbp) {
|
|
||||||
|
|
||||||
if (usbp->usb_ep0n > 0) {
|
|
||||||
/* The received data cannot exceed the available amount.*/
|
|
||||||
if (usbp->usb_ep0n > usbp->usb_ep0max)
|
|
||||||
usbp->usb_ep0n = usbp->usb_ep0max;
|
|
||||||
|
|
||||||
/* Determines the maximum amount that can be received using a
|
|
||||||
single packet.*/
|
|
||||||
if (usbp->usb_ep0n > usbp->usb_ep[0]->uep_config->uepc_out_maxsize)
|
|
||||||
usbp->usb_ep0lastsize = usbp->usb_ep[0]->uep_config->uepc_out_maxsize;
|
|
||||||
else
|
|
||||||
usbp->usb_ep0lastsize = usbp->usb_ep0n;
|
|
||||||
usbp->usb_ep0state = USB_EP0_RX;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
/* Sending zero sized status packet.*/
|
|
||||||
usb_lld_write(usbp, 0, NULL, 0);
|
|
||||||
usbp->usb_ep0state = USB_EP0_SENDING_STS;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Starts a transmission phase on the endpoint zero.
|
|
||||||
*
|
|
||||||
* @param[in] usbp pointer to the @p USBDriver object
|
|
||||||
*/
|
|
||||||
static void start_tx_ep0(USBDriver *usbp) {
|
|
||||||
|
|
||||||
if (usbp->usb_ep0n > 0) {
|
|
||||||
/* The transmitted data cannot exceed the requested amount.*/
|
|
||||||
if (usbp->usb_ep0n > usbp->usb_ep0max)
|
|
||||||
usbp->usb_ep0n = usbp->usb_ep0max;
|
|
||||||
|
|
||||||
/* Determines the maximum amount that can be transmitted using a
|
|
||||||
single packet.*/
|
|
||||||
if (usbp->usb_ep0n > usbp->usb_ep[0]->uep_config->uepc_in_maxsize)
|
|
||||||
usbp->usb_ep0lastsize = usbp->usb_ep[0]->uep_config->uepc_in_maxsize;
|
|
||||||
else
|
|
||||||
usbp->usb_ep0lastsize = usbp->usb_ep0n;
|
|
||||||
|
|
||||||
/* Starts transmission.*/
|
|
||||||
usb_lld_write(usbp, 0, usbp->usb_ep0next, usbp->usb_ep0lastsize);
|
|
||||||
usbp->usb_ep0state = USB_EP0_TX;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
usbp->usb_ep0state = USB_EP0_WAITING_STS;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Standard requests handler.
|
* @brief Standard requests handler.
|
||||||
* @details This is the standard requests default handler, most standard
|
* @details This is the standard requests default handler, most standard
|
||||||
* requests are handled here, the user can override the standard
|
* requests are handled here, the user can override the standard
|
||||||
* handling using the @p uc_requests_hook_cb hook in the
|
* handling using the @p requests_hook_cb hook in the
|
||||||
* @p USBConfig structure.
|
* @p USBConfig structure.
|
||||||
*
|
*
|
||||||
* @param[in] usbp pointer to the @p USBDriver object
|
* @param[in] usbp pointer to the @p USBDriver object
|
||||||
|
@ -133,87 +79,93 @@ static bool_t default_handler(USBDriver *usbp) {
|
||||||
const USBDescriptor *dp;
|
const USBDescriptor *dp;
|
||||||
|
|
||||||
/* Decoding the request.*/
|
/* Decoding the request.*/
|
||||||
switch (((usbp->usb_setup[0] & (USB_RTYPE_RECIPIENT_MASK |
|
switch (((usbp->setup[0] & (USB_RTYPE_RECIPIENT_MASK |
|
||||||
USB_RTYPE_TYPE_MASK)) |
|
USB_RTYPE_TYPE_MASK)) |
|
||||||
(usbp->usb_setup[1] << 8))) {
|
(usbp->setup[1] << 8))) {
|
||||||
case USB_RTYPE_RECIPIENT_DEVICE | (USB_REQ_GET_STATUS << 8):
|
case USB_RTYPE_RECIPIENT_DEVICE | (USB_REQ_GET_STATUS << 8):
|
||||||
/* Just returns the current status word.*/
|
/* Just returns the current status word.*/
|
||||||
usbSetupTransfer(usbp, (uint8_t *)&usbp->usb_status, 2, NULL);
|
usbSetupTransfer(usbp, (uint8_t *)&usbp->status, 2);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
case USB_RTYPE_RECIPIENT_DEVICE | (USB_REQ_CLEAR_FEATURE << 8):
|
case USB_RTYPE_RECIPIENT_DEVICE | (USB_REQ_CLEAR_FEATURE << 8):
|
||||||
/* Only the DEVICE_REMOTE_WAKEUP is handled here, any other feature
|
/* Only the DEVICE_REMOTE_WAKEUP is handled here, any other feature
|
||||||
number is handled as an error.*/
|
number is handled as an error.*/
|
||||||
if (usbp->usb_setup[2] == USB_FEATURE_DEVICE_REMOTE_WAKEUP) {
|
if (usbp->setup[2] == USB_FEATURE_DEVICE_REMOTE_WAKEUP) {
|
||||||
usbp->usb_status &= ~2;
|
usbp->status &= ~2;
|
||||||
usbSetupTransfer(usbp, NULL, 0, NULL);
|
usbSetupTransfer(usbp, NULL, 0);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
return FALSE;
|
return FALSE;
|
||||||
case USB_RTYPE_RECIPIENT_DEVICE | (USB_REQ_SET_FEATURE << 8):
|
case USB_RTYPE_RECIPIENT_DEVICE | (USB_REQ_SET_FEATURE << 8):
|
||||||
/* Only the DEVICE_REMOTE_WAKEUP is handled here, any other feature
|
/* Only the DEVICE_REMOTE_WAKEUP is handled here, any other feature
|
||||||
number is handled as an error.*/
|
number is handled as an error.*/
|
||||||
if (usbp->usb_setup[2] == USB_FEATURE_DEVICE_REMOTE_WAKEUP) {
|
if (usbp->setup[2] == USB_FEATURE_DEVICE_REMOTE_WAKEUP) {
|
||||||
usbp->usb_status |= 2;
|
usbp->status |= 2;
|
||||||
usbSetupTransfer(usbp, NULL, 0, NULL);
|
usbSetupTransfer(usbp, NULL, 0);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
return FALSE;
|
return FALSE;
|
||||||
case USB_RTYPE_RECIPIENT_DEVICE | (USB_REQ_SET_ADDRESS << 8):
|
case USB_RTYPE_RECIPIENT_DEVICE | (USB_REQ_SET_ADDRESS << 8):
|
||||||
/* The handling is posponed to after the status phase in order to allow
|
/* The SET_ADDRESS handling can be performed here or postponed after
|
||||||
the proper completion of the transaction.*/
|
the status packed depending on the USB_SET_ADDRESS_MODE low
|
||||||
usbSetupTransfer(usbp, NULL, 0, set_address);
|
driver setting.*/
|
||||||
|
#if USB_SET_ADDRESS_MODE == USB_EARLY_SET_ADDRESS
|
||||||
|
if ((usbp->setup[0] == USB_RTYPE_RECIPIENT_DEVICE) &&
|
||||||
|
(usbp->setup[1] == USB_REQ_SET_ADDRESS))
|
||||||
|
set_address(usbp);
|
||||||
|
#endif
|
||||||
|
usbSetupTransfer(usbp, NULL, 0);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
case USB_RTYPE_RECIPIENT_DEVICE | (USB_REQ_GET_DESCRIPTOR << 8):
|
case USB_RTYPE_RECIPIENT_DEVICE | (USB_REQ_GET_DESCRIPTOR << 8):
|
||||||
/* Handling descriptor requests from the host.*/
|
/* Handling descriptor requests from the host.*/
|
||||||
dp = usbp->usb_config->uc_get_descriptor_cb(
|
dp = usbp->config->get_descriptor_cb(
|
||||||
usbp, usbp->usb_setup[3], usbp->usb_setup[2],
|
usbp, usbp->setup[3], usbp->setup[2],
|
||||||
usb_lld_fetch_word(&usbp->usb_setup[4]));
|
usb_lld_fetch_word(&usbp->setup[4]));
|
||||||
if (dp == NULL)
|
if (dp == NULL)
|
||||||
return FALSE;
|
return FALSE;
|
||||||
usbSetupTransfer(usbp, (uint8_t *)dp->ud_string, dp->ud_size, NULL);
|
usbSetupTransfer(usbp, (uint8_t *)dp->ud_string, dp->ud_size);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
case USB_RTYPE_RECIPIENT_DEVICE | (USB_REQ_GET_CONFIGURATION << 8):
|
case USB_RTYPE_RECIPIENT_DEVICE | (USB_REQ_GET_CONFIGURATION << 8):
|
||||||
/* Returning the last selected configuration.*/
|
/* Returning the last selected configuration.*/
|
||||||
usbSetupTransfer(usbp, &usbp->usb_configuration, 1, NULL);
|
usbSetupTransfer(usbp, &usbp->configuration, 1);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
case USB_RTYPE_RECIPIENT_DEVICE | (USB_REQ_SET_CONFIGURATION << 8):
|
case USB_RTYPE_RECIPIENT_DEVICE | (USB_REQ_SET_CONFIGURATION << 8):
|
||||||
/* Handling configuration selection from the host.*/
|
/* Handling configuration selection from the host.*/
|
||||||
usbp->usb_configuration = usbp->usb_setup[2];
|
usbp->configuration = usbp->setup[2];
|
||||||
if (usbp->usb_configuration == 0)
|
if (usbp->configuration == 0)
|
||||||
usbp->usb_state = USB_SELECTED;
|
usbp->state = USB_SELECTED;
|
||||||
else
|
else
|
||||||
usbp->usb_state = USB_ACTIVE;
|
usbp->state = USB_ACTIVE;
|
||||||
if (usbp->usb_config->uc_event_cb)
|
if (usbp->config->event_cb)
|
||||||
usbp->usb_config->uc_event_cb(usbp, USB_EVENT_CONFIGURED);
|
usbp->config->event_cb(usbp, USB_EVENT_CONFIGURED);
|
||||||
usbSetupTransfer(usbp, NULL, 0, NULL);
|
usbSetupTransfer(usbp, NULL, 0);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
case USB_RTYPE_RECIPIENT_INTERFACE | (USB_REQ_GET_STATUS << 8):
|
case USB_RTYPE_RECIPIENT_INTERFACE | (USB_REQ_GET_STATUS << 8):
|
||||||
case USB_RTYPE_RECIPIENT_ENDPOINT | (USB_REQ_SYNCH_FRAME << 8):
|
case USB_RTYPE_RECIPIENT_ENDPOINT | (USB_REQ_SYNCH_FRAME << 8):
|
||||||
/* Just sending two zero bytes, the application can change the behavior
|
/* Just sending two zero bytes, the application can change the behavior
|
||||||
using a hook..*/
|
using a hook..*/
|
||||||
usbSetupTransfer(usbp, (uint8_t *)zero_status, 2, NULL);
|
usbSetupTransfer(usbp, (uint8_t *)zero_status, 2);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
case USB_RTYPE_RECIPIENT_ENDPOINT | (USB_REQ_GET_STATUS << 8):
|
case USB_RTYPE_RECIPIENT_ENDPOINT | (USB_REQ_GET_STATUS << 8):
|
||||||
/* Sending the EP status.*/
|
/* Sending the EP status.*/
|
||||||
if (usbp->usb_setup[4] & 0x80) {
|
if (usbp->setup[4] & 0x80) {
|
||||||
switch (usb_lld_get_status_in(usbp, usbp->usb_setup[4] & 0x0F)) {
|
switch (usb_lld_get_status_in(usbp, usbp->setup[4] & 0x0F)) {
|
||||||
case EP_STATUS_STALLED:
|
case EP_STATUS_STALLED:
|
||||||
usbSetupTransfer(usbp, (uint8_t *)halted_status, 2, NULL);
|
usbSetupTransfer(usbp, (uint8_t *)halted_status, 2);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
case EP_STATUS_ACTIVE:
|
case EP_STATUS_ACTIVE:
|
||||||
usbSetupTransfer(usbp, (uint8_t *)active_status, 2, NULL);
|
usbSetupTransfer(usbp, (uint8_t *)active_status, 2);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
default:
|
default:
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
switch (usb_lld_get_status_out(usbp, usbp->usb_setup[4] & 0x0F)) {
|
switch (usb_lld_get_status_out(usbp, usbp->setup[4] & 0x0F)) {
|
||||||
case EP_STATUS_STALLED:
|
case EP_STATUS_STALLED:
|
||||||
usbSetupTransfer(usbp, (uint8_t *)halted_status, 2, NULL);
|
usbSetupTransfer(usbp, (uint8_t *)halted_status, 2);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
case EP_STATUS_ACTIVE:
|
case EP_STATUS_ACTIVE:
|
||||||
usbSetupTransfer(usbp, (uint8_t *)active_status, 2, NULL);
|
usbSetupTransfer(usbp, (uint8_t *)active_status, 2);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
default:
|
default:
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
@ -221,29 +173,29 @@ static bool_t default_handler(USBDriver *usbp) {
|
||||||
}
|
}
|
||||||
case USB_RTYPE_RECIPIENT_ENDPOINT | (USB_REQ_CLEAR_FEATURE << 8):
|
case USB_RTYPE_RECIPIENT_ENDPOINT | (USB_REQ_CLEAR_FEATURE << 8):
|
||||||
/* Only ENDPOINT_HALT is handled as feature.*/
|
/* Only ENDPOINT_HALT is handled as feature.*/
|
||||||
if (usbp->usb_setup[2] != USB_FEATURE_ENDPOINT_HALT)
|
if (usbp->setup[2] != USB_FEATURE_ENDPOINT_HALT)
|
||||||
return FALSE;
|
return FALSE;
|
||||||
/* Clearing the EP status, not valid for EP0, it is ignored in that case.*/
|
/* Clearing the EP status, not valid for EP0, it is ignored in that case.*/
|
||||||
if ((usbp->usb_setup[4] & 0x0F) > 0) {
|
if ((usbp->setup[4] & 0x0F) > 0) {
|
||||||
if (usbp->usb_setup[4] & 0x80)
|
if (usbp->setup[4] & 0x80)
|
||||||
usb_lld_clear_in(usbp, usbp->usb_setup[4] & 0x0F);
|
usb_lld_clear_in(usbp, usbp->setup[4] & 0x0F);
|
||||||
else
|
else
|
||||||
usb_lld_clear_out(usbp, usbp->usb_setup[4] & 0x0F);
|
usb_lld_clear_out(usbp, usbp->setup[4] & 0x0F);
|
||||||
}
|
}
|
||||||
usbSetupTransfer(usbp, NULL, 0, NULL);
|
usbSetupTransfer(usbp, NULL, 0);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
case USB_RTYPE_RECIPIENT_ENDPOINT | (USB_REQ_SET_FEATURE << 8):
|
case USB_RTYPE_RECIPIENT_ENDPOINT | (USB_REQ_SET_FEATURE << 8):
|
||||||
/* Only ENDPOINT_HALT is handled as feature.*/
|
/* Only ENDPOINT_HALT is handled as feature.*/
|
||||||
if (usbp->usb_setup[2] != USB_FEATURE_ENDPOINT_HALT)
|
if (usbp->setup[2] != USB_FEATURE_ENDPOINT_HALT)
|
||||||
return FALSE;
|
return FALSE;
|
||||||
/* Stalling the EP, not valid for EP0, it is ignored in that case.*/
|
/* Stalling the EP, not valid for EP0, it is ignored in that case.*/
|
||||||
if ((usbp->usb_setup[4] & 0x0F) > 0) {
|
if ((usbp->setup[4] & 0x0F) > 0) {
|
||||||
if (usbp->usb_setup[4] & 0x80)
|
if (usbp->setup[4] & 0x80)
|
||||||
usb_lld_stall_in(usbp, usbp->usb_setup[4] & 0x0F);
|
usb_lld_stall_in(usbp, usbp->setup[4] & 0x0F);
|
||||||
else
|
else
|
||||||
usb_lld_stall_out(usbp, usbp->usb_setup[4] & 0x0F);
|
usb_lld_stall_out(usbp, usbp->setup[4] & 0x0F);
|
||||||
}
|
}
|
||||||
usbSetupTransfer(usbp, NULL, 0, NULL);
|
usbSetupTransfer(usbp, NULL, 0);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
case USB_RTYPE_RECIPIENT_DEVICE | (USB_REQ_SET_DESCRIPTOR << 8):
|
case USB_RTYPE_RECIPIENT_DEVICE | (USB_REQ_SET_DESCRIPTOR << 8):
|
||||||
case USB_RTYPE_RECIPIENT_INTERFACE | (USB_REQ_CLEAR_FEATURE << 8):
|
case USB_RTYPE_RECIPIENT_INTERFACE | (USB_REQ_CLEAR_FEATURE << 8):
|
||||||
|
@ -282,9 +234,9 @@ void usbInit(void) {
|
||||||
*/
|
*/
|
||||||
void usbObjectInit(USBDriver *usbp) {
|
void usbObjectInit(USBDriver *usbp) {
|
||||||
|
|
||||||
usbp->usb_state = USB_STOP;
|
usbp->state = USB_STOP;
|
||||||
usbp->usb_config = NULL;
|
usbp->config = NULL;
|
||||||
usbp->usb_param = NULL;
|
usbp->param = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -301,13 +253,13 @@ void usbStart(USBDriver *usbp, const USBConfig *config) {
|
||||||
chDbgCheck((usbp != NULL) && (config != NULL), "usbStart");
|
chDbgCheck((usbp != NULL) && (config != NULL), "usbStart");
|
||||||
|
|
||||||
chSysLock();
|
chSysLock();
|
||||||
chDbgAssert((usbp->usb_state == USB_STOP) || (usbp->usb_state == USB_READY),
|
chDbgAssert((usbp->state == USB_STOP) || (usbp->state == USB_READY),
|
||||||
"usbStart(), #1", "invalid state");
|
"usbStart(), #1", "invalid state");
|
||||||
usbp->usb_config = config;
|
usbp->config = config;
|
||||||
for (i = 0; i <= USB_MAX_ENDPOINTS; i++)
|
for (i = 0; i <= USB_MAX_ENDPOINTS; i++)
|
||||||
usbp->usb_ep[i] = NULL;
|
usbp->ep[i] = NULL;
|
||||||
usb_lld_start(usbp);
|
usb_lld_start(usbp);
|
||||||
usbp->usb_state = USB_READY;
|
usbp->state = USB_READY;
|
||||||
chSysUnlock();
|
chSysUnlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -323,11 +275,10 @@ void usbStop(USBDriver *usbp) {
|
||||||
chDbgCheck(usbp != NULL, "usbStop");
|
chDbgCheck(usbp != NULL, "usbStop");
|
||||||
|
|
||||||
chSysLock();
|
chSysLock();
|
||||||
chDbgAssert((usbp->usb_state == USB_STOP) || (usbp->usb_state == USB_READY),
|
chDbgAssert((usbp->state == USB_STOP) || (usbp->state == USB_READY),
|
||||||
"usbStop(), #1",
|
"usbStop(), #1", "invalid state");
|
||||||
"invalid state");
|
|
||||||
usb_lld_stop(usbp);
|
usb_lld_stop(usbp);
|
||||||
usbp->usb_state = USB_STOP;
|
usbp->state = USB_STOP;
|
||||||
chSysUnlock();
|
chSysUnlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -348,15 +299,15 @@ void usbStop(USBDriver *usbp) {
|
||||||
void usbInitEndpointI(USBDriver *usbp, usbep_t ep, USBEndpointState *epp,
|
void usbInitEndpointI(USBDriver *usbp, usbep_t ep, USBEndpointState *epp,
|
||||||
const USBEndpointConfig *epcp) {
|
const USBEndpointConfig *epcp) {
|
||||||
|
|
||||||
chDbgAssert(usbp->usb_state == USB_ACTIVE,
|
chDbgAssert(usbp->state == USB_ACTIVE,
|
||||||
"usbEnableEndpointI(), #1", "invalid state");
|
"usbEnableEndpointI(), #1", "invalid state");
|
||||||
chDbgAssert(usbp->usb_ep[ep] != NULL,
|
chDbgAssert(usbp->ep[ep] != NULL,
|
||||||
"usbEnableEndpointI(), #2", "already initialized");
|
"usbEnableEndpointI(), #2", "already initialized");
|
||||||
|
|
||||||
/* Logically enabling the endpoint in the USBDriver structure.*/
|
/* Logically enabling the endpoint in the USBDriver structure.*/
|
||||||
memset(epp, 0, sizeof(USBEndpointState));
|
memset(epp, 0, sizeof(USBEndpointState));
|
||||||
epp->uep_config = epcp;
|
epp->config = epcp;
|
||||||
usbp->usb_ep[ep] = epp;
|
usbp->ep[ep] = epp;
|
||||||
|
|
||||||
/* Low level endpoint activation.*/
|
/* Low level endpoint activation.*/
|
||||||
usb_lld_init_endpoint(usbp, ep);
|
usb_lld_init_endpoint(usbp, ep);
|
||||||
|
@ -376,16 +327,100 @@ void usbInitEndpointI(USBDriver *usbp, usbep_t ep, USBEndpointState *epp,
|
||||||
void usbDisableEndpointsI(USBDriver *usbp) {
|
void usbDisableEndpointsI(USBDriver *usbp) {
|
||||||
unsigned i;
|
unsigned i;
|
||||||
|
|
||||||
chDbgAssert(usbp->usb_state == USB_SELECTED,
|
chDbgAssert(usbp->state == USB_SELECTED,
|
||||||
"usbDisableEndpointsI(), #1", "invalid state");
|
"usbDisableEndpointsI(), #1", "invalid state");
|
||||||
|
|
||||||
for (i = 1; i <= USB_MAX_ENDPOINTS; i++)
|
for (i = 1; i <= USB_MAX_ENDPOINTS; i++)
|
||||||
usbp->usb_ep[i] = NULL;
|
usbp->ep[i] = NULL;
|
||||||
|
|
||||||
/* Low level endpoints deactivation.*/
|
/* Low level endpoints deactivation.*/
|
||||||
usb_lld_disable_endpoints(usbp);
|
usb_lld_disable_endpoints(usbp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Starts a receive operation on an OUT endpoint.
|
||||||
|
*
|
||||||
|
* @param[in] usbp pointer to the @p USBDriver object
|
||||||
|
* @param[in] ep endpoint number
|
||||||
|
* @param[out] buf buffer where to copy the endpoint data
|
||||||
|
* @param[in] n maximum number of bytes to copy in the buffer
|
||||||
|
* @return The operation status.
|
||||||
|
* @retval FALSE Receive operation started.
|
||||||
|
* @retval TRUE Endpoint already receiving.
|
||||||
|
*
|
||||||
|
* @iclass
|
||||||
|
*/
|
||||||
|
bool_t usbStartReceiveI(USBDriver *usbp, usbep_t ep,
|
||||||
|
uint8_t *buf, size_t n) {
|
||||||
|
|
||||||
|
if (usbp->ep[ep]->receiving)
|
||||||
|
return TRUE;
|
||||||
|
usbp->ep[ep]->receiving = TRUE;
|
||||||
|
usb_lld_start_out(usbp, ep, buf, n);
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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
|
||||||
|
* @return The operation status.
|
||||||
|
* @retval FALSE Transmit operation started.
|
||||||
|
* @retval TRUE Endpoint already transmitting.
|
||||||
|
*
|
||||||
|
* @iclass
|
||||||
|
*/
|
||||||
|
bool_t usbStartTransmitI(USBDriver *usbp, usbep_t ep,
|
||||||
|
const uint8_t *buf, size_t n) {
|
||||||
|
|
||||||
|
if (usbp->ep[ep]->transmitting)
|
||||||
|
return TRUE;
|
||||||
|
usbp->ep[ep]->transmitting = TRUE;
|
||||||
|
usb_lld_start_in(usbp, ep, buf, n);
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Stalls an OUT endpoint.
|
||||||
|
*
|
||||||
|
* @param[in] usbp pointer to the @p USBDriver object
|
||||||
|
* @param[in] ep endpoint number
|
||||||
|
* @return The operation status.
|
||||||
|
* @retval FALSE Endpoint stalled.
|
||||||
|
* @retval TRUE The endpoint was within a transaction, not stalled.
|
||||||
|
*
|
||||||
|
* @iclass
|
||||||
|
*/
|
||||||
|
bool_t usbStallReceiveI(USBDriver *usbp, usbep_t ep) {
|
||||||
|
|
||||||
|
if (usbp->ep[ep]->receiving)
|
||||||
|
return TRUE;
|
||||||
|
usb_lld_stall_out(usbp, ep);
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Stalls an IN endpoint.
|
||||||
|
*
|
||||||
|
* @param[in] usbp pointer to the @p USBDriver object
|
||||||
|
* @param[in] ep endpoint number
|
||||||
|
* @return The operation status.
|
||||||
|
* @retval FALSE Endpoint stalled.
|
||||||
|
* @retval TRUE The endpoint was within a transaction, not stalled.
|
||||||
|
*
|
||||||
|
* @iclass
|
||||||
|
*/
|
||||||
|
bool_t usbStallTransmitI(USBDriver *usbp, usbep_t ep) {
|
||||||
|
|
||||||
|
if (usbp->ep[ep]->transmitting)
|
||||||
|
return TRUE;
|
||||||
|
usb_lld_stall_in(usbp, ep);
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief USB reset routine.
|
* @brief USB reset routine.
|
||||||
*
|
*
|
||||||
|
@ -396,24 +431,20 @@ void usbDisableEndpointsI(USBDriver *usbp) {
|
||||||
void _usb_reset(USBDriver *usbp) {
|
void _usb_reset(USBDriver *usbp) {
|
||||||
unsigned i;
|
unsigned i;
|
||||||
|
|
||||||
usbp->usb_state = USB_READY;
|
usbp->state = USB_READY;
|
||||||
usbp->usb_status = 0;
|
usbp->status = 0;
|
||||||
usbp->usb_address = 0;
|
usbp->address = 0;
|
||||||
usbp->usb_configuration = 0;
|
usbp->configuration = 0;
|
||||||
|
|
||||||
/* Invalidates all endpoints into the USBDriver structure.*/
|
/* Invalidates all endpoints into the USBDriver structure.*/
|
||||||
for (i = 0; i <= USB_MAX_ENDPOINTS; i++)
|
for (i = 0; i <= USB_MAX_ENDPOINTS; i++)
|
||||||
usbp->usb_ep[i] = NULL;
|
usbp->ep[i] = NULL;
|
||||||
|
|
||||||
/* EP0 state machine initialization.*/
|
/* EP0 state machine initialization.*/
|
||||||
usbp->usb_ep0state = USB_EP0_WAITING_SETUP;
|
usbp->ep0state = USB_EP0_WAITING_SETUP;
|
||||||
|
|
||||||
/* Low level reset.*/
|
/* Low level reset.*/
|
||||||
usb_lld_reset(usbp);
|
usb_lld_reset(usbp);
|
||||||
|
|
||||||
/* Endpoint zero initialization.*/
|
|
||||||
/* usbp->usb_ep[0].uep_config = &usb_lld_ep0config;
|
|
||||||
usb_lld_init_endpoint(usbp, 0, &usb_lld_ep0config);*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -427,33 +458,30 @@ void _usb_reset(USBDriver *usbp) {
|
||||||
* @notapi
|
* @notapi
|
||||||
*/
|
*/
|
||||||
void _usb_ep0in(USBDriver *usbp, usbep_t ep) {
|
void _usb_ep0in(USBDriver *usbp, usbep_t ep) {
|
||||||
|
size_t max;
|
||||||
|
|
||||||
(void)ep;
|
(void)ep;
|
||||||
switch (usbp->usb_ep0state) {
|
switch (usbp->ep0state) {
|
||||||
case USB_EP0_TX:
|
case USB_EP0_TX:
|
||||||
usbp->usb_ep0next += usbp->usb_ep0lastsize;
|
max = usb_lld_fetch_word(&usbp->setup[6]);
|
||||||
usbp->usb_ep0max -= usbp->usb_ep0lastsize;
|
/* If the transmitted size is less than the requested size and it is a
|
||||||
usbp->usb_ep0n -= usbp->usb_ep0lastsize;
|
multiple of the maximum packet size then a zero size packet must be
|
||||||
|
transmitted.*/
|
||||||
/* The final condition is when the requested size has been transmitted or
|
if ((usbp->ep0n < max) &&
|
||||||
when a packet has been sent with size less than the maximum packet
|
((usbp->ep0n % usbp->ep[0]->config->in_maxsize) == 0)) {
|
||||||
size.*/
|
usb_lld_start_in(usbp, 0, NULL, 0);
|
||||||
if ((usbp->usb_ep0max == 0) ||
|
return;
|
||||||
(usbp->usb_ep0lastsize < usbp->usb_ep[0]->uep_config->uepc_in_maxsize))
|
}
|
||||||
usbp->usb_ep0state = USB_EP0_WAITING_STS;
|
usbp->ep0state = USB_EP0_WAITING_STS;
|
||||||
else {
|
usb_lld_start_out(usbp, 0, NULL, 0);
|
||||||
usbp->usb_ep0lastsize =
|
|
||||||
usbp->usb_ep0n > usbp->usb_ep[0]->uep_config->uepc_in_maxsize ?
|
|
||||||
usbp->usb_ep[0]->uep_config->uepc_in_maxsize :
|
|
||||||
usbp->usb_ep0n;
|
|
||||||
usb_lld_write(usbp, 0, usbp->usb_ep0next, usbp->usb_ep0lastsize);
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
case USB_EP0_SENDING_STS:
|
case USB_EP0_SENDING_STS:
|
||||||
if (usbp->usb_ep0endcb)
|
#if USB_SET_ADDRESS_MODE == USB_LATE_SET_ADDRESS
|
||||||
usbp->usb_ep0endcb(usbp);
|
if ((usbp->setup[0] == USB_RTYPE_RECIPIENT_DEVICE) &&
|
||||||
|
(usbp->setup[1] == USB_REQ_SET_ADDRESS))
|
||||||
usbp->usb_ep0state = USB_EP0_WAITING_SETUP;
|
set_address(usbp);
|
||||||
|
#endif
|
||||||
|
usbp->ep0state = USB_EP0_WAITING_SETUP;
|
||||||
return;
|
return;
|
||||||
default:
|
default:
|
||||||
;
|
;
|
||||||
|
@ -461,9 +489,9 @@ void _usb_ep0in(USBDriver *usbp, usbep_t ep) {
|
||||||
/* Error response.*/
|
/* Error response.*/
|
||||||
usb_lld_stall_in(usbp, 0);
|
usb_lld_stall_in(usbp, 0);
|
||||||
usb_lld_stall_out(usbp, 0);
|
usb_lld_stall_out(usbp, 0);
|
||||||
if (usbp->usb_config->uc_event_cb)
|
if (usbp->config->event_cb)
|
||||||
usbp->usb_config->uc_event_cb(usbp, USB_EVENT_STALLED);
|
usbp->config->event_cb(usbp, USB_EVENT_STALLED);
|
||||||
usbp->usb_ep0state = USB_EP0_WAITING_SETUP;
|
usbp->ep0state = USB_EP0_WAITING_SETUP;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -477,62 +505,67 @@ void _usb_ep0in(USBDriver *usbp, usbep_t ep) {
|
||||||
* @notapi
|
* @notapi
|
||||||
*/
|
*/
|
||||||
void _usb_ep0out(USBDriver *usbp, usbep_t ep) {
|
void _usb_ep0out(USBDriver *usbp, usbep_t ep) {
|
||||||
size_t n, size;
|
size_t n, max;
|
||||||
uint8_t buf[1];
|
|
||||||
|
|
||||||
(void)ep;
|
(void)ep;
|
||||||
switch (usbp->usb_ep0state) {
|
switch (usbp->ep0state) {
|
||||||
case USB_EP0_WAITING_SETUP:
|
case USB_EP0_WAITING_SETUP:
|
||||||
/* SETUP packet handling.*/
|
/* SETUP packet handling.
|
||||||
n = usb_lld_read(usbp, 0, usbp->usb_setup, 8);
|
First verify if the application has an handler installed for this
|
||||||
if (n != 8)
|
|
||||||
break;
|
|
||||||
|
|
||||||
/* First verify if the application has an handler installed for this
|
|
||||||
request.*/
|
request.*/
|
||||||
if (!(usbp->usb_config->uc_requests_hook_cb) ||
|
if (!(usbp->config->requests_hook_cb) ||
|
||||||
!(usbp->usb_config->uc_requests_hook_cb(usbp))) {
|
!(usbp->config->requests_hook_cb(usbp))) {
|
||||||
/* Invoking the default handler, if this fails then stalls the
|
/* Invoking the default handler, if this fails then stalls the
|
||||||
endpoint zero as error.*/
|
endpoint zero as error.*/
|
||||||
if (((usbp->usb_setup[0] & USB_RTYPE_TYPE_MASK) != USB_RTYPE_TYPE_STD) ||
|
if (((usbp->setup[0] & USB_RTYPE_TYPE_MASK) != USB_RTYPE_TYPE_STD) ||
|
||||||
!default_handler(usbp))
|
!default_handler(usbp))
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Transfer preparation. The request handler must have populated
|
/* Transfer preparation. The request handler must have populated
|
||||||
correctly the fields usb_ep0next, usb_ep0n and usb_ep0endcb using
|
correctly the fields ep0next, ep0n and ep0endcb using
|
||||||
the macro usbSetupTransfer().*/
|
the macro usbSetupTransfer().*/
|
||||||
usbp->usb_ep0max = usb_lld_fetch_word(&usbp->usb_setup[6]);
|
max = usb_lld_fetch_word(&usbp->setup[6]);
|
||||||
if ((usbp->usb_setup[0] & USB_RTYPE_DIR_MASK) == USB_RTYPE_DIR_DEV2HOST)
|
/* The transfer size cannot exceed the specified amount.*/
|
||||||
start_tx_ep0(usbp);
|
if (usbp->ep0n > max)
|
||||||
else
|
usbp->ep0n = max;
|
||||||
start_rx_ep0(usbp);
|
if ((usbp->setup[0] & USB_RTYPE_DIR_MASK) == USB_RTYPE_DIR_DEV2HOST) {
|
||||||
|
/* IN phase.*/
|
||||||
|
if (usbp->ep0n > 0) {
|
||||||
|
/* Starts transmission.*/
|
||||||
|
usbp->ep0state = USB_EP0_TX;
|
||||||
|
usb_lld_start_in(usbp, 0, usbp->ep0next, usbp->ep0n);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
/* Receiving the zero sized status packet.*/
|
||||||
|
usbp->ep0state = USB_EP0_WAITING_STS;
|
||||||
|
usb_lld_start_out(usbp, 0, NULL, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
/* OUT phase.*/
|
||||||
|
if (usbp->ep0n > 0) {
|
||||||
|
/* Starts reception.*/
|
||||||
|
usbp->ep0state = USB_EP0_RX;
|
||||||
|
usb_lld_start_out(usbp, 0, usbp->ep0next, usbp->ep0n);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
/* Sending zero sized status packet.*/
|
||||||
|
usbp->ep0state = USB_EP0_SENDING_STS;
|
||||||
|
usb_lld_start_in(usbp, 0, NULL, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
case USB_EP0_RX:
|
case USB_EP0_RX:
|
||||||
/* Check for buffer overflow.*/
|
usbp->ep0state = USB_EP0_SENDING_STS;
|
||||||
n = size = usb_lld_get_readable(usbp, 0);
|
usb_lld_start_in(usbp, 0, NULL, 0);
|
||||||
if (n > usbp->usb_ep0n)
|
|
||||||
n = usbp->usb_ep0n;
|
|
||||||
/* Fetching received data packet.*/
|
|
||||||
n = usb_lld_read(usbp, 0, usbp->usb_ep0next, n);
|
|
||||||
if (n > usbp->usb_ep0max)
|
|
||||||
break;
|
|
||||||
usbp->usb_ep0max -= size;
|
|
||||||
usbp->usb_ep0n -= n;
|
|
||||||
usbp->usb_ep0next += n;
|
|
||||||
if (usbp->usb_ep0max == 0) {
|
|
||||||
usb_lld_write(usbp, 0, NULL, 0);
|
|
||||||
usbp->usb_ep0state = USB_EP0_SENDING_STS;
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
case USB_EP0_WAITING_STS:
|
case USB_EP0_WAITING_STS:
|
||||||
/* STATUS received packet handling, it must be zero sized.*/
|
/* STATUS received packet handling, it must be zero sized.*/
|
||||||
n = usb_lld_read(usbp, 0, buf, 1);
|
n = usbp->ep[0]->rxsize;
|
||||||
if (n != 0)
|
if (n != 0)
|
||||||
break;
|
break;
|
||||||
if (usbp->usb_ep0endcb)
|
usbp->ep0state = USB_EP0_WAITING_SETUP;
|
||||||
usbp->usb_ep0endcb(usbp);
|
|
||||||
usbp->usb_ep0state = USB_EP0_WAITING_SETUP;
|
|
||||||
return;
|
return;
|
||||||
default:
|
default:
|
||||||
;
|
;
|
||||||
|
@ -540,9 +573,9 @@ void _usb_ep0out(USBDriver *usbp, usbep_t ep) {
|
||||||
/* Error response.*/
|
/* Error response.*/
|
||||||
usb_lld_stall_in(usbp, 0);
|
usb_lld_stall_in(usbp, 0);
|
||||||
usb_lld_stall_out(usbp, 0);
|
usb_lld_stall_out(usbp, 0);
|
||||||
if (usbp->usb_config->uc_event_cb)
|
if (usbp->config->event_cb)
|
||||||
usbp->usb_config->uc_event_cb(usbp, USB_EVENT_STALLED);
|
usbp->config->event_cb(usbp, USB_EVENT_STALLED);
|
||||||
usbp->usb_ep0state = USB_EP0_WAITING_SETUP;
|
usbp->ep0state = USB_EP0_WAITING_SETUP;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* HAL_USE_USB */
|
#endif /* HAL_USE_USB */
|
||||||
|
|
|
@ -93,7 +93,7 @@
|
||||||
* @brief Enables the SERIAL over USB subsystem.
|
* @brief Enables the SERIAL over USB subsystem.
|
||||||
*/
|
*/
|
||||||
#if !defined(HAL_USE_SERIAL_USB) || defined(__DOXYGEN__)
|
#if !defined(HAL_USE_SERIAL_USB) || defined(__DOXYGEN__)
|
||||||
#define HAL_USE_SERIAL_USB TRUE
|
#define HAL_USE_SERIAL_USB FALSE
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -32,7 +32,7 @@
|
||||||
/*
|
/*
|
||||||
* USB driver structure.
|
* USB driver structure.
|
||||||
*/
|
*/
|
||||||
static SerialUSBDriver SDU1;
|
//static SerialUSBDriver SDU1;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* USB Device Descriptor.
|
* USB Device Descriptor.
|
||||||
|
@ -247,6 +247,24 @@ USBEndpointState ep2state;
|
||||||
*/
|
*/
|
||||||
USBEndpointState ep3state;
|
USBEndpointState ep3state;
|
||||||
|
|
||||||
|
void sduDataRequest(USBDriver *usbp, usbep_t ep) {
|
||||||
|
|
||||||
|
(void)usbp;
|
||||||
|
(void)ep;
|
||||||
|
}
|
||||||
|
|
||||||
|
void sduInterruptRequest(USBDriver *usbp, usbep_t ep) {
|
||||||
|
|
||||||
|
(void)usbp;
|
||||||
|
(void)ep;
|
||||||
|
}
|
||||||
|
|
||||||
|
void sduDataAvailable(USBDriver *usbp, usbep_t ep) {
|
||||||
|
|
||||||
|
(void)usbp;
|
||||||
|
(void)ep;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief EP1 initialization structure (IN only).
|
* @brief EP1 initialization structure (IN only).
|
||||||
*/
|
*/
|
||||||
|
@ -317,6 +335,7 @@ static void usb_event(USBDriver *usbp, usbevent_t event) {
|
||||||
/*
|
/*
|
||||||
* Serial over USB driver configuration.
|
* Serial over USB driver configuration.
|
||||||
*/
|
*/
|
||||||
|
#if 0
|
||||||
static const SerialUSBConfig serusbcfg = {
|
static const SerialUSBConfig serusbcfg = {
|
||||||
&USBD1,
|
&USBD1,
|
||||||
{
|
{
|
||||||
|
@ -329,6 +348,40 @@ static const SerialUSBConfig serusbcfg = {
|
||||||
DATA_AVAILABLE_EP,
|
DATA_AVAILABLE_EP,
|
||||||
INTERRUPT_REQUEST_EP
|
INTERRUPT_REQUEST_EP
|
||||||
};
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "usb_cdc.h"
|
||||||
|
static cdc_linecoding_t linecoding = {
|
||||||
|
{0x00, 0x96, 0x00, 0x00}, /* 38400. */
|
||||||
|
LC_STOP_1, LC_PARITY_NONE, 8
|
||||||
|
};
|
||||||
|
bool_t sduRequestsHook(USBDriver *usbp) {
|
||||||
|
|
||||||
|
if ((usbp->setup[0] & USB_RTYPE_TYPE_MASK) == USB_RTYPE_TYPE_CLASS) {
|
||||||
|
switch (usbp->setup[1]) {
|
||||||
|
case CDC_GET_LINE_CODING:
|
||||||
|
usbSetupTransfer(usbp, (uint8_t *)&linecoding, sizeof(linecoding));
|
||||||
|
return TRUE;
|
||||||
|
case CDC_SET_LINE_CODING:
|
||||||
|
usbSetupTransfer(usbp, (uint8_t *)&linecoding, sizeof(linecoding));
|
||||||
|
return TRUE;
|
||||||
|
case CDC_SET_CONTROL_LINE_STATE:
|
||||||
|
/* Nothing to do, there are no control lines.*/
|
||||||
|
usbSetupTransfer(usbp, NULL, 0);
|
||||||
|
return TRUE;
|
||||||
|
default:
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
USBConfig usbconfig = {
|
||||||
|
usb_event,
|
||||||
|
get_descriptor,
|
||||||
|
sduRequestsHook,
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
|
||||||
/*===========================================================================*/
|
/*===========================================================================*/
|
||||||
/* Generic code. */
|
/* Generic code. */
|
||||||
|
@ -352,6 +405,7 @@ static msg_t Thread1(void *arg) {
|
||||||
/*
|
/*
|
||||||
* USB CDC loopback thread.
|
* USB CDC loopback thread.
|
||||||
*/
|
*/
|
||||||
|
#if 0
|
||||||
static WORKING_AREA(waThread2, 256);
|
static WORKING_AREA(waThread2, 256);
|
||||||
static msg_t Thread2(void *arg) {
|
static msg_t Thread2(void *arg) {
|
||||||
SerialUSBDriver *sdup = arg;
|
SerialUSBDriver *sdup = arg;
|
||||||
|
@ -368,6 +422,7 @@ static msg_t Thread2(void *arg) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Application entry point.
|
* Application entry point.
|
||||||
|
@ -385,11 +440,12 @@ int main(void) {
|
||||||
chSysInit();
|
chSysInit();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Activates the USB bus and then the USB driver.
|
* Activates the USB driver and then the USB bus pull-up on D+.
|
||||||
*/
|
*/
|
||||||
|
usbStart(&USBD1, &usbconfig);
|
||||||
palClearPad(GPIOC, GPIOC_USB_DISC);
|
palClearPad(GPIOC, GPIOC_USB_DISC);
|
||||||
sduObjectInit(&SDU1);
|
// sduObjectInit(&SDU1);
|
||||||
sduStart(&SDU1, &serusbcfg);
|
// sduStart(&SDU1, &serusbcfg);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Activates the serial driver 2 using the driver default configuration.
|
* Activates the serial driver 2 using the driver default configuration.
|
||||||
|
@ -404,7 +460,7 @@ int main(void) {
|
||||||
/*
|
/*
|
||||||
* Creates the USB CDC loopback thread.
|
* Creates the USB CDC loopback thread.
|
||||||
*/
|
*/
|
||||||
chThdCreateStatic(waThread2, sizeof(waThread2), NORMALPRIO, Thread2, &SDU1);
|
// chThdCreateStatic(waThread2, sizeof(waThread2), NORMALPRIO, Thread2, &SDU1);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Normal main() thread activity, in this demo it does nothing except
|
* Normal main() thread activity, in this demo it does nothing except
|
||||||
|
|
Loading…
Reference in New Issue