ThreadX——IPC应用之消息队列 (3)

TX_WAIT_ERROR (0x04) 在非线程调用中指定了TX_NO_WAIT以外的等待选项

UINT tx_queue_send( TX_QUEUE *queue_ptr, VOID *source_ptr, ULONG wait_option); 8、注册发送通知回调函数

描述

此服务注册一个通知回调函数,每当一条消息发送到指定的队列时就会调用该函数。 通知回调的处理由应用程序定义

不允许在应用程序的队列发送通知回调函数中调用具有暂停选项的ThreadX API

参数

queue_ptr 指向先前创建的队列的指针

queue_send_notify 指向应用程序队列发送通知功能的指针。 如果此值为TX_NULL,则禁用通知

返回值

TX_SUCCESS (0x00) 操作成功

TX_QUEUE_ERROR (0x09) 无效的队列指针

TX_FEATURE_NOT_ENABLED (0xFF) 禁用了通知功能

UINT tx_queue_send_notify( TX_QUEUE *queue_ptr, VOID (*queue_send_notify)(TX_QUEUE *)); 三、实例演示

该应用实例创建三个任务和一个队列消息发送通知回调

任务1:按键1按一次向消息队列1发送一条消息(单个变量消息)

任务2:按键2按一次向消息队列2发送一条消息(结构体指针消息)

任务3:向消息队列3发送消息;接收任务1和任务2的消息并打印输出消息内容

回调功能:输出消息队列3的相关信息

创建消息队列

#define DEMO_STACK_SIZE (2 * 1024) #define DEMO_BYTE_POOL_SIZE (32 * 1024) TX_THREAD thread_0; TX_THREAD thread_1; TX_THREAD thread_2; TX_BYTE_POOL byte_pool_0; UCHAR memory_area[DEMO_BYTE_POOL_SIZE]; /* 消息队列 */ TX_QUEUE tx_queue1; TX_QUEUE tx_queue2; TX_QUEUE tx_queue3; ULONG msg_queue1[32]; ULONG msg_queue2[16]; ULONG msg_queue3[8]; struct S_DATA{ uint32_t id; uint16_t flag; uint8_t msg[2]; }; struct S_DATA data_package; void thread_0_entry(ULONG thread_input); void thread_1_entry(ULONG thread_input); void thread_2_entry(ULONG thread_input); void queue3_send_notify(TX_QUEUE *input); void tx_application_define(void *first_unused_memory) { CHAR *pointer = TX_NULL; /* Create a byte memory pool from which to allocate the thread stacks. */ tx_byte_pool_create(&byte_pool_0, "byte pool 0", memory_area, DEMO_BYTE_POOL_SIZE); /* Allocate the stack for thread 0. */ tx_byte_allocate(&byte_pool_0, (VOID **) &pointer, DEMO_STACK_SIZE, TX_NO_WAIT); /* Create the main thread. */ tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0, pointer, DEMO_STACK_SIZE, 1, 1, TX_NO_TIME_SLICE, TX_AUTO_START); /* Allocate the stack for thread 1. */ tx_byte_allocate(&byte_pool_0, (VOID **) &pointer, DEMO_STACK_SIZE, TX_NO_WAIT); /* Create threads 1 */ tx_thread_create(&thread_1, "thread 1", thread_1_entry, 0, pointer, DEMO_STACK_SIZE, 2, 2, TX_NO_TIME_SLICE, TX_AUTO_START); /* Allocate the stack for thread 2. */ tx_byte_allocate(&byte_pool_0, (VOID **) &pointer, DEMO_STACK_SIZE, TX_NO_WAIT); /* Create threads 1 */ tx_thread_create(&thread_2, "thread 2", thread_2_entry, 0, pointer, DEMO_STACK_SIZE, 3, 3, TX_NO_TIME_SLICE, TX_AUTO_START); /* 创建消息队列 */ tx_queue_create(&tx_queue1, "tx_queue1", 1, msg_queue1, sizeof(msg_queue1)); tx_queue_create(&tx_queue2, "tx_queue2", 1, msg_queue2, sizeof(msg_queue2)); tx_queue_create(&tx_queue3, "tx_queue2", 1, msg_queue3, sizeof(msg_queue3)); /* 注册发送消息回调 */ tx_queue_send_notify(&tx_queue3, queue3_send_notify); }

任务1

void thread_0_entry(ULONG thread_input) { uint8_t i =0, key_flag = 0; uint8_t data_single = 0; while(1) { if (0 == key_flag) { if (GPIO_PIN_SET == HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin)) { key_flag = 1; } } else { if (GPIO_PIN_RESET == HAL_GPIO_ReadPin(KEY1_GPIO_Port,KEY1_Pin)) { key_flag = 0; /*按键1触发,向队列1发送消息*/ data_single++; tx_queue_send(&tx_queue1, &data_single, TX_NO_WAIT); } } tx_thread_sleep(20); } }

任务2

void thread_1_entry(ULONG thread_input) { uint8_t key_flag = 0; struct S_DATA *pData; pData = &data_package; pData->id = 1; pData->flag = 2; pData->msg[0] = 3; pData->msg[1] = 4; while(1) { if (0 == key_flag) { if (GPIO_PIN_SET == HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin)) { key_flag = 1; } } else { if (GPIO_PIN_RESET == HAL_GPIO_ReadPin(KEY2_GPIO_Port,KEY2_Pin)) { key_flag = 0; /*按键2触发,向队列2发送消息*/ pData->id += 8; pData->flag += 4; pData->msg[0] += 2; pData->msg[1] += 1; tx_queue_send(&tx_queue2, &pData, TX_NO_WAIT); } } tx_thread_sleep(20); } }

任务3

void thread_2_entry(ULONG thread_input) { UINT status; uint8_t char_data; ULONG long_data = 0; struct S_DATA *buf_data; while(1) { /* 向队列3发送消息 */ long_data++; tx_queue_send(&tx_queue3, &long_data, TX_NO_WAIT); if (0 == (long_data & 7)) { tx_queue_flush(&tx_queue3); } /* 接收队列1消息 */ status = tx_queue_receive(&tx_queue1, &char_data, 1000); if (TX_SUCCESS == status) { SEGGER_RTT_SetTerminal(0); SEGGER_RTT_printf(0, RTT_CTRL_TEXT_BRIGHT_GREEN"message queue1 receive data is %d\r\n", char_data); } /* 接收队列2消息 */ status = tx_queue_receive(&tx_queue2, &buf_data, 1000); if (TX_SUCCESS == status) { SEGGER_RTT_SetTerminal(1); SEGGER_RTT_printf(0, RTT_CTRL_TEXT_BRIGHT_YELLOW"message queue2 receive data is %d\t%d\t%d\t%d \r\n", \ buf_data->id, \ buf_data->flag, \ buf_data->msg[0], \ buf_data->msg[1]); } } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpdzdd.html