I2C implementation for STM32
Re: I2C implementation for STM32
Sounds good, but how to realize thread safe mechanism inside driver? Is it good idea to use semaphores/mutexes in the driver?
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: I2C implementation for STM32
Usually drivers based on callbacks are not thread safe, some drivers offer an optional abstraction API on mutexes, see SPI for example, but only when a synchronous API is also present.
Giovanni
Giovanni
Re: I2C implementation for STM32
Hi, Giovanni.
I am trying to realize synchronous mode in driver using waiting tread (like SPI driver). But have some unpredictable bugs.
I have create dedicated thread to serve accelerometer data. Enclose i2cMasterTransmit() function with i2cAcquireBus() and i2cReleaseBus(). Transmission calls every 8 milliseconds. Thread works fine about 5-8 second, after that it ...hm "die". No data from it and I can not jump in it by breakpoint. Sometimes kernel panic because stack overflow or invalid tread state.
Where to look for the root of evil in that cases?
I am trying to realize synchronous mode in driver using waiting tread (like SPI driver). But have some unpredictable bugs.
I have create dedicated thread to serve accelerometer data. Enclose i2cMasterTransmit() function with i2cAcquireBus() and i2cReleaseBus(). Transmission calls every 8 milliseconds. Thread works fine about 5-8 second, after that it ...hm "die". No data from it and I can not jump in it by breakpoint. Sometimes kernel panic because stack overflow or invalid tread state.
Where to look for the root of evil in that cases?
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: I2C implementation for STM32
Hard to tell, this is one of those things that you have to debug hands on.
Make sure that all the interrupt sources are reset from interrupt handlers, resetting of IRQ sources must never be delegated to the threads awakened by the ISR (other threads could be awakened and the ISR would be triggered again and again), also try to increase the interrupt stack size in ch.ld.
Giovanni
Make sure that all the interrupt sources are reset from interrupt handlers, resetting of IRQ sources must never be delegated to the threads awakened by the ISR (other threads could be awakened and the ISR would be triggered again and again), also try to increase the interrupt stack size in ch.ld.
Giovanni
Re: I2C implementation for STM32
Hi all.
After 2 days of debugging I began to gravitate to the idea, that error somewhere in my project, not in I2C driver. System sporadically hangs up even if I do not exchange data with I2C slaves. But if I try to use I2C than kernel panics much more faster. I would appreciate if someone will test the driver in your own project.
Note, API changed, working examples you can find in testhal\STM32\I2C.
After 2 days of debugging I began to gravitate to the idea, that error somewhere in my project, not in I2C driver. System sporadically hangs up even if I do not exchange data with I2C slaves. But if I try to use I2C than kernel panics much more faster. I would appreciate if someone will test the driver in your own project.
Note, API changed, working examples you can find in testhal\STM32\I2C.
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: I2C implementation for STM32
Hi,
If you application crashes even without I2C then I would focus on the application first or the risk is that you are debugging the wrong thing.
Giovanni
If you application crashes even without I2C then I would focus on the application first or the risk is that you are debugging the wrong thing.
Giovanni
Re: I2C implementation for STM32
With recent I2C code (but not the changes over the past few days - will test those soon) I've been having unexplained lockups, where it runs for a few seconds and then stops working. Unfortunately I have no means of attaching a debugger to the board with i2c devices to find out where it is failing..
Re: I2C implementation for STM32
Seems like trouble was in my ugly xbee parser thread. Without it system works fine about 30 minutes by this time. I will leave it running all night. And now I'm going to sleep.
Update: no, problem is somewhere else.
Update2: when I set I2C_USE_WAIT to FALSE and poll driver from thread like:
while(I2CD_tmp75.id_state != I2C_READY){
chThdSleepMilliseconds(1);
}
than problem is occur much rarely.
Also I have try to increase __main_stack_size__ and __process_stack_size__ to 0x0800 in ch.ld without any positive results.
stack if it will helpful:
4 port_halt() chcore.c:43 0x080001e2
3 chDbgPanic() chdebug.c:91 0x08000786
2 chSchDoRescheduleI() chschd.c:251 0x08000c66
1 _port_switch_from_isr() chcore_v7m.c:145 0x080002a4
Update: no, problem is somewhere else.
Update2: when I set I2C_USE_WAIT to FALSE and poll driver from thread like:
while(I2CD_tmp75.id_state != I2C_READY){
chThdSleepMilliseconds(1);
}
than problem is occur much rarely.
Also I have try to increase __main_stack_size__ and __process_stack_size__ to 0x0800 in ch.ld without any positive results.
stack if it will helpful:
4 port_halt() chcore.c:43 0x080001e2
3 chDbgPanic() chdebug.c:91 0x08000786
2 chSchDoRescheduleI() chschd.c:251 0x08000c66
1 _port_switch_from_isr() chcore_v7m.c:145 0x080002a4
Re: I2C implementation for STM32
Hi, Giovanni.
I have include in my project your ISR storm test and began debugging. Troubles really somewhere in I2C driver, but I still do not know where. Anyway, I have some results:
1. If I use blocking polling of driver after each data transfer call, than system runs stable (20 iterations, saturated at 167 uS).
2. If I switch CH_USE_NESTED_LOCKS to FALSE, than system always halts on 4th iteration.
3. If I switch CH_USE_NESTED_LOCKS to TRUE, than system always halts on 1st iteration.
4. If I switch CORTEX_SIMPLIFIED_PRIORITY to TRUE, than system runs stable (40 iterations, saturated at 87 uS).
Any ideas?
I have include in my project your ISR storm test and began debugging. Troubles really somewhere in I2C driver, but I still do not know where. Anyway, I have some results:
1. If I use blocking polling of driver after each data transfer call, than system runs stable (20 iterations, saturated at 167 uS).
2. If I switch CH_USE_NESTED_LOCKS to FALSE, than system always halts on 4th iteration.
3. If I switch CH_USE_NESTED_LOCKS to TRUE, than system always halts on 1st iteration.
4. If I switch CORTEX_SIMPLIFIED_PRIORITY to TRUE, than system runs stable (40 iterations, saturated at 87 uS).
Any ideas?
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: I2C implementation for STM32
Hi Barthess,
Hard to say, probably it could be some violation of the API protocol (unmatched locks/unlocks and similar). Do you compile with debug options enabled?
The IRQ storm is great at detecting race conditions in IRQ handlers, you should look for that.
Anyway, I am adding extra debugging features that could help you in next version.
Giovanni
Hard to say, probably it could be some violation of the API protocol (unmatched locks/unlocks and similar). Do you compile with debug options enabled?
The IRQ storm is great at detecting race conditions in IRQ handlers, you should look for that.
Anyway, I am adding extra debugging features that could help you in next version.
Giovanni