bsp.c 2.1 KB

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