]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4_SimpleLink_CC3220SF_CCS/ti/devices/cc32xx/driverlib/hwspinlock.c
Add SimpleLink CC3220SF demo.
[freertos] / FreeRTOS / Demo / CORTEX_M4_SimpleLink_CC3220SF_CCS / ti / devices / cc32xx / driverlib / hwspinlock.c
1 /*
2  * -------------------------------------------
3  *    CC3220 SDK - v0.10.00.00 
4  * -------------------------------------------
5  *
6  *  Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/ 
7  *  
8  *  Redistribution and use in source and binary forms, with or without 
9  *  modification, are permitted provided that the following conditions 
10  *  are met:
11  *
12  *    Redistributions of source code must retain the above copyright 
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  *    Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the 
17  *    documentation and/or other materials provided with the   
18  *    distribution.
19  *
20  *    Neither the name of Texas Instruments Incorporated nor the names of
21  *    its contributors may be used to endorse or promote products derived
22  *    from this software without specific prior written permission.
23  *
24  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
25  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
26  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
28  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
29  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
30  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
33  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
34  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  *  
36  */
37 //*****************************************************************************
38 //
39 //  hwspinlock.c
40 //
41 //  Driver for the Apps-NWP spinlock
42 //
43 //*****************************************************************************
44
45 //*****************************************************************************
46 //
47 //! \addtogroup HwSpinLock_api
48 //! @{
49 //
50 //*****************************************************************************
51
52 #include <stdbool.h>
53 #include <stdint.h>
54 #include "inc/hw_types.h"
55 #include "inc/hw_memmap.h"
56 #include "inc/hw_ints.h"
57 #include "inc/hw_common_reg.h"
58 #include "hwspinlock.h"
59
60 //*****************************************************************************
61 // Global semaphore register list
62 //*****************************************************************************
63 static const uint32_t HwSpinLock_RegLst[]=
64 {
65     COMMON_REG_BASE + COMMON_REG_O_SPI_Properties_Register
66 };
67
68 //*****************************************************************************
69 //
70 //! Acquire specified spin lock.
71 //!
72 //! \param ui32LockID is one of the valid spin lock.
73 //!
74 //! This function acquires specified spin lock and will not retun util the
75 //! specified lock is acquired.
76 //!
77 //! The parameter \e ui32LockID should \b HWSPINLOCK_MCSPIS0.
78 //!
79 //! return None.
80 //
81 //*****************************************************************************
82 void HwSpinLockAcquire(uint32_t ui32LockID)
83 {
84   uint32_t ui32BitPos;
85   uint32_t ui32SemVal;
86   uint32_t ui32RegAddr;
87
88   //
89   // Extract the bit position from the
90   // LockID
91   //
92   ui32BitPos  = ((ui32LockID >> 16) & 0x0FFF);
93   ui32RegAddr = HwSpinLock_RegLst[ui32LockID & 0xF];
94
95   //
96   // Set the corresponding
97   // ownership bits to 'b01
98   //
99   ui32SemVal = (0xFFFFFFFF ^ (0x2 << ui32BitPos));
100
101   //
102   // Retry untill we succeed
103   //
104   do
105   {
106     HWREG(ui32RegAddr) = ui32SemVal;
107   }
108   while( !(HWREG(ui32RegAddr) & (1 << ui32BitPos )) );
109
110 }
111
112 //*****************************************************************************
113 //
114 //! Try to acquire specified spin lock.
115 //!
116 //! \param ui32LockID is one of the valid spin lock.
117 //! \param ui32Retry is the number of reties.
118 //!
119 //! This function tries acquire specified spin lock in \e ui32Retry retries.
120 //!
121 //! The parameter \e ui32Retry can be any value between 0 and 2^32.
122 //!
123 //! return Returns 0 on success, -1 otherwise.
124 //
125 //*****************************************************************************
126 int32_t HwSpinLockTryAcquire(uint32_t ui32LockID, uint32_t ui32Retry)
127 {
128   uint32_t ui32BitPos;
129   uint32_t ui32SemVal;
130   uint32_t ui32RegAddr;
131
132   //
133   // Extract the bit position from the
134   // LockID
135   //
136   ui32BitPos  = ((ui32LockID >> 16) & 0x0FFF);
137   ui32RegAddr = HwSpinLock_RegLst[ui32LockID & 0xF];
138
139   //
140   // Set the corresponding
141   // ownership bits to 'b01
142   //
143   ui32SemVal = (0xFFFFFFFF ^ (0x2 << ui32BitPos));
144
145   //
146   // Check for 0 retry.
147   //
148   if(ui32Retry == 0)
149   {
150     ui32Retry = 1;
151   }
152
153   //
154   // Retry the number of times specified
155   //
156   do
157   {
158     HWREG(ui32RegAddr) = ui32SemVal;
159     ui32Retry--;
160   }
161   while( !(HWREG(ui32RegAddr) & (1 << ui32BitPos )) && ui32Retry );
162
163
164   //
165   // Check the semaphore status
166   //
167   if(HWREG(ui32RegAddr) & (1 << ui32BitPos ))
168   {
169     return 0;
170   }
171   else
172   {
173     return -1;
174   }
175 }
176
177 //*****************************************************************************
178 //
179 //! Release a previously owned spin lock
180 //!
181 //! \param ui32LockID is one of the valid spin lock.
182 //!
183 //! This function releases previously owned spin lock.
184 //!
185 //! \return None.
186 //
187 //*****************************************************************************
188 void HwSpinLockRelease(uint32_t ui32LockID)
189 {
190   uint32_t ui32BitPos;
191   uint32_t ui32SemVal;
192
193   //
194   // Extract the bit position from the
195   // lock id.
196   //
197   ui32BitPos = ((ui32LockID >> 16) & 0x00FF);
198
199   //
200   // Release the spin lock, only if already owned
201   //
202   if(HWREG(HwSpinLock_RegLst[ui32LockID & 0xF]) & (1 << ui32BitPos ))
203   {
204     ui32SemVal = (0xFFFFFFFF & ~(0x3 << ui32BitPos));
205     HWREG(HwSpinLock_RegLst[ui32LockID & 0xF]) = ui32SemVal;
206   }
207 }
208
209 //*****************************************************************************
210 //
211 //! Get the current or previous ownership status.
212 //!
213 //! \param ui32LockID is one of the valid spin lock.
214 //! \param bCurrentStatus is \b true for current status, \b flase otherwise
215 //!
216 //! This function gets the current or previous ownership status of the
217 //! specified spin lock based on \e bCurrentStatus parameter.
218 //!
219 //! \return Returns \b HWSPINLOCK_OWNER_APPS, \b HWSPINLOCK_OWNER_NWP or
220 //! \b HWSPINLOCK_OWNER_NONE.
221 //
222 //*****************************************************************************
223 uint32_t HwSpinLockTest(uint32_t ui32LockID, bool bCurrentStatus)
224 {
225   uint32_t ui32BitPos;
226   uint32_t ui32SemVal;
227
228   if(bCurrentStatus)
229   {
230     //
231     // Extract the bit position from the
232     // lock id.
233     //
234     ui32BitPos = ((ui32LockID >> 16) & 0x00FF);
235
236     //
237     // return semaphore
238     //
239     return((HWREG(HwSpinLock_RegLst[ui32LockID & 0xF]) >> ui32BitPos ) & 0x3 );
240   }
241   else
242   {
243     //
244     // Extract the bit position
245     //
246     ui32BitPos = ((ui32LockID >> 24) & 0xFF);
247
248     //
249     // Identify which register to read
250     //
251     if(ui32LockID & 0xF > 4)
252     {
253       ui32SemVal = ((HWREG(COMMON_REG_BASE +
254                      COMMON_REG_O_SEMAPHORE_PREV_OWNER1) >> ui32BitPos ) & 0x3);
255     }
256     else
257     {
258       ui32SemVal = ((HWREG(COMMON_REG_BASE +
259                      COMMON_REG_O_SEMAPHORE_PREV_OWNER2) >> ui32BitPos ) & 0x3);
260     }
261
262     //
263     // return the owner
264     //
265     return ui32SemVal;
266   }
267 }
268
269 //*****************************************************************************
270 //
271 // Close the Doxygen group.
272 //! @}
273 //
274 //*****************************************************************************