hal_adapter.c 37 KB

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