Page 1 of 1

Managing resources... or how to program for ChibiOS?

Posted: Sun Jan 13, 2013 11:20 pm
by Lupin
I am using ChibiOS for a while now, it's really an amazing piece of software, seriously :-)

But i always wonder how to manage hardware resources correctly.

1) How to determine the work-area size for a thread? Usually i just try to decrease the work area until the system crashes and then give the thread twice as much work area. Is there a proper way to determine the size of work-area for a thread?

2) I noticed that the fatfs file system library needs quite a huge work-area. I created a thread without file system access, then added file system access and noticed that it ran out of work area (system halted).
If i'd have multiple threads that need fatfs I would need to give each thread a huge work-area for fatfs, right? Is there a way to have only one big work-area for fatfs library? Or am I doing something wrong?

3) How can i make sure that multiple threads won't try to use the same hardware resources at the same time?
For example i just tried to read via SDIO (STM32F4) from SD card using fatfs. With one thread it all works fine. But when i start two threads that try to access the SD card it will fail (i guess this is because both threads try to use SDIO hardware). Is there a way to make sure threads will wait for the hardware to become available?
Another example is sound output. Of course only one thread can have sound output. I need a way to signal other threads that the sound output is free or already taken.


Is there something like a beginners tutorial for ChibiOS/threaded programming? A tutorial that goes a bit further than just showing how to create a thread... I'd like to get to know some programming techniques and when it's appropriate to use them.

Re: Managing resources... or how to program for ChibiOS?

Posted: Mon Jan 14, 2013 8:46 am
by Tectu
1) That's exactly the way how to determine the needed stacksize. Making it twice as big might however be a bit overkilled. You always need to know what happens in your threads and handle out of memory stuff correctly. Here is some article about that.

2) What exactly is a huge area for you? Also, when you are working on the same SD card out of multiple threads you must take care about multithreadsafe operations. A community member, mabl, wrote an awesome out of the box working wrapper for this (It's basically a command queue). You can find it here.

3) See above. There are different ways: For one, you can use stuff like mutexes and semaphores, for another, a message queue might be the right thing to go with. The GDISP module of the LCD library is completely threadsafe using such a queue (This feature can be turned on and off using a macro).

4) As far as I know there is nothing such as a tutorial. Here is an article about threads. Also, I don't think that much more is needed since the basic concept of threads and stuff must be understood when working with an RTOS. For another point, the API as well as the doxygen docs are so clean and clear, what do you want more? :P


~ Tectu

Re: Managing resources... or how to program for ChibiOS?

Posted: Mon Jan 14, 2013 9:38 am
by Giovanni
Hi,

About your question 1, if you work in an Eclipse IDE the ChibiOS debug plugin allows to evaluate the real stack area used by all threads. You can assign an arbitrarily large areas, make a test run and then measure the actual usage using the debug plugin.

Giovanni