7 years, 11 months ago.

LPC11U35 USBSerial

Hello, I use USBserial for my project and it is powered by Li-pol battery. My first problem was that the code was not launch without USB connection. It was solved by connect_blocking = false but there is next problem during disconnecting USB cable from PC. When I disconnect cable from PC then the code is stoped but when I shutdown PC with connected USB cable then is everything ok and the code continues.

code

#include "mbed.h"
#include "USBSerial.h"
#include "MODSERIAL.h"


static const uint8_t VCELL_MSB =            0x02; //Read only
static const uint8_t VCELL_LSB =            0x03; //Read only
static const uint8_t SOC_MSB =              0x04; //Read only
static const uint8_t SOC_LSB =              0x05; //Read only
static const uint8_t MODE_MSB =             0x06; //Write only
static const uint8_t MODE_LSB =             0x07; //Write only
static const uint8_t VERSION_MSB =          0x08; //Read only
static const uint8_t VERSION_LSB =          0x09; //Read only
static const uint8_t CONFIG_MSB =           0x0C; //Read/write
static const uint8_t CONFIG_LSB =           0x0D; //Read/write
static const uint8_t COMMAND_MSB =          0xFE; //Write only
static const uint8_t COMMAND_LSB =          0xFF; //Write only

static const uint16_t RST_CODE =            0x5400; //reset code for COMMAND 16-bit register
static const uint16_t QUICKSTART =          0x4000;

//Levels
static const float DIV_VCELL =              1.25e-3; //1.25 mV/level
static const float DIV_SOC =                0.00390625; //1/256% / level

DigitalOut LedStatus(P0_13);
DigitalOut LedBattery(P0_14);
I2C i2c(P0_5, P0_4);
USBSerial usb(0x1f00, 0x2012, 0x0001, false);
//MODSERIAL uart(P0_19, P0_18);
Serial uart(P0_19, P0_18);
InterruptIn GPS_fix(P0_17);


//void Rx_interrupt(MODSERIAL_IRQ_INFO *q)
void Rx_interrupt()
{
    LedBattery = !LedBattery;
    usb.putc(uart.getc());

}


void toogle()
{
    LedStatus = !LedStatus;
}

int SOC()
{
    char cmd[1];
    char data_read[1];
    __disable_irq();
    cmd[0] = 0x04;
    i2c.write(108, cmd, 1);
    i2c.read(109, data_read, 1);
    __enable_irq();
    return data_read[0];
}

int main()
{

    char cmd[2];

    i2c.frequency(400000);

    cmd[0] = 0x00;
    cmd[1] = 0x54;

    i2c.write(108, cmd, 2);

    cmd[0] = 0x4000;

    i2c.write(108, cmd, 1);

    GPS_fix.rise(&toogle);
    uart.baud(9600);
    uart.attach(&Rx_interrupt, Serial::RxIrq);
    

    while(1) {
        wait(1);


        usb.printf("SOC: %d\r\n", SOC());

    

    }
}

Hello Peter,
I recommend you to start with something more simple. Like for instance the code below:

#include "mbed.h"
#include "USBSerial.h"

Serial      uart(P0_19, P0_18);
USBSerial   usb(0x1f00, 0x2012, 0x0001, false);

int main(void) {
    while(1) {
		uart.printf("I am a UART serial port\r\n");
        usb.printf("I am a USB serial port\r\n");
        wait(1);
    }
}


Check how it performs in both cases you described above (I have tested the code above with this and this demo an it worked OK). Then extend the code little by little to see when things go wrong.

posted by Zoltan Hudak 25 Dec 2016

Hello Zoltan,

I added LED to get an information that my board is still alive. So, i disconnected USB cable from PC and LED was permanent off or on. It means the code was frozen. It is very bad feature. I do not know why. But i am surprised that when I turn off my PC with connected USB the problem does not occur and LED continues its flashing.

#include "mbed.h"
#include "USBSerial.h"
 
