I have it somehow working. I would like to know your opinion about the solution because I had to make a small change in hal_serial_usb.c in function sduDataReceived I changed the position of chnAddFlagsI and ibqPostFullBufferI function calls.
Now it fills the buffer first, then it adds the flag. Now in the flag I can use a callback to read data from the filled buffer. (before it was first activating the flag and then filling the buffer, Therefore the callback was being called before filling the buffer. So reading the buffer was 1 step behind.)
Now it looks like this:
Code: Select all
void sduDataReceived(USBDriver *usbp, usbep_t ep) {
SerialUSBDriver *sdup = usbp->out_params[ep - 1U];
if (sdup == NULL) {
return;
}
osalSysLockFromISR();
/* Posting the filled buffer in the queue.*/
ibqPostFullBufferI(&sdup->ibqueue,
usbGetReceiveTransactionSizeX(sdup->config->usbp,
sdup->config->bulk_out));
/* Signaling that data is available in the input queue.*/
chnAddFlagsI(sdup, CHN_INPUT_AVAILABLE);
/* The endpoint cannot be busy, we are in the context of the callback,
so a packet is in the buffer for sure. Trying to get a free buffer
for the next transaction.*/
(void) sdu_start_receive(sdup);
osalSysUnlockFromISR();
}
What do you think about this change in HAL source code??
Now in initialization I have like this.
Code: Select all
sduObjectInit(&PORTAB_SDU1);
sduStart(&PORTAB_SDU1, &serusbcfg);
PORTAB_SDU1.event.cb = &sduCB; // <<---------------------------------------------------- attention
usbDisconnectBus(serusbcfg.usbp);
osalThreadSleepMilliseconds(1500);
usbStart(serusbcfg.usbp, &usbcfg);
usbConnectBus(serusbcfg.usbp);
where
Code: Select all
unsigned char buffer[5];
void sduCB (event_source_t *esp){
volatile unsigned int temp = esp->flags & CHN_INPUT_AVAILABLE;
if (temp != CHN_INPUT_AVAILABLE)
return;
volatile unsigned int size_read;
volatile unsigned int size_written;
while( (size_read = chnReadTimeout(&PORTAB_SDU1, (unsigned char*)buffer, 1, TIME_IMMEDIATE)) )
{
size_written = chnWriteTimeout(&PORTAB_SDU1, (unsigned char*)buffer, 1, TIME_IMMEDIATE);
}
(void)size_written;
}
Do you see any problem in the change I made in HAL source code and my approach??
How can I find out how many char in buffer is available?