HAL_CM4.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*----------------------------------------------------------------------------
  2. * CMSIS-RTOS - RTX
  3. *----------------------------------------------------------------------------
  4. * Name: HAL_CM4.S
  5. * Purpose: Hardware Abstraction Layer for Cortex-M4
  6. * Rev.: V4.79
  7. *----------------------------------------------------------------------------
  8. *
  9. * Copyright (c) 1999-2009 KEIL, 2009-2017 ARM Germany GmbH. All rights reserved.
  10. *
  11. * SPDX-License-Identifier: Apache-2.0
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the License); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *---------------------------------------------------------------------------*/
  25. .syntax unified
  26. .equ TCB_STACKF, 37
  27. .equ TCB_TSTACK, 40
  28. /*----------------------------------------------------------------------------
  29. * Functions
  30. *---------------------------------------------------------------------------*/
  31. .thumb
  32. .section ".text"
  33. .align 2
  34. /*--------------------------- rt_set_PSP ------------------------------------*/
  35. # void rt_set_PSP (U32 stack);
  36. .thumb_func
  37. .type rt_set_PSP, %function
  38. .global rt_set_PSP
  39. rt_set_PSP:
  40. .fnstart
  41. .cantunwind
  42. MSR PSP,R0
  43. BX LR
  44. .fnend
  45. .size rt_set_PSP, .-rt_set_PSP
  46. /*--------------------------- rt_get_PSP ------------------------------------*/
  47. # U32 rt_get_PSP (void);
  48. .thumb_func
  49. .type rt_get_PSP, %function
  50. .global rt_get_PSP
  51. rt_get_PSP:
  52. .fnstart
  53. .cantunwind
  54. MRS R0,PSP
  55. BX LR
  56. .fnend
  57. .size rt_get_PSP, .-rt_get_PSP
  58. /*--------------------------- os_set_env ------------------------------------*/
  59. # void os_set_env (void);
  60. /* Switch to Unprivileged/Privileged Thread mode, use PSP. */
  61. .thumb_func
  62. .type os_set_env, %function
  63. .global os_set_env
  64. os_set_env:
  65. .fnstart
  66. .cantunwind
  67. MOV R0,SP /* PSP = MSP */
  68. MSR PSP,R0
  69. LDR R0,=os_flags
  70. LDRB R0,[R0]
  71. LSLS R0,#31
  72. ITE NE
  73. MOVNE R0,#0x02 /* Privileged Thread mode, use PSP */
  74. MOVEQ R0,#0x03 /* Unprivileged Thread mode, use PSP */
  75. MSR CONTROL,R0
  76. BX LR
  77. .fnend
  78. .size os_set_env, .-os_set_env
  79. /*--------------------------- _alloc_box ------------------------------------*/
  80. # void *_alloc_box (void *box_mem);
  81. /* Function wrapper for Unprivileged/Privileged mode. */
  82. .thumb_func
  83. .type _alloc_box, %function
  84. .global _alloc_box
  85. _alloc_box:
  86. .fnstart
  87. .cantunwind
  88. LDR R12,=rt_alloc_box
  89. MRS R3,IPSR
  90. LSLS R3,#24
  91. IT NE
  92. BXNE R12
  93. MRS R3,CONTROL
  94. LSLS R3,#31
  95. IT EQ
  96. BXEQ R12
  97. SVC 0
  98. BX LR
  99. .fnend
  100. .size _alloc_box, .-_alloc_box
  101. /*--------------------------- _free_box -------------------------------------*/
  102. # U32 _free_box (void *box_mem, void *box);
  103. /* Function wrapper for Unprivileged/Privileged mode. */
  104. .thumb_func
  105. .type _free_box, %function
  106. .global _free_box
  107. _free_box:
  108. .fnstart
  109. .cantunwind
  110. LDR R12,=rt_free_box
  111. MRS R3,IPSR
  112. LSLS R3,#24
  113. IT NE
  114. BXNE R12
  115. MRS R3,CONTROL
  116. LSLS R3,#31
  117. IT EQ
  118. BXEQ R12
  119. SVC 0
  120. BX LR
  121. .fnend
  122. .size _free_box, .-_free_box
  123. /*-------------------------- SVC_Handler ------------------------------------*/
  124. # void SVC_Handler (void);
  125. .thumb_func
  126. .type SVC_Handler, %function
  127. .global SVC_Handler
  128. SVC_Handler:
  129. .ifdef IFX_XMC4XXX
  130. .global SVC_Handler_Veneer
  131. SVC_Handler_Veneer:
  132. .endif
  133. .fnstart
  134. .cantunwind
  135. MRS R0,PSP /* Read PSP */
  136. LDR R1,[R0,#24] /* Read Saved PC from Stack */
  137. LDRB R1,[R1,#-2] /* Load SVC Number */
  138. CBNZ R1,SVC_User
  139. LDM R0,{R0-R3,R12} /* Read R0-R3,R12 from stack */
  140. PUSH {R4,LR} /* Save EXC_RETURN */
  141. BLX R12 /* Call SVC Function */
  142. POP {R4,LR} /* Restore EXC_RETURN */
  143. MRS R12,PSP /* Read PSP */
  144. STM R12,{R0-R2} /* Store return values */
  145. LDR R3,=os_tsk
  146. LDM R3,{R1,R2} /* os_tsk.run, os_tsk.next */
  147. CMP R1,R2
  148. .ifdef IFX_XMC4XXX
  149. ITT EQ
  150. PUSHEQ {LR}
  151. POPEQ {PC}
  152. .else
  153. IT EQ
  154. BXEQ LR /* RETI, no task switch */
  155. .endif
  156. CBNZ R1,SVC_ContextSave /* Runtask not deleted? */
  157. TST LR,#0x10 /* is it extended frame? */
  158. BNE SVC_ContextRestore
  159. LDR R1,=0xE000EF34
  160. LDR R0,[R1] /* Load FPCCR */
  161. BIC R0,#1 /* Clear LSPACT (Lazy state) */
  162. STR R0,[R1] /* Store FPCCR */
  163. B SVC_ContextRestore
  164. SVC_ContextSave:
  165. TST LR,#0x10 /* is it extended frame? */
  166. ITTE EQ
  167. VSTMDBEQ R12!,{S16-S31} /* yes, stack also VFP hi-regs */
  168. MOVEQ R0,#0x01 /* os_tsk->stack_frame val */
  169. MOVNE R0,#0x00
  170. STRB R0,[R1,#TCB_STACKF] /* os_tsk.run->stack_frame = val */
  171. STMDB R12!,{R4-R11} /* Save Old context */
  172. STR R12,[R1,#TCB_TSTACK] /* Update os_tsk.run->tsk_stack */
  173. PUSH {R2,R3}
  174. BL rt_stk_check /* Check for Stack overflow */
  175. POP {R2,R3}
  176. SVC_ContextRestore:
  177. STR R2,[R3] /* os_tsk.run = os_tsk.next */
  178. LDR R12,[R2,#TCB_TSTACK] /* os_tsk.next->tsk_stack */
  179. LDMIA R12!,{R4-R11} /* Restore New Context */
  180. LDRB R0,[R2,#TCB_STACKF] /* Stack Frame */
  181. CMP R0,#0 /* Basic/Extended Stack Frame */
  182. ITEE EQ
  183. MVNEQ LR,#~0xFFFFFFFD /* set EXC_RETURN value */
  184. MVNNE LR,#~0xFFFFFFED
  185. VLDMIANE R12!,{S16-S31} /* restore VFP hi-registers */
  186. MSR PSP,R12 /* Write PSP */
  187. SVC_Exit:
  188. .ifdef IFX_XMC4XXX
  189. PUSH {LR}
  190. POP {PC}
  191. .else
  192. BX LR
  193. .endif
  194. /*------------------- User SVC ------------------------------*/
  195. SVC_User:
  196. PUSH {R4,LR} /* Save Registers */
  197. LDR R2,=SVC_Count
  198. LDR R2,[R2]
  199. CMP R1,R2
  200. BHI SVC_Done /* Overflow */
  201. LDR R4,=SVC_Table-4
  202. LDR R4,[R4,R1,LSL #2] /* Load SVC Function Address */
  203. LDM R0,{R0-R3,R12} /* Read R0-R3,R12 from stack */
  204. BLX R4 /* Call SVC Function */
  205. MRS R12,PSP
  206. STM R12,{R0-R3} /* Function return values */
  207. SVC_Done:
  208. POP {R4,PC} /* RETI */
  209. .fnend
  210. .size SVC_Handler, .-SVC_Handler
  211. /*-------------------------- PendSV_Handler ---------------------------------*/
  212. # void PendSV_Handler (void);
  213. .thumb_func
  214. .type PendSV_Handler, %function
  215. .global PendSV_Handler
  216. .global Sys_Switch
  217. PendSV_Handler:
  218. .ifdef IFX_XMC4XXX
  219. .global PendSV_Handler_Veneer
  220. PendSV_Handler_Veneer:
  221. .endif
  222. .fnstart
  223. .cantunwind
  224. PUSH {R4,LR} /* Save EXC_RETURN */
  225. BL rt_pop_req
  226. Sys_Switch:
  227. POP {R4,LR} /* Restore EXC_RETURN */
  228. LDR R3,=os_tsk
  229. LDM R3,{R1,R2} /* os_tsk.run, os_tsk.next */
  230. CMP R1,R2
  231. .ifdef IFX_XMC4XXX
  232. ITT EQ
  233. PUSHEQ {LR}
  234. POPEQ {PC}
  235. .else
  236. IT EQ
  237. BXEQ LR /* RETI, no task switch */
  238. .endif
  239. MRS R12,PSP /* Read PSP */
  240. TST LR,#0x10 /* is it extended frame? */
  241. ITTE EQ
  242. VSTMDBEQ R12!,{S16-S31} /* yes, stack also VFP hi-regs */
  243. MOVEQ R0,#0x01 /* os_tsk->stack_frame val */
  244. MOVNE R0,#0x00
  245. STRB R0,[R1,#TCB_STACKF] /* os_tsk.run->stack_frame = val */
  246. STMDB R12!,{R4-R11} /* Save Old context */
  247. STR R12,[R1,#TCB_TSTACK] /* Update os_tsk.run->tsk_stack */
  248. PUSH {R2,R3}
  249. BL rt_stk_check /* Check for Stack overflow */
  250. POP {R2,R3}
  251. STR R2,[R3] /* os_tsk.run = os_tsk.next */
  252. LDR R12,[R2,#TCB_TSTACK] /* os_tsk.next->tsk_stack */
  253. LDMIA R12!,{R4-R11} /* Restore New Context */
  254. LDRB R0,[R2,#TCB_STACKF] /* Stack Frame */
  255. CMP R0,#0 /* Basic/Extended Stack Frame */
  256. ITEE EQ
  257. MVNEQ LR,#~0xFFFFFFFD /* set EXC_RETURN value */
  258. MVNNE LR,#~0xFFFFFFED
  259. VLDMIANE R12!,{S16-S31} /* restore VFP hi-registers */
  260. MSR PSP,R12 /* Write PSP */
  261. Sys_Exit:
  262. .ifdef IFX_XMC4XXX
  263. PUSH {LR}
  264. POP {PC}
  265. .else
  266. BX LR /* Return to Thread Mode */
  267. .endif
  268. .fnend
  269. .size PendSV_Handler, .-PendSV_Handler
  270. /*-------------------------- SysTick_Handler --------------------------------*/
  271. # void SysTick_Handler (void);
  272. .thumb_func
  273. .type SysTick_Handler, %function
  274. .global SysTick_Handler
  275. SysTick_Handler:
  276. .ifdef IFX_XMC4XXX
  277. .global SysTick_Handler_Veneer
  278. SysTick_Handler_Veneer:
  279. .endif
  280. .fnstart
  281. .cantunwind
  282. PUSH {R4,LR} /* Save EXC_RETURN */
  283. BL rt_systick
  284. B Sys_Switch
  285. .fnend
  286. .size SysTick_Handler, .-SysTick_Handler
  287. /*-------------------------- OS_Tick_Handler --------------------------------*/
  288. # void OS_Tick_Handler (void);
  289. .thumb_func
  290. .type OS_Tick_Handler, %function
  291. .global OS_Tick_Handler
  292. OS_Tick_Handler:
  293. .fnstart
  294. .cantunwind
  295. PUSH {R4,LR} /* Save EXC_RETURN */
  296. BL os_tick_irqack
  297. BL rt_systick
  298. B Sys_Switch
  299. .fnend
  300. .size OS_Tick_Handler, .-OS_Tick_Handler
  301. .end
  302. /*----------------------------------------------------------------------------
  303. * end of file
  304. *---------------------------------------------------------------------------*/