Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
8 years, 10 months ago.
Why program start after plug in USB cable ?
Hello, I have very simple program but there is a problem because the program starts only after plug in USB cable to board. After unplug USB from board the program is stopped. Could You help me with this issue ?
#include "mbed.h"
#include "USBSerial.h"
#include "MODSERIAL/MODSERIAL.h"
DigitalOut LedStatus(P0_13);
DigitalOut LedBattery(P0_14);
USBSerial usb;
MODSERIAL uart(P0_19, P0_18);
InterruptIn timepulse(P0_17);
void rxCallback(MODSERIAL_IRQ_INFO *q) {
LedBattery = !LedBattery;
usb.putc(uart.getc());
}
void toogle(){
LedStatus = !LedStatus;
}
int main(){
// int c = 'A';
timepulse.rise(&toogle);
uart.baud(9600);
// uart.attach(&txCallback, MODSERIAL::TxIrq);
uart.attach(&rxCallback, MODSERIAL::RxIrq);
// uart.attach(&txEmpty, MODSERIAL::TxEmpty);
while(1){
LedStatus = !LedStatus;
};
}
2 Answers
8 years, 10 months ago.
Hello,
Please notice that in case you create a USBSerial object with no parameters passed to the constructor then by default it blocks the program until a USB serial device has been found. To turn off blocking try to create a USBSerial object as follows:
... USBSerial usb(0x1f00, 0x2012, 0x0001, false); ...
Hello, my board is powered by battery. I think circuit is ok but i am not sure. please see my schematic.
https://drive.google.com/uc?id=0B_OOjYgNKTK-THJuNVpjM1pKLVE&export=download
posted by 14 Dec 2016Yes, it seems OK. I did not notice that you use USBSerial so my first thought was that it could be power supply issue.
posted by 14 Dec 2016Hello,
i made a little changes in my code and i do not know why USBSerial again is blocking my code after disconnect USB cable.
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());
}
}