board.c 5.3 KB
Newer Older
1
/*
G
greedyhao 已提交
2
 * Copyright (c) 2020-2021, Bluetrum Development Team
mysterywolf's avatar
mysterywolf 已提交
3
 *
4 5 6 7 8 9 10 11 12 13 14 15
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author            Notes
 * 2020-11-18     greedyhao         first version
 */

#include <rthw.h>
#include "board.h"

int rt_hw_usart_init(void);
void my_printf(const char *format, ...);
G
greedyhao 已提交
16
void my_print_r(const void *buf, uint16_t cnt);
17 18 19 20
void timer0_cfg(uint32_t ticks);
void rt_soft_isr(int vector, void *param);
void cpu_irq_comm(void);
void set_cpu_irq_comm(void (*irq_hook)(void));
G
greedyhao 已提交
21
void load_cache();
G
greedyhao 已提交
22
void os_cache_init(void);
G
greedyhao 已提交
23
void sys_error_hook(uint8_t err_no);
24
void huart_timer_isr(void);
G
greedyhao 已提交
25 26 27 28

typedef void (*spiflash_init_func)(uint8_t sf_read, uint8_t dummy);

static struct rt_mutex mutex_spiflash = {0};
G
greedyhao 已提交
29
static struct rt_mutex mutex_cache = {0};
G
greedyhao 已提交
30
extern volatile rt_uint8_t rt_interrupt_nest;
G
greedyhao 已提交
31
extern uint32_t __heap_start, __heap_end;
32

33
#ifdef RT_USING_CONSOLE
G
greedyhao 已提交
34
void hal_printf(const char *fmt, ...)
35
{
G
greedyhao 已提交
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
    rt_device_t console = rt_console_get_device();

    va_list args;
    rt_size_t length;
    static char rt_log_buf[RT_CONSOLEBUF_SIZE];

    va_start(args, fmt);
    /* the return value of vsnprintf is the number of bytes that would be
     * written to buffer had if the size of the buffer been sufficiently
     * large excluding the terminating null byte. If the output string
     * would be larger than the rt_log_buf, we have to adjust the output
     * length. */
    length = rt_vsnprintf(rt_log_buf, sizeof(rt_log_buf) - 1, fmt, args);
    if (length > RT_CONSOLEBUF_SIZE - 1)
        length = RT_CONSOLEBUF_SIZE - 1;
#ifdef RT_USING_DEVICE
    if (console == RT_NULL)
    {
        rt_hw_console_output(rt_log_buf);
    }
    else
    {
        rt_uint16_t old_flag = console->open_flag;

        console->open_flag |= RT_DEVICE_FLAG_STREAM;
        rt_device_write(console, 0, rt_log_buf, length);
        console->open_flag = old_flag;
    }
#else
    rt_hw_console_output(rt_log_buf);
#endif
    va_end(args);
68
}
69
#endif
70

G
greedyhao 已提交
71
RT_SECTION(".irq.timer")
72 73 74 75 76
void timer0_isr(int vector, void *param)
{
    rt_interrupt_enter();
    TMR0CPND = BIT(9);
    rt_tick_increase();
77 78 79
#ifdef RT_USING_SERIAL
    huart_timer_isr();
#endif
80 81 82 83 84
    rt_interrupt_leave();
}

void timer0_init(void)
{
mysterywolf's avatar
mysterywolf 已提交
85 86
    TMR0CON =  BIT(7); //TIE
    TMR0CNT = 0;
87

mysterywolf's avatar
mysterywolf 已提交
88
    rt_hw_interrupt_install(IRQ_TMR0_VECTOR, timer0_isr, RT_NULL, "tick");
89 90 91 92
}

void timer0_cfg(uint32_t ticks)
{
mysterywolf's avatar
mysterywolf 已提交
93 94
    TMR0PR  = (uint32_t)(ticks - 1UL);       //1ms interrupt
    TMR0CON |= BIT(0); // EN
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
}

void hal_mdelay(uint32_t ms)
{
    rt_thread_mdelay(ms);
}

