bsp.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "bsp/bsp.h"
  2. #include "bsp/bsp_driver.h"
  3. #include "libs/logger.h"
  4. #include "os/os_types.h"
  5. #include "version.h"
  6. static void wdog_enable(void);
  7. void bsp_init(void){
  8. wdog_enable();
  9. gd32_bkp_init();
  10. dbg_periph_enable(DBG_TIMER0_HOLD);
  11. dbg_periph_enable(DBG_TIMER1_HOLD);
  12. nvic_priority_group_set(NVIC_PRIGROUP_PRE4_SUB0);
  13. systick_open();
  14. task_ticks_enable();
  15. gpio_pin_init();
  16. shark_uart_init(SHARK_UART0);
  17. }
  18. void system_reboot(void){
  19. NVIC_SystemReset();
  20. }
  21. void systick_close(void)
  22. {
  23. SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
  24. }
  25. void systick_open(void)
  26. {
  27. SysTick_Config(SystemCoreClock / 1000);
  28. }
  29. u8 mcu_chip_id(u8 *buff)
  30. {
  31. u32 values[] = { REG32(0x1FFFF7E8), REG32(0x1FFFF7EC), REG32(0x1FFFF7F0), REG32(0x1FFFF7E0) };
  32. memcpy(buff, values, sizeof(values));
  33. return sizeof(values);
  34. }
  35. static u32 _mcu_rst_status = 0xFFFFFFFF;
  36. u32 get_mcu_reset_source(void)
  37. {
  38. if (_mcu_rst_status == 0xFFFFFFFF) {
  39. _mcu_rst_status = RCU_RSTSCK;
  40. rcu_all_reset_flag_clear();
  41. }
  42. return _mcu_rst_status;
  43. }
  44. void wdog_reload(void){
  45. #if CONFIG_DEBUG == 0
  46. fwdgt_counter_reload();
  47. #endif
  48. }
  49. static void wdog_enable(void)
  50. {
  51. #if CONFIG_DEBUG == 0
  52. /* enable IRC40K */
  53. rcu_osci_on(RCU_IRC40K);
  54. /* wait till IRC40K is ready */
  55. while(SUCCESS != rcu_osci_stab_wait(RCU_IRC40K)){
  56. }
  57. /* confiure FWDGT counter clock: 40KHz(IRC40K) / 256 = 0.15625 KHz */
  58. fwdgt_config(4*40000UL/256, FWDGT_PSC_DIV256);
  59. /* after 4 seconds to generate a reset */
  60. fwdgt_enable();
  61. #endif
  62. }
  63. /* write value to FWDGT_RLD_RLD bit field */
  64. #define RLD_RLD(regval) (BITS(0,11) & ((uint32_t)(regval) << 0))
  65. int wdog_set_timeout(int wdog_time)
  66. {
  67. #if CONFIG_DEBUG == 0
  68. uint32_t flag_status = RESET;
  69. uint32_t timeout = FWDGT_RLD_TIMEOUT;
  70. /* enable write access to FWDGT_PSC,and FWDGT_RLD */
  71. FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
  72. /* wait until the RUD flag to be reset */
  73. do{
  74. flag_status = FWDGT_STAT & FWDGT_STAT_RUD;
  75. }while((--timeout > 0U) && (RESET != flag_status));
  76. if (RESET != flag_status){
  77. return -1;
  78. }
  79. FWDGT_RLD = RLD_RLD(wdog_time*40000UL/256);
  80. /* reload the counter */
  81. FWDGT_CTL = FWDGT_KEY_RELOAD;
  82. #endif
  83. return 0;
  84. }