hal_adapter.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  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. if(UartIdx==UART_LPUART0)
  355. {
  356. Dio_WriteChannel(DioConf_DioChannel_PTB4_GPIO_OUT_MCU_RS485_EN, STD_ON);
  357. }
  358. T_Uart_Status[UartIdx] = Uart_AsyncSend(UartIdx, UartSendMsg.dataPtr, UartSendMsg.DataLen);
  359. if (E_OK != T_Uart_Status[UartIdx])
  360. {
  361. Uart_Abort(UartIdx, UART_SEND);
  362. UartSendState[UartIdx] = UartAbortSend;
  363. }
  364. else
  365. {
  366. UartSendState[UartIdx] = UartStartSend;
  367. }
  368. }
  369. /*开始发送后的判定*/
  370. if(UartSendState[UartIdx] == UartStartSend)
  371. {
  372. Uart_TransmitStatus[UartIdx] = Uart_GetStatus(UartIdx, &T_bytesRemaining[UartIdx], UART_SEND);
  373. T_timeout[UartIdx]++;
  374. }
  375. if(T_timeout[UartIdx]>=1000 || ((Uart_TransmitStatus[UartIdx] != UART_STATUS_OPERATION_ONGOING) && (UartSendState[UartIdx] == UartStartSend)))
  376. {
  377. if(T_timeout[UartIdx]>=1000)
  378. {
  379. Uart_Abort(UartIdx, UART_SEND);
  380. UartSendState[UartIdx] = UartAbortSend;
  381. }
  382. else if(Uart_TransmitStatus[UartIdx] == UART_STATUS_NO_ERROR)
  383. {
  384. UartSendState[UartIdx] = UartSendComplete;
  385. }
  386. T_timeout[UartIdx] = 0;
  387. }
  388. UartIdx = (UartIdx + 1) > 2 ? 0 : (UartIdx + 1);
  389. }
  390. }
  391. //
  392. //Std_ReturnType UART_Query_Data(uint8 transChannel, uint8 recvChannel, const uint8 *txBuffer, uint32 sendLength, uint8 *rxBuffer, uint16 *rxlen, uint32 T_timeout)
  393. //{
  394. // volatile Std_ReturnType R_Uart_Status;
  395. // volatile Std_ReturnType T_Uart_Status;
  396. // volatile Uart_StatusType Uart_ReceiveStatus = UART_STATUS_TIMEOUT;
  397. // volatile Uart_StatusType Uart_TransmitStatus = UART_STATUS_TIMEOUT;
  398. // uint32 T_bytesRemaining;
  399. // uint32 R_bytesRemaining;
  400. // uint32 timeout = T_timeout;
  401. // uint32 retVal = E_NOT_OK;
  402. // bufferIdx[recvChannel] = 0;
  403. // switch (recvChannel)
  404. // {
  405. // case 0:
  406. // IP_LPUART0->CTRL |= LPUART_CTRL_ILIE(1);
  407. // break;
  408. // case 1:
  409. // IP_LPUART1->CTRL |= LPUART_CTRL_ILIE(1);
  410. // break;
  411. // case 2:
  412. // IP_LPUART2->CTRL |= LPUART_CTRL_ILIE(1);
  413. // break;
  414. // default:
  415. // break;
  416. // }
  417. // if (txBuffer == NULL || rxBuffer == NULL)
  418. // {
  419. // return retVal;
  420. // }
  421. //
  422. // /* Uart_AsyncSend transmit data */
  423. // Uart_SetBuffer(transChannel, txBuffer, sendLength, UART_SEND);
  424. // T_Uart_Status = Uart_AsyncSend(transChannel, txBuffer, sendLength);
  425. // if (E_OK != T_Uart_Status)
  426. // {
  427. // Uart_Abort(transChannel, UART_SEND);
  428. // return E_NOT_OK;
  429. // }
  430. // Uart_SetBuffer(recvChannel, &RX_Buffer[recvChannel][0], DMA_SIZE, UART_RECEIVE);
  431. // R_Uart_Status = Uart_AsyncReceive(recvChannel, rxBuffer, DMA_SIZE);
  432. // if (E_OK != R_Uart_Status)
  433. // {
  434. // Uart_Abort(recvChannel, UART_RECEIVE);
  435. // return E_NOT_OK;
  436. // }
  437. // /* Check for no on-going transmission */
  438. // do
  439. // {
  440. // if (Uart_TransmitStatus != UART_STATUS_NO_ERROR)
  441. // {
  442. // Uart_TransmitStatus = Uart_GetStatus(transChannel, &T_bytesRemaining, UART_SEND);
  443. // }
  444. // if (Uart_ReceiveStatus != UART_STATUS_NO_ERROR)
  445. // {
  446. // Uart_ReceiveStatus = Uart_GetStatus(recvChannel, &R_bytesRemaining, UART_RECEIVE);
  447. // }
  448. // vTaskDelay(pdMS_TO_TICKS(1));
  449. // } while (((UART_STATUS_NO_ERROR != Uart_TransmitStatus || UART_STATUS_NO_ERROR != Uart_ReceiveStatus) && 0 < --timeout));
  450. // if ((UART_STATUS_NO_ERROR != Uart_TransmitStatus))
  451. // {
  452. // Uart_Abort(transChannel, UART_SEND);
  453. // retVal = E_NOT_OK;
  454. // }
  455. // else
  456. // {
  457. // retVal = E_OK;
  458. // }
  459. // if ((UART_STATUS_NO_ERROR != Uart_ReceiveStatus))
  460. // {
  461. // Uart_Abort(recvChannel, UART_RECEIVE);
  462. // *rxlen = bufferIdx[recvChannel];
  463. // retVal = E_NOT_OK;
  464. // }
  465. // else
  466. // {
  467. // *rxlen = bufferIdx[recvChannel];
  468. // retVal = E_OK;
  469. // }
  470. // return retVal;
  471. //}
  472. //
  473. //Std_ReturnType UART_Send_Data(uint8 transChannel, const uint8 *txBuffer, uint32 sendLength, uint32 T_timeout)
  474. //{
  475. //
  476. // volatile Std_ReturnType T_Uart_Status;
  477. // volatile Uart_StatusType Uart_TransmitStatus = UART_STATUS_TIMEOUT;
  478. // uint32 T_bytesRemaining;
  479. // uint32 timeout = T_timeout;
  480. // uint32 retVal = E_NOT_OK;
  481. // if (txBuffer == NULL)
  482. // {
  483. // return retVal;
  484. // }
  485. //
  486. // /* Uart_AsyncSend transmit data */
  487. // T_Uart_Status = Uart_AsyncSend(transChannel, txBuffer, sendLength);
  488. // if (E_OK != T_Uart_Status)
  489. // {
  490. // Uart_Abort(transChannel, UART_SEND);
  491. // return E_NOT_OK;
  492. // }
  493. // /* Check for no on-going transmission */
  494. // do
  495. // {
  496. // Uart_TransmitStatus = Uart_GetStatus(transChannel, &T_bytesRemaining, UART_SEND);
  497. // vTaskDelay(pdMS_TO_TICKS(1));
  498. // } while ((UART_STATUS_NO_ERROR != Uart_TransmitStatus && 0 < --timeout));
  499. //
  500. // if ((UART_STATUS_NO_ERROR != Uart_TransmitStatus))
  501. // {
  502. // retVal = E_NOT_OK;
  503. // }
  504. // else
  505. // {
  506. // retVal = E_OK;
  507. // }
  508. // return retVal;
  509. //}
  510. //
  511. //Std_ReturnType UART_Receive_Data(uint8 recvChannel, uint8 *rxBuffer, uint16 *rxlen, sint32 T_timeout)
  512. //{
  513. // volatile Std_ReturnType R_Uart_Status = E_NOT_OK;
  514. // volatile Uart_StatusType Uart_ReceiveStatus = UART_STATUS_TIMEOUT;
  515. // uint32 T_bytesRemaining = 0;
  516. // uint32 retVal = E_NOT_OK;
  517. // // uint8 Rx_Buffer[MSG_LEN];
  518. // bufferIdx[recvChannel] = 0;
  519. // *rxlen = 0;
  520. // if (rxBuffer == NULL)
  521. // {
  522. // return retVal;
  523. // }
  524. // /* Uart_AsyncReceive transmit data */
  525. // switch (recvChannel)
  526. // {
  527. // case 0:
  528. // IP_LPUART0->CTRL |= LPUART_CTRL_ILIE(1);
  529. // break;
  530. // case 1:
  531. // IP_LPUART1->CTRL |= LPUART_CTRL_ILIE(1);
  532. // break;
  533. // case 2:
  534. // IP_LPUART2->CTRL |= LPUART_CTRL_ILIE(1);
  535. // break;
  536. // default:
  537. // break;
  538. // }
  539. // Uart_SetBuffer(recvChannel, rxBuffer, DMA_SIZE, UART_RECEIVE);
  540. // R_Uart_Status = Uart_AsyncReceive(recvChannel, rxBuffer, DMA_SIZE);
  541. // if (E_OK != R_Uart_Status)
  542. // {
  543. // Uart_Abort(recvChannel, UART_RECEIVE);
  544. // return E_NOT_OK;
  545. // }
  546. // /* Check for no on-going transmission */
  547. // do
  548. // {
  549. // Uart_ReceiveStatus = Uart_GetStatus(recvChannel, &T_bytesRemaining, UART_RECEIVE);
  550. // vTaskDelay(pdMS_TO_TICKS(1));
  551. //
  552. // } while ((UART_STATUS_NO_ERROR != Uart_ReceiveStatus) && 0 < T_timeout--);
  553. // if ((UART_STATUS_NO_ERROR != Uart_ReceiveStatus))
  554. // {
  555. // Uart_Abort(recvChannel, UART_RECEIVE);
  556. // *rxlen = bufferIdx[recvChannel];
  557. // retVal = E_NOT_OK;
  558. // }
  559. // else
  560. // {
  561. // *rxlen = bufferIdx[recvChannel];
  562. // retVal = E_OK;
  563. // }
  564. // return retVal;
  565. //}
  566. extern Lpuart_Uart_Ip_StateStructureType *Lpuart_Uart_Ip_apStateStructuresArray[LPUART_UART_IP_NUMBER_OF_INSTANCES];
  567. void UART_Callback(uint32 hwInstance, Lpuart_Uart_Ip_EventType event)
  568. {
  569. // (void)userData;
  570. Lpuart_Uart_Ip_StateStructureType * UartState;
  571. UartState = (Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[hwInstance];
  572. if (hwInstance==0&&event == LPUART_UART_IP_EVENT_END_TRANSFER)
  573. {
  574. Dio_WriteChannel(DioConf_DioChannel_PTB4_GPIO_OUT_MCU_RS485_EN, STD_OFF);
  575. }
  576. /* Check the event type */
  577. if (event == LPUART_UART_IP_EVENT_RX_FULL)
  578. {
  579. /* The reception stops when receiving idle is detected or the buffer is full */
  580. if (bufferIdx[hwInstance] <= (BUFFER_SIZE - DMA_SIZE))
  581. {
  582. /* Update the buffer index and the rx buffer */
  583. bufferIdx[hwInstance] += DMA_SIZE;
  584. Uart_SetBuffer(hwInstance, &RX_Buffer[hwInstance][bufferIdx[hwInstance]], DMA_SIZE, UART_RECEIVE);
  585. // Lpuart_Uart_Ip_SetRxBuffer(hwInstance, &RX_Buffer[bufferIdx], DMA_SIZE);
  586. }
  587. }
  588. if (event == LPUART_UART_IP_EVENT_ERROR)
  589. {
  590. // /*Get the transfered data size. DMA Channel 1 is used for LPUART DMA receiving, please modify accordingly.*/
  591. // temp = DMA_SIZE - (uint32_t)IP_DMA->TCD->CITER.ELINKNO;
  592. // /*Add the remaining data size to the sum of the received size*/
  593. // bufferIdx[hwInstance] += temp;
  594. /*Abort the receiving after detecting IDLE receiving*/
  595. Lpuart_Uart_Ip_AbortReceivingData(hwInstance);
  596. Lpuart_Uart_Ip_AbortSendingData(hwInstance);
  597. // bufferIdx = 0;
  598. }
  599. if (event == LPUART_UART_IP_EVENT_RECV_IDLE)
  600. {
  601. uint32_t temp;
  602. UartHalMsg_t UartHalMsg;
  603. UartHalMsg.Channel = hwInstance;
  604. UartHalMsg.event = event;
  605. /*Get the transfered data size. DMA Channel 1 is used for LPUART DMA receiving, please modify accordingly.*/
  606. temp = DMA_SIZE - (uint32_t)IP_DMA->TCD[hwInstance].CITER.ELINKNO;
  607. /*Add the remaining data size to the sum of the received size*/
  608. bufferIdx[hwInstance] += temp;
  609. /*Abort the receiving after detecting IDLE receiving*/
  610. UartHalMsg.value = bufferIdx[hwInstance];
  611. xQueueSendFromISR(UartHalQueueHandle,&UartHalMsg,pdFALSE);
  612. }
  613. }
  614. /*CAN*/
  615. Can_PduType Can_CreatePduInfo(Can_IdType id, CAN_IdFrameType idFrame, PduIdType swPduHandle, uint8 length, uint8 *sdu)
  616. {
  617. Can_PduType PduInfo;
  618. switch (idFrame)
  619. {
  620. case CAN_STANDARD_ID_TYPE:
  621. id = id & 0x7FF;
  622. break;
  623. case CANFD_STANDARD_ID_TYPE:
  624. id = (id & 0x7FF) | 0x40000000;
  625. break;
  626. case CAN_EXTENDED_ID_TYPE:
  627. id = id | 0x80000000;
  628. break;
  629. case CANFD_EXTENDED_ID_TYPE:
  630. id = id | 0xC0000000;
  631. break;
  632. default:
  633. id = id & 0x7FF;
  634. break;
  635. }
  636. PduInfo.id = id;
  637. PduInfo.swPduHandle = swPduHandle;
  638. PduInfo.length = length;
  639. PduInfo.sdu = sdu;
  640. return PduInfo;
  641. }
  642. Std_ReturnType CanIf_SendMessage(uint8 ControllerId, Can_Msg_Type CanMsg)
  643. {
  644. volatile Can_PduType Can_PduInfo;
  645. volatile Std_ReturnType CAN_Write_Status;
  646. Std_ReturnType retVal = E_NOT_OK;
  647. uint32 u8TimeOut = 100 * 100;
  648. Can_HwHandleType Hth = Can0HardwareObject_TX + (Can_HwHandleType)ControllerId; // controller 0 --> Can0HardwareObject_TX
  649. Can_PduInfo = Can_CreatePduInfo(CanMsg.id, CanMsg.idFrame, 0, CanMsg.length, CanMsg.sdu);
  650. CAN_Write_Status = Can_Write(Hth, &Can_PduInfo);
  651. CanIf_bTxFlag = FALSE;
  652. if (CAN_Write_Status == E_OK)
  653. {
  654. while ((!CanIf_bTxFlag) && (u8TimeOut != 0U))
  655. {
  656. Can_MainFunction_Write();
  657. u8TimeOut--;
  658. }
  659. }
  660. else //send message fail, we need to recover the send function by make the can controller bus off
  661. {
  662. Can_MainFunction_BusOff();
  663. }
  664. if (CanIf_bTxFlag == TRUE)
  665. {
  666. retVal = E_OK;
  667. }
  668. else
  669. {
  670. retVal = E_NOT_OK;
  671. }
  672. return retVal;
  673. }
  674. Can_Msg_Type Can_GetMsgInfo(Can_IdType id, uint8 length, uint8 *sdu)
  675. {
  676. Can_Msg_Type CanMsgInfo;
  677. CanMsgInfo.idFrame = (CAN_IdFrameType)((id >> 30) & 0x03);
  678. if (CanMsgInfo.idFrame & 0x01)
  679. {
  680. CanMsgInfo.id = id & 0x7FF;
  681. }
  682. else
  683. {
  684. CanMsgInfo.id = id & 0x1FFFFFFF;
  685. }
  686. CanMsgInfo.length = length;
  687. CanMsgInfo.sdu = sdu;
  688. return CanMsgInfo;
  689. }
  690. void CanIf_ControllerBusOff(uint8 ControllerId)
  691. {
  692. (void)ControllerId;
  693. Can_SetControllerMode(ControllerId, CAN_CS_STARTED); //if the bus off occured, we need to restart the can controller
  694. }
  695. void CanIf_ControllerModeIndication(uint8 ControllerId, Can_ControllerStateType ControllerMode)
  696. {
  697. (void)ControllerId;
  698. (void)ControllerMode;
  699. }
  700. void CanIf_TxConfirmation(PduIdType CanTxPduId)
  701. {
  702. CanIf_u8TxConfirmCnt++;
  703. CanIf_bTxFlag = TRUE;
  704. (void)CanTxPduId;
  705. }
  706. void CanIf_RxIndication(const Can_HwType *Mailbox, const PduInfoType *PduInfoPtr)
  707. {
  708. Can_Msg_Type canRxMsg_Buff;
  709. Can_Msg_Type_Data canRxMsgQueueData;
  710. CanIf_bRxFlag = TRUE; // should not be delete
  711. // should put the msg into message queue
  712. canRxMsg_Buff = Can_GetMsgInfo(Mailbox->CanId, PduInfoPtr->SduLength, PduInfoPtr->SduDataPtr);
  713. canRxMsgQueueData.id = canRxMsg_Buff.id;
  714. canRxMsgQueueData.length = canRxMsg_Buff.length;
  715. memcpy(canRxMsgQueueData.data, canRxMsg_Buff.sdu, canRxMsgQueueData.length);
  716. switch(Mailbox->Hoh)
  717. {
  718. case 0:
  719. xQueueSend(CanRecvQueueHandle0, &canRxMsgQueueData, 0);
  720. break;
  721. case 1:
  722. xQueueSend(CanRecvQueueHandle1, &canRxMsgQueueData, 0);
  723. break;
  724. case 2:
  725. xQueueSend(CanRecvQueueHandle2, &canRxMsgQueueData, 0);
  726. break;
  727. }
  728. }
  729. void CanIf_CurrentIcomConfiguration(uint8 ControllerId, IcomConfigIdType ConfigurationId, IcomSwitch_ErrorType Error)
  730. {
  731. (void)ControllerId;
  732. (void)ConfigurationId;
  733. (void)Error;
  734. }
  735. void Notification_0(void)
  736. {
  737. ADC_Converter(ResultBuffer, ConvertedBuffer);
  738. memcpy(BattTempR, &ConvertedBuffer[3], 4 * sizeof(uint32));
  739. }
  740. void Notification_1(void)
  741. {
  742. VarNotification_1++;
  743. }
  744. Std_ReturnType ADC_Converter(Adc_ValueGroupType *Buffer, TP_Value_Type *ConvertedValueR)
  745. {
  746. Adc_ValueGroupType REFH, REFL;
  747. REFH = Buffer[0];
  748. REFL = Buffer[2];
  749. for (int i = 3; i < NUM_RESULTS; i++)
  750. {
  751. if (Buffer[i] >= REFH)
  752. {
  753. ConvertedValueR[i] = 40930000;
  754. }
  755. else if (Buffer[i] <= REFL)
  756. {
  757. ConvertedValueR[i] = 0x00;
  758. }
  759. else
  760. {
  761. ConvertedValueR[i] = (TP_Value_Type)((float)(10000 * (Buffer[i] - REFL) / (float)(REFH - REFL)) / (1 - (float)((Buffer[i] - REFL) / (float)(REFH - REFL))));
  762. }
  763. }
  764. return 0;
  765. }
  766. Std_ReturnType ADC_ReadValue()
  767. {
  768. Std_ReturnType ret = E_NOT_OK;
  769. for (uint8 i = 0; i < NUM_RESULTS; i++)
  770. {
  771. ResultBuffer[i] = 0xFFFF;
  772. ConvertedBuffer[i] = 0x00;
  773. }
  774. Adc_SetupResultBuffer(AdcGroupSoftwareOneShot, ResultBuffer);
  775. Adc_EnableGroupNotification(AdcGroupSoftwareOneShot);
  776. VarNotification_0 = 0;
  777. Adc_StartGroupConversion(AdcGroupSoftwareOneShot);
  778. return ret;
  779. }
  780. /*EEP*/
  781. static Std_ReturnType TestEep_FlexNvmProgramPartCmd(
  782. VAR(TestEep_CsecKeySize, AUTOMATIC) eepKeysize,
  783. VAR(TestEep_SfeType, AUTOMATIC) eepSecurityFlagExtension,
  784. VAR(TestEep_LoadFlexRamType, AUTOMATIC) eepLoadFlexRamAtReset,
  785. VAR(TestEep_Eeprom_FlexRamPartitionType, AUTOMATIC) eepFlexRamPartition,
  786. VAR(TestEep_Eeprom_FlexNvmPartitionType, AUTOMATIC) eepFlexNvmPartition)
  787. {
  788. Std_ReturnType u8RetVal = (Std_ReturnType)E_OK;
  789. uint32 u32FlexNvmPartSize = 0;
  790. uint32 u32RegSimFcfg1 = 0UL;
  791. u32RegSimFcfg1 = IP_SIM->FCFG1;
  792. /*get DEPART value */
  793. u32FlexNvmPartSize = (uint32)((u32RegSimFcfg1 & SIM_FCFG1_DEPART_MASK) >> SIM_FCFG1_DEPART_SHIFT);
  794. /* check that it was not partitioned before */
  795. if (u32FlexNvmPartSize == 0xF)
  796. {
  797. // /* if error flags are set the cmd is not executed */
  798. // REG_WRITE8(TEST_EEP_EEPROM_FSTAT_ADDR32, TEST_EEP_EEPROM_FSTAT_ACCERR_U8 | TEST_EEP_EEPROM_FSTAT_FPVIOL_U8);
  799. //
  800. // /*erase DF 0 sector*/
  801. // u32Addr=(TEST_EEP_DEEPROM_SECTOR_0_ADDR32 - D_EEPROM_BASE_ADDR) + 0x800000UL;
  802. //
  803. // REG_WRITE8(TEST_EEP_EEPROM_FCCOB0_ADDR32, TEST_EEP_EEPROM_CMD_ERASE_SECTOR);
  804. // REG_WRITE8(TEST_EEP_EEPROM_FCCOB1_ADDR32, (uint8)(u32Addr >> 16UL));
  805. // REG_WRITE8(TEST_EEP_EEPROM_FCCOB2_ADDR32, (uint8)(u32Addr >> 8UL));
  806. // REG_WRITE8(TEST_EEP_EEPROM_FCCOB3_ADDR32, (uint8)(u32Addr >> 0UL));
  807. // REG_WRITE8(TEST_EEP_EEPROM_FSTAT_ADDR32 , TEST_EEP_EEPROM_FSTAT_CCIF_U8);
  808. // while((0U == REG_BIT_GET8(TEST_EEP_EEPROM_FSTAT_ADDR32, TEST_EEP_EEPROM_FSTAT_CCIF_U8)))
  809. // {
  810. // }
  811. //
  812. if (0U == REG_BIT_GET8(TEST_EEP_EEPROM_FSTAT_ADDR32, TEST_EEP_EEPROM_FSTAT_ACCERR_U8 | TEST_EEP_EEPROM_FSTAT_FPVIOL_U8))
  813. {
  814. /* run program partition command */
  815. REG_WRITE8(TEST_EEP_EEPROM_FCCOB0_ADDR32, EEPROM_CMD_PROGRAM_PARTITION);
  816. REG_WRITE8(TEST_EEP_EEPROM_FCCOB1_ADDR32, (uint8)eepKeysize);
  817. REG_WRITE8(TEST_EEP_EEPROM_FCCOB2_ADDR32, (uint8)eepSecurityFlagExtension);
  818. REG_WRITE8(TEST_EEP_EEPROM_FCCOB3_ADDR32, (uint8)eepLoadFlexRamAtReset);
  819. REG_WRITE8(TEST_EEP_EEPROM_FCCOB4_ADDR32, (uint8)eepFlexRamPartition);
  820. REG_WRITE8(TEST_EEP_EEPROM_FCCOB5_ADDR32, (uint8)eepFlexNvmPartition);
  821. REG_WRITE8(TEST_EEP_EEPROM_FSTAT_ADDR32, TEST_EEP_EEPROM_FSTAT_CCIF_U8);
  822. while ((0U == REG_BIT_GET8(TEST_EEP_EEPROM_FSTAT_ADDR32, TEST_EEP_EEPROM_FSTAT_CCIF_U8)))
  823. {
  824. /* wait for operation to finish */
  825. }
  826. /* check if errors occured */
  827. if (REG_BIT_GET8(TEST_EEP_EEPROM_FSTAT_ADDR32, TEST_EEP_EEPROM_FSTAT_ACCERR_U8 | TEST_EEP_EEPROM_FSTAT_FPVIOL_U8))
  828. {
  829. /* NOK, error flags are set */
  830. u8RetVal = (Std_ReturnType)E_NOT_OK;
  831. }
  832. }
  833. else
  834. {
  835. /* NOK, error flags are set */
  836. u8RetVal = (Std_ReturnType)E_NOT_OK;
  837. }
  838. }
  839. else
  840. {
  841. /* NOK, partitioned already */
  842. u8RetVal = (Std_ReturnType)E_NOT_OK;
  843. }
  844. return u8RetVal;
  845. }
  846. void Eep_DepartParitition(TestEep_Eeprom_FlexNvmPartitionType T_EEP_SIZE)
  847. {
  848. uint32 u32FlexNvmPartSize = 0;
  849. uint32 u32RegSimFcfg1 = 0UL;
  850. u32RegSimFcfg1 = IP_SIM->FCFG1;
  851. /*get DEPART value */
  852. u32FlexNvmPartSize = (uint32)((u32RegSimFcfg1 & SIM_FCFG1_DEPART_MASK) >> SIM_FCFG1_DEPART_SHIFT);
  853. if (u32FlexNvmPartSize == 0xF) /* We just partition again if curent size different with expected */
  854. {
  855. /* partition for EERAM 64K with NOT loading EERAM at reset in hardware */
  856. TestEep_FlexNvmProgramPartCmd(EEP_FTFC_KEY_SIZE_0_BYTES, EEP_FTFC_VERIFY_ONLY_DISABLED,
  857. EEP_FTFC_LOAD_AT_RESET_ENABLED, EEP_FTFC_EERAM_SIZE_4K, T_EEP_SIZE);
  858. }
  859. }
  860. /* Erase memory by writing erase value */
  861. Std_ReturnType HAL_EEP_Erase(uint32 eepEraseStartAddr, uint32 eepEraseSize)
  862. {
  863. Std_ReturnType retReturnType = E_OK;
  864. MemIf_JobResultType retJobResultType;
  865. retReturnType = Eep_Erase(eepEraseStartAddr, eepEraseSize);
  866. if (E_OK != retReturnType)
  867. {
  868. return E_NOT_OK;
  869. }
  870. while (MEMIF_IDLE != Eep_GetStatus())
  871. {
  872. Eep_MainFunction();
  873. }
  874. retJobResultType = Eep_GetJobResult();
  875. if (MEMIF_JOB_OK != retJobResultType)
  876. {
  877. return E_NOT_OK;
  878. }
  879. return E_OK;
  880. }
  881. /* Write one or more complete eeprom pages to the eeprom device */
  882. Std_ReturnType HAL_EEP_Write(uint32 eepWriteStartAddr, uint8 *pDataNeedtoWrite, uint32 dataSize)
  883. {
  884. Std_ReturnType retReturnType = E_OK;
  885. MemIf_JobResultType retJobResultType;
  886. /*Erase the EEP before write*/
  887. retReturnType = HAL_EEP_Erase(eepWriteStartAddr, dataSize);
  888. if (E_OK != retReturnType)
  889. {
  890. return E_NOT_OK;
  891. }
  892. retReturnType = Eep_Write(eepWriteStartAddr, pDataNeedtoWrite, dataSize);
  893. if (E_OK != retReturnType)
  894. {
  895. return E_NOT_OK;
  896. }
  897. while (MEMIF_IDLE != Eep_GetStatus())
  898. {
  899. Eep_MainFunction();
  900. }
  901. retJobResultType = Eep_GetJobResult();
  902. if (MEMIF_JOB_OK != retJobResultType)
  903. {
  904. return E_NOT_OK;
  905. }
  906. return E_OK;
  907. }
  908. /* Reads from eeprom memory */
  909. Std_ReturnType HAL_EEP_Read(uint32 eepReadStartAddr, uint8 *pDataBuffer, uint32 dataSize)
  910. {
  911. Std_ReturnType retReturnType = E_OK;
  912. MemIf_JobResultType retJobResultType;
  913. retReturnType = Eep_Read(eepReadStartAddr, pDataBuffer, dataSize);
  914. if (E_OK != retReturnType)
  915. {
  916. return E_NOT_OK;
  917. }
  918. while (MEMIF_IDLE != Eep_GetStatus())
  919. {
  920. Eep_MainFunction();
  921. }
  922. retJobResultType = Eep_GetJobResult();
  923. if (MEMIF_JOB_OK != retJobResultType)
  924. {
  925. return E_NOT_OK;
  926. }
  927. return E_OK;
  928. }
  929. /* Compares a eeprom memory area with an application data buffer */
  930. Std_ReturnType HAL_EEP_Compare(uint32 eepCompareStartAddr, uint8 *pDataNeedtoCompare, uint32 dataSize)
  931. {
  932. Std_ReturnType retReturnType = E_OK;
  933. MemIf_JobResultType retJobResultType;
  934. retReturnType = Eep_Compare(eepCompareStartAddr, pDataNeedtoCompare, dataSize);
  935. if (E_OK != retReturnType)
  936. {
  937. return E_NOT_OK;
  938. }
  939. while (MEMIF_IDLE != Eep_GetStatus())
  940. {
  941. Eep_MainFunction();
  942. }
  943. retJobResultType = Eep_GetJobResult();
  944. if (MEMIF_JOB_OK != retJobResultType)
  945. {
  946. return E_NOT_OK;
  947. }
  948. return E_OK;
  949. }
  950. /* @brief VECTKEY value so that AIRCR register write is not ignored. */
  951. #define FEATURE_SCB_VECTKEY (0x05FAU)
  952. void SystemSoftwareReset(void)
  953. {
  954. uint32_t regValue;
  955. /* Read Application Interrupt and Reset Control Register */
  956. regValue = S32_SCB->AIRCR;
  957. /* Clear register key */
  958. regValue &= ~( S32_SCB_AIRCR_VECTKEY_MASK);
  959. /* Configure System reset request bit and Register Key */
  960. regValue |= S32_SCB_AIRCR_VECTKEY(FEATURE_SCB_VECTKEY);
  961. regValue |= S32_SCB_AIRCR_SYSRESETREQ(0x1u);
  962. /* Write computed register value */
  963. S32_SCB->AIRCR = regValue;
  964. }
  965. void MCUSleep(void)
  966. {
  967. #if (ICU_PRECOMPILE_SUPPORT == STD_ON)
  968. Icu_Init(NULL_PTR);
  969. #elif (ICU_PRECOMPILE_SUPPORT == STD_OFF)
  970. Icu_Init(&Icu_Config_VS_0);
  971. #endif
  972. #define TIME_S_GPT_WEAKUP(X) ((unsigned int)(1.9073 * (X)))
  973. /* 最大定时时间 = 65535/1.907 34365 S
  974. * 9.5H
  975. */
  976. Gpt_StartTimer(GptConf_GptChannelConfiguration_GptChannelConfiguration_LPTMR, TIME_S_GPT_WEAKUP(60));
  977. Gpt_EnableWakeup(GptConf_GptChannelConfiguration_GptChannelConfiguration_LPTMR);
  978. Gpt_EnableNotification(GptConf_GptChannelConfiguration_GptChannelConfiguration_LPTMR);
  979. IP_SCG->SIRCCSR |= SCG_SIRCCSR_SIRCSTEN(1U);
  980. Gpt_SetMode(GPT_MODE_SLEEP);
  981. Mcu_SetMode(McuModeSettingConf_VLPS);
  982. Mcu_SetMode(McuModeSettingConf_Run);
  983. Gpt_GetTimeElapsed(GptConf_GptChannelConfiguration_GptChannelConfiguration_LPTMR);
  984. //Gpt_GetTimeElapsed(GptConf_GptChannelConfiguration_GptChannelConfiguration_LPTMR);
  985. Gpt_GetTimeRemaining(GptConf_GptChannelConfiguration_GptChannelConfiguration_LPTMR);
  986. // typedef void (*AppAddr)(void);
  987. // AppAddr resetHandle = (AppAddr)(0x14601);
  988. // OsIf_SuspendAllInterrupts();
  989. // (resetHandle)();
  990. SystemSoftwareReset();
  991. // coreInit();
  992. }
  993. void LPTMR_WakeUp_Notification(void)
  994. {
  995. Mcu_InitClock(McuClockSettingConfig_0);
  996. Mcu_SetMode(McuModeSettingConf_Run);
  997. Gpt_StopTimer(GptConf_GptChannelConfiguration_GptChannelConfiguration_LPTMR);
  998. Gpt_DisableWakeup(GptConf_GptChannelConfiguration_GptChannelConfiguration_LPTMR);
  999. Gpt_DisableNotification(GptConf_GptChannelConfiguration_GptChannelConfiguration_LPTMR);
  1000. }
  1001. void SystemDeinit(void)
  1002. {
  1003. Dio_WriteChannel(DioConf_DioChannel_PTA7_GPIO_OUT_MCU_4G_PWRKEY, STD_OFF);
  1004. vTaskDelay(pdMS_TO_TICKS(3000));
  1005. Dio_WriteChannel(DioConf_DioChannel_PTA6_GPIO_OUT_MCU_4G_POW_EN, STD_OFF);
  1006. Dio_WriteChannel(DioConf_DioChannel_PTD1_GPIO_OUT_MCU_GPS_POW_EN, STD_OFF);//GPS ShutDown
  1007. Dio_WriteChannel(DioConf_DioChannel_PTE7_GPIO_OUT_MCU_LED3, STD_ON);
  1008. Dio_WriteChannel(DioConf_DioChannel_PTE1_GPIO_OUT_MCU_LED2, STD_ON);
  1009. Dio_WriteChannel(DioConf_DioChannel_PTE0_GPIO_OUT_MCU_LED1, STD_ON);
  1010. Uart_Deinit();
  1011. Can_SetControllerMode(CanController_0, CAN_CS_STOPPED);
  1012. Can_SetControllerMode(CanController_1, CAN_CS_STOPPED);
  1013. // Can_SetControllerMode(CanController_2, CAN_CS_STOPPED);
  1014. Dio_WriteChannel(DioConf_DioChannel_PTC17_GPIO_OUT_MCU_CAN0_STB, STD_ON);
  1015. Dio_WriteChannel(DioConf_DioChannel_PTC16_GPIO_OUT_MCU_CAN1_STB, STD_ON);
  1016. Can_DeInit();
  1017. Adc_DeInit();
  1018. // Gpt_DisableNotification(GptConf_GptChannelConfiguration_GptChannelConfiguration_0);
  1019. // Gpt_DeInit();
  1020. Spi_DeInit();
  1021. Mcl_DeInit();
  1022. //port DeInit
  1023. for(int pinIndex = 0; pinIndex <PortConfigSet_PortContainer_GPIO_PTB4_GPIO_OUT_MCU_RS485_EN; pinIndex++)
  1024. {
  1025. if(pinIndex == PortConfigSet_PortContainer_CAN_PTA12_CAN1_RX_MCU_CAN1_RX
  1026. || pinIndex == PortConfigSet_PortContainer_INT_PTE11_GPIO_IN_MCU_3D_INT1
  1027. || pinIndex == PortConfigSet_PortContainer_INT_PTD5_GPIO_IN_MCU_3D_INT2
  1028. || pinIndex == PortConfigSet_PortContainer_INT_PTB0_GPIO_IN_MCU_WAKEUP1
  1029. || pinIndex == PortConfigSet_PortContainer_INT_PTE2_GPIO_IN_MCU_WAKEUP2
  1030. || pinIndex == DioConf_DioChannel_PTA1_GPIO_OUT_MCU_BAT_PWR_EN)
  1031. {
  1032. continue;
  1033. }
  1034. else
  1035. {
  1036. Port_SetAsUnusedPin(pinIndex);
  1037. }
  1038. }
  1039. Port_SetPinMode(PortConfigSet_PortContainer_CAN_PTA12_CAN1_RX_MCU_CAN1_RX,PORT_GPIO_MODE);
  1040. // systemInitFlag = false;
  1041. }
  1042. void MCUEnterSleep(void)
  1043. {
  1044. if(pdTRUE == xSemaphoreTake(sleep_mutex,1) && Fota_Process_Going == false)
  1045. {
  1046. // if the IO wakeup exists(high side), the mcu should not go to vlps, either.
  1047. Dio_LevelType wakeup1,wakeup2 = STD_HIGH;
  1048. wakeup1 = Dio_ReadChannel(DioConf_DioChannel_PTB0_GPIO_IN_MCU_WAKEUP1);
  1049. wakeup2 = Dio_ReadChannel(DioConf_DioChannel_PTE2_GPIO_IN_MCU_WAKEUP2);
  1050. if(wakeup1==STD_LOW && wakeup2 == STD_LOW)
  1051. {
  1052. extern boolean Uart_4G_Task_Sleep_FLag;
  1053. WdgDeInit();
  1054. Std_ReturnType Ret = E_NOT_OK;
  1055. uint8 appConfigWriteTimes = 0;
  1056. do
  1057. {
  1058. waitForSleepFlag = true;
  1059. //save the app configure before power off
  1060. if(Ret == E_NOT_OK)
  1061. {
  1062. AppConfigInfo.appSaveFlg = false;
  1063. Ret = HAL_EEP_Write(0,(uint8 *)&AppConfigInfo,sizeof(AppConfigInfo));
  1064. appConfigWriteTimes++;
  1065. }
  1066. vTaskDelay(pdMS_TO_TICKS(10));
  1067. }while(Uart_4G_Task_Sleep_FLag == false || (Ret == E_NOT_OK && appConfigWriteTimes<5) );
  1068. vTaskDelete(Uart_Hal_RecvTask_Handle);
  1069. vTaskDelete(Uart_Hal_SendTask_Handle);
  1070. vTaskDelete(CanTask_Handle);
  1071. vTaskDelete(GpsTask_Handle);
  1072. vTaskDelete(Uart_4G_Task_Handle);
  1073. // vTaskDelete(MainTask_Handle);
  1074. SystemDeinit();
  1075. MCUSleep();
  1076. }
  1077. }
  1078. }
  1079. void coreInit(void)
  1080. {
  1081. /* Initialize the Mcu driver */
  1082. #if (MCU_PRECOMPILE_SUPPORT == STD_ON)
  1083. Mcu_Init(NULL_PTR);
  1084. #elif (MCU_PRECOMPILE_SUPPORT == STD_OFF)
  1085. Mcu_Init(&Mcu_Config_VS_0);
  1086. #endif /* (MCU_PRECOMPILE_SUPPORT == STD_ON) */
  1087. Mcu_InitClock(McuClockSettingConfig_0);
  1088. /* Wait until PLL is locked */
  1089. while (MCU_PLL_LOCKED != Mcu_GetPllStatus())
  1090. {
  1091. /* Busy wait until the System PLL is locked */
  1092. }
  1093. Mcu_DistributePllClock();
  1094. OsIf_Init(NULL_PTR);
  1095. Platform_Init(NULL_PTR);
  1096. /* Initialize all pins*/
  1097. #if (PORT_PRECOMPILE_SUPPORT == STD_ON)
  1098. Port_Init(NULL_PTR);
  1099. #elif (PORT_PRECOMPILE_SUPPORT == STD_OFF)
  1100. Port_Init(&Port_Config_VS_0);
  1101. #endif
  1102. }
  1103. void SystemModulesInit(void)
  1104. {
  1105. // Dio_WriteChannel(DioConf_DioChannel_PTE0_GPIO_OUT_MCU_LED1, STD_OFF);
  1106. /* Initialize Mcl module */
  1107. Mcl_Init(NULL_PTR);
  1108. // SEGGER_RTT_Init();
  1109. /* Initializes an UART driver*/
  1110. #if (UART_PRECOMPILE_SUPPORT == STD_ON)
  1111. Uart_Init(NULL_PTR);
  1112. #elif (UART_PRECOMPILE_SUPPORT == STD_OFF)
  1113. Uart_Init(&Uart_xConfig_VS_0);
  1114. #endif
  1115. IP_LPUART0->CTRL |= LPUART_CTRL_ILT(1);
  1116. IP_LPUART1->CTRL |= LPUART_CTRL_ILT(1);
  1117. IP_LPUART2->CTRL |= LPUART_CTRL_ILT(1);
  1118. IP_LPUART0->CTRL |= LPUART_CTRL_IDLECFG(3);
  1119. IP_LPUART1->CTRL |= LPUART_CTRL_IDLECFG(3);
  1120. IP_LPUART2->CTRL |= LPUART_CTRL_IDLECFG(3);
  1121. #if 1 /* Initialize Platform driver */
  1122. #if (CAN_PRECOMPILE_SUPPORT == STD_ON)
  1123. Can_Init(NULL_PTR);
  1124. #elif (CAN_PRECOMPILE_SUPPORT == STD_OFF)
  1125. Can_Init(&Can_Config_VS_0);
  1126. #endif
  1127. Can_SetControllerMode(CanController_0, CAN_CS_STARTED);
  1128. Can_SetControllerMode(CanController_1, CAN_CS_STARTED);
  1129. Dio_WriteChannel(DioConf_DioChannel_PTC17_GPIO_OUT_MCU_CAN0_STB, STD_OFF);
  1130. Dio_WriteChannel(DioConf_DioChannel_PTC16_GPIO_OUT_MCU_CAN1_STB, STD_OFF);
  1131. // Can_SetControllerMode(CanController_2, CAN_CS_STARTED);
  1132. #endif
  1133. #if (ADC_PRECOMPILE_SUPPORT == STD_ON)
  1134. Adc_Init(NULL_PTR);
  1135. #else
  1136. Adc_Init(&Adc_Config_VS_0);
  1137. #endif /* ADC_PRECOMPILE_SUPPORT == STD_ON */
  1138. //the calibration is needed to increase the ADC accuracy
  1139. Adc_CalibrationStatusType CalibStatus;
  1140. Adc_Calibrate(AdcHwUnit_1, &CalibStatus);
  1141. if (CalibStatus.Adc_UnitSelfTestStatus == E_NOT_OK)
  1142. {
  1143. // if so, the adc is failed to Calibrate;
  1144. }
  1145. /* Initialize Eep driver */
  1146. #if defined (EEP_PRECOMPILE_SUPPORT)
  1147. Eep_Init(NULL_PTR);
  1148. #else
  1149. Eep_Init(&Eep_Config_VS_0);
  1150. #endif
  1151. /* Partition only if it was not partitioned before for EERAM with code 0x4 */
  1152. // Eep_DepartParitition(T_EEEPROM_SIZE);
  1153. //Init Flash Driver
  1154. #if defined (FLS_PRECOMPILE_SUPPORT)
  1155. Fls_Init(NULL_PTR);
  1156. #else
  1157. Fls_Init(&Fls_Config_VS_0);
  1158. while(MEMIF_IDLE == Fls_GetStatus())
  1159. {
  1160. ;
  1161. }
  1162. #endif
  1163. Spi_Init(NULL_PTR);
  1164. /* Initialize the Gpt driver */
  1165. Gpt_Init(&Gpt_Config_VS_0);
  1166. /* Enable the Gpt notification to periodically service the Wdg */
  1167. Gpt_EnableNotification(GptConf_GptChannelConfiguration_GptChannelConfiguration_LPIT0_CH0);
  1168. //enable the batt power
  1169. Dio_WriteChannel(DioConf_DioChannel_PTA1_GPIO_OUT_MCU_BAT_PWR_EN,STD_ON);
  1170. Icu_DeInit();
  1171. // WdgInit();
  1172. //
  1173. // IsFeedWdg = true;
  1174. }
  1175. void displayResetReasonWithLED(void)
  1176. {
  1177. Mcu_ResetType bootreason;
  1178. bootreason = Mcu_GetResetReason();
  1179. }
  1180. void readConfigAndDataFromEEPROM(void)
  1181. {
  1182. uint32 *eppromAddStart = 0x14000000;
  1183. if (*(uint8 *)(eppromAddStart) == 0xff)
  1184. {
  1185. HAL_EEP_Write(0, (uint8 *)&AppConfigInfo, sizeof(AppConfigInfo));
  1186. }
  1187. Std_ReturnType readRet = HAL_EEP_Read(0, (uint8 *)&AppConfigInfo, sizeof(AppConfigInfo));
  1188. if(readRet == E_NOT_OK)
  1189. {
  1190. uint8 cycleTimes = 0;
  1191. while(HAL_EEP_Read(200, (uint8 *)&AppConfigInfo, sizeof(AppConfigInfo))==E_NOT_OK && cycleTimes<=5) //backup data addr:200
  1192. {
  1193. cycleTimes++;
  1194. }
  1195. }
  1196. //read bcu data
  1197. HAL_EEP_Read(BCU_SAVE_ADDR, (uint8 *)&BcuSaveDataInfo, sizeof(BcuSaveDataInfo));
  1198. if(BcuSaveDataInfo.header.crc16 != crc_chk(&(BcuSaveDataInfo.BcuAppSaveDataInfo),sizeof(BcuSaveDataInfo.BcuAppSaveDataInfo)))
  1199. {
  1200. memcpy(&BcuSaveDataInfo.BcuAppSaveDataInfo,&BcuAppSaveDataInfoDefaultValue,sizeof(BcuSaveDataInfo.BcuAppSaveDataInfo));
  1201. // BcuSaveDataInfo.BcuAppSaveDataInfo.bcuSaveFlg = false;
  1202. BcuSaveDataInfo.header.crc16 = crc_chk(&(BcuSaveDataInfo.BcuAppSaveDataInfo),sizeof(BcuSaveDataInfo.BcuAppSaveDataInfo));
  1203. HAL_EEP_Write(BCU_SAVE_ADDR, (uint8 *)&BcuSaveDataInfo, sizeof(BcuSaveDataInfo));
  1204. rtev_flg_EESt[0] = 1;
  1205. rtev_flg_EESt[1] = 1;
  1206. rtev_flg_EESt[2] = 1;
  1207. rtev_flg_EESt[3] = 1;
  1208. }
  1209. }
  1210. void IH_Data_Update(void)
  1211. {
  1212. hvmd_st_chgCnctObj_T2 = BBox_VCUConSt;
  1213. ihd_pct_socCor = BBox_SocCorrectionValue;
  1214. BBox_SocCorrectionValue = 0; //clear the SOC correction value from cloud which only available once
  1215. if(BBox_SohCorrectionValue != 0)
  1216. {
  1217. sohd_pct_bcuSoh = BBox_SohCorrectionValue;
  1218. BBox_SohCorrectionValue = 0;
  1219. }
  1220. if(BMS_MaxCellVolt != 0 && BMS_MaxCellVolt != 0xFFFF)
  1221. {
  1222. ihd_V_cellUMax = (real32_T)(BMS_MaxCellVolt/1000.0);
  1223. }
  1224. if(BMS_MinCellVolt != 0 && BMS_MinCellVolt != 0xFFFF)
  1225. {
  1226. ihd_V_cellUMin = (real32_T)(BMS_MinCellVolt/1000.0);
  1227. }
  1228. if(BMS_AverCellVolt != 0 && BMS_AverCellVolt != 0xFFFF)
  1229. {
  1230. ihd_V_cellUAvrg = (real32_T)(BMS_AverCellVolt/1000.0);
  1231. }
  1232. if(BMS_BattVolt != 0 && BMS_BattVolt != 0xFFFF)
  1233. {
  1234. ihd_V_battU_T2 = (real32_T)(BMS_BattVolt*0.1);
  1235. }
  1236. ihd_I_battCurr_T2 = (real32_T)(BMS_PackCurr*0.1)-1000;
  1237. dcmd_st_chrgSt_T2 = BMS_CharSt;
  1238. }
  1239. void OH_Data_Update(void)
  1240. {
  1241. //socd_pct_ekfSoc; //EKFSOC
  1242. //socd_pct_battSoc; //电池SOC
  1243. //socd_pct_bcuSoc; //显示SOC
  1244. BBox_PackSOC = (uint8)(socd_pct_battSoc /0.4); //安时SOC->battSoc
  1245. BBox_PackSOH = (uint8)(sohd_pct_bcuSoh); //Soh
  1246. BBox_Dq_Dv_MIN = (uint32)(socd_Q_dMin * 1000); //dQ/dV
  1247. BBox_Dq_Dv_MAX = (uint32)(socd_Q_dMax * 1000); //dQ/dV
  1248. BBox_TotalDischarEngy = (uint32)(damd_E_totalDisChrgEng*10); //累计放电电电量
  1249. BBox_TotalDischarCapy = (uint32)(damd_Q_totalDisChrgCp*10); //累计放电电容量
  1250. BBox_TotalBackCharEngy = (uint32)(damd_E_totalRegenEng*10); //累计回收电量
  1251. BBox_TotalBackCharCapy = (uint32)(damd_Q_totalRegenCp*10); //累计回收容量
  1252. BBox_TotalCharEngy = BBox_TotalBackCharEngy + (uint32)(damd_E_totalChrgEng*10); //累计充电电量
  1253. BBox_TotalCharCapy = BBox_TotalBackCharCapy + (uint32)(damd_Q_totalChrgCp*10); //累计充电容量
  1254. BBox_TotalStaCharCapy = (uint32)(damd_Q_totalSatChrgCp*10); //累计回收容量
  1255. BBox_TotalStaCharEngy = (uint32)(damd_E_totalSatChrgEng*10); //累计回收容量
  1256. BBox_TotalGunCharCapy = (uint32)(damd_Q_totalVehChrgCp*10); //累计回收容量
  1257. BBox_TotalGunCharEngy = (uint32)(damd_E_totalVehChrgEng*10); //累计回收容量
  1258. }