cpuport.c
5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* File : cpuport.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2009 - 2011, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
*
* Change Logs:
* Date Author Notes
* 2011-02-23 Bernard the first version
* 2012-03-03 xuzhenglim modify for rx62N
*/
#include <rthw.h>
#include <rtthread.h>
#include "cpuconfig.h"
#include "machine.h"
#include "iorx62n.h"
#define ENTER_INTERRUPT() ICU.SWINTR.BIT.SWINT = 1;
extern volatile rt_uint8_t rt_interrupt_nest;
/* switch flag on interrupt and thread pointer to save switch record */
rt_uint32_t rt_interrupt_from_thread;
rt_uint32_t rt_interrupt_to_thread;
rt_uint32_t rt_thread_switch_interrupt_flag;
/* stack frame*/
struct stack_frame
{
rt_uint32_t ACCLO;
rt_uint32_t ACCHI;
rt_uint32_t FPSW;
rt_uint32_t R1;
rt_uint32_t R2;
rt_uint32_t R3;
rt_uint32_t R4;
rt_uint32_t R5;
rt_uint32_t R6;
rt_uint32_t R7;
rt_uint32_t R8;
rt_uint32_t R9;
rt_uint32_t R10;
rt_uint32_t R11;
rt_uint32_t R12;
rt_uint32_t R13;
rt_uint32_t R14;
rt_uint32_t R15;
//there is not R0 register,it is special for stack pointer
rt_uint32_t PC;
rt_uint32_t PSW;
};
/**
* Initilial the thread stack.
*
* @author LXZ (2014/11/8)
*
* @param void* tentry
* @param void* parameter
* @param rt_uint8_t* stack_addr
* @param void* texit
*
* @return rt_uint8_t*
*/
rt_uint8_t *rt_hw_stack_init(void *tentry, void *parameter,
rt_uint8_t *stack_addr, void *texit)
{
unsigned long *stk;
struct stack_frame *stack_frame;
unsigned long i;
stk = (unsigned long *)stack_addr;
*(stk) = (unsigned long)texit;
stack_frame = (struct stack_frame *)(stack_addr - sizeof(struct stack_frame)) ;
//Initilial all register
for (i = 0; i < sizeof(struct stack_frame) / sizeof(rt_uint32_t); i ++)
{
((rt_uint32_t *)stack_frame)[i] = 0xdeadbeef;
}
stack_frame->PSW = (unsigned long)0x00030000 ; /* psw */
stack_frame->PC = (unsigned long)tentry; /* thread entery*/
stack_frame->R1 = (unsigned long )parameter; /* r1 : parameter */
stack_frame->FPSW = 0x00000100; /* fpsw */
return(rt_uint8_t *)stack_frame;
}
#if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
extern void list_thread(void);
#endif
extern rt_thread_t rt_current_thread;
/**
* deal exception
*
* @author LXZ (2014/11/8)
*
* @param struct stack_frame* exception_contex
*/
void rt_hw_hard_fault_exception(struct stack_frame* exception_contex)
{
if (exception_contex != RT_NULL) {
rt_kprintf("psw: 0x%08x\n", exception_contex->PSW);
rt_kprintf("pc: 0x%08x\n", exception_contex->PC);
rt_kprintf("r0: 0x%08x\n", exception_contex->R1);
rt_kprintf("r0: 0x%08x\n", exception_contex->R2);
rt_kprintf("r0: 0x%08x\n", exception_contex->R3);
rt_kprintf("r0: 0x%08x\n", exception_contex->R4);
rt_kprintf("r0: 0x%08x\n", exception_contex->R5);
rt_kprintf("r0: 0x%08x\n", exception_contex->R6);
rt_kprintf("r0: 0x%08x\n", exception_contex->R7);
rt_kprintf("r0: 0x%08x\n", exception_contex->R8);
rt_kprintf("r0: 0x%08x\n", exception_contex->R9);
rt_kprintf("r0: 0x%08x\n", exception_contex->R10);
rt_kprintf("r0: 0x%08x\n", exception_contex->R11);
rt_kprintf("r0: 0x%08x\n", exception_contex->R12);
rt_kprintf("r0: 0x%08x\n", exception_contex->R13);
rt_kprintf("r0: 0x%08x\n", exception_contex->R14);
rt_kprintf("r0: 0x%08x\n", exception_contex->R15);
rt_kprintf("fpsw: 0x%08x\n", exception_contex->FPSW);
rt_kprintf("acchi: 0x%08x\n", exception_contex->ACCHI);
rt_kprintf("acclo: 0x%08x\n", exception_contex->ACCLO);
}
rt_kprintf("hard fault on thread: %s\n", rt_current_thread->name);
#if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
list_thread();
#endif
while (1);
}
/**
* switch thread in interrupt
*
* @author LXZ (2014/11/8)
*
* @param rt_uint32_t from
* @param rt_uint32_t to
*/
void rt_hw_context_switch(rt_uint32_t from, rt_uint32_t to)
{
if (rt_thread_switch_interrupt_flag == 0)
{
rt_thread_switch_interrupt_flag = 1;
rt_interrupt_from_thread = from;
}
rt_interrupt_to_thread = to;
ENTER_INTERRUPT();
}
/**
* switch thread out the interrupt
*
* @author LXZ (2014/11/8)
*
* @param rt_uint32_t from
* @param rt_uint32_t to
*/
void rt_hw_context_switch_interrupt(rt_uint32_t from, rt_uint32_t to)
{
if (rt_thread_switch_interrupt_flag == 0)
{
rt_thread_switch_interrupt_flag = 1;
rt_interrupt_from_thread = from;
}
rt_interrupt_to_thread = to;
ENTER_INTERRUPT();
}
/**
* shut down the chip
*
* @author LXZ (2014/11/8)
*/
RT_WEAK void rt_hw_cpu_shutdown(void)
{
rt_kprintf("shutdown...\n");
RT_ASSERT(0);
}
/**
* switch to the first thread,it just call one time
*
* @author LXZ (2014/11/8)
*
* @param rt_uint32_t to
*/
void rt_hw_context_switch_to(rt_uint32_t to)
{
rt_interrupt_from_thread = 0;
rt_interrupt_to_thread = to;
rt_thread_switch_interrupt_flag = 1;
/* enable interrupt*/
_IEN( _ICU_SWINT ) = 1;
/*clear the interrupt flag*/
_IR( _ICU_SWINT ) = 0;
_IPR( _ICU_SWINT ) = MAX_SYSCALL_INTERRUPT_PRIORITY + 1;
/*touch the software interrupt*/
ENTER_INTERRUPT();
/*wait for first thread start up*/
while(1);
}