bsp_sys.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #include "include.h"
  2. #include "driver_lowpwr.h"
  3. #include "bsp_saradc_key.h"
  4. #include "usb_com.h"
  5. #include "usb_hid.h"
  6. #include "usb_audio.h"
  7. #include "usb_vendor.h"
  8. xcfg_cb_t xcfg_cb;
  9. sys_cb_t sys_cb AT(.buf.bsp.sys_cb);
  10. u16 tmr5ms_cnt;
  11. AT(.text.bsp.sys.dbg)
  12. void sys_flash_guid_dump(void)
  13. {
  14. flash_id_t flash_id;
  15. os_spiflash_id_get(flash_id);
  16. printf("Flash GUID: ");
  17. print_r(flash_id, sizeof(flash_id_t));
  18. }
  19. void sys_ram_info_dump(void)
  20. {
  21. extern u32 __comm_size;
  22. extern u32 __bss_size;
  23. extern u32 __bram_size;
  24. extern u32 __heap_size;
  25. extern u32 __bram_vma;
  26. extern u32 __bram_ram_size;
  27. extern u32 __data_vma;
  28. extern u32 __data_ram_size;
  29. extern u32 __comm_vma;
  30. extern u32 __comm_ram_size;
  31. #if COMM_EX_SIZE
  32. extern u32 __commEx_size;
  33. extern u32 __commEx_vma;
  34. extern u32 __commEx_ram_size;
  35. #define commEx_free ((u32)&__commEx_ram_size - (u32)&__commEx_size)
  36. #define __show(...) printf(__VA_ARGS__)
  37. #else
  38. #define commEx_free 0
  39. #define __show(...)
  40. #endif
  41. u32 comm_free = (u32)&__comm_ram_size - (u32)&__comm_size;
  42. u32 bss_free = (u32)&__data_ram_size - (u32)&__bss_size - (u32)&__heap_size;
  43. u32 bram_free = (u32)&__bram_ram_size - (u32)&__bram_size;
  44. u32 ram_total = comm_free + commEx_free + bss_free + bram_free;
  45. printf("Ram_info:\n");
  46. printf("bss: remain:%6d(Byte), total:%6d(Byte), range:[0x%x - 0x%x]\n", bss_free, (u32)&__data_ram_size, (u32)&__data_vma, (u32)&__data_vma + (u32)&__data_ram_size);
  47. printf("comm: remain:%6d(Byte), total:%6d(Byte), range:[0x%x - 0x%x]\n", comm_free, (u32)&__comm_ram_size, (u32)&__comm_vma, (u32)&__comm_vma + (u32)&__comm_ram_size);
  48. __show("commEx: remain:%6d(Byte), total:%6d(Byte), range:[0x%x - 0x%x]\n", commEx_free, (u32)&__commEx_ram_size, (u32)&__commEx_vma, (u32)&__commEx_vma + (u32)&__commEx_ram_size);
  49. printf("bram: remain:%6d(Byte), total:%6d(Byte), range:[0x%x - 0x%x]\n", bram_free, (u32)&__bram_ram_size, (u32)&__bram_vma, (u32)&__bram_vma + (u32)&__bram_ram_size);
  50. printf("remain_total: %dK\n",ram_total / 1024);
  51. }
  52. #if USB_EN
  53. AT(.com_text.dev.detect)
  54. void usb_detect(void)
  55. {
  56. int evt = usb_bus_event_check();
  57. switch (evt) {
  58. case USB_BUS_EVENT_DISCONNECT:
  59. msg_enqueue(MSG_SYS_PC_REMOVE);
  60. break;
  61. case USB_BUS_EVENT_CONNECT:
  62. msg_enqueue(MSG_SYS_PC_INSERT);
  63. break;
  64. default:
  65. break;
  66. }
  67. }
  68. #endif
  69. //timer tick interrupt(5ms)
  70. AT(.com_text.timer)
  71. void usr_tmr5ms_thread_do(void)
  72. {
  73. tmr5ms_cnt++;
  74. #if BSP_KEY_EN
  75. bsp_key_scan(0);
  76. bsp_key_status_check();
  77. #endif
  78. #if USB_EN
  79. usb_detect();
  80. #endif
  81. //100ms timer process
  82. if ((tmr5ms_cnt % 20) == 0) {
  83. lowpwr_tout_ticks();
  84. }
  85. //500ms timer process
  86. if ((tmr5ms_cnt % 100) == 0) {
  87. msg_enqueue(MSG_SYS_500MS);
  88. }
  89. //1s timer process
  90. if ((tmr5ms_cnt % 200) == 0) {
  91. msg_enqueue(MSG_SYS_1S);
  92. }
  93. }
  94. AT(.text.bsp.sys.init)
  95. static void bsp_var_init(void)
  96. {
  97. #if SYS_SLEEP_LEVEL
  98. sys_cb.sleep_time = -1L;
  99. sys_cb.sleep_delay = -1L;
  100. if (SYS_SLEEP_TIME != 0) {
  101. sys_cb.sleep_time = (u32)SYS_SLEEP_TIME * 10; //100ms为单位
  102. sys_cb.sleep_delay = sys_cb.sleep_time;
  103. }
  104. #endif
  105. #if SYS_OFF_TIME
  106. sys_cb.pwroff_time = -1L;
  107. sys_cb.pwroff_delay = -1L;
  108. if (SYS_OFF_TIME != 0) {
  109. sys_cb.pwroff_time = (u32)SYS_OFF_TIME * 10; //100ms为单位
  110. sys_cb.pwroff_delay = sys_cb.pwroff_time;
  111. }
  112. #endif
  113. if(!xcfg_cb.ft_osc_cap_en || (xcfg_cb.osci_cap == 0 && xcfg_cb.osco_cap == 0)) { //没有过产测时,使用自定义OSC电容
  114. xcfg_cb.osci_cap = xcfg_cb.uosci_cap;
  115. xcfg_cb.osco_cap = xcfg_cb.uosco_cap;
  116. xcfg_cb.osc_both_cap = xcfg_cb.uosc_both_cap;
  117. }
  118. msg_queue_init();
  119. }
  120. void power_on_check(void)
  121. {
  122. u32 power_on_check_time = SYS_PWRUP_TIME;
  123. if (sys_cb.wakeup_reason != WK_LP_WK0 || power_on_check_time == 0) {
  124. return;
  125. }
  126. #if BSP_KEY_EN
  127. u32 tick = tick_get();
  128. u16 key;
  129. u16 cnt_up = 0;
  130. while (1) {
  131. WDT_CLR();
  132. delay_5ms(1);
  133. key = bsp_key_scan(1);
  134. if (!tick_check_expire(tick, power_on_check_time)) {
  135. if (key != KEY_ID_PP) {
  136. cnt_up++;
  137. if(cnt_up > 5){
  138. func_pwroff();
  139. }
  140. }else{
  141. cnt_up = 0;
  142. }
  143. } else {
  144. bsp_key_set_power_on_flag(true);
  145. return;
  146. }
  147. }
  148. #endif
  149. }
  150. AT(.text.bsp.sys.init)
  151. void bsp_sys_init(void)
  152. {
  153. uint8_t pmu_cfg = BUCK_MODE_EN * PMU_CFG_BUCK_EN;
  154. #if !BSP_CHARGE_EN
  155. pmu_cfg |= PMU_CFG_CHARGE_DIS;
  156. #endif
  157. #if VUSB_2_VDDIO_EN
  158. pmu_cfg |= PMU_CFG_VUSB_TO_VDDIO;
  159. #endif
  160. if (!xcfg_init(&xcfg_cb, sizeof(xcfg_cb))) { //获取配置参数
  161. printf("xcfg init error\n");
  162. }
  163. bsp_var_init();
  164. #if BSP_SARADC_EN
  165. bsp_saradc_init();
  166. #endif
  167. #if BSP_KEY_EN
  168. bsp_key_init();
  169. #endif
  170. power_on_check();
  171. // power init
  172. pmu_init(pmu_cfg);
  173. // clock init
  174. sys_clk_set(SYS_CLK_SEL);
  175. bsp_param_init();
  176. xosc_init();
  177. // enable user timer
  178. sys_set_tmr_enable(1, 1);
  179. #if BSP_CHARGE_EN
  180. bsp_charge_init();
  181. #endif
  182. #if BSP_VBAT_DETECT_EN
  183. bsp_vbat_detect_init();
  184. #endif // BSP_VBAT_DETECT_EN
  185. #if BSP_SDADC_EN
  186. pmu_set_vddio(PMU_VDDIO_LEVEL);
  187. bsp_sdadc_init();
  188. #if AUDIO_OUT_LOCAL_DEBUG
  189. bsp_sddac_init();
  190. #endif // AUDIO_OUT_LOCAL_DEBUG
  191. #endif // BSP_SDADC_EN
  192. #if BSP_MOUSE_SENSEN_EN
  193. bsp_mouse_sense_init();
  194. #endif // BSP_MOUSE_SENSEN_EN
  195. #if USB_EN
  196. usb_device_init();
  197. #endif // USB_EN
  198. func_cb.sta = FUNC_WIRELESS;
  199. printf("%s, %d\n",__func__, func_cb.sta);
  200. }
  201. void bsp_periph_init(void)
  202. {
  203. #if BSP_UART_DEBUG_EN
  204. bsp_uart_debug_init();
  205. #endif
  206. #if BSP_SARADC_EN
  207. bsp_saradc_init();
  208. #endif
  209. #if BSP_ADKEY_EN
  210. bsp_adkey_init();
  211. #endif
  212. #if BSP_VBAT_DETECT_EN
  213. bsp_vbat_detect_init();
  214. #endif
  215. #if BSP_SDADC_EN
  216. bsp_sdadc_init();
  217. #if AUDIO_OUT_LOCAL_DEBUG
  218. bsp_sddac_init();
  219. #endif // AUDIO_OUT_LOCAL_DEBUG
  220. #endif // BSP_SDADC_EN
  221. #if BSP_MOUSE_SENSEN_EN
  222. bsp_mouse_sense_init();
  223. #endif // BSP_MOUSE_SENSEN_EN
  224. #if USB_EN
  225. usb_init();
  226. #endif
  227. }