1 /* USER CODE BEGIN Header */
2 /**
3   ******************************************************************************
4   * @file    stm_list.c
5   * @author  MCD Application Team
6   * @brief   TCircular Linked List Implementation.
7   ******************************************************************************
8   * @attention
9   *
10   * Copyright (c) 2022 STMicroelectronics.
11   * All rights reserved.
12   *
13   * This software is licensed under terms that can be found in the LICENSE file
14   * in the root directory of this software component.
15   * If no LICENSE file comes with this software, it is provided AS-IS.
16   *
17   ******************************************************************************
18   */
19 /* USER CODE END Header */
20 
21 /******************************************************************************
22  * Include Files
23  ******************************************************************************/
24 #include "utilities_common.h"
25 
26 #include "stm_list.h"
27 
28 /******************************************************************************
29  * Function Definitions
30  ******************************************************************************/
LST_init_head(tListNode * listHead)31 void LST_init_head (tListNode * listHead)
32 {
33   listHead->next = listHead;
34   listHead->prev = listHead;
35 }
36 
LST_is_empty(tListNode * listHead)37 uint8_t LST_is_empty (tListNode * listHead)
38 {
39   uint32_t primask_bit;
40   uint8_t return_value;
41 
42   primask_bit = __get_PRIMASK();  /**< backup PRIMASK bit */
43   __disable_irq();                  /**< Disable all interrupts by setting PRIMASK bit on Cortex*/
44   if(listHead->next == listHead)
45   {
46     return_value = TRUE;
47   }
48   else
49   {
50     return_value = FALSE;
51   }
52   __set_PRIMASK(primask_bit);     /**< Restore PRIMASK bit*/
53 
54   return return_value;
55 }
56 
LST_insert_head(tListNode * listHead,tListNode * node)57 void LST_insert_head (tListNode * listHead, tListNode * node)
58 {
59   uint32_t primask_bit;
60 
61   primask_bit = __get_PRIMASK();  /**< backup PRIMASK bit */
62   __disable_irq();                  /**< Disable all interrupts by setting PRIMASK bit on Cortex*/
63 
64   node->next = listHead->next;
65   node->prev = listHead;
66   listHead->next = node;
67   (node->next)->prev = node;
68 
69   __set_PRIMASK(primask_bit);     /**< Restore PRIMASK bit*/
70 }
71 
LST_insert_tail(tListNode * listHead,tListNode * node)72 void LST_insert_tail (tListNode * listHead, tListNode * node)
73 {
74   uint32_t primask_bit;
75 
76   primask_bit = __get_PRIMASK();  /**< backup PRIMASK bit */
77   __disable_irq();                  /**< Disable all interrupts by setting PRIMASK bit on Cortex*/
78 
79   node->next = listHead;
80   node->prev = listHead->prev;
81   listHead->prev = node;
82   (node->prev)->next = node;
83 
84   __set_PRIMASK(primask_bit);     /**< Restore PRIMASK bit*/
85 }
86 
LST_remove_node(tListNode * node)87 void LST_remove_node (tListNode * node)
88 {
89   uint32_t primask_bit;
90 
91   primask_bit = __get_PRIMASK();  /**< backup PRIMASK bit */
92   __disable_irq();                  /**< Disable all interrupts by setting PRIMASK bit on Cortex*/
93 
94   (node->prev)->next = node->next;
95   (node->next)->prev = node->prev;
96 
97   __set_PRIMASK(primask_bit);     /**< Restore PRIMASK bit*/
98 }
99 
LST_remove_head(tListNode * listHead,tListNode ** node)100 void LST_remove_head (tListNode * listHead, tListNode ** node )
101 {
102   uint32_t primask_bit;
103 
104   primask_bit = __get_PRIMASK();  /**< backup PRIMASK bit */
105   __disable_irq();                  /**< Disable all interrupts by setting PRIMASK bit on Cortex*/
106 
107   *node = listHead->next;
108   LST_remove_node (listHead->next);
109 
110   __set_PRIMASK(primask_bit);     /**< Restore PRIMASK bit*/
111 }
112 
LST_remove_tail(tListNode * listHead,tListNode ** node)113 void LST_remove_tail (tListNode * listHead, tListNode ** node )
114 {
115   uint32_t primask_bit;
116 
117   primask_bit = __get_PRIMASK();  /**< backup PRIMASK bit */
118   __disable_irq();                  /**< Disable all interrupts by setting PRIMASK bit on Cortex*/
119 
120   *node = listHead->prev;
121   LST_remove_node (listHead->prev);
122 
123   __set_PRIMASK(primask_bit);     /**< Restore PRIMASK bit*/
124 }
125 
LST_insert_node_after(tListNode * node,tListNode * ref_node)126 void LST_insert_node_after (tListNode * node, tListNode * ref_node)
127 {
128   uint32_t primask_bit;
129 
130   primask_bit = __get_PRIMASK();  /**< backup PRIMASK bit */
131   __disable_irq();                  /**< Disable all interrupts by setting PRIMASK bit on Cortex*/
132 
133   node->next = ref_node->next;
134   node->prev = ref_node;
135   ref_node->next = node;
136   (node->next)->prev = node;
137 
138   __set_PRIMASK(primask_bit);     /**< Restore PRIMASK bit*/
139 }
140 
LST_insert_node_before(tListNode * node,tListNode * ref_node)141 void LST_insert_node_before (tListNode * node, tListNode * ref_node)
142 {
143   uint32_t primask_bit;
144 
145   primask_bit = __get_PRIMASK();  /**< backup PRIMASK bit */
146   __disable_irq();                  /**< Disable all interrupts by setting PRIMASK bit on Cortex*/
147 
148   node->next = ref_node;
149   node->prev = ref_node->prev;
150   ref_node->prev = node;
151   (node->prev)->next = node;
152 
153   __set_PRIMASK(primask_bit);     /**< Restore PRIMASK bit*/
154 }
155 
LST_get_size(tListNode * listHead)156 int LST_get_size (tListNode * listHead)
157 {
158   int size = 0;
159   tListNode * temp;
160   uint32_t primask_bit;
161 
162   primask_bit = __get_PRIMASK();  /**< backup PRIMASK bit */
163   __disable_irq();                  /**< Disable all interrupts by setting PRIMASK bit on Cortex*/
164 
165   temp = listHead->next;
166   while (temp != listHead)
167   {
168     size++;
169     temp = temp->next;
170   }
171 
172   __set_PRIMASK(primask_bit);     /**< Restore PRIMASK bit*/
173 
174   return (size);
175 }
176 
LST_get_next_node(tListNode * ref_node,tListNode ** node)177 void LST_get_next_node (tListNode * ref_node, tListNode ** node)
178 {
179   uint32_t primask_bit;
180 
181   primask_bit = __get_PRIMASK();  /**< backup PRIMASK bit */
182   __disable_irq();                  /**< Disable all interrupts by setting PRIMASK bit on Cortex*/
183 
184   *node = ref_node->next;
185 
186   __set_PRIMASK(primask_bit);     /**< Restore PRIMASK bit*/
187 }
188 
LST_get_prev_node(tListNode * ref_node,tListNode ** node)189 void LST_get_prev_node (tListNode * ref_node, tListNode ** node)
190 {
191   uint32_t primask_bit;
192 
193   primask_bit = __get_PRIMASK();  /**< backup PRIMASK bit */
194   __disable_irq();                  /**< Disable all interrupts by setting PRIMASK bit on Cortex*/
195 
196   *node = ref_node->prev;
197 
198   __set_PRIMASK(primask_bit);      /**< Restore PRIMASK bit*/
199 }
200