STM32F103 LibUsb

This forum is dedicated to feedback, discussions about ongoing or future developments, ideas and suggestions regarding the ChibiOS projects are welcome. This forum is NOT for support.
goahunter
Posts: 29
Joined: Thu Aug 16, 2012 10:59 pm

STM32F103 LibUsb

Post by goahunter »

Hey all,

So I'm using an STM32F103 for a data acquisition project that details the use of USB support (cannot use Serial over USB). I've noticed that chibios has two drivers for USB and Serial USB as well as an example for Serial USB. Are there any examples for USB-OTG? I have been following the thread for the STM32F4's, is it possible to use this code with the stm32f1's?
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: STM32F103 LibUsb

Post by Giovanni »

Hi,

There is a specific driver for the F103 and it can be used with or without serial_usb.

I don't have an example at hand not using serial_usb however the API is the same of the F4 driver so any code working for one will work for the other one too, they are meant to be interchangeable.

Note, there is a small fix pending for the F103 driver, it will be probably committed tomorrow.

Giovanni
Mathias
Posts: 43
Joined: Thu Jun 28, 2012 6:19 pm

Re: STM32F103 LibUsb

Post by Mathias »

I'd say copy the testhal/STM32F1xx/USB_CDC Folder, replace the main.c with the one from the F4 Libusb thread and give it a try. Apart from commenting the "palTogglePad"s because of the references to the LEDs it compiles fine, I don't have hardware to test though.
I'm not sure about the latest changes to the F4 USB driver, maybe Giovanni can comment on what the F1 driver does when an EP multiplier is set? (Warnings are thrown, so I assume the structure is different, but it should be an easy change).
goahunter
Posts: 29
Joined: Thu Aug 16, 2012 10:59 pm

Re: STM32F103 LibUsb

Post by goahunter »

Mathias, So far I've used your main.c and and most everything is lining up but, im having trouble with the out endpoint.

The first error thrown is at:

Code: Select all

static const USBEndpointConfig ep1config = {
  USB_EP_MODE_TYPE_BULK,    //Type and mode of the endpoint
  NULL,                     //Setup packet notification callback (NULL for non-control EPs)
  dataTransmitted,          //IN endpoint notification callback
  NULL,                     //OUT endpoint notification callback
  IN_PACKETSIZE,            //IN endpoint maximum packet size
  0x0000,                   //OUT endpoint maximum packet size
  &ep1instate,              //USBEndpointState associated to the IN endpoint
  NULL,                     //USBEndpointState associated to the OUTendpoint
  IN_MULT,
  NULL                      //Pointer to a buffer for setup packets (NULL for non-control EPs)
};


I have to remove the following to get epconfig1 to line up with the parameters for the 103:

Code: Select all

  IN_MULT,
  NULL                      //Pointer to a buffer for setup packets (NULL for non-control EPs)


Next I have to do the same thing for ep2config:

Code: Select all

static const USBEndpointConfig ep2config = {
  USB_EP_MODE_TYPE_BULK,
  NULL,
  NULL,
  dataReceived,
  0x0000,
  OUT_PACKETSIZE,
  NULL,
  &ep2outstate
};


Finally I get to a problem that I'm not quite sure what it means and maybe you all might be able to help me:

When the compiler gets to

Code: Select all

   USBOutEndpointState *osp = usbp->epc[ep]->out_state;

I get the following error:
..\main.c(59): error: #268: declaration may not appear after executable statement in block
Any ideas as to what this means? Please forgive if this is a stupid question or I am doing something horribly wrong as I am still learning chibios and the stm32f103.

The section of code above comes from:

Code: Select all

