Thread and State machine

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
Saran_yim
Posts: 13
Joined: Wed Dec 22, 2010 9:18 am

Thread and State machine

Post by Saran_yim »

Hi,

I have some question

My application is Displayed OBD Data, On my board I have LCD, Switch, and communicate with OBD Reader via UART

So I have create 3 thread + main thread

1st thread is scan for 4 switches
2nd thread handle UART Communication
3rd thread is Display data on LCD 16x2

and in main thread I put state machine
in my state machine has 3 main state
1 Display OBD Data State
2 Menu State
3 Init State

problem is when I press OK switch in Display OBD Data State I want to go to Menu state
Thread switch get switch but in main thread I have to wait for display state to finish and then I can go to Menu State this take sometime and user feel that switch is slow response.

I have no idea at the moment how to solve this

I think about Create and Terminate thread instead of state machine for example
When Program Start I create Init thread
when Init finish terminate Init Thread and create Display OBD Data Thread

when Switch Ok (Go for menu) press -> terminate Display thread Create Menu Thread

when return from menu -> terminate Menu thread and create Display Thread

is this will work or should I use another method?

Thank you

Saran
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Thread and State machine

Post by Giovanni »

How did you assign priorities to threads?

Thread 1 seems to do a continuous polling so it never releases CPU until its tick is over, this may explain the slowness, it should have the lowest priority among all threads, you may also consider to insert a chThdSleepMilliseconds() in the polling loop, this would free time for the other threads.

In general, try to avoid to write code that never goes into a sleep state.

Giovanni
Post Reply