bsp.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "bsp/bsp.h"
  2. #include "libs/logger.h"
  3. #include "os/os_type.h"
  4. #include "bsp/uart.h"
  5. #include "version.h"
  6. static void wdog_enable(void);
  7. static void normal_task_timer_init(void);
  8. void bsp_init(void){
  9. wdog_enable();
  10. cpu_counts_enable();
  11. #if LOG_UART==1
  12. shark_uart_init(SHARK_UART0);
  13. #endif
  14. normal_task_timer_init();
  15. }
  16. void system_reboot(void){
  17. NVIC_SystemReset();
  18. }
  19. void wdog_reload(void){
  20. #if CONFIG_DEBUG == 0
  21. fwdgt_counter_reload();
  22. #endif
  23. }
  24. static void wdog_enable(void)
  25. {
  26. #if CONFIG_DEBUG == 0
  27. /* enable IRC40K */
  28. rcu_osci_on(RCU_IRC40K);
  29. /* wait till IRC40K is ready */
  30. while(SUCCESS != rcu_osci_stab_wait(RCU_IRC40K)){
  31. }
  32. /* confiure FWDGT counter clock: 40KHz(IRC40K) / 256 = 0.15625 KHz */
  33. fwdgt_config(4*40000UL/256, FWDGT_PSC_DIV256);
  34. /* after 4 seconds to generate a reset */
  35. fwdgt_enable();
  36. #endif
  37. }
  38. /* write value to FWDGT_RLD_RLD bit field */
  39. #define RLD_RLD(regval) (BITS(0,11) & ((uint32_t)(regval) << 0))
  40. int wdog_set_timeout(int wdog_time)
  41. {
  42. #if CONFIG_DEBUG == 0
  43. uint32_t flag_status = RESET;
  44. uint32_t timeout = FWDGT_RLD_TIMEOUT;
  45. /* enable write access to FWDGT_PSC,and FWDGT_RLD */
  46. FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
  47. /* wait until the RUD flag to be reset */
  48. do{
  49. flag_status = FWDGT_STAT & FWDGT_STAT_RUD;
  50. }while((--timeout > 0U) && (RESET != flag_status));
  51. if (RESET != flag_status){
  52. return -1;
  53. }
  54. FWDGT_RLD = RLD_RLD(wdog_time*40000UL/256);
  55. /* reload the counter */
  56. FWDGT_CTL = FWDGT_KEY_RELOAD;
  57. #endif
  58. return 0;
  59. }
  60. //10 ms
  61. static void normal_task_timer_init(void) {
  62. timer_parameter_struct timer_initpara;
  63. u32 timer = TIMER5;
  64. rcu_periph_clock_enable(RCU_TIMER5);
  65. timer_deinit(timer);
  66. memset(&timer_initpara, 0, sizeof(timer_initpara));
  67. timer_initpara.prescaler = 12000 - 1; //clk 10000
  68. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  69. timer_initpara.period = 100;
  70. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  71. timer_initpara.repetitioncounter = 0;
  72. timer_init(timer,&timer_initpara);
  73. timer_counter_value_config(timer, 0);
  74. timer_autoreload_value_config(timer, 100);
  75. timer_counter_up_direction(timer);
  76. timer_auto_reload_shadow_enable(timer);
  77. timer_interrupt_enable(timer, TIMER_INT_UP);
  78. timer_interrupt_flag_clear(timer, TIMER_INT_FLAG_UP);
  79. nvic_irq_enable(TIMER5_IRQn, 5, 0);
  80. timer_enable(timer);
  81. }