aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--demos/ARMCM4-STM32F407-LWIP-FATFS-USB/main.c2
-rw-r--r--os/hal/platforms/STM32/USBv1/usb_lld.c12
-rw-r--r--os/hal/platforms/STM32/USBv1/usb_lld.h5
-rw-r--r--testhal/STM32F1xx/USB_CDC/main.c118
-rw-r--r--testhal/STM32F4xx/USB_CDC/main.c2
5 files changed, 90 insertions, 49 deletions
diff --git a/demos/ARMCM4-STM32F407-LWIP-FATFS-USB/main.c b/demos/ARMCM4-STM32F407-LWIP-FATFS-USB/main.c
index 0c6079516..e22710174 100644
--- a/demos/ARMCM4-STM32F407-LWIP-FATFS-USB/main.c
+++ b/demos/ARMCM4-STM32F407-LWIP-FATFS-USB/main.c
@@ -156,7 +156,7 @@ static FRESULT scan_files(BaseSequentialStream *chp, char *path) {
/*===========================================================================*/
/*
- * USB Driver structure.
+ * Serial over USB Driver structure.
*/
static SerialUSBDriver SDU1;
diff --git a/os/hal/platforms/STM32/USBv1/usb_lld.c b/os/hal/platforms/STM32/USBv1/usb_lld.c
index 0b72c3a56..1e35336df 100644
--- a/os/hal/platforms/STM32/USBv1/usb_lld.c
+++ b/os/hal/platforms/STM32/USBv1/usb_lld.c
@@ -81,6 +81,7 @@ static const USBEndpointConfig ep0config = {
0x40,
&ep0_state.in,
&ep0_state.out,
+ 1,
ep0setup_buffer
};
@@ -347,7 +348,6 @@ CH_IRQ_HANDLER(STM32_USB1_LP_HANDLER) {
EPR_CLEAR_CTR_TX(ep);
n = (size_t)USB_GET_DESCRIPTOR(ep)->TXCOUNT0;
- epcp->in_state->mode.linear.txbuf += n;
epcp->in_state->txcnt += n;
epcp->in_state->txsize -= n;
if (epcp->in_state->txsize > 0) {
@@ -361,10 +361,12 @@ CH_IRQ_HANDLER(STM32_USB1_LP_HANDLER) {
usb_packet_write_from_queue(USB_GET_DESCRIPTOR(ep),
epcp->in_state->mode.queue.txqueue,
n);
- else
+ else {
+ epcp->in_state->mode.linear.txbuf += n;
usb_packet_write_from_buffer(USB_GET_DESCRIPTOR(ep),
epcp->in_state->mode.linear.txbuf,
n);
+ }
chSysLockFromIsr();
usb_lld_start_in(usbp, ep);
chSysUnlockFromIsr();
@@ -391,13 +393,13 @@ CH_IRQ_HANDLER(STM32_USB1_LP_HANDLER) {
usb_packet_read_to_queue(udp,
epcp->out_state->mode.queue.rxqueue,
n);
- else
+ else {
usb_packet_read_to_buffer(udp,
epcp->out_state->mode.linear.rxbuf,
n);
-
+ epcp->out_state->mode.linear.rxbuf += n;
+ }
/* Transaction data updated.*/
- epcp->out_state->mode.linear.rxbuf += n;
epcp->out_state->rxcnt += n;
epcp->out_state->rxsize -= n;
epcp->out_state->rxpkts -= 1;
diff --git a/os/hal/platforms/STM32/USBv1/usb_lld.h b/os/hal/platforms/STM32/USBv1/usb_lld.h
index 7e7253abd..bce83681a 100644
--- a/os/hal/platforms/STM32/USBv1/usb_lld.h
+++ b/os/hal/platforms/STM32/USBv1/usb_lld.h
@@ -237,6 +237,11 @@ typedef struct {
USBOutEndpointState *out_state;
/* End of the mandatory fields.*/
/**
+ * @brief Reserved field, not currently used.
+ * @note Initialize this field to 1 in order to be forward compatible.
+ */
+ uint16_t ep_buffers;
+ /**
* @brief Pointer to a buffer for setup packets.
* @details Setup packets require a dedicated 8-bytes buffer, set this
* field to @p NULL for non-control endpoints.
diff --git a/testhal/STM32F1xx/USB_CDC/main.c b/testhal/STM32F1xx/USB_CDC/main.c
index b1af1b1bd..9ba972bb3 100644
--- a/testhal/STM32F1xx/USB_CDC/main.c
+++ b/testhal/STM32F1xx/USB_CDC/main.c
@@ -34,7 +34,7 @@
/*===========================================================================*/
/*
- * USB Driver structure.
+ * Serial over USB Driver structure.
*/
static SerialUSBDriver SDU1;
@@ -231,24 +231,30 @@ static const USBDescriptor *get_descriptor(USBDriver *usbp,
static USBInEndpointState ep1instate;
/**
- * @brief EP1 initialization structure (IN only).
+ * @brief OUT EP1 state.
+ */
+static USBOutEndpointState ep1outstate;
+
+/**
+ * @brief EP1 initialization structure (both IN and OUT).
*/
static const USBEndpointConfig ep1config = {
USB_EP_MODE_TYPE_BULK,
NULL,
sduDataTransmitted,
- NULL,
+ sduDataReceived,
+ 0x0040,
0x0040,
- 0x0000,
&ep1instate,
- NULL,
+ &ep1outstate,
+ 1,
NULL
};
/**
- * @brief OUT EP2 state.
+ * @brief IN EP2 state.
*/
-USBOutEndpointState ep2outstate;
+static USBInEndpointState ep2instate;
/**
* @brief EP2 initialization structure (IN only).
@@ -260,28 +266,9 @@ static const USBEndpointConfig ep2config = {
NULL,
0x0010,
0x0000,
+ &ep2instate,
NULL,
- &ep2outstate,
- NULL
-};
-
-/**
- * @brief OUT EP3 state.
- */
-USBOutEndpointState ep3outstate;
-
-/**
- * @brief EP3 initialization structure (OUT only).
- */
-static const USBEndpointConfig ep3config = {
- USB_EP_MODE_TYPE_BULK,
- NULL,
- NULL,
- sduDataReceived,
- 0x0000,
- 0x0040,
- NULL,
- &ep3outstate,
+ 1,
NULL
};
@@ -296,13 +283,17 @@ static void usb_event(USBDriver *usbp, usbevent_t event) {
case USB_EVENT_ADDRESS:
return;
case USB_EVENT_CONFIGURED:
+ chSysLockFromIsr();
+
/* Enables the endpoints specified into the configuration.
Note, this callback is invoked from an ISR so I-Class functions
must be used.*/
- chSysLockFromIsr();
usbInitEndpointI(usbp, USB_CDC_DATA_REQUEST_EP, &ep1config);
usbInitEndpointI(usbp, USB_CDC_INTERRUPT_REQUEST_EP, &ep2config);
- usbInitEndpointI(usbp, USB_CDC_DATA_AVAILABLE_EP, &ep3config);
+
+ /* Resetting the state of the CDC subsystem.*/
+ sduConfigureHookI(usbp);
+
chSysUnlockFromIsr();
return;
case USB_EVENT_SUSPEND:
@@ -316,16 +307,20 @@ static void usb_event(USBDriver *usbp, usbevent_t event) {
}
/*
+ * USB driver configuration.
+ */
+static const USBConfig usbcfg = {
+ usb_event,
+ get_descriptor,
+ sduRequestsHook,
+ NULL
+};
+
+/*
* Serial over USB driver configuration.
*/
static const SerialUSBConfig serusbcfg = {
- &USBD1,
- {
- usb_event,
- get_descriptor,
- sduRequestsHook,
- NULL
- }
+ &USBD1
};
/*===========================================================================*/
@@ -386,10 +381,42 @@ static void cmd_test(BaseSequentialStream *chp, int argc, char *argv[]) {
chThdWait(tp);
}
+static void cmd_write(BaseSequentialStream *chp, int argc, char *argv[]) {
+ static uint8_t buf[] =
+ "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
+ "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
+ "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
+ "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
+ "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
+ "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
+ "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
+ "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
+ "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
+ "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
+ "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
+ "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
+ "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
+ "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
+ "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
+ "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
+
+ (void)argv;
+ if (argc > 0) {
+ chprintf(chp, "Usage: write\r\n");
+ return;
+ }
+
+ while (chnGetTimeout((BaseChannel *)chp, TIME_IMMEDIATE) == Q_TIMEOUT) {
+ chSequentialStreamWrite(&SDU1, buf, sizeof buf - 1);
+ }
+ chprintf(chp, "\r\n\nstopped\r\n");
+}
+
static const ShellCommand commands[] = {
{"mem", cmd_mem},
{"threads", cmd_threads},
{"test", cmd_test},
+ {"write", cmd_write},
{NULL, NULL}
};
@@ -411,10 +438,11 @@ static msg_t Thread1(void *arg) {
(void)arg;
chRegSetThreadName("blinker");
while (TRUE) {
+ systime_t time = serusbcfg.usbp->state == USB_ACTIVE ? 250 : 500;
palClearPad(IOPORT3, GPIOC_LED);
- chThdSleepMilliseconds(500);
+ chThdSleepMilliseconds(time);
palSetPad(IOPORT3, GPIOC_LED);
- chThdSleepMilliseconds(500);
+ chThdSleepMilliseconds(time);
}
}
@@ -435,14 +463,20 @@ int main(void) {
chSysInit();
/*
+ * Initializes a serial-over-USB CDC driver.
+ */
+ sduObjectInit(&SDU1);
+ sduStart(&SDU1, &serusbcfg);
+
+ /*
* Activates the USB driver and then the USB bus pull-up on D+.
+ * Note, a delay is inserted in order to not have to disconnect the cable
+ * after a reset.
*/
usbDisconnectBus(serusbcfg.usbp);
chThdSleepMilliseconds(1000);
- sduObjectInit(&SDU1);
- sduStart(&SDU1, &serusbcfg);
+ usbStart(serusbcfg.usbp, &usbcfg);
usbConnectBus(serusbcfg.usbp);
- palClearPad(GPIOC, GPIOC_USB_DISC);
/*
* Shell manager initialization.
diff --git a/testhal/STM32F4xx/USB_CDC/main.c b/testhal/STM32F4xx/USB_CDC/main.c
index 49a31a674..5503884ff 100644
--- a/testhal/STM32F4xx/USB_CDC/main.c
+++ b/testhal/STM32F4xx/USB_CDC/main.c
@@ -34,7 +34,7 @@
/*===========================================================================*/
/*
- * USB Driver structure.
+ * Serial over USB Driver structure.
*/
static SerialUSBDriver SDU1;