bsp.c 2.8 KB

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