bsp.c 2.8 KB

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