2020-12-10 11:02:26 +08:00
|
|
|
/*
|
2021-01-28 17:06:09 +08:00
|
|
|
* Copyright (c) 2020-2021, Bluetrum Development Team
|
2021-03-11 13:26:54 +08:00
|
|
|
*
|
2020-12-10 11:02:26 +08:00
|
|
|
* 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, ...);
|
2021-04-09 17:35:26 +08:00
|
|
|
void my_print_r(const void *buf, uint16_t cnt);
|
2020-12-10 11:02:26 +08:00
|
|
|
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));
|
2021-04-09 17:35:26 +08:00
|
|
|
void load_cache();
|
2021-05-13 10:22:09 +08:00
|
|
|
void os_cache_init(void);
|
2021-04-09 17:35:26 +08:00
|
|
|
void sys_error_hook(uint8_t err_no);
|
2021-08-08 16:31:00 +08:00
|
|
|
void huart_timer_isr(void);
|
2021-04-09 17:35:26 +08:00
|
|
|
|
|
|
|
typedef void (*spiflash_init_func)(uint8_t sf_read, uint8_t dummy);
|
|
|
|
|
|
|
|
static struct rt_mutex mutex_spiflash = {0};
|
2021-05-13 10:22:09 +08:00
|
|
|
static struct rt_mutex mutex_cache = {0};
|
2021-04-09 17:35:26 +08:00
|
|
|
extern volatile rt_uint8_t rt_interrupt_nest;
|
2020-12-15 23:43:04 +08:00
|
|
|
extern uint32_t __heap_start, __heap_end;
|
2020-12-10 11:02:26 +08:00
|
|
|
|
2021-08-12 17:12:52 +08:00
|
|
|
#ifdef RT_USING_CONSOLE
|
2020-12-10 22:07:49 +08:00
|
|
|
void hal_printf(const char *fmt, ...)
|
2020-12-10 11:02:26 +08:00
|
|
|
{
|
2020-12-10 22:07:49 +08:00
|
|
|
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);
|
2020-12-10 11:02:26 +08:00
|
|
|
}
|
2021-08-12 17:12:52 +08:00
|
|
|
#endif
|
2020-12-10 11:02:26 +08:00
|
|
|
|
2022-12-12 02:12:03 +08:00
|
|
|
rt_section(".irq")
|
2021-11-30 14:38:51 +08:00
|
|
|
void os_interrupt_enter(void)
|
|
|
|
{
|
|
|
|
rt_interrupt_enter();
|
|
|
|
}
|
|
|
|
|
2022-12-12 02:12:03 +08:00
|
|
|
rt_section(".irq")
|
2021-11-30 14:38:51 +08:00
|
|
|
void os_interrupt_leave(void)
|
|
|
|
{
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef void (*isr_t)(void);
|
2022-12-12 02:12:03 +08:00
|
|
|
rt_section(".irq")
|
2021-11-30 14:38:51 +08:00
|
|
|
isr_t register_isr(int vector, isr_t isr)
|
|
|
|
{
|
|
|
|
char buf[8] = {0};
|
|
|
|
rt_snprintf(buf, sizeof(buf), "sys%d", vector);
|
|
|
|
rt_isr_handler_t handle = (rt_isr_handler_t)isr;
|
|
|
|
rt_hw_interrupt_install(vector, handle, RT_NULL, buf);
|
|
|
|
}
|
|
|
|
|
2022-12-12 02:12:03 +08:00
|
|
|
rt_section(".irq.timer")
|
2020-12-10 11:02:26 +08:00
|
|
|
void timer0_isr(int vector, void *param)
|
|
|
|
{
|
|
|
|
rt_interrupt_enter();
|
|
|
|
TMR0CPND = BIT(9);
|
|
|
|
rt_tick_increase();
|
2021-08-08 16:31:00 +08:00
|
|
|
#ifdef RT_USING_SERIAL
|
|
|
|
huart_timer_isr();
|
|
|
|
#endif
|
2020-12-10 11:02:26 +08:00
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
|
|
|
|
void timer0_init(void)
|
|
|
|
{
|
2021-03-11 13:26:54 +08:00
|
|
|
TMR0CON = BIT(7); //TIE
|
|
|
|
TMR0CNT = 0;
|
2020-12-10 11:02:26 +08:00
|
|
|
|
2021-03-11 13:26:54 +08:00
|
|
|
rt_hw_interrupt_install(IRQ_TMR0_VECTOR, timer0_isr, RT_NULL, "tick");
|
2020-12-10 11:02:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void timer0_cfg(uint32_t ticks)
|
|
|
|
{
|
2021-03-11 13:26:54 +08:00
|
|
|
TMR0PR = (uint32_t)(ticks - 1UL); //1ms interrupt
|
|
|
|
TMR0CON |= BIT(0); // EN
|
2020-12-10 11:02:26 +08:00
|
|
|
}
|
|
|
|
|
2021-11-30 14:38:51 +08:00
|
|
|
uint32_t hal_get_ticks(void)
|
2020-12-10 11:02:26 +08:00
|
|
|
{
|
2021-11-30 14:38:51 +08:00
|
|
|
return rt_tick_get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void hal_mdelay(uint32_t nms)
|
|
|
|
{
|
|
|
|
rt_thread_mdelay(nms);
|
|
|
|
}
|
|
|
|
|
|
|
|
void hal_udelay(uint32_t nus)
|
|
|
|
{
|
|
|
|
rt_hw_us_delay(nus);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The time delay function.
|
|
|
|
*
|
|
|
|
* @param us microseconds.
|
|
|
|
*/
|
2022-12-12 02:12:03 +08:00
|
|
|
rt_section(".com_text")
|
2021-11-30 14:38:51 +08:00
|
|
|
void rt_hw_us_delay(rt_uint32_t us)
|
|
|
|
{
|
|
|
|
rt_uint32_t ticks;
|
|
|
|
rt_uint32_t told, tnow, tcnt = 0;
|
|
|
|
rt_uint32_t reload = TMR0PR;
|
|
|
|
|
|
|
|
ticks = us * reload / (1000 / RT_TICK_PER_SECOND);
|
|
|
|
told = TMR0CNT;
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
tnow = TMR0CNT;
|
|
|
|
if (tnow != told)
|
|
|
|
{
|
|
|
|
if (tnow < told)
|
|
|
|
{
|
|
|
|
tcnt += told - tnow;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tcnt += reload - tnow + told;
|
|
|
|
}
|
|
|
|
told = tnow;
|
|
|
|
if (tcnt >= ticks)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-10 11:02:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2021-11-30 17:26:39 +08:00
|
|
|
hal_set_ticks(get_sysclk_nhz() / RT_TICK_PER_SECOND);
|
2020-12-10 11:02:26 +08:00
|
|
|
|
|
|
|
PICCON |= 0x10002;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rt_hw_board_init(void)
|
|
|
|
{
|
|
|
|
WDT_DIS();
|
|
|
|
rt_hw_systick_init();
|
|
|
|
|
|
|
|
#ifdef RT_USING_HEAP
|
2020-12-15 23:43:04 +08:00
|
|
|
rt_system_heap_init(&__heap_start, &__heap_end);
|
2020-12-10 11:02:26 +08:00
|
|
|
#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
|
|
|
|
|
2022-01-08 23:29:41 +08:00
|
|
|
#if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
|
2020-12-10 11:02:26 +08:00
|
|
|
rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
|
|
|
|
#endif // RT_USING_CONSOLE
|
|
|
|
|
|
|
|
#ifdef RT_USING_COMPONENTS_INIT
|
|
|
|
rt_components_board_init();
|
|
|
|
#endif
|
|
|
|
}
|
2021-03-11 13:24:28 +08:00
|
|
|
|
2022-12-12 02:12:03 +08:00
|
|
|
rt_section(".irq.cache")
|
2021-04-09 17:35:26 +08:00
|
|
|
void cache_init(void)
|
|
|
|
{
|
2021-05-13 10:22:09 +08:00
|
|
|
os_cache_init();
|
2021-11-18 04:57:15 +08:00
|
|
|
rt_mutex_init(&mutex_spiflash, "flash_mutex", RT_IPC_FLAG_PRIO);
|
|
|
|
rt_mutex_init(&mutex_cache, "cache_mutex", RT_IPC_FLAG_PRIO);
|
2021-04-09 17:35:26 +08:00
|
|
|
}
|
|
|
|
|
2022-12-12 02:12:03 +08:00
|
|
|
rt_section(".irq.cache")
|
2021-04-09 17:35:26 +08:00
|
|
|
void os_spiflash_lock(void)
|
|
|
|
{
|
2021-11-30 17:26:39 +08:00
|
|
|
if ((rt_thread_self() != RT_NULL) && (rt_interrupt_nest == 0))
|
|
|
|
{
|
2021-04-09 17:35:26 +08:00
|
|
|
rt_mutex_take(&mutex_spiflash, RT_WAITING_FOREVER);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-12 02:12:03 +08:00
|
|
|
rt_section(".irq.cache")
|
2021-04-09 17:35:26 +08:00
|
|
|
void os_spiflash_unlock(void)
|
|
|
|
{
|
2021-11-30 17:26:39 +08:00
|
|
|
if ((rt_thread_self() != RT_NULL) && (rt_interrupt_nest == 0))
|
|
|
|
{
|
2021-04-09 17:35:26 +08:00
|
|
|
rt_mutex_release(&mutex_spiflash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-12 02:12:03 +08:00
|
|
|
rt_section(".irq.cache")
|
2021-05-13 10:22:09 +08:00
|
|
|
void os_cache_lock(void)
|
|
|
|
{
|
2021-11-30 17:26:39 +08:00
|
|
|
if ((rt_thread_self() != RT_NULL) && (rt_interrupt_nest == 0))
|
|
|
|
{
|
2021-05-13 10:22:09 +08:00
|
|
|
rt_mutex_take(&mutex_cache, RT_WAITING_FOREVER);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-12 02:12:03 +08:00
|
|
|
rt_section(".irq.cache")
|
2021-05-13 10:22:09 +08:00
|
|
|
void os_cache_unlock(void)
|
|
|
|
{
|
2021-11-30 17:26:39 +08:00
|
|
|
if ((rt_thread_self() != RT_NULL) && (rt_interrupt_nest == 0))
|
|
|
|
{
|
2021-05-13 10:22:09 +08:00
|
|
|
rt_mutex_release(&mutex_cache);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-12 02:12:03 +08:00
|
|
|
rt_section(".irq.err.str")
|
2021-04-09 17:35:26 +08:00
|
|
|
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.
|
2021-11-08 09:58:25 +08:00
|
|
|
* @note (IRQ in Flash: %x %x - %x %x\n, -, rt_interrupt_nest, PC, miss_addr)
|
|
|
|
* miss_addr: The address in map file minus 0x10000000
|
2021-04-09 17:35:26 +08:00
|
|
|
*/
|
2022-12-12 02:12:03 +08:00
|
|
|
rt_section(".irq.err")
|
2021-04-09 17:35:26 +08:00
|
|
|
void exception_isr(void)
|
|
|
|
{
|
2021-08-29 04:48:08 +08:00
|
|
|
#if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
|
2021-04-09 17:35:26 +08:00
|
|
|
extern long list_thread(void);
|
2021-08-26 07:10:01 +08:00
|
|
|
#endif
|
2021-04-09 17:35:26 +08:00
|
|
|
sys_error_hook(1);
|
|
|
|
|
2022-01-08 23:29:41 +08:00
|
|
|
#if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
|
2021-04-09 17:35:26 +08:00
|
|
|
rt_console_set_device(RT_NULL);
|
2023-04-04 21:06:27 +08:00
|
|
|
rt_kprintf(stack_info, rt_thread_self()->sp, rt_thread_self()->parent.name);
|
2021-08-12 17:12:52 +08:00
|
|
|
#endif
|
2021-04-09 17:35:26 +08:00
|
|
|
|
2021-11-30 17:26:39 +08:00
|
|
|
while (1);
|
2021-04-09 17:35:26 +08:00
|
|
|
}
|