bsp_custom.c 7.9 KB

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