1 /*
2 * File : cpuport.c
3 * This file is part of RT-Thread RTOS
4 * COPYRIGHT (C) 2009 - 2012, RT-Thread Development Team
5 *
6 * The license and distribution terms for this file may be
7 * found in the file LICENSE in this distribution or at
8 * http://www.rt-thread.org/license/LICENSE
9 *
10 * Change Logs:
11 * Date Author Notes
12 * 2011-02-23 Bernard the first version
13 * 2012-09-23 lgnq set the texit to R31
14 */
15
16 #include <rtthread.h>
17
18 extern volatile rt_uint8_t rt_interrupt_nest;
19
20 /* switch flag on interrupt and thread pointer to save switch record */
21 rt_uint32_t rt_interrupt_from_thread;
22 rt_uint32_t rt_interrupt_to_thread;
23 rt_uint32_t rt_thread_switch_interrupt_flag;
24
25 /**
26 * This function will initialize hardware interrupt
27 */
rt_hw_interrupt_init(void)28 void rt_hw_interrupt_init(void)
29 {
30 /* init interrupt nest, and context in thread sp */
31 rt_interrupt_nest = 0;
32 rt_interrupt_from_thread = 0;
33 rt_interrupt_to_thread = 0;
34 rt_thread_switch_interrupt_flag = 0;
35 }
36
37 /**
38 * This function will initialize thread stack
39 *
40 * @param tentry the entry of thread
41 * @param parameter the parameter of entry
42 * @param stack_addr the beginning stack address
43 * @param texit the function will be called when thread exit
44 *
45 * @return stack address
46 */
rt_hw_stack_init(void * tentry,void * parameter,rt_uint8_t * stack_addr,void * texit)47 rt_uint8_t *rt_hw_stack_init(void *tentry,
48 void *parameter,
49 rt_uint8_t *stack_addr,
50 void *texit)
51 {
52 rt_uint32_t *stk;
53
54 stk = (rt_uint32_t *)stack_addr; /* Load stack pointer */
55
56 *(--stk) = (rt_uint32_t)0x23232323; /* r23 */
57 *(--stk) = (rt_uint32_t)0x24242424; /* r24 */
58 *(--stk) = (rt_uint32_t)0x25252525; /* r25 */
59 *(--stk) = (rt_uint32_t)0x26262626; /* r26 */
60 *(--stk) = (rt_uint32_t)0x27272727; /* r27 */
61 *(--stk) = (rt_uint32_t)0x28282828; /* r28 */
62 *(--stk) = (rt_uint32_t)0x29292929; /* r29 */
63 *(--stk) = (rt_uint32_t)0x30303030; /* r30 */
64 *(--stk) = (rt_uint32_t)texit; /* r31 */
65 *(--stk) = (rt_uint32_t)0x00000000; /* Task PSW = Interrupts enabled */
66 *(--stk) = (rt_uint32_t)tentry; /* Task's PC */
67 *(--stk) = (rt_uint32_t)0x16161616; /* r16 */
68 *(--stk) = (rt_uint32_t)0x15151515; /* r15 */
69 *(--stk) = (rt_uint32_t)0x14141414; /* r14 */
70 *(--stk) = (rt_uint32_t)0x13131313; /* r13 */
71 *(--stk) = (rt_uint32_t)0x12121212; /* r12 */
72 *(--stk) = (rt_uint32_t)0x11111111; /* r11 */
73 *(--stk) = (rt_uint32_t)0x10101010; /* r10 */
74 *(--stk) = (rt_uint32_t)0x09090909; /* r9 */
75 *(--stk) = (rt_uint32_t)0x08080808; /* r8 */
76 *(--stk) = (rt_uint32_t)0x07070707; /* r7 */
77 *(--stk) = (rt_uint32_t)0x06060606; /* r6 */
78 *(--stk) = (rt_uint32_t)0x05050505; /* r5 */
79 *(--stk) = (rt_uint32_t)0x02020202; /* r2 */
80 *(--stk) = (rt_uint32_t)parameter; /* r1 */
81
82 return ((rt_uint8_t *)stk);
83 }
84
rt_hw_context_switch(rt_uint32_t from,rt_uint32_t to)85 void rt_hw_context_switch(rt_uint32_t from, rt_uint32_t to)
86 {
87 rt_interrupt_from_thread = from;
88 rt_interrupt_to_thread = to;
89 asm("trap 0x10");
90 }
91
rt_hw_context_switch_interrupt(rt_uint32_t from,rt_uint32_t to)92 void rt_hw_context_switch_interrupt(rt_uint32_t from, rt_uint32_t to)
93 {
94 if (rt_thread_switch_interrupt_flag != 1)
95 {
96 rt_thread_switch_interrupt_flag = 1;
97 rt_interrupt_from_thread = from;
98 }
99 rt_interrupt_to_thread = to;
100 }
101