1 /******************************************************************************
2 *  Filename:       smph.h
3 *
4 *  Description:    Defines and prototypes for the MCU Semaphore.
5 *
6 *  Copyright (c) 2015 - 2022, Texas Instruments Incorporated
7 *  All rights reserved.
8 *
9 *  Redistribution and use in source and binary forms, with or without
10 *  modification, are permitted provided that the following conditions are met:
11 *
12 *  1) Redistributions of source code must retain the above copyright notice,
13 *     this list of conditions and the following disclaimer.
14 *
15 *  2) Redistributions in binary form must reproduce the above copyright notice,
16 *     this list of conditions and the following disclaimer in the documentation
17 *     and/or other materials provided with the distribution.
18 *
19 *  3) Neither the name of the ORGANIZATION nor the names of its contributors may
20 *     be used to endorse or promote products derived from this software without
21 *     specific prior written permission.
22 *
23 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 *  POSSIBILITY OF SUCH DAMAGE.
34 *
35 ******************************************************************************/
36 
37 //*****************************************************************************
38 //
39 //! \addtogroup peripheral_group
40 //! @{
41 //! \addtogroup mcusemaphore_api
42 //! @{
43 //
44 //*****************************************************************************
45 
46 #ifndef __SMPH_H__
47 #define __SMPH_H__
48 
49 //*****************************************************************************
50 //
51 // If building with a C++ compiler, make all of the definitions in this header
52 // have a C binding.
53 //
54 //*****************************************************************************
55 #ifdef __cplusplus
56 extern "C"
57 {
58 #endif
59 
60 #include <stdbool.h>
61 #include <stdint.h>
62 #include "../inc/hw_types.h"
63 #include "../inc/hw_smph.h"
64 #include "../inc/hw_memmap.h"
65 #include "debug.h"
66 
67 //*****************************************************************************
68 //
69 // Support for DriverLib in ROM:
70 // This section renames all functions that are not "static inline", so that
71 // calling these functions will default to implementation in flash. At the end
72 // of this file a second renaming will change the defaults to implementation in
73 // ROM for available functions.
74 //
75 // To force use of the implementation in flash, e.g. for debugging:
76 // - Globally: Define DRIVERLIB_NOROM at project level
77 // - Per function: Use prefix "NOROM_" when calling the function
78 //
79 //*****************************************************************************
80 #if !defined(DOXYGEN)
81     #define SMPHAcquire                     NOROM_SMPHAcquire
82 #endif
83 
84 //*****************************************************************************
85 //
86 // General constants and defines
87 //
88 //*****************************************************************************
89 #define SMPH_FREE     0x00000001 // MCU Semaphore has not been claimed
90 #define SMPH_CLAIMED  0x00000000 // MCU Semaphore has been claimed
91 
92 //*****************************************************************************
93 //
94 // Values that can be passed to SMPHAcquire, SMPHTryAcquire and SMPHRelease
95 // as the ui32Semaphore parameter.
96 //
97 //*****************************************************************************
98 #define SMPH_0    0 // MCU Semaphore  0
99 #define SMPH_1    1 // MCU Semaphore  1
100 #define SMPH_2    2 // MCU Semaphore  2
101 #define SMPH_3    3 // MCU Semaphore  3
102 #define SMPH_4    4 // MCU Semaphore  4
103 #define SMPH_5    5 // MCU Semaphore  5
104 #define SMPH_6    6 // MCU Semaphore  6
105 #define SMPH_7    7 // MCU Semaphore  7
106 #define SMPH_8    8 // MCU Semaphore  8
107 #define SMPH_9    9 // MCU Semaphore  9
108 #define SMPH_10  10 // MCU Semaphore 10
109 #define SMPH_11  11 // MCU Semaphore 11
110 #define SMPH_12  12 // MCU Semaphore 12
111 #define SMPH_13  13 // MCU Semaphore 13
112 #define SMPH_14  14 // MCU Semaphore 14
113 #define SMPH_15  15 // MCU Semaphore 15
114 #define SMPH_16  16 // MCU Semaphore 16
115 #define SMPH_17  17 // MCU Semaphore 17
116 #define SMPH_18  18 // MCU Semaphore 18
117 #define SMPH_19  19 // MCU Semaphore 19
118 #define SMPH_20  20 // MCU Semaphore 20
119 #define SMPH_21  21 // MCU Semaphore 21
120 #define SMPH_22  22 // MCU Semaphore 22
121 #define SMPH_23  23 // MCU Semaphore 23
122 #define SMPH_24  24 // MCU Semaphore 24
123 #define SMPH_25  25 // MCU Semaphore 25
124 #define SMPH_26  26 // MCU Semaphore 26
125 #define SMPH_27  27 // MCU Semaphore 27
126 #define SMPH_28  28 // MCU Semaphore 28
127 #define SMPH_29  29 // MCU Semaphore 29
128 #define SMPH_30  30 // MCU Semaphore 30
129 #define SMPH_31  31 // MCU Semaphore 31
130 
131 //*****************************************************************************
132 //
133 // API Functions and prototypes
134 //
135 //*****************************************************************************
136 
137 //*****************************************************************************
138 //
139 //! \brief Acquire a semaphore.
140 //!
141 //! This function acquires the given semaphore, blocking the call until
142 //! the semaphore is available.
143 //!
144 //! \param ui32Semaphore is the semaphore number.
145 //! - \ref SMPH_0
146 //! - \ref SMPH_1
147 //! - ...
148 //! - \ref SMPH_31
149 //!
150 //! \return None
151 //
152 //*****************************************************************************
153 extern void SMPHAcquire(uint32_t ui32Semaphore);
154 
155 //*****************************************************************************
156 //
157 //! \brief Try to Acquire a semaphore.
158 //!
159 //! This function tries to acquire the given semaphore, if the semaphore
160 //! could not be claimed the function returns false.
161 //!
162 //! \param ui32Semaphore is the semaphore number.
163 //! - \ref SMPH_0
164 //! - \ref SMPH_1
165 //! - ...
166 //! - \ref SMPH_31
167 //!
168 //! \return Returns if a semaphore was acquired
169 //! - \c true  : Semaphore acquired.
170 //! - \c false : Semaphore \b not acquired.
171 //
172 //*****************************************************************************
173 __STATIC_INLINE bool
SMPHTryAcquire(uint32_t ui32Semaphore)174 SMPHTryAcquire(uint32_t ui32Semaphore)
175 {
176     uint32_t ui32SemaReg;
177 
178     // Check the arguments.
179     ASSERT((ui32Semaphore == SMPH_0) ||
180            (ui32Semaphore == SMPH_1) ||
181            (ui32Semaphore == SMPH_2) ||
182            (ui32Semaphore == SMPH_3) ||
183            (ui32Semaphore == SMPH_4) ||
184            (ui32Semaphore == SMPH_5) ||
185            (ui32Semaphore == SMPH_6) ||
186            (ui32Semaphore == SMPH_7) ||
187            (ui32Semaphore == SMPH_8) ||
188            (ui32Semaphore == SMPH_9) ||
189            (ui32Semaphore == SMPH_10) ||
190            (ui32Semaphore == SMPH_11) ||
191            (ui32Semaphore == SMPH_12) ||
192            (ui32Semaphore == SMPH_13) ||
193            (ui32Semaphore == SMPH_14) ||
194            (ui32Semaphore == SMPH_15) ||
195            (ui32Semaphore == SMPH_16) ||
196            (ui32Semaphore == SMPH_17) ||
197            (ui32Semaphore == SMPH_18) ||
198            (ui32Semaphore == SMPH_19) ||
199            (ui32Semaphore == SMPH_20) ||
200            (ui32Semaphore == SMPH_21) ||
201            (ui32Semaphore == SMPH_22) ||
202            (ui32Semaphore == SMPH_23) ||
203            (ui32Semaphore == SMPH_24) ||
204            (ui32Semaphore == SMPH_25) ||
205            (ui32Semaphore == SMPH_26) ||
206            (ui32Semaphore == SMPH_27) ||
207            (ui32Semaphore == SMPH_28) ||
208            (ui32Semaphore == SMPH_29) ||
209            (ui32Semaphore == SMPH_30) ||
210            (ui32Semaphore == SMPH_31));
211 
212     // Semaphore register reads 1 if lock was acquired
213     // (i.e. SMPH_FREE).
214     ui32SemaReg = HWREG(SMPH_BASE + SMPH_O_SMPH0 + 4 * ui32Semaphore);
215 
216     return (ui32SemaReg == SMPH_FREE);
217 }
218 
219 //*****************************************************************************
220 //
221 //! \brief Release a semaphore.
222 //!
223 //! This function releases the given semaphore.
224 //!
225 //! \note It is up to the application to provide the convention for clearing
226 //! semaphore.
227 //!
228 //! \param ui32Semaphore is the semaphore number.
229 //! - \ref SMPH_0
230 //! - \ref SMPH_1
231 //! - ...
232 //! - \ref SMPH_31
233 //!
234 //! \return None
235 //
236 //*****************************************************************************
237 __STATIC_INLINE void
SMPHRelease(uint32_t ui32Semaphore)238 SMPHRelease(uint32_t ui32Semaphore)
239 {
240     // Check the arguments.
241     ASSERT((ui32Semaphore == SMPH_0) ||
242            (ui32Semaphore == SMPH_1) ||
243            (ui32Semaphore == SMPH_2) ||
244            (ui32Semaphore == SMPH_3) ||
245            (ui32Semaphore == SMPH_4) ||
246            (ui32Semaphore == SMPH_5) ||
247            (ui32Semaphore == SMPH_6) ||
248            (ui32Semaphore == SMPH_7) ||
249            (ui32Semaphore == SMPH_8) ||
250            (ui32Semaphore == SMPH_9) ||
251            (ui32Semaphore == SMPH_10) ||
252            (ui32Semaphore == SMPH_11) ||
253            (ui32Semaphore == SMPH_12) ||
254            (ui32Semaphore == SMPH_13) ||
255            (ui32Semaphore == SMPH_14) ||
256            (ui32Semaphore == SMPH_15) ||
257            (ui32Semaphore == SMPH_16) ||
258            (ui32Semaphore == SMPH_17) ||
259            (ui32Semaphore == SMPH_18) ||
260            (ui32Semaphore == SMPH_19) ||
261            (ui32Semaphore == SMPH_20) ||
262            (ui32Semaphore == SMPH_21) ||
263            (ui32Semaphore == SMPH_22) ||
264            (ui32Semaphore == SMPH_23) ||
265            (ui32Semaphore == SMPH_24) ||
266            (ui32Semaphore == SMPH_25) ||
267            (ui32Semaphore == SMPH_26) ||
268            (ui32Semaphore == SMPH_27) ||
269            (ui32Semaphore == SMPH_28) ||
270            (ui32Semaphore == SMPH_29) ||
271            (ui32Semaphore == SMPH_30) ||
272            (ui32Semaphore == SMPH_31));
273 
274     // No check before release, it is up to the application to provide the
275     // conventions for who and when a semaphore can be released.
276     HWREG(SMPH_BASE + SMPH_O_SMPH0 + 4 * ui32Semaphore) = SMPH_FREE;
277 }
278 
279 //*****************************************************************************
280 //
281 // Support for DriverLib in ROM:
282 // Redirect to implementation in ROM when available.
283 //
284 //*****************************************************************************
285 #if !defined(DRIVERLIB_NOROM) && !defined(DOXYGEN)
286     #include "../driverlib/rom.h"
287     #ifdef ROM_SMPHAcquire
288         #undef  SMPHAcquire
289         #define SMPHAcquire                     ROM_SMPHAcquire
290     #endif
291 #endif
292 
293 //*****************************************************************************
294 //
295 // Mark the end of the C bindings section for C++ compilers.
296 //
297 //*****************************************************************************
298 #ifdef __cplusplus
299 }
300 #endif
301 
302 #endif //  __SMPH_H__
303 
304 //*****************************************************************************
305 //
306 //! Close the Doxygen group.
307 //! @}
308 //! @}
309 //
310 //*****************************************************************************
311