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?