cpuport.c 4.9 KB
Newer Older
B
bernard.xiong@gmail.com 已提交
1 2 3
/*
 * File      : cpuport.c
 * This file is part of RT-Thread RTOS
wuyangyong's avatar
wuyangyong 已提交
4
 * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
B
bernard.xiong@gmail.com 已提交
5 6 7 8 9 10
 *
 * 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:
11 12 13 14
 * Date         Author      Notes
 * 2009-01-05   Bernard     first version
 * 2011-02-14   onelife     Modify for EFM32
 * 2011-06-17   onelife     Merge all of the C source code into cpuport.c
wuyangyong's avatar
wuyangyong 已提交
15
 * 2012-12-23   aozima      stack addr align to 8byte.
16
 * 2012-12-29   Bernard     Add exception hook.
B
bernard.xiong@gmail.com 已提交
17
 */
wuyangyong's avatar
wuyangyong 已提交
18 19 20

#include <rtthread.h>

wuyangyong's avatar
wuyangyong 已提交
21
struct exception_stack_frame
wuyangyong's avatar
wuyangyong 已提交
22
{
wuyangyong's avatar
wuyangyong 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    rt_uint32_t r0;
    rt_uint32_t r1;
    rt_uint32_t r2;
    rt_uint32_t r3;
    rt_uint32_t r12;
    rt_uint32_t lr;
    rt_uint32_t pc;
    rt_uint32_t psr;
};

struct stack_frame
{
    /* r4 ~ r11 register */
    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;

    struct exception_stack_frame exception_stack_frame;
wuyangyong's avatar
wuyangyong 已提交
46 47
};

B
bernard.xiong@gmail.com 已提交
48
/* flag in interrupt handling */
wuyangyong's avatar
wuyangyong 已提交
49 50
rt_uint32_t rt_interrupt_from_thread, rt_interrupt_to_thread;
rt_uint32_t rt_thread_switch_interrupt_flag;
51 52
/* exception hook */
static rt_err_t (*rt_exception_hook)(void *context) = RT_NULL;
wuyangyong's avatar
wuyangyong 已提交
53

B
bernard.xiong@gmail.com 已提交
54
/**
wuyangyong's avatar
wuyangyong 已提交
55 56 57 58 59 60 61 62
 * This function will initialize thread stack
 *
 * @param tentry the entry of thread
 * @param parameter the parameter of entry
 * @param stack_addr the beginning stack address
 * @param texit the function will be called when thread exit
 *
 * @return stack address
B
bernard.xiong@gmail.com 已提交
63
 */
64 65
rt_uint8_t *rt_hw_stack_init(void       *tentry,
                             void       *parameter,
wuyangyong's avatar
wuyangyong 已提交
66
                             rt_uint8_t *stack_addr,
67
                             void       *texit)
wuyangyong's avatar
wuyangyong 已提交
68
{
wuyangyong's avatar
wuyangyong 已提交
69 70 71 72
    struct stack_frame *stack_frame;
    rt_uint8_t         *stk;
    unsigned long       i;

73 74 75
    stk  = stack_addr + sizeof(rt_uint32_t);
    stk  = (rt_uint8_t *)RT_ALIGN_DOWN((rt_uint32_t)stk, 8);
    stk -= sizeof(struct stack_frame);
wuyangyong's avatar
wuyangyong 已提交
76 77

    stack_frame = (struct stack_frame *)stk;
wuyangyong's avatar
wuyangyong 已提交
78

wuyangyong's avatar
wuyangyong 已提交
79 80 81 82 83
    /* init all register */
    for (i = 0; i < sizeof(struct stack_frame) / sizeof(rt_uint32_t); i ++)
    {
        ((rt_uint32_t *)stack_frame)[i] = 0xdeadbeef;
    }
wuyangyong's avatar
wuyangyong 已提交
84

wuyangyong's avatar
wuyangyong 已提交
85 86 87 88 89 90 91 92
    stack_frame->exception_stack_frame.r0  = (unsigned long)parameter; /* r0 : argument */
    stack_frame->exception_stack_frame.r1  = 0;                        /* r1 */
    stack_frame->exception_stack_frame.r2  = 0;                        /* r2 */
    stack_frame->exception_stack_frame.r3  = 0;                        /* r3 */
    stack_frame->exception_stack_frame.r12 = 0;                        /* r12 */
    stack_frame->exception_stack_frame.lr  = (unsigned long)texit;     /* lr */
    stack_frame->exception_stack_frame.pc  = (unsigned long)tentry;    /* entry point, pc */
    stack_frame->exception_stack_frame.psr = 0x01000000L;              /* PSR */
wuyangyong's avatar
wuyangyong 已提交
93

wuyangyong's avatar
wuyangyong 已提交
94 95
    /* return task's current stack address */
    return stk;
wuyangyong's avatar
wuyangyong 已提交
96 97
}

