board.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (c) 2019 Winner Microelectronics Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-09-15 flyingcys 1st version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include "wm_type_def.h"
  13. #include "misc.h"
  14. #include "wm_cpu.h"
  15. #include "wm_debug.h"
  16. #include "wm_io.h"
  17. #include "wm_gpio_afsel.h"
  18. #include "wm_crypto_hard.h"
  19. #include "wm_hostspi.h"
  20. #include "wm_flash.h"
  21. #include "wm_internal_flash.h"
  22. #include "wm_fwup.h"
  23. #include "wm_pmu.h"
  24. #include "drv_uart.h"
  25. #include "board.h"
  26. #define FW_MAJOR_VER 0x03
  27. #define FW_MINOR_VER 0x00
  28. #define FW_PATCH_VER 0x00
  29. const char FirmWareVer[4] =
  30. {
  31. 'G',
  32. FW_MAJOR_VER, /* Main version */
  33. FW_MINOR_VER, /* Subversion */
  34. FW_PATCH_VER /* Internal version */
  35. };
  36. const char HwVer[6] =
  37. {
  38. 'H',
  39. 0x1,
  40. 0x0,
  41. 0x0,
  42. 0x0,
  43. 0x0
  44. };
  45. const unsigned int HZ = RT_TICK_PER_SECOND;
  46. struct tls_ethif *tls_netif_get_ethif(void)
  47. {
  48. return RT_NULL;
  49. }
  50. int tls_os_get_type(void)
  51. {
  52. return 0;
  53. }
  54. err_t tls_dhcp_stop(void)
  55. {
  56. return 0;
  57. }
  58. u8 tls_get_isr_count(void)
  59. {
  60. return 0;
  61. }
  62. void *mem_alloc_debug(u32 size)
  63. {
  64. return rt_malloc(size);
  65. }
  66. void mem_free_debug(void *p)
  67. {
  68. rt_free(p);
  69. }
  70. void disp_version_info(void)
  71. {
  72. extern const char WiFiVer[];
  73. TLS_DBGPRT_INFO("\n\n");
  74. TLS_DBGPRT_INFO("****************************************************************\n");
  75. TLS_DBGPRT_INFO("* *\n");
  76. TLS_DBGPRT_INFO("* Copyright (C) 2014 WinnerMicro Co. Ltd. *\n");
  77. TLS_DBGPRT_INFO("* All rights reserved. *\n");
  78. TLS_DBGPRT_INFO("* WinnerMicro Firmware Version: %x.%x.%X *\n",
  79. FirmWareVer[1], FirmWareVer[2], FirmWareVer[3]);
  80. TLS_DBGPRT_INFO("* WinnerMicro Hardware Version: %x.%x.%x.%x.%x *\n",
  81. HwVer[1], HwVer[2], HwVer[3], HwVer[4], HwVer[5]);
  82. TLS_DBGPRT_INFO("* *\n");
  83. TLS_DBGPRT_INFO("* WinnerMicro Wi-Fi Lib Version: %x.%x.%x *\n",
  84. WiFiVer[0], WiFiVer[1], WiFiVer[2]);
  85. TLS_DBGPRT_INFO("****************************************************************\n");
  86. }
  87. void wm_gpio_config(void)
  88. {
  89. /* must call first */
  90. wm_gpio_af_disable();
  91. /*MASTER SPI configuratioin*/
  92. wm_spi_cs_config(WM_IO_PA_02);
  93. wm_spi_ck_config(WM_IO_PA_11);
  94. wm_spi_di_config(WM_IO_PA_03);
  95. wm_spi_do_config(WM_IO_PA_09);
  96. }
  97. static int wm_infsl_init(void)
  98. {
  99. tls_spi_init();
  100. tls_spifls_init();
  101. tls_fls_init();
  102. /*initialize flash layout parameter according to image type*/
  103. tls_fls_layout_init();
  104. return 0;
  105. }
  106. INIT_DEVICE_EXPORT(wm_infsl_init);
  107. static void _idle_hook_callback(void)
  108. {
  109. #if !defined(__CC_ARM)
  110. __asm volatile ("wfi");
  111. #else
  112. __asm
  113. {
  114. WFI;
  115. }
  116. #endif
  117. }
  118. static void _thread_inited_hook(rt_thread_t thread)
  119. {
  120. rt_uint32_t stack_size, stk_start;
  121. stack_size = (rt_uint32_t)thread->stack_size;
  122. stk_start = (rt_uint32_t)thread->stack_addr;
  123. if ((stk_start + stack_size) >= TASK_STACK_USING_MEM_UPPER_RANGE)
  124. {
  125. rt_kprintf("thread[%s] stack only between 0x%08x and 0x%08x, please use rt_create_thread()!!\n",
  126. thread->name, TASK_STACK_USING_MEM_LOWER_RANGE, TASK_STACK_USING_MEM_UPPER_RANGE);
  127. }
  128. RT_ASSERT((stk_start + stack_size) < TASK_STACK_USING_MEM_UPPER_RANGE);
  129. }
  130. void wm_sys_clk_config(void)
  131. {
  132. tls_sys_clk sysclk;
  133. tls_sys_clk_set(CPU_CLK_80M);
  134. tls_sys_clk_get(&sysclk);
  135. SysTick_Config(sysclk.cpuclk * UNIT_MHZ / RT_TICK_PER_SECOND);
  136. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
  137. tls_pmu_clk_select(0);
  138. }
  139. /**
  140. * This is the timer interrupt service routine.
  141. *
  142. */
  143. void OS_CPU_SysTickHandler(void)
  144. {
  145. /* enter interrupt */
  146. rt_interrupt_enter();
  147. rt_tick_increase();
  148. /* leave interrupt */
  149. rt_interrupt_leave();
  150. }
  151. /**
  152. * This function will initial board.
  153. */
  154. void rt_hw_board_init(void)
  155. {
  156. /* must call first to configure gpio Alternate functions according the hardware design */
  157. wm_gpio_config();
  158. wm_sys_clk_config();
  159. #ifdef RT_USING_HEAP
  160. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  161. #endif
  162. #ifdef RT_USING_COMPONENTS_INIT
  163. rt_components_board_init();
  164. #endif
  165. #ifdef RT_USING_CONSOLE
  166. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  167. #endif
  168. #if TLS_CONFIG_HARD_CRYPTO
  169. tls_crypto_init();
  170. #endif
  171. NVIC_SystemLPConfig(NVIC_LP_SLEEPDEEP, ENABLE);
  172. rt_thread_idle_sethook(_idle_hook_callback);
  173. rt_thread_inited_sethook(_thread_inited_hook);
  174. }
  175. #include <wm_watchdog.h>
  176. void rt_hw_cpu_reset(void)
  177. {
  178. extern void tls_sys_reset(void);
  179. tls_sys_reset();
  180. }
  181. /**
  182. * The time delay function.
  183. *
  184. * @param microseconds.
  185. */
  186. #include "wm_regs.h"
  187. void rt_hw_us_delay(rt_uint32_t us)
  188. {
  189. rt_uint32_t ticks;
  190. rt_uint32_t told, tnow, tcnt = 0;
  191. rt_uint32_t reload = SysTick->LOAD;
  192. ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
  193. told = SysTick->VAL;
  194. while (1)
  195. {
  196. tnow = SysTick->VAL;
  197. if (tnow != told)
  198. {
  199. if (tnow < told)
  200. {
  201. tcnt += told - tnow;
  202. }
  203. else
  204. {
  205. tcnt += reload - tnow + told;
  206. }
  207. told = tnow;
  208. if (tcnt >= ticks)
  209. {
  210. break;
  211. }
  212. }
  213. }
  214. }
  215. #ifdef RT_USING_FINSH
  216. #include <finsh.h>
  217. static void reboot(uint8_t argc, char **argv)
  218. {
  219. rt_hw_cpu_reset();
  220. }
  221. FINSH_FUNCTION_EXPORT_ALIAS(reboot, __cmd_reboot, Reboot System);
  222. #endif /* RT_USING_FINSH */
  223. /*@}*/