2017-03-23 04:13 AM
Hello,
I have been trying to solve problem with the osMessagePut function from
CMSIS-RTOS wrapper. I am not able to insert instances of C structure into
the message queue.
My code is following:
// transmission queue
static
osMessageQId
tx_CAN_Message_queue;
// mutex related to transmission queue
static
osMutexId
tx_mutex;
// function initializes the task's internal objects
void
Init_Task(
void){
// transmission queue
osMessageQDef(tx_CAN_Message_queue, CAN_BUFFER_LENGTH,
msg_t
);
tx_CAN_Message_queue = osMessageCreate(osMessageQ(tx_CAN_Message_queue),
NULL
);// mutex related to transmission queue
osMutexDef(tx_mut);
tx_mutex = osMutexCreate(osMutex(tx_mut));
}
void
send_single_gdt_read_resp(
uint8_t
blk_index,uint8_t
rec_index){
// created message
thb_msg_t
tx_msg;
// created packets for the transmitted message
thb_pkt_t
tx_pkt_array[MAX_NO_PACKETS];
uint8_t
i;uint8_t
no_packets;
can_message_t
can_msg;
// prepare the message
build_single_gdt_msg(
SHORT_SINGLE_GDT_READ_RESP_MSG, blk_index, rec_index, &tx_msg);
// divide the message into packets
no_packets = calc_no_packets(&tx_msg);
tx_msg_to_packets(&tx_msg, no_packets, tx_pkt_array);
// convert each packet into appropriate format and send it
for
(i = 0; i < no_packets; i++){
can_msg = packet_to_can_msg(tx_pkt_array+i);
// insert the prepared packet into the transmission queue
// osOK = 0x0
if
(!osMutexWait(tx_mutex, osWaitForever)){
// put the packet into the queue
//
wait 5
ms
?osMessagePut(tx_CAN_Message_queue, (
uint32_t
)(&can_msg), 5);osMutexRelease(tx_mutex);
}
}
}
I have tried to insert integers into the message queue and it was successfull.
Please can anybody tell me what I do wrong? Thanks.
#cmsis-rtos #stm32f #free-rtos2017-03-23 05:50 AM
Note the differences between Mail Queues and Message Queues.
2017-03-23 07:42 AM
Hello riggs.rob,
thank you for your reaction. I am not sure whether I understand you
in right manner. Do you mean that I use wrong type of queue for passing
the structures?
2017-03-23 07:57 AM
Hello riggs.rob,
thank you for your reaction. I am not sure whether I understand you
in right manner. Do you mean that I use wrong type of queue for passing
the structures?
2017-03-23 08:39 AM
As riggs.rob indicates, you actually want to use a mail queue. The OS nomenclature is a bit confusing, and changed in CMSIS RTOS v2. I'm using the following template. I don't use the OS macros but declare the necessary structures directly:
template <typename T, uint16_t SIZE>
class MailQueue
{
public:
MailQueue()
{
for (uint16_t i = 0; i < 4 + SIZE; ++i)
m_queue[i] = 0;
m_pointers[0] = m_queue;
m_pointers[1] = m_memory;
m_def.queue_sz = SIZE;
m_def.item_sz = sizeof(T);
m_def.pool = m_pointers;
m_id = osMailCreate(&m_def, NULL); // , thread id);
ASSERT(m_id);
}
void put(const T& message)
{
T* mail = static_cast<T*>(osMailAlloc(m_id, 0));
ASSERT(mail);
if (mail)
{
*mail = message;
osMailPut(m_id, mail);
}
}
void get(T& message)
{
osEvent event = osMailGet(m_id, osWaitForever);
if (event.status == osEventMail)
{
T* mail = static_cast<T*>(event.value.p);
message = *mail;
osMailFree(m_id, mail);
}
}
private:
uint32_t m_queue[4 + SIZE];
uint32_t m_memory[3 + ((sizeof(T) + 3) / 4) * SIZE];
void* m_pointers[2];
osMailQDef_t m_def; // Redundant after osMailCreate() called?
osMailQId m_id;
};
2018-11-22 02:39 AM
check this, work pretty fine:
https://www.keil.com/pack/doc/CMSIS/RTOS/html/group__CMSIS__RTOS__Message.html
the ST examples in the Cube Repro are very simple!
maybe this is what you need / search for... You can send every structure over the Queue