172 lines
4.6 KiB
C
172 lines
4.6 KiB
C
/*
|
||
* Copyright (c) 2006-2019, RT-Thread Development Team
|
||
*
|
||
* SPDX-License-Identifier: Apache-2.0
|
||
*
|
||
* Change Logs:
|
||
* Date Author Notes
|
||
* 2021-05-24 the first version
|
||
*/
|
||
|
||
#include <rthw.h>
|
||
#include <rtthread.h>
|
||
#include "at32f403a_407.h"
|
||
#include "includes.h"
|
||
#if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
|
||
/*
|
||
* Please modify RT_HEAP_SIZE if you enable RT_USING_HEAP
|
||
* the RT_HEAP_SIZE max value = (sram size - ZI size), 1024 means 1024 bytes
|
||
*/
|
||
#define RT_HEAP_SIZE (19*1024) /* 20KB-1KB to fit 41KB preset buffer in 96KB RAM */
|
||
static rt_uint8_t rt_heap[RT_HEAP_SIZE];
|
||
|
||
RT_WEAK void *rt_heap_begin_get(void)
|
||
{
|
||
return rt_heap;
|
||
}
|
||
|
||
RT_WEAK void *rt_heap_end_get(void)
|
||
{
|
||
return rt_heap + RT_HEAP_SIZE;
|
||
}
|
||
#endif
|
||
|
||
void rt_os_tick_callback(void)
|
||
{
|
||
rt_interrupt_enter();
|
||
|
||
rt_tick_increase();
|
||
|
||
rt_interrupt_leave();
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* This function will initial your board.
|
||
*/
|
||
void rt_hw_board_init(void)
|
||
{
|
||
//#error "TODO 1: OS Tick Configuration."
|
||
/*
|
||
* TODO 1: OS Tick Configuration
|
||
* Enable the hardware timer and call the rt_os_tick_callback function
|
||
* periodically with the frequency RT_TICK_PER_SECOND.
|
||
*/
|
||
wk_system_clock_config();
|
||
system_core_clock_update();
|
||
|
||
/* 使能 FPU(CP10/CP11 全访问)。
|
||
RT-Thread 经 __low_level_init() 接管启动后,IAR cstartup 的 __iar_init_vfp 不会执行;
|
||
若不手动使能,任何 VFP 指令(含 IAR 用 VFP 寄存器优化的 memcpy)
|
||
都会触发 NOCP UsageFault,升级为 HardFault。 */
|
||
SCB->CPACR |= ((3UL << (10 * 2)) | (3UL << (11 * 2)));
|
||
__DSB();
|
||
__ISB();
|
||
|
||
//_SysTick_Config(system_core_clock / RT_TICK_PER_SECOND);
|
||
SysTick_Config(system_core_clock/RT_TICK_PER_SECOND);
|
||
|
||
/* Call components board initial (use INIT_BOARD_EXPORT()) */
|
||
#ifdef RT_USING_COMPONENTS_INIT
|
||
rt_components_board_init();
|
||
#endif
|
||
|
||
#if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
|
||
rt_system_heap_init(rt_heap_begin_get(), rt_heap_end_get());
|
||
#endif
|
||
}
|
||
|
||
#ifdef RT_USING_CONSOLE
|
||
|
||
//#include <LowLevelIOInterface.h>
|
||
|
||
/* --------------------------------------------------------------------------
|
||
* 控制台输出:USART2(PA2=TX, PA3=RX)@115200
|
||
* 惰性初始化:首次调用时自行使能时钟并配置 UART,任意时机都可用
|
||
* (rt_show_version、rt_kprintf、HardFault dump、printf 都走这里)
|
||
* ------------------------------------------------------------------------ */
|
||
static uint8_t console_uart_inited = 0;
|
||
|
||
static void console_uart_init(void)
|
||
{
|
||
gpio_init_type gpio_init_struct;
|
||
|
||
if (console_uart_inited)
|
||
return;
|
||
console_uart_inited = 1;
|
||
|
||
gpio_default_para_init(&gpio_init_struct);
|
||
|
||
/* 使能时钟 */
|
||
crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
|
||
crm_periph_clock_enable(CRM_USART2_PERIPH_CLOCK, TRUE);
|
||
|
||
/* PA2 = USART2_TX(复用推挽输出) */
|
||
gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
|
||
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
|
||
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
|
||
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
|
||
gpio_init_struct.gpio_pins = GPIO_PINS_2;
|
||
gpio_init(GPIOA, &gpio_init_struct);
|
||
|
||
/* PA3 = USART2_RX(输入) */
|
||
gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
|
||
gpio_init_struct.gpio_pins = GPIO_PINS_3;
|
||
gpio_init(GPIOA, &gpio_init_struct);
|
||
|
||
usart_init(USART2, 115200, USART_DATA_8BITS, USART_STOP_1_BIT);
|
||
usart_transmitter_enable(USART2, TRUE);
|
||
usart_receiver_enable(USART2, TRUE);
|
||
usart_parity_selection_config(USART2, USART_PARITY_NONE);
|
||
usart_hardware_flow_control_set(USART2, USART_HARDWARE_FLOW_NONE);
|
||
usart_enable(USART2, TRUE);
|
||
}
|
||
|
||
static void console_uart_putchar(char c)
|
||
{
|
||
while ((USART2->sts & USART_TDBE_FLAG) == RESET)
|
||
{
|
||
}
|
||
USART2->dt = (uint16_t)c;
|
||
}
|
||
|
||
void rt_hw_console_output(const char *str)
|
||
{
|
||
console_uart_init();
|
||
while (*str)
|
||
{
|
||
if (*str == '\n')
|
||
console_uart_putchar('\r');
|
||
console_uart_putchar(*str++);
|
||
}
|
||
}
|
||
|
||
///* printf() 重定向到 USART2(IAR DLib low-level interface) */
|
||
//size_t __write(int handle, const unsigned char *buffer, size_t size)
|
||
//{
|
||
// size_t n;
|
||
//
|
||
// if (handle != _LLIO_STDOUT && handle != _LLIO_STDERR)
|
||
// return 0;
|
||
//
|
||
// console_uart_init();
|
||
// for (n = 0; n < size; n++)
|
||
// {
|
||
// if (buffer[n] == '\n')
|
||
// console_uart_putchar('\r');
|
||
// console_uart_putchar((char)buffer[n]);
|
||
// }
|
||
// return size;
|
||
//}
|
||
|
||
static int uart_init(void)
|
||
{
|
||
/* console 采用惰性初始化,见 rt_hw_console_output / __write */
|
||
return 0;
|
||
}
|
||
INIT_BOARD_EXPORT(uart_init);
|
||
|
||
#endif
|
||
|