core_cmFunc.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /**************************************************************************//**
  2. * @file core_cmFunc.h
  3. * @brief CMSIS Cortex-M Core Function Access Header File
  4. * @version V3.20
  5. * @date 25. February 2013
  6. *
  7. * @note
  8. *
  9. ******************************************************************************/
  10. /* Copyright (c) 2009 - 2013 ARM LIMITED
  11. All rights reserved.
  12. Redistribution and use in source and binary forms, with or without
  13. modification, are permitted provided that the following conditions are met:
  14. - Redistributions of source code must retain the above copyright
  15. notice, this list of conditions and the following disclaimer.
  16. - Redistributions in binary form must reproduce the above copyright
  17. notice, this list of conditions and the following disclaimer in the
  18. documentation and/or other materials provided with the distribution.
  19. - Neither the name of ARM nor the names of its contributors may be used
  20. 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 COPYRIGHT HOLDERS AND 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. #ifndef __CORE_CMFUNC_H
  36. #define __CORE_CMFUNC_H
  37. /* ########################### Core Function Access ########################### */
  38. /** \ingroup CMSIS_Core_FunctionInterface
  39. \defgroup CMSIS_Core_RegAccFunctions CMSIS Core Register Access Functions
  40. @{
  41. */
  42. #if defined ( __CC_ARM ) /*------------------RealView Compiler -----------------*/
  43. /* ARM armcc specific functions */
  44. #if (__ARMCC_VERSION < 400677)
  45. #error "Please use ARM Compiler Toolchain V4.0.677 or later!"
  46. #endif
  47. /* intrinsic void __enable_irq(); */
  48. /* intrinsic void __disable_irq(); */
  49. /** \brief Get Control Register
  50. This function returns the content of the Control Register.
  51. \return Control Register value
  52. */
  53. __STATIC_INLINE uint32_t __get_CONTROL(void)
  54. {
  55. register uint32_t __regControl __ASM("control");
  56. return(__regControl);
  57. }
  58. /** \brief Set Control Register
  59. This function writes the given value to the Control Register.
  60. \param [in] control Control Register value to set
  61. */
  62. __STATIC_INLINE void __set_CONTROL(uint32_t control)
  63. {
  64. register uint32_t __regControl __ASM("control");
  65. __regControl = control;
  66. }
  67. /** \brief Get IPSR Register
  68. This function returns the content of the IPSR Register.
  69. \return IPSR Register value
  70. */
  71. __STATIC_INLINE uint32_t __get_IPSR(void)
  72. {
  73. register uint32_t __regIPSR __ASM("ipsr");
  74. return(__regIPSR);
  75. }
  76. /** \brief Get APSR Register
  77. This function returns the content of the APSR Register.
  78. \return APSR Register value
  79. */
  80. __STATIC_INLINE uint32_t __get_APSR(void)
  81. {
  82. register uint32_t __regAPSR __ASM("apsr");
  83. return(__regAPSR);
  84. }
  85. /** \brief Get xPSR Register
  86. This function returns the content of the xPSR Register.
  87. \return xPSR Register value
  88. */
  89. __STATIC_INLINE uint32_t __get_xPSR(void)
  90. {
  91. register uint32_t __regXPSR __ASM("xpsr");
  92. return(__regXPSR);
  93. }
  94. /** \brief Get Process Stack Pointer
  95. This function returns the current value of the Process Stack Pointer (PSP).
  96. \return PSP Register value
  97. */
  98. __STATIC_INLINE uint32_t __get_PSP(void)
  99. {
  100. register uint32_t __regProcessStackPointer __ASM("psp");
  101. return(__regProcessStackPointer);
  102. }
  103. /** \brief Set Process Stack Pointer
  104. This function assigns the given value to the Process Stack Pointer (PSP).
  105. \param [in] topOfProcStack Process Stack Pointer value to set
  106. */
  107. __STATIC_INLINE void __set_PSP(uint32_t topOfProcStack)
  108. {
  109. register uint32_t __regProcessStackPointer __ASM("psp");
  110. __regProcessStackPointer = topOfProcStack;
  111. }
  112. /** \brief Get Main Stack Pointer
  113. This function returns the current value of the Main Stack Pointer (MSP).
  114. \return MSP Register value
  115. */
  116. __STATIC_INLINE uint32_t __get_MSP(void)
  117. {
  118. register uint32_t __regMainStackPointer __ASM("msp");
  119. return(__regMainStackPointer);
  120. }
  121. /** \brief Set Main Stack Pointer
  122. This function assigns the given value to the Main Stack Pointer (MSP).
  123. \param [in] topOfMainStack Main Stack Pointer value to set
  124. */
  125. __STATIC_INLINE void __set_MSP(uint32_t topOfMainStack)
  126. {
  127. register uint32_t __regMainStackPointer __ASM("msp");
  128. __regMainStackPointer = topOfMainStack;
  129. }
  130. /** \brief Get Priority Mask
  131. This function returns the current state of the priority mask bit from the Priority Mask Register.
  132. \return Priority Mask value
  133. */
  134. __STATIC_INLINE uint32_t __get_PRIMASK(void)
  135. {
  136. register uint32_t __regPriMask __ASM("primask");
  137. return(__regPriMask);
  138. }
  139. /** \brief Set Priority Mask
  140. This function assigns the given value to the Priority Mask Register.
  141. \param [in] priMask Priority Mask
  142. */
  143. __STATIC_INLINE void __set_PRIMASK(uint32_t priMask)
  144. {
  145. register uint32_t __regPriMask __ASM("primask");
  146. __regPriMask = (priMask);
  147. }
  148. #if (__CORTEX_M >= 0x03)
  149. /** \brief Enable FIQ
  150. This function enables FIQ interrupts by clearing the F-bit in the CPSR.
  151. Can only be executed in Privileged modes.
  152. */
  153. #define __enable_fault_irq __enable_fiq
  154. /** \brief Disable FIQ
  155. This function disables FIQ interrupts by setting the F-bit in the CPSR.
  156. Can only be executed in Privileged modes.
  157. */
  158. #define __disable_fault_irq __disable_fiq
  159. /** \brief Get Base Priority
  160. This function returns the current value of the Base Priority register.
  161. \return Base Priority register value
  162. */
  163. __STATIC_INLINE uint32_t __get_BASEPRI(void)
  164. {
  165. register uint32_t __regBasePri __ASM("basepri");
  166. return(__regBasePri);
  167. }
  168. /** \brief Set Base Priority
  169. This function assigns the given value to the Base Priority register.
  170. \param [in] basePri Base Priority value to set
  171. */
  172. __STATIC_INLINE void __set_BASEPRI(uint32_t basePri)
  173. {
  174. register uint32_t __regBasePri __ASM("basepri");
  175. __regBasePri = (basePri & 0xff);
  176. }
  177. /** \brief Get Fault Mask
  178. This function returns the current value of the Fault Mask register.
  179. \return Fault Mask register value
  180. */
  181. __STATIC_INLINE uint32_t __get_FAULTMASK(void)
  182. {
  183. register uint32_t __regFaultMask __ASM("faultmask");
  184. return(__regFaultMask);
  185. }
  186. /** \brief Set Fault Mask
  187. This function assigns the given value to the Fault Mask register.
  188. \param [in] faultMask Fault Mask value to set
  189. */
  190. __STATIC_INLINE void __set_FAULTMASK(uint32_t faultMask)
  191. {
  192. register uint32_t __regFaultMask __ASM("faultmask");
  193. __regFaultMask = (faultMask & (uint32_t)1);
  194. }
  195. #endif /* (__CORTEX_M >= 0x03) */
  196. #if (__CORTEX_M == 0x04)
  197. /** \brief Get FPSCR
  198. This function returns the current value of the Floating Point Status/Control register.
  199. \return Floating Point Status/Control register value
  200. */
  201. __STATIC_INLINE uint32_t __get_FPSCR(void)
  202. {
  203. #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
  204. register uint32_t __regfpscr __ASM("fpscr");
  205. return(__regfpscr);
  206. #else
  207. return(0);
  208. #endif
  209. }
  210. /** \brief Set FPSCR
  211. This function assigns the given value to the Floating Point Status/Control register.
  212. \param [in] fpscr Floating Point Status/Control value to set
  213. */
  214. __STATIC_INLINE void __set_FPSCR(uint32_t fpscr)
  215. {
  216. #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
  217. register uint32_t __regfpscr __ASM("fpscr");
  218. __regfpscr = (fpscr);
  219. #endif
  220. }
  221. #endif /* (__CORTEX_M == 0x04) */
  222. #elif defined ( __ICCARM__ ) /*------------------ ICC Compiler -------------------*/
  223. /* IAR iccarm specific functions */
  224. #include <cmsis_iar.h>
  225. #elif defined ( __TMS470__ ) /*---------------- TI CCS Compiler ------------------*/
  226. /* TI CCS specific functions */
  227. #include <cmsis_ccs.h>
  228. #elif defined ( __GNUC__ ) /*------------------ GNU Compiler ---------------------*/
  229. /* GNU gcc specific functions */
  230. /** \brief Enable IRQ Interrupts
  231. This function enables IRQ interrupts by clearing the I-bit in the CPSR.
  232. Can only be executed in Privileged modes.
  233. */
  234. __attribute__( ( always_inline ) ) __STATIC_INLINE void __enable_irq(void)
  235. {
  236. __ASM volatile ("cpsie i" : : : "memory");
  237. }
  238. /** \brief Disable IRQ Interrupts
  239. This function disables IRQ interrupts by setting the I-bit in the CPSR.
  240. Can only be executed in Privileged modes.
  241. */
  242. __attribute__( ( always_inline ) ) __STATIC_INLINE void __disable_irq(void)
  243. {
  244. __ASM volatile ("cpsid i" : : : "memory");
  245. }
  246. /** \brief Get Control Register
  247. This function returns the content of the Control Register.
  248. \return Control Register value
  249. */
  250. __attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __get_CONTROL(void)
  251. {
  252. uint32_t result;
  253. __ASM volatile ("MRS %0, control" : "=r" (result) );
  254. return(result);
  255. }
  256. /** \brief Set Control Register
  257. This function writes the given value to the Control Register.
  258. \param [in] control Control Register value to set
  259. */
  260. __attribute__( ( always_inline ) ) __STATIC_INLINE void __set_CONTROL(uint32_t control)
  261. {
  262. __ASM volatile ("MSR control, %0" : : "r" (control) : "memory");
  263. }
  264. /** \brief Get IPSR Register
  265. This function returns the content of the IPSR Register.
  266. \return IPSR Register value
  267. */
  268. __attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __get_IPSR(void)
  269. {
  270. uint32_t result;
  271. __ASM volatile ("MRS %0, ipsr" : "=r" (result) );
  272. return(result);
  273. }
  274. /** \brief Get APSR Register
  275. This function returns the content of the APSR Register.
  276. \return APSR Register value
  277. */
  278. __attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __get_APSR(void)
  279. {
  280. uint32_t result;
  281. __ASM volatile ("MRS %0, apsr" : "=r" (result) );
  282. return(result);
  283. }
  284. /** \brief Get xPSR Register
  285. This function returns the content of the xPSR Register.
  286. \return xPSR Register value
  287. */
  288. __attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __get_xPSR(void)
  289. {
  290. uint32_t result;
  291. __ASM volatile ("MRS %0, xpsr" : "=r" (result) );
  292. return(result);
  293. }
  294. /** \brief Get Process Stack Pointer
  295. This function returns the current value of the Process Stack Pointer (PSP).
  296. \return PSP Register value
  297. */
  298. __attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __get_PSP(void)
  299. {
  300. register uint32_t result;
  301. __ASM volatile ("MRS %0, psp\n" : "=r" (result) );
  302. return(result);
  303. }
  304. /** \brief Set Process Stack Pointer
  305. This function assigns the given value to the Process Stack Pointer (PSP).
  306. \param [in] topOfProcStack Process Stack Pointer value to set
  307. */
  308. __attribute__( ( always_inline ) ) __STATIC_INLINE void __set_PSP(uint32_t topOfProcStack)
  309. {
  310. __ASM volatile ("MSR psp, %0\n" : : "r" (topOfProcStack) : "sp");
  311. }
  312. /** \brief Get Main Stack Pointer
  313. This function returns the current value of the Main Stack Pointer (MSP).
  314. \return MSP Register value
  315. */
  316. __attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __get_MSP(void)
  317. {
  318. register uint32_t result;
  319. __ASM volatile ("MRS %0, msp\n" : "=r" (result) );
  320. return(result);
  321. }
  322. /** \brief Set Main Stack Pointer
  323. This function assigns the given value to the Main Stack Pointer (MSP).
  324. \param [in] topOfMainStack Main Stack Pointer value to set
  325. */
  326. __attribute__( ( always_inline ) ) __STATIC_INLINE void __set_MSP(uint32_t topOfMainStack)
  327. {
  328. __ASM volatile ("MSR msp, %0\n" : : "r" (topOfMainStack) : "sp");
  329. }
  330. /** \brief Get Priority Mask
  331. This function returns the current state of the priority mask bit from the Priority Mask Register.
  332. \return Priority Mask value
  333. */
  334. __attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __get_PRIMASK(void)
  335. {
  336. uint32_t result;
  337. __ASM volatile ("MRS %0, primask" : "=r" (result) );
  338. return(result);
  339. }
  340. /** \brief Set Priority Mask
  341. This function assigns the given value to the Priority Mask Register.
  342. \param [in] priMask Priority Mask
  343. */
  344. __attribute__( ( always_inline ) ) __STATIC_INLINE void __set_PRIMASK(uint32_t priMask)
  345. {
  346. __ASM volatile ("MSR primask, %0" : : "r" (priMask) : "memory");
  347. }
  348. #if (__CORTEX_M >= 0x03)
  349. /** \brief Enable FIQ
  350. This function enables FIQ interrupts by clearing the F-bit in the CPSR.
  351. Can only be executed in Privileged modes.
  352. */
  353. __attribute__( ( always_inline ) ) __STATIC_INLINE void __enable_fault_irq(void)
  354. {
  355. __ASM volatile ("cpsie f" : : : "memory");
  356. }
  357. /** \brief Disable FIQ
  358. This function disables FIQ interrupts by setting the F-bit in the CPSR.
  359. Can only be executed in Privileged modes.
  360. */
  361. __attribute__( ( always_inline ) ) __STATIC_INLINE void __disable_fault_irq(void)
  362. {
  363. __ASM volatile ("cpsid f" : : : "memory");
  364. }
  365. /** \brief Get Base Priority
  366. This function returns the current value of the Base Priority register.
  367. \return Base Priority register value
  368. */
  369. __attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __get_BASEPRI(void)
  370. {
  371. uint32_t result;
  372. __ASM volatile ("MRS %0, basepri_max" : "=r" (result) );
  373. return(result);
  374. }
  375. /** \brief Set Base Priority
  376. This function assigns the given value to the Base Priority register.
  377. \param [in] basePri Base Priority value to set
  378. */
  379. __attribute__( ( always_inline ) ) __STATIC_INLINE void __set_BASEPRI(uint32_t value)
  380. {
  381. __ASM volatile ("MSR basepri, %0" : : "r" (value) : "memory");
  382. }
  383. /** \brief Get Fault Mask
  384. This function returns the current value of the Fault Mask register.
  385. \return Fault Mask register value
  386. */
  387. __attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __get_FAULTMASK(void)
  388. {
  389. uint32_t result;
  390. __ASM volatile ("MRS %0, faultmask" : "=r" (result) );
  391. return(result);
  392. }
  393. /** \brief Set Fault Mask
  394. This function assigns the given value to the Fault Mask register.
  395. \param [in] faultMask Fault Mask value to set
  396. */
  397. __attribute__( ( always_inline ) ) __STATIC_INLINE void __set_FAULTMASK(uint32_t faultMask)
  398. {
  399. __ASM volatile ("MSR faultmask, %0" : : "r" (faultMask) : "memory");
  400. }
  401. #endif /* (__CORTEX_M >= 0x03) */
  402. #if (__CORTEX_M == 0x04)
  403. /** \brief Get FPSCR
  404. This function returns the current value of the Floating Point Status/Control register.
  405. \return Floating Point Status/Control register value
  406. */
  407. __attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __get_FPSCR(void)
  408. {
  409. #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
  410. uint32_t result;
  411. /* Empty asm statement works as a scheduling barrier */
  412. __ASM volatile ("");
  413. __ASM volatile ("VMRS %0, fpscr" : "=r" (result) );
  414. __ASM volatile ("");
  415. return(result);
  416. #else
  417. return(0);
  418. #endif
  419. }
  420. /** \brief Set FPSCR
  421. This function assigns the given value to the Floating Point Status/Control register.
  422. \param [in] fpscr Floating Point Status/Control value to set
  423. */
  424. __attribute__( ( always_inline ) ) __STATIC_INLINE void __set_FPSCR(uint32_t fpscr)
  425. {
  426. #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
  427. /* Empty asm statement works as a scheduling barrier */
  428. __ASM volatile ("");
  429. __ASM volatile ("VMSR fpscr, %0" : : "r" (fpscr) : "vfpcc");
  430. __ASM volatile ("");
  431. #endif
  432. }
  433. #endif /* (__CORTEX_M == 0x04) */
  434. #elif defined ( __TASKING__ ) /*------------------ TASKING Compiler --------------*/
  435. /* TASKING carm specific functions */
  436. /*
  437. * The CMSIS functions have been implemented as intrinsics in the compiler.
  438. * Please use "carm -?i" to get an up to date list of all instrinsics,
  439. * Including the CMSIS ones.
  440. */
  441. #endif
  442. /*@} end of CMSIS_Core_RegAccFunctions */
  443. #endif /* __CORE_CMFUNC_H */