Problem using USB-Serial on STM32F407

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
LHelge
Posts: 15
Joined: Sun Sep 21, 2014 10:58 pm
Been thanked: 1 time

Problem using USB-Serial on STM32F407

Post by LHelge »

I have some problems getting the USB serial functions to work on my STM32F4Discovery board. I've made my own project based mostly on the examples included in ChibiOS, but I cannot get the receive parts working. The USB code are copied straight from the STM32F4xx-USB_CDC example with the minor change from OTG2 to OTG1 to suit the STM32F4 discovery board.

I've made a, from what I can see, fairly straight forward echo program.

in my usbserial.c file i have these functions:

Code: Select all

static WORKING_AREA(waFlashThread, 128);
static msg_t flashThread(void *arg) {

    (void)arg;
    chRegSetThreadName("blinker");

    while (TRUE) {
        if(isUsbSerialActive()) {
            chThdSleepMilliseconds(250);
        }
        else {
            chThdSleepMilliseconds(500);
        }

        palTogglePad(GPIOD, GPIOD_LED3);
    }
    return 0;
}

void initUsbSerial(void) {
      sduObjectInit(&SDU1);
      sduStart(&SDU1, &serusbcfg);

      usbDisconnectBus(serusbcfg.usbp);
      chThdSleepMilliseconds(1500);
      usbStart(serusbcfg.usbp, &usbcfg);
      usbConnectBus(serusbcfg.usbp);

      chThdCreateStatic(waFlashThread, sizeof(waFlashThread), NORMALPRIO-1, flashThread, NULL);
}

bool isUsbSerialActive(void) {
    return (SDU1.config->usbp->state == USB_ACTIVE);
}

void writeUsbSerial(uint8_t *buf, uint32_t len) {
    if(isUsbSerialActive()) {
        chSequentialStreamWrite(&SDU1, buf, len);
    }
}

uint32_t readUsbSerial(uint8_t *buf, uint32_t len) {
    if(isUsbSerialActive()) {
        return chSequentialStreamRead(&SDU1, buf, len);
    }

    return 0;
}


And in my main.c i have these

Code: Select all

int main(void) {
  uint8_t buf[128];
  uint32_t n;

  halInit();
  chSysInit();

  initUsbSerial();

  while (TRUE) {
      if (isUsbSerialActive()) {
        n = readUsbSerial(buf, 128);
        writeUsbSerial(buf, n);
      }
      if(n > 0) {
        palSetPad(GPIOD, GPIOD_LED4);
      }
      n = 0;
      chThdSleepMilliseconds(500);
      palClearPad(GPIOD, GPIOD_LED4);
    }
}


The only problem is that I don't get anything back, and LED4 stays dark. I've verified that the connection is good, the USB device gets enumerated, LED3 starts flashing quicker, and the send function works fine. If I just set it to send a constant sting the message is received by my terminal. What have I done wrong?
wizhippo
Posts: 18
Joined: Thu Oct 31, 2013 3:41 pm

Re: Problem using USB-Serial on STM32F407

Post by wizhippo »

I have success using the demo specific for that board. Take a look at ARMCM4-STM32F407-DISCOVERY-MEMS demo project.

I can't tell from your description how you have set up your project, but the basing your project off this demo project should almost guarantee you success with that board.
LHelge
Posts: 15
Joined: Sun Sep 21, 2014 10:58 pm
Been thanked: 1 time

Re: Problem using USB-Serial on STM32F407

Post by LHelge »

Fore some reason chSequentioalStreamGet() works but not chSequentialStreamRead(). Can't say why but it'll do it for now.
Post Reply