Page 1 of 1

Serial over USB Transmit Problem, STM32F4

Posted: Thu Oct 23, 2014 2:42 am
by hkerner
Hi there,

I am trying to transmit data using the HAL Serial over USB Driver on an STM32F4. I am watching the value of sdup->config->usbp->state in gdb and have found that the application is returning at line 158 of serial_usb.c as shown below because sdup->config->usbp->state is USB_READY not USB_ACTIVE as it should be (I did check that sdup->state == SDU_READY so that part is fine).

Code: Select all

  
/* If the USB driver is not in the appropriate state then transactions
     must not be started.*/
  if ((usbGetDriverStateI(sdup->config->usbp) != USB_ACTIVE) ||
      (sdup->state != SDU_READY))
    return;


Here is my usb setup function -- is there something I should be doing to make the state USB_ACTIVE rather than USB_READY after these steps? I pulled this and the configuration structures (edited for my board obviously) straight from the testhal example.

Code: Select all

void usb_setup(void) {
  /*
   * 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);
  usbStart(serusbcfg.usbp, &usbcfg);
  usbConnectBus(serusbcfg.usbp);
}


Thank you in advance!

Re: Serial over USB Transmit Problem, STM32F4

Posted: Thu Oct 23, 2014 2:55 am
by mobyfab
You need to wait for the USB to be active.

This is what I use in my usb threads before my while loop:
while(USBD1.state != USB_READY) chThdSleepMilliseconds(10);
while(SDU1.state != SDU_READY) chThdSleepMilliseconds(10); // (sduStart is in the main thread)

Re: Serial over USB Transmit Problem, STM32F4

Posted: Thu Oct 23, 2014 3:29 am
by hkerner
Your code waits for the USB to be ready, not active, though - right? At the point in the code where it's returning, the states are USB_READY and SDU_READY when they should be USB_ACTIVE and SDU_READY.

Re: Serial over USB Transmit Problem, STM32F4

Posted: Thu Oct 23, 2014 8:23 pm
by mobyfab
Yes, my bad :D

It's actualy how it's supposed to work.
This code is in the notify functions, which are not supposed to work if the usb is inactive. (active = enumerated on the host)

Does your usb enumerates properly? You can you transmit any data?

Re: Serial over USB Transmit Problem, STM32F4

Posted: Thu Oct 23, 2014 9:51 pm
by hkerner
The only code for USB before I attempt the chnWriteTimeout() is in that setup function I've posted here. I'm not able to transmit data because the usbp state is never changed from USB_READY to USB_ACTIVE. I'm wondering if I'm missing something that I should be doing after the usb_setup functions and before calling the usb_write function to bring the state to USB_ACTIVE before calling chnWriteTimeout()?

Re: Serial over USB Transmit Problem, STM32F4

Posted: Thu Oct 23, 2014 9:58 pm
by Giovanni
It all depends from the host, if the enumeration is done then you should see a new virtual USB serial port on your PC. BTW, you need a driver for windows or the enumeration is not completed.

Assuming the driver is your problem, you could install this: http://www.st.com/web/en/catalog/tools/ ... pe=keyword

If you start writing before the USB goes in the active state nothing bad happens, the data is queued but not transmitted until it becomes active.

Giovanni