bsp.c 3.1 KB

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