usb_serial stops working after reset

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

User avatar
part1zan_
Posts: 44
Joined: Sun Sep 08, 2013 9:04 pm

usb_serial stops working after reset

Post by part1zan_ »

What I do is a small demo app to play with on an STM32F3Discovery board. It utilizes the ChibiOS console. What is wrong is that when I reset the device (to check something, for instance), I cannot connect to the USB console. The device (/dev/ttyACM0) appears in the system, but unfortunately is inaccessible. The code that invokes the shell looks like the code below:

Code: Select all

#include "ch.h"
#include "hal.h"
#include "test.h"
#include "spi.h"
#include "i2c.h"
#include "usbcfg.h"
#include "chprintf.h"
#include "shell.h"
#include "chthreads.h"
// testing rtc
#include "rtc.h"
#include "chrtclib.h"

#define usb_lld_connect_bus(usbp)
#define usb_lld_disconnect_bus(usbp)

/* Virtual serial port over USB.*/
SerialUSBDriver SDU1;
/*
blah, blah, blah, some functions
*/
#define SHELL_WA_SIZE   THD_WA_SIZE(1024)
/*
blah, blah, blah, funcitons with shell commands
*/
static const ShellCommand shCmds[] = {
  {"test",      cmd_test},
  {"helpme",   cmd_help},
  {"gyrodata",   cmd_gyrodata},
  {"magdata", cmd_magdata},
  {"adjust", cmd_adjust},
  {"nextsch", cmd_nextsch},
  {"time", cmd_time},
  {"free", cmd_mem},
  {"reboot", cmd_reboot},
  {NULL, NULL}
};

static const ShellConfig shCfg = {
  (BaseSequentialStream *)&SDU1,
  shCmds
};
/*
blah, blah, blah, some other irrelevant code
*/
int main(void) {
   Thread *sh = NULL;

    halInit();
    chSysInit();
   sduObjectInit(&SDU1);
   sduStart(&SDU1, &serusbcfg);

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


    spiStart(&SPID1, &spi1cfg);
    i2cStart(&I2CD1, &i2cconfig);
    initGyro();
    initAccel();
    initMag();
   shellInit();
   chThdCreateStatic(waThreadBlink, sizeof(waThreadBlink), NORMALPRIO, ThreadBlink, NULL);

    while (TRUE) {
      if (!sh) {
         sh = shellCreate(&shCfg, SHELL_WA_SIZE, NORMALPRIO);
      }
      else if (chThdTerminated(sh)) {
         chThdRelease(sh);
         sh = NULL;
      }
      chThdSleepMilliseconds(1000);
    }
   return 0; // never returns, lol
}

Somewhy, I am pretty sure that the bug is in my code. Please tell me where it might be. Also, this is unlikely to be an STM32-specific issue.
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: usb_serial stops working after reset

Post by Tectu »

What exactly do you mean by inaccessible? Probably you really just have a permissions problem on your machine. I didn't check your code but if the device shows up, you should be able to build up a connection to it. Does the LED keep blinking?


~ Tectu
User avatar
part1zan_
Posts: 44
Joined: Sun Sep 08, 2013 9:04 pm

Re: usb_serial stops working after reset

Post by part1zan_ »

Yes, the LED keeps blinking, the device's permissions are ok:

Code: Select all

$ ls -l /dev/ttyA*
crw-rw---- 1 root dialout 166, 0 окт.  17 22:39 /dev/ttyACM0
User avatar
part1zan_
Posts: 44
Joined: Sun Sep 08, 2013 9:04 pm

Re: usb_serial stops working after reset

Post by part1zan_ »

Tried using screen for connecting to the device, and screen said:

Code: Select all

Cannot open line '/dev/ttyACM0' for R/W: Input/output error  
And then:

Code: Select all

Sorry, could not find a PTY.  
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: usb_serial stops working after reset

Post by Tectu »

Did you actually use root access to talk to the device? otherwise make sure that your user is in the allowed groups.
Personally, I use minicom with which I never had any issues at all.


~ Tectu
User avatar
part1zan_
Posts: 44
Joined: Sun Sep 08, 2013 9:04 pm

Re: usb_serial stops working after reset

Post by part1zan_ »

I tried root user, my user is in dialout group (which has rw permissions for the device -- see above). I also mostly use minicom, because it's reliable and really convenient
User avatar
part1zan_
Posts: 44
Joined: Sun Sep 08, 2013 9:04 pm

Re: usb_serial stops working after reset

Post by part1zan_ »

Let me paraphrase my question a little bit. My main subroutine (which I somewhy find the buggiest place in the code listed above) looks like this:

Code: Select all

int main(void) {
   Thread *sh = NULL;

    halInit();
    chSysInit();
   sduObjectInit(&SDU1);
   sduStart(&SDU1, &serusbcfg);

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


    spiStart(&SPID1, &spi1cfg);
    i2cStart(&I2CD1, &i2cconfig);
    initGyro();
    initAccel();
    initMag();
   shellInit();
//   palSetPadMode(GPIOA, 9, PAL_MODE_ALTERNATE(7));
//   palSetPadMode(GPIOA, 10, PAL_MODE_ALTERNATE(7));
   chThdCreateStatic(waThreadBlink, sizeof(waThreadBlink), NORMALPRIO, ThreadBlink, NULL);

    while (TRUE) {
      if (!sh) {
         sh = shellCreate(&shCfg, SHELL_WA_SIZE, NORMALPRIO);
      }
      else if (chThdTerminated(sh)) {
         chThdRelease(sh);
         sh = NULL;
      }
      chThdSleepMilliseconds(1000);
      
    }
   return 0; // never returns, lol
}

Can the above code cause disconnections and shell malfunctions after reboot? Could I forget to state something above the main subroutine?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: usb_serial stops working after reset

Post by Giovanni »

You should first start the USB then SDU, please follow the examples.

Giovanni
User avatar
part1zan_
Posts: 44
Joined: Sun Sep 08, 2013 9:04 pm

Re: usb_serial stops working after reset

Post by part1zan_ »

Sorry to say that, but starting SDU before USB didn't help. Could you tell me which exact examples (with links) I should look at?

Could this be a hardware problem?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: usb_serial stops working after reset

Post by Giovanni »

The STM32F3-Discovery is unable to control the USB pull-up resistor that allows to detect the device, it is always ON. After restarting an application or resetting the board is mandatory to disconnect the USB cable and connect it again or the host cannot detect the board again.

This is a problem specific to this board only.

Giovanni
Post Reply