Something strange with timers
Posted: Mon Jun 25, 2012 5:25 am
I have ported my app to ChibiOS and now step by step trying to switch my 'HAL needs' to ChibiOS.
I had UART protocol that worked with ChibiOS and custom IRQ/queue. But when I tried to switch it to Serial driver of ChibiOS, I found something strange.
First, function sdReadAsynchronous just not worked. It worked by itself after some minor changes in another thread.
I changed it to sdReadTimeout, tried to set breakpoint to it and step, it just jumps to start of 'while' loop instead of following to next statement.
Sometimes it worked for a second, then tried to execute empty memory content.
Couple times I have noted that something brokes variables (for example, in I2C driver of STM8S that I have ported according to STM32 driver, but without DMA).
I noted that 'dp' variable brokes after timer function (I didn't remember exactly, but I had breakpoint in this place in right time and tried to trace).
Increasing stack for thread that doing I2C exchange solved the problem that time.
I tried to increase stack size of threads in this case too, this helped a bit (at least it does not broking 'dp' variable in I2C driver in master_receive_timeout, yes, it started do it again!), but serial thread makes surprises from time to time (executing empty flash, or something like this).
Here is my code:
Here is two side effects:
1. When I increased timeout from TIME_INFINITE to MS2ST(50) in sdReadTimeout, time from start to end of transmission was increased.
2. When serial communication was stalled, I breaked execution and looked at timeout_console_msg. It was 0xFF3F, what is definitely wrong.
I have tried to be provide maximum detailed explanation of problem.
Can you give me some advice?
Thanks in advance!
p.s. and sorry for my english
I had UART protocol that worked with ChibiOS and custom IRQ/queue. But when I tried to switch it to Serial driver of ChibiOS, I found something strange.
First, function sdReadAsynchronous just not worked. It worked by itself after some minor changes in another thread.
I changed it to sdReadTimeout, tried to set breakpoint to it and step, it just jumps to start of 'while' loop instead of following to next statement.
Sometimes it worked for a second, then tried to execute empty memory content.
Couple times I have noted that something brokes variables (for example, in I2C driver of STM8S that I have ported according to STM32 driver, but without DMA).
Code: Select all
I2C_TypeDef *dp = i2cp->i2c; <<<----- here dp actually equal to i2cp->i2c, i.e to IO register
VirtualTimer vt;
msg_t rdymsg;
chDbgCheck((rxbytes > 1), "i2c_lld_master_receive_timeout");
/* Global timeout for the whole operation.*/
chVTSetI(&vt, timeout, i2c_lld_safety_timeout, (void *)i2cp);
/* Releases the lock from high level driver.*/
chSysUnlock();
/* Initializes driver fields, LSB = 1 -> receive.*/
i2cp->addr = (addr << 1) | 0x01;
i2cp->errors = 0;
i2cp->rxbuf = rxbuf;
i2cp->rxbytes = rxbytes;
i2cp->rxindex = 0; // start with first byte :)
/* Waits until BUSY flag is reset and the STOP from the previous operation
is completed, alternatively for a timeout condition.*/
while ((dp->SR3 & I2C_SR3_BUSY) || (dp->CR2 & I2C_CR2_STOP)) { <<<<--- here it sometimes points to somewhere in RAM
if (!chVTIsArmedI(&vt)) {
chSysLock();
return RDY_TIMEOUT;
}
}
I noted that 'dp' variable brokes after timer function (I didn't remember exactly, but I had breakpoint in this place in right time and tried to trace).
Increasing stack for thread that doing I2C exchange solved the problem that time.
I tried to increase stack size of threads in this case too, this helped a bit (at least it does not broking 'dp' variable in I2C driver in master_receive_timeout, yes, it started do it again!), but serial thread makes surprises from time to time (executing empty flash, or something like this).
Here is my code:
Code: Select all
// timeout(s) functions
void setTimeout(systime_t * timer, unsigned int timeout) {
*timer = chTimeNow() + MS2ST(timeout);
}
unsigned char timeoutExpired(systime_t * timer) {
return chTimeIsWithin(0, *timer)?0:1;
}
Code: Select all
while (1) {
// check if message was received
if (sdReadTimeout(&SD3, (uint8_t *) tmp, 1, MS2ST(50)) && shproto_received(&received_msg, 0x81, tmp[0])) { // read a byte (successful)
if (!console.found) {
if (console.lowspeed && !timeoutExpired(&timeout_console_lowspeed)) { // found just after switching to low speed
error_register(ERROR_CONSOLE_LOWSPEED_OK); // console was found then lost
}
console.found = 1;
}
// collect data
console.on = received_msg.data[1];
console.level = received_msg.data[0];
timeout_counter = 3; // 3 tries left from this point
waiting_answer = 0;
setTimeout(&timeout_console_msg, MS2ST(100)); // wait 100 ms to next query
}
// send message
if (!waiting_answer && timeoutExpired(&timeout_console_msg)) {
shproto_transmit(buffer, 0x02, 0x02, (unsigned char *) &console.led, 1);
sdWrite(&SD3, (uint8_t const*) &buffer, 7+1);
setTimeout(&timeout_console, TIMEOUT_CONSOLE);
waiting_answer = 1;
}
// timeout condition
if (timeoutExpired(&timeout_console)) {
if (console.found) {
error_register(ERROR_CONSOLE_LOST); // console was found then lost
} else {
error_register(ERROR_CONSOLE_NOTFOUND); // console was notfound
}
console.found = 0;
console.on = 0;
console.level = 0;
if (!timeout_counter) { // already tried 3 times..
if (!console.lowspeed) { // try to switch to low speed
console.lowspeed = 1; // lower and lower...
//UART3_BRR2 = 0x1B; UART3_BRR1 = 0xA0; // set low speed (2400)
setTimeout(&timeout_console_lowspeed, TIMEOUT_CONSOLE_LOWSPEED);
} // ... else work as usual and try to send messages
} else {
timeout_counter--; // decrement counter
}
waiting_answer = 0;
}
chThdSleepMilliseconds(1); // we need to receive own echo from channel
}
Here is two side effects:
1. When I increased timeout from TIME_INFINITE to MS2ST(50) in sdReadTimeout, time from start to end of transmission was increased.
2. When serial communication was stalled, I breaked execution and looked at timeout_console_msg. It was 0xFF3F, what is definitely wrong.
I have tried to be provide maximum detailed explanation of problem.
Can you give me some advice?
Thanks in advance!
p.s. and sorry for my english