1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** Thread-Metric Component                                               */
16 /**                                                                       */
17 /**   Message Processing Test                                             */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 
23 /**************************************************************************/
24 /*                                                                        */
25 /*  FUCTION                                                RELEASE        */
26 /*                                                                        */
27 /*    tm_message_processing_test                          PORTABLE C      */
28 /*                                                           6.1.7        */
29 /*  AUTHOR                                                                */
30 /*                                                                        */
31 /*    William E. Lamie, Microsoft Corporation                             */
32 /*                                                                        */
33 /*  DESCRIPTION                                                           */
34 /*                                                                        */
35 /*    Basic test for message exchange processing.                         */
36 /*                                                                        */
37 /*  RELEASE HISTORY                                                       */
38 /*                                                                        */
39 /*    DATE              NAME                      DESCRIPTION             */
40 /*                                                                        */
41 /*  10-15-2021     William E. Lamie         Initial Version 6.1.7         */
42 /*                                                                        */
43 /**************************************************************************/
44 #include "tm_api.h"
45 
46 
47 /* Define the counters used in the demo application...  */
48 
49 unsigned long   tm_message_processing_counter;
50 unsigned long   tm_message_sent[4];
51 unsigned long   tm_message_received[4];
52 
53 
54 /* Define the test thread prototypes.  */
55 
56 void            tm_message_processing_thread_0_entry(void);
57 
58 
59 /* Define the reporting thread prototype.  */
60 
61 void            tm_message_processing_thread_report(void);
62 
63 
64 /* Define the initialization prototype.  */
65 
66 void            tm_message_processing_initialize(void);
67 
68 
69 /* Define main entry point.  */
70 
tm_main()71 void tm_main()
72 {
73 
74     /* Initialize the test.  */
75     tm_initialize(tm_message_processing_initialize);
76 }
77 
78 
79 /* Define the message processing test initialization.  */
80 
tm_message_processing_initialize(void)81 void  tm_message_processing_initialize(void)
82 {
83 
84     /* Create thread 0 at priority 10.  */
85     tm_thread_create(0, 10, tm_message_processing_thread_0_entry);
86 
87     /* Resume thread 0.  */
88     tm_thread_resume(0);
89 
90     /* Create a queue for the message passing.  */
91     tm_queue_create(0);
92 
93     /* Create the reporting thread. It will preempt the other
94        threads and print out the test results.  */
95     tm_thread_create(5, 2, tm_message_processing_thread_report);
96     tm_thread_resume(5);
97 }
98 
99 
100 /* Define the message processing thread.  */
tm_message_processing_thread_0_entry(void)101 void  tm_message_processing_thread_0_entry(void)
102 {
103 
104     /* Initialize the source message.   */
105     tm_message_sent[0] =  0x11112222;
106     tm_message_sent[1] =  0x33334444;
107     tm_message_sent[2] =  0x55556666;
108     tm_message_sent[3] =  0x77778888;
109 
110     while(1)
111     {
112         /* Send a message to the queue.  */
113         tm_queue_send(0, tm_message_sent);
114 
115         /* Receive a message from the queue.  */
116         tm_queue_receive(0, tm_message_received);
117 
118         /* Check for invalid message.  */
119         if (tm_message_received[3] != tm_message_sent[3])
120             break;
121 
122         /* Increment the last word of the 16-byte message.  */
123         tm_message_sent[3]++;
124 
125         /* Increment the number of messages sent and received.  */
126         tm_message_processing_counter++;
127     }
128 }
129 
130 
131 /* Define the message test reporting thread.  */
tm_message_processing_thread_report(void)132 void  tm_message_processing_thread_report(void)
133 {
134 
135 unsigned long   last_counter;
136 unsigned long   relative_time;
137 
138 
139     /* Initialize the last counter.  */
140     last_counter =  0;
141 
142     /* Initialize the relative time.  */
143     relative_time =  0;
144 
145     while(1)
146     {
147 
148         /* Sleep to allow the test to run.  */
149         tm_thread_sleep(TM_TEST_DURATION);
150 
151         /* Increment the relative time.  */
152         relative_time =  relative_time + TM_TEST_DURATION;
153 
154         /* Print results to the stdio window.  */
155         printf("**** Thread-Metric Message Processing Test **** Relative Time: %lu\n", relative_time);
156 
157         /* See if there are any errors.  */
158         if (tm_message_processing_counter == last_counter)
159         {
160 
161             printf("ERROR: Invalid counter value(s). Error sending/receiving messages!\n");
162         }
163 
164         /* Show the time period total.  */
165         printf("Time Period Total:  %lu\n\n", tm_message_processing_counter - last_counter);
166 
167         /* Save the last counter.  */
168         last_counter =  tm_message_processing_counter;
169     }
170 }
171