/* This demonstrates IPC between two modules. Each module creates a queue and the other module gets a pointer to that queue. */ /* Specify that this is a module! */ #define TXM_MODULE /* Include the ThreadX module definitions. */ #include "txm_module.h" #include /* Define constants. */ #define DEMO_STACK_SIZE 512 #define DEMO_BYTE_POOL_SIZE 6000 #define DEMO_BLOCK_POOL_SIZE 100 #define DEMO_QUEUE_SIZE 100 /* Define the pool space in the bss section of the module. ULONG is used to get the word alignment. */ ULONG demo_module_pool_space[DEMO_BYTE_POOL_SIZE / 4]; ULONG thread_1_messages_sent; /* Define the ThreadX object control blocks... */ TX_THREAD *thread; TX_QUEUE *queue_rx; TX_QUEUE *queue_tx; TX_BYTE_POOL *byte_pool; /* Define thread prototypes. */ void thread_entry(ULONG thread_input); /* Define the module start function. */ void demo_module_start(ULONG id) { CHAR *pointer; /* Allocate all the objects. In MPU mode, modules cannot allocate control blocks within their own memory area so they cannot corrupt the resident portion of ThreadX by overwriting the control block(s). */ printf("module1 allocating thread\n"); txm_module_object_allocate((void*)&thread, sizeof(TX_THREAD)); // printf("module1 allocating thread\n"); // txm_module_object_allocate((void*)&queue_rx, sizeof(TX_QUEUE)); printf("module1 allocating queue\n"); txm_module_object_allocate((void*)&queue_tx, sizeof(TX_QUEUE)); printf("module1 allocating byte pool\n"); txm_module_object_allocate((void*)&byte_pool, sizeof(TX_BYTE_POOL)); /* Create a byte memory pool from which to allocate the thread stack. */ tx_byte_pool_create(byte_pool, "module byte pool 0", (UCHAR*)demo_module_pool_space, DEMO_BYTE_POOL_SIZE); /* Put system definition stuff in here, e.g. thread creates and other assorted create information. */ /* Allocate the stack for thread 0. */ tx_byte_allocate(byte_pool, (VOID **) &pointer, DEMO_STACK_SIZE, TX_NO_WAIT); /* Create the main thread. */ tx_thread_create(thread, "module1 thread", thread_entry, 0, pointer, DEMO_STACK_SIZE, 1, 1, TX_NO_TIME_SLICE, TX_AUTO_START); /* Allocate the message queue. */ tx_byte_allocate(byte_pool, (VOID **) &pointer, DEMO_QUEUE_SIZE*sizeof(ULONG), TX_NO_WAIT); /* Create the message queue to send to module 2. */ tx_queue_create(queue_tx, "queue_1_to_2", TX_1_ULONG, pointer, DEMO_QUEUE_SIZE*sizeof(ULONG)); } void thread_entry(ULONG thread_input) { ULONG received_message; UINT status; printf("module1 getting pointer to queue created by module2\n"); do { /* Get the message queue created by module 2. */ status = txm_module_object_pointer_get(TXM_QUEUE_OBJECT, "queue_2_to_1", (void*) &queue_rx); } while(status != TX_SUCCESS); /* This thread sends messages to a queue that module 2 reads. This thread also receives messages from module 2. */ while(1) { /* Send message to queue. */ status = tx_queue_send(queue_tx, &thread_1_messages_sent, TX_WAIT_FOREVER); /* Check completion status. */ if (status != TX_SUCCESS) break; /* Increment the message sent. */ thread_1_messages_sent++; status = tx_queue_receive(queue_rx, &received_message, TX_WAIT_FOREVER); /* Check completion status. */ if (status != TX_SUCCESS) break; } }