bsp.c 2.6 KB

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