core_cm0.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. /******************************************************************************
  2. * @file: core_cm0.h
  3. * @purpose: CMSIS Cortex-M0 Core Peripheral Access Layer Header File
  4. * @version: V1.10
  5. * @date: 24. Feb. 2009
  6. *----------------------------------------------------------------------------
  7. *
  8. * Copyright (C) 2009 ARM Limited. All rights reserved.
  9. *
  10. * ARM Limited (ARM) is supplying this software for use with Cortex-Mx
  11. * processor based microcontrollers. This file can be freely distributed
  12. * within development tools that are supporting such ARM based processors.
  13. *
  14. * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
  15. * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
  16. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
  17. * ARM SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
  18. * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
  19. *
  20. ******************************************************************************/
  21. #ifndef __CM0_CORE_H__
  22. #define __CM0_CORE_H__
  23. #define __CM0_CMSIS_VERSION_MAIN (0x01) /*!< [31:16] CMSIS HAL main version */
  24. #define __CM0_CMSIS_VERSION_SUB (0x10) /*!< [15:0] CMSIS HAL sub version */
  25. #define __CM0_CMSIS_VERSION ((__CM0_CMSIS_VERSION_MAIN << 16) | __CM0_CMSIS_VERSION_SUB) /*!< CMSIS HAL version number */
  26. #define __CORTEX_M (0x00) /*!< Cortex core */
  27. #include <stdint.h> /* Include standard types */
  28. #if defined (__ICCARM__)
  29. #include <intrinsics.h> /* IAR Intrinsics */
  30. #endif
  31. #ifndef __NVIC_PRIO_BITS
  32. #define __NVIC_PRIO_BITS 2 /*!< standard definition for NVIC Priority Bits */
  33. #endif
  34. /**
  35. * IO definitions
  36. *
  37. * define access restrictions to peripheral registers
  38. */
  39. #define __I volatile const /*!< defines 'read only' permissions */
  40. #define __O volatile /*!< defines 'write only' permissions */
  41. #define __IO volatile /*!< defines 'read / write' permissions */
  42. /*******************************************************************************
  43. * Register Abstraction
  44. ******************************************************************************/
  45. /* System Reset */
  46. #define NVIC_VECTRESET 0 /*!< Vector Reset Bit */
  47. #define NVIC_SYSRESETREQ 2 /*!< System Reset Request */
  48. #define NVIC_AIRCR_VECTKEY (0x5FA << 16) /*!< AIRCR Key for write access */
  49. #define NVIC_AIRCR_ENDIANESS 15 /*!< Endianess */
  50. /* memory mapping struct for Nested Vectored Interrupt Controller (NVIC) */
  51. typedef struct
  52. {
  53. __IO uint32_t ISER[1]; /*!< Interrupt Set Enable Register */
  54. uint32_t RESERVED0[31];
  55. __IO uint32_t ICER[1]; /*!< Interrupt Clear Enable Register */
  56. uint32_t RSERVED1[31];
  57. __IO uint32_t ISPR[1]; /*!< Interrupt Set Pending Register */
  58. uint32_t RESERVED2[31];
  59. __IO uint32_t ICPR[1]; /*!< Interrupt Clear Pending Register */
  60. uint32_t RESERVED3[31];
  61. uint32_t RESERVED4[64];
  62. __IO uint32_t IP[8]; /*!< Interrupt Priority Register */
  63. } NVIC_Type;
  64. /* memory mapping struct for System Control Block */
  65. typedef struct
  66. {
  67. __I uint32_t CPUID; /*!< CPU ID Base Register */
  68. __IO uint32_t ICSR; /*!< Interrupt Control State Register */
  69. uint32_t RESERVED0;
  70. __IO uint32_t AIRCR; /*!< Application Interrupt / Reset Control Register */
  71. __IO uint32_t SCR; /*!< System Control Register */
  72. __IO uint32_t CCR; /*!< Configuration Control Register */
  73. uint32_t RESERVED1;
  74. __IO uint32_t SHP[2]; /*!< System Handlers Priority Registers. [0] is RESERVED */
  75. __IO uint32_t SHCSR; /*!< System Handler Control and State Register */
  76. uint32_t RESERVED2[2];
  77. __IO uint32_t DFSR; /*!< Debug Fault Status Register */
  78. } SCB_Type;
  79. /* memory mapping struct for SysTick */
  80. typedef struct
  81. {
  82. __IO uint32_t CTRL; /*!< SysTick Control and Status Register */
  83. __IO uint32_t LOAD; /*!< SysTick Reload Value Register */
  84. __IO uint32_t VAL; /*!< SysTick Current Value Register */
  85. __I uint32_t CALIB; /*!< SysTick Calibration Register */
  86. } SysTick_Type;
  87. /* Core Debug Register */
  88. typedef struct
  89. {
  90. __IO uint32_t DHCSR; /*!< Debug Halting Control and Status Register */
  91. __O uint32_t DCRSR; /*!< Debug Core Register Selector Register */
  92. __IO uint32_t DCRDR; /*!< Debug Core Register Data Register */
  93. __IO uint32_t DEMCR; /*!< Debug Exception and Monitor Control Register */
  94. } CoreDebug_Type;
  95. /* Memory mapping of Cortex-M0 Hardware */
  96. #define SCS_BASE (0xE000E000) /*!< System Control Space Base Address */
  97. #define CoreDebug_BASE (0xE000EDF0) /*!< Core Debug Base Address */
  98. #define SysTick_BASE (SCS_BASE + 0x0010) /*!< SysTick Base Address */
  99. #define NVIC_BASE (SCS_BASE + 0x0100) /*!< NVIC Base Address */
  100. #define SCB_BASE (SCS_BASE + 0x0D00) /*!< System Control Block Base Address */
  101. #define SCB ((SCB_Type *) SCB_BASE) /*!< SCB configuration struct */
  102. #define SysTick ((SysTick_Type *) SysTick_BASE) /*!< SysTick configuration struct */
  103. #define NVIC ((NVIC_Type *) NVIC_BASE) /*!< NVIC configuration struct */
  104. #define CoreDebug ((CoreDebug_Type *) CoreDebug_BASE) /*!< Core Debug configuration struct */
  105. /*******************************************************************************
  106. * Hardware Abstraction Layer
  107. ******************************************************************************/
  108. #if defined ( __CC_ARM )
  109. #define __ASM __asm /*!< asm keyword for ARM Compiler */
  110. #define __INLINE __inline /*!< inline keyword for ARM Compiler */
  111. #elif defined ( __ICCARM__ )
  112. #define __ASM __asm /*!< asm keyword for IAR Compiler */
  113. #define __INLINE inline /*!< inline keyword for IAR Compiler. Only avaiable in High optimization mode! */
  114. #define __NOP __no_operation /*!< no operation intrinsic in IAR Compiler */
  115. #elif defined ( __GNUC__ )
  116. #define __ASM asm /*!< asm keyword for GNU Compiler */
  117. #define __INLINE inline /*!< inline keyword for GNU Compiler */
  118. #endif
  119. /* ################### Compiler specific Intrinsics ########################### */
  120. #if defined ( __CC_ARM ) /*------------------RealView Compiler -----------------*/
  121. /* ARM armcc specific functions */
  122. #define __enable_fault_irq __enable_fiq
  123. #define __disable_fault_irq __disable_fiq
  124. #define __NOP __nop
  125. #define __WFI __wfi
  126. #define __WFE __wfe
  127. #define __SEV __sev
  128. #define __ISB() __isb(0)
  129. #define __DSB() __dsb(0)
  130. #define __DMB() __dmb(0)
  131. #define __REV __rev
  132. /* intrinsic void __enable_irq(); */
  133. /* intrinsic void __disable_irq(); */
  134. /**
  135. * @brief Return the Process Stack Pointer
  136. *
  137. * @param none
  138. * @return uint32_t ProcessStackPointer
  139. *
  140. * Return the actual process stack pointer
  141. */
  142. extern uint32_t __get_PSP(void);
  143. /**
  144. * @brief Set the Process Stack Pointer
  145. *
  146. * @param uint32_t Process Stack Pointer
  147. * @return none
  148. *
  149. * Assign the value ProcessStackPointer to the MSP
  150. * (process stack pointer) Cortex processor register
  151. */
  152. extern void __set_PSP(uint32_t topOfProcStack);
  153. /**
  154. * @brief Return the Main Stack Pointer
  155. *
  156. * @param none
  157. * @return uint32_t Main Stack Pointer
  158. *
  159. * Return the current value of the MSP (main stack pointer)
  160. * Cortex processor register
  161. */
  162. extern uint32_t __get_MSP(void);
  163. /**
  164. * @brief Set the Main Stack Pointer
  165. *
  166. * @param uint32_t Main Stack Pointer
  167. * @return none
  168. *
  169. * Assign the value mainStackPointer to the MSP
  170. * (main stack pointer) Cortex processor register
  171. */
  172. extern void __set_MSP(uint32_t topOfMainStack);
  173. /**
  174. * @brief Reverse byte order in unsigned short value
  175. *
  176. * @param uint16_t value to reverse
  177. * @return uint32_t reversed value
  178. *
  179. * Reverse byte order in unsigned short value
  180. */
  181. extern uint32_t __REV16(uint16_t value);
  182. /*
  183. * @brief Reverse byte order in signed short value with sign extension to integer
  184. *
  185. * @param int16_t value to reverse
  186. * @return int32_t reversed value
  187. *
  188. * Reverse byte order in signed short value with sign extension to integer
  189. */
  190. extern int32_t __REVSH(int16_t value);
  191. #if (__ARMCC_VERSION < 400000)
  192. /**
  193. * @brief Return the Priority Mask value
  194. *
  195. * @param none
  196. * @return uint32_t PriMask
  197. *
  198. * Return the state of the priority mask bit from the priority mask
  199. * register
  200. */
  201. extern uint32_t __get_PRIMASK(void);
  202. /**
  203. * @brief Set the Priority Mask value
  204. *
  205. * @param uint32_t PriMask
  206. * @return none
  207. *
  208. * Set the priority mask bit in the priority mask register
  209. */
  210. extern void __set_PRIMASK(uint32_t priMask);
  211. /**
  212. * @brief Return the Control Register value
  213. *
  214. * @param none
  215. * @return uint32_t Control value
  216. *
  217. * Return the content of the control register
  218. */
  219. extern uint32_t __get_CONTROL(void);
  220. /**
  221. * @brief Set the Control Register value
  222. *
  223. * @param uint32_t Control value
  224. * @return none
  225. *
  226. * Set the control register
  227. */
  228. extern void __set_CONTROL(uint32_t control);
  229. #else /* (__ARMCC_VERSION >= 400000) */
  230. /**
  231. * @brief Return the Priority Mask value
  232. *
  233. * @param none
  234. * @return uint32_t PriMask
  235. *
  236. * Return the state of the priority mask bit from the priority mask
  237. * register
  238. */
  239. static __INLINE uint32_t __get_PRIMASK(void)
  240. {
  241. register uint32_t __regPriMask __ASM("primask");
  242. return(__regPriMask);
  243. }
  244. /**
  245. * @brief Set the Priority Mask value
  246. *
  247. * @param uint32_t PriMask
  248. * @return none
  249. *
  250. * Set the priority mask bit in the priority mask register
  251. */
  252. static __INLINE void __set_PRIMASK(uint32_t priMask)
  253. {
  254. register uint32_t __regPriMask __ASM("primask");
  255. __regPriMask = (priMask);
  256. }
  257. /**
  258. * @brief Return the Control Register value
  259. *
  260. * @param none
  261. * @return uint32_t Control value
  262. *
  263. * Return the content of the control register
  264. */
  265. static __INLINE uint32_t __get_CONTROL(void)
  266. {
  267. register uint32_t __regControl __ASM("control");
  268. return(__regControl);
  269. }
  270. /**
  271. * @brief Set the Control Register value
  272. *
  273. * @param uint32_t Control value
  274. * @return none
  275. *
  276. * Set the control register
  277. */
  278. static __INLINE void __set_CONTROL(uint32_t control)
  279. {
  280. register uint32_t __regControl __ASM("control");
  281. __regControl = control;
  282. }
  283. #endif /* __ARMCC_VERSION */
  284. #elif (defined (__ICCARM__)) /*------------------ ICC Compiler -------------------*/
  285. /* IAR iccarm specific functions */
  286. #define __enable_irq __enable_interrupt /*!< global Interrupt enable */
  287. #define __disable_irq __disable_interrupt /*!< global Interrupt disable */
  288. static __INLINE void __enable_fault_irq() { __ASM ("cpsie f"); }
  289. static __INLINE void __disable_fault_irq() { __ASM ("cpsid f"); }
  290. static __INLINE void __WFI() { __ASM ("wfi"); }
  291. static __INLINE void __WFE() { __ASM ("wfe"); }
  292. static __INLINE void __SEV() { __ASM ("sev"); }
  293. //static __INLINE void __ISB(arg) { __ASM ("isb"); }
  294. //static __INLINE void __DSB(arg) { __ASM ("dsb"); }
  295. //static __INLINE void __DMB(arg) { __ASM ("dmb"); }
  296. /**
  297. * @brief Return the Process Stack Pointer
  298. *
  299. * @param none
  300. * @return uint32_t ProcessStackPointer
  301. *
  302. * Return the actual process stack pointer
  303. */
  304. extern uint32_t __get_PSP(void);
  305. /**
  306. * @brief Set the Process Stack Pointer
  307. *
  308. * @param uint32_t Process Stack Pointer
  309. * @return none
  310. *
  311. * Assign the value ProcessStackPointer to the MSP
  312. * (process stack pointer) Cortex processor register
  313. */
  314. extern void __set_PSP(uint32_t topOfProcStack);
  315. /**
  316. * @brief Return the Main Stack Pointer
  317. *
  318. * @param none
  319. * @return uint32_t Main Stack Pointer
  320. *
  321. * Return the current value of the MSP (main stack pointer)
  322. * Cortex processor register
  323. */
  324. extern uint32_t __get_MSP(void);
  325. /**
  326. * @brief Set the Main Stack Pointer
  327. *
  328. * @param uint32_t Main Stack Pointer
  329. * @return none
  330. *
  331. * Assign the value mainStackPointer to the MSP
  332. * (main stack pointer) Cortex processor register
  333. */
  334. extern void __set_MSP(uint32_t topOfMainStack);
  335. /**
  336. * @brief Reverse byte order in unsigned short value
  337. *
  338. * @param uint16_t value to reverse
  339. * @return uint32_t reversed value
  340. *
  341. * Reverse byte order in unsigned short value
  342. */
  343. extern uint32_t __REV16(uint16_t value);
  344. /* intrinsic void __set_PRIMASK(); */
  345. /* intrinsic void __get_PRIMASK(); */
  346. /* intrinsic uint32_t __REV(uint32_t value); */
  347. /* intrinsic uint32_t __REVSH(uint32_t value); */
  348. #elif (defined (__GNUC__)) /*------------------ GNU Compiler ---------------------*/
  349. /* GNU gcc specific functions */
  350. static __INLINE void __NOP() { __ASM volatile ("nop"); }
  351. static __INLINE void __enable_irq() { __ASM volatile ("cpsie i"); }
  352. static __INLINE void __disable_irq() { __ASM volatile ("cpsid i"); }
  353. static __INLINE void __enable_fault_irq() { __ASM volatile ("cpsie f"); }
  354. static __INLINE void __disable_fault_irq() { __ASM volatile ("cpsid f"); }
  355. static __INLINE void __WFI() { __ASM volatile ("wfi"); }
  356. static __INLINE void __WFE() { __ASM volatile ("wfe"); }
  357. static __INLINE void __SEV() { __ASM volatile ("sev"); }
  358. static __INLINE void __ISB(arg) { __ASM volatile ("isb"); }
  359. static __INLINE void __DSB(arg) { __ASM volatile ("dsb"); }
  360. static __INLINE void __DMB(arg) { __ASM volatile ("dmb"); }
  361. /**
  362. * @brief Return the Process Stack Pointer
  363. *
  364. * @param none
  365. * @return uint32_t ProcessStackPointer
  366. *
  367. * Return the actual process stack pointer
  368. */
  369. extern uint32_t __get_PSP(void);
  370. /**
  371. * @brief Set the Process Stack Pointer
  372. *
  373. * @param uint32_t Process Stack Pointer
  374. * @return none
  375. *
  376. * Assign the value ProcessStackPointer to the MSP
  377. * (process stack pointer) Cortex processor register
  378. */
  379. extern void __set_PSP(uint32_t topOfProcStack);
  380. /**
  381. * @brief Return the Main Stack Pointer
  382. *
  383. * @param none
  384. * @return uint32_t Main Stack Pointer
  385. *
  386. * Return the current value of the MSP (main stack pointer)
  387. * Cortex processor register
  388. */
  389. extern uint32_t __get_MSP(void);
  390. /**
  391. * @brief Set the Main Stack Pointer
  392. *
  393. * @param uint32_t Main Stack Pointer
  394. * @return none
  395. *
  396. * Assign the value mainStackPointer to the MSP
  397. * (main stack pointer) Cortex processor register
  398. */
  399. extern void __set_MSP(uint32_t topOfMainStack);
  400. /**
  401. * @brief Return the Priority Mask value
  402. *
  403. * @param none
  404. * @return uint32_t PriMask
  405. *
  406. * Return the state of the priority mask bit from the priority mask
  407. * register
  408. */
  409. extern uint32_t __get_PRIMASK(void);
  410. /**
  411. * @brief Set the Priority Mask value
  412. *
  413. * @param uint32_t PriMask
  414. * @return none
  415. *
  416. * Set the priority mask bit in the priority mask register
  417. */
  418. extern void __set_PRIMASK(uint32_t priMask);
  419. /**
  420. * @brief Return the Control Register value
  421. *
  422. * @param none
  423. * @return uint32_t Control value
  424. *
  425. * Return the content of the control register
  426. */
  427. extern uint32_t __get_CONTROL(void);
  428. /**
  429. * @brief Set the Control Register value
  430. *
  431. * @param uint32_t Control value
  432. * @return none
  433. *
  434. * Set the control register
  435. */
  436. extern void __set_CONTROL(uint32_t control);
  437. /**
  438. * @brief Reverse byte order in integer value
  439. *
  440. * @param uint32_t value to reverse
  441. * @return uint32_t reversed value
  442. *
  443. * Reverse byte order in integer value
  444. */
  445. extern uint32_t __REV(uint32_t value);
  446. /**
  447. * @brief Reverse byte order in unsigned short value
  448. *
  449. * @param uint16_t value to reverse
  450. * @return uint32_t reversed value
  451. *
  452. * Reverse byte order in unsigned short value
  453. */
  454. extern uint32_t __REV16(uint16_t value);
  455. /*
  456. * Reverse byte order in signed short value with sign extension to integer
  457. *
  458. * @param int16_t value to reverse
  459. * @return int32_t reversed value
  460. *
  461. * @brief Reverse byte order in signed short value with sign extension to integer
  462. */
  463. extern int32_t __REVSH(int16_t value);
  464. #endif
  465. /* ########################## NVIC functions #################################### */
  466. /* Interrupt Priorities are WORD accessible only under ARMv6M */
  467. /* The following MACROS handle generation of the register offset and byte masks */
  468. #define _BIT_SHIFT(IRQn) ( (((uint32_t)(IRQn) ) & 0x03) * 8 )
  469. #define _SHP_IDX(IRQn) ( ((((uint32_t)(IRQn) & 0x0F)-8) >> 2) )
  470. #define _IP_IDX(IRQn) ( ((uint32_t)(IRQn) >> 2) )
  471. /**
  472. * @brief Set the Priority Grouping in NVIC Interrupt Controller
  473. *
  474. * @param uint32_t priority_grouping is priority grouping field
  475. * @return
  476. *
  477. * Set the priority grouping field using the required unlock sequence.
  478. * The parameter priority_grouping is assigned to the field
  479. * SCB->AIRCR [10:8] PRIGROUP field.
  480. */
  481. static __INLINE void NVIC_SetPriorityGrouping(uint32_t priority_grouping)
  482. {
  483. uint32_t reg_value=0;
  484. reg_value = SCB->AIRCR; /* read old register configuration */
  485. reg_value &= ~((0xFFFFU << 16) | (0x0F << 8)); /* clear bits to change */
  486. reg_value = ((reg_value | NVIC_AIRCR_VECTKEY | (priority_grouping << 8))); /* Insert write key and priorty group */
  487. SCB->AIRCR = reg_value;
  488. }
  489. /**
  490. * @brief Enable Interrupt in NVIC Interrupt Controller
  491. *
  492. * @param IRQn_Type IRQn specifies the interrupt number
  493. * @return none
  494. *
  495. * Enable a device specific interupt in the NVIC interrupt controller.
  496. * The interrupt number cannot be a negative value.
  497. */
  498. static __INLINE void NVIC_EnableIRQ(IRQn_Type IRQn)
  499. {
  500. NVIC->ISER[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F)); /* enable interrupt */
  501. }
  502. /**
  503. * @brief Disable the interrupt line for external interrupt specified
  504. *
  505. * @param IRQn_Type IRQn is the positive number of the external interrupt
  506. * @return none
  507. *
  508. * Disable a device specific interupt in the NVIC interrupt controller.
  509. * The interrupt number cannot be a negative value.
  510. */
  511. static __INLINE void NVIC_DisableIRQ(IRQn_Type IRQn)
  512. {
  513. NVIC->ICER[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F)); /* disable interrupt */
  514. }
  515. /**
  516. * @brief Read the interrupt pending bit for a device specific interrupt source
  517. *
  518. * @param IRQn_Type IRQn is the number of the device specifc interrupt
  519. * @return IRQn_Type Number of pending interrupt or zero
  520. *
  521. * Read the pending register in NVIC and return the number of the
  522. * specified interrupt if its status is pending, otherwise it returns
  523. * zero. The interrupt number cannot be a negative value.
  524. */
  525. static __INLINE IRQn_Type NVIC_GetPendingIRQ(IRQn_Type IRQn)
  526. {
  527. return((IRQn_Type) (NVIC->ISPR[(uint32_t)(IRQn) >> 5] & (1 << ((uint32_t)(IRQn) & 0x1F)))); /* Return Interrupt bit or 'zero' */
  528. }
  529. /**
  530. * @brief Set the pending bit for an external interrupt
  531. *
  532. * @param IRQn_Type IRQn is the Number of the interrupt
  533. * @return none
  534. *
  535. * Set the pending bit for the specified interrupt.
  536. * The interrupt number cannot be a negative value.
  537. */
  538. static __INLINE void NVIC_SetPendingIRQ(IRQn_Type IRQn)
  539. {
  540. NVIC->ISPR[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F)); /* set interrupt pending */
  541. }
  542. /**
  543. * @brief Clear the pending bit for an external interrupt
  544. *
  545. * @param IRQn_Type IRQn is the Number of the interrupt
  546. * @return none
  547. *
  548. * Clear the pending bit for the specified interrupt.
  549. * The interrupt number cannot be a negative value.
  550. */
  551. static __INLINE void NVIC_ClearPendingIRQ(IRQn_Type IRQn)
  552. {
  553. NVIC->ICPR[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F)); /* Clear pending interrupt */
  554. }
  555. /**
  556. * @brief Set the priority for an interrupt
  557. *
  558. * @param IRQn_Type IRQn is the Number of the interrupt
  559. * @param priority is the priority for the interrupt
  560. * @return none
  561. *
  562. * Set the priority for the specified interrupt. The interrupt
  563. * number can be positive to specify an external (device specific)
  564. * interrupt, or negative to specify an internal (core) interrupt. \n
  565. *
  566. * Note: The priority cannot be set for every core interrupt.
  567. */
  568. static __INLINE void NVIC_SetPriority(IRQn_Type IRQn, int32_t priority)
  569. {
  570. if(IRQn < 0) {
  571. SCB->SHP[_SHP_IDX(IRQn)] = (SCB->SHP[_SHP_IDX(IRQn)] & ~(0xFF << _BIT_SHIFT(IRQn))) |
  572. (((priority << (8 - __NVIC_PRIO_BITS)) & 0xFF) << _BIT_SHIFT(IRQn)); }
  573. else {
  574. NVIC->IP[_IP_IDX(IRQn)] = (NVIC->IP[_IP_IDX(IRQn)] & ~(0xFF << _BIT_SHIFT(IRQn))) |
  575. (((priority << (8 - __NVIC_PRIO_BITS)) & 0xFF) << _BIT_SHIFT(IRQn)); }
  576. }
  577. /**
  578. * @brief Read the priority for an interrupt
  579. *
  580. * @param IRQn_Type IRQn is the Number of the interrupt
  581. * @return priority is the priority for the interrupt
  582. *
  583. * Read the priority for the specified interrupt. The interrupt
  584. * number can be positive to specify an external (device specific)
  585. * interrupt, or negative to specify an internal (core) interrupt.
  586. *
  587. * The returned priority value is automatically aligned to the implemented
  588. * priority bits of the microcontroller.
  589. *
  590. * Note: The priority cannot be set for every core interrupt.
  591. */
  592. static __INLINE uint32_t NVIC_GetPriority(IRQn_Type IRQn)
  593. {
  594. if(IRQn < 0) {
  595. return((uint32_t)((SCB->SHP[_SHP_IDX(IRQn)] >> _BIT_SHIFT(IRQn) ) >> (8 - __NVIC_PRIO_BITS))); } /* get priority for Cortex-M0 system interrupts */
  596. else {
  597. return((uint32_t)((NVIC->IP[_IP_IDX(IRQn)] >> _BIT_SHIFT(IRQn) ) >> (8 - __NVIC_PRIO_BITS))); } /* get priority for device specific interrupts */
  598. }
  599. /* ################################## SysTick function ############################################ */
  600. #if (!defined (__Vendor_SysTickConfig)) || (__Vendor_SysTickConfig == 0)
  601. /* SysTick constants */
  602. #define SYSTICK_ENABLE 0 /* Config-Bit to start or stop the SysTick Timer */
  603. #define SYSTICK_TICKINT 1 /* Config-Bit to enable or disable the SysTick interrupt */
  604. #define SYSTICK_CLKSOURCE 2 /* Clocksource has the offset 2 in SysTick Control and Status Register */
  605. #define SYSTICK_MAXCOUNT ((1<<24) -1) /* SysTick MaxCount */
  606. /**
  607. * @brief Initialize and start the SysTick counter and its interrupt.
  608. *
  609. * @param uint32_t ticks is the number of ticks between two interrupts
  610. * @return none
  611. *
  612. * Initialise the system tick timer and its interrupt and start the
  613. * system tick timer / counter in free running mode to generate
  614. * periodical interrupts.
  615. */
  616. static __INLINE uint32_t SysTick_Config(uint32_t ticks)
  617. {
  618. if (ticks > SYSTICK_MAXCOUNT) return (1); /* Reload value impossible */
  619. SysTick->LOAD = (ticks & SYSTICK_MAXCOUNT) - 1; /* set reload register */
  620. NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1); /* set Priority for Cortex-M0 System Interrupts */
  621. SysTick->VAL = (0x00); /* Load the SysTick Counter Value */
  622. SysTick->CTRL = (1 << SYSTICK_CLKSOURCE) | (1<<SYSTICK_ENABLE) | (1<<SYSTICK_TICKINT); /* Enable SysTick IRQ and SysTick Timer */
  623. return (0); /* Function successful */
  624. }
  625. #endif
  626. /* ################################## Reset function ############################################ */
  627. /**
  628. * @brief Initiate a system reset request.
  629. *
  630. * @param none
  631. * @return none
  632. *
  633. * Initialize a system reset request to reset the MCU
  634. */
  635. static __INLINE void NVIC_SystemReset(void)
  636. {
  637. SCB->AIRCR = (NVIC_AIRCR_VECTKEY | (1<<NVIC_SYSRESETREQ)); /* Keep priority group unchanged */
  638. }
  639. #endif