cpu.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /******************************************************************************
  2. * Filename: cpu.c
  3. * Revised: $Date: 2013-01-21 15:25:21 +0100 (Mon, 21 Jan 2013) $
  4. * Revision: $Revision: 9178 $
  5. *
  6. * Description: Instruction wrappers for special CPU instructions needed by
  7. * the drivers.
  8. *
  9. * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
  10. *
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in the
  21. * documentation and/or other materials provided with the distribution.
  22. *
  23. * Neither the name of Texas Instruments Incorporated nor the names of
  24. * its contributors may be used to endorse or promote products derived
  25. * from this software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  30. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  31. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  34. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  35. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  36. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. ******************************************************************************/
  40. #include "cpu.h"
  41. //*****************************************************************************
  42. //
  43. // Wrapper function for the CPSID instruction. Returns the state of PRIMASK
  44. // on entry.
  45. //
  46. //*****************************************************************************
  47. #if defined(__GNUC__)
  48. uint32_t __attribute__((naked))
  49. CPUcpsid(void)
  50. {
  51. uint32_t ui32Ret;
  52. //
  53. // Read PRIMASK and disable interrupts.
  54. //
  55. __asm(" mrs r0, PRIMASK\n"
  56. " cpsid i\n"
  57. " bx lr\n"
  58. : "=r" (ui32Ret));
  59. //
  60. // The return is handled in the inline assembly, but the compiler will
  61. // still complain if there is not an explicit return here (despite the fact
  62. // that this does not result in any code being produced because of the
  63. // naked attribute).
  64. //
  65. return(ui32Ret);
  66. }
  67. #endif
  68. #if (__ICCARM__)
  69. uint32_t
  70. CPUcpsid(void)
  71. {
  72. //
  73. // Read PRIMASK and disable interrupts.
  74. //
  75. __asm(" mrs r0, PRIMASK\n"
  76. " cpsid i\n");
  77. //
  78. // "Warning[Pe940]: missing return statement at end of non-void function"
  79. // is suppressed here to avoid putting a "bx lr" in the inline assembly
  80. // above and a superfluous return statement here.
  81. //
  82. #pragma diag_suppress=Pe940
  83. }
  84. #pragma diag_default=Pe940
  85. #endif
  86. #if defined(__KEIL__) || defined(__ARMCC_VERSION)
  87. __asm uint32_t
  88. CPUcpsid(void)
  89. {
  90. //
  91. // Read PRIMASK and disable interrupts.
  92. //
  93. mrs r0, PRIMASK;
  94. cpsid i;
  95. bx lr
  96. }
  97. #endif
  98. #if defined(__TI_COMPILER_VERSION__)
  99. uint32_t
  100. CPUcpsid(void)
  101. {
  102. //
  103. // Read PRIMASK and disable interrupts.
  104. //
  105. __asm(" mrs r0, PRIMASK\n"
  106. " cpsid i\n"
  107. " bx lr\n");
  108. //
  109. // The following keeps the compiler happy, because it wants to see a
  110. // return value from this function. It will generate code to return
  111. // a zero. However, the real return is the "bx lr" above, so the
  112. // return(0) is never executed and the function returns with the value
  113. // you expect in R0.
  114. //
  115. return(0);
  116. }
  117. #endif
  118. //*****************************************************************************
  119. //
  120. // Wrapper function returning the state of PRIMASK (indicating whether
  121. // interrupts are enabled or disabled).
  122. //
  123. //*****************************************************************************
  124. #if defined(__GNUC__)
  125. uint32_t __attribute__((naked))
  126. CPUprimask(void)
  127. {
  128. uint32_t ui32Ret;
  129. //
  130. // Read PRIMASK and disable interrupts.
  131. //
  132. __asm(" mrs r0, PRIMASK\n"
  133. " bx lr\n"
  134. : "=r" (ui32Ret));
  135. //
  136. // The return is handled in the inline assembly, but the compiler will
  137. // still complain if there is not an explicit return here (despite the fact
  138. // that this does not result in any code being produced because of the
  139. // naked attribute).
  140. //
  141. return(ui32Ret);
  142. }
  143. #endif
  144. #if (__ICCARM__)
  145. uint32_t
  146. CPUprimask(void)
  147. {
  148. //
  149. // Read PRIMASK and disable interrupts.
  150. //
  151. __asm(" mrs r0, PRIMASK\n");
  152. //
  153. // "Warning[Pe940]: missing return statement at end of non-void function"
  154. // is suppressed here to avoid putting a "bx lr" in the inline assembly
  155. // above and a superfluous return statement here.
  156. //
  157. #pragma diag_suppress=Pe940
  158. }
  159. #pragma diag_default=Pe940
  160. #endif
  161. #if defined(__KEIL__) || defined(__ARMCC_VERSION)
  162. __asm uint32_t
  163. CPUprimask(void)
  164. {
  165. //
  166. // Read PRIMASK and disable interrupts.
  167. //
  168. mrs r0, PRIMASK;
  169. bx lr
  170. }
  171. #endif
  172. #if defined(__TI_COMPILER_VERSION__)
  173. uint32_t
  174. CPUprimask(void)
  175. {
  176. //
  177. // Read PRIMASK and disable interrupts.
  178. //
  179. __asm(" mrs r0, PRIMASK\n"
  180. " bx lr\n");
  181. //
  182. // The following keeps the compiler happy, because it wants to see a
  183. // return value from this function. It will generate code to return
  184. // a zero. However, the real return is the "bx lr" above, so the
  185. // return(0) is never executed and the function returns with the value
  186. // you expect in R0.
  187. //
  188. return(0);
  189. }
  190. #endif
  191. //*****************************************************************************
  192. //
  193. // Wrapper function for the CPSIE instruction. Returns the state of PRIMASK
  194. // on entry.
  195. //
  196. //*****************************************************************************
  197. #if defined(__GNUC__)
  198. uint32_t __attribute__((naked))
  199. CPUcpsie(void)
  200. {
  201. uint32_t ui32Ret;
  202. //
  203. // Read PRIMASK and enable interrupts.
  204. //
  205. __asm(" mrs r0, PRIMASK\n"
  206. " cpsie i\n"
  207. " bx lr\n"
  208. : "=r" (ui32Ret));
  209. //
  210. // The return is handled in the inline assembly, but the compiler will
  211. // still complain if there is not an explicit return here (despite the fact
  212. // that this does not result in any code being produced because of the
  213. // naked attribute).
  214. //
  215. return(ui32Ret);
  216. }
  217. #endif
  218. #if (__ICCARM__)
  219. uint32_t
  220. CPUcpsie(void)
  221. {
  222. //
  223. // Read PRIMASK and enable interrupts.
  224. //
  225. __asm(" mrs r0, PRIMASK\n"
  226. " cpsie i\n");
  227. //
  228. // "Warning[Pe940]: missing return statement at end of non-void function"
  229. // is suppressed here to avoid putting a "bx lr" in the inline assembly
  230. // above and a superfluous return statement here.
  231. //
  232. #pragma diag_suppress=Pe940
  233. }
  234. #pragma diag_default=Pe940
  235. #endif
  236. #if defined(__KEIL__) || defined(__ARMCC_VERSION)
  237. __asm uint32_t
  238. CPUcpsie(void)
  239. {
  240. //
  241. // Read PRIMASK and enable interrupts.
  242. //
  243. mrs r0, PRIMASK;
  244. cpsie i;
  245. bx lr
  246. }
  247. #endif
  248. #if defined(__TI_COMPILER_VERSION__)
  249. uint32_t
  250. CPUcpsie(void)
  251. {
  252. //
  253. // Read PRIMASK and enable interrupts.
  254. //
  255. __asm(" mrs r0, PRIMASK\n"
  256. " cpsie i\n"
  257. " bx lr\n");
  258. //
  259. // The following keeps the compiler happy, because it wants to see a
  260. // return value from this function. It will generate code to return
  261. // a zero. However, the real return is the "bx lr" above, so the
  262. // return(0) is never executed and the function returns with the value
  263. // you expect in R0.
  264. //
  265. return(0);
  266. }
  267. #endif
  268. //*****************************************************************************
  269. //
  270. // Wrapper function for the WFI instruction.
  271. //
  272. //*****************************************************************************
  273. #if defined(__GNUC__)
  274. void __attribute__((naked))
  275. CPUwfi(void)
  276. {
  277. //
  278. // Wait for the next interrupt.
  279. //
  280. __asm(" wfi\n"
  281. " bx lr\n");
  282. }
  283. #endif
  284. #if (__ICCARM__)
  285. void
  286. CPUwfi(void)
  287. {
  288. //
  289. // Wait for the next interrupt.
  290. //
  291. __asm(" wfi\n");
  292. }
  293. #endif
  294. #if defined(__KEIL__) || defined(__ARMCC_VERSION)
  295. __asm void
  296. CPUwfi(void)
  297. {
  298. //
  299. // Wait for the next interrupt.
  300. //
  301. wfi;
  302. bx lr
  303. }
  304. #endif
  305. #if defined(__TI_COMPILER_VERSION__)
  306. void
  307. CPUwfi(void)
  308. {
  309. //
  310. // Wait for the next interrupt.
  311. //
  312. __asm(" wfi\n");
  313. }
  314. #endif
  315. //*****************************************************************************
  316. //
  317. // Wrapper function for the WFE instruction.
  318. //
  319. //*****************************************************************************
  320. #if defined(__GNUC__)
  321. void __attribute__((naked))
  322. CPUwfe(void)
  323. {
  324. //
  325. // Wait for the next event
  326. //
  327. __asm(" wfe\n"
  328. " bx lr\n");
  329. }
  330. #endif
  331. #if (__ICCARM__)
  332. void
  333. CPUwfe(void)
  334. {
  335. //
  336. // Wait for the next event
  337. //
  338. __asm(" wfe\n");
  339. }
  340. #endif
  341. #if defined(__KEIL__) || defined(__ARMCC_VERSION)
  342. __asm void
  343. CPUwfe(void)
  344. {
  345. //
  346. // Wait for the next event
  347. //
  348. wfe;
  349. bx lr
  350. }
  351. #endif
  352. #if defined(__TI_COMPILER_VERSION__)
  353. void
  354. CPUwfe(void)
  355. {
  356. //
  357. // Wait for the next event
  358. //
  359. __asm(" wfe\n");
  360. }
  361. #endif
  362. //*****************************************************************************
  363. //
  364. // Wrapper function for the SEV instruction (Send event).
  365. //
  366. //*****************************************************************************
  367. #if defined(__GNUC__)
  368. void __attribute__((naked))
  369. CPUsev(void)
  370. {
  371. //
  372. // Send event
  373. //
  374. __asm(" sev\n"
  375. " bx lr\n");
  376. }
  377. #endif
  378. #if (__ICCARM__)
  379. void
  380. CPUsev(void)
  381. {
  382. //
  383. // Send event
  384. //
  385. __asm(" sev\n");
  386. }
  387. #endif
  388. #if defined(__KEIL__) || defined(__ARMCC_VERSION)
  389. __asm void
  390. CPUsev(void)
  391. {
  392. //
  393. // Send event
  394. //
  395. sev;
  396. bx lr
  397. }
  398. #endif
  399. #if defined(__TI_COMPILER_VERSION__)
  400. void
  401. CPUsev(void)
  402. {
  403. //
  404. // Send event
  405. //
  406. __asm(" sev\n");
  407. }
  408. #endif
  409. //*****************************************************************************
  410. //
  411. // Wrapper function for writing the BASEPRI register.
  412. //
  413. //*****************************************************************************
  414. #if defined(__GNUC__)
  415. void __attribute__((naked))
  416. CPUbasepriSet(uint32_t ui32NewBasepri)
  417. {
  418. //
  419. // Set the BASEPRI register
  420. //
  421. __asm(" msr BASEPRI, r0\n"
  422. " bx lr\n");
  423. }
  424. #endif
  425. #if (__ICCARM__)
  426. void
  427. CPUbasepriSet(uint32_t ui32NewBasepri)
  428. {
  429. //
  430. // Set the BASEPRI register
  431. //
  432. __asm(" msr BASEPRI, r0\n");
  433. }
  434. #endif
  435. #if defined(__KEIL__) || defined(__ARMCC_VERSION)
  436. __asm void
  437. CPUbasepriSet(uint32_t ui32NewBasepri)
  438. {
  439. //
  440. // Set the BASEPRI register
  441. //
  442. msr BASEPRI, r0;
  443. bx lr
  444. }
  445. #endif
  446. #if defined(__TI_COMPILER_VERSION__)
  447. void
  448. CPUbasepriSet(uint32_t ui32NewBasepri)
  449. {
  450. //
  451. // Set the BASEPRI register
  452. //
  453. __asm(" msr BASEPRI, r0\n");
  454. }
  455. #endif
  456. //*****************************************************************************
  457. //
  458. // Wrapper function for reading the BASEPRI register.
  459. //
  460. //*****************************************************************************
  461. #if defined(__GNUC__)
  462. uint32_t __attribute__((naked))
  463. CPUbasepriGet(void)
  464. {
  465. uint32_t ui32Ret;
  466. //
  467. // Read BASEPRI
  468. //
  469. __asm(" mrs r0, BASEPRI\n"
  470. " bx lr\n"
  471. : "=r" (ui32Ret));
  472. //
  473. // The return is handled in the inline assembly, but the compiler will
  474. // still complain if there is not an explicit return here (despite the fact
  475. // that this does not result in any code being produced because of the
  476. // naked attribute).
  477. //
  478. return(ui32Ret);
  479. }
  480. #endif
  481. #if (__ICCARM__)
  482. uint32_t
  483. CPUbasepriGet(void)
  484. {
  485. //
  486. // Read BASEPRI
  487. //
  488. __asm(" mrs r0, BASEPRI\n");
  489. //
  490. // "Warning[Pe940]: missing return statement at end of non-void function"
  491. // is suppressed here to avoid putting a "bx lr" in the inline assembly
  492. // above and a superfluous return statement here.
  493. //
  494. #pragma diag_suppress=Pe940
  495. }
  496. #pragma diag_default=Pe940
  497. #endif
  498. #if defined(rvmdk) || defined(__ARMCC_VERSION)
  499. __asm uint32_t
  500. CPUbasepriGet(void)
  501. {
  502. //
  503. // Read BASEPRI
  504. //
  505. mrs r0, BASEPRI;
  506. bx lr
  507. }
  508. #endif
  509. #if defined(__TI_COMPILER_VERSION__)
  510. uint32_t
  511. CPUbasepriGet(void)
  512. {
  513. //
  514. // Read BASEPRI
  515. //
  516. __asm(" mrs r0, BASEPRI\n"
  517. " bx lr\n");
  518. //
  519. // The following keeps the compiler happy, because it wants to see a
  520. // return value from this function. It will generate code to return
  521. // a zero. However, the real return is the "bx lr" above, so the
  522. // return(0) is never executed and the function returns with the value
  523. // you expect in R0.
  524. //
  525. return(0);
  526. }
  527. #endif