hal_adapter.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  1. /*
  2. * hal_adapter.c
  3. *中间层函数调用库
  4. * Created on: 2022年1月18日
  5. * Author: QiXiang_CHENJIE
  6. */
  7. #include "hal_adapter.h"
  8. #include "AppGlobalVar.h"
  9. #include "stdio.h"
  10. #include "stdarg.h"
  11. /********************************/
  12. #include "Icu.h"
  13. #include "Gpt.h"
  14. uint8_t __attribute__((section(".non_cacheable_data"))) RX_Buffer[3][BUFFER_SIZE];
  15. uint32_t bufferIdx[3] = {0};
  16. volatile uint32 VarNotification_0 = 0;
  17. volatile uint32 VarNotification_1 = 0;
  18. TP_Value_Type ConvertedBuffer[NUM_RESULTS];
  19. Adc_ValueGroupType ResultBuffer[NUM_RESULTS];
  20. volatile Uart_StatusType Uart_TransmitStatus[3] = {UART_STATUS_TIMEOUT,UART_STATUS_TIMEOUT,UART_STATUS_TIMEOUT};
  21. QueueHandle_t UartRecvQueue[3];
  22. QueueHandle_t UartSendQueue[3];
  23. QueueHandle_t UartHalQueueHandle;
  24. Std_ReturnType UartStartRecvFunc(uint8 channel);
  25. Std_ReturnType ADC_Converter(Adc_ValueGroupType *Buffer, TP_Value_Type *ConvertedValueR);
  26. void create_ringBuffer(ringbuffer_t *ringBuf, uint8_t *buf, uint32_t buf_len);
  27. void clear_ringBuffer(ringbuffer_t *ringBuf);
  28. uint32_t write_ringBuffer(uint8_t *buffer, uint32_t size, ringbuffer_t *ringBuf);
  29. uint32_t read_ringBuffer(uint8_t *buffer, uint32_t size, ringbuffer_t *ringBuf);
  30. uint8 ringBufferforUart[3][BUFFER_SIZE];
  31. ringbuffer_t uartRingBuffer[3];
  32. sint8 AtcmdDelayRecvFunc(uint8 recvChannel,char *ResultStrPtr,uint16 delayTime)
  33. {
  34. sint8 outValue = -1;
  35. uint8 delayCnt = 0;
  36. uint8 UartData[256];
  37. uint16 ReadLen = 0;
  38. char *retptr = NULL;
  39. while (delayCnt<(delayTime/1000)&&outValue!=0)
  40. {
  41. UART_Receive_Data(recvChannel,UartData, &ReadLen,1000);
  42. if(ReadLen>0)
  43. {
  44. retptr = (char *)strstr((char *)UartData, ResultStrPtr);
  45. if (retptr)
  46. {
  47. outValue = 0;
  48. break;
  49. }
  50. }
  51. else
  52. {
  53. delayCnt++;
  54. }
  55. }
  56. return outValue;
  57. }
  58. #if 0
  59. uint16 myPrintf(const char *fmt, ...)
  60. {
  61. int n;
  62. uint8 databuffer[512]={0};
  63. va_list args;
  64. va_start(args, fmt);
  65. n = vsprintf((char *)databuffer, fmt, args);
  66. va_end(args);
  67. if( (printfRingBuffer.bw + n) <= printfRingBuffer.length )
  68. {
  69. memcpy(printfRingBuffer.source + printfRingBuffer.bw, databuffer, n);
  70. UART_Send_Data(UART_LPUART0, printfRingBuffer.source + printfRingBuffer.bw, n, 10);
  71. printfRingBuffer.bw = printfRingBuffer.bw + n;
  72. }
  73. else
  74. {
  75. printfRingBuffer.bw = 0;
  76. memcpy(printfRingBuffer.source + printfRingBuffer.bw, databuffer, n);
  77. UART_Send_Data(UART_LPUART0, printfRingBuffer.source + printfRingBuffer.bw, n, 10);
  78. }
  79. return n;
  80. }
  81. #endif
  82. void create_ringBuffer(ringbuffer_t *ringBuf, uint8_t *buf, uint32_t buf_len)
  83. {
  84. ringBuf->br = 0;
  85. ringBuf->bw = 0;
  86. ringBuf->btoRead = 0;
  87. ringBuf->source = buf;
  88. ringBuf->length = buf_len;
  89. }
  90. void clear_ringBuffer(ringbuffer_t *ringBuf)
  91. {
  92. ringBuf->br = 0;
  93. ringBuf->bw = 0;
  94. ringBuf->btoRead = 0;
  95. }
  96. uint32_t write_ringBuffer(uint8_t *buffer, uint32_t size, ringbuffer_t *ringBuf)
  97. {
  98. uint32_t len = 0;
  99. volatile uint32_t ringBuf_bw = ringBuf->bw;
  100. uint32_t ringBuf_len = ringBuf->length;
  101. uint8_t *ringBuf_source = ringBuf->source;
  102. if( (ringBuf_bw + size) > ringBuf_len )
  103. {
  104. ringBuf_bw = 0;
  105. }
  106. memcpy(ringBuf_source + ringBuf_bw, buffer, size);
  107. ringBuf->bw = (ringBuf_bw + size) % ringBuf_len;
  108. ringBuf->btoRead += size;
  109. /*
  110. if(ringBuf->br!=0)
  111. {
  112. memcpy(ringBuf_source, buffer, size);
  113. ringBuf->br = 0;
  114. }
  115. */
  116. /*
  117. if( (ringBuf_bw + size) <= ringBuf_len )
  118. {
  119. memcpy(ringBuf_source + ringBuf_bw, buffer, size);
  120. }
  121. else
  122. {
  123. len = ringBuf_len - ringBuf_bw;
  124. memcpy(ringBuf_source + ringBuf_bw, buffer, len);
  125. memcpy(ringBuf_source, buffer + ringBuf_bw, size - len);
  126. }
  127. ringBuf->bw = (ringBuf->bw + size) % ringBuf_len;
  128. ringBuf->btoRead += size;
  129. */
  130. return size;
  131. }
  132. uint32_t read_ringBuffer(uint8_t *buffer, uint32_t size, ringbuffer_t *ringBuf)
  133. {
  134. uint32_t len = 0;
  135. volatile uint32_t ringBuf_br = ringBuf->br;
  136. uint32_t ringBuf_len = ringBuf->length;
  137. uint8_t *ringBuf_source = ringBuf->source;
  138. memcpy(buffer, ringBuf_source, size);
  139. ringBuf->br = size;
  140. // if( (ringBuf_br + size ) <= ringBuf_len )
  141. // {
  142. // memcpy(buffer, ringBuf_source + ringBuf_br, size);
  143. // }
  144. // else
  145. // {
  146. // len = ringBuf_len - ringBuf_br;
  147. // memcpy(buffer, ringBuf_source + ringBuf_br, len);
  148. // memcpy(buffer + len, ringBuf_source, size - len);
  149. // }
  150. // ringBuf->br = (ringBuf->br + size) % ringBuf_len;
  151. // ringBuf->btoRead -= size;
  152. return size;
  153. }
  154. Std_ReturnType UART_Query_Data(uint8 transChannel, uint8 recvChannel, uint8 *txBuffer, uint16 sendLength, uint8 *rxBuffer, uint16 *rxlen, uint32 T_timeout)
  155. {
  156. UartMsg_t UartRecvMsg;
  157. UartMsg_t UartSendMsg;
  158. BaseType_t Sendret = pdFALSE;
  159. BaseType_t Recvret = pdFALSE;
  160. uint32 retVal = E_NOT_OK;
  161. UartSendMsg.DataLen = sendLength;
  162. UartSendMsg.dataPtr = txBuffer;
  163. *rxlen = 0;
  164. Sendret = xQueueSend(UartSendQueue[transChannel],&UartSendMsg,50);
  165. if(Sendret == pdTRUE)
  166. {
  167. Recvret = xQueueReceive(UartRecvQueue[recvChannel],&UartRecvMsg,T_timeout);
  168. if(Recvret == pdTRUE)
  169. {
  170. *rxlen = UartRecvMsg.DataLen;
  171. // read_ringBuffer(rxBuffer, queueRecvSize, &uartRingBuffer[recvChannel]);
  172. memcpy(rxBuffer,(uint8 *)(UartRecvMsg.dataAddr),UartRecvMsg.DataLen);
  173. retVal = E_OK;
  174. }
  175. else
  176. {
  177. retVal = 3;
  178. }
  179. }
  180. else
  181. {
  182. retVal = 2;
  183. }
  184. return retVal;
  185. }
  186. Std_ReturnType UART_Receive_Data(uint8 recvChannel, uint8 *rxBuffer, uint8 *rxlen, uint32 T_timeout)
  187. {
  188. UartMsg_t UartRecvMsg;
  189. BaseType_t ret = pdFALSE;
  190. uint32 retVal = E_NOT_OK;
  191. *rxlen = 0;
  192. ret = xQueueReceive(UartRecvQueue[recvChannel],&UartRecvMsg,T_timeout);
  193. if(ret == pdTRUE)
  194. {
  195. *rxlen = UartRecvMsg.DataLen;
  196. // read_ringBuffer(rxBuffer, queueRecvSize, &uartRingBuffer[recvChannel]);
  197. memcpy(rxBuffer,(uint8 *)UartRecvMsg.dataAddr,UartRecvMsg.DataLen);
  198. retVal = E_OK;
  199. }
  200. return retVal;
  201. }
  202. Std_ReturnType UART_Reset(uint8 recvChannel)
  203. {
  204. uint32 retVal = E_NOT_OK;
  205. retVal = xQueueReset(UartRecvQueue[recvChannel]);
  206. return retVal;
  207. }
  208. Std_ReturnType UART_Send_Data(uint8 transChannel, const uint8 *txBuffer, uint32 sendLength, uint32 T_timeout)
  209. {
  210. UartMsg_t UartSendMsg;
  211. BaseType_t ret = pdFALSE;
  212. uint32 retVal = E_NOT_OK;
  213. UartSendMsg.DataLen = sendLength;
  214. UartSendMsg.dataPtr = txBuffer;
  215. ret = xQueueSend(UartSendQueue[transChannel],&UartSendMsg,T_timeout);
  216. if(ret == pdTRUE)
  217. {
  218. retVal = E_OK;
  219. }
  220. return retVal;
  221. }
  222. void UartInit(void)
  223. {
  224. create_ringBuffer(&uartRingBuffer[0],ringBufferforUart[0],sizeof(ringBufferforUart[0]));
  225. create_ringBuffer(&uartRingBuffer[1],ringBufferforUart[1],sizeof(ringBufferforUart[1]));
  226. create_ringBuffer(&uartRingBuffer[2],ringBufferforUart[2],sizeof(ringBufferforUart[2]));
  227. UartRecvQueue[0] = xQueueCreate(6, sizeof(UartMsg_t));
  228. UartRecvQueue[1] = xQueueCreate(6, sizeof(UartMsg_t));
  229. UartRecvQueue[2] = xQueueCreate(6, sizeof(UartMsg_t));
  230. UartSendQueue[0] = xQueueCreate(3, sizeof(UartMsg_t));
  231. UartSendQueue[1] = xQueueCreate(1, sizeof(UartMsg_t));
  232. UartSendQueue[2] = xQueueCreate(1, sizeof(UartMsg_t));
  233. UartHalQueueHandle = xQueueCreate(9, sizeof(UartHalMsg_t));
  234. xTaskCreate(Uart_Hal_RecvTask, (const char *const)"UartRecv", 256, (void *)0, main_TASK_PRIORITY + 5, &Uart_Hal_RecvTask_Handle);
  235. xTaskCreate(Uart_Hal_SendTask, (const char *const)"UartSend", 256, (void *)0, main_TASK_PRIORITY + 4, &Uart_Hal_SendTask_Handle);
  236. }
  237. Std_ReturnType UartStartRecvFunc(uint8 channel)
  238. {
  239. sint8 out = 0;
  240. volatile Std_ReturnType R_Uart_Status=E_NOT_OK;
  241. bufferIdx[channel]=0;
  242. memset(RX_Buffer[channel],0x00,BUFFER_SIZE);
  243. switch(channel)
  244. {
  245. case 0:
  246. IP_LPUART0->CTRL |= LPUART_CTRL_ILIE(1);
  247. break;
  248. case 1:
  249. IP_LPUART1->CTRL |= LPUART_CTRL_ILIE(1);
  250. break;
  251. case 2:
  252. IP_LPUART2->CTRL |= LPUART_CTRL_ILIE(1);
  253. break;
  254. default:
  255. break;
  256. }
  257. Uart_SetBuffer(channel, RX_Buffer[channel], DMA_SIZE, UART_RECEIVE);
  258. R_Uart_Status = Uart_AsyncReceive(channel, RX_Buffer[channel], DMA_SIZE);
  259. if (E_OK != R_Uart_Status)
  260. {
  261. Uart_Abort(channel, UART_RECEIVE);
  262. out = E_NOT_OK;
  263. }
  264. return out;
  265. }
  266. void Uart_Hal_RecvTask(void *pvParameters)
  267. {
  268. UartHalMsg_t UartHalMsgRecv;
  269. UartMsg_t UartRecvMsg;
  270. uint16 recvSize = 0;
  271. BaseType_t ret = pdFALSE;
  272. BaseType_t ret_send = pdFALSE;
  273. uint32 T_bytesRemaining[3] = {0};
  274. uint16 T_timeout[3] = {0};
  275. volatile Uart_StatusType Uart_ReceiveStatus[3] = {UART_STATUS_TIMEOUT,UART_STATUS_TIMEOUT,UART_STATUS_TIMEOUT};
  276. uint8 UartIdx = UART_LPUART0;
  277. uint8 UartState[3] = {UartAbortRecv,UartAbortRecv,UartAbortRecv};
  278. while(1)
  279. {
  280. if((T_timeout[UartIdx]>1000) && (Uart_ReceiveStatus[UartIdx] != UART_STATUS_NO_ERROR) )
  281. {
  282. Uart_Abort(UartIdx, UART_RECEIVE);
  283. UartState[UartIdx] = UartAbortRecv;
  284. T_timeout[UartIdx] = 0;
  285. }
  286. else if(Uart_ReceiveStatus[UartIdx] == UART_STATUS_NO_ERROR)
  287. {
  288. UartState[UartIdx] = UartRecvComplete;
  289. }
  290. if((UartState[UartIdx] == UartAbortRecv) || (UartState[UartIdx] == UartRecvComplete))
  291. {
  292. if(E_OK == UartStartRecvFunc(UartIdx))
  293. {
  294. UartState[UartIdx] = UartStartRecv;
  295. }
  296. }
  297. Uart_ReceiveStatus[UartIdx] = Uart_GetStatus(UartIdx, &T_bytesRemaining[UartIdx], UART_RECEIVE);
  298. T_timeout[UartIdx]++;
  299. UartIdx = (UartIdx + 1) > 2 ? 1 : (UartIdx + 1);
  300. ret = xQueueReceive(UartHalQueueHandle,&UartHalMsgRecv,1);
  301. if(ret==pdTRUE)
  302. {
  303. if(UartHalMsgRecv.event==LPUART_UART_IP_EVENT_RECV_IDLE)
  304. {
  305. if(UartHalMsgRecv.value>0)
  306. {
  307. recvSize = write_ringBuffer(RX_Buffer[UartHalMsgRecv.Channel],UartHalMsgRecv.value,&uartRingBuffer[UartHalMsgRecv.Channel]);
  308. UartRecvMsg.DataLen = UartHalMsgRecv.value;
  309. UartRecvMsg.dataAddr = (uint32)(uartRingBuffer[UartHalMsgRecv.Channel].bw + uartRingBuffer[UartHalMsgRecv.Channel].source - UartHalMsgRecv.value);
  310. ret_send = xQueueSend(UartRecvQueue[UartHalMsgRecv.Channel],&UartRecvMsg,10);
  311. #ifdef SEGGER_RTT_PRINTF1
  312. if(UartHalMsgRecv.Channel==1)
  313. {
  314. SEGGER_RTT_printf("[%d]source=%X,dataAddr=%s\r\n",__LINE__,uartRingBuffer[UartHalMsgRecv.Channel].source,(uint8 *)UartRecvMsg.dataAddr);
  315. }
  316. #endif
  317. }
  318. T_timeout[UartHalMsgRecv.Channel] = 0;
  319. UartState[UartHalMsgRecv.Channel] = UartRecvComplete;
  320. }
  321. }
  322. }
  323. }
  324. void Uart_Hal_SendTask(void *pvParameters)
  325. {
  326. UartMsg_t UartSendMsg;
  327. BaseType_t ret = pdFALSE;
  328. uint32 T_bytesRemaining[3] = {0};
  329. uint16 T_timeout[3] = {0};
  330. volatile Std_ReturnType T_Uart_Status[3];
  331. uint8 UartIdx = UART_LPUART0;
  332. uint8 UartSendState[3] = {UartNoDataSend,UartNoDataSend,UartNoDataSend};
  333. while(1)
  334. {
  335. ret = xQueueReceive(UartSendQueue[UartIdx],&UartSendMsg,1);
  336. if(ret==pdTRUE)
  337. {
  338. T_Uart_Status[UartIdx] = Uart_AsyncSend(UartIdx, UartSendMsg.dataPtr, UartSendMsg.DataLen);
  339. if (E_OK != T_Uart_Status[UartIdx])
  340. {
  341. Uart_Abort(UartIdx, UART_SEND);
  342. UartSendState[UartIdx] = UartAbortSend;
  343. }
  344. else
  345. {
  346. UartSendState[UartIdx] = UartStartSend;
  347. }
  348. }
  349. /*开始发送后的判定*/
  350. if(UartSendState[UartIdx] == UartStartSend)
  351. {
  352. Uart_TransmitStatus[UartIdx] = Uart_GetStatus(UartIdx, &T_bytesRemaining[UartIdx], UART_SEND);
  353. T_timeout[UartIdx]++;
  354. }
  355. if(T_timeout[UartIdx]>=1000 || ((Uart_TransmitStatus[UartIdx] != UART_STATUS_OPERATION_ONGOING) && (UartSendState[UartIdx] == UartStartSend)))
  356. {
  357. if(T_timeout[UartIdx]>=1000)
  358. {
  359. Uart_Abort(UartIdx, UART_SEND);
  360. UartSendState[UartIdx] = UartAbortSend;
  361. }
  362. else if(Uart_TransmitStatus[UartIdx] == UART_STATUS_NO_ERROR)
  363. {
  364. UartSendState[UartIdx] = UartSendComplete;
  365. }
  366. T_timeout[UartIdx] = 0;
  367. }
  368. UartIdx = (UartIdx + 1) > 2 ? 0 : (UartIdx + 1);
  369. }
  370. }
  371. //
  372. //Std_ReturnType UART_Query_Data(uint8 transChannel, uint8 recvChannel, const uint8 *txBuffer, uint32 sendLength, uint8 *rxBuffer, uint16 *rxlen, uint32 T_timeout)
  373. //{
  374. // volatile Std_ReturnType R_Uart_Status;
  375. // volatile Std_ReturnType T_Uart_Status;
  376. // volatile Uart_StatusType Uart_ReceiveStatus = UART_STATUS_TIMEOUT;
  377. // volatile Uart_StatusType Uart_TransmitStatus = UART_STATUS_TIMEOUT;
  378. // uint32 T_bytesRemaining;
  379. // uint32 R_bytesRemaining;
  380. // uint32 timeout = T_timeout;
  381. // uint32 retVal = E_NOT_OK;
  382. // bufferIdx[recvChannel] = 0;
  383. // switch (recvChannel)
  384. // {
  385. // case 0:
  386. // IP_LPUART0->CTRL |= LPUART_CTRL_ILIE(1);
  387. // break;
  388. // case 1:
  389. // IP_LPUART1->CTRL |= LPUART_CTRL_ILIE(1);
  390. // break;
  391. // case 2:
  392. // IP_LPUART2->CTRL |= LPUART_CTRL_ILIE(1);
  393. // break;
  394. // default:
  395. // break;
  396. // }
  397. // if (txBuffer == NULL || rxBuffer == NULL)
  398. // {
  399. // return retVal;
  400. // }
  401. //
  402. // /* Uart_AsyncSend transmit data */
  403. // Uart_SetBuffer(transChannel, txBuffer, sendLength, UART_SEND);
  404. // T_Uart_Status = Uart_AsyncSend(transChannel, txBuffer, sendLength);
  405. // if (E_OK != T_Uart_Status)
  406. // {
  407. // Uart_Abort(transChannel, UART_SEND);
  408. // return E_NOT_OK;
  409. // }
  410. // Uart_SetBuffer(recvChannel, &RX_Buffer[recvChannel][0], DMA_SIZE, UART_RECEIVE);
  411. // R_Uart_Status = Uart_AsyncReceive(recvChannel, rxBuffer, DMA_SIZE);
  412. // if (E_OK != R_Uart_Status)
  413. // {
  414. // Uart_Abort(recvChannel, UART_RECEIVE);
  415. // return E_NOT_OK;
  416. // }
  417. // /* Check for no on-going transmission */
  418. // do
  419. // {
  420. // if (Uart_TransmitStatus != UART_STATUS_NO_ERROR)
  421. // {
  422. // Uart_TransmitStatus = Uart_GetStatus(transChannel, &T_bytesRemaining, UART_SEND);
  423. // }
  424. // if (Uart_ReceiveStatus != UART_STATUS_NO_ERROR)
  425. // {
  426. // Uart_ReceiveStatus = Uart_GetStatus(recvChannel, &R_bytesRemaining, UART_RECEIVE);
  427. // }
  428. // vTaskDelay(pdMS_TO_TICKS(1));
  429. // } while (((UART_STATUS_NO_ERROR != Uart_TransmitStatus || UART_STATUS_NO_ERROR != Uart_ReceiveStatus) && 0 < --timeout));
  430. // if ((UART_STATUS_NO_ERROR != Uart_TransmitStatus))
  431. // {
  432. // Uart_Abort(transChannel, UART_SEND);
  433. // retVal = E_NOT_OK;
  434. // }
  435. // else
  436. // {
  437. // retVal = E_OK;
  438. // }
  439. // if ((UART_STATUS_NO_ERROR != Uart_ReceiveStatus))
  440. // {
  441. // Uart_Abort(recvChannel, UART_RECEIVE);
  442. // *rxlen = bufferIdx[recvChannel];
  443. // retVal = E_NOT_OK;
  444. // }
  445. // else
  446. // {
  447. // *rxlen = bufferIdx[recvChannel];
  448. // retVal = E_OK;
  449. // }
  450. // return retVal;
  451. //}
  452. //
  453. //Std_ReturnType UART_Send_Data(uint8 transChannel, const uint8 *txBuffer, uint32 sendLength, uint32 T_timeout)
  454. //{
  455. //
  456. // volatile Std_ReturnType T_Uart_Status;
  457. // volatile Uart_StatusType Uart_TransmitStatus = UART_STATUS_TIMEOUT;
  458. // uint32 T_bytesRemaining;
  459. // uint32 timeout = T_timeout;
  460. // uint32 retVal = E_NOT_OK;
  461. // if (txBuffer == NULL)
  462. // {
  463. // return retVal;
  464. // }
  465. //
  466. // /* Uart_AsyncSend transmit data */
  467. // T_Uart_Status = Uart_AsyncSend(transChannel, txBuffer, sendLength);
  468. // if (E_OK != T_Uart_Status)
  469. // {
  470. // Uart_Abort(transChannel, UART_SEND);
  471. // return E_NOT_OK;
  472. // }
  473. // /* Check for no on-going transmission */
  474. // do
  475. // {
  476. // Uart_TransmitStatus = Uart_GetStatus(transChannel, &T_bytesRemaining, UART_SEND);
  477. // vTaskDelay(pdMS_TO_TICKS(1));
  478. // } while ((UART_STATUS_NO_ERROR != Uart_TransmitStatus && 0 < --timeout));
  479. //
  480. // if ((UART_STATUS_NO_ERROR != Uart_TransmitStatus))
  481. // {
  482. // retVal = E_NOT_OK;
  483. // }
  484. // else
  485. // {
  486. // retVal = E_OK;
  487. // }
  488. // return retVal;
  489. //}
  490. //
  491. //Std_ReturnType UART_Receive_Data(uint8 recvChannel, uint8 *rxBuffer, uint16 *rxlen, sint32 T_timeout)
  492. //{
  493. // volatile Std_ReturnType R_Uart_Status = E_NOT_OK;
  494. // volatile Uart_StatusType Uart_ReceiveStatus = UART_STATUS_TIMEOUT;
  495. // uint32 T_bytesRemaining = 0;
  496. // uint32 retVal = E_NOT_OK;
  497. // // uint8 Rx_Buffer[MSG_LEN];
  498. // bufferIdx[recvChannel] = 0;
  499. // *rxlen = 0;
  500. // if (rxBuffer == NULL)
  501. // {
  502. // return retVal;
  503. // }
  504. // /* Uart_AsyncReceive transmit data */
  505. // switch (recvChannel)
  506. // {
  507. // case 0:
  508. // IP_LPUART0->CTRL |= LPUART_CTRL_ILIE(1);
  509. // break;
  510. // case 1:
  511. // IP_LPUART1->CTRL |= LPUART_CTRL_ILIE(1);
  512. // break;
  513. // case 2:
  514. // IP_LPUART2->CTRL |= LPUART_CTRL_ILIE(1);
  515. // break;
  516. // default:
  517. // break;
  518. // }
  519. // Uart_SetBuffer(recvChannel, rxBuffer, DMA_SIZE, UART_RECEIVE);
  520. // R_Uart_Status = Uart_AsyncReceive(recvChannel, rxBuffer, DMA_SIZE);
  521. // if (E_OK != R_Uart_Status)
  522. // {
  523. // Uart_Abort(recvChannel, UART_RECEIVE);
  524. // return E_NOT_OK;
  525. // }
  526. // /* Check for no on-going transmission */
  527. // do
  528. // {
  529. // Uart_ReceiveStatus = Uart_GetStatus(recvChannel, &T_bytesRemaining, UART_RECEIVE);
  530. // vTaskDelay(pdMS_TO_TICKS(1));
  531. //
  532. // } while ((UART_STATUS_NO_ERROR != Uart_ReceiveStatus) && 0 < T_timeout--);
  533. // if ((UART_STATUS_NO_ERROR != Uart_ReceiveStatus))
  534. // {
  535. // Uart_Abort(recvChannel, UART_RECEIVE);
  536. // *rxlen = bufferIdx[recvChannel];
  537. // retVal = E_NOT_OK;
  538. // }
  539. // else
  540. // {
  541. // *rxlen = bufferIdx[recvChannel];
  542. // retVal = E_OK;
  543. // }
  544. // return retVal;
  545. //}
  546. extern Lpuart_Uart_Ip_StateStructureType *Lpuart_Uart_Ip_apStateStructuresArray[LPUART_UART_IP_NUMBER_OF_INSTANCES];
  547. void UART_Callback(uint32 hwInstance, Lpuart_Uart_Ip_EventType event)
  548. {
  549. // (void)userData;
  550. Lpuart_Uart_Ip_StateStructureType * UartState;
  551. UartState = (Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[hwInstance];
  552. /* Check the event type */
  553. if (event == LPUART_UART_IP_EVENT_RX_FULL)
  554. {
  555. /* The reception stops when receiving idle is detected or the buffer is full */
  556. if (bufferIdx[hwInstance] <= (BUFFER_SIZE - DMA_SIZE))
  557. {
  558. /* Update the buffer index and the rx buffer */
  559. bufferIdx[hwInstance] += DMA_SIZE;
  560. Uart_SetBuffer(hwInstance, &RX_Buffer[hwInstance][bufferIdx[hwInstance]], DMA_SIZE, UART_RECEIVE);
  561. // Lpuart_Uart_Ip_SetRxBuffer(hwInstance, &RX_Buffer[bufferIdx], DMA_SIZE);
  562. }
  563. }
  564. if (event == LPUART_UART_IP_EVENT_ERROR)
  565. {
  566. // /*Get the transfered data size. DMA Channel 1 is used for LPUART DMA receiving, please modify accordingly.*/
  567. // temp = DMA_SIZE - (uint32_t)IP_DMA->TCD->CITER.ELINKNO;
  568. // /*Add the remaining data size to the sum of the received size*/
  569. // bufferIdx[hwInstance] += temp;
  570. /*Abort the receiving after detecting IDLE receiving*/
  571. Lpuart_Uart_Ip_AbortReceivingData(hwInstance);
  572. Lpuart_Uart_Ip_AbortSendingData(hwInstance);
  573. // bufferIdx = 0;
  574. }
  575. if (event == LPUART_UART_IP_EVENT_RECV_IDLE)
  576. {
  577. uint32_t temp;
  578. UartHalMsg_t UartHalMsg;
  579. UartHalMsg.Channel = hwInstance;
  580. UartHalMsg.event = event;
  581. /*Get the transfered data size. DMA Channel 1 is used for LPUART DMA receiving, please modify accordingly.*/
  582. temp = DMA_SIZE - (uint32_t)IP_DMA->TCD[hwInstance].CITER.ELINKNO;
  583. /*Add the remaining data size to the sum of the received size*/
  584. bufferIdx[hwInstance] += temp;
  585. /*Abort the receiving after detecting IDLE receiving*/
  586. UartHalMsg.value = bufferIdx[hwInstance];
  587. xQueueSendFromISR(UartHalQueueHandle,&UartHalMsg,pdFALSE);
  588. }
  589. }
  590. /*CAN*/
  591. Can_PduType Can_CreatePduInfo(Can_IdType id, CAN_IdFrameType idFrame, PduIdType swPduHandle, uint8 length, uint8 *sdu)
  592. {
  593. Can_PduType PduInfo;
  594. switch (idFrame)
  595. {
  596. case CAN_STANDARD_ID_TYPE:
  597. id = id & 0x7FF;
  598. break;
  599. case CANFD_STANDARD_ID_TYPE:
  600. id = (id & 0x7FF) | 0x40000000;
  601. break;
  602. case CAN_EXTENDED_ID_TYPE:
  603. id = id | 0x80000000;
  604. break;
  605. case CANFD_EXTENDED_ID_TYPE:
  606. id = id | 0xC0000000;
  607. break;
  608. default:
  609. id = id & 0x7FF;
  610. break;
  611. }
  612. PduInfo.id = id;
  613. PduInfo.swPduHandle = swPduHandle;
  614. PduInfo.length = length;
  615. PduInfo.sdu = sdu;
  616. return PduInfo;
  617. }
  618. Std_ReturnType CanIf_SendMessage(uint8 ControllerId, Can_Msg_Type CanMsg)
  619. {
  620. volatile Can_PduType Can_PduInfo;
  621. volatile Std_ReturnType CAN_Write_Status;
  622. Std_ReturnType retVal = E_NOT_OK;
  623. uint32 u8TimeOut = 100 * 100;
  624. Can_HwHandleType Hth = Can0HardwareObject_TX + (Can_HwHandleType)ControllerId; // controller 0 --> Can0HardwareObject_TX
  625. Can_PduInfo = Can_CreatePduInfo(CanMsg.id, CanMsg.idFrame, 0, CanMsg.length, CanMsg.sdu);
  626. CAN_Write_Status = Can_Write(Hth, &Can_PduInfo);
  627. CanIf_bTxFlag = FALSE;
  628. if (CAN_Write_Status == E_OK)
  629. {
  630. while ((!CanIf_bTxFlag) && (u8TimeOut != 0U))
  631. {
  632. Can_MainFunction_Write();
  633. u8TimeOut--;
  634. }
  635. }
  636. if (CanIf_bTxFlag == TRUE)
  637. {
  638. retVal = E_OK;
  639. }
  640. else
  641. {
  642. retVal = E_NOT_OK;
  643. }
  644. return retVal;
  645. }
  646. Can_Msg_Type Can_GetMsgInfo(Can_IdType id, uint8 length, uint8 *sdu)
  647. {
  648. Can_Msg_Type CanMsgInfo;
  649. CanMsgInfo.idFrame = (CAN_IdFrameType)((id >> 30) & 0x03);
  650. if (CanMsgInfo.idFrame & 0x01)
  651. {
  652. CanMsgInfo.id = id & 0x7FF;
  653. }
  654. else
  655. {
  656. CanMsgInfo.id = id & 0x1FFFFFFF;
  657. }
  658. CanMsgInfo.length = length;
  659. CanMsgInfo.sdu = sdu;
  660. return CanMsgInfo;
  661. }
  662. void CanIf_ControllerBusOff(uint8 ControllerId)
  663. {
  664. (void)ControllerId;
  665. }
  666. void CanIf_ControllerModeIndication(uint8 ControllerId, Can_ControllerStateType ControllerMode)
  667. {
  668. (void)ControllerId;
  669. (void)ControllerMode;
  670. }
  671. void CanIf_TxConfirmation(PduIdType CanTxPduId)
  672. {
  673. CanIf_u8TxConfirmCnt++;
  674. CanIf_bTxFlag = TRUE;
  675. (void)CanTxPduId;
  676. }
  677. void CanIf_RxIndication(const Can_HwType *Mailbox, const PduInfoType *PduInfoPtr)
  678. {
  679. Can_Msg_Type canRxMsg_Buff;
  680. Can_Msg_Type_Data canRxMsgQueueData;
  681. CanIf_bRxFlag = TRUE; // should not be delete
  682. // should put the msg into message queue
  683. canRxMsg_Buff = Can_GetMsgInfo(Mailbox->CanId, PduInfoPtr->SduLength, PduInfoPtr->SduDataPtr);
  684. canRxMsgQueueData.id = canRxMsg_Buff.id;
  685. canRxMsgQueueData.length = canRxMsg_Buff.length;
  686. memcpy(canRxMsgQueueData.data, canRxMsg_Buff.sdu, canRxMsgQueueData.length);
  687. switch(Mailbox->Hoh)
  688. {
  689. case 0:
  690. xQueueSend(CanRecvQueueHandle0, &canRxMsgQueueData, 0);
  691. break;
  692. case 1:
  693. xQueueSend(CanRecvQueueHandle1, &canRxMsgQueueData, 0);
  694. break;
  695. case 2:
  696. xQueueSend(CanRecvQueueHandle2, &canRxMsgQueueData, 0);
  697. break;
  698. }
  699. }
  700. void CanIf_CurrentIcomConfiguration(uint8 ControllerId, IcomConfigIdType ConfigurationId, IcomSwitch_ErrorType Error)
  701. {
  702. (void)ControllerId;
  703. (void)ConfigurationId;
  704. (void)Error;
  705. }
  706. void Notification_0(void)
  707. {
  708. ADC_Converter(ResultBuffer, ConvertedBuffer);
  709. memcpy(BattTempR, &ConvertedBuffer[3], 4 * sizeof(uint32));
  710. }
  711. void Notification_1(void)
  712. {
  713. VarNotification_1++;
  714. }
  715. Std_ReturnType ADC_Converter(Adc_ValueGroupType *Buffer, TP_Value_Type *ConvertedValueR)
  716. {
  717. Adc_ValueGroupType REFH, REFL;
  718. REFH = Buffer[0];
  719. REFL = Buffer[2];
  720. for (int i = 3; i < NUM_RESULTS; i++)
  721. {
  722. if (Buffer[i] >= REFH)
  723. {
  724. ConvertedValueR[i] = 40930000;
  725. }
  726. else if (Buffer[i] <= REFL)
  727. {
  728. ConvertedValueR[i] = 0x00;
  729. }
  730. else
  731. {
  732. ConvertedValueR[i] = (TP_Value_Type)((float)(10000 * (Buffer[i] - REFL) / (float)(REFH - REFL)) / (1 - (float)((Buffer[i] - REFL) / (float)(REFH - REFL))));
  733. }
  734. }
  735. return 0;
  736. }
  737. Std_ReturnType ADC_ReadValue()
  738. {
  739. Std_ReturnType ret = E_NOT_OK;
  740. for (uint8 i = 0; i < NUM_RESULTS; i++)
  741. {
  742. ResultBuffer[i] = 0xFFFF;
  743. ConvertedBuffer[i] = 0x00;
  744. }
  745. Adc_SetupResultBuffer(AdcGroupSoftwareOneShot, ResultBuffer);
  746. Adc_EnableGroupNotification(AdcGroupSoftwareOneShot);
  747. VarNotification_0 = 0;
  748. Adc_StartGroupConversion(AdcGroupSoftwareOneShot);
  749. return ret;
  750. }
  751. /*EEP*/
  752. static Std_ReturnType TestEep_FlexNvmProgramPartCmd(
  753. VAR(TestEep_CsecKeySize, AUTOMATIC) eepKeysize,
  754. VAR(TestEep_SfeType, AUTOMATIC) eepSecurityFlagExtension,
  755. VAR(TestEep_LoadFlexRamType, AUTOMATIC) eepLoadFlexRamAtReset,
  756. VAR(TestEep_Eeprom_FlexRamPartitionType, AUTOMATIC) eepFlexRamPartition,
  757. VAR(TestEep_Eeprom_FlexNvmPartitionType, AUTOMATIC) eepFlexNvmPartition)
  758. {
  759. Std_ReturnType u8RetVal = (Std_ReturnType)E_OK;
  760. uint32 u32FlexNvmPartSize = 0;
  761. uint32 u32RegSimFcfg1 = 0UL;
  762. u32RegSimFcfg1 = IP_SIM->FCFG1;
  763. /*get DEPART value */
  764. u32FlexNvmPartSize = (uint32)((u32RegSimFcfg1 & SIM_FCFG1_DEPART_MASK) >> SIM_FCFG1_DEPART_SHIFT);
  765. /* check that it was not partitioned before */
  766. if (u32FlexNvmPartSize == 0xF)
  767. {
  768. // /* if error flags are set the cmd is not executed */
  769. // REG_WRITE8(TEST_EEP_EEPROM_FSTAT_ADDR32, TEST_EEP_EEPROM_FSTAT_ACCERR_U8 | TEST_EEP_EEPROM_FSTAT_FPVIOL_U8);
  770. //
  771. // /*erase DF 0 sector*/
  772. // u32Addr=(TEST_EEP_DEEPROM_SECTOR_0_ADDR32 - D_EEPROM_BASE_ADDR) + 0x800000UL;
  773. //
  774. // REG_WRITE8(TEST_EEP_EEPROM_FCCOB0_ADDR32, TEST_EEP_EEPROM_CMD_ERASE_SECTOR);
  775. // REG_WRITE8(TEST_EEP_EEPROM_FCCOB1_ADDR32, (uint8)(u32Addr >> 16UL));
  776. // REG_WRITE8(TEST_EEP_EEPROM_FCCOB2_ADDR32, (uint8)(u32Addr >> 8UL));
  777. // REG_WRITE8(TEST_EEP_EEPROM_FCCOB3_ADDR32, (uint8)(u32Addr >> 0UL));
  778. // REG_WRITE8(TEST_EEP_EEPROM_FSTAT_ADDR32 , TEST_EEP_EEPROM_FSTAT_CCIF_U8);
  779. // while((0U == REG_BIT_GET8(TEST_EEP_EEPROM_FSTAT_ADDR32, TEST_EEP_EEPROM_FSTAT_CCIF_U8)))
  780. // {
  781. // }
  782. //
  783. if (0U == REG_BIT_GET8(TEST_EEP_EEPROM_FSTAT_ADDR32, TEST_EEP_EEPROM_FSTAT_ACCERR_U8 | TEST_EEP_EEPROM_FSTAT_FPVIOL_U8))
  784. {
  785. /* run program partition command */
  786. REG_WRITE8(TEST_EEP_EEPROM_FCCOB0_ADDR32, EEPROM_CMD_PROGRAM_PARTITION);
  787. REG_WRITE8(TEST_EEP_EEPROM_FCCOB1_ADDR32, (uint8)eepKeysize);
  788. REG_WRITE8(TEST_EEP_EEPROM_FCCOB2_ADDR32, (uint8)eepSecurityFlagExtension);
  789. REG_WRITE8(TEST_EEP_EEPROM_FCCOB3_ADDR32, (uint8)eepLoadFlexRamAtReset);
  790. REG_WRITE8(TEST_EEP_EEPROM_FCCOB4_ADDR32, (uint8)eepFlexRamPartition);
  791. REG_WRITE8(TEST_EEP_EEPROM_FCCOB5_ADDR32, (uint8)eepFlexNvmPartition);
  792. REG_WRITE8(TEST_EEP_EEPROM_FSTAT_ADDR32, TEST_EEP_EEPROM_FSTAT_CCIF_U8);
  793. while ((0U == REG_BIT_GET8(TEST_EEP_EEPROM_FSTAT_ADDR32, TEST_EEP_EEPROM_FSTAT_CCIF_U8)))
  794. {
  795. /* wait for operation to finish */
  796. }
  797. /* check if errors occured */
  798. if (REG_BIT_GET8(TEST_EEP_EEPROM_FSTAT_ADDR32, TEST_EEP_EEPROM_FSTAT_ACCERR_U8 | TEST_EEP_EEPROM_FSTAT_FPVIOL_U8))
  799. {
  800. /* NOK, error flags are set */
  801. u8RetVal = (Std_ReturnType)E_NOT_OK;
  802. }
  803. }
  804. else
  805. {
  806. /* NOK, error flags are set */
  807. u8RetVal = (Std_ReturnType)E_NOT_OK;
  808. }
  809. }
  810. else
  811. {
  812. /* NOK, partitioned already */
  813. u8RetVal = (Std_ReturnType)E_NOT_OK;
  814. }
  815. return u8RetVal;
  816. }
  817. void Eep_DepartParitition(TestEep_Eeprom_FlexNvmPartitionType T_EEP_SIZE)
  818. {
  819. uint32 u32FlexNvmPartSize = 0;
  820. uint32 u32RegSimFcfg1 = 0UL;
  821. u32RegSimFcfg1 = IP_SIM->FCFG1;
  822. /*get DEPART value */
  823. u32FlexNvmPartSize = (uint32)((u32RegSimFcfg1 & SIM_FCFG1_DEPART_MASK) >> SIM_FCFG1_DEPART_SHIFT);
  824. if (u32FlexNvmPartSize == 0xF) /* We just partition again if curent size different with expected */
  825. {
  826. /* partition for EERAM 64K with NOT loading EERAM at reset in hardware */
  827. TestEep_FlexNvmProgramPartCmd(EEP_FTFC_KEY_SIZE_0_BYTES, EEP_FTFC_VERIFY_ONLY_DISABLED,
  828. EEP_FTFC_LOAD_AT_RESET_ENABLED, EEP_FTFC_EERAM_SIZE_4K, T_EEP_SIZE);
  829. }
  830. }
  831. /* Erase memory by writing erase value */
  832. Std_ReturnType HAL_EEP_Erase(uint32 eepEraseStartAddr, uint32 eepEraseSize)
  833. {
  834. Std_ReturnType retReturnType = E_OK;
  835. MemIf_JobResultType retJobResultType;
  836. retReturnType = Eep_Erase(eepEraseStartAddr, eepEraseSize);
  837. if (E_OK != retReturnType)
  838. {
  839. return E_NOT_OK;
  840. }
  841. while (MEMIF_IDLE != Eep_GetStatus())
  842. {
  843. Eep_MainFunction();
  844. }
  845. retJobResultType = Eep_GetJobResult();
  846. if (MEMIF_JOB_OK != retJobResultType)
  847. {
  848. return E_NOT_OK;
  849. }
  850. return E_OK;
  851. }
  852. /* Write one or more complete eeprom pages to the eeprom device */
  853. Std_ReturnType HAL_EEP_Write(uint32 eepWriteStartAddr, uint8 *pDataNeedtoWrite, uint32 dataSize)
  854. {
  855. Std_ReturnType retReturnType = E_OK;
  856. MemIf_JobResultType retJobResultType;
  857. /*Erase the EEP before write*/
  858. retReturnType = HAL_EEP_Erase(eepWriteStartAddr, dataSize);
  859. if (E_OK != retReturnType)
  860. {
  861. return E_NOT_OK;
  862. }
  863. retReturnType = Eep_Write(eepWriteStartAddr, pDataNeedtoWrite, dataSize);
  864. if (E_OK != retReturnType)
  865. {
  866. return E_NOT_OK;
  867. }
  868. while (MEMIF_IDLE != Eep_GetStatus())
  869. {
  870. Eep_MainFunction();
  871. }
  872. retJobResultType = Eep_GetJobResult();
  873. if (MEMIF_JOB_OK != retJobResultType)
  874. {
  875. return E_NOT_OK;
  876. }
  877. return E_OK;
  878. }
  879. /* Reads from eeprom memory */
  880. Std_ReturnType HAL_EEP_Read(uint32 eepReadStartAddr, uint8 *pDataBuffer, uint32 dataSize)
  881. {
  882. Std_ReturnType retReturnType = E_OK;
  883. MemIf_JobResultType retJobResultType;
  884. retReturnType = Eep_Read(eepReadStartAddr, pDataBuffer, dataSize);
  885. if (E_OK != retReturnType)
  886. {
  887. return E_NOT_OK;
  888. }
  889. while (MEMIF_IDLE != Eep_GetStatus())
  890. {
  891. Eep_MainFunction();
  892. }
  893. retJobResultType = Eep_GetJobResult();
  894. if (MEMIF_JOB_OK != retJobResultType)
  895. {
  896. return E_NOT_OK;
  897. }
  898. return E_OK;
  899. }
  900. /* Compares a eeprom memory area with an application data buffer */
  901. Std_ReturnType HAL_EEP_Compare(uint32 eepCompareStartAddr, uint8 *pDataNeedtoCompare, uint32 dataSize)
  902. {
  903. Std_ReturnType retReturnType = E_OK;
  904. MemIf_JobResultType retJobResultType;
  905. retReturnType = Eep_Compare(eepCompareStartAddr, pDataNeedtoCompare, dataSize);
  906. if (E_OK != retReturnType)
  907. {
  908. return E_NOT_OK;
  909. }
  910. while (MEMIF_IDLE != Eep_GetStatus())
  911. {
  912. Eep_MainFunction();
  913. }
  914. retJobResultType = Eep_GetJobResult();
  915. if (MEMIF_JOB_OK != retJobResultType)
  916. {
  917. return E_NOT_OK;
  918. }
  919. return E_OK;
  920. }
  921. /* @brief VECTKEY value so that AIRCR register write is not ignored. */
  922. #define FEATURE_SCB_VECTKEY (0x05FAU)
  923. void SystemSoftwareReset(void)
  924. {
  925. uint32_t regValue;
  926. /* Read Application Interrupt and Reset Control Register */
  927. regValue = S32_SCB->AIRCR;
  928. /* Clear register key */
  929. regValue &= ~( S32_SCB_AIRCR_VECTKEY_MASK);
  930. /* Configure System reset request bit and Register Key */
  931. regValue |= S32_SCB_AIRCR_VECTKEY(FEATURE_SCB_VECTKEY);
  932. regValue |= S32_SCB_AIRCR_SYSRESETREQ(0x1u);
  933. /* Write computed register value */
  934. S32_SCB->AIRCR = regValue;
  935. }
  936. void MCUSleep(void)
  937. {
  938. #if (ICU_PRECOMPILE_SUPPORT == STD_ON)
  939. Icu_Init(NULL_PTR);
  940. #elif (ICU_PRECOMPILE_SUPPORT == STD_OFF)
  941. Icu_Init(&Icu_Config_VS_0);
  942. #endif
  943. Mcu_SetMode(McuModeSettingConf_VLPS);
  944. // typedef void (*AppAddr)(void);
  945. // AppAddr resetHandle = (AppAddr)(0x14601);
  946. // OsIf_SuspendAllInterrupts();
  947. // (resetHandle)();
  948. SystemSoftwareReset();
  949. // coreInit();
  950. }
  951. void SystemDeinit(void)
  952. {
  953. Dio_WriteChannel(DioConf_DioChannel_PTA7_GPIO_OUT_MCU_4G_PWRKEY, STD_OFF);
  954. vTaskDelay(pdMS_TO_TICKS(3000));
  955. Dio_WriteChannel(DioConf_DioChannel_PTA6_GPIO_OUT_MCU_4G_POW_EN, STD_OFF);
  956. Dio_WriteChannel(DioConf_DioChannel_PTD1_GPIO_OUT_MCU_GPS_POW_EN, STD_OFF);//GPS ShutDown
  957. Dio_WriteChannel(DioConf_DioChannel_PTE0_GPIO_OUT_MCU_LED1, STD_ON);
  958. Dio_WriteChannel(DioConf_DioChannel_PTE1_GPIO_OUT_MCU_LED2, STD_ON);
  959. Dio_WriteChannel(DioConf_DioChannel_PTE7_GPIO_OUT_MCU_LED3, STD_ON);
  960. Dio_WriteChannel(DioConf_DioChannel_PTE8_GPIO_OUT_MCU_LED4, STD_ON);
  961. Dio_WriteChannel(DioConf_DioChannel_PTE9_GPIO_OUT_MCU_LED5, STD_ON);
  962. Uart_Deinit();
  963. Can_SetControllerMode(CanController_0, CAN_CS_STOPPED);
  964. Can_SetControllerMode(CanController_1, CAN_CS_STOPPED);
  965. // Can_SetControllerMode(CanController_2, CAN_CS_STOPPED);
  966. Can_DeInit();
  967. Adc_DeInit();
  968. Gpt_DisableNotification(GptConf_GptChannelConfiguration_GptChannelConfiguration_0);
  969. Gpt_DeInit();
  970. Spi_DeInit();
  971. Mcl_DeInit();
  972. //port DeInit
  973. for(int pinIndex = 0; pinIndex <PortConfigSet_PortContainer_GPIO_PTB4_GPIO_OUT_MCU_RS485_EN; pinIndex++)
  974. {
  975. if(pinIndex == PortConfigSet_PortContainer_CAN_PTA12_CAN1_RX_MCU_CAN1_RX /*|| pinIndex == PortConfigSet_PortContainer_INT_PTB0_LPTMR0_ATL3_MCU_CC1_INT || pinIndex == PortConfigSet_PortContainer_INT_PTE11_LPTMR0_ALT1_MCU_3D_INT1 || pinIndex == PortConfigSet_PortContainer_INT_PTD5_LPTMR0_ATL2_MCU_3D_INT2*/)
  976. {
  977. continue;
  978. }
  979. else
  980. {
  981. Port_SetAsUnusedPin(pinIndex);
  982. }
  983. }
  984. Port_SetPinMode(PortConfigSet_PortContainer_CAN_PTA12_CAN1_RX_MCU_CAN1_RX,PORT_GPIO_MODE);
  985. // systemInitFlag = false;
  986. }
  987. void MCUEnterSleep(void)
  988. {
  989. if(pdTRUE == xSemaphoreTake(sleep_mutex,1) && Fota_Process_Going == false)
  990. {
  991. extern boolean Uart_4G_Task_Sleep_FLag;
  992. WdgDeInit();
  993. Std_ReturnType Ret = E_NOT_OK;
  994. uint8 appConfigWriteTimes = 0;
  995. do
  996. {
  997. waitForSleepFlag = true;
  998. //save the app configure before power off
  999. if(Ret == E_NOT_OK)
  1000. {
  1001. AppConfigInfo.appSaveFlg = false;
  1002. Ret = HAL_EEP_Write(0,(uint8 *)&AppConfigInfo,sizeof(AppConfigInfo));
  1003. appConfigWriteTimes++;
  1004. }
  1005. vTaskDelay(pdMS_TO_TICKS(10));
  1006. }while(Uart_4G_Task_Sleep_FLag == false || (Ret == E_NOT_OK && appConfigWriteTimes<5) );
  1007. vTaskDelete(Uart_Hal_RecvTask_Handle);
  1008. vTaskDelete(Uart_Hal_SendTask_Handle);
  1009. vTaskDelete(CanTask_Handle);
  1010. vTaskDelete(GpsTask_Handle);
  1011. vTaskDelete(Uart_4G_Task_Handle);
  1012. SystemDeinit();
  1013. MCUSleep();
  1014. // WdgInit();
  1015. // DoResetECUWithWdg();
  1016. }
  1017. }
  1018. void coreInit(void)
  1019. {
  1020. /* Initialize the Mcu driver */
  1021. #if (MCU_PRECOMPILE_SUPPORT == STD_ON)
  1022. Mcu_Init(NULL_PTR);
  1023. #elif (MCU_PRECOMPILE_SUPPORT == STD_OFF)
  1024. Mcu_Init(&Mcu_Config_VS_0);
  1025. #endif /* (MCU_PRECOMPILE_SUPPORT == STD_ON) */
  1026. Mcu_InitClock(McuClockSettingConfig_0);
  1027. /* Wait until PLL is locked */
  1028. while (MCU_PLL_LOCKED != Mcu_GetPllStatus())
  1029. {
  1030. /* Busy wait until the System PLL is locked */
  1031. }
  1032. Mcu_DistributePllClock();
  1033. OsIf_Init(NULL_PTR);
  1034. Platform_Init(NULL_PTR);
  1035. /* Initialize all pins*/
  1036. #if (PORT_PRECOMPILE_SUPPORT == STD_ON)
  1037. Port_Init(NULL_PTR);
  1038. #elif (PORT_PRECOMPILE_SUPPORT == STD_OFF)
  1039. Port_Init(&Port_Config_VS_0);
  1040. #endif
  1041. }
  1042. void SystemModulesInit(void)
  1043. {
  1044. // Dio_WriteChannel(DioConf_DioChannel_PTE0_GPIO_OUT_MCU_LED1, STD_OFF);
  1045. /* Initialize Mcl module */
  1046. Mcl_Init(NULL_PTR);
  1047. SEGGER_RTT_Init();
  1048. /* Initializes an UART driver*/
  1049. #if (UART_PRECOMPILE_SUPPORT == STD_ON)
  1050. Uart_Init(NULL_PTR);
  1051. #elif (UART_PRECOMPILE_SUPPORT == STD_OFF)
  1052. Uart_Init(&Uart_xConfig_VS_0);
  1053. #endif
  1054. IP_LPUART0->CTRL |= LPUART_CTRL_ILT(1);
  1055. IP_LPUART1->CTRL |= LPUART_CTRL_ILT(1);
  1056. IP_LPUART2->CTRL |= LPUART_CTRL_ILT(1);
  1057. IP_LPUART0->CTRL |= LPUART_CTRL_IDLECFG(3);
  1058. IP_LPUART1->CTRL |= LPUART_CTRL_IDLECFG(3);
  1059. IP_LPUART2->CTRL |= LPUART_CTRL_IDLECFG(3);
  1060. #if 1 /* Initialize Platform driver */
  1061. #if (CAN_PRECOMPILE_SUPPORT == STD_ON)
  1062. Can_Init(NULL_PTR);
  1063. #elif (CAN_PRECOMPILE_SUPPORT == STD_OFF)
  1064. Can_Init(&Can_Config_VS_0);
  1065. #endif
  1066. Can_SetControllerMode(CanController_0, CAN_CS_STARTED);
  1067. Can_SetControllerMode(CanController_1, CAN_CS_STARTED);
  1068. // Can_SetControllerMode(CanController_2, CAN_CS_STARTED);
  1069. #endif
  1070. #if (ADC_PRECOMPILE_SUPPORT == STD_ON)
  1071. Adc_Init(NULL_PTR);
  1072. #else
  1073. Adc_Init(&Adc_Config_VS_0);
  1074. #endif /* ADC_PRECOMPILE_SUPPORT == STD_ON */
  1075. /* Partition only if it was not partitioned before for EERAM with code 0x4 */
  1076. // Eep_DepartParitition(T_EEEPROM_SIZE);
  1077. /* Initialize Eep driver */
  1078. #if defined (EEP_PRECOMPILE_SUPPORT)
  1079. Eep_Init(NULL_PTR);
  1080. #else
  1081. Eep_Init(&Eep_Config_VS_0);
  1082. #endif
  1083. //Init Flash Driver
  1084. #if defined (FLS_PRECOMPILE_SUPPORT)
  1085. Fls_Init(NULL_PTR);
  1086. #else
  1087. Fls_Init(&Fls_Config_VS_0);
  1088. while(MEMIF_IDLE == Fls_GetStatus())
  1089. {
  1090. ;
  1091. }
  1092. #endif
  1093. Spi_Init(NULL_PTR);
  1094. /* Initialize the Gpt driver */
  1095. Gpt_Init(&Gpt_Config_VS_0);
  1096. /* Enable the Gpt notification to periodically service the Wdg */
  1097. Gpt_EnableNotification(GptConf_GptChannelConfiguration_GptChannelConfiguration_0);
  1098. Icu_DeInit();
  1099. WdgInit();
  1100. IsFeedWdg = true;
  1101. }
  1102. void displayResetReasonWithLED(void)
  1103. {
  1104. Mcu_ResetType bootreason;
  1105. bootreason = Mcu_GetResetReason();
  1106. if(bootreason == MCU_STOP_ACKNOWLEDGE_ERROR_RESET)
  1107. {
  1108. Dio_WriteChannel(DioConf_DioChannel_PTE0_GPIO_OUT_MCU_LED1, STD_OFF);
  1109. // Dio_WriteChannel(DioConf_DioChannel_PTE1_GPIO_OUT_MCU_LED2, STD_OFF);
  1110. // Dio_WriteChannel(DioConf_DioChannel_PTE7_GPIO_OUT_MCU_LED3, STD_OFF);
  1111. // Dio_WriteChannel(DioConf_DioChannel_PTE8_GPIO_OUT_MCU_LED4, STD_OFF);
  1112. }
  1113. else if(bootreason == MCU_MDM_AP_SYSTEM_RESET)
  1114. {
  1115. // Dio_WriteChannel(DioConf_DioChannel_PTE0_GPIO_OUT_MCU_LED1, STD_OFF);
  1116. Dio_WriteChannel(DioConf_DioChannel_PTE1_GPIO_OUT_MCU_LED2, STD_OFF);
  1117. // Dio_WriteChannel(DioConf_DioChannel_PTE7_GPIO_OUT_MCU_LED3, STD_OFF);
  1118. // Dio_WriteChannel(DioConf_DioChannel_PTE8_GPIO_OUT_MCU_LED4, STD_OFF);
  1119. }
  1120. else if(bootreason == MCU_SW_RESET)
  1121. {
  1122. Dio_WriteChannel(DioConf_DioChannel_PTE0_GPIO_OUT_MCU_LED1, STD_OFF);
  1123. Dio_WriteChannel(DioConf_DioChannel_PTE1_GPIO_OUT_MCU_LED2, STD_OFF);
  1124. // Dio_WriteChannel(DioConf_DioChannel_PTE7_GPIO_OUT_MCU_LED3, STD_OFF);
  1125. // Dio_WriteChannel(DioConf_DioChannel_PTE8_GPIO_OUT_MCU_LED4, STD_OFF);
  1126. }
  1127. else if(bootreason == MCU_CORE_LOCKUP_RESET)
  1128. {
  1129. // Dio_WriteChannel(DioConf_DioChannel_PTE0_GPIO_OUT_MCU_LED1, STD_OFF);
  1130. // Dio_WriteChannel(DioConf_DioChannel_PTE1_GPIO_OUT_MCU_LED2, STD_OFF);
  1131. Dio_WriteChannel(DioConf_DioChannel_PTE7_GPIO_OUT_MCU_LED3, STD_OFF);
  1132. // Dio_WriteChannel(DioConf_DioChannel_PTE8_GPIO_OUT_MCU_LED4, STD_OFF);
  1133. }
  1134. else if(bootreason == MCU_JTAG_RESET)
  1135. {
  1136. Dio_WriteChannel(DioConf_DioChannel_PTE0_GPIO_OUT_MCU_LED1, STD_OFF);
  1137. // Dio_WriteChannel(DioConf_DioChannel_PTE1_GPIO_OUT_MCU_LED2, STD_OFF);
  1138. Dio_WriteChannel(DioConf_DioChannel_PTE7_GPIO_OUT_MCU_LED3, STD_OFF);
  1139. // Dio_WriteChannel(DioConf_DioChannel_PTE8_GPIO_OUT_MCU_LED4, STD_OFF);
  1140. }
  1141. else if(bootreason == MCU_POWER_ON_RESET)
  1142. {
  1143. // Dio_WriteChannel(DioConf_DioChannel_PTE0_GPIO_OUT_MCU_LED1, STD_OFF);
  1144. Dio_WriteChannel(DioConf_DioChannel_PTE1_GPIO_OUT_MCU_LED2, STD_OFF);
  1145. Dio_WriteChannel(DioConf_DioChannel_PTE7_GPIO_OUT_MCU_LED3, STD_OFF);
  1146. // Dio_WriteChannel(DioConf_DioChannel_PTE8_GPIO_OUT_MCU_LED4, STD_OFF);
  1147. }
  1148. else if(bootreason == MCU_EXTERNAL_PIN_RESET)
  1149. {
  1150. Dio_WriteChannel(DioConf_DioChannel_PTE0_GPIO_OUT_MCU_LED1, STD_OFF);
  1151. Dio_WriteChannel(DioConf_DioChannel_PTE1_GPIO_OUT_MCU_LED2, STD_OFF);
  1152. Dio_WriteChannel(DioConf_DioChannel_PTE7_GPIO_OUT_MCU_LED3, STD_OFF);
  1153. // Dio_WriteChannel(DioConf_DioChannel_PTE8_GPIO_OUT_MCU_LED4, STD_OFF);
  1154. }
  1155. else if(bootreason == MCU_WATCHDOG_RESET)
  1156. {
  1157. // Dio_WriteChannel(DioConf_DioChannel_PTE0_GPIO_OUT_MCU_LED1, STD_OFF);
  1158. // Dio_WriteChannel(DioConf_DioChannel_PTE1_GPIO_OUT_MCU_LED2, STD_OFF);
  1159. // Dio_WriteChannel(DioConf_DioChannel_PTE7_GPIO_OUT_MCU_LED3, STD_OFF);
  1160. Dio_WriteChannel(DioConf_DioChannel_PTE8_GPIO_OUT_MCU_LED4, STD_OFF);
  1161. }
  1162. else if(bootreason == MCU_CMU_LOSS_OF_CLOCK_RESET)
  1163. {
  1164. Dio_WriteChannel(DioConf_DioChannel_PTE0_GPIO_OUT_MCU_LED1, STD_OFF);
  1165. // Dio_WriteChannel(DioConf_DioChannel_PTE1_GPIO_OUT_MCU_LED2, STD_OFF);
  1166. // Dio_WriteChannel(DioConf_DioChannel_PTE7_GPIO_OUT_MCU_LED3, STD_OFF);
  1167. Dio_WriteChannel(DioConf_DioChannel_PTE8_GPIO_OUT_MCU_LED4, STD_OFF);
  1168. }
  1169. else if(bootreason == MCU_LOSS_OF_LOCK_RESET)
  1170. {
  1171. // Dio_WriteChannel(DioConf_DioChannel_PTE0_GPIO_OUT_MCU_LED1, STD_OFF);
  1172. Dio_WriteChannel(DioConf_DioChannel_PTE1_GPIO_OUT_MCU_LED2, STD_OFF);
  1173. // Dio_WriteChannel(DioConf_DioChannel_PTE7_GPIO_OUT_MCU_LED3, STD_OFF);
  1174. Dio_WriteChannel(DioConf_DioChannel_PTE8_GPIO_OUT_MCU_LED4, STD_OFF);
  1175. }
  1176. else if(bootreason == MCU_LOSS_OF_CLOCK_RESET)
  1177. {
  1178. Dio_WriteChannel(DioConf_DioChannel_PTE0_GPIO_OUT_MCU_LED1, STD_OFF);
  1179. Dio_WriteChannel(DioConf_DioChannel_PTE1_GPIO_OUT_MCU_LED2, STD_OFF);
  1180. // Dio_WriteChannel(DioConf_DioChannel_PTE7_GPIO_OUT_MCU_LED3, STD_OFF);
  1181. Dio_WriteChannel(DioConf_DioChannel_PTE8_GPIO_OUT_MCU_LED4, STD_OFF);
  1182. }
  1183. else if(bootreason == MCU_LOW_OR_HIGH_VOLTAGE_DETECT_RESET)
  1184. {
  1185. // Dio_WriteChannel(DioConf_DioChannel_PTE0_GPIO_OUT_MCU_LED1, STD_OFF);
  1186. // Dio_WriteChannel(DioConf_DioChannel_PTE1_GPIO_OUT_MCU_LED2, STD_OFF);
  1187. Dio_WriteChannel(DioConf_DioChannel_PTE7_GPIO_OUT_MCU_LED3, STD_OFF);
  1188. Dio_WriteChannel(DioConf_DioChannel_PTE8_GPIO_OUT_MCU_LED4, STD_OFF);
  1189. }
  1190. else if(bootreason == MCU_NO_RESET_REASON)
  1191. {
  1192. Dio_WriteChannel(DioConf_DioChannel_PTE0_GPIO_OUT_MCU_LED1, STD_OFF);
  1193. // Dio_WriteChannel(DioConf_DioChannel_PTE1_GPIO_OUT_MCU_LED2, STD_OFF);
  1194. Dio_WriteChannel(DioConf_DioChannel_PTE7_GPIO_OUT_MCU_LED3, STD_OFF);
  1195. Dio_WriteChannel(DioConf_DioChannel_PTE8_GPIO_OUT_MCU_LED4, STD_OFF);
  1196. }
  1197. else if(bootreason == MCU_MULTIPLE_RESET_REASON)
  1198. {
  1199. // Dio_WriteChannel(DioConf_DioChannel_PTE0_GPIO_OUT_MCU_LED1, STD_OFF);
  1200. Dio_WriteChannel(DioConf_DioChannel_PTE1_GPIO_OUT_MCU_LED2, STD_OFF);
  1201. Dio_WriteChannel(DioConf_DioChannel_PTE7_GPIO_OUT_MCU_LED3, STD_OFF);
  1202. Dio_WriteChannel(DioConf_DioChannel_PTE8_GPIO_OUT_MCU_LED4, STD_OFF);
  1203. }
  1204. else if(bootreason == MCU_RESET_UNDEFINED)
  1205. {
  1206. Dio_WriteChannel(DioConf_DioChannel_PTE0_GPIO_OUT_MCU_LED1, STD_OFF);
  1207. Dio_WriteChannel(DioConf_DioChannel_PTE1_GPIO_OUT_MCU_LED2, STD_OFF);
  1208. Dio_WriteChannel(DioConf_DioChannel_PTE7_GPIO_OUT_MCU_LED3, STD_OFF);
  1209. Dio_WriteChannel(DioConf_DioChannel_PTE8_GPIO_OUT_MCU_LED4, STD_OFF);
  1210. }
  1211. }