bsp.c 2.0 KB

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