Module_InternalComm.c 96 KB

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