bsp.c 1.7 KB

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