bsp_sys.c 6.3 KB

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