| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- #include "bsp/bsp.h"
- #include "bsp/gd32_bkp.h"
- #include "libs/logger.h"
- #include "os/os_type.h"
- #include "bsp/uart.h"
- #include "bsp/timer_count32.h"
- #include "version.h"
- static void wdog_enable(void);
- static void normal_task_timer_init(void);
- void bsp_init(void){
- wdog_enable();
- gd32_bkp_init();
- dbg_periph_enable(DBG_TIMER0_HOLD);
- dbg_periph_enable(DBG_TIMER1_HOLD);
- dbg_periph_enable(DBG_TIMER2_HOLD);
- cpu_counts_enable();
- timer_count32_init();
- shark_uart_init(SHARK_UART0);
- normal_task_timer_init();
- }
- void system_reboot(void){
- NVIC_SystemReset();
- }
- void wdog_reload(void){
- #if CONFIG_DEBUG == 0
- fwdgt_counter_reload();
- #endif
- }
- static void wdog_enable(void)
- {
- #if CONFIG_DEBUG == 0
- /* enable IRC40K */
- rcu_osci_on(RCU_IRC40K);
- /* wait till IRC40K is ready */
- while(SUCCESS != rcu_osci_stab_wait(RCU_IRC40K)){
- }
- /* confiure FWDGT counter clock: 40KHz(IRC40K) / 256 = 0.15625 KHz */
- fwdgt_config(4*40000UL/256, FWDGT_PSC_DIV256);
- /* after 4 seconds to generate a reset */
- fwdgt_enable();
- #endif
- }
- /* write value to FWDGT_RLD_RLD bit field */
- #define RLD_RLD(regval) (BITS(0,11) & ((uint32_t)(regval) << 0))
- int wdog_set_timeout(int wdog_time)
- {
- #if CONFIG_DEBUG == 0
- uint32_t flag_status = RESET;
- uint32_t timeout = FWDGT_RLD_TIMEOUT;
- /* enable write access to FWDGT_PSC,and FWDGT_RLD */
- FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
-
- /* wait until the RUD flag to be reset */
- do{
- flag_status = FWDGT_STAT & FWDGT_STAT_RUD;
- }while((--timeout > 0U) && (RESET != flag_status));
-
- if (RESET != flag_status){
- return -1;
- }
-
- FWDGT_RLD = RLD_RLD(wdog_time*40000UL/256);
- /* reload the counter */
- FWDGT_CTL = FWDGT_KEY_RELOAD;
- #endif
- return 0;
- }
- //10 ms
- static void normal_task_timer_init(void) {
- timer_parameter_struct timer_initpara;
- u32 timer = TIMER5;
- rcu_periph_clock_enable(RCU_TIMER5);
- timer_deinit(timer);
-
- memset(&timer_initpara, 0, sizeof(timer_initpara));
- timer_initpara.prescaler = 12000 - 1; //clk 10000
- timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
- timer_initpara.period = 100;
- timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
- timer_initpara.repetitioncounter = 0;
- timer_init(timer,&timer_initpara);
- timer_counter_value_config(timer, 0);
- timer_autoreload_value_config(timer, 100);
- timer_counter_up_direction(timer);
- timer_auto_reload_shadow_enable(timer);
- timer_interrupt_enable(timer, TIMER_INT_UP);
- timer_interrupt_flag_clear(timer, TIMER_INT_FLAG_UP);
- nvic_irq_enable(TIMER5_IRQn, 5, 0);
- timer_enable(timer);
- }
|