dataReceived(USBDriver *usbp, usbep_t ep){
    (void) usbp;
    (void) ep;
    if(!usbStatus) return;
    //osp == ep2outstate
    USBOutEndpointState *osp = usbp->epc[ep]->out_state;
    if(osp->rxcnt){
        switch(receiveBuf[0]){
            case '1':
//                palTogglePad(GPIOD, GPIOD_LED3);
                break;
            case '2':
//                palTogglePad(GPIOD, GPIOD_LED4);
                break;
            case '3':
//                palTogglePad(GPIOD, GPIOD_LED5);
                break;
            case '4':
//                palTogglePad(GPIOD, GPIOD_LED6);
                break;

        }
    }

    /*
     * Initiate next receive
     */
    usbPrepareReceive(usbp, EP_OUT, receiveBuf, 64);

    chSysLockFromIsr();
    usbStartReceiveI(usbp, EP_OUT);
    chSysUnlockFromIsr();
}
Mathias
Posts: 43
Joined: Thu Jun 28, 2012 6:19 pm

Re: STM32F103 LibUsb

Post by Mathias »

The first two problems youre encountering have to do with differences in the HAL. Giovanni just recently introduced changes to the F4 USB driver that improved the speed quite a lot. Those two fields are hardware dependent, I don't know about the second, but the Multiplier is probably F4 specific at the moment. I assume Giovanni will add it to the other drivers later.

I didn't know about the last error message, so I looked it up. It means that a variable got declared after there were some executable statements. Changing

Code: Select all

dataReceived(USBDriver *usbp, usbep_t ep){
    (void) usbp;
    (void) ep;
    if(!usbStatus) return;
    //osp == ep2outstate
    USBOutEndpointState *osp = usbp->epc[ep]->out_state;
    if(osp->rxcnt){

into

Code: Select all

dataReceived(USBDriver *usbp, usbep_t ep){
    (void) usbp;
    (void) ep;
    USBOutEndpointState *osp = usbp->epc[ep]->out_state;
    //osp == ep2outstate
    if(!usbStatus) return;
    if(osp->rxcnt){

should fix the problem. I will commit the fix in the next push.
goahunter
Posts: 29
Joined: Thu Aug 16, 2012 10:59 pm

Re: STM32F103 LibUsb

Post by goahunter »

Made the change, but still same error at the same line.
goahunter
Posts: 29
Joined: Thu Aug 16, 2012 10:59 pm

Re: STM32F103 LibUsb

Post by goahunter »

I don't know if this makes sense but i somehow got it to compile bychanging this:

Code: Select all

void dataReceived(USBDriver *usbp, usbep_t ep){
    (void) usbp;
    (void) ep;
    USBOutEndpointState *osp = usbp->epc[ep]->out_state;
    //osp == ep2outstate
    if(!usbStatus) return;
    if(osp->rxcnt){


to

Code: Select all

void dataReceived(USBDriver *usbp, usbep_t ep){
    USBOutEndpointState *osp = usbp->epc[ep]->out_state;
    (void) usbp;
    (void) ep;
    //osp == ep2outstate
    if(!usbStatus) return;
    if(osp->rxcnt){
Mathias
Posts: 43
Joined: Thu Jun 28, 2012 6:19 pm

Re: STM32F103 LibUsb

Post by Mathias »

It seems to make sense. "(void) usbp;" isn't a variable declaration, so the variable declaration has to be moved before that.
Stupid me. (Perhaps I should increase my compiler warning levels?)
*edit* I looked it up, that error only occurs with C90. Since even the // comments are not C90 compliant, I don't intend to change to C90 and fix all of those warnings. I wonder which standard your compiler supports...
goahunter
Posts: 29
Joined: Thu Aug 16, 2012 10:59 pm

Re: STM32F103 LibUsb

Post by goahunter »

It might help. Not sure why I got the error and yours didn't. I do have the USB starting to cooperate but still an error showing up. When I plus in my device it shows up as a Chi BIOS/custom device and searches windows update for a driver bit does not seem to fully enumerate. Any ideas on something different between the stm32f1 and the stm32f4
goahunter
Posts: 29
Joined: Thu Aug 16, 2012 10:59 pm

Re: STM32F103 LibUsb

Post by goahunter »

So device is recognized only after flashing and plugging in. Device manager shows up CHibios/Custom Hardware but shows a device error code #10 device could not start. I know the hardware works with USB as I've tested it with other USB Code and it works. Any suggestions would be appreciated. If I find anything I'll post it.
Post Reply