hal_adapter.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  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] = {0};
  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 //send message fail, we need to recover the send function by make the can controller bus off
  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); //if the bus off occured, we need to restart the can controller
  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 LPTMR_WakeUp_Notification(void)
  973. {
  974. Mcu_InitClock(McuClockSettingConfig_0);
  975. Mcu_SetMode(McuModeSettingConf_Run);
  976. Gpt_StopTimer(GptConf_GptChannelConfiguration_GptChannelConfiguration_LPTMR);
  977. Gpt_DisableWakeup(GptConf_GptChannelConfiguration_GptChannelConfiguration_LPTMR);
  978. Gpt_DisableNotification(GptConf_GptChannelConfiguration_GptChannelConfiguration_LPTMR);
  979. }
  980. void SystemDeinit(void)
  981. {
  982. Dio_WriteChannel(DioConf_DioChannel_PTA7_GPIO_OUT_MCU_4G_PWRKEY, STD_OFF);
  983. vTaskDelay(pdMS_TO_TICKS(3000));
  984. Dio_WriteChannel(DioConf_DioChannel_PTA6_GPIO_OUT_MCU_4G_POW_EN, STD_OFF);
  985. Dio_WriteChannel(DioConf_DioChannel_PTD1_GPIO_OUT_MCU_GPS_POW_EN, STD_OFF);//GPS ShutDown
  986. Dio_WriteChannel(DioConf_DioChannel_PTE7_GPIO_OUT_MCU_LED3, STD_ON);
  987. Dio_WriteChannel(DioConf_DioChannel_PTE1_GPIO_OUT_MCU_LED2, STD_ON);
  988. Dio_WriteChannel(DioConf_DioChannel_PTE0_GPIO_OUT_MCU_LED1, STD_ON);
  989. Uart_Deinit();
  990. Can_SetControllerMode(CanController_0, CAN_CS_STOPPED);
  991. Can_SetControllerMode(CanController_1, CAN_CS_STOPPED);
  992. // Can_SetControllerMode(CanController_2, CAN_CS_STOPPED);
  993. Dio_WriteChannel(DioConf_DioChannel_PTC17_GPIO_OUT_MCU_CAN0_STB, STD_ON);
  994. Dio_WriteChannel(DioConf_DioChannel_PTC16_GPIO_OUT_MCU_CAN1_STB, STD_ON);
  995. Can_DeInit();
  996. Adc_DeInit();
  997. // Gpt_DisableNotification(GptConf_GptChannelConfiguration_GptChannelConfiguration_0);
  998. Gpt_DeInit();
  999. Spi_DeInit();
  1000. Mcl_DeInit();
  1001. //port DeInit
  1002. for(int pinIndex = 0; pinIndex <PortConfigSet_PortContainer_GPIO_PTB4_GPIO_OUT_MCU_RS485_EN; pinIndex++)
  1003. {
  1004. if(pinIndex == PortConfigSet_PortContainer_CAN_PTA12_CAN1_RX_MCU_CAN1_RX
  1005. || pinIndex == PortConfigSet_PortContainer_INT_PTE11_GPIO_IN_MCU_3D_INT1
  1006. || pinIndex == PortConfigSet_PortContainer_INT_PTD5_GPIO_IN_MCU_3D_INT2
  1007. || pinIndex == PortConfigSet_PortContainer_INT_PTB0_GPIO_IN_MCU_WAKEUP1
  1008. || pinIndex == PortConfigSet_PortContainer_INT_PTE2_GPIO_IN_MCU_WAKEUP2)
  1009. {
  1010. continue;
  1011. }
  1012. else
  1013. {
  1014. Port_SetAsUnusedPin(pinIndex);
  1015. }
  1016. }
  1017. Port_SetPinMode(PortConfigSet_PortContainer_CAN_PTA12_CAN1_RX_MCU_CAN1_RX,PORT_GPIO_MODE);
  1018. // systemInitFlag = false;
  1019. }
  1020. void MCUEnterSleep(void)
  1021. {
  1022. if(pdTRUE == xSemaphoreTake(sleep_mutex,1) && Fota_Process_Going == false)
  1023. {
  1024. // if the IO wakeup exists(high side), the mcu should not go to vlps, either.
  1025. Dio_LevelType wakeup1,wakeup2 = STD_HIGH;
  1026. wakeup1 = Dio_ReadChannel(DioConf_DioChannel_PTB0_GPIO_IN_MCU_WAKEUP1);
  1027. wakeup2 = Dio_ReadChannel(DioConf_DioChannel_PTE2_GPIO_IN_MCU_WAKEUP2);
  1028. if(wakeup1==STD_LOW && wakeup2 == STD_LOW)
  1029. {
  1030. extern boolean Uart_4G_Task_Sleep_FLag;
  1031. WdgDeInit();
  1032. Std_ReturnType Ret = E_NOT_OK;
  1033. uint8 appConfigWriteTimes = 0;
  1034. do
  1035. {
  1036. waitForSleepFlag = true;
  1037. //save the app configure before power off
  1038. if(Ret == E_NOT_OK)
  1039. {
  1040. AppConfigInfo.appSaveFlg = false;
  1041. Ret = HAL_EEP_Write(0,(uint8 *)&AppConfigInfo,sizeof(AppConfigInfo));
  1042. appConfigWriteTimes++;
  1043. }
  1044. vTaskDelay(pdMS_TO_TICKS(10));
  1045. }while(Uart_4G_Task_Sleep_FLag == false || (Ret == E_NOT_OK && appConfigWriteTimes<5) );
  1046. vTaskDelete(Uart_Hal_RecvTask_Handle);
  1047. vTaskDelete(Uart_Hal_SendTask_Handle);
  1048. vTaskDelete(CanTask_Handle);
  1049. vTaskDelete(GpsTask_Handle);
  1050. vTaskDelete(Uart_4G_Task_Handle);
  1051. // vTaskDelete(MainTask_Handle);
  1052. SystemDeinit();
  1053. MCUSleep();
  1054. }
  1055. }
  1056. }
  1057. void coreInit(void)
  1058. {
  1059. /* Initialize the Mcu driver */
  1060. #if (MCU_PRECOMPILE_SUPPORT == STD_ON)
  1061. Mcu_Init(NULL_PTR);
  1062. #elif (MCU_PRECOMPILE_SUPPORT == STD_OFF)
  1063. Mcu_Init(&Mcu_Config_VS_0);
  1064. #endif /* (MCU_PRECOMPILE_SUPPORT == STD_ON) */
  1065. Mcu_InitClock(McuClockSettingConfig_0);
  1066. /* Wait until PLL is locked */
  1067. while (MCU_PLL_LOCKED != Mcu_GetPllStatus())
  1068. {
  1069. /* Busy wait until the System PLL is locked */
  1070. }
  1071. Mcu_DistributePllClock();
  1072. OsIf_Init(NULL_PTR);
  1073. Platform_Init(NULL_PTR);
  1074. /* Initialize all pins*/
  1075. #if (PORT_PRECOMPILE_SUPPORT == STD_ON)
  1076. Port_Init(NULL_PTR);
  1077. #elif (PORT_PRECOMPILE_SUPPORT == STD_OFF)
  1078. Port_Init(&Port_Config_VS_0);
  1079. #endif
  1080. }
  1081. void SystemModulesInit(void)
  1082. {
  1083. // Dio_WriteChannel(DioConf_DioChannel_PTE0_GPIO_OUT_MCU_LED1, STD_OFF);
  1084. /* Initialize Mcl module */
  1085. Mcl_Init(NULL_PTR);
  1086. // SEGGER_RTT_Init();
  1087. /* Initializes an UART driver*/
  1088. #if (UART_PRECOMPILE_SUPPORT == STD_ON)
  1089. Uart_Init(NULL_PTR);
  1090. #elif (UART_PRECOMPILE_SUPPORT == STD_OFF)
  1091. Uart_Init(&Uart_xConfig_VS_0);
  1092. #endif
  1093. IP_LPUART0->CTRL |= LPUART_CTRL_ILT(1);
  1094. IP_LPUART1->CTRL |= LPUART_CTRL_ILT(1);
  1095. IP_LPUART2->CTRL |= LPUART_CTRL_ILT(1);
  1096. IP_LPUART0->CTRL |= LPUART_CTRL_IDLECFG(3);
  1097. IP_LPUART1->CTRL |= LPUART_CTRL_IDLECFG(3);
  1098. IP_LPUART2->CTRL |= LPUART_CTRL_IDLECFG(3);
  1099. #if 1 /* Initialize Platform driver */
  1100. #if (CAN_PRECOMPILE_SUPPORT == STD_ON)
  1101. Can_Init(NULL_PTR);
  1102. #elif (CAN_PRECOMPILE_SUPPORT == STD_OFF)
  1103. Can_Init(&Can_Config_VS_0);
  1104. #endif
  1105. Can_SetControllerMode(CanController_0, CAN_CS_STARTED);
  1106. Can_SetControllerMode(CanController_1, CAN_CS_STARTED);
  1107. Dio_WriteChannel(DioConf_DioChannel_PTC17_GPIO_OUT_MCU_CAN0_STB, STD_OFF);
  1108. Dio_WriteChannel(DioConf_DioChannel_PTC16_GPIO_OUT_MCU_CAN1_STB, STD_OFF);
  1109. // Can_SetControllerMode(CanController_2, CAN_CS_STARTED);
  1110. #endif
  1111. #if (ADC_PRECOMPILE_SUPPORT == STD_ON)
  1112. Adc_Init(NULL_PTR);
  1113. #else
  1114. Adc_Init(&Adc_Config_VS_0);
  1115. #endif /* ADC_PRECOMPILE_SUPPORT == STD_ON */
  1116. //the calibration is needed to increase the ADC accuracy
  1117. Adc_CalibrationStatusType CalibStatus;
  1118. Adc_Calibrate(AdcHwUnit_1, &CalibStatus);
  1119. if (CalibStatus.Adc_UnitSelfTestStatus == E_NOT_OK)
  1120. {
  1121. // if so, the adc is failed to Calibrate;
  1122. }
  1123. /* Initialize Eep driver */
  1124. #if defined (EEP_PRECOMPILE_SUPPORT)
  1125. Eep_Init(NULL_PTR);
  1126. #else
  1127. Eep_Init(&Eep_Config_VS_0);
  1128. #endif
  1129. /* Partition only if it was not partitioned before for EERAM with code 0x4 */
  1130. // Eep_DepartParitition(T_EEEPROM_SIZE);
  1131. //Init Flash Driver
  1132. #if defined (FLS_PRECOMPILE_SUPPORT)
  1133. Fls_Init(NULL_PTR);
  1134. #else
  1135. Fls_Init(&Fls_Config_VS_0);
  1136. while(MEMIF_IDLE == Fls_GetStatus())
  1137. {
  1138. ;
  1139. }
  1140. #endif
  1141. Spi_Init(NULL_PTR);
  1142. /* Initialize the Gpt driver */
  1143. Gpt_Init(&Gpt_Config_VS_0);
  1144. /* Enable the Gpt notification to periodically service the Wdg */
  1145. // Gpt_EnableNotification(GptConf_GptChannelConfiguration_GptChannelConfiguration_0);
  1146. Icu_DeInit();
  1147. WdgInit();
  1148. IsFeedWdg = true;
  1149. }
  1150. void displayResetReasonWithLED(void)
  1151. {
  1152. Mcu_ResetType bootreason;
  1153. bootreason = Mcu_GetResetReason();
  1154. }
  1155. void readConfigAndDataFromEEPROM(void)
  1156. {
  1157. uint32 *eppromAddStart = 0x14000000;
  1158. if (*(uint8 *)(eppromAddStart) == 0xff)
  1159. {
  1160. HAL_EEP_Write(0, (uint8 *)&AppConfigInfo, sizeof(AppConfigInfo));
  1161. }
  1162. Std_ReturnType readRet = HAL_EEP_Read(0, (uint8 *)&AppConfigInfo, sizeof(AppConfigInfo));
  1163. if(readRet == E_NOT_OK)
  1164. {
  1165. uint8 cycleTimes = 0;
  1166. while(HAL_EEP_Read(200, (uint8 *)&AppConfigInfo, sizeof(AppConfigInfo))==E_NOT_OK && cycleTimes<=5) //backup data addr:200
  1167. {
  1168. cycleTimes++;
  1169. }
  1170. }
  1171. //read bcu data
  1172. HAL_EEP_Read(BCU_SAVE_ADDR, (uint8 *)&BcuSaveDataInfo, sizeof(BcuSaveDataInfo));
  1173. if(BcuSaveDataInfo.header.crc16 != crc_chk(&(BcuSaveDataInfo.BcuAppSaveDataInfo),sizeof(BcuSaveDataInfo.BcuAppSaveDataInfo)))
  1174. {
  1175. memcpy(&BcuSaveDataInfo.BcuAppSaveDataInfo,&BcuAppSaveDataInfoDefaultValue,sizeof(BcuSaveDataInfo.BcuAppSaveDataInfo));
  1176. // BcuSaveDataInfo.BcuAppSaveDataInfo.bcuSaveFlg = false;
  1177. BcuSaveDataInfo.header.crc16 = crc_chk(&(BcuSaveDataInfo.BcuAppSaveDataInfo),sizeof(BcuSaveDataInfo.BcuAppSaveDataInfo));
  1178. HAL_EEP_Write(BCU_SAVE_ADDR, (uint8 *)&BcuSaveDataInfo, sizeof(BcuSaveDataInfo));
  1179. rtev_flg_EESt[0] = 1;
  1180. rtev_flg_EESt[1] = 1;
  1181. rtev_flg_EESt[2] = 1;
  1182. rtev_flg_EESt[3] = 1;
  1183. }
  1184. }
  1185. void IH_Data_Update(void)
  1186. {
  1187. hvmd_st_chgCnctObj_T2 = BBox_VCUConSt;
  1188. ihd_pct_socCor = BBox_SocCorrectionValue;
  1189. BBox_SocCorrectionValue = 0; //clear the SOC correction value from cloud which only available once
  1190. if(BBox_SohCorrectionValue != 0)
  1191. {
  1192. sohd_pct_bcuSoh = BBox_SohCorrectionValue;
  1193. BBox_SohCorrectionValue = 0;
  1194. }
  1195. if(BMS_MaxCellVolt != 0 && BMS_MaxCellVolt != 0xFFFF)
  1196. {
  1197. ihd_V_cellUMax = (real32_T)(BMS_MaxCellVolt/1000.0);
  1198. }
  1199. if(BMS_MinCellVolt != 0 && BMS_MinCellVolt != 0xFFFF)
  1200. {
  1201. ihd_V_cellUMin = (real32_T)(BMS_MinCellVolt/1000.0);
  1202. }
  1203. if(BMS_AverCellVolt != 0 && BMS_AverCellVolt != 0xFFFF)
  1204. {
  1205. ihd_V_cellUAvrg = (real32_T)(BMS_AverCellVolt/1000.0);
  1206. }
  1207. if(BMS_BattVolt != 0 && BMS_BattVolt != 0xFFFF)
  1208. {
  1209. ihd_V_battU_T2 = (real32_T)(BMS_BattVolt*0.1);
  1210. }
  1211. ihd_I_battCurr_T2 = (real32_T)(BMS_PackCurr*0.1)-1000;
  1212. dcmd_st_chrgSt_T2 = BMS_CharSt;
  1213. }
  1214. void OH_Data_Update(void)
  1215. {
  1216. //socd_pct_ekfSoc; //EKFSOC
  1217. //socd_pct_battSoc; //电池SOC
  1218. //socd_pct_bcuSoc; //显示SOC
  1219. BBox_PackSOC = (uint8)(socd_pct_ahSoc /0.4); //安时SOC
  1220. BBox_PackSOH = (uint8)(sohd_pct_bcuSoh); //Soh
  1221. BBox_Dq_Dv_MIN = (uint32)(socd_Q_dMin * 1000); //dQ/dV
  1222. BBox_Dq_Dv_MAX = (uint32)(socd_Q_dMax * 1000); //dQ/dV
  1223. BBox_TotalDischarEngy = (uint32)(damd_E_totalDisChrgEng*10); //累计放电电电量
  1224. BBox_TotalDischarCapy = (uint32)(damd_Q_totalDisChrgCp*10); //累计放电电容量
  1225. BBox_TotalBackCharEngy = (uint32)(damd_E_totalRegenEng*10); //累计回收电量
  1226. BBox_TotalBackCharCapy = (uint32)(damd_Q_totalRegenCp*10); //累计回收容量
  1227. BBox_TotalCharEngy = BBox_TotalBackCharEngy + (uint32)(damd_E_totalChrgEng*10); //累计充电电量
  1228. BBox_TotalCharCapy = BBox_TotalBackCharCapy + (uint32)(damd_Q_totalChrgCp*10); //累计充电容量
  1229. BBox_TotalStaCharCapy = (uint32)(damd_Q_totalSatChrgCp*10); //累计回收容量
  1230. BBox_TotalStaCharEngy = (uint32)(damd_E_totalSatChrgEng*10); //累计回收容量
  1231. BBox_TotalGunCharCapy = (uint32)(damd_Q_totalVehChrgCp*10); //累计回收容量
  1232. BBox_TotalGunCharEngy = (uint32)(damd_E_totalVehChrgEng*10); //累计回收容量
  1233. }