bsp.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include "bsp/shark_bsp.h"
  2. #include "bsp/gpio.h"
  3. #include "bsp/uart.h"
  4. #include "bsp/AT24CXX.h"
  5. #include "bsp/shark_rtc.h"
  6. #include "bsp/clock.h"
  7. #include "bsp/fmc_flash.h"
  8. #include "bsp/cht8305.h"
  9. #include "libs/logger.h"
  10. #include "libs/shark_task.h"
  11. #include "version.h"
  12. #include <string.h>
  13. #if defined CONFIG_BOARD_SP700
  14. #ifdef SP710_V2
  15. const char iap_board_name[] __attribute__((at(0x08002800))) = "SP710";
  16. #else
  17. const char iap_board_name[] __attribute__((at(0x08002800))) = "SP700";
  18. #endif
  19. #elif defined CONFIG_BOARD_SP600
  20. const char iap_board_name[] __attribute__((at(0x08002800))) = "SP600";
  21. #endif
  22. const char iap_fw_version[] __attribute__((at(0x08002A00))) = CONFIG_VERSION;
  23. const char iap_fw_name[] __attribute__((at(0x08002C00))) = "App";
  24. extern void system_clock_config(void);
  25. extern void SystemCoreClockUpdate(void);
  26. extern void gpio_key_init(void);
  27. static void dianostic_timer_handler(shark_timer_t *timer);
  28. static shark_timer_t _dianostic_stop_timer = {.handler = dianostic_timer_handler};
  29. #ifndef CONFIG_DEBUG
  30. #define CONFIG_DEBUG 0
  31. #endif
  32. static uint32_t reset_source = 0;
  33. static uint32_t backup_reg = 0;
  34. //all board's low level init is here
  35. void bsp_init(void){
  36. reset_source = RCU_RSTSCK;
  37. backup_reg = RTC_BKP0;
  38. wdog_start(4);
  39. shark_rtc_init();
  40. enable_mcu_power();
  41. system_clock_config(); //after dcdc open, MCU can run on full speed
  42. SystemCoreClockUpdate();
  43. rcu_all_reset_flag_clear();
  44. task_ticks_enable();
  45. gpio_init();
  46. set_log_level(MOD_SYSTEM, L_debug);
  47. shark_uart_init(SHARK_UART0);
  48. #if UART_NUM==2
  49. shark_uart_init(SHARK_UART1);
  50. #endif
  51. AT24CXX_Init();
  52. cht8305_init();
  53. AUX_VOL_OPEN(0);
  54. gpio_key_init();
  55. DIANOCTIC_LED(1);
  56. shark_timer_post(&_dianostic_stop_timer, 5000);
  57. RTC_BKP0 = 0;
  58. }
  59. static void dianostic_timer_handler(shark_timer_t *timer) {
  60. DIANOCTIC_LED(0);
  61. }
  62. uint32_t bsp_get_rst_reson(void){
  63. return reset_source;
  64. }
  65. uint32_t bsp_get_backup(void){
  66. return backup_reg;
  67. }
  68. void systick_close(void)
  69. {
  70. SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
  71. }
  72. void systick_open(void)
  73. {
  74. SysTick_Config(SystemCoreClock / 1000);
  75. }
  76. char* bsp_get_fversion(void){
  77. return (char *)iap_fw_version;
  78. }
  79. void system_reboot(void){
  80. NVIC_SystemReset();
  81. }
  82. /* timeout:1-25 */
  83. void wdog_start(int timeout){
  84. #if CONFIG_DEBUG == 0
  85. /* enable IRC40K */
  86. rcu_osci_on(RCU_IRC40K);
  87. /* wait till IRC40K is ready */
  88. while(SUCCESS != rcu_osci_stab_wait(RCU_IRC40K)){
  89. }
  90. /* confiure FWDGT counter clock: 40KHz(IRC40K) / 256 = 0.15625 KHz */
  91. fwdgt_config(timeout*40000UL/256, FWDGT_PSC_DIV256);
  92. /* after 4 seconds to generate a reset */
  93. fwdgt_enable();
  94. #endif
  95. }
  96. void wdog_reload(void){
  97. #if CONFIG_DEBUG == 0
  98. fwdgt_counter_reload();
  99. #endif
  100. }
  101. int wdog_set_timeout(int wdog_time)
  102. {
  103. #if CONFIG_DEBUG == 0
  104. uint32_t flag_status = RESET;
  105. uint32_t timeout = FWDGT_RLD_TIMEOUT;
  106. /* enable write access to FWDGT_PSC,and FWDGT_RLD */
  107. FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
  108. /* wait until the RUD flag to be reset */
  109. do{
  110. flag_status = FWDGT_STAT & FWDGT_STAT_RUD;
  111. }while((--timeout > 0U) && (RESET != flag_status));
  112. if (RESET != flag_status){
  113. return -1;
  114. }
  115. FWDGT_RLD = RLD_RLD(wdog_time*40000UL/256);
  116. /* reload the counter */
  117. FWDGT_CTL = FWDGT_KEY_RELOAD;
  118. #endif
  119. return 0;
  120. }