Hello,
I'd like to write some reusabel code module to handle e.g. UART ports. Therefore I'd like to create a variable number (based on some kind of define...) of threads dealing with UART port 1,2,3 ... since it is possible to declare CMSIS-RTOS threads with a variable number of instances it could look like this:
#define UART_PORT_NUM 3 osThreadDef(uart_thread, osPriorityNormal, UART_PORT_NUM, 0)
my problem: If I need any kind of inter-thread communication (ITC) object, like mutexes or mail queues, for each of this threads I don't know how to declare them, since osMailQDef, osMutexDef and so on, are not designed with a "variable" number of instances...
Any suggestions?
For example: If I'd like to create some interface like
uart_write(uint8_t id, /*< UART channel e.g. 1, 2, 3 */ uint32_t len, /*< number of bytes data is pointing to */ void *data); /*< data to send */
I could use internally a CMSIS-RTOS memory pool to get pseudo dynamic <len> bytes to store the content of <data>. After that I send a signal/message to the thread handling UART<id> to write out this data (that could lead to multiple interrupt acknowledged UART periphery calls...) In this case all threads are equal and designed in a generic way, but I need multiple memory pools to deal with different priorities, otherwise e.g. one thread handling some kind of debug messages could occupy all memory and block another UART thread used for important process data...
Anyway, this is actually meant to be just an example. It doesn't matter if this is the best way handling UART interfaces.
The central question is: How can I create multiple instances of ITC objects?
Since your code would normally need very explicit knowledge about what to do with data from each UART, I can't see why it would be an advantage to try to create n identical threds.
Why not make four calls to instantiate the four threads? In a real situation, it would be likely that you would want different priority for different UART.
Is it really identical data you want to process from all your UART? Because if it was, then it would be likely that you would be expected to use the same mail queues for forwarding your received and processed data. And if the four UART produces identical data, then it would be likely that you would only need one single thread to pick up data and process - while you have four ISR that reacts to the hardware and posts the received data to that single queue.
View all questions in Keil forum