bsp.c 2.7 KB

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