This is an old revision of the document!
Messages is one of way to exchange data between tasks. The message can contain any information: data from USART, external sensorv values, buttons and switches state, ect. There are two types of messages in OSA: pointers to messages and simple byte messages.
Pointer to messages are most universal. Through this type of messages user can send any information of any size. Task send only pointer to this information.
Simple byte messages can accept values form 1 to 255 (0 - message absent). This type of message is realized in OSA to reduce memory usage. Simple byte message needs 1 byte of RAM (pointer to message needs 2 bytes for PIC10, PIC12 and PIC16 and 3 bytes for other).
The message can be in on of two states: free (or absent) and busy (or existing). If message contains information, then it is existing, else it is free.
By default the type of pointer to message is void*. User can change it by defining OST_MSG type in OSAcfg.h.:
#define OS_MSG_TYPE const char *
In this example we tell to compiller that pointer to message will point to data in ROM.
To work with pointer to message we have to create it by service OS_Msg_Create.
There are three services to send message: OS_Msg_Send, OS_Msg_Send_TO and OS_Msg_Send_Now.
OS_Msg_Send first checks for message is free. If it is busy (i.e. previous message was not received by another task yet) then task put in wait state while message will not became free. If message is free, then it can be sent immediately.
OS_Msg_Send_TO - same as OS_Msg_Send with exit if timeout expired while waiting for message is free.
OS_Msg_Send_Now sends message immediately regardless of current message state. If message was busy before calling this service, then it will be overwritten by new data, and system flag OS_IsEventError will be set.
Here is a small example of using pointer to message:
OST_MSG_CB msg_cb; void Task_USARTReceive (void) { static char BUF[10]; // Buffer for received data from UART static char Message[10]; // Message body OS_Msg_Create(msg_cb); // First we create message for (;;){ ... // Receiving UART data ... // Wait for message became free OS_Cond_Wait(!OS_Msg_Check(msg_cb)); // When we get here we can be sure that previous message was accepted by another task // and Message[] array does not contain any information. // Copying received data to message buffer memcpy(Message, BUF, 10); // and sending message OS_Msg_Send(msg_cb, (OST_MSG)Message); ... } } . . . void Task_Work (void) { OST_MSG msg; for (;;) { // Waiting for message OS_Msg_Wait(msg_cb, msg); // Now msg points to array Message from Task_USARTReceive // After service OS_Msg_Wait message msg_cb becames free ... } }
By default type of simple byte message is unsigned char. However user can change type of message by defining OST_SMSG in OSAcfg.h:
#define OS_SMSG_TYPE unsigned long // change SMSG type
Changing type of simple message we lossing advantage in RAM usage. But it can be usefull in some cases. I.e. when we using pointer to message, we can't modify memory where body of message placed until message will be accepted (array Message[] in previous example). When we using simple message this restriction removed because body of message copied in message variable.
Work with simple messages is similar to pointer to messages. Here is an example of simple byte message using:
OST_SMSG smsg_Buttons; . . . void Task_Buttons (void) { OS_Smsg_Create(smsg_Buttons); for (;;) { . . . if (!RB0 || !RB1 || !RB2) OS_Smsg_Send(smsg_Buttons, (OST_SMSG)PORTB & 0x07); ... } } ... void Task_Work (void) { OST_SMSG smsg; for (;;) { OS_Smsg_Wait(smsg_Buttons, smsg); // Processing pressed button if (smsg & 0x01) ...; if (smsg & 0x02) ...; if (smsg & 0x04) ...; ... } }
Service | Arguments | Description | Properties |
---|---|---|---|
Creating | |||
OS_Msg_Create | (msg_cb) | Create message and zero it. | |
Sending | |||
OS_Msg_Send | (msg_cb, message) | Send message. If message is already exists then wait when it became free. | |
OS_Msg_Send_TO | (msg_cb, message, timeout) | Send message. If message is already exists then wait when it became free. Exit if timeout expired. | |
OS_Msg_Send_Now | (msg_cb, message) | Send message. If message is already exists then it will be overwritten. | |
Checking | |||
bool OS_Msg_Check | (msg_cb) | Check for message exists | |
OS_Msg_Accept | (msg_cb, os_msg_type_var) | Accept existing message. After accepting message is set free. | |
Waiting | |||
OS_Msg_Wait | (msg_cb, os_msg_type_var) | Wait for message. After waiting message is set free | |
OS_Msg_Wait_TO | (msg_cb, os_msg_type_var, timeout) | Wait for message. Exit if timeout expired. |
Service | Arguments | Description | Properties |
---|---|---|---|
Creating | |||
OS_Smsg_Create | (smsg) | Create and zero simple message | |
Sending | |||
OS_Smsg_Send | (smsg, smessage) | Send simple message. If message already exists then wait when it became free. | |
OS_Smsg_Send_TO | (smsg, smessage, timeout) | Send simple message. If message already exists then wait when it became free. Exit if timeout expired. | |
OS_Smsg_Send_Now | (smsg, smessage) | Send simple message. If message already exists then it will be overwritten. | |
Checking | |||
bool OS_Smsg_Check | (smsg) | Check for simple message exists | |
OS_Smsg_Accept | (smsg, os_smsg_type_var) | Accept existing simple message. After accepting message is cleared. | |
Waiting | |||
OS_Smsg_Wait | (smsg, os_smsg_type_var) | Wait for simple message. After accepting simple message is cleared. | |
OS_Smsg_Wait_TO | (smsg, os_smsg_type_var, timeout) | Wait for simple message. After accepting simple message is cleared. Exit if timeout expired. |