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 /**                                                                       */
16 /** ThreadX Component                                                     */
17 /**                                                                       */
18 /**   Queue                                                               */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define TX_SOURCE_CODE
24 
25 
26 /* Include necessary system files.  */
27 
28 #include "tx_api.h"
29 #include "tx_trace.h"
30 #include "tx_queue.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _tx_queue_create                                    PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    William E. Lamie, Microsoft Corporation                             */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function creates a message queue.  The message size and depth  */
46 /*    of the queue is specified by the caller.                            */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    queue_ptr                         Pointer to queue control block    */
51 /*    name_ptr                          Pointer to queue name             */
52 /*    message_size                      Size of each queue message        */
53 /*    queue_start                       Starting address of the queue area*/
54 /*    queue_size                        Number of bytes in the queue      */
55 /*                                                                        */
56 /*  OUTPUT                                                                */
57 /*                                                                        */
58 /*    TX_SUCCESS                        Successful completion status      */
59 /*                                                                        */
60 /*  CALLS                                                                 */
61 /*                                                                        */
62 /*    None                                                                */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    Application Code                                                    */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
73 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
74 /*                                            resulting in version 6.1    */
75 /*                                                                        */
76 /**************************************************************************/
_tx_queue_create(TX_QUEUE * queue_ptr,CHAR * name_ptr,UINT message_size,VOID * queue_start,ULONG queue_size)77 UINT  _tx_queue_create(TX_QUEUE *queue_ptr, CHAR *name_ptr, UINT message_size,
78                         VOID *queue_start, ULONG queue_size)
79 {
80 
81 TX_INTERRUPT_SAVE_AREA
82 
83 UINT            capacity;
84 UINT            used_words;
85 TX_QUEUE        *next_queue;
86 TX_QUEUE        *previous_queue;
87 
88 
89     /* Initialize queue control block to all zeros.  */
90     TX_MEMSET(queue_ptr, 0, (sizeof(TX_QUEUE)));
91 
92     /* Setup the basic queue fields.  */
93     queue_ptr -> tx_queue_name =             name_ptr;
94 
95     /* Save the message size in the control block.  */
96     queue_ptr -> tx_queue_message_size =  message_size;
97 
98     /* Determine how many messages will fit in the queue area and the number
99        of ULONGs used.  */
100     capacity =    (UINT) (queue_size / ((ULONG) (((ULONG) message_size) * (sizeof(ULONG)))));
101     used_words =  capacity * message_size;
102 
103     /* Save the starting address and calculate the ending address of
104        the queue.  Note that the ending address is really one past the
105        end!  */
106     queue_ptr -> tx_queue_start =  TX_VOID_TO_ULONG_POINTER_CONVERT(queue_start);
107     queue_ptr -> tx_queue_end =    TX_ULONG_POINTER_ADD(queue_ptr -> tx_queue_start, used_words);
108 
109     /* Set the read and write pointers to the beginning of the queue
110        area.  */
111     queue_ptr -> tx_queue_read =   TX_VOID_TO_ULONG_POINTER_CONVERT(queue_start);
112     queue_ptr -> tx_queue_write =  TX_VOID_TO_ULONG_POINTER_CONVERT(queue_start);
113 
114     /* Setup the number of enqueued messages and the number of message
115        slots available in the queue.  */
116     queue_ptr -> tx_queue_available_storage =  (UINT) capacity;
117     queue_ptr -> tx_queue_capacity =           (UINT) capacity;
118 
119     /* Disable interrupts to put the queue on the created list.  */
120     TX_DISABLE
121 
122     /* Setup the queue ID to make it valid.  */
123     queue_ptr -> tx_queue_id =  TX_QUEUE_ID;
124 
125     /* Place the queue on the list of created queues.  First,
126        check for an empty list.  */
127     if (_tx_queue_created_count == TX_EMPTY)
128     {
129 
130         /* The created queue list is empty.  Add queue to empty list.  */
131         _tx_queue_created_ptr =                   queue_ptr;
132         queue_ptr -> tx_queue_created_next =      queue_ptr;
133         queue_ptr -> tx_queue_created_previous =  queue_ptr;
134     }
135     else
136     {
137 
138         /* This list is not NULL, add to the end of the list.  */
139         next_queue =      _tx_queue_created_ptr;
140         previous_queue =  next_queue -> tx_queue_created_previous;
141 
142         /* Place the new queue in the list.  */
143         next_queue -> tx_queue_created_previous =  queue_ptr;
144         previous_queue -> tx_queue_created_next =  queue_ptr;
145 
146         /* Setup this queues's created links.  */
147         queue_ptr -> tx_queue_created_previous =  previous_queue;
148         queue_ptr -> tx_queue_created_next =      next_queue;
149     }
150 
151     /* Increment the created queue count.  */
152     _tx_queue_created_count++;
153 
154     /* Optional queue create extended processing.  */
155     TX_QUEUE_CREATE_EXTENSION(queue_ptr)
156 
157     /* If trace is enabled, register this object.  */
158     TX_TRACE_OBJECT_REGISTER(TX_TRACE_OBJECT_TYPE_QUEUE, queue_ptr, name_ptr, queue_size, message_size)
159 
160     /* If trace is enabled, insert this event into the trace buffer.  */
161     TX_TRACE_IN_LINE_INSERT(TX_TRACE_QUEUE_CREATE, queue_ptr, message_size, TX_POINTER_TO_ULONG_CONVERT(queue_start), queue_size, TX_TRACE_QUEUE_EVENTS)
162 
163     /* Log this kernel call.  */
164     TX_EL_QUEUE_CREATE_INSERT
165 
166     /* Restore interrupts.  */
167     TX_RESTORE
168 
169     /* Return TX_SUCCESS.  */
170     return(TX_SUCCESS);
171 }
172 
173