bsp.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include "bsp/bsp.h"
  2. #include "version.h"
  3. static void wdog_enable(void);
  4. static void sys_tick_init(void);
  5. void bsp_init(void){
  6. wdog_enable();
  7. sys_tick_init();
  8. }
  9. static void sys_tick_init(void){
  10. /* setup systick timer for 1000Hz interrupts */
  11. SysTick_Config(SystemCoreClock / 1000);
  12. /* configure the systick handler priority */
  13. NVIC_SetPriority(SysTick_IRQn, 0x00U);
  14. }
  15. void system_reboot(void){
  16. NVIC_SystemReset();
  17. }
  18. void wdog_reload(void){
  19. #if CONFIG_DEBUG == 0
  20. fwdgt_counter_reload();
  21. #endif
  22. }
  23. static void wdog_enable(void)
  24. {
  25. #if CONFIG_DEBUG == 0
  26. /* enable IRC40K */
  27. rcu_osci_on(RCU_IRC40K);
  28. /* wait till IRC40K is ready */
  29. while(SUCCESS != rcu_osci_stab_wait(RCU_IRC40K)){
  30. }
  31. /* confiure FWDGT counter clock: 40KHz(IRC40K) / 256 = 0.15625 KHz */
  32. fwdgt_config(4*40000UL/256, FWDGT_PSC_DIV256);
  33. /* after 4 seconds to generate a reset */
  34. fwdgt_enable();
  35. #endif
  36. }
  37. /* write value to FWDGT_RLD_RLD bit field */
  38. #define RLD_RLD(regval) (BITS(0,11) & ((uint32_t)(regval) << 0))
  39. int wdog_set_timeout(int wdog_time)
  40. {
  41. #if CONFIG_DEBUG == 0
  42. uint32_t flag_status = RESET;
  43. uint32_t timeout = FWDGT_RLD_TIMEOUT;
  44. /* enable write access to FWDGT_PSC,and FWDGT_RLD */
  45. FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
  46. /* wait until the RUD flag to be reset */
  47. do{
  48. flag_status = FWDGT_STAT & FWDGT_STAT_RUD;
  49. }while((--timeout > 0U) && (RESET != flag_status));
  50. if (RESET != flag_status){
  51. return -1;
  52. }
  53. FWDGT_RLD = RLD_RLD(wdog_time*40000UL/256);
  54. /* reload the counter */
  55. FWDGT_CTL = FWDGT_KEY_RELOAD;
  56. #endif
  57. return 0;
  58. }