1 /******************************************************************************
2 *  Filename:       aux_smph.h
3 *  Revised:        2016-10-06 17:21:09 +0200 (Thu, 06 Oct 2016)
4 *  Revision:       47343
5 *
6 *  Description:    Defines and prototypes for the AUX Semaphore
7 *
8 *  Copyright (c) 2015 - 2020, Texas Instruments Incorporated
9 *  All rights reserved.
10 *
11 *  Redistribution and use in source and binary forms, with or without
12 *  modification, are permitted provided that the following conditions are met:
13 *
14 *  1) Redistributions of source code must retain the above copyright notice,
15 *     this list of conditions and the following disclaimer.
16 *
17 *  2) Redistributions in binary form must reproduce the above copyright notice,
18 *     this list of conditions and the following disclaimer in the documentation
19 *     and/or other materials provided with the distribution.
20 *
21 *  3) Neither the name of the ORGANIZATION nor the names of its contributors may
22 *     be used to endorse or promote products derived from this software without
23 *     specific prior written permission.
24 *
25 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
29 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 *  POSSIBILITY OF SUCH DAMAGE.
36 *
37 ******************************************************************************/
38 
39 //*****************************************************************************
40 //
41 //! \addtogroup aux_group
42 //! @{
43 //! \addtogroup auxsmph_api
44 //! @{
45 //
46 //*****************************************************************************
47 
48 #ifndef __AUX_SMPH_H__
49 #define __AUX_SMPH_H__
50 
51 //*****************************************************************************
52 //
53 // If building with a C++ compiler, make all of the definitions in this header
54 // have a C binding.
55 //
56 //*****************************************************************************
57 #ifdef __cplusplus
58 extern "C"
59 {
60 #endif
61 
62 #include <stdbool.h>
63 #include <stdint.h>
64 #include "../inc/hw_types.h"
65 #include "../inc/hw_aux_smph.h"
66 #include "../inc/hw_memmap.h"
67 #include "debug.h"
68 
69 //*****************************************************************************
70 //
71 // General constants and defines
72 //
73 //*****************************************************************************
74 #define AUX_SMPH_FREE     0x00000001 // MCU Semaphore has not been claimed
75 #define AUX_SMPH_CLAIMED  0x00000000 // MCU Semaphore has been claimed
76 
77 //*****************************************************************************
78 //
79 // Values that can be passed to AUXSMPHAcquire and AUXSMPHRelease
80 // as the ui32Semaphore parameter.
81 //
82 //*****************************************************************************
83 #define AUX_SMPH_0                0 // AUX Semaphore  0
84 #define AUX_SMPH_1                1 // AUX Semaphore  1
85 #define AUX_SMPH_2                2 // AUX Semaphore  2
86 #define AUX_SMPH_3                3 // AUX Semaphore  3
87 #define AUX_SMPH_4                4 // AUX Semaphore  4
88 #define AUX_SMPH_5                5 // AUX Semaphore  5
89 #define AUX_SMPH_6                6 // AUX Semaphore  6
90 #define AUX_SMPH_7                7 // AUX Semaphore  7
91 
92 //*****************************************************************************
93 //
94 // API Functions and prototypes
95 //
96 //*****************************************************************************
97 
98 //*****************************************************************************
99 //
100 //! \brief Acquire an AUX semaphore.
101 //!
102 //! This function acquires the given AUX semaphore, blocking the call until
103 //! the semaphore is available.
104 //!
105 //! \note The semaphore can also be acquired by the dedicated Sensor Controller.
106 //! The System CPU master can thus be competing for the shared resource, i.e.
107 //! the specified semaphore.
108 //!
109 //! \param ui32Semaphore is the semaphore number.
110 //! - \ref AUX_SMPH_0
111 //! - \ref AUX_SMPH_1
112 //! - \ref AUX_SMPH_2
113 //! - \ref AUX_SMPH_3
114 //! - \ref AUX_SMPH_4
115 //! - \ref AUX_SMPH_5
116 //! - \ref AUX_SMPH_6
117 //! - \ref AUX_SMPH_7
118 //!
119 //! \return None
120 //!
121 //! \sa AUXSMPHTryAcquire(), AUXSMPHRelease()
122 //
123 //*****************************************************************************
124 __STATIC_INLINE void
AUXSMPHAcquire(uint32_t ui32Semaphore)125 AUXSMPHAcquire(uint32_t ui32Semaphore)
126 {
127     // Check the arguments.
128     ASSERT((ui32Semaphore == AUX_SMPH_0) ||
129            (ui32Semaphore == AUX_SMPH_1) ||
130            (ui32Semaphore == AUX_SMPH_2) ||
131            (ui32Semaphore == AUX_SMPH_3) ||
132            (ui32Semaphore == AUX_SMPH_4) ||
133            (ui32Semaphore == AUX_SMPH_5) ||
134            (ui32Semaphore == AUX_SMPH_6) ||
135            (ui32Semaphore == AUX_SMPH_7));
136 
137     // Wait for semaphore to be released such that it can be claimed
138     // Semaphore register reads 1 when lock was acquired otherwise 0
139     // (i.e. AUX_SMPH_CLAIMED).
140     while(HWREG(AUX_SMPH_BASE + AUX_SMPH_O_SMPH0 + 4 * ui32Semaphore) ==
141             AUX_SMPH_CLAIMED)
142     {
143     }
144 }
145 
146 //*****************************************************************************
147 //
148 //! \brief Try to acquire an AUX semaphore.
149 //!
150 //! This function tries to acquire the given AUX semaphore, if the semaphore
151 //! could not be claimed the function returns false.
152 //!
153 //! \note The semaphore can also be acquired by the dedicated Sensor Controller.
154 //! The System CPU master can thus be competing for the shared resource, i.e.
155 //! the specified semaphore.
156 //!
157 //! \param ui32Semaphore is the semaphore number.
158 //! - \ref AUX_SMPH_0
159 //! - \ref AUX_SMPH_1
160 //! - \ref AUX_SMPH_2
161 //! - \ref AUX_SMPH_3
162 //! - \ref AUX_SMPH_4
163 //! - \ref AUX_SMPH_5
164 //! - \ref AUX_SMPH_6
165 //! - \ref AUX_SMPH_7
166 //!
167 //! \return Returns true if semaphore was acquired, false otherwise
168 //!
169 //! \sa AUXSMPHAcquire(), AUXSMPHRelease()
170 //
171 //*****************************************************************************
172 __STATIC_INLINE bool
AUXSMPHTryAcquire(uint32_t ui32Semaphore)173 AUXSMPHTryAcquire(uint32_t ui32Semaphore)
174 {
175     uint32_t ui32SemaReg;
176 
177     // Check the arguments.
178     ASSERT((ui32Semaphore == AUX_SMPH_0) ||
179            (ui32Semaphore == AUX_SMPH_1) ||
180            (ui32Semaphore == AUX_SMPH_2) ||
181            (ui32Semaphore == AUX_SMPH_3) ||
182            (ui32Semaphore == AUX_SMPH_4) ||
183            (ui32Semaphore == AUX_SMPH_5) ||
184            (ui32Semaphore == AUX_SMPH_6) ||
185            (ui32Semaphore == AUX_SMPH_7));
186 
187     // AUX Semaphore register reads 1 if lock was acquired
188     // (i.e. SMPH_FREE when read) but subsequent reads will read 0.
189     ui32SemaReg = HWREG(AUX_SMPH_BASE + AUX_SMPH_O_SMPH0 + 4 * ui32Semaphore);
190 
191     return (ui32SemaReg == AUX_SMPH_FREE);
192 }
193 
194 //*****************************************************************************
195 //
196 //! \brief Release an AUX semaphore by System CPU master.
197 //!
198 //! This function releases the given AUX semaphore.
199 //!
200 //! \note It is up to the application to provide the convention for clearing
201 //! semaphore.
202 //!
203 //! \note The semaphore can also be acquired by the dedicated Sensor Controller.
204 //! The System CPU master can thus be competing for the shared resource, i.e.
205 //! the specified semaphore.
206 //!
207 //! \param ui32Semaphore is the semaphore number.
208 //! - \ref AUX_SMPH_0
209 //! - \ref AUX_SMPH_1
210 //! - \ref AUX_SMPH_2
211 //! - \ref AUX_SMPH_3
212 //! - \ref AUX_SMPH_4
213 //! - \ref AUX_SMPH_5
214 //! - \ref AUX_SMPH_6
215 //! - \ref AUX_SMPH_7
216 //!
217 //! \return None
218 //!
219 //! \sa AUXSMPHAcquire(), AUXSMPHTryAcquire()
220 //
221 //*****************************************************************************
222 __STATIC_INLINE void
AUXSMPHRelease(uint32_t ui32Semaphore)223 AUXSMPHRelease(uint32_t ui32Semaphore)
224 {
225     // Check the arguments.
226     ASSERT((ui32Semaphore == AUX_SMPH_0) ||
227            (ui32Semaphore == AUX_SMPH_1) ||
228            (ui32Semaphore == AUX_SMPH_2) ||
229            (ui32Semaphore == AUX_SMPH_3) ||
230            (ui32Semaphore == AUX_SMPH_4) ||
231            (ui32Semaphore == AUX_SMPH_5) ||
232            (ui32Semaphore == AUX_SMPH_6) ||
233            (ui32Semaphore == AUX_SMPH_7));
234 
235     // No check before release. It is up to the application to provide the
236     // conventions for who and when a semaphore can be released.
237     HWREG(AUX_SMPH_BASE + AUX_SMPH_O_SMPH0 + 4 * ui32Semaphore) =
238         AUX_SMPH_FREE;
239 }
240 
241 //*****************************************************************************
242 //
243 // Mark the end of the C bindings section for C++ compilers.
244 //
245 //*****************************************************************************
246 #ifdef __cplusplus
247 }
248 #endif
249 
250 #endif // __AUX_SMPH_H__
251 
252 //*****************************************************************************
253 //
254 //! Close the Doxygen group.
255 //! @}
256 //! @}
257 //
258 //*****************************************************************************
259