Serial      uart(P0_19, P0_18);
USBSerial   usb(0x1f00, 0x2012, 0x0001, false);
DigitalOut Led_live(P0_13);

 
int main(void) {
    while(1) {
        uart.printf("I am a UART serial port\r\n");
        usb.printf("I am a USB serial port\r\n");
        Led_live= !Led_live;
        wait(1);
    }
}
posted by Peter Augustin 26 Dec 2016

Hello Peter,
That is strange. Because when I run the code above with my boards (see the links above) the LED remains flashing and the UART serial port keeps sending messages even in case I disconnect the USB cable. Then when I reconnect the USB cable I see the USB serial messages on the PC's terminal again.

posted by Zoltan Hudak 26 Dec 2016

Hello Zoltan,

are those boards powered by external power or battery and not USB ? Maybe I have a bug in my circuit.

posted by Peter Augustin 26 Dec 2016

The boards are powered from external source.
One more thing I'd like to mention is that I could not see how you control the USB connect resistor. I recommend you to add something like below and see how it works:

#include "mbed.h"
#include "USBSerial.h"
 
Serial      uart(P0_19, P0_18);
USBSerial   usb(0x1f00, 0x2012, 0x0001, false);
DigitalOut  usb_connect(P0_6);
DigitalOut  Led_live(P0_13);
 
 
int main(void) {
    usb_connect = 0;
    while(1) {
        uart.printf("I am a UART serial port\r\n");
        usb.printf("I am a USB serial port\r\n");
        Led_live= !Led_live;
        wait(1);
    }
}
posted by Zoltan Hudak 26 Dec 2016

Hello Zoltan,

The control of USB connect does not help. Please, see video and green LED on board.

https://drive.google.com/uc?id=0B_OOjYgNKTK-R1NKOVVmX0c3YUE&export=download

posted by Peter Augustin 26 Dec 2016

It wouldn't be a clean solution but now that the program starts without having the USB cable connected a watchdog could be a reasonable workaround.

#include "mbed.h"
#include "USBSerial.h"
#include "Watchdog.h"
 
Serial      uart(P0_19, P0_18);
USBSerial   usb(0x1f00, 0x2012, 0x0001, false);
DigitalOut  usb_connect(P0_6);
DigitalOut  Led_live(P0_13);
Watchdog    wd;
  
int main(void) {
    usb_connect = 0;
    wd.Configure(3);    // set watchdog's timeout to 3 seconds
    while(1) {
        wd.Service();
        uart.printf("I am a UART serial port\r\n");
        usb.printf("I am a USB serial port\r\n");
        Led_live= !Led_live;
        wait(1);
    }
}
posted by Zoltan Hudak 26 Dec 2016

Hello Zoltan,

i added vbug flag for checking USB is present. Now the board is working super but this is not good solutions. What do You think is it possible that USBSerial library has a bug ?

#include "mbed.h"
#include "USBSerial.h"
 
Serial      uart(P0_19, P0_18);
USBSerial   usb(0x1f00, 0x2012, 0x0001, false);
DigitalOut LedStatus(P0_13);
//DigitalOut LedStatus(P0_7);
DigitalOut USB_connect(P0_6);
DigitalIn vbus (P0_3);

 
int main(void) {
    USB_connect = 0;
    while(1) {
        //uart.printf("I am a UART serial port\r\n");
        if (vbus){
        usb.printf("I am a USB serial port\r\n");
        };
        LedStatus = !LedStatus;
        wait(1);
    }
}
posted by Peter Augustin 27 Dec 2016

Hello Peter,
Good job. I'm glad that you finally found a solution. From programming point of view, USB is a quite complex protocol. So no surprise when something does not work perfectly yet in mbed. In addition, not all mbed boards are supported yet. Actually (officially) LPC11U35 isn't. Anyway, I'm very thankful to mbed people for developing the USBDevice stack for us users.

posted by Zoltan Hudak 29 Dec 2016

I needed low cost MCU with small package size and drag and drop flashing. The LPC11U35FHN33/401 is good for my project. I would like to know if it is possible to send bug report to someone.

Thanks for your support.

posted by Peter Augustin 29 Dec 2016
Be the first to answer this question.