jnz
Newbie
Offline
Posts: 24
Thank You
-Given: 4
-Receive: 1
|
|
« on: July 01, 2015, 05:01:19 17:01 » |
|
Building my first RTOS application. Using Keil RTX which is CMSIS, but this should apply to really any RTOS. Have a fair understanding of the data transfer and blocking options; Signals, Semaphores, Mutexes, Mailboxes, Messages, Yield, Delay, etc.
I'm not sure when to use globals variables vs directly sending messages/signals/mail between threads. The idea is that depending on who you asking, Globals are a fine option - or the devil in code. On the other hand, if I want my threads to be re-useable I don't really want to make assumptions with messages/mail since I won't know what application they are being plugged into.
For example:
I have an 8channel SPI LED driver. I want to store this thread in LED_Driver.c so I can hopefully plug it into any application that later uses this chip. I just initialize and call it when needed. The thread should take in data to set the LEDs to, should be able to return or send back data about diagnostics. So my immediate options are:
1. There is a global struct. It has the LEDs requested states, diagnostic info, etc. The LED thread is run when called, or run at an interval, which then has to call SPI thread and toss a mutex on that, it reads the struct and stores the result there. Any other thread can set requests then call the LED Driver Thread, or can read the diagnostic results.
2. There is no global struct. But there a local one to my main logic thread. I could pass this pointer over in a message/mailbox to the LED thread, set a signal or a semaphore that I need the thread to do something, it has the data now to do so. Call the SPI thread once or many times depending on how it works with this part. If I needed to send diagnostic data back, I could put it in a mailbox and anyone interested could read it. The issue I with this I don't know who will be reading from the mailbox, when/how often, etc.
Which of these is the better option in the long run? Considering the real application would get much more complicated than this and I want an emphasis on re-usable code for threads and drivers etc?
I'm also confused as to how I might make a thread self-containted and re-usable if I don't know what system it'll get plugged into later. For instance, if I wanted to pack away this LED Driver thread so I could just include it, init, and run in another program, that'll greatly change how I treat the data it reads and writes to and what method I use. Right?
|