context_icc.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * Copyright (C) 2001-2005 by egnite Software GmbH. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. *
  32. */
  33. /*!
  34. * \file arch/avr/os/context_icc.c
  35. * \brief Context switching ported to ImageCraft AVR.
  36. *
  37. * \verbatim File version $Id: context_icc.c 4477 2012-08-20 17:50:01Z haraldkipp $ \endverbatim
  38. */
  39. #include <cfg/os.h>
  40. #include <cfg/memory.h>
  41. #include <string.h>
  42. #include <sys/atom.h>
  43. #include <sys/heap.h>
  44. #include <sys/thread.h>
  45. /*!
  46. * \addtogroup xgNutArchAvrOsContextIcc ImageCraft Context Switching for AVR CPUs
  47. * \ingroup xgNutArchAvrOs
  48. * \brief Thread context switching for ImageCraft ICCAVR.
  49. */
  50. /*@{*/
  51. /*!
  52. * \brief ICC AVR context switch frame layout.
  53. *
  54. * This is the layout of the stack after a thread's context has been
  55. * switched-out.
  56. */
  57. typedef struct {
  58. uint8_t csf_r29;
  59. uint8_t csf_r28;
  60. uint8_t csf_r23;
  61. uint8_t csf_r22;
  62. uint8_t csf_r21;
  63. uint8_t csf_r20;
  64. uint8_t csf_r15;
  65. uint8_t csf_r14;
  66. uint8_t csf_r13;
  67. uint8_t csf_r12;
  68. uint8_t csf_r11;
  69. uint8_t csf_r10;
  70. #ifdef __AVR_ATmega2561__
  71. uint8_t csf_pcex;
  72. #endif
  73. uint8_t csf_pchi;
  74. uint8_t csf_pclo;
  75. } SWITCHFRAME;
  76. /*!
  77. * \brief Thread entry frame layout.
  78. *
  79. * This is the stack layout being build to enter a new thread.
  80. */
  81. typedef struct {
  82. uint8_t cef_arghi;
  83. uint8_t cef_arglo;
  84. /*!
  85. * \brief ImageCraft software stack.
  86. */
  87. uint8_t cef_yhi;
  88. uint8_t cef_ylo;
  89. uint8_t cef_rampz;
  90. uint8_t cef_sreg;
  91. uint8_t cef_r1;
  92. #ifdef __AVR_3_BYTE_PC__
  93. uint8_t cef_pcex;
  94. #endif
  95. uint8_t cef_pchi;
  96. uint8_t cef_pclo;
  97. } ENTERFRAME;
  98. /*
  99. * This code is executed when entering a thread.
  100. */
  101. static void NutThreadEntry(void)
  102. {
  103. asm("pop r17"); // first parameter for ICC
  104. asm("pop r16");
  105. asm("pop r29"); // SW-Stack; Y-Register
  106. asm("pop r28");
  107. asm("pop r0"); // r0 = _tmp_reg_
  108. asm("out 0x3B, r0"); // RAMPZ; replace with define later
  109. asm("pop r0");
  110. asm("pop r1"); // r1 = _zero_reg_
  111. asm("out 0x3F, r0"); // SREG; replace with define later
  112. asm("reti");
  113. }
  114. void NutThreadSwitch(void)
  115. {
  116. /*
  117. * Save all CPU registers.
  118. */
  119. register uint8_t i = 0;
  120. register uint8_t j = 0;
  121. asm("push r10");
  122. asm("push r11");
  123. asm("push r12");
  124. asm("push r13");
  125. asm("push r14");
  126. asm("push r15");
  127. asm("push r20");
  128. asm("push r21");
  129. asm("push r22");
  130. asm("push r23");
  131. asm("push r28");
  132. asm("push r29");
  133. asm("in %i, $3D"); // SPL
  134. asm("in %j, $3E"); // SPH
  135. runningThread->td_sp = (((uint16_t) j) << 8) & 0xFF00 | (i & 0xFF);
  136. /*
  137. * This defines a global label, which may be called
  138. * as an entry point into this function.
  139. */
  140. asm(".globl thread_start");
  141. asm("thread_start:");
  142. /*
  143. * Reload CPU registers from the thread in front
  144. * of the run queue.
  145. */
  146. runningThread = runQueue;
  147. runningThread->td_state = TDS_RUNNING;
  148. i = (uint8_t) (runningThread->td_sp & 0xFF);
  149. j = (uint8_t) ((runningThread->td_sp >> 8) & 0xFF);
  150. asm("out $3D, %i"); // SPL
  151. asm("out $3E, %j"); // SPH
  152. asm("pop r29");
  153. asm("pop r28");
  154. asm("pop r23");
  155. asm("pop r22");
  156. asm("pop r21");
  157. asm("pop r20");
  158. asm("pop r15");
  159. asm("pop r14");
  160. asm("pop r13");
  161. asm("pop r12");
  162. asm("pop r11");
  163. asm("pop r10");
  164. }
  165. HANDLE NutThreadCreate(uint8_t * name, void (*fn) (void *), void *arg, size_t stackSize)
  166. {
  167. uint8_t *threadMem;
  168. SWITCHFRAME *sf;
  169. ENTERFRAME *ef;
  170. NUTTHREADINFO *td;
  171. uint16_t yreg;
  172. const uint8_t *paddr;
  173. /*
  174. * Allocate stack and thread info structure in one block.
  175. */
  176. if ((threadMem = NutStackAlloc(stackSize + sizeof(NUTTHREADINFO))) == 0) {
  177. return 0;
  178. }
  179. td = (NUTTHREADINFO *) (threadMem + stackSize);
  180. ef = (ENTERFRAME *) ((uint16_t) td - sizeof(ENTERFRAME));
  181. sf = (SWITCHFRAME *) ((uint16_t) ef - sizeof(SWITCHFRAME));
  182. memcpy(td->td_name, name, sizeof(td->td_name) - 1);
  183. td->td_name[sizeof(td->td_name) - 1] = 0;
  184. td->td_sp = (uint16_t) sf - 1;
  185. td->td_memory = threadMem;
  186. #if defined(NUTDEBUG_CHECK_STACKMIN) || defined(NUTDEBUG_CHECK_STACK)
  187. {
  188. uint32_t *fip = (uint32_t *)threadMem;
  189. while (fip < (uint32_t *)sf) {
  190. *fip++ = DEADBEEF;
  191. }
  192. }
  193. #else
  194. *((uint32_t *) threadMem) = DEADBEEF;
  195. *((uint32_t *) (threadMem + 4)) = DEADBEEF;
  196. *((uint32_t *) (threadMem + 8)) = DEADBEEF;
  197. *((uint32_t *) (threadMem + 12)) = DEADBEEF;
  198. #endif
  199. td->td_priority = 64;
  200. /*
  201. * Setup entry frame to simulate C function entry.
  202. */
  203. paddr = (const uint8_t *) fn;
  204. ef->cef_pclo = *paddr;
  205. ef->cef_pchi = *(paddr + 1);
  206. #ifdef __AVR_3_BYTE_PC__
  207. ef->cef_pcex = *(paddr + 2);
  208. #endif
  209. ef->cef_sreg = 0x80;
  210. ef->cef_rampz = 0;
  211. ef->cef_r1 = 0;
  212. ef->cef_arglo = (uint8_t) (((uint16_t) arg) & 0xff);
  213. ef->cef_arghi = (uint8_t) (((uint16_t) arg) >> 8);
  214. yreg = td->td_sp - 40;
  215. ef->cef_yhi = (uint8_t) (yreg >> 8);
  216. ef->cef_ylo = (uint8_t) (yreg & 0xFF);
  217. paddr = (const uint8_t *) NutThreadEntry;
  218. sf->csf_pclo = *paddr;
  219. sf->csf_pchi = *(paddr + 1);
  220. #ifdef __AVR_3_BYTE_PC__
  221. sf->csf_pcex = *(paddr + 2);
  222. #endif
  223. /*
  224. * Insert into the thread list and the run queue.
  225. */
  226. td->td_next = nutThreadList;
  227. nutThreadList = td;
  228. td->td_state = TDS_READY;
  229. td->td_timer = 0;
  230. td->td_queue = 0;
  231. #ifdef NUTDEBUG
  232. if (__os_trf)
  233. fprintf(__os_trs, "Cre<%04x>", (uintptr_t) td);
  234. #endif
  235. NutThreadAddPriQueue(td, (NUTTHREADINFO **) & runQueue);
  236. #ifdef NUTDEBUG
  237. if (__os_trf) {
  238. NutDumpThreadList(__os_trs);
  239. //NutDumpThreadQueue(__os_trs, runQueue);
  240. }
  241. #endif
  242. /*
  243. * If no thread is active, switch to new thread.
  244. */
  245. if (runningThread == 0) {
  246. NutEnterCritical();
  247. asm("rjmp thread_start");
  248. /* we will never come back here .. */
  249. }
  250. /*
  251. * If current context is not in front of
  252. * the run queue (highest priority), then
  253. * switch to the thread in front.
  254. */
  255. if (runningThread != runQueue) {
  256. runningThread->td_state = TDS_READY;
  257. #ifdef NUTDEBUG
  258. if (__os_trf)
  259. fprintf(__os_trs, "New<%04x %04x>", (uintptr_t) runningThread, (uintptr_t) runQueue);
  260. #endif
  261. NutEnterCritical();
  262. NutThreadSwitch();
  263. NutExitCritical();
  264. }
  265. return td;
  266. }
  267. /*@}*/