1 /******************************************************************************
2 *  Filename:       aux_smph.h
3 *
4 *  Description:    Defines and prototypes for the AUX 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 aux_group
40 //! @{
41 //! \addtogroup auxsmph_api
42 //! @{
43 //
44 //*****************************************************************************
45 
46 #ifndef __AUX_SMPH_H__
47 #define __AUX_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_aux_smph.h"
64 #include "../inc/hw_memmap.h"
65 #include "debug.h"
66 
67 //*****************************************************************************
68 //
69 // General constants and defines
70 //
71 //*****************************************************************************
72 #define AUX_SMPH_FREE     0x00000001 // MCU Semaphore has not been claimed
73 #define AUX_SMPH_CLAIMED  0x00000000 // MCU Semaphore has been claimed
74 
75 //*****************************************************************************
76 //
77 // Values that can be passed to AUXSMPHAcquire and AUXSMPHRelease
78 // as the ui32Semaphore parameter.
79 //
80 //*****************************************************************************
81 #define AUX_SMPH_0                0 // AUX Semaphore  0
82 #define AUX_SMPH_1                1 // AUX Semaphore  1
83 #define AUX_SMPH_2                2 // AUX Semaphore  2
84 #define AUX_SMPH_3                3 // AUX Semaphore  3
85 #define AUX_SMPH_4                4 // AUX Semaphore  4
86 #define AUX_SMPH_5                5 // AUX Semaphore  5
87 #define AUX_SMPH_6                6 // AUX Semaphore  6
88 #define AUX_SMPH_7                7 // AUX Semaphore  7
89 
90 //*****************************************************************************
91 //
92 // API Functions and prototypes
93 //
94 //*****************************************************************************
95 
96 //*****************************************************************************
97 //
98 //! \brief Acquire an AUX semaphore.
99 //!
100 //! This function acquires the given AUX semaphore, blocking the call until
101 //! the semaphore is available.
102 //!
103 //! \note The semaphore can also be acquired by the dedicated Sensor Controller.
104 //! The System CPU master can thus be competing for the shared resource, i.e.
105 //! the specified semaphore.
106 //!
107 //! \param ui32Semaphore is the semaphore number.
108 //! - \ref AUX_SMPH_0
109 //! - \ref AUX_SMPH_1
110 //! - \ref AUX_SMPH_2
111 //! - \ref AUX_SMPH_3
112 //! - \ref AUX_SMPH_4
113 //! - \ref AUX_SMPH_5
114 //! - \ref AUX_SMPH_6
115 //! - \ref AUX_SMPH_7
116 //!
117 //! \return None
118 //!
119 //! \sa AUXSMPHTryAcquire(), AUXSMPHRelease()
120 //
121 //*****************************************************************************
122 __STATIC_INLINE void
AUXSMPHAcquire(uint32_t ui32Semaphore)123 AUXSMPHAcquire(uint32_t ui32Semaphore)
124 {
125     // Check the arguments.
126     ASSERT((ui32Semaphore == AUX_SMPH_0) ||
127            (ui32Semaphore == AUX_SMPH_1) ||
128            (ui32Semaphore == AUX_SMPH_2) ||
129            (ui32Semaphore == AUX_SMPH_3) ||
130            (ui32Semaphore == AUX_SMPH_4) ||
131            (ui32Semaphore == AUX_SMPH_5) ||
132            (ui32Semaphore == AUX_SMPH_6) ||
133            (ui32Semaphore == AUX_SMPH_7));
134 
135     // Wait for semaphore to be released such that it can be claimed
136     // Semaphore register reads 1 when lock was acquired otherwise 0
137     // (i.e. AUX_SMPH_CLAIMED).
138     while(HWREG(AUX_SMPH_BASE + AUX_SMPH_O_SMPH0 + 4 * ui32Semaphore) ==
139             AUX_SMPH_CLAIMED)
140     {
141     }
142 }
143 
144 //*****************************************************************************
145 //
146 //! \brief Try to acquire an AUX semaphore.
147 //!
148 //! This function tries to acquire the given AUX semaphore, if the semaphore
149 //! could not be claimed the function returns false.
150 //!
151 //! \note The semaphore can also be acquired by the dedicated Sensor Controller.
152 //! The System CPU master can thus be competing for the shared resource, i.e.
153 //! the specified semaphore.
154 //!
155 //! \param ui32Semaphore is the semaphore number.
156 //! - \ref AUX_SMPH_0
157 //! - \ref AUX_SMPH_1
158 //! - \ref AUX_SMPH_2
159 //! - \ref AUX_SMPH_3
160 //! - \ref AUX_SMPH_4
161 //! - \ref AUX_SMPH_5
162 //! - \ref AUX_SMPH_6
163 //! - \ref AUX_SMPH_7
164 //!
165 //! \return Returns true if semaphore was acquired, false otherwise
166 //!
167 //! \sa AUXSMPHAcquire(), AUXSMPHRelease()
168 //
169 //*****************************************************************************
170 __STATIC_INLINE bool
AUXSMPHTryAcquire(uint32_t ui32Semaphore)171 AUXSMPHTryAcquire(uint32_t ui32Semaphore)
172 {
173     uint32_t ui32SemaReg;
174 
175     // Check the arguments.
176     ASSERT((ui32Semaphore == AUX_SMPH_0) ||
177            (ui32Semaphore == AUX_SMPH_1) ||
178            (ui32Semaphore == AUX_SMPH_2) ||
179            (ui32Semaphore == AUX_SMPH_3) ||
180            (ui32Semaphore == AUX_SMPH_4) ||
181            (ui32Semaphore == AUX_SMPH_5) ||
182            (ui32Semaphore == AUX_SMPH_6) ||
183            (ui32Semaphore == AUX_SMPH_7));
184 
185     // AUX Semaphore register reads 1 if lock was acquired
186     // (i.e. SMPH_FREE when read) but subsequent reads will read 0.
187     ui32SemaReg = HWREG(AUX_SMPH_BASE + AUX_SMPH_O_SMPH0 + 4 * ui32Semaphore);
188 
189     return (ui32SemaReg == AUX_SMPH_FREE);
190 }
191 
192 //*****************************************************************************
193 //
194 //! \brief Release an AUX semaphore by System CPU master.
195 //!
196 //! This function releases the given AUX semaphore.
197 //!
198 //! \note It is up to the application to provide the convention for clearing
199 //! semaphore.
200 //!
201 //! \note The semaphore can also be acquired by the dedicated Sensor Controller.
202 //! The System CPU master can thus be competing for the shared resource, i.e.
203 //! the specified semaphore.
204 //!
205 //! \param ui32Semaphore is the semaphore number.
206 //! - \ref AUX_SMPH_0
207 //! - \ref AUX_SMPH_1
208 //! - \ref AUX_SMPH_2
209 //! - \ref AUX_SMPH_3
210 //! - \ref AUX_SMPH_4
211 //! - \ref AUX_SMPH_5
212 //! - \ref AUX_SMPH_6
213 //! - \ref AUX_SMPH_7
214 //!
215 //! \return None
216 //!
217 //! \sa AUXSMPHAcquire(), AUXSMPHTryAcquire()
218 //
219 //*****************************************************************************
220 __STATIC_INLINE void
AUXSMPHRelease(uint32_t ui32Semaphore)221 AUXSMPHRelease(uint32_t ui32Semaphore)
222 {
223     // Check the arguments.
224     ASSERT((ui32Semaphore == AUX_SMPH_0) ||
225            (ui32Semaphore == AUX_SMPH_1) ||
226            (ui32Semaphore == AUX_SMPH_2) ||
227            (ui32Semaphore == AUX_SMPH_3) ||
228            (ui32Semaphore == AUX_SMPH_4) ||
229            (ui32Semaphore == AUX_SMPH_5) ||
230            (ui32Semaphore == AUX_SMPH_6) ||
231            (ui32Semaphore == AUX_SMPH_7));
232 
233     // No check before release. It is up to the application to provide the
234     // conventions for who and when a semaphore can be released.
235     HWREG(AUX_SMPH_BASE + AUX_SMPH_O_SMPH0 + 4 * ui32Semaphore) =
236         AUX_SMPH_FREE;
237 }
238 
239 //*****************************************************************************
240 //
241 // Mark the end of the C bindings section for C++ compilers.
242 //
243 //*****************************************************************************
244 #ifdef __cplusplus
245 }
246 #endif
247 
248 #endif // __AUX_SMPH_H__
249 
250 //*****************************************************************************
251 //
252 //! Close the Doxygen group.
253 //! @}
254 //! @}
255 //
256 //*****************************************************************************
257