Module_InternalComm.c 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817
  1. #include <sys/time.h>
  2. #include <sys/timeb.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <sys/types.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/socket.h>
  8. #include <sys/ipc.h>
  9. #include <sys/shm.h>
  10. #include <sys/shm.h>
  11. #include <sys/mman.h>
  12. #include <linux/wireless.h>
  13. #include <arpa/inet.h>
  14. #include <netinet/in.h>
  15. #include <unistd.h>
  16. #include <stdarg.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <fcntl.h>
  21. #include <termios.h>
  22. #include <errno.h>
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <time.h>
  26. #include <ctype.h>
  27. #include <ifaddrs.h>
  28. #include <math.h>
  29. #include <limits.h>
  30. #include "define.h"
  31. #include "main.h"
  32. #include "Module_InternalComm.h"
  33. //#define SIMULATION
  34. #define FAIL_SPEC_COMM 100
  35. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  36. #define PASS 1
  37. #define FAIL 0
  38. #define ON 1
  39. #define OFF 0
  40. #define PRIORITY_HIGH 0
  41. #define PRIORITY_LOW 1
  42. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  43. struct StatusCodeData *ShmStatusCodeData;
  44. struct PrimaryMcuData *ShmPrimaryMcuData;
  45. struct OCPP16Data *ShmOCPP16Data;
  46. struct Charger *ShmCharger;
  47. uint16_t stepIndex=21, logIndex;
  48. long long tsLast, tsNow, tsPrintLog[2];
  49. double tmpPowerConsumption;
  50. void trim(char *s);
  51. int mystrcmp(char *p1,char *p2);
  52. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  53. void split(char **arr, char *str, const char *del);
  54. int StoreLogMsg(const char *fmt, ...)
  55. {
  56. char Buf[4096+256];
  57. char buffer[4096];
  58. time_t CurrentTime;
  59. struct tm *tm;
  60. va_list args;
  61. va_start(args, fmt);
  62. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  63. va_end(args);
  64. memset(Buf,0,sizeof(Buf));
  65. CurrentTime = time(NULL);
  66. tm=localtime(&CurrentTime);
  67. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d] - %s\" >> /Storage/SystemLog/[%04d.%02d]Module_InterComm",
  68. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
  69. buffer,
  70. tm->tm_year+1900,tm->tm_mon+1);
  71. #ifdef SystemLogMessage
  72. system(Buf);
  73. #endif
  74. #ifdef ConsloePrintLog
  75. printf("[%04d.%02d.%02d %02d:%02d:%02d] - %s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec, buffer);
  76. #endif
  77. return rc;
  78. }
  79. int DiffTimeb(struct timeb ST, struct timeb ET)
  80. {
  81. //return milli-second
  82. unsigned int StartTime,StopTime;
  83. StartTime=(unsigned int)ST.time;
  84. StopTime=(unsigned int)ET.time;
  85. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  86. }
  87. long long current_timestamp()
  88. {
  89. struct timeval te;
  90. gettimeofday(&te, NULL); // get current time
  91. long long milliseconds = te.tv_sec*1000LL + te.tv_usec/1000; // calculate milliseconds
  92. return milliseconds;
  93. }
  94. //=================================
  95. // Common routine
  96. //=================================
  97. void trim(char *s)
  98. {
  99. int i=0, j, k, l=0;
  100. while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
  101. i++;
  102. j = strlen(s)-1;
  103. while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
  104. j--;
  105. if(i==0 && j==strlen(s)-1) { }
  106. else if(i==0) s[j+1] = '\0';
  107. else {
  108. for(k=i; k<=j; k++) s[l++] = s[k];
  109. s[l] = '\0';
  110. }
  111. }
  112. int mystrcmp(char *p1,char *p2)
  113. {
  114. while(*p1==*p2)
  115. {
  116. if(*p1=='\0' || *p2=='\0')
  117. break;
  118. p1++;
  119. p2++;
  120. }
  121. if(*p1=='\0' && *p2=='\0')
  122. return(PASS);
  123. else
  124. return(FAIL);
  125. }
  126. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  127. {
  128. strncpy(dest, src + start, cnt);
  129. dest[cnt] = 0;
  130. }
  131. void split(char **arr, char *str, const char *del)
  132. {
  133. char *s = strtok(str, del);
  134. while(s != NULL)
  135. {
  136. *arr++ = s;
  137. s = strtok(NULL, del);
  138. }
  139. }
  140. //==========================================
  141. // Init all share memory
  142. //==========================================
  143. int InitShareMemory()
  144. {
  145. int result = PASS;
  146. int MeterSMId;
  147. //Initial ShmSysConfigAndInfo
  148. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  149. {
  150. #ifdef SystemLogMessage
  151. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  152. #endif
  153. result = FAIL;
  154. }
  155. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  156. {
  157. #ifdef SystemLogMessage
  158. DEBUG_ERROR("[shmat ShmSysConfigAndInfo NG\n");
  159. #endif
  160. result = FAIL;
  161. }
  162. //Initial ShmStatusCodeData
  163. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  164. {
  165. #ifdef SystemLogMessage
  166. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  167. #endif
  168. result = FAIL;
  169. }
  170. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  171. {
  172. #ifdef SystemLogMessage
  173. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  174. #endif
  175. result = FAIL;
  176. }
  177. //Initial ShmPrimaryMcuKey
  178. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), 0777)) < 0)
  179. {
  180. #ifdef SystemLogMessage
  181. DEBUG_ERROR("shmget ShmPrimaryMcuData NG\n");
  182. #endif
  183. result = FAIL;
  184. }
  185. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  186. {
  187. #ifdef SystemLogMessage
  188. DEBUG_ERROR("shmat ShmPrimaryMcuData NG\n");
  189. #endif
  190. result = FAIL;
  191. }
  192. //creat ShmOCPP16Data
  193. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), 0777)) < 0)
  194. {
  195. #ifdef SystemLogMessage
  196. DEBUG_ERROR("shmget ShmOCPP16Data NG");
  197. #endif
  198. result = FAIL;
  199. }
  200. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  201. {
  202. #ifdef SystemLogMessage
  203. DEBUG_ERROR("shmat ShmOCPP16Data NG");
  204. #endif
  205. result = FAIL;
  206. }
  207. else
  208. {}
  209. //Initial ShmCharger
  210. if ((MeterSMId = shmget(ShmChargerKey, sizeof(struct Charger), 0777)) < 0)
  211. {
  212. #ifdef SystemLogMessage
  213. DEBUG_ERROR("shmget ShmChargerKey NG\n");
  214. #endif
  215. result = FAIL;
  216. }
  217. else if ((ShmCharger = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  218. {
  219. #ifdef SystemLogMessage
  220. DEBUG_ERROR("shmat ShmChargerKey NG\n");
  221. #endif
  222. result = FAIL;
  223. }
  224. return result;
  225. }
  226. int InitComPort()
  227. {
  228. int fd;
  229. struct termios tios;
  230. fd = open("/dev/ttyS1", O_RDWR);
  231. if(fd<=0)
  232. {
  233. #ifdef SystemLogMessage
  234. DEBUG_ERROR("open /dev/ttyS1 NG\n");
  235. #endif
  236. return -1;
  237. }
  238. ioctl (fd, TCGETS, &tios);
  239. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  240. tios.c_lflag = 0;
  241. tios.c_iflag = 0;
  242. tios.c_oflag = 0;
  243. tios.c_cc[VMIN]=0;
  244. tios.c_cc[VTIME]=(unsigned char)5; // timeout 0.5 secod
  245. tios.c_lflag=0;
  246. tcflush(fd, TCIFLUSH);
  247. ioctl (fd, TCSETS, &tios);
  248. return fd;
  249. }
  250. void ack_delay(unsigned char cmd)
  251. {
  252. switch(cmd)
  253. {
  254. case CMD_UPDATE_START:
  255. case CMD_UPDATE_END:
  256. usleep(300000);
  257. break;
  258. default:
  259. usleep(100000);
  260. break;
  261. }
  262. }
  263. int tranceive(int fd, unsigned char* cmd, unsigned char cmd_len, unsigned char* rx)
  264. {
  265. int len;
  266. tcflush(fd,TCIOFLUSH);
  267. if(write(fd, cmd, cmd_len) >= cmd_len)
  268. {
  269. ack_delay(cmd[3]);
  270. len = read(fd, rx, 512);
  271. }
  272. else
  273. {
  274. #ifdef SystemLogMessage
  275. DEBUG_ERROR("Serial command %s response fail.\n", cmd);
  276. #endif
  277. }
  278. return len;
  279. }
  280. unsigned char Query_FW_Ver(unsigned char fd, unsigned char targetAddr, Ver *Ret_Buf)
  281. {
  282. unsigned char result = FAIL;
  283. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_FW_VER, 0x00, 0x00, 0x00};
  284. unsigned char rx[512];
  285. unsigned char chksum = 0x00;
  286. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  287. if(len > 6)
  288. {
  289. if (len < 6+(rx[4] | rx[5]<<8))
  290. return result;
  291. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  292. {
  293. chksum ^= rx[6+idx];
  294. }
  295. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  296. (rx[2] == tx[1]) &&
  297. (rx[1] == tx[2]) &&
  298. (rx[3] == tx[3]))
  299. {
  300. strncpy(Ret_Buf->Version_FW, (char *)rx+6, (rx[4] | rx[5]<<8));
  301. result = PASS;
  302. }
  303. }
  304. return result;
  305. }
  306. unsigned char Query_HW_Ver(unsigned char fd, unsigned char targetAddr, Ver *Ret_Buf)
  307. {
  308. unsigned char result = FAIL;
  309. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_HW_VER, 0x00, 0x00, 0x00};
  310. unsigned char rx[512];
  311. unsigned char chksum = 0x00;
  312. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  313. if(len > 6)
  314. {
  315. if (len < 6+(rx[4] | rx[5]<<8))
  316. return result;
  317. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  318. {
  319. chksum ^= rx[6+idx];
  320. }
  321. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  322. (rx[2] == tx[1]) &&
  323. (rx[1] == tx[2]) &&
  324. (rx[3] == tx[3]))
  325. {
  326. strncpy(Ret_Buf->Version_HW, (char *)rx+6, (rx[4] | rx[5]<<8));
  327. result = PASS;
  328. }
  329. }
  330. return result;
  331. }
  332. unsigned char Query_Present_InputVoltage(unsigned char fd, unsigned char targetAddr, PresentInputVoltage *Ret_Buf)
  333. {
  334. unsigned char result = FAIL;
  335. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_PRESENT_INPUTVOLTAGE, 0x00, 0x00, 0x00};
  336. unsigned char rx[512];
  337. unsigned char chksum = 0x00;
  338. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  339. if(len > 6)
  340. {
  341. if (len < 6+(rx[4] | rx[5]<<8))
  342. return result;
  343. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  344. {
  345. chksum ^= rx[6+idx];
  346. }
  347. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  348. (rx[2] == tx[1]) &&
  349. (rx[1] == tx[2]) &&
  350. (rx[3] == tx[3]))
  351. {
  352. Ret_Buf->inputType = rx[6];
  353. Ret_Buf->L1N_L12 =( rx[7] | (rx[8]<<8))/10.0;
  354. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  355. {
  356. Ret_Buf->L2N_L23 =( rx[9] | (rx[10]<<8))/10.0;
  357. Ret_Buf->L3N_L31 =( rx[11] | (rx[12]<<8))/10.0;
  358. }
  359. result = PASS;
  360. }
  361. }
  362. return result;
  363. }
  364. unsigned char Query_Present_OutputVoltage(unsigned char fd, unsigned char targetAddr, PresentOutputVoltage *Ret_Buf)
  365. {
  366. unsigned char result = FAIL;
  367. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_PRESENT_OUTPUTVOLTAGE, 0x00, 0x00, 0x00};
  368. unsigned char rx[512];
  369. unsigned char chksum = 0x00;
  370. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  371. if(len > 6)
  372. {
  373. if (len < 6+(rx[4] | rx[5]<<8))
  374. return result;
  375. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  376. {
  377. chksum ^= rx[6+idx];
  378. }
  379. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  380. (rx[2] == tx[1]) &&
  381. (rx[1] == tx[2]) &&
  382. (rx[3] == tx[3]))
  383. {
  384. Ret_Buf->behindFuse_Voltage_C1 =(rx[6] | (rx[7]<<8))/10.0;
  385. Ret_Buf->behindRelay_Voltage_C1 =(rx[8] | (rx[9]<<8))/10.0;
  386. if((rx[4] | rx[5]<<8) > 4)
  387. {
  388. Ret_Buf->behindFuse_Voltage_C2 =(rx[10] | (rx[11]<<8))/10.0;
  389. Ret_Buf->behindRelay_Voltage_C2 =(rx[12] | (rx[13]<<8))/10.0;
  390. }
  391. result = PASS;
  392. }
  393. }
  394. return result;
  395. }
  396. unsigned char Query_Fan_Speed(unsigned char fd, unsigned char targetAddr, FanSpeed *Ret_Buf)
  397. {
  398. unsigned char result = FAIL;
  399. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_FAN_SPEED, 0x00, 0x00, 0x00};
  400. unsigned char rx[512];
  401. unsigned char chksum = 0x00;
  402. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  403. if(len > 6)
  404. {
  405. if (len < 6+(rx[4] | rx[5]<<8))
  406. return result;
  407. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  408. {
  409. chksum ^= rx[6+idx];
  410. }
  411. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  412. (rx[2] == tx[1]) &&
  413. (rx[1] == tx[2]) &&
  414. (rx[3] == tx[3]))
  415. {
  416. for(int idx=0;idx<((rx[4] | rx[5]<<8)>>1);idx++)
  417. Ret_Buf->speed[idx] =(rx[6+(2*idx)] | (rx[6+(2*idx)+1]<<8));
  418. result = PASS;
  419. }
  420. }
  421. return result;
  422. }
  423. unsigned char Query_Temperature(unsigned char fd, unsigned char targetAddr, Temperature *Ret_Buf)
  424. {
  425. unsigned char result = FAIL;
  426. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_TEMPERATURE, 0x00, 0x00, 0x00};
  427. unsigned char rx[512];
  428. unsigned char chksum = 0x00;
  429. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  430. if(len > 6)
  431. {
  432. if (len < 6+(rx[4] | rx[5]<<8))
  433. return result;
  434. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  435. {
  436. chksum ^= rx[6+idx];
  437. }
  438. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  439. (rx[2] == tx[1]) &&
  440. (rx[1] == tx[2]) &&
  441. (rx[3] == tx[3]))
  442. {
  443. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  444. Ret_Buf->point[idx] = rx[6+idx] - 60;
  445. result = PASS;
  446. }
  447. }
  448. return result;
  449. }
  450. unsigned char Query_Aux_PowerVoltage(unsigned char fd, unsigned char targetAddr, AuxPower *Ret_Buf)
  451. {
  452. unsigned char result = FAIL;
  453. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_AUX_POWERVOLTAGE, 0x00, 0x00, 0x00};
  454. unsigned char rx[512];
  455. unsigned char chksum = 0x00;
  456. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  457. if(len > 6)
  458. {
  459. if (len < 6+(rx[4] | rx[5]<<8))
  460. return result;
  461. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  462. {
  463. chksum ^= rx[6+idx];
  464. }
  465. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  466. (rx[2] == tx[1]) &&
  467. (rx[1] == tx[2]) &&
  468. (rx[3] == tx[3]))
  469. {
  470. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  471. Ret_Buf->voltage[idx] = rx[6+idx];
  472. result = PASS;
  473. }
  474. }
  475. return result;
  476. }
  477. unsigned char Query_Relay_Output(unsigned char fd, unsigned char targetAddr, Relay *Ret_Buf)
  478. {
  479. unsigned char result = FAIL;
  480. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_RELAY_OUTPUT, 0x00, 0x00, 0x00};
  481. unsigned char rx[512];
  482. unsigned char chksum = 0x00;
  483. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  484. if(len > 6)
  485. {
  486. if (len < 6+(rx[4] | rx[5]<<8))
  487. return result;
  488. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  489. {
  490. chksum ^= rx[6+idx];
  491. }
  492. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  493. (rx[2] == tx[1]) &&
  494. (rx[1] == tx[2]) &&
  495. (rx[3] == tx[3]))
  496. {
  497. for(int idx_connector=0;idx_connector<(rx[4] | rx[5]<<8);idx_connector++)
  498. {
  499. for(int idx=0;idx<8;idx++)
  500. Ret_Buf->relay_status[idx_connector][idx] = (rx[6+idx_connector]>>idx) & 0x01;
  501. }
  502. result = PASS;
  503. }
  504. }
  505. return result;
  506. }
  507. unsigned char Query_Gfd_Adc(unsigned char fd, unsigned char targetAddr, Gfd *Ret_Buf)
  508. {
  509. unsigned char result = FAIL;
  510. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_GFD_ADC, 0x00, 0x00, 0x00};
  511. unsigned char rx[512];
  512. unsigned char chksum = 0x00;
  513. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  514. if(len > 6)
  515. {
  516. if (len < 6+(rx[4] | rx[5]<<8))
  517. return result;
  518. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  519. {
  520. chksum ^= rx[6+idx];
  521. }
  522. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  523. (rx[2] == tx[1]) &&
  524. (rx[1] == tx[2]) &&
  525. (rx[3] == tx[3]))
  526. {
  527. for(int idx_connector=0;idx_connector<((rx[4] | rx[5]<<8)>>2);idx_connector++)
  528. {
  529. Ret_Buf->adc_value_positive[idx_connector] = (rx[6+(4*idx_connector)] | rx[6+(4*idx_connector)+1]<<8);
  530. Ret_Buf->adc_value_negative[idx_connector] = (rx[6+(4*idx_connector)+2] | rx[6+(4*idx_connector)+3]<<8);;
  531. }
  532. result = PASS;
  533. }
  534. }
  535. return result;
  536. }
  537. unsigned char Query_Gpio_Input(unsigned char fd, unsigned char targetAddr, Gpio_in *Ret_Buf)
  538. {
  539. unsigned char result = FAIL;
  540. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_GPIO_INPUT, 0x00, 0x00, 0x00};
  541. unsigned char rx[512];
  542. unsigned char chksum = 0x00;
  543. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  544. if(len > 6)
  545. {
  546. if (len < 6+(rx[4] | rx[5]<<8))
  547. return result;
  548. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  549. {
  550. chksum ^= rx[6+idx];
  551. }
  552. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  553. (rx[2] == tx[1]) &&
  554. (rx[1] == tx[2]) &&
  555. (rx[3] == tx[3]))
  556. {
  557. Ret_Buf->AC_Connector = (rx[6] >> 0) & 0x01;
  558. Ret_Buf->AC_MainBreaker = (rx[6] >> 1) & 0x01;
  559. Ret_Buf->SPD = (rx[6] >> 2) & 0x01;
  560. Ret_Buf->Door_Open = (rx[6] >> 3) & 0x01;
  561. Ret_Buf->GFD[0] = (rx[6] >> 4) & 0x01;
  562. Ret_Buf->GFD[1] = (rx[6] >> 5) & 0x01;
  563. Ret_Buf->Button[0] = (rx[6] >> 6) & 0x01;
  564. Ret_Buf->Button[1] = (rx[6] >> 7) & 0x01;
  565. Ret_Buf->Button_Emergency = (rx[7] >> 0) & 0x01;
  566. result = PASS;
  567. }
  568. }
  569. return result;
  570. }
  571. unsigned char Query_Alarm_Log(unsigned char fd, unsigned char targetAddr, Alarm_Log *Ret_Buf)
  572. {
  573. unsigned char result = FAIL;
  574. unsigned char tx[10] = {0xaa, 0x00, targetAddr, CMD_QUERY_ALARM_LOG, 0x03, 0x00, Ret_Buf->logArea, Ret_Buf->alarmIndex&0xff, ((Ret_Buf->alarmIndex>>8)&0xff), 0x00};
  575. unsigned char rx[512];
  576. unsigned char chksum = Ret_Buf->logArea ^ (Ret_Buf->alarmIndex&0xff) ^ ((Ret_Buf->alarmIndex>>8)&0xff);
  577. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  578. if(len > 6)
  579. {
  580. if (len < 6+(rx[4] | rx[5]<<8))
  581. return result;
  582. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  583. {
  584. chksum ^= rx[6+idx];
  585. }
  586. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  587. (rx[2] == tx[1]) &&
  588. (rx[1] == tx[2]) &&
  589. (rx[3] == tx[3]))
  590. {
  591. memcpy(&Ret_Buf->log[0], &rx[8], 8);
  592. result = PASS;
  593. }
  594. }
  595. return result;
  596. }
  597. unsigned char Query_RTC(unsigned char fd, unsigned char targetAddr, Rtc *Ret_Buf)
  598. {
  599. unsigned char result = FAIL;
  600. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_RTC, 0x00, 0x00, 0x00};
  601. unsigned char rx[512];
  602. unsigned char chksum = 0x00;
  603. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  604. if(len > 6)
  605. {
  606. if (len < 6+(rx[4] | rx[5]<<8))
  607. return result;
  608. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  609. {
  610. chksum ^= rx[6+idx];
  611. }
  612. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  613. (rx[2] == tx[1]) &&
  614. (rx[1] == tx[2]) &&
  615. (rx[3] == tx[3]))
  616. {
  617. Ret_Buf->year = ((rx[6]-'0')*1000) + ((rx[7]-'0')*100) + ((rx[8]-'0')*10) + ((rx[9]-'0')*1);
  618. Ret_Buf->month = ((rx[10]-'0')*10) + ((rx[11]-'0')*1);
  619. Ret_Buf->day = ((rx[12]-'0')*10) + ((rx[13]-'0')*1);
  620. Ret_Buf->hour = ((rx[14]-'0')*10) + ((rx[15]-'0')*1);
  621. Ret_Buf->min = ((rx[16]-'0')*10) + ((rx[17]-'0')*1);
  622. Ret_Buf->sec = ((rx[18]-'0')*10) + ((rx[19]-'0')*1);
  623. result = PASS;
  624. }
  625. }
  626. return result;
  627. }
  628. unsigned char Query_AC_MCU_Status(unsigned char fd, unsigned char targetAddr, Ac_Primary_Mcu *Ret_Buf)
  629. {
  630. unsigned char result = FAIL;
  631. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_AC_STATUS, 0x00, 0x00, 0x00};
  632. unsigned char rx[512];
  633. unsigned char chksum = 0x00;
  634. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  635. if(len > 6)
  636. {
  637. if (len < 6+(rx[4] | rx[5]<<8))
  638. return result;
  639. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  640. {
  641. chksum ^= rx[6+idx];
  642. }
  643. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  644. (rx[2] == tx[1]) &&
  645. (rx[1] == tx[2]) &&
  646. (rx[3] == tx[3]))
  647. {
  648. Ret_Buf->cp_state = rx[6];
  649. Ret_Buf->current_limit = rx[7] | (rx[8]<<0x08);
  650. Ret_Buf->cp_voltage_positive = (rx[9] | (rx[10]<<0x08))/100.0;
  651. Ret_Buf->cp_voltage_negtive = (rx[11] | (rx[12]<<0x08))/100.0;
  652. Ret_Buf->locker_state = rx[13];
  653. Ret_Buf->relay_state = rx[14];
  654. Ret_Buf->shutter_state = rx[15];
  655. Ret_Buf->meter_state = rx[16];
  656. Ret_Buf->pp_state = rx[17];
  657. Ret_Buf->rating_current = rx[18];
  658. Ret_Buf->rotatory_switch = (rx[19] & 0x0F);
  659. result = PASS;
  660. }
  661. }
  662. return result;
  663. }
  664. unsigned char Query_AC_MCU_Alarm(unsigned char fd, unsigned char targetAddr, Ac_Primary_Mcu_Alarm *Ret_Buf)
  665. {
  666. unsigned char result = FAIL;
  667. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_AC_ALARM, 0x00, 0x00, 0x00};
  668. unsigned char rx[512];
  669. unsigned char chksum = 0x00;
  670. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  671. if(len > 6)
  672. {
  673. if (len < 6+(rx[4] | rx[5]<<8))
  674. return result;
  675. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  676. {
  677. chksum ^= rx[6+idx];
  678. }
  679. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  680. (rx[2] == tx[1]) &&
  681. (rx[1] == tx[2]) &&
  682. (rx[3] == tx[3]))
  683. {
  684. //rx[6]
  685. Ret_Buf->bits.OVP_L1 = (((rx[6]>>0)&0x01)?1:0);
  686. Ret_Buf->bits.UVP_L1 = (((rx[6]>>1)&0x01)?1:0);
  687. Ret_Buf->bits.OCP_L1 = (((rx[6]>>2)&0x01)?1:0);
  688. Ret_Buf->bits.OTP = (((rx[6]>>3)&0x01)?1:0);
  689. Ret_Buf->bits.gmi_fault = (((rx[6]>>4)&0x01)?1:0);
  690. Ret_Buf->bits.cp_fault = (((rx[6]>>5)&0x01)?1:0);
  691. Ret_Buf->bits.ac_leak = (((rx[6]>>6)&0x01)?1:0);
  692. Ret_Buf->bits.dc_leak = (((rx[6]>>7)&0x01)?1:0);
  693. //rx[7]
  694. Ret_Buf->bits.mcu_selftest_fail = (((rx[7]>>0)&0x01)?1:0);
  695. Ret_Buf->bits.handshaking_timeout = (((rx[7]>>1)&0x01)?1:0);
  696. Ret_Buf->bits.emergency_stop = (((rx[7]>>2)&0x01)?1:0);
  697. Ret_Buf->bits.relay_welding = (((rx[7]>>3)&0x01)?1:0);
  698. Ret_Buf->bits.leak_module_fail = (((rx[7]>>4)&0x01)?1:0);
  699. Ret_Buf->bits.shutter_fault = (((rx[7]>>5)&0x01)?1:0);
  700. Ret_Buf->bits.locker_fault = (((rx[7]>>6)&0x01)?1:0);
  701. Ret_Buf->bits.power_drop = (((rx[7]>>7)&0x01)?1:0);
  702. //rx[8] 3 bits reserved
  703. Ret_Buf->bits.short_circuit_L1 = (((rx[8]>>0)&0x01)?1:0);
  704. Ret_Buf->bits.rotate_switch_fault = (((rx[8]>>1)&0x01)?1:0);
  705. Ret_Buf->bits.relay_drive_fault = (((rx[8]>>2)&0x01)?1:0);
  706. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  707. {
  708. Ret_Buf->bits.OVP_L2 = (((rx[8]>>4)&0x01)?1:0);
  709. Ret_Buf->bits.OVP_L3 = (((rx[8]>>5)&0x01)?1:0);
  710. Ret_Buf->bits.UVP_L2 = (((rx[8]>>6)&0x01)?1:0);
  711. Ret_Buf->bits.UVP_L3 = (((rx[8]>>7)&0x01)?1:0);
  712. }
  713. //rx[9] 2~7bits reserved
  714. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  715. {
  716. Ret_Buf->bits.OCP_L2 = (((rx[9]>>0)&0x01)?1:0);
  717. Ret_Buf->bits.OCP_L3 = (((rx[9]>>1)&0x01)?1:0);
  718. Ret_Buf->bits.short_circuit_L2 = (((rx[9]>>2)&0x01)?1:0);
  719. Ret_Buf->bits.short_circuit_L3 = (((rx[9]>>3)&0x01)?1:0);
  720. }
  721. result = PASS;
  722. }
  723. }
  724. return result;
  725. }
  726. unsigned char Query_Present_OutputCurrent(unsigned char fd, unsigned char targetAddr, Presentoutputcurrent *Ret_Buf)
  727. {
  728. unsigned char result = FAIL;
  729. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_PRESENT_OUTPUTCURRENT, 0x00, 0x00, 0x00};
  730. unsigned char rx[512];
  731. unsigned char chksum = 0x00;
  732. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  733. if(len > 0)
  734. {
  735. if(len < 6+(rx[4] | rx[5]<<8))
  736. return result;
  737. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  738. {
  739. chksum ^= rx[6+idx];
  740. }
  741. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  742. (rx[2] == tx[1]) &&
  743. (rx[1] == tx[2]) &&
  744. (rx[3] == tx[3]))
  745. {
  746. Ret_Buf->L1N_L12[0] = (rx[6] | (rx[7]<<8))/10.0;
  747. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  748. {
  749. Ret_Buf->L2N_L23[0] = (rx[8] | (rx[9]<<8))/10.0;
  750. Ret_Buf->L3N_L31[0] = (rx[10] | (rx[11]<<8))/10.0;
  751. }
  752. Ret_Buf->L1N_L12[1] = (rx[12] | (rx[13]<<8))/10.0;
  753. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  754. {
  755. Ret_Buf->L2N_L23[1] = (rx[14] | (rx[15]<<8))/10.0;
  756. Ret_Buf->L3N_L31[1] = (rx[16] | (rx[17]<<8))/10.0;
  757. }
  758. result = PASS;
  759. }
  760. }
  761. return result;
  762. }
  763. unsigned char Query_Ble_Config(unsigned char fd, unsigned char targetAddr, Ble_Config_Data *Ret_Buf)
  764. {
  765. unsigned char result = FAIL;
  766. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_BLE_CONFIG_DATA, 0x00, 0x00, 0x00};
  767. unsigned char rx[512];
  768. unsigned char chksum = 0x00;
  769. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  770. if(len > 0)
  771. {
  772. if(len < 6+(rx[4] | rx[5]<<8))
  773. return result;
  774. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  775. {
  776. chksum ^= rx[6+idx];
  777. }
  778. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  779. (rx[2] == tx[1]) &&
  780. (rx[1] == tx[2]) &&
  781. (rx[3] == tx[3]))
  782. {
  783. Ret_Buf->isLogin = (rx[6]?0x01:0x00);
  784. Ret_Buf->isRequestStart = (rx[7]?0x01:0x00);
  785. Ret_Buf->isRequestStop = (rx[8]?0x01:0x00);
  786. result = PASS;
  787. }
  788. }
  789. return result;
  790. }
  791. unsigned char Query_Ble_Central_ID(unsigned char fd, unsigned char targetAddr, Ble_Login_Central_Id *Ret_Buf)
  792. {
  793. unsigned char result = FAIL;
  794. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_BLE_CENTRAL_ID, 0x00, 0x00, 0x00};
  795. unsigned char rx[512];
  796. unsigned char chksum = 0x00;
  797. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  798. if(len > 0)
  799. {
  800. if(len < 6+(rx[4] | rx[5]<<8))
  801. return result;
  802. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  803. {
  804. chksum ^= rx[6+idx];
  805. }
  806. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  807. (rx[2] == tx[1]) &&
  808. (rx[1] == tx[2]) &&
  809. (rx[3] == tx[3]))
  810. {
  811. memset(Ret_Buf->id, 0x00, ARRAY_SIZE(Ret_Buf->id));
  812. memcpy(&Ret_Buf->id[0], &rx[6], (rx[4] | (rx[5]<<8)));
  813. result = PASS;
  814. }
  815. }
  816. return result;
  817. }
  818. unsigned char Query_Power_Consumption(unsigned char fd, unsigned char targetAddr, Power_Consumption *Ret_Buf_T, Power_Consumption *Ret_Buf_L1, Power_Consumption *Ret_Buf_L2, Power_Consumption *Ret_Buf_L3)
  819. {
  820. unsigned char result = FAIL;
  821. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_POWER_CONSUMPTION, 0x00, 0x00, 0x00};
  822. unsigned char rx[512];
  823. unsigned char chksum = 0x00;
  824. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  825. if(len > 0)
  826. {
  827. if(len < 6+(rx[4] | rx[5]<<8))
  828. return result;
  829. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  830. {
  831. chksum ^= rx[6+idx];
  832. }
  833. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  834. (rx[2] == tx[1]) &&
  835. (rx[1] == tx[2]) &&
  836. (rx[3] == tx[3]))
  837. {
  838. #ifndef SIMULATION
  839. Ret_Buf_T-> power_consumption = ((uint32_t)rx[6] | (((uint32_t)rx[7])<<8) | (((uint32_t)rx[8])<<16) | (((uint32_t)rx[9])<<24));
  840. Ret_Buf_L1-> power_consumption = ((uint32_t)rx[10] | (((uint32_t)rx[11])<<8) | (((uint32_t)rx[12])<<16) | (((uint32_t)rx[13])<<24));
  841. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  842. {
  843. Ret_Buf_L2-> power_consumption = ((uint32_t)rx[14] | (((uint32_t)rx[15])<<8) | (((uint32_t)rx[16])<<16) | (((uint32_t)rx[17])<<24));
  844. Ret_Buf_L3-> power_consumption = ((uint32_t)rx[18] | (((uint32_t)rx[19])<<8) | (((uint32_t)rx[20])<<16) | (((uint32_t)rx[21])<<24));
  845. }
  846. #else //SIMULATION
  847. tsNow = current_timestamp();
  848. tmpPowerConsumption += (ShmSysConfigAndInfo->SysInfo.AcChargingData[0].PresentChargingVoltage*ShmSysConfigAndInfo->SysInfo.AcChargingData[0].PresentChargingCurrent)*((tsNow-tsLast)/36000000.0);
  849. Ret_Buf_T-> power_consumption = (uint32_t)tmpPowerConsumption;
  850. tsLast = tsNow;
  851. #endif //SIMULATION
  852. result = PASS;
  853. }
  854. }
  855. return result;
  856. }
  857. unsigned char Config_Fan_Speed(unsigned char fd, unsigned char targetAddr, FanSpeed *Set_Buf)
  858. {
  859. unsigned char result = FAIL;
  860. unsigned char tx[15] = {0xaa, 0x00, targetAddr, CMD_CONFIG_FAN_SPEED, 0x08, 0x00, Set_Buf->speed[0]&0xff, (Set_Buf->speed[0]>>8)&0xff, Set_Buf->speed[1]&0xff, (Set_Buf->speed[1]>>8)&0xff, Set_Buf->speed[2]&0xff, (Set_Buf->speed[2]>>8)&0xff, Set_Buf->speed[3]&0xff, (Set_Buf->speed[3]>>8)&0xff, 0x00};
  861. unsigned char rx[512];
  862. unsigned char chksum = 0x00;
  863. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  864. chksum ^= tx[6+idx];
  865. tx[14] = chksum;
  866. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  867. if(len > 6)
  868. {
  869. if (len < 6+(rx[4] | rx[5]<<8))
  870. return result;
  871. chksum = 0x00;
  872. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  873. {
  874. chksum ^= rx[6+idx];
  875. }
  876. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  877. (rx[2] == tx[1]) &&
  878. (rx[1] == tx[2]) &&
  879. (rx[3] == tx[3]) &&
  880. (rx[6] == 0x01))
  881. {
  882. result = PASS;
  883. }
  884. }
  885. return result;
  886. }
  887. unsigned char Config_Serial_Number(unsigned char fd, unsigned char targetAddr, Evse_Id *Set_Buf)
  888. {
  889. unsigned char result = FAIL;
  890. unsigned char tx[27] = {0xaa, 0x00, targetAddr, CMD_CONFIG_SERIAL_NUMBER, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  891. unsigned char rx[512];
  892. unsigned char chksum = 0x00;
  893. memcpy(&tx[14], &Set_Buf->serial_number[0], ARRAY_SIZE(Set_Buf->serial_number));
  894. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  895. chksum ^= tx[6+idx];
  896. tx[26] = chksum;
  897. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  898. if(len > 6)
  899. {
  900. if (len < 6+(rx[4] | rx[5]<<8))
  901. return result;
  902. chksum = 0x00;
  903. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  904. {
  905. chksum ^= rx[6+idx];
  906. }
  907. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  908. (rx[2] == tx[1]) &&
  909. (rx[1] == tx[2]) &&
  910. (rx[3] == tx[3]) &&
  911. (rx[6] == 0x01))
  912. {
  913. result = PASS;
  914. }
  915. }
  916. return result;
  917. }
  918. unsigned char Config_Model_Name(unsigned char fd, unsigned char targetAddr, Evse_Id *Set_Buf)
  919. {
  920. unsigned char result = FAIL;
  921. unsigned char tx[21] = {0xaa, 0x00, targetAddr, CMD_CONFIG_MODEL_NAME, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  922. unsigned char rx[512];
  923. unsigned char chksum = 0x00;
  924. memcpy(&tx[6], &Set_Buf->model_name[0], ARRAY_SIZE(Set_Buf->model_name));
  925. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  926. chksum ^= tx[6+idx];
  927. tx[20] = chksum;
  928. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  929. if(len > 6)
  930. {
  931. if (len < 6+(rx[4] | rx[5]<<8))
  932. return result;
  933. chksum = 0x00;
  934. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  935. {
  936. chksum ^= rx[6+idx];
  937. }
  938. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  939. (rx[2] == tx[1]) &&
  940. (rx[1] == tx[2]) &&
  941. (rx[3] == tx[3]) &&
  942. (rx[6] == 0x01))
  943. {
  944. result = PASS;
  945. }
  946. }
  947. return result;
  948. }
  949. unsigned char Config_Relay_Output(unsigned char fd, unsigned char targetAddr, Relay *Set_Buf)
  950. {
  951. unsigned char result = FAIL;
  952. unsigned char tx[9] = {0xaa, 0x00, targetAddr, CMD_CONFIG_RELAY_OUTPUT, 0x02, 0x00, 0x00, 0x00, 0x00};
  953. unsigned char rx[512];
  954. unsigned char chksum = 0x00;
  955. for(int idx_connector=0;idx_connector<2;idx_connector++)
  956. for(int idx = 0;idx<8;idx++)
  957. tx[6+idx_connector] |= ((Set_Buf->relay_status[idx_connector][idx]?0x01:0x00)<<idx);
  958. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  959. chksum ^= tx[6+idx];
  960. tx[8] = chksum;
  961. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  962. if(len > 6)
  963. {
  964. if (len < 6+(rx[4] | rx[5]<<8))
  965. return result;
  966. chksum = 0x00;
  967. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  968. {
  969. chksum ^= rx[6+idx];
  970. }
  971. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  972. (rx[2] == tx[1]) &&
  973. (rx[1] == tx[2]) &&
  974. (rx[3] == tx[3]) &&
  975. (rx[6] == 0x01))
  976. {
  977. result = PASS;
  978. }
  979. }
  980. return result;
  981. }
  982. unsigned char Config_Gpio_Output(unsigned char fd, unsigned char targetAddr, Gpio_out *Set_Buf)
  983. {
  984. unsigned char result = FAIL;
  985. unsigned char tx[9] = {0xaa, 0x00, targetAddr, CMD_CONFIG_GPIO_OUTPUT, 0x01, 0x00, 0x00, 0x00};
  986. unsigned char rx[512];
  987. unsigned char chksum = 0x00;
  988. tx[6] |= (Set_Buf->AC_Connector?0x01:0x00);
  989. for(int idx=0;idx<2;idx++)
  990. tx[6] |= (Set_Buf->Button_LED[idx]?0x01:0x00)<<(1+idx);
  991. for(int idx=0;idx<4;idx++)
  992. tx[6] |= (Set_Buf->System_LED[idx]?0x01:0x00)<<(3+idx);
  993. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  994. chksum ^= tx[6+idx];
  995. tx[14] = chksum;
  996. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  997. if(len > 6)
  998. {
  999. if (len < 6+(rx[4] | rx[5]<<8))
  1000. return result;
  1001. chksum = 0x00;
  1002. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1003. {
  1004. chksum ^= rx[6+idx];
  1005. }
  1006. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1007. (rx[2] == tx[1]) &&
  1008. (rx[1] == tx[2]) &&
  1009. (rx[3] == tx[3]) &&
  1010. (rx[6] == tx[6]))
  1011. {
  1012. result = PASS;
  1013. }
  1014. }
  1015. return result;
  1016. }
  1017. unsigned char Config_RTC(unsigned char fd, unsigned char targetAddr, Rtc *Set_Buf)
  1018. {
  1019. unsigned char result = FAIL;
  1020. unsigned char tx[21];
  1021. unsigned char rx[512];
  1022. unsigned char chksum = 0x00;
  1023. tx[0] = 0xaa;
  1024. tx[1] = 0x00;
  1025. tx[2] = targetAddr;
  1026. tx[3] = CMD_CONFIG_RTC;
  1027. tx[4] = 0x0e;
  1028. tx[5] = 0x00;
  1029. tx[6] = ((Set_Buf->year)/1000)+'0';
  1030. tx[7] = ((Set_Buf->year)/100%10)+'0';
  1031. tx[8] = ((Set_Buf->year)/10%10)+'0';
  1032. tx[9] = ((Set_Buf->year)%10)+'0';
  1033. tx[10] = ((Set_Buf->month)/10)+'0';
  1034. tx[11] = ((Set_Buf->month)%10)+'0';
  1035. tx[12] = ((Set_Buf->day)/10)+'0';
  1036. tx[13] = ((Set_Buf->day)%10)+'0';
  1037. tx[14] = ((Set_Buf->hour)/10)+'0';
  1038. tx[15] = ((Set_Buf->hour)%10)+'0';
  1039. tx[16] = ((Set_Buf->min)/10)+'0';
  1040. tx[17] = ((Set_Buf->min)%10)+'0';
  1041. tx[18] = ((Set_Buf->sec)/10)+'0';
  1042. tx[19] = ((Set_Buf->sec)%10)+'0';
  1043. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1044. chksum ^= tx[6+idx];
  1045. tx[20] = chksum;
  1046. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1047. if(len > 6)
  1048. {
  1049. if (len < 6+(rx[4] | rx[5]<<8))
  1050. return result;
  1051. chksum = 0x00;
  1052. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1053. {
  1054. chksum ^= rx[6+idx];
  1055. }
  1056. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1057. (rx[2] == tx[1]) &&
  1058. (rx[1] == tx[2]) &&
  1059. (rx[3] == tx[3]) &&
  1060. (rx[6] == 0x01))
  1061. {
  1062. result = PASS;
  1063. }
  1064. }
  1065. return result;
  1066. }
  1067. unsigned char Config_AC_MCU_LED(unsigned char fd, unsigned char targetAddr, Ac_Primary_Mcu_Led *Set_Buf)
  1068. {
  1069. unsigned char result = FAIL;
  1070. unsigned char tx[12] ;
  1071. unsigned char rx[512];
  1072. unsigned char chksum = 0x00;
  1073. tx[0] = 0xaa;
  1074. tx[1] = 0x00;
  1075. tx[2] = targetAddr;
  1076. tx[3] = CMD_CONFIG_AC_LED;
  1077. tx[4] = 0x05;
  1078. tx[5] = 0x00;
  1079. tx[6] = Set_Buf->mode;
  1080. tx[7] = ((Set_Buf->alarm_code>>0)&0xff);
  1081. tx[8] = ((Set_Buf->alarm_code>>8)&0xff);
  1082. tx[9] = ((Set_Buf->alarm_code>>16)&0xff);
  1083. tx[10] = ((Set_Buf->alarm_code>>24)&0xff);
  1084. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1085. chksum ^= tx[6+idx];
  1086. tx[11] = chksum;
  1087. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1088. if(len > 6)
  1089. {
  1090. if (len < 6+(rx[4] | rx[5]<<8))
  1091. return result;
  1092. chksum = 0x00;
  1093. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1094. {
  1095. chksum ^= rx[6+idx];
  1096. }
  1097. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1098. (rx[2] == tx[1]) &&
  1099. (rx[1] == tx[2]) &&
  1100. (rx[3] == tx[3]) &&
  1101. (rx[6] == 0x01))
  1102. {
  1103. result = PASS;
  1104. }
  1105. }
  1106. return result;
  1107. }
  1108. unsigned char Config_AC_MCU_LEGACY_REQUEST(unsigned char fd, unsigned char targetAddr, Legacy_Request *Set_Buf)
  1109. {
  1110. unsigned char result = FAIL;
  1111. unsigned char tx[9];
  1112. unsigned char rx[512];
  1113. unsigned char chksum = 0x00;
  1114. tx[0] = 0xaa;
  1115. tx[1] = 0x00;
  1116. tx[2] = targetAddr;
  1117. tx[3] = CMD_CONFIG_LEGACY_REQUEST;
  1118. tx[4] = 0x02;
  1119. tx[5] = 0x00;
  1120. tx[6] = Set_Buf->isLegacyRequest;
  1121. tx[7] = 0x00;
  1122. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1123. chksum ^= tx[6+idx];
  1124. tx[8] = chksum;
  1125. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1126. if(len > 6)
  1127. {
  1128. if (len < 6+(rx[4] | rx[5]<<8))
  1129. return result;
  1130. chksum = 0x00;
  1131. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1132. {
  1133. chksum ^= rx[6+idx];
  1134. }
  1135. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1136. (rx[2] == tx[1]) &&
  1137. (rx[1] == tx[2]) &&
  1138. (rx[3] == tx[3]) &&
  1139. (rx[6] == 0x01))
  1140. {
  1141. result = PASS;
  1142. }
  1143. }
  1144. return result;
  1145. }
  1146. unsigned char Config_AC_MCU_RESET_REQUEST(unsigned char fd, unsigned char targetAddr, Mcu_Reset_Request *Set_Buf)
  1147. {
  1148. unsigned char result = FAIL;
  1149. unsigned char tx[9];
  1150. unsigned char rx[512];
  1151. unsigned char chksum = 0x00;
  1152. tx[0] = 0xaa;
  1153. tx[1] = 0x00;
  1154. tx[2] = targetAddr;
  1155. tx[3] = CMD_CONFIG_MCU_RESET_REQUEST;
  1156. tx[4] = 0x02;
  1157. tx[5] = 0x00;
  1158. tx[6] = Set_Buf->isMcuResetRequest;
  1159. tx[7] = 0x00;
  1160. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1161. chksum ^= tx[6+idx];
  1162. tx[8] = chksum;
  1163. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1164. if(len > 6)
  1165. {
  1166. if (len < 6+(rx[4] | rx[5]<<8))
  1167. return result;
  1168. chksum = 0x00;
  1169. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1170. {
  1171. chksum ^= rx[6+idx];
  1172. }
  1173. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1174. (rx[2] == tx[1]) &&
  1175. (rx[1] == tx[2]) &&
  1176. (rx[3] == tx[3]) &&
  1177. (rx[6] == 0x01))
  1178. {
  1179. result = PASS;
  1180. }
  1181. }
  1182. return result;
  1183. }
  1184. unsigned char Query_AC_GUN_PLUGIN_TIMES(unsigned char fd, unsigned char targetAddr, Gun_Plugin_Times *Ret_Buf)
  1185. {
  1186. unsigned char result = FAIL;
  1187. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_GUN_PLUGIN_TIMES, 0x00, 0x00, 0x00};
  1188. unsigned char rx[512];
  1189. unsigned char chksum = 0x00;
  1190. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1191. if(len > 6)
  1192. {
  1193. if(len < 6+(rx[4] | rx[5]<<8))
  1194. return result;
  1195. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1196. {
  1197. chksum ^= rx[6+idx];
  1198. }
  1199. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1200. (rx[2] == tx[1]) &&
  1201. (rx[1] == tx[2]) &&
  1202. (rx[3] == tx[3]))
  1203. {
  1204. Ret_Buf-> GunPluginTimes = ((uint32_t)rx[6] | (((uint32_t)rx[7])<<8) | (((uint32_t)rx[8])<<16) | (((uint32_t)rx[9])<<24));
  1205. result = PASS;
  1206. }
  1207. }
  1208. return result;
  1209. }
  1210. unsigned char Config_AC_MaxCurrent_And_CpPwmDuty(unsigned char fd, unsigned char targetAddr, Ac_Primary_Mcu_Cp_Pwm_Duty*Set_Buf)
  1211. {
  1212. unsigned char result = FAIL;
  1213. unsigned char tx[8];
  1214. unsigned char rx[512];
  1215. unsigned char chksum = 0x00;
  1216. tx[0] = 0xaa;
  1217. tx[1] = 0x00;
  1218. tx[2] = targetAddr;
  1219. tx[3] = CMD_CONFIG_CURRENT_LINIT;
  1220. tx[4] = 0x01;
  1221. tx[5] = 0x00;
  1222. tx[6] = Set_Buf->max_current;
  1223. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1224. chksum ^= tx[6+idx];
  1225. tx[7] = chksum;
  1226. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1227. if(len > 6)
  1228. {
  1229. if(len < 6+(rx[4] | rx[5]<<8))
  1230. return result;
  1231. chksum = 0x00;
  1232. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1233. {
  1234. chksum ^= rx[6+idx];
  1235. }
  1236. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1237. (rx[2] == tx[1]) &&
  1238. (rx[1] == tx[2]) &&
  1239. (rx[3] == tx[3]) &&
  1240. (rx[6] == 0x01))
  1241. {
  1242. result = PASS;
  1243. }
  1244. }
  1245. return result;
  1246. }
  1247. unsigned char Config_AC_Set_Breathe_Led_Timing(unsigned char fd, unsigned char targetAddr,Set_Breathe_Led_Timing*Set_Buf)
  1248. {
  1249. unsigned char result = FAIL;
  1250. unsigned char tx[19];
  1251. unsigned char rx[512];
  1252. unsigned char chksum = 0x00;
  1253. tx[0] = 0xaa;
  1254. tx[1] = 0x00;
  1255. tx[2] = targetAddr;
  1256. tx[3] = CMD_CONFIG_MCU_SET_BREATHE_LED_TIMING;
  1257. tx[4] = 0x0C;
  1258. tx[5] = 0x00;
  1259. // Increase LED_ACTION_CONNECTED
  1260. tx[6] = (Set_Buf->set_Led_Action_Connected_Fade_In & 0xff);
  1261. tx[7] = (Set_Buf->set_Led_Action_Connected_Fade_In >> 8);
  1262. // Decrease LED_ACTION_CONNECTED
  1263. tx[8] = (Set_Buf->set_Led_Action_Connected_Fade_Out & 0xff);
  1264. tx[9] = (Set_Buf->set_Led_Action_Connected_Fade_Out >> 8);
  1265. // Increase LED_ACTION_AUTHED
  1266. tx[10] = (Set_Buf->set_Led_Action_Authed_Fade_In & 0xff);
  1267. tx[11] = (Set_Buf->set_Led_Action_Authed_Fade_In >> 8);
  1268. // Decrease LED_ACTION_AUTHED
  1269. tx[12] = (Set_Buf->set_Led_Action_Authed_Fade_Out & 0xff);
  1270. tx[13] = (Set_Buf->set_Led_Action_Authed_Fade_Out >> 8);
  1271. // Increase_LED_ACTION_CHARGING
  1272. tx[14] = (Set_Buf->Set_Led_Action_Chaging_Fade_In & 0xff);
  1273. tx[15] = (Set_Buf->Set_Led_Action_Chaging_Fade_In >> 8);
  1274. // Decrease_LED_ACTION_CHARGING
  1275. tx[16] = (Set_Buf->set_Led_Action_Chaging_Fade_Out & 0xff);
  1276. tx[17] = (Set_Buf->set_Led_Action_Chaging_Fade_Out >> 8);
  1277. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1278. chksum ^= tx[6+idx];
  1279. tx[18] = chksum;
  1280. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1281. if(len > 6)
  1282. {
  1283. if(len < 6+(rx[4] | rx[5]<<8))
  1284. return result;
  1285. chksum = 0x00;
  1286. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1287. {
  1288. chksum ^= rx[6+idx];
  1289. }
  1290. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1291. (rx[2] == tx[1]) &&
  1292. (rx[1] == tx[2]) &&
  1293. (rx[3] == tx[3]) &&
  1294. (rx[6] == 0x01))
  1295. {
  1296. result = PASS;
  1297. }
  1298. }
  1299. return result;
  1300. }
  1301. unsigned char Config_AC_Set_Led_Brightness(unsigned char fd, unsigned char targetAddr,Set_Led_Brightness*Set_Buf)
  1302. {
  1303. unsigned char result = FAIL;
  1304. unsigned char tx[19];
  1305. unsigned char rx[512];
  1306. unsigned char chksum = 0x00;
  1307. tx[0] = 0xaa;
  1308. tx[1] = 0x00;
  1309. tx[2] = targetAddr;
  1310. tx[3] = CMD_CONFIG_MCU_SET_LED_BRIGHTNESS;
  1311. tx[4] = 0x0C;
  1312. tx[5] = 0x00;
  1313. tx[6] = Set_Buf-> sector_1; // 0~1 AM and 1~2 AM
  1314. tx[7] = Set_Buf-> sector_2; // 2~3 AM and 3~4 AM
  1315. tx[8] = Set_Buf-> sector_3; // 4~5 AM and 5~6 AM
  1316. tx[9] = Set_Buf-> sector_4; // 6~7 AM and 7~8 AM
  1317. tx[10] = Set_Buf-> sector_5; // 8~9 AM and 9~10 AM
  1318. tx[11] = Set_Buf-> sector_6; // 10~11 AM and 11~12 AM
  1319. tx[12] = Set_Buf-> sector_7; // 12~13 PM and 13~14 PM
  1320. tx[13] = Set_Buf-> sector_8; // 14~15 PM and 15~16 PM
  1321. tx[14] = Set_Buf-> sector_9; // 16~17 PM and 17~18 PM
  1322. tx[15] = Set_Buf-> sector_10; // 18~19 PM and 19~20 PM
  1323. tx[16] = Set_Buf-> sector_11; // 20~21 PM and 21~22 PM
  1324. tx[17] = Set_Buf-> sector_12; // 22~23 PM and 23~24 PM
  1325. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1326. chksum ^= tx[6+idx];
  1327. tx[18] = chksum;
  1328. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1329. if(len > 6)
  1330. {
  1331. if(len < 6+(rx[4] | rx[5]<<8))
  1332. return result;
  1333. chksum = 0x00;
  1334. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1335. {
  1336. chksum ^= rx[6+idx];
  1337. }
  1338. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1339. (rx[2] == tx[1]) &&
  1340. (rx[1] == tx[2]) &&
  1341. (rx[3] == tx[3]) &&
  1342. (rx[6] == 0x01))
  1343. {
  1344. result = PASS;
  1345. }
  1346. }
  1347. return result;
  1348. }
  1349. unsigned char Update_Start(unsigned char fd, unsigned char targetAddr, unsigned int crc32)
  1350. {
  1351. unsigned char result = FAIL;
  1352. unsigned char tx[11] = {0xaa, 0x00, targetAddr, CMD_UPDATE_START, 0x04, 0x00, (crc32>>0)&0xff, (crc32>>8)&0xff, (crc32>>16)&0xff, (crc32>>24)&0xff, 0x00};
  1353. unsigned char rx[512];
  1354. unsigned char chksum = 0x00;
  1355. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1356. chksum ^= tx[6+idx];
  1357. tx[10] = chksum;
  1358. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1359. if(len > 6)
  1360. {
  1361. if (len < 6+(rx[4] | rx[5]<<8))
  1362. return result;
  1363. chksum = 0x00;
  1364. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  1365. {
  1366. chksum ^= rx[6+idx];
  1367. }
  1368. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1369. (rx[2] == tx[1]) &&
  1370. (rx[1] == tx[2]) &&
  1371. (rx[3] == tx[3]) &&
  1372. (rx[6] == 0x00))
  1373. {
  1374. result = PASS;
  1375. }
  1376. }
  1377. return result;
  1378. }
  1379. unsigned char Update_Abord(unsigned char fd, unsigned char targetAddr)
  1380. {
  1381. unsigned char result = FAIL;
  1382. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_UPDATE_ABOARD, 0x04, 0x00, 0x00};
  1383. unsigned char rx[512];
  1384. unsigned char chksum = 0x00;
  1385. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1386. if(len > 6)
  1387. {
  1388. if (len < 6+(rx[4] | rx[5]<<8))
  1389. return result;
  1390. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  1391. {
  1392. chksum ^= rx[6+idx];
  1393. }
  1394. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1395. (rx[2] == tx[1]) &&
  1396. (rx[1] == tx[2]) &&
  1397. (rx[3] == tx[3]) &&
  1398. (rx[6] == 0x00))
  1399. {
  1400. result = PASS;
  1401. }
  1402. }
  1403. return result;
  1404. }
  1405. unsigned char Update_Transfer(unsigned char fd, unsigned char targetAddr, unsigned int startAddr, unsigned char *data, unsigned short int length)
  1406. {
  1407. unsigned char result = FAIL;
  1408. unsigned char tx[11 + length];
  1409. unsigned char rx[512];
  1410. unsigned char chksum = 0x00;
  1411. tx[0] = 0xaa;
  1412. tx[1] = 0x00;
  1413. tx[2] = targetAddr;
  1414. tx[3] = CMD_UPDATE_TRANSFER;
  1415. tx[4] = (4 + length) & 0xff;
  1416. tx[5] = ((4 + length)>>8) & 0xff;
  1417. tx[6] = (startAddr>>0) & 0xff;
  1418. tx[7] = (startAddr>>8) & 0xff;
  1419. tx[8] = (startAddr>>16) & 0xff;
  1420. tx[9] = (startAddr>>24) & 0xff;
  1421. memcpy(tx+10, data, length);
  1422. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1423. chksum ^= tx[6+idx];
  1424. tx[sizeof(tx)-1] = chksum;
  1425. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1426. if(len > 6)
  1427. {
  1428. if (len < 6+(rx[4] | rx[5]<<8))
  1429. return result;
  1430. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  1431. {
  1432. chksum ^= rx[6+idx];
  1433. }
  1434. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1435. (rx[2] == tx[1]) &&
  1436. (rx[1] == tx[2]) &&
  1437. (rx[3] == tx[3]) &&
  1438. (rx[6] == 0x00))
  1439. {
  1440. result = PASS;
  1441. }
  1442. }
  1443. return result;
  1444. }
  1445. unsigned char Update_Finish(unsigned char fd, unsigned char targetAddr)
  1446. {
  1447. unsigned char result = FAIL;
  1448. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_UPDATE_END, 0x04, 0x00, 0x00};
  1449. unsigned char rx[512];
  1450. unsigned char chksum = 0x00;
  1451. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1452. if(len > 6)
  1453. {
  1454. if (len < 6+(rx[4] | rx[5]<<8))
  1455. return result;
  1456. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1457. {
  1458. chksum ^= rx[6+idx];
  1459. }
  1460. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1461. (rx[2] == tx[1]) &&
  1462. (rx[1] == tx[2]) &&
  1463. (rx[3] == tx[3]) &&
  1464. (rx[6] == 0x01))
  1465. {
  1466. result = PASS;
  1467. }
  1468. }
  1469. return result;
  1470. }
  1471. //================================================
  1472. // Main process
  1473. //================================================
  1474. int main(void)
  1475. {
  1476. int Uart1Fd;
  1477. unsigned short int failCount[2] = {0,0};
  1478. if(InitShareMemory() == FAIL)
  1479. {
  1480. #ifdef SystemLogMessage
  1481. DEBUG_ERROR("InitShareMemory NG\n");
  1482. #endif
  1483. if(ShmStatusCodeData!=NULL)
  1484. {
  1485. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  1486. }
  1487. sleep(5);
  1488. return 0;
  1489. }
  1490. else
  1491. {
  1492. DEBUG_INFO("InitShareMemory OK.\n");
  1493. }
  1494. Uart1Fd=InitComPort();
  1495. if(Uart1Fd<0)
  1496. {
  1497. DEBUG_ERROR("InitComPort NG\n");
  1498. if(ShmStatusCodeData!=NULL)
  1499. {
  1500. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1501. }
  1502. sleep(5);
  1503. return 0;
  1504. }
  1505. else
  1506. {
  1507. DEBUG_INFO("ttyS1 port open success.\n");
  1508. }
  1509. for(;;)
  1510. {
  1511. for(int gun_index=0;gun_index<AC_QUANTITY;gun_index++)
  1512. {
  1513. /*
  1514. * Polling loop
  1515. */
  1516. if((stepIndex%2)==0)
  1517. {
  1518. /*
  1519. * High priority communication
  1520. */
  1521. //===============================
  1522. // Case 1 : Config primary MCU LED
  1523. //===============================
  1524. ShmCharger->gun_info[gun_index].primaryMcuLed.alarm_code = ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode;
  1525. if(Config_AC_MCU_LED(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].primaryMcuLed))
  1526. {
  1527. failCount[gun_index] = 0;
  1528. }
  1529. else
  1530. {
  1531. DEBUG_WARN("MCU-%d set led fail...%d\n", gun_index, failCount[gun_index]);
  1532. if(failCount[gun_index]<USHRT_MAX)
  1533. failCount[gun_index]++;
  1534. else
  1535. failCount[gun_index] = FAIL_SPEC_COMM;
  1536. }
  1537. //===============================
  1538. // Case 2 : Config primary Legacy request
  1539. //===============================
  1540. if(Config_AC_MCU_LEGACY_REQUEST(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1),&ShmCharger->gun_info[gun_index].legacyRequest))
  1541. {
  1542. failCount[gun_index] = 0;
  1543. }
  1544. else
  1545. {
  1546. DEBUG_WARN("MCU-%d set request fail...%d\n", gun_index, failCount[gun_index]);
  1547. if(failCount[gun_index]<USHRT_MAX)
  1548. failCount[gun_index]++;
  1549. else
  1550. failCount[gun_index] = FAIL_SPEC_COMM;
  1551. }
  1552. //===============================
  1553. // Case 2-X : Config primary Relay
  1554. //===============================
  1555. if(Config_Relay_Output(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1),&ShmCharger->gun_info[gun_index].primaryMcuState.relayState))
  1556. {
  1557. failCount[gun_index] = 0;
  1558. }
  1559. else
  1560. {
  1561. DEBUG_WARN("MCU-%d set relay fail...%d\n", gun_index, failCount[gun_index]);
  1562. if(failCount[gun_index]<USHRT_MAX)
  1563. failCount[gun_index]++;
  1564. else
  1565. failCount[gun_index] = FAIL_SPEC_COMM;
  1566. }
  1567. //===============================
  1568. // Case 3 : Query primary MCU status
  1569. //===============================
  1570. if(Query_AC_MCU_Status(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].primaryMcuState))
  1571. {
  1572. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState = ShmCharger->gun_info[gun_index].primaryMcuState.cp_state;
  1573. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotDuty = (ShmCharger->gun_info[gun_index].primaryMcuState.current_limit>51?(unsigned char)((ShmCharger->gun_info[gun_index].primaryMcuState.current_limit/2.5)+64):(unsigned char)(ShmCharger->gun_info[gun_index].primaryMcuState.current_limit/0.6));
  1574. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotVoltage = ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_positive;
  1575. ShmCharger->gun_info[gun_index].PilotVoltage.PilotVoltagePositive = ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_positive;
  1576. ShmCharger->gun_info[gun_index].PilotVoltage.PilotVoltageNegative = ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_negtive;
  1577. //pass info 2 CCS task
  1578. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_UNKNOWN)
  1579. {
  1580. if(ShmCharger->gun_info[gun_index].PilotVoltage.PilotVoltageNegative >= -12)
  1581. {
  1582. ShmCharger->gun_info[gun_index].acCcsInfo.CpPresentState = CCS_CP_STATE_G;
  1583. }
  1584. else
  1585. {
  1586. ShmCharger->gun_info[gun_index].acCcsInfo.CpPresentState = CCS_CP_STATE_H;
  1587. }
  1588. }
  1589. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  1590. {
  1591. ShmCharger->gun_info[gun_index].acCcsInfo.CpPresentState = CCS_CP_STATE_A;
  1592. }
  1593. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B)
  1594. {
  1595. if(ShmCharger->gun_info[gun_index].PilotVoltage.PilotVoltageNegative == 0.0)
  1596. {
  1597. ShmCharger->gun_info[gun_index].acCcsInfo.CpPresentState = CCS_CP_STATE_B1;
  1598. }
  1599. else
  1600. {
  1601. ShmCharger->gun_info[gun_index].acCcsInfo.CpPresentState = CCS_CP_STATE_B2;
  1602. }
  1603. }
  1604. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)
  1605. {
  1606. ShmCharger->gun_info[gun_index].acCcsInfo.CpPresentState = CCS_CP_STATE_C;
  1607. }
  1608. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_D)
  1609. {
  1610. ShmCharger->gun_info[gun_index].acCcsInfo.CpPresentState = CCS_CP_STATE_D;
  1611. }
  1612. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_E)
  1613. {
  1614. ShmCharger->gun_info[gun_index].acCcsInfo.CpPresentState = CCS_CP_STATE_E;
  1615. }
  1616. else //CP_STATE_F
  1617. {
  1618. ShmCharger->gun_info[gun_index].acCcsInfo.CpPresentState = CCS_CP_STATE_F;
  1619. }
  1620. ShmCharger->gun_info[gun_index].acCcsInfo.CpPositiveVoltage = ShmCharger->gun_info[gun_index].PilotVoltage.PilotVoltagePositive;
  1621. ShmCharger->gun_info[gun_index].acCcsInfo.CpNegativeVoltage = ShmCharger->gun_info[gun_index].PilotVoltage.PilotVoltageNegative;
  1622. ShmCharger->gun_info[gun_index].acCcsInfo.CpPresentPWMDuty = ShmCharger->gun_info[gun_index].primaryMcuState.current_limit;
  1623. if(ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0]>0)
  1624. {
  1625. ShmCharger->gun_info[gun_index].acCcsInfo.OutputRelayStatus = ON;
  1626. }
  1627. else
  1628. {
  1629. ShmCharger->gun_info[gun_index].acCcsInfo.OutputRelayStatus = OFF;
  1630. }
  1631. failCount[gun_index] = 0;
  1632. }
  1633. else
  1634. {
  1635. DEBUG_WARN("MCU-%d get status fail...%d\n", gun_index, failCount[gun_index]);
  1636. if(failCount[gun_index]<USHRT_MAX)
  1637. failCount[gun_index]++;
  1638. else
  1639. failCount[gun_index] = FAIL_SPEC_COMM;
  1640. }
  1641. //===============================
  1642. // Case 4 : Query primary MCU Alarm code
  1643. //===============================
  1644. if(Query_AC_MCU_Alarm(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].primaryMcuAlarm))
  1645. {
  1646. //DEBUG_INFO("MCU-%d get alarm code success.\n",gun_index);
  1647. //================================================
  1648. // Byte[6]
  1649. // Byte[7]
  1650. // Byte[8] Alarm code 3~7 bits Reserved
  1651. // Byte[9] Alarm code 0~7 bits Reserved
  1652. //================================================
  1653. //================================================
  1654. // Byte[6]
  1655. //================================================
  1656. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OVP_L1 == 0x01)
  1657. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<0;
  1658. else
  1659. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<0);
  1660. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.UVP_L1 == 0x01)
  1661. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<1;
  1662. else
  1663. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<1);
  1664. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OCP_L1 == 0x01)
  1665. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<2;
  1666. else
  1667. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<2);
  1668. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OTP == 0x01)
  1669. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<3;
  1670. else
  1671. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<3);
  1672. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.gmi_fault == 0x01)
  1673. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<4;
  1674. else
  1675. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<4);
  1676. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.cp_fault == 0x01)
  1677. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<5;
  1678. else
  1679. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<5);
  1680. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.ac_leak == 0x01)
  1681. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<6;
  1682. else
  1683. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<6);
  1684. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.dc_leak == 0x01)
  1685. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<7;
  1686. else
  1687. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<7);
  1688. //================================================
  1689. // Byte[7]
  1690. //================================================
  1691. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.mcu_selftest_fail == 0x01)
  1692. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<8;
  1693. else
  1694. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<8);
  1695. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.handshaking_timeout == 0x01)
  1696. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<9;
  1697. else
  1698. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<9);
  1699. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.emergency_stop == 0x01)
  1700. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<10;
  1701. else
  1702. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<10);
  1703. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.relay_welding == 0x01)
  1704. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<11;
  1705. else
  1706. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<11);
  1707. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.leak_module_fail == 0x01)
  1708. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<12;
  1709. else
  1710. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<12);
  1711. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.shutter_fault == 0x01)
  1712. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<13;
  1713. else
  1714. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<13);
  1715. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.locker_fault == 0x01)
  1716. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<14;
  1717. else
  1718. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<14);
  1719. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.power_drop == 0x01)
  1720. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<15;
  1721. else
  1722. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<15);
  1723. //================================================
  1724. // Byte[8]
  1725. //================================================
  1726. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.short_circuit_L1 == 0x01)
  1727. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<16;
  1728. else
  1729. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<16);
  1730. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.rotate_switch_fault == 0x01)
  1731. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<17;
  1732. else
  1733. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<17);
  1734. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.relay_drive_fault == 0x01)
  1735. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<18;
  1736. else
  1737. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<18);
  1738. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OVP_L2 == 0x01)
  1739. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<20;
  1740. else
  1741. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<20);
  1742. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OVP_L3 == 0x01)
  1743. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<21;
  1744. else
  1745. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<21);
  1746. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.UVP_L2 == 0x01)
  1747. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<22;
  1748. else
  1749. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<22);
  1750. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.UVP_L3 == 0x01)
  1751. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<23;
  1752. else
  1753. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<23);
  1754. //================================================
  1755. // Byte[9]
  1756. //================================================
  1757. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OCP_L2 == 0x01)
  1758. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<24;
  1759. else
  1760. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<24);
  1761. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OCP_L3 == 0x01)
  1762. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<25;
  1763. else
  1764. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<25);
  1765. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.short_circuit_L2 == 0x01)
  1766. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<26;
  1767. else
  1768. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<26);
  1769. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.short_circuit_L3 == 0x01)
  1770. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<27;
  1771. else
  1772. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<27);
  1773. failCount[gun_index] = 0;
  1774. ShmCharger->gun_info[gun_index].acCcsInfo.CSUAlarmStatusCode = ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode;
  1775. }
  1776. else
  1777. {
  1778. DEBUG_WARN("MCU-%d get alarm fail...%d\n", gun_index, failCount[gun_index]);
  1779. if(failCount[gun_index]<USHRT_MAX)
  1780. failCount[gun_index]++;
  1781. else
  1782. failCount[gun_index] = FAIL_SPEC_COMM;
  1783. }
  1784. //===============================
  1785. // Case 5 : Query primary MCU BLE config
  1786. //===============================
  1787. if(Query_Ble_Config(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].bleConfigData))
  1788. {
  1789. ShmSysConfigAndInfo->SysConfig.Bluetooth.isLogin = ShmCharger->gun_info[gun_index].bleConfigData.isLogin;
  1790. ShmSysConfigAndInfo->SysConfig.Bluetooth.isRequestStart = ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart;
  1791. ShmSysConfigAndInfo->SysConfig.Bluetooth.isRequestStop = ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop;
  1792. failCount[gun_index] = 0;
  1793. }
  1794. else
  1795. {
  1796. DEBUG_WARN("MCU-%d get ble config fail...%d\n", gun_index, failCount[gun_index]);
  1797. if(failCount[gun_index]<USHRT_MAX)
  1798. failCount[gun_index]++;
  1799. else
  1800. failCount[gun_index] = FAIL_SPEC_COMM;
  1801. }
  1802. //===============================
  1803. // Case 6 : Query primary MCU ble login id
  1804. //===============================
  1805. if(Query_Ble_Central_ID(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].bleLoginCentralId))
  1806. {
  1807. memcpy(ShmSysConfigAndInfo->SysConfig.Bluetooth.LoginCentralID, ShmCharger->gun_info[gun_index].bleLoginCentralId.id, sizeof ShmSysConfigAndInfo->SysConfig.Bluetooth.LoginCentralID);
  1808. failCount[gun_index] = 0;
  1809. }
  1810. else
  1811. {
  1812. DEBUG_WARN("MCU-%d get ble login central id fail...%d\n", gun_index, failCount[gun_index]);
  1813. if(failCount[gun_index]<USHRT_MAX)
  1814. failCount[gun_index]++;
  1815. else
  1816. failCount[gun_index] = FAIL_SPEC_COMM;
  1817. }
  1818. //===============================
  1819. // Case 7 : Config primary MCU reset request
  1820. //===============================
  1821. if((access("/sys/class/gpio/gpio116/value", F_OK)) != -1)
  1822. {
  1823. if(ShmCharger->gun_info[gun_index].mcuResetRequest.isMcuResetRequest == ON)
  1824. {
  1825. if(Config_AC_MCU_RESET_REQUEST(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].mcuResetRequest) == PASS)
  1826. {
  1827. DEBUG_INFO("**************************************************\n");
  1828. DEBUG_INFO("********* High priority polling : Case 7 *********\n");
  1829. DEBUG_INFO("**************************************************\n");
  1830. DEBUG_INFO("MCU-%d set MCU reset Request : %d\n", gun_index, ShmCharger->gun_info[gun_index].mcuResetRequest.isMcuResetRequest);
  1831. ShmCharger->gun_info[gun_index].mcuResetRequest.isMcuResetRequest = OFF;
  1832. failCount[gun_index] = 0;
  1833. }
  1834. else
  1835. {
  1836. DEBUG_WARN("MCU-%d get MCU reset fail...%d\n", gun_index, failCount[gun_index]);
  1837. if(failCount[gun_index]<USHRT_MAX)
  1838. failCount[gun_index]++;
  1839. else
  1840. failCount[gun_index] = FAIL_SPEC_COMM;
  1841. }
  1842. }
  1843. }
  1844. //==========================================================
  1845. // High priority polling log print out
  1846. //==========================================================
  1847. if((current_timestamp() - tsPrintLog[PRIORITY_HIGH]) > (ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest?3000:6000))
  1848. {
  1849. tsPrintLog[PRIORITY_HIGH] = current_timestamp();
  1850. //===============================
  1851. // Config primary MCU LED
  1852. //===============================
  1853. DEBUG_INFO("**************************************************\n");
  1854. DEBUG_INFO("********* High priority polling : Case 1 *********\n");
  1855. DEBUG_INFO("**************************************************\n");
  1856. DEBUG_INFO("MCU-%d set Led mode : %d\n",gun_index, ShmCharger->gun_info[gun_index].primaryMcuLed.mode);
  1857. DEBUG_INFO("MCU-%d set Alarm code : %x\n",gun_index, ShmCharger->gun_info[gun_index].primaryMcuLed.alarm_code);
  1858. //===============================
  1859. // Config primary Legacy request
  1860. //===============================
  1861. DEBUG_INFO("**************************************************\n");
  1862. DEBUG_INFO("********* High priority polling : Case 2 *********\n");
  1863. DEBUG_INFO("**************************************************\n");
  1864. DEBUG_INFO("MCU-%d set relay request : %d\n", gun_index, ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest);
  1865. //===============================
  1866. // Query primary MCU status
  1867. //===============================
  1868. DEBUG_INFO("**************************************************\n");
  1869. DEBUG_INFO("********* High priority polling : Case 3 *********\n");
  1870. DEBUG_INFO("**************************************************\n");
  1871. DEBUG_INFO("MCU-%d get Pilot State : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.cp_state);
  1872. DEBUG_INFO("MCU-%d get Pilot Duty : %.2f\n", gun_index, (float)ShmCharger->gun_info[gun_index].primaryMcuState.current_limit);
  1873. DEBUG_INFO("MCU-%d get Pilot Voltage Positive : %.2f\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_positive);
  1874. DEBUG_INFO("MCU-%d get Pilot Voltage Negative : %.2f\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_negtive);
  1875. DEBUG_INFO("MCU-%d get Relay State : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.relay_state);
  1876. DEBUG_INFO("MCU-%d get Rating Current : %.2f\n", gun_index, (float)ShmCharger->gun_info[gun_index].primaryMcuState.rating_current);
  1877. DEBUG_INFO("MCU-%d get Rotary switch : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch);
  1878. /*
  1879. DEBUG_INFO("MCU-%d get Locker State : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.locker_state);
  1880. DEBUG_INFO("MCU-%d get Shutter State : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.shutter_state);
  1881. DEBUG_INFO("MCU-%d get Meter State : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.meter_state);
  1882. DEBUG_INFO("MCU-%d get PP State : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.pp_state);
  1883. */
  1884. //===============================
  1885. // Query primary MCU Alarm code
  1886. //===============================
  1887. if((ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode>0))
  1888. {
  1889. DEBUG_INFO("**************************************************\n");
  1890. DEBUG_INFO("********* High priority polling : Case 4 *********\n");
  1891. DEBUG_INFO("**************************************************\n");
  1892. DEBUG_INFO("MCU-%d get OVP_L1 : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OVP_L1);
  1893. DEBUG_INFO("MCU-%d get UVP_L1 : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.UVP_L1);
  1894. DEBUG_INFO("MCU-%d get OCP_L1 : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OCP_L1);
  1895. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  1896. {
  1897. DEBUG_INFO("MCU-%d get OVP_L2 : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OVP_L2);
  1898. DEBUG_INFO("MCU-%d get UVP_L2 : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.UVP_L2);
  1899. DEBUG_INFO("MCU-%d get OCP_L2 : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OCP_L2);
  1900. DEBUG_INFO("MCU-%d get OVP_L3 : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OVP_L3);
  1901. DEBUG_INFO("MCU-%d get UVP_L3 : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.UVP_L3);
  1902. DEBUG_INFO("MCU-%d get OCP_L3 : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OCP_L3);
  1903. }
  1904. DEBUG_INFO("MCU-%d get OTP : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OTP);
  1905. DEBUG_INFO("MCU-%d get gmi_fault : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.gmi_fault);
  1906. DEBUG_INFO("MCU-%d get cp_fault : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.cp_fault);
  1907. DEBUG_INFO("MCU-%d get ac_leak : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.ac_leak);
  1908. DEBUG_INFO("MCU-%d get dc_leak : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.dc_leak);
  1909. DEBUG_INFO("MCU-%d get mcu_selftest_fail : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.mcu_selftest_fail);
  1910. DEBUG_INFO("MCU-%d get handshaking_timeout : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.handshaking_timeout);
  1911. DEBUG_INFO("MCU-%d get emergency_stop : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.emergency_stop);
  1912. DEBUG_INFO("MCU-%d get relay_welding : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.relay_welding);
  1913. DEBUG_INFO("MCU-%d get leak_module_fail : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.leak_module_fail);
  1914. DEBUG_INFO("MCU-%d get shutter_fault : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.shutter_fault);
  1915. DEBUG_INFO("MCU-%d get locker_fault : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.locker_fault);
  1916. DEBUG_INFO("MCU-%d get power_drop : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.power_drop);
  1917. DEBUG_INFO("MCU-%d get rotate_switch_fault : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.rotate_switch_fault);
  1918. DEBUG_INFO("MCU-%d get short_circuit_L1 : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.short_circuit_L1);
  1919. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  1920. {
  1921. DEBUG_INFO("MCU-%d get short_circuit_L2 : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.short_circuit_L2);
  1922. DEBUG_INFO("MCU-%d get short_circuit_L3 : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.short_circuit_L3);
  1923. }
  1924. DEBUG_INFO("MCU-%d get relay_drive_fault : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.relay_drive_fault);
  1925. DEBUG_INFO("MCU-%d get InputAlarmCode : %x\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode);
  1926. }
  1927. //===============================
  1928. // Query primary MCU BLE config
  1929. //===============================
  1930. if(ShmCharger->gun_info[gun_index].bleConfigData.isLogin == ON)
  1931. {
  1932. DEBUG_INFO("**************************************************\n");
  1933. DEBUG_INFO("********* High priority polling : Case 5 *********\n");
  1934. DEBUG_INFO("**************************************************\n");
  1935. DEBUG_INFO("MCU-%d get isUserLogin : %d\n", gun_index, ShmCharger->gun_info[gun_index].bleConfigData.isLogin);
  1936. DEBUG_INFO("MCU-%d get isRequestStartCharger : %d\n", gun_index, ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart);
  1937. DEBUG_INFO("MCU-%d get isRequestStopCharger : %d\n", gun_index, ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop);
  1938. }
  1939. //===============================
  1940. // Query primary MCU ble login id
  1941. //===============================
  1942. if(strcmp((char *)&ShmCharger->gun_info[gun_index].bleLoginCentralId.id,"") != 0)
  1943. {
  1944. DEBUG_INFO("**************************************************\n");
  1945. DEBUG_INFO("********* High priority polling : Case 6 *********\n");
  1946. DEBUG_INFO("**************************************************\n");
  1947. DEBUG_INFO("MCU-%d get ble central id : %s\n", gun_index, ShmCharger->gun_info[gun_index].bleLoginCentralId.id);
  1948. }
  1949. }
  1950. }
  1951. else
  1952. {
  1953. /*
  1954. * Normal priority communication
  1955. */
  1956. switch(stepIndex)
  1957. {
  1958. case 1:
  1959. //===============================
  1960. // Query present input voltage
  1961. //===============================
  1962. if(Query_Present_InputVoltage(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].inputVoltage) == PASS)
  1963. {
  1964. ShmSysConfigAndInfo->SysInfo.InputVoltageR = ShmCharger->gun_info[gun_index].inputVoltage.L1N_L12;
  1965. ShmSysConfigAndInfo->SysInfo.InputVoltageS = ShmCharger->gun_info[gun_index].inputVoltage.L2N_L23;
  1966. ShmSysConfigAndInfo->SysInfo.InputVoltageT = ShmCharger->gun_info[gun_index].inputVoltage.L3N_L31;
  1967. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltage = ShmCharger->gun_info[gun_index].inputVoltage.L1N_L12;
  1968. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltageL2 = ShmCharger->gun_info[gun_index].inputVoltage.L2N_L23;
  1969. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltageL3 = ShmCharger->gun_info[gun_index].inputVoltage.L3N_L31;
  1970. ShmCharger->gun_info[gun_index].acCcsInfo.GridVoltage[0] = ShmSysConfigAndInfo->SysInfo.InputVoltageR;
  1971. ShmCharger->gun_info[gun_index].acCcsInfo.GridVoltage[1] = ShmSysConfigAndInfo->SysInfo.InputVoltageS;
  1972. ShmCharger->gun_info[gun_index].acCcsInfo.GridVoltage[2] = ShmSysConfigAndInfo->SysInfo.InputVoltageT;
  1973. failCount[gun_index] = 0;
  1974. }
  1975. else
  1976. {
  1977. DEBUG_WARN("MCU-%d get input voltage fail...%d\n", gun_index, failCount[gun_index]);
  1978. if(failCount[gun_index]<USHRT_MAX)
  1979. failCount[gun_index]++;
  1980. else
  1981. failCount[gun_index] = FAIL_SPEC_COMM;
  1982. }
  1983. break;
  1984. case 3:
  1985. //===============================
  1986. // Query present output current
  1987. //===============================
  1988. if(Query_Present_OutputCurrent(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].outputCurrent) == PASS)
  1989. {
  1990. #ifndef SIMULATION
  1991. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent = (float)ShmCharger->gun_info[gun_index].outputCurrent.L1N_L12[0];
  1992. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrentL2 = (float)ShmCharger->gun_info[gun_index].outputCurrent.L2N_L23[0];
  1993. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrentL3 = (float)ShmCharger->gun_info[gun_index].outputCurrent.L3N_L31[0];
  1994. ShmCharger->gun_info[gun_index].acCcsInfo.EVSEPresentCurrent[0] = (float)ShmCharger->gun_info[gun_index].outputCurrent.L1N_L12[0];
  1995. ShmCharger->gun_info[gun_index].acCcsInfo.EVSEPresentCurrent[1] = (float)ShmCharger->gun_info[gun_index].outputCurrent.L1N_L12[0];
  1996. ShmCharger->gun_info[gun_index].acCcsInfo.EVSEPresentCurrent[2] = (float)ShmCharger->gun_info[gun_index].outputCurrent.L1N_L12[0];
  1997. #else //SIMULATION
  1998. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent = (float)(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus==SYS_MODE_CHARGING?(((rand()%10)+((ShmCharger->gun_info[gun_index].targetCurrent*10)-5))/10.0):0) ;
  1999. #endif //SIMULATION
  2000. failCount[gun_index] = 0;
  2001. }
  2002. else
  2003. {
  2004. DEBUG_WARN("MCU-%d get output current fail...%d\n", gun_index, failCount[gun_index]);
  2005. if(failCount[gun_index]<USHRT_MAX)
  2006. failCount[gun_index]++;
  2007. else
  2008. failCount[gun_index] = FAIL_SPEC_COMM;
  2009. }
  2010. break;
  2011. case 5:
  2012. //===============================
  2013. // Query gun plug-in times
  2014. //===============================
  2015. if(Query_AC_GUN_PLUGIN_TIMES(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].gunPluginTimes) == PASS)
  2016. {
  2017. ShmSysConfigAndInfo->SysConfig.AcPlugInTimes = ((long)ShmCharger->gun_info[gun_index].gunPluginTimes.GunPluginTimes & 0xFFFF);
  2018. failCount[gun_index] = 0;
  2019. }
  2020. else
  2021. {
  2022. DEBUG_WARN("MCU-%d get gun plugin times fail...%d\n", gun_index, failCount[gun_index]);
  2023. if(failCount[gun_index]<USHRT_MAX)
  2024. failCount[gun_index]++;
  2025. else
  2026. failCount[gun_index] = FAIL_SPEC_COMM;
  2027. }
  2028. break;
  2029. case 7:
  2030. //===============================
  2031. // Query temperature
  2032. //===============================
  2033. if(Query_Temperature(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].temperature) == PASS)
  2034. {
  2035. ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp = ShmCharger->gun_info[gun_index].temperature.point[0];
  2036. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ConnectorTemp = ShmCharger->gun_info[gun_index].temperature.point[0];
  2037. failCount[gun_index] = 0;
  2038. }
  2039. else
  2040. {
  2041. DEBUG_WARN("MCU-%d get temperature fail...%d\n", gun_index, failCount[gun_index]);
  2042. if(failCount[gun_index]<USHRT_MAX)
  2043. failCount[gun_index]++;
  2044. else
  2045. failCount[gun_index] = FAIL_SPEC_COMM;
  2046. }
  2047. break;
  2048. case 9:
  2049. //===============================
  2050. // Config primary set CP PWN duty
  2051. //===============================
  2052. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == ON)
  2053. {
  2054. if(Config_AC_MaxCurrent_And_CpPwmDuty(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty))
  2055. {
  2056. failCount[gun_index] = 0;
  2057. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = OFF;
  2058. }
  2059. else
  2060. {
  2061. DEBUG_WARN("MCU-%d set cp pwn duty fail...%d\n", gun_index, failCount[gun_index]);
  2062. if(failCount[gun_index]<USHRT_MAX)
  2063. failCount[gun_index]++;
  2064. else
  2065. failCount[gun_index] = FAIL_SPEC_COMM;
  2066. }
  2067. }
  2068. break;
  2069. case 11:
  2070. //===============================
  2071. // Query RTC
  2072. //===============================
  2073. if(Query_RTC(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].rtc))
  2074. {
  2075. struct timeb csuTime, mcuTime;
  2076. struct tm *tmCSU;
  2077. struct tm tmMcu;
  2078. ftime(&csuTime);
  2079. tmCSU = localtime(&csuTime.time);
  2080. tmMcu.tm_year = ShmCharger->gun_info[gun_index].rtc.year-1900;
  2081. tmMcu.tm_mon = ShmCharger->gun_info[gun_index].rtc.month-1;
  2082. tmMcu.tm_mday = ShmCharger->gun_info[gun_index].rtc.day;
  2083. tmMcu.tm_hour = ShmCharger->gun_info[gun_index].rtc.hour;
  2084. tmMcu.tm_min = ShmCharger->gun_info[gun_index].rtc.min;
  2085. tmMcu.tm_sec = ShmCharger->gun_info[gun_index].rtc.sec;
  2086. mcuTime.time = mktime(&tmMcu);
  2087. if(ShmCharger->gun_info[gun_index].bleConfigData.isLogin && !ShmOCPP16Data->OcppConnStatus)
  2088. {
  2089. if(abs(DiffTimeb(csuTime, mcuTime)) > 10000)
  2090. {
  2091. char cmdBuf[128];
  2092. sprintf(cmdBuf, "date -u -s \"%04d-%02d-%02d %02d:%02d:%02d\"", ShmCharger->gun_info[gun_index].rtc.year,
  2093. ShmCharger->gun_info[gun_index].rtc.month,
  2094. ShmCharger->gun_info[gun_index].rtc.day,
  2095. ShmCharger->gun_info[gun_index].rtc.hour,
  2096. ShmCharger->gun_info[gun_index].rtc.min,
  2097. ShmCharger->gun_info[gun_index].rtc.sec);
  2098. system(cmdBuf);
  2099. system("hwclock -w -u");
  2100. system("hwclock -s");
  2101. }
  2102. }
  2103. else
  2104. {
  2105. if(abs(DiffTimeb(csuTime, mcuTime)) > 10000)
  2106. {
  2107. ShmCharger->gun_info[gun_index].rtc.year = tmCSU->tm_year+1900;
  2108. ShmCharger->gun_info[gun_index].rtc.month = tmCSU->tm_mon+1;
  2109. ShmCharger->gun_info[gun_index].rtc.day = tmCSU->tm_mday;
  2110. ShmCharger->gun_info[gun_index].rtc.hour = tmCSU->tm_hour;
  2111. ShmCharger->gun_info[gun_index].rtc.min = tmCSU->tm_min;
  2112. ShmCharger->gun_info[gun_index].rtc.sec = tmCSU->tm_sec;
  2113. Config_RTC(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].rtc);
  2114. }
  2115. }
  2116. failCount[gun_index] = 0;
  2117. }
  2118. else
  2119. {
  2120. DEBUG_WARN("MCU-%d get rtc fail...%d\n", gun_index, failCount[gun_index]);
  2121. if(failCount[gun_index]<USHRT_MAX)
  2122. failCount[gun_index]++;
  2123. else
  2124. failCount[gun_index] = FAIL_SPEC_COMM;
  2125. }
  2126. break;
  2127. case 13:
  2128. //===============================
  2129. // Query primary MCU power consumption
  2130. //===============================
  2131. if(Query_Power_Consumption(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].powerConsumptionTotal, &ShmCharger->gun_info[gun_index].powerConsumption[0], &ShmCharger->gun_info[gun_index].powerConsumption[1], &ShmCharger->gun_info[gun_index].powerConsumption[2]))
  2132. {
  2133. failCount[gun_index] = 0;
  2134. }
  2135. else
  2136. {
  2137. DEBUG_WARN("MCU-%d get power consumption fail...%d\n", gun_index, failCount[gun_index]);
  2138. if(failCount[gun_index]<USHRT_MAX)
  2139. failCount[gun_index]++;
  2140. else
  2141. failCount[gun_index] = FAIL_SPEC_COMM;
  2142. }
  2143. break;
  2144. case 15:
  2145. //===============================
  2146. // Upgrade MCU
  2147. //===============================
  2148. if(ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq)
  2149. {
  2150. DEBUG_INFO("==================================================\n");
  2151. DEBUG_INFO("======== Normal priority polling : Case 15 =======\n");
  2152. DEBUG_INFO("==================================================\n");
  2153. unsigned char cmd[512];
  2154. if(Upgrade_UART(Uart1Fd, AC_WALLMOUNT_CONTROLLER, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), ShmCharger->fwUpgradeInfo.location, ShmCharger->fwUpgradeInfo.modelName))
  2155. {
  2156. DEBUG_INFO("MCU-%d upgrade firmware OK...%s\n", gun_index, ShmCharger->gun_info[gun_index].ver.Version_FW);
  2157. sleep(20);
  2158. ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq = OFF;
  2159. failCount[gun_index] = 0;
  2160. }
  2161. else
  2162. {
  2163. DEBUG_WARN("MCU-%d upgrade firmware fail...%d\n", gun_index, failCount[gun_index]);
  2164. if(failCount[gun_index]<USHRT_MAX)
  2165. failCount[gun_index]++;
  2166. else
  2167. failCount[gun_index] = FAIL_SPEC_COMM;
  2168. }
  2169. sprintf((char*)cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2170. system((char*)cmd);
  2171. }
  2172. break;
  2173. case 17:
  2174. //===============================
  2175. // Config set breathe led timing
  2176. //===============================
  2177. if(ShmCharger->gun_info[gun_index].isSetBreatheLedTiming == ON)
  2178. {
  2179. if(Config_AC_Set_Breathe_Led_Timing(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].setBreatheLedTiming))
  2180. {
  2181. failCount[gun_index] = 0;
  2182. }
  2183. else
  2184. {
  2185. DEBUG_WARN("MCU-%d set breathe led timing fail...%d\n", gun_index, failCount[gun_index]);
  2186. if(failCount[gun_index]<USHRT_MAX)
  2187. failCount[gun_index]++;
  2188. else
  2189. failCount[gun_index] = FAIL_SPEC_COMM;
  2190. }
  2191. }
  2192. break;
  2193. case 19:
  2194. //===============================
  2195. // Config set led brightness
  2196. //===============================
  2197. if(ShmCharger->gun_info[gun_index].isSetLedBrightness == ON)
  2198. {
  2199. if(Config_AC_Set_Led_Brightness(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].setLedBrightness))
  2200. {
  2201. failCount[gun_index] = 0;
  2202. }
  2203. else
  2204. {
  2205. DEBUG_WARN("MCU-%d set led brightness fail...%d\n", gun_index, failCount[gun_index]);
  2206. if(failCount[gun_index]<USHRT_MAX)
  2207. failCount[gun_index]++;
  2208. else
  2209. failCount[gun_index] = FAIL_SPEC_COMM;
  2210. }
  2211. }
  2212. break;
  2213. case 21:
  2214. //===============================
  2215. // Query firmware version
  2216. //===============================
  2217. if(ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass != PASS)
  2218. {
  2219. DEBUG_INFO("==================================================\n");
  2220. DEBUG_INFO("======== Normal priority polling : Case 21-1======\n");
  2221. DEBUG_INFO("==================================================\n");
  2222. if(Query_FW_Ver(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].ver) == PASS)
  2223. {
  2224. DEBUG_INFO("MCU-%d get firmware version : %s\n", gun_index, ShmCharger->gun_info[gun_index].ver.Version_FW);
  2225. memcpy(ShmPrimaryMcuData->version, ShmCharger->gun_info[gun_index].ver.Version_FW, sizeof(ShmPrimaryMcuData->version));
  2226. ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass = PASS;
  2227. failCount[gun_index] = 0;
  2228. }
  2229. else
  2230. {
  2231. DEBUG_WARN("MCU-%d get firmware version fail...%d\n", gun_index, failCount[gun_index]);
  2232. if(failCount[gun_index]<USHRT_MAX)
  2233. failCount[gun_index]++;
  2234. else
  2235. failCount[gun_index] = FAIL_SPEC_COMM;
  2236. }
  2237. }
  2238. //===============================
  2239. // Config primary MCU serial number
  2240. //===============================
  2241. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetSerialNumberPass != PASS)
  2242. {
  2243. DEBUG_INFO("==================================================\n");
  2244. DEBUG_INFO("======== Normal priority polling : Case 21-2======\n");
  2245. DEBUG_INFO("==================================================\n");
  2246. memcpy(ShmCharger->evseId.serial_number, ShmSysConfigAndInfo->SysConfig.SerialNumber, ARRAY_SIZE(ShmCharger->evseId.serial_number));
  2247. if(Config_Serial_Number(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->evseId))
  2248. {
  2249. DEBUG_INFO("MCU-%d set serial number : %.12s\n",gun_index,ShmCharger->evseId.serial_number);
  2250. ShmCharger->gun_info[gun_index].mcuFlag.isSetSerialNumberPass = PASS;
  2251. failCount[gun_index] = 0;
  2252. }
  2253. else
  2254. {
  2255. DEBUG_WARN("MCU-%d set serial number fail...%d\n", gun_index, failCount[gun_index]);
  2256. if(failCount[gun_index]<USHRT_MAX)
  2257. failCount[gun_index]++;
  2258. else
  2259. failCount[gun_index] = FAIL_SPEC_COMM;
  2260. }
  2261. }
  2262. //===============================
  2263. // Config primary MCU model name
  2264. //===============================
  2265. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetModelNamePass != PASS)
  2266. {
  2267. DEBUG_INFO("==================================================\n");
  2268. DEBUG_INFO("======== Normal priority polling : Case 21-3======\n");
  2269. DEBUG_INFO("==================================================\n");
  2270. memcpy(ShmCharger->evseId.model_name, ShmSysConfigAndInfo->SysConfig.ModelName, ARRAY_SIZE(ShmCharger->evseId.model_name));
  2271. if(Config_Model_Name(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->evseId))
  2272. {
  2273. DEBUG_INFO("MCU-%d set model name : %.14s\n",gun_index,ShmCharger->evseId.model_name);
  2274. ShmCharger->gun_info[gun_index].mcuFlag.isSetModelNamePass = PASS;
  2275. failCount[gun_index] = 0;
  2276. }
  2277. else
  2278. {
  2279. DEBUG_WARN("MCU-%d set model name fail...%d\n", gun_index, failCount[gun_index]);
  2280. if(failCount[gun_index]<USHRT_MAX)
  2281. failCount[gun_index]++;
  2282. else
  2283. failCount[gun_index] = FAIL_SPEC_COMM;
  2284. }
  2285. }
  2286. break;
  2287. default:
  2288. stepIndex = 0;
  2289. break;
  2290. }
  2291. //==========================================================
  2292. // Low priority polling log print out
  2293. //==========================================================
  2294. if((current_timestamp() - tsPrintLog[PRIORITY_LOW]) > (ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest?3000:6000))
  2295. {
  2296. tsPrintLog[PRIORITY_LOW] = current_timestamp();
  2297. switch(logIndex)
  2298. {
  2299. case 1:
  2300. DEBUG_INFO("==================================================\n");
  2301. DEBUG_INFO("======== Normal priority polling : Case 3 ========\n");
  2302. DEBUG_INFO("==================================================\n");
  2303. DEBUG_INFO("MCU-%d get output current L1: %f\n", gun_index, (float)ShmCharger->gun_info[gun_index].outputCurrent.L1N_L12[0]);
  2304. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  2305. {
  2306. DEBUG_INFO("MCU-%d get output current L2: %f\n", gun_index, (float)ShmCharger->gun_info[gun_index].outputCurrent.L2N_L23[0]);
  2307. DEBUG_INFO("MCU-%d get output current L3: %f\n", gun_index, (float)ShmCharger->gun_info[gun_index].outputCurrent.L3N_L31[0]);
  2308. }
  2309. break;
  2310. case 2:
  2311. DEBUG_INFO("==================================================\n");
  2312. DEBUG_INFO("======== Normal priority polling : Case 5 ========\n");
  2313. DEBUG_INFO("==================================================\n");
  2314. DEBUG_INFO("MCU-%d get gun plugin times : %ld\n", gun_index, (long)ShmCharger->gun_info[gun_index].gunPluginTimes.GunPluginTimes);
  2315. break;
  2316. case 3:
  2317. DEBUG_INFO("==================================================\n");
  2318. DEBUG_INFO("======== Normal priority polling : Case 7 ========\n");
  2319. DEBUG_INFO("==================================================\n");
  2320. DEBUG_INFO("MCU-%d get temperature : %d\n", gun_index, ShmCharger->gun_info[gun_index].temperature.point[0]);
  2321. break;
  2322. case 4:
  2323. DEBUG_INFO("==================================================\n");
  2324. DEBUG_INFO("======== Normal priority polling : Case 9 ========\n");
  2325. DEBUG_INFO("==================================================\n");
  2326. DEBUG_INFO("MCU-%d set cp pwn duty : %d\n",gun_index, ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current);
  2327. break;
  2328. case 5:
  2329. DEBUG_INFO("==================================================\n");
  2330. DEBUG_INFO("======== Normal priority polling : Case 11 =======\n");
  2331. DEBUG_INFO("==================================================\n");
  2332. if(ShmCharger->gun_info[gun_index].bleConfigData.isLogin && !ShmOCPP16Data->OcppConnStatus)
  2333. {
  2334. DEBUG_INFO("Sync from MCU-%d rtc OK...%04d-%02d-%02d %02d:%02d:%02d\n", gun_index,
  2335. ShmCharger->gun_info[gun_index].rtc.year,
  2336. ShmCharger->gun_info[gun_index].rtc.month,
  2337. ShmCharger->gun_info[gun_index].rtc.day,
  2338. ShmCharger->gun_info[gun_index].rtc.hour,
  2339. ShmCharger->gun_info[gun_index].rtc.min,
  2340. ShmCharger->gun_info[gun_index].rtc.sec);
  2341. }
  2342. else
  2343. {
  2344. DEBUG_INFO("MCU-%d set rtc OK...%04d-%02d-%02d %02d:%02d:%02d\n", gun_index,
  2345. ShmCharger->gun_info[gun_index].rtc.year,
  2346. ShmCharger->gun_info[gun_index].rtc.month,
  2347. ShmCharger->gun_info[gun_index].rtc.day,
  2348. ShmCharger->gun_info[gun_index].rtc.hour,
  2349. ShmCharger->gun_info[gun_index].rtc.min,
  2350. ShmCharger->gun_info[gun_index].rtc.sec);
  2351. }
  2352. break;
  2353. case 6:
  2354. DEBUG_INFO("==================================================\n");
  2355. DEBUG_INFO("======== Normal priority polling : Case 13 =======\n");
  2356. DEBUG_INFO("==================================================\n");
  2357. DEBUG_INFO("MCU-%d get total power consumption : %f kWh\n",gun_index, ((float)ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/100));
  2358. DEBUG_INFO("MCU-%d get L1N_L12 power consumption : %f kWh\n",gun_index, ((float)ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption/100));
  2359. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  2360. {
  2361. DEBUG_INFO("MCU-%d get L2N_L23 power consumption : %f kWh\n",gun_index, ((float)ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption/100));
  2362. DEBUG_INFO("MCU-%d get L3N_L31 power consumption : %f kWh\n",gun_index, ((float)ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption/100));
  2363. }
  2364. break;
  2365. case 7:
  2366. if(ShmCharger->gun_info[gun_index].isSetBreatheLedTiming == ON)
  2367. {
  2368. DEBUG_INFO("==================================================\n");
  2369. DEBUG_INFO("======== Normal priority polling : Case 17 =======\n");
  2370. DEBUG_INFO("==================================================\n");
  2371. DEBUG_INFO("MCU-%d set breathe led timing : Authed Fade in [%ld].\n", gun_index, (long)ShmCharger->gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Authed_Fade_In);
  2372. DEBUG_INFO("MCU-%d set breathe led timing : Authed Fade out [%ld].\n", gun_index, (long)ShmCharger->gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Authed_Fade_Out);
  2373. DEBUG_INFO("MCU-%d set breathe led timing : Charging Fade in [%ld].\n", gun_index, (long)ShmCharger->gun_info[gun_index].setBreatheLedTiming.Set_Led_Action_Chaging_Fade_In);
  2374. DEBUG_INFO("MCU-%d set breathe led timing : Charging Fade out [%ld].\n", gun_index, (long)ShmCharger->gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Chaging_Fade_Out);
  2375. DEBUG_INFO("MCU-%d set breathe led timing : Connected Fade in [%ld].\n", gun_index, (long)ShmCharger->gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Connected_Fade_In);
  2376. DEBUG_INFO("MCU-%d set breathe led timing : Connected Fade out [%ld].\n", gun_index, (long)ShmCharger->gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Connected_Fade_Out);
  2377. }
  2378. break;
  2379. case 8:
  2380. if(ShmCharger->gun_info[gun_index].isSetLedBrightness == ON)
  2381. {
  2382. DEBUG_INFO("==================================================\n");
  2383. DEBUG_INFO("======== Normal priority polling : Case 19 =======\n");
  2384. DEBUG_INFO("==================================================\n");
  2385. DEBUG_INFO("MCU-%d set led brightness Sector 01 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_1);
  2386. DEBUG_INFO("MCU-%d set led brightness Sector 02 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_2);
  2387. DEBUG_INFO("MCU-%d set led brightness Sector 03 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_3);
  2388. DEBUG_INFO("MCU-%d set led brightness Sector 04 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_4);
  2389. DEBUG_INFO("MCU-%d set led brightness Sector 05 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_5);
  2390. DEBUG_INFO("MCU-%d set led brightness Sector 06 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_6);
  2391. DEBUG_INFO("MCU-%d set led brightness Sector 07 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_7);
  2392. DEBUG_INFO("MCU-%d set led brightness Sector 08 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_8);
  2393. DEBUG_INFO("MCU-%d set led brightness Sector 09 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_9);
  2394. DEBUG_INFO("MCU-%d set led brightness Sector 10 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_10);
  2395. DEBUG_INFO("MCU-%d set led brightness Sector 11 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_11);
  2396. DEBUG_INFO("MCU-%d set led brightness Sector 12 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_12);
  2397. }
  2398. break;
  2399. default:
  2400. DEBUG_INFO("==================================================\n");
  2401. DEBUG_INFO("======== Normal priority polling : Case 1 ========\n");
  2402. DEBUG_INFO("==================================================\n");
  2403. DEBUG_INFO("MCU-%d get input voltage L1: %f\n", gun_index, (float)ShmCharger->gun_info[gun_index].inputVoltage.L1N_L12);
  2404. DEBUG_INFO("MCU-%d get PresentChargingVoltage L1: %.2f\n", gun_index, (float)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltage);
  2405. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  2406. {
  2407. DEBUG_INFO("MCU-%d get input voltage L2: %f\n", gun_index, (float)ShmCharger->gun_info[gun_index].inputVoltage.L2N_L23);
  2408. DEBUG_INFO("MCU-%d get PresentChargingVoltage L2: %.2f\n", gun_index, (float)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltageL2);
  2409. DEBUG_INFO("MCU-%d get input voltage L3: %f\n", gun_index, (float)ShmCharger->gun_info[gun_index].inputVoltage.L3N_L31);
  2410. DEBUG_INFO("MCU-%d get PresentChargingVoltage L3: %.2f\n", gun_index, (float)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltageL3);
  2411. }
  2412. logIndex = 0;
  2413. break;
  2414. }
  2415. logIndex++;
  2416. }
  2417. }
  2418. stepIndex++;
  2419. //===============================
  2420. // Communication fail check
  2421. //===============================
  2422. if(failCount[gun_index] >= FAIL_SPEC_COMM)
  2423. {
  2424. if((0 <= failCount[gun_index]%FAIL_SPEC_COMM) && (failCount[gun_index]%FAIL_SPEC_COMM < 10))
  2425. sleep(10);
  2426. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.comm_timeout == OFF)
  2427. {
  2428. DEBUG_ERROR("Primary MCU-%d communication fault", gun_index);
  2429. ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.comm_timeout = ON;
  2430. }
  2431. }
  2432. else
  2433. {
  2434. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.comm_timeout == ON)
  2435. {
  2436. DEBUG_ERROR("Primary MCU-%d communication recover", gun_index);
  2437. ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.comm_timeout = OFF;
  2438. }
  2439. }
  2440. usleep(100000);
  2441. }
  2442. }
  2443. return FAIL;
  2444. }