bsp.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "bsp/bsp.h"
  2. #include "bsp/gd32_bkp.h"
  3. #include "libs/logger.h"
  4. #include "os/os_type.h"
  5. #include "bsp/uart.h"
  6. #include "bsp/timer_count32.h"
  7. #include "version.h"
  8. static void wdog_enable(void);
  9. static void normal_task_timer_init(void);
  10. void bsp_init(void){
  11. wdog_enable();
  12. gd32_bkp_init();
  13. dbg_periph_enable(DBG_TIMER0_HOLD);
  14. dbg_periph_enable(DBG_TIMER1_HOLD);
  15. dbg_periph_enable(DBG_TIMER2_HOLD);
  16. cpu_counts_enable();
  17. timer_count32_init();
  18. #if LOG_UART==1
  19. shark_uart_init(SHARK_UART0);
  20. #endif
  21. normal_task_timer_init();
  22. }
  23. void system_reboot(void){
  24. NVIC_SystemReset();
  25. }
  26. void wdog_reload(void){
  27. #if CONFIG_DEBUG == 0
  28. fwdgt_counter_reload();
  29. #endif
  30. }
  31. static void wdog_enable(void)
  32. {
  33. #if CONFIG_DEBUG == 0
  34. /* enable IRC40K */
  35. rcu_osci_on(RCU_IRC40K);
  36. /* wait till IRC40K is ready */
  37. while(SUCCESS != rcu_osci_stab_wait(RCU_IRC40K)){
  38. }
  39. /* confiure FWDGT counter clock: 40KHz(IRC40K) / 256 = 0.15625 KHz */
  40. fwdgt_config(4*40000UL/256, FWDGT_PSC_DIV256);
  41. /* after 4 seconds to generate a reset */
  42. fwdgt_enable();
  43. #endif
  44. }
  45. /* write value to FWDGT_RLD_RLD bit field */
  46. #define RLD_RLD(regval) (BITS(0,11) & ((uint32_t)(regval) << 0))
  47. int wdog_set_timeout(int wdog_time)
  48. {
  49. #if CONFIG_DEBUG == 0
  50. uint32_t flag_status = RESET;
  51. uint32_t timeout = FWDGT_RLD_TIMEOUT;
  52. /* enable write access to FWDGT_PSC,and FWDGT_RLD */
  53. FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
  54. /* wait until the RUD flag to be reset */
  55. do{
  56. flag_status = FWDGT_STAT & FWDGT_STAT_RUD;
  57. }while((--timeout > 0U) && (RESET != flag_status));
  58. if (RESET != flag_status){
  59. return -1;
  60. }
  61. FWDGT_RLD = RLD_RLD(wdog_time*40000UL/256);
  62. /* reload the counter */
  63. FWDGT_CTL = FWDGT_KEY_RELOAD;
  64. #endif
  65. return 0;
  66. }
  67. //10 ms
  68. static void normal_task_timer_init(void) {
  69. timer_parameter_struct timer_initpara;
  70. u32 timer = TIMER5;
  71. rcu_periph_clock_enable(RCU_TIMER5);
  72. timer_deinit(timer);
  73. memset(&timer_initpara, 0, sizeof(timer_initpara));
  74. timer_initpara.prescaler = 12000 - 1; //clk 10000
  75. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  76. timer_initpara.period = 100;
  77. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  78. timer_initpara.repetitioncounter = 0;
  79. timer_init(timer,&timer_initpara);
  80. timer_counter_value_config(timer, 0);
  81. timer_autoreload_value_config(timer, 100);
  82. timer_counter_up_direction(timer);
  83. timer_auto_reload_shadow_enable(timer);
  84. timer_interrupt_enable(timer, TIMER_INT_UP);
  85. timer_interrupt_flag_clear(timer, TIMER_INT_FLAG_UP);
  86. nvic_irq_enable(TIMER5_IRQn, 5, 0);
  87. timer_enable(timer);
  88. }