A bundled STM32F10x Std Periph and CMSIS library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

830 lines
16 KiB

  1. /******************************************************************************
  2. * @file: core_cm3.c
  3. * @purpose: CMSIS Cortex-M3 Core Peripheral Access Layer Source File
  4. * @version: V1.20
  5. * @date: 22. May 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. #include <stdint.h>
  22. /* define compiler specific symbols */
  23. #if defined ( __CC_ARM )
  24. #define __ASM __asm /*!< asm keyword for armcc */
  25. #define __INLINE __inline /*!< inline keyword for armcc */
  26. #elif defined ( __ICCARM__ )
  27. #define __ASM __asm /*!< asm keyword for iarcc */
  28. #define __INLINE inline /*!< inline keyword for iarcc. Only avaiable in High optimization mode! */
  29. #elif defined ( __GNUC__ )
  30. #define __ASM __asm /*!< asm keyword for gcc */
  31. #define __INLINE inline /*!< inline keyword for gcc */
  32. #elif defined ( __TASKING__ )
  33. #define __ASM __asm /*!< asm keyword for TASKING Compiler */
  34. #define __INLINE inline /*!< inline keyword for TASKING Compiler */
  35. #endif
  36. #if defined ( __CC_ARM ) /*------------------RealView Compiler -----------------*/
  37. /**
  38. * @brief Return the Process Stack Pointer
  39. *
  40. * @param none
  41. * @return uint32_t ProcessStackPointer
  42. *
  43. * Return the actual process stack pointer
  44. */
  45. __ASM uint32_t __get_PSP(void)
  46. {
  47. mrs r0, psp
  48. bx lr
  49. }
  50. /**
  51. * @brief Set the Process Stack Pointer
  52. *
  53. * @param uint32_t Process Stack Pointer
  54. * @return none
  55. *
  56. * Assign the value ProcessStackPointer to the MSP
  57. * (process stack pointer) Cortex processor register
  58. */
  59. __ASM void __set_PSP(uint32_t topOfProcStack)
  60. {
  61. msr psp, r0
  62. bx lr
  63. }
  64. /**
  65. * @brief Return the Main Stack Pointer
  66. *
  67. * @param none
  68. * @return uint32_t Main Stack Pointer
  69. *
  70. * Return the current value of the MSP (main stack pointer)
  71. * Cortex processor register
  72. */
  73. __ASM uint32_t __get_MSP(void)
  74. {
  75. mrs r0, msp
  76. bx lr
  77. }
  78. /**
  79. * @brief Set the Main Stack Pointer
  80. *
  81. * @param uint32_t Main Stack Pointer
  82. * @return none
  83. *
  84. * Assign the value mainStackPointer to the MSP
  85. * (main stack pointer) Cortex processor register
  86. */
  87. __ASM void __set_MSP(uint32_t mainStackPointer)
  88. {
  89. msr msp, r0
  90. bx lr
  91. }
  92. /**
  93. * @brief Reverse byte order in unsigned short value
  94. *
  95. * @param uint16_t value to reverse
  96. * @return uint32_t reversed value
  97. *
  98. * Reverse byte order in unsigned short value
  99. */
  100. __ASM uint32_t __REV16(uint16_t value)
  101. {
  102. rev16 r0, r0
  103. bx lr
  104. }
  105. /**
  106. * @brief Reverse byte order in signed short value with sign extension to integer
  107. *
  108. * @param int16_t value to reverse
  109. * @return int32_t reversed value
  110. *
  111. * Reverse byte order in signed short value with sign extension to integer
  112. */
  113. __ASM int32_t __REVSH(int16_t value)
  114. {
  115. revsh r0, r0
  116. bx lr
  117. }
  118. #if (__ARMCC_VERSION < 400000)
  119. /**
  120. * @brief Remove the exclusive lock created by ldrex
  121. *
  122. * @param none
  123. * @return none
  124. *
  125. * Removes the exclusive lock which is created by ldrex.
  126. */
  127. __ASM void __CLREX(void)
  128. {
  129. clrex
  130. }
  131. /**
  132. * @brief Return the Base Priority value
  133. *
  134. * @param none
  135. * @return uint32_t BasePriority
  136. *
  137. * Return the content of the base priority register
  138. */
  139. __ASM uint32_t __get_BASEPRI(void)
  140. {
  141. mrs r0, basepri
  142. bx lr
  143. }
  144. /**
  145. * @brief Set the Base Priority value
  146. *
  147. * @param uint32_t BasePriority
  148. * @return none
  149. *
  150. * Set the base priority register
  151. */
  152. __ASM void __set_BASEPRI(uint32_t basePri)
  153. {
  154. msr basepri, r0
  155. bx lr
  156. }
  157. /**
  158. * @brief Return the Priority Mask value
  159. *
  160. * @param none
  161. * @return uint32_t PriMask
  162. *
  163. * Return the state of the priority mask bit from the priority mask
  164. * register
  165. */
  166. __ASM uint32_t __get_PRIMASK(void)
  167. {
  168. mrs r0, primask
  169. bx lr
  170. }
  171. /**
  172. * @brief Set the Priority Mask value
  173. *
  174. * @param uint32_t PriMask
  175. * @return none
  176. *
  177. * Set the priority mask bit in the priority mask register
  178. */
  179. __ASM void __set_PRIMASK(uint32_t priMask)
  180. {
  181. msr primask, r0
  182. bx lr
  183. }
  184. /**
  185. * @brief Return the Fault Mask value
  186. *
  187. * @param none
  188. * @return uint32_t FaultMask
  189. *
  190. * Return the content of the fault mask register
  191. */
  192. __ASM uint32_t __get_FAULTMASK(void)
  193. {
  194. mrs r0, faultmask
  195. bx lr
  196. }
  197. /**
  198. * @brief Set the Fault Mask value
  199. *
  200. * @param uint32_t faultMask value
  201. * @return none
  202. *
  203. * Set the fault mask register
  204. */
  205. __ASM void __set_FAULTMASK(uint32_t faultMask)
  206. {
  207. msr faultmask, r0
  208. bx lr
  209. }
  210. /**
  211. * @brief Return the Control Register value
  212. *
  213. * @param none
  214. * @return uint32_t Control value
  215. *
  216. * Return the content of the control register
  217. */
  218. __ASM uint32_t __get_CONTROL(void)
  219. {
  220. mrs r0, control
  221. bx lr
  222. }
  223. /**
  224. * @brief Set the Control Register value
  225. *
  226. * @param uint32_t Control value
  227. * @return none
  228. *
  229. * Set the control register
  230. */
  231. __ASM void __set_CONTROL(uint32_t control)
  232. {
  233. msr control, r0
  234. bx lr
  235. }
  236. #endif /* __ARMCC_VERSION */
  237. #elif (defined (__ICCARM__)) /*------------------ ICC Compiler -------------------*/
  238. #pragma diag_suppress=Pe940
  239. /**
  240. * @brief Return the Process Stack Pointer
  241. *
  242. * @param none
  243. * @return uint32_t ProcessStackPointer
  244. *
  245. * Return the actual process stack pointer
  246. */
  247. uint32_t __get_PSP(void)
  248. {
  249. __ASM("mrs r0, psp");
  250. __ASM("bx lr");
  251. }
  252. /**
  253. * @brief Set the Process Stack Pointer
  254. *
  255. * @param uint32_t Process Stack Pointer
  256. * @return none
  257. *
  258. * Assign the value ProcessStackPointer to the MSP
  259. * (process stack pointer) Cortex processor register
  260. */
  261. void __set_PSP(uint32_t topOfProcStack)
  262. {
  263. __ASM("msr psp, r0");
  264. __ASM("bx lr");
  265. }
  266. /**
  267. * @brief Return the Main Stack Pointer
  268. *
  269. * @param none
  270. * @return uint32_t Main Stack Pointer
  271. *
  272. * Return the current value of the MSP (main stack pointer)
  273. * Cortex processor register
  274. */
  275. uint32_t __get_MSP(void)
  276. {
  277. __ASM("mrs r0, msp");
  278. __ASM("bx lr");
  279. }
  280. /**
  281. * @brief Set the Main Stack Pointer
  282. *
  283. * @param uint32_t Main Stack Pointer
  284. * @return none
  285. *
  286. * Assign the value mainStackPointer to the MSP
  287. * (main stack pointer) Cortex processor register
  288. */
  289. void __set_MSP(uint32_t topOfMainStack)
  290. {
  291. __ASM("msr msp, r0");
  292. __ASM("bx lr");
  293. }
  294. /**
  295. * @brief Reverse byte order in unsigned short value
  296. *
  297. * @param uint16_t value to reverse
  298. * @return uint32_t reversed value
  299. *
  300. * Reverse byte order in unsigned short value
  301. */
  302. uint32_t __REV16(uint16_t value)
  303. {
  304. __ASM("rev16 r0, r0");
  305. __ASM("bx lr");
  306. }
  307. /**
  308. * @brief Reverse bit order of value
  309. *
  310. * @param uint32_t value to reverse
  311. * @return uint32_t reversed value
  312. *
  313. * Reverse bit order of value
  314. */
  315. uint32_t __RBIT(uint32_t value)
  316. {
  317. __ASM("rbit r0, r0");
  318. __ASM("bx lr");
  319. }
  320. /**
  321. * @brief LDR Exclusive
  322. *
  323. * @param uint8_t* address
  324. * @return uint8_t value of (*address)
  325. *
  326. * Exclusive LDR command
  327. */
  328. uint8_t __LDREXB(uint8_t *addr)
  329. {
  330. __ASM("ldrexb r0, [r0]");
  331. __ASM("bx lr");
  332. }
  333. /**
  334. * @brief LDR Exclusive
  335. *
  336. * @param uint16_t* address
  337. * @return uint16_t value of (*address)
  338. *
  339. * Exclusive LDR command
  340. */
  341. uint16_t __LDREXH(uint16_t *addr)
  342. {
  343. __ASM("ldrexh r0, [r0]");
  344. __ASM("bx lr");
  345. }
  346. /**
  347. * @brief LDR Exclusive
  348. *
  349. * @param uint32_t* address
  350. * @return uint32_t value of (*address)
  351. *
  352. * Exclusive LDR command
  353. */
  354. uint32_t __LDREXW(uint32_t *addr)
  355. {
  356. __ASM("ldrex r0, [r0]");
  357. __ASM("bx lr");
  358. }
  359. /**
  360. * @brief STR Exclusive
  361. *
  362. * @param uint8_t *address
  363. * @param uint8_t value to store
  364. * @return uint32_t successful / failed
  365. *
  366. * Exclusive STR command
  367. */
  368. uint32_t __STREXB(uint8_t value, uint8_t *addr)
  369. {
  370. __ASM("strexb r0, r0, [r1]");
  371. __ASM("bx lr");
  372. }
  373. /**
  374. * @brief STR Exclusive
  375. *
  376. * @param uint16_t *address
  377. * @param uint16_t value to store
  378. * @return uint32_t successful / failed
  379. *
  380. * Exclusive STR command
  381. */
  382. uint32_t __STREXH(uint16_t value, uint16_t *addr)
  383. {
  384. __ASM("strexh r0, r0, [r1]");
  385. __ASM("bx lr");
  386. }
  387. /**
  388. * @brief STR Exclusive
  389. *
  390. * @param uint32_t *address
  391. * @param uint32_t value to store
  392. * @return uint32_t successful / failed
  393. *
  394. * Exclusive STR command
  395. */
  396. uint32_t __STREXW(uint32_t value, uint32_t *addr)
  397. {
  398. __ASM("strex r0, r0, [r1]");
  399. __ASM("bx lr");
  400. }
  401. #pragma diag_default=Pe940
  402. #elif (defined (__GNUC__)) /*------------------ GNU Compiler ---------------------*/
  403. /**
  404. * @brief Return the Process Stack Pointer
  405. *
  406. * @param none
  407. * @return uint32_t ProcessStackPointer
  408. *
  409. * Return the actual process stack pointer
  410. */
  411. uint32_t __get_PSP(void) __attribute__( ( naked ) );
  412. uint32_t __get_PSP(void)
  413. {
  414. uint32_t result=0;
  415. __ASM volatile ("MRS %0, psp\n\t"
  416. "MOV r0, %0 \n\t"
  417. "BX lr \n\t" : "=r" (result) );
  418. return(result);
  419. }
  420. /**
  421. * @brief Set the Process Stack Pointer
  422. *
  423. * @param uint32_t Process Stack Pointer
  424. * @return none
  425. *
  426. * Assign the value ProcessStackPointer to the MSP
  427. * (process stack pointer) Cortex processor register
  428. */
  429. void __set_PSP(uint32_t topOfProcStack) __attribute__( ( naked ) );
  430. void __set_PSP(uint32_t topOfProcStack)
  431. {
  432. __ASM volatile ("MSR psp, %0\n\t"
  433. "BX lr \n\t" : : "r" (topOfProcStack) );
  434. }
  435. /**
  436. * @brief Return the Main Stack Pointer
  437. *
  438. * @param none
  439. * @return uint32_t Main Stack Pointer
  440. *
  441. * Return the current value of the MSP (main stack pointer)
  442. * Cortex processor register
  443. */
  444. uint32_t __get_MSP(void) __attribute__( ( naked ) );
  445. uint32_t __get_MSP(void)
  446. {
  447. uint32_t result=0;
  448. __ASM volatile ("MRS %0, msp\n\t"
  449. "MOV r0, %0 \n\t"
  450. "BX lr \n\t" : "=r" (result) );
  451. return(result);
  452. }
  453. /**
  454. * @brief Set the Main Stack Pointer
  455. *
  456. * @param uint32_t Main Stack Pointer
  457. * @return none
  458. *
  459. * Assign the value mainStackPointer to the MSP
  460. * (main stack pointer) Cortex processor register
  461. */
  462. void __set_MSP(uint32_t topOfMainStack) __attribute__( ( naked ) );
  463. void __set_MSP(uint32_t topOfMainStack)
  464. {
  465. __ASM volatile ("MSR msp, %0\n\t"
  466. "BX lr \n\t" : : "r" (topOfMainStack) );
  467. }
  468. /**
  469. * @brief Return the Base Priority value
  470. *
  471. * @param none
  472. * @return uint32_t BasePriority
  473. *
  474. * Return the content of the base priority register
  475. */
  476. uint32_t __get_BASEPRI(void)
  477. {
  478. uint32_t result=0;
  479. __ASM volatile ("MRS %0, basepri_max" : "=r" (result) );
  480. return(result);
  481. }
  482. /**
  483. * @brief Set the Base Priority value
  484. *
  485. * @param uint32_t BasePriority
  486. * @return none
  487. *
  488. * Set the base priority register
  489. */
  490. void __set_BASEPRI(uint32_t value)
  491. {
  492. __ASM volatile ("MSR basepri, %0" : : "r" (value) );
  493. }
  494. /**
  495. * @brief Return the Priority Mask value
  496. *
  497. * @param none
  498. * @return uint32_t PriMask
  499. *
  500. * Return the state of the priority mask bit from the priority mask
  501. * register
  502. */
  503. uint32_t __get_PRIMASK(void)
  504. {
  505. uint32_t result=0;
  506. __ASM volatile ("MRS %0, primask" : "=r" (result) );
  507. return(result);
  508. }
  509. /**
  510. * @brief Set the Priority Mask value
  511. *
  512. * @param uint32_t PriMask
  513. * @return none
  514. *
  515. * Set the priority mask bit in the priority mask register
  516. */
  517. void __set_PRIMASK(uint32_t priMask)
  518. {
  519. __ASM volatile ("MSR primask, %0" : : "r" (priMask) );
  520. }
  521. /**
  522. * @brief Return the Fault Mask value
  523. *
  524. * @param none
  525. * @return uint32_t FaultMask
  526. *
  527. * Return the content of the fault mask register
  528. */
  529. uint32_t __get_FAULTMASK(void)
  530. {
  531. uint32_t result=0;
  532. __ASM volatile ("MRS %0, faultmask" : "=r" (result) );
  533. return(result);
  534. }
  535. /**
  536. * @brief Set the Fault Mask value
  537. *
  538. * @param uint32_t faultMask value
  539. * @return none
  540. *
  541. * Set the fault mask register
  542. */
  543. void __set_FAULTMASK(uint32_t faultMask)
  544. {
  545. __ASM volatile ("MSR faultmask, %0" : : "r" (faultMask) );
  546. }
  547. /**
  548. * @brief Reverse byte order in integer value
  549. *
  550. * @param uint32_t value to reverse
  551. * @return uint32_t reversed value
  552. *
  553. * Reverse byte order in integer value
  554. */
  555. uint32_t __REV(uint32_t value)
  556. {
  557. uint32_t result=0;
  558. __ASM volatile ("rev %0, %1" : "=r" (result) : "r" (value) );
  559. return(result);
  560. }
  561. /**
  562. * @brief Reverse byte order in unsigned short value
  563. *
  564. * @param uint16_t value to reverse
  565. * @return uint32_t reversed value
  566. *
  567. * Reverse byte order in unsigned short value
  568. */
  569. uint32_t __REV16(uint16_t value)
  570. {
  571. uint32_t result=0;
  572. __ASM volatile ("rev16 %0, %1" : "=r" (result) : "r" (value) );
  573. return(result);
  574. }
  575. /**
  576. * @brief Reverse byte order in signed short value with sign extension to integer
  577. *
  578. * @param int32_t value to reverse
  579. * @return int32_t reversed value
  580. *
  581. * Reverse byte order in signed short value with sign extension to integer
  582. */
  583. int32_t __REVSH(int16_t value)
  584. {
  585. uint32_t result=0;
  586. __ASM volatile ("revsh %0, %1" : "=r" (result) : "r" (value) );
  587. return(result);
  588. }
  589. /**
  590. * @brief Reverse bit order of value
  591. *
  592. * @param uint32_t value to reverse
  593. * @return uint32_t reversed value
  594. *
  595. * Reverse bit order of value
  596. */
  597. uint32_t __RBIT(uint32_t value)
  598. {
  599. uint32_t result=0;
  600. __ASM volatile ("rbit %0, %1" : "=r" (result) : "r" (value) );
  601. return(result);
  602. }
  603. /**
  604. * @brief LDR Exclusive
  605. *
  606. * @param uint8_t* address
  607. * @return uint8_t value of (*address)
  608. *
  609. * Exclusive LDR command
  610. */
  611. uint8_t __LDREXB(uint8_t *addr)
  612. {
  613. uint8_t result=0;
  614. __ASM volatile ("ldrexb %0, [%1]" : "=r" (result) : "r" (addr) );
  615. return(result);
  616. }
  617. /**
  618. * @brief LDR Exclusive
  619. *
  620. * @param uint16_t* address
  621. * @return uint16_t value of (*address)
  622. *
  623. * Exclusive LDR command
  624. */
  625. uint16_t __LDREXH(uint16_t *addr)
  626. {
  627. uint16_t result=0;
  628. __ASM volatile ("ldrexh %0, [%1]" : "=r" (result) : "r" (addr) );
  629. return(result);
  630. }
  631. /**
  632. * @brief LDR Exclusive
  633. *
  634. * @param uint32_t* address
  635. * @return uint32_t value of (*address)
  636. *
  637. * Exclusive LDR command
  638. */
  639. uint32_t __LDREXW(uint32_t *addr)
  640. {
  641. uint32_t result=0;
  642. __ASM volatile ("ldrex %0, [%1]" : "=r" (result) : "r" (addr) );
  643. return(result);
  644. }
  645. /**
  646. * @brief STR Exclusive
  647. *
  648. * @param uint8_t *address
  649. * @param uint8_t value to store
  650. * @return uint32_t successful / failed
  651. *
  652. * Exclusive STR command
  653. */
  654. uint32_t __STREXB(uint8_t value, uint8_t *addr)
  655. {
  656. uint32_t result=0;
  657. __ASM volatile ("strexb %0, %2, [%1]" : "=&r" (result) : "r" (addr), "r" (value) );
  658. return(result);
  659. }
  660. /**
  661. * @brief STR Exclusive
  662. *
  663. * @param uint16_t *address
  664. * @param uint16_t value to store
  665. * @return uint32_t successful / failed
  666. *
  667. * Exclusive STR command
  668. */
  669. uint32_t __STREXH(uint16_t value, uint16_t *addr)
  670. {
  671. uint32_t result=0;
  672. __ASM volatile ("strexh %0, %2, [%1]" : "=&r" (result) : "r" (addr), "r" (value) );
  673. return(result);
  674. }
  675. /**
  676. * @brief STR Exclusive
  677. *
  678. * @param uint32_t *address
  679. * @param uint32_t value to store
  680. * @return uint32_t successful / failed
  681. *
  682. * Exclusive STR command
  683. */
  684. uint32_t __STREXW(uint32_t value, uint32_t *addr)
  685. {
  686. uint32_t result=0;
  687. __ASM volatile ("strex %0, %2, [%1]" : "=r" (result) : "r" (addr), "r" (value) );
  688. return(result);
  689. }
  690. /**
  691. * @brief Return the Control Register value
  692. *
  693. * @param none
  694. * @return uint32_t Control value
  695. *
  696. * Return the content of the control register
  697. */
  698. uint32_t __get_CONTROL(void)
  699. {
  700. uint32_t result=0;
  701. __ASM volatile ("MRS %0, control" : "=r" (result) );
  702. return(result);
  703. }
  704. /**
  705. * @brief Set the Control Register value
  706. *
  707. * @param uint32_t Control value
  708. * @return none
  709. *
  710. * Set the control register
  711. */
  712. void __set_CONTROL(uint32_t control)
  713. {
  714. __ASM volatile ("MSR control, %0" : : "r" (control) );
  715. }
  716. #elif (defined (__TASKING__)) /*------------------ TASKING Compiler ---------------------*/
  717. /* TASKING carm specific functions */
  718. /*
  719. * The CMSIS functions have been implemented as intrinsics in the compiler.
  720. * Please use "carm -?i" to get an up to date list of all instrinsics,
  721. * Including the CMSIS ones.
  722. */
  723. #endif