B
bernard.xiong@gmail.com 已提交
98
/**
99 100 101 102 103 104 105 106 107 108 109
 * This function set the hook, which is invoked on fault exception handling.
 *
 * @param exception_handle the exception handling hook function.
 */
void rt_hw_exception_install(rt_err_t (*exception_handle)(void* context))
{
	rt_exception_hook = exception_handle;
}

/*
 * fault exception handler
B
bernard.xiong@gmail.com 已提交
110
 */
111
void rt_hw_hard_fault_exception(struct exception_stack_frame* context)
wuyangyong's avatar
wuyangyong 已提交
112
{
113 114 115 116 117 118 119 120 121
	extern long list_thread(void);

	if (rt_exception_hook != RT_NULL)
	{
		rt_err_t result;

		result = rt_exception_hook(context);
		if (result == RT_EOK) return;
	}
wuyangyong's avatar
wuyangyong 已提交
122

123 124 125 126 127 128 129 130 131 132
    rt_kprintf("psr: 0x%08x\n", context->psr);
    rt_kprintf(" pc: 0x%08x\n", context->pc);
    rt_kprintf(" lr: 0x%08x\n", context->lr);
    rt_kprintf("r12: 0x%08x\n", context->r12);
    rt_kprintf("r03: 0x%08x\n", context->r3);
    rt_kprintf("r02: 0x%08x\n", context->r2);
    rt_kprintf("r01: 0x%08x\n", context->r1);
    rt_kprintf("r00: 0x%08x\n", context->r0);

    rt_kprintf("hard fault on thread: %s\n", rt_thread_self()->name);
133

wuyangyong's avatar
wuyangyong 已提交
134
#ifdef RT_USING_FINSH
wuyangyong's avatar
wuyangyong 已提交
135
    list_thread();
wuyangyong's avatar
wuyangyong 已提交
136
#endif
137

wuyangyong's avatar
wuyangyong 已提交
138
    while (1);
wuyangyong's avatar
wuyangyong 已提交
139 140
}

B
bernard.xiong@gmail.com 已提交
141 142 143
/**
 * shutdown CPU
 */
144
void rt_hw_cpu_shutdown(void)
wuyangyong's avatar
wuyangyong 已提交
145
{
wuyangyong's avatar
wuyangyong 已提交
146
    rt_kprintf("shutdown...\n");
wuyangyong's avatar
wuyangyong 已提交
147

wuyangyong's avatar
wuyangyong 已提交
148
    RT_ASSERT(0);
wuyangyong's avatar
wuyangyong 已提交
149
}
150

151 152 153 154 155 156 157 158 159 160 161 162 163 164
#ifdef RT_USING_CPU_FFS
/**
 * This function finds the first bit set (beginning with the least significant bit) 
 * in value and return the index of that bit.
 *
 * Bits are numbered starting at 1 (the least significant bit).  A return value of 
 * zero from any of these functions means that the argument was zero.
 * 
 * @return return the index of the first bit set. If value is 0, then this function 
 * shall return 0.
 */
#if defined(__CC_ARM)
__asm int __rt_ffs(int value)
{
165 166 167 168 169
    CMP     r0, #0x00
    BEQ     exit
    RBIT    r0, r0
    CLZ     r0, r0
    ADDS    r0, r0, #0x01
170 171

exit
172
    BX		lr
173 174 175 176
}
#elif defined(__IAR_SYSTEMS_ICC__)
int __rt_ffs(int value)
{
177 178 179 180 181
    if (value == 0) return value;

    __ASM("RBIT r0, r0");
    __ASM("CLZ	r0, r0");
    __ASM("ADDS	r0, r0, #0x01");
182 183 184 185
}
#elif defined(__GNUC__)
int __rt_ffs(int value)
{
186
    return __builtin_ffs(value);
187 188 189 190
}
#endif

#endif