bsp.c 2.6 KB

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