bsp_custom.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /****************************************************************************
  2. *
  3. * Copy right: 2018 Copyrigths of EigenComm Ltd.
  4. * File name: bsp_custom.c
  5. * Description:
  6. * History:
  7. *
  8. ****************************************************************************/
  9. #include <stdio.h>
  10. #include <stdarg.h>
  11. #include "clock_ec616.h"
  12. #include "bsp_custom.h"
  13. #include "slpman_ec616.h"
  14. #include "plat_config.h"
  15. #include "debug_log.h"
  16. #include "os_exception.h"
  17. #if (WDT_FEATURE_ENABLE == 1)
  18. #include "wdt_ec616.h"
  19. #define WDT_TIMEOUT_VALUE (20) // in unit of second, shall be less than 256s
  20. #endif
  21. #include "AppSignal.h"
  22. #ifdef BL_FILE_LOG
  23. extern uint8_t lockoutState;
  24. #endif
  25. extern UINT8 Lockstatus;
  26. void GPR_SetUartClk(void)
  27. {
  28. GPR_ClockDisable(GPR_UART0FuncClk);
  29. GPR_ClockDisable(GPR_UART1FuncClk);
  30. GPR_ClockDisable(GPR_UART2FuncClk);
  31. GPR_SetClockSrc(GPR_UART0FuncClk, GPR_UART0ClkSel_26M);
  32. GPR_SetClockSrc(GPR_UART1FuncClk, GPR_UART1ClkSel_26M);
  33. GPR_SetClockSrc(GPR_UART2FuncClk, GPR_UART1ClkSel_26M);
  34. GPR_ClockEnable(GPR_UART0FuncClk);
  35. GPR_ClockEnable(GPR_UART1FuncClk);
  36. GPR_ClockEnable(GPR_UART2FuncClk);
  37. GPR_SWReset(GPR_ResetUART0Func);
  38. GPR_SWReset(GPR_ResetUART1Func);
  39. GPR_SWReset(GPR_ResetUART2Func);
  40. }
  41. extern ARM_DRIVER_USART Driver_USART0;
  42. extern ARM_DRIVER_USART Driver_USART1;
  43. /*
  44. * set printf uart port
  45. * Parameter: port: for printf
  46. */
  47. static void SetPrintUart(usart_port_t port)
  48. {
  49. if (port == PORT_USART_0)
  50. {
  51. #if (RTE_UART0)
  52. UsartPrintHandle = &CREATE_SYMBOL(Driver_USART, 0);
  53. GPR_ClockDisable(GPR_UART0FuncClk);
  54. GPR_SetClockSrc(GPR_UART0FuncClk, GPR_UART0ClkSel_26M);
  55. GPR_ClockEnable(GPR_UART0FuncClk);
  56. GPR_SWReset(GPR_ResetUART0Func);
  57. #endif
  58. }
  59. else if (port == PORT_USART_1)
  60. {
  61. #if (RTE_UART1)
  62. UsartPrintHandle = &CREATE_SYMBOL(Driver_USART, 1);
  63. GPR_ClockDisable(GPR_UART1FuncClk);
  64. GPR_SetClockSrc(GPR_UART1FuncClk, GPR_UART1ClkSel_26M);
  65. GPR_ClockEnable(GPR_UART1FuncClk);
  66. GPR_SWReset(GPR_ResetUART1Func);
  67. #endif
  68. }
  69. if (UsartPrintHandle == NULL)
  70. return;
  71. UsartPrintHandle->Initialize(NULL);
  72. UsartPrintHandle->PowerControl(ARM_POWER_FULL);
  73. UsartPrintHandle->Control(ARM_USART_MODE_ASYNCHRONOUS | ARM_USART_DATA_BITS_8 |
  74. ARM_USART_PARITY_NONE | ARM_USART_STOP_BITS_1 |
  75. ARM_USART_FLOW_CONTROL_NONE,
  76. 115200ul);
  77. }
  78. #if LOW_POWER_AT_TEST
  79. slpManSlpState_t CheckUsrdefSlpStatus(void)
  80. {
  81. slpManSlpState_t status = SLP_HIB_STATE;
  82. if ((slpManGetWakeupPinValue() & (0x1 << 1)) == 0) // pad2 value is low
  83. status = SLP_IDLE_STATE;
  84. else
  85. status = SLP_HIB_STATE;
  86. return status;
  87. }
  88. #endif
  89. /**
  90. \fn void CheckLockOutState(void)
  91. \brief CheckLockOutState
  92. \param void
  93. */
  94. void CheckLockOutState(void)
  95. {
  96. if ((slpManGetWakeupPinValue() & (0x1 << 2)) == 0)
  97. {
  98. #ifdef USING_PRINTF
  99. printf("LOCK_OUT DOWN\r\n");
  100. #endif
  101. Lockstatus = true;
  102. #ifdef BL_FILE_LOG
  103. lockoutState = 1;
  104. #endif
  105. }
  106. else
  107. {
  108. Lockstatus = false;
  109. #ifdef USING_PRINTF
  110. printf("LOCK_OUT UP\r\n");
  111. #endif
  112. #ifdef BL_FILE_LOG
  113. lockoutState = 0;
  114. #endif
  115. }
  116. }
  117. static void PMU_WakeupPadInit(void)
  118. {
  119. const padWakeupSettings_t cfg =
  120. {
  121. false, true, // group0 posedge, negedge
  122. true, true, // group1 posedge, negedge
  123. true, true, // group2 posedge, negedge
  124. };
  125. slpManSetWakeupPad(cfg);
  126. }
  127. #if (WDT_FEATURE_ENABLE == 1)
  128. /*
  129. * WDT Initialize, wdt timeout value is 20s
  130. * Parameter: none
  131. */
  132. void BSP_WdtInit(void)
  133. {
  134. // Config WDT clock, source from 32.768KHz and divide by WDT_TIMEOUT_VALUE
  135. GPR_SetClockSrc(GPR_WDGFuncClk, GPR_WDGClkSel_32K);
  136. GPR_SetClockDiv(GPR_WDGFuncClk, WDT_TIMEOUT_VALUE);
  137. wdt_config_t wdtConfig;
  138. wdtConfig.mode = WDT_InterruptResetMode;
  139. wdtConfig.timeoutValue = 32768U;
  140. WDT_Init(&wdtConfig);
  141. }
  142. void NMI_Handler()
  143. {
  144. ECOMM_TRACE(UNILOG_PLA_APP, enter_NMI_handler, P_ERROR, 0, "WDT timeout!!! Enter NMI Handler!!!");
  145. // If we have been in exception handler excecution, we shall resume it.
  146. if (is_in_excep_handler())
  147. {
  148. WDT_Stop();
  149. }
  150. else
  151. {
  152. if (BSP_GetPlatConfigItemValue(PLAT_CONFIG_ITEM_FAULT_ACTION) == EXCEP_OPTION_SILENT_RESET)
  153. {
  154. ResetReasonWrite(RESET_REASON_WDT);
  155. EC_SystemReset();
  156. while (1)
  157. ;
  158. }
  159. else
  160. {
  161. EC_ASSERT(0, 0, 0, 0);
  162. }
  163. }
  164. }
  165. #endif
  166. /*
  167. * custom board related init
  168. * Parameter: none
  169. * note: this function shall be called in OS task context for dependency of reading configure file
  170. * which is implemented based on file system
  171. */
  172. void BSP_CustomInit(void)
  173. {
  174. extern void mpu_init(void);
  175. mpu_init();
  176. GPR_SetUartClk();
  177. relayConfigInit();
  178. #if LOW_POWER_AT_TEST
  179. //slpManRegisterUsrSlpDepthCb(CheckUsrdefSlpStatus); //暂时屏蔽Rx拉低不允许睡眠的操作
  180. #endif
  181. plat_config_raw_flash_t *rawFlashPlatConfig;
  182. BSP_LoadPlatConfigFromRawFlash();
  183. rawFlashPlatConfig = BSP_GetRawFlashPlatConfig();
  184. #if 1
  185. #ifdef USING_PRINTF
  186. SetPrintUart(PORT_USART_0);
  187. #else
  188. if (rawFlashPlatConfig && (rawFlashPlatConfig->logControl != 0))
  189. {
  190. SetUnilogUart(PORT_USART_0, rawFlashPlatConfig->uartBaudRate, true);
  191. uniLogInitStart(UART_0_FOR_UNILOG);
  192. ECOMM_STRING(UNILOG_PLA_STRING, build_info, P_SIG, "%s", getBuildInfo());
  193. }
  194. #endif
  195. #endif
  196. #if (WDT_FEATURE_ENABLE == 1)
  197. if (BSP_GetPlatConfigItemValue(PLAT_CONFIG_ITEM_START_WDT))
  198. {
  199. BSP_WdtInit();
  200. WDT_Start();
  201. }
  202. #endif
  203. slpManGetPMUSettings();
  204. ResetReasonInit();
  205. PMU_WakeupPadInit();
  206. NVIC_EnableIRQ(PadWakeup0_IRQn); //can
  207. NVIC_DisableIRQ(PadWakeup1_IRQn); //rx
  208. NVIC_EnableIRQ(PadWakeup2_IRQn); //lock
  209. NVIC_DisableIRQ(PadWakeup3_IRQn); //gsensor
  210. NVIC_DisableIRQ(PadWakeup4_IRQn); //gsensor
  211. NVIC_EnableIRQ(PadWakeup5_IRQn); //wakeup2
  212. CheckLockOutState();
  213. slpManStartWaitATTimer();
  214. CanSPIHandler(NULL, ARM_SPI_CPOL0_CPHA0, 8, 200000U);
  215. slpManAONIOPowerOn();
  216. PowerPinConfig(AON_IO);
  217. PowerPinConfig(NORMAL_IO);
  218. posGGAReset();
  219. GPSUsartHandler(9600);
  220. NetSocDisplay(LED_SOC_0, LED_TURN_ON);
  221. NetSocDisplay(LED_SOC_1, LED_TURN_ON);
  222. NetSocDisplay(LED_SOC_2, LED_TURN_ON);
  223. NetSocDisplay(LED_SOC_3, LED_TURN_ON);
  224. FaultDisplay(LED_TURN_ON);
  225. }
  226. /**
  227. \fn void NVIC_WakeupIntHandler(void)
  228. \brief NVIC wakeup interrupt handler
  229. \param void
  230. */
  231. void Pad0_WakeupIntHandler(void)
  232. {
  233. if (slpManExtIntPreProcess(PadWakeup0_IRQn) == false)
  234. {
  235. return;
  236. }
  237. else
  238. {
  239. setbit(PadInterrupt, 0);
  240. }
  241. #ifdef USING_PRINTF1
  242. printf("-[%d]-[%s]\n", __LINE__, __FUNCTION__);
  243. #endif
  244. }
  245. void Pad1_WakeupIntHandler(void)
  246. {
  247. UartInterruptcount++;
  248. if (slpManExtIntPreProcess(PadWakeup1_IRQn) == false)
  249. {
  250. return;
  251. }
  252. else
  253. {
  254. #ifdef USING_PRINTF
  255. printf("Pad1-[%d]\n", __LINE__);
  256. #endif
  257. setbit(PadInterrupt, 1);
  258. }
  259. }
  260. void Pad2_WakeupIntHandler(void)
  261. {
  262. CheckLockOutState();
  263. if (slpManExtIntPreProcess(PadWakeup2_IRQn) == false)
  264. {
  265. return;
  266. }
  267. else
  268. {
  269. setbit(PadInterrupt, 2);
  270. }
  271. }
  272. void Pad3_WakeupIntHandler(void)
  273. {
  274. if (slpManExtIntPreProcess(PadWakeup3_IRQn) == false)
  275. {
  276. return;
  277. }
  278. else
  279. {
  280. setbit(PadInterrupt, 3);
  281. }
  282. }
  283. void Pad4_WakeupIntHandler(void)
  284. {
  285. if (slpManExtIntPreProcess(PadWakeup4_IRQn) == false)
  286. {
  287. return;
  288. }
  289. else
  290. {
  291. setbit(PadInterrupt, 4);
  292. }
  293. }
  294. void Pad5_WakeupIntHandler(void)
  295. {
  296. if (slpManExtIntPreProcess(PadWakeup5_IRQn) == false)
  297. {
  298. return;
  299. }
  300. else
  301. {
  302. setbit(PadInterrupt, 5);
  303. }
  304. }