freertos.c 423 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * File Name : freertos.c
  5. * Description : Code for freertos applications
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under Ultimate Liberty license
  13. * SLA0044, the "License"; You may not use this file except in compliance with
  14. * the License. You may obtain a copy of the License at:
  15. * www.st.com/SLA0044
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "FreeRTOS.h"
  22. #include "task.h"
  23. #include "main.h"
  24. #include "cmsis_os.h"
  25. /* Private includes ----------------------------------------------------------*/
  26. /* USER CODE BEGIN Includes */
  27. #include "adc.h"
  28. #include "crc.h"
  29. #include "flash_if.h"
  30. #include "gpio.h"
  31. #include "iwdg.h"
  32. #include "rtc.h"
  33. #include "tim.h"
  34. #include "usart.h"
  35. #include "cp_detection.h"
  36. #include "W25QXX.h"
  37. #include "usbd_cdc_if.h"
  38. #include "time.h"
  39. /* USER CODE END Includes */
  40. /* Private typedef -----------------------------------------------------------*/
  41. /* USER CODE BEGIN PTD */
  42. /* USER CODE END PTD */
  43. /* Private define ------------------------------------------------------------*/
  44. /* USER CODE BEGIN PD */
  45. /* USER CODE END PD */
  46. /* Private macro -------------------------------------------------------------*/
  47. /* USER CODE BEGIN PM */
  48. /* USER CODE END PM */
  49. /* Private variables ---------------------------------------------------------*/
  50. /* USER CODE BEGIN Variables */
  51. uint32_t alarm_record_code[32] =
  52. {
  53. 0x00012200, // Input OVP
  54. 0x00012203, // Input UVP
  55. 0x00012216, // Output OCP
  56. 0x00012223, // Over temperature
  57. 0x00012255, // Ground fault
  58. 0x00023703, // Pilot error
  59. 0x00012233, // AC CCID
  60. 0x00012233, // DC CCID
  61. 0x00012256, // MCU self test
  62. 0x00012258, // Hand shaking timeout
  63. 0x00012251, // Emergency stop
  64. 0x00011009, // Relay welding
  65. 0x00011032, // Leak module test fail
  66. 0x00011034, // shutter fault
  67. 0x00012212, // L1 Power drop
  68. 0x00012262, // Circuit short
  69. 0x00011033, // Maximum Output Current setup error
  70. 0x00011010, // AC output relay driving fault
  71. 0x00011035, // Ble module broken
  72. 0x00000000,
  73. 0x00000000,
  74. 0x00000000,
  75. 0x00000000,
  76. 0x00000000,
  77. 0x00000000,
  78. 0x00000000,
  79. 0x00000000,
  80. 0x00000000,
  81. 0x00000000,
  82. 0x00000000,
  83. 0x00000000
  84. };
  85. uint32_t binCRCTarget, binCRCCal;
  86. __IO uint32_t flashdestination;
  87. __IO uint32_t newdestination;
  88. volatile unsigned long ulHighFrequencyTimerTicks;
  89. sCPModuleResult_t CpDetectionResult;
  90. #ifndef FUNC_CP_ADC_MODIFY
  91. sCPVoltageBoundary_t CpBoundary = {
  92. CP_P12V_HIGH,
  93. CP_P12V_LOW,
  94. CP_P9V_HIGH,
  95. CP_P9V_LOW,
  96. CP_P6V_HIGH,
  97. CP_P6V_LOW,
  98. CP_P3V_HIGH,
  99. CP_P3V_LOW,
  100. CP_0V_HIGH,
  101. CP_0V_LOW,
  102. CP_N12V_HIGH,
  103. CP_N12V_LOW,
  104. };
  105. sCPVoltageBoundary_t SpecDefBoundary = {
  106. CP_SPEC_P12V_HIGH,
  107. CP_SPEC_P12V_LOW,
  108. CP_SPEC_P9V_HIGH,
  109. CP_SPEC_P9V_LOW,
  110. CP_SPEC_P6V_HIGH,
  111. CP_SPEC_P6V_LOW,
  112. CP_SPEC_P3V_HIGH,
  113. CP_SPEC_P3V_LOW,
  114. CP_SPEC_0V_HIGH,
  115. CP_SPEC_0V_LOW,
  116. CP_SPEC_N12V_HIGH,
  117. CP_SPEC_N12V_LOW,
  118. };
  119. sCPVoltageHysteresis_t CpHysteresis = {
  120. HYSTERESIS_P12V_HIGH,
  121. HYSTERESIS_P12V_LOW,
  122. HYSTERESIS_P9V_HIGH,
  123. HYSTERESIS_P9V_LOW,
  124. HYSTERESIS_P6V_HIGH,
  125. HYSTERESIS_P6V_LOW,
  126. HYSTERESIS_P3V_HIGH,
  127. HYSTERESIS_P3V_LOW,
  128. HYSTERESIS_0V_HIGH,
  129. HYSTERESIS_0V_LOW,
  130. };
  131. #endif //FUNC_CP_ADC_MODIFY
  132. struct EVSE Charger;
  133. const unsigned char CharacterArray[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  134. uint32_t test_logA = 0 ;
  135. uint32_t test_logB = 0 ;
  136. extern I2C_HandleTypeDef hi2c2;
  137. /* USER CODE END Variables */
  138. #ifndef DISABLE_OS_ETH_TASK
  139. osThreadId ethTaskHandle;
  140. #endif
  141. osThreadId uartTaskHandle;
  142. osThreadId adcTaskHandle;
  143. osThreadId timeoutTaskHandle;
  144. osThreadId rfidTaskHandle;
  145. osThreadId led_speakerTaskHandle;
  146. osThreadId cpTaskHandle;
  147. osThreadId alarmTaskHandle;
  148. osThreadId bleTaskHandle;
  149. osThreadId memoryTaskHandle;
  150. osThreadId wifiTaskHandle;
  151. /* Private function prototypes -----------------------------------------------*/
  152. /* USER CODE BEGIN FunctionPrototypes */
  153. #ifdef FUNC_RS485_SLAVE
  154. uint8_t isValidCheckSum_IAP(uint8_t *RxBuf);
  155. #else
  156. uint8_t isValidCheckSum_IAP(void);
  157. #endif
  158. uint8_t isValidCheckSum_BLE(void);
  159. uint8_t isValidCheckSum_WIFI(void);
  160. void setChargerMode(uint8_t mode);
  161. uint8_t isModeChange(void);
  162. uint8_t isMode(uint8_t mode);
  163. uint8_t rfidCheckSumCal(uint8_t *data, uint8_t length);
  164. uint8_t Test_LeakModule(void);
  165. uint8_t Valid_RFID_SN(void);
  166. void recordAlarmHis(void);
  167. uint8_t recordChargingHis(uint8_t isColdLoadPickUp, uint32_t statusCode);
  168. void setLedMotion(uint8_t action);
  169. void SetADCWDGBoundary( void);
  170. void getlastRecord(void);
  171. void getRotarySwitchSetting(void);
  172. void CLC_Corr_Gain_Par(uint16_t SpecData_H ,uint16_t SpecData_L ,uint16_t MCUData_H , uint16_t MCUData_L , float *GainA , float *GainB ) ;
  173. void CLC_Corr_Gain_Par2(uint16_t SpecData_H ,uint16_t SpecData_L ,uint16_t MCUData_H , uint16_t MCUData_L , float *GainA , float *GainB , uint8_t *PosorNeg ) ;
  174. uint8_t Array_data_Check ( uint8_t *s1, uint8_t *s2 ) ;
  175. void Relay_Control_Function (void) ;
  176. uint8_t WatchDogLeakRawDataCheck_Event(void) ;
  177. uint16_t getBrightnessDuty(uint16_t max_duty);
  178. void parseVersionInfoFromModelname(void);
  179. void HTK_GetBufferString(const u8* buf, const u8 len, char* szOutBuf);
  180. void HTK_PrintBufferString(const u8* buf, const u8 len);
  181. #ifdef ENABLE_PRINT_IMCP_MSG
  182. int CheckPrintImcpMsg(uint8_t MsgID);
  183. #endif
  184. #ifdef MODIFY_IMCP
  185. void UpdateCheckSum(uint8_t *Buf, uint16_t Len);
  186. #endif
  187. #ifdef MODIFY_COLD_LOAD_PICKUP_DELAY
  188. void ColdLoadPickup_Delay(const char* Description);
  189. #endif
  190. #ifdef FUNC_FORCE_RUN_CSU_MODE_WITH_DC_MODELNAME
  191. void Update_McuCtrlMode(void);
  192. #endif
  193. #ifdef MODIFY_DC_RS485_UPGRADE_ISSUE
  194. void RS485_TX_Enable(void);
  195. void RS485_TX_Disable(void);
  196. void Prefix_UartTX(uint8_t* TxBuf, uint8_t* RxBuf);
  197. void Postfix_UartTX(uint8_t* TxBuf, uint8_t* RxBuf);
  198. #endif
  199. /* USER CODE END FunctionPrototypes */
  200. #ifndef DISABLE_OS_ETH_TASK
  201. void StartEthTask(void const * argument);
  202. #endif
  203. void StartUartTask(void const * argument);
  204. void StartAdcTask(void const * argument);
  205. void StartTimeoutTask(void const * argument);
  206. void StartRfidTask(void const * argument);
  207. void StartLedSpeakerTask(void const * argument);
  208. void StartCpTask(void const * argument);
  209. void StartAlarmTask(void const * argument);
  210. void StartBleTask(void const * argument);
  211. void StartMemoryTask(void const * argument);
  212. void StartWifiTask(void const * argument);
  213. extern void MX_USB_DEVICE_Init(void);
  214. void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */
  215. /* GetIdleTaskMemory prototype (linked to static allocation support) */
  216. void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );
  217. /* Hook prototypes */
  218. void configureTimerForRunTimeStats(void);
  219. unsigned long getRunTimeCounterValue(void);
  220. /* USER CODE BEGIN 1 */
  221. /* Functions needed when configGENERATE_RUN_TIME_STATS is on */
  222. __weak void configureTimerForRunTimeStats(void)
  223. {
  224. ulHighFrequencyTimerTicks = 0ul;
  225. }
  226. __weak unsigned long getRunTimeCounterValue(void)
  227. {
  228. return ulHighFrequencyTimerTicks;
  229. }
  230. /* USER CODE END 1 */
  231. /* USER CODE BEGIN GET_IDLE_TASK_MEMORY */
  232. static StaticTask_t xIdleTaskTCBBuffer;
  233. static StackType_t xIdleStack[configMINIMAL_STACK_SIZE];
  234. void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
  235. {
  236. *ppxIdleTaskTCBBuffer = &xIdleTaskTCBBuffer;
  237. *ppxIdleTaskStackBuffer = &xIdleStack[0];
  238. *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
  239. /* place for user code */
  240. }
  241. /* USER CODE END GET_IDLE_TASK_MEMORY */
  242. /**
  243. * @brief FreeRTOS initialization
  244. * @param None
  245. * @retval None
  246. */
  247. void UpdateFirmwareVersion(void)
  248. {
  249. #ifdef FUNC_FW_VER_TRACE
  250. #if (FW_VER_TYPE == FW_VER_TYPE_DEVELOP)
  251. sprintf(Charger.Ver_FW, "D%4.2f.X0.0007.P0", FW_VER_NUM * 0.01);
  252. #elif (FW_VER_TYPE == FW_VER_TYPE_FORMAL)
  253. #ifdef FORCE_VER_WITH_T_PREFIX
  254. //---------------
  255. #ifdef FORCE_VER_WITH_F_PREFIX
  256. sprintf(Charger.Ver_FW, "F%4.2f.X0.0011.P0", FW_VER_NUM * 0.00001);
  257. #else
  258. sprintf(Charger.Ver_FW, "T%4.2f.X0.0011.P0", FW_VER_NUM * 0.00001);
  259. #endif
  260. //---------------
  261. //sprintf(Charger.Ver_FW, "T%4.2f.X0.0011.P0", FW_VER_NUM * 0.00001);
  262. #else //FORCE_VER_WITH_T_PREFIX
  263. sprintf(Charger.Ver_FW, "V%4.2f.X0.0007.P0", FW_VER_NUM * 0.00001);
  264. #endif //FORCE_VER_WITH_T_PREFIX
  265. #elif (FW_VER_TYPE == FW_VER_TYPE_TEST)
  266. sprintf(Charger.Ver_FW, "T%4.2f.X0.0007.P0", FW_VER_NUM * 0.00001);
  267. #endif
  268. #else //FUNC_FW_VER_TRACE
  269. //sprintf(Charger.Ver_FW, "V0.05.X0.0007.P0");
  270. //sprintf(Charger.Ver_FW, "V0.06.X0.0007.P0"); //20210611
  271. #endif //FUNC_FW_VER_TRACE
  272. #ifdef FUNC_VERSION_USE_NEW_SPEC_20240926
  273. //Indicate firmware belong which module board
  274. //L: LED board
  275. //R: Relay board
  276. //C: CHAdeMO board
  277. //G: GBT board
  278. //F: Fan board
  279. //D: DCM 407
  280. //A: AC main board
  281. //S: CCS board
  282. //M: CSU board
  283. Charger.Ver_FW[6] = 'A';
  284. //0: For formal release specification
  285. //1: For lab certificate specification
  286. #ifdef FUNC_VERSION_FOR_LAB_CERTIFICATE_SPEC
  287. Charger.Ver_FW[7] = '1';
  288. #else
  289. Charger.Ver_FW[7] = '0';
  290. #endif
  291. #endif //FUNC_VERSION_USE_NEW_SPEC_20240926
  292. }
  293. //---------------
  294. #ifdef FUNC_STM32_FLASH_INFO
  295. enum { STM32_UID_LEN = 12 }; //96 bits (12 bytes)
  296. typedef struct _STM32_Flash_Info
  297. {
  298. union
  299. {
  300. u8 m_UID[STM32_UID_LEN];
  301. u32 m_UIDw[3];
  302. };
  303. u32 m_DeviceID;
  304. u32 m_FlashSizeKB;
  305. }
  306. STM32_Flash_Info, *PSTM32_Flash_Info;
  307. void STM32_Flash_Info_Init(PSTM32_Flash_Info p)
  308. {
  309. memset(p, 0, sizeof(STM32_Flash_Info));
  310. }
  311. void STM32_Flash_Info_Read(PSTM32_Flash_Info p)
  312. {
  313. //UID
  314. memcpy(p->m_UID, (u8*)UID_BASE, STM32_UID_LEN);
  315. //Device ID
  316. p->m_DeviceID = (uint32_t)(READ_BIT(DBGMCU->IDCODE, DBGMCU_IDCODE_DEV_ID));
  317. //Flash size
  318. p->m_FlashSizeKB = (uint32_t)(READ_REG(*((uint32_t *)FLASHSIZE_BASE)) & 0xFFFF);
  319. }
  320. void STM32_Flash_Info_Disp(PSTM32_Flash_Info p)
  321. {
  322. XP("[FlashInfo]------------------------\r\n");
  323. //UID
  324. HTK_ByteArray2HexStr_XP("UID", p->m_UID, 0, STM32_UID_LEN);
  325. //Device ID
  326. XP("DeviceID: 0x%03X\r\n", p->m_DeviceID);
  327. {
  328. char *s = "DeviceFamily: ";
  329. if (p->m_DeviceID == 0x413)
  330. {
  331. XP("%sSTM32F40xxx/F41xxx\r\n", s);
  332. }
  333. else if (p->m_DeviceID == 0x419)
  334. {
  335. XP("%sSTM32F42xxx/F43xxx\r\n", s);
  336. }
  337. else
  338. {
  339. XP("%sUnknow\r\n", s);
  340. }
  341. }
  342. //Flash size
  343. XP("DeviceFlashSize: %d Kbytes\r\n", p->m_FlashSizeKB);
  344. }
  345. void STM32_Flash_Info_Proc(void)
  346. {
  347. PSTM32_Flash_Info p = HTK_Malloc(sizeof(STM32_Flash_Info));
  348. if (p != NULL)
  349. {
  350. STM32_Flash_Info_Init(p);
  351. STM32_Flash_Info_Read(p);
  352. STM32_Flash_Info_Disp(p);
  353. }
  354. else
  355. {
  356. XP("PSTM32_Flash_Info malloc NG\r\n");
  357. }
  358. }
  359. #endif //FUNC_STM32_FLASH_INFO
  360. #ifdef FUNC_STM32_FLASH_OPTION_BYTES
  361. typedef struct _STM32_Flash_OB
  362. {
  363. FLASH_OBProgramInitTypeDef m_SetOB;
  364. FLASH_OBProgramInitTypeDef m_CurOB;
  365. u8 m_nRST_STDBY: 1; //0: Reset generated when entering the Standby mode
  366. //1: No reset generated
  367. u8 m_nRST_STOP: 1; //0: Reset generated when entering the Stop mode
  368. //1: No reset generated
  369. u8 m_WDG_SW: 1; //0: Hardware independent watchdog
  370. //1: Software independent watchdog
  371. }
  372. STM32_Flash_OB, *PSTM32_Flash_OB;
  373. void STM32_Flash_OB_Init(PSTM32_Flash_OB p)
  374. {
  375. memset(p, 0, sizeof(STM32_Flash_OB));
  376. //Set default
  377. p->m_SetOB.OptionType = OPTIONBYTE_WRP /*| OPTIONBYTE_RDP*/ | OPTIONBYTE_USER | OPTIONBYTE_BOR;
  378. p->m_SetOB.WRPState = OB_WRPSTATE_ENABLE;
  379. p->m_SetOB.Banks = 0;
  380. p->m_SetOB.BORLevel = OB_BOR_LEVEL3;
  381. p->m_SetOB.USERConfig = OB_IWDG_SW | OB_STOP_NO_RST | OB_STDBY_NO_RST;
  382. //#ifdef FUNC_USE_STM32_SBSFU
  383. // //Sector_0~4: Write Protection; Sector_5~11: No Write Protection
  384. // p->m_SetOB.WRPSector = OB_WRP_SECTOR_0 | OB_WRP_SECTOR_1 | OB_WRP_SECTOR_2 | OB_WRP_SECTOR_3 | OB_WRP_SECTOR_4;
  385. // p->m_SetOB.RDPLevel = OB_RDP_LEVEL_1;
  386. //#else
  387. //Sector_0~11: No Write Protection
  388. p->m_SetOB.WRPSector = ~OB_WRP_SECTOR_All;
  389. p->m_SetOB.RDPLevel = OB_RDP_LEVEL_0;
  390. //#endif
  391. }
  392. void STM32_Flash_OB_Read(PSTM32_Flash_OB p)
  393. {
  394. __HAL_FLASH_PREFETCH_BUFFER_DISABLE();
  395. HAL_FLASHEx_OBGetConfig(&p->m_CurOB);
  396. __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
  397. p->m_nRST_STDBY = BIT_GET(p->m_CurOB.USERConfig, 7);
  398. p->m_nRST_STOP = BIT_GET(p->m_CurOB.USERConfig, 6);
  399. p->m_WDG_SW = BIT_GET(p->m_CurOB.USERConfig, 5);
  400. }
  401. void STM32_Flash_OB_WriteDefault(PSTM32_Flash_OB p)
  402. {
  403. XP("<Write default OB>\r\n");
  404. __HAL_FLASH_PREFETCH_BUFFER_DISABLE();
  405. HAL_FLASH_Unlock();
  406. HAL_FLASH_OB_Unlock();
  407. {
  408. {
  409. XP("<Set OB Default>\r\n");
  410. HAL_FLASHEx_OBProgram(&p->m_SetOB);
  411. HAL_FLASH_OB_Launch();
  412. }
  413. {
  414. FLASH_OBProgramInitTypeDef ob;
  415. memcpy(&ob, &p->m_SetOB, sizeof(p->m_SetOB));
  416. ob.OptionType = OPTIONBYTE_WRP;
  417. ob.WRPState = OB_WRPSTATE_DISABLE;
  418. ob.WRPSector = (~p->m_SetOB.WRPSector) & OB_WRP_SECTOR_All;
  419. XP("<Set OB.WRPSector => Disable>\r\n");
  420. HAL_FLASHEx_OBProgram(&ob);
  421. HAL_FLASH_OB_Launch();
  422. }
  423. if (p->m_CurOB.RDPLevel == OB_RDP_LEVEL_0 && p->m_SetOB.RDPLevel == OB_RDP_LEVEL_1)
  424. {
  425. FLASH_OBProgramInitTypeDef ob;
  426. memcpy(&ob, &p->m_SetOB, sizeof(p->m_SetOB));
  427. ob.OptionType = OPTIONBYTE_RDP;
  428. XP("<Set OB.RDPLevel => Level 1> Please power off and then power on to restart !\r\n");
  429. HAL_FLASHEx_OBProgram(&ob);
  430. HAL_FLASH_OB_Launch();
  431. /* Set the OPTSTRT bit in OPTCR register */
  432. // XP("*(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= FLASH_OPTCR_OPTSTRT [BEG]\r\n");
  433. // *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= FLASH_OPTCR_OPTSTRT;
  434. // XP("*(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= FLASH_OPTCR_OPTSTRT [BEG]\r\n");
  435. //HAL_Delay(1000);
  436. //NVIC_SystemReset();
  437. }
  438. }
  439. HAL_FLASH_OB_Lock();
  440. HAL_FLASH_Lock();
  441. __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
  442. }
  443. void STM32_Flash_OB_Disp(PSTM32_Flash_OB p)
  444. {
  445. XP("[OB]-------------------------------\r\n");
  446. XP("OptionType: 0x%08X\r\n", p->m_CurOB.OptionType);
  447. XP("WRPState : 0x%08X\r\n", p->m_CurOB.WRPState);
  448. XP("WRPSector : 0x%08X\r\n", p->m_CurOB.WRPSector);
  449. {
  450. for (int i = 0; i < 12; i++)
  451. {
  452. XP("\tSector_%d: %s Protection\r\n", i, BIT_GET(p->m_CurOB.WRPSector, i) == 1 ? "No" : "Write");
  453. }
  454. }
  455. XP("Banks : 0x%08X\r\n", p->m_CurOB.Banks);
  456. XP("RDPLevel : 0x%08X (0: 0xAA, 1: 0x55, 2: 0xCC)\r\n", p->m_CurOB.RDPLevel);
  457. XP("BORLevel : 0x%08X (3: 0x00, 2: 0x04, 1: 0x08, OFF: 0x0C)\r\n", p->m_CurOB.BORLevel);
  458. XP("USERConfig: 0x%02X\r\n", p->m_CurOB.USERConfig);
  459. {
  460. XP("\tnRST_STDBY = %d (%s)\r\n", p->m_nRST_STDBY, (p->m_nRST_STDBY == 0 ? "STDBY_RST(0)" : "STDBY_NO_RST(0x80)"));
  461. XP("\tnRST_STOP = %d (%s)\r\n", p->m_nRST_STOP, (p->m_nRST_STOP == 0 ? "STOP_RST(0)" : "STOP_NO_RST(0x40)"));
  462. XP("\tWDG_SW = %d (%s)\r\n", p->m_WDG_SW, (p->m_WDG_SW == 0 ? "IWDG_HW(0)" : "IWDG_SW(0x20)"));
  463. }
  464. }
  465. HTK_BOOL STM32_Flash_OB_IsDefault(PSTM32_Flash_OB p)
  466. {
  467. XP("m_CurOB.BORLevel = 0x%08X, m_SetOB.BORLevel = 0x%08X\r\n", p->m_CurOB.BORLevel, p->m_SetOB.BORLevel);
  468. XP("m_CurOB.RDPLevel = 0x%08X, m_SetOB.RDPLevel = 0x%08X\r\n", p->m_CurOB.RDPLevel, p->m_SetOB.RDPLevel);
  469. XP("m_CurOB.USERConfig = 0x%08X, m_SetOB.USERConfig = 0x%08X\r\n", p->m_CurOB.USERConfig, p->m_SetOB.USERConfig);
  470. XP("m_CurOB.WRPSector = 0x%08X, m_SetOB.WRPSector = 0x%08X\r\n", p->m_CurOB.WRPSector, p->m_SetOB.WRPSector);
  471. XP("(~p->m_SetOB.WRPSector) & OB_WRP_SECTOR_All = 0x%08X\r\n", (~p->m_SetOB.WRPSector) & OB_WRP_SECTOR_All);
  472. if (
  473. (p->m_CurOB.BORLevel == p->m_SetOB.BORLevel) &&
  474. (p->m_CurOB.RDPLevel == p->m_SetOB.RDPLevel) &&
  475. (p->m_CurOB.USERConfig == p->m_SetOB.USERConfig) &&
  476. (p->m_CurOB.WRPSector == ((~p->m_SetOB.WRPSector) & OB_WRP_SECTOR_All))
  477. )
  478. {
  479. XP("<OB is Default>\r\n");
  480. return HTK_TRUE;
  481. }
  482. else
  483. {
  484. XP("<OB is NOT Default>\r\n");
  485. return HTK_FALSE;
  486. }
  487. }
  488. void STM32_Flash_OB_ProcDefault(void)
  489. {
  490. PSTM32_Flash_OB p = HTK_Malloc(sizeof(STM32_Flash_OB));
  491. if (p != NULL)
  492. {
  493. STM32_Flash_OB_Init(p);
  494. STM32_Flash_OB_Read(p);
  495. STM32_Flash_OB_Disp(p);
  496. if (!STM32_Flash_OB_IsDefault(p))
  497. {
  498. STM32_Flash_OB_WriteDefault(p);
  499. STM32_Flash_OB_Read(p);
  500. STM32_Flash_OB_Disp(p);
  501. if (STM32_Flash_OB_IsDefault(p))
  502. {
  503. XP("<Write default OB success>\r\n");
  504. }
  505. else
  506. {
  507. XP("<Write default OB fail>\r\n");
  508. }
  509. }
  510. }
  511. else
  512. {
  513. XP("STM32_Flash_OB malloc NG\r\n");
  514. }
  515. }
  516. #endif //FUNC_STM32_FLASH_OPTION_BYTES
  517. void MX_FREERTOS_Init(void) {
  518. /* USER CODE BEGIN Init */
  519. #ifdef FUNC_BOOT_TICK
  520. Charger.m_BootTick = HAL_GetTick();
  521. #endif
  522. #ifdef FUNC_STM32_FLASH_INFO
  523. STM32_Flash_Info_Proc();
  524. #endif //FUNC_STM32_FLASH_INFO
  525. #ifdef FUNC_STM32_FLASH_OPTION_BYTES
  526. STM32_Flash_OB_ProcDefault();
  527. #endif
  528. UpdateFirmwareVersion();
  529. sprintf(Charger.Ver_HW, "RA2");
  530. HAL_GPIO_WritePin(OUT_PHY_Reset_GPIO_Port, OUT_PHY_Reset_Pin, GPIO_PIN_RESET);
  531. HAL_GPIO_WritePin(OUT_Leak_Test_GPIO_Port, OUT_Leak_Test_Pin, GPIO_PIN_SET);
  532. /* USER CODE END Init */
  533. /* USER CODE BEGIN RTOS_MUTEX */
  534. /* add mutexes, ... */
  535. /* USER CODE END RTOS_MUTEX */
  536. /* USER CODE BEGIN RTOS_SEMAPHORES */
  537. /* add semaphores, ... */
  538. /* USER CODE END RTOS_SEMAPHORES */
  539. /* USER CODE BEGIN RTOS_TIMERS */
  540. /* start timers, add new ones, ... */
  541. /* USER CODE END RTOS_TIMERS */
  542. /* USER CODE BEGIN RTOS_QUEUES */
  543. /* add queues, ... */
  544. /* USER CODE END RTOS_QUEUES */
  545. /* Create the thread(s) */
  546. /* definition and creation of ethTask */
  547. #ifndef DISABLE_OS_ETH_TASK
  548. osThreadDef(ethTask, StartEthTask, osPriorityIdle, 0, 1024);
  549. ethTaskHandle = osThreadCreate(osThread(ethTask), NULL);
  550. #endif
  551. /* definition and creation of uartTask */
  552. osThreadDef(uartTask, StartUartTask, osPriorityNormal, 0, 1024);
  553. uartTaskHandle = osThreadCreate(osThread(uartTask), NULL);
  554. /* definition and creation of adcTask */
  555. osThreadDef(adcTask, StartAdcTask, osPriorityRealtime, 0, 256);
  556. adcTaskHandle = osThreadCreate(osThread(adcTask), NULL);
  557. /* definition and creation of timeoutTask */
  558. osThreadDef(timeoutTask, StartTimeoutTask, osPriorityAboveNormal, 0, 128);
  559. timeoutTaskHandle = osThreadCreate(osThread(timeoutTask), NULL);
  560. /* definition and creation of rfidTask */
  561. osThreadDef(rfidTask, StartRfidTask, osPriorityIdle, 0, 256);
  562. rfidTaskHandle = osThreadCreate(osThread(rfidTask), NULL);
  563. /* definition and creation of led_speakerTask */
  564. osThreadDef(led_speakerTask, StartLedSpeakerTask, osPriorityAboveNormal, 0, 128);
  565. led_speakerTaskHandle = osThreadCreate(osThread(led_speakerTask), NULL);
  566. /* definition and creation of cpTask */
  567. osThreadDef(cpTask, StartCpTask, osPriorityHigh, 0, 256);
  568. cpTaskHandle = osThreadCreate(osThread(cpTask), NULL);
  569. /* definition and creation of alarmTask */
  570. osThreadDef(alarmTask, StartAlarmTask, osPriorityHigh, 0, 256);
  571. alarmTaskHandle = osThreadCreate(osThread(alarmTask), NULL);
  572. /* definition and creation of bleTask */
  573. #ifdef LOCAL_LINK_BLE
  574. osThreadDef(bleTask, StartBleTask, osPriorityHigh, 0, 1024);
  575. bleTaskHandle = osThreadCreate(osThread(bleTask), NULL);
  576. #endif
  577. /* definition and creation of memoryTask */
  578. osThreadDef(memoryTask, StartMemoryTask, osPriorityIdle, 0, 256);
  579. memoryTaskHandle = osThreadCreate(osThread(memoryTask), NULL);
  580. /* definition and creation of wifiTask */
  581. #ifdef LOCAL_LINK_WIFI
  582. osThreadDef(wifiTask, StartWifiTask, osPriorityIdle, 0, 1024);
  583. wifiTaskHandle = osThreadCreate(osThread(wifiTask), NULL);
  584. #endif
  585. /* USER CODE BEGIN RTOS_THREADS */
  586. /* add threads, ... */
  587. /* USER CODE END RTOS_THREADS */
  588. }
  589. #ifndef DISABLE_OS_ETH_TASK
  590. /* USER CODE BEGIN Header_StartEthTask */
  591. /**
  592. * @brief Function implementing the ethTask thread.
  593. * @param argument: Not used
  594. * @retval None
  595. */
  596. /* USER CODE END Header_StartEthTask */
  597. void StartEthTask(void const * argument)
  598. {
  599. /* init code for USB_DEVICE */
  600. MX_USB_DEVICE_Init();
  601. /* USER CODE BEGIN StartEthTask */
  602. /* Infinite loop */
  603. for(;;)
  604. {
  605. osDelay(1);
  606. }
  607. /* USER CODE END StartEthTask */
  608. }
  609. #endif //DISABLE_OS_ETH_TASK
  610. /* USER CODE BEGIN Header_StartUartTask */
  611. /**
  612. * @brief Function implementing the uartTask thread.
  613. * @param argument: Not used
  614. * @retval None
  615. */
  616. /* USER CODE END Header_StartUartTask */
  617. void StartUartTask(void const * argument)
  618. {
  619. /* USER CODE BEGIN StartUartTask */
  620. //uuart
  621. #ifndef DISABLE_PRINT_RTOS_MSG
  622. uint8_t pcWriteBuffer[1024];
  623. #endif
  624. uint8_t endFlag[4]={0x55,0xaa,0x55,0xaa};
  625. __IO uint32_t flash;
  626. uint32_t checksum;
  627. uint8_t dbgIdx;
  628. #if defined(DEBUG) || defined(RTOS_STAT)
  629. DEBUG_INFO("========== AP initial OK ==========\n\r");
  630. DEBUG_INFO(" Version: %s\n\r", Charger.Ver_FW);
  631. DEBUG_INFO("===================================\n\r");
  632. if (__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST) != RESET) DEBUG_INFO("Reset flag: Pin reset \r\n");
  633. if (__HAL_RCC_GET_FLAG(RCC_FLAG_PORRST) != RESET) DEBUG_INFO("Reset flag: POR reset \r\n");
  634. if (__HAL_RCC_GET_FLAG(RCC_FLAG_SFTRST) != RESET) DEBUG_INFO("Reset flag: SW reset \r\n");
  635. if (__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST) != RESET) DEBUG_INFO("Reset flag: IWDG reset \r\n");
  636. if (__HAL_RCC_GET_FLAG(RCC_FLAG_WWDGRST) != RESET) DEBUG_INFO("Reset flag: WWDG reset \r\n");
  637. if (__HAL_RCC_GET_FLAG(RCC_FLAG_LPWRRST) != RESET) DEBUG_INFO("Reset flag: LP reset \r\n");
  638. DEBUG_INFO("\r\n");
  639. __HAL_RCC_CLEAR_RESET_FLAGS();
  640. #endif
  641. Charger.CSUisReady = OFF ;
  642. #ifdef FUNC_RS485_SLAVE
  643. HAL_GPIO_WritePin(OUT_Meter_485_DE_GPIO_Port, OUT_Meter_485_DE_Pin, GPIO_PIN_RESET);
  644. HAL_GPIO_WritePin(OUT_Meter_485_RE_GPIO_Port, OUT_Meter_485_RE_Pin, GPIO_PIN_RESET);
  645. #endif
  646. #ifdef FUNC_RELAY_OFF_WHEN_GET_TWICE_OFF_CMD
  647. u8 CsuSetRelayOffCount = 0;
  648. #endif
  649. #ifdef FUNC_REQUEST_OFF_WHEN_GET_TWICE_OFF_CMD
  650. u8 CsuSetRequestOffCount = 0;
  651. #endif
  652. /* Infinite loop */
  653. for(;;)
  654. {
  655. timerEnable(TIMER_IDX_RTOS_STAT, 500);
  656. if(timer[TIMER_IDX_RTOS_STAT].isAlarm)
  657. {
  658. switch(dbgIdx)
  659. {
  660. case 0:
  661. #ifdef RTOS_STAT
  662. #ifndef DISABLE_PRINT_RTOS_MSG
  663. printf("=================================================\r\n");
  664. printf("Task_Name \tState \tPrior \tStack \tTask_Id\r\n");
  665. vTaskList((char *)&pcWriteBuffer);
  666. printf("%s\r\n", pcWriteBuffer);
  667. printf("-------------------------------------------------\r\n");
  668. printf("Task_Name \tRun_Time(10us) \tUsage_Percent\r\n");
  669. vTaskGetRunTimeStats((char *)&pcWriteBuffer);
  670. printf("%s\r\n", pcWriteBuffer);
  671. #endif //DISABLE_PRINT_RTOS_MSG
  672. #endif
  673. dbgIdx++;
  674. break;
  675. case 1:
  676. #ifdef FUNC_EMPX10_CUSTOMIZE
  677. DEBUG_INFO("Firmware version: %s (AWeMP/ID:%d)\r\n", Charger.Ver_FW, Charger.m_EMPX10_RS485_ClientAddr);
  678. #else
  679. DEBUG_INFO("Firmware version: %s\r\n", Charger.Ver_FW);
  680. #endif
  681. #ifdef FUNC_BUILD_DATE_TIME
  682. DEBUG_INFO("Build: %s %s\r\n", __DATE__, __TIME__); //ex: Build: Mar 10 2022 14:53:25
  683. #endif
  684. //DEBUG_INFO("MCU_Control_Mode: %x\r\n",Charger.memory.EVSE_Config.data.item.MCU_Control_Mode);
  685. DEBUG_INFO("MCU_Control_Mode: %x (%s)\r\n",Charger.memory.EVSE_Config.data.item.MCU_Control_Mode,
  686. Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_CSU ? "CSU" : "No CSU");
  687. DEBUG_INFO("ModelName: %s\r\n",Charger.memory.EVSE_Config.data.item.ModelName);
  688. DEBUG_INFO("SerialNumber: %s\r\n",Charger.memory.EVSE_Config.data.item.SerialNumber);
  689. #ifdef FUNC_BOOT_TICK
  690. DEBUG_INFO("UpTime(s): %d\r\n", (HAL_GetTick() - Charger.m_BootTick) / 1000);
  691. #endif
  692. dbgIdx++;
  693. break;
  694. case 2:
  695. if (Charger.CCID_Module_Type == CCID_MODULE_CORMEX)
  696. {
  697. DEBUG_INFO("Charger.CCID_Module_Type is CCID_MODULE_CORMEX\n\r");
  698. }
  699. else if (Charger.CCID_Module_Type == CCID_MODULE_VAC)
  700. {
  701. DEBUG_INFO("Charger.CCID_Module_Type is CCID_MODULE_VAC\n\r");
  702. }
  703. DEBUG_INFO("System mode: %d\r\n", Charger.Mode);
  704. DEBUG_INFO("Alarm code: 0x%08X\r\n", Charger.Alarm_Code);
  705. #ifdef FUNC_SHOW_RELAY_INFO
  706. DEBUG_INFO("Relay action: Main(%d)\r\n", Charger.Relay_Action);
  707. #endif
  708. dbgIdx++;
  709. break;
  710. case 3:
  711. DEBUG_INFO("Original Voltage[0]: %f\r\n", adc_value.ADC3_IN9_Voltage_L1.value/100.0);
  712. DEBUG_INFO("Original Current[0]: %f\r\n", adc_value.ADC3_IN7_Current_L1.value/100.0);
  713. dbgIdx++;
  714. break;
  715. case 4:
  716. DEBUG_INFO("Correction Voltage[0]: %f\r\n", Charger.Voltage[0]/100.0);
  717. DEBUG_INFO("Correction Current[0]: %f\r\n", Charger.Current[0]/100.0);
  718. dbgIdx++;
  719. break;
  720. case 5:
  721. DEBUG_INFO("PE(ADC): %d\r\n", adc_value.ADC3_IN4_GMI_VL1.value );
  722. DEBUG_INFO("PE(V): %f\r\n", adc_value.ADC3_IN4_GMI_VL1.value*3.3/4095 );
  723. DEBUG_INFO("adc_value.ADC2_IN5_Welding.value : %d(unit:0.01v)\r\n", adc_value.ADC2_IN5_Welding.value );
  724. DEBUG_INFO("Charger.temperature.SystemAmbientTemp : %d(unit:C)\r\n", Charger.temperature.SystemAmbientTemp);
  725. dbgIdx++;
  726. break;
  727. case 6:
  728. if (Charger.CCID_Module_Type == CCID_MODULE_CORMEX)
  729. {
  730. DEBUG_INFO("Original GF[0]: %fmA\r\n", adc_value.ADC2_IN6_GF.value/100.0);
  731. DEBUG_INFO("Charger.Leak_Current: %fmA\r\n", Charger.Leak_Current/100.0);
  732. }
  733. else
  734. {
  735. DEBUG_INFO("Temperature ADC raw data : %d\r\n", adc_value.ADC3_IN15_Temp.value);
  736. DEBUG_INFO("Temperature : %d\r\n", Charger.temperature.SystemAmbientTemp);
  737. DEBUG_INFO("ADC3_Buffer[1] (ADC): %d(unit:ADC)\r\n", ADC3_Buffer[1] );
  738. DEBUG_INFO("Charger.Led_Mode: %d\r\n", Charger.Led_Mode );
  739. }
  740. dbgIdx++;
  741. break;
  742. case 7:
  743. DEBUG_INFO("CP+(ADC): %d(unit:ADC)\r\n",CpDetectionResult.PositiveValue );
  744. DEBUG_INFO("CP-(ADC): %d(unit:ADC)\r\n",CpDetectionResult.NegativeValue );
  745. if(CpDetectionResult.PositiveValue>0)
  746. {
  747. #ifdef FUNC_CP_ADC_MODIFY
  748. DEBUG_INFO("CP +voltage: %f\r\n", CP_GET_OPA_VIN_USE_ADC(CpDetectionResult.PositiveValue));
  749. #else
  750. DEBUG_INFO("CP +voltage: %f\r\n", (float)((1.59131-((3.3*CpDetectionResult.PositiveValue)/4095))*(475/53.6)));
  751. #endif
  752. }
  753. else
  754. {
  755. DEBUG_INFO("CP +voltage: 0\r\n");
  756. }
  757. if(CpDetectionResult.NegativeValue>0)
  758. {
  759. #ifdef FUNC_CP_ADC_MODIFY
  760. DEBUG_INFO("CP -voltage: %f\r\n", CP_GET_OPA_VIN_USE_ADC(CpDetectionResult.NegativeValue));
  761. #else
  762. DEBUG_INFO("CP -voltage: %f\r\n", (float)((1.59131-((3.3*CpDetectionResult.NegativeValue)/4095))*(475/53.6)));
  763. #endif
  764. }else
  765. {
  766. DEBUG_INFO("CP -voltage: 0\r\n");
  767. }
  768. DEBUG_INFO("CP state: %d\r\n", Charger.CP_State);
  769. dbgIdx++;
  770. break;
  771. case 8:
  772. DEBUG_INFO("Power_Consumption_Cumulative: %d (0.0001KWH)\r\n", Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative);
  773. DEBUG_INFO("Current rating value: %d A\r\n", Charger.maxRatingCurrent);
  774. if (Charger.memory.EVSE_Config.data.item.MaxChargingCurrent>=20)
  775. {
  776. DEBUG_INFO("OCP tigger UL: %d\r\n", (Charger.memory.EVSE_Config.data.item.MaxChargingCurrent*110)-100 );
  777. DEBUG_INFO("OCP tigger IEC: %d\r\n", (Charger.memory.EVSE_Config.data.item.MaxChargingCurrent*125)-100 );
  778. }
  779. else
  780. {
  781. DEBUG_INFO("OCP tigger UL: %d\r\n", (Charger.memory.EVSE_Config.data.item.MaxChargingCurrent+1)*100 );
  782. DEBUG_INFO("OCP tigger IEC: %d\r\n", (Charger.memory.EVSE_Config.data.item.MaxChargingCurrent*125)-100 );
  783. }
  784. DEBUG_INFO("Charger.am3352.isRequestOn: %d\r\n", Charger.am3352.isRequestOn);
  785. #ifdef FUNC_OCP_WITH_PP
  786. #ifdef MODIFY_OCP_PROC_WITH_MODELNAME_IDX_3_EURO_SPEC
  787. DEBUG_INFO("#OCPInfo(%d, %d, %d, %d) [%s]\r\n",
  788. Charger.m_OCP_CurMaxCurr, Charger.OCP_Magnification,
  789. Charger.m_OCP_BegThreshold, Charger.m_OCP_EndThreshold,
  790. Charger.m_bModelNameWithEuroSpecOCP ? "EuroSpec" : "NotEuroSpec");
  791. #else
  792. DEBUG_INFO("#OCPInfo(%d, %d, %d, %d)\r\n",
  793. Charger.m_OCP_CurMaxCurr, Charger.OCP_Magnification,
  794. Charger.m_OCP_BegThreshold, Charger.m_OCP_EndThreshold);
  795. #endif //MODIFY_OCP_PROC_WITH_MODELNAME_IDX_3_EURO_SPEC
  796. #endif //FUNC_OCP_WITH_PP
  797. #ifdef FUNC_GET_TRIP_TIME
  798. if (Charger.m_TripBeg != 0 && Charger.m_TripBeg < Charger.m_TripEnd)
  799. {
  800. DEBUG_INFO("Trip period: %d\r\n", Charger.m_TripEnd - Charger.m_TripBeg);
  801. }
  802. #endif
  803. dbgIdx++;
  804. break;
  805. case 9:
  806. if (Charger.Alarm_Code & ALARM_OVER_VOLTAGE)
  807. DEBUG_INFO("ALARM_OVER_VOLTAGE \n\r");
  808. if (Charger.Alarm_Code & ALARM_UNDER_VOLTAGE)
  809. DEBUG_INFO("ALARM_UNDER_VOLTAGE \n\r");
  810. if (Charger.Alarm_Code & ALARM_OVER_CURRENT)
  811. DEBUG_INFO("ALARM_OVER_CURRENT \n\r");
  812. if (Charger.Alarm_Code & ALARM_OVER_TEMPERATURE)
  813. DEBUG_INFO("ALARM_OVER_TEMPERATURE \n\r");
  814. if (Charger.Alarm_Code & ALARM_GROUND_FAIL)
  815. DEBUG_INFO("ALARM_GROUND_FAIL \n\r");
  816. if (Charger.Alarm_Code & ALARM_CP_ERROR)
  817. DEBUG_INFO("ALARM_CP_ERROR \n\r");
  818. if (Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC)
  819. DEBUG_INFO("ALARM_CURRENT_LEAK_AC \n\r");
  820. if (Charger.Alarm_Code & ALARM_CURRENT_LEAK_DC)
  821. DEBUG_INFO("ALARM_CURRENT_LEAK_DC \n\r");
  822. if (Charger.Alarm_Code & ALARM_MCU_TESTFAIL)
  823. DEBUG_INFO("ALARM_MCU_TESTFAIL \n\r");
  824. dbgIdx++;
  825. break;
  826. case 10:
  827. if (Charger.Alarm_Code & ALARM_HANDSHAKE_TIMEOUT)
  828. DEBUG_INFO("ALARM_HANDSHAKE_TIMEOUT \n\r");
  829. if (Charger.Alarm_Code & ALARM_EMERGENCY_STOP)
  830. DEBUG_INFO("ALARM_EMERGENCY_STOP \n\r");
  831. if (Charger.Alarm_Code & ALARM_RELAY_STATUS)
  832. DEBUG_INFO("ALARM_RELAY_STATUS \n\r");
  833. if (Charger.Alarm_Code & ALARM_LEAK_MODULE_FAIL)
  834. DEBUG_INFO("ALARM_LEAK_MODULE_FAIL \n\r");
  835. if (Charger.Alarm_Code & ALARM_SHUTTER_FAULT)
  836. DEBUG_INFO("ALARM_SHUTTER_FAULT \n\r");
  837. if (Charger.Alarm_Code & ALARM_LOCKER_FAULT)
  838. DEBUG_INFO("ALARM_LOCKER_FAULT \n\r");
  839. if (Charger.Alarm_Code & ALARM_POWER_DROP)
  840. DEBUG_INFO("ALARM_POWER_DROP \n\r");
  841. if (Charger.Alarm_Code & ALARM_CIRCUIT_SHORT)
  842. DEBUG_INFO("ALARM_CIRCUIT_SHORT \n\r");
  843. if (Charger.Alarm_Code & ALARM_ROTATORY_SWITCH_FAULT)
  844. DEBUG_INFO("ALARM_ROTATORY_SWITCH_FAULT \n\r");
  845. if (Charger.Alarm_Code & ALARM_RELAY_DRIVE_FUALT)
  846. DEBUG_INFO("ALARM_RELAY_DRIVE_FUALT \n\r");
  847. dbgIdx++;
  848. break;
  849. default:
  850. getlastRecord();
  851. dbgIdx = 0;
  852. break;
  853. }
  854. timerRefresh(TIMER_IDX_RTOS_STAT);
  855. }
  856. HAL_IWDG_Refresh(&hiwdg);
  857. #ifdef FUNC_RS485_SLAVE
  858. if(UART_IAP_recv_end_flag == 1 || UART_RS485_recv_end_flag == 1)
  859. #else
  860. if(UART_IAP_recv_end_flag == 1)
  861. #endif
  862. {
  863. #ifdef FUNC_UART_IAP_TX_BUF_INIT_ZERO
  864. uint8_t tx[UART_BUFFER_SIZE] = { 0 };
  865. #else
  866. uint8_t tx[UART_BUFFER_SIZE];
  867. #endif
  868. #ifdef MODIFY_UART_TX_LEN_FROM_U8_TO_U16
  869. uint16_t tx_len;
  870. #else
  871. uint8_t tx_len;
  872. #endif
  873. uint8_t chksum = 0;
  874. uint8_t tmpBuf = 0 ;
  875. // Protocol process
  876. #ifdef FUNC_RS485_SLAVE
  877. uint8_t *CurRxBuf = NULL;
  878. uint16_t *pCurRxBuf_RxLen = NULL;
  879. uint8_t *pCurRecvEndFlag = NULL;
  880. UART_HandleTypeDef *pUartHandle = NULL;
  881. if (UART_IAP_recv_end_flag == 1 && isValidCheckSum_IAP(UART_IAP_rx_buffer) == ON)
  882. {
  883. CurRxBuf = UART_IAP_rx_buffer;
  884. pCurRxBuf_RxLen = &UART_IAP_rx_len;
  885. pCurRecvEndFlag = &UART_IAP_recv_end_flag;
  886. pUartHandle = &IAP_USART;
  887. #ifdef FUNC_RELAY_OFF_WHEN_CSU_CMD_TIMEOUT
  888. Charger.m_CSU_RxTick = HAL_GetTick();
  889. #endif
  890. }
  891. else if (UART_RS485_recv_end_flag == 1 && isValidCheckSum_IAP(UART_RS485_rx_buffer) == ON)
  892. {
  893. CurRxBuf = UART_RS485_rx_buffer;
  894. pCurRxBuf_RxLen = &UART_RS485_rx_len;
  895. pCurRecvEndFlag = &UART_RS485_recv_end_flag;
  896. pUartHandle = &RS485_USART;
  897. #ifdef FUNC_RELAY_OFF_WHEN_CSU_CMD_TIMEOUT //DC Model
  898. Charger.m_CSU_RxTick = HAL_GetTick();
  899. #endif
  900. }
  901. if (CurRxBuf != NULL)
  902. #else
  903. uint8_t *CurRxBuf = UART_IAP_rx_buffer;
  904. if(isValidCheckSum_IAP() == ON)
  905. #endif
  906. {
  907. Charger.CSUisReady = ON ;
  908. #ifdef DEBUG_EMPX10_COMM
  909. if (CurRxBuf == UART_RS485_rx_buffer)
  910. {
  911. XP("-------------------------------------------\r\n");
  912. HTK_ByteArray2HexStr_XP(NULL, CurRxBuf, 0, IMCP_FM_HEAD_LEN + HTK_U16(CurRxBuf[IMCP_FM_DATALEN_LSB_IDX]) + IMCP_FM_TAIL_LEN);
  913. }
  914. #endif //DEBUG_EMPX10_COMM
  915. #ifdef ENABLE_PRINT_IMCP_MSG
  916. #ifdef FUNC_RS485_SLAVE
  917. #ifdef PRINT_IMCP_MSG_ONLY_ON_UPDATEPORT
  918. if (CurRxBuf == UART_IAP_rx_buffer && CheckPrintImcpMsg(CurRxBuf[IMCP_FM_MSGID_IDX]))
  919. {
  920. XP("-------------------------------------------\r\n");
  921. HTK_ByteArray2HexStr_XP(NULL, CurRxBuf, 0, IMCP_FM_HEAD_LEN + HTK_U16(CurRxBuf[IMCP_FM_DATALEN_LSB_IDX]) + IMCP_FM_TAIL_LEN);
  922. }
  923. #else
  924. if (CheckPrintImcpMsg(CurRxBuf[IMCP_FM_MSGID_IDX]))
  925. {
  926. XP("-------------------------------------------\r\n");
  927. HTK_ByteArray2HexStr_XP(NULL, CurRxBuf, 0, IMCP_FM_HEAD_LEN + HTK_U16(CurRxBuf[IMCP_FM_DATALEN_LSB_IDX]) + IMCP_FM_TAIL_LEN);
  928. }
  929. #endif //PRINT_IMCP_MSG_ONLY_ON_UPDATEPORT
  930. #else //FUNC_RS485_SLAVE
  931. if (CheckPrintImcpMsg(UART_IAP_rx_buffer[IMCP_FM_MSGID_IDX]))
  932. {
  933. XP("-------------------------------------------\r\n");
  934. HTK_ByteArray2HexStr_XP(NULL, UART_IAP_rx_buffer, 0, IMCP_FM_HEAD_LEN + HTK_U16(UART_IAP_rx_buffer[IMCP_FM_DATALEN_LSB_IDX]) + IMCP_FM_TAIL_LEN);
  935. }
  936. #endif //FUNC_RS485_SLAVE
  937. #endif //ENABLE_PRINT_IMCP_MSG
  938. #ifdef FUNC_RS485_SLAVE
  939. switch (CurRxBuf[3])
  940. #else
  941. switch (UART_IAP_rx_buffer[3])
  942. #endif
  943. {
  944. /*
  945. Query message
  946. */
  947. case PROTOCOL_MESSAGE_QUERY_FW_VER:
  948. tx_len = 7 + (strlen(Charger.Ver_FW));
  949. tx[0] = 0xaa;
  950. tx[1] = PROTOCOL_ADDR;
  951. tx[2] = CurRxBuf[1];
  952. tx[3] = PROTOCOL_MESSAGE_QUERY_FW_VER;
  953. tx[4] = strlen(Charger.Ver_FW)&0xff;
  954. tx[5] = (strlen(Charger.Ver_FW)>>0x08) & 0xff;
  955. for(uint8_t idx=0;idx<strlen(Charger.Ver_FW);idx++)
  956. {
  957. tx[6+idx] = Charger.Ver_FW[idx];
  958. }
  959. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  960. {
  961. chksum ^= tx[6 + idx];
  962. }
  963. tx[6+(tx[4] | (tx[5]<<8))] = chksum;
  964. break;
  965. case PROTOCOL_MESSAGE_QUERY_HW_VER:
  966. tx_len = 7 + strlen(Charger.Ver_HW);
  967. tx[0] = 0xaa;
  968. tx[1] = PROTOCOL_ADDR;
  969. tx[2] = CurRxBuf[1];
  970. tx[3] = PROTOCOL_MESSAGE_QUERY_HW_VER;
  971. tx[4] = strlen(Charger.Ver_HW) & 0xff;
  972. tx[5] = (strlen(Charger.Ver_HW)>>0x08) & 0xff;
  973. for(uint8_t idx=0;idx<strlen(Charger.Ver_HW);idx++)
  974. {
  975. tx[6+idx] = Charger.Ver_HW[idx];
  976. }
  977. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  978. {
  979. chksum ^= tx[6 + idx];
  980. }
  981. tx[6+(tx[4] | (tx[5]<<8))] = chksum;
  982. break;
  983. case PROTOCOL_MESSAGE_QUERY_PRESENT_INPUT_VOLTAGE:
  984. tx_len = 14;
  985. tx[0] = 0xaa;
  986. tx[1] = PROTOCOL_ADDR;
  987. tx[2] = CurRxBuf[1];
  988. tx[3] = PROTOCOL_MESSAGE_QUERY_PRESENT_INPUT_VOLTAGE;
  989. tx[4] = 0x07;
  990. tx[5] = 0x00;
  991. tx[6] = 0x00;
  992. tx[7] = (((Charger.Voltage[0]/10)>>0) & 0xff);
  993. tx[8] = (((Charger.Voltage[0]/10)>>8) & 0xff);
  994. tx[9] = (((Charger.Voltage[1]/10)>>0) & 0xff);
  995. tx[10] = (((Charger.Voltage[1]/10)>>8) & 0xff);
  996. tx[11] = (((Charger.Voltage[2]/10)>>0) & 0xff);
  997. tx[12] = (((Charger.Voltage[2]/10)>>8) & 0xff);
  998. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  999. {
  1000. chksum ^= tx[6 + idx];
  1001. }
  1002. tx[13] = chksum;
  1003. break;
  1004. case PROTOCOL_MESSAGE_QUERY_PRESENT_OUTPUT_VOLTAGE:
  1005. tx_len = 15;
  1006. tx[0] = 0xaa;
  1007. tx[1] = PROTOCOL_ADDR;
  1008. tx[2] = CurRxBuf[1];
  1009. tx[3] = PROTOCOL_MESSAGE_QUERY_PRESENT_OUTPUT_VOLTAGE;
  1010. tx[4] = 0x08;
  1011. tx[5] = 0x00;
  1012. tx[6] = 0x00;
  1013. tx[7] = 0x00;
  1014. tx[8] = 0x00;
  1015. tx[9] = 0x00;
  1016. tx[10] = 0x00;
  1017. tx[11] = 0x00;
  1018. tx[12] = 0x00;
  1019. tx[13] = 0x00;
  1020. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1021. {
  1022. chksum ^= tx[6 + idx];
  1023. }
  1024. tx[14] = chksum;
  1025. break;
  1026. case PROTOCOL_MESSAGE_QUERY_FAN_SPEED:
  1027. tx_len = 15;
  1028. tx[0] = 0xaa;
  1029. tx[1] = PROTOCOL_ADDR;
  1030. tx[2] = CurRxBuf[1];
  1031. tx[3] = PROTOCOL_MESSAGE_QUERY_FAN_SPEED;
  1032. tx[4] = 0x08;
  1033. tx[5] = 0x00;
  1034. tx[6] = 0x00;
  1035. tx[7] = 0x00;
  1036. tx[8] = 0x00;
  1037. tx[9] = 0x00;
  1038. tx[10] = 0x00;
  1039. tx[11] = 0x00;
  1040. tx[12] = 0x00;
  1041. tx[13] = 0x00;
  1042. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1043. {
  1044. chksum ^= tx[6 + idx];
  1045. }
  1046. tx[14] = chksum;
  1047. break;
  1048. case PROTOCOL_MESSAGE_QUERY_TEMPERATURE:
  1049. tx_len = 15;
  1050. tx[0] = 0xaa;
  1051. tx[1] = PROTOCOL_ADDR;
  1052. tx[2] = CurRxBuf[1];
  1053. tx[3] = PROTOCOL_MESSAGE_QUERY_TEMPERATURE;
  1054. tx[4] = 0x08;
  1055. tx[5] = 0x00;
  1056. tx[6] = (((Charger.temperature.SystemAmbientTemp+60)>>0) & 0xff);
  1057. tx[7] = (((Charger.temperature.CcsConnectorTemp+60)>>0) & 0xff);
  1058. tx[8] = 0x00;
  1059. tx[9] = 0x00;
  1060. tx[10] = 0x00;
  1061. tx[11] = 0x00;
  1062. tx[12] = 0x00;
  1063. tx[13] = 0x00;
  1064. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1065. {
  1066. chksum ^= tx[6 + idx];
  1067. }
  1068. tx[14] = chksum;
  1069. break;
  1070. case PROTOCOL_MESSAGE_QUERY_AUX_POWER_VOLTAGE:
  1071. tx_len = 15;
  1072. tx[0] = 0xaa;
  1073. tx[1] = PROTOCOL_ADDR;
  1074. tx[2] = CurRxBuf[1];
  1075. tx[3] = PROTOCOL_MESSAGE_QUERY_AUX_POWER_VOLTAGE;
  1076. tx[4] = 0x08;
  1077. tx[5] = 0x00;
  1078. tx[6] = 0x00;
  1079. tx[7] = 0x00;
  1080. tx[8] = 0x00;
  1081. tx[9] = 0x00;
  1082. tx[10] = 0x00;
  1083. tx[11] = 0x00;
  1084. tx[12] = 0x00;
  1085. tx[13] = 0x00;
  1086. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1087. {
  1088. chksum ^= tx[6 + idx];
  1089. }
  1090. tx[14] = chksum;
  1091. break;
  1092. case PROTOCOL_MESSAGE_QUERY_RELAY_OUTPUT_STATUS:
  1093. tx_len = 9;
  1094. tx[0] = 0xaa;
  1095. tx[1] = PROTOCOL_ADDR;
  1096. tx[2] = CurRxBuf[1];
  1097. tx[3] = PROTOCOL_MESSAGE_QUERY_RELAY_OUTPUT_STATUS;
  1098. tx[4] = 0x02;
  1099. tx[5] = 0x00;
  1100. tx[6] = ((Relay1_Action_Get()==GPIO_RELAY1_ACTION_OFF)?0:1);
  1101. tx[7] = ((Relay2_Action_Get()==GPIO_RELAY2_ACTION_OFF)?0:1);
  1102. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1103. {
  1104. chksum ^= tx[6 + idx];
  1105. }
  1106. tx[8] = chksum;
  1107. break;
  1108. case PROTOCOL_MESSAGE_QUERY_GFD_ADC_VALUE:
  1109. tx_len = 15;
  1110. tx[0] = 0xaa;
  1111. tx[1] = PROTOCOL_ADDR;
  1112. tx[2] = CurRxBuf[1];
  1113. tx[3] = PROTOCOL_MESSAGE_QUERY_GFD_ADC_VALUE;
  1114. tx[4] = 0x08;
  1115. tx[5] = 0x00;
  1116. tx[6] = ((adc_value.ADC2_IN6_GF.value>>0) & 0xff);
  1117. tx[7] = ((adc_value.ADC2_IN6_GF.value>>8) & 0xff);
  1118. tx[8] = 0x00;
  1119. tx[9] = 0x00;
  1120. tx[10] = 0x00;
  1121. tx[11] = 0x00;
  1122. tx[12] = 0x00;
  1123. tx[13] = 0x00;
  1124. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1125. {
  1126. chksum ^= tx[6 + idx];
  1127. }
  1128. tx[14] = chksum;
  1129. break;
  1130. case PROTOCOL_MESSAGE_QUERY_INPUT_GPIO_STATUS:
  1131. tx_len = 9;
  1132. tx[0] = 0xaa;
  1133. tx[1] = PROTOCOL_ADDR;
  1134. tx[2] = CurRxBuf[1];
  1135. tx[3] = PROTOCOL_MESSAGE_QUERY_INPUT_GPIO_STATUS;
  1136. tx[4] = 0x02;
  1137. tx[5] = 0x00;
  1138. tx[6] = 0x00;
  1139. tx[7] = 0x00;
  1140. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1141. {
  1142. chksum ^= tx[6 + idx];
  1143. }
  1144. tx[8] = chksum;
  1145. break;
  1146. case PROTOCOL_MESSAGE_QUERY_ALARM_LOG:
  1147. tx_len = 17;
  1148. tx[0] = 0xaa;
  1149. tx[1] = PROTOCOL_ADDR;
  1150. tx[2] = CurRxBuf[1];
  1151. tx[3] = PROTOCOL_MESSAGE_QUERY_ALARM_LOG;
  1152. tx[4] = 0x0a;
  1153. tx[5] = 0x00;
  1154. tx[6] = CurRxBuf[7] ;
  1155. tx[7] = CurRxBuf[8] ;
  1156. tx[8] = CharacterArray[(Charger.memory.hisAlarm.data.item[CurRxBuf[7] | (CurRxBuf[8]>>8)].alarmCode & 0xF0000000)>>28];
  1157. tx[9] = CharacterArray[(Charger.memory.hisAlarm.data.item[CurRxBuf[7] | (CurRxBuf[8]>>8)].alarmCode & 0x0F000000)>>24];
  1158. tx[10] = CharacterArray[(Charger.memory.hisAlarm.data.item[CurRxBuf[7] | (CurRxBuf[8]>>8)].alarmCode & 0x00F00000)>>20];
  1159. tx[11] = CharacterArray[(Charger.memory.hisAlarm.data.item[CurRxBuf[7] | (CurRxBuf[8]>>8)].alarmCode & 0x000F0000)>>16];
  1160. tx[12] = CharacterArray[(Charger.memory.hisAlarm.data.item[CurRxBuf[7] | (CurRxBuf[8]>>8)].alarmCode & 0x0000F000)>>12];
  1161. tx[13] = CharacterArray[(Charger.memory.hisAlarm.data.item[CurRxBuf[7] | (CurRxBuf[8]>>8)].alarmCode & 0x00000F00)>>8];
  1162. tx[14] = CharacterArray[(Charger.memory.hisAlarm.data.item [CurRxBuf[7] | (CurRxBuf[8]>>8)].alarmCode & 0x000000F0)>>4];
  1163. tx[15] = CharacterArray[ Charger.memory.hisAlarm.data.item [CurRxBuf[7] | (CurRxBuf[8]>>8)].alarmCode & 0x0000000F ];
  1164. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1165. {
  1166. chksum ^= tx[6 + idx];
  1167. }
  1168. tx[16] = chksum;
  1169. /*
  1170. tx[8] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].DateTime.year>>0) & 0xff);
  1171. tx[9] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].DateTime.year>>8) & 0xff);
  1172. tx[10] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].DateTime.month>>0) & 0xff);;
  1173. tx[11] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].DateTime.day>>0) & 0xff);
  1174. tx[12] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].DateTime.hour>>0) & 0xff);
  1175. tx[13] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].DateTime.min>>0) & 0xff);
  1176. tx[14] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].DateTime.sec>>0) & 0xff);
  1177. tx[15] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].alarmCode>>0) & 0xff);
  1178. tx[16] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].alarmCode>>8) & 0xff);
  1179. tx[17] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].alarmCode>>16) & 0xff);
  1180. tx[18] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].alarmCode>>24) & 0xff);
  1181. tx[19] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].mode>>0) & 0xff);
  1182. tx[20] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].isRecover>0) & 0xff);
  1183. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1184. {
  1185. chksum ^= tx[6 + idx];
  1186. }
  1187. tx[21] = chksum;
  1188. */
  1189. break;
  1190. case PROTOCOL_MESSAGE_QUERY_SN:
  1191. // head +checksum = 7 , SystemDateTimeyear month day = 8 , SerialNumber
  1192. tx_len = 15 + strlen(Charger.memory.EVSE_Config.data.item.SerialNumber);
  1193. tx[0] = 0xaa;
  1194. tx[1] = PROTOCOL_ADDR;
  1195. tx[2] = CurRxBuf[1];
  1196. tx[3] = PROTOCOL_MESSAGE_QUERY_SN;
  1197. tx[4] = (strlen(Charger.memory.EVSE_Config.data.item.SerialNumber)+8)&0xff;
  1198. tx[5] = ((strlen(Charger.memory.EVSE_Config.data.item.SerialNumber)+8)>>0x08) & 0xff;
  1199. tx[6] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.year/1000)+'0';
  1200. tx[7] = ((Charger.memory.EVSE_Config.data.item.SystemDateTime.year%1000)/100)+'0';
  1201. tx[8] = ((Charger.memory.EVSE_Config.data.item.SystemDateTime.year%100)/10)+'0';
  1202. tx[9] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.year%10) +'0';
  1203. tx[10] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.month/10) +'0';
  1204. tx[11] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.month%10) +'0';
  1205. tx[12] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.day/10) +'0';
  1206. tx[13] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.day%10) +'0';
  1207. for(uint8_t idx=0;idx<strlen(Charger.memory.EVSE_Config.data.item.SerialNumber);idx++)
  1208. {
  1209. tx[14+idx] = Charger.memory.EVSE_Config.data.item.SerialNumber[idx];
  1210. }
  1211. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1212. {
  1213. chksum ^= tx[6 + idx];
  1214. }
  1215. tx[6+(tx[4] | (tx[5]<<8))] = chksum;
  1216. break;
  1217. case PROTOCOL_MESSAGE_QUERY_MODEL_NAME:
  1218. tx_len = 7 + strlen(Charger.memory.EVSE_Config.data.item.ModelName);;
  1219. tx[0] = 0xaa;
  1220. tx[1] = PROTOCOL_ADDR;
  1221. tx[2] = CurRxBuf[1];
  1222. tx[3] = PROTOCOL_MESSAGE_QUERY_MODEL_NAME;
  1223. tx[4] = strlen(Charger.memory.EVSE_Config.data.item.ModelName)&0xff;
  1224. tx[5] = (strlen(Charger.memory.EVSE_Config.data.item.ModelName)>>0x08) & 0xff;
  1225. for(uint8_t idx=0;idx<strlen(Charger.memory.EVSE_Config.data.item.ModelName);idx++)
  1226. {
  1227. tx[6+idx] = Charger.memory.EVSE_Config.data.item.ModelName[idx];
  1228. }
  1229. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1230. {
  1231. chksum ^= tx[6 + idx];
  1232. }
  1233. tx[6+(tx[4] | (tx[5]<<8))] = chksum;
  1234. break;
  1235. case PROTOCOL_MESSAGE_QUERY_PARAMETER:
  1236. tx_len = 11;
  1237. tx[0] = 0xaa;
  1238. tx[1] = PROTOCOL_ADDR;
  1239. tx[2] = CurRxBuf[1];
  1240. tx[3] = PROTOCOL_MESSAGE_QUERY_PARAMETER;
  1241. tx[4] = 0x04;
  1242. tx[5] = 0x00;
  1243. tx[6] = CurRxBuf[6];
  1244. tx[7] = CurRxBuf[7];
  1245. switch(CurRxBuf[6])
  1246. {
  1247. case INPUT_L1_AC_VOLTAGE:
  1248. tx[8] = Charger.memory.EVSE_Config.data.item.Correction_VL1[CurRxBuf[7]][1] & 0xff;
  1249. tx[9] = (Charger.memory.EVSE_Config.data.item.Correction_VL1[CurRxBuf[7]][1]>>8) & 0xff;
  1250. break;
  1251. case INPUT_L2_AC_VOLTAGE:
  1252. tx[8] = Charger.memory.EVSE_Config.data.item.Correction_VL2[CurRxBuf[7]][1] & 0xff;
  1253. tx[9] = (Charger.memory.EVSE_Config.data.item.Correction_VL2[CurRxBuf[7]][1]>>8) & 0xff;
  1254. break;
  1255. case INPUT_L3_AC_VOLTAGE:
  1256. tx[8] = Charger.memory.EVSE_Config.data.item.Correction_VL3[CurRxBuf[7]][1] & 0xff;
  1257. tx[9] = (Charger.memory.EVSE_Config.data.item.Correction_VL3[CurRxBuf[7]][1]>>8) & 0xff;
  1258. break;
  1259. case OUTPUT_L1_CURRENT:
  1260. tx[8] = Charger.memory.EVSE_Config.data.item.Correction_CL1[CurRxBuf[7]][1] & 0xff;
  1261. tx[9] = (Charger.memory.EVSE_Config.data.item.Correction_CL1[CurRxBuf[7]][1]>>8) & 0xff;
  1262. break;
  1263. case OUTPUT_L2_CURRENT:
  1264. tx[8] = Charger.memory.EVSE_Config.data.item.Correction_CL2[CurRxBuf[7]][1] & 0xff;
  1265. tx[9] = (Charger.memory.EVSE_Config.data.item.Correction_CL2[CurRxBuf[7]][1]>>8) & 0xff;
  1266. break;
  1267. case OUTPUT_L3_CURRENT:
  1268. tx[8] = Charger.memory.EVSE_Config.data.item.Correction_CL3[CurRxBuf[7]][1] & 0xff;
  1269. tx[9] = (Charger.memory.EVSE_Config.data.item.Correction_CL3[CurRxBuf[7]][1]>>8) & 0xff;
  1270. break;
  1271. case LEAK_CURRENT:
  1272. tx[8] = (Charger.memory.EVSE_Config.data.item.Correction_Leak[CurRxBuf[7]][1]/100) & 0xff;
  1273. tx[9] = ((Charger.memory.EVSE_Config.data.item.Correction_Leak[CurRxBuf[7]][1]/100)>>8) & 0xff;
  1274. break;
  1275. }
  1276. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1277. {
  1278. chksum ^= tx[6 + idx];
  1279. }
  1280. tx[10] = chksum;
  1281. break;
  1282. case PROTOCOL_MESSAGE_QUERY_RTC:
  1283. tx_len = 21;
  1284. tx[0] = 0xaa;
  1285. tx[1] = PROTOCOL_ADDR;
  1286. tx[2] = CurRxBuf[1];
  1287. tx[3] = PROTOCOL_MESSAGE_QUERY_RTC;
  1288. tx[4] = 0x0e;
  1289. tx[5] = 0x00;
  1290. tx[6] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.year/1000)+'0';
  1291. tx[7] = ((Charger.memory.EVSE_Config.data.item.SystemDateTime.year%1000)/100)+'0';
  1292. tx[8] = ((Charger.memory.EVSE_Config.data.item.SystemDateTime.year%100)/10)+'0';
  1293. tx[9] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.year%10) +'0';
  1294. tx[10] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.month/10) +'0';
  1295. tx[11] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.month%10) +'0';
  1296. tx[12] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.day/10) +'0';
  1297. tx[13] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.day%10) +'0';
  1298. tx[14] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.hour/10) +'0';
  1299. tx[15] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.hour%10) +'0';
  1300. tx[16] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.min/10) +'0';
  1301. tx[17] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.min%10) +'0';
  1302. tx[18] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.sec/10) +'0';
  1303. tx[19] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.sec%10) +'0';
  1304. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1305. {
  1306. chksum ^= tx[6 + idx];
  1307. }
  1308. tx[20] = chksum;
  1309. break;
  1310. case PROTOCOL_MESSAGE_QUERY_PRESENT_OUTPUT_CURRENT:
  1311. tx_len = 19;
  1312. tx[0] = 0xaa;
  1313. tx[1] = PROTOCOL_ADDR;
  1314. tx[2] = CurRxBuf[1];
  1315. tx[3] = PROTOCOL_MESSAGE_QUERY_PRESENT_OUTPUT_CURRENT;
  1316. tx[4] = 0x0c;
  1317. tx[5] = 0x00;
  1318. #ifdef FUNC_ZERO_CURRENT_WHEN_OUTP_OFF
  1319. if (Charger.Relay_Action == GPIO_RELAY_ACTION_ON)
  1320. {
  1321. tx[6] = (((Charger.Current[0]/10)>>0) & 0xff);
  1322. tx[7] = (((Charger.Current[0]/10)>>8) & 0xff);
  1323. tx[8] = (((Charger.Current[1]/10)>>0) & 0xff);
  1324. tx[9] = (((Charger.Current[1]/10)>>8) & 0xff);
  1325. tx[10] = (((Charger.Current[2]/10)>>0) & 0xff);
  1326. tx[11] = (((Charger.Current[2]/10)>>8) & 0xff);
  1327. }
  1328. else
  1329. {
  1330. tx[6] = 0;
  1331. tx[7] = 0;
  1332. tx[8] = 0;
  1333. tx[9] = 0;
  1334. tx[10] = 0;
  1335. tx[11] = 0;
  1336. }
  1337. #else
  1338. tx[6] = (((Charger.Current[0]/10)>>0) & 0xff);
  1339. tx[7] = (((Charger.Current[0]/10)>>8) & 0xff);
  1340. tx[8] = (((Charger.Current[1]/10)>>0) & 0xff);
  1341. tx[9] = (((Charger.Current[1]/10)>>8) & 0xff);
  1342. tx[10] = (((Charger.Current[2]/10)>>0) & 0xff);
  1343. tx[11] = (((Charger.Current[2]/10)>>8) & 0xff);
  1344. #endif
  1345. tx[12] = 0x00;
  1346. tx[13] = 0x00;
  1347. tx[14] = 0x00;
  1348. tx[15] = 0x00;
  1349. tx[16] = 0x00;
  1350. tx[17] = 0x00;
  1351. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1352. {
  1353. chksum ^= tx[6 + idx];
  1354. }
  1355. tx[18] = chksum;
  1356. break;
  1357. //edward 2019/08/27
  1358. case PROTOCOL_MESSAGE_QUERY_EVSE_STATE:
  1359. tx_len = 21;
  1360. tx[0] = 0xaa;
  1361. tx[1] = PROTOCOL_ADDR;
  1362. tx[2] = CurRxBuf[1];
  1363. tx[3] = PROTOCOL_MESSAGE_QUERY_EVSE_STATE;
  1364. tx[4] = 14;
  1365. tx[5] = 0x00;
  1366. tx[6] = Charger.CP_State;
  1367. #ifdef MODIFY_AC_EVSE_STATUS_MAX_CHARGING_CURRENT_VARIABLE
  1368. tx[7] = (Charger.AC_MaxChargingCurrentOrDuty&0xff);
  1369. tx[8] = Charger.AC_MaxChargingCurrentOrDuty>>8;
  1370. #else
  1371. tx[7] = (Charger.memory.EVSE_Config.data.item.MaxChargingCurrent&0xff);
  1372. tx[8] = Charger.memory.EVSE_Config.data.item.MaxChargingCurrent>>8;
  1373. #endif
  1374. uint16_t transfer_float;
  1375. {
  1376. #ifdef FUNC_CP_ADC_MODIFY
  1377. transfer_float = (uint16_t)(CpDetectionResult.PositiveValue == 0 ? 0 : HTK_GetPosFromZero(CP_GET_OPA_VIN_USE_ADC(CpDetectionResult.PositiveValue)) * 100);
  1378. #else
  1379. //transfer_float = (uint16_t)((float)(((1.59131-((3.3*CpDetectionResult.PositiveValue)/4095))*(475/53.6)))*100);
  1380. #endif
  1381. }
  1382. tx[9] = (transfer_float&0xff);
  1383. tx[10] = (transfer_float&0xff00)>>8;
  1384. {
  1385. #ifdef FUNC_CP_ADC_MODIFY
  1386. transfer_float = (uint16_t)(CpDetectionResult.NegativeValue == 0 ? 0 : HTK_GetNegFromZero(CP_GET_OPA_VIN_USE_ADC(CpDetectionResult.NegativeValue)) * 100);
  1387. #else
  1388. //transfer_float = (uint16_t)((float)(((((3.3*CpDetectionResult.NegativeValue)/4095)-1.59131)*(475/53.6)))*100);
  1389. #endif
  1390. }
  1391. tx[11] = (transfer_float&0xff);
  1392. tx[12] = (transfer_float&0xff00)>>8;
  1393. tx[13] = 0x00;
  1394. if(Charger.Relay_Action == GPIO_RELAY_ACTION_ON)
  1395. {
  1396. tx[14] = 0x01;
  1397. }
  1398. else
  1399. {
  1400. tx[14] = 0x00;
  1401. }
  1402. tx[15] = 0x00;
  1403. tx[16] = 0x00;
  1404. tx[17] = 0x00;
  1405. tx[18] = Charger.maxRatingCurrent;
  1406. tmpBuf += (HAL_GPIO_ReadPin(IN_AC_Current_Set1_GPIO_Port, IN_AC_Current_Set1_Pin)?0:1)<<0;
  1407. tmpBuf += (HAL_GPIO_ReadPin(IN_AC_Current_Set2_GPIO_Port, IN_AC_Current_Set2_Pin)?0:1)<<1;
  1408. tmpBuf += (HAL_GPIO_ReadPin(IN_AC_Current_Set3_GPIO_Port, IN_AC_Current_Set3_Pin)?0:1)<<2;
  1409. tmpBuf += (HAL_GPIO_ReadPin(IN_AC_Current_Set4_GPIO_Port, IN_AC_Current_Set4_Pin)?0:1)<<3;
  1410. tmpBuf += (HAL_GPIO_ReadPin(IN_GridType_Select_GPIO_Port, IN_GridType_Select_Pin)?0:1)<<4;
  1411. tmpBuf += (HAL_GPIO_ReadPin(IN_Grid_Groundding_GPIO_Port, IN_Grid_Groundding_Pin)?0:1)<<5;
  1412. tx[19] = tmpBuf;
  1413. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1414. {
  1415. chksum ^= tx[6 + idx];
  1416. }
  1417. tx[20] = chksum;
  1418. break;
  1419. case PROTOCOL_MESSAGE_QUERY_EVSE_ALARM:
  1420. tx_len = 11;
  1421. tx[0] = 0xaa;
  1422. tx[1] = PROTOCOL_ADDR;
  1423. tx[2] = CurRxBuf[1];
  1424. tx[3] = PROTOCOL_MESSAGE_QUERY_EVSE_ALARM;
  1425. tx[4] = 4;
  1426. tx[5] = 0x00;
  1427. tx[6] = (Charger.Alarm_Code&0xff);
  1428. tx[7] = (Charger.Alarm_Code&0xff00)>>8;
  1429. tx[8] = (Charger.Alarm_Code&0xff0000)>>16;
  1430. tx[9] = (Charger.Alarm_Code&0xff000000)>>24;
  1431. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1432. {
  1433. chksum ^= tx[6 + idx];
  1434. }
  1435. tx[10] = chksum;
  1436. break;
  1437. case PROTOCOL_MESSAGE_QUERY_BLE_STATE:
  1438. tx_len = 10;
  1439. tx[0] = 0xaa;
  1440. tx[1] = PROTOCOL_ADDR;
  1441. tx[2] = CurRxBuf[1];
  1442. tx[3] = PROTOCOL_MESSAGE_QUERY_BLE_STATE;
  1443. tx[4] = 3;
  1444. tx[5] = 0x00;
  1445. tx[6] = 0x00 ;
  1446. tx[7] = 0x00 ;
  1447. tx[8] = 0x00 ;
  1448. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1449. {
  1450. chksum ^= tx[6 + idx];
  1451. }
  1452. tx[9] = chksum;
  1453. break;
  1454. case PROTOCOL_MESSAGE_QUERY_BLE_LOGIC_CENTRAL_ID:
  1455. tx_len = 39;
  1456. tx[0] = 0xaa;
  1457. tx[1] = PROTOCOL_ADDR;
  1458. tx[2] = CurRxBuf[1];
  1459. tx[3] = PROTOCOL_MESSAGE_QUERY_BLE_LOGIC_CENTRAL_ID;
  1460. tx[4] = 32;
  1461. tx[5] = 0x00;
  1462. tx[6] = 'P';
  1463. tx[7] = 'H';
  1464. tx[8] = 'I';
  1465. tx[9] = 'H';
  1466. tx[10] = 'O';
  1467. tx[11] = 'N';
  1468. tx[12] = 'G';
  1469. tx[13] = ' ';
  1470. tx[14] = 'A';
  1471. tx[15] = 'C';
  1472. tx[16] = '-';
  1473. tx[17] = 'S';
  1474. tx[18] = 'i';
  1475. tx[19] = 'm';
  1476. tx[20] = 'p';
  1477. tx[21] = 'l';
  1478. tx[22] = 'e';
  1479. tx[23] = ' ';
  1480. tx[24] = '1';
  1481. tx[25] = '2';
  1482. tx[26] = '3';
  1483. tx[27] = '4';
  1484. tx[28] = '5';
  1485. tx[29] = '6';
  1486. tx[30] = '7';
  1487. tx[31] = '8';
  1488. tx[32] = '9';
  1489. tx[33] = 'A';
  1490. tx[34] = 'B';
  1491. tx[35] = 'C';
  1492. tx[36] = 'D';
  1493. tx[37] = 'E';
  1494. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1495. {
  1496. chksum ^= tx[6 + idx];
  1497. }
  1498. tx[38] = chksum;
  1499. break;
  1500. case PROTOCOL_MESSAGE_QUERY_POWER_CONSUME:
  1501. tx_len = 11;
  1502. tx[0] = 0xaa;
  1503. tx[1] = PROTOCOL_ADDR;
  1504. tx[2] = CurRxBuf[1];
  1505. tx[3] = PROTOCOL_MESSAGE_QUERY_POWER_CONSUME;
  1506. tx[4] = 4;
  1507. tx[5] = 0x00;
  1508. tx[6] = ((Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative/100)&0xff);
  1509. tx[7] = ((Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative/100)&0xff00)>>8;
  1510. tx[8] = ((Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative/100)&0xff0000)>>16;
  1511. tx[9] = ((Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative/100)&0xff000000)>>24;
  1512. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1513. {
  1514. chksum ^= tx[6 + idx];
  1515. }
  1516. tx[10] = chksum;
  1517. break;
  1518. case PROTOCOL_MESSAGE_QUERY_GUN_PLUGIN_TIMES:
  1519. tx_len = 11;
  1520. tx[0] = 0xaa;
  1521. tx[1] = PROTOCOL_ADDR;
  1522. tx[2] = CurRxBuf[1];
  1523. tx[3] = PROTOCOL_MESSAGE_QUERY_GUN_PLUGIN_TIMES;
  1524. tx[4] = 4;
  1525. tx[5] = 0x00;
  1526. tx[6] = (Charger.memory.EVSE_Config.data.item.AcPlugInTimes&0xff);
  1527. tx[7] = (Charger.memory.EVSE_Config.data.item.AcPlugInTimes&0xff00)>>8;
  1528. tx[8] = (Charger.memory.EVSE_Config.data.item.AcPlugInTimes&0xff0000)>>16;
  1529. tx[9] = (Charger.memory.EVSE_Config.data.item.AcPlugInTimes&0xff000000)>>24;
  1530. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1531. {
  1532. chksum ^= tx[6 + idx];
  1533. }
  1534. tx[10] = chksum;
  1535. break;
  1536. case PROTOCOL_MESSAGE_QUERY_EVSE_AUTH_MODE:
  1537. tx_len = 9;
  1538. tx[0] = 0xaa;
  1539. tx[1] = PROTOCOL_ADDR;
  1540. tx[2] = CurRxBuf[1];
  1541. tx[3] = PROTOCOL_MESSAGE_QUERY_EVSE_AUTH_MODE;
  1542. tx[4] = 2;
  1543. tx[5] = 0x00;
  1544. tx[6] = Charger.memory.EVSE_Config.data.item.AuthMode;
  1545. tx[7] = Charger.memory.EVSE_Config.data.item.OfflinePolicy;
  1546. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1547. {
  1548. chksum ^= tx[6 + idx];
  1549. }
  1550. tx[8] = chksum;
  1551. break;
  1552. case PROTOCOL_MESSAGE_QUERY_PRESENT_LEAK_CURRENT:
  1553. tx_len = 9;
  1554. tx[0] = 0xaa;
  1555. tx[1] = PROTOCOL_ADDR;
  1556. tx[2] = CurRxBuf[1];
  1557. tx[3] = PROTOCOL_MESSAGE_QUERY_PRESENT_LEAK_CURRENT;
  1558. tx[4] = 2;
  1559. tx[5] = 0x00;
  1560. tx[6] = Charger.Leak_Current/100;
  1561. tx[7] = 0;
  1562. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1563. {
  1564. chksum ^= tx[6 + idx];
  1565. }
  1566. tx[8] = chksum;
  1567. break;
  1568. /*
  1569. Config message
  1570. */
  1571. case PROTOCOL_MESSAGE_CONFIG_FAN_SPEED:
  1572. tx_len = 8;
  1573. tx[0] = 0xaa;
  1574. tx[1] = PROTOCOL_ADDR;
  1575. tx[2] = CurRxBuf[1];
  1576. tx[3] = PROTOCOL_MESSAGE_CONFIG_FAN_SPEED;
  1577. tx[4] = 0x01;
  1578. tx[5] = 0x00;
  1579. tx[6] = 0x01;
  1580. tx[7] = 0x01;
  1581. break;
  1582. case PROTOCOL_MESSAGE_CONFIG_SN:
  1583. tx_len = 8;
  1584. tx[0] = 0xaa;
  1585. tx[1] = PROTOCOL_ADDR;
  1586. tx[2] = CurRxBuf[1];
  1587. tx[3] = PROTOCOL_MESSAGE_CONFIG_SN;
  1588. tx[4] = 0x01;
  1589. tx[5] = 0x00;
  1590. #ifdef MODIFY_CONFIG_SN
  1591. {
  1592. uint16_t len = HTK_U16(CurRxBuf[4]);
  1593. if (len < sizeof(Charger.memory.EVSE_Config.data.item.ModelName) && HTK_IS_DISP_CHAR(CurRxBuf[14]))
  1594. {
  1595. //OK
  1596. tx[6] = 0x01;
  1597. tx[7] = 0x01;
  1598. memset(Charger.memory.EVSE_Config.data.item.SerialNumber, 0, ARRAY_SIZE(Charger.memory.EVSE_Config.data.item.SerialNumber));
  1599. memset(Charger.memory.EVSE_Config.data.item.System_ID, 0, ARRAY_SIZE(Charger.memory.EVSE_Config.data.item.System_ID));
  1600. for(uint16_t idx=0;idx<(len-8);idx++)
  1601. {
  1602. Charger.memory.EVSE_Config.data.item.SerialNumber[idx] = CurRxBuf[14+idx];
  1603. }
  1604. Charger.memory.EVSE_Config.data.item.SerialNumber[len-8] = '\0';
  1605. memcpy(Charger.memory.EVSE_Config.data.item.System_ID, Charger.memory.EVSE_Config.data.item.ModelName, strlen(Charger.memory.EVSE_Config.data.item.ModelName));
  1606. memcpy(&Charger.memory.EVSE_Config.data.item.System_ID[strlen(Charger.memory.EVSE_Config.data.item.ModelName)], Charger.memory.EVSE_Config.data.item.SerialNumber, len);
  1607. XP("#CONFIG_SN: %s\r\n", Charger.memory.EVSE_Config.data.item.SerialNumber);
  1608. //parseVersionInfoFromModelname() ;
  1609. Charger.memory.EVSE_Config.op_bits.update = ON;
  1610. break;
  1611. }
  1612. else
  1613. {
  1614. //NG
  1615. tx[6] = 0x00;
  1616. tx[7] = 0x00;
  1617. break;
  1618. }
  1619. }
  1620. #else //MODIFY_CONFIG_SN
  1621. // tx[6] = 0x01;
  1622. // tx[7] = 0x01;
  1623. //
  1624. // memset(Charger.memory.EVSE_Config.data.item.SerialNumber, 0x00, sizeof(Charger.memory.EVSE_Config.data.item.SerialNumber)/sizeof(Charger.memory.EVSE_Config.data.item.SerialNumber[0]));
  1625. // memset(Charger.memory.EVSE_Config.data.item.System_ID, 0x00, sizeof(Charger.memory.EVSE_Config.data.item.System_ID)/sizeof(Charger.memory.EVSE_Config.data.item.System_ID[0]));
  1626. // for(uint16_t idx=0;idx<((CurRxBuf[4] | (CurRxBuf[5]<<8))-8);idx++)
  1627. // {
  1628. // Charger.memory.EVSE_Config.data.item.SerialNumber[idx] = CurRxBuf[14+idx];
  1629. // }
  1630. // Charger.memory.EVSE_Config.data.item.SerialNumber[(CurRxBuf[4] | (CurRxBuf[5]<<8))-8] = '\0';
  1631. // memcpy(Charger.memory.EVSE_Config.data.item.System_ID, Charger.memory.EVSE_Config.data.item.ModelName, strlen(Charger.memory.EVSE_Config.data.item.ModelName));
  1632. // memcpy(&Charger.memory.EVSE_Config.data.item.System_ID[strlen(Charger.memory.EVSE_Config.data.item.ModelName)], Charger.memory.EVSE_Config.data.item.SerialNumber, (CurRxBuf[4] | (CurRxBuf[5]<<8)));
  1633. //
  1634. // parseVersionInfoFromModelname() ;
  1635. // Charger.memory.EVSE_Config.op_bits.update = ON;
  1636. // break;
  1637. #endif //MODIFY_CONFIG_SN
  1638. case PROTOCOL_MESSAGE_CONFIG_MODEL_NAME:
  1639. tx_len = 8;
  1640. tx[0] = 0xaa;
  1641. tx[1] = PROTOCOL_ADDR;
  1642. tx[2] = CurRxBuf[1];
  1643. tx[3] = PROTOCOL_MESSAGE_CONFIG_MODEL_NAME;
  1644. tx[4] = 0x01;
  1645. tx[5] = 0x00;
  1646. #ifdef MODIFY_CONFIG_MODEL_NAME
  1647. {
  1648. uint16_t len = HTK_U16(CurRxBuf[4]);
  1649. if (len < sizeof(Charger.memory.EVSE_Config.data.item.ModelName) && HTK_IS_DISP_CHAR(CurRxBuf[6]))
  1650. {
  1651. //OK
  1652. tx[6] = 0x01;
  1653. tx[7] = 0x01;
  1654. memset(Charger.memory.EVSE_Config.data.item.ModelName, 0, ARRAY_SIZE(Charger.memory.EVSE_Config.data.item.ModelName));
  1655. memset(Charger.memory.EVSE_Config.data.item.System_ID, 0, ARRAY_SIZE(Charger.memory.EVSE_Config.data.item.System_ID));
  1656. for(uint16_t idx=0;idx<len;idx++)
  1657. {
  1658. Charger.memory.EVSE_Config.data.item.ModelName[idx] = CurRxBuf[6+idx];
  1659. }
  1660. Charger.memory.EVSE_Config.data.item.ModelName[len] = '\0';
  1661. memcpy(Charger.memory.EVSE_Config.data.item.System_ID, Charger.memory.EVSE_Config.data.item.ModelName, strlen(Charger.memory.EVSE_Config.data.item.ModelName));
  1662. memcpy(&Charger.memory.EVSE_Config.data.item.System_ID[strlen(Charger.memory.EVSE_Config.data.item.ModelName)], Charger.memory.EVSE_Config.data.item.SerialNumber, len);
  1663. XP("#CONFIG_MODEL_NAME: %s\r\n", Charger.memory.EVSE_Config.data.item.ModelName);
  1664. #ifdef FUNC_AUTO_MODIFY_CCID_MODULE
  1665. Charger.m_bCCID_MustModify = HTK_TRUE;
  1666. #endif
  1667. parseVersionInfoFromModelname() ;
  1668. Charger.memory.EVSE_Config.op_bits.update = ON;
  1669. break;
  1670. }
  1671. else
  1672. {
  1673. //NG
  1674. tx[6] = 0x00;
  1675. tx[7] = 0x00;
  1676. break;
  1677. }
  1678. }
  1679. #else //MODIFY_CONFIG_MODEL_NAME
  1680. // tx[6] = 0x01;
  1681. // tx[7] = 0x01;
  1682. //
  1683. // memset(Charger.memory.EVSE_Config.data.item.ModelName, 0x00, sizeof(Charger.memory.EVSE_Config.data.item.ModelName)/sizeof(Charger.memory.EVSE_Config.data.item.ModelName[0]));
  1684. // memset(Charger.memory.EVSE_Config.data.item.System_ID, 0x00, sizeof(Charger.memory.EVSE_Config.data.item.System_ID)/sizeof(Charger.memory.EVSE_Config.data.item.System_ID[0]));
  1685. // for(uint16_t idx=0;idx<(CurRxBuf[4] | (CurRxBuf[5]<<8));idx++)
  1686. // {
  1687. // Charger.memory.EVSE_Config.data.item.ModelName[idx] = CurRxBuf[6+idx];
  1688. // }
  1689. // Charger.memory.EVSE_Config.data.item.ModelName[(CurRxBuf[4] | (CurRxBuf[5]<<8))] = '\0';
  1690. // memcpy(Charger.memory.EVSE_Config.data.item.System_ID, Charger.memory.EVSE_Config.data.item.ModelName, strlen(Charger.memory.EVSE_Config.data.item.ModelName));
  1691. // memcpy(&Charger.memory.EVSE_Config.data.item.System_ID[strlen(Charger.memory.EVSE_Config.data.item.ModelName)], Charger.memory.EVSE_Config.data.item.SerialNumber, (CurRxBuf[4] | (CurRxBuf[5]<<8)));
  1692. //
  1693. // parseVersionInfoFromModelname() ;
  1694. // Charger.memory.EVSE_Config.op_bits.update = ON;
  1695. // break;
  1696. #endif //MODIFY_CONFIG_MODEL_NAME
  1697. case PROTOCOL_MESSAGE_CONFIG_PARAMETER:
  1698. tx_len = 8;
  1699. tx[0] = 0xaa;
  1700. tx[1] = PROTOCOL_ADDR;
  1701. tx[2] = CurRxBuf[1];
  1702. tx[3] = PROTOCOL_MESSAGE_CONFIG_PARAMETER;
  1703. tx[4] = 0x01;
  1704. tx[5] = 0x00;
  1705. tx[6] = 0x01;
  1706. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1707. {
  1708. chksum ^= tx[6 + idx];
  1709. }
  1710. tx[7] = chksum; //checksum
  1711. switch(CurRxBuf[6])
  1712. {
  1713. case INPUT_L1_AC_VOLTAGE:
  1714. Charger.memory.EVSE_Config.data.item.Correction_VL1[CurRxBuf[7]][0] = adc_value.ADC3_IN9_Voltage_L1.value/10;
  1715. Charger.memory.EVSE_Config.data.item.Correction_VL1[CurRxBuf[7]][1] = CurRxBuf[8] | (CurRxBuf[9]<<8);
  1716. break;
  1717. case INPUT_L2_AC_VOLTAGE:
  1718. Charger.memory.EVSE_Config.data.item.Correction_VL2[CurRxBuf[7]][0] = Charger.Voltage[1];
  1719. Charger.memory.EVSE_Config.data.item.Correction_VL2[CurRxBuf[7]][1] = CurRxBuf[8] | (CurRxBuf[9]<<8);
  1720. break;
  1721. case INPUT_L3_AC_VOLTAGE:
  1722. Charger.memory.EVSE_Config.data.item.Correction_VL3[CurRxBuf[7]][0] = Charger.Voltage[2];
  1723. Charger.memory.EVSE_Config.data.item.Correction_VL3[CurRxBuf[7]][1] = CurRxBuf[8] | (CurRxBuf[9]<<8);
  1724. break;
  1725. case OUTPUT_L1_CURRENT:
  1726. Charger.memory.EVSE_Config.data.item.Correction_CL1[CurRxBuf[7]][0] = adc_value.ADC3_IN7_Current_L1.value/10;
  1727. Charger.memory.EVSE_Config.data.item.Correction_CL1[CurRxBuf[7]][1] = CurRxBuf[8] | (CurRxBuf[9]<<8);
  1728. break;
  1729. case OUTPUT_L2_CURRENT:
  1730. Charger.memory.EVSE_Config.data.item.Correction_CL2[CurRxBuf[7]][0] = Charger.Current[0];
  1731. Charger.memory.EVSE_Config.data.item.Correction_CL2[CurRxBuf[7]][1] = CurRxBuf[8] | (CurRxBuf[9]<<8);
  1732. break;
  1733. case OUTPUT_L3_CURRENT:
  1734. Charger.memory.EVSE_Config.data.item.Correction_CL3[CurRxBuf[7]][0] = Charger.Current[0];
  1735. Charger.memory.EVSE_Config.data.item.Correction_CL3[CurRxBuf[7]][1] = CurRxBuf[8] | (CurRxBuf[9]<<8);
  1736. break;
  1737. case LEAK_CURRENT:
  1738. Charger.memory.EVSE_Config.data.item.Correction_Leak[CurRxBuf[7]][0] = adc_value.ADC2_IN6_GF.value; // 2500->25mA
  1739. Charger.memory.EVSE_Config.data.item.Correction_Leak[CurRxBuf[7]][1] = ((CurRxBuf[8] | (CurRxBuf[9]<<8))*100); // mA
  1740. DEBUG_INFO("Charger.memory.EVSE_Config.data.item.Correction_Leak[[0] = %d\r\n",Charger.memory.EVSE_Config.data.item.Correction_Leak[CurRxBuf[7]][0]);
  1741. DEBUG_INFO("Charger.memory.EVSE_Config.data.item.Correction_Leak[[1] = %d\r\n",Charger.memory.EVSE_Config.data.item.Correction_Leak[CurRxBuf[7]][1]);
  1742. break;
  1743. case GROUND_FAULT_VOLTAGE: //max 3.3V
  1744. Charger.memory.EVSE_Config.data.item.Correction_GMI[CurRxBuf[7]][0] = (uint16_t)(adc_value.ADC3_IN4_GMI_VL1.value*10*3.3/4095) ;
  1745. Charger.memory.EVSE_Config.data.item.Correction_GMI[CurRxBuf[7]][1] = CurRxBuf[8] | (CurRxBuf[9]<<8);
  1746. break;
  1747. #ifdef FUNC_SIMU_CHARGE_CURRENT_OUTPUT
  1748. case SIMU_CHARGE_CURRENT_CMD:
  1749. Charger.m_bEnableSimuChargeCurrent =
  1750. (
  1751. (
  1752. CurRxBuf[7] == SIMU_CHARGE_CURRENT_ON &&
  1753. HTK_U16(CurRxBuf[8]) == SIMU_CHARGE_CURRENT_ON_PWD
  1754. ) ? 1 : 0
  1755. );
  1756. XP("#CSU_ParameterSimuCurr: %s\r\n", Charger.m_bEnableSimuChargeCurrent ? "ON" : "OFF");
  1757. break;
  1758. #endif //FUNC_SIMU_CHARGE_CURRENT_OUTPUT
  1759. #ifdef FUNC_SWITCH_RELAY_WELDING_PROTECT
  1760. case SIMU_SWITCH_RELAY_WELDING_CMD:
  1761. Charger.m_bDisableRelayWelding =
  1762. (
  1763. (
  1764. CurRxBuf[7] == SIMU_SWITCH_RELAY_WELDING_DISABLE &&
  1765. HTK_U16(CurRxBuf[8]) == SIMU_SWITCH_RELAY_WELDING_PWD
  1766. ) ? 1 : 0
  1767. );
  1768. XP("#CSU_ParameterDisableRelayWelding: %s\r\n", Charger.m_bDisableRelayWelding ? "ON" : "OFF");
  1769. Charger.m_bDisableCCID = Charger.m_bDisableRelayWelding;
  1770. XP("#CSU_ParameterDisableCCID: %s\r\n", Charger.m_bDisableCCID ? "ON" : "OFF");
  1771. Charger.m_bSetOCP_32A = Charger.m_bDisableRelayWelding;
  1772. XP("#CSU_ParameterSetOCP_32A: %s\r\n", Charger.m_bSetOCP_32A ? "ON" : "OFF");
  1773. break;
  1774. #endif //FUNC_SWITCH_RELAY_WELDING_PROTECT
  1775. }
  1776. #ifdef FUNC_SIMU_CHARGE_CURRENT_OUTPUT
  1777. #ifdef FUNC_SWITCH_RELAY_WELDING_PROTECT
  1778. if (CurRxBuf[6] != SIMU_CHARGE_CURRENT_CMD && CurRxBuf[6] != SIMU_SWITCH_RELAY_WELDING_CMD)
  1779. {
  1780. Charger.memory.EVSE_Config.op_bits.update = ON;
  1781. }
  1782. #else //FUNC_SWITCH_RELAY_WELDING_PROTECT
  1783. if (CurRxBuf[6] != SIMU_CHARGE_CURRENT_CMD)
  1784. {
  1785. Charger.memory.EVSE_Config.op_bits.update = ON;
  1786. }
  1787. #endif //FUNC_SWITCH_RELAY_WELDING_PROTECT
  1788. #else //FUNC_SIMU_CHARGE_CURRENT_OUTPUT
  1789. Charger.memory.EVSE_Config.op_bits.update = ON;
  1790. #endif //FUNC_SIMU_CHARGE_CURRENT_OUTPUT
  1791. break;
  1792. case PROTOCOL_MESSAGE_CONFIG_RELAY_OUTPUT:
  1793. tx_len = 8;
  1794. tx[0] = 0xaa;
  1795. tx[1] = PROTOCOL_ADDR;
  1796. tx[2] = CurRxBuf[1];
  1797. tx[3] = PROTOCOL_MESSAGE_CONFIG_RELAY_OUTPUT;
  1798. tx[4] = 0x01;
  1799. tx[5] = 0x00;
  1800. tx[6] = 0x01;
  1801. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1802. {
  1803. chksum ^= tx[6 + idx];
  1804. }
  1805. tx[7] = chksum;
  1806. #ifndef USE_OLD_ONE_STEP_CHARGE_FOR_AW_NOODOE
  1807. if (
  1808. #ifdef MODIFY_PROTOCOL_MESSAGE_CONFIG_RELAY_OUTPUT_CHECK_LEGACY_REQUEST
  1809. Charger.am3352.isRequestOn &&
  1810. #endif
  1811. #ifdef MODIFY_PROTOCOL_MESSAGE_CONFIG_RELAY_OUTPUT_CHECK_ALARM_CODE
  1812. Charger.Alarm_Code == 0 &&
  1813. #endif
  1814. #ifdef MODIFY_PROTOCOL_MESSAGE_CONFIG_RELAY_OUTPUT_CHECK_CP_PWM
  1815. Charger.m_CP_CurPWM != PWM_DUTY_FULL &&
  1816. #endif
  1817. Charger.CP_State == SYSTEM_STATE_C &&
  1818. CurRxBuf[9] == 0x07 //Main relay (0111b)
  1819. )
  1820. {
  1821. Charger.Relay_Action = GPIO_RELAY_ACTION_ON;
  1822. #ifdef FUNC_RELAY_OFF_WHEN_GET_TWICE_OFF_CMD
  1823. CsuSetRelayOffCount = 0;
  1824. #endif
  1825. }
  1826. else
  1827. {
  1828. #ifdef FUNC_RELAY_OFF_WHEN_GET_TWICE_OFF_CMD
  1829. if (++CsuSetRelayOffCount >= 2)
  1830. {
  1831. CsuSetRelayOffCount = 0;
  1832. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF ;
  1833. }
  1834. #else //FUNC_RELAY_OFF_WHEN_GET_TWICE_OFF_CMD
  1835. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF ;
  1836. #endif //FUNC_RELAY_OFF_WHEN_GET_TWICE_OFF_CMD
  1837. }
  1838. #endif //USE_OLD_ONE_STEP_CHARGE_FOR_AW_NOODOE
  1839. #ifdef FUNC_CSU_CMD_DEBUG
  1840. XP("#CSU_Relay: %d\r\n", CurRxBuf[9]);
  1841. #endif
  1842. DEBUG_INFO("Set Relay Action By CSU: %d => %d\r\n", CurRxBuf[9], Charger.Relay_Action);
  1843. break;
  1844. case PROTOCOL_MESSAGE_CONFIG_GPIO_OUTPUT:
  1845. tx_len = 8;
  1846. tx[0] = 0xaa;
  1847. tx[1] = PROTOCOL_ADDR;
  1848. tx[2] = CurRxBuf[1];
  1849. tx[3] = PROTOCOL_MESSAGE_CONFIG_GPIO_OUTPUT;
  1850. tx[4] = 0x01;
  1851. tx[5] = 0x00;
  1852. tx[6] = CurRxBuf[6];
  1853. tx[7] = 0x00 ^ CurRxBuf[6];
  1854. break;
  1855. case PROTOCOL_MESSAGE_CONFIG_RTC:
  1856. tx_len = 8;
  1857. tx[0] = 0xaa;
  1858. tx[1] = PROTOCOL_ADDR;
  1859. tx[2] = CurRxBuf[1];
  1860. tx[3] = PROTOCOL_MESSAGE_CONFIG_RTC;
  1861. tx[4] = 0x01;
  1862. tx[5] = 0x00;
  1863. tx[6] = 0x01;
  1864. tx[7] = 0x01;
  1865. setTime.Hours = (CurRxBuf[14]-'0')*10 + (CurRxBuf[15]-'0');
  1866. setTime.Minutes = (CurRxBuf[16]-'0')*10 + (CurRxBuf[17]-'0');
  1867. setTime.Seconds = (CurRxBuf[18]-'0')*10 + (CurRxBuf[19]-'0');
  1868. setTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  1869. setTime.StoreOperation = RTC_STOREOPERATION_RESET;
  1870. if (HAL_RTC_SetTime(&hrtc, &setTime, RTC_FORMAT_BIN) != HAL_OK)
  1871. {
  1872. Error_Handler();
  1873. }
  1874. setDate.Year = (CurRxBuf[6]-'0')*1000 + (CurRxBuf[7]-'0')*100 + (CurRxBuf[8]-'0')*10 + (CurRxBuf[9]-'0') - 2000;
  1875. setDate.Month = (CurRxBuf[10]-'0')*10+ (CurRxBuf[11]-'0');
  1876. setDate.Date = (CurRxBuf[12]-'0')*10 + (CurRxBuf[13]-'0');
  1877. if (HAL_RTC_SetDate(&hrtc, &setDate, RTC_FORMAT_BIN) != HAL_OK)
  1878. {
  1879. Error_Handler();
  1880. }
  1881. break;
  1882. case PROTOCOL_MESSAGE_CONFIG_LED_ACTION_ALARM:
  1883. #ifdef FUNC_DISABLE_LED_CMD_CHECK
  1884. //do nothing
  1885. #else
  1886. if((Charger.am3352.LedActionState != CurRxBuf[6])||(Charger.am3352.LedAlarmState != (CurRxBuf[7]|(CurRxBuf[8]<<8)|(CurRxBuf[9]<<16)|(CurRxBuf[10]<<24))))
  1887. #endif
  1888. {
  1889. Charger.am3352.LedActionState = CurRxBuf[6];
  1890. Charger.am3352.LedAlarmState = (CurRxBuf[7]|(CurRxBuf[8]<<8)|(CurRxBuf[9]<<16)|(CurRxBuf[10]<<24));
  1891. #ifdef FUNC_SHOW_CSU_CONFIG_LED_ACTION_INFO
  1892. XP("#CSU_SetLed (%d, %d)\r\n", Charger.am3352.LedActionState, Charger.am3352.LedAlarmState);
  1893. #endif
  1894. switch(Charger.am3352.LedActionState)
  1895. {
  1896. case LED_ACTION_INIT:
  1897. setLedMotion(LED_ACTION_INIT);
  1898. break;
  1899. case LED_ACTION_IDLE:
  1900. setLedMotion(LED_ACTION_IDLE);
  1901. break;
  1902. case LED_ACTION_AUTHED:
  1903. setLedMotion(LED_ACTION_AUTHED);
  1904. break;
  1905. case LED_ACTION_CONNECTED:
  1906. setLedMotion(LED_ACTION_CONNECTED);
  1907. break;
  1908. case LED_ACTION_CHARGING:
  1909. setLedMotion(LED_ACTION_CHARGING);
  1910. break;
  1911. case LED_ACTION_STOP:
  1912. setLedMotion(LED_ACTION_STOP);
  1913. break;
  1914. case LED_ACTION_ALARM:
  1915. setLedMotion(LED_ACTION_ALARM);
  1916. break;
  1917. case LED_ACTION_MAINTAIN:
  1918. setLedMotion(LED_ACTION_MAINTAIN);
  1919. break;
  1920. case LED_ACTION_RFID_PASS:
  1921. setLedMotion(LED_ACTION_RFID_PASS);
  1922. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  1923. break;
  1924. case LED_ACTION_RFID_FAIL:
  1925. setLedMotion(LED_ACTION_RFID_FAIL);
  1926. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  1927. break;
  1928. case LED_ACTION_BLE_CONNECT:
  1929. setLedMotion(LED_ACTION_BLE_CONNECT);
  1930. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  1931. break;
  1932. case LED_ACTION_BLE_DISABLE:
  1933. setLedMotion(LED_ACTION_BLE_DISABLE);
  1934. break;
  1935. case LED_ACTION_ALL_OFF:
  1936. setLedMotion(LED_ACTION_ALL_OFF);
  1937. break;
  1938. case LED_ACTION_INTERNET_DISCONNECT:
  1939. setLedMotion(LED_ACTION_INTERNET_DISCONNECT);
  1940. break;
  1941. case LED_ACTION_HANDSHAKE_FAIL:
  1942. setLedMotion(LED_ACTION_HANDSHAKE_FAIL);
  1943. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  1944. break;
  1945. case LED_ACTION_RESTORE_SETTING:
  1946. setLedMotion(LED_ACTION_RESTORE_SETTING);
  1947. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  1948. break;
  1949. default:
  1950. break;
  1951. }
  1952. }
  1953. tx_len = 8;
  1954. tx[0] = 0xaa;
  1955. tx[1] = PROTOCOL_ADDR;
  1956. tx[2] = CurRxBuf[1];
  1957. tx[3] = PROTOCOL_MESSAGE_CONFIG_LED_ACTION_ALARM;
  1958. tx[4] = 0x01;
  1959. tx[5] = 0x00;
  1960. tx[6] = 0x01;
  1961. tx[7] = 0x01;
  1962. break;
  1963. case PROTOCOL_MESSAGE_CONFIG_MAX_CURRENT_PWM_DUTY:
  1964. tx_len = 8;
  1965. tx[0] = 0xaa;
  1966. tx[1] = PROTOCOL_ADDR;
  1967. tx[2] = CurRxBuf[1];
  1968. tx[3] = PROTOCOL_MESSAGE_CONFIG_MAX_CURRENT_PWM_DUTY;
  1969. tx[4] = 0x01;
  1970. tx[5] = 0x00;
  1971. #ifdef FUNC_CSU_CMD_DEBUG
  1972. XP("#CSU_SetCpPwmDuty: %d %s\r\n", CurRxBuf[6],
  1973. (Charger.m_bBlockCsuCpPwmDuty || Charger.m_bBlockCsuCpPwmDutyWhenAlarmCpF) ? "(Block)" : "");
  1974. #endif
  1975. if (
  1976. #ifdef FUNC_BLOCK_CSU_CONFIG_CP_PWM_DUTY
  1977. !Charger.m_bBlockCsuCpPwmDuty &&
  1978. #endif
  1979. #ifdef FUNC_BLOCK_CSU_CMD_SET_DUTY_WHEN_ALARM_CP_F_STATE
  1980. !Charger.m_bBlockCsuCpPwmDutyWhenAlarmCpF &&
  1981. #endif
  1982. (
  1983. (CurRxBuf[6] <= 32 && CurRxBuf[6] >= 6) ||
  1984. (CurRxBuf[6] == PWM_DUTY_OFF) ||
  1985. (CurRxBuf[6] == 1) ||
  1986. (CurRxBuf[6] == 5) ||
  1987. (CurRxBuf[6] == 100)
  1988. )
  1989. )
  1990. {
  1991. #ifdef MODIFY_AC_EVSE_STATUS_MAX_CHARGING_CURRENT_VARIABLE
  1992. Charger.AC_MaxChargingCurrentOrDuty = CurRxBuf[6];
  1993. #endif
  1994. tx[6] = 0x00;
  1995. if(CurRxBuf[6] <= 32 && CurRxBuf[6] >= 6)
  1996. {
  1997. Charger.memory.EVSE_Config.data.item.MaxChargingCurrent = CurRxBuf[6];
  1998. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  1999. user_pwm_setvalue(PWM_CH_CP, GetCpPwmDuty(Charger.memory.EVSE_Config.data.item.MaxChargingCurrent));
  2000. #else
  2001. //user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_651(Charger.memory.EVSE_Config.data.item.MaxChargingCurrent));
  2002. #endif
  2003. tx[6] = 0x01;
  2004. }
  2005. else if (CurRxBuf[6] == PWM_DUTY_OFF)
  2006. {
  2007. user_pwm_setvalue(PWM_CH_CP, 0); // State F
  2008. tx[6] = 0x01;
  2009. }
  2010. else if (CurRxBuf[6] == 5)
  2011. {
  2012. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_5);
  2013. tx[6] = 0x01;
  2014. }
  2015. else if (CurRxBuf[6] == 100)
  2016. {
  2017. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  2018. tx[6] = 0x01;
  2019. }
  2020. //control pilot E
  2021. if (CurRxBuf[6] == 1)
  2022. {
  2023. HAL_GPIO_WritePin(OUT_StateE_GPIO_Port, OUT_StateE_Pin, GPIO_PIN_SET); // ON // State E
  2024. user_pwm_setvalue(PWM_CH_CP, 0); //Temporarily, wait for EE to change the hardware and delete
  2025. tx[6] = 0x01;
  2026. }
  2027. else
  2028. {
  2029. HAL_GPIO_WritePin(OUT_StateE_GPIO_Port, OUT_StateE_Pin, GPIO_PIN_RESET); //OFF
  2030. }
  2031. XP("#CSU_SetCpPwmDuty_Final: %d\r\n", CurRxBuf[6]);
  2032. }
  2033. else
  2034. {
  2035. tx[6] = 0x00;
  2036. DEBUG_INFO("CSU_SetCpPwmDuty [NG] (%d) !!!!! \r\n", CurRxBuf[6]);
  2037. }
  2038. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  2039. {
  2040. chksum ^= tx[6 + idx];
  2041. }
  2042. tx[7] = chksum; //checksum
  2043. break;
  2044. case PROTOCOL_MESSAGE_CONFIG_LEGACY_REQUEST:
  2045. #ifdef FUNC_CSU_CMD_DEBUG
  2046. XP("#CSU_SetRequest: %d\r\n", CurRxBuf[6]);
  2047. #endif
  2048. if((CurRxBuf[6]&SETTING_LEGACY_REQUEST)>0)
  2049. {
  2050. #ifdef FUNC_REQUEST_OFF_WHEN_GET_TWICE_OFF_CMD
  2051. CsuSetRequestOffCount = 0;
  2052. #endif
  2053. Charger.am3352.isRequestOff = OFF;
  2054. Charger.rfid.op_bits.reqStart = ON;
  2055. if(!Charger.am3352.isRequestOn)
  2056. {
  2057. DEBUG_INFO("#Request Pin On.\r\n");
  2058. Charger.am3352.isRequestOn = ON;
  2059. }
  2060. }
  2061. else
  2062. {
  2063. #ifdef FUNC_REQUEST_OFF_WHEN_GET_TWICE_OFF_CMD
  2064. if (++CsuSetRequestOffCount >= 2)
  2065. {
  2066. CsuSetRequestOffCount = 0;
  2067. Charger.am3352.isRequestOn = OFF;
  2068. Charger.am3352.RequestPinDebounceCnt = 0;
  2069. Charger.rfid.op_bits.reqStart = OFF;
  2070. if(!Charger.am3352.isRequestOff)
  2071. {
  2072. DEBUG_INFO("#Request Pin Off.\r\n");
  2073. Charger.am3352.isRequestOff = ON;
  2074. }
  2075. }
  2076. #else //FUNC_REQUEST_OFF_WHEN_GET_TWICE_OFF_CMD
  2077. Charger.am3352.isRequestOn = OFF;
  2078. Charger.am3352.RequestPinDebounceCnt = 0;
  2079. Charger.rfid.op_bits.reqStart = OFF;
  2080. if(!Charger.am3352.isRequestOff)
  2081. {
  2082. DEBUG_INFO("#Request Pin Off.\r\n");
  2083. Charger.am3352.isRequestOff = ON;
  2084. }
  2085. #endif //FUNC_REQUEST_OFF_WHEN_GET_TWICE_OFF_CMD
  2086. }
  2087. tx_len = 8;
  2088. tx[0] = 0xaa;
  2089. tx[1] = PROTOCOL_ADDR;
  2090. tx[2] = CurRxBuf[1];
  2091. tx[3] = PROTOCOL_MESSAGE_CONFIG_LEGACY_REQUEST;
  2092. tx[4] = 0x01;
  2093. tx[5] = 0x00;
  2094. tx[6] = 0x01;
  2095. tx[7] = 0x01;
  2096. break;
  2097. case PROTOCOL_MESSAGE_CONFIG_MCU_RESET:
  2098. tx_len = 8;
  2099. tx[0] = 0xaa;
  2100. tx[1] = PROTOCOL_ADDR;
  2101. tx[2] = CurRxBuf[1];
  2102. tx[3] = PROTOCOL_MESSAGE_CONFIG_MCU_RESET;
  2103. tx[4] = 0x01;
  2104. tx[5] = 0x00;
  2105. tx[6] = 0x01;
  2106. tx[7] = 0x01;
  2107. if((CurRxBuf[6]&0x01)>0){
  2108. DEBUG_INFO("MCU reset request.\r\n");
  2109. #ifdef MODIFY_DC_RS485_UPGRADE_ISSUE
  2110. Prefix_UartTX(tx, CurRxBuf);
  2111. HAL_UART_Transmit(pUartHandle, (uint8_t *)tx, tx_len , 0x0ffff);
  2112. Postfix_UartTX(tx, CurRxBuf);
  2113. #else
  2114. HAL_UART_Transmit(&IAP_USART, (uint8_t *)tx, tx_len , 0x0ffff);
  2115. #endif
  2116. osDelay(1000);
  2117. NVIC_SystemReset();
  2118. }
  2119. else
  2120. {
  2121. tx[6] = 0x00;
  2122. tx[7] = 0x00;
  2123. }
  2124. break;
  2125. case PROTOCOL_MESSAGE_CONFIG_BREATHE_LED_TIMING:
  2126. Charger.am3352.BreatheLedConnectFadeIn = (((uint16_t)CurRxBuf[7]<<8)+(uint16_t)CurRxBuf[6]);
  2127. Charger.am3352.BreatheLedConnectFadeOut = (((uint16_t)CurRxBuf[9]<<8)+(uint16_t)CurRxBuf[8]);
  2128. DEBUG_INFO("Connect Breathe Led Fade In time: %d ms.\r\n",Charger.am3352.BreatheLedConnectFadeIn);
  2129. DEBUG_INFO("Connect Breathe Led Fade Out time: %d ms.\r\n",Charger.am3352.BreatheLedConnectFadeOut);
  2130. Charger.am3352.BreatheLedAuthdFadeIn = (((uint16_t)CurRxBuf[11]<<8)+(uint16_t)CurRxBuf[10]);
  2131. Charger.am3352.BreatheLedAuthdFadeOut = (((uint16_t)CurRxBuf[13]<<8)+(uint16_t)CurRxBuf[12]);
  2132. DEBUG_INFO("Authd Breathe Led Fade In time: %d ms.\r\n",Charger.am3352.BreatheLedAuthdFadeIn);
  2133. DEBUG_INFO("Authd Breathe Led Fade Out time: %d ms.\r\n",Charger.am3352.BreatheLedAuthdFadeOut);
  2134. Charger.am3352.BreatheLedChargeFadeIn = (((uint16_t)CurRxBuf[15]<<8)+(uint16_t)CurRxBuf[14]);
  2135. Charger.am3352.BreatheLedChargeFadeOut = (((uint16_t)CurRxBuf[17]<<8)+(uint16_t)CurRxBuf[16]);
  2136. DEBUG_INFO("Charge Breathe Led Fade In time: %d ms.\r\n",Charger.am3352.BreatheLedChargeFadeIn);
  2137. DEBUG_INFO("Charge Breathe Led Fade Out time: %d ms.\r\n",Charger.am3352.BreatheLedChargeFadeOut);
  2138. tx_len = 8;
  2139. tx[0] = 0xaa;
  2140. tx[1] = PROTOCOL_ADDR;
  2141. tx[2] = CurRxBuf[1];
  2142. tx[3] = PROTOCOL_MESSAGE_CONFIG_BREATHE_LED_TIMING;
  2143. tx[4] = 0x01;
  2144. tx[5] = 0x00;
  2145. tx[6] = 0x01;
  2146. tx[7] = 0x01;
  2147. break;
  2148. case PROTOCOL_MESSAGE_CONFIG_LED_BRIGHTNESS:
  2149. memcpy(Charger.Led_Brightness, &CurRxBuf[6], 12);
  2150. //#ifdef FUNC_SHOW_CSU_CONFIG_LED_BRIGHTNESS
  2151. // XP("#CSU_SetLedBrightness(%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d)\r\n",
  2152. // Charger.Led_Brightness[0],
  2153. // Charger.Led_Brightness[1],
  2154. // Charger.Led_Brightness[2],
  2155. // Charger.Led_Brightness[3],
  2156. // Charger.Led_Brightness[4],
  2157. // Charger.Led_Brightness[5],
  2158. // Charger.Led_Brightness[6],
  2159. // Charger.Led_Brightness[7],
  2160. // Charger.Led_Brightness[8],
  2161. // Charger.Led_Brightness[9],
  2162. // Charger.Led_Brightness[10],
  2163. // Charger.Led_Brightness[11]);
  2164. //#endif
  2165. tx_len = 8;
  2166. tx[0] = 0xaa;
  2167. tx[1] = PROTOCOL_ADDR;
  2168. tx[2] = CurRxBuf[1];
  2169. tx[3] = PROTOCOL_MESSAGE_CONFIG_LED_BRIGHTNESS;
  2170. tx[4] = 0x01;
  2171. tx[5] = 0x00;
  2172. tx[6] = 0x01;
  2173. tx[7] = 0x01;
  2174. break;
  2175. case PROTOCOL_MESSAGE_CONFIG_EVSE_AUTH_MODE:
  2176. switch(CurRxBuf[6])
  2177. {
  2178. case AUTH_MODE_OCPP:
  2179. Charger.memory.EVSE_Config.data.item.AuthMode = AUTH_MODE_OCPP ;
  2180. break;
  2181. case AUTH_MODE_FREE:
  2182. Charger.memory.EVSE_Config.data.item.AuthMode = AUTH_MODE_FREE ;
  2183. break;
  2184. default:
  2185. Charger.memory.EVSE_Config.data.item.AuthMode = AUTH_MODE_FREE ;
  2186. Charger.memory.EVSE_Config.data.item.OfflinePolicy = RFID_USER_AUTH_FREE ;
  2187. break ;
  2188. }
  2189. switch(CurRxBuf[7])
  2190. {
  2191. case RFID_USER_AUTH_LOCAL_LIST:
  2192. Charger.memory.EVSE_Config.data.item.OfflinePolicy = RFID_USER_AUTH_LOCAL_LIST ;
  2193. break;
  2194. case RFID_USER_AUTH_PH:
  2195. Charger.memory.EVSE_Config.data.item.OfflinePolicy = RFID_USER_AUTH_PH ;
  2196. break;
  2197. case RFID_USER_AUTH_FREE:
  2198. Charger.memory.EVSE_Config.data.item.OfflinePolicy = RFID_USER_AUTH_FREE ;
  2199. break;
  2200. case RFID_USER_AUTH_NO_CHARGING:
  2201. Charger.memory.EVSE_Config.data.item.OfflinePolicy = RFID_USER_AUTH_NO_CHARGING ;
  2202. break;
  2203. default:
  2204. Charger.memory.EVSE_Config.data.item.AuthMode = AUTH_MODE_FREE ;
  2205. Charger.memory.EVSE_Config.data.item.OfflinePolicy = RFID_USER_AUTH_FREE ;
  2206. break ;
  2207. }
  2208. tx_len = 8;
  2209. tx[0] = 0xaa;
  2210. tx[1] = PROTOCOL_ADDR;
  2211. tx[2] = CurRxBuf[1];
  2212. tx[3] = PROTOCOL_MESSAGE_CONFIG_EVSE_AUTH_MODE;
  2213. tx[4] = 0x01;
  2214. tx[5] = 0x00;
  2215. tx[6] = 0x01;
  2216. tx[7] = 0x01;
  2217. Charger.memory.EVSE_Config.op_bits.update = ON;
  2218. break;
  2219. case PROTOCOL_MESSAGE_CONFIG_ERASE_MEMORY:
  2220. switch(CurRxBuf[6])
  2221. {
  2222. case 0:
  2223. Charger.memory.EVSE_Config.op_bits.clear = ON;
  2224. Charger.memory.whiteList.op_bits.clear = ON;
  2225. Charger.memory.hisCharging.op_bits.clear = ON;
  2226. Charger.memory.hisAlarm.op_bits.clear = ON;
  2227. Charger.memory.coldLoadPickUp.op_bits.clear = ON;
  2228. break;
  2229. case 1:
  2230. Charger.memory.EVSE_Config.op_bits.clear = ON;
  2231. break;
  2232. case 2:
  2233. Charger.memory.whiteList.op_bits.clear = ON;
  2234. break;
  2235. case 3:
  2236. Charger.memory.hisCharging.op_bits.clear = ON;
  2237. break;
  2238. case 4:
  2239. Charger.memory.hisAlarm.op_bits.clear = ON;
  2240. break;
  2241. case 5:
  2242. Charger.memory.coldLoadPickUp.op_bits.clear = ON;
  2243. break;
  2244. default:
  2245. break;
  2246. }
  2247. tx_len = 8;
  2248. tx[0] = 0xaa;
  2249. tx[1] = PROTOCOL_ADDR;
  2250. tx[2] = CurRxBuf[1];
  2251. tx[3] = PROTOCOL_MESSAGE_CONFIG_ERASE_MEMORY;
  2252. tx[4] = 0x01;
  2253. tx[5] = 0x00;
  2254. tx[6] = 0x01;
  2255. tx[7] = 0x01;
  2256. break;
  2257. #ifdef FUNC_RESET_CONNECTOR_PLUGIN_TIMES
  2258. case PROTOCOL_MESSAGE_CONFIG_RESET_CONNECTOR_PLUGIN_TIMES:
  2259. tx_len = 8;
  2260. tx[0] = 0xaa;
  2261. tx[1] = PROTOCOL_ADDR;
  2262. tx[2] = CurRxBuf[1];
  2263. tx[3] = PROTOCOL_MESSAGE_CONFIG_RESET_CONNECTOR_PLUGIN_TIMES;
  2264. tx[4] = 0x01;
  2265. tx[5] = 0x00;
  2266. tx[6] = 0x01;
  2267. tx[7] = 0x01;
  2268. if(CurRxBuf[6] == 0x01)
  2269. {
  2270. DEBUG_INFO("CSU reset connector plugin times.\r\n");
  2271. Charger.memory.EVSE_Config.data.item.AcPlugInTimes = 0;
  2272. Charger.memory.EVSE_Config.op_bits.update = ON;
  2273. }
  2274. else
  2275. {
  2276. tx[6] = 0x00;
  2277. tx[7] = 0x00;
  2278. }
  2279. break;
  2280. #endif //FUNC_RESET_CONNECTOR_PLUGIN_TIMES
  2281. /*
  2282. Firmware update message
  2283. */
  2284. case PROTOCOL_MESSAGE_UPGRADE_START:
  2285. tx_len = 8;
  2286. tx[0] = 0xaa;
  2287. tx[1] = PROTOCOL_ADDR;
  2288. tx[2] = CurRxBuf[1];
  2289. tx[3] = PROTOCOL_MESSAGE_UPGRADE_START;
  2290. tx[4] = 0x01;
  2291. tx[5] = 0x00;
  2292. binCRCTarget = (CurRxBuf[6] << 24) | (CurRxBuf[6] << 16) | (CurRxBuf[6] << 8) | (CurRxBuf[6] << 0);
  2293. flashdestination = NEW_CODE_ADDRESS;
  2294. if(FLASH_If_Erase(NEW_CODE_ADDRESS, 3)== FLASHIF_OK)
  2295. {
  2296. #if defined(DEBUG) || defined(RTOS_STAT)
  2297. DEBUG_INFO("Firmware transfer start, earase flash success....\n\r");
  2298. #endif
  2299. tx[6] = 0x01;
  2300. tx[7] = 0x01;
  2301. if (Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU)
  2302. {
  2303. setChargerMode(MODE_MAINTAIN);
  2304. timerEnable(TIMER_IDX_UPDATE, 10000);
  2305. }
  2306. }
  2307. else
  2308. {
  2309. #if defined(DEBUG) || defined(RTOS_STAT)
  2310. DEBUG_INFO("Firmware transfer start, earase flash fail....\n\r");
  2311. #endif
  2312. tx[6] = 0x00;
  2313. tx[7] = 0x00;
  2314. }
  2315. break;
  2316. case PROTOCOL_MESSAGE_UPGRADE_TRANS:
  2317. tx_len = 8;
  2318. tx[0] = 0xaa;
  2319. tx[1] = PROTOCOL_ADDR;
  2320. tx[2] = CurRxBuf[1];
  2321. tx[3] = PROTOCOL_MESSAGE_UPGRADE_TRANS;
  2322. tx[4] = 0x01;
  2323. tx[5] = 0x00;
  2324. flashdestination = NEW_CODE_ADDRESS + ((CurRxBuf[6]<<0) | (CurRxBuf[7]<<8) | (CurRxBuf[8]<<16) |(CurRxBuf[9]<<24));
  2325. if (FLASH_If_Write(flashdestination, (uint32_t*) &CurRxBuf[10], ((((CurRxBuf[4]) | (CurRxBuf[5])<<8)-4)>>2)) == FLASHIF_OK)
  2326. {
  2327. #if defined(DEBUG) || defined(RTOS_STAT)
  2328. //DEBUG_INFO("Firmware transfer start address: 0x%x, length: %d...Pass\n\r", flashdestination, (((CurRxBuf[4]) | (CurRxBuf[5])<<8)-4));
  2329. #endif
  2330. tx[6] = 0x01;
  2331. tx[7] = 0x01;
  2332. if (Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU)
  2333. {
  2334. setLedMotion(LED_ACTION_MAINTAIN);
  2335. timerRefresh(TIMER_IDX_UPDATE);
  2336. }
  2337. }
  2338. else
  2339. {
  2340. #if defined(DEBUG) || defined(RTOS_STAT)
  2341. DEBUG_INFO("Firmware transfer start address: 0x%x, length: %d...Fail\n\r", flashdestination, (((CurRxBuf[4]) | (CurRxBuf[5])<<8)-4));
  2342. #endif
  2343. tx[6] = 0x00;
  2344. tx[7] = 0x00;
  2345. }
  2346. break;
  2347. case PROTOCOL_MESSAGE_UPGRADE_STOP:
  2348. tx_len = 8;
  2349. tx[0] = 0xaa;
  2350. tx[1] = PROTOCOL_ADDR;
  2351. tx[2] = CurRxBuf[1];
  2352. tx[3] = PROTOCOL_MESSAGE_UPGRADE_STOP;
  2353. tx[4] = 0x01;
  2354. tx[5] = 0x00;
  2355. flash = NEW_CODE_ADDRESS;
  2356. checksum = HAL_CRC_Calculate(&hcrc, (uint32_t *)flash, ((FLASH_AP_LENGTH-4)>>2));
  2357. flash = ((uint32_t)(NEW_CODE_ADDRESS+FLASH_AP_LENGTH-4));
  2358. #if defined(DEBUG) || defined(RTOS_STAT)
  2359. DEBUG_INFO("Firmware transfer end, AP CRC checksum, flash: 0x%x, 0x%x\r\n", checksum, *((uint32_t *)flash) );
  2360. #endif
  2361. if(checksum == *((uint32_t *)flash))
  2362. {
  2363. if (FLASH_If_Write(UPGRADE_REQ_ADDRESS, (uint32_t *)&endFlag[0], 1) == FLASHIF_OK)
  2364. {
  2365. #if defined(DEBUG) || defined(RTOS_STAT)
  2366. DEBUG_INFO("Firmware Confirm Tag write ok..\n\r");
  2367. #endif
  2368. tx[6] = 0x01;
  2369. tx[7] = 0x01;
  2370. if(isUSB_CDC_IAP)
  2371. {
  2372. CDC_Transmit_FS((uint8_t *)tx, tx_len);
  2373. }
  2374. else
  2375. {
  2376. #ifdef MODIFY_DC_RS485_UPGRADE_ISSUE
  2377. Prefix_UartTX(tx, CurRxBuf);
  2378. HAL_UART_Transmit(pUartHandle, (uint8_t *)tx, tx_len , 0x0ffff);
  2379. Postfix_UartTX(tx, CurRxBuf);
  2380. #else
  2381. HAL_UART_Transmit(&IAP_USART, (uint8_t *)tx, tx_len , 0x0ffff);
  2382. #endif
  2383. }
  2384. osDelay(1000);
  2385. NVIC_SystemReset();
  2386. }
  2387. else
  2388. {
  2389. #if defined(DEBUG) || defined(RTOS_STAT)
  2390. DEBUG_INFO("Firmware Confirm Tag write fail...\n\r");
  2391. #endif
  2392. tx[6] = 0x00;
  2393. tx[7] = 0x00;
  2394. }
  2395. }
  2396. else
  2397. {
  2398. #if defined(DEBUG) || defined(RTOS_STAT)
  2399. DEBUG_INFO("Firmware checksum compare fail...\n\r");
  2400. #endif
  2401. tx[6] = 0x00;
  2402. tx[7] = 0x00;
  2403. }
  2404. if (Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU)
  2405. {
  2406. timerDisable(TIMER_IDX_UPDATE);
  2407. }
  2408. break;
  2409. case PROTOCOL_MESSAGE_UPGRADE_ABOARD:
  2410. #if defined(DEBUG) || defined(RTOS_STAT)
  2411. DEBUG_INFO("Firmware update transfer aboard...\n\r");
  2412. #endif
  2413. tx_len = 8;
  2414. tx[0] = 0xaa;
  2415. tx[1] = PROTOCOL_ADDR;
  2416. tx[2] = CurRxBuf[1];
  2417. tx[3] = PROTOCOL_MESSAGE_UPGRADE_ABOARD;
  2418. tx[4] = 0x01;
  2419. tx[5] = 0x00;
  2420. tx[6] = 0x01;
  2421. tx[7] = 0x01;
  2422. break;
  2423. default:
  2424. /* Todo: bin file receive aboard */
  2425. #if defined(DEBUG) || defined(RTOS_STAT)
  2426. #ifdef FUNC_RS485_SLAVE
  2427. DEBUG_INFO("Protocol message unknow (0x%02X)\n\r", CurRxBuf[3]);
  2428. #else
  2429. DEBUG_INFO("Protocol message unknow...\n\r");
  2430. #endif
  2431. #endif
  2432. tx_len = 8;
  2433. #ifdef FIX_RS485_UNKNOW_MSG
  2434. tx[0] = 0x55;
  2435. #else
  2436. tx[0] = 0xaa;
  2437. #endif
  2438. tx[1] = PROTOCOL_ADDR;
  2439. tx[2] = CurRxBuf[1];
  2440. tx[3] = CurRxBuf[3];
  2441. tx[4] = 0x01;
  2442. tx[5] = 0x00;
  2443. tx[6] = 0x00;
  2444. tx[7] = 0x00;
  2445. break;
  2446. }
  2447. }
  2448. else
  2449. {
  2450. #ifndef FUNC_RS485_SLAVE
  2451. #if defined(DEBUG) || defined(RTOS_STAT)
  2452. DEBUG_INFO("Protocol check sum is wrong...\n\r");
  2453. #endif
  2454. tx_len = 8;
  2455. #ifdef FIX_RS485_UNKNOW_MSG
  2456. tx[0] = 0x55;
  2457. #else
  2458. tx[0] = 0xaa;
  2459. #endif
  2460. tx[1] = PROTOCOL_ADDR;
  2461. tx[2] = CurRxBuf[1];
  2462. tx[3] = CurRxBuf[3];
  2463. tx[4] = 0x01;
  2464. tx[5] = 0x00;
  2465. tx[6] = 0x00;
  2466. tx[7] = 0x00;
  2467. #endif //FUNC_RS485_SLAVE
  2468. }
  2469. if(isUSB_CDC_IAP)
  2470. {
  2471. CDC_Transmit_FS((uint8_t *)tx, tx_len);
  2472. }
  2473. else
  2474. {
  2475. #ifdef FUNC_RS485_SLAVE
  2476. #ifdef MODIFY_DC_RS485_UPGRADE_ISSUE
  2477. if (CurRxBuf != NULL)
  2478. {
  2479. #ifdef FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  2480. if (CurRxBuf == UART_IAP_rx_buffer)
  2481. {
  2482. Prefix_UartTX(tx, CurRxBuf);
  2483. HAL_UART_Transmit(pUartHandle, (uint8_t *)tx, tx_len , 0x0ffff);
  2484. Postfix_UartTX(tx, CurRxBuf);
  2485. }
  2486. else if (CurRxBuf == UART_RS485_rx_buffer)
  2487. {
  2488. #ifdef FIX_RS485_UNKNOW_MSG
  2489. if (CurRxBuf[IMCP_FM_DSTADDR_IDX] == Charger.m_EMPX10_RS485_ClientAddr && tx[0] != 0x55)
  2490. #else
  2491. if (CurRxBuf[IMCP_FM_DSTADDR_IDX] == Charger.m_EMPX10_RS485_ClientAddr)
  2492. #endif
  2493. {
  2494. Prefix_UartTX(tx, CurRxBuf);
  2495. HAL_UART_Transmit(pUartHandle, (uint8_t *)tx, tx_len , 0x0ffff);
  2496. Postfix_UartTX(tx, CurRxBuf);
  2497. }
  2498. // else if (CurRxBuf[IMCP_FM_DSTADDR_IDX] == EMPX10_RS485_ADDR_BROADCAST)
  2499. // {
  2500. // Prefix_UartTX(tx, CurRxBuf);
  2501. // HAL_UART_Transmit(pUartHandle, (uint8_t *)tx, tx_len , 0x0ffff);
  2502. // Postfix_UartTX(tx, CurRxBuf);
  2503. // }
  2504. }
  2505. #else //FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  2506. Prefix_UartTX(tx, CurRxBuf);
  2507. HAL_UART_Transmit(pUartHandle, (uint8_t *)tx, tx_len , 0x0ffff);
  2508. Postfix_UartTX(tx, CurRxBuf);
  2509. #endif //FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  2510. }
  2511. #else //MODIFY_DC_RS485_UPGRADE_ISSUE
  2512. if (CurRxBuf != NULL)
  2513. {
  2514. tx[1] = Charger.m_RS485SlaveAddr; //Update Slave Address
  2515. HAL_UART_Transmit(pUartHandle, (uint8_t *)tx, tx_len, 0xffff);
  2516. }
  2517. #endif //MODIFY_DC_RS485_UPGRADE_ISSUE
  2518. #else //FUNC_RS485_SLAVE
  2519. HAL_UART_Transmit(&IAP_USART, (uint8_t *)tx, tx_len, 0xffff);
  2520. #endif //FUNC_RS485_SLAVE
  2521. }
  2522. #ifdef DEBUG_EMPX10_COMM
  2523. if (CurRxBuf == UART_RS485_rx_buffer)
  2524. {
  2525. HTK_ByteArray2HexStr_XP(NULL, tx, 0, tx_len);
  2526. }
  2527. #endif //DEBUG_EMPX10_COMM
  2528. #ifdef ENABLE_PRINT_IMCP_MSG
  2529. #ifdef FUNC_RS485_SLAVE
  2530. #ifdef PRINT_IMCP_MSG_ONLY_ON_UPDATEPORT
  2531. if (CurRxBuf == UART_IAP_rx_buffer && CheckPrintImcpMsg(CurRxBuf[3]))
  2532. {
  2533. HTK_ByteArray2HexStr_XP(NULL, tx, 0, tx_len);
  2534. }
  2535. #else
  2536. if (CheckPrintImcpMsg(CurRxBuf[3]))
  2537. {
  2538. HTK_ByteArray2HexStr_XP(NULL, tx, 0, tx_len);
  2539. }
  2540. #endif //PRINT_IMCP_MSG_ONLY_ON_UPDATEPORT
  2541. #else //FUNC_RS485_SLAVE
  2542. if (CheckPrintImcpMsg(UART_IAP_rx_buffer[3]))
  2543. {
  2544. HTK_ByteArray2HexStr_XP(NULL, tx, 0, tx_len);
  2545. }
  2546. #endif //FUNC_RS485_SLAVE
  2547. #endif //ENABLE_PRINT_IMCP_MSG
  2548. #ifdef FUNC_RS485_SLAVE
  2549. // Clear rx_buffer and related varaible
  2550. for(uint16_t i = 0; i < *pCurRxBuf_RxLen ; i++)
  2551. CurRxBuf[i]=0;
  2552. *pCurRxBuf_RxLen = 0;
  2553. *pCurRecvEndFlag = 0;
  2554. isUSB_CDC_IAP = OFF;
  2555. #else
  2556. // Clear rx_buffer and related varaible
  2557. for(uint16_t i = 0; i < UART_IAP_rx_len ; i++)
  2558. UART_IAP_rx_buffer[i]=0;
  2559. UART_IAP_rx_len=0;
  2560. UART_IAP_recv_end_flag=0;
  2561. isUSB_CDC_IAP = OFF;
  2562. #endif //FUNC_RS485_SLAVE
  2563. }
  2564. if(timer[TIMER_IDX_UPDATE].isAlarm == ON && Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU)
  2565. {
  2566. setChargerMode(MODE_IDLE);
  2567. setLedMotion(LED_ACTION_IDLE);
  2568. timerDisable(TIMER_IDX_UPDATE);
  2569. }
  2570. #ifdef FUNC_RS485_SLAVE
  2571. HAL_UART_Receive_DMA(&IAP_USART,(uint8_t*)UART_IAP_rx_buffer, UART_BUFFER_SIZE);
  2572. HAL_UART_Receive_DMA(&RS485_USART,(uint8_t*)UART_RS485_rx_buffer, UART_BUFFER_SIZE);
  2573. #else
  2574. HAL_UART_Receive_DMA(&IAP_USART,(uint8_t*)UART_IAP_rx_buffer, UART_BUFFER_SIZE);
  2575. #endif //FUNC_RS485_SLAVE
  2576. osDelay(1);
  2577. }
  2578. /* USER CODE END StartUartTask */
  2579. }
  2580. /* USER CODE BEGIN Header_StartAdcTask */
  2581. /**
  2582. * @brief Function implementing the adcTask thread.
  2583. * @param argument: Not used
  2584. * @retval None
  2585. */
  2586. /* USER CODE END Header_StartAdcTask */
  2587. #ifdef FUNC_DEBUG_MSG_CP_STATE
  2588. const char *CP_STATE_TAG[7] = {"0:UNKNOW", "1:A(12V)", "2:B(9V)", "3:C(6V)", "4:D(3V)", "5:E(0V)", "6:F(-12V)"};
  2589. #endif
  2590. void StartAdcTask(void const * argument)
  2591. {
  2592. /* USER CODE BEGIN StartAdcTask */
  2593. //aadc
  2594. uint8_t previous_cp_state = SYSTEM_STATE_A;
  2595. #ifdef MODIFY_POWER_CONSUME_ACCU_TIMER
  2596. uint16_t updateMemReq = 0;
  2597. #else
  2598. uint8_t updateMemReq = 0;
  2599. #endif
  2600. struct TimeTick
  2601. {
  2602. uint32_t timeTick_Start;
  2603. uint32_t timeTick_End;
  2604. uint32_t timeTick_Delta;
  2605. }timeTick;
  2606. timeTick.timeTick_Start = HAL_GetTick();
  2607. #ifdef FUNC_CP_ADC_MODIFY
  2608. #ifdef NEW_CP_SPEC
  2609. while (!Charger.m_bCpDetectModuleInitOK)
  2610. {
  2611. osDelay(10);
  2612. }
  2613. #endif //NEW_CP_SPEC
  2614. #endif //FUNC_CP_ADC_MODIFY
  2615. #ifdef MODIFY_POWER_CONSUME_ACCU_TIMER
  2616. timerEnable(TIMER_IDX_POWER, POWER_CONSUME_ACCU_TIMEOUT_SPEC);
  2617. #else
  2618. timerEnable(TIMER_IDX_POWER, 1000);
  2619. #endif
  2620. /* Infinite loop */
  2621. for(;;)
  2622. {
  2623. Relay_Control_Function( ) ;
  2624. // ADC chanel 1
  2625. if(isDMAEnd_ADC1)
  2626. {
  2627. isDMAEnd_ADC1 = OFF;
  2628. CpCalculate(ADC1_Buffer, ADC1_CHANEL_COUNT*ADC1_SAMPLE_COUNT, &CpDetectionResult);
  2629. #ifdef FUNC_EMPX10_CUSTOMIZE_CP_STATE_OPERATION
  2630. if (Charger.am3352.isRequestOn == ON)
  2631. {
  2632. if (Charger.Alarm_Code == 0)
  2633. {
  2634. if (Charger.m_CP_CurPWM == PWM_DUTY_FULL)
  2635. {
  2636. Charger.CP_State = SYSTEM_STATE_B;
  2637. }
  2638. else
  2639. {
  2640. Charger.CP_State = SYSTEM_STATE_C;
  2641. }
  2642. }
  2643. else
  2644. {
  2645. Charger.CP_State = SYSTEM_STATE_B;
  2646. }
  2647. }
  2648. else
  2649. {
  2650. Charger.CP_State = SYSTEM_STATE_A;
  2651. }
  2652. #else
  2653. Charger.CP_State = CpDetectionResult.State;
  2654. #endif
  2655. #ifdef FUNC_RELAY_OFF_WHEN_CP_NOT_STATE_C
  2656. if (Charger.CP_State != SYSTEM_STATE_C)
  2657. {
  2658. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  2659. }
  2660. #endif
  2661. #ifdef FUNC_DEBUG_MSG_CP_STATE
  2662. if (previous_cp_state != Charger.CP_State)
  2663. {
  2664. XP("#CP: [%s] >>> [%s] (%+f, %+f)\r\n",
  2665. CP_STATE_TAG[previous_cp_state],
  2666. CP_STATE_TAG[Charger.CP_State],
  2667. (CpDetectionResult.PositiveValue == 0 ? 0 : CP_GET_OPA_VIN_USE_ADC(CpDetectionResult.PositiveValue)),
  2668. (CpDetectionResult.NegativeValue == 0 ? 0 : CP_GET_OPA_VIN_USE_ADC(CpDetectionResult.NegativeValue))
  2669. );
  2670. }
  2671. #endif
  2672. if (Charger.Mode!= MODE_DEBUG)
  2673. {
  2674. #ifdef MODIFY_ADCTASK_CP_STATE_CHANGE
  2675. if((previous_cp_state != Charger.CP_State))
  2676. #else
  2677. if((previous_cp_state != Charger.CP_State) && (Charger.CP_State !=SYSTEM_STATE_UNKNOWN ))
  2678. #endif
  2679. {
  2680. previous_cp_state = Charger.CP_State;
  2681. if(Charger.CP_State == SYSTEM_STATE_A)
  2682. {
  2683. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL); // CP PWM duty set 100%
  2684. Charger.Relay_Action=GPIO_RELAY_ACTION_OFF;
  2685. Charger.memory.EVSE_Config.data.item.AcPlugInTimes++;
  2686. Charger.memory.EVSE_Config.op_bits.update = ON;
  2687. DEBUG_INFO("EVSE connector plug counts: %d\r\n", Charger.memory.EVSE_Config.data.item.AcPlugInTimes);
  2688. }
  2689. }
  2690. }
  2691. //DEBUG_INFO("HAL_GetTick() = %d\r\n",HAL_GetTick());
  2692. if(HAL_ADC_Start_DMA(&hadc1, (uint32_t*)&ADC1_Buffer, ADC1_CHANEL_COUNT*ADC1_SAMPLE_COUNT)!=HAL_OK)Error_Handler();
  2693. }
  2694. // ADC chanel 2
  2695. if(isDMAEnd_ADC2)
  2696. {
  2697. isDMAEnd_ADC2 = OFF;
  2698. for(uint16_t idx=0;idx<ADC2_SAMPLE_COUNT;idx++)
  2699. {
  2700. ADC2_Buffer_Each[0][idx] = ADC2_Buffer[(idx*ADC2_CHANEL_COUNT) + 0];
  2701. ADC2_Buffer_Each[1][idx] = ADC2_Buffer[(idx*ADC2_CHANEL_COUNT) + 1];
  2702. }
  2703. if (WatchDogLeakRawDataCheck_Event()) //clear leak WatchDogcount
  2704. {
  2705. Charger.counter.WatchDogLeak = 0 ;
  2706. }
  2707. //RlyVmax_cal
  2708. //filter_move_avg(&adc_value.ADC2_IN5_Welding, RlyVmax_cal(ADC2_Buffer_Each[0], ADC2_SAMPLE_COUNT));
  2709. if (Charger.GroundingSystem == GROUNGING_SYSTEM_LL)
  2710. {
  2711. filter_move_avg(&adc_value.ADC2_IN5_Welding, avg_v_cal(ADC2_Buffer_Each[0], ADC2_SAMPLE_COUNT)); //unit: 0.01V
  2712. }
  2713. else
  2714. {
  2715. filter_move_avg(&adc_value.ADC2_IN5_Welding, RlyVmax_cal(ADC2_Buffer_Each[0], ADC2_SAMPLE_COUNT)); //unit: 0.01V
  2716. }
  2717. //Leak
  2718. filter_move_avg(&adc_value.ADC2_IN6_GF, GFRms_cal(ADC2_Buffer_Each[1], ADC2_SAMPLE_COUNT));
  2719. if ((((adc_value.ADC2_IN6_GF.value)*Charger.coefficient.gain_leak[0])+Charger.coefficient.offset_leak[0]) <=0)
  2720. Charger.Leak_Current = adc_value.ADC2_IN6_GF.value;
  2721. else
  2722. Charger.Leak_Current = (uint16_t) (((adc_value.ADC2_IN6_GF.value)*Charger.coefficient.gain_leak[0])+Charger.coefficient.offset_leak[0]) ;
  2723. if(HAL_ADC_Start_DMA(&hadc2, (uint32_t*)&ADC2_Buffer, ADC2_CHANEL_COUNT*ADC2_SAMPLE_COUNT)!=HAL_OK)Error_Handler();
  2724. }
  2725. // ADC chanel 3
  2726. if(isDMAEnd_ADC3)
  2727. {
  2728. isDMAEnd_ADC3 = OFF;
  2729. for(uint16_t idx=0;idx<ADC3_SAMPLE_COUNT;idx++)
  2730. {
  2731. ADC3_Buffer_Each[0][idx] = ADC3_Buffer[(idx*ADC3_CHANEL_COUNT) + 0];
  2732. ADC3_Buffer_Each[1][idx] = ADC3_Buffer[(idx*ADC3_CHANEL_COUNT) + 1];
  2733. ADC3_Buffer_Each[2][idx] = ADC3_Buffer[(idx*ADC3_CHANEL_COUNT) + 2];
  2734. ADC3_Buffer_Each[3][idx] = ADC3_Buffer[(idx*ADC3_CHANEL_COUNT) + 3];
  2735. ADC3_Buffer_Each[4][idx] = ADC3_Buffer[(idx*ADC3_CHANEL_COUNT) + 4];
  2736. }
  2737. //VT
  2738. filter_move_avg(&adc_value.ADC3_IN9_Voltage_L1, vRms_cal2(ADC3_Buffer_Each[0], ADC3_SAMPLE_COUNT));
  2739. //SetADCWDGBoundary();
  2740. //MCU VDD
  2741. filter_move_avg(&adc_value.ADC3_IN14_UL_1998, avg_cal(ADC3_Buffer_Each[1], ADC3_SAMPLE_COUNT));
  2742. //TEMP
  2743. filter_move_avg(&adc_value.ADC3_IN15_Temp, avg_cal(ADC3_Buffer_Each[2], ADC3_SAMPLE_COUNT));
  2744. Charger.temperature.SystemAmbientTemp = getTemperature(adc_value.ADC3_IN15_Temp.value);
  2745. //PE
  2746. filter_move_avg(&adc_value.ADC3_IN4_GMI_VL1, avg_cal(ADC3_Buffer_Each[3], ADC3_SAMPLE_COUNT));
  2747. //CT
  2748. Current_filter_move_avg(&adc_value.ADC3_IN7_Current_L1, cRms_cal(ADC3_Buffer_Each[4], ADC3_SAMPLE_COUNT));
  2749. //Correction
  2750. //VT
  2751. Charger.Voltage[0] = (uint16_t) (((adc_value.ADC3_IN9_Voltage_L1.value)*Charger.coefficient.gain_v[0])+Charger.coefficient.offset_v[0]) ;
  2752. //CT
  2753. #ifdef FUNC_SIMU_CHARGE_CURRENT_OUTPUT
  2754. if (Charger.m_bEnableSimuChargeCurrent)
  2755. {
  2756. if (Charger.Relay_Action == GPIO_RELAY_ACTION_ON)
  2757. {
  2758. Charger.Current[0] = Charger.memory.EVSE_Config.data.item.MaxChargingCurrent * 100 - (rand() % 30);
  2759. }
  2760. else
  2761. {
  2762. Charger.Current[0] = 0;
  2763. }
  2764. }
  2765. else
  2766. {
  2767. Charger.Current[0] = (uint16_t)((adc_value.ADC3_IN7_Current_L1.value*Charger.coefficient.gain_c[0])+Charger.coefficient.offset_c[0]);
  2768. }
  2769. #else //FUNC_SIMU_CHARGE_CURRENT_OUTPUT
  2770. Charger.Current[0] = (uint16_t)((adc_value.ADC3_IN7_Current_L1.value*Charger.coefficient.gain_c[0])+Charger.coefficient.offset_c[0]);
  2771. #endif //FUNC_SIMU_CHARGE_CURRENT_OUTPUT
  2772. //GMI
  2773. Charger.Gmi_Voltage = (uint16_t) (((adc_value.ADC3_IN4_GMI_VL1.value)*Charger.coefficient.gain_gmi[0])+Charger.coefficient.offset_gmi[0]) ;
  2774. #ifdef COLD_LOAD_PICKUP
  2775. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  2776. // check Power drop is not alarm
  2777. // Power drop detection
  2778. if((vRms_cal2(ADC3_Buffer_Each[0], ADC3_SAMPLE_COUNT) <= ALARM_SPEC_POWER_DROP) && (!Charger.AC_DripisTure) && (Charger.Mode != MODE_INITIAL))
  2779. {
  2780. if(Charger.counter.AC_Drip.fail > 1000)
  2781. {
  2782. //Charger.memory.EVSE_Config.data.item.isDispDebug = OFF;
  2783. if((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))
  2784. {
  2785. Charger.memory.coldLoadPickUp.data.item.isEnable = ON;
  2786. Charger.memory.coldLoadPickUp.data.item.DateTime.year = Charger.cycle_info.startDateTime.year;
  2787. Charger.memory.coldLoadPickUp.data.item.DateTime.month = Charger.cycle_info.startDateTime.month;
  2788. Charger.memory.coldLoadPickUp.data.item.DateTime.day = Charger.cycle_info.startDateTime.day;
  2789. Charger.memory.coldLoadPickUp.data.item.DateTime.hour = Charger.cycle_info.startDateTime.hour;
  2790. Charger.memory.coldLoadPickUp.data.item.DateTime.min = Charger.cycle_info.startDateTime.min;
  2791. Charger.memory.coldLoadPickUp.data.item.DateTime.sec = Charger.cycle_info.startDateTime.sec;
  2792. Charger.memory.coldLoadPickUp.data.item.Duration = Charger.cycle_info.Duration;
  2793. sprintf(Charger.memory.coldLoadPickUp.data.item.PlugType, EVSE_CONNECTOR_TYPE);
  2794. Charger.memory.coldLoadPickUp.data.item.powerSum = Charger.cycle_info.Power_Consumption;
  2795. Charger.memory.coldLoadPickUp.data.item.startType = Charger.cycle_info.StartType;
  2796. //memcpy(Charger.memory.coldLoadPickUp.data.item.user, Charger.rfid.currentCard, 16);
  2797. Charger.memory.coldLoadPickUp.op_bits.update = ON;
  2798. }
  2799. Charger.AC_DripisTure = ON ;
  2800. }
  2801. else
  2802. Charger.counter.AC_Drip.fail ++ ;
  2803. }
  2804. else if((vRms_cal2(ADC3_Buffer_Each[0], ADC3_SAMPLE_COUNT) > (ALARM_SPEC_UV+ALARM_SPEC_OUV_HYSTERESIS)))
  2805. {
  2806. Charger.counter.AC_Drip.fail = 0 ;
  2807. if(Charger.AC_DripisTure)
  2808. {
  2809. Charger.AC_DripisTure = OFF ;
  2810. DEBUG_INFO("Alarm AC Drip recover.\r\n");
  2811. }
  2812. }
  2813. }
  2814. #endif //COLD_LOAD_PICKUP
  2815. // Power accumulate calculation
  2816. if(timer[TIMER_IDX_POWER].isAlarm)
  2817. {
  2818. timeTick.timeTick_End = HAL_GetTick();
  2819. timeTick.timeTick_Delta = timeTick.timeTick_End - timeTick.timeTick_Start;
  2820. timeTick.timeTick_Start = timeTick.timeTick_End;
  2821. if(Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative >= 0xFFFFFFFF)
  2822. {
  2823. Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative = 0;
  2824. }
  2825. else
  2826. {
  2827. #ifdef MODIFY_POWER_CONSUME_ACCU_TIMER
  2828. {
  2829. static double Energy = 0;
  2830. #ifdef FUNC_ZERO_CURRENT_WHEN_OUTP_OFF
  2831. u16 Curr = (Charger.Relay_Action == GPIO_RELAY_ACTION_ON) ? Charger.Current[0] : 0;
  2832. Energy += (((double)Charger.Voltage[0]/100.0)*((double)Curr/100.0)*((double)timeTick.timeTick_Delta/3600000.0)*10.0);
  2833. #else
  2834. Energy += (((double)Charger.Voltage[0]/100.0)*((double)Charger.Current[0]/100.0)*((double)timeTick.timeTick_Delta/3600000.0)*10.0);
  2835. #endif
  2836. if (Energy >= 1)
  2837. {
  2838. uint32_t EnergyIntPart = (uint32_t)Energy;
  2839. Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative += EnergyIntPart;
  2840. Energy -= EnergyIntPart;
  2841. }
  2842. }
  2843. #else //MODIFY_POWER_CONSUME_ACCU_TIMER
  2844. //unit : 0.0001kW , 0.1W *10 = W
  2845. Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative += (uint32_t)((Charger.Voltage[0]/100.0)*(Charger.Current[0]/100.0)*(timeTick.timeTick_Delta/3600000.0)*10.0);
  2846. #endif //MODIFY_POWER_CONSUME_ACCU_TIMER
  2847. }
  2848. #ifdef MODIFY_POWER_CONSUME_ACCU_TIMER
  2849. if((++updateMemReq) > (180 * (1000 / (double)POWER_CONSUME_ACCU_TIMEOUT_SPEC))) //Interval = POWER_CONSUME_ACCU_TIMEOUT_SPEC ms
  2850. #else
  2851. if(++updateMemReq>180) //Interval = 1000ms
  2852. #endif
  2853. {
  2854. Charger.memory.EVSE_Config.op_bits.update = ON;
  2855. updateMemReq = 0;
  2856. }
  2857. timerRefresh(TIMER_IDX_POWER);
  2858. }
  2859. if(HAL_ADC_Start_DMA(&hadc3, (uint32_t*)&ADC3_Buffer, ADC3_CHANEL_COUNT*ADC3_SAMPLE_COUNT)!=HAL_OK)Error_Handler();
  2860. }
  2861. osDelay(1);
  2862. }
  2863. /* USER CODE END StartAdcTask */
  2864. }
  2865. /* USER CODE BEGIN Header_StartTimeoutTask */
  2866. /**
  2867. * @brief Function implementing the timeoutTask thread.
  2868. * @param argument: Not used
  2869. * @retval None
  2870. */
  2871. /* USER CODE END Header_StartTimeoutTask */
  2872. void StartTimeoutTask(void const * argument)
  2873. {
  2874. /* USER CODE BEGIN StartTimeoutTask */
  2875. breathe.isUp = ON;
  2876. for(int idx=0;idx<BLINKER_COUNT;idx++)
  2877. blinker[idx].blinkCount = 0;
  2878. flickerTimeSet(FLICKER_IDX_BREATHE, 500, 500);
  2879. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 0, 1);
  2880. breatheTimeSet(1000, 1000, 1000, 1000, 1000, 0 );
  2881. Charger.am3352.BreatheLedChargeFadeIn = 2500;
  2882. Charger.am3352.BreatheLedChargeFadeOut = 2500;
  2883. Charger.am3352.BreatheLedConnectFadeIn = 1000;
  2884. Charger.am3352.BreatheLedConnectFadeOut = 1000;
  2885. Charger.am3352.BreatheLedAuthdFadeIn = 1000;
  2886. Charger.am3352.BreatheLedAuthdFadeOut = 1000;
  2887. /* Infinite loop */
  2888. for(;;)
  2889. {
  2890. if(Charger.Mode != MODE_INITIAL)
  2891. {
  2892. HAL_RTC_GetTime(&hrtc, &currentTime, RTC_FORMAT_BIN);
  2893. HAL_RTC_GetDate(&hrtc, &currentDate, RTC_FORMAT_BIN);
  2894. Charger.memory.EVSE_Config.data.item.SystemDateTime.year = currentDate.Year + 2000;
  2895. Charger.memory.EVSE_Config.data.item.SystemDateTime.month = currentDate.Month;
  2896. Charger.memory.EVSE_Config.data.item.SystemDateTime.day = currentDate.Date;
  2897. Charger.memory.EVSE_Config.data.item.SystemDateTime.hour = currentTime.Hours;
  2898. Charger.memory.EVSE_Config.data.item.SystemDateTime.min = currentTime.Minutes;
  2899. Charger.memory.EVSE_Config.data.item.SystemDateTime.sec = currentTime.Seconds;
  2900. }
  2901. /*
  2902. Timer check process
  2903. */
  2904. for(int idx=0;idx<TIMER_COUNT;idx++)
  2905. {
  2906. if(timer[idx].isEnable)
  2907. {
  2908. if((HAL_GetTick() - timer[idx].startTime) > timer[idx].timeoutSpec)
  2909. {
  2910. timer[idx].isAlarm = ON;
  2911. }
  2912. else
  2913. {
  2914. timer[idx].isAlarm = OFF;
  2915. }
  2916. }
  2917. else
  2918. {
  2919. timer[idx].startTime = HAL_GetTick();
  2920. timer[idx].isAlarm = OFF;
  2921. }
  2922. }
  2923. /*
  2924. Blink timer process
  2925. */
  2926. for(int idx=0;idx<BLINKER_COUNT;idx++)
  2927. {
  2928. if((HAL_GetTick() - blinker[idx].blinkStartTime) > (blinker[idx].isOn ? blinker[idx].blinkOnSpec : blinker[idx].blinkOffSpec))
  2929. {
  2930. if(blinker[idx].blinkCount >= blinker[idx].blinkCountSpec)
  2931. {
  2932. if((HAL_GetTick() - blinker[idx].resetStartTime) > blinker[idx].blinkRestSpec)
  2933. {
  2934. if(!blinker[idx].blinkRestSpec) //no rest case
  2935. blinker[idx].blinkisFinish = ON ;
  2936. blinker[idx].blinkCount = 0;
  2937. }
  2938. else
  2939. {
  2940. blinker[idx].isOn= OFF;
  2941. blinker[idx].blinkisFinish = ON ;
  2942. }
  2943. }
  2944. else
  2945. {
  2946. if(blinker[idx].isOn)
  2947. blinker[idx].blinkCount++;
  2948. blinker[idx].blinkStartTime = HAL_GetTick();
  2949. blinker[idx].resetStartTime = HAL_GetTick();
  2950. blinker[idx].isOn ^= ON;
  2951. if(blinker[idx].blinkRestSpec > 0) //had rest case
  2952. blinker[idx].blinkisFinish = OFF ;
  2953. }
  2954. }
  2955. }
  2956. /*
  2957. Flick timer process
  2958. */
  2959. for(int idx=0;idx<FLICKER_COUNT;idx++)
  2960. {
  2961. if((HAL_GetTick() - flicker[idx].StartTime) > (flicker[idx].isOn ? flicker[idx].OnSpec : flicker[idx].OffSpec))
  2962. {
  2963. flicker[idx].StartTime = HAL_GetTick();
  2964. flicker[idx].isOn ^= ON;
  2965. }
  2966. }
  2967. /*
  2968. Breathe timer process
  2969. */
  2970. if(breathe.isUp)
  2971. {
  2972. breathe.countPause = 0;
  2973. breathe.countDown = breathe.timeDown;
  2974. if(++breathe.countUp >= breathe.timeUp)
  2975. breathe.isUp = OFF;
  2976. breathe.duty[0] = breathe.RGB[0]*breathe.countUp/breathe.timeUp;
  2977. breathe.duty[1] = breathe.RGB[1]*breathe.countUp/breathe.timeUp;
  2978. breathe.duty[2] = breathe.RGB[2]*breathe.countUp/breathe.timeUp;
  2979. breathe.duty[3] = breathe.RGB[3]*breathe.countUp/breathe.timeUp;
  2980. }
  2981. else
  2982. {
  2983. breathe.countUp = 1000*BREATHE_LED_LOW_BOUNDARY/breathe.timeDown;
  2984. if(breathe.countDown <= 1000*BREATHE_LED_LOW_BOUNDARY/breathe.timeDown)
  2985. {
  2986. //if(++breathe.countPause>250) //breathe ON->oFF need delay nedd Uncomment annotations
  2987. breathe.isUp = ON;
  2988. }
  2989. else
  2990. {
  2991. if(breathe.countDown > 1000*BREATHE_LED_LOW_BOUNDARY/breathe.timeDown)
  2992. breathe.countDown -=1;
  2993. }
  2994. breathe.duty[0] = breathe.RGB[0]*breathe.countDown/breathe.timeDown;
  2995. breathe.duty[1] = breathe.RGB[1]*breathe.countDown/breathe.timeDown;
  2996. breathe.duty[2] = breathe.RGB[2]*breathe.countDown/breathe.timeDown;
  2997. breathe.duty[3] = breathe.RGB[3]*breathe.countDown/breathe.timeDown;
  2998. }
  2999. osDelay(1);
  3000. }
  3001. /* USER CODE END StartTimeoutTask */
  3002. }
  3003. /* USER CODE BEGIN Header_StartRfidTask */
  3004. /**
  3005. * @brief Function implementing the rfidTask thread.
  3006. * @param argument: Not used
  3007. * @retval None
  3008. */
  3009. /* USER CODE END Header_StartRfidTask */
  3010. void StartRfidTask(void const * argument)
  3011. {
  3012. /* USER CODE BEGIN StartRfidTask */
  3013. //rrfid
  3014. uint8_t cardType = 0;
  3015. uint8_t tmpBuf[32];
  3016. uint8_t tx[UART_BUFFER_SIZE>>4];
  3017. uint8_t tx_len;
  3018. HAL_GPIO_WritePin(OUT_RFID_Reset_GPIO_Port, OUT_RFID_Reset_Pin, GPIO_PIN_SET);
  3019. /* Infinite loop */
  3020. for(;;)
  3021. {
  3022. // Read card SN
  3023. if(!Charger.rfid.op_bits.reqHalt && !Charger.rfid.op_bits.reqBlockRead)
  3024. {
  3025. switch(cardType)
  3026. {
  3027. case 0x00:
  3028. tx_len = 4;
  3029. tx[0] = tx_len - 0x01;
  3030. tx[1] = RFID_CMD_REQUEST_SN_14443A;
  3031. tx[2] = 0x01;
  3032. tx[3] = rfidCheckSumCal(tx, tx_len-1);
  3033. cardType++;
  3034. break;
  3035. case 0x01:
  3036. tx_len = 5;
  3037. tx[0] = tx_len - 0x01;
  3038. tx[1] = RFID_CMD_REQUEST_SN_14443B;
  3039. tx[2] = 0x00;
  3040. tx[3] = 0x00;
  3041. tx[4] = rfidCheckSumCal(tx, tx_len-1);
  3042. cardType++;
  3043. break;
  3044. case 0x02:
  3045. tx_len = 9;
  3046. tx[0] = tx_len - 0x01;
  3047. tx[1] = RFID_CMD_REQUEST_SN_FELICA;
  3048. tx[2] = 0x06;
  3049. tx[3] = 0x00;
  3050. tx[4] = 0xff;
  3051. tx[5] = 0xff;
  3052. tx[6] = 0x01;
  3053. tx[7] = 0x00;
  3054. tx[8] = rfidCheckSumCal(tx, tx_len-1);
  3055. cardType++;
  3056. break;
  3057. default:
  3058. cardType = 0;
  3059. break;
  3060. }
  3061. HAL_UART_Transmit(&RFID_USART, (uint8_t *)tx, tx_len, 0xffff);
  3062. osDelay(100);
  3063. }
  3064. if(Charger.rfid.op_bits.reqHalt)
  3065. {
  3066. tx_len = 3;
  3067. tx[0] = tx_len - 0x01;
  3068. tx[1] = RFID_CMD_HALT_14443A;
  3069. tx[2] = rfidCheckSumCal(tx, tx_len-1);
  3070. HAL_UART_Transmit(&RFID_USART, (uint8_t *)tx, tx_len, 0xffff);
  3071. osDelay(100);
  3072. }
  3073. if(Charger.rfid.op_bits.reqBlockRead)
  3074. {
  3075. tx_len = 0x0b;
  3076. tx[0] = tx_len - 0x01;
  3077. tx[1] = RFID_CMD_BLOCK_READ;
  3078. tx[2] = Charger.rfid.keyType;
  3079. tx[3] = Charger.rfid.targetBlock;
  3080. tx[4] = Charger.rfid.key[0];
  3081. tx[5] = Charger.rfid.key[1];
  3082. tx[6] = Charger.rfid.key[2];
  3083. tx[7] = Charger.rfid.key[3];
  3084. tx[8] = Charger.rfid.key[4];
  3085. tx[9] = Charger.rfid.key[5];
  3086. tx[10] = rfidCheckSumCal(tx, tx_len-1);
  3087. HAL_UART_Transmit(&RFID_USART, (uint8_t *)tx, tx_len, 0xffff);
  3088. osDelay(100);
  3089. }
  3090. // RFID module feedback process
  3091. if(UART_RFID_recv_end_flag == ON)
  3092. {
  3093. if((rfidCheckSumCal(UART_RFID_rx_buffer, UART_RFID_rx_buffer[0]) == UART_RFID_rx_buffer[UART_RFID_rx_buffer[0]]) && (Charger.Mode != MODE_INITIAL) && (Charger.Mode != MODE_ALARM))
  3094. {
  3095. switch(UART_RFID_rx_buffer[1])
  3096. {
  3097. case RFID_CMD_LED:
  3098. break;
  3099. case RFID_CMD_REQUEST_SN_14443A:
  3100. if(UART_RFID_rx_buffer[0] == 9)
  3101. {
  3102. Charger.rfid.snType = RFID_SN_TYPE_4BYTE;
  3103. memset(&Charger.rfid.data_Sn[0], 0x00, ARRAY_SIZE(Charger.rfid.data_Sn));
  3104. for(uint8_t idx=0;idx<16;idx++)
  3105. {
  3106. if(idx<4)
  3107. {
  3108. Charger.rfid.data_Sn[idx] = UART_RFID_rx_buffer[idx+2];
  3109. }
  3110. else
  3111. {
  3112. Charger.rfid.data_Sn[idx] = 0x00;
  3113. }
  3114. }
  3115. if(isMode(MODE_IDLE))
  3116. {
  3117. memset(&Charger.rfid.currentCard[0], 0x00, ARRAY_SIZE(Charger.rfid.currentCard));
  3118. }
  3119. if(Charger.memory.EVSE_Config.data.item.RFID_SN_Endian)
  3120. {
  3121. DEBUG_INFO("RFID SN: %02X%02X%02X%02X\r\n", Charger.rfid.data_Sn[3], Charger.rfid.data_Sn[2], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[0]);
  3122. sprintf((char *)tmpBuf, "%02X%02X%02X%02X", Charger.rfid.data_Sn[3], Charger.rfid.data_Sn[2], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[0]);
  3123. memcpy(&Charger.rfid.nowCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.currentCard));
  3124. }
  3125. else
  3126. {
  3127. DEBUG_INFO("RFID SN: %02X%02X%02X%02X\r\n", Charger.rfid.data_Sn[0], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[2], Charger.rfid.data_Sn[3]);
  3128. sprintf((char *)tmpBuf, "%02X%02X%02X%02X", Charger.rfid.data_Sn[0], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[2], Charger.rfid.data_Sn[3]);
  3129. memcpy(&Charger.rfid.nowCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.currentCard));
  3130. }
  3131. }
  3132. else if(UART_RFID_rx_buffer[0] == 0x0c)
  3133. {
  3134. Charger.rfid.snType = RFID_SN_TYPE_7BYTE;
  3135. memset(&Charger.rfid.data_Sn[0], 0x00, ARRAY_SIZE(Charger.rfid.data_Sn));
  3136. for(uint8_t idx=0;idx<16;idx++)
  3137. {
  3138. if(idx<7)
  3139. {
  3140. Charger.rfid.data_Sn[idx] = UART_RFID_rx_buffer[idx+2];
  3141. }
  3142. else
  3143. {
  3144. Charger.rfid.data_Sn[idx] = 0x00;
  3145. }
  3146. }
  3147. if(isMode(MODE_IDLE))
  3148. {
  3149. memset(&Charger.rfid.currentCard[0], 0x00, ARRAY_SIZE(Charger.rfid.currentCard));
  3150. }
  3151. if(Charger.memory.EVSE_Config.data.item.RFID_SN_Endian)
  3152. {
  3153. DEBUG_INFO("RFID SN: %02X%02X%02X%02X%02X%02X%02X\r\n", Charger.rfid.data_Sn[6], Charger.rfid.data_Sn[5], Charger.rfid.data_Sn[4], Charger.rfid.data_Sn[3], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[0]);
  3154. sprintf((char *)tmpBuf, "%02X%02X%02X%02X%02X%02X%02X", Charger.rfid.data_Sn[6], Charger.rfid.data_Sn[5], Charger.rfid.data_Sn[4], Charger.rfid.data_Sn[3], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[0]);
  3155. memcpy(&Charger.rfid.nowCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.currentCard));
  3156. }
  3157. else
  3158. {
  3159. DEBUG_INFO("RFID SN: %02X%02X%02X%02X%02X%02X%02X\r\n", Charger.rfid.data_Sn[0], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[2], Charger.rfid.data_Sn[3], Charger.rfid.data_Sn[4], Charger.rfid.data_Sn[5], Charger.rfid.data_Sn[6]);
  3160. sprintf((char *)tmpBuf, "%02X%02X%02X%02X%02X%02X%02X", Charger.rfid.data_Sn[0], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[2], Charger.rfid.data_Sn[3], Charger.rfid.data_Sn[4], Charger.rfid.data_Sn[5], Charger.rfid.data_Sn[6]);
  3161. memcpy(&Charger.rfid.nowCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.currentCard));
  3162. }
  3163. }
  3164. //PH
  3165. if (Charger.memory.EVSE_Config.data.item.OfflinePolicy == RFID_USER_AUTH_PH )
  3166. {
  3167. if(UART_RFID_rx_buffer[0] >= 9)
  3168. {
  3169. if(isMode(MODE_IDLE))
  3170. {
  3171. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU)
  3172. {
  3173. Charger.rfid.targetBlock = 0x02;
  3174. Charger.rfid.keyType = 0x00;
  3175. memset(&Charger.rfid.key[0], 0xff, 6);
  3176. Charger.rfid.op_bits.reqBlockRead = ON;
  3177. //control in RFID_CMD_BLOCK_READ to Valid_RFID_SN
  3178. }
  3179. }
  3180. else if(isMode(MODE_CHARGING) || isMode(MODE_STOP))
  3181. {
  3182. if ((Array_data_Check(Charger.rfid.currentCard,Charger.rfid.nowCard))&&(Charger.cycle_info.StartType != START_METHOD_BLE))
  3183. {
  3184. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU)
  3185. {
  3186. Charger.rfid.op_bits.reqStop = ON;
  3187. setLedMotion(LED_ACTION_RFID_PASS);
  3188. Charger.speaker.motion = SPEAKER_SHORT;
  3189. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3190. }
  3191. DEBUG_WARN("reqStop = ON.\r\n");
  3192. }
  3193. else
  3194. {
  3195. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU)
  3196. {
  3197. setLedMotion(LED_ACTION_RFID_FAIL);
  3198. Charger.speaker.motion = SPEAKER_BLINK;
  3199. blinkerTimeSet(BLINKER_IDX_SPEAKER, 100, 100, SPEAK_RESET_RFID_TIME, 3) ; // SPEAK_RESET_RFID_TIME -> Avoid repeating
  3200. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3201. }
  3202. DEBUG_WARN("reqStop = Fail\r\n");
  3203. }
  3204. Charger.rfid.op_bits.reqHalt = ON;
  3205. Charger.rfid.op_bits.reqBlockRead = OFF;
  3206. }
  3207. }
  3208. }
  3209. //White List
  3210. else if (Charger.memory.EVSE_Config.data.item.OfflinePolicy == RFID_USER_AUTH_LOCAL_LIST)
  3211. {
  3212. DEBUG_WARN("OfflinePolicy is White List\r\n");
  3213. }
  3214. //Free
  3215. else if (Charger.memory.EVSE_Config.data.item.OfflinePolicy == RFID_USER_AUTH_FREE)
  3216. {
  3217. Charger.rfid.op_bits.reqHalt = ON;
  3218. Charger.rfid.op_bits.reqBlockRead = OFF;
  3219. if(isMode(MODE_IDLE))
  3220. {
  3221. for(uint8_t idx=0;idx<16;idx++)
  3222. {
  3223. Charger.rfid.currentCard[idx] = Charger.rfid.data_Sn[idx];
  3224. }
  3225. Charger.rfid.op_bits.reqStart = ON;
  3226. memcpy(&Charger.rfid.currentCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.currentCard));
  3227. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  3228. setLedMotion(LED_ACTION_RFID_PASS);
  3229. Charger.speaker.motion = SPEAKER_SHORT;
  3230. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3231. }
  3232. DEBUG_WARN("RFID authorize pass.\r\n");
  3233. }
  3234. else if(isMode(MODE_CHARGING) || isMode(MODE_STOP))
  3235. {
  3236. DEBUG_WARN("RFID authorize pass.\r\n");
  3237. if (Array_data_Check(Charger.rfid.currentCard,Charger.rfid.nowCard))
  3238. {
  3239. Charger.rfid.op_bits.reqStop = ON;
  3240. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  3241. setLedMotion(LED_ACTION_RFID_PASS);
  3242. Charger.speaker.motion = SPEAKER_SHORT;
  3243. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3244. }
  3245. DEBUG_WARN("reqStop = ON.\r\n");
  3246. }
  3247. else
  3248. {
  3249. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  3250. setLedMotion(LED_ACTION_RFID_FAIL);
  3251. Charger.speaker.motion = SPEAKER_BLINK;
  3252. blinkerTimeSet(BLINKER_IDX_SPEAKER, 100, 100, SPEAK_RESET_RFID_TIME, 3) ; // SPEAK_RESET_RFID_TIME -> Avoid repeating
  3253. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3254. }
  3255. DEBUG_WARN("reqStop = Fail\r\n");
  3256. }
  3257. }
  3258. }
  3259. //NO charger
  3260. else // Charger.memory.EVSE_Config.data.item.OfflinePolicy == RFID_USER_AUTH_NO_CHARGING
  3261. {
  3262. setLedMotion(LED_ACTION_RFID_FAIL);
  3263. Charger.speaker.motion = SPEAKER_BLINK;
  3264. blinkerTimeSet(BLINKER_IDX_SPEAKER, 100, 100, SPEAK_RESET_RFID_TIME, 3) ; // SPEAK_RESET_RFID_TIME -> Avoid repeating
  3265. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3266. Charger.rfid.op_bits.reqHalt = ON;
  3267. Charger.rfid.op_bits.reqBlockRead = OFF;
  3268. DEBUG_WARN("It is RFID NO charger mode \r\n");
  3269. }
  3270. break;
  3271. case RFID_CMD_REQUEST_SN_14443B:
  3272. if(UART_RFID_rx_buffer[1] == RFID_CMD_REQUEST_SN_14443B)
  3273. {
  3274. Charger.rfid.snType = RFID_SN_TYPE_14443B;
  3275. memset(&Charger.rfid.data_Sn[0], 0x00, ARRAY_SIZE(Charger.rfid.data_Sn));
  3276. for(uint8_t idx=0;idx<16;idx++)
  3277. {
  3278. if(idx<13)
  3279. {
  3280. Charger.rfid.data_Sn[idx] = UART_RFID_rx_buffer[idx+2];
  3281. }
  3282. }
  3283. if(isMode(MODE_IDLE))
  3284. memset(&Charger.rfid.nowCard[0], 0x00, ARRAY_SIZE(Charger.rfid.nowCard));
  3285. if(Charger.memory.EVSE_Config.data.item.RFID_SN_Endian)
  3286. {
  3287. DEBUG_INFO("RFID SN: %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\r\n", Charger.rfid.data_Sn[12], Charger.rfid.data_Sn[11], Charger.rfid.data_Sn[10], Charger.rfid.data_Sn[9], Charger.rfid.data_Sn[8], Charger.rfid.data_Sn[7], Charger.rfid.data_Sn[6], Charger.rfid.data_Sn[5], Charger.rfid.data_Sn[4], Charger.rfid.data_Sn[3], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[0]);
  3288. sprintf((char *)tmpBuf, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", Charger.rfid.data_Sn[12], Charger.rfid.data_Sn[11], Charger.rfid.data_Sn[10], Charger.rfid.data_Sn[9], Charger.rfid.data_Sn[8], Charger.rfid.data_Sn[7], Charger.rfid.data_Sn[6], Charger.rfid.data_Sn[5], Charger.rfid.data_Sn[4], Charger.rfid.data_Sn[3], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[0]);
  3289. memcpy(&Charger.rfid.nowCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.nowCard));
  3290. }
  3291. else
  3292. {
  3293. DEBUG_INFO("RFID SN: %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\r\n", Charger.rfid.data_Sn[0], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[2], Charger.rfid.data_Sn[3], Charger.rfid.data_Sn[4], Charger.rfid.data_Sn[5], Charger.rfid.data_Sn[6], Charger.rfid.data_Sn[7], Charger.rfid.data_Sn[8], Charger.rfid.data_Sn[9], Charger.rfid.data_Sn[10], Charger.rfid.data_Sn[11], Charger.rfid.data_Sn[12]);
  3294. sprintf((char *)tmpBuf, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", Charger.rfid.data_Sn[0], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[2], Charger.rfid.data_Sn[3], Charger.rfid.data_Sn[4], Charger.rfid.data_Sn[5], Charger.rfid.data_Sn[6], Charger.rfid.data_Sn[7], Charger.rfid.data_Sn[8], Charger.rfid.data_Sn[9], Charger.rfid.data_Sn[10], Charger.rfid.data_Sn[11], Charger.rfid.data_Sn[12]);
  3295. memcpy(&Charger.rfid.nowCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.nowCard));
  3296. }
  3297. }
  3298. //White List
  3299. if (Charger.memory.EVSE_Config.data.item.OfflinePolicy == RFID_USER_AUTH_LOCAL_LIST)
  3300. {
  3301. DEBUG_WARN("OfflinePolicy is White List\r\n");
  3302. }
  3303. //Free
  3304. else if (Charger.memory.EVSE_Config.data.item.OfflinePolicy == RFID_USER_AUTH_FREE)
  3305. {
  3306. Charger.rfid.op_bits.reqHalt = ON;
  3307. Charger.rfid.op_bits.reqBlockRead = OFF;
  3308. if(isMode(MODE_IDLE))
  3309. {
  3310. for(uint8_t idx=0;idx<16;idx++)
  3311. {
  3312. Charger.rfid.currentCard[idx] = Charger.rfid.data_Sn[idx];
  3313. }
  3314. Charger.rfid.op_bits.reqStart = ON;
  3315. memcpy(&Charger.rfid.currentCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.currentCard));
  3316. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  3317. setLedMotion(LED_ACTION_RFID_PASS);
  3318. Charger.speaker.motion = SPEAKER_SHORT;
  3319. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3320. }
  3321. DEBUG_WARN("RFID authorize pass.\r\n");
  3322. }
  3323. else if(isMode(MODE_CHARGING) || isMode(MODE_STOP))
  3324. {
  3325. DEBUG_WARN("RFID authorize pass.\r\n");
  3326. if (Array_data_Check(Charger.rfid.currentCard,Charger.rfid.nowCard))
  3327. {
  3328. Charger.rfid.op_bits.reqStop = ON;
  3329. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  3330. setLedMotion(LED_ACTION_RFID_PASS);
  3331. Charger.speaker.motion = SPEAKER_SHORT;
  3332. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3333. }
  3334. DEBUG_WARN("reqStop = ON.\r\n");
  3335. }
  3336. else
  3337. {
  3338. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  3339. setLedMotion(LED_ACTION_RFID_FAIL);
  3340. Charger.speaker.motion = SPEAKER_BLINK;
  3341. blinkerTimeSet(BLINKER_IDX_SPEAKER, 100, 100, SPEAK_RESET_RFID_TIME, 3) ; // SPEAK_RESET_RFID_TIME -> Avoid repeating
  3342. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3343. }
  3344. DEBUG_WARN("reqStop = Fail\r\n");
  3345. }
  3346. }
  3347. }
  3348. //NO charger
  3349. else // Charger.memory.EVSE_Config.data.item.OfflinePolicy == RFID_USER_AUTH_NO_CHARGING
  3350. {
  3351. setLedMotion(LED_ACTION_RFID_FAIL);
  3352. Charger.speaker.motion = SPEAKER_BLINK;
  3353. blinkerTimeSet(BLINKER_IDX_SPEAKER, 100, 100, SPEAK_RESET_RFID_TIME, 3) ; // SPEAK_RESET_RFID_TIME -> Avoid repeating
  3354. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3355. Charger.rfid.op_bits.reqHalt = ON;
  3356. Charger.rfid.op_bits.reqBlockRead = OFF;
  3357. DEBUG_WARN("It is RFID NO charger mode \r\n");
  3358. }
  3359. break;
  3360. case RFID_CMD_REQUEST_SN_FELICA:
  3361. if(UART_RFID_rx_buffer[1] == RFID_CMD_REQUEST_SN_FELICA)
  3362. {
  3363. Charger.rfid.snType = RFID_SN_TYPE_FELICA;
  3364. memset(&Charger.rfid.data_Sn[0], 0x00, ARRAY_SIZE(Charger.rfid.data_Sn));
  3365. for(uint8_t idx=0;idx<16;idx++)
  3366. {
  3367. if(idx<6)
  3368. {
  3369. Charger.rfid.data_Sn[idx] = UART_RFID_rx_buffer[idx+6];
  3370. }
  3371. }
  3372. if(isMode(MODE_IDLE))
  3373. memset(&Charger.rfid.nowCard[0], 0x00, ARRAY_SIZE(Charger.rfid.nowCard));
  3374. if(Charger.memory.EVSE_Config.data.item.RFID_SN_Endian)
  3375. {
  3376. DEBUG_INFO("RFID SN: %02X%02X%02X%02X%02X%02X\r\n", Charger.rfid.data_Sn[5], Charger.rfid.data_Sn[4], Charger.rfid.data_Sn[3], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[0]);
  3377. sprintf((char *)tmpBuf, "%02X%02X%02X%02X%02X%02X", Charger.rfid.data_Sn[5], Charger.rfid.data_Sn[4], Charger.rfid.data_Sn[3], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[0]);
  3378. memcpy(&Charger.rfid.nowCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.nowCard));
  3379. }
  3380. else
  3381. {
  3382. DEBUG_INFO("RFID SN: %02X%02X%02X%02X%02X%02X\r\n", Charger.rfid.data_Sn[0], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[2], Charger.rfid.data_Sn[3], Charger.rfid.data_Sn[4], Charger.rfid.data_Sn[5]);
  3383. sprintf((char *)tmpBuf, "%02X%02X%02X%02X%02X%02X", Charger.rfid.data_Sn[0], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[2], Charger.rfid.data_Sn[3], Charger.rfid.data_Sn[4], Charger.rfid.data_Sn[5]);
  3384. memcpy(&Charger.rfid.nowCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.nowCard));
  3385. }
  3386. }
  3387. //White List
  3388. if (Charger.memory.EVSE_Config.data.item.OfflinePolicy == RFID_USER_AUTH_LOCAL_LIST)
  3389. {
  3390. DEBUG_WARN("OfflinePolicy is White List\r\n");
  3391. }
  3392. //Free
  3393. else if (Charger.memory.EVSE_Config.data.item.OfflinePolicy == RFID_USER_AUTH_FREE)
  3394. {
  3395. Charger.rfid.op_bits.reqHalt = ON;
  3396. Charger.rfid.op_bits.reqBlockRead = OFF;
  3397. if(isMode(MODE_IDLE))
  3398. {
  3399. for(uint8_t idx=0;idx<16;idx++)
  3400. {
  3401. Charger.rfid.currentCard[idx] = Charger.rfid.data_Sn[idx];
  3402. }
  3403. Charger.rfid.op_bits.reqStart = ON;
  3404. memcpy(&Charger.rfid.currentCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.currentCard));
  3405. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  3406. setLedMotion(LED_ACTION_RFID_PASS);
  3407. Charger.speaker.motion = SPEAKER_SHORT;
  3408. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3409. }
  3410. DEBUG_WARN("RFID authorize pass.\r\n");
  3411. }
  3412. else if(isMode(MODE_CHARGING) || isMode(MODE_STOP))
  3413. {
  3414. DEBUG_WARN("RFID authorize pass.\r\n");
  3415. if (Array_data_Check(Charger.rfid.currentCard,Charger.rfid.nowCard))
  3416. {
  3417. Charger.rfid.op_bits.reqStop = ON;
  3418. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  3419. setLedMotion(LED_ACTION_RFID_PASS);
  3420. Charger.speaker.motion = SPEAKER_SHORT;
  3421. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3422. }
  3423. DEBUG_WARN("reqStop = ON.\r\n");
  3424. }
  3425. else
  3426. {
  3427. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  3428. setLedMotion(LED_ACTION_RFID_FAIL);
  3429. Charger.speaker.motion = SPEAKER_BLINK;
  3430. blinkerTimeSet(BLINKER_IDX_SPEAKER, 100, 100, SPEAK_RESET_RFID_TIME, 3) ; // SPEAK_RESET_RFID_TIME -> Avoid repeating
  3431. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3432. }
  3433. DEBUG_WARN("reqStop = Fail\r\n");
  3434. }
  3435. }
  3436. }
  3437. //NO charger
  3438. else // Charger.memory.EVSE_Config.data.item.OfflinePolicy == RFID_USER_AUTH_NO_CHARGING
  3439. {
  3440. setLedMotion(LED_ACTION_RFID_FAIL);
  3441. Charger.speaker.motion = SPEAKER_BLINK;
  3442. blinkerTimeSet(BLINKER_IDX_SPEAKER, 100, 100, SPEAK_RESET_RFID_TIME, 3) ; // SPEAK_RESET_RFID_TIME -> Avoid repeating
  3443. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3444. Charger.rfid.op_bits.reqHalt = ON;
  3445. Charger.rfid.op_bits.reqBlockRead = OFF;
  3446. DEBUG_WARN("It is RFID NO charger mode \r\n");
  3447. }
  3448. osDelay(1000);
  3449. break;
  3450. case RFID_CMD_BLOCK_READ:
  3451. if(UART_RFID_rx_buffer[1] == RFID_CMD_BLOCK_READ)
  3452. {
  3453. memcpy(Charger.rfid.data_Block, &UART_RFID_rx_buffer[2], 0x10);
  3454. Charger.rfid.op_bits.reqHalt = ON;
  3455. Charger.rfid.op_bits.reqBlockRead = OFF;
  3456. //RFID_PH
  3457. if(Valid_RFID_SN())
  3458. {
  3459. if(isMode(MODE_IDLE))
  3460. {
  3461. Charger.rfid.op_bits.reqStart = ON;
  3462. setLedMotion(LED_ACTION_RFID_PASS);
  3463. Charger.speaker.motion = SPEAKER_SHORT;
  3464. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3465. memcpy(&Charger.rfid.currentCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.currentCard)); }
  3466. else if(isMode(MODE_CHARGING) || isMode(MODE_STOP))
  3467. {
  3468. Charger.rfid.op_bits.reqStop = ON;
  3469. }
  3470. DEBUG_WARN("RFID authorize pass.\r\n");
  3471. }
  3472. else
  3473. {
  3474. setLedMotion(LED_ACTION_RFID_FAIL);
  3475. Charger.speaker.motion = SPEAKER_BLINK;
  3476. blinkerTimeSet(BLINKER_IDX_SPEAKER, 100, 100, SPEAK_RESET_RFID_TIME, 3) ; // SPEAK_RESET_RFID_TIME -> Avoid repeating
  3477. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3478. DEBUG_WARN("RFID authorize fault.\r\n");
  3479. }
  3480. }
  3481. break;
  3482. case RFID_CMD_BLOCK_WRITE:
  3483. break;
  3484. case RFID_CMD_HALT_14443A:
  3485. if(UART_RFID_rx_buffer[1] == RFID_CMD_HALT_14443A)
  3486. {
  3487. Charger.rfid.op_bits.reqHalt = OFF;
  3488. DEBUG_INFO("RFID card(14443A) halt succcess.\r\n");
  3489. }
  3490. break;
  3491. case RFID_CMD_HALT_14443B:
  3492. if(UART_RFID_rx_buffer[1] == RFID_CMD_HALT_14443B)
  3493. {
  3494. Charger.rfid.op_bits.reqHalt = OFF;
  3495. DEBUG_INFO("RFID card(14443B) halt succcess.\r\n");
  3496. }
  3497. break;
  3498. case (RFID_CMD_BLOCK_READ ^ 0xff):
  3499. Charger.rfid.op_bits.reqBlockRead = OFF;
  3500. DEBUG_WARN("RFID cread block fail.\r\n");
  3501. break;
  3502. default:
  3503. break;
  3504. }
  3505. }
  3506. // Clear rx_buffer and related varaible
  3507. for(uint16_t i = 0; i < UART_RFID_rx_len ; i++)
  3508. UART_RFID_rx_buffer[i]=0;
  3509. UART_RFID_rx_len = 0;
  3510. UART_RFID_recv_end_flag = OFF;
  3511. }
  3512. HAL_UART_Receive_DMA(&RFID_USART,(uint8_t*)UART_RFID_rx_buffer, (UART_BUFFER_SIZE>>4));
  3513. osDelay(300);
  3514. }
  3515. /* USER CODE END StartRfidTask */
  3516. }
  3517. /* USER CODE BEGIN Header_StartLedSpeakerTask */
  3518. /**
  3519. * @brief Function implementing the led_speakerTask thread.
  3520. * @param argument: Not used
  3521. * @retval None
  3522. */
  3523. /* USER CODE END Header_StartLedSpeakerTask */
  3524. void StartLedSpeakerTask(void const * argument)
  3525. {
  3526. /* USER CODE BEGIN StartLedSpeakerTask */
  3527. uint16_t led_pwm_duty_now[4];
  3528. uint16_t breathe_duty_now[4];
  3529. //standard default
  3530. for(uint8_t idx=0;idx<12;idx++)
  3531. Charger.Led_Brightness[idx] = 0x33;
  3532. /*
  3533. //Night mode starndard default brightness
  3534. Charger.Led_Brightness[0] = 0x11; //00:00~ 01:59
  3535. Charger.Led_Brightness[1] = 0x11; //02:00~ 03:59
  3536. Charger.Led_Brightness[2] = 0x11; //04:00~ 05:59
  3537. Charger.Led_Brightness[3] = 0x33; //06:00~ 07:59
  3538. Charger.Led_Brightness[4] = 0x33; //08:00~ 09:59
  3539. Charger.Led_Brightness[5] = 0x33; //10:00~ 11:59
  3540. Charger.Led_Brightness[6] = 0x33; //12:00~ 13:59
  3541. Charger.Led_Brightness[7] = 0x33; //14:00~ 15:59
  3542. Charger.Led_Brightness[8] = 0x33; //16:00~ 17:59
  3543. Charger.Led_Brightness[9] = 0x11; //18:00~ 19:59
  3544. Charger.Led_Brightness[10] = 0x11; //20:00~ 21:59
  3545. Charger.Led_Brightness[11] = 0x11; //22:00~ 23:59
  3546. */
  3547. /* Infinite loop */
  3548. for(;;)
  3549. {
  3550. led_pwm_duty_now[0] = getBrightnessDuty(Charger.led_pwm_duty[0]);
  3551. led_pwm_duty_now[1] = getBrightnessDuty(Charger.led_pwm_duty[1]);
  3552. led_pwm_duty_now[2] = getBrightnessDuty(Charger.led_pwm_duty[2]);
  3553. led_pwm_duty_now[3] = getBrightnessDuty(Charger.led_pwm_duty[3]);
  3554. breathe_duty_now[0] = getBrightnessDuty(breathe.duty[0]);
  3555. breathe_duty_now[1] = getBrightnessDuty(breathe.duty[1]);
  3556. breathe_duty_now[2] = getBrightnessDuty(breathe.duty[2]);
  3557. breathe_duty_now[3] = getBrightnessDuty(breathe.duty[3]);
  3558. if(timer[TIMER_IDX_LED_TEMP].isAlarm)
  3559. {
  3560. timerDisable(TIMER_IDX_LED_TEMP);
  3561. setLedMotion(Charger.Led_Mode);
  3562. }
  3563. /*
  3564. LED red process
  3565. */
  3566. switch(Charger.led_R.motion)
  3567. {
  3568. case LED_MOTION_ON:
  3569. user_pwm_setvalue(PWM_CH_LED_R, led_pwm_duty_now[0]);
  3570. break;
  3571. case LED_MOTION_BLINK:
  3572. if(blinker[BLINKER_IDX_LED].isOn)
  3573. user_pwm_setvalue(PWM_CH_LED_R, led_pwm_duty_now[0]);
  3574. else
  3575. user_pwm_setvalue(PWM_CH_LED_R, PWM_DUTY_OFF);
  3576. break;
  3577. case LED_MOTION_BREATHE:
  3578. user_pwm_setvalue(PWM_CH_LED_R, breathe_duty_now[0]);
  3579. break;
  3580. case LED_MOTION_OFF:
  3581. default:
  3582. user_pwm_setvalue(PWM_CH_LED_R, PWM_DUTY_OFF);
  3583. break;
  3584. }
  3585. /*
  3586. LED green process
  3587. */
  3588. switch(Charger.led_G.motion)
  3589. {
  3590. case LED_MOTION_ON:
  3591. user_pwm_setvalue(PWM_CH_LED_G, led_pwm_duty_now[1]);
  3592. break;
  3593. case LED_MOTION_BLINK:
  3594. if(blinker[BLINKER_IDX_LED].isOn)
  3595. user_pwm_setvalue(PWM_CH_LED_G, led_pwm_duty_now[1]);
  3596. else
  3597. user_pwm_setvalue(PWM_CH_LED_G, PWM_DUTY_OFF);
  3598. break;
  3599. case LED_MOTION_BREATHE:
  3600. user_pwm_setvalue(PWM_CH_LED_G, breathe_duty_now[1]);
  3601. break;
  3602. case LED_MOTION_OFF:
  3603. default:
  3604. user_pwm_setvalue(PWM_CH_LED_G, PWM_DUTY_OFF);
  3605. break;
  3606. }
  3607. /*
  3608. LED blue process
  3609. */
  3610. switch(Charger.led_B.motion)
  3611. {
  3612. case LED_MOTION_ON:
  3613. user_pwm_setvalue(PWM_CH_LED_B, led_pwm_duty_now[2]);
  3614. break;
  3615. case LED_MOTION_BLINK:
  3616. if(blinker[BLINKER_IDX_LED].isOn)
  3617. user_pwm_setvalue(PWM_CH_LED_B, led_pwm_duty_now[2]);
  3618. else
  3619. user_pwm_setvalue(PWM_CH_LED_B, PWM_DUTY_OFF);
  3620. break;
  3621. case LED_MOTION_BREATHE:
  3622. user_pwm_setvalue(PWM_CH_LED_B, breathe_duty_now[2]);
  3623. break;
  3624. case LED_MOTION_OFF:
  3625. default:
  3626. user_pwm_setvalue(PWM_CH_LED_B, PWM_DUTY_OFF);
  3627. break;
  3628. }
  3629. /*
  3630. LED white process
  3631. */
  3632. switch(Charger.led_W.motion)
  3633. {
  3634. case LED_MOTION_ON:
  3635. user_pwm_setvalue(PWM_CH_LED_W, led_pwm_duty_now[3]);
  3636. break;
  3637. case LED_MOTION_BLINK:
  3638. if(blinker[BLINKER_IDX_LED].isOn)
  3639. user_pwm_setvalue(PWM_CH_LED_W, led_pwm_duty_now[3]);
  3640. else
  3641. user_pwm_setvalue(PWM_CH_LED_W, PWM_DUTY_OFF);
  3642. break;
  3643. case LED_MOTION_BREATHE:
  3644. user_pwm_setvalue(PWM_CH_LED_W, breathe_duty_now[3]);
  3645. break;
  3646. case LED_MOTION_OFF:
  3647. default:
  3648. user_pwm_setvalue(PWM_CH_LED_W, PWM_DUTY_OFF);
  3649. break;
  3650. }
  3651. /*
  3652. LED breathe process
  3653. */
  3654. if(flicker[FLICKER_IDX_BREATHE].isOn)
  3655. HAL_GPIO_WritePin(OUT_LED_Breathe_GPIO_Port, OUT_LED_Breathe_Pin, GPIO_PIN_SET);
  3656. else
  3657. HAL_GPIO_WritePin(OUT_LED_Breathe_GPIO_Port, OUT_LED_Breathe_Pin, GPIO_PIN_RESET);
  3658. /*
  3659. Spearker process
  3660. */
  3661. switch(Charger.speaker.motion)
  3662. {
  3663. case SPEAKER_LONG:
  3664. timerEnable(TIMER_IDX_SPEAKER, 600);
  3665. HAL_GPIO_WritePin(OUT_Speaker_GPIO_Port, OUT_Speaker_Pin, GPIO_PIN_SET);
  3666. if(timer[TIMER_IDX_SPEAKER].isAlarm)
  3667. {
  3668. timerDisable(TIMER_IDX_SPEAKER);
  3669. Charger.speaker.motion = SPEAKER_NONE;
  3670. }
  3671. break;
  3672. case SPEAKER_SHORT:
  3673. timerEnable(TIMER_IDX_SPEAKER, 200);
  3674. HAL_GPIO_WritePin(OUT_Speaker_GPIO_Port, OUT_Speaker_Pin, GPIO_PIN_SET);
  3675. if(timer[TIMER_IDX_SPEAKER].isAlarm)
  3676. {
  3677. timerDisable(TIMER_IDX_SPEAKER);
  3678. Charger.speaker.motion = SPEAKER_NONE;
  3679. }
  3680. break;
  3681. case SPEAKER_BLINK:
  3682. if(blinker[BLINKER_IDX_SPEAKER].isOn)
  3683. HAL_GPIO_WritePin(OUT_Speaker_GPIO_Port, OUT_Speaker_Pin, GPIO_PIN_SET);
  3684. else
  3685. HAL_GPIO_WritePin(OUT_Speaker_GPIO_Port, OUT_Speaker_Pin, GPIO_PIN_RESET);
  3686. if(blinker[BLINKER_IDX_SPEAKER].blinkisFinish)
  3687. {
  3688. disblinkerTime(BLINKER_IDX_SPEAKER);
  3689. }
  3690. break;
  3691. case SPEAKER_NONE:
  3692. default:
  3693. HAL_GPIO_WritePin(OUT_Speaker_GPIO_Port, OUT_Speaker_Pin, GPIO_PIN_RESET);
  3694. break;
  3695. }
  3696. osDelay(1);
  3697. }
  3698. /* USER CODE END StartLedSpeakerTask */
  3699. }
  3700. /* USER CODE BEGIN Header_StartCpTask */
  3701. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  3702. void CpTask_CtrlCpPwm_P0(void)
  3703. {
  3704. //#ifdef FUNC_CCS //======================================================================
  3705. uint16_t MaxCurr = Charger.memory.EVSE_Config.data.item.MaxChargingCurrent;
  3706. // if (Charger.m_bRunCCS) //CCS Charge
  3707. // {
  3708. // user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_5);
  3709. // Charger.MaxChargingCurrent_Previous = MaxCurr;
  3710. // }
  3711. // else //Normal Charge
  3712. {
  3713. #ifdef FUNC_MODIFY_CP_PWM_CTRL_20231225
  3714. if (Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU || Charger.m_bModelNameDC)
  3715. {
  3716. if ((MaxCurr >= 6) && (MaxCurr <= Charger.maxRatingCurrent))
  3717. {
  3718. user_pwm_setvalue(PWM_CH_CP, GetCpPwmDuty(MaxCurr));
  3719. Charger.MaxChargingCurrent_Previous = MaxCurr;
  3720. }
  3721. else
  3722. {
  3723. user_pwm_setvalue(PWM_CH_CP, GetCpPwmDuty(Charger.maxRatingCurrent));
  3724. Charger.MaxChargingCurrent_Previous = Charger.maxRatingCurrent;
  3725. }
  3726. }
  3727. else
  3728. {
  3729. if ((Charger.AC_MaxChargingCurrentOrDuty >= 6) && (Charger.AC_MaxChargingCurrentOrDuty <= Charger.maxRatingCurrent))
  3730. {
  3731. user_pwm_setvalue(PWM_CH_CP, GetCpPwmDuty(Charger.AC_MaxChargingCurrentOrDuty));
  3732. }
  3733. else
  3734. {
  3735. if (Charger.AC_MaxChargingCurrentOrDuty == 0)
  3736. {
  3737. user_pwm_setvalue(PWM_CH_CP, 0);
  3738. }
  3739. else if (Charger.AC_MaxChargingCurrentOrDuty == 1)
  3740. {
  3741. user_pwm_setvalue(PWM_CH_CP, 0);
  3742. }
  3743. else if (Charger.AC_MaxChargingCurrentOrDuty == 5)
  3744. {
  3745. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_5);
  3746. }
  3747. else if (Charger.AC_MaxChargingCurrentOrDuty == 100)
  3748. {
  3749. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  3750. }
  3751. else
  3752. {
  3753. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  3754. }
  3755. }
  3756. Charger.MaxChargingCurrent_Previous = Charger.AC_MaxChargingCurrentOrDuty;
  3757. }
  3758. #else //FUNC_MODIFY_CP_PWM_CTRL_20231225
  3759. if ((MaxCurr >= 6) && (MaxCurr <= Charger.maxRatingCurrent))
  3760. {
  3761. user_pwm_setvalue(PWM_CH_CP, GetCpPwmDuty(MaxCurr));
  3762. Charger.MaxChargingCurrent_Previous = MaxCurr;
  3763. }
  3764. else
  3765. {
  3766. user_pwm_setvalue(PWM_CH_CP, GetCpPwmDuty(Charger.maxRatingCurrent));
  3767. Charger.MaxChargingCurrent_Previous = Charger.maxRatingCurrent;
  3768. }
  3769. #endif //FUNC_MODIFY_CP_PWM_CTRL_20231225
  3770. }
  3771. //#else //FUNC_CCS //======================================================================
  3772. // uint16_t MaxCurr = Charger.memory.EVSE_Config.data.item.MaxChargingCurrent;
  3773. // if (MaxCurr < 6)
  3774. // {
  3775. //#ifdef MODIFY_CP_TASK_CTRL_CP_PWM_BELOW_SIX
  3776. // if (MaxCurr == 0)
  3777. // {
  3778. // user_pwm_setvalue(PWM_CH_CP, 0);
  3779. // Charger.MaxChargingCurrent_Previous = MaxCurr;
  3780. // }
  3781. // else
  3782. // {
  3783. // user_pwm_setvalue(PWM_CH_CP, GetCpPwmDuty(6));
  3784. // Charger.MaxChargingCurrent_Previous = MaxCurr;
  3785. // }
  3786. //#else //MODIFY_CP_TASK_CTRL_CP_PWM_BELOW_SIX
  3787. // user_pwm_setvalue(PWM_CH_CP, GetCpPwmDuty(6));
  3788. // Charger.MaxChargingCurrent_Previous = MaxCurr;
  3789. //#endif //MODIFY_CP_TASK_CTRL_CP_PWM_BELOW_SIX
  3790. // }
  3791. // else if ((MaxCurr >= 6) && (MaxCurr <= Charger.maxRatingCurrent))
  3792. // {
  3793. // user_pwm_setvalue(PWM_CH_CP, GetCpPwmDuty(MaxCurr));
  3794. // Charger.MaxChargingCurrent_Previous = MaxCurr;
  3795. // }
  3796. // else
  3797. // {
  3798. // user_pwm_setvalue(PWM_CH_CP, GetCpPwmDuty(Charger.maxRatingCurrent));
  3799. // Charger.MaxChargingCurrent_Previous = Charger.maxRatingCurrent;
  3800. // }
  3801. //#endif //FUNC_CCS //======================================================================
  3802. }
  3803. void CpTask_CtrlCpPwm_P1(void)
  3804. {
  3805. if (
  3806. (Charger.memory.EVSE_Config.data.item.MaxChargingCurrent != Charger.MaxChargingCurrent_Previous) &&
  3807. (Charger.memory.EVSE_Config.data.item.MaxChargingCurrent <= Charger.maxRatingCurrent)
  3808. )
  3809. {
  3810. CpTask_CtrlCpPwm_P0();
  3811. }
  3812. }
  3813. #endif //MODIFY_CP_TASK_CTRL_CP_PWM
  3814. #ifdef TRIM_CCID_SELFTEST
  3815. void Proc_CCID_SelfTest(void)
  3816. {
  3817. // check Leak Module do CCID selftset.
  3818. if (Charger.CCID_Module_Type == CCID_MODULE_CORMEX)
  3819. {
  3820. Charger.Test_LeakModuleisPass = Test_LeakModule();
  3821. }
  3822. else if (Charger.CCID_Module_Type == CCID_MODULE_VAC)
  3823. {
  3824. Charger.Test_LeakModuleisPass = PASS;
  3825. }
  3826. else
  3827. {
  3828. DEBUG_INFO("Did not judge Leak module can't self test before charging.\r\n");
  3829. }
  3830. XP("Proc CCID Self-Test(%d): %s\r\n", Charger.CCID_Module_Type, Charger.Test_LeakModuleisPass == PASS ? "OK" : "NG");
  3831. }
  3832. #endif
  3833. #ifdef FUNC_AUTO_MODIFY_CCID_MODULE
  3834. uint8_t AutoModifyCCID(void)
  3835. {
  3836. uint8_t old = Charger.CCID_Module_Type;
  3837. if (Test_LeakModule() == PASS)
  3838. {
  3839. Charger.m_bCCID_MustModify = HTK_FALSE;
  3840. Charger.m_CCID_ModuleTestOK = Charger.CCID_Module_Type;
  3841. return PASS;
  3842. }
  3843. else
  3844. {
  3845. Charger.CCID_Module_Type = (Charger.CCID_Module_Type == CCID_MODULE_CORMEX ? CCID_MODULE_VAC : CCID_MODULE_CORMEX);
  3846. XP("#AutoModifyCCID: %s\r\n", CCID_MODULE_TYPE_STR);
  3847. if (Test_LeakModule() == PASS)
  3848. {
  3849. Charger.m_bCCID_MustModify = HTK_TRUE;
  3850. Charger.m_CCID_ModuleTestOK = Charger.CCID_Module_Type;
  3851. return PASS;
  3852. }
  3853. else
  3854. {
  3855. Charger.m_bCCID_MustModify = HTK_FALSE;
  3856. Charger.m_CCID_ModuleTestOK = CCID_MODULE_UNKNOW;
  3857. Charger.CCID_Module_Type = old;
  3858. return FAIL;
  3859. }
  3860. }
  3861. }
  3862. #endif
  3863. /**
  3864. * @brief Function implementing the cpTask thread.
  3865. * @param argument: Not used
  3866. * @retval None
  3867. */
  3868. /* USER CODE END Header_StartCpTask */
  3869. void StartCpTask(void const * argument)
  3870. {
  3871. /* USER CODE BEGIN StartCpTask */
  3872. //ccp
  3873. uint32_t duration_delta;
  3874. Charger.Mode = MODE_INITIAL;
  3875. Charger.Relay_Action=GPIO_RELAY_ACTION_OFF;
  3876. setLedMotion(LED_ACTION_INIT);
  3877. //Check MCU control mode depend on GPIO state
  3878. #ifdef FUNC_FORCE_RUN_CSU_MODE_WITH_DC_MODELNAME
  3879. Update_McuCtrlMode();
  3880. #else
  3881. if(HAL_GPIO_ReadPin(IN_ACT_REQ_GPIO_Port, IN_ACT_REQ_Pin) == GPIO_PIN_RESET)
  3882. {
  3883. Charger.memory.EVSE_Config.data.item.MCU_Control_Mode = MCU_CONTROL_MODE_CSU;
  3884. DEBUG_INFO(" Check MCU_Control_Mode is CSU in CP task\r\n");
  3885. }
  3886. else
  3887. {
  3888. Charger.memory.EVSE_Config.data.item.MCU_Control_Mode = MCU_CONTROL_MODE_NO_CSU;
  3889. DEBUG_INFO(" MCU_Control_Mode is NO NO NO CSU in CP task\r\n");
  3890. }
  3891. #endif
  3892. //Charger.ModelReadisOK = FAIL ;
  3893. /* Infinite loop */
  3894. for(;;)
  3895. {
  3896. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU)
  3897. {
  3898. #ifdef MODIFY_CPTASK_HEAD
  3899. Proc_CpTaskHead(HTK_FALSE);
  3900. #endif
  3901. // Cancel EV ready restart cp logic
  3902. if (Charger.Mode != MODE_HANDSHAKE)
  3903. {
  3904. timerDisable(TIMER_IDX_SIMU_DISCONNECT);
  3905. }
  3906. switch(Charger.Mode)
  3907. {
  3908. case MODE_INITIAL:
  3909. setLedMotion(LED_ACTION_INIT);
  3910. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  3911. HAL_GPIO_WritePin(OUT_Leak_Test_GPIO_Port, OUT_Leak_Test_Pin, GPIO_PIN_SET); // for VAC LeakModule use
  3912. if (Charger.ModelReadisOK ==PASS)
  3913. {
  3914. getRotarySwitchSetting( );
  3915. getGridTypeSwitchSetting( );
  3916. Charger.isTestLeakModule = ON ;
  3917. #ifdef FUNC_AUTO_MODIFY_CCID_MODULE
  3918. if (AutoModifyCCID() == PASS)
  3919. #else
  3920. if(Test_LeakModule() == PASS)
  3921. #endif
  3922. {
  3923. DEBUG_INFO("Leak module self test pass when initial.\r\n");
  3924. osDelay(1000);
  3925. if(Charger.isDebugEnable)
  3926. setChargerMode(MODE_DEBUG);
  3927. else
  3928. {
  3929. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  3930. // Cold load pick up check
  3931. if((Charger.CP_State == SYSTEM_STATE_A) && Charger.memory.coldLoadPickUp.data.item.isEnable)
  3932. {
  3933. recordChargingHis(OFF, END_STATUS_CODE_DROP);
  3934. Charger.memory.coldLoadPickUp.data.item.isEnable = OFF;
  3935. Charger.memory.coldLoadPickUp.op_bits.update = ON;
  3936. }
  3937. }
  3938. osDelay(5000) ; //wait hardware PE feedback data so delay some time
  3939. /*
  3940. TEST CALI VAL
  3941. Charger.memory.EVSE_Config.data.item.Correction_VL1[1][0] = 0x0A59;
  3942. Charger.memory.EVSE_Config.data.item.Correction_VL1[1][1] = 0x0A91;
  3943. Charger.memory.EVSE_Config.data.item.Correction_VL1[2][0] = 0x0684;
  3944. Charger.memory.EVSE_Config.data.item.Correction_VL1[2][1] = 0x06A7;
  3945. Charger.memory.EVSE_Config.data.item.Correction_CL1[1][0] = 0x0139;
  3946. Charger.memory.EVSE_Config.data.item.Correction_CL1[1][1] = 0x0140;
  3947. Charger.memory.EVSE_Config.data.item.Correction_CL1[2][0] = 0x0062;
  3948. Charger.memory.EVSE_Config.data.item.Correction_CL1[2][1] = 0x0064;
  3949. Charger.memory.EVSE_Config.data.item.Correction_Leak[1][0] = 0x0632;
  3950. Charger.memory.EVSE_Config.data.item.Correction_Leak[1][1] = 0x05DC;
  3951. Charger.memory.EVSE_Config.data.item.Correction_Leak[2][0] = 0x0488;
  3952. Charger.memory.EVSE_Config.data.item.Correction_Leak[2][1] = 0x03E8;
  3953. Charger.memory.EVSE_Config.op_bits.update = ON;
  3954. */
  3955. setChargerMode(MODE_IDLE);
  3956. }
  3957. }
  3958. else
  3959. {
  3960. Charger.Alarm_Code |= ALARM_LEAK_MODULE_FAIL;
  3961. DEBUG_INFO("Leak module self test fail when initial.\r\n");
  3962. Charger.counter.LEAK_MODULE.isLatch = ON ;
  3963. }
  3964. Charger.isTestLeakModule = OFF ;
  3965. }
  3966. break;
  3967. case MODE_IDLE:
  3968. if(isModeChange())
  3969. {
  3970. setLedMotion(LED_ACTION_IDLE);
  3971. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  3972. Charger.memory.EVSE_Config.data.item.MaxChargingCurrent = Charger.maxRatingCurrent;
  3973. #ifdef MODIFY_AC_EVSE_STATUS_MAX_CHARGING_CURRENT_VARIABLE
  3974. Charger.AC_MaxChargingCurrentOrDuty = Charger.maxRatingCurrent;
  3975. #endif
  3976. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  3977. Charger.isSetStartTime = ON ;
  3978. }
  3979. if (Charger.memory.EVSE_Config.data.item.AuthMode == AUTH_MODE_OCPP)
  3980. {
  3981. if( Charger.memory.coldLoadPickUp.data.item.isEnable || Charger.rfid.op_bits.reqStart)
  3982. {
  3983. osDelay(LED_RFID_TIME); // for Buzzer and RFID /Fail LED use
  3984. setLedMotion(LED_ACTION_AUTHED);
  3985. // Check start method type
  3986. memset(&Charger.cycle_info.userId[0], 0x00, ARRAY_SIZE(Charger.cycle_info.userId));
  3987. if(Charger.rfid.op_bits.reqStart)
  3988. {
  3989. Charger.cycle_info.StartType = START_METHOD_RFID;
  3990. memcpy(&Charger.cycle_info.userId[0], &Charger.rfid.currentCard[0], ARRAY_SIZE(Charger.rfid.currentCard));
  3991. Charger.istoHandShakeMode = ON ;
  3992. }
  3993. Charger.rfid.op_bits.reqStart = OFF;
  3994. if(Charger.memory.coldLoadPickUp.data.item.isEnable)
  3995. {
  3996. Charger.memory.coldLoadPickUp.data.item.isEnable = OFF;
  3997. Charger.memory.coldLoadPickUp.op_bits.update = ON;
  3998. DEBUG_INFO("coldLoadPickUp ON to rand wait sec.\r\n");
  3999. #ifdef MODIFY_COLD_LOAD_PICKUP_DELAY
  4000. ColdLoadPickup_Delay("coldLoadPickUp ON to rand wait sec.");
  4001. #else
  4002. osDelay((rand()%175000)+5000);
  4003. //osDelay((rand()%5000)+5000);
  4004. #endif
  4005. Charger.istoHandShakeMode = ON ;
  4006. }
  4007. }
  4008. if (Charger.istoHandShakeMode == ON) //charger can to HandShakeMode
  4009. {
  4010. if ((Charger.CP_State == SYSTEM_STATE_B)||(Charger.CP_State == SYSTEM_STATE_C))
  4011. {
  4012. timerDisable(TIMER_IDX_CP);
  4013. Charger.istoHandShakeMode = OFF ;
  4014. setChargerMode(MODE_HANDSHAKE);
  4015. if (Charger.memory.EVSE_Config.data.item.ModelName[3] == 'E')
  4016. timerEnable(TIMER_IDX_SIMU_DISCONNECT, 30000);
  4017. }
  4018. else
  4019. {
  4020. timerEnable(TIMER_IDX_CP, 180000);
  4021. }
  4022. }
  4023. }
  4024. else if (Charger.memory.EVSE_Config.data.item.AuthMode == AUTH_MODE_FREE)
  4025. {
  4026. if((Charger.CP_State == SYSTEM_STATE_B) ||
  4027. (Charger.CP_State == SYSTEM_STATE_C) ||
  4028. Charger.memory.coldLoadPickUp.data.item.isEnable )
  4029. {
  4030. setLedMotion(LED_ACTION_AUTHED);
  4031. // Check start method type
  4032. memset(&Charger.cycle_info.userId[0], 0x00, ARRAY_SIZE(Charger.cycle_info.userId));
  4033. if(Charger.rfid.op_bits.reqStart)
  4034. {
  4035. Charger.cycle_info.StartType = START_METHOD_RFID;
  4036. memcpy(&Charger.cycle_info.userId[0], &Charger.rfid.currentCard[0], ARRAY_SIZE(Charger.rfid.currentCard));
  4037. }
  4038. else
  4039. Charger.cycle_info.StartType = START_METHOD_CP;
  4040. Charger.ble.isRequestOn = OFF;
  4041. Charger.rfid.op_bits.reqStart = OFF;
  4042. if(Charger.memory.coldLoadPickUp.data.item.isEnable)
  4043. {
  4044. Charger.memory.coldLoadPickUp.data.item.isEnable = OFF;
  4045. Charger.memory.coldLoadPickUp.op_bits.update = ON;
  4046. DEBUG_INFO("coldLoadPickUp ON to rand wait sec.\r\n");
  4047. #ifdef MODIFY_COLD_LOAD_PICKUP_DELAY
  4048. ColdLoadPickup_Delay("coldLoadPickUp ON to rand wait sec.");
  4049. #else
  4050. osDelay((rand()%175000)+5000);
  4051. #endif
  4052. }
  4053. setChargerMode(MODE_HANDSHAKE);
  4054. if (Charger.memory.EVSE_Config.data.item.ModelName[3] == 'E')
  4055. timerEnable(TIMER_IDX_SIMU_DISCONNECT, 30000);
  4056. }
  4057. }
  4058. break;
  4059. case MODE_HANDSHAKE:
  4060. if(isModeChange())
  4061. {
  4062. Charger.isCP_in_B = OFF ;
  4063. Charger.isTestLeakModule = ON ;
  4064. #ifdef TRIM_CCID_SELFTEST
  4065. Proc_CCID_SelfTest();
  4066. #endif
  4067. //check Leak test pass
  4068. if (Charger.Test_LeakModuleisPass)
  4069. {
  4070. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4071. CpTask_CtrlCpPwm_P0();
  4072. #endif
  4073. DEBUG_INFO("Leak module self test pass before charging.\r\n");
  4074. }
  4075. else
  4076. {
  4077. Charger.Alarm_Code |= ALARM_LEAK_MODULE_FAIL;
  4078. DEBUG_INFO("Leak module self test fail before charging.\r\n");
  4079. Charger.counter.LEAK_MODULE.retry ++ ;
  4080. if (Charger.counter.LEAK_MODULE.retry >=2)
  4081. {
  4082. Charger.counter.LEAK_MODULE.isLatch = ON ;
  4083. }
  4084. }
  4085. osDelay(300);
  4086. Charger.isTestLeakModule = OFF ;
  4087. }
  4088. // to chaging moder
  4089. if(Charger.CP_State == SYSTEM_STATE_C)
  4090. {
  4091. setChargerMode(MODE_CHARGING);
  4092. if (Charger.isSetStartTime == ON)
  4093. {
  4094. Charger.cycle_info.startTimeTick = HAL_GetTick();
  4095. Charger.cycle_info.startDateTime.year = Charger.memory.EVSE_Config.data.item.SystemDateTime.year;
  4096. Charger.cycle_info.startDateTime.month = Charger.memory.EVSE_Config.data.item.SystemDateTime.month;
  4097. Charger.cycle_info.startDateTime.day = Charger.memory.EVSE_Config.data.item.SystemDateTime.day;
  4098. Charger.cycle_info.startDateTime.hour = Charger.memory.EVSE_Config.data.item.SystemDateTime.hour;
  4099. Charger.cycle_info.startDateTime.min = Charger.memory.EVSE_Config.data.item.SystemDateTime.min;
  4100. Charger.cycle_info.startDateTime.sec = Charger.memory.EVSE_Config.data.item.SystemDateTime.sec;
  4101. Charger.cycle_info.meterStart = Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative;
  4102. }
  4103. }
  4104. if(Charger.CP_State == SYSTEM_STATE_B)
  4105. {
  4106. Charger.isCP_in_B = ON ;
  4107. setLedMotion(LED_ACTION_CONNECTED);
  4108. }
  4109. //RFID remove gun
  4110. if ((Charger.cycle_info.StartType == START_METHOD_RFID) && (Charger.CP_State == SYSTEM_STATE_A) && (Charger.isCP_in_B == ON))
  4111. {
  4112. Charger.isCP_in_B = OFF ;
  4113. setChargerMode(MODE_IDLE);
  4114. }
  4115. //FREE remove gun
  4116. if ((Charger.cycle_info.StartType == START_METHOD_CP) && Charger.CP_State == SYSTEM_STATE_A)
  4117. {
  4118. setChargerMode(MODE_IDLE);
  4119. }
  4120. // EV ready restart cp logic
  4121. if (Charger.memory.EVSE_Config.data.item.ModelName[3] == 'E')
  4122. {
  4123. if(timer[TIMER_IDX_SIMU_DISCONNECT].isAlarm == ON)
  4124. {
  4125. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_ZERO);
  4126. osDelay(4000);
  4127. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  4128. osDelay(600);
  4129. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4130. CpTask_CtrlCpPwm_P0();
  4131. #endif
  4132. timerDisable(TIMER_IDX_SIMU_DISCONNECT);
  4133. }
  4134. }
  4135. break;
  4136. case MODE_CHARGING:
  4137. if(isModeChange())
  4138. {
  4139. #ifdef MODIFY_CPTASK_CSU_0_1_CHARGE_MODE
  4140. CpTask_CtrlCpPwm_P0();
  4141. #endif
  4142. setLedMotion(LED_ACTION_CHARGING);
  4143. }
  4144. if (Charger.memory.EVSE_Config.data.item.AuthMode == AUTH_MODE_OCPP)
  4145. {
  4146. #ifdef MODIFY_AW_CP_TASK_CSU_0_1_CHARGING_MODE_STOP_WITHOUT_STATE_C
  4147. if(Charger.CP_State!=SYSTEM_STATE_C || Charger.rfid.op_bits.reqStop )
  4148. #else
  4149. if(((Charger.CP_State!=SYSTEM_STATE_C)&&(Charger.CP_State!=SYSTEM_STATE_UNKNOWN)) || Charger.rfid.op_bits.reqStop )
  4150. #endif
  4151. {
  4152. setChargerMode(MODE_STOP);
  4153. }
  4154. else
  4155. {
  4156. Charger.Relay_Action = GPIO_RELAY_ACTION_ON;
  4157. duration_delta = (HAL_GetTick() - Charger.cycle_info.startTimeTick);
  4158. Charger.cycle_info.Duration = duration_delta;
  4159. Charger.cycle_info.Power_Consumption = Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative - Charger.cycle_info.meterStart;
  4160. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4161. CpTask_CtrlCpPwm_P1();
  4162. #endif
  4163. }
  4164. }
  4165. else if (Charger.memory.EVSE_Config.data.item.AuthMode == AUTH_MODE_FREE)
  4166. {
  4167. #ifdef MODIFY_AW_CP_TASK_CSU_0_1_CHARGING_MODE_STOP_WITHOUT_STATE_C
  4168. if(Charger.CP_State!=SYSTEM_STATE_C)
  4169. #else
  4170. if((Charger.CP_State != SYSTEM_STATE_C) && (Charger.CP_State != SYSTEM_STATE_UNKNOWN))
  4171. #endif
  4172. {
  4173. setChargerMode(MODE_STOP);
  4174. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4175. }
  4176. else
  4177. {
  4178. Charger.Relay_Action = GPIO_RELAY_ACTION_ON;
  4179. duration_delta = (HAL_GetTick() - Charger.cycle_info.startTimeTick);
  4180. Charger.cycle_info.Duration = duration_delta;
  4181. Charger.cycle_info.Power_Consumption = Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative - Charger.cycle_info.meterStart;
  4182. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4183. CpTask_CtrlCpPwm_P1();
  4184. #endif
  4185. }
  4186. }
  4187. break;
  4188. case MODE_STOP:
  4189. if(isModeChange())
  4190. {
  4191. if ((Charger.memory.EVSE_Config.data.item.AuthMode == AUTH_MODE_OCPP) && Charger.rfid.op_bits.reqStop)
  4192. {
  4193. osDelay(LED_RFID_TIME); // for Buzzer and RFID /Fail LED use
  4194. }
  4195. setLedMotion(LED_ACTION_STOP);
  4196. Charger.isCP_in_B = OFF ;
  4197. Charger.is_RFIDKeepCharger = ON ;
  4198. Charger.isSetStartTime = OFF ;
  4199. }
  4200. if (Charger.memory.EVSE_Config.data.item.AuthMode == AUTH_MODE_OCPP)
  4201. {
  4202. //GB
  4203. if(Charger.CP_State == SYSTEM_STATE_A)
  4204. {
  4205. Charger.cycle_info.endTimeTick = HAL_GetTick();
  4206. recordChargingHis(OFF, END_STATUS_CODE_NORMAL);
  4207. Charger.rfid.op_bits.reqStop = OFF;
  4208. setChargerMode(MODE_IDLE);
  4209. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4210. timerDisable(TIMER_IDX_RFID_RELAY_OFF);
  4211. Charger.isCP_in_B = OFF ;
  4212. Charger.is_RFIDKeepCharger = OFF ;
  4213. Charger.cycle_info.StartType = START_METHOD_NONE;
  4214. }
  4215. else
  4216. {
  4217. //rfid close PWM and relay
  4218. if ((Charger.rfid.op_bits.reqStop == ON)&&Charger.cycle_info.StartType == START_METHOD_RFID)
  4219. {
  4220. Charger.rfid.op_bits.reqStop = OFF;
  4221. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL); // close pwm
  4222. timerEnable( TIMER_IDX_RFID_RELAY_OFF, 3000); //is set wait 3 sec
  4223. Charger.is_RFIDKeepCharger = OFF ;
  4224. }
  4225. if (timer[TIMER_IDX_RFID_RELAY_OFF].isAlarm == ON) //is wait 3 sec
  4226. {
  4227. timerDisable(TIMER_IDX_RFID_RELAY_OFF);
  4228. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4229. setChargerMode(MODE_IDLE);
  4230. Charger.cycle_info.StartType = START_METHOD_NONE;
  4231. }
  4232. //cp close or open PWM
  4233. if((Charger.CP_State == SYSTEM_STATE_B))
  4234. {
  4235. timerDisable(TIMER_IDX_RFID_RELAY_OFF);
  4236. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4237. Charger.isCP_in_B = ON ;
  4238. }
  4239. else if (Charger.CP_State == SYSTEM_STATE_C )
  4240. {
  4241. if ( Charger.isCP_in_B == ON && Charger.is_RFIDKeepCharger == ON)
  4242. {
  4243. Charger.is_RFIDKeepCharger = OFF ;
  4244. Charger.isCP_in_B = OFF ;
  4245. Charger.isTestLeakModule = ON ;
  4246. #ifdef TRIM_CCID_SELFTEST
  4247. Proc_CCID_SelfTest();
  4248. #endif
  4249. //check Leak test pass
  4250. if (Charger.Test_LeakModuleisPass)
  4251. {
  4252. timerEnable(TIMER_IDX_CP, 180000);
  4253. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4254. CpTask_CtrlCpPwm_P0();
  4255. #endif
  4256. setChargerMode(MODE_CHARGING);
  4257. DEBUG_INFO("Leak module self test pass before charging.\r\n");
  4258. }
  4259. else
  4260. {
  4261. Charger.Alarm_Code |= ALARM_LEAK_MODULE_FAIL;
  4262. DEBUG_INFO("Leak module self test fail before charging.\r\n");
  4263. Charger.counter.LEAK_MODULE.retry ++ ;
  4264. if (Charger.counter.LEAK_MODULE.retry >=2)
  4265. {
  4266. Charger.counter.LEAK_MODULE.isLatch = ON ;
  4267. }
  4268. }
  4269. osDelay(300);
  4270. Charger.isTestLeakModule = OFF ;
  4271. }
  4272. }
  4273. }
  4274. }
  4275. else if (Charger.memory.EVSE_Config.data.item.AuthMode == AUTH_MODE_FREE)
  4276. {
  4277. if (Charger.CP_State == SYSTEM_STATE_A)
  4278. {
  4279. Charger.cycle_info.endTimeTick = HAL_GetTick();
  4280. recordChargingHis(OFF, END_STATUS_CODE_NORMAL);
  4281. Charger.ble.isRequestOff = OFF;
  4282. Charger.rfid.op_bits.reqStop = OFF;
  4283. setChargerMode(MODE_IDLE);
  4284. }
  4285. else
  4286. {
  4287. if (Charger.CP_State == SYSTEM_STATE_B)
  4288. {
  4289. Charger.isCP_in_B = ON ;
  4290. }
  4291. if((Charger.CP_State == SYSTEM_STATE_C))
  4292. {
  4293. setChargerMode(MODE_CHARGING);
  4294. }
  4295. }
  4296. }
  4297. break;
  4298. case MODE_ALARM:
  4299. if(isModeChange())
  4300. {
  4301. Charger.isSetStartTime = OFF ;
  4302. //clear blink time , Prevent red light from being snatched by others
  4303. disblinkerTime (BLINKER_IDX_LED) ;
  4304. }
  4305. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4306. //Set alarm led
  4307. if ((Charger.Alarm_Code != Charger.Alarm_Code_before) && (blinker[BLINKER_IDX_LED].blinkisFinish))
  4308. {
  4309. setLedMotion(LED_ACTION_ALARM);
  4310. Charger.Alarm_Code_before = Charger.Alarm_Code ;
  4311. }
  4312. if(Charger.Alarm_Code == 0x00)
  4313. {
  4314. Charger.Alarm_Code_before = Charger.Alarm_Code ;
  4315. if (Charger.Mode_Before_Alarm == MODE_CHARGING) // in charging mode
  4316. {
  4317. if(Charger.CP_State == SYSTEM_STATE_A) // to idle
  4318. {
  4319. Charger.cycle_info.endTimeTick = HAL_GetTick();
  4320. recordChargingHis(OFF, END_STATUS_CODE_NORMAL);
  4321. Charger.rfid.op_bits.reqStop = OFF;
  4322. Charger.ble.isRequestOff = OFF;
  4323. setChargerMode(MODE_IDLE);
  4324. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4325. timerDisable(TIMER_IDX_RFID_RELAY_OFF);
  4326. Charger.isCP_in_B = OFF ;
  4327. Charger.is_RFIDKeepCharger = OFF ;
  4328. Charger.cycle_info.StartType = START_METHOD_NONE;
  4329. }
  4330. else // to handshake
  4331. {
  4332. setChargerMode(MODE_HANDSHAKE);
  4333. Charger.isCP_in_B = ON ;
  4334. setLedMotion(LED_ACTION_CONNECTED);
  4335. }
  4336. }
  4337. else if(Charger.Mode_Before_Alarm == MODE_STOP) // in stop mode
  4338. {
  4339. if(Charger.CP_State == SYSTEM_STATE_A) // to idle
  4340. {
  4341. Charger.cycle_info.endTimeTick = HAL_GetTick();
  4342. recordChargingHis(OFF, END_STATUS_CODE_NORMAL);
  4343. Charger.rfid.op_bits.reqStop = OFF;
  4344. setChargerMode(MODE_IDLE);
  4345. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4346. timerDisable(TIMER_IDX_RFID_RELAY_OFF);
  4347. Charger.isCP_in_B = OFF ;
  4348. Charger.is_RFIDKeepCharger = OFF ;
  4349. Charger.cycle_info.StartType = START_METHOD_NONE;
  4350. }
  4351. else if ((Charger.CP_State == SYSTEM_STATE_B) && (Charger.memory.EVSE_Config.data.item.AuthMode == AUTH_MODE_OCPP)) // OCPP
  4352. {
  4353. if ( ( ((Charger.cycle_info.StartType == START_METHOD_BLE) && (Charger.is_BLEKeepCharger == ON))|| // to handshake
  4354. ((Charger.cycle_info.StartType == START_METHOD_RFID) && (Charger.is_RFIDKeepCharger == ON)) ) &&
  4355. ( Charger.isCP_in_B == ON ) )
  4356. {
  4357. setChargerMode(MODE_HANDSHAKE);
  4358. setLedMotion(LED_ACTION_CONNECTED);
  4359. }
  4360. else //to stop
  4361. {
  4362. setChargerMode(MODE_STOP);
  4363. }
  4364. }
  4365. else if ((Charger.CP_State == SYSTEM_STATE_B) && (Charger.memory.EVSE_Config.data.item.AuthMode == AUTH_MODE_FREE)) //Free
  4366. {
  4367. if ( Charger.isCP_in_B == ON ) // to handshake
  4368. {
  4369. setChargerMode(MODE_HANDSHAKE);
  4370. setLedMotion(LED_ACTION_CONNECTED);
  4371. }
  4372. else //to stop
  4373. {
  4374. setChargerMode(MODE_STOP);
  4375. }
  4376. }
  4377. else //to stop
  4378. {
  4379. setChargerMode(MODE_STOP);
  4380. }
  4381. }
  4382. else // idle
  4383. {
  4384. if(timer[TIMER_IDX_STATE_E].isAlarm == ON)
  4385. {
  4386. setChargerMode(MODE_IDLE);
  4387. }
  4388. }
  4389. }
  4390. break;
  4391. case MODE_MAINTAIN:
  4392. if(isModeChange())
  4393. {}
  4394. break;
  4395. case MODE_DEBUG:
  4396. if(isModeChange())
  4397. {
  4398. setLedMotion(LED_ACTION_DEBUG);
  4399. Charger.Relay_Action = GPIO_RELAY_ACTION_ON;
  4400. timerEnable(TIMER_IDX_DEBUG, 1000);
  4401. }
  4402. if(timer[TIMER_IDX_DEBUG].isAlarm || (Charger.Alarm_Code>0))
  4403. {
  4404. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4405. Charger.isDebugModeCPtest = ON;
  4406. }
  4407. if(Charger.isDebugModeCPtest)
  4408. {
  4409. if(Charger.CP_State == SYSTEM_STATE_A)
  4410. {
  4411. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4412. Charger.memory.EVSE_Config.data.item.MaxChargingCurrent = Charger.maxRatingCurrent;
  4413. #ifdef MODIFY_AC_EVSE_STATUS_MAX_CHARGING_CURRENT_VARIABLE
  4414. Charger.AC_MaxChargingCurrentOrDuty = Charger.maxRatingCurrent;
  4415. #endif
  4416. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  4417. }
  4418. else if (Charger.CP_State == SYSTEM_STATE_B)
  4419. {
  4420. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4421. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4422. CpTask_CtrlCpPwm_P1();
  4423. #endif
  4424. }
  4425. else if (Charger.CP_State == SYSTEM_STATE_C)
  4426. {
  4427. Charger.Relay_Action = GPIO_RELAY_ACTION_ON;
  4428. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4429. CpTask_CtrlCpPwm_P1();
  4430. #endif
  4431. }
  4432. }
  4433. break;
  4434. }
  4435. }
  4436. else if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_CSU) //CP_CSU
  4437. {
  4438. /*
  4439. MCU control mdoe with CSU
  4440. */
  4441. #ifdef MODIFY_CPTASK_HEAD
  4442. Proc_CpTaskHead(HTK_TRUE);
  4443. #endif
  4444. // Cancel EV ready restart cp logic
  4445. if (Charger.Mode != MODE_HANDSHAKE)
  4446. {
  4447. timerDisable(TIMER_IDX_SIMU_DISCONNECT);
  4448. }
  4449. switch(Charger.Mode)
  4450. {
  4451. case MODE_INITIAL:
  4452. setLedMotion(LED_ACTION_INIT);
  4453. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  4454. HAL_GPIO_WritePin(OUT_Leak_Test_GPIO_Port, OUT_Leak_Test_Pin, GPIO_PIN_SET); // for VAC LeakModule use
  4455. #ifdef FUNC_RELAY_OFF_WHEN_CSU_CMD_TIMEOUT
  4456. Charger.m_CSU_RxTick = HAL_GetTick();
  4457. #endif
  4458. if (Charger.ModelReadisOK ==PASS)
  4459. {
  4460. getRotarySwitchSetting( );
  4461. getGridTypeSwitchSetting( );
  4462. Charger.isTestLeakModule = ON ;
  4463. #ifdef FUNC_AUTO_MODIFY_CCID_MODULE
  4464. if (AutoModifyCCID() == PASS)
  4465. #else
  4466. if(Test_LeakModule() == PASS)
  4467. #endif
  4468. {
  4469. DEBUG_INFO("Leak module self test pass when initial.\r\n");
  4470. osDelay(1000);
  4471. if(Charger.isDebugEnable)
  4472. setChargerMode(MODE_DEBUG);
  4473. else
  4474. {
  4475. setChargerMode(MODE_IDLE);
  4476. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  4477. // Cold load pick up check
  4478. if((Charger.CP_State == SYSTEM_STATE_A) && Charger.memory.coldLoadPickUp.data.item.isEnable)
  4479. {
  4480. recordChargingHis(OFF, END_STATUS_CODE_DROP);
  4481. Charger.memory.coldLoadPickUp.data.item.isEnable = OFF;
  4482. Charger.memory.coldLoadPickUp.op_bits.update = ON;
  4483. }
  4484. }
  4485. }
  4486. }
  4487. else
  4488. {
  4489. Charger.Alarm_Code |= ALARM_LEAK_MODULE_FAIL;
  4490. DEBUG_INFO("Leak module self test fail when initial.\r\n");
  4491. Charger.counter.LEAK_MODULE.isLatch = ON ;
  4492. }
  4493. Charger.isTestLeakModule = OFF ;
  4494. }
  4495. break;
  4496. case MODE_IDLE:
  4497. if(isModeChange())
  4498. {
  4499. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4500. Charger.memory.EVSE_Config.data.item.MaxChargingCurrent = Charger.maxRatingCurrent;
  4501. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  4502. Charger.isSetStartTime = ON ;
  4503. #ifdef FUNC_BLOCK_CSU_CONFIG_CP_PWM_DUTY
  4504. Charger.m_bBlockCsuCpPwmDuty = HTK_DISABLE;
  4505. #endif
  4506. }
  4507. if(Charger.am3352.isRequestOn &&
  4508. (Charger.CP_State != SYSTEM_STATE_A))
  4509. {
  4510. setChargerMode(MODE_HANDSHAKE);
  4511. if (Charger.memory.EVSE_Config.data.item.ModelName[3] == 'E')
  4512. timerEnable(TIMER_IDX_SIMU_DISCONNECT, 30000);
  4513. }
  4514. break;
  4515. case MODE_HANDSHAKE:
  4516. if(isModeChange())
  4517. {
  4518. Charger.isCP_in_B = OFF ;
  4519. Charger.isTestLeakModule = ON ;
  4520. #ifdef TRIM_CCID_SELFTEST
  4521. Proc_CCID_SelfTest();
  4522. #endif
  4523. //check Leak test pass
  4524. if (Charger.Test_LeakModuleisPass)
  4525. {
  4526. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4527. #ifdef MODIFY_MODE_HANDSHAKE_STOP_PWM_WITHOUT_OK_STATE
  4528. if (Charger.m_bSafetyRegulationGB)
  4529. {
  4530. if(Charger.CP_State == SYSTEM_STATE_B || Charger.CP_State == SYSTEM_STATE_C)
  4531. {
  4532. CpTask_CtrlCpPwm_P0();
  4533. }
  4534. }
  4535. else
  4536. {
  4537. CpTask_CtrlCpPwm_P0();
  4538. }
  4539. #endif //MODIFY_MODE_HANDSHAKE_STOP_PWM_WITHOUT_OK_STATE
  4540. #else //MODIFY_CP_TASK_CTRL_CP_PWM
  4541. CpTask_CtrlCpPwm_P0();
  4542. #endif //MODIFY_CP_TASK_CTRL_CP_PWM
  4543. osDelay(300);
  4544. DEBUG_INFO("Leak module self test pass before charging.\r\n");
  4545. }
  4546. else
  4547. {
  4548. Charger.Alarm_Code |= ALARM_LEAK_MODULE_FAIL;
  4549. DEBUG_INFO("Leak module self test fail before charging.\r\n");
  4550. }
  4551. Charger.isTestLeakModule = OFF ;
  4552. }
  4553. if(Charger.CP_State == SYSTEM_STATE_C)
  4554. {
  4555. #ifdef MODIFY_MODE_HANDSHAKE_STOP_PWM_WITHOUT_OK_STATE
  4556. if (Charger.m_bSafetyRegulationGB)
  4557. {
  4558. Charger.m_bBlockCsuCpPwmDuty = HTK_DISABLE;
  4559. if (Charger.m_CP_CurPWM == PWM_DUTY_FULL)
  4560. {
  4561. CpTask_CtrlCpPwm_P0();
  4562. }
  4563. }
  4564. #endif
  4565. setChargerMode(MODE_CHARGING);
  4566. timerDisable(TIMER_IDX_CP);
  4567. if (Charger.isSetStartTime == ON)
  4568. {
  4569. Charger.cycle_info.startTimeTick = HAL_GetTick();
  4570. Charger.cycle_info.startDateTime.year = Charger.memory.EVSE_Config.data.item.SystemDateTime.year;
  4571. Charger.cycle_info.startDateTime.month = Charger.memory.EVSE_Config.data.item.SystemDateTime.month;
  4572. Charger.cycle_info.startDateTime.day = Charger.memory.EVSE_Config.data.item.SystemDateTime.day;
  4573. Charger.cycle_info.startDateTime.hour = Charger.memory.EVSE_Config.data.item.SystemDateTime.hour;
  4574. Charger.cycle_info.startDateTime.min = Charger.memory.EVSE_Config.data.item.SystemDateTime.min;
  4575. Charger.cycle_info.startDateTime.sec = Charger.memory.EVSE_Config.data.item.SystemDateTime.sec;
  4576. Charger.cycle_info.meterStart = Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative;
  4577. }
  4578. }
  4579. if(Charger.CP_State == SYSTEM_STATE_B)
  4580. {
  4581. #ifdef MODIFY_MODE_HANDSHAKE_STOP_PWM_WITHOUT_OK_STATE
  4582. if (Charger.m_bSafetyRegulationGB)
  4583. {
  4584. Charger.m_bBlockCsuCpPwmDuty = HTK_DISABLE;
  4585. if (Charger.m_CP_CurPWM == PWM_DUTY_FULL)
  4586. {
  4587. CpTask_CtrlCpPwm_P0();
  4588. }
  4589. }
  4590. #endif
  4591. Charger.isCP_in_B = ON ;
  4592. }
  4593. if(((Charger.CP_State == SYSTEM_STATE_A) && Charger.isCP_in_B) || !Charger.am3352.isRequestOn)
  4594. {
  4595. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL); // close pwm
  4596. setChargerMode(MODE_IDLE);
  4597. timerDisable(TIMER_IDX_CP);
  4598. }
  4599. // EV ready restart cp logic
  4600. if (Charger.memory.EVSE_Config.data.item.ModelName[3] == 'E')
  4601. {
  4602. if(timer[TIMER_IDX_SIMU_DISCONNECT].isAlarm == ON)
  4603. {
  4604. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_ZERO);
  4605. osDelay(4000);
  4606. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  4607. osDelay(600);
  4608. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4609. CpTask_CtrlCpPwm_P0();
  4610. #endif
  4611. timerDisable(TIMER_IDX_SIMU_DISCONNECT);
  4612. timer[TIMER_IDX_SIMU_DISCONNECT].isAlarm = OFF ;
  4613. }
  4614. }
  4615. #ifdef MODIFY_MODE_HANDSHAKE_STOP_PWM_WITHOUT_OK_STATE
  4616. if (Charger.m_bSafetyRegulationGB)
  4617. {
  4618. if (
  4619. (Charger.CP_State == SYSTEM_STATE_D || Charger.CP_State == SYSTEM_STATE_E) &&
  4620. (Charger.am3352.isRequestOn)
  4621. )
  4622. {
  4623. Charger.m_bBlockCsuCpPwmDuty = HTK_ENABLE;
  4624. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  4625. }
  4626. }
  4627. #endif
  4628. break;
  4629. case MODE_CHARGING:
  4630. if(isModeChange())
  4631. {
  4632. #ifdef MODIFY_CPTASK_CSU_0_1_CHARGE_MODE
  4633. CpTask_CtrlCpPwm_P0();
  4634. #endif
  4635. }
  4636. #ifdef MODIFY_AW_CP_TASK_CSU_0_1_CHARGING_MODE_STOP_WITHOUT_STATE_C
  4637. if(Charger.CP_State != SYSTEM_STATE_C || !Charger.am3352.isRequestOn)
  4638. #else
  4639. if(!Charger.am3352.isRequestOn)
  4640. #endif
  4641. {
  4642. setChargerMode(MODE_STOP);
  4643. if(Charger.CP_State != SYSTEM_STATE_C)
  4644. {
  4645. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4646. #ifdef MODIFY_MODE_CHARGING_STOP_PWM_WITHOUT_OK_STATE
  4647. if (Charger.m_bSafetyRegulationGB)
  4648. {
  4649. if (
  4650. (Charger.CP_State == SYSTEM_STATE_D || Charger.CP_State == SYSTEM_STATE_E) &&
  4651. (Charger.am3352.isRequestOn)
  4652. )
  4653. {
  4654. Charger.m_bBlockCsuCpPwmDuty = HTK_ENABLE;
  4655. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  4656. }
  4657. }
  4658. #endif //MODIFY_MODE_CHARGING_STOP_PWM_WITHOUT_OK_STATE
  4659. }
  4660. }
  4661. #ifdef FUNC_RELAY_OFF_WHEN_CSU_CMD_TIMEOUT
  4662. else if (HTK_IsTimeout(Charger.m_CSU_RxTick, CSU_CMD_TIMEOUT_SEC * 1000))
  4663. {
  4664. if (Charger.Relay_Action != GPIO_RELAY_ACTION_OFF)
  4665. {
  4666. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4667. XP("#<CSU_TIMEOUT> RELAY => OFF\r\n");
  4668. }
  4669. #ifdef FUNC_REQUEST_OFF_WHEN_CSU_CMD_TIMEOUT
  4670. if (Charger.am3352.isRequestOn != OFF)
  4671. {
  4672. Charger.am3352.isRequestOn = OFF;
  4673. XP("#<CSU_TIMEOUT> REQUEST => OFF\r\n");
  4674. }
  4675. #endif
  4676. }
  4677. #endif //FUNC_RELAY_OFF_WHEN_CSU_CMD_TIMEOUT
  4678. else
  4679. {
  4680. #ifdef USE_OLD_ONE_STEP_CHARGE_FOR_AW_NOODOE
  4681. Charger.Relay_Action = GPIO_RELAY_ACTION_ON;
  4682. #endif
  4683. duration_delta = (HAL_GetTick() - Charger.cycle_info.startTimeTick);
  4684. Charger.cycle_info.Duration = duration_delta;
  4685. Charger.cycle_info.Power_Consumption = Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative - Charger.cycle_info.meterStart;
  4686. }
  4687. break;
  4688. case MODE_STOP:
  4689. if(isModeChange())
  4690. {
  4691. Charger.isSetStartTime = OFF ;
  4692. Charger.isCP_in_B = OFF ;
  4693. if (!Charger.am3352.isRequestOn) //represent swip card stop charge in 6V
  4694. {
  4695. Charger.rfid.op_bits.reqStop = ON ;
  4696. }
  4697. }
  4698. if((Charger.CP_State == SYSTEM_STATE_A) || !Charger.am3352.isRequestOn)
  4699. {
  4700. if( !Charger.am3352.isRequestOn)
  4701. {
  4702. if (Charger.rfid.op_bits.reqStop == ON || (Charger.CP_State == SYSTEM_STATE_B)) //1. swip card stop charge in 6V. , 2.cp-> B then swip card stop charge
  4703. {
  4704. if(Charger.CP_State == SYSTEM_STATE_B)
  4705. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4706. Charger.rfid.op_bits.reqStop = OFF;
  4707. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL); // close pwm
  4708. timerEnable( TIMER_IDX_RFID_RELAY_OFF, 3000); //is set wait 3 sec
  4709. Charger.cycle_info.endTimeTick = HAL_GetTick();
  4710. recordChargingHis(OFF, END_STATUS_CODE_NORMAL);
  4711. Charger.isCP_in_B = OFF ;
  4712. }
  4713. if (timer[TIMER_IDX_RFID_RELAY_OFF].isAlarm == ON ) //is wait 3 sec
  4714. {
  4715. timerDisable(TIMER_IDX_RFID_RELAY_OFF);
  4716. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4717. #ifdef FUNC_BLOCK_CSU_CONFIG_CP_PWM_DUTY
  4718. Charger.m_bBlockCsuCpPwmDuty = HTK_DISABLE;
  4719. #endif
  4720. setChargerMode(MODE_IDLE);
  4721. Charger.cycle_info.StartType = START_METHOD_NONE;
  4722. }
  4723. }
  4724. else //CP_State == SYSTEM_STATE_A
  4725. {
  4726. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL); // close pwm
  4727. Charger.cycle_info.endTimeTick = HAL_GetTick();
  4728. recordChargingHis(OFF, END_STATUS_CODE_NORMAL);
  4729. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4730. #ifdef FUNC_BLOCK_CSU_CONFIG_CP_PWM_DUTY
  4731. Charger.m_bBlockCsuCpPwmDuty = HTK_DISABLE;
  4732. #endif
  4733. setChargerMode(MODE_IDLE);
  4734. Charger.isCP_in_B = OFF ;
  4735. }
  4736. }
  4737. else // Charger.am3352.isRequestOn == 1
  4738. {
  4739. if((Charger.CP_State == SYSTEM_STATE_B))
  4740. {
  4741. timerDisable(TIMER_IDX_RFID_RELAY_OFF);
  4742. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4743. Charger.isCP_in_B = ON ;
  4744. }
  4745. if((Charger.CP_State == SYSTEM_STATE_C) && (!Charger.am3352.isRequestOff) )
  4746. {
  4747. #ifdef FUNC_BLOCK_CSU_CONFIG_CP_PWM_DUTY
  4748. Charger.m_bBlockCsuCpPwmDuty = HTK_DISABLE;
  4749. #endif
  4750. #ifdef MODIFY_MODE_STOP_TO_CHARGE_PROCESS
  4751. setChargerMode(MODE_CHARGING);
  4752. #else
  4753. setChargerMode(MODE_HANDSHAKE);
  4754. #endif
  4755. }
  4756. #ifdef MODIFY_MODE_STOP_STOP_PWM_WITHOUT_OK_STATE
  4757. if (Charger.m_bSafetyRegulationGB)
  4758. {
  4759. if (
  4760. (Charger.CP_State == SYSTEM_STATE_D || Charger.CP_State == SYSTEM_STATE_E) &&
  4761. (Charger.am3352.isRequestOn)
  4762. )
  4763. {
  4764. Charger.m_bBlockCsuCpPwmDuty = HTK_ENABLE;
  4765. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  4766. }
  4767. if ((Charger.CP_State == SYSTEM_STATE_B) && (Charger.am3352.isRequestOn))
  4768. {
  4769. Charger.m_bBlockCsuCpPwmDuty = HTK_DISABLE;
  4770. if (Charger.m_CP_CurPWM == PWM_DUTY_FULL)
  4771. {
  4772. CpTask_CtrlCpPwm_P0();
  4773. }
  4774. }
  4775. }
  4776. #endif //MODIFY_MODE_STOP_STOP_PWM_WITHOUT_OK_STATE
  4777. }
  4778. break;
  4779. case MODE_ALARM:
  4780. if(isModeChange())
  4781. {
  4782. Charger.isSetStartTime = OFF ;
  4783. if (Charger.CSUisReady == ON )
  4784. {
  4785. //clear blink time , Prevent red light from being snatched by others
  4786. disblinkerTime (BLINKER_IDX_LED) ;
  4787. //clear led Action
  4788. Charger.am3352.LedActionState = 0 ;
  4789. }
  4790. }
  4791. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4792. if(Charger.Alarm_Code == 0x00)
  4793. {
  4794. Charger.am3352.LedActionState = 0 ;
  4795. if (Charger.Mode_Before_Alarm == MODE_CHARGING) // in charging mode
  4796. {
  4797. if(Charger.CP_State == SYSTEM_STATE_B ) // to handshake
  4798. {
  4799. setChargerMode(MODE_HANDSHAKE);
  4800. }
  4801. else // to idle
  4802. {
  4803. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  4804. setChargerMode(MODE_IDLE);
  4805. }
  4806. }
  4807. else if(Charger.Mode_Before_Alarm == MODE_STOP)
  4808. {
  4809. if (Charger.isCP_in_B == ON) // to handshake
  4810. {
  4811. setChargerMode(MODE_HANDSHAKE);
  4812. }
  4813. else // to stop
  4814. {
  4815. setChargerMode(MODE_STOP);
  4816. }
  4817. }
  4818. else // to idle
  4819. {
  4820. if(timer[TIMER_IDX_STATE_E].isAlarm == ON)
  4821. {
  4822. setChargerMode(MODE_IDLE);
  4823. }
  4824. }
  4825. }
  4826. break;
  4827. case MODE_DEBUG:
  4828. if(isModeChange())
  4829. {
  4830. setLedMotion(LED_ACTION_DEBUG);
  4831. Charger.Relay_Action = GPIO_RELAY_ACTION_ON;
  4832. timerEnable(TIMER_IDX_DEBUG, 1000);
  4833. }
  4834. if(timer[TIMER_IDX_DEBUG].isAlarm || (Charger.Alarm_Code>0))
  4835. {
  4836. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4837. Charger.isDebugModeCPtest = ON;
  4838. }
  4839. if(Charger.isDebugModeCPtest)
  4840. {
  4841. if(Charger.CP_State == SYSTEM_STATE_A)
  4842. {
  4843. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4844. Charger.memory.EVSE_Config.data.item.MaxChargingCurrent = Charger.maxRatingCurrent;
  4845. #ifdef MODIFY_AC_EVSE_STATUS_MAX_CHARGING_CURRENT_VARIABLE
  4846. Charger.AC_MaxChargingCurrentOrDuty = Charger.maxRatingCurrent;
  4847. #endif
  4848. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  4849. }
  4850. else if (Charger.CP_State == SYSTEM_STATE_B)
  4851. {
  4852. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4853. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4854. CpTask_CtrlCpPwm_P1();
  4855. #endif
  4856. }
  4857. else if (Charger.CP_State == SYSTEM_STATE_C)
  4858. {
  4859. Charger.Relay_Action = GPIO_RELAY_ACTION_ON;
  4860. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4861. CpTask_CtrlCpPwm_P1();
  4862. #endif
  4863. }
  4864. }
  4865. break;
  4866. default:
  4867. setChargerMode(MODE_IDLE);
  4868. break;
  4869. }
  4870. }
  4871. else
  4872. {
  4873. /*By Pass Mode*/
  4874. }
  4875. osDelay(1);
  4876. }
  4877. /* USER CODE END StartCpTask */
  4878. }
  4879. /* USER CODE BEGIN Header_StartAlarmTask */
  4880. /**
  4881. * @brief Function implementing the alarmTask thread.
  4882. * @param argument: Not used
  4883. * @retval None
  4884. */
  4885. /* USER CODE END Header_StartAlarmTask */
  4886. void StartAlarmTask(void const * argument)
  4887. {
  4888. /* USER CODE BEGIN StartAlarmTask */
  4889. //aalarm
  4890. timerEnable(TIMER_IDX_PE_DETECT, 30000);
  4891. /* Infinite loop */
  4892. for(;;)
  4893. {
  4894. if(Charger.Mode != MODE_INITIAL && Charger.Mode != MODE_DEBUG)
  4895. {
  4896. recordAlarmHis();
  4897. //================================
  4898. // CP fail alarm detect
  4899. //================================
  4900. #ifdef CP_ALARM_PROTECT
  4901. if ((Charger.CP_State == SYSTEM_STATE_UNKNOWN)&&(!(Charger.Alarm_Code & ALARM_MCU_TESTFAIL)))
  4902. {
  4903. if(Charger.counter.CP.fail >1000)
  4904. {
  4905. if(!(Charger.Alarm_Code & ALARM_CP_ERROR))
  4906. {
  4907. Charger.counter.CP.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  4908. Charger.Alarm_Code |= ALARM_CP_ERROR;
  4909. DEBUG_ERROR("Alarm CP occur.\r\n");
  4910. }
  4911. }
  4912. else
  4913. Charger.counter.CP.fail++;
  4914. }
  4915. #ifdef MODIFY_ALARMTASK_CP_ERROR_RECOVER
  4916. else if (Charger.CP_State != SYSTEM_STATE_UNKNOWN)
  4917. #else
  4918. else if ((Charger.CP_State != SYSTEM_STATE_UNKNOWN)&&(Charger.CP_State!=SYSTEM_STATE_D)&&
  4919. (Charger.CP_State != SYSTEM_STATE_E)&&(Charger.CP_State != SYSTEM_STATE_F))
  4920. #endif
  4921. {
  4922. Charger.counter.CP.fail = 0;
  4923. if((Charger.Alarm_Code & ALARM_CP_ERROR))
  4924. {
  4925. osDelay(1000);
  4926. Charger.counter.CP.retry++;
  4927. Charger.Alarm_Code &= ~ALARM_CP_ERROR;
  4928. DEBUG_INFO("Alarm CP recover.\r\n");
  4929. }
  4930. }
  4931. #endif //CP_ALARM_PROTECT
  4932. //================================
  4933. // Over voltage alarm detect
  4934. //================================
  4935. #ifdef OVP_PROTECT
  4936. if(Charger.Voltage[0] > ALARM_SPEC_OV)
  4937. {
  4938. if(Charger.counter.OV.fail > 1000)
  4939. {
  4940. Charger.counter.OV.restore = 0 ;
  4941. if(!(Charger.Alarm_Code & ALARM_OVER_VOLTAGE))
  4942. {
  4943. Charger.Alarm_Code |= ALARM_OVER_VOLTAGE;
  4944. DEBUG_ERROR("Alarm OV occur.\r\n");
  4945. }
  4946. }
  4947. else
  4948. Charger.counter.OV.fail++;
  4949. }
  4950. else if(Charger.Voltage[0] < (ALARM_SPEC_OV-ALARM_SPEC_OUV_HYSTERESIS))
  4951. {
  4952. if((Charger.Alarm_Code & ALARM_OVER_VOLTAGE))
  4953. {
  4954. if (Charger.counter.OV.restore > 1000 && blinker[BLINKER_IDX_LED].blinkisFinish)
  4955. {
  4956. Charger.counter.OV.fail = 0 ;
  4957. DEBUG_INFO("Alarm OV is coldpickup ing..... \r\n");
  4958. Charger.counter.OV.isOccurInCharging = ((Charger.CP_State == SYSTEM_STATE_B)?ON:OFF);
  4959. if (Charger.counter.OV.isOccurInCharging)
  4960. {
  4961. #ifdef MODIFY_COLD_LOAD_PICKUP_DELAY
  4962. ColdLoadPickup_Delay("Alarm OV is coldpickup ing..... ");
  4963. #else
  4964. osDelay((rand()%175000)+5000);
  4965. #endif
  4966. }
  4967. Charger.Alarm_Code &= ~ALARM_OVER_VOLTAGE;
  4968. DEBUG_INFO("Alarm OV recover.\r\n");
  4969. }
  4970. else
  4971. {
  4972. Charger.counter.OV.restore++;
  4973. }
  4974. }
  4975. else
  4976. {
  4977. Charger.counter.OV.fail = 0 ;
  4978. Charger.counter.OV.restore = 0 ;
  4979. }
  4980. }
  4981. #endif //OVP_PROTECT
  4982. //================================
  4983. // Under voltage alarm detect
  4984. //================================
  4985. #ifdef UVP_PROTECT
  4986. if(Charger.Voltage[0] < ALARM_SPEC_UV)
  4987. {
  4988. if(Charger.counter.UV.fail > 1000)
  4989. {
  4990. Charger.counter.UV.restore = 0 ;
  4991. if(!(Charger.Alarm_Code & ALARM_UNDER_VOLTAGE))
  4992. {
  4993. Charger.Alarm_Code |= ALARM_UNDER_VOLTAGE;
  4994. DEBUG_ERROR("Alarm UV occur.\r\n");
  4995. }
  4996. }
  4997. else
  4998. Charger.counter.UV.fail++;
  4999. }
  5000. else if(Charger.Voltage[0] > (ALARM_SPEC_UV+ALARM_SPEC_OUV_HYSTERESIS))
  5001. {
  5002. if(Charger.Alarm_Code & ALARM_UNDER_VOLTAGE)
  5003. {
  5004. if (Charger.counter.UV.restore > 1000 && blinker[BLINKER_IDX_LED].blinkisFinish)
  5005. {
  5006. Charger.counter.UV.fail = 0 ;
  5007. DEBUG_INFO("Alarm UV is coldpickup ing..... \r\n");
  5008. Charger.counter.UV.isOccurInCharging = ((Charger.CP_State == SYSTEM_STATE_B)?ON:OFF);
  5009. if (Charger.counter.UV.isOccurInCharging)
  5010. {
  5011. #ifdef MODIFY_COLD_LOAD_PICKUP_DELAY
  5012. ColdLoadPickup_Delay("Alarm UV is coldpickup ing..... ");
  5013. #else
  5014. osDelay((rand()%175000)+5000);
  5015. #endif
  5016. }
  5017. Charger.Alarm_Code &= ~ALARM_UNDER_VOLTAGE;
  5018. DEBUG_INFO("Alarm UV recover.\r\n");
  5019. }
  5020. else
  5021. {
  5022. Charger.counter.UV.restore++ ;
  5023. }
  5024. }
  5025. else
  5026. {
  5027. Charger.counter.UV.restore = 0 ;
  5028. Charger.counter.UV.fail = 0 ;
  5029. }
  5030. }
  5031. #endif //UVP_PROTECT
  5032. //================================
  5033. // Over current alarm detect
  5034. //================================
  5035. #ifdef OCP_PROTECT
  5036. #ifdef FUNC_OCP_WITH_PP
  5037. {
  5038. uint16_t CurMaxCurr = 0; //A
  5039. uint16_t OC_BegThreshold = 0; //Occur: 0.01A
  5040. uint16_t OC_EndThreshold = 0; //Recover: 0.01A
  5041. CurMaxCurr = HTK_GET_VAL_MIN(Charger.memory.EVSE_Config.data.item.MaxChargingCurrent, Charger.maxRatingCurrent);
  5042. #ifdef FUNC_SWITCH_RELAY_WELDING_PROTECT
  5043. if (Charger.m_bSetOCP_32A == 1)
  5044. {
  5045. CurMaxCurr = 32;
  5046. }
  5047. #endif
  5048. //// if (Charger.m_bDetectPP)
  5049. //// {
  5050. //// CurMaxCurr = HTK_GET_VAL_MIN(CurMaxCurr, Charger.m_PPInfo.m_CurCurr);
  5051. //// }
  5052. #ifdef MODIFY_OCP_PROC_WITH_MODELNAME_IDX_3_EURO_SPEC
  5053. if (Charger.m_bModelNameWithEuroSpecOCP)
  5054. #else
  5055. if (Charger.memory.EVSE_Config.data.item.ModelName[3] == 'E')
  5056. #endif
  5057. {
  5058. OC_BegThreshold = (CurMaxCurr * Charger.OCP_Magnification) + 0;
  5059. OC_EndThreshold = (CurMaxCurr * Charger.OCP_Magnification) - 200;
  5060. }
  5061. else
  5062. {
  5063. if (CurMaxCurr > 20)
  5064. {
  5065. OC_BegThreshold = (CurMaxCurr * Charger.OCP_Magnification) + 0;
  5066. OC_EndThreshold = (CurMaxCurr * Charger.OCP_Magnification) - 200;
  5067. }
  5068. else
  5069. {
  5070. OC_BegThreshold = (CurMaxCurr * 100) + 200;
  5071. OC_EndThreshold = (CurMaxCurr * 100) + 0;
  5072. }
  5073. }
  5074. Charger.m_OCP_CurMaxCurr = CurMaxCurr;
  5075. Charger.m_OCP_BegThreshold = OC_BegThreshold;
  5076. Charger.m_OCP_EndThreshold = OC_EndThreshold;
  5077. //--------------------------------
  5078. // L1 Over current
  5079. if(Charger.Current[0] >= 4800)
  5080. {
  5081. if(Charger.counter.OC.fail > 5)
  5082. {
  5083. if(!(Charger.Alarm_Code & ALARM_CIRCUIT_SHORT))
  5084. {
  5085. Charger.counter.OC.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5086. Charger.Alarm_Code |= ALARM_CIRCUIT_SHORT;
  5087. if (Charger.counter.OC.isLatch == OFF)
  5088. {
  5089. Charger.counter.OC.isLatch = ON ;
  5090. setLedMotion(LED_ACTION_ALARM);
  5091. timerDisable(TIMER_IDX_RETRY_OC);
  5092. }
  5093. DEBUG_INFO("Correction Current[0]: %f\r\n", Charger.Current[0]/100.0);
  5094. DEBUG_ERROR("Alarm SCP occur.\r\n");
  5095. }
  5096. }
  5097. else
  5098. Charger.counter.OC.fail++;
  5099. }
  5100. //else if(Charger.Current[0] > (Charger.memory.EVSE_Config.data.item.MaxChargingCurrent>=Charger.maxRatingCurrent ? Charger.maxRatingCurrent*Charger.OCP_Magnification : Charger.memory.EVSE_Config.data.item.MaxChargingCurrent*Charger.OCP_Magnification))
  5101. else if (Charger.Current[0] > OC_BegThreshold)
  5102. {
  5103. if(Charger.counter.OC.fail > 6000)
  5104. {
  5105. if(!(Charger.Alarm_Code & ALARM_OVER_CURRENT))
  5106. {
  5107. Charger.counter.OC.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5108. Charger.Alarm_Code |= ALARM_OVER_CURRENT;
  5109. DEBUG_INFO("Correction Current[0]: %f\r\n", Charger.Current[0]/100.0);
  5110. DEBUG_ERROR("Alarm OC occur.\r\n");
  5111. timerEnable(TIMER_IDX_RETRY_OC, ALARM_RETRY_INTERVAL_OC);
  5112. Charger.counter.OC.retry++;
  5113. }
  5114. }
  5115. else
  5116. Charger.counter.OC.fail++;
  5117. }
  5118. //else if(Charger.Current[0] < (Charger.memory.EVSE_Config.data.item.MaxChargingCurrent>=Charger.maxRatingCurrent ? ((Charger.maxRatingCurrent*Charger.OCP_Magnification)-200) : ((Charger.memory.EVSE_Config.data.item.MaxChargingCurrent*Charger.OCP_Magnification)-200)))
  5119. else if (Charger.Current[0] < OC_EndThreshold)
  5120. {
  5121. Charger.counter.OC.fail = 0;
  5122. if ((Charger.counter.OC.retry > ALARM_OC_RETRY_COUNT))
  5123. {
  5124. if (Charger.counter.OC.isLatch == OFF)
  5125. {
  5126. Charger.counter.OC.isLatch = ON ;
  5127. setLedMotion(LED_ACTION_ALARM);
  5128. timerDisable(TIMER_IDX_RETRY_OC);
  5129. }
  5130. }
  5131. else if((Charger.Alarm_Code & ALARM_OVER_CURRENT) &&
  5132. (Charger.counter.OC.retry <= ALARM_OC_RETRY_COUNT) &&
  5133. (timer[TIMER_IDX_RETRY_OC].isAlarm == ON) &&
  5134. blinker[BLINKER_IDX_LED].blinkisFinish )
  5135. {
  5136. timerDisable(TIMER_IDX_RETRY_OC);
  5137. osDelay(1000);
  5138. Charger.Alarm_Code &= ~ALARM_OVER_CURRENT;
  5139. Charger.Alarm_Code &= ~ALARM_CIRCUIT_SHORT;
  5140. DEBUG_INFO("Alarm OC recover.\r\n");
  5141. }
  5142. }
  5143. }
  5144. #else //FUNC_OCP_WITH_PP
  5145. //Old Procedure (Deleted)
  5146. #endif //FUNC_OCP_WITH_PP
  5147. #endif //OCP_PROTECT
  5148. //================================
  5149. // Over temperature alarm detect
  5150. //================================
  5151. #ifdef OTP_PROTECT
  5152. if (Charger.temperature.SystemAmbientTemp > ALARM_SPEC_OT_2)
  5153. {
  5154. if(Charger.counter.OT.fail > 2000)
  5155. {
  5156. if (Charger.counter.OT.isLatch == OFF)
  5157. {
  5158. Charger.counter.OT.isLatch = ON ;
  5159. setLedMotion(LED_ACTION_ALARM);
  5160. }
  5161. //Charger.counter.OT.isLatch = ON ;
  5162. if(!(Charger.Alarm_Code & ALARM_OVER_TEMPERATURE))
  5163. {
  5164. Charger.counter.OT.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5165. Charger.Alarm_Code |= ALARM_OVER_TEMPERATURE;
  5166. DEBUG_ERROR("Latch OT2 occur.\r\n");
  5167. DEBUG_ERROR("Tmp = %d.\r\n",Charger.temperature.SystemAmbientTemp);
  5168. }
  5169. }
  5170. else
  5171. Charger.counter.OT.fail++;
  5172. }
  5173. else if((Charger.temperature.SystemAmbientTemp > ALARM_SPEC_OT_1) && (!Charger.counter.OT.isLatch))
  5174. {
  5175. if(Charger.counter.OT.fail > 1000)
  5176. {
  5177. if(!(Charger.Alarm_Code & ALARM_OVER_TEMPERATURE))
  5178. {
  5179. Charger.counter.OT.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5180. Charger.Alarm_Code |= ALARM_OVER_TEMPERATURE;
  5181. DEBUG_ERROR("Alarm OT occur.\r\n");
  5182. }
  5183. }
  5184. else
  5185. Charger.counter.OT.fail++;
  5186. }
  5187. else if((Charger.temperature.SystemAmbientTemp < (ALARM_SPEC_OT_1-ALARM_SPEC_OT_HYSTERESIS)) && (!Charger.counter.OT.isLatch))
  5188. {
  5189. Charger.counter.OT.fail = 0;
  5190. if((Charger.Alarm_Code & ALARM_OVER_TEMPERATURE) && blinker[BLINKER_IDX_LED].blinkisFinish)
  5191. {
  5192. Charger.counter.OT.retry++;
  5193. Charger.Alarm_Code &= ~ALARM_OVER_TEMPERATURE;
  5194. DEBUG_INFO("Alarm OT recover.\r\n");
  5195. }
  5196. }
  5197. /*
  5198. // For multi step OTP logic
  5199. if(Charger.temperature.SystemAmbientTemp > ALARM_SPEC_OT_2)
  5200. {
  5201. if(Charger.counter.OT.fail > 1000)
  5202. {
  5203. if(!(Charger.Alarm_Code & ALARM_OVER_TEMPERATURE))
  5204. {
  5205. Charger.counter.OT.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5206. Charger.Alarm_Code |= ALARM_OVER_TEMPERATURE;
  5207. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  5208. blinkerTimeSet(500, 500, 3000, 3);
  5209. }
  5210. DEBUG_ERROR("Alarm OT occur.\r\n");
  5211. }
  5212. }
  5213. else
  5214. Charger.counter.OT.fail++;
  5215. }
  5216. else if(Charger.temperature.SystemAmbientTemp > ALARM_SPEC_OT_1)
  5217. {
  5218. if(Charger.counter.OT.fail > 1000)
  5219. {
  5220. if(!timer[TIMER_IDX_RETRY_OT].isEnable || timer[TIMER_IDX_RETRY_OT].isAlarm)
  5221. {
  5222. Charger.memory.EVSE_Config.data.item.MaxChargingCurrent *= 0.75;
  5223. }
  5224. timerEnable(TIMER_IDX_RETRY_OT, ALARM_RETRY_INTERVAL_OT);
  5225. }
  5226. else
  5227. Charger.counter.OT.fail++;
  5228. }
  5229. else if(Charger.temperature.SystemAmbientTemp < (ALARM_SPEC_OT_1-ALARM_SPEC_OT_HYSTERESIS))
  5230. {
  5231. Charger.counter.OT.fail = 0;
  5232. if((Charger.Alarm_Code & ALARM_OVER_TEMPERATURE) &&
  5233. timer[TIMER_IDX_RETRY_OT].isAlarm)
  5234. {
  5235. Charger.memory.EVSE_Config.data.item.MaxChargingCurrent = Charger.maxRatingCurrent;
  5236. timerDisable(TIMER_IDX_RETRY_OT);
  5237. osDelay(3000);
  5238. Charger.counter.OT.retry++;
  5239. Charger.Alarm_Code &= ~ALARM_OVER_TEMPERATURE;
  5240. DEBUG_INFO("Alarm OT recover.\r\n");
  5241. }
  5242. }*/
  5243. #endif //OTP_PROTECT
  5244. #ifdef FUNC_EMP_FAN_CTRL_USE_LOCKER_IO
  5245. if (Charger.temperature.SystemAmbientTemp > 35)
  5246. {
  5247. if (GET_EMP_FAN == OFF)
  5248. {
  5249. DEBUG_ERROR("<SET_FAN_ON>\r\n");
  5250. SET_EMP_FAN(ON);
  5251. }
  5252. }
  5253. else if (Charger.temperature.SystemAmbientTemp < 25)
  5254. {
  5255. if (GET_EMP_FAN == ON)
  5256. {
  5257. DEBUG_ERROR("<SET_FAN_OFF>\r\n");
  5258. SET_EMP_FAN(OFF);
  5259. }
  5260. }
  5261. #endif //FUNC_EMP_FAN_CTRL_USE_LOCKER_IO
  5262. //================================
  5263. // Current leakage alarm detect
  5264. // TODO: ADC channel & spec need to check for other detection method
  5265. //================================
  5266. #ifdef CCID_PROTECT
  5267. #ifdef FUNC_SWITCH_RELAY_WELDING_PROTECT
  5268. if (Charger.m_bDisableCCID == 0 && ((HAL_GetTick() - Charger.m_BootTick) / 1000) > 30)
  5269. {
  5270. #endif
  5271. if (Charger.CCID_Module_Type == CCID_MODULE_VAC)
  5272. {
  5273. //module test
  5274. if((HAL_GPIO_ReadPin(IN_Leak_Error_GPIO_Port, IN_Leak_Error_Pin) == GPIO_PIN_SET) && (!Charger.isTestLeakModule))
  5275. {
  5276. if(Charger.counter.LEAK.fail > 1000)
  5277. {
  5278. if(!(Charger.Alarm_Code & ALARM_LEAK_MODULE_FAIL))
  5279. {
  5280. Charger.counter.LEAK.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5281. Charger.Alarm_Code |= ALARM_LEAK_MODULE_FAIL;
  5282. DEBUG_ERROR("Alarm leakage module error occur.\r\n");
  5283. timerEnable(TIMER_IDX_RETRY_LEAK, ALARM_RETRY_INTERVAL_LEAK);
  5284. }
  5285. }
  5286. else
  5287. Charger.counter.LEAK.fail++;
  5288. }
  5289. else
  5290. {
  5291. Charger.counter.LEAK.fail = 0;
  5292. if((Charger.Alarm_Code & ALARM_LEAK_MODULE_FAIL) &&
  5293. (Charger.counter.LEAK.retry < ALARM_RETRY_COUNT) &&
  5294. (timer[TIMER_IDX_RETRY_LEAK].isAlarm == ON) &&
  5295. blinker[BLINKER_IDX_LED].blinkisFinish)
  5296. {
  5297. timerDisable(TIMER_IDX_RETRY_LEAK);
  5298. Charger.Alarm_Code &= ~ALARM_LEAK_MODULE_FAIL;
  5299. osDelay(1000);
  5300. #ifdef DEBUG
  5301. DEBUG_INFO("Alarm leakage module error recover.\r\n");
  5302. #endif
  5303. }
  5304. }
  5305. //DC leakage
  5306. if((HAL_GPIO_ReadPin(IN_Leak_DC_GPIO_Port, IN_Leak_DC_Pin) == GPIO_PIN_SET) && (!Charger.isTestLeakModule))
  5307. {
  5308. if(Charger.counter.LEAK_DC.fail > 10)
  5309. {
  5310. if(!(Charger.Alarm_Code & ALARM_CURRENT_LEAK_DC))
  5311. {
  5312. Charger.counter.LEAK_DC.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5313. Charger.Alarm_Code |= ALARM_CURRENT_LEAK_DC;
  5314. DEBUG_ERROR("Alarm DC leakage occur.\r\n");
  5315. timerEnable(TIMER_IDX_RETRY_LEAK_DC, ALARM_RETRY_INTERVAL_LEAK);
  5316. Charger.counter.LEAK_DC.retry++;
  5317. }
  5318. if (timer[TIMER_IDX_RETRY_LEAK_DC].isAlarm == ON || (Charger.counter.LEAK_DC.retry >ALARM_RETRY_COUNT))
  5319. {
  5320. timerDisable(TIMER_IDX_RETRY_LEAK_DC);
  5321. //Charger.counter.LEAK.isLatch = ON ;
  5322. if (Charger.counter.LEAK.isLatch == OFF)
  5323. {
  5324. Charger.counter.LEAK.isLatch = ON ;
  5325. setLedMotion(LED_ACTION_ALARM);
  5326. }
  5327. DEBUG_ERROR("Alarm leakage DC isLatch.\r\n");
  5328. }
  5329. }
  5330. else
  5331. Charger.counter.LEAK_DC.fail++;
  5332. }
  5333. else
  5334. {
  5335. Charger.counter.LEAK_DC.fail = 0;
  5336. if((Charger.Alarm_Code & ALARM_CURRENT_LEAK_DC) &&
  5337. (Charger.counter.LEAK_DC.retry <= ALARM_RETRY_COUNT) &&
  5338. (timer[TIMER_IDX_RETRY_LEAK_DC].isAlarm == ON) &&
  5339. blinker[BLINKER_IDX_LED].blinkisFinish)
  5340. {
  5341. timerDisable(TIMER_IDX_RETRY_LEAK_DC);
  5342. Charger.Alarm_Code &= ~ALARM_CURRENT_LEAK_DC;
  5343. DEBUG_INFO("Alarm DC leakage recover.\r\n");
  5344. }
  5345. }
  5346. //AC leakage
  5347. if((HAL_GPIO_ReadPin(IN_Leak_AC_GPIO_Port, IN_Leak_AC_Pin) == GPIO_PIN_SET) && (!Charger.isTestLeakModule))
  5348. {
  5349. if(Charger.counter.LEAK_AC.fail > 10)
  5350. {
  5351. if(!(Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC))
  5352. {
  5353. Charger.counter.LEAK_AC.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5354. Charger.Alarm_Code |= ALARM_CURRENT_LEAK_AC;
  5355. DEBUG_ERROR("Alarm AC leakage occur.\r\n");
  5356. timerEnable(TIMER_IDX_RETRY_LEAK_AC, ALARM_RETRY_INTERVAL_LEAK);
  5357. Charger.counter.LEAK_AC.retry++;
  5358. }
  5359. if (timer[TIMER_IDX_RETRY_LEAK_AC].isAlarm == ON || (Charger.counter.LEAK_AC.retry > ALARM_RETRY_COUNT))
  5360. {
  5361. timerDisable(TIMER_IDX_RETRY_LEAK_AC);
  5362. //Charger.counter.LEAK.isLatch = ON ;
  5363. if (Charger.counter.LEAK.isLatch == OFF)
  5364. {
  5365. Charger.counter.LEAK.isLatch = ON ;
  5366. setLedMotion(LED_ACTION_ALARM);
  5367. }
  5368. DEBUG_ERROR("Alarm leakage AC isLatch.\r\n");
  5369. }
  5370. }
  5371. else
  5372. Charger.counter.LEAK_AC.fail++;
  5373. }
  5374. else
  5375. {
  5376. Charger.counter.LEAK_AC.fail = 0;
  5377. if((Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC) &&
  5378. (Charger.counter.LEAK_AC.retry <= ALARM_RETRY_COUNT) &&
  5379. (timer[TIMER_IDX_RETRY_LEAK_AC].isAlarm == ON) &&
  5380. blinker[BLINKER_IDX_LED].blinkisFinish )
  5381. {
  5382. timerDisable(TIMER_IDX_RETRY_LEAK_AC);
  5383. Charger.Alarm_Code &= ~ALARM_CURRENT_LEAK_AC;
  5384. DEBUG_INFO("Alarm AC leakage recover.\r\n");
  5385. }
  5386. }
  5387. }
  5388. else // Charger.CCID_Module_Type is CCID_MODULE_CORMEX
  5389. {
  5390. if((Charger.Leak_Current >= Charger.alarm_spec.Current_LEAK_AC) && (!Charger.isTestLeakModule)) // adc correction data to check
  5391. {
  5392. if(Charger.counter.LEAK.fail >= 5)
  5393. {
  5394. if(!(Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC))
  5395. {
  5396. #ifdef FUNC_MODIFY_LEAK_AC_DISPLAY
  5397. XP("#Leak_Current (%d >= %d)\r\n", Charger.Leak_Current, Charger.alarm_spec.Current_LEAK_AC);
  5398. #else
  5399. XP("************************************************************\r\n");
  5400. XP("adc_value.ADC2_IN6_GF.value = %d\r\n", adc_value.ADC2_IN6_GF.value);
  5401. XP("Charger.Leak_Current(mA) = %d\r\n", Charger.Leak_Current);
  5402. XP("Charger.alarm_spec.Current_LEAK_AC(mA) = %d\r\n", Charger.alarm_spec.Current_LEAK_AC);
  5403. XP("Charger.isTestLeakModule = %s\r\n", Charger.isTestLeakModule ? "ON" : "OFF");
  5404. XP("************************************************************\r\n");
  5405. #endif
  5406. Charger.counter.LEAK.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5407. Charger.Alarm_Code |= ALARM_CURRENT_LEAK_AC; //Hao##### CSU Charge error here
  5408. DEBUG_ERROR("Alarm leakage occur.\r\n");
  5409. timerEnable(TIMER_IDX_RETRY_LEAK_AC, ALARM_RETRY_INTERVAL_LEAK);
  5410. Charger.counter.LEAK.retry ++ ;
  5411. }
  5412. }
  5413. else
  5414. {
  5415. Charger.counter.LEAK.fail++;
  5416. }
  5417. }
  5418. else if (Charger.Leak_Current < (Charger.alarm_spec.Current_LEAK_AC - ALARM_SPEC_CURRENT_LEAK_HYSTERESIS)) // adc correction data to check
  5419. {
  5420. Charger.counter.LEAK.fail = 0;
  5421. if (Charger.counter.LEAK.retry > ALARM_LEAK_RETRY_COUNT)
  5422. {
  5423. if (Charger.counter.LEAK.isLatch == OFF)
  5424. {
  5425. Charger.counter.LEAK.isLatch = ON ;
  5426. setLedMotion(LED_ACTION_ALARM);
  5427. }
  5428. }
  5429. else
  5430. {
  5431. if(((Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC)||(Charger.Alarm_Code & ALARM_CURRENT_LEAK_DC)) &&
  5432. (Charger.counter.LEAK.retry <= ALARM_LEAK_RETRY_COUNT) &&
  5433. (timer[TIMER_IDX_RETRY_LEAK_AC].isAlarm == ON) &&
  5434. blinker[BLINKER_IDX_LED].blinkisFinish )
  5435. {
  5436. timerDisable(TIMER_IDX_RETRY_LEAK_AC);
  5437. osDelay(1000);
  5438. Charger.Alarm_Code &= ~ALARM_CURRENT_LEAK_AC;
  5439. DEBUG_INFO("Alarm leakage recover.\r\n");
  5440. }
  5441. }
  5442. }
  5443. }
  5444. #ifdef FUNC_SWITCH_RELAY_WELDING_PROTECT
  5445. }
  5446. #endif
  5447. #endif //CCID_PROTECT
  5448. //================================
  5449. // Ground fail alarm detect
  5450. // TODO: Spec need to check
  5451. //================================
  5452. #ifdef GROUND_FAULT_PROTECT
  5453. if(Charger.GroundingDetect==ON){
  5454. #ifdef FUNC_RELAXSPEC_GROUND_FAULT_PROTECT
  5455. static double GF_SPEC_OCCUR = 0;
  5456. static double GF_SPEC_RECOVER = 0;
  5457. if (GF_SPEC_OCCUR == 0 || GF_SPEC_RECOVER == 0)
  5458. {
  5459. GF_SPEC_OCCUR = 3.0 / 220.0; //GF_SPEC_OCCUR = 0.013636(3.0)
  5460. GF_SPEC_RECOVER = 2.0 / 220.0; //GF_SPEC_RECOVER = 0.009091(2.0)
  5461. XP("GF_SPEC_OCCUR = %lf, GF_SPEC_RECOVER = %lf\r\n", GF_SPEC_OCCUR, GF_SPEC_RECOVER);
  5462. }
  5463. #endif
  5464. if (timer[TIMER_IDX_PE_DETECT].isAlarm == ON) // after 30 sec check (GMI adc_value > spec) and (GMI adc_value <= 1 )
  5465. {
  5466. #ifdef FUNC_RELAXSPEC_GROUND_FAULT_PROTECT
  5467. if(((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) > (Charger.Voltage[0]*GF_SPEC_OCCUR)) || (adc_value.ADC3_IN4_GMI_VL1.value <= 1))
  5468. #else
  5469. if(((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) > (Charger.Voltage[0]*0.011)) || (adc_value.ADC3_IN4_GMI_VL1.value <= 1))
  5470. #endif
  5471. {
  5472. if(Charger.counter.GF.fail > 4000)
  5473. {
  5474. if(!(Charger.Alarm_Code & ALARM_GROUND_FAIL))
  5475. {
  5476. Charger.counter.GF.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5477. Charger.Alarm_Code |= ALARM_GROUND_FAIL;
  5478. DEBUG_ERROR("Alarm GF occur.\r\n");
  5479. }
  5480. }
  5481. else
  5482. Charger.counter.GF.fail++;
  5483. }
  5484. #ifdef FUNC_RELAXSPEC_GROUND_FAULT_PROTECT
  5485. else if((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) < (Charger.Voltage[0]*GF_SPEC_RECOVER))
  5486. #else
  5487. else if((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) < (Charger.Voltage[0]*0.00566))
  5488. #endif
  5489. {
  5490. Charger.counter.GF.fail = 0;
  5491. if((Charger.Alarm_Code & ALARM_GROUND_FAIL) && blinker[BLINKER_IDX_LED].blinkisFinish)
  5492. {
  5493. //osDelay(3000);
  5494. Charger.counter.GF.retry++;
  5495. Charger.Alarm_Code &= ~ALARM_GROUND_FAIL;
  5496. DEBUG_INFO("Alarm GF recover.\r\n");
  5497. }
  5498. }
  5499. }
  5500. else // before 30 sec check (GMI adc_value > spec)
  5501. {
  5502. #ifdef FUNC_RELAXSPEC_GROUND_FAULT_PROTECT
  5503. if((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) > (Charger.Voltage[0]*GF_SPEC_OCCUR))
  5504. #else
  5505. if((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) > (Charger.Voltage[0]*0.011))
  5506. #endif
  5507. {
  5508. if(Charger.counter.GF.fail > 4000)
  5509. {
  5510. if(!(Charger.Alarm_Code & ALARM_GROUND_FAIL))
  5511. {
  5512. Charger.counter.GF.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5513. Charger.Alarm_Code |= ALARM_GROUND_FAIL;
  5514. DEBUG_ERROR("Alarm GF occur.\r\n");
  5515. }
  5516. }
  5517. else
  5518. Charger.counter.GF.fail++;
  5519. }
  5520. #ifdef FUNC_RELAXSPEC_GROUND_FAULT_PROTECT
  5521. else if((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) < (Charger.Voltage[0]*GF_SPEC_RECOVER))
  5522. #else
  5523. else if((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) < (Charger.Voltage[0]*0.00566))
  5524. #endif
  5525. {
  5526. Charger.counter.GF.fail = 0;
  5527. if((Charger.Alarm_Code & ALARM_GROUND_FAIL) && blinker[BLINKER_IDX_LED].blinkisFinish)
  5528. {
  5529. //osDelay(3000);
  5530. Charger.counter.GF.retry++;
  5531. Charger.Alarm_Code &= ~ALARM_GROUND_FAIL;
  5532. DEBUG_INFO("Alarm GF recover.\r\n");
  5533. }
  5534. }
  5535. }
  5536. }
  5537. #endif //GROUND_FAULT_PROTECT
  5538. //================================
  5539. // Relay status error detect
  5540. // TODO: ADC channel & spec need to check
  5541. //================================
  5542. #ifdef RELAY_WELDING_PROTECT
  5543. #ifdef FUNC_SWITCH_RELAY_WELDING_PROTECT
  5544. if (Charger.m_bDisableRelayWelding == 0 && ((HAL_GetTick() - Charger.m_BootTick) / 1000) > 30)
  5545. {
  5546. #endif
  5547. //GROUNGING_SYSTEM_LL
  5548. if (Charger.GroundingSystem == GROUNGING_SYSTEM_LL)
  5549. {
  5550. if((Charger.Relay_Action == GPIO_RELAY_ACTION_ON) && (Charger.Relay_isOperationCompleted == 0x11 ) && ((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) < (Charger.Voltage[0]*0.011)) )
  5551. {
  5552. if(adc_value.ADC2_IN5_Welding.value < ((Charger.Voltage[0]*0.0048)+3)) // for UL dloule L syste a=0.0048 b=0.03V
  5553. {
  5554. if(Charger.counter.RELAY_Drive_Fualt.fail > 1000)
  5555. {
  5556. if(!(Charger.Alarm_Code & ALARM_RELAY_DRIVE_FUALT))
  5557. {
  5558. Charger.counter.RELAY.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5559. Charger.Alarm_Code |= ALARM_RELAY_DRIVE_FUALT;
  5560. DEBUG_ERROR("Alarm Welding Voltage = %d.\r\n",adc_value.ADC2_IN5_Welding.value);
  5561. DEBUG_ERROR("Alarm relay ON status fault occur.\r\n");
  5562. }
  5563. }
  5564. else
  5565. Charger.counter.RELAY_Drive_Fualt.fail++;
  5566. }
  5567. else
  5568. {
  5569. Charger.counter.RELAY_Drive_Fualt.fail = 0 ;
  5570. }
  5571. }
  5572. else if ((Charger.Relay_Action == GPIO_RELAY_ACTION_OFF) && (Charger.Relay_isOperationCompleted == 0x00) && ((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) < (Charger.Voltage[0]*0.011)) )
  5573. {
  5574. //if(adc_value.ADC2_IN5_Welding.value > 24) // 0.24 V
  5575. if(adc_value.ADC2_IN5_Welding.value > ALARM_SPEC_RELAY_LL)
  5576. {
  5577. if(Charger.counter.RELAY.fail > 1000)
  5578. {
  5579. if(!(Charger.Alarm_Code & ALARM_RELAY_STATUS))
  5580. {
  5581. Charger.counter.RELAY.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5582. Charger.Alarm_Code |= ALARM_RELAY_STATUS;
  5583. DEBUG_ERROR("Alarm Welding Voltage = %d.\r\n",adc_value.ADC2_IN5_Welding.value);
  5584. DEBUG_ERROR("Alarm relay OFF status fault occur.\r\n");
  5585. }
  5586. }
  5587. else
  5588. Charger.counter.RELAY.fail++;
  5589. }
  5590. else
  5591. {
  5592. Charger.counter.RELAY.fail = 0 ;
  5593. }
  5594. }
  5595. }
  5596. else // GROUNGING_SYSTEM_LN
  5597. {
  5598. if((Charger.Relay_Action == GPIO_RELAY_ACTION_ON) && (Charger.Relay_isOperationCompleted == 0x11) && ((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) < (Charger.Voltage[0]*0.011)) )
  5599. {
  5600. if(adc_value.ADC2_IN5_Welding.value < ALARM_SPEC_RELAY_LN_DRIVE_FAULT ) // old spec use
  5601. {
  5602. if(Charger.counter.RELAY.fail > 1000)
  5603. {
  5604. if(!(Charger.Alarm_Code & ALARM_RELAY_DRIVE_FUALT))
  5605. {
  5606. Charger.counter.RELAY.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5607. Charger.Alarm_Code |= ALARM_RELAY_DRIVE_FUALT;
  5608. DEBUG_ERROR("Alarm Welding Voltage = %d.\r\n",adc_value.ADC2_IN5_Welding.value);
  5609. DEBUG_ERROR("Alarm relay ON status fault occur.\r\n");
  5610. }
  5611. }
  5612. else
  5613. Charger.counter.RELAY.fail++;
  5614. }
  5615. else
  5616. {
  5617. Charger.counter.RELAY.fail = 0 ;
  5618. }
  5619. }
  5620. else if ((Charger.Relay_Action == GPIO_RELAY_ACTION_OFF) && (Charger.Relay_isOperationCompleted == 0x00) && ((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) < (Charger.Voltage[0]*0.011)) )
  5621. {
  5622. if(adc_value.ADC2_IN5_Welding.value > ALARM_SPEC_RELAY_LN_WELDING)
  5623. {
  5624. if(Charger.counter.RELAY.fail > 1000)
  5625. {
  5626. if(!(Charger.Alarm_Code & ALARM_RELAY_STATUS))
  5627. {
  5628. Charger.counter.RELAY.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5629. Charger.Alarm_Code |= ALARM_RELAY_STATUS;
  5630. DEBUG_ERROR("Alarm Welding Voltage = %d.\r\n",adc_value.ADC2_IN5_Welding.value);
  5631. DEBUG_ERROR("Alarm relay OFF status fault occur.\r\n");
  5632. }
  5633. }
  5634. else
  5635. Charger.counter.RELAY.fail++;
  5636. }
  5637. else
  5638. {
  5639. Charger.counter.RELAY.fail = 0 ;
  5640. }
  5641. }
  5642. }
  5643. #ifdef FUNC_SWITCH_RELAY_WELDING_PROTECT
  5644. }
  5645. #endif
  5646. #endif //RELAY_WELDING_PROTECT
  5647. //================================
  5648. // Emergency stop alarm detect
  5649. //================================
  5650. #ifdef EMC_BUTTON_PROTECT
  5651. if(HAL_GPIO_ReadPin(IN_Emergency_GPIO_Port, IN_Emergency_Pin) == GPIO_PIN_RESET)
  5652. {
  5653. if(Charger.counter.EMO.fail > 100)
  5654. {
  5655. if(!(Charger.Alarm_Code & ALARM_EMERGENCY_STOP))
  5656. {
  5657. Charger.counter.EMO.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5658. Charger.Alarm_Code |= ALARM_EMERGENCY_STOP;
  5659. DEBUG_ERROR("Alarm EMO occur.\r\n");
  5660. }
  5661. }
  5662. else
  5663. Charger.counter.EMO.fail++;
  5664. }
  5665. else
  5666. {
  5667. Charger.counter.EMO.fail = 0;
  5668. if((Charger.Alarm_Code & ALARM_EMERGENCY_STOP) && blinker[BLINKER_IDX_LED].blinkisFinish)
  5669. {
  5670. Charger.counter.EMO.retry++;
  5671. Charger.Alarm_Code &= ~ALARM_EMERGENCY_STOP;
  5672. DEBUG_INFO("Alarm EMO recover.\r\n");
  5673. }
  5674. }
  5675. #endif //EMC_BUTTON_PROTECT
  5676. //================================
  5677. // Handshaking timeout detect
  5678. //================================
  5679. #ifdef HANDSHAKE_PROTECT
  5680. if(timer[TIMER_IDX_CP].isAlarm == ON)
  5681. {
  5682. if(!(Charger.Alarm_Code & ALARM_HANDSHAKE_TIMEOUT))
  5683. {
  5684. Charger.Alarm_Code |= ALARM_HANDSHAKE_TIMEOUT;
  5685. timerDisable(TIMER_IDX_CP);
  5686. Charger.rfid.op_bits.reqStart = OFF;
  5687. Charger.istoHandShakeMode = OFF ;
  5688. DEBUG_ERROR("Alarm handshake overtime occur.\r\n");
  5689. }
  5690. }
  5691. else
  5692. {
  5693. if((Charger.Alarm_Code & ALARM_HANDSHAKE_TIMEOUT))
  5694. {
  5695. osDelay(10000);
  5696. Charger.Alarm_Code &= ~ALARM_HANDSHAKE_TIMEOUT;
  5697. DEBUG_INFO("Alarm handshake overtime recover.\r\n");
  5698. }
  5699. }
  5700. #endif //HANDSHAKE_PROTECT
  5701. //================================
  5702. // Rotary Switch Maxium Current Error detect
  5703. //================================
  5704. #ifdef ROTATE_SWITCH_CHECK
  5705. if (Charger.isRotarySwitchError)
  5706. {
  5707. if(!(Charger.Alarm_Code & ALARM_ROTATORY_SWITCH_FAULT))
  5708. {
  5709. Charger.Alarm_Code |= ALARM_ROTATORY_SWITCH_FAULT;
  5710. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  5711. blinkerTimeSet(BLINKER_IDX_LED, 200, 200, 0, 1);
  5712. }
  5713. DEBUG_INFO("Rotary Switch Maxium Current Error occur.\r\n");
  5714. }
  5715. }
  5716. #endif //ROTATE_SWITCH_CHECK
  5717. //================================
  5718. // Recover alarm info in state A
  5719. //================================
  5720. if(Charger.CP_State == SYSTEM_STATE_A)
  5721. {
  5722. //SCP Recover
  5723. if(Charger.Alarm_Code & ALARM_CIRCUIT_SHORT)
  5724. {
  5725. Charger.Alarm_Code &= ~ALARM_CIRCUIT_SHORT;
  5726. DEBUG_INFO("Alarm SCP recover.\r\n");
  5727. }
  5728. //OCP Recover
  5729. if(Charger.Alarm_Code & ALARM_OVER_CURRENT)
  5730. {
  5731. Charger.Alarm_Code &= ~ALARM_OVER_CURRENT;
  5732. DEBUG_INFO("Alarm OC recover.\r\n");
  5733. }
  5734. Charger.counter.OC.fail = 0 ;
  5735. timerDisable(TIMER_IDX_RETRY_OC);
  5736. Charger.counter.OC.retry = 0 ; // clear retry times
  5737. Charger.counter.OC.isLatch = OFF ;
  5738. //CCID Recover
  5739. if (Charger.CCID_Module_Type == CCID_MODULE_VAC)
  5740. {
  5741. if ((HAL_GPIO_ReadPin(IN_Leak_AC_GPIO_Port, IN_Leak_AC_Pin) == GPIO_PIN_RESET) && (Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC))
  5742. {
  5743. Charger.Alarm_Code &= ~ALARM_CURRENT_LEAK_AC;
  5744. DEBUG_INFO("Alarm leakage AC recover for VAC.\r\n");
  5745. }
  5746. if ((HAL_GPIO_ReadPin(IN_Leak_DC_GPIO_Port, IN_Leak_DC_Pin) == GPIO_PIN_RESET) && (Charger.Alarm_Code & ALARM_CURRENT_LEAK_DC))
  5747. {
  5748. Charger.Alarm_Code &= ~ALARM_CURRENT_LEAK_DC;
  5749. DEBUG_INFO("Alarm leakage DC recover for VAC.\r\n");
  5750. }
  5751. }
  5752. else //Charger.CCID_Module_Type is CCID_MODULE_CORMEX
  5753. {
  5754. #ifdef MODIFY_CCID_MODULE_CORMEX_RECOVER_AC_DC
  5755. if (Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC)
  5756. {
  5757. Charger.Alarm_Code &= ~ALARM_CURRENT_LEAK_AC;
  5758. DEBUG_INFO("Alarm leakage AC recover.\r\n");
  5759. }
  5760. if (Charger.Alarm_Code & ALARM_CURRENT_LEAK_DC)
  5761. {
  5762. Charger.Alarm_Code &= ~ALARM_CURRENT_LEAK_DC;
  5763. DEBUG_INFO("Alarm leakage DC recover.\r\n");
  5764. }
  5765. #else //MODIFY_CCID_MODULE_CORMEX_RECOVER_AC_DC
  5766. if((Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC)||(Charger.Alarm_Code & ALARM_CURRENT_LEAK_DC))
  5767. {
  5768. Charger.Alarm_Code &= ~ALARM_CURRENT_LEAK_AC;
  5769. DEBUG_INFO("Alarm leakage recover.\r\n");
  5770. }
  5771. #endif //MODIFY_CCID_MODULE_CORMEX_RECOVER_AC_DC
  5772. }
  5773. Charger.counter.LEAK.fail = 0;
  5774. timerDisable(TIMER_IDX_RETRY_LEAK) ;
  5775. timerDisable(TIMER_IDX_RETRY_LEAK_AC) ;
  5776. timerDisable(TIMER_IDX_RETRY_LEAK_DC) ;
  5777. Charger.counter.LEAK.retry = 0 ; // clear latch times
  5778. Charger.counter.LEAK_DC.retry = 0 ; // clear latch times
  5779. Charger.counter.LEAK_AC.retry = 0 ; // clear latch times
  5780. Charger.counter.LEAK.isLatch = OFF ;
  5781. //LEAK_MODULE selfrest Recover
  5782. if (Charger.CCID_Module_Type == CCID_MODULE_VAC)
  5783. {
  5784. if(Charger.Alarm_Code & ALARM_LEAK_MODULE_FAIL & (HAL_GPIO_ReadPin(IN_Leak_Error_GPIO_Port, IN_Leak_Error_Pin) == GPIO_PIN_RESET)) // leak module is VAC
  5785. {
  5786. if ((Charger.counter.LEAK_MODULE.retry < 2) && (!Charger.counter.LEAK_MODULE.isLatch))
  5787. {
  5788. Charger.Alarm_Code &= ~ALARM_LEAK_MODULE_FAIL;
  5789. DEBUG_INFO("Alarm leakage module recover.\r\n");
  5790. }
  5791. }
  5792. }
  5793. else //Charger.CCID_Module_Type is CCID_MODULE_CORMEX
  5794. {
  5795. if(Charger.Alarm_Code & ALARM_LEAK_MODULE_FAIL) // leak module is CORMEX
  5796. {
  5797. if ((Charger.counter.LEAK_MODULE.retry < 2) && (!Charger.counter.LEAK_MODULE.isLatch))
  5798. {
  5799. Charger.Alarm_Code &= ~ALARM_LEAK_MODULE_FAIL;
  5800. DEBUG_INFO("Alarm leakage module recover.\r\n");
  5801. }
  5802. }
  5803. }
  5804. }
  5805. }
  5806. else if (Charger.Mode == MODE_DEBUG)
  5807. {
  5808. //================================
  5809. // Relay status error detect
  5810. // TODO: ADC channel & spec need to check
  5811. //================================
  5812. #ifdef RELAY_WELDING_PROTECT
  5813. //GROUNGING_SYSTEM_LL
  5814. if (Charger.GroundingSystem == GROUNGING_SYSTEM_LL)
  5815. {
  5816. if((Charger.Relay_Action == GPIO_RELAY_ACTION_ON) && (Charger.Relay_isOperationCompleted == 0x11 ) && ((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) < (Charger.Voltage[0]*0.011)) )
  5817. {
  5818. if(adc_value.ADC2_IN5_Welding.value < ((Charger.Voltage[0]*0.0048)+3)) // for UL dloule L syste a=0.0048 b=0.03V
  5819. {
  5820. if(Charger.counter.RELAY_Drive_Fualt.fail > 1000)
  5821. {
  5822. if(!(Charger.Alarm_Code & ALARM_RELAY_DRIVE_FUALT))
  5823. {
  5824. Charger.counter.RELAY.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5825. Charger.Alarm_Code |= ALARM_RELAY_DRIVE_FUALT;
  5826. DEBUG_ERROR("Alarm Welding Voltage = %d.\r\n",adc_value.ADC2_IN5_Welding.value);
  5827. DEBUG_ERROR("Alarm relay ON status fault occur.\r\n");
  5828. }
  5829. }
  5830. else
  5831. Charger.counter.RELAY_Drive_Fualt.fail++;
  5832. }
  5833. else
  5834. {
  5835. Charger.counter.RELAY_Drive_Fualt.fail = 0 ;
  5836. }
  5837. }
  5838. else if ((Charger.Relay_Action == GPIO_RELAY_ACTION_OFF) && (Charger.Relay_isOperationCompleted == 0x00) && ((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) < (Charger.Voltage[0]*0.011)) )
  5839. {
  5840. //if(adc_value.ADC2_IN5_Welding.value > 24) // 0.24 V
  5841. if(adc_value.ADC2_IN5_Welding.value > ALARM_SPEC_RELAY_LL)
  5842. {
  5843. if(Charger.counter.RELAY.fail > 1000)
  5844. {
  5845. if(!(Charger.Alarm_Code & ALARM_RELAY_STATUS))
  5846. {
  5847. Charger.counter.RELAY.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5848. Charger.Alarm_Code |= ALARM_RELAY_STATUS;
  5849. DEBUG_ERROR("Alarm Welding Voltage = %d.\r\n",adc_value.ADC2_IN5_Welding.value);
  5850. DEBUG_ERROR("Alarm relay OFF status fault occur.\r\n");
  5851. }
  5852. }
  5853. else
  5854. Charger.counter.RELAY.fail++;
  5855. }
  5856. else
  5857. {
  5858. Charger.counter.RELAY.fail = 0 ;
  5859. }
  5860. }
  5861. }
  5862. else // GROUNGING_SYSTEM_LN
  5863. {
  5864. if((Charger.Relay_Action == GPIO_RELAY_ACTION_ON) && (Charger.Relay_isOperationCompleted == 0x11) && ((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) < (Charger.Voltage[0]*0.011)) )
  5865. {
  5866. if(adc_value.ADC2_IN5_Welding.value < ALARM_SPEC_RELAY_LN_DRIVE_FAULT ) // old spec use
  5867. {
  5868. if(Charger.counter.RELAY.fail > 1000)
  5869. {
  5870. if(!(Charger.Alarm_Code & ALARM_RELAY_DRIVE_FUALT))
  5871. {
  5872. Charger.counter.RELAY.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5873. Charger.Alarm_Code |= ALARM_RELAY_DRIVE_FUALT;
  5874. DEBUG_ERROR("Alarm Welding Voltage = %d.\r\n",adc_value.ADC2_IN5_Welding.value);
  5875. DEBUG_ERROR("Alarm relay ON status fault occur.\r\n");
  5876. }
  5877. }
  5878. else
  5879. Charger.counter.RELAY.fail++;
  5880. }
  5881. else
  5882. {
  5883. Charger.counter.RELAY.fail = 0 ;
  5884. }
  5885. }
  5886. else if ((Charger.Relay_Action == GPIO_RELAY_ACTION_OFF) && (Charger.Relay_isOperationCompleted == 0x00) && ((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) < (Charger.Voltage[0]*0.011)) )
  5887. {
  5888. if(adc_value.ADC2_IN5_Welding.value > ALARM_SPEC_RELAY_LN_WELDING)
  5889. {
  5890. if(Charger.counter.RELAY.fail > 1000)
  5891. {
  5892. if(!(Charger.Alarm_Code & ALARM_RELAY_STATUS))
  5893. {
  5894. Charger.counter.RELAY.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5895. Charger.Alarm_Code |= ALARM_RELAY_STATUS;
  5896. DEBUG_ERROR("Alarm Welding Voltage = %d.\r\n",adc_value.ADC2_IN5_Welding.value);
  5897. DEBUG_ERROR("Alarm relay OFF status fault occur.\r\n");
  5898. }
  5899. }
  5900. else
  5901. Charger.counter.RELAY.fail++;
  5902. }
  5903. else
  5904. {
  5905. Charger.counter.RELAY.fail = 0 ;
  5906. }
  5907. }
  5908. }
  5909. #endif //RELAY_WELDING_PROTECT
  5910. }
  5911. osDelay(1);
  5912. }
  5913. /* USER CODE END StartAlarmTask */
  5914. }
  5915. /* USER CODE BEGIN Header_StartBleTask */
  5916. /**
  5917. * @brief Function implementing the bleTask thread.
  5918. * @param argument: Not used
  5919. * @retval None
  5920. */
  5921. /* USER CODE END Header_StartBleTask */
  5922. void StartBleTask(void const * argument)
  5923. {
  5924. /* USER CODE BEGIN StartBleTask */
  5925. char tmpBuf[128];
  5926. uint8_t endFlag[4]={0x55,0xaa,0x55,0xaa};
  5927. __IO uint32_t flash;
  5928. uint32_t checksum;
  5929. uint32_t rndNumber;
  5930. //=====================================
  5931. // Module reset
  5932. //=====================================
  5933. HAL_GPIO_WritePin(OUT_BLE_RESET_GPIO_Port, OUT_BLE_RESET_Pin, GPIO_PIN_RESET);
  5934. osDelay(60);
  5935. HAL_GPIO_WritePin(OUT_BLE_RESET_GPIO_Port, OUT_BLE_RESET_Pin, GPIO_PIN_SET);
  5936. HAL_GPIO_WritePin(OUT_BLE_DSR_GPIO_Port, OUT_BLE_DSR_Pin, GPIO_PIN_SET);
  5937. Charger.ble.initSeq = 0;
  5938. Charger.ble.isDataMode = OFF;
  5939. UART_BLE_Init_OK = OFF;
  5940. osDelay(1000);
  5941. /* Infinite loop */
  5942. for(;;)
  5943. {
  5944. //=====================================
  5945. // BLE module initialization process
  5946. //=====================================
  5947. switch(Charger.ble.initSeq)
  5948. {
  5949. case 0x00:
  5950. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_RESTORE], strlen(BLE_CMD[BLE_CMD_SET_RESTORE]), 0xffff);
  5951. osDelay(BLE_INIT_DELAY);
  5952. break;
  5953. case 0x01:
  5954. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_ROLE], strlen(BLE_CMD[BLE_CMD_SET_ROLE]), 0xffff);
  5955. osDelay(BLE_INIT_DELAY);
  5956. break;
  5957. case 0x02:
  5958. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_DTR], strlen(BLE_CMD[BLE_CMD_SET_DTR]), 0xffff);
  5959. osDelay(BLE_INIT_DELAY);
  5960. break;
  5961. case 0x03:
  5962. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_DSR], strlen(BLE_CMD[BLE_CMD_SET_DSR]), 0xffff);
  5963. osDelay(BLE_INIT_DELAY);
  5964. break;
  5965. case 0x04:
  5966. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_PAIRING_MODE], strlen(BLE_CMD[BLE_CMD_SET_PAIRING_MODE]), 0xffff);
  5967. osDelay(BLE_INIT_DELAY);
  5968. break;
  5969. case 0x05:
  5970. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_SECURITY_MODE], strlen(BLE_CMD[BLE_CMD_SET_SECURITY_MODE]), 0xffff);
  5971. osDelay(BLE_INIT_DELAY);
  5972. break;
  5973. case 0x06:
  5974. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_SECURITY_TYPE], strlen(BLE_CMD[BLE_CMD_SET_SECURITY_TYPE]), 0xffff);
  5975. osDelay(BLE_INIT_DELAY);
  5976. break;
  5977. case 0x07:
  5978. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_DISCOVERABILITY], strlen(BLE_CMD[BLE_CMD_SET_DISCOVERABILITY]), 0xffff);
  5979. osDelay(BLE_INIT_DELAY);
  5980. break;
  5981. case 0x08:
  5982. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_CONNECTABILITY], strlen(BLE_CMD[BLE_CMD_SET_CONNECTABILITY]), 0xffff);
  5983. osDelay(BLE_INIT_DELAY);
  5984. break;
  5985. case 0x09:
  5986. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_GET_ADDR], strlen(BLE_CMD[BLE_CMD_GET_ADDR]), 0xffff);
  5987. osDelay(BLE_INIT_DELAY);
  5988. break;
  5989. case 0x0a:
  5990. sprintf(tmpBuf, "%s\"BYTON_EVSE_%s\"\r\n", BLE_CMD[BLE_CMD_SET_NAME], Charger.ble.peripheral_mac);
  5991. DEBUG_INFO("BLE module set name:BYTON_EVSE_%s\r\n", Charger.ble.peripheral_mac);
  5992. HAL_UART_Transmit(&BLE_USART, (uint8_t *)tmpBuf, strlen(tmpBuf), 0xffff);
  5993. osDelay(BLE_INIT_DELAY);
  5994. break;
  5995. case 0x0b:
  5996. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_MAX_POWER], strlen(BLE_CMD[BLE_CMD_SET_MAX_POWER]), 0xffff);
  5997. osDelay(BLE_INIT_DELAY);
  5998. break;
  5999. case 0x0c:
  6000. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_MIN_INTERVAL], strlen(BLE_CMD[BLE_CMD_SET_MIN_INTERVAL]), 0xffff);
  6001. osDelay(BLE_INIT_DELAY);
  6002. break;
  6003. case 0x0d:
  6004. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_MAX_INTERVAL], strlen(BLE_CMD[BLE_CMD_SET_MAX_INTERVAL]), 0xffff);
  6005. osDelay(BLE_INIT_DELAY);
  6006. break;
  6007. case 0x0e:
  6008. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_TX_PHY], strlen(BLE_CMD[BLE_CMD_SET_TX_PHY]), 0xffff);
  6009. osDelay(BLE_INIT_DELAY);
  6010. break;
  6011. case 0x0f:
  6012. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_RX_PHY], strlen(BLE_CMD[BLE_CMD_SET_RX_PHY]), 0xffff);
  6013. osDelay(BLE_INIT_DELAY);
  6014. break;
  6015. case 0x10:
  6016. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_DATA_MODE], strlen(BLE_CMD[BLE_CMD_SET_DATA_MODE]), 0xffff);
  6017. DEBUG_INFO("BLE module switch to data mode.\r\n");
  6018. osDelay(BLE_INIT_DELAY);
  6019. default:
  6020. UART_BLE_Init_OK = ON;
  6021. break;
  6022. }
  6023. //=====================================
  6024. // BLE central device connect status
  6025. //=====================================
  6026. if((HAL_GPIO_ReadPin(IN_BLE_DTR_GPIO_Port, IN_BLE_DTR_Pin) == GPIO_PIN_SET) && (UART_BLE_Init_OK == ON))
  6027. {
  6028. // Device disconnect
  6029. timerRefresh(TIMER_IDX_BLE);
  6030. if(Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT || Charger.ble.loginRole == BLE_LOGIN_ROLE_USER)
  6031. {
  6032. DEBUG_INFO("BLE central device disconnect.\r\n.");
  6033. Charger.ble.loginRole = BLE_LOGIN_ROLE_UNKOWN;
  6034. }
  6035. }
  6036. else if((HAL_GPIO_ReadPin(IN_BLE_DTR_GPIO_Port, IN_BLE_DTR_Pin) == GPIO_PIN_RESET) && (UART_BLE_Init_OK == ON))
  6037. {
  6038. // Device connect in
  6039. if(Charger.ble.loginRole == BLE_LOGIN_ROLE_UNKOWN)
  6040. {
  6041. DEBUG_INFO("BLE central device connect.....wait sign in.\r\n.");
  6042. osDelay(1000);
  6043. }
  6044. }
  6045. //=====================================
  6046. // Communication timeout process
  6047. //=====================================
  6048. if(timer[TIMER_IDX_BLE].isAlarm)
  6049. {
  6050. Charger.ble.loginRole = BLE_LOGIN_ROLE_UNKOWN;
  6051. DEBUG_INFO("BLE no communication over time, disconnect central device.\r\n");
  6052. timerRefresh(TIMER_IDX_BLE);
  6053. HAL_GPIO_WritePin(OUT_BLE_DSR_GPIO_Port, OUT_BLE_DSR_Pin, GPIO_PIN_RESET);
  6054. osDelay(100);
  6055. HAL_GPIO_WritePin(OUT_BLE_DSR_GPIO_Port, OUT_BLE_DSR_Pin, GPIO_PIN_SET);
  6056. }
  6057. //=====================================
  6058. // BLE uart recieve data process
  6059. //=====================================
  6060. if(UART_BLE_recv_end_flag == ON)
  6061. {
  6062. if((HAL_GPIO_ReadPin(IN_BLE_DTR_GPIO_Port, IN_BLE_DTR_Pin) == GPIO_PIN_SET) || !Charger.ble.isDataMode)
  6063. {
  6064. // AT command rx process
  6065. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_RESTORE])!=NULL)
  6066. {
  6067. Charger.ble.initSeq++;
  6068. }
  6069. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_ROLE])!=NULL)
  6070. {
  6071. Charger.ble.initSeq++;
  6072. }
  6073. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_DTR])!=NULL)
  6074. {
  6075. Charger.ble.initSeq++;
  6076. }
  6077. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_DSR])!=NULL)
  6078. {
  6079. Charger.ble.initSeq++;
  6080. }
  6081. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_PAIRING_MODE])!=NULL)
  6082. {
  6083. Charger.ble.initSeq++;
  6084. }
  6085. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_SECURITY_MODE])!=NULL)
  6086. {
  6087. Charger.ble.initSeq++;
  6088. }
  6089. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_SECURITY_TYPE])!=NULL)
  6090. {
  6091. Charger.ble.initSeq++;
  6092. }
  6093. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_DISCOVERABILITY])!=NULL)
  6094. {
  6095. Charger.ble.initSeq++;
  6096. }
  6097. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_CONNECTABILITY])!=NULL)
  6098. {
  6099. Charger.ble.initSeq++;
  6100. }
  6101. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_GET_ADDR])!=NULL)
  6102. {
  6103. memcpy(Charger.ble.peripheral_mac, &UART_BLE_rx_buffer[strcspn((char *)UART_BLE_rx_buffer, ":")+1], 12);
  6104. Charger.ble.peripheral_mac[12] = '\0';
  6105. DEBUG_INFO("Peripheral mac address: %s\r\n", Charger.ble.peripheral_mac);
  6106. Charger.ble.initSeq++;
  6107. }
  6108. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_NAME])!=NULL)
  6109. {
  6110. Charger.ble.initSeq++;
  6111. }
  6112. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_MAX_POWER])!=NULL)
  6113. {
  6114. Charger.ble.initSeq++;
  6115. }
  6116. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_MIN_INTERVAL])!=NULL)
  6117. {
  6118. Charger.ble.initSeq++;
  6119. }
  6120. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_MAX_INTERVAL])!=NULL)
  6121. {
  6122. Charger.ble.initSeq++;
  6123. }
  6124. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_TX_PHY])!=NULL)
  6125. {
  6126. Charger.ble.initSeq++;
  6127. }
  6128. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_RX_PHY])!=NULL)
  6129. {
  6130. Charger.ble.initSeq++;
  6131. }
  6132. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_DATA_MODE])!=NULL)
  6133. {
  6134. Charger.ble.isDataMode = ON;
  6135. Charger.ble.initSeq++;
  6136. }
  6137. }
  6138. else
  6139. {
  6140. // Application protocol rx process
  6141. uint8_t tx[UART_BUFFER_SIZE];
  6142. uint8_t tx_len;
  6143. uint8_t chksum = 0;
  6144. uint32_t tmp;
  6145. //HTK_ByteArray2HexStr_XP("BLE_RX: ", UART_BLE_rx_buffer, 0, 16);
  6146. if(isValidCheckSum_BLE() && (rndNumber != ((UART_BLE_rx_buffer[3]<<0) | (UART_BLE_rx_buffer[4]<<8) | (UART_BLE_rx_buffer[5]<<16) | (UART_BLE_rx_buffer[6]<<24))))
  6147. {
  6148. // Timer reset
  6149. timerRefresh(TIMER_IDX_BLE);
  6150. rndNumber = ((UART_BLE_rx_buffer[3]<<0) | (UART_BLE_rx_buffer[4]<<8) | (UART_BLE_rx_buffer[5]<<16) | (UART_BLE_rx_buffer[6]<<24));
  6151. switch(UART_BLE_rx_buffer[2])
  6152. {
  6153. case BLE_PROTOCOL_MESSAGE_SIGN_IN:
  6154. DEBUG_INFO("BLE device sign request...%d\r\n", UART_BLE_rx_buffer[7]);
  6155. tx_len = 5;
  6156. tx[0] = (tx_len>>8)&0xff;
  6157. tx[1] = (tx_len&0xff);
  6158. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6159. tx[3] = 0x00;
  6160. if(Charger.memory.EVSE_Config.data.item.bleConfig.isRegPuk)
  6161. {
  6162. if(UART_BLE_rx_buffer[7] == 0x00)
  6163. {
  6164. if(memcmp(&UART_BLE_rx_buffer[8], &Charger.memory.EVSE_Config.data.item.bleConfig.idRoot[0], ((UART_BLE_rx_buffer[0]<<8) | (UART_BLE_rx_buffer[1]<<0)-9)) == 0)
  6165. {
  6166. // Root sign in
  6167. tx[3] = 0x02;
  6168. Charger.ble.loginRole = BLE_LOGIN_ROLE_ROOT;
  6169. DEBUG_INFO("Sign in as root.\r\n");
  6170. }
  6171. }
  6172. else
  6173. {
  6174. if(memcmp(&UART_BLE_rx_buffer[8], &Charger.memory.whiteList.data.item[0].pin[0], ((UART_BLE_rx_buffer[0]<<8) | (UART_BLE_rx_buffer[1]<<0)-9)) == 0)
  6175. {
  6176. // User sign in
  6177. tx[3] = 0x03;
  6178. Charger.ble.loginRole = BLE_LOGIN_ROLE_USER;
  6179. DEBUG_INFO("Sign in as user.\r\n");
  6180. }
  6181. }
  6182. }
  6183. else
  6184. {
  6185. if(UART_BLE_rx_buffer[7] == 0x00)
  6186. {
  6187. // Visitor
  6188. tx[3] = 0x01;
  6189. Charger.ble.loginRole = BLE_LOGIN_ROLE_VISITOR;
  6190. memcpy(&Charger.ble.loginId[0], &UART_BLE_rx_buffer[8], ((UART_BLE_rx_buffer[0]<<8) | (UART_BLE_rx_buffer[1]<<0)-9) );
  6191. DEBUG_WARN("Sign in as visitor.\r\n");
  6192. }
  6193. }
  6194. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6195. {
  6196. chksum ^= tx[2 + idx];
  6197. }
  6198. tx[4] = chksum;
  6199. break;
  6200. case BLE_PROTOCOL_MESSAGE_PUK_REG:
  6201. DEBUG_INFO("BLE device PUK registe.%02x%02x%02x%02x%02x%02x%02x%02x\r\n", UART_BLE_rx_buffer[7], UART_BLE_rx_buffer[8], UART_BLE_rx_buffer[9], UART_BLE_rx_buffer[10], UART_BLE_rx_buffer[11], UART_BLE_rx_buffer[12], UART_BLE_rx_buffer[13], UART_BLE_rx_buffer[14]);
  6202. tx_len = 11;
  6203. tx[0] = (tx_len>>8)&0xff;
  6204. tx[1] = (tx_len&0xff);
  6205. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6206. tx[3] = 0x00;
  6207. if(Charger.memory.EVSE_Config.data.item.bleConfig.isRegPuk)
  6208. {
  6209. if(memcmp(&Charger.ble.loginId[0], &Charger.memory.EVSE_Config.data.item.bleConfig.idRoot[0], ARRAY_SIZE(Charger.ble.loginId)))
  6210. {
  6211. tx[3] = 0x02;
  6212. DEBUG_INFO("ID registered exist.\r\n");
  6213. }
  6214. }
  6215. else
  6216. {
  6217. if(Charger.ble.loginRole == BLE_LOGIN_ROLE_VISITOR)
  6218. {
  6219. if(memcmp(&UART_BLE_rx_buffer[7], &Charger.memory.EVSE_Config.data.item.bleConfig.puk[0], 8) == 0x00)
  6220. {
  6221. tx[3] = 0x01;
  6222. // PIN list generation
  6223. Charger.memory.EVSE_Config.data.item.bleConfig.isGenPin = ON;
  6224. for(uint8_t idx=0;idx<20;idx++)
  6225. {
  6226. Charger.memory.whiteList.data.item[idx].listType = WHITE_LIST_TYPE_BLE;
  6227. Charger.memory.whiteList.data.item[idx].pin[0] = (rand()%0x2b)+0x30;
  6228. Charger.memory.whiteList.data.item[idx].pin[1] = (rand()%0x2b)+0x30;
  6229. Charger.memory.whiteList.data.item[idx].pin[2] = (rand()%0x2b)+0x30;
  6230. Charger.memory.whiteList.data.item[idx].pin[3] = (rand()%0x2b)+0x30;
  6231. Charger.memory.whiteList.data.item[idx].pin[4] = (rand()%0x2b)+0x30;
  6232. Charger.memory.whiteList.data.item[idx].pin[5] = (rand()%0x2b)+0x30;
  6233. Charger.memory.whiteList.data.item[idx].isReg = OFF;
  6234. }
  6235. Charger.memory.whiteList.op_bits.update = ON;
  6236. Charger.memory.EVSE_Config.data.item.bleConfig.isRegPuk = ON;
  6237. memcpy(&Charger.memory.EVSE_Config.data.item.bleConfig.idRoot[0], &Charger.ble.loginId[0], ARRAY_SIZE(Charger.ble.loginId));
  6238. Charger.memory.EVSE_Config.op_bits.update = ON;
  6239. DEBUG_INFO("Root registered and PIN list generate success.\r\n");
  6240. }
  6241. }
  6242. }
  6243. memcpy(&tx[4], &UART_BLE_rx_buffer[7], 6);
  6244. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6245. {
  6246. chksum ^= tx[2 + idx];
  6247. }
  6248. tx[10] = chksum;
  6249. break;
  6250. case BLE_PROTOCOL_MESSAGE_PIN_GET:
  6251. DEBUG_INFO("PIN get request.\r\n");
  6252. tx_len = 11;
  6253. tx[0] = (tx_len>>8)&0xff;
  6254. tx[1] = (tx_len&0xff);
  6255. tx[2] = UART_BLE_rx_buffer[2] + 0x80;;
  6256. tx[3] = 0x00;
  6257. if(Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT)
  6258. {
  6259. tx[3] = 0x01;
  6260. memcpy(&tx[4], &Charger.memory.whiteList.data.item[0].pin[0], 6);
  6261. DEBUG_INFO("PIN: %s\r\n", Charger.memory.whiteList.data.item[0].pin);
  6262. }
  6263. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6264. {
  6265. chksum ^= tx[2 + idx];
  6266. }
  6267. tx[10] = chksum;
  6268. break;
  6269. case BLE_PROTOCOL_MESSAGE_PIN_RENEW:
  6270. DEBUG_INFO("BLE device PIN renew request.\r\n");
  6271. tx_len = 13;
  6272. tx[0] = (tx_len>>8)&0xff;
  6273. tx[1] = (tx_len&0xff);
  6274. tx[2] = UART_BLE_rx_buffer[2] + 0x80;;
  6275. tx[3] = 0x00;
  6276. if((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT))
  6277. {
  6278. for(uint8_t idx=0;idx<20;idx++)
  6279. {
  6280. Charger.memory.whiteList.data.item[idx].listType = WHITE_LIST_TYPE_BLE;
  6281. Charger.memory.whiteList.data.item[idx].pin[0] = (rand()%0x2b)+0x30;
  6282. Charger.memory.whiteList.data.item[idx].pin[1] = (rand()%0x2b)+0x30;
  6283. Charger.memory.whiteList.data.item[idx].pin[2] = (rand()%0x2b)+0x30;
  6284. Charger.memory.whiteList.data.item[idx].pin[3] = (rand()%0x2b)+0x30;
  6285. Charger.memory.whiteList.data.item[idx].pin[4] = (rand()%0x2b)+0x30;
  6286. Charger.memory.whiteList.data.item[idx].pin[5] = (rand()%0x2b)+0x30;
  6287. Charger.memory.whiteList.data.item[idx].isReg = OFF;
  6288. }
  6289. Charger.memory.whiteList.op_bits.update = ON;
  6290. tx[3] = 0x01;
  6291. memcpy(&tx[4], &Charger.memory.whiteList.data.item[0].pin[0], 6);
  6292. DEBUG_INFO("PIN list renew: %s.\r\n", Charger.memory.whiteList.data.item[0].pin);
  6293. }
  6294. else
  6295. DEBUG_WARN("Login id not root.\r\n");
  6296. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6297. {
  6298. chksum ^= tx[2 + idx];
  6299. }
  6300. tx[4] = chksum;
  6301. break;
  6302. case BLE_PROTOCOL_MESSAGE_CHARGING_START:
  6303. DEBUG_INFO("BLE device request charging start.\r\n");
  6304. tx_len = 0x08;
  6305. tx[0] = (tx_len>>8)&0xff;
  6306. tx[1] = (tx_len&0xff);
  6307. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6308. if(Charger.Alarm_Code>0)
  6309. {
  6310. tx[3] = 0x02;
  6311. if((Charger.Alarm_Code & ALARM_OVER_VOLTAGE))
  6312. {
  6313. tx[4] = 0x01;
  6314. tx[5] = 0x22;
  6315. tx[6] = 0x00;
  6316. }
  6317. else if((Charger.Alarm_Code & ALARM_UNDER_VOLTAGE))
  6318. {
  6319. tx[4] = 0x01;
  6320. tx[5] = 0x22;
  6321. tx[6] = 0x03;
  6322. }
  6323. else if((Charger.Alarm_Code & ALARM_OVER_CURRENT))
  6324. {
  6325. tx[4] = 0x01;
  6326. tx[5] = 0x21;
  6327. tx[6] = 0x16;
  6328. }
  6329. else if((Charger.Alarm_Code & ALARM_OVER_TEMPERATURE))
  6330. {
  6331. tx[4] = 0x01;
  6332. tx[5] = 0x22;
  6333. tx[6] = 0x23;
  6334. }
  6335. else if((Charger.Alarm_Code & ALARM_GROUND_FAIL))
  6336. {
  6337. tx[4] = 0x01;
  6338. tx[5] = 0x22;
  6339. tx[6] = 0x56;
  6340. }
  6341. else if((Charger.Alarm_Code & ALARM_CP_ERROR))
  6342. {
  6343. tx[4] = 0x02;
  6344. tx[5] = 0x37;
  6345. tx[6] = 0x03;
  6346. }
  6347. else if((Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC)||(Charger.Alarm_Code & ALARM_CURRENT_LEAK_DC))
  6348. {
  6349. tx[4] = 0x01;
  6350. tx[5] = 0x22;
  6351. tx[6] = 0x33;
  6352. }
  6353. else if((Charger.Alarm_Code & ALARM_MCU_TESTFAIL))
  6354. {
  6355. tx[4] = 0x01;
  6356. tx[5] = 0x22;
  6357. tx[6] = 0x57;
  6358. }
  6359. else if((Charger.Alarm_Code & ALARM_EMERGENCY_STOP))
  6360. {
  6361. tx[4] = 0x01;
  6362. tx[5] = 0x22;
  6363. tx[6] = 0x51;
  6364. }
  6365. else if((Charger.Alarm_Code & ALARM_LEAK_MODULE_FAIL))
  6366. {
  6367. tx[4] = 0x01;
  6368. tx[5] = 0x10;
  6369. tx[6] = 0x04;
  6370. }
  6371. else if((Charger.Alarm_Code & ALARM_RELAY_STATUS))
  6372. {
  6373. tx[4] = 0x01;
  6374. tx[5] = 0x22;
  6375. tx[6] = 0x58;
  6376. }
  6377. DEBUG_WARN("EVSE in alarm state, can not start charging.\r\n");
  6378. }
  6379. else
  6380. {
  6381. if((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))
  6382. {
  6383. if(Charger.Mode == MODE_IDLE)
  6384. {
  6385. Charger.ble.isRequestOn = ON;
  6386. tx[3] = 0x01;
  6387. DEBUG_INFO("Start OK.\r\n");
  6388. }
  6389. else
  6390. {
  6391. tx[3] = 0x00;
  6392. DEBUG_WARN("EVSE not in IDLE.\r\n");
  6393. }
  6394. }
  6395. else
  6396. {
  6397. tx[3] = 0x00;
  6398. DEBUG_WARN("Login first\r\n");
  6399. }
  6400. tx[4] = 0x00;
  6401. tx[5] = 0x00;
  6402. tx[6] = 0x00;
  6403. }
  6404. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6405. {
  6406. chksum ^= tx[2 + idx];
  6407. }
  6408. tx[7] = chksum;
  6409. break;
  6410. case BLE_PROTOCOL_MESSAGE_CHARGING_STOP:
  6411. DEBUG_INFO("BLE device request charging stop.\r\n");
  6412. tx_len = 0x08;
  6413. tx[0] = (tx_len>>8)&0xff;
  6414. tx[1] = (tx_len&0xff);
  6415. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6416. if(Charger.Alarm_Code>0)
  6417. {
  6418. tx[3] = 0x02;
  6419. if((Charger.Alarm_Code & ALARM_OVER_VOLTAGE))
  6420. {
  6421. tx[4] = 0x01;
  6422. tx[5] = 0x22;
  6423. tx[6] = 0x00;
  6424. }
  6425. else if((Charger.Alarm_Code & ALARM_UNDER_VOLTAGE))
  6426. {
  6427. tx[4] = 0x01;
  6428. tx[5] = 0x22;
  6429. tx[6] = 0x03;
  6430. }
  6431. else if((Charger.Alarm_Code & ALARM_OVER_CURRENT))
  6432. {
  6433. tx[4] = 0x01;
  6434. tx[5] = 0x21;
  6435. tx[6] = 0x16;
  6436. }
  6437. else if((Charger.Alarm_Code & ALARM_OVER_TEMPERATURE))
  6438. {
  6439. tx[4] = 0x01;
  6440. tx[5] = 0x22;
  6441. tx[6] = 0x23;
  6442. }
  6443. else if((Charger.Alarm_Code & ALARM_GROUND_FAIL))
  6444. {
  6445. tx[4] = 0x01;
  6446. tx[5] = 0x22;
  6447. tx[6] = 0x56;
  6448. }
  6449. else if((Charger.Alarm_Code & ALARM_CP_ERROR))
  6450. {
  6451. tx[4] = 0x02;
  6452. tx[5] = 0x37;
  6453. tx[6] = 0x03;
  6454. }
  6455. else if((Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC)||(Charger.Alarm_Code & ALARM_CURRENT_LEAK_DC))
  6456. {
  6457. tx[4] = 0x01;
  6458. tx[5] = 0x22;
  6459. tx[6] = 0x33;
  6460. }
  6461. else if((Charger.Alarm_Code & ALARM_MCU_TESTFAIL))
  6462. {
  6463. tx[4] = 0x01;
  6464. tx[5] = 0x22;
  6465. tx[6] = 0x57;
  6466. }
  6467. else if((Charger.Alarm_Code & ALARM_EMERGENCY_STOP))
  6468. {
  6469. tx[4] = 0x01;
  6470. tx[5] = 0x22;
  6471. tx[6] = 0x51;
  6472. }
  6473. else if((Charger.Alarm_Code & ALARM_LEAK_MODULE_FAIL))
  6474. {
  6475. tx[4] = 0x01;
  6476. tx[5] = 0x10;
  6477. tx[6] = 0x04;
  6478. }
  6479. else if((Charger.Alarm_Code & ALARM_RELAY_STATUS))
  6480. {
  6481. tx[4] = 0x01;
  6482. tx[5] = 0x22;
  6483. tx[6] = 0x58;
  6484. }
  6485. DEBUG_WARN("EVSE in alarm state, can not stop charging.\r\n");
  6486. }
  6487. else
  6488. {
  6489. if((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))
  6490. {
  6491. if((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))
  6492. {
  6493. Charger.ble.isRequestOff = ON;
  6494. tx[3] = 0x01;
  6495. DEBUG_INFO("Stop OK.\r\n");
  6496. }
  6497. else
  6498. {
  6499. tx[3] = 0x00;
  6500. DEBUG_WARN("EVSE not in CHARGING.\r\n");
  6501. }
  6502. }
  6503. else
  6504. {
  6505. tx[3] = 0x00;
  6506. DEBUG_WARN("Login first\r\n");
  6507. }
  6508. tx[4] = 0x00;
  6509. tx[5] = 0x00;
  6510. tx[6] = 0x00;
  6511. }
  6512. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6513. {
  6514. chksum ^= tx[2 + idx];
  6515. }
  6516. tx[7] = chksum;
  6517. break;
  6518. case BLE_PROTOCOL_MESSAGE_STATUS_QUERY:
  6519. DEBUG_INFO("BLE device query EVSE status request.\r\n");
  6520. tx_len = 0x13;
  6521. tx[0] = (tx_len>>8)&0xff;
  6522. tx[1] = (tx_len&0xff);
  6523. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6524. tx[3] = (((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))?Charger.Mode:0x00);
  6525. tx[4] = (((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))?(((Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative/10000)>>0) & 0xff):0x00);
  6526. tx[5] = (((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))?(((Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative/10000)>>8) & 0xff):0x00);
  6527. tx[6] = (((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))?(((Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative/10000)>>16) & 0xff):0x00);
  6528. tx[7] = (((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))?(((Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative/10000)>>24) & 0xff):0x00);
  6529. tx[8] = (((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))?(((Charger.cycle_info.Power_Consumption/10)>>0) & 0xff):0x00);
  6530. tx[9] = (((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))?(((Charger.cycle_info.Power_Consumption/10)>>8) & 0xff):0x00);
  6531. tx[10] = (((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))?(((Charger.cycle_info.Duration/60000)>>0) & 0xff):0x00);
  6532. tx[11] = (((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))?(((Charger.cycle_info.Duration/60000)>>8) & 0xff):0x00);
  6533. if((Charger.Alarm_Code>0) && ((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER)))
  6534. {
  6535. if((Charger.Alarm_Code & ALARM_OVER_VOLTAGE))
  6536. {
  6537. tx[12] = 0x01;
  6538. tx[13] = 0x22;
  6539. tx[14] = 0x00;
  6540. }
  6541. else if((Charger.Alarm_Code & ALARM_UNDER_VOLTAGE))
  6542. {
  6543. tx[12] = 0x01;
  6544. tx[13] = 0x22;
  6545. tx[14] = 0x03;
  6546. }
  6547. else if((Charger.Alarm_Code & ALARM_OVER_CURRENT))
  6548. {
  6549. tx[12] = 0x01;
  6550. tx[13] = 0x21;
  6551. tx[14] = 0x16;
  6552. }
  6553. else if((Charger.Alarm_Code & ALARM_OVER_TEMPERATURE))
  6554. {
  6555. tx[12] = 0x01;
  6556. tx[13] = 0x22;
  6557. tx[14] = 0x23;
  6558. }
  6559. else if((Charger.Alarm_Code & ALARM_GROUND_FAIL))
  6560. {
  6561. tx[12] = 0x01;
  6562. tx[13] = 0x22;
  6563. tx[14] = 0x56;
  6564. }
  6565. else if((Charger.Alarm_Code & ALARM_CP_ERROR))
  6566. {
  6567. tx[12] = 0x02;
  6568. tx[13] = 0x37;
  6569. tx[14] = 0x03;
  6570. }
  6571. else if((Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC)||(Charger.Alarm_Code & ALARM_CURRENT_LEAK_DC))
  6572. {
  6573. tx[12] = 0x01;
  6574. tx[13] = 0x22;
  6575. tx[14] = 0x33;
  6576. }
  6577. else if((Charger.Alarm_Code & ALARM_MCU_TESTFAIL))
  6578. {
  6579. tx[12] = 0x01;
  6580. tx[13] = 0x22;
  6581. tx[14] = 0x57;
  6582. }
  6583. else if((Charger.Alarm_Code & ALARM_EMERGENCY_STOP))
  6584. {
  6585. tx[12] = 0x01;
  6586. tx[13] = 0x22;
  6587. tx[14] = 0x51;
  6588. }
  6589. else if((Charger.Alarm_Code & ALARM_LEAK_MODULE_FAIL))
  6590. {
  6591. tx[12] = 0x01;
  6592. tx[13] = 0x10;
  6593. tx[14] = 0x04;
  6594. }
  6595. else if((Charger.Alarm_Code & ALARM_RELAY_STATUS))
  6596. {
  6597. tx[12] = 0x01;
  6598. tx[13] = 0x22;
  6599. tx[14] = 0x58;
  6600. }
  6601. }
  6602. else
  6603. {
  6604. tx[12] = 0x00;
  6605. tx[13] = 0x00;
  6606. tx[14] = 0x00;
  6607. }
  6608. tx[15] = Charger.memory.EVSE_Config.data.item.reservation.mode;
  6609. tx[16] = Charger.memory.EVSE_Config.data.item.reservation.hour;
  6610. tx[17] = Charger.memory.EVSE_Config.data.item.reservation.min;
  6611. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6612. {
  6613. chksum ^= tx[2 + idx];
  6614. }
  6615. tx[18] = chksum;
  6616. break;
  6617. case BLE_PROTOCOL_MESSAGE_RESERVATION:
  6618. switch(UART_BLE_rx_buffer[7])
  6619. {
  6620. case RESERVATION_DISABLE:
  6621. DEBUG_INFO("BLE device reservation request disable.\r\n");
  6622. break;
  6623. case RESERVATION_ONCE:
  6624. DEBUG_INFO("BLE device reservation request once on %d:%d.\r\n", UART_BLE_rx_buffer[8], UART_BLE_rx_buffer[9]);
  6625. break;
  6626. case RESERVATION_ALWAYS:
  6627. DEBUG_INFO("BLE device reservation request always on %d:%d.\r\n", UART_BLE_rx_buffer[8], UART_BLE_rx_buffer[9]);
  6628. break;
  6629. }
  6630. tx_len = 0x05;
  6631. tx[0] = (tx_len>>8)&0xff;
  6632. tx[1] = (tx_len&0xff);
  6633. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6634. Charger.memory.EVSE_Config.data.item.reservation.mode = UART_BLE_rx_buffer[7];
  6635. Charger.memory.EVSE_Config.data.item.reservation.hour = UART_BLE_rx_buffer[8];
  6636. Charger.memory.EVSE_Config.data.item.reservation.min = UART_BLE_rx_buffer[9];
  6637. Charger.memory.EVSE_Config.op_bits.update = ON;
  6638. tx[3] = 0x01;
  6639. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6640. {
  6641. chksum ^= tx[2 + idx];
  6642. }
  6643. tx[4] = chksum;
  6644. break;
  6645. case BLE_PROTOCOL_MESSAGE_TIME_QUERY:
  6646. DEBUG_INFO("BLE device query date time request.\r\n");
  6647. tx_len = 0x08;
  6648. tx[0] = (tx_len>>8)&0xff;
  6649. tx[1] = (tx_len&0xff);
  6650. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6651. struct tm gm_date;
  6652. gm_date.tm_year = Charger.memory.EVSE_Config.data.item.SystemDateTime.year-1900;
  6653. gm_date.tm_mon = Charger.memory.EVSE_Config.data.item.SystemDateTime.month-1;
  6654. gm_date.tm_mday = Charger.memory.EVSE_Config.data.item.SystemDateTime.day;
  6655. gm_date.tm_hour = Charger.memory.EVSE_Config.data.item.SystemDateTime.hour;
  6656. gm_date.tm_min = Charger.memory.EVSE_Config.data.item.SystemDateTime.min;
  6657. gm_date.tm_sec = Charger.memory.EVSE_Config.data.item.SystemDateTime.sec;
  6658. tmp = mktime(&gm_date);
  6659. tx[3] = ((tmp >> 0)&0xff);
  6660. tx[4] = ((tmp >> 8)&0xff);
  6661. tx[5] = ((tmp >> 16)&0xff);
  6662. tx[6] = ((tmp >> 24)&0xff);
  6663. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6664. {
  6665. chksum ^= tx[2 + idx];
  6666. }
  6667. tx[7] = chksum;
  6668. break;
  6669. case BLE_PROTOCOL_MESSAGE_TIME_CONFIG:
  6670. DEBUG_INFO("BLE device config date time.\r\n");
  6671. tx_len = 0x09;
  6672. tx[0] = (tx_len>>8)&0xff;
  6673. tx[1] = (tx_len&0xff);
  6674. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6675. if((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))
  6676. {
  6677. struct tm *sm_date;
  6678. tmp = (UART_BLE_rx_buffer[7]<<0) | (UART_BLE_rx_buffer[8]<<8) | (UART_BLE_rx_buffer[9]<<16) | (UART_BLE_rx_buffer[10]<<24);
  6679. sm_date = gmtime(&tmp) ;
  6680. setTime.Hours = sm_date->tm_hour;
  6681. setTime.Minutes = sm_date->tm_min;
  6682. setTime.Seconds = sm_date->tm_sec;
  6683. setTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  6684. setTime.StoreOperation = RTC_STOREOPERATION_RESET;
  6685. if (HAL_RTC_SetTime(&hrtc, &setTime, RTC_FORMAT_BIN) != HAL_OK)
  6686. {
  6687. Error_Handler();
  6688. }
  6689. setDate.Year = (sm_date->tm_year + 1900)-2000;
  6690. setDate.Month = sm_date->tm_mon + 1;
  6691. setDate.Date = sm_date->tm_mday;
  6692. if (HAL_RTC_SetDate(&hrtc, &setDate, RTC_FORMAT_BIN) != HAL_OK)
  6693. {
  6694. Error_Handler();
  6695. }
  6696. tx[3] = 0x01;
  6697. DEBUG_INFO("RTC config OK.\r\n");
  6698. }
  6699. else
  6700. {
  6701. tx[3] = 0x00;
  6702. DEBUG_WARN("User con not config RTC.\r\n");
  6703. }
  6704. tx[4] = UART_BLE_rx_buffer[7];
  6705. tx[5] = UART_BLE_rx_buffer[8];
  6706. tx[6] = UART_BLE_rx_buffer[9];
  6707. tx[7] = UART_BLE_rx_buffer[10];
  6708. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6709. {
  6710. chksum ^= tx[2 + idx];
  6711. }
  6712. tx[8] = chksum;
  6713. break;
  6714. case BLE_PROTOCOL_MESSAGE_LED_QUERY:
  6715. DEBUG_INFO("BLE device query led setting.\r\n");
  6716. tx_len = 0x08;
  6717. tx[0] = (tx_len>>8)&0xff;
  6718. tx[1] = (tx_len&0xff);
  6719. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6720. /*
  6721. TODO: Query led result here
  6722. */
  6723. if((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))
  6724. {
  6725. tx[3] = 0xff;
  6726. tx[4] = 0xff;
  6727. tx[5] = 0xff;
  6728. tx[6] = 0xff;
  6729. }
  6730. else
  6731. {
  6732. tx[3] = 0x00;
  6733. tx[4] = 0x00;
  6734. tx[5] = 0x00;
  6735. tx[6] = 0x00;
  6736. }
  6737. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6738. {
  6739. chksum ^= tx[2 + idx];
  6740. }
  6741. tx[7] = chksum;
  6742. break;
  6743. case BLE_PROTOCOL_MESSAGE_LED_CONFIG:
  6744. DEBUG_INFO("BLE device config led setting.\r\n");
  6745. tx_len = 0x09;
  6746. tx[0] = (tx_len>>8)&0xff;
  6747. tx[1] = (tx_len&0xff);
  6748. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6749. /*
  6750. TODO: Config led result here
  6751. */
  6752. if((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))
  6753. tx[3] = 0x01;
  6754. else
  6755. tx[3] = 0x00;
  6756. tx[4] = UART_BLE_rx_buffer[7];
  6757. tx[5] = UART_BLE_rx_buffer[8];
  6758. tx[6] = UART_BLE_rx_buffer[9];
  6759. tx[7] = UART_BLE_rx_buffer[10];
  6760. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6761. {
  6762. chksum ^= tx[2 + idx];
  6763. }
  6764. tx[8] = chksum;
  6765. break;
  6766. case BLE_PROTOCOL_MESSAGE_CURRENT_LIMIT_QUERY:
  6767. DEBUG_INFO("BLE device query current limit request.\r\n");
  6768. tx_len = 0x05;
  6769. tx[0] = (tx_len>>8)&0xff;
  6770. tx[1] = (tx_len&0xff);
  6771. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6772. if((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))
  6773. {
  6774. tx[3] = Charger.memory.EVSE_Config.data.item.MaxChargingCurrent;
  6775. DEBUG_INFO("Query current limit: %02dA.\r\n", Charger.memory.EVSE_Config.data.item.MaxChargingCurrent);
  6776. }
  6777. else
  6778. {
  6779. tx[3] = 0x00;
  6780. DEBUG_WARN("Please login first, before query current limit.\r\n");
  6781. }
  6782. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6783. {
  6784. chksum ^= tx[2 + idx];
  6785. }
  6786. tx[8] = chksum;
  6787. break;
  6788. case BLE_PROTOCOL_MESSAGE_CURRENT_LIMIT_CONFIG:
  6789. DEBUG_INFO("BLE device config current limit.\r\n");
  6790. tx_len = 0x06;
  6791. tx[0] = (tx_len>>8)&0xff;
  6792. tx[1] = (tx_len&0xff);
  6793. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6794. if((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))
  6795. {
  6796. if(UART_BLE_rx_buffer[7]>Charger.maxRatingCurrent)
  6797. {
  6798. tx[3] = 0x02;
  6799. Charger.memory.EVSE_Config.data.item.MaxChargingCurrent = Charger.maxRatingCurrent;
  6800. DEBUG_INFO("Setting value over max rating value, set as max rating .\r\n");
  6801. }
  6802. else
  6803. {
  6804. tx[3] = 0x01;
  6805. Charger.memory.EVSE_Config.data.item.MaxChargingCurrent = UART_BLE_rx_buffer[7];
  6806. }
  6807. DEBUG_INFO("Config current limit: %02dA\r\n", Charger.memory.EVSE_Config.data.item.MaxChargingCurrent);
  6808. }
  6809. else
  6810. {
  6811. tx[3] = 0x00;
  6812. DEBUG_WARN("Please login first, before setting current limit.\r\n");
  6813. }
  6814. tx[4] = UART_BLE_rx_buffer[7];
  6815. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6816. {
  6817. chksum ^= tx[2 + idx];
  6818. }
  6819. tx[5] = chksum;
  6820. break;
  6821. case BLE_PROTOCOL_MESSAGE_FIRMWARE_VERSION_QUERY:
  6822. DEBUG_INFO("BLE device query firmware version.\r\n");
  6823. tx_len = 4 + strlen(Charger.Ver_FW);
  6824. tx[0] = (tx_len>>8)&0xff;
  6825. tx[1] = (tx_len&0xff);
  6826. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6827. for(uint16_t idx=0;idx<strlen(Charger.Ver_FW);idx++)
  6828. {
  6829. if((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))
  6830. tx[3+idx] = Charger.Ver_FW[idx];
  6831. else
  6832. tx[3+idx] = 0x00;
  6833. }
  6834. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6835. {
  6836. chksum ^= tx[2 + idx];
  6837. }
  6838. tx[3+strlen(Charger.Ver_FW)] = chksum;
  6839. break;
  6840. case BLE_PROTOCOL_MESSAGE_FIRMWARE_UPDATE_REQ:
  6841. tx_len = 5;
  6842. tx[0] = 0x00;
  6843. tx[1] = 0x05;
  6844. tx[2] = UART_BLE_rx_buffer[2] + 0x80;;
  6845. if(Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT)
  6846. {
  6847. binCRCTarget = (UART_BLE_rx_buffer[7] << 24) | (UART_BLE_rx_buffer[8] << 16) | (UART_BLE_rx_buffer[9] << 8) | (UART_BLE_rx_buffer[10] << 0);
  6848. flashdestination = NEW_CODE_ADDRESS;
  6849. if(FLASH_If_Erase(NEW_CODE_ADDRESS, 3)== FLASHIF_OK)
  6850. {
  6851. DEBUG_INFO("Firmware transfer start, earase flash success....\n\r");
  6852. tx[3] = 0x01;
  6853. }
  6854. else
  6855. {
  6856. DEBUG_INFO("Firmware transfer start, earase flash fail....\n\r");
  6857. tx[3] = 0x00;
  6858. }
  6859. }
  6860. else
  6861. {
  6862. tx[3] = 0x00;
  6863. DEBUG_WARN("User can not upgrade start.\r\n");
  6864. }
  6865. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6866. {
  6867. chksum ^= tx[2 + idx];
  6868. }
  6869. tx[4] = chksum;
  6870. break;
  6871. case BLE_PROTOCOL_MESSAGE_FIRMWARE_TRANS_REQ:
  6872. tx_len = 5;
  6873. tx[0] = 0x00;
  6874. tx[1] = 0x05;
  6875. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6876. if(Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT)
  6877. {
  6878. flashdestination = NEW_CODE_ADDRESS + ((UART_BLE_rx_buffer[7]<<24) | (UART_BLE_rx_buffer[8]<<16) | (UART_BLE_rx_buffer[9]<<8) |(UART_BLE_rx_buffer[10]<<0));
  6879. if (FLASH_If_Write(flashdestination, (uint32_t*) &UART_BLE_rx_buffer[11], ((((UART_BLE_rx_buffer[1]) | (UART_BLE_rx_buffer[0])<<8)-12)>>2)) == FLASHIF_OK)
  6880. {
  6881. DEBUG_INFO("Firmware transfer start address: 0x%x ,length: %d...Pass\n\r", flashdestination, (((UART_BLE_rx_buffer[1]) | (UART_BLE_rx_buffer[0])<<8)-12));
  6882. tx[3] = 0x01;
  6883. }
  6884. else
  6885. {
  6886. DEBUG_INFO("Firmware transfer start address: 0x%x, length: %d...Fail\n\r", flashdestination, (((UART_BLE_rx_buffer[1]) | (UART_BLE_rx_buffer[0])<<8)-12));
  6887. tx[3] = 0x00;
  6888. }
  6889. }
  6890. else
  6891. {
  6892. tx[3] = 0x00;
  6893. DEBUG_WARN("User can not upgrade transfer.\r\n");
  6894. }
  6895. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6896. {
  6897. chksum ^= tx[2 + idx];
  6898. }
  6899. tx[4] = chksum;
  6900. break;
  6901. case BLE_PROTOCOL_MESSAGE_FIRMWARE_END_REQ:
  6902. tx_len = 5;
  6903. tx[0] = 0x00;
  6904. tx[1] = 0x05;
  6905. tx[2] = UART_BLE_rx_buffer[2] + 0x80;;
  6906. if(Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT)
  6907. {
  6908. flash = NEW_CODE_ADDRESS;
  6909. checksum = HAL_CRC_Calculate(&hcrc, (uint32_t *)flash, ((FLASH_AP_LENGTH-4)>>2));
  6910. flash = ((uint32_t)(NEW_CODE_ADDRESS+FLASH_AP_LENGTH-4));
  6911. DEBUG_INFO("Firmware transfer end, AP CRC checksum, flash: 0x%x, 0x%x\r\n", checksum, *((uint32_t *)flash) );
  6912. if(checksum == *((uint32_t *)flash))
  6913. {
  6914. if (FLASH_If_Write(UPGRADE_REQ_ADDRESS, (uint32_t *)&endFlag[0], 1) == FLASHIF_OK)
  6915. {
  6916. DEBUG_INFO("Firmware Confirm Tag write ok..\n\r");
  6917. tx[3] = 0x01;
  6918. tx[4] = 0xB5;
  6919. HAL_UART_Transmit(&IAP_USART, (uint8_t *)tx, sizeof(tx), 0xffff);
  6920. osDelay(1000);
  6921. NVIC_SystemReset();
  6922. }
  6923. else
  6924. {
  6925. DEBUG_INFO("Firmware Confirm Tag write fail...\n\r");
  6926. tx[3] = 0x00;
  6927. }
  6928. }
  6929. else
  6930. {
  6931. DEBUG_INFO("Firmware checksum compare fail...\n\r");
  6932. tx[3] = 0x00;
  6933. }
  6934. }
  6935. else
  6936. {
  6937. tx[3] = 0x00;
  6938. DEBUG_WARN("User can not upgrade stop.\r\n");
  6939. }
  6940. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6941. {
  6942. chksum ^= tx[2 + idx];
  6943. }
  6944. tx[4] = chksum;
  6945. break;
  6946. default:
  6947. /*
  6948. TODO: Unknow command response here
  6949. */
  6950. tx_len = 5;
  6951. tx[0] = 0x00;
  6952. tx[1] = 0x05;
  6953. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6954. tx[3] = 0x00;
  6955. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6956. {
  6957. chksum ^= tx[2 + idx];
  6958. }
  6959. tx[4] = chksum;
  6960. DEBUG_ERROR("BLE unknow command.\r\n");
  6961. break;
  6962. }
  6963. }
  6964. else
  6965. {
  6966. /*
  6967. TODO: Protocol check sum error response here
  6968. */
  6969. tx_len = 5;
  6970. tx[0] = 0x00;
  6971. tx[1] = 0x05;
  6972. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6973. tx[3] = 0x00;
  6974. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6975. {
  6976. chksum ^= tx[2 + idx];
  6977. }
  6978. tx[4] = chksum;
  6979. DEBUG_ERROR("BLE check sum validation error.\r\n");
  6980. }
  6981. HAL_UART_Transmit(&BLE_USART, (uint8_t *)tx, tx_len, 0xffff);
  6982. }
  6983. UART_BLE_rx_len=0;
  6984. UART_BLE_recv_end_flag = OFF;
  6985. }
  6986. HAL_UART_Receive_DMA(&BLE_USART, (uint8_t*)UART_BLE_rx_buffer, UART_BUFFER_SIZE);
  6987. osDelay(1);
  6988. }
  6989. /* USER CODE END StartBleTask */
  6990. }
  6991. #ifdef MODIFY_FLASH_OPERATION_20241104
  6992. void Proc_EVSE_Config(void)
  6993. {
  6994. #ifdef MODIFY_UNDEFINED_MODEL_NAME_AND_SN_ISSUE
  6995. if (Charger.memory.EVSE_Config.data.item.ModelName[0] == 0xFF ||
  6996. Charger.memory.EVSE_Config.data.item.SerialNumber[0] == 0xFF)
  6997. {
  6998. Charger.memory.EVSE_Config.data.item.ModelName[MODEL_NAME_LEN - 1] = '\0';
  6999. Charger.memory.EVSE_Config.data.item.SerialNumber[SERIAL_NUM_LEN - 1] = '\0';
  7000. Charger.memory.EVSE_Config.op_bits.update = ON;
  7001. }
  7002. #endif //MODIFY_UNDEFINED_MODEL_NAME_AND_SN_ISSUE
  7003. Charger.memory.EVSE_Config.data.item.isDispDebug = ON;
  7004. sprintf(Charger.memory.EVSE_Config.data.item.System_ID, "%s%s", Charger.memory.EVSE_Config.data.item.ModelName, Charger.memory.EVSE_Config.data.item.SerialNumber);
  7005. //sprintf((char *)tmpBuf, "%08x", ~HAL_CRC_Calculate(&hcrc, (uint32_t *)&Charger.memory.EVSE_Config.data.item.System_ID[0], (ARRAY_SIZE(Charger.memory.EVSE_Config.data.item.System_ID)>>2)));
  7006. //memcpy(&Charger.memory.EVSE_Config.data.item.bleConfig.puk[0], &tmpBuf[0], ARRAY_SIZE(Charger.memory.EVSE_Config.data.item.bleConfig.puk));
  7007. //DEBUG_INFO("BLE PUK: %s\r\n", Charger.memory.EVSE_Config.data.item.bleConfig.puk);
  7008. srand(Charger.memory.EVSE_Config.data.item.bleConfig.puk[0] | (Charger.memory.EVSE_Config.data.item.bleConfig.puk[1]<<8));
  7009. setTime.Hours = Charger.memory.EVSE_Config.data.item.SystemDateTime.hour;
  7010. setTime.Minutes = Charger.memory.EVSE_Config.data.item.SystemDateTime.min;
  7011. setTime.Seconds = Charger.memory.EVSE_Config.data.item.SystemDateTime.sec;
  7012. setTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  7013. setTime.StoreOperation = RTC_STOREOPERATION_RESET;
  7014. if (HAL_RTC_SetTime(&hrtc, &setTime, RTC_FORMAT_BIN) != HAL_OK)
  7015. {
  7016. Error_Handler();
  7017. }
  7018. setDate.Year = Charger.memory.EVSE_Config.data.item.SystemDateTime.year-2000;
  7019. setDate.Month = Charger.memory.EVSE_Config.data.item.SystemDateTime.month;
  7020. setDate.Date = Charger.memory.EVSE_Config.data.item.SystemDateTime.day;
  7021. if (HAL_RTC_SetDate(&hrtc, &setDate, RTC_FORMAT_BIN) != HAL_OK)
  7022. {
  7023. Error_Handler();
  7024. }
  7025. //CCLC_Corr_Gain_Par
  7026. //VT
  7027. #ifdef MODIFY_CORRECTION_CHECK
  7028. if (
  7029. (Charger.memory.EVSE_Config.data.item.Correction_VL1[1][MCU_DATA] > Charger.memory.EVSE_Config.data.item.Correction_VL1[2][MCU_DATA]) &&
  7030. (Charger.memory.EVSE_Config.data.item.Correction_VL1[1][METER_DATA] > Charger.memory.EVSE_Config.data.item.Correction_VL1[2][METER_DATA])
  7031. )
  7032. #else
  7033. if(Charger.memory.EVSE_Config.data.item.Correction_VL1[1][MCU_DATA] > Charger.memory.EVSE_Config.data.item.Correction_VL1[2][MCU_DATA])
  7034. #endif
  7035. {
  7036. CLC_Corr_Gain_Par( Charger.memory.EVSE_Config.data.item.Correction_VL1[1][METER_DATA],Charger.memory.EVSE_Config.data.item.Correction_VL1[2][METER_DATA] ,
  7037. Charger.memory.EVSE_Config.data.item.Correction_VL1[1][MCU_DATA],Charger.memory.EVSE_Config.data.item.Correction_VL1[2][MCU_DATA] ,
  7038. &Charger.coefficient.gain_v[0], &Charger.coefficient.offset_v[0]);
  7039. }
  7040. else
  7041. {
  7042. Charger.coefficient.gain_v[0] = 1;
  7043. Charger.coefficient.offset_v[0] = 0;
  7044. }
  7045. //CT
  7046. #ifdef MODIFY_CORRECTION_CHECK
  7047. if (
  7048. (Charger.memory.EVSE_Config.data.item.Correction_CL1[1][MCU_DATA] > Charger.memory.EVSE_Config.data.item.Correction_CL1[2][MCU_DATA]) &&
  7049. (Charger.memory.EVSE_Config.data.item.Correction_CL1[1][METER_DATA] > Charger.memory.EVSE_Config.data.item.Correction_CL1[2][METER_DATA])
  7050. )
  7051. #else
  7052. if(Charger.memory.EVSE_Config.data.item.Correction_CL1[1][MCU_DATA] > Charger.memory.EVSE_Config.data.item.Correction_CL1[2][MCU_DATA])
  7053. #endif
  7054. {
  7055. CLC_Corr_Gain_Par(Charger.memory.EVSE_Config.data.item.Correction_CL1[1][METER_DATA],Charger.memory.EVSE_Config.data.item.Correction_CL1[2][METER_DATA] ,
  7056. Charger.memory.EVSE_Config.data.item.Correction_CL1[1][MCU_DATA],Charger.memory.EVSE_Config.data.item.Correction_CL1[2][MCU_DATA] ,
  7057. &Charger.coefficient.gain_c[0], &Charger.coefficient.offset_c[0]);
  7058. }
  7059. else
  7060. {
  7061. Charger.coefficient.gain_c[0] = 1;
  7062. Charger.coefficient.offset_c[0] = 0;
  7063. }
  7064. //LEAK
  7065. if(Charger.memory.EVSE_Config.data.item.Correction_Leak[1][MCU_DATA] > Charger.memory.EVSE_Config.data.item.Correction_Leak[2][MCU_DATA])
  7066. {
  7067. CLC_Corr_Gain_Par(Charger.memory.EVSE_Config.data.item.Correction_Leak[1][METER_DATA],Charger.memory.EVSE_Config.data.item.Correction_Leak[2][METER_DATA] ,
  7068. Charger.memory.EVSE_Config.data.item.Correction_Leak[1][MCU_DATA],Charger.memory.EVSE_Config.data.item.Correction_Leak[2][MCU_DATA] ,
  7069. &Charger.coefficient.gain_leak[0], &Charger.coefficient.offset_leak[0]);
  7070. }
  7071. else
  7072. {
  7073. Charger.coefficient.gain_leak[0] = 1;
  7074. Charger.coefficient.offset_leak[0] = 0;
  7075. }
  7076. DEBUG_INFO("Charger.coefficient.gain_leak[0] = %f\r\n",Charger.coefficient.gain_leak[0]);
  7077. DEBUG_INFO("Charger.coefficient.offset_leak[0] = %f\r\n",Charger.coefficient.offset_leak[0]);
  7078. //GMI
  7079. if(Charger.memory.EVSE_Config.data.item.Correction_GMI[1][MCU_DATA] > Charger.memory.EVSE_Config.data.item.Correction_GMI[2][MCU_DATA])
  7080. {
  7081. CLC_Corr_Gain_Par2(Charger.memory.EVSE_Config.data.item.Correction_GMI[1][METER_DATA],Charger.memory.EVSE_Config.data.item.Correction_GMI[2][METER_DATA] ,
  7082. Charger.memory.EVSE_Config.data.item.Correction_GMI[1][MCU_DATA],Charger.memory.EVSE_Config.data.item.Correction_GMI[2][MCU_DATA] ,
  7083. &Charger.coefficient.gain_gmi[0], &Charger.coefficient.offset_gmi[0], &Charger.coefficient.offsetisPosOrNeg_gmi[0]);
  7084. }
  7085. else
  7086. {
  7087. Charger.coefficient.gain_gmi[0] = 1;
  7088. Charger.coefficient.offset_gmi[0] = 0;
  7089. }
  7090. //DEBUG_INFO("Charger.coefficient.gain_gmi[0] = %f\r\n",Charger.coefficient.gain_gmi[0]);
  7091. //DEBUG_INFO("Charger.coefficient.offset_gmi[0] = %f\r\n",Charger.coefficient.offset_gmi[0]);
  7092. #ifdef FUNC_SHOW_CORRECTION_DATA
  7093. {
  7094. XP("[CORRECT DATA]======================\r\n");
  7095. XP("<Correction_VL1>\r\n");
  7096. for (int i = 0; i < 4; i++)
  7097. {
  7098. XP("[%d][0] = 0x%04X, [%d][1] = 0x%04X\r\n",
  7099. i, Charger.memory.EVSE_Config.data.item.Correction_VL1[i][0],
  7100. i, Charger.memory.EVSE_Config.data.item.Correction_VL1[i][1]);
  7101. }
  7102. XP("<Correction_VL2>\r\n");
  7103. for (int i = 0; i < 2; i++)
  7104. {
  7105. XP("[%d][0] = 0x%04X, [%d][1] = 0x%04X\r\n",
  7106. i, Charger.memory.EVSE_Config.data.item.Correction_VL2[i][0],
  7107. i, Charger.memory.EVSE_Config.data.item.Correction_VL2[i][1]);
  7108. }
  7109. XP("<Correction_VL3>\r\n");
  7110. for (int i = 0; i < 2; i++)
  7111. {
  7112. XP("[%d][0] = 0x%04X, [%d][1] = 0x%04X\r\n",
  7113. i, Charger.memory.EVSE_Config.data.item.Correction_VL3[i][0],
  7114. i, Charger.memory.EVSE_Config.data.item.Correction_VL3[i][1]);
  7115. }
  7116. XP("<Correction_CL1>\r\n");
  7117. for (int i = 0; i < 4; i++)
  7118. {
  7119. XP("[%d][0] = 0x%04X, [%d][1] = 0x%04X\r\n",
  7120. i, Charger.memory.EVSE_Config.data.item.Correction_CL1[i][0],
  7121. i, Charger.memory.EVSE_Config.data.item.Correction_CL1[i][1]);
  7122. }
  7123. XP("<Correction_CL2>\r\n");
  7124. for (int i = 0; i < 2; i++)
  7125. {
  7126. XP("[%d][0] = 0x%04X, [%d][1] = 0x%04X\r\n",
  7127. i, Charger.memory.EVSE_Config.data.item.Correction_CL2[i][0],
  7128. i, Charger.memory.EVSE_Config.data.item.Correction_CL2[i][1]);
  7129. }
  7130. XP("<Correction_CL3>\r\n");
  7131. for (int i = 0; i < 2; i++)
  7132. {
  7133. XP("[%d][0] = 0x%04X, [%d][1] = 0x%04X\r\n",
  7134. i, Charger.memory.EVSE_Config.data.item.Correction_CL3[i][0],
  7135. i, Charger.memory.EVSE_Config.data.item.Correction_CL3[i][1]);
  7136. }
  7137. XP("<Correction_Leak>\r\n");
  7138. for (int i = 0; i < 4; i++)
  7139. {
  7140. XP("[%d][0] = 0x%04X, [%d][1] = 0x%04X\r\n",
  7141. i, Charger.memory.EVSE_Config.data.item.Correction_Leak[i][0],
  7142. i, Charger.memory.EVSE_Config.data.item.Correction_Leak[i][1]);
  7143. }
  7144. XP("<Correction_GMI>\r\n");
  7145. for (int i = 0; i < 4; i++)
  7146. {
  7147. XP("[%d][0] = 0x%04X, [%d][1] = 0x%04X\r\n",
  7148. i, Charger.memory.EVSE_Config.data.item.Correction_GMI[i][0],
  7149. i, Charger.memory.EVSE_Config.data.item.Correction_GMI[i][1]);
  7150. }
  7151. XP("====================================\r\n");
  7152. }
  7153. #endif //FUNC_SHOW_CORRECTION_DATA
  7154. //DEBUG_INFO("Read MEM_ADDR_EVSE_CONFIG block(4k byte) pass.\r\n");
  7155. // Check MCU control mode depend on GPIO state ~again~ , Prevent W25Qx from being empty(0xFF)
  7156. if(HAL_GPIO_ReadPin(IN_ACT_REQ_GPIO_Port, IN_ACT_REQ_Pin) == GPIO_PIN_RESET)
  7157. {
  7158. Charger.memory.EVSE_Config.data.item.MCU_Control_Mode = MCU_CONTROL_MODE_CSU;
  7159. DEBUG_INFO("Check MCU_Control_Mode is CSU in memmeory task\r\n");
  7160. }
  7161. else
  7162. {
  7163. Charger.memory.EVSE_Config.data.item.MCU_Control_Mode = MCU_CONTROL_MODE_NO_CSU;
  7164. if (Charger.memory.EVSE_Config.data.item.AuthMode > AUTH_MODE_FREE)
  7165. {
  7166. Charger.memory.EVSE_Config.data.item.AuthMode = AUTH_MODE_FREE ;
  7167. DEBUG_INFO("AuthMode is defualt AUTH_MODE_FREE \r\n");
  7168. }
  7169. if (Charger.memory.EVSE_Config.data.item.OfflinePolicy > RFID_USER_AUTH_NO_CHARGING)
  7170. {
  7171. Charger.memory.EVSE_Config.data.item.OfflinePolicy = RFID_USER_AUTH_FREE ;
  7172. DEBUG_INFO("OfflinePolicy is defualt RFID_USER_AUTH_FREE \r\n");
  7173. }
  7174. DEBUG_INFO("Check MCU_Control_Mode is NO NO NO CSU in memmeory task\r\n");
  7175. }
  7176. // Clean update & clear flag when model name not configured
  7177. /*
  7178. if(strlen(Charger.memory.EVSE_Config.data.item.ModelName)>20)
  7179. {
  7180. Charger.memory.EVSE_Config.op_bits.update = OFF;
  7181. Charger.memory.EVSE_Config.op_bits.clear = OFF;
  7182. Charger.memory.EVSE_Config.op_bits.backup = OFF;
  7183. Charger.memory.EVSE_Config.op_bits.restore = OFF;
  7184. }
  7185. */
  7186. parseVersionInfoFromModelname();
  7187. }
  7188. #endif //MODIFY_FLASH_OPERATION_20241104
  7189. /* USER CODE BEGIN Header_StartMemoryTask */
  7190. /**
  7191. * @brief Function implementing the memoryTask thread.
  7192. * @param argument: Not used
  7193. * @retval None
  7194. */
  7195. /* USER CODE END Header_StartMemoryTask */
  7196. void StartMemoryTask(void const * argument)
  7197. {
  7198. /* USER CODE BEGIN StartMemoryTask */
  7199. //mmemory
  7200. //uint8_t tmpBuf[64];
  7201. uint8_t isSuccess;
  7202. uint8_t ID[2];
  7203. BSP_W25Qx_Init();
  7204. BSP_W25Qx_Read_ID(ID);
  7205. DEBUG_INFO("W25Qxxx ID is : 0x%02X 0x%02X \r\n",ID[0],ID[1]);
  7206. Charger.memory.EVSE_Config.op_bits.read = ON;
  7207. Charger.memory.coldLoadPickUp.op_bits.read = ON;
  7208. Charger.memory.whiteList.op_bits.read = ON;
  7209. Charger.memory.hisAlarm.op_bits.read = ON;
  7210. Charger.memory.hisCharging.op_bits.read = ON;
  7211. /*
  7212. Charger.memory.EVSE_Config.op_bits.clear = ON ;
  7213. Charger.memory.EVSE_Config.op_bits.clear = ON;
  7214. Charger.memory.coldLoadPickUp.op_bits.clear = ON;
  7215. Charger.memory.whiteList.op_bits.clear = ON;
  7216. Charger.memory.hisAlarm.op_bits.clear = ON;
  7217. Charger.memory.hisCharging.op_bits.clear = ON;
  7218. */
  7219. /* Infinite loop */
  7220. for(;;)
  7221. {
  7222. /*
  7223. EVSE config operation
  7224. */
  7225. if(Charger.memory.EVSE_Config.op_bits.read)
  7226. {
  7227. #ifdef FUNC_CALC_FLASH_CRC_IN_CRITICAL
  7228. taskENTER_CRITICAL();
  7229. #endif
  7230. #ifndef FUNC_CALC_FLASH_CRC_IN_CRITICAL
  7231. osDelay(100);
  7232. #endif
  7233. // Read data from block
  7234. if(BSP_W25Qx_Read(Charger.memory.EVSE_Config.data.value, MEM_ADDR_EVSE_CONFIG, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7235. {
  7236. #ifdef FUNC_FLASH_RESTORE_CONFIG_WHEN_PRIMARY_LOSS
  7237. #ifdef FUNC_FLASH_CONFIG_APPEND_CRC
  7238. u32 crc32 = HAL_CRC_Calculate(&hcrc, (u32*)Charger.memory.EVSE_Config.data.verify.buf, ((W25Q16FV_BLOCK_SIZE-4)>>2));
  7239. if ((Charger.memory.EVSE_Config.data.verify.crc32 == crc32 /*|| Charger.memory.EVSE_Config.data.verify.crc32 == HTK_U32_MAX*/))
  7240. #else
  7241. if (1)
  7242. #endif
  7243. {
  7244. DEBUG_INFO("Read MEM_ADDR_EVSE_CONFIG block(4k byte) pass.\r\n");
  7245. Proc_EVSE_Config();
  7246. Charger.ModelReadisOK = PASS;
  7247. }
  7248. else
  7249. {
  7250. static u8 bProc = 0;
  7251. if (!bProc)
  7252. {
  7253. bProc = 1;
  7254. DEBUG_WARN("MEM_ADDR_EVSE_CONFIG data area check sum error => restore from backup area.\r\n");
  7255. Charger.memory.EVSE_Config.op_bits.restore = ON;
  7256. }
  7257. else
  7258. {
  7259. Proc_EVSE_Config();
  7260. Charger.ModelReadisOK = PASS;
  7261. }
  7262. }
  7263. #endif //FUNC_FLASH_RESTORE_CONFIG_WHEN_PRIMARY_LOSS
  7264. Charger.memory.EVSE_Config.op_bits.read = OFF;
  7265. }
  7266. else
  7267. {
  7268. DEBUG_INFO("Read MEM_ADDR_EVSE_CONFIG block(4k byte) fail.\r\n");
  7269. }
  7270. #ifdef FUNC_CALC_FLASH_CRC_IN_CRITICAL
  7271. taskEXIT_CRITICAL();
  7272. #endif
  7273. }
  7274. if(Charger.memory.EVSE_Config.op_bits.update)
  7275. {
  7276. #ifdef FUNC_CALC_FLASH_CRC_IN_CRITICAL
  7277. taskENTER_CRITICAL();
  7278. #endif
  7279. #ifdef FUNC_AUTO_MODIFY_CCID_MODULE
  7280. Charger.memory.EVSE_Config.op_bits.update = OFF;
  7281. #endif
  7282. isSuccess = PASS;
  7283. #ifdef FUNC_FLASH_CONFIG_APPEND_CRC
  7284. #ifndef FUNC_CALC_FLASH_CRC_IN_CRITICAL
  7285. osDelay(100);
  7286. #endif
  7287. Charger.memory.EVSE_Config.data.verify.crc32 = HAL_CRC_Calculate(&hcrc, (u32*)Charger.memory.EVSE_Config.data.verify.buf, ((W25Q16FV_BLOCK_SIZE-4)>>2));
  7288. #endif
  7289. // Erase block data
  7290. if(BSP_W25Qx_Erase_Block(MEM_ADDR_EVSE_CONFIG) == W25Qx_OK)
  7291. {
  7292. // Write data to block
  7293. for(uint16_t idx=0;idx<((W25Q16FV_BLOCK_SIZE)>>8);idx++)
  7294. {
  7295. if(BSP_W25Qx_Write(&Charger.memory.EVSE_Config.data.value[idx*W25Q16FV_PAGE_SIZE], MEM_ADDR_EVSE_CONFIG+(idx*W25Q16FV_PAGE_SIZE), W25Q16FV_PAGE_SIZE) != W25Qx_OK)
  7296. {
  7297. isSuccess = FAIL;
  7298. }
  7299. }
  7300. if(isSuccess)
  7301. DEBUG_INFO("Update MEM_ADDR_EVSE_CONFIG success.\r\n");
  7302. else
  7303. DEBUG_WARN("Update MEM_ADDR_EVSE_CONFIG fail.\r\n");
  7304. #ifndef FUNC_AUTO_MODIFY_CCID_MODULE
  7305. Charger.memory.EVSE_Config.op_bits.update = OFF;
  7306. #endif
  7307. #ifdef FUNC_FLASH_UPDATE_CONFIG_BOTH_PRIMARY_AND_BACKUP
  7308. Charger.memory.EVSE_Config.op_bits.backup = ON;
  7309. #endif
  7310. }
  7311. else
  7312. {
  7313. DEBUG_WARN("Update MEM_ADDR_EVSE_CONFIG block(4k byte) fail.\r\n");
  7314. }
  7315. #ifdef FUNC_CALC_FLASH_CRC_IN_CRITICAL
  7316. taskEXIT_CRITICAL();
  7317. #endif
  7318. }
  7319. if(Charger.memory.EVSE_Config.op_bits.clear)
  7320. {
  7321. //***************
  7322. // Erase backup block data
  7323. if(BSP_W25Qx_Erase_Block(MEM_ADDR_BACKUP_EVSE_CONFIG) == W25Qx_OK)
  7324. {
  7325. // Read data from block
  7326. if(BSP_W25Qx_Read(Charger.memory.EVSE_Config.data.value, MEM_ADDR_BACKUP_EVSE_CONFIG, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7327. {
  7328. DEBUG_INFO("Erase MEM_ADDR_BACKUP_EVSE_CONFIG block(4k byte) pass.\r\n");
  7329. Charger.memory.EVSE_Config.op_bits.clear = OFF;
  7330. }
  7331. else
  7332. {
  7333. DEBUG_WARN("Erase MEM_ADDR_BACKUP_EVSE_CONFIG block(4k byte) fail.\r\n");
  7334. }
  7335. }
  7336. else
  7337. {
  7338. DEBUG_WARN("Erase MEM_ADDR_BACKUP_EVSE_CONFIG block(4k byte) fail.\r\n");
  7339. }
  7340. //***************
  7341. // Erase block data
  7342. if(BSP_W25Qx_Erase_Block(MEM_ADDR_EVSE_CONFIG) == W25Qx_OK)
  7343. {
  7344. // Read data from block
  7345. if(BSP_W25Qx_Read(Charger.memory.EVSE_Config.data.value, MEM_ADDR_EVSE_CONFIG, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7346. {
  7347. DEBUG_INFO("Erase MEM_ADDR_EVSE_CONFIG block(4k byte) pass.\r\n");
  7348. Charger.memory.EVSE_Config.op_bits.clear = OFF;
  7349. }
  7350. else
  7351. {
  7352. DEBUG_WARN("Erase MEM_ADDR_EVSE_CONFIG block(4k byte) fail.\r\n");
  7353. }
  7354. }
  7355. else
  7356. {
  7357. DEBUG_WARN("Erase MEM_ADDR_EVSE_CONFIG block(4k byte) fail.\r\n");
  7358. }
  7359. }
  7360. if(Charger.memory.EVSE_Config.op_bits.backup)
  7361. {
  7362. #ifdef FUNC_CALC_FLASH_CRC_IN_CRITICAL
  7363. taskENTER_CRITICAL();
  7364. #endif
  7365. isSuccess = PASS;
  7366. #ifndef FUNC_CALC_FLASH_CRC_IN_CRITICAL
  7367. osDelay(100);
  7368. #endif
  7369. Charger.memory.EVSE_Config.data.verify.crc32 = HAL_CRC_Calculate(&hcrc, (u32*)Charger.memory.EVSE_Config.data.verify.buf, ((W25Q16FV_BLOCK_SIZE-4)>>2));
  7370. // Erase block data
  7371. if(BSP_W25Qx_Erase_Block(MEM_ADDR_BACKUP_EVSE_CONFIG) == W25Qx_OK)
  7372. {
  7373. // Write data to block
  7374. for(uint16_t idx=0;idx<((W25Q16FV_BLOCK_SIZE)>>8);idx++)
  7375. {
  7376. if(BSP_W25Qx_Write(&Charger.memory.EVSE_Config.data.value[idx*W25Q16FV_PAGE_SIZE], MEM_ADDR_BACKUP_EVSE_CONFIG+(idx*W25Q16FV_PAGE_SIZE), W25Q16FV_PAGE_SIZE) != W25Qx_OK)
  7377. {
  7378. isSuccess = FAIL;
  7379. }
  7380. }
  7381. if(isSuccess)
  7382. DEBUG_INFO("Backup MEM_ADDR_BACKUP_EVSE_CONFIG success.\r\n");
  7383. else
  7384. DEBUG_WARN("Backup MEM_ADDR_BACKUP_EVSE_CONFIG fail.\r\n");
  7385. Charger.memory.EVSE_Config.op_bits.backup = OFF;
  7386. #ifndef FUNC_FLASH_UPDATE_CONFIG_BOTH_PRIMARY_AND_BACKUP
  7387. //Charger.memory.EVSE_Config.op_bits.clear = ON;
  7388. #endif
  7389. }
  7390. else
  7391. {
  7392. DEBUG_WARN("Backup MEM_ADDR_BACKUP_EVSE_CONFIG block(4k byte) fail.\r\n");
  7393. }
  7394. #ifdef FUNC_CALC_FLASH_CRC_IN_CRITICAL
  7395. taskEXIT_CRITICAL();
  7396. #endif
  7397. }
  7398. if(Charger.memory.EVSE_Config.op_bits.restore)
  7399. {
  7400. // Restore data from backup block
  7401. if(BSP_W25Qx_Read(Charger.memory.EVSE_Config.data.value, MEM_ADDR_BACKUP_EVSE_CONFIG, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7402. {
  7403. u32 crc32 = HAL_CRC_Calculate(&hcrc, (u32*)Charger.memory.EVSE_Config.data.verify.buf, ((W25Q16FV_BLOCK_SIZE-4)>>2));
  7404. if(Charger.memory.EVSE_Config.data.verify.crc32 == crc32)
  7405. {
  7406. DEBUG_INFO("Restore MEM_ADDR_BACKUP_EVSE_CONFIG block(4k byte) pass.\r\n");
  7407. // Update data to primary area
  7408. Charger.memory.EVSE_Config.op_bits.update = ON;
  7409. Proc_EVSE_Config();
  7410. Charger.ModelReadisOK = PASS;
  7411. }
  7412. else
  7413. {
  7414. DEBUG_WARN("[FLASH] MEM_ADDR_BACKUP_EVSE_CONFIG block check sum error.\r\n");
  7415. // TODO:
  7416. static u8 bProc = 0;
  7417. if (!bProc)
  7418. {
  7419. bProc = 1;
  7420. setLedMotion(LED_ACTION_ALARM);
  7421. Charger.memory.EVSE_Config.op_bits.read = ON;
  7422. }
  7423. }
  7424. Charger.memory.EVSE_Config.op_bits.restore = OFF;
  7425. }
  7426. else
  7427. {
  7428. DEBUG_WARN("Restore MEM_ADDR_BACKUP_EVSE_CONFIG block(4k byte) fail.\r\n");
  7429. }
  7430. }
  7431. #ifdef FUNC_TEST_SIMU_LOAD_BUTTON
  7432. {
  7433. static int buttonDownCount = 0;
  7434. static HTK_BOOL bExec = HTK_FALSE;
  7435. if((HAL_GPIO_ReadPin(IN_MEM_Erase_GPIO_Port, IN_MEM_Erase_Pin) == GPIO_PIN_RESET))
  7436. {
  7437. if (buttonDownCount > 100 && !bExec)
  7438. {
  7439. bExec = HTK_TRUE;
  7440. Charger.m_bEnableSimuChargeCurrent = !Charger.m_bEnableSimuChargeCurrent;
  7441. XP("\r\n\r\n#BUTTON:ParameterSimuCurr: %s\r\n\r\n\r\n", Charger.m_bEnableSimuChargeCurrent ? "ON" : "OFF");
  7442. }
  7443. else
  7444. {
  7445. buttonDownCount++;
  7446. }
  7447. }
  7448. else
  7449. {
  7450. bExec = HTK_FALSE;
  7451. buttonDownCount = 0;
  7452. }
  7453. }
  7454. #else //FUNC_TEST_SIMU_LOAD_BUTTON
  7455. if((HAL_GPIO_ReadPin(IN_MEM_Erase_GPIO_Port, IN_MEM_Erase_Pin) == GPIO_PIN_RESET) && (Charger.Mode == MODE_IDLE) )
  7456. {
  7457. //prevent idiot to pree this button too long to cause watchdog reset
  7458. HAL_IWDG_Refresh(&hiwdg);
  7459. if(Charger.counter.RST_CFG>3000)
  7460. {
  7461. // Reset EVSE configuration to factory default value
  7462. Charger.memory.EVSE_Config.data.item.bleConfig.isGenPin = OFF;
  7463. Charger.memory.EVSE_Config.data.item.bleConfig.isRegPuk = OFF;
  7464. Charger.memory.EVSE_Config.data.item.AuthMode = AUTH_MODE_OCPP;
  7465. Charger.memory.EVSE_Config.data.item.RFID_SN_Endian = RFID_ENDIAN_LITTLE;
  7466. //Charger.memory.EVSE_Config.data.item.isAuthEnable = OFF;
  7467. Charger.memory.EVSE_Config.data.item.OfflinePolicy = RFID_USER_AUTH_FREE ;
  7468. //Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative = 0;
  7469. Charger.memory.EVSE_Config.data.item.reservation.mode = RESERVATION_DISABLE;
  7470. Charger.memory.EVSE_Config.op_bits.update = ON;
  7471. // Reset cold load pick up to factory default value
  7472. Charger.memory.coldLoadPickUp.data.item.isEnable = OFF;
  7473. Charger.memory.coldLoadPickUp.op_bits.update = ON;
  7474. // Reset white list to factory default value
  7475. for(uint8_t idx=0;idx<20;idx++)
  7476. {
  7477. Charger.memory.whiteList.data.item[idx].listType = 0xff;
  7478. Charger.memory.whiteList.data.item[idx].isReg = OFF;
  7479. memset(&Charger.memory.whiteList.data.item[idx].pin[0], 0x00, 6);
  7480. memset(&Charger.memory.whiteList.data.item[idx].listID[0], 0x00, 6);
  7481. }
  7482. Charger.memory.whiteList.op_bits.update = ON;
  7483. //restore led
  7484. setLedMotion(LED_ACTION_RESTORE_SETTING) ;
  7485. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  7486. setChargerMode(MODE_IDLE);
  7487. osDelay(1000);
  7488. }
  7489. else
  7490. Charger.counter.RST_CFG++;
  7491. }
  7492. else
  7493. Charger.counter.RST_CFG = 0;
  7494. #endif //FUNC_TEST_SIMU_LOAD_BUTTON
  7495. /*
  7496. Cold load pick up operation
  7497. */
  7498. if(Charger.memory.coldLoadPickUp.op_bits.read)
  7499. {
  7500. osDelay(100);
  7501. // Read data from block
  7502. if(BSP_W25Qx_Read(Charger.memory.coldLoadPickUp.data.value, MEM_ADDR_COLD_LOAD_PICKUP, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7503. {
  7504. DEBUG_INFO("Read MEM_ADDR_COLD_LOAD_PICKUP block(4k byte) pass.\r\n");
  7505. Charger.memory.coldLoadPickUp.op_bits.read = OFF;
  7506. }
  7507. else
  7508. {
  7509. DEBUG_WARN("Read MEM_ADDR_COLD_LOAD_PICKUP block(4k byte) fail.\r\n");
  7510. }
  7511. }
  7512. if(Charger.memory.coldLoadPickUp.op_bits.update)
  7513. {
  7514. isSuccess = PASS;
  7515. // Erase block data
  7516. if(BSP_W25Qx_Erase_Block(MEM_ADDR_COLD_LOAD_PICKUP) == W25Qx_OK)
  7517. {
  7518. // Write data to block
  7519. for(uint16_t idx=0;idx<((W25Q16FV_BLOCK_SIZE)>>8);idx++)
  7520. {
  7521. if(BSP_W25Qx_Write(&Charger.memory.coldLoadPickUp.data.value[idx*W25Q16FV_PAGE_SIZE], MEM_ADDR_COLD_LOAD_PICKUP+(idx*W25Q16FV_PAGE_SIZE), W25Q16FV_PAGE_SIZE) != W25Qx_OK)
  7522. {
  7523. isSuccess = FAIL;
  7524. }
  7525. }
  7526. if(isSuccess)
  7527. DEBUG_INFO("Update MEM_ADDR_COLD_LOAD_PICKUP success.\r\n");
  7528. else
  7529. DEBUG_WARN("Update MEM_ADDR_COLD_LOAD_PICKUP fail.\r\n");
  7530. Charger.memory.coldLoadPickUp.op_bits.update = OFF;
  7531. }
  7532. else
  7533. {
  7534. DEBUG_WARN("Update MEM_ADDR_COLD_LOAD_PICKUP block(4k byte) fail.\r\n");
  7535. }
  7536. }
  7537. if(Charger.memory.coldLoadPickUp.op_bits.clear)
  7538. {
  7539. // Erase block data
  7540. if(BSP_W25Qx_Erase_Block(MEM_ADDR_COLD_LOAD_PICKUP) == W25Qx_OK)
  7541. {
  7542. // Read data from block
  7543. if(BSP_W25Qx_Read(Charger.memory.coldLoadPickUp.data.value, MEM_ADDR_COLD_LOAD_PICKUP, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7544. {
  7545. DEBUG_INFO("Erase MEM_ADDR_COLD_LOAD_PICKUP block(4k byte) pass.\r\n");
  7546. Charger.memory.coldLoadPickUp.op_bits.clear = OFF;
  7547. }
  7548. else
  7549. {
  7550. DEBUG_WARN("Erase MEM_ADDR_COLD_LOAD_PICKUP block(4k byte) fail.\r\n");
  7551. }
  7552. }
  7553. else
  7554. {
  7555. DEBUG_WARN("Erase MEM_ADDR_COLD_LOAD_PICKUP block(4k byte) fail.\r\n");
  7556. }
  7557. }
  7558. if(Charger.memory.coldLoadPickUp.op_bits.backup)
  7559. {
  7560. isSuccess = PASS;
  7561. // Erase block data
  7562. if(BSP_W25Qx_Erase_Block(MEM_ADDR_BACKUP_COLD_LOAD_PICKUP) == W25Qx_OK)
  7563. {
  7564. // Write data to block
  7565. for(uint16_t idx=0;idx<((W25Q16FV_BLOCK_SIZE)>>8);idx++)
  7566. {
  7567. if(BSP_W25Qx_Write(&Charger.memory.coldLoadPickUp.data.value[idx*W25Q16FV_PAGE_SIZE], MEM_ADDR_BACKUP_COLD_LOAD_PICKUP+(idx*W25Q16FV_PAGE_SIZE), W25Q16FV_PAGE_SIZE) != W25Qx_OK)
  7568. {
  7569. isSuccess = FAIL;
  7570. }
  7571. }
  7572. if(isSuccess)
  7573. DEBUG_INFO("Backup MEM_ADDR_BACKUP_COLD_LOAD_PICKUP success.\r\n");
  7574. else
  7575. DEBUG_WARN("Backup MEM_ADDR_BACKUP_COLD_LOAD_PICKUP fail.\r\n");
  7576. Charger.memory.coldLoadPickUp.op_bits.backup = OFF;
  7577. Charger.memory.coldLoadPickUp.op_bits.clear = ON;
  7578. }
  7579. else
  7580. {
  7581. DEBUG_WARN("Backup MEM_ADDR_BACKUP_COLD_LOAD_PICKUP block(4k byte) fail.\r\n");
  7582. }
  7583. }
  7584. if(Charger.memory.coldLoadPickUp.op_bits.restore)
  7585. {
  7586. // Restore data from backup block
  7587. if(BSP_W25Qx_Read(Charger.memory.coldLoadPickUp.data.value, MEM_ADDR_BACKUP_COLD_LOAD_PICKUP, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7588. {
  7589. DEBUG_INFO("Restore MEM_ADDR_BACKUP_COLD_LOAD_PICKUP block(4k byte) pass.\r\n");
  7590. Charger.memory.coldLoadPickUp.op_bits.restore = OFF;
  7591. }
  7592. else
  7593. {
  7594. DEBUG_WARN("Restore MEM_ADDR_BACKUP_COLD_LOAD_PICKUP block(4k byte) fail.\r\n");
  7595. }
  7596. }
  7597. /*
  7598. White list operation
  7599. */
  7600. if(Charger.memory.whiteList.op_bits.read)
  7601. {
  7602. osDelay(100);
  7603. // Read data from block
  7604. if(BSP_W25Qx_Read(Charger.memory.whiteList.data.value, MEM_ADDR_WHITE_LIST, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7605. {
  7606. DEBUG_INFO("Read MEM_ADDR_WHITE_LIST block(4k byte) pass.\r\n");
  7607. Charger.memory.whiteList.op_bits.read = OFF;
  7608. }
  7609. else
  7610. {
  7611. DEBUG_WARN("Read MEM_ADDR_WHITE_LIST block(4k byte) fail.\r\n");
  7612. }
  7613. }
  7614. if(Charger.memory.whiteList.op_bits.update)
  7615. {
  7616. isSuccess = PASS;
  7617. // Erase block data
  7618. if(BSP_W25Qx_Erase_Block(MEM_ADDR_WHITE_LIST) == W25Qx_OK)
  7619. {
  7620. // Write data to block
  7621. for(uint16_t idx=0;idx<((W25Q16FV_BLOCK_SIZE)>>8);idx++)
  7622. {
  7623. if(BSP_W25Qx_Write(&Charger.memory.whiteList.data.value[idx*W25Q16FV_PAGE_SIZE], MEM_ADDR_WHITE_LIST+(idx*W25Q16FV_PAGE_SIZE), W25Q16FV_PAGE_SIZE) != W25Qx_OK)
  7624. {
  7625. isSuccess = FAIL;
  7626. }
  7627. }
  7628. if(isSuccess)
  7629. DEBUG_INFO("Update MEM_ADDR_WHITE_LIST success.\r\n");
  7630. else
  7631. DEBUG_WARN("Update MEM_ADDR_WHITE_LIST fail.\r\n");
  7632. Charger.memory.whiteList.op_bits.update = OFF;
  7633. }
  7634. else
  7635. {
  7636. DEBUG_WARN("Update MEM_ADDR_WHITE_LIST block(4k byte) fail.\r\n");
  7637. }
  7638. }
  7639. if(Charger.memory.whiteList.op_bits.clear)
  7640. {
  7641. // Erase block data
  7642. if(BSP_W25Qx_Erase_Block(MEM_ADDR_WHITE_LIST) == W25Qx_OK)
  7643. {
  7644. // Read data from block
  7645. if(BSP_W25Qx_Read(Charger.memory.whiteList.data.value, MEM_ADDR_WHITE_LIST, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7646. {
  7647. DEBUG_INFO("Erase MEM_ADDR_WHITE_LIST block(4k byte) pass.\r\n");
  7648. Charger.memory.whiteList.op_bits.clear = OFF;
  7649. }
  7650. else
  7651. {
  7652. DEBUG_WARN("Erase MEM_ADDR_WHITE_LIST block(4k byte) fail.\r\n");
  7653. }
  7654. }
  7655. else
  7656. {
  7657. DEBUG_WARN("Erase MEM_ADDR_WHITE_LIST block(4k byte) fail.\r\n");
  7658. }
  7659. }
  7660. /*
  7661. Alarm history operation
  7662. */
  7663. if(Charger.memory.hisAlarm.op_bits.read)
  7664. {
  7665. osDelay(100);
  7666. // Read data from block
  7667. if(BSP_W25Qx_Read(Charger.memory.hisAlarm.data.value, MEM_ADDR_HIS_ALARM, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7668. {
  7669. DEBUG_INFO("Read MEM_ADDR_HIS_ALARM block(4k byte) pass.\r\n");
  7670. Charger.memory.hisAlarm.op_bits.read = OFF;
  7671. }
  7672. else
  7673. {
  7674. DEBUG_WARN("Read MEM_ADDR_HIS_ALARM block(4k byte) fail.\r\n");
  7675. }
  7676. }
  7677. if(Charger.memory.hisAlarm.op_bits.update)
  7678. {
  7679. isSuccess = PASS;
  7680. // Write data to block
  7681. for(uint16_t idx=0;idx<((W25Q16FV_BLOCK_SIZE)>>8);idx++)
  7682. {
  7683. if(BSP_W25Qx_Write(&Charger.memory.hisAlarm.data.value[idx*W25Q16FV_PAGE_SIZE], MEM_ADDR_HIS_ALARM+(idx*W25Q16FV_PAGE_SIZE), W25Q16FV_PAGE_SIZE) != W25Qx_OK)
  7684. {
  7685. isSuccess = FAIL;
  7686. }
  7687. }
  7688. if(isSuccess)
  7689. DEBUG_INFO("Update MEM_ADDR_HIS_ALARM success.\r\n");
  7690. else
  7691. DEBUG_WARN("Update MEM_ADDR_HIS_ALARM fail.\r\n");
  7692. Charger.memory.hisAlarm.op_bits.update = OFF;
  7693. }
  7694. if(Charger.memory.hisAlarm.op_bits.clear)
  7695. {
  7696. // Erase block data
  7697. if(BSP_W25Qx_Erase_Block(MEM_ADDR_HIS_ALARM) == W25Qx_OK)
  7698. {
  7699. // Read data from block
  7700. if(BSP_W25Qx_Read(Charger.memory.hisAlarm.data.value, MEM_ADDR_HIS_ALARM, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7701. {
  7702. DEBUG_INFO("Erase MEM_ADDR_HIS_ALARM block(4k byte) pass.\r\n");
  7703. Charger.memory.hisAlarm.op_bits.clear = OFF;
  7704. }
  7705. else
  7706. {
  7707. DEBUG_WARN("Erase MEM_ADDR_HIS_ALARM block(4k byte) fail.\r\n");
  7708. }
  7709. }
  7710. else
  7711. {
  7712. DEBUG_WARN("Erase MEM_ADDR_HIS_ALARM block(4k byte) fail.\r\n");
  7713. }
  7714. }
  7715. if(Charger.memory.hisAlarm.op_bits.backup)
  7716. {
  7717. isSuccess = PASS;
  7718. // Erase block data
  7719. if(BSP_W25Qx_Erase_Block(MEM_ADDR_BACKUP_HIS_ALARM) == W25Qx_OK)
  7720. {
  7721. // Write data to block
  7722. for(uint16_t idx=0;idx<((W25Q16FV_BLOCK_SIZE)>>8);idx++)
  7723. {
  7724. if(BSP_W25Qx_Write(&Charger.memory.hisAlarm.data.value[idx*W25Q16FV_PAGE_SIZE], MEM_ADDR_BACKUP_HIS_ALARM+(idx*W25Q16FV_PAGE_SIZE), W25Q16FV_PAGE_SIZE) != W25Qx_OK)
  7725. {
  7726. isSuccess = FAIL;
  7727. }
  7728. }
  7729. if(isSuccess)
  7730. DEBUG_INFO("Backup MEM_ADDR_BACKUP_HIS_ALARM success.\r\n");
  7731. else
  7732. DEBUG_WARN("Backup MEM_ADDR_BACKUP_HIS_ALARM fail.\r\n");
  7733. Charger.memory.hisAlarm.op_bits.backup = OFF;
  7734. Charger.memory.hisAlarm.op_bits.clear = ON;
  7735. }
  7736. else
  7737. {
  7738. DEBUG_WARN("Backup MEM_ADDR_BACKUP_HIS_ALARM block(4k byte) fail.\r\n");
  7739. }
  7740. }
  7741. if(Charger.memory.hisAlarm.op_bits.restore)
  7742. {
  7743. // Restore data from backup block
  7744. if(BSP_W25Qx_Read(Charger.memory.hisAlarm.data.value, MEM_ADDR_BACKUP_HIS_ALARM, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7745. {
  7746. DEBUG_INFO("Restore MEM_ADDR_BACKUP_HIS_ALARM block(4k byte) pass.\r\n");
  7747. Charger.memory.hisAlarm.op_bits.restore = OFF;
  7748. }
  7749. else
  7750. {
  7751. DEBUG_WARN("Restore MEM_ADDR_BACKUP_HIS_ALARM block(4k byte) fail.\r\n");
  7752. }
  7753. }
  7754. /*
  7755. Charging history operation
  7756. */
  7757. if(Charger.memory.hisCharging.op_bits.read)
  7758. {
  7759. osDelay(100);
  7760. // Read data from block
  7761. if(BSP_W25Qx_Read(Charger.memory.hisCharging.data.value, MEM_ADDR_HIS_CHARGING, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7762. {
  7763. DEBUG_INFO("Read MEM_ADDR_HIS_CHARGING block(4k byte) pass.\r\n");
  7764. Charger.memory.hisCharging.op_bits.read = OFF;
  7765. }
  7766. else
  7767. {
  7768. DEBUG_WARN("Read MEM_ADDR_HIS_CHARGING block(4k byte) fail.\r\n");
  7769. }
  7770. }
  7771. if(Charger.memory.hisCharging.op_bits.update)
  7772. {
  7773. isSuccess = PASS;
  7774. // Write data to block
  7775. for(uint16_t idx=0;idx<((W25Q16FV_BLOCK_SIZE)>>8);idx++)
  7776. {
  7777. if(BSP_W25Qx_Write(&Charger.memory.hisCharging.data.value[idx*W25Q16FV_PAGE_SIZE], MEM_ADDR_HIS_CHARGING+(idx*W25Q16FV_PAGE_SIZE), W25Q16FV_PAGE_SIZE) != W25Qx_OK)
  7778. {
  7779. isSuccess = FAIL;
  7780. }
  7781. }
  7782. if(isSuccess)
  7783. DEBUG_INFO("Update MEM_ADDR_HIS_CHARGING success.\r\n");
  7784. else
  7785. DEBUG_WARN("Update MEM_ADDR_HIS_CHARGING fail.\r\n");
  7786. Charger.memory.hisCharging.op_bits.update = OFF;
  7787. }
  7788. if(Charger.memory.hisCharging.op_bits.clear)
  7789. {
  7790. // Erase block data
  7791. if(BSP_W25Qx_Erase_Block(MEM_ADDR_HIS_CHARGING) == W25Qx_OK)
  7792. {
  7793. // Read data from block
  7794. if(BSP_W25Qx_Read(Charger.memory.hisCharging.data.value, MEM_ADDR_HIS_CHARGING, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7795. {
  7796. DEBUG_INFO("Erase MEM_ADDR_HIS_CHARGING block(4k byte) pass.\r\n");
  7797. Charger.memory.hisCharging.op_bits.clear = OFF;
  7798. }
  7799. else
  7800. {
  7801. DEBUG_WARN("Erase MEM_ADDR_HIS_CHARGING block(4k byte) fail.\r\n");
  7802. }
  7803. }
  7804. else
  7805. {
  7806. DEBUG_WARN("Erase MEM_ADDR_HIS_CHARGING block(4k byte) fail.\r\n");
  7807. }
  7808. }
  7809. if(Charger.memory.hisCharging.op_bits.backup)
  7810. {
  7811. isSuccess = PASS;
  7812. // Erase block data
  7813. if(BSP_W25Qx_Erase_Block(MEM_ADDR_BACKUP_HIS_CHARGING) == W25Qx_OK)
  7814. {
  7815. // Write data to block
  7816. for(uint16_t idx=0;idx<((W25Q16FV_BLOCK_SIZE)>>8);idx++)
  7817. {
  7818. if(BSP_W25Qx_Write(&Charger.memory.hisCharging.data.value[idx*W25Q16FV_PAGE_SIZE], MEM_ADDR_BACKUP_HIS_CHARGING+(idx*W25Q16FV_PAGE_SIZE), W25Q16FV_PAGE_SIZE) != W25Qx_OK)
  7819. {
  7820. isSuccess = FAIL;
  7821. }
  7822. }
  7823. if(isSuccess)
  7824. DEBUG_INFO("Backup MEM_ADDR_BACKUP_HIS_CHARGING success.\r\n");
  7825. else
  7826. DEBUG_WARN("Backup MEM_ADDR_BACKUP_HIS_CHARGING fail.\r\n");
  7827. Charger.memory.hisCharging.op_bits.backup = OFF;
  7828. Charger.memory.hisCharging.op_bits.clear = ON;
  7829. }
  7830. else
  7831. {
  7832. DEBUG_WARN("Backup MEM_ADDR_BACKUP_HIS_CHARGING block(4k byte) fail.\r\n");
  7833. }
  7834. }
  7835. if(Charger.memory.hisCharging.op_bits.restore)
  7836. {
  7837. // Restore data from backup block
  7838. if(BSP_W25Qx_Read(Charger.memory.hisCharging.data.value, MEM_ADDR_BACKUP_HIS_CHARGING, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7839. {
  7840. DEBUG_INFO("Restore MEM_ADDR_BACKUP_HIS_CHARGING block(4k byte) pass.\r\n");
  7841. Charger.memory.hisCharging.op_bits.restore = OFF;
  7842. }
  7843. else
  7844. {
  7845. DEBUG_WARN("Restore MEM_ADDR_BACKUP_HIS_CHARGING block(4k byte) fail.\r\n");
  7846. }
  7847. }
  7848. osDelay(1);
  7849. }
  7850. /* USER CODE END StartMemoryTask */
  7851. }
  7852. /* USER CODE BEGIN Header_StartWifiTask */
  7853. /**
  7854. * @brief Function implementing the wifiTask thread.
  7855. * @param argument: Not used
  7856. * @retval None
  7857. */
  7858. /* USER CODE END Header_StartWifiTask */
  7859. void StartWifiTask(void const * argument)
  7860. {
  7861. /* USER CODE BEGIN StartWifiTask */
  7862. /* Infinite loop */
  7863. char tmpBuf[128];
  7864. //uint8_t endFlag[4]={0x55,0xaa,0x55,0xaa};
  7865. __IO uint32_t flash;
  7866. //uint32_t checksum;
  7867. uint32_t rndNumber;
  7868. //=====================================
  7869. // Module reset
  7870. //=====================================
  7871. HAL_GPIO_WritePin(OUT_BLE_RESET_GPIO_Port, OUT_BLE_RESET_Pin, GPIO_PIN_RESET);
  7872. osDelay(60);
  7873. HAL_GPIO_WritePin(OUT_BLE_RESET_GPIO_Port, OUT_BLE_RESET_Pin, GPIO_PIN_SET);
  7874. HAL_GPIO_WritePin(OUT_BLE_DSR_GPIO_Port, OUT_BLE_DSR_Pin, GPIO_PIN_SET);
  7875. Charger.wifi.initSeq = 0;
  7876. Charger.wifi.isDataMode = OFF;
  7877. UART_WIFI_Init_OK = OFF;
  7878. osDelay(1000);
  7879. /* Infinite loop */
  7880. for(;;)
  7881. {
  7882. //=====================================
  7883. // WIFI module initialization process
  7884. //=====================================
  7885. switch(Charger.wifi.initSeq)
  7886. {
  7887. case 0x00:
  7888. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)WIFI_CMD[WIFI_CMD_SET_RESTORE], strlen(WIFI_CMD[WIFI_CMD_SET_RESTORE]), 0xffff);
  7889. osDelay(WIFI_INIT_DELAY);
  7890. break;
  7891. case 0x01:
  7892. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)WIFI_CMD[WIFI_CMD_SET_DTR], strlen(WIFI_CMD[WIFI_CMD_SET_DTR]), 0xffff);
  7893. osDelay(WIFI_INIT_DELAY);
  7894. break;
  7895. case 0x02:
  7896. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)WIFI_CMD[WIFI_CMD_SET_DSR], strlen(WIFI_CMD[WIFI_CMD_SET_DSR]), 0xffff);
  7897. osDelay(WIFI_INIT_DELAY);
  7898. break;
  7899. case 0x03:
  7900. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)WIFI_CMD[WIFI_CMD_GET_ADDR], strlen(WIFI_CMD[WIFI_CMD_GET_ADDR]), 0xffff);
  7901. osDelay(WIFI_INIT_DELAY);
  7902. break;
  7903. case 0x04:
  7904. sprintf(tmpBuf, "%s\"EVSE_AP_%sP\"\r\n", WIFI_CMD[WIFI_CMD_SET_SSID], Charger.wifi.module_mac);
  7905. DEBUG_INFO("WIFI module set name: EVSE_AP_%s\r\n", Charger.wifi.module_mac);
  7906. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)tmpBuf, strlen(tmpBuf), 0xffff);
  7907. osDelay(WIFI_INIT_DELAY);
  7908. break;
  7909. case 0x05:
  7910. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)WIFI_CMD[WIFI_CMD_SET_CHANNEL], strlen(WIFI_CMD[WIFI_CMD_SET_CHANNEL]), 0xffff);
  7911. osDelay(WIFI_INIT_DELAY);
  7912. break;
  7913. case 0x06:
  7914. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)WIFI_CMD[WIFI_CMD_SET_SECURITY_MODE], strlen(WIFI_CMD[WIFI_CMD_SET_SECURITY_MODE]), 0xffff);
  7915. osDelay(WIFI_INIT_DELAY);
  7916. break;
  7917. case 0x07:
  7918. sprintf(tmpBuf, "%s\"%s\"\r\n", WIFI_CMD[WIFI_CMD_SET_PASSWORD], Charger.memory.EVSE_Config.data.item.wifiConfig.passwd);
  7919. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)tmpBuf, strlen(tmpBuf), 0xffff);
  7920. osDelay(WIFI_INIT_DELAY);
  7921. break;
  7922. case 0x08:
  7923. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)WIFI_CMD[WIFI_CMD_SET_DHCPSERVER], strlen(WIFI_CMD[WIFI_CMD_SET_DHCPSERVER]), 0xffff);
  7924. osDelay(WIFI_INIT_DELAY);
  7925. break;
  7926. case 0x09:
  7927. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)WIFI_CMD[WIFI_CMD_SET_TCPSETVER_CONFIG], strlen(WIFI_CMD[WIFI_CMD_SET_TCPSETVER_CONFIG]), 0xffff);
  7928. osDelay(WIFI_INIT_DELAY);
  7929. break;
  7930. case 0x0a:
  7931. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)WIFI_CMD[WIFI_CMD_SET_AP_ACTIVE], strlen(WIFI_CMD[WIFI_CMD_SET_AP_ACTIVE]), 0xffff);
  7932. osDelay(WIFI_INIT_DELAY);
  7933. break;
  7934. case 0x0b:
  7935. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)WIFI_CMD[WIFI_CMD_SET_DATA_MODE], strlen(WIFI_CMD[WIFI_CMD_SET_DATA_MODE]), 0xffff);
  7936. DEBUG_INFO("WIFI module switch to data mode.\r\n");
  7937. osDelay(WIFI_INIT_DELAY);
  7938. default:
  7939. UART_WIFI_Init_OK = ON;
  7940. break;
  7941. }
  7942. //=====================================
  7943. // WIFI central device connect status
  7944. //=====================================
  7945. if((HAL_GPIO_ReadPin(IN_BLE_DTR_GPIO_Port, IN_BLE_DTR_Pin) == GPIO_PIN_SET) && (UART_WIFI_Init_OK == ON))
  7946. {
  7947. // Device disconnect
  7948. timerRefresh(TIMER_IDX_WIFI);
  7949. if(Charger.wifi.loginRole == WIFI_LOGIN_ROLE_ROOT || Charger.wifi.loginRole == WIFI_LOGIN_ROLE_USER)
  7950. {
  7951. DEBUG_INFO("WIFI client device disconnect.\r\n.");
  7952. Charger.wifi.loginRole = WIFI_LOGIN_ROLE_UNKOWN;
  7953. }
  7954. }
  7955. else if((HAL_GPIO_ReadPin(IN_BLE_DTR_GPIO_Port, IN_BLE_DTR_Pin) == GPIO_PIN_RESET) && (UART_WIFI_Init_OK == ON))
  7956. {
  7957. // Device connect in
  7958. if(Charger.wifi.loginRole == WIFI_LOGIN_ROLE_UNKOWN)
  7959. {
  7960. DEBUG_INFO("WIFI client device connect.....wait sign in.\r\n.");
  7961. osDelay(1000);
  7962. }
  7963. }
  7964. //=====================================
  7965. // Communication timeout process
  7966. //=====================================
  7967. if(timer[TIMER_IDX_WIFI].isAlarm)
  7968. {
  7969. Charger.wifi.loginRole = WIFI_LOGIN_ROLE_UNKOWN;
  7970. DEBUG_INFO("WIFI no communication over time, disconnect client device.\r\n");
  7971. timerRefresh(TIMER_IDX_WIFI);
  7972. HAL_GPIO_WritePin(OUT_BLE_DSR_GPIO_Port, OUT_BLE_DSR_Pin, GPIO_PIN_RESET);
  7973. osDelay(100);
  7974. HAL_GPIO_WritePin(OUT_BLE_DSR_GPIO_Port, OUT_BLE_DSR_Pin, GPIO_PIN_SET);
  7975. }
  7976. //=====================================
  7977. // WIFI uart recieve data process
  7978. //=====================================
  7979. if(UART_WIFI_recv_end_flag == ON)
  7980. {
  7981. if((HAL_GPIO_ReadPin(IN_BLE_DTR_GPIO_Port, IN_BLE_DTR_Pin) == GPIO_PIN_SET) || !Charger.wifi.isDataMode)
  7982. {
  7983. // AT command rx process
  7984. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_RESTORE])!=NULL)
  7985. {
  7986. Charger.wifi.initSeq++;
  7987. }
  7988. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_DTR])!=NULL)
  7989. {
  7990. Charger.wifi.initSeq++;
  7991. }
  7992. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_DSR])!=NULL)
  7993. {
  7994. Charger.wifi.initSeq++;
  7995. }
  7996. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_GET_ADDR])!=NULL)
  7997. {
  7998. memcpy(Charger.wifi.module_mac, &UART_WIFI_rx_buffer[strcspn((char *)UART_WIFI_rx_buffer, ":")+1], 12);
  7999. Charger.wifi.module_mac[12] = '\0';
  8000. DEBUG_INFO("Wifi module mac address: %s\r\n", Charger.wifi.module_mac);
  8001. Charger.wifi.initSeq++;
  8002. }
  8003. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_SSID])!=NULL)
  8004. {
  8005. Charger.wifi.initSeq++;
  8006. }
  8007. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_CHANNEL])!=NULL)
  8008. {
  8009. Charger.wifi.initSeq++;
  8010. }
  8011. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_SECURITY_MODE])!=NULL)
  8012. {
  8013. Charger.wifi.initSeq++;
  8014. }
  8015. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_PASSWORD])!=NULL)
  8016. {
  8017. Charger.wifi.initSeq++;
  8018. }
  8019. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_DHCPSERVER])!=NULL)
  8020. {
  8021. Charger.wifi.initSeq++;
  8022. }
  8023. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_TCPSETVER_CONFIG])!=NULL)
  8024. {
  8025. Charger.wifi.initSeq++;
  8026. }
  8027. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_AP_ACTIVE])!=NULL)
  8028. {
  8029. Charger.wifi.initSeq++;
  8030. }
  8031. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_DATA_MODE])!=NULL)
  8032. {
  8033. Charger.wifi.isDataMode = ON;
  8034. Charger.wifi.initSeq++;
  8035. }
  8036. }
  8037. else
  8038. {
  8039. // Application protocol rx process
  8040. uint8_t tx[UART_BUFFER_SIZE];
  8041. uint8_t tx_len;
  8042. uint8_t chksum = 0;
  8043. if(isValidCheckSum_WIFI() && (rndNumber != ((UART_WIFI_rx_buffer[3]<<0) | (UART_WIFI_rx_buffer[4]<<8) | (UART_WIFI_rx_buffer[5]<<16) | (UART_WIFI_rx_buffer[6]<<24))))
  8044. {
  8045. // Timer reset
  8046. timerRefresh(TIMER_IDX_WIFI);
  8047. rndNumber = ((UART_WIFI_rx_buffer[3]<<0) | (UART_WIFI_rx_buffer[4]<<8) | (UART_WIFI_rx_buffer[5]<<16) | (UART_WIFI_rx_buffer[6]<<24));
  8048. switch(UART_WIFI_rx_buffer[2])
  8049. {
  8050. case WIFI_PROTOCOL_MESSAGE_SIGN_IN:
  8051. DEBUG_INFO("WIFI device sign request...%d\r\n", UART_WIFI_rx_buffer[7]);
  8052. tx_len = 5;
  8053. tx[0] = (tx_len>>8)&0xff;
  8054. tx[1] = (tx_len&0xff);
  8055. tx[2] = UART_WIFI_rx_buffer[2] + 0x80;
  8056. tx[3] = 0x00;
  8057. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  8058. {
  8059. chksum ^= tx[2 + idx];
  8060. }
  8061. tx[4] = chksum;
  8062. break;
  8063. default:
  8064. /*
  8065. TODO: Unknow command response here
  8066. */
  8067. tx_len = 5;
  8068. tx[0] = 0x00;
  8069. tx[1] = 0x05;
  8070. tx[2] = UART_WIFI_rx_buffer[2] + 0x80;
  8071. tx[3] = 0x00;
  8072. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  8073. {
  8074. chksum ^= tx[2 + idx];
  8075. }
  8076. tx[4] = chksum;
  8077. DEBUG_ERROR("WIFI unknow command.\r\n");
  8078. break;
  8079. }
  8080. }
  8081. else
  8082. {
  8083. /*
  8084. TODO: Protocol check sum error response here
  8085. */
  8086. tx_len = 5;
  8087. tx[0] = 0x00;
  8088. tx[1] = 0x05;
  8089. tx[2] = UART_WIFI_rx_buffer[2] + 0x80;
  8090. tx[3] = 0x00;
  8091. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  8092. {
  8093. chksum ^= tx[2 + idx];
  8094. }
  8095. tx[4] = chksum;
  8096. DEBUG_ERROR("WIFI check sum validation error.\r\n");
  8097. }
  8098. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)tx, tx_len, 0xffff);
  8099. }
  8100. UART_WIFI_rx_len=0;
  8101. UART_WIFI_recv_end_flag=0;
  8102. }
  8103. HAL_UART_Receive_DMA(&WIFI_USART, (uint8_t*)UART_WIFI_rx_buffer, UART_BUFFER_SIZE);
  8104. osDelay(1);
  8105. }
  8106. /* USER CODE END StartWifiTask */
  8107. }
  8108. /* Private application code --------------------------------------------------*/
  8109. /* USER CODE BEGIN Application */
  8110. #ifdef MODIFY_IMCP
  8111. void UpdateCheckSum(uint8_t *Buf, uint16_t Len) //Len: Total package length
  8112. {
  8113. uint16_t DataLen = Len - (IMCP_FM_HEAD_LEN + IMCP_FM_TAIL_LEN);
  8114. uint8_t CheckSum = 0;
  8115. //Update DataLen
  8116. HTK_U16(Buf[IMCP_FM_DATALEN_LSB_IDX]) = DataLen;
  8117. //Update CheckSum
  8118. for (uint16_t i = 0; i < DataLen; i++)
  8119. {
  8120. CheckSum ^= Buf[IMCP_FM_DATA_BEG_IDX + i];
  8121. }
  8122. Buf[Len - 1] = CheckSum;
  8123. }
  8124. #ifdef FUNC_RS485_SLAVE
  8125. uint8_t isValidCheckSum_IAP(uint8_t *RxBuf)
  8126. {
  8127. uint8_t result = OFF;
  8128. uint8_t chksum = 0x00;
  8129. #ifdef FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  8130. if (RxBuf[IMCP_FM_FRAMEID_IDX] != PROTOCOL_HEAD)
  8131. {
  8132. return FAIL;
  8133. }
  8134. else if (RxBuf == UART_IAP_rx_buffer)
  8135. {
  8136. if (RxBuf[IMCP_FM_DSTADDR_IDX] != PROTOCOL_ADDR &&
  8137. RxBuf[IMCP_FM_DSTADDR_IDX] != PROTOCOL_ADDR_AC_MAINBOARD &&
  8138. RxBuf[IMCP_FM_DSTADDR_IDX] != PROTOCOL_ADDR_BROADCAST)
  8139. {
  8140. return FAIL;
  8141. }
  8142. }
  8143. else if (RxBuf == UART_RS485_rx_buffer)
  8144. {
  8145. if (RxBuf[IMCP_FM_SRCADDR_IDX] != EMPX10_RS485_ADDR_CSU)
  8146. {
  8147. return FAIL;
  8148. }
  8149. else if (
  8150. RxBuf[IMCP_FM_DSTADDR_IDX] != Charger.m_EMPX10_RS485_ClientAddr &&
  8151. RxBuf[IMCP_FM_DSTADDR_IDX] != EMPX10_RS485_ADDR_BROADCAST)
  8152. {
  8153. return FAIL;
  8154. }
  8155. }
  8156. #else //FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  8157. if (
  8158. (RxBuf[IMCP_FM_FRAMEID_IDX] == PROTOCOL_HEAD) &&
  8159. (
  8160. (RxBuf[IMCP_FM_DSTADDR_IDX] == PROTOCOL_ADDR)
  8161. || (RxBuf[IMCP_FM_DSTADDR_IDX] == PROTOCOL_ADDR_AC_MAINBOARD)
  8162. || (RxBuf[IMCP_FM_DSTADDR_IDX] == PROTOCOL_ADDR_BROADCAST)
  8163. )
  8164. )
  8165. #endif //FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  8166. {
  8167. uint16_t nCheckSumIdx = IMCP_FM_DATA_BEG_IDX + HTK_U16(RxBuf[IMCP_FM_DATALEN_LSB_IDX]);
  8168. for(uint16_t i = IMCP_FM_DATA_BEG_IDX; i < nCheckSumIdx; i++)
  8169. {
  8170. chksum ^= RxBuf[i];
  8171. }
  8172. if(chksum == RxBuf[nCheckSumIdx])
  8173. result = ON;
  8174. }
  8175. return result;
  8176. }
  8177. #else //FUNC_RS485_SLAVE
  8178. uint8_t isValidCheckSum_IAP(void)
  8179. {
  8180. uint8_t result = OFF;
  8181. uint8_t chksum = 0x00;
  8182. if (
  8183. (UART_IAP_rx_buffer[IMCP_FM_FRAMEID_IDX] == PROTOCOL_HEAD) &&
  8184. (
  8185. (UART_IAP_rx_buffer[IMCP_FM_DSTADDR_IDX] == PROTOCOL_ADDR)
  8186. || (UART_IAP_rx_buffer[IMCP_FM_DSTADDR_IDX] == PROTOCOL_ADDR_BROADCAST)
  8187. )
  8188. )
  8189. {
  8190. uint16_t nCheckSumIdx = IMCP_FM_DATA_BEG_IDX + HTK_U16(UART_IAP_rx_buffer[IMCP_FM_DATALEN_LSB_IDX]);
  8191. for(uint16_t i = IMCP_FM_DATA_BEG_IDX; i < nCheckSumIdx; i++)
  8192. {
  8193. chksum ^= UART_IAP_rx_buffer[i];
  8194. }
  8195. if(chksum == UART_IAP_rx_buffer[nCheckSumIdx])
  8196. result = ON;
  8197. }
  8198. return result;
  8199. }
  8200. #endif //FUNC_RS485_SLAVE
  8201. #else //MODIFY_IMCP
  8202. uint8_t isValidCheckSum_IAP(void)
  8203. {
  8204. uint8_t result = OFF;
  8205. uint8_t chksum = 0x00;
  8206. if((UART_IAP_rx_buffer[0] == PROTOCOL_HEAD) &&
  8207. ((UART_IAP_rx_buffer[2] == PROTOCOL_ADDR) | (UART_IAP_rx_buffer[2] == PROTOCOL_ADDR_BROADCAST)))
  8208. {
  8209. for(int idx=0;idx<(UART_IAP_rx_buffer[4] | (UART_IAP_rx_buffer[5]<<8));idx++)
  8210. {
  8211. chksum ^= UART_IAP_rx_buffer[6+ idx];
  8212. }
  8213. if(chksum == UART_IAP_rx_buffer[6+(UART_IAP_rx_buffer[4] | (UART_IAP_rx_buffer[5]<<8))])
  8214. result = ON;
  8215. }
  8216. return result;
  8217. }
  8218. #endif //MODIFY_IMCP
  8219. //-------------------------------------------------------------------------------------------------
  8220. uint8_t isValidCheckSum_BLE(void)
  8221. {
  8222. uint8_t result = OFF;
  8223. uint8_t chksum = 0x00;
  8224. if(((UART_BLE_rx_buffer[1] | (UART_BLE_rx_buffer[0]<<8))-1) < UART_BUFFER_SIZE)
  8225. {
  8226. for(int idx=2;idx<((UART_BLE_rx_buffer[1] | (UART_BLE_rx_buffer[0]<<8))-1);idx++)
  8227. {
  8228. chksum ^= UART_BLE_rx_buffer[idx];
  8229. }
  8230. if(chksum == UART_BLE_rx_buffer[(UART_BLE_rx_buffer[1] | (UART_BLE_rx_buffer[0]<<8))-1])
  8231. result = ON;
  8232. }
  8233. return result;
  8234. }
  8235. //-------------------------------------------------------------------------------------------------
  8236. uint8_t isValidCheckSum_WIFI(void)
  8237. {
  8238. uint8_t result = OFF;
  8239. uint8_t chksum = 0x00;
  8240. if(((UART_WIFI_rx_buffer[1] | (UART_WIFI_rx_buffer[0]<<8))-1) < UART_BUFFER_SIZE)
  8241. {
  8242. for(int idx=2;idx<((UART_WIFI_rx_buffer[1] | (UART_WIFI_rx_buffer[0]<<8))-1);idx++)
  8243. {
  8244. chksum ^= UART_WIFI_rx_buffer[idx];
  8245. }
  8246. if(chksum == UART_WIFI_rx_buffer[(UART_WIFI_rx_buffer[1] | (UART_WIFI_rx_buffer[0]<<8))-1])
  8247. result = ON;
  8248. }
  8249. return result;
  8250. }
  8251. //-------------------------------------------------------------------------------------------------
  8252. #ifdef FUNC_DEBUG_MSG_SET_CHARGER_MODE
  8253. char* getModeName(uint8_t mode);
  8254. #endif
  8255. void setChargerMode(uint8_t mode)
  8256. {
  8257. #ifdef FUNC_DEBUG_MSG_SET_CHARGER_MODE
  8258. XP("#SET CHARGER MODE: [%s] >>> [%s]\r\n", getModeName(Charger.Mode_Previous), getModeName(mode));
  8259. #endif
  8260. Charger.Mode_Previous = Charger.Mode;
  8261. Charger.Mode = mode;
  8262. }
  8263. //-------------------------------------------------------------------------------------------------
  8264. char* getModeName(uint8_t mode)
  8265. {
  8266. switch(mode)
  8267. {
  8268. case MODE_INITIAL:
  8269. return "INITIAL";
  8270. case MODE_IDLE:
  8271. return "IDLE";
  8272. case MODE_HANDSHAKE:
  8273. return "HANDSHAKING";
  8274. case MODE_CHARGING:
  8275. return "CHARGING";
  8276. case MODE_STOP:
  8277. return "STOP";
  8278. case MODE_ALARM:
  8279. return "ALARM";
  8280. case MODE_MAINTAIN:
  8281. return "MAINTAIN";
  8282. case MODE_DEBUG:
  8283. return "DEBUG";
  8284. default:
  8285. return "UNKNOW";
  8286. }
  8287. }
  8288. //-------------------------------------------------------------------------------------------------
  8289. uint8_t isModeChange(void)
  8290. {
  8291. uint8_t result = NO;
  8292. if(!isMode(Charger.Mode_Previous))
  8293. {
  8294. DEBUG_INFO("#MODE from %s to %s\r\n", getModeName(Charger.Mode_Previous), getModeName(Charger.Mode));
  8295. result |= YES;
  8296. if(Charger.Mode == MODE_ALARM)
  8297. {
  8298. Charger.Mode_Before_Alarm = Charger.Mode_Previous;
  8299. }
  8300. Charger.Mode_Previous = Charger.Mode;
  8301. }
  8302. return result;
  8303. }
  8304. //-------------------------------------------------------------------------------------------------
  8305. uint8_t isMode(uint8_t mode)
  8306. {
  8307. return ((Charger.Mode == mode)? YES : NO);
  8308. }
  8309. //-------------------------------------------------------------------------------------------------
  8310. uint8_t rfidCheckSumCal(uint8_t *data, uint8_t length)
  8311. {
  8312. uint8_t result= 0x00;
  8313. for(uint8_t idx=0;idx<length;idx++)
  8314. result ^= data[idx];
  8315. return result;
  8316. }
  8317. //-------------------------------------------------------------------------------------------------
  8318. void HAL_PWR_PVDCallback(void)
  8319. {
  8320. HAL_Delay(100);
  8321. NVIC_SystemReset();
  8322. }
  8323. //-------------------------------------------------------------------------------------------------
  8324. uint8_t Test_LeakModule(void)
  8325. {
  8326. // uint8_t result = PASS, DC_check = false, AC_check=false;
  8327. // uint16_t time_out_count;
  8328. // uint8_t retry_cnt = 0 ;
  8329. uint8_t result = PASS;
  8330. #ifdef CCID_PROTECT
  8331. #ifdef FUNC_SWITCH_RELAY_WELDING_PROTECT
  8332. if (Charger.m_bDisableCCID == 0 && ((HAL_GetTick() - Charger.m_BootTick) / 1000) > 30)
  8333. {
  8334. #endif
  8335. uint8_t DC_check = false;
  8336. uint8_t AC_check=false;
  8337. uint16_t time_out_count;
  8338. uint8_t retry_cnt = 0 ;
  8339. #ifdef FUNC_AUTO_MODIFY_CCID_MODULE
  8340. XP("#Test_LeakModule use CCID Module: %s\r\n", CCID_MODULE_TYPE_STR);
  8341. #endif
  8342. if (Charger.CCID_Module_Type == CCID_MODULE_VAC)
  8343. {
  8344. osDelay(500);
  8345. if(HAL_GPIO_ReadPin(IN_Leak_Error_GPIO_Port, IN_Leak_Error_Pin) == GPIO_PIN_SET)
  8346. {
  8347. DEBUG_INFO("VAC Leak Error pin ON.\r\n");
  8348. result = FAIL;
  8349. }
  8350. else
  8351. {
  8352. HAL_Delay(270);
  8353. HAL_GPIO_WritePin(OUT_Leak_Test_GPIO_Port , OUT_Leak_Test_Pin , GPIO_PIN_RESET);
  8354. HAL_Delay(50); //40ms~1.2sec
  8355. HAL_GPIO_WritePin(OUT_Leak_Test_GPIO_Port , OUT_Leak_Test_Pin , GPIO_PIN_SET);
  8356. //HAL_Delay(40); //36ms
  8357. time_out_count = 0;
  8358. while(1)
  8359. {
  8360. if(HAL_GPIO_ReadPin(IN_Leak_DC_GPIO_Port, IN_Leak_DC_Pin) == GPIO_PIN_SET)
  8361. {
  8362. DC_check = true;
  8363. }
  8364. if(HAL_GPIO_ReadPin(IN_Leak_AC_GPIO_Port, IN_Leak_AC_Pin) == GPIO_PIN_SET)
  8365. {
  8366. AC_check = true;
  8367. }
  8368. //if(DC_check&&AC_check)
  8369. if ((DC_check&&AC_check) && (HAL_GPIO_ReadPin(IN_Leak_AC_GPIO_Port, IN_Leak_AC_Pin) == GPIO_PIN_RESET) && (HAL_GPIO_ReadPin(IN_Leak_DC_GPIO_Port, IN_Leak_DC_Pin) == GPIO_PIN_RESET))
  8370. {
  8371. break;
  8372. }
  8373. if(time_out_count>2000)
  8374. {
  8375. //result = FAIL;
  8376. if(retry_cnt < 1 )
  8377. {
  8378. HAL_Delay(270);
  8379. HAL_GPIO_WritePin(OUT_Leak_Test_GPIO_Port , OUT_Leak_Test_Pin , GPIO_PIN_RESET);
  8380. HAL_Delay(50); //40ms~1.2sec
  8381. HAL_GPIO_WritePin(OUT_Leak_Test_GPIO_Port , OUT_Leak_Test_Pin , GPIO_PIN_SET);
  8382. //HAL_Delay(40); //36ms
  8383. retry_cnt ++ ;
  8384. time_out_count = 0 ;
  8385. }
  8386. else
  8387. {
  8388. DEBUG_INFO("Leak module self test Fail in timeout.\r\n");
  8389. result = FAIL;
  8390. break;
  8391. }
  8392. }
  8393. else
  8394. {
  8395. time_out_count++;
  8396. HAL_Delay(1);
  8397. }
  8398. }
  8399. }
  8400. }
  8401. else // Charger.CCID_Module_Type is CCID_MODULE_CORMEX
  8402. {
  8403. SPWM_Switch(ON);
  8404. HAL_Delay(1000);
  8405. XP("#[SPWM] adc_value.ADC2_IN6_GF.value = %d\r\n",adc_value.ADC2_IN6_GF.value);
  8406. if ((adc_value.ADC2_IN6_GF.value<3000)&&(adc_value.ADC2_IN6_GF.value>2000)) //25mA
  8407. {
  8408. DEBUG_INFO("#Test_LeakModule(): OK\r\n");
  8409. result = PASS ;
  8410. }
  8411. else
  8412. {
  8413. DEBUG_INFO("Test_LeakModule(): NG\r\n");
  8414. //DEBUG_INFO("adc_value.ADC2_IN6_GF.value = %d\n\r",adc_value.ADC2_IN6_GF.value);
  8415. result = FAIL;
  8416. }
  8417. SPWM_Switch(OFF);
  8418. }
  8419. #ifdef FUNC_SWITCH_RELAY_WELDING_PROTECT
  8420. }
  8421. #endif
  8422. #endif //CCID_PROTECT
  8423. return result;
  8424. }
  8425. //-------------------------------------------------------------------------------------------------
  8426. uint8_t Valid_RFID_SN(void)
  8427. {
  8428. /*
  8429. TODO: Validation logic check
  8430. */
  8431. uint8_t result = FAIL;
  8432. uint32_t crc_result;
  8433. uint8_t sn_len;
  8434. uint8_t *buf;
  8435. if(Charger.rfid.snType == RFID_SN_TYPE_4BYTE)
  8436. {
  8437. // SN length: 4
  8438. sn_len = 4;
  8439. }
  8440. else if(Charger.rfid.snType == RFID_SN_TYPE_7BYTE)
  8441. {
  8442. // SN length: 7
  8443. sn_len = 7;
  8444. }
  8445. else
  8446. {
  8447. // SN length: 10
  8448. sn_len = 10;
  8449. }
  8450. buf = malloc(25 + sn_len);
  8451. memcpy(buf, Charger.memory.EVSE_Config.data.item.System_ID, 25);
  8452. memcpy(&buf[25], Charger.rfid.data_Sn, sn_len);
  8453. crc_result = crc32(buf, 25 + sn_len);
  8454. if(((Charger.rfid.data_Block[0] << 8) | (Charger.rfid.data_Block[1] << 0) | (Charger.rfid.data_Block[2] << 24) | (Charger.rfid.data_Block[3] << 16)) == ~crc_result)
  8455. {
  8456. result = PASS;
  8457. }
  8458. free(buf);
  8459. return result;
  8460. }
  8461. //-------------------------------------------------------------------------------------------------
  8462. void recordAlarmHis(void)
  8463. {
  8464. uint8_t isSuccess = FAIL;
  8465. // Check record alarm request, if alarm code bit different with previous
  8466. for(uint8_t errIdx=0;errIdx<32;errIdx++)
  8467. {
  8468. if(((Charger.Alarm_Code>>errIdx)&0x01) != ((Charger.Alarm_Code_Previous>>errIdx)&0x01))
  8469. {
  8470. // Find empty memory space
  8471. for(uint16_t idx=0;idx<(W25Q16FV_BLOCK_SIZE>>4);idx++)
  8472. {
  8473. if((Charger.memory.hisAlarm.data.item[idx].alarmCode == 0xffffffff) &&
  8474. (Charger.memory.hisAlarm.data.item[idx].mode == 0xff))
  8475. {
  8476. isSuccess = PASS;
  8477. Charger.memory.hisAlarm.data.item[idx].DateTime.year = Charger.memory.EVSE_Config.data.item.SystemDateTime.year;
  8478. Charger.memory.hisAlarm.data.item[idx].DateTime.month = Charger.memory.EVSE_Config.data.item.SystemDateTime.month;
  8479. Charger.memory.hisAlarm.data.item[idx].DateTime.day = Charger.memory.EVSE_Config.data.item.SystemDateTime.day;
  8480. Charger.memory.hisAlarm.data.item[idx].DateTime.hour = Charger.memory.EVSE_Config.data.item.SystemDateTime.hour;
  8481. Charger.memory.hisAlarm.data.item[idx].DateTime.min = Charger.memory.EVSE_Config.data.item.SystemDateTime.min;
  8482. Charger.memory.hisAlarm.data.item[idx].DateTime.sec = Charger.memory.EVSE_Config.data.item.SystemDateTime.sec;
  8483. Charger.memory.hisAlarm.data.item[idx].mode = Charger.Mode;
  8484. if((Charger.Alarm_Code>>errIdx)&0x01)
  8485. {
  8486. // Record alarm occur
  8487. Charger.memory.hisAlarm.data.item[idx].alarmCode = alarm_record_code[errIdx];
  8488. Charger.memory.hisAlarm.data.item[idx].isRecover = OFF;
  8489. Charger.Alarm_Code_Previous |= (0x00000001<<errIdx);
  8490. }
  8491. else
  8492. {
  8493. // Record alarm recover
  8494. Charger.memory.hisAlarm.data.item[idx].alarmCode = alarm_record_code[errIdx];
  8495. Charger.memory.hisAlarm.data.item[idx].isRecover = ON;
  8496. Charger.Alarm_Code_Previous &= ~(0x00000001<<errIdx);
  8497. }
  8498. Charger.memory.hisAlarm.op_bits.update = ON;
  8499. // If speace is last item, move data to backup area
  8500. if(idx == ((W25Q16FV_BLOCK_SIZE>>4)-1))
  8501. {
  8502. osDelay(500);
  8503. Charger.memory.hisAlarm.op_bits.backup = ON;
  8504. osDelay(500);
  8505. }
  8506. break;
  8507. }
  8508. }
  8509. // If there is not any free space
  8510. if(!isSuccess)
  8511. {
  8512. osDelay(500);
  8513. Charger.memory.hisAlarm.op_bits.backup = ON;
  8514. osDelay(500);
  8515. }
  8516. }
  8517. }
  8518. }
  8519. //-------------------------------------------------------------------------------------------------
  8520. uint8_t recordChargingHis(uint8_t isColdLoadPickUp, uint32_t statusCode)
  8521. {
  8522. uint8_t isSuccess = FAIL;
  8523. if(isColdLoadPickUp)
  8524. {
  8525. for(uint16_t idx=0;idx<(W25Q16FV_BLOCK_SIZE>>6);idx++)
  8526. {
  8527. if((Charger.memory.hisCharging.data.item[idx].Duration == 0xffffffff) &&
  8528. (Charger.memory.hisCharging.data.item[idx].powerSum == 0xffff) &&
  8529. (Charger.memory.hisCharging.data.item[idx].stopStatusCode == 0xffffffff) )
  8530. {
  8531. isSuccess = PASS;
  8532. Charger.memory.hisCharging.data.item[idx].DateTime.year = Charger.memory.coldLoadPickUp.data.item.DateTime.year;
  8533. Charger.memory.hisCharging.data.item[idx].DateTime.month = Charger.memory.coldLoadPickUp.data.item.DateTime.month;
  8534. Charger.memory.hisCharging.data.item[idx].DateTime.day = Charger.memory.coldLoadPickUp.data.item.DateTime.day;
  8535. Charger.memory.hisCharging.data.item[idx].DateTime.hour = Charger.memory.coldLoadPickUp.data.item.DateTime.hour;
  8536. Charger.memory.hisCharging.data.item[idx].DateTime.min = Charger.memory.coldLoadPickUp.data.item.DateTime.min;
  8537. Charger.memory.hisCharging.data.item[idx].DateTime.sec = Charger.memory.coldLoadPickUp.data.item.DateTime.sec;
  8538. Charger.memory.hisCharging.data.item[idx].Duration = Charger.memory.coldLoadPickUp.data.item.Duration;
  8539. sprintf(Charger.memory.hisCharging.data.item[idx].PlugType, EVSE_CONNECTOR_TYPE);
  8540. Charger.memory.hisCharging.data.item[idx].powerSum = Charger.memory.coldLoadPickUp.data.item.powerSum;
  8541. Charger.memory.hisCharging.data.item[idx].startType = Charger.memory.coldLoadPickUp.data.item.startType;
  8542. Charger.memory.hisCharging.data.item[idx].stopStatusCode = statusCode;
  8543. memcpy(Charger.memory.hisCharging.data.item[idx].user, Charger.rfid.currentCard, 16);
  8544. Charger.memory.hisCharging.op_bits.update = ON;
  8545. // If speace is last item, move data to backup area
  8546. if(idx == ((W25Q16FV_BLOCK_SIZE>>4)-1))
  8547. {
  8548. osDelay(500);
  8549. Charger.memory.hisCharging.op_bits.backup = ON;
  8550. }
  8551. break;
  8552. }
  8553. }
  8554. }
  8555. else
  8556. {
  8557. for(uint16_t idx=0;idx<(W25Q16FV_BLOCK_SIZE>>6);idx++)
  8558. {
  8559. if((Charger.memory.hisCharging.data.item[idx].Duration == 0xffffffff) &&
  8560. (Charger.memory.hisCharging.data.item[idx].powerSum == 0xffff) &&
  8561. (Charger.memory.hisCharging.data.item[idx].stopStatusCode == 0xffffffff) )
  8562. {
  8563. isSuccess = PASS;
  8564. Charger.memory.hisCharging.data.item[idx].DateTime.year = Charger.cycle_info.startDateTime.year;
  8565. Charger.memory.hisCharging.data.item[idx].DateTime.month = Charger.cycle_info.startDateTime.month;
  8566. Charger.memory.hisCharging.data.item[idx].DateTime.day = Charger.cycle_info.startDateTime.day;
  8567. Charger.memory.hisCharging.data.item[idx].DateTime.hour = Charger.cycle_info.startDateTime.hour;
  8568. Charger.memory.hisCharging.data.item[idx].DateTime.min = Charger.cycle_info.startDateTime.min;
  8569. Charger.memory.hisCharging.data.item[idx].DateTime.sec = Charger.cycle_info.startDateTime.sec;
  8570. Charger.memory.hisCharging.data.item[idx].Duration = Charger.cycle_info.Duration;
  8571. sprintf(Charger.memory.hisCharging.data.item[idx].PlugType, EVSE_CONNECTOR_TYPE);
  8572. Charger.memory.hisCharging.data.item[idx].powerSum = Charger.cycle_info.Power_Consumption;
  8573. Charger.memory.hisCharging.data.item[idx].startType = Charger.cycle_info.StartType;
  8574. Charger.memory.hisCharging.data.item[idx].stopStatusCode = statusCode;
  8575. memcpy(Charger.memory.hisCharging.data.item[idx].user, Charger.rfid.currentCard, 16);
  8576. Charger.memory.hisCharging.op_bits.update = ON;
  8577. // If speace is last item, move data to backup area
  8578. if(idx == ((W25Q16FV_BLOCK_SIZE>>4)-1))
  8579. {
  8580. osDelay(500);
  8581. Charger.memory.hisCharging.op_bits.backup = ON;
  8582. }
  8583. break;
  8584. }
  8585. }
  8586. }
  8587. // If there is not any free space
  8588. if(!isSuccess)
  8589. {
  8590. osDelay(500);
  8591. Charger.memory.hisCharging.op_bits.backup = ON;
  8592. }
  8593. return isSuccess;
  8594. }
  8595. //-------------------------------------------------------------------------------------------------
  8596. void setLedMotion(uint8_t action)
  8597. {//ssetled
  8598. //standard
  8599. switch(action)
  8600. {
  8601. case LED_ACTION_INIT:
  8602. Charger.led_R.motion = LED_MOTION_ON;
  8603. Charger.led_G.motion = LED_MOTION_ON;
  8604. Charger.led_B.motion = LED_MOTION_ON;
  8605. Charger.led_W.motion = LED_MOTION_OFF;
  8606. LedOnRGBSet(1000, 1000, 1000, 0);
  8607. break;
  8608. case LED_ACTION_IDLE: //eMP: IDLE BACKEND ONLINE
  8609. Charger.led_R.motion = LED_MOTION_OFF;
  8610. Charger.led_G.motion = LED_MOTION_ON;
  8611. Charger.led_B.motion = LED_MOTION_OFF;
  8612. Charger.led_W.motion = LED_MOTION_OFF;
  8613. LedOnRGBSet(0, 1000, 0, 0);
  8614. break;
  8615. case LED_ACTION_AUTHED:
  8616. Charger.led_R.motion = LED_MOTION_OFF;
  8617. Charger.led_G.motion = LED_MOTION_OFF;
  8618. Charger.led_B.motion = LED_MOTION_ON;
  8619. Charger.led_W.motion = LED_MOTION_OFF;
  8620. LedOnRGBSet(0, 0, 1000, 0);
  8621. break;
  8622. case LED_ACTION_CONNECTED: //eMP: IDLE BACKEND OFFLINE
  8623. #ifdef FUNC_EMPX10_LED
  8624. Charger.led_R.motion = LED_MOTION_ON;
  8625. Charger.led_G.motion = LED_MOTION_ON;
  8626. Charger.led_B.motion = LED_MOTION_OFF;
  8627. Charger.led_W.motion = LED_MOTION_OFF;
  8628. LedOnRGBSet(1000, 500, 0, 0);
  8629. #else
  8630. Charger.led_R.motion = LED_MOTION_OFF;
  8631. Charger.led_G.motion = LED_MOTION_OFF;
  8632. Charger.led_B.motion = LED_MOTION_ON;
  8633. Charger.led_W.motion = LED_MOTION_OFF;
  8634. LedOnRGBSet(0, 0, 1000, 0);
  8635. #endif
  8636. break;
  8637. case LED_ACTION_CHARGING:
  8638. Charger.led_R.motion = LED_MOTION_OFF;
  8639. Charger.led_G.motion = LED_MOTION_OFF;
  8640. Charger.led_B.motion = LED_MOTION_BLINK;
  8641. Charger.led_W.motion = LED_MOTION_OFF;
  8642. LedOnRGBSet(0, 0, 1000, 0);
  8643. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 0, 1);
  8644. break;
  8645. case LED_ACTION_STOP:
  8646. Charger.led_R.motion = LED_MOTION_OFF;
  8647. Charger.led_G.motion = LED_MOTION_OFF;
  8648. Charger.led_B.motion = LED_MOTION_ON;
  8649. Charger.led_W.motion = LED_MOTION_OFF;
  8650. LedOnRGBSet(0, 0, 1000, 0);
  8651. break;
  8652. case LED_ACTION_ALARM:
  8653. #ifdef FUNC_EMPX10_LED_ALARM_FIX_20240730
  8654. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  8655. Charger.Alarm_LED = Charger.Alarm_Code;
  8656. }else{
  8657. Charger.Alarm_LED = Charger.am3352.LedAlarmState;
  8658. }
  8659. if(Charger.Alarm_LED & ALARM_EMERGENCY_STOP)
  8660. {
  8661. Charger.led_R.motion = LED_MOTION_BLINK;
  8662. Charger.led_G.motion = LED_MOTION_OFF;
  8663. Charger.led_B.motion = LED_MOTION_OFF;
  8664. Charger.led_W.motion = LED_MOTION_OFF;
  8665. LedOnRGBSet(1000, 0, 0, 0);
  8666. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 3000, 7);
  8667. }
  8668. else
  8669. {
  8670. Charger.led_R.motion = LED_MOTION_ON;
  8671. Charger.led_G.motion = LED_MOTION_OFF;
  8672. Charger.led_B.motion = LED_MOTION_OFF;
  8673. Charger.led_W.motion = LED_MOTION_OFF;
  8674. LedOnRGBSet(1000, 0, 0, 0);
  8675. }
  8676. #else //FUNC_EMPX10_LED_ALARM_FIX_20240730
  8677. Charger.led_R.motion = LED_MOTION_BLINK;
  8678. Charger.led_G.motion = LED_MOTION_OFF;
  8679. Charger.led_B.motion = LED_MOTION_OFF;
  8680. Charger.led_W.motion = LED_MOTION_OFF;
  8681. LedOnRGBSet(1000, 0, 0, 0);
  8682. //set alarm led blink duty
  8683. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  8684. Charger.Alarm_LED = Charger.Alarm_Code;
  8685. }else{
  8686. Charger.Alarm_LED = Charger.am3352.LedAlarmState;
  8687. }
  8688. if(Charger.Alarm_LED & ALARM_CP_ERROR)
  8689. blinkerTimeSet(BLINKER_IDX_LED, 500 , 500, 0, 1);
  8690. if(Charger.Alarm_LED & ALARM_OVER_VOLTAGE)
  8691. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 3000, 1);
  8692. if(Charger.Alarm_LED & ALARM_UNDER_VOLTAGE)
  8693. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 3000, 2);
  8694. if(Charger.Alarm_LED & ALARM_OVER_CURRENT)
  8695. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 3000, 3);
  8696. if(Charger.Alarm_LED & ALARM_OVER_TEMPERATURE)
  8697. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 3000, 4);
  8698. if(Charger.Alarm_LED & ALARM_CURRENT_LEAK_AC)
  8699. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 3000, 5);
  8700. if(Charger.Alarm_LED & ALARM_CURRENT_LEAK_DC)
  8701. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 3000, 5);
  8702. if(Charger.Alarm_LED & ALARM_GROUND_FAIL)
  8703. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 3000, 6);
  8704. if(Charger.Alarm_LED & ALARM_HANDSHAKE_TIMEOUT)
  8705. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 0, 1);
  8706. if(Charger.Alarm_LED & ALARM_EMERGENCY_STOP)
  8707. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 3000, 7);
  8708. if(Charger.Alarm_LED & ALARM_ROTATORY_SWITCH_FAULT)
  8709. blinkerTimeSet(BLINKER_IDX_LED, 200, 200, 0, 1);
  8710. //OCP Latch
  8711. if (Charger.counter.OC.isLatch == ON)
  8712. {
  8713. Charger.led_R.motion = LED_MOTION_ON ;
  8714. Charger.led_B.motion = LED_MOTION_ON ;
  8715. LedOnRGBSet(1000, 0, 1000, 0);
  8716. }
  8717. //CCID Latch
  8718. if (Charger.counter.LEAK.isLatch == ON)
  8719. {
  8720. Charger.led_R.motion = LED_MOTION_ON ;
  8721. Charger.led_G.motion = LED_MOTION_ON ;
  8722. LedOnRGBSet(1000, 1000, 0, 0);
  8723. }
  8724. //OTP
  8725. if (Charger.counter.OT.isLatch == ON)
  8726. {
  8727. Charger.led_R.motion = LED_MOTION_ON;
  8728. Charger.led_G.motion = LED_MOTION_BLINK;
  8729. Charger.led_B.motion = LED_MOTION_BLINK;
  8730. Charger.led_W.motion = LED_MOTION_OFF;
  8731. LedOnRGBSet(1000, 1000, 1000, 0);
  8732. blinkerTimeSet(BLINKER_IDX_LED, 500 , 500, 0, 1);
  8733. }
  8734. //self test error
  8735. if ((Charger.Alarm_Code & ALARM_MCU_TESTFAIL) ||
  8736. (Charger.Alarm_Code & ALARM_RELAY_STATUS) ||
  8737. (Charger.Alarm_Code & ALARM_RELAY_DRIVE_FUALT ) ||
  8738. (Charger.Alarm_Code & ALARM_LEAK_MODULE_FAIL))
  8739. {
  8740. Charger.led_R.motion = LED_MOTION_ON;
  8741. Charger.led_G.motion = LED_MOTION_OFF;
  8742. Charger.led_B.motion = LED_MOTION_OFF;
  8743. Charger.led_W.motion = LED_MOTION_OFF;
  8744. LedOnRGBSet(1000, 0, 0, 0);
  8745. }
  8746. #endif //FUNC_EMPX10_LED_ALARM_FIX_20240730
  8747. break;
  8748. case LED_ACTION_MAINTAIN:
  8749. #ifdef FUNC_EMPX10_LED
  8750. Charger.led_R.motion = LED_MOTION_BLINK;
  8751. Charger.led_G.motion = LED_MOTION_BLINK;
  8752. Charger.led_B.motion = LED_MOTION_OFF;
  8753. Charger.led_W.motion = LED_MOTION_OFF;
  8754. LedOnRGBSet(1000, 500, 0, 0);
  8755. #else
  8756. Charger.led_R.motion = LED_MOTION_OFF;
  8757. Charger.led_G.motion = LED_MOTION_BLINK;
  8758. Charger.led_B.motion = LED_MOTION_OFF;
  8759. Charger.led_W.motion = LED_MOTION_OFF;
  8760. LedOnRGBSet(0, 1000, 0, 0);
  8761. #endif
  8762. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 0, 1);
  8763. break;
  8764. case LED_ACTION_RFID_PASS:
  8765. Charger.led_R.motion = LED_MOTION_OFF;
  8766. Charger.led_G.motion = LED_MOTION_BLINK;
  8767. Charger.led_B.motion = LED_MOTION_OFF;
  8768. Charger.led_W.motion = LED_MOTION_OFF;
  8769. LedOnRGBSet(0, 1000, 0, 0);
  8770. blinkerTimeSet(BLINKER_IDX_LED, 200, 200, 0, 1);
  8771. break;
  8772. case LED_ACTION_RFID_FAIL:
  8773. Charger.led_R.motion = LED_MOTION_BLINK;
  8774. Charger.led_G.motion = LED_MOTION_OFF;
  8775. Charger.led_B.motion = LED_MOTION_OFF;
  8776. Charger.led_W.motion = LED_MOTION_OFF;
  8777. LedOnRGBSet(1000, 0, 0, 0);
  8778. blinkerTimeSet(BLINKER_IDX_LED, 200, 200, 0, 1);
  8779. break;
  8780. case LED_ACTION_DEBUG:
  8781. Charger.led_R.motion = LED_MOTION_BLINK;
  8782. Charger.led_G.motion = LED_MOTION_BLINK;
  8783. Charger.led_B.motion = LED_MOTION_BLINK;
  8784. Charger.led_W.motion = LED_MOTION_BLINK;
  8785. LedOnRGBSet(1000, 1000, 1000, 1000);
  8786. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 0, 1);
  8787. break;
  8788. case LED_ACTION_ALL_OFF:
  8789. Charger.led_R.motion = LED_MOTION_OFF;
  8790. Charger.led_G.motion = LED_MOTION_OFF;
  8791. Charger.led_B.motion = LED_MOTION_OFF;
  8792. Charger.led_W.motion = LED_MOTION_OFF;
  8793. LedOnRGBSet(0, 0, 0, 0);
  8794. break ;
  8795. case LED_ACTION_HANDSHAKE_FAIL:
  8796. Charger.led_R.motion = LED_MOTION_BLINK;
  8797. Charger.led_G.motion = LED_MOTION_OFF;
  8798. Charger.led_B.motion = LED_MOTION_OFF;
  8799. Charger.led_W.motion = LED_MOTION_OFF;
  8800. LedOnRGBSet(1000, 0, 0, 0);
  8801. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 0, 1);
  8802. break;
  8803. case LED_ACTION_INTERNET_DISCONNECT:
  8804. Charger.led_R.motion = LED_MOTION_ON;
  8805. Charger.led_G.motion = LED_MOTION_ON;
  8806. Charger.led_B.motion = LED_MOTION_OFF;
  8807. Charger.led_W.motion = LED_MOTION_OFF;
  8808. LedOnRGBSet(1000, 1000, 0, 0);
  8809. break;
  8810. case LED_ACTION_RESTORE_SETTING:
  8811. Charger.led_R.motion = LED_MOTION_OFF;
  8812. Charger.led_G.motion = LED_MOTION_OFF;
  8813. Charger.led_B.motion = LED_MOTION_OFF;
  8814. Charger.led_W.motion = LED_MOTION_BLINK;
  8815. LedOnRGBSet(0, 0, 0, 1000);
  8816. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 0, 1);
  8817. break;
  8818. default :
  8819. Charger.led_R.motion = LED_MOTION_OFF;
  8820. Charger.led_G.motion = LED_MOTION_OFF;
  8821. Charger.led_B.motion = LED_MOTION_OFF;
  8822. Charger.led_W.motion = LED_MOTION_OFF;
  8823. LedOnRGBSet(0, 0, 0, 0);
  8824. break;
  8825. }
  8826. if((action != LED_ACTION_RFID_PASS) &&
  8827. (action != LED_ACTION_RFID_FAIL) &&
  8828. (action != LED_ACTION_BLE_CONNECT) &&
  8829. (action != LED_ACTION_BLE_DISABLE) &&
  8830. (action != LED_ACTION_HANDSHAKE_FAIL) &&
  8831. (action != LED_ACTION_RESTORE_SETTING))
  8832. {
  8833. Charger.Led_Mode = action;
  8834. }
  8835. }
  8836. //-------------------------------------------------------------------------------------------------
  8837. void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef* hadc)
  8838. {
  8839. #ifdef CCID_PROTECT
  8840. #ifdef FUNC_SWITCH_RELAY_WELDING_PROTECT
  8841. if (Charger.m_bDisableCCID == 0 && ((HAL_GetTick() - Charger.m_BootTick) / 1000) > 30)
  8842. {
  8843. #endif
  8844. if (Charger.CCID_Module_Type == CCID_MODULE_CORMEX)
  8845. {
  8846. if(hadc->Instance == ADC2) //use watch dog to check ccid level
  8847. {
  8848. if (!Charger.isTestLeakModule)
  8849. {
  8850. if(Charger.counter.WatchDogLeak >= 5)
  8851. {
  8852. if(!(Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC))
  8853. {
  8854. DEBUG_INFO("leak adc watch happen\r\n");
  8855. Charger.counter.LEAK.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  8856. Charger.Alarm_Code |= ALARM_CURRENT_LEAK_AC;
  8857. timerEnable(TIMER_IDX_RETRY_LEAK_AC, ALARM_RETRY_INTERVAL_LEAK);
  8858. Charger.counter.LEAK.retry++ ;
  8859. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF ;
  8860. HAL_GPIO_WritePin(OUT_RelayDrv_L1_GPIO_Port, OUT_RelayDrv_L1_Pin, GPIO_PIN_RESET);
  8861. HAL_GPIO_WritePin(OUT_RelayHold_L1_GPIO_Port, OUT_RelayHold_L1_Pin, GPIO_PIN_RESET);
  8862. HAL_GPIO_WritePin(OUT_RelayDrv_L2_GPIO_Port, OUT_RelayDrv_L2_Pin, GPIO_PIN_RESET);
  8863. HAL_GPIO_WritePin(OUT_RelayHold_L2_GPIO_Port, OUT_RelayHold_L2_Pin, GPIO_PIN_RESET);
  8864. Charger.counter.WatchDogLeak = 0 ;
  8865. }
  8866. }
  8867. else
  8868. {
  8869. Charger.counter.WatchDogLeak ++ ;
  8870. }
  8871. }
  8872. }
  8873. }
  8874. #ifdef FUNC_SWITCH_RELAY_WELDING_PROTECT
  8875. }
  8876. #endif
  8877. #endif //CCID_PROTECT
  8878. }
  8879. //-------------------------------------------------------------------------------------------------
  8880. void SetADCWDGBoundary(void)
  8881. {
  8882. if(!ADCWDGHighThreshold)
  8883. {
  8884. //220V 3200 820
  8885. //110V 2640 1400
  8886. ADCWDGHighThreshold = 3200;
  8887. ADCWDGLowThreshold = 820;
  8888. }
  8889. ADC_AnalogWDGConfTypeDef AnalogWDGConfig = {0};
  8890. /*
  8891. * Configure the analog watchdog
  8892. */
  8893. AnalogWDGConfig.WatchdogMode = ADC_ANALOGWATCHDOG_SINGLE_REG;
  8894. AnalogWDGConfig.HighThreshold = ADCWDGHighThreshold;
  8895. AnalogWDGConfig.LowThreshold = ADCWDGLowThreshold;
  8896. AnalogWDGConfig.Channel = ADC_CHANNEL_9;
  8897. AnalogWDGConfig.ITMode = ENABLE;
  8898. if (HAL_ADC_AnalogWDGConfig(&hadc3, &AnalogWDGConfig) != HAL_OK)
  8899. {
  8900. Error_Handler();
  8901. }
  8902. }
  8903. //-------------------------------------------------------------------------------------------------
  8904. void vApplicationStackOverflowHook( TaskHandle_t xTask, signed char *pcTaskName )
  8905. {
  8906. DEBUG_ERROR("Stack overflow: %s\r\n", pcTaskName);
  8907. osDelay(500);
  8908. }
  8909. //-------------------------------------------------------------------------------------------------
  8910. void getlastRecord(void)
  8911. {
  8912. for(uint16_t idx=0;idx<(W25Q16FV_BLOCK_SIZE>>4);idx++)
  8913. {
  8914. if((Charger.memory.hisCharging.data.item[idx].Duration == 0xffffffff) &&
  8915. (Charger.memory.hisCharging.data.item[idx].powerSum == 0xffff) &&
  8916. (Charger.memory.hisCharging.data.item[idx].stopStatusCode == 0xffffffff) )
  8917. {
  8918. DEBUG_INFO("Charging record last index: %d\r\n", (idx));
  8919. break;
  8920. }
  8921. }
  8922. for(uint16_t idx=0;idx<(W25Q16FV_BLOCK_SIZE>>6);idx++)
  8923. {
  8924. if((Charger.memory.hisAlarm.data.item[idx].alarmCode == 0xffffffff) &&
  8925. (Charger.memory.hisAlarm.data.item[idx].mode == 0xff))
  8926. {
  8927. DEBUG_INFO("Alarm record last index: %d\r\n", (idx));
  8928. break;
  8929. }
  8930. }
  8931. }
  8932. //-------------------------------------------------------------------------------------------------
  8933. void getRotarySwitchSetting(void)
  8934. {
  8935. uint8_t tmpBuf = 0;
  8936. tmpBuf += (HAL_GPIO_ReadPin(IN_AC_Current_Set1_GPIO_Port, IN_AC_Current_Set1_Pin)?0:1)<<0;
  8937. tmpBuf += (HAL_GPIO_ReadPin(IN_AC_Current_Set2_GPIO_Port, IN_AC_Current_Set2_Pin)?0:1)<<1;
  8938. tmpBuf += (HAL_GPIO_ReadPin(IN_AC_Current_Set3_GPIO_Port, IN_AC_Current_Set3_Pin)?0:1)<<2;
  8939. tmpBuf += (HAL_GPIO_ReadPin(IN_AC_Current_Set4_GPIO_Port, IN_AC_Current_Set4_Pin)?0:1)<<3;
  8940. Charger.isDebugEnable = OFF;
  8941. #ifdef FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  8942. if (!(((sbyte)tmpBuf) >= EMPX10_RS485_ADDR_MIN && ((sbyte)tmpBuf) <= EMPX10_RS485_ADDR_MAX))
  8943. {
  8944. Charger.isRotarySwitchError = ON;
  8945. }
  8946. DEBUG_INFO("Rotary switch (RS485 Addr) Setting: %d (%s)\r\n", tmpBuf, Charger.isRotarySwitchError ? "NG" : "OK");
  8947. Charger.m_EMPX10_RS485_ClientAddr = tmpBuf;
  8948. #ifdef FUNC_EMPX10_SET_FIXED_CURRENT_16A
  8949. Charger.maxRatingCurrent = 16;
  8950. #else //FUNC_EMPX10_SET_FIXED_CURRENT_16A
  8951. #ifdef FUNC_EMPX10_SET_FIXED_CURRENT_20A
  8952. Charger.maxRatingCurrent = 20;
  8953. #else
  8954. Charger.maxRatingCurrent = EVSE_MODEL_CURRENT_RATING;
  8955. #endif
  8956. #endif //FUNC_EMPX10_SET_FIXED_CURRENT_16A
  8957. #else //FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  8958. switch(tmpBuf)
  8959. {
  8960. case 0x00:
  8961. Charger.maxRatingCurrent = EVSE_MODEL_CURRENT_RATING;
  8962. DEBUG_INFO("Set maxRatingCurrent is Test mode\r\n");
  8963. Charger.isDebugEnable = ON;
  8964. break;
  8965. case 0x01:
  8966. Charger.maxRatingCurrent = (6>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:6);
  8967. Charger.isDebugEnable = OFF;
  8968. break;
  8969. case 0x02:
  8970. Charger.maxRatingCurrent = (8>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:8);
  8971. Charger.isDebugEnable = OFF;
  8972. break;
  8973. case 0x03:
  8974. Charger.maxRatingCurrent = (10>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:10);
  8975. Charger.isDebugEnable = OFF;
  8976. break;
  8977. case 0x04:
  8978. Charger.maxRatingCurrent = (13>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:13);
  8979. Charger.isDebugEnable = OFF;
  8980. break;
  8981. case 0x05:
  8982. Charger.maxRatingCurrent = (16>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:16);
  8983. Charger.isDebugEnable = OFF;
  8984. break;
  8985. case 0x06:
  8986. Charger.maxRatingCurrent = (20>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:20);
  8987. Charger.isDebugEnable = OFF;
  8988. break;
  8989. case 0x07:
  8990. Charger.maxRatingCurrent = (25>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:25);
  8991. Charger.isDebugEnable = OFF;
  8992. break;
  8993. case 0x08:
  8994. Charger.maxRatingCurrent = (32>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:32);
  8995. Charger.isDebugEnable = OFF;
  8996. break;
  8997. case 0x09:
  8998. Charger.maxRatingCurrent = (32>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:32);
  8999. Charger.isDebugEnable = OFF;
  9000. break ;
  9001. case 0x0a:
  9002. Charger.maxRatingCurrent = (30>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:30); // for JARI
  9003. Charger.isDebugEnable = OFF;
  9004. break ;
  9005. case 0x0b:
  9006. case 0x0c:
  9007. case 0x0d:
  9008. case 0x0e:
  9009. Charger.maxRatingCurrent = (32>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:32);
  9010. Charger.isRotarySwitchError = ON ;
  9011. Charger.isDebugEnable = OFF;
  9012. break ;
  9013. case 0x0f:
  9014. Charger.maxRatingCurrent = EVSE_MODEL_CURRENT_RATING;
  9015. DEBUG_INFO("Knob error Set maxRatingCurrent defualt 32A\r\n");
  9016. DEBUG_INFO("EVSE is Set Slave Mode\r\n");
  9017. Charger.isDebugEnable = OFF;
  9018. break;
  9019. default:
  9020. Charger.maxRatingCurrent = EVSE_MODEL_CURRENT_RATING;
  9021. DEBUG_INFO("knob default Set maxRatingCurrent defualt 32A\r\n");
  9022. Charger.isDebugEnable = OFF;
  9023. break;
  9024. }
  9025. #endif //FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  9026. DEBUG_INFO("Current rating value: %d A\r\n", Charger.maxRatingCurrent);
  9027. Charger.memory.EVSE_Config.data.item.MaxChargingCurrent = Charger.maxRatingCurrent;
  9028. #ifdef MODIFY_AC_EVSE_STATUS_MAX_CHARGING_CURRENT_VARIABLE
  9029. Charger.AC_MaxChargingCurrentOrDuty = Charger.maxRatingCurrent;
  9030. #endif
  9031. }
  9032. //------------------------------------------------------------------------------//
  9033. void CLC_Corr_Gain_Par(uint16_t SpecData_H ,uint16_t SpecData_L ,uint16_t MCUData_H , uint16_t MCUData_L , float *GainA , float *GainB )
  9034. {
  9035. *GainA = (float)((float)(SpecData_H -SpecData_L) / (float)( MCUData_H - MCUData_L)) ;
  9036. *GainB = (float)(SpecData_H - (float)(*GainA * MCUData_H)) ;
  9037. }
  9038. //------------------------------------------------------------------------------//
  9039. void CLC_Corr_Gain_Par2(uint16_t SpecData_H ,uint16_t SpecData_L ,uint16_t MCUData_H , uint16_t MCUData_L , float *GainA , float *GainB , uint8_t *PosorNeg )
  9040. {
  9041. *GainA = (float)((float)(SpecData_H -SpecData_L) / (float)( MCUData_H - MCUData_L)) ;
  9042. if (SpecData_H > (*GainA * MCUData_H))
  9043. {
  9044. *GainB = SpecData_H - (*GainA * MCUData_H) ;
  9045. *PosorNeg = ON ;
  9046. }
  9047. else
  9048. {
  9049. *GainB = (*GainA * MCUData_H) - SpecData_H ;
  9050. *PosorNeg = OFF ;
  9051. }
  9052. }
  9053. //------------------------------------------------------------------------------
  9054. uint8_t Array_data_Check ( uint8_t *s1, uint8_t *s2 )
  9055. {
  9056. uint8_t compare = 0;
  9057. uint8_t result = PASS;
  9058. for(compare=0;compare<sizeof(s1);compare++)
  9059. {
  9060. if(s1[compare]!=s2[compare])
  9061. {
  9062. result = FAIL;
  9063. break;
  9064. }
  9065. }
  9066. return result;
  9067. }
  9068. //------------------------------------------------------------------------------
  9069. void Relay_Control_Function (void)
  9070. {
  9071. switch(Charger.Relay_Action)
  9072. {
  9073. #ifdef FUNC_TEST_RELAY_ALWAYS_ON
  9074. case GPIO_RELAY_ACTION_OFF:
  9075. #endif
  9076. case GPIO_RELAY_ACTION_ON:
  9077. if(Relay1_Action_Get() == GPIO_RELAY1_ACTION_OFF && Relay2_Action_Get() == GPIO_RELAY2_ACTION_OFF)
  9078. {
  9079. Relay_Action_Set(GPIO_RELAY1_ACTION_ON);
  9080. Relay_Action_Set(GPIO_RELAY2_ACTION_ON);
  9081. }
  9082. if(timer[TIMER_IDX_RELAY_1].isAlarm == ON && timer[TIMER_IDX_RELAY_2].isAlarm == ON)
  9083. {
  9084. Relay_Action_Set(GPIO_RELAY1_ACTION_HOLD);
  9085. Relay_Action_Set(GPIO_RELAY2_ACTION_HOLD);
  9086. timerDisable(TIMER_IDX_RELAY_1);
  9087. timerDisable(TIMER_IDX_RELAY_2);
  9088. Charger.Relay_isOperationCompleted |= 0x11 ;
  9089. }
  9090. break;
  9091. #ifndef FUNC_TEST_RELAY_ALWAYS_ON
  9092. case GPIO_RELAY_ACTION_OFF:
  9093. timer[TIMER_IDX_RELAY_1].isEnable = OFF;
  9094. timer[TIMER_IDX_RELAY_2].isEnable = OFF;
  9095. if(Relay1_Action_Get() != GPIO_RELAY1_ACTION_OFF)
  9096. {
  9097. Relay_Action_Set(GPIO_RELAY1_ACTION_OFF);
  9098. Relay_Action_Set(GPIO_RELAY2_ACTION_OFF);
  9099. }
  9100. osDelay(10);
  9101. Charger.Relay_isOperationCompleted &= 0x00 ;
  9102. #ifdef FUNC_GET_TRIP_TIME
  9103. if (Charger.m_TripBeg > 0 && Charger.m_TripEnd == HTK_U32_MAX)
  9104. {
  9105. Charger.m_TripEnd = HAL_GetTick();
  9106. }
  9107. #endif
  9108. break;
  9109. #endif //FUNC_TEST_RELAY_ALWAYS_ON
  9110. default:
  9111. break;
  9112. }
  9113. }
  9114. //--------------------------------------------------------------------------------
  9115. uint8_t WatchDogLeakRawDataCheck_Event(void)
  9116. {
  9117. uint8_t result = PASS ;
  9118. for(uint16_t idx = 0 ; idx < ADC2_SAMPLE_COUNT; idx++)
  9119. {
  9120. if (ADC2_Buffer_Each[1][idx] >= 4000)
  9121. {
  9122. result = FAIL;
  9123. break;
  9124. }
  9125. }
  9126. return result ;
  9127. }
  9128. //--------------------------------------------------------------------------------
  9129. uint16_t getBrightnessDuty(uint16_t max_duty)
  9130. {
  9131. uint8_t Lv_Brightness = 3;
  9132. uint16_t duty = 1000;
  9133. Lv_Brightness = (Charger.Led_Brightness[(Charger.memory.EVSE_Config.data.item.SystemDateTime.hour/2)]>>(((Charger.memory.EVSE_Config.data.item.SystemDateTime.hour+1)%2)*4))&0xf;
  9134. switch(Lv_Brightness)
  9135. {
  9136. #ifdef FUNC_EMP_LED_SET_MIN_BRIGHTNESS
  9137. case 0:
  9138. duty = max_duty/10;
  9139. break;
  9140. case 1:
  9141. duty = max_duty/10;
  9142. break;
  9143. #else //FUNC_EMP_LED_SET_MIN_BRIGHTNESS
  9144. case 0:
  9145. duty = max_duty/4;
  9146. break;
  9147. case 1:
  9148. duty = max_duty/3;
  9149. break;
  9150. #endif //FUNC_EMP_LED_SET_MIN_BRIGHTNESS
  9151. case 2:
  9152. duty = max_duty/2;
  9153. break;
  9154. case 3:
  9155. duty = max_duty;
  9156. break;
  9157. default:
  9158. duty = max_duty;
  9159. break;
  9160. }
  9161. return duty;
  9162. }
  9163. //--------------------------------------------------------------------------------
  9164. void parseVersionInfoFromModelname(void)
  9165. {
  9166. #ifdef FUNC_VERSION_USE_NEW_SPEC_20240926
  9167. //do nothing
  9168. #else //FUNC_VERSION_USE_NEW_SPEC_20240926
  9169. //--------------------------------------
  9170. // Version Naming Rule "tx.yz.ab.cddd.vv"
  9171. //Version Naming Rule "a"
  9172. for(uint8_t idx=0;idx<3;idx++)
  9173. {
  9174. switch(Charger.memory.EVSE_Config.data.item.ModelName[7+idx])
  9175. {
  9176. case '1':
  9177. // J1772 Plug
  9178. Charger.Ver_FW[6] = '4';
  9179. break;
  9180. case '2':
  9181. // J1772 Socket
  9182. Charger.Ver_FW[6] = '1';
  9183. break;
  9184. case '3':
  9185. // CE Plug
  9186. Charger.Ver_FW[6] = '5';
  9187. break;
  9188. case '4':
  9189. // CE Socket
  9190. Charger.Ver_FW[6] = '2';
  9191. break;
  9192. case '5':
  9193. // GB Plug
  9194. Charger.Ver_FW[6] = '6';
  9195. break;
  9196. case '6':
  9197. // GB Socket
  9198. Charger.Ver_FW[6] = '3';
  9199. break;
  9200. #ifdef FUNC_ADD_FW_VER_WITH_GUN_TYPE_NACS
  9201. case '9':
  9202. // NACS
  9203. Charger.Ver_FW[6] = '9';
  9204. break;
  9205. #endif
  9206. }
  9207. }
  9208. #endif //FUNC_VERSION_USE_NEW_SPEC_20240926
  9209. //Version Naming Rule "c"
  9210. switch(Charger.memory.EVSE_Config.data.item.ModelName[10])
  9211. {
  9212. case 'B':
  9213. case 'U':
  9214. //Blue tooth
  9215. Charger.Ver_FW[9] = '3';
  9216. break;
  9217. case 'W':
  9218. // WIFI
  9219. Charger.Ver_FW[9] = '1';
  9220. break;
  9221. case 'T':
  9222. // 3G/4G
  9223. Charger.Ver_FW[9] = '2';
  9224. break;
  9225. #ifdef FUNC_ADD_FW_VER_WITH_NET_TYPE_WIFI_AND_4G
  9226. case 'D':
  9227. // WIFI + 4G
  9228. Charger.Ver_FW[9] = '5';
  9229. break;
  9230. #endif
  9231. default:
  9232. // LAN
  9233. Charger.Ver_FW[9] = '0';
  9234. break;
  9235. }
  9236. //Version Naming Rule "d3" , Get rating power from model name
  9237. memcpy(&Charger.Ver_FW[10], &Charger.memory.EVSE_Config.data.item.ModelName[4], 0x03);
  9238. // Get vender code from model name
  9239. memcpy(&Charger.Ver_FW[14], &Charger.memory.EVSE_Config.data.item.ModelName[12], 0x02);
  9240. //--------------------------------------
  9241. #ifdef MODIFY_OCP_PROC_WITH_MODELNAME_IDX_3_EURO_SPEC
  9242. HTK_BOOL bModelNameWithEuroSpecOCP = HTK_FALSE;
  9243. #endif
  9244. // CCID_Module_Type check
  9245. Charger.CCID_Module_Type = CCID_MODULE_CORMEX ;
  9246. switch(Charger.memory.EVSE_Config.data.item.ModelName[3])
  9247. {
  9248. case 'E': //CE / European Market
  9249. case 'T': //TR25 / Singapor Market
  9250. case 'Z': //EV ready / French Market
  9251. #ifdef MODIFY_OCP_PROC_WITH_MODELNAME_IDX_3_EURO_SPEC
  9252. case 'M': //E + MID Meter
  9253. case 'P': //E + PTB certification
  9254. case 'I': //Z + TIC Meter pcb
  9255. case 'F': //Z + MID Meter
  9256. bModelNameWithEuroSpecOCP = HTK_TRUE;
  9257. #endif
  9258. Charger.CCID_Module_Type = CCID_MODULE_VAC ;
  9259. ////#ifdef MODIFY_OCP_MAGNIFICATION
  9260. //// Charger.OCP_Magnification = 125 ;
  9261. ////#else
  9262. Charger.OCP_Magnification = 115 ;
  9263. ////#endif
  9264. break;
  9265. case 'U':
  9266. //UL / North America Market
  9267. Charger.CCID_Module_Type = CCID_MODULE_CORMEX ;
  9268. Charger.alarm_spec.Current_LEAK_AC = 1800 ; // unit 0.01mA
  9269. Charger.OCP_Magnification = 110 ;
  9270. break;
  9271. case 'G':
  9272. //GB / China Market
  9273. Charger.CCID_Module_Type = CCID_MODULE_CORMEX ;
  9274. Charger.alarm_spec.Current_LEAK_AC = 2500 ; // unit 0.01mA
  9275. Charger.OCP_Magnification = 110 ;
  9276. break;
  9277. case 'C':
  9278. //CNS / Taiwan Market
  9279. #ifdef FUNC_REPLACE_CORMEX_WITH_VAC_FOR_CNS_MODEL
  9280. Charger.CCID_Module_Type = CCID_MODULE_VAC;
  9281. #else
  9282. Charger.CCID_Module_Type = CCID_MODULE_CORMEX ;
  9283. #endif
  9284. Charger.alarm_spec.Current_LEAK_AC = 2500 ; // unit 0.01mA
  9285. Charger.OCP_Magnification = 110 ;
  9286. break;
  9287. case 'J':
  9288. //JARI / Japan Market
  9289. Charger.CCID_Module_Type = CCID_MODULE_CORMEX ;
  9290. Charger.alarm_spec.Current_LEAK_AC = 2500 ; // unit 0.01mA
  9291. Charger.OCP_Magnification = 110 ;
  9292. break;
  9293. case 'K':
  9294. //KC / Korea Market
  9295. Charger.CCID_Module_Type = CCID_MODULE_CORMEX ;
  9296. Charger.alarm_spec.Current_LEAK_AC = 1800 ; // unit 0.01mA
  9297. Charger.OCP_Magnification = 110 ;
  9298. break;
  9299. case 'B':
  9300. //British / UK Market
  9301. Charger.CCID_Module_Type = CCID_MODULE_CORMEX ;
  9302. Charger.alarm_spec.Current_LEAK_AC = 1800 ; // unit 0.01mA
  9303. Charger.OCP_Magnification = 110 ;
  9304. break;
  9305. default:
  9306. Charger.CCID_Module_Type = CCID_MODULE_CORMEX ;
  9307. Charger.alarm_spec.Current_LEAK_AC = 1800 ; // unit 0.01mA
  9308. Charger.OCP_Magnification = 110 ;
  9309. break;
  9310. }
  9311. #ifdef MODIFY_OCP_PROC_WITH_MODELNAME_IDX_3_EURO_SPEC
  9312. Charger.m_bModelNameWithEuroSpecOCP = bModelNameWithEuroSpecOCP;
  9313. XP("#Use EuropeanSpecOCP: %s\r\n", Charger.m_bModelNameWithEuroSpecOCP ? "True" : "False");
  9314. #endif
  9315. #ifdef FUNC_AUTO_MODIFY_CCID_MODULE
  9316. XP("#Use CCID Module (ModelName: %s): %s\r\n", Charger.memory.EVSE_Config.data.item.ModelName, CCID_MODULE_TYPE_STR);
  9317. if (Charger.m_bCCID_MustModify && Charger.m_CCID_ModuleTestOK != CCID_MODULE_UNKNOW)
  9318. {
  9319. Charger.CCID_Module_Type = Charger.m_CCID_ModuleTestOK;
  9320. XP("#Use CCID Module (AutoModify): %s\r\n", CCID_MODULE_TYPE_STR);
  9321. }
  9322. #endif
  9323. #ifdef FUNC_RS485_SLAVE
  9324. Charger.m_bRS485SlaveForDCModel =
  9325. (
  9326. (Charger.memory.EVSE_Config.data.item.ModelName[0] == 'D') //response to DC Model
  9327. ) ? 1 : 0;
  9328. Charger.m_RS485SlaveAddr = (Charger.m_bRS485SlaveForDCModel ? PROTOCOL_ADDR_AC_MAINBOARD : PROTOCOL_ADDR);
  9329. #endif
  9330. #ifdef FUNC_FORCE_RUN_CSU_MODE_WITH_DC_MODELNAME
  9331. Charger.m_bModelNameDC =
  9332. (
  9333. (Charger.memory.EVSE_Config.data.item.ModelName[0] == 'D') //response to DC Model
  9334. ) ? 1 : 0;
  9335. Charger.m_bModelNameDCReadOK = 1;
  9336. XP("#ModelNameDC: %s\r\n", Charger.m_bModelNameDC ? "True" : "False");
  9337. Update_McuCtrlMode();
  9338. #endif
  9339. #ifdef FUNC_CP_ADC_MODIFY
  9340. #ifdef NEW_CP_SPEC
  9341. CpDetectModuleInitialize(Charger.memory.EVSE_Config.data.item.ModelName[3]);
  9342. Charger.m_bCpDetectModuleInitOK = 1;
  9343. #endif //NEW_CP_SPEC
  9344. #endif //FUNC_CP_ADC_MODIFY
  9345. #ifdef FUNC_RULE_GB_202108
  9346. Charger.m_bSafetyRegulationGB = (Charger.memory.EVSE_Config.data.item.ModelName[3] == 'G');
  9347. #endif
  9348. #ifdef FUNC_UPDATE_ROTARY_SWITCH_SETTING_AFTER_PARSE_MODELNAME
  9349. getRotarySwitchSetting();
  9350. #endif
  9351. }
  9352. void HTK_ByteArray2HexStr(const u8* buf, const u16 BegIdx, const u16 len, char* OutStr, u16 nMaxBytesPerLine, HTK_BOOL bSpace)
  9353. {
  9354. char sTmp[8] = { 0 };
  9355. //char sTmp[8] = { 0 };
  9356. strcpy(OutStr, "\0");
  9357. for (u16 i = BegIdx; i < (BegIdx + len); i++)
  9358. {
  9359. sprintf(sTmp, bSpace ? "%02X " : "%02X", buf[i]);
  9360. strcat(OutStr, sTmp);
  9361. if ((i - BegIdx) % nMaxBytesPerLine == (nMaxBytesPerLine - 1))
  9362. {
  9363. strcat(OutStr, "\r\n");
  9364. }
  9365. }
  9366. }
  9367. void HTK_ByteArray2HexStr_XP(const char* Title, const u8* buf, const u16 BegIdx, const u16 len)
  9368. {
  9369. //char OutStr[256] = { 0 };
  9370. static char OutStr[1024 * 3 + 8] = { 0 };
  9371. HTK_ByteArray2HexStr(buf, BegIdx, len, OutStr, 0, HTK_TRUE);
  9372. XP("[%s] %s(%d)\r\n", (Title == NULL ? "" : Title), OutStr, len);
  9373. }
  9374. void HTK_ByteArray2HexStr_XT(const char* Title, const u8* buf, const u16 BegIdx, const u16 len)
  9375. {
  9376. //char OutStr[256] = { 0 };
  9377. static char OutStr[1024 * 3 + 8] = { 0 };
  9378. HTK_ByteArray2HexStr(buf, BegIdx, len, OutStr, 0, HTK_TRUE);
  9379. XT("[%s] %s(%d)\r\n", (Title == NULL ? "" : Title), OutStr, len);
  9380. }
  9381. #ifdef ENABLE_PRINT_IMCP_MSG
  9382. int CheckPrintImcpMsg(uint8_t MsgID)
  9383. {
  9384. #ifdef ENABLE_PRINT_IMCP_SPEC_MSG
  9385. return
  9386. (
  9387. // MsgID == PROTOCOL_MESSAGE_CONFIG_PARAMETER
  9388. // || MsgID == PROTOCOL_MESSAGE_QUERY_METER_IC_MEAS_PARAMETER
  9389. // || MsgID == PROTOCOL_MESSAGE_QUERY_METER_IC_CALI_PARAMETER
  9390. // || MsgID == PROTOCOL_MESSAGE_CONFIG_METER_IC_CALI_PARAMETER
  9391. MsgID == PROTOCOL_MESSAGE_CONFIG_RELAY_OUTPUT
  9392. || MsgID == PROTOCOL_MESSAGE_CONFIG_MAX_CURRENT_PWM_DUTY
  9393. || MsgID == PROTOCOL_MESSAGE_CONFIG_LEGACY_REQUEST
  9394. );
  9395. #else
  9396. return true;
  9397. #endif //ENABLE_PRINT_IMCP_SPEC_MSG
  9398. }
  9399. #endif //ENABLE_PRINT_IMCP_MSG
  9400. #ifdef MODIFY_COLD_LOAD_PICKUP_DELAY
  9401. void ColdLoadPickup_Delay(const char* Description)
  9402. {
  9403. //osDelay((rand()%175000)+5000);
  9404. int r = rand();
  9405. if (r < 0)
  9406. {
  9407. r = (-1) * r;
  9408. }
  9409. //uint32_t dt = (r % 175000) + 5000; //ms
  9410. uint32_t dt = (r % (COLD_LOAD_PICKUP_DELAY_TIME_MAX_RANGE_SEC * 1000)) + 5000; //ms
  9411. XP("ColdLoadPickup_Delay(ms) [%s]: %d\r\n", Description, dt);
  9412. osDelay(dt);
  9413. }
  9414. #endif
  9415. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  9416. uint16_t GetCpPwmDuty(uint16_t Curr)
  9417. {
  9418. uint16_t rtn = (uint16_t)((Curr / 0.6) * 10);
  9419. XP("#GetCpPwmDuty(%d) => %d\r\n", Curr, rtn);
  9420. return rtn;
  9421. }
  9422. #endif
  9423. #ifdef HTK
  9424. HTK_BOOL HTK_IsInTime(u32 BegTick, u32 MaxTick)
  9425. {
  9426. u32 EndTick = HAL_GetTick();
  9427. u32 ElapseTick = (EndTick >= BegTick ? EndTick - BegTick : HTK_U32_MAX - BegTick + EndTick);
  9428. return ElapseTick <= MaxTick ? HTK_TRUE : HTK_FALSE;
  9429. }
  9430. HTK_BOOL HTK_IsTimeout(u32 BegTick, u32 MaxTick)
  9431. {
  9432. u32 EndTick = HAL_GetTick();
  9433. u32 ElapseTick = (EndTick >= BegTick ? EndTick - BegTick : HTK_U32_MAX - BegTick + EndTick);
  9434. return ElapseTick > MaxTick ? HTK_TRUE : HTK_FALSE;
  9435. }
  9436. void HTK_Timer_Reset(PHTK_Timer p)
  9437. {
  9438. XP("#<Timer reset>\r\n\r\n");
  9439. p->m_bStart = HTK_FALSE;
  9440. p->m_bTimeout = HTK_FALSE;
  9441. p->m_BegTick = 0;
  9442. p->m_MaxTick = 0;
  9443. }
  9444. void HTK_Timer_Start(PHTK_Timer p, u32 MaxTick)
  9445. {
  9446. XP("#<Timer start>\r\n\r\n");
  9447. p->m_bStart = HTK_TRUE;
  9448. p->m_BegTick = HAL_GetTick();
  9449. p->m_MaxTick = MaxTick;
  9450. }
  9451. HTK_BOOL HTK_Timer_CheckTimeout(PHTK_Timer p)
  9452. {
  9453. // return HTK_IsTimeout(p->m_BegTick, p->m_MaxTick);
  9454. HTK_BOOL rtn = HTK_IsTimeout(p->m_BegTick, p->m_MaxTick);
  9455. if (rtn == HTK_TRUE)
  9456. {
  9457. XP("#<Timer timeout>\r\n\r\n");
  9458. p->m_bTimeout = HTK_TRUE;
  9459. }
  9460. return rtn;
  9461. }
  9462. uint8_t HTK_CheckSum_Calulate(uint8_t* pBeg, uint32_t len)
  9463. {
  9464. if (pBeg != NULL)
  9465. {
  9466. uint8_t CheckSum = 0;
  9467. for (uint32_t i = 0; i < len; i++)
  9468. {
  9469. CheckSum ^= *(pBeg + i);
  9470. }
  9471. return CheckSum;
  9472. }
  9473. else
  9474. {
  9475. return 0;
  9476. }
  9477. }
  9478. HTK_BOOL HTK_CheckSum_Update(uint8_t* pBeg, uint32_t len, uint8_t* pCheckSum)
  9479. {
  9480. if (pBeg != NULL && pCheckSum != NULL)
  9481. {
  9482. uint8_t CheckSum = HTK_CheckSum_Calulate(pBeg, len);
  9483. *pCheckSum = CheckSum;
  9484. return HTK_TRUE;
  9485. }
  9486. else
  9487. {
  9488. return HTK_FALSE;
  9489. }
  9490. }
  9491. HTK_BOOL HTK_CheckSum_IsOK(uint8_t* pBeg, uint32_t len, uint8_t* pCheckSum)
  9492. {
  9493. if (pBeg != NULL && pCheckSum != NULL)
  9494. {
  9495. uint8_t CheckSum = HTK_CheckSum_Calulate(pBeg, len);
  9496. return (*pCheckSum == CheckSum) ? HTK_TRUE : HTK_FALSE;
  9497. }
  9498. else
  9499. {
  9500. return HTK_FALSE;
  9501. }
  9502. }
  9503. static HTK_MallocHandle s_MallocHandle;
  9504. void HTK_Malloc_Init(void)
  9505. {
  9506. memset(&s_MallocHandle, 0, sizeof(s_MallocHandle));
  9507. //XP("HTK_Malloc_Init(): %d\r\n", HTK_Malloc_BlockTest(1024));
  9508. XP("HTK_Malloc_Init(): %d\r\n", HTK_Malloc_BlockTest(8000));
  9509. }
  9510. void* HTK_Malloc(s32 size)
  9511. {
  9512. void* p = malloc(size);
  9513. if (p != NULL)
  9514. {
  9515. s_MallocHandle.m_Allocated += size;
  9516. s_MallocHandle.m_Reserved -= size;
  9517. memset(p, 0, size);
  9518. }
  9519. XP("HTK_Malloc(%d) %s\r\n", size, (p == NULL) ? "*** NG ***" : "OK");
  9520. return p;
  9521. }
  9522. s32 HTK_Malloc_BlockTest(s32 size)
  9523. {
  9524. int Total = 0;
  9525. for (int i = 1; ; i++)
  9526. {
  9527. void* p = HTK_Malloc(size);
  9528. if (p == NULL)
  9529. {
  9530. break;
  9531. }
  9532. else
  9533. {
  9534. Total += size;
  9535. }
  9536. }
  9537. s_MallocHandle.m_Total = Total;
  9538. s_MallocHandle.m_Allocated = 0;
  9539. s_MallocHandle.m_Reserved = Total;
  9540. return Total;
  9541. }
  9542. #endif //HTK
  9543. #ifdef MODIFY_CPTASK_HEAD
  9544. void Proc_CpTaskHead(HTK_BOOL bCSU)
  9545. {
  9546. if(Charger.Alarm_Code > 0) // alarm occur
  9547. {
  9548. if(Charger.Mode != MODE_ALARM)
  9549. {
  9550. #ifdef FUNC_BLOCK_CSU_CMD_SET_DUTY_WHEN_ALARM_CP_F_STATE
  9551. Charger.m_bBlockCsuCpPwmDutyWhenAlarmCpF = HTK_TRUE;
  9552. #endif
  9553. HAL_GPIO_WritePin(OUT_StateE_GPIO_Port, OUT_StateE_Pin, GPIO_PIN_SET);
  9554. setChargerMode(MODE_ALARM);
  9555. user_pwm_setvalue(PWM_CH_CP, 0);
  9556. timerEnable(TIMER_IDX_STATE_E, 4000);
  9557. Charger.isAlarmSetPWM = OFF ;
  9558. }
  9559. // CP E or F to X1
  9560. if(timer[TIMER_IDX_STATE_E].isAlarm == ON)
  9561. {
  9562. HAL_GPIO_WritePin(OUT_StateE_GPIO_Port, OUT_StateE_Pin, GPIO_PIN_RESET);
  9563. if ( Charger.isAlarmSetPWM == OFF )
  9564. {
  9565. Charger.isAlarmSetPWM = ON ;
  9566. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  9567. }
  9568. #ifdef FUNC_BLOCK_CSU_CMD_SET_DUTY_WHEN_ALARM_CP_F_STATE
  9569. if (Charger.m_bBlockCsuCpPwmDutyWhenAlarmCpF)
  9570. {
  9571. Charger.m_bBlockCsuCpPwmDutyWhenAlarmCpF = HTK_FALSE;
  9572. }
  9573. #endif
  9574. }
  9575. }
  9576. else
  9577. {
  9578. if(Charger.Mode != MODE_ALARM)
  9579. {
  9580. //if (!bCSU) //only in AX code
  9581. {
  9582. HAL_GPIO_WritePin(OUT_StateE_GPIO_Port, OUT_StateE_Pin, GPIO_PIN_RESET); //CCS communication, so skip this line
  9583. }
  9584. timerDisable(TIMER_IDX_STATE_E);
  9585. }
  9586. #ifdef FIX_OUTP_PWM_WHEN_ALARM_RECOVER_IN_4SEC
  9587. #ifdef FUNC_BLOCK_CSU_CMD_SET_DUTY_WHEN_ALARM_CP_F_STATE
  9588. if (Charger.m_bBlockCsuCpPwmDutyWhenAlarmCpF)
  9589. {
  9590. Charger.m_bBlockCsuCpPwmDutyWhenAlarmCpF = HTK_FALSE;
  9591. }
  9592. #endif
  9593. #endif
  9594. }
  9595. }
  9596. #endif //MODIFY_CPTASK_HEAD
  9597. #ifdef FUNC_FORCE_RUN_CSU_MODE_WITH_DC_MODELNAME
  9598. void Update_McuCtrlMode(void)
  9599. {
  9600. while (!Charger.m_bModelNameDCReadOK)
  9601. {
  9602. osDelay(100);
  9603. }
  9604. #ifdef FUNC_EMPX10_CUSTOMIZE_RUN_CSU_MODE
  9605. {
  9606. Charger.memory.EVSE_Config.data.item.MCU_Control_Mode = MCU_CONTROL_MODE_CSU;
  9607. DEBUG_INFO("#MCU_CONTROL_MODE_CSU\r\n");
  9608. }
  9609. #else
  9610. if (HAL_GPIO_ReadPin(IN_ACT_REQ_GPIO_Port, IN_ACT_REQ_Pin) == GPIO_PIN_RESET ||
  9611. Charger.m_bModelNameDC == 1)
  9612. {
  9613. Charger.memory.EVSE_Config.data.item.MCU_Control_Mode = MCU_CONTROL_MODE_CSU;
  9614. DEBUG_INFO("#MCU_CONTROL_MODE_CSU\r\n");
  9615. }
  9616. else
  9617. {
  9618. Charger.memory.EVSE_Config.data.item.MCU_Control_Mode = MCU_CONTROL_MODE_NO_CSU;
  9619. DEBUG_INFO("#MCU_CONTROL_MODE_NO_CSU\r\n");
  9620. // if (Charger.memory.EVSE_Config.data.item.AuthMode > AUTH_MODE_FREE)
  9621. // {
  9622. // Charger.memory.EVSE_Config.data.item.AuthMode = AUTH_MODE_FREE ;
  9623. // DEBUG_INFO("#AuthMode set defualt AUTH_MODE_FREE \r\n");
  9624. // }
  9625. //
  9626. // if (Charger.memory.EVSE_Config.data.item.OfflinePolicy > RFID_USER_AUTH_NO_CHARGING)
  9627. // {
  9628. // Charger.memory.EVSE_Config.data.item.OfflinePolicy = RFID_USER_AUTH_FREE ;
  9629. // DEBUG_INFO("#OfflinePolicy set defualt RFID_USER_AUTH_FREE \r\n");
  9630. // }
  9631. }
  9632. #endif //FUNC_EMPX10_CUSTOMIZE_RUN_CSU_MODE
  9633. }
  9634. #endif //FUNC_FORCE_RUN_CSU_MODE_WITH_DC_MODELNAME
  9635. #ifdef MODIFY_DC_RS485_UPGRADE_ISSUE
  9636. void RS485_TX_Enable(void)
  9637. {
  9638. HAL_GPIO_WritePin(OUT_Meter_485_DE_GPIO_Port, OUT_Meter_485_DE_Pin, GPIO_PIN_SET);
  9639. HAL_GPIO_WritePin(OUT_Meter_485_RE_GPIO_Port, OUT_Meter_485_RE_Pin, GPIO_PIN_SET);
  9640. osDelay(1);
  9641. }
  9642. void RS485_TX_Disable(void)
  9643. {
  9644. HAL_GPIO_WritePin(OUT_Meter_485_DE_GPIO_Port, OUT_Meter_485_DE_Pin, GPIO_PIN_RESET);
  9645. HAL_GPIO_WritePin(OUT_Meter_485_RE_GPIO_Port, OUT_Meter_485_RE_Pin, GPIO_PIN_RESET);
  9646. }
  9647. void Prefix_UartTX(uint8_t* TxBuf, uint8_t* RxBuf)
  9648. {
  9649. #ifdef FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  9650. if (RxBuf == UART_IAP_rx_buffer)
  9651. {
  9652. TxBuf[1] = RxBuf[2];
  9653. }
  9654. else
  9655. #endif
  9656. if (RxBuf == UART_RS485_rx_buffer)
  9657. {
  9658. RS485_TX_Enable();
  9659. #ifdef FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  9660. //TxBuf[1] = RxBuf[2]; //Charger.m_EMPX10_RS485_ClientAddr;
  9661. TxBuf[1] = Charger.m_EMPX10_RS485_ClientAddr;
  9662. #else
  9663. TxBuf[1] = Charger.m_RS485SlaveAddr; //Update Slave Address
  9664. #endif
  9665. }
  9666. }
  9667. void Postfix_UartTX(uint8_t* TxBuf, uint8_t* RxBuf)
  9668. {
  9669. if (RxBuf == UART_RS485_rx_buffer)
  9670. {
  9671. RS485_TX_Disable();
  9672. }
  9673. }
  9674. #endif //MODIFY_DC_RS485_UPGRADE_ISSUE
  9675. /* USER CODE END Application */
  9676. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/