1 /*
2  * Copyright (c) 2020-2020, Bluetrum Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2020/11/18     greedyhao    Bluetrum RISC-V porting code.
9  */
10 
11 #include <rthw.h>
12 #include <rtthread.h>
13 
14 volatile rt_ubase_t  rt_interrupt_from_thread = 0;
15 volatile rt_ubase_t  rt_interrupt_to_thread   = 0;
16 volatile rt_uint32_t rt_thread_switch_interrupt_flag = 0;
17 rt_uint32_t rt_cur_thread_sp = 0;
18 
19 /**
20  * This function will initialize thread stack
21  *
22  * @param tentry the entry of thread
23  * @param parameter the parameter of entry
24  * @param stack_addr the beginning stack address
25  * @param texit the function will be called when thread exit
26  *
27  * @return stack address
28  */
rt_hw_stack_init(void * tentry,void * parameter,rt_uint8_t * stack_addr,void * texit)29 rt_uint8_t *rt_hw_stack_init(void       *tentry,
30                              void       *parameter,
31                              rt_uint8_t *stack_addr,
32                              void       *texit)
33 {
34     rt_uint32_t *stk;
35     register int *tp asm("x3");
36 
37     stack_addr += sizeof(rt_uint32_t);
38     stack_addr  = (rt_uint8_t *)RT_ALIGN_DOWN((rt_uint32_t)stack_addr, 8);
39     stk  = (rt_uint32_t *)stack_addr;
40 
41     stk--;
42     *stk = (rt_uint32_t)0x10003;         /* Start address */
43     stk--;
44     *stk = (rt_uint32_t)tentry;         /* Start address */
45     stk -= 22;
46     *stk = (rt_uint32_t)parameter;      /* Register a0  parameter*/
47     stk -= 6;
48     *stk = (rt_uint32_t)tp;             /* Register thread pointer */
49     stk --;
50     *stk = (rt_uint32_t)texit;          /* Register ra   texit*/
51 
52     /* return task's current stack address */
53     return (rt_uint8_t *)stk;
54 }
55