PageRenderTime 27ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/FreeRTOS/Demo/CORTEX_M4_SimpleLink_CC3220SF_CCS/ti/devices/cc32xx/driverlib/hwspinlock.c

https://gitlab.com/21mece13/FreeRTOS
C | 274 lines | 88 code | 24 blank | 162 comment | 7 complexity | 6db119c776b4125368395d9e60fa1a3e MD5 | raw file
  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. //! \addtogroup HwSpinLock_api
  47. //! @{
  48. //
  49. //*****************************************************************************
  50. #include <stdbool.h>
  51. #include <stdint.h>
  52. #include "inc/hw_types.h"
  53. #include "inc/hw_memmap.h"
  54. #include "inc/hw_ints.h"
  55. #include "inc/hw_common_reg.h"
  56. #include "hwspinlock.h"
  57. //*****************************************************************************
  58. // Global semaphore register list
  59. //*****************************************************************************
  60. static const uint32_t HwSpinLock_RegLst[]=
  61. {
  62. COMMON_REG_BASE + COMMON_REG_O_SPI_Properties_Register
  63. };
  64. //*****************************************************************************
  65. //
  66. //! Acquire specified spin lock.
  67. //!
  68. //! \param ui32LockID is one of the valid spin lock.
  69. //!
  70. //! This function acquires specified spin lock and will not retun util the
  71. //! specified lock is acquired.
  72. //!
  73. //! The parameter \e ui32LockID should \b HWSPINLOCK_MCSPIS0.
  74. //!
  75. //! return None.
  76. //
  77. //*****************************************************************************
  78. void HwSpinLockAcquire(uint32_t ui32LockID)
  79. {
  80. uint32_t ui32BitPos;
  81. uint32_t ui32SemVal;
  82. uint32_t ui32RegAddr;
  83. //
  84. // Extract the bit position from the
  85. // LockID
  86. //
  87. ui32BitPos = ((ui32LockID >> 16) & 0x0FFF);
  88. ui32RegAddr = HwSpinLock_RegLst[ui32LockID & 0xF];
  89. //
  90. // Set the corresponding
  91. // ownership bits to 'b01
  92. //
  93. ui32SemVal = (0xFFFFFFFF ^ (0x2 << ui32BitPos));
  94. //
  95. // Retry untill we succeed
  96. //
  97. do
  98. {
  99. HWREG(ui32RegAddr) = ui32SemVal;
  100. }
  101. while( !(HWREG(ui32RegAddr) & (1 << ui32BitPos )) );
  102. }
  103. //*****************************************************************************
  104. //
  105. //! Try to acquire specified spin lock.
  106. //!
  107. //! \param ui32LockID is one of the valid spin lock.
  108. //! \param ui32Retry is the number of reties.
  109. //!
  110. //! This function tries acquire specified spin lock in \e ui32Retry retries.
  111. //!
  112. //! The parameter \e ui32Retry can be any value between 0 and 2^32.
  113. //!
  114. //! return Returns 0 on success, -1 otherwise.
  115. //
  116. //*****************************************************************************
  117. int32_t HwSpinLockTryAcquire(uint32_t ui32LockID, uint32_t ui32Retry)
  118. {
  119. uint32_t ui32BitPos;
  120. uint32_t ui32SemVal;
  121. uint32_t ui32RegAddr;
  122. //
  123. // Extract the bit position from the
  124. // LockID
  125. //
  126. ui32BitPos = ((ui32LockID >> 16) & 0x0FFF);
  127. ui32RegAddr = HwSpinLock_RegLst[ui32LockID & 0xF];
  128. //
  129. // Set the corresponding
  130. // ownership bits to 'b01
  131. //
  132. ui32SemVal = (0xFFFFFFFF ^ (0x2 << ui32BitPos));
  133. //
  134. // Check for 0 retry.
  135. //
  136. if(ui32Retry == 0)
  137. {
  138. ui32Retry = 1;
  139. }
  140. //
  141. // Retry the number of times specified
  142. //
  143. do
  144. {
  145. HWREG(ui32RegAddr) = ui32SemVal;
  146. ui32Retry--;
  147. }
  148. while( !(HWREG(ui32RegAddr) & (1 << ui32BitPos )) && ui32Retry );
  149. //
  150. // Check the semaphore status
  151. //
  152. if(HWREG(ui32RegAddr) & (1 << ui32BitPos ))
  153. {
  154. return 0;
  155. }
  156. else
  157. {
  158. return -1;
  159. }
  160. }
  161. //*****************************************************************************
  162. //
  163. //! Release a previously owned spin lock
  164. //!
  165. //! \param ui32LockID is one of the valid spin lock.
  166. //!
  167. //! This function releases previously owned spin lock.
  168. //!
  169. //! \return None.
  170. //
  171. //*****************************************************************************
  172. void HwSpinLockRelease(uint32_t ui32LockID)
  173. {
  174. uint32_t ui32BitPos;
  175. uint32_t ui32SemVal;
  176. //
  177. // Extract the bit position from the
  178. // lock id.
  179. //
  180. ui32BitPos = ((ui32LockID >> 16) & 0x00FF);
  181. //
  182. // Release the spin lock, only if already owned
  183. //
  184. if(HWREG(HwSpinLock_RegLst[ui32LockID & 0xF]) & (1 << ui32BitPos ))
  185. {
  186. ui32SemVal = (0xFFFFFFFF & ~(0x3 << ui32BitPos));
  187. HWREG(HwSpinLock_RegLst[ui32LockID & 0xF]) = ui32SemVal;
  188. }
  189. }
  190. //*****************************************************************************
  191. //
  192. //! Get the current or previous ownership status.
  193. //!
  194. //! \param ui32LockID is one of the valid spin lock.
  195. //! \param bCurrentStatus is \b true for current status, \b flase otherwise
  196. //!
  197. //! This function gets the current or previous ownership status of the
  198. //! specified spin lock based on \e bCurrentStatus parameter.
  199. //!
  200. //! \return Returns \b HWSPINLOCK_OWNER_APPS, \b HWSPINLOCK_OWNER_NWP or
  201. //! \b HWSPINLOCK_OWNER_NONE.
  202. //
  203. //*****************************************************************************
  204. uint32_t HwSpinLockTest(uint32_t ui32LockID, bool bCurrentStatus)
  205. {
  206. uint32_t ui32BitPos;
  207. uint32_t ui32SemVal;
  208. if(bCurrentStatus)
  209. {
  210. //
  211. // Extract the bit position from the
  212. // lock id.
  213. //
  214. ui32BitPos = ((ui32LockID >> 16) & 0x00FF);
  215. //
  216. // return semaphore
  217. //
  218. return((HWREG(HwSpinLock_RegLst[ui32LockID & 0xF]) >> ui32BitPos ) & 0x3 );
  219. }
  220. else
  221. {
  222. //
  223. // Extract the bit position
  224. //
  225. ui32BitPos = ((ui32LockID >> 24) & 0xFF);
  226. //
  227. // Identify which register to read
  228. //
  229. if(ui32LockID & 0xF > 4)
  230. {
  231. ui32SemVal = ((HWREG(COMMON_REG_BASE +
  232. COMMON_REG_O_SEMAPHORE_PREV_OWNER1) >> ui32BitPos ) & 0x3);
  233. }
  234. else
  235. {
  236. ui32SemVal = ((HWREG(COMMON_REG_BASE +
  237. COMMON_REG_O_SEMAPHORE_PREV_OWNER2) >> ui32BitPos ) & 0x3);
  238. }
  239. //
  240. // return the owner
  241. //
  242. return ui32SemVal;
  243. }
  244. }
  245. //*****************************************************************************
  246. //
  247. // Close the Doxygen group.
  248. //! @}
  249. //
  250. //*****************************************************************************