bsp.c 1.7 KB

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