rt-thread-official/bsp/apollo2/board/board.c

169 lines
4.1 KiB
C
Raw Normal View History

2017-09-15 18:10:51 +08:00
/*
2021-03-29 07:20:47 +08:00
* Copyright (c) 2006-2021, RT-Thread Development Team
2017-09-15 18:10:51 +08:00
*
2021-03-29 07:20:47 +08:00
* SPDX-License-Identifier: Apache-2.0
2017-09-15 18:10:51 +08:00
*
* Change Logs:
* Date Author Notes
* 2017-09-14 Haley first implementation
*/
#include "board.h"
#include <rtthread.h>
#include <rthw.h>
#include "am_mcu_apollo.h"
#include "hal/am_hal_clkgen.h"
#include "hal/am_hal_cachectrl.h"
2017-09-20 11:14:45 +08:00
#include "uart.h"
#ifdef __CC_ARM
extern int Image$$RW_IRAM1$$ZI$$Limit;
#define AM_SRAM_BEGIN (&Image$$RW_IRAM1$$ZI$$Limit)
#elif __ICCARM__
#pragma section="HEAP"
#define AM_SRAM_BEGIN (__segment_end("HEAP"))
#else
extern int __bss_end;
#define AM_SRAM_BEGIN (&__bss_end)
2017-09-20 11:14:45 +08:00
#endif
2017-09-15 18:10:51 +08:00
#define TICK_RATE_HZ RT_TICK_PER_SECOND
#define SYSTICK_CLOCK_HZ ( 32768UL )
2017-09-20 11:14:45 +08:00
#define WAKE_INTERVAL ( (uint32_t) ((SYSTICK_CLOCK_HZ / TICK_RATE_HZ)))
2017-09-15 18:10:51 +08:00
/**
* This is the timer interrupt service routine.
*
*/
void am_stimer_cmpr0_isr(void)
{
/* Check the timer interrupt status */
am_hal_stimer_int_clear(AM_HAL_STIMER_INT_COMPAREA);
am_hal_stimer_compare_delta_set(0, WAKE_INTERVAL);
2017-09-20 11:14:45 +08:00
2017-09-15 18:10:51 +08:00
if (rt_thread_self() != RT_NULL)
{
2017-09-20 11:14:45 +08:00
/* enter interrupt */
rt_interrupt_enter();
2017-09-15 18:10:51 +08:00
2017-09-20 11:14:45 +08:00
rt_tick_increase();
2017-09-15 18:10:51 +08:00
2017-09-20 11:14:45 +08:00
/* leave interrupt */
rt_interrupt_leave();
2017-09-15 18:10:51 +08:00
}
}
/**
* This is the SysTick Configure.
*
*/
void SysTick_Configuration(void)
{
/* Enable compare A interrupt in STIMER */
am_hal_stimer_int_enable(AM_HAL_STIMER_INT_COMPAREA);
2017-09-20 11:14:45 +08:00
/* Enable the timer interrupt in the NVIC, making sure to use the appropriate priority level */
2017-09-15 18:10:51 +08:00
am_hal_interrupt_enable(AM_HAL_INTERRUPT_STIMER_CMPR0);
/* Configure the STIMER and run */
am_hal_stimer_config(AM_HAL_STIMER_CFG_CLEAR | AM_HAL_STIMER_CFG_FREEZE);
am_hal_stimer_compare_delta_set(0, WAKE_INTERVAL);
am_hal_stimer_config(AM_HAL_STIMER_XTAL_32KHZ |
AM_HAL_STIMER_CFG_COMPARE_A_ENABLE);
}
/**
* This is the low power operation.
* This function enables several power-saving features of the MCU, and
* disables some of the less-frequently used peripherals. It also sets the
* system clock to 24 MHz.
*/
void am_low_power_init(void)
{
/* Enable internal buck converters */
am_hal_pwrctrl_bucks_init();
/* Initialize for low power in the power control block */
am_hal_pwrctrl_low_power_init();
/* Turn off the voltage comparator as this is enabled on reset */
am_hal_vcomp_disable();
2018-09-21 16:10:44 +08:00
#ifndef RT_USING_RTC
2017-09-15 18:10:51 +08:00
/* Run the RTC off the LFRC */
am_hal_rtc_osc_select(AM_HAL_RTC_OSC_LFRC);
/* Stop the XT and LFRC */
am_hal_clkgen_osc_stop(AM_HAL_CLKGEN_OSC_XT);
2018-09-21 16:10:44 +08:00
//am_hal_clkgen_osc_stop(AM_HAL_CLKGEN_OSC_LFRC);
2017-09-15 18:10:51 +08:00
/* Disable the RTC */
am_hal_rtc_osc_disable();
2018-09-21 16:10:44 +08:00
#endif
2017-09-15 18:10:51 +08:00
}
/**
* This is the deep power save.
*
*/
void deep_power_save(void)
{
2017-09-20 11:14:45 +08:00
am_hal_interrupt_master_disable();
am_hal_sysctrl_sleep(AM_HAL_SYSCTRL_SLEEP_DEEP);
am_hal_interrupt_master_enable();
2017-09-15 18:10:51 +08:00
}
/**
* This function will initial APOLLO2 board.
*/
void rt_hw_board_init(void)
{
2017-09-20 11:14:45 +08:00
/* Set the system clock to maximum frequency */
am_hal_clkgen_sysclk_select(AM_HAL_CLKGEN_SYSCLK_MAX);
/* Set the default cache configuration */
am_hal_cachectrl_enable(&am_hal_cachectrl_defaults);
2017-09-15 18:10:51 +08:00
/* Configure the board for low power operation */
2017-09-20 11:14:45 +08:00
am_low_power_init();
/* Config SysTick */
SysTick_Configuration();
#ifdef RT_USING_IDLE_HOOK
/* Set sleep deep mode */
rt_thread_idle_sethook(deep_power_save);
#ifndef NO_FPU
/* Enable the floating point module, and configure the core for lazy stacking */
am_hal_sysctrl_fpu_enable();
am_hal_sysctrl_fpu_stacking_enable(true);
#else
am_hal_sysctrl_fpu_disable();
#endif
/* Turn off unused Flash & SRAM */
am_hal_pwrctrl_memory_enable(AM_HAL_PWRCTRL_MEMEN_FLASH512K);
2018-09-21 16:10:44 +08:00
//am_hal_pwrctrl_memory_enable(AM_HAL_PWRCTRL_MEMEN_SRAM32K);
2017-09-20 11:14:45 +08:00
2017-09-15 18:10:51 +08:00
#endif
2017-09-20 11:14:45 +08:00
2017-09-15 18:10:51 +08:00
#ifdef RT_USING_CONSOLE
2017-09-20 11:14:45 +08:00
rt_hw_uart_init();
2017-09-15 18:10:51 +08:00
rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
#endif
#ifdef RT_USING_COMPONENTS_INIT
rt_components_board_init();
#endif
2017-09-20 11:14:45 +08:00
#ifdef RT_USING_HEAP
rt_system_heap_init((void*)AM_SRAM_BEGIN, (void*)AM_SRAM_END);
#endif
2017-09-15 18:10:51 +08:00
}
/*@}*/