Keyboard firmwares for Atmel AVR and Cortex-M
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.

rt_HAL_CM.h 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*----------------------------------------------------------------------------
  2. * RL-ARM - RTX
  3. *----------------------------------------------------------------------------
  4. * Name: RT_HAL_CM.H
  5. * Purpose: Hardware Abstraction Layer for Cortex-M definitions
  6. * Rev.: V4.60
  7. *----------------------------------------------------------------------------
  8. *
  9. * Copyright (c) 1999-2009 KEIL, 2009-2012 ARM Germany GmbH
  10. * All rights reserved.
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. * - Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  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 distribution.
  18. * - Neither the name of ARM nor the names of its contributors may be used
  19. * to endorse or promote products derived from this software without
  20. * specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *---------------------------------------------------------------------------*/
  34. /* Definitions */
  35. #define INITIAL_xPSR 0x01000000
  36. #define DEMCR_TRCENA 0x01000000
  37. #define ITM_ITMENA 0x00000001
  38. #define MAGIC_WORD 0xE25A2EA5
  39. #if defined (__CC_ARM) /* ARM Compiler */
  40. #if ((__TARGET_ARCH_7_M || __TARGET_ARCH_7E_M) && !NO_EXCLUSIVE_ACCESS)
  41. #define __USE_EXCLUSIVE_ACCESS
  42. #else
  43. #undef __USE_EXCLUSIVE_ACCESS
  44. #endif
  45. #elif defined (__GNUC__) /* GNU Compiler */
  46. #undef __USE_EXCLUSIVE_ACCESS
  47. #if defined (__CORTEX_M0) || defined (__CORTEX_M0PLUS)
  48. #define __TARGET_ARCH_6S_M 1
  49. #else
  50. #define __TARGET_ARCH_6S_M 0
  51. #endif
  52. #if defined (__VFP_FP__) && !defined(__SOFTFP__)
  53. #define __TARGET_FPU_VFP 1
  54. #else
  55. #define __TARGET_FPU_VFP 0
  56. #endif
  57. #define __inline inline
  58. #define __weak __attribute__((weak))
  59. #ifndef __CMSIS_GENERIC
  60. __attribute__((always_inline)) static inline void __enable_irq(void)
  61. {
  62. __asm volatile ("cpsie i");
  63. }
  64. __attribute__((always_inline)) static inline U32 __disable_irq(void)
  65. {
  66. U32 result;
  67. __asm volatile ("mrs %0, primask" : "=r" (result));
  68. __asm volatile ("cpsid i");
  69. return(result & 1);
  70. }
  71. #endif
  72. __attribute__(( always_inline)) static inline U8 __clz(U32 value)
  73. {
  74. U8 result;
  75. __asm volatile ("clz %0, %1" : "=r" (result) : "r" (value));
  76. return(result);
  77. }
  78. #elif defined (__ICCARM__) /* IAR Compiler */
  79. #undef __USE_EXCLUSIVE_ACCESS
  80. #if (__CORE__ == __ARM6M__)
  81. #define __TARGET_ARCH_6S_M 1
  82. #else
  83. #define __TARGET_ARCH_6S_M 0
  84. #endif
  85. #if defined __ARMVFP__
  86. #define __TARGET_FPU_VFP 1
  87. #else
  88. #define __TARGET_FPU_VFP 0
  89. #endif
  90. #define __inline inline
  91. #ifndef __CMSIS_GENERIC
  92. static inline void __enable_irq(void)
  93. {
  94. __asm volatile ("cpsie i");
  95. }
  96. static inline U32 __disable_irq(void)
  97. {
  98. U32 result;
  99. __asm volatile ("mrs %0, primask" : "=r" (result));
  100. __asm volatile ("cpsid i");
  101. return(result & 1);
  102. }
  103. #endif
  104. static inline U8 __clz(U32 value)
  105. {
  106. U8 result;
  107. __asm volatile ("clz %0, %1" : "=r" (result) : "r" (value));
  108. return(result);
  109. }
  110. #endif
  111. /* NVIC registers */
  112. #define NVIC_ST_CTRL (*((volatile U32 *)0xE000E010))
  113. #define NVIC_ST_RELOAD (*((volatile U32 *)0xE000E014))
  114. #define NVIC_ST_CURRENT (*((volatile U32 *)0xE000E018))
  115. #define NVIC_ISER ((volatile U32 *)0xE000E100)
  116. #define NVIC_ICER ((volatile U32 *)0xE000E180)
  117. #if (__TARGET_ARCH_6S_M)
  118. #define NVIC_IP ((volatile U32 *)0xE000E400)
  119. #else
  120. #define NVIC_IP ((volatile U8 *)0xE000E400)
  121. #endif
  122. #define NVIC_INT_CTRL (*((volatile U32 *)0xE000ED04))
  123. #define NVIC_AIR_CTRL (*((volatile U32 *)0xE000ED0C))
  124. #define NVIC_SYS_PRI2 (*((volatile U32 *)0xE000ED1C))
  125. #define NVIC_SYS_PRI3 (*((volatile U32 *)0xE000ED20))
  126. #define OS_PEND_IRQ() NVIC_INT_CTRL = (1<<28)
  127. #define OS_PENDING ((NVIC_INT_CTRL >> 26) & (1<<2 | 1))
  128. #define OS_UNPEND(fl) NVIC_INT_CTRL = (*fl = OS_PENDING) << 25
  129. #define OS_PEND(fl,p) NVIC_INT_CTRL = (fl | p<<2) << 26
  130. #define OS_LOCK() NVIC_ST_CTRL = 0x0005
  131. #define OS_UNLOCK() NVIC_ST_CTRL = 0x0007
  132. #define OS_X_PENDING ((NVIC_INT_CTRL >> 28) & 1)
  133. #define OS_X_UNPEND(fl) NVIC_INT_CTRL = (*fl = OS_X_PENDING) << 27
  134. #define OS_X_PEND(fl,p) NVIC_INT_CTRL = (fl | p) << 28
  135. #if (__TARGET_ARCH_6S_M)
  136. #define OS_X_INIT(n) NVIC_IP[n>>2] |= 0xFF << (8*(n & 0x03)); \
  137. NVIC_ISER[n>>5] = 1 << (n & 0x1F)
  138. #else
  139. #define OS_X_INIT(n) NVIC_IP[n] = 0xFF; \
  140. NVIC_ISER[n>>5] = 1 << (n & 0x1F)
  141. #endif
  142. #define OS_X_LOCK(n) NVIC_ICER[n>>5] = 1 << (n & 0x1F)
  143. #define OS_X_UNLOCK(n) NVIC_ISER[n>>5] = 1 << (n & 0x1F)
  144. /* Core Debug registers */
  145. #define DEMCR (*((volatile U32 *)0xE000EDFC))
  146. /* ITM registers */
  147. #define ITM_CONTROL (*((volatile U32 *)0xE0000E80))
  148. #define ITM_ENABLE (*((volatile U32 *)0xE0000E00))
  149. #define ITM_PORT30_U32 (*((volatile U32 *)0xE0000078))
  150. #define ITM_PORT31_U32 (*((volatile U32 *)0xE000007C))
  151. #define ITM_PORT31_U16 (*((volatile U16 *)0xE000007C))
  152. #define ITM_PORT31_U8 (*((volatile U8 *)0xE000007C))
  153. /* Variables */
  154. extern BIT dbg_msg;
  155. /* Functions */
  156. #ifdef __USE_EXCLUSIVE_ACCESS
  157. #define rt_inc(p) while(__strex((__ldrex(p)+1),p))
  158. #define rt_dec(p) while(__strex((__ldrex(p)-1),p))
  159. #else
  160. #define rt_inc(p) __disable_irq();(*p)++;__enable_irq();
  161. #define rt_dec(p) __disable_irq();(*p)--;__enable_irq();
  162. #endif
  163. __inline static U32 rt_inc_qi (U32 size, U8 *count, U8 *first) {
  164. U32 cnt,c2;
  165. #ifdef __USE_EXCLUSIVE_ACCESS
  166. do {
  167. if ((cnt = __ldrex(count)) == size) {
  168. __clrex();
  169. return (cnt); }
  170. } while (__strex(cnt+1, count));
  171. do {
  172. c2 = (cnt = __ldrex(first)) + 1;
  173. if (c2 == size) c2 = 0;
  174. } while (__strex(c2, first));
  175. #else
  176. __disable_irq();
  177. if ((cnt = *count) < size) {
  178. *count = cnt+1;
  179. c2 = (cnt = *first) + 1;
  180. if (c2 == size) c2 = 0;
  181. *first = c2;
  182. }
  183. __enable_irq ();
  184. #endif
  185. return (cnt);
  186. }
  187. __inline static void rt_systick_init (void) {
  188. NVIC_ST_RELOAD = os_trv;
  189. NVIC_ST_CURRENT = 0;
  190. NVIC_ST_CTRL = 0x0007;
  191. NVIC_SYS_PRI3 |= 0xFF000000;
  192. }
  193. __inline static void rt_svc_init (void) {
  194. #if !(__TARGET_ARCH_6S_M)
  195. int sh,prigroup;
  196. #endif
  197. NVIC_SYS_PRI3 |= 0x00FF0000;
  198. #if (__TARGET_ARCH_6S_M)
  199. NVIC_SYS_PRI2 |= (NVIC_SYS_PRI3<<(8+1)) & 0xFC000000;
  200. #else
  201. sh = 8 - __clz (~((NVIC_SYS_PRI3 << 8) & 0xFF000000));
  202. prigroup = ((NVIC_AIR_CTRL >> 8) & 0x07);
  203. if (prigroup >= sh) {
  204. sh = prigroup + 1;
  205. }
  206. NVIC_SYS_PRI2 = ((0xFEFFFFFF << sh) & 0xFF000000) | (NVIC_SYS_PRI2 & 0x00FFFFFF);
  207. #endif
  208. }
  209. extern void rt_set_PSP (U32 stack);
  210. extern U32 rt_get_PSP (void);
  211. extern void os_set_env (void);
  212. extern void *_alloc_box (void *box_mem);
  213. extern int _free_box (void *box_mem, void *box);
  214. extern void rt_init_stack (P_TCB p_TCB, FUNCP task_body);
  215. extern void rt_ret_val (P_TCB p_TCB, U32 v0);
  216. extern void rt_ret_val2 (P_TCB p_TCB, U32 v0, U32 v1);
  217. extern void dbg_init (void);
  218. extern void dbg_task_notify (P_TCB p_tcb, BOOL create);
  219. extern void dbg_task_switch (U32 task_id);
  220. #ifdef DBG_MSG
  221. #define DBG_INIT() dbg_init()
  222. #define DBG_TASK_NOTIFY(p_tcb,create) if (dbg_msg) dbg_task_notify(p_tcb,create)
  223. #define DBG_TASK_SWITCH(task_id) if (dbg_msg && (os_tsk.new_tsk != os_tsk.run)) \
  224. dbg_task_switch(task_id)
  225. #else
  226. #define DBG_INIT()
  227. #define DBG_TASK_NOTIFY(p_tcb,create)
  228. #define DBG_TASK_SWITCH(task_id)
  229. #endif
  230. /*----------------------------------------------------------------------------
  231. * end of file
  232. *---------------------------------------------------------------------------*/