void rt_hw_systick_init(void)
{
    CLKCON2 &= 0x00ffffff;
    CLKCON2 |= (25 << 24);                                  //配置x26m_div_clk = 1M (timer, ir, fmam ...用到)
    CLKCON0 &= ~(7 << 23);
    CLKCON0 |= BIT(24);                                     //tmr_inc select x26m_div_clk = 1M

    set_sysclk(SYSCLK_48M);

    /* Setting software interrupt */
    set_cpu_irq_comm(cpu_irq_comm);
    rt_hw_interrupt_install(IRQ_SW_VECTOR, rt_soft_isr, RT_NULL, "sw_irq");

    timer0_init();
    hal_set_tick_hook(timer0_cfg);
    hal_set_ticks(get_sysclk_nhz()/RT_TICK_PER_SECOND);

    PICCON |= 0x10002;
}

void rt_hw_board_init(void)
{
    WDT_DIS();
    rt_hw_systick_init();

#ifdef RT_USING_HEAP
G
greedyhao 已提交
128
    rt_system_heap_init(&__heap_start, &__heap_end);
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
#endif

#ifdef RT_USING_PIN
    rt_hw_pin_init();
#endif // RT_USING_PIN

#ifdef RT_USING_SERIAL
    rt_hw_usart_init();
#endif // RT_USING_SERIAL

#ifdef RT_USING_CONSOLE
    rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
#endif // RT_USING_CONSOLE

#ifdef RT_USING_COMPONENTS_INIT
    rt_components_board_init();
#endif
}
147

G
greedyhao 已提交
148 149 150
RT_SECTION(".irq.cache")
void cache_init(void)
{
G
greedyhao 已提交
151
    os_cache_init();
G
greedyhao 已提交
152
    rt_mutex_init(&mutex_spiflash, "flash_mutex", RT_IPC_FLAG_FIFO);
G
greedyhao 已提交
153
    rt_mutex_init(&mutex_cache, "cache_mutex", RT_IPC_FLAG_FIFO);
G
greedyhao 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
}

RT_SECTION(".irq.cache")
void os_spiflash_lock(void)
{
    // if (rt_thread_self()->stat == RT_THREAD_RUNNING) {
    if ((rt_thread_self() != RT_NULL) && (rt_interrupt_nest == 0)) {
        rt_mutex_take(&mutex_spiflash, RT_WAITING_FOREVER);
    }
}

RT_SECTION(".irq.cache")
void os_spiflash_unlock(void)
{
    // if (rt_thread_self()->stat == RT_THREAD_RUNNING) {
    if ((rt_thread_self() != RT_NULL) && (rt_interrupt_nest == 0)) {
        rt_mutex_release(&mutex_spiflash);
    }
}

G
greedyhao 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
RT_SECTION(".irq.cache")
void os_cache_lock(void)
{
    // if (rt_thread_self()->stat == RT_THREAD_RUNNING) {
    if ((rt_thread_self() != RT_NULL) && (rt_interrupt_nest == 0)) {
        rt_mutex_take(&mutex_cache, RT_WAITING_FOREVER);
    }
}

RT_SECTION(".irq.cache")
void os_cache_unlock(void)
{
    // if (rt_thread_self()->stat == RT_THREAD_RUNNING) {
    if ((rt_thread_self() != RT_NULL) && (rt_interrupt_nest == 0)) {
        rt_mutex_release(&mutex_cache);
    }
}

G
greedyhao 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
RT_SECTION(".irq.err.str")
static const char stack_info[] = "thread sp=0x%x name=%s";

void rt_hw_console_output(const char *str)
{
    my_printf(str);
}

/**
 * @brief print exception error
 * @note Every message needed to print, must put in .comm exction.
 */
RT_SECTION(".irq.err")
void exception_isr(void)
{
207
#if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
G
greedyhao 已提交
208
    extern long list_thread(void);
209
#endif
G
greedyhao 已提交
210 211
    sys_error_hook(1);

212
#ifdef RT_USING_CONSOLE
G
greedyhao 已提交
213 214
    rt_console_set_device(RT_NULL);
    rt_kprintf(stack_info, rt_thread_self()->sp, rt_thread_self()->name);
215
#endif
G
greedyhao 已提交
216 217 218

    while(1);
}