freertos.c 412 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296
  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. }
  273. //---------------
  274. #ifdef FUNC_STM32_FLASH_INFO
  275. enum { STM32_UID_LEN = 12 }; //96 bits (12 bytes)
  276. typedef struct _STM32_Flash_Info
  277. {
  278. union
  279. {
  280. u8 m_UID[STM32_UID_LEN];
  281. u32 m_UIDw[3];
  282. };
  283. u32 m_DeviceID;
  284. u32 m_FlashSizeKB;
  285. }
  286. STM32_Flash_Info, *PSTM32_Flash_Info;
  287. void STM32_Flash_Info_Init(PSTM32_Flash_Info p)
  288. {
  289. memset(p, 0, sizeof(STM32_Flash_Info));
  290. }
  291. void STM32_Flash_Info_Read(PSTM32_Flash_Info p)
  292. {
  293. //UID
  294. memcpy(p->m_UID, (u8*)UID_BASE, STM32_UID_LEN);
  295. //Device ID
  296. p->m_DeviceID = (uint32_t)(READ_BIT(DBGMCU->IDCODE, DBGMCU_IDCODE_DEV_ID));
  297. //Flash size
  298. p->m_FlashSizeKB = (uint32_t)(READ_REG(*((uint32_t *)FLASHSIZE_BASE)) & 0xFFFF);
  299. }
  300. void STM32_Flash_Info_Disp(PSTM32_Flash_Info p)
  301. {
  302. XP("[FlashInfo]------------------------\r\n");
  303. //UID
  304. HTK_ByteArray2HexStr_XP("UID", p->m_UID, 0, STM32_UID_LEN);
  305. //Device ID
  306. XP("DeviceID: 0x%03X\r\n", p->m_DeviceID);
  307. {
  308. char *s = "DeviceFamily: ";
  309. if (p->m_DeviceID == 0x413)
  310. {
  311. XP("%sSTM32F40xxx/F41xxx\r\n", s);
  312. }
  313. else if (p->m_DeviceID == 0x419)
  314. {
  315. XP("%sSTM32F42xxx/F43xxx\r\n", s);
  316. }
  317. else
  318. {
  319. XP("%sUnknow\r\n", s);
  320. }
  321. }
  322. //Flash size
  323. XP("DeviceFlashSize: %d Kbytes\r\n", p->m_FlashSizeKB);
  324. }
  325. void STM32_Flash_Info_Proc(void)
  326. {
  327. PSTM32_Flash_Info p = HTK_Malloc(sizeof(STM32_Flash_Info));
  328. if (p != NULL)
  329. {
  330. STM32_Flash_Info_Init(p);
  331. STM32_Flash_Info_Read(p);
  332. STM32_Flash_Info_Disp(p);
  333. }
  334. else
  335. {
  336. XP("PSTM32_Flash_Info malloc NG\r\n");
  337. }
  338. }
  339. #endif //FUNC_STM32_FLASH_INFO
  340. #ifdef FUNC_STM32_FLASH_OPTION_BYTES
  341. typedef struct _STM32_Flash_OB
  342. {
  343. FLASH_OBProgramInitTypeDef m_SetOB;
  344. FLASH_OBProgramInitTypeDef m_CurOB;
  345. u8 m_nRST_STDBY: 1; //0: Reset generated when entering the Standby mode
  346. //1: No reset generated
  347. u8 m_nRST_STOP: 1; //0: Reset generated when entering the Stop mode
  348. //1: No reset generated
  349. u8 m_WDG_SW: 1; //0: Hardware independent watchdog
  350. //1: Software independent watchdog
  351. }
  352. STM32_Flash_OB, *PSTM32_Flash_OB;
  353. void STM32_Flash_OB_Init(PSTM32_Flash_OB p)
  354. {
  355. memset(p, 0, sizeof(STM32_Flash_OB));
  356. //Set default
  357. p->m_SetOB.OptionType = OPTIONBYTE_WRP /*| OPTIONBYTE_RDP*/ | OPTIONBYTE_USER | OPTIONBYTE_BOR;
  358. p->m_SetOB.WRPState = OB_WRPSTATE_ENABLE;
  359. p->m_SetOB.Banks = 0;
  360. p->m_SetOB.BORLevel = OB_BOR_LEVEL3;
  361. p->m_SetOB.USERConfig = OB_IWDG_SW | OB_STOP_NO_RST | OB_STDBY_NO_RST;
  362. //#ifdef FUNC_USE_STM32_SBSFU
  363. // //Sector_0~4: Write Protection; Sector_5~11: No Write Protection
  364. // p->m_SetOB.WRPSector = OB_WRP_SECTOR_0 | OB_WRP_SECTOR_1 | OB_WRP_SECTOR_2 | OB_WRP_SECTOR_3 | OB_WRP_SECTOR_4;
  365. // p->m_SetOB.RDPLevel = OB_RDP_LEVEL_1;
  366. //#else
  367. //Sector_0~11: No Write Protection
  368. p->m_SetOB.WRPSector = ~OB_WRP_SECTOR_All;
  369. p->m_SetOB.RDPLevel = OB_RDP_LEVEL_0;
  370. //#endif
  371. }
  372. void STM32_Flash_OB_Read(PSTM32_Flash_OB p)
  373. {
  374. __HAL_FLASH_PREFETCH_BUFFER_DISABLE();
  375. HAL_FLASHEx_OBGetConfig(&p->m_CurOB);
  376. __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
  377. p->m_nRST_STDBY = BIT_GET(p->m_CurOB.USERConfig, 7);
  378. p->m_nRST_STOP = BIT_GET(p->m_CurOB.USERConfig, 6);
  379. p->m_WDG_SW = BIT_GET(p->m_CurOB.USERConfig, 5);
  380. }
  381. void STM32_Flash_OB_WriteDefault(PSTM32_Flash_OB p)
  382. {
  383. XP("<Write default OB>\r\n");
  384. __HAL_FLASH_PREFETCH_BUFFER_DISABLE();
  385. HAL_FLASH_Unlock();
  386. HAL_FLASH_OB_Unlock();
  387. {
  388. {
  389. XP("<Set OB Default>\r\n");
  390. HAL_FLASHEx_OBProgram(&p->m_SetOB);
  391. HAL_FLASH_OB_Launch();
  392. }
  393. {
  394. FLASH_OBProgramInitTypeDef ob;
  395. memcpy(&ob, &p->m_SetOB, sizeof(p->m_SetOB));
  396. ob.OptionType = OPTIONBYTE_WRP;
  397. ob.WRPState = OB_WRPSTATE_DISABLE;
  398. ob.WRPSector = (~p->m_SetOB.WRPSector) & OB_WRP_SECTOR_All;
  399. XP("<Set OB.WRPSector => Disable>\r\n");
  400. HAL_FLASHEx_OBProgram(&ob);
  401. HAL_FLASH_OB_Launch();
  402. }
  403. if (p->m_CurOB.RDPLevel == OB_RDP_LEVEL_0 && p->m_SetOB.RDPLevel == OB_RDP_LEVEL_1)
  404. {
  405. FLASH_OBProgramInitTypeDef ob;
  406. memcpy(&ob, &p->m_SetOB, sizeof(p->m_SetOB));
  407. ob.OptionType = OPTIONBYTE_RDP;
  408. XP("<Set OB.RDPLevel => Level 1> Please power off and then power on to restart !\r\n");
  409. HAL_FLASHEx_OBProgram(&ob);
  410. HAL_FLASH_OB_Launch();
  411. /* Set the OPTSTRT bit in OPTCR register */
  412. // XP("*(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= FLASH_OPTCR_OPTSTRT [BEG]\r\n");
  413. // *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= FLASH_OPTCR_OPTSTRT;
  414. // XP("*(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= FLASH_OPTCR_OPTSTRT [BEG]\r\n");
  415. //HAL_Delay(1000);
  416. //NVIC_SystemReset();
  417. }
  418. }
  419. HAL_FLASH_OB_Lock();
  420. HAL_FLASH_Lock();
  421. __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
  422. }
  423. void STM32_Flash_OB_Disp(PSTM32_Flash_OB p)
  424. {
  425. XP("[OB]-------------------------------\r\n");
  426. XP("OptionType: 0x%08X\r\n", p->m_CurOB.OptionType);
  427. XP("WRPState : 0x%08X\r\n", p->m_CurOB.WRPState);
  428. XP("WRPSector : 0x%08X\r\n", p->m_CurOB.WRPSector);
  429. {
  430. for (int i = 0; i < 12; i++)
  431. {
  432. XP("\tSector_%d: %s Protection\r\n", i, BIT_GET(p->m_CurOB.WRPSector, i) == 1 ? "No" : "Write");
  433. }
  434. }
  435. XP("Banks : 0x%08X\r\n", p->m_CurOB.Banks);
  436. XP("RDPLevel : 0x%08X (0: 0xAA, 1: 0x55, 2: 0xCC)\r\n", p->m_CurOB.RDPLevel);
  437. XP("BORLevel : 0x%08X (3: 0x00, 2: 0x04, 1: 0x08, OFF: 0x0C)\r\n", p->m_CurOB.BORLevel);
  438. XP("USERConfig: 0x%02X\r\n", p->m_CurOB.USERConfig);
  439. {
  440. XP("\tnRST_STDBY = %d (%s)\r\n", p->m_nRST_STDBY, (p->m_nRST_STDBY == 0 ? "STDBY_RST(0)" : "STDBY_NO_RST(0x80)"));
  441. XP("\tnRST_STOP = %d (%s)\r\n", p->m_nRST_STOP, (p->m_nRST_STOP == 0 ? "STOP_RST(0)" : "STOP_NO_RST(0x40)"));
  442. XP("\tWDG_SW = %d (%s)\r\n", p->m_WDG_SW, (p->m_WDG_SW == 0 ? "IWDG_HW(0)" : "IWDG_SW(0x20)"));
  443. }
  444. }
  445. HTK_BOOL STM32_Flash_OB_IsDefault(PSTM32_Flash_OB p)
  446. {
  447. XP("m_CurOB.BORLevel = 0x%08X, m_SetOB.BORLevel = 0x%08X\r\n", p->m_CurOB.BORLevel, p->m_SetOB.BORLevel);
  448. XP("m_CurOB.RDPLevel = 0x%08X, m_SetOB.RDPLevel = 0x%08X\r\n", p->m_CurOB.RDPLevel, p->m_SetOB.RDPLevel);
  449. XP("m_CurOB.USERConfig = 0x%08X, m_SetOB.USERConfig = 0x%08X\r\n", p->m_CurOB.USERConfig, p->m_SetOB.USERConfig);
  450. XP("m_CurOB.WRPSector = 0x%08X, m_SetOB.WRPSector = 0x%08X\r\n", p->m_CurOB.WRPSector, p->m_SetOB.WRPSector);
  451. XP("(~p->m_SetOB.WRPSector) & OB_WRP_SECTOR_All = 0x%08X\r\n", (~p->m_SetOB.WRPSector) & OB_WRP_SECTOR_All);
  452. if (
  453. (p->m_CurOB.BORLevel == p->m_SetOB.BORLevel) &&
  454. (p->m_CurOB.RDPLevel == p->m_SetOB.RDPLevel) &&
  455. (p->m_CurOB.USERConfig == p->m_SetOB.USERConfig) &&
  456. (p->m_CurOB.WRPSector == ((~p->m_SetOB.WRPSector) & OB_WRP_SECTOR_All))
  457. )
  458. {
  459. XP("<OB is Default>\r\n");
  460. return HTK_TRUE;
  461. }
  462. else
  463. {
  464. XP("<OB is NOT Default>\r\n");
  465. return HTK_FALSE;
  466. }
  467. }
  468. void STM32_Flash_OB_ProcDefault(void)
  469. {
  470. PSTM32_Flash_OB p = HTK_Malloc(sizeof(STM32_Flash_OB));
  471. if (p != NULL)
  472. {
  473. STM32_Flash_OB_Init(p);
  474. STM32_Flash_OB_Read(p);
  475. STM32_Flash_OB_Disp(p);
  476. if (!STM32_Flash_OB_IsDefault(p))
  477. {
  478. STM32_Flash_OB_WriteDefault(p);
  479. STM32_Flash_OB_Read(p);
  480. STM32_Flash_OB_Disp(p);
  481. if (STM32_Flash_OB_IsDefault(p))
  482. {
  483. XP("<Write default OB success>\r\n");
  484. }
  485. else
  486. {
  487. XP("<Write default OB fail>\r\n");
  488. }
  489. }
  490. }
  491. else
  492. {
  493. XP("STM32_Flash_OB malloc NG\r\n");
  494. }
  495. }
  496. #endif //FUNC_STM32_FLASH_OPTION_BYTES
  497. void MX_FREERTOS_Init(void) {
  498. /* USER CODE BEGIN Init */
  499. #ifdef FUNC_BOOT_TICK
  500. Charger.m_BootTick = HAL_GetTick();
  501. #endif
  502. #ifdef FUNC_STM32_FLASH_INFO
  503. STM32_Flash_Info_Proc();
  504. #endif //FUNC_STM32_FLASH_INFO
  505. #ifdef FUNC_STM32_FLASH_OPTION_BYTES
  506. STM32_Flash_OB_ProcDefault();
  507. #endif
  508. UpdateFirmwareVersion();
  509. sprintf(Charger.Ver_HW, "RA2");
  510. HAL_GPIO_WritePin(OUT_PHY_Reset_GPIO_Port, OUT_PHY_Reset_Pin, GPIO_PIN_RESET);
  511. HAL_GPIO_WritePin(OUT_Leak_Test_GPIO_Port, OUT_Leak_Test_Pin, GPIO_PIN_SET);
  512. /* USER CODE END Init */
  513. /* USER CODE BEGIN RTOS_MUTEX */
  514. /* add mutexes, ... */
  515. /* USER CODE END RTOS_MUTEX */
  516. /* USER CODE BEGIN RTOS_SEMAPHORES */
  517. /* add semaphores, ... */
  518. /* USER CODE END RTOS_SEMAPHORES */
  519. /* USER CODE BEGIN RTOS_TIMERS */
  520. /* start timers, add new ones, ... */
  521. /* USER CODE END RTOS_TIMERS */
  522. /* USER CODE BEGIN RTOS_QUEUES */
  523. /* add queues, ... */
  524. /* USER CODE END RTOS_QUEUES */
  525. /* Create the thread(s) */
  526. /* definition and creation of ethTask */
  527. #ifndef DISABLE_OS_ETH_TASK
  528. osThreadDef(ethTask, StartEthTask, osPriorityIdle, 0, 1024);
  529. ethTaskHandle = osThreadCreate(osThread(ethTask), NULL);
  530. #endif
  531. /* definition and creation of uartTask */
  532. osThreadDef(uartTask, StartUartTask, osPriorityNormal, 0, 1024);
  533. uartTaskHandle = osThreadCreate(osThread(uartTask), NULL);
  534. /* definition and creation of adcTask */
  535. osThreadDef(adcTask, StartAdcTask, osPriorityRealtime, 0, 256);
  536. adcTaskHandle = osThreadCreate(osThread(adcTask), NULL);
  537. /* definition and creation of timeoutTask */
  538. osThreadDef(timeoutTask, StartTimeoutTask, osPriorityAboveNormal, 0, 128);
  539. timeoutTaskHandle = osThreadCreate(osThread(timeoutTask), NULL);
  540. /* definition and creation of rfidTask */
  541. osThreadDef(rfidTask, StartRfidTask, osPriorityIdle, 0, 256);
  542. rfidTaskHandle = osThreadCreate(osThread(rfidTask), NULL);
  543. /* definition and creation of led_speakerTask */
  544. osThreadDef(led_speakerTask, StartLedSpeakerTask, osPriorityAboveNormal, 0, 128);
  545. led_speakerTaskHandle = osThreadCreate(osThread(led_speakerTask), NULL);
  546. /* definition and creation of cpTask */
  547. osThreadDef(cpTask, StartCpTask, osPriorityHigh, 0, 256);
  548. cpTaskHandle = osThreadCreate(osThread(cpTask), NULL);
  549. /* definition and creation of alarmTask */
  550. osThreadDef(alarmTask, StartAlarmTask, osPriorityHigh, 0, 256);
  551. alarmTaskHandle = osThreadCreate(osThread(alarmTask), NULL);
  552. /* definition and creation of bleTask */
  553. #ifdef LOCAL_LINK_BLE
  554. osThreadDef(bleTask, StartBleTask, osPriorityHigh, 0, 1024);
  555. bleTaskHandle = osThreadCreate(osThread(bleTask), NULL);
  556. #endif
  557. /* definition and creation of memoryTask */
  558. osThreadDef(memoryTask, StartMemoryTask, osPriorityIdle, 0, 256);
  559. memoryTaskHandle = osThreadCreate(osThread(memoryTask), NULL);
  560. /* definition and creation of wifiTask */
  561. #ifdef LOCAL_LINK_WIFI
  562. osThreadDef(wifiTask, StartWifiTask, osPriorityIdle, 0, 1024);
  563. wifiTaskHandle = osThreadCreate(osThread(wifiTask), NULL);
  564. #endif
  565. /* USER CODE BEGIN RTOS_THREADS */
  566. /* add threads, ... */
  567. /* USER CODE END RTOS_THREADS */
  568. }
  569. #ifndef DISABLE_OS_ETH_TASK
  570. /* USER CODE BEGIN Header_StartEthTask */
  571. /**
  572. * @brief Function implementing the ethTask thread.
  573. * @param argument: Not used
  574. * @retval None
  575. */
  576. /* USER CODE END Header_StartEthTask */
  577. void StartEthTask(void const * argument)
  578. {
  579. /* init code for USB_DEVICE */
  580. MX_USB_DEVICE_Init();
  581. /* USER CODE BEGIN StartEthTask */
  582. /* Infinite loop */
  583. for(;;)
  584. {
  585. osDelay(1);
  586. }
  587. /* USER CODE END StartEthTask */
  588. }
  589. #endif //DISABLE_OS_ETH_TASK
  590. /* USER CODE BEGIN Header_StartUartTask */
  591. /**
  592. * @brief Function implementing the uartTask thread.
  593. * @param argument: Not used
  594. * @retval None
  595. */
  596. /* USER CODE END Header_StartUartTask */
  597. void StartUartTask(void const * argument)
  598. {
  599. /* USER CODE BEGIN StartUartTask */
  600. //uuart
  601. #ifndef DISABLE_PRINT_RTOS_MSG
  602. uint8_t pcWriteBuffer[1024];
  603. #endif
  604. uint8_t endFlag[4]={0x55,0xaa,0x55,0xaa};
  605. __IO uint32_t flash;
  606. uint32_t checksum;
  607. uint8_t dbgIdx;
  608. #if defined(DEBUG) || defined(RTOS_STAT)
  609. DEBUG_INFO("========== AP initial OK ==========\n\r");
  610. DEBUG_INFO(" Version: %s\n\r", Charger.Ver_FW);
  611. DEBUG_INFO("===================================\n\r");
  612. if (__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST) != RESET) DEBUG_INFO("Reset flag: Pin reset \r\n");
  613. if (__HAL_RCC_GET_FLAG(RCC_FLAG_PORRST) != RESET) DEBUG_INFO("Reset flag: POR reset \r\n");
  614. if (__HAL_RCC_GET_FLAG(RCC_FLAG_SFTRST) != RESET) DEBUG_INFO("Reset flag: SW reset \r\n");
  615. if (__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST) != RESET) DEBUG_INFO("Reset flag: IWDG reset \r\n");
  616. if (__HAL_RCC_GET_FLAG(RCC_FLAG_WWDGRST) != RESET) DEBUG_INFO("Reset flag: WWDG reset \r\n");
  617. if (__HAL_RCC_GET_FLAG(RCC_FLAG_LPWRRST) != RESET) DEBUG_INFO("Reset flag: LP reset \r\n");
  618. DEBUG_INFO("\r\n");
  619. __HAL_RCC_CLEAR_RESET_FLAGS();
  620. #endif
  621. Charger.CSUisReady = OFF ;
  622. #ifdef FUNC_RS485_SLAVE
  623. HAL_GPIO_WritePin(OUT_Meter_485_DE_GPIO_Port, OUT_Meter_485_DE_Pin, GPIO_PIN_RESET);
  624. HAL_GPIO_WritePin(OUT_Meter_485_RE_GPIO_Port, OUT_Meter_485_RE_Pin, GPIO_PIN_RESET);
  625. #endif
  626. #ifdef FUNC_RELAY_OFF_WHEN_GET_TWICE_OFF_CMD
  627. u8 CsuSetRelayOffCount = 0;
  628. #endif
  629. #ifdef FUNC_REQUEST_OFF_WHEN_GET_TWICE_OFF_CMD
  630. u8 CsuSetRequestOffCount = 0;
  631. #endif
  632. /* Infinite loop */
  633. for(;;)
  634. {
  635. timerEnable(TIMER_IDX_RTOS_STAT, 500);
  636. if(timer[TIMER_IDX_RTOS_STAT].isAlarm)
  637. {
  638. switch(dbgIdx)
  639. {
  640. case 0:
  641. #ifdef RTOS_STAT
  642. #ifndef DISABLE_PRINT_RTOS_MSG
  643. printf("=================================================\r\n");
  644. printf("Task_Name \tState \tPrior \tStack \tTask_Id\r\n");
  645. vTaskList((char *)&pcWriteBuffer);
  646. printf("%s\r\n", pcWriteBuffer);
  647. printf("-------------------------------------------------\r\n");
  648. printf("Task_Name \tRun_Time(10us) \tUsage_Percent\r\n");
  649. vTaskGetRunTimeStats((char *)&pcWriteBuffer);
  650. printf("%s\r\n", pcWriteBuffer);
  651. #endif //DISABLE_PRINT_RTOS_MSG
  652. #endif
  653. dbgIdx++;
  654. break;
  655. case 1:
  656. #ifdef FUNC_EMPX10_CUSTOMIZE
  657. DEBUG_INFO("Firmware version: %s (AWeMP)\r\n", Charger.Ver_FW);
  658. #else
  659. DEBUG_INFO("Firmware version: %s\r\n", Charger.Ver_FW);
  660. #endif
  661. #ifdef FUNC_BUILD_DATE_TIME
  662. DEBUG_INFO("Build: %s %s\r\n", __DATE__, __TIME__); //ex: Build: Mar 10 2022 14:53:25
  663. #endif
  664. //DEBUG_INFO("MCU_Control_Mode: %x\r\n",Charger.memory.EVSE_Config.data.item.MCU_Control_Mode);
  665. DEBUG_INFO("MCU_Control_Mode: %x (%s)\r\n",Charger.memory.EVSE_Config.data.item.MCU_Control_Mode,
  666. Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_CSU ? "CSU" : "No CSU");
  667. DEBUG_INFO("ModelName: %s\r\n",Charger.memory.EVSE_Config.data.item.ModelName);
  668. DEBUG_INFO("SerialNumber: %s\r\n",Charger.memory.EVSE_Config.data.item.SerialNumber);
  669. #ifdef FUNC_BOOT_TICK
  670. DEBUG_INFO("UpTime(s): %d\r\n", (HAL_GetTick() - Charger.m_BootTick) / 1000);
  671. #endif
  672. dbgIdx++;
  673. break;
  674. case 2:
  675. if (Charger.CCID_Module_Type == CCID_MODULE_CORMEX)
  676. {
  677. DEBUG_INFO("Charger.CCID_Module_Type is CCID_MODULE_CORMEX\n\r");
  678. }
  679. else if (Charger.CCID_Module_Type == CCID_MODULE_VAC)
  680. {
  681. DEBUG_INFO("Charger.CCID_Module_Type is CCID_MODULE_VAC\n\r");
  682. }
  683. DEBUG_INFO("System mode: %d\r\n", Charger.Mode);
  684. DEBUG_INFO("Alarm code: 0x%08X\r\n", Charger.Alarm_Code);
  685. #ifdef FUNC_SHOW_RELAY_INFO
  686. DEBUG_INFO("Relay action: Main(%d)\r\n", Charger.Relay_Action);
  687. #endif
  688. dbgIdx++;
  689. break;
  690. case 3:
  691. DEBUG_INFO("Original Voltage[0]: %f\r\n", adc_value.ADC3_IN9_Voltage_L1.value/100.0);
  692. DEBUG_INFO("Original Current[0]: %f\r\n", adc_value.ADC3_IN7_Current_L1.value/100.0);
  693. dbgIdx++;
  694. break;
  695. case 4:
  696. DEBUG_INFO("Correction Voltage[0]: %f\r\n", Charger.Voltage[0]/100.0);
  697. DEBUG_INFO("Correction Current[0]: %f\r\n", Charger.Current[0]/100.0);
  698. dbgIdx++;
  699. break;
  700. case 5:
  701. DEBUG_INFO("PE(ADC): %d\r\n", adc_value.ADC3_IN4_GMI_VL1.value );
  702. DEBUG_INFO("PE(V): %f\r\n", adc_value.ADC3_IN4_GMI_VL1.value*3.3/4095 );
  703. DEBUG_INFO("adc_value.ADC2_IN5_Welding.value : %d(unit:0.01v)\r\n", adc_value.ADC2_IN5_Welding.value );
  704. DEBUG_INFO("Charger.temperature.SystemAmbientTemp : %d(unit:C)\r\n", Charger.temperature.SystemAmbientTemp);
  705. dbgIdx++;
  706. break;
  707. case 6:
  708. if (Charger.CCID_Module_Type == CCID_MODULE_CORMEX)
  709. {
  710. DEBUG_INFO("Original GF[0]: %fmA\r\n", adc_value.ADC2_IN6_GF.value/100.0);
  711. DEBUG_INFO("Charger.Leak_Current: %fmA\r\n", Charger.Leak_Current/100.0);
  712. }
  713. else
  714. {
  715. DEBUG_INFO("Temperature ADC raw data : %d\r\n", adc_value.ADC3_IN15_Temp.value);
  716. DEBUG_INFO("Temperature : %d\r\n", Charger.temperature.SystemAmbientTemp);
  717. DEBUG_INFO("ADC3_Buffer[1] (ADC): %d(unit:ADC)\r\n", ADC3_Buffer[1] );
  718. DEBUG_INFO("Charger.Led_Mode: %d\r\n", Charger.Led_Mode );
  719. }
  720. dbgIdx++;
  721. break;
  722. case 7:
  723. DEBUG_INFO("CP+(ADC): %d(unit:ADC)\r\n",CpDetectionResult.PositiveValue );
  724. DEBUG_INFO("CP-(ADC): %d(unit:ADC)\r\n",CpDetectionResult.NegativeValue );
  725. if(CpDetectionResult.PositiveValue>0)
  726. {
  727. #ifdef FUNC_CP_ADC_MODIFY
  728. DEBUG_INFO("CP +voltage: %f\r\n", CP_GET_OPA_VIN_USE_ADC(CpDetectionResult.PositiveValue));
  729. #else
  730. DEBUG_INFO("CP +voltage: %f\r\n", (float)((1.59131-((3.3*CpDetectionResult.PositiveValue)/4095))*(475/53.6)));
  731. #endif
  732. }
  733. else
  734. {
  735. DEBUG_INFO("CP +voltage: 0\r\n");
  736. }
  737. if(CpDetectionResult.NegativeValue>0)
  738. {
  739. #ifdef FUNC_CP_ADC_MODIFY
  740. DEBUG_INFO("CP -voltage: %f\r\n", CP_GET_OPA_VIN_USE_ADC(CpDetectionResult.NegativeValue));
  741. #else
  742. DEBUG_INFO("CP -voltage: %f\r\n", (float)((1.59131-((3.3*CpDetectionResult.NegativeValue)/4095))*(475/53.6)));
  743. #endif
  744. }else
  745. {
  746. DEBUG_INFO("CP -voltage: 0\r\n");
  747. }
  748. DEBUG_INFO("CP state: %d\r\n", Charger.CP_State);
  749. dbgIdx++;
  750. break;
  751. case 8:
  752. DEBUG_INFO("Power_Consumption_Cumulative: %d (0.0001KWH)\r\n", Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative);
  753. DEBUG_INFO("Current rating value: %d A\r\n", Charger.maxRatingCurrent);
  754. if (Charger.memory.EVSE_Config.data.item.MaxChargingCurrent>=20)
  755. {
  756. DEBUG_INFO("OCP tigger UL: %d\r\n", (Charger.memory.EVSE_Config.data.item.MaxChargingCurrent*110)-100 );
  757. DEBUG_INFO("OCP tigger IEC: %d\r\n", (Charger.memory.EVSE_Config.data.item.MaxChargingCurrent*125)-100 );
  758. }
  759. else
  760. {
  761. DEBUG_INFO("OCP tigger UL: %d\r\n", (Charger.memory.EVSE_Config.data.item.MaxChargingCurrent+1)*100 );
  762. DEBUG_INFO("OCP tigger IEC: %d\r\n", (Charger.memory.EVSE_Config.data.item.MaxChargingCurrent*125)-100 );
  763. }
  764. DEBUG_INFO("Charger.am3352.isRequestOn: %d\r\n", Charger.am3352.isRequestOn);
  765. #ifdef FUNC_OCP_WITH_PP
  766. #ifdef MODIFY_OCP_PROC_WITH_MODELNAME_IDX_3_EURO_SPEC
  767. DEBUG_INFO("#OCPInfo(%d, %d, %d, %d) [%s]\r\n",
  768. Charger.m_OCP_CurMaxCurr, Charger.OCP_Magnification,
  769. Charger.m_OCP_BegThreshold, Charger.m_OCP_EndThreshold,
  770. Charger.m_bModelNameWithEuroSpecOCP ? "EuroSpec" : "NotEuroSpec");
  771. #else
  772. DEBUG_INFO("#OCPInfo(%d, %d, %d, %d)\r\n",
  773. Charger.m_OCP_CurMaxCurr, Charger.OCP_Magnification,
  774. Charger.m_OCP_BegThreshold, Charger.m_OCP_EndThreshold);
  775. #endif //MODIFY_OCP_PROC_WITH_MODELNAME_IDX_3_EURO_SPEC
  776. #endif //FUNC_OCP_WITH_PP
  777. #ifdef FUNC_GET_TRIP_TIME
  778. if (Charger.m_TripBeg != 0 && Charger.m_TripBeg < Charger.m_TripEnd)
  779. {
  780. DEBUG_INFO("Trip period: %d\r\n", Charger.m_TripEnd - Charger.m_TripBeg);
  781. }
  782. #endif
  783. dbgIdx++;
  784. break;
  785. case 9:
  786. if (Charger.Alarm_Code & ALARM_OVER_VOLTAGE)
  787. DEBUG_INFO("ALARM_OVER_VOLTAGE \n\r");
  788. if (Charger.Alarm_Code & ALARM_UNDER_VOLTAGE)
  789. DEBUG_INFO("ALARM_UNDER_VOLTAGE \n\r");
  790. if (Charger.Alarm_Code & ALARM_OVER_CURRENT)
  791. DEBUG_INFO("ALARM_OVER_CURRENT \n\r");
  792. if (Charger.Alarm_Code & ALARM_OVER_TEMPERATURE)
  793. DEBUG_INFO("ALARM_OVER_TEMPERATURE \n\r");
  794. if (Charger.Alarm_Code & ALARM_GROUND_FAIL)
  795. DEBUG_INFO("ALARM_GROUND_FAIL \n\r");
  796. if (Charger.Alarm_Code & ALARM_CP_ERROR)
  797. DEBUG_INFO("ALARM_CP_ERROR \n\r");
  798. if (Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC)
  799. DEBUG_INFO("ALARM_CURRENT_LEAK_AC \n\r");
  800. if (Charger.Alarm_Code & ALARM_CURRENT_LEAK_DC)
  801. DEBUG_INFO("ALARM_CURRENT_LEAK_DC \n\r");
  802. if (Charger.Alarm_Code & ALARM_MCU_TESTFAIL)
  803. DEBUG_INFO("ALARM_MCU_TESTFAIL \n\r");
  804. dbgIdx++;
  805. break;
  806. case 10:
  807. if (Charger.Alarm_Code & ALARM_HANDSHAKE_TIMEOUT)
  808. DEBUG_INFO("ALARM_HANDSHAKE_TIMEOUT \n\r");
  809. if (Charger.Alarm_Code & ALARM_EMERGENCY_STOP)
  810. DEBUG_INFO("ALARM_EMERGENCY_STOP \n\r");
  811. if (Charger.Alarm_Code & ALARM_RELAY_STATUS)
  812. DEBUG_INFO("ALARM_RELAY_STATUS \n\r");
  813. if (Charger.Alarm_Code & ALARM_LEAK_MODULE_FAIL)
  814. DEBUG_INFO("ALARM_LEAK_MODULE_FAIL \n\r");
  815. if (Charger.Alarm_Code & ALARM_SHUTTER_FAULT)
  816. DEBUG_INFO("ALARM_SHUTTER_FAULT \n\r");
  817. if (Charger.Alarm_Code & ALARM_LOCKER_FAULT)
  818. DEBUG_INFO("ALARM_LOCKER_FAULT \n\r");
  819. if (Charger.Alarm_Code & ALARM_POWER_DROP)
  820. DEBUG_INFO("ALARM_POWER_DROP \n\r");
  821. if (Charger.Alarm_Code & ALARM_CIRCUIT_SHORT)
  822. DEBUG_INFO("ALARM_CIRCUIT_SHORT \n\r");
  823. if (Charger.Alarm_Code & ALARM_ROTATORY_SWITCH_FAULT)
  824. DEBUG_INFO("ALARM_ROTATORY_SWITCH_FAULT \n\r");
  825. if (Charger.Alarm_Code & ALARM_RELAY_DRIVE_FUALT)
  826. DEBUG_INFO("ALARM_RELAY_DRIVE_FUALT \n\r");
  827. dbgIdx++;
  828. break;
  829. default:
  830. getlastRecord();
  831. dbgIdx = 0;
  832. break;
  833. }
  834. timerRefresh(TIMER_IDX_RTOS_STAT);
  835. }
  836. HAL_IWDG_Refresh(&hiwdg);
  837. #ifdef FUNC_RS485_SLAVE
  838. if(UART_IAP_recv_end_flag == 1 || UART_RS485_recv_end_flag == 1)
  839. #else
  840. if(UART_IAP_recv_end_flag == 1)
  841. #endif
  842. {
  843. #ifdef FUNC_UART_IAP_TX_BUF_INIT_ZERO
  844. uint8_t tx[UART_BUFFER_SIZE] = { 0 };
  845. #else
  846. uint8_t tx[UART_BUFFER_SIZE];
  847. #endif
  848. #ifdef MODIFY_UART_TX_LEN_FROM_U8_TO_U16
  849. uint16_t tx_len;
  850. #else
  851. uint8_t tx_len;
  852. #endif
  853. uint8_t chksum = 0;
  854. uint8_t tmpBuf = 0 ;
  855. // Protocol process
  856. #ifdef FUNC_RS485_SLAVE
  857. uint8_t *CurRxBuf = NULL;
  858. uint16_t *pCurRxBuf_RxLen = NULL;
  859. uint8_t *pCurRecvEndFlag = NULL;
  860. UART_HandleTypeDef *pUartHandle = NULL;
  861. if (UART_IAP_recv_end_flag == 1 && isValidCheckSum_IAP(UART_IAP_rx_buffer) == ON)
  862. {
  863. CurRxBuf = UART_IAP_rx_buffer;
  864. pCurRxBuf_RxLen = &UART_IAP_rx_len;
  865. pCurRecvEndFlag = &UART_IAP_recv_end_flag;
  866. pUartHandle = &IAP_USART;
  867. #ifdef FUNC_RELAY_OFF_WHEN_CSU_CMD_TIMEOUT
  868. Charger.m_CSU_RxTick = HAL_GetTick();
  869. #endif
  870. }
  871. else if (UART_RS485_recv_end_flag == 1 && isValidCheckSum_IAP(UART_RS485_rx_buffer) == ON)
  872. {
  873. CurRxBuf = UART_RS485_rx_buffer;
  874. pCurRxBuf_RxLen = &UART_RS485_rx_len;
  875. pCurRecvEndFlag = &UART_RS485_recv_end_flag;
  876. pUartHandle = &RS485_USART;
  877. #ifdef FUNC_RELAY_OFF_WHEN_CSU_CMD_TIMEOUT //DC Model
  878. Charger.m_CSU_RxTick = HAL_GetTick();
  879. #endif
  880. }
  881. if (CurRxBuf != NULL)
  882. #else
  883. uint8_t *CurRxBuf = UART_IAP_rx_buffer;
  884. if(isValidCheckSum_IAP() == ON)
  885. #endif
  886. {
  887. Charger.CSUisReady = ON ;
  888. #ifdef DEBUG_EMPX10_COMM
  889. if (CurRxBuf == UART_RS485_rx_buffer)
  890. {
  891. XP("-------------------------------------------\r\n");
  892. HTK_ByteArray2HexStr_XP(NULL, CurRxBuf, 0, IMCP_FM_HEAD_LEN + HTK_U16(CurRxBuf[IMCP_FM_DATALEN_LSB_IDX]) + IMCP_FM_TAIL_LEN);
  893. }
  894. #endif //DEBUG_EMPX10_COMM
  895. #ifdef ENABLE_PRINT_IMCP_MSG
  896. #ifdef FUNC_RS485_SLAVE
  897. #ifdef PRINT_IMCP_MSG_ONLY_ON_UPDATEPORT
  898. if (CurRxBuf == UART_IAP_rx_buffer && CheckPrintImcpMsg(CurRxBuf[IMCP_FM_MSGID_IDX]))
  899. {
  900. XP("-------------------------------------------\r\n");
  901. HTK_ByteArray2HexStr_XP(NULL, CurRxBuf, 0, IMCP_FM_HEAD_LEN + HTK_U16(CurRxBuf[IMCP_FM_DATALEN_LSB_IDX]) + IMCP_FM_TAIL_LEN);
  902. }
  903. #else
  904. if (CheckPrintImcpMsg(CurRxBuf[IMCP_FM_MSGID_IDX]))
  905. {
  906. XP("-------------------------------------------\r\n");
  907. HTK_ByteArray2HexStr_XP(NULL, CurRxBuf, 0, IMCP_FM_HEAD_LEN + HTK_U16(CurRxBuf[IMCP_FM_DATALEN_LSB_IDX]) + IMCP_FM_TAIL_LEN);
  908. }
  909. #endif //PRINT_IMCP_MSG_ONLY_ON_UPDATEPORT
  910. #else //FUNC_RS485_SLAVE
  911. if (CheckPrintImcpMsg(UART_IAP_rx_buffer[IMCP_FM_MSGID_IDX]))
  912. {
  913. XP("-------------------------------------------\r\n");
  914. 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);
  915. }
  916. #endif //FUNC_RS485_SLAVE
  917. #endif //ENABLE_PRINT_IMCP_MSG
  918. #ifdef FUNC_RS485_SLAVE
  919. switch (CurRxBuf[3])
  920. #else
  921. switch (UART_IAP_rx_buffer[3])
  922. #endif
  923. {
  924. /*
  925. Query message
  926. */
  927. case PROTOCOL_MESSAGE_QUERY_FW_VER:
  928. tx_len = 7 + (strlen(Charger.Ver_FW));
  929. tx[0] = 0xaa;
  930. tx[1] = PROTOCOL_ADDR;
  931. tx[2] = CurRxBuf[1];
  932. tx[3] = PROTOCOL_MESSAGE_QUERY_FW_VER;
  933. tx[4] = strlen(Charger.Ver_FW)&0xff;
  934. tx[5] = (strlen(Charger.Ver_FW)>>0x08) & 0xff;
  935. for(uint8_t idx=0;idx<strlen(Charger.Ver_FW);idx++)
  936. {
  937. tx[6+idx] = Charger.Ver_FW[idx];
  938. }
  939. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  940. {
  941. chksum ^= tx[6 + idx];
  942. }
  943. tx[6+(tx[4] | (tx[5]<<8))] = chksum;
  944. break;
  945. case PROTOCOL_MESSAGE_QUERY_HW_VER:
  946. tx_len = 7 + strlen(Charger.Ver_HW);
  947. tx[0] = 0xaa;
  948. tx[1] = PROTOCOL_ADDR;
  949. tx[2] = CurRxBuf[1];
  950. tx[3] = PROTOCOL_MESSAGE_QUERY_HW_VER;
  951. tx[4] = strlen(Charger.Ver_HW) & 0xff;
  952. tx[5] = (strlen(Charger.Ver_HW)>>0x08) & 0xff;
  953. for(uint8_t idx=0;idx<strlen(Charger.Ver_HW);idx++)
  954. {
  955. tx[6+idx] = Charger.Ver_HW[idx];
  956. }
  957. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  958. {
  959. chksum ^= tx[6 + idx];
  960. }
  961. tx[6+(tx[4] | (tx[5]<<8))] = chksum;
  962. break;
  963. case PROTOCOL_MESSAGE_QUERY_PRESENT_INPUT_VOLTAGE:
  964. tx_len = 14;
  965. tx[0] = 0xaa;
  966. tx[1] = PROTOCOL_ADDR;
  967. tx[2] = CurRxBuf[1];
  968. tx[3] = PROTOCOL_MESSAGE_QUERY_PRESENT_INPUT_VOLTAGE;
  969. tx[4] = 0x07;
  970. tx[5] = 0x00;
  971. tx[6] = 0x00;
  972. tx[7] = (((Charger.Voltage[0]/10)>>0) & 0xff);
  973. tx[8] = (((Charger.Voltage[0]/10)>>8) & 0xff);
  974. tx[9] = (((Charger.Voltage[1]/10)>>0) & 0xff);
  975. tx[10] = (((Charger.Voltage[1]/10)>>8) & 0xff);
  976. tx[11] = (((Charger.Voltage[2]/10)>>0) & 0xff);
  977. tx[12] = (((Charger.Voltage[2]/10)>>8) & 0xff);
  978. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  979. {
  980. chksum ^= tx[6 + idx];
  981. }
  982. tx[13] = chksum;
  983. break;
  984. case PROTOCOL_MESSAGE_QUERY_PRESENT_OUTPUT_VOLTAGE:
  985. tx_len = 15;
  986. tx[0] = 0xaa;
  987. tx[1] = PROTOCOL_ADDR;
  988. tx[2] = CurRxBuf[1];
  989. tx[3] = PROTOCOL_MESSAGE_QUERY_PRESENT_OUTPUT_VOLTAGE;
  990. tx[4] = 0x08;
  991. tx[5] = 0x00;
  992. tx[6] = 0x00;
  993. tx[7] = 0x00;
  994. tx[8] = 0x00;
  995. tx[9] = 0x00;
  996. tx[10] = 0x00;
  997. tx[11] = 0x00;
  998. tx[12] = 0x00;
  999. tx[13] = 0x00;
  1000. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1001. {
  1002. chksum ^= tx[6 + idx];
  1003. }
  1004. tx[14] = chksum;
  1005. break;
  1006. case PROTOCOL_MESSAGE_QUERY_FAN_SPEED:
  1007. tx_len = 15;
  1008. tx[0] = 0xaa;
  1009. tx[1] = PROTOCOL_ADDR;
  1010. tx[2] = CurRxBuf[1];
  1011. tx[3] = PROTOCOL_MESSAGE_QUERY_FAN_SPEED;
  1012. tx[4] = 0x08;
  1013. tx[5] = 0x00;
  1014. tx[6] = 0x00;
  1015. tx[7] = 0x00;
  1016. tx[8] = 0x00;
  1017. tx[9] = 0x00;
  1018. tx[10] = 0x00;
  1019. tx[11] = 0x00;
  1020. tx[12] = 0x00;
  1021. tx[13] = 0x00;
  1022. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1023. {
  1024. chksum ^= tx[6 + idx];
  1025. }
  1026. tx[14] = chksum;
  1027. break;
  1028. case PROTOCOL_MESSAGE_QUERY_TEMPERATURE:
  1029. tx_len = 15;
  1030. tx[0] = 0xaa;
  1031. tx[1] = PROTOCOL_ADDR;
  1032. tx[2] = CurRxBuf[1];
  1033. tx[3] = PROTOCOL_MESSAGE_QUERY_TEMPERATURE;
  1034. tx[4] = 0x08;
  1035. tx[5] = 0x00;
  1036. tx[6] = (((Charger.temperature.SystemAmbientTemp+60)>>0) & 0xff);
  1037. tx[7] = (((Charger.temperature.CcsConnectorTemp+60)>>0) & 0xff);
  1038. tx[8] = 0x00;
  1039. tx[9] = 0x00;
  1040. tx[10] = 0x00;
  1041. tx[11] = 0x00;
  1042. tx[12] = 0x00;
  1043. tx[13] = 0x00;
  1044. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1045. {
  1046. chksum ^= tx[6 + idx];
  1047. }
  1048. tx[14] = chksum;
  1049. break;
  1050. case PROTOCOL_MESSAGE_QUERY_AUX_POWER_VOLTAGE:
  1051. tx_len = 15;
  1052. tx[0] = 0xaa;
  1053. tx[1] = PROTOCOL_ADDR;
  1054. tx[2] = CurRxBuf[1];
  1055. tx[3] = PROTOCOL_MESSAGE_QUERY_AUX_POWER_VOLTAGE;
  1056. tx[4] = 0x08;
  1057. tx[5] = 0x00;
  1058. tx[6] = 0x00;
  1059. tx[7] = 0x00;
  1060. tx[8] = 0x00;
  1061. tx[9] = 0x00;
  1062. tx[10] = 0x00;
  1063. tx[11] = 0x00;
  1064. tx[12] = 0x00;
  1065. tx[13] = 0x00;
  1066. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1067. {
  1068. chksum ^= tx[6 + idx];
  1069. }
  1070. tx[14] = chksum;
  1071. break;
  1072. case PROTOCOL_MESSAGE_QUERY_RELAY_OUTPUT_STATUS:
  1073. tx_len = 9;
  1074. tx[0] = 0xaa;
  1075. tx[1] = PROTOCOL_ADDR;
  1076. tx[2] = CurRxBuf[1];
  1077. tx[3] = PROTOCOL_MESSAGE_QUERY_RELAY_OUTPUT_STATUS;
  1078. tx[4] = 0x02;
  1079. tx[5] = 0x00;
  1080. tx[6] = ((Relay1_Action_Get()==GPIO_RELAY1_ACTION_OFF)?0:1);
  1081. tx[7] = ((Relay2_Action_Get()==GPIO_RELAY2_ACTION_OFF)?0:1);
  1082. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1083. {
  1084. chksum ^= tx[6 + idx];
  1085. }
  1086. tx[8] = chksum;
  1087. break;
  1088. case PROTOCOL_MESSAGE_QUERY_GFD_ADC_VALUE:
  1089. tx_len = 15;
  1090. tx[0] = 0xaa;
  1091. tx[1] = PROTOCOL_ADDR;
  1092. tx[2] = CurRxBuf[1];
  1093. tx[3] = PROTOCOL_MESSAGE_QUERY_GFD_ADC_VALUE;
  1094. tx[4] = 0x08;
  1095. tx[5] = 0x00;
  1096. tx[6] = ((adc_value.ADC2_IN6_GF.value>>0) & 0xff);
  1097. tx[7] = ((adc_value.ADC2_IN6_GF.value>>8) & 0xff);
  1098. tx[8] = 0x00;
  1099. tx[9] = 0x00;
  1100. tx[10] = 0x00;
  1101. tx[11] = 0x00;
  1102. tx[12] = 0x00;
  1103. tx[13] = 0x00;
  1104. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1105. {
  1106. chksum ^= tx[6 + idx];
  1107. }
  1108. tx[14] = chksum;
  1109. break;
  1110. case PROTOCOL_MESSAGE_QUERY_INPUT_GPIO_STATUS:
  1111. tx_len = 9;
  1112. tx[0] = 0xaa;
  1113. tx[1] = PROTOCOL_ADDR;
  1114. tx[2] = CurRxBuf[1];
  1115. tx[3] = PROTOCOL_MESSAGE_QUERY_INPUT_GPIO_STATUS;
  1116. tx[4] = 0x02;
  1117. tx[5] = 0x00;
  1118. tx[6] = 0x00;
  1119. tx[7] = 0x00;
  1120. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1121. {
  1122. chksum ^= tx[6 + idx];
  1123. }
  1124. tx[8] = chksum;
  1125. break;
  1126. case PROTOCOL_MESSAGE_QUERY_ALARM_LOG:
  1127. tx_len = 17;
  1128. tx[0] = 0xaa;
  1129. tx[1] = PROTOCOL_ADDR;
  1130. tx[2] = CurRxBuf[1];
  1131. tx[3] = PROTOCOL_MESSAGE_QUERY_ALARM_LOG;
  1132. tx[4] = 0x0a;
  1133. tx[5] = 0x00;
  1134. tx[6] = CurRxBuf[7] ;
  1135. tx[7] = CurRxBuf[8] ;
  1136. tx[8] = CharacterArray[(Charger.memory.hisAlarm.data.item[CurRxBuf[7] | (CurRxBuf[8]>>8)].alarmCode & 0xF0000000)>>28];
  1137. tx[9] = CharacterArray[(Charger.memory.hisAlarm.data.item[CurRxBuf[7] | (CurRxBuf[8]>>8)].alarmCode & 0x0F000000)>>24];
  1138. tx[10] = CharacterArray[(Charger.memory.hisAlarm.data.item[CurRxBuf[7] | (CurRxBuf[8]>>8)].alarmCode & 0x00F00000)>>20];
  1139. tx[11] = CharacterArray[(Charger.memory.hisAlarm.data.item[CurRxBuf[7] | (CurRxBuf[8]>>8)].alarmCode & 0x000F0000)>>16];
  1140. tx[12] = CharacterArray[(Charger.memory.hisAlarm.data.item[CurRxBuf[7] | (CurRxBuf[8]>>8)].alarmCode & 0x0000F000)>>12];
  1141. tx[13] = CharacterArray[(Charger.memory.hisAlarm.data.item[CurRxBuf[7] | (CurRxBuf[8]>>8)].alarmCode & 0x00000F00)>>8];
  1142. tx[14] = CharacterArray[(Charger.memory.hisAlarm.data.item [CurRxBuf[7] | (CurRxBuf[8]>>8)].alarmCode & 0x000000F0)>>4];
  1143. tx[15] = CharacterArray[ Charger.memory.hisAlarm.data.item [CurRxBuf[7] | (CurRxBuf[8]>>8)].alarmCode & 0x0000000F ];
  1144. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1145. {
  1146. chksum ^= tx[6 + idx];
  1147. }
  1148. tx[16] = chksum;
  1149. /*
  1150. tx[8] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].DateTime.year>>0) & 0xff);
  1151. tx[9] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].DateTime.year>>8) & 0xff);
  1152. tx[10] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].DateTime.month>>0) & 0xff);;
  1153. tx[11] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].DateTime.day>>0) & 0xff);
  1154. tx[12] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].DateTime.hour>>0) & 0xff);
  1155. tx[13] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].DateTime.min>>0) & 0xff);
  1156. tx[14] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].DateTime.sec>>0) & 0xff);
  1157. tx[15] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].alarmCode>>0) & 0xff);
  1158. tx[16] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].alarmCode>>8) & 0xff);
  1159. tx[17] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].alarmCode>>16) & 0xff);
  1160. tx[18] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].alarmCode>>24) & 0xff);
  1161. tx[19] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].mode>>0) & 0xff);
  1162. tx[20] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].isRecover>0) & 0xff);
  1163. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1164. {
  1165. chksum ^= tx[6 + idx];
  1166. }
  1167. tx[21] = chksum;
  1168. */
  1169. break;
  1170. case PROTOCOL_MESSAGE_QUERY_SN:
  1171. // head +checksum = 7 , SystemDateTimeyear month day = 8 , SerialNumber
  1172. tx_len = 15 + strlen(Charger.memory.EVSE_Config.data.item.SerialNumber);
  1173. tx[0] = 0xaa;
  1174. tx[1] = PROTOCOL_ADDR;
  1175. tx[2] = CurRxBuf[1];
  1176. tx[3] = PROTOCOL_MESSAGE_QUERY_SN;
  1177. tx[4] = (strlen(Charger.memory.EVSE_Config.data.item.SerialNumber)+8)&0xff;
  1178. tx[5] = ((strlen(Charger.memory.EVSE_Config.data.item.SerialNumber)+8)>>0x08) & 0xff;
  1179. tx[6] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.year/1000)+'0';
  1180. tx[7] = ((Charger.memory.EVSE_Config.data.item.SystemDateTime.year%1000)/100)+'0';
  1181. tx[8] = ((Charger.memory.EVSE_Config.data.item.SystemDateTime.year%100)/10)+'0';
  1182. tx[9] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.year%10) +'0';
  1183. tx[10] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.month/10) +'0';
  1184. tx[11] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.month%10) +'0';
  1185. tx[12] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.day/10) +'0';
  1186. tx[13] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.day%10) +'0';
  1187. for(uint8_t idx=0;idx<strlen(Charger.memory.EVSE_Config.data.item.SerialNumber);idx++)
  1188. {
  1189. tx[14+idx] = Charger.memory.EVSE_Config.data.item.SerialNumber[idx];
  1190. }
  1191. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1192. {
  1193. chksum ^= tx[6 + idx];
  1194. }
  1195. tx[6+(tx[4] | (tx[5]<<8))] = chksum;
  1196. break;
  1197. case PROTOCOL_MESSAGE_QUERY_MODEL_NAME:
  1198. tx_len = 7 + strlen(Charger.memory.EVSE_Config.data.item.ModelName);;
  1199. tx[0] = 0xaa;
  1200. tx[1] = PROTOCOL_ADDR;
  1201. tx[2] = CurRxBuf[1];
  1202. tx[3] = PROTOCOL_MESSAGE_QUERY_MODEL_NAME;
  1203. tx[4] = strlen(Charger.memory.EVSE_Config.data.item.ModelName)&0xff;
  1204. tx[5] = (strlen(Charger.memory.EVSE_Config.data.item.ModelName)>>0x08) & 0xff;
  1205. for(uint8_t idx=0;idx<strlen(Charger.memory.EVSE_Config.data.item.ModelName);idx++)
  1206. {
  1207. tx[6+idx] = Charger.memory.EVSE_Config.data.item.ModelName[idx];
  1208. }
  1209. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1210. {
  1211. chksum ^= tx[6 + idx];
  1212. }
  1213. tx[6+(tx[4] | (tx[5]<<8))] = chksum;
  1214. break;
  1215. case PROTOCOL_MESSAGE_QUERY_PARAMETER:
  1216. tx_len = 11;
  1217. tx[0] = 0xaa;
  1218. tx[1] = PROTOCOL_ADDR;
  1219. tx[2] = CurRxBuf[1];
  1220. tx[3] = PROTOCOL_MESSAGE_QUERY_PARAMETER;
  1221. tx[4] = 0x04;
  1222. tx[5] = 0x00;
  1223. tx[6] = CurRxBuf[6];
  1224. tx[7] = CurRxBuf[7];
  1225. switch(CurRxBuf[6])
  1226. {
  1227. case INPUT_L1_AC_VOLTAGE:
  1228. tx[8] = Charger.memory.EVSE_Config.data.item.Correction_VL1[CurRxBuf[7]][1] & 0xff;
  1229. tx[9] = (Charger.memory.EVSE_Config.data.item.Correction_VL1[CurRxBuf[7]][1]>>8) & 0xff;
  1230. break;
  1231. case INPUT_L2_AC_VOLTAGE:
  1232. tx[8] = Charger.memory.EVSE_Config.data.item.Correction_VL2[CurRxBuf[7]][1] & 0xff;
  1233. tx[9] = (Charger.memory.EVSE_Config.data.item.Correction_VL2[CurRxBuf[7]][1]>>8) & 0xff;
  1234. break;
  1235. case INPUT_L3_AC_VOLTAGE:
  1236. tx[8] = Charger.memory.EVSE_Config.data.item.Correction_VL3[CurRxBuf[7]][1] & 0xff;
  1237. tx[9] = (Charger.memory.EVSE_Config.data.item.Correction_VL3[CurRxBuf[7]][1]>>8) & 0xff;
  1238. break;
  1239. case OUTPUT_L1_CURRENT:
  1240. tx[8] = Charger.memory.EVSE_Config.data.item.Correction_CL1[CurRxBuf[7]][1] & 0xff;
  1241. tx[9] = (Charger.memory.EVSE_Config.data.item.Correction_CL1[CurRxBuf[7]][1]>>8) & 0xff;
  1242. break;
  1243. case OUTPUT_L2_CURRENT:
  1244. tx[8] = Charger.memory.EVSE_Config.data.item.Correction_CL2[CurRxBuf[7]][1] & 0xff;
  1245. tx[9] = (Charger.memory.EVSE_Config.data.item.Correction_CL2[CurRxBuf[7]][1]>>8) & 0xff;
  1246. break;
  1247. case OUTPUT_L3_CURRENT:
  1248. tx[8] = Charger.memory.EVSE_Config.data.item.Correction_CL3[CurRxBuf[7]][1] & 0xff;
  1249. tx[9] = (Charger.memory.EVSE_Config.data.item.Correction_CL3[CurRxBuf[7]][1]>>8) & 0xff;
  1250. break;
  1251. case LEAK_CURRENT:
  1252. tx[8] = (Charger.memory.EVSE_Config.data.item.Correction_Leak[CurRxBuf[7]][1]/100) & 0xff;
  1253. tx[9] = ((Charger.memory.EVSE_Config.data.item.Correction_Leak[CurRxBuf[7]][1]/100)>>8) & 0xff;
  1254. break;
  1255. }
  1256. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1257. {
  1258. chksum ^= tx[6 + idx];
  1259. }
  1260. tx[10] = chksum;
  1261. break;
  1262. case PROTOCOL_MESSAGE_QUERY_RTC:
  1263. tx_len = 21;
  1264. tx[0] = 0xaa;
  1265. tx[1] = PROTOCOL_ADDR;
  1266. tx[2] = CurRxBuf[1];
  1267. tx[3] = PROTOCOL_MESSAGE_QUERY_RTC;
  1268. tx[4] = 0x0e;
  1269. tx[5] = 0x00;
  1270. tx[6] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.year/1000)+'0';
  1271. tx[7] = ((Charger.memory.EVSE_Config.data.item.SystemDateTime.year%1000)/100)+'0';
  1272. tx[8] = ((Charger.memory.EVSE_Config.data.item.SystemDateTime.year%100)/10)+'0';
  1273. tx[9] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.year%10) +'0';
  1274. tx[10] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.month/10) +'0';
  1275. tx[11] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.month%10) +'0';
  1276. tx[12] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.day/10) +'0';
  1277. tx[13] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.day%10) +'0';
  1278. tx[14] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.hour/10) +'0';
  1279. tx[15] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.hour%10) +'0';
  1280. tx[16] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.min/10) +'0';
  1281. tx[17] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.min%10) +'0';
  1282. tx[18] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.sec/10) +'0';
  1283. tx[19] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.sec%10) +'0';
  1284. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1285. {
  1286. chksum ^= tx[6 + idx];
  1287. }
  1288. tx[20] = chksum;
  1289. break;
  1290. case PROTOCOL_MESSAGE_QUERY_PRESENT_OUTPUT_CURRENT:
  1291. tx_len = 19;
  1292. tx[0] = 0xaa;
  1293. tx[1] = PROTOCOL_ADDR;
  1294. tx[2] = CurRxBuf[1];
  1295. tx[3] = PROTOCOL_MESSAGE_QUERY_PRESENT_OUTPUT_CURRENT;
  1296. tx[4] = 0x0c;
  1297. tx[5] = 0x00;
  1298. #ifdef FUNC_ZERO_CURRENT_WHEN_OUTP_OFF
  1299. if (Charger.Relay_Action == GPIO_RELAY_ACTION_ON)
  1300. {
  1301. tx[6] = (((Charger.Current[0]/10)>>0) & 0xff);
  1302. tx[7] = (((Charger.Current[0]/10)>>8) & 0xff);
  1303. tx[8] = (((Charger.Current[1]/10)>>0) & 0xff);
  1304. tx[9] = (((Charger.Current[1]/10)>>8) & 0xff);
  1305. tx[10] = (((Charger.Current[2]/10)>>0) & 0xff);
  1306. tx[11] = (((Charger.Current[2]/10)>>8) & 0xff);
  1307. }
  1308. else
  1309. {
  1310. tx[6] = 0;
  1311. tx[7] = 0;
  1312. tx[8] = 0;
  1313. tx[9] = 0;
  1314. tx[10] = 0;
  1315. tx[11] = 0;
  1316. }
  1317. #else
  1318. tx[6] = (((Charger.Current[0]/10)>>0) & 0xff);
  1319. tx[7] = (((Charger.Current[0]/10)>>8) & 0xff);
  1320. tx[8] = (((Charger.Current[1]/10)>>0) & 0xff);
  1321. tx[9] = (((Charger.Current[1]/10)>>8) & 0xff);
  1322. tx[10] = (((Charger.Current[2]/10)>>0) & 0xff);
  1323. tx[11] = (((Charger.Current[2]/10)>>8) & 0xff);
  1324. #endif
  1325. tx[12] = 0x00;
  1326. tx[13] = 0x00;
  1327. tx[14] = 0x00;
  1328. tx[15] = 0x00;
  1329. tx[16] = 0x00;
  1330. tx[17] = 0x00;
  1331. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1332. {
  1333. chksum ^= tx[6 + idx];
  1334. }
  1335. tx[18] = chksum;
  1336. break;
  1337. //edward 2019/08/27
  1338. case PROTOCOL_MESSAGE_QUERY_EVSE_STATE:
  1339. tx_len = 21;
  1340. tx[0] = 0xaa;
  1341. tx[1] = PROTOCOL_ADDR;
  1342. tx[2] = CurRxBuf[1];
  1343. tx[3] = PROTOCOL_MESSAGE_QUERY_EVSE_STATE;
  1344. tx[4] = 14;
  1345. tx[5] = 0x00;
  1346. tx[6] = Charger.CP_State;
  1347. #ifdef MODIFY_AC_EVSE_STATUS_MAX_CHARGING_CURRENT_VARIABLE
  1348. tx[7] = (Charger.AC_MaxChargingCurrentOrDuty&0xff);
  1349. tx[8] = Charger.AC_MaxChargingCurrentOrDuty>>8;
  1350. #else
  1351. tx[7] = (Charger.memory.EVSE_Config.data.item.MaxChargingCurrent&0xff);
  1352. tx[8] = Charger.memory.EVSE_Config.data.item.MaxChargingCurrent>>8;
  1353. #endif
  1354. uint16_t transfer_float;
  1355. {
  1356. #ifdef FUNC_CP_ADC_MODIFY
  1357. transfer_float = (uint16_t)(CpDetectionResult.PositiveValue == 0 ? 0 : HTK_GetPosFromZero(CP_GET_OPA_VIN_USE_ADC(CpDetectionResult.PositiveValue)) * 100);
  1358. #else
  1359. //transfer_float = (uint16_t)((float)(((1.59131-((3.3*CpDetectionResult.PositiveValue)/4095))*(475/53.6)))*100);
  1360. #endif
  1361. }
  1362. tx[9] = (transfer_float&0xff);
  1363. tx[10] = (transfer_float&0xff00)>>8;
  1364. {
  1365. #ifdef FUNC_CP_ADC_MODIFY
  1366. transfer_float = (uint16_t)(CpDetectionResult.NegativeValue == 0 ? 0 : HTK_GetNegFromZero(CP_GET_OPA_VIN_USE_ADC(CpDetectionResult.NegativeValue)) * 100);
  1367. #else
  1368. //transfer_float = (uint16_t)((float)(((((3.3*CpDetectionResult.NegativeValue)/4095)-1.59131)*(475/53.6)))*100);
  1369. #endif
  1370. }
  1371. tx[11] = (transfer_float&0xff);
  1372. tx[12] = (transfer_float&0xff00)>>8;
  1373. tx[13] = 0x00;
  1374. if(Charger.Relay_Action == GPIO_RELAY_ACTION_ON)
  1375. {
  1376. tx[14] = 0x01;
  1377. }
  1378. else
  1379. {
  1380. tx[14] = 0x00;
  1381. }
  1382. tx[15] = 0x00;
  1383. tx[16] = 0x00;
  1384. tx[17] = 0x00;
  1385. tx[18] = Charger.maxRatingCurrent;
  1386. tmpBuf += (HAL_GPIO_ReadPin(IN_AC_Current_Set1_GPIO_Port, IN_AC_Current_Set1_Pin)?0:1)<<0;
  1387. tmpBuf += (HAL_GPIO_ReadPin(IN_AC_Current_Set2_GPIO_Port, IN_AC_Current_Set2_Pin)?0:1)<<1;
  1388. tmpBuf += (HAL_GPIO_ReadPin(IN_AC_Current_Set3_GPIO_Port, IN_AC_Current_Set3_Pin)?0:1)<<2;
  1389. tmpBuf += (HAL_GPIO_ReadPin(IN_AC_Current_Set4_GPIO_Port, IN_AC_Current_Set4_Pin)?0:1)<<3;
  1390. tmpBuf += (HAL_GPIO_ReadPin(IN_GridType_Select_GPIO_Port, IN_GridType_Select_Pin)?0:1)<<4;
  1391. tmpBuf += (HAL_GPIO_ReadPin(IN_Grid_Groundding_GPIO_Port, IN_Grid_Groundding_Pin)?0:1)<<5;
  1392. tx[19] = tmpBuf;
  1393. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1394. {
  1395. chksum ^= tx[6 + idx];
  1396. }
  1397. tx[20] = chksum;
  1398. break;
  1399. case PROTOCOL_MESSAGE_QUERY_EVSE_ALARM:
  1400. tx_len = 11;
  1401. tx[0] = 0xaa;
  1402. tx[1] = PROTOCOL_ADDR;
  1403. tx[2] = CurRxBuf[1];
  1404. tx[3] = PROTOCOL_MESSAGE_QUERY_EVSE_ALARM;
  1405. tx[4] = 4;
  1406. tx[5] = 0x00;
  1407. tx[6] = (Charger.Alarm_Code&0xff);
  1408. tx[7] = (Charger.Alarm_Code&0xff00)>>8;
  1409. tx[8] = (Charger.Alarm_Code&0xff0000)>>16;
  1410. tx[9] = (Charger.Alarm_Code&0xff000000)>>24;
  1411. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1412. {
  1413. chksum ^= tx[6 + idx];
  1414. }
  1415. tx[10] = chksum;
  1416. break;
  1417. case PROTOCOL_MESSAGE_QUERY_BLE_STATE:
  1418. tx_len = 10;
  1419. tx[0] = 0xaa;
  1420. tx[1] = PROTOCOL_ADDR;
  1421. tx[2] = CurRxBuf[1];
  1422. tx[3] = PROTOCOL_MESSAGE_QUERY_BLE_STATE;
  1423. tx[4] = 3;
  1424. tx[5] = 0x00;
  1425. tx[6] = 0x00 ;
  1426. tx[7] = 0x00 ;
  1427. tx[8] = 0x00 ;
  1428. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1429. {
  1430. chksum ^= tx[6 + idx];
  1431. }
  1432. tx[9] = chksum;
  1433. break;
  1434. case PROTOCOL_MESSAGE_QUERY_BLE_LOGIC_CENTRAL_ID:
  1435. tx_len = 39;
  1436. tx[0] = 0xaa;
  1437. tx[1] = PROTOCOL_ADDR;
  1438. tx[2] = CurRxBuf[1];
  1439. tx[3] = PROTOCOL_MESSAGE_QUERY_BLE_LOGIC_CENTRAL_ID;
  1440. tx[4] = 32;
  1441. tx[5] = 0x00;
  1442. tx[6] = 'P';
  1443. tx[7] = 'H';
  1444. tx[8] = 'I';
  1445. tx[9] = 'H';
  1446. tx[10] = 'O';
  1447. tx[11] = 'N';
  1448. tx[12] = 'G';
  1449. tx[13] = ' ';
  1450. tx[14] = 'A';
  1451. tx[15] = 'C';
  1452. tx[16] = '-';
  1453. tx[17] = 'S';
  1454. tx[18] = 'i';
  1455. tx[19] = 'm';
  1456. tx[20] = 'p';
  1457. tx[21] = 'l';
  1458. tx[22] = 'e';
  1459. tx[23] = ' ';
  1460. tx[24] = '1';
  1461. tx[25] = '2';
  1462. tx[26] = '3';
  1463. tx[27] = '4';
  1464. tx[28] = '5';
  1465. tx[29] = '6';
  1466. tx[30] = '7';
  1467. tx[31] = '8';
  1468. tx[32] = '9';
  1469. tx[33] = 'A';
  1470. tx[34] = 'B';
  1471. tx[35] = 'C';
  1472. tx[36] = 'D';
  1473. tx[37] = 'E';
  1474. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1475. {
  1476. chksum ^= tx[6 + idx];
  1477. }
  1478. tx[38] = chksum;
  1479. break;
  1480. case PROTOCOL_MESSAGE_QUERY_POWER_CONSUME:
  1481. tx_len = 11;
  1482. tx[0] = 0xaa;
  1483. tx[1] = PROTOCOL_ADDR;
  1484. tx[2] = CurRxBuf[1];
  1485. tx[3] = PROTOCOL_MESSAGE_QUERY_POWER_CONSUME;
  1486. tx[4] = 4;
  1487. tx[5] = 0x00;
  1488. tx[6] = ((Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative/100)&0xff);
  1489. tx[7] = ((Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative/100)&0xff00)>>8;
  1490. tx[8] = ((Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative/100)&0xff0000)>>16;
  1491. tx[9] = ((Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative/100)&0xff000000)>>24;
  1492. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1493. {
  1494. chksum ^= tx[6 + idx];
  1495. }
  1496. tx[10] = chksum;
  1497. break;
  1498. case PROTOCOL_MESSAGE_QUERY_GUN_PLUGIN_TIMES:
  1499. tx_len = 11;
  1500. tx[0] = 0xaa;
  1501. tx[1] = PROTOCOL_ADDR;
  1502. tx[2] = CurRxBuf[1];
  1503. tx[3] = PROTOCOL_MESSAGE_QUERY_GUN_PLUGIN_TIMES;
  1504. tx[4] = 4;
  1505. tx[5] = 0x00;
  1506. tx[6] = (Charger.memory.EVSE_Config.data.item.AcPlugInTimes&0xff);
  1507. tx[7] = (Charger.memory.EVSE_Config.data.item.AcPlugInTimes&0xff00)>>8;
  1508. tx[8] = (Charger.memory.EVSE_Config.data.item.AcPlugInTimes&0xff0000)>>16;
  1509. tx[9] = (Charger.memory.EVSE_Config.data.item.AcPlugInTimes&0xff000000)>>24;
  1510. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1511. {
  1512. chksum ^= tx[6 + idx];
  1513. }
  1514. tx[10] = chksum;
  1515. break;
  1516. case PROTOCOL_MESSAGE_QUERY_EVSE_AUTH_MODE:
  1517. tx_len = 9;
  1518. tx[0] = 0xaa;
  1519. tx[1] = PROTOCOL_ADDR;
  1520. tx[2] = CurRxBuf[1];
  1521. tx[3] = PROTOCOL_MESSAGE_QUERY_EVSE_AUTH_MODE;
  1522. tx[4] = 2;
  1523. tx[5] = 0x00;
  1524. tx[6] = Charger.memory.EVSE_Config.data.item.AuthMode;
  1525. tx[7] = Charger.memory.EVSE_Config.data.item.OfflinePolicy;
  1526. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1527. {
  1528. chksum ^= tx[6 + idx];
  1529. }
  1530. tx[8] = chksum;
  1531. break;
  1532. case PROTOCOL_MESSAGE_QUERY_PRESENT_LEAK_CURRENT:
  1533. tx_len = 9;
  1534. tx[0] = 0xaa;
  1535. tx[1] = PROTOCOL_ADDR;
  1536. tx[2] = CurRxBuf[1];
  1537. tx[3] = PROTOCOL_MESSAGE_QUERY_PRESENT_LEAK_CURRENT;
  1538. tx[4] = 2;
  1539. tx[5] = 0x00;
  1540. tx[6] = Charger.Leak_Current/100;
  1541. tx[7] = 0;
  1542. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1543. {
  1544. chksum ^= tx[6 + idx];
  1545. }
  1546. tx[8] = chksum;
  1547. break;
  1548. /*
  1549. Config message
  1550. */
  1551. case PROTOCOL_MESSAGE_CONFIG_FAN_SPEED:
  1552. tx_len = 8;
  1553. tx[0] = 0xaa;
  1554. tx[1] = PROTOCOL_ADDR;
  1555. tx[2] = CurRxBuf[1];
  1556. tx[3] = PROTOCOL_MESSAGE_CONFIG_FAN_SPEED;
  1557. tx[4] = 0x01;
  1558. tx[5] = 0x00;
  1559. tx[6] = 0x01;
  1560. tx[7] = 0x01;
  1561. break;
  1562. case PROTOCOL_MESSAGE_CONFIG_SN:
  1563. tx_len = 8;
  1564. tx[0] = 0xaa;
  1565. tx[1] = PROTOCOL_ADDR;
  1566. tx[2] = CurRxBuf[1];
  1567. tx[3] = PROTOCOL_MESSAGE_CONFIG_SN;
  1568. tx[4] = 0x01;
  1569. tx[5] = 0x00;
  1570. #ifdef MODIFY_CONFIG_SN
  1571. {
  1572. uint16_t len = HTK_U16(CurRxBuf[4]);
  1573. if (len < sizeof(Charger.memory.EVSE_Config.data.item.ModelName) && HTK_IS_DISP_CHAR(CurRxBuf[14]))
  1574. {
  1575. //OK
  1576. tx[6] = 0x01;
  1577. tx[7] = 0x01;
  1578. memset(Charger.memory.EVSE_Config.data.item.SerialNumber, 0, ARRAY_SIZE(Charger.memory.EVSE_Config.data.item.SerialNumber));
  1579. memset(Charger.memory.EVSE_Config.data.item.System_ID, 0, ARRAY_SIZE(Charger.memory.EVSE_Config.data.item.System_ID));
  1580. for(uint16_t idx=0;idx<(len-8);idx++)
  1581. {
  1582. Charger.memory.EVSE_Config.data.item.SerialNumber[idx] = CurRxBuf[14+idx];
  1583. }
  1584. Charger.memory.EVSE_Config.data.item.SerialNumber[len-8] = '\0';
  1585. memcpy(Charger.memory.EVSE_Config.data.item.System_ID, Charger.memory.EVSE_Config.data.item.ModelName, strlen(Charger.memory.EVSE_Config.data.item.ModelName));
  1586. 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);
  1587. XP("#CONFIG_SN: %s\r\n", Charger.memory.EVSE_Config.data.item.SerialNumber);
  1588. //parseVersionInfoFromModelname() ;
  1589. Charger.memory.EVSE_Config.op_bits.update = ON;
  1590. break;
  1591. }
  1592. else
  1593. {
  1594. //NG
  1595. tx[6] = 0x00;
  1596. tx[7] = 0x00;
  1597. break;
  1598. }
  1599. }
  1600. #else //MODIFY_CONFIG_SN
  1601. // tx[6] = 0x01;
  1602. // tx[7] = 0x01;
  1603. //
  1604. // 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]));
  1605. // 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]));
  1606. // for(uint16_t idx=0;idx<((CurRxBuf[4] | (CurRxBuf[5]<<8))-8);idx++)
  1607. // {
  1608. // Charger.memory.EVSE_Config.data.item.SerialNumber[idx] = CurRxBuf[14+idx];
  1609. // }
  1610. // Charger.memory.EVSE_Config.data.item.SerialNumber[(CurRxBuf[4] | (CurRxBuf[5]<<8))-8] = '\0';
  1611. // memcpy(Charger.memory.EVSE_Config.data.item.System_ID, Charger.memory.EVSE_Config.data.item.ModelName, strlen(Charger.memory.EVSE_Config.data.item.ModelName));
  1612. // 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)));
  1613. //
  1614. // parseVersionInfoFromModelname() ;
  1615. // Charger.memory.EVSE_Config.op_bits.update = ON;
  1616. // break;
  1617. #endif //MODIFY_CONFIG_SN
  1618. case PROTOCOL_MESSAGE_CONFIG_MODEL_NAME:
  1619. tx_len = 8;
  1620. tx[0] = 0xaa;
  1621. tx[1] = PROTOCOL_ADDR;
  1622. tx[2] = CurRxBuf[1];
  1623. tx[3] = PROTOCOL_MESSAGE_CONFIG_MODEL_NAME;
  1624. tx[4] = 0x01;
  1625. tx[5] = 0x00;
  1626. #ifdef MODIFY_CONFIG_MODEL_NAME
  1627. {
  1628. uint16_t len = HTK_U16(CurRxBuf[4]);
  1629. if (len < sizeof(Charger.memory.EVSE_Config.data.item.ModelName) && HTK_IS_DISP_CHAR(CurRxBuf[6]))
  1630. {
  1631. //OK
  1632. tx[6] = 0x01;
  1633. tx[7] = 0x01;
  1634. memset(Charger.memory.EVSE_Config.data.item.ModelName, 0, ARRAY_SIZE(Charger.memory.EVSE_Config.data.item.ModelName));
  1635. memset(Charger.memory.EVSE_Config.data.item.System_ID, 0, ARRAY_SIZE(Charger.memory.EVSE_Config.data.item.System_ID));
  1636. for(uint16_t idx=0;idx<len;idx++)
  1637. {
  1638. Charger.memory.EVSE_Config.data.item.ModelName[idx] = CurRxBuf[6+idx];
  1639. }
  1640. Charger.memory.EVSE_Config.data.item.ModelName[len] = '\0';
  1641. memcpy(Charger.memory.EVSE_Config.data.item.System_ID, Charger.memory.EVSE_Config.data.item.ModelName, strlen(Charger.memory.EVSE_Config.data.item.ModelName));
  1642. 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);
  1643. XP("#CONFIG_MODEL_NAME: %s\r\n", Charger.memory.EVSE_Config.data.item.ModelName);
  1644. #ifdef FUNC_AUTO_MODIFY_CCID_MODULE
  1645. Charger.m_bCCID_MustModify = HTK_TRUE;
  1646. #endif
  1647. parseVersionInfoFromModelname() ;
  1648. Charger.memory.EVSE_Config.op_bits.update = ON;
  1649. break;
  1650. }
  1651. else
  1652. {
  1653. //NG
  1654. tx[6] = 0x00;
  1655. tx[7] = 0x00;
  1656. break;
  1657. }
  1658. }
  1659. #else //MODIFY_CONFIG_MODEL_NAME
  1660. // tx[6] = 0x01;
  1661. // tx[7] = 0x01;
  1662. //
  1663. // 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]));
  1664. // 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]));
  1665. // for(uint16_t idx=0;idx<(CurRxBuf[4] | (CurRxBuf[5]<<8));idx++)
  1666. // {
  1667. // Charger.memory.EVSE_Config.data.item.ModelName[idx] = CurRxBuf[6+idx];
  1668. // }
  1669. // Charger.memory.EVSE_Config.data.item.ModelName[(CurRxBuf[4] | (CurRxBuf[5]<<8))] = '\0';
  1670. // memcpy(Charger.memory.EVSE_Config.data.item.System_ID, Charger.memory.EVSE_Config.data.item.ModelName, strlen(Charger.memory.EVSE_Config.data.item.ModelName));
  1671. // 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)));
  1672. //
  1673. // parseVersionInfoFromModelname() ;
  1674. // Charger.memory.EVSE_Config.op_bits.update = ON;
  1675. // break;
  1676. #endif //MODIFY_CONFIG_MODEL_NAME
  1677. case PROTOCOL_MESSAGE_CONFIG_PARAMETER:
  1678. tx_len = 8;
  1679. tx[0] = 0xaa;
  1680. tx[1] = PROTOCOL_ADDR;
  1681. tx[2] = CurRxBuf[1];
  1682. tx[3] = PROTOCOL_MESSAGE_CONFIG_PARAMETER;
  1683. tx[4] = 0x01;
  1684. tx[5] = 0x00;
  1685. tx[6] = 0x01;
  1686. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1687. {
  1688. chksum ^= tx[6 + idx];
  1689. }
  1690. tx[7] = chksum; //checksum
  1691. switch(CurRxBuf[6])
  1692. {
  1693. case INPUT_L1_AC_VOLTAGE:
  1694. Charger.memory.EVSE_Config.data.item.Correction_VL1[CurRxBuf[7]][0] = adc_value.ADC3_IN9_Voltage_L1.value/10;
  1695. Charger.memory.EVSE_Config.data.item.Correction_VL1[CurRxBuf[7]][1] = CurRxBuf[8] | (CurRxBuf[9]<<8);
  1696. break;
  1697. case INPUT_L2_AC_VOLTAGE:
  1698. Charger.memory.EVSE_Config.data.item.Correction_VL2[CurRxBuf[7]][0] = Charger.Voltage[1];
  1699. Charger.memory.EVSE_Config.data.item.Correction_VL2[CurRxBuf[7]][1] = CurRxBuf[8] | (CurRxBuf[9]<<8);
  1700. break;
  1701. case INPUT_L3_AC_VOLTAGE:
  1702. Charger.memory.EVSE_Config.data.item.Correction_VL3[CurRxBuf[7]][0] = Charger.Voltage[2];
  1703. Charger.memory.EVSE_Config.data.item.Correction_VL3[CurRxBuf[7]][1] = CurRxBuf[8] | (CurRxBuf[9]<<8);
  1704. break;
  1705. case OUTPUT_L1_CURRENT:
  1706. Charger.memory.EVSE_Config.data.item.Correction_CL1[CurRxBuf[7]][0] = adc_value.ADC3_IN7_Current_L1.value/10;
  1707. Charger.memory.EVSE_Config.data.item.Correction_CL1[CurRxBuf[7]][1] = CurRxBuf[8] | (CurRxBuf[9]<<8);
  1708. break;
  1709. case OUTPUT_L2_CURRENT:
  1710. Charger.memory.EVSE_Config.data.item.Correction_CL2[CurRxBuf[7]][0] = Charger.Current[0];
  1711. Charger.memory.EVSE_Config.data.item.Correction_CL2[CurRxBuf[7]][1] = CurRxBuf[8] | (CurRxBuf[9]<<8);
  1712. break;
  1713. case OUTPUT_L3_CURRENT:
  1714. Charger.memory.EVSE_Config.data.item.Correction_CL3[CurRxBuf[7]][0] = Charger.Current[0];
  1715. Charger.memory.EVSE_Config.data.item.Correction_CL3[CurRxBuf[7]][1] = CurRxBuf[8] | (CurRxBuf[9]<<8);
  1716. break;
  1717. case LEAK_CURRENT:
  1718. Charger.memory.EVSE_Config.data.item.Correction_Leak[CurRxBuf[7]][0] = adc_value.ADC2_IN6_GF.value; // 2500->25mA
  1719. Charger.memory.EVSE_Config.data.item.Correction_Leak[CurRxBuf[7]][1] = ((CurRxBuf[8] | (CurRxBuf[9]<<8))*100); // mA
  1720. 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]);
  1721. 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]);
  1722. break;
  1723. case GROUND_FAULT_VOLTAGE: //max 3.3V
  1724. Charger.memory.EVSE_Config.data.item.Correction_GMI[CurRxBuf[7]][0] = (uint16_t)(adc_value.ADC3_IN4_GMI_VL1.value*10*3.3/4095) ;
  1725. Charger.memory.EVSE_Config.data.item.Correction_GMI[CurRxBuf[7]][1] = CurRxBuf[8] | (CurRxBuf[9]<<8);
  1726. break;
  1727. #ifdef FUNC_SIMU_CHARGE_CURRENT_OUTPUT
  1728. case SIMU_CHARGE_CURRENT_CMD:
  1729. Charger.m_bEnableSimuChargeCurrent =
  1730. (
  1731. (
  1732. CurRxBuf[7] == SIMU_CHARGE_CURRENT_ON &&
  1733. HTK_U16(CurRxBuf[8]) == SIMU_CHARGE_CURRENT_ON_PWD
  1734. ) ? 1 : 0
  1735. );
  1736. XP("#CSU_ParameterSimuCurr: %s\r\n", Charger.m_bEnableSimuChargeCurrent ? "ON" : "OFF");
  1737. break;
  1738. #endif
  1739. }
  1740. #ifdef FUNC_SIMU_CHARGE_CURRENT_OUTPUT
  1741. if (CurRxBuf[6] != SIMU_CHARGE_CURRENT_CMD)
  1742. {
  1743. Charger.memory.EVSE_Config.op_bits.update = ON;
  1744. }
  1745. #else
  1746. Charger.memory.EVSE_Config.op_bits.update = ON;
  1747. #endif
  1748. break;
  1749. case PROTOCOL_MESSAGE_CONFIG_RELAY_OUTPUT:
  1750. tx_len = 8;
  1751. tx[0] = 0xaa;
  1752. tx[1] = PROTOCOL_ADDR;
  1753. tx[2] = CurRxBuf[1];
  1754. tx[3] = PROTOCOL_MESSAGE_CONFIG_RELAY_OUTPUT;
  1755. tx[4] = 0x01;
  1756. tx[5] = 0x00;
  1757. tx[6] = 0x01;
  1758. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1759. {
  1760. chksum ^= tx[6 + idx];
  1761. }
  1762. tx[7] = chksum;
  1763. #ifndef USE_OLD_ONE_STEP_CHARGE_FOR_AW_NOODOE
  1764. if (
  1765. #ifdef MODIFY_PROTOCOL_MESSAGE_CONFIG_RELAY_OUTPUT_CHECK_LEGACY_REQUEST
  1766. Charger.am3352.isRequestOn &&
  1767. #endif
  1768. #ifdef MODIFY_PROTOCOL_MESSAGE_CONFIG_RELAY_OUTPUT_CHECK_ALARM_CODE
  1769. Charger.Alarm_Code == 0 &&
  1770. #endif
  1771. #ifdef MODIFY_PROTOCOL_MESSAGE_CONFIG_RELAY_OUTPUT_CHECK_CP_PWM
  1772. Charger.m_CP_CurPWM != PWM_DUTY_FULL &&
  1773. #endif
  1774. Charger.CP_State == SYSTEM_STATE_C &&
  1775. CurRxBuf[9] == 0x07 //Main relay (0111b)
  1776. )
  1777. {
  1778. Charger.Relay_Action = GPIO_RELAY_ACTION_ON;
  1779. #ifdef FUNC_RELAY_OFF_WHEN_GET_TWICE_OFF_CMD
  1780. CsuSetRelayOffCount = 0;
  1781. #endif
  1782. }
  1783. else
  1784. {
  1785. #ifdef FUNC_RELAY_OFF_WHEN_GET_TWICE_OFF_CMD
  1786. if (++CsuSetRelayOffCount > 2)
  1787. {
  1788. CsuSetRelayOffCount = 0;
  1789. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF ;
  1790. }
  1791. #else //FUNC_RELAY_OFF_WHEN_GET_TWICE_OFF_CMD
  1792. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF ;
  1793. #endif //FUNC_RELAY_OFF_WHEN_GET_TWICE_OFF_CMD
  1794. }
  1795. #endif //USE_OLD_ONE_STEP_CHARGE_FOR_AW_NOODOE
  1796. #ifdef FUNC_CSU_CMD_DEBUG
  1797. XP("#CSU_Relay: %d\r\n", CurRxBuf[9]);
  1798. #endif
  1799. DEBUG_INFO("Set Relay Action By CSU: %d => %d\r\n", CurRxBuf[9], Charger.Relay_Action);
  1800. break;
  1801. case PROTOCOL_MESSAGE_CONFIG_GPIO_OUTPUT:
  1802. tx_len = 8;
  1803. tx[0] = 0xaa;
  1804. tx[1] = PROTOCOL_ADDR;
  1805. tx[2] = CurRxBuf[1];
  1806. tx[3] = PROTOCOL_MESSAGE_CONFIG_GPIO_OUTPUT;
  1807. tx[4] = 0x01;
  1808. tx[5] = 0x00;
  1809. tx[6] = CurRxBuf[6];
  1810. tx[7] = 0x00 ^ CurRxBuf[6];
  1811. break;
  1812. case PROTOCOL_MESSAGE_CONFIG_RTC:
  1813. tx_len = 8;
  1814. tx[0] = 0xaa;
  1815. tx[1] = PROTOCOL_ADDR;
  1816. tx[2] = CurRxBuf[1];
  1817. tx[3] = PROTOCOL_MESSAGE_CONFIG_RTC;
  1818. tx[4] = 0x01;
  1819. tx[5] = 0x00;
  1820. tx[6] = 0x01;
  1821. tx[7] = 0x01;
  1822. setTime.Hours = (CurRxBuf[14]-'0')*10 + (CurRxBuf[15]-'0');
  1823. setTime.Minutes = (CurRxBuf[16]-'0')*10 + (CurRxBuf[17]-'0');
  1824. setTime.Seconds = (CurRxBuf[18]-'0')*10 + (CurRxBuf[19]-'0');
  1825. setTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  1826. setTime.StoreOperation = RTC_STOREOPERATION_RESET;
  1827. if (HAL_RTC_SetTime(&hrtc, &setTime, RTC_FORMAT_BIN) != HAL_OK)
  1828. {
  1829. Error_Handler();
  1830. }
  1831. setDate.Year = (CurRxBuf[6]-'0')*1000 + (CurRxBuf[7]-'0')*100 + (CurRxBuf[8]-'0')*10 + (CurRxBuf[9]-'0') - 2000;
  1832. setDate.Month = (CurRxBuf[10]-'0')*10+ (CurRxBuf[11]-'0');
  1833. setDate.Date = (CurRxBuf[12]-'0')*10 + (CurRxBuf[13]-'0');
  1834. if (HAL_RTC_SetDate(&hrtc, &setDate, RTC_FORMAT_BIN) != HAL_OK)
  1835. {
  1836. Error_Handler();
  1837. }
  1838. break;
  1839. case PROTOCOL_MESSAGE_CONFIG_LED_ACTION_ALARM:
  1840. if((Charger.am3352.LedActionState != CurRxBuf[6])||(Charger.am3352.LedAlarmState != (CurRxBuf[7]|(CurRxBuf[8]<<8)|(CurRxBuf[9]<<16)|(CurRxBuf[10]<<24))))
  1841. {
  1842. Charger.am3352.LedActionState = CurRxBuf[6];
  1843. Charger.am3352.LedAlarmState = (CurRxBuf[7]|(CurRxBuf[8]<<8)|(CurRxBuf[9]<<16)|(CurRxBuf[10]<<24));
  1844. #ifdef FUNC_SHOW_CSU_CONFIG_LED_ACTION_INFO
  1845. XP("#CSU_SetLed (%d, %d)\r\n", Charger.am3352.LedActionState, Charger.am3352.LedAlarmState);
  1846. #endif
  1847. switch(Charger.am3352.LedActionState)
  1848. {
  1849. case LED_ACTION_INIT:
  1850. setLedMotion(LED_ACTION_INIT);
  1851. break;
  1852. case LED_ACTION_IDLE:
  1853. setLedMotion(LED_ACTION_IDLE);
  1854. break;
  1855. case LED_ACTION_AUTHED:
  1856. setLedMotion(LED_ACTION_AUTHED);
  1857. break;
  1858. case LED_ACTION_CONNECTED:
  1859. setLedMotion(LED_ACTION_CONNECTED);
  1860. break;
  1861. case LED_ACTION_CHARGING:
  1862. setLedMotion(LED_ACTION_CHARGING);
  1863. break;
  1864. case LED_ACTION_STOP:
  1865. setLedMotion(LED_ACTION_STOP);
  1866. break;
  1867. case LED_ACTION_ALARM:
  1868. setLedMotion(LED_ACTION_ALARM);
  1869. break;
  1870. case LED_ACTION_MAINTAIN:
  1871. setLedMotion(LED_ACTION_MAINTAIN);
  1872. break;
  1873. case LED_ACTION_RFID_PASS:
  1874. setLedMotion(LED_ACTION_RFID_PASS);
  1875. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  1876. break;
  1877. case LED_ACTION_RFID_FAIL:
  1878. setLedMotion(LED_ACTION_RFID_FAIL);
  1879. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  1880. break;
  1881. case LED_ACTION_BLE_CONNECT:
  1882. setLedMotion(LED_ACTION_BLE_CONNECT);
  1883. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  1884. break;
  1885. case LED_ACTION_BLE_DISABLE:
  1886. setLedMotion(LED_ACTION_BLE_DISABLE);
  1887. break;
  1888. case LED_ACTION_ALL_OFF:
  1889. setLedMotion(LED_ACTION_ALL_OFF);
  1890. break;
  1891. case LED_ACTION_INTERNET_DISCONNECT:
  1892. setLedMotion(LED_ACTION_INTERNET_DISCONNECT);
  1893. break;
  1894. case LED_ACTION_HANDSHAKE_FAIL:
  1895. setLedMotion(LED_ACTION_HANDSHAKE_FAIL);
  1896. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  1897. break;
  1898. case LED_ACTION_RESTORE_SETTING:
  1899. setLedMotion(LED_ACTION_RESTORE_SETTING);
  1900. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  1901. break;
  1902. default:
  1903. break;
  1904. }
  1905. }
  1906. tx_len = 8;
  1907. tx[0] = 0xaa;
  1908. tx[1] = PROTOCOL_ADDR;
  1909. tx[2] = CurRxBuf[1];
  1910. tx[3] = PROTOCOL_MESSAGE_CONFIG_LED_ACTION_ALARM;
  1911. tx[4] = 0x01;
  1912. tx[5] = 0x00;
  1913. tx[6] = 0x01;
  1914. tx[7] = 0x01;
  1915. break;
  1916. case PROTOCOL_MESSAGE_CONFIG_MAX_CURRENT_PWM_DUTY:
  1917. tx_len = 8;
  1918. tx[0] = 0xaa;
  1919. tx[1] = PROTOCOL_ADDR;
  1920. tx[2] = CurRxBuf[1];
  1921. tx[3] = PROTOCOL_MESSAGE_CONFIG_MAX_CURRENT_PWM_DUTY;
  1922. tx[4] = 0x01;
  1923. tx[5] = 0x00;
  1924. #ifdef FUNC_CSU_CMD_DEBUG
  1925. XP("#CSU_SetCpPwmDuty: %d %s\r\n", CurRxBuf[6],
  1926. (Charger.m_bBlockCsuCpPwmDuty || Charger.m_bBlockCsuCpPwmDutyWhenAlarmCpF) ? "(Block)" : "");
  1927. #endif
  1928. if (
  1929. #ifdef FUNC_BLOCK_CSU_CONFIG_CP_PWM_DUTY
  1930. !Charger.m_bBlockCsuCpPwmDuty &&
  1931. #endif
  1932. #ifdef FUNC_BLOCK_CSU_CMD_SET_DUTY_WHEN_ALARM_CP_F_STATE
  1933. !Charger.m_bBlockCsuCpPwmDutyWhenAlarmCpF &&
  1934. #endif
  1935. (
  1936. (CurRxBuf[6] <= 32 && CurRxBuf[6] >= 6) ||
  1937. (CurRxBuf[6] == PWM_DUTY_OFF) ||
  1938. (CurRxBuf[6] == 1) ||
  1939. (CurRxBuf[6] == 5) ||
  1940. (CurRxBuf[6] == 100)
  1941. )
  1942. )
  1943. {
  1944. #ifdef MODIFY_AC_EVSE_STATUS_MAX_CHARGING_CURRENT_VARIABLE
  1945. Charger.AC_MaxChargingCurrentOrDuty = CurRxBuf[6];
  1946. #endif
  1947. tx[6] = 0x00;
  1948. if(CurRxBuf[6] <= 32 && CurRxBuf[6] >= 6)
  1949. {
  1950. Charger.memory.EVSE_Config.data.item.MaxChargingCurrent = CurRxBuf[6];
  1951. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  1952. user_pwm_setvalue(PWM_CH_CP, GetCpPwmDuty(Charger.memory.EVSE_Config.data.item.MaxChargingCurrent));
  1953. #else
  1954. //user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_651(Charger.memory.EVSE_Config.data.item.MaxChargingCurrent));
  1955. #endif
  1956. tx[6] = 0x01;
  1957. }
  1958. else if (CurRxBuf[6] == PWM_DUTY_OFF)
  1959. {
  1960. user_pwm_setvalue(PWM_CH_CP, 0); // State F
  1961. tx[6] = 0x01;
  1962. }
  1963. else if (CurRxBuf[6] == 5)
  1964. {
  1965. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_5);
  1966. tx[6] = 0x01;
  1967. }
  1968. else if (CurRxBuf[6] == 100)
  1969. {
  1970. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  1971. tx[6] = 0x01;
  1972. }
  1973. //control pilot E
  1974. if (CurRxBuf[6] == 1)
  1975. {
  1976. HAL_GPIO_WritePin(OUT_StateE_GPIO_Port, OUT_StateE_Pin, GPIO_PIN_SET); // ON // State E
  1977. user_pwm_setvalue(PWM_CH_CP, 0); //Temporarily, wait for EE to change the hardware and delete
  1978. tx[6] = 0x01;
  1979. }
  1980. else
  1981. {
  1982. HAL_GPIO_WritePin(OUT_StateE_GPIO_Port, OUT_StateE_Pin, GPIO_PIN_RESET); //OFF
  1983. }
  1984. XP("#CSU_SetCpPwmDuty_Final: %d\r\n", CurRxBuf[6]);
  1985. }
  1986. else
  1987. {
  1988. tx[6] = 0x00;
  1989. DEBUG_INFO("CSU_SetCpPwmDuty [NG] (%d) !!!!! \r\n", CurRxBuf[6]);
  1990. }
  1991. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1992. {
  1993. chksum ^= tx[6 + idx];
  1994. }
  1995. tx[7] = chksum; //checksum
  1996. break;
  1997. case PROTOCOL_MESSAGE_CONFIG_LEGACY_REQUEST:
  1998. #ifdef FUNC_CSU_CMD_DEBUG
  1999. XP("#CSU_SetRequest: %d\r\n", CurRxBuf[6]);
  2000. #endif
  2001. if((CurRxBuf[6]&SETTING_LEGACY_REQUEST)>0)
  2002. {
  2003. #ifdef FUNC_REQUEST_OFF_WHEN_GET_TWICE_OFF_CMD
  2004. CsuSetRequestOffCount = 0;
  2005. #endif
  2006. Charger.am3352.isRequestOff = OFF;
  2007. Charger.rfid.op_bits.reqStart = ON;
  2008. if(!Charger.am3352.isRequestOn)
  2009. {
  2010. DEBUG_INFO("#Request Pin On.\r\n");
  2011. Charger.am3352.isRequestOn = ON;
  2012. }
  2013. }
  2014. else
  2015. {
  2016. #ifdef FUNC_REQUEST_OFF_WHEN_GET_TWICE_OFF_CMD
  2017. if (++CsuSetRequestOffCount > 2)
  2018. {
  2019. CsuSetRequestOffCount = 0;
  2020. Charger.am3352.isRequestOn = OFF;
  2021. Charger.am3352.RequestPinDebounceCnt = 0;
  2022. Charger.rfid.op_bits.reqStart = OFF;
  2023. if(!Charger.am3352.isRequestOff)
  2024. {
  2025. DEBUG_INFO("#Request Pin Off.\r\n");
  2026. Charger.am3352.isRequestOff = ON;
  2027. }
  2028. }
  2029. #else //FUNC_REQUEST_OFF_WHEN_GET_TWICE_OFF_CMD
  2030. Charger.am3352.isRequestOn = OFF;
  2031. Charger.am3352.RequestPinDebounceCnt = 0;
  2032. Charger.rfid.op_bits.reqStart = OFF;
  2033. if(!Charger.am3352.isRequestOff)
  2034. {
  2035. DEBUG_INFO("#Request Pin Off.\r\n");
  2036. Charger.am3352.isRequestOff = ON;
  2037. }
  2038. #endif //FUNC_REQUEST_OFF_WHEN_GET_TWICE_OFF_CMD
  2039. }
  2040. tx_len = 8;
  2041. tx[0] = 0xaa;
  2042. tx[1] = PROTOCOL_ADDR;
  2043. tx[2] = CurRxBuf[1];
  2044. tx[3] = PROTOCOL_MESSAGE_CONFIG_LEGACY_REQUEST;
  2045. tx[4] = 0x01;
  2046. tx[5] = 0x00;
  2047. tx[6] = 0x01;
  2048. tx[7] = 0x01;
  2049. break;
  2050. case PROTOCOL_MESSAGE_CONFIG_MCU_RESET:
  2051. tx_len = 8;
  2052. tx[0] = 0xaa;
  2053. tx[1] = PROTOCOL_ADDR;
  2054. tx[2] = CurRxBuf[1];
  2055. tx[3] = PROTOCOL_MESSAGE_CONFIG_MCU_RESET;
  2056. tx[4] = 0x01;
  2057. tx[5] = 0x00;
  2058. tx[6] = 0x01;
  2059. tx[7] = 0x01;
  2060. if((CurRxBuf[6]&0x01)>0){
  2061. DEBUG_INFO("MCU reset request.\r\n");
  2062. #ifdef MODIFY_DC_RS485_UPGRADE_ISSUE
  2063. Prefix_UartTX(tx, CurRxBuf);
  2064. HAL_UART_Transmit(pUartHandle, (uint8_t *)tx, tx_len , 0x0ffff);
  2065. Postfix_UartTX(tx, CurRxBuf);
  2066. #else
  2067. HAL_UART_Transmit(&IAP_USART, (uint8_t *)tx, tx_len , 0x0ffff);
  2068. #endif
  2069. osDelay(1000);
  2070. NVIC_SystemReset();
  2071. }
  2072. else
  2073. {
  2074. tx[6] = 0x00;
  2075. tx[7] = 0x00;
  2076. }
  2077. break;
  2078. case PROTOCOL_MESSAGE_CONFIG_BREATHE_LED_TIMING:
  2079. Charger.am3352.BreatheLedConnectFadeIn = (((uint16_t)CurRxBuf[7]<<8)+(uint16_t)CurRxBuf[6]);
  2080. Charger.am3352.BreatheLedConnectFadeOut = (((uint16_t)CurRxBuf[9]<<8)+(uint16_t)CurRxBuf[8]);
  2081. DEBUG_INFO("Connect Breathe Led Fade In time: %d ms.\r\n",Charger.am3352.BreatheLedConnectFadeIn);
  2082. DEBUG_INFO("Connect Breathe Led Fade Out time: %d ms.\r\n",Charger.am3352.BreatheLedConnectFadeOut);
  2083. Charger.am3352.BreatheLedAuthdFadeIn = (((uint16_t)CurRxBuf[11]<<8)+(uint16_t)CurRxBuf[10]);
  2084. Charger.am3352.BreatheLedAuthdFadeOut = (((uint16_t)CurRxBuf[13]<<8)+(uint16_t)CurRxBuf[12]);
  2085. DEBUG_INFO("Authd Breathe Led Fade In time: %d ms.\r\n",Charger.am3352.BreatheLedAuthdFadeIn);
  2086. DEBUG_INFO("Authd Breathe Led Fade Out time: %d ms.\r\n",Charger.am3352.BreatheLedAuthdFadeOut);
  2087. Charger.am3352.BreatheLedChargeFadeIn = (((uint16_t)CurRxBuf[15]<<8)+(uint16_t)CurRxBuf[14]);
  2088. Charger.am3352.BreatheLedChargeFadeOut = (((uint16_t)CurRxBuf[17]<<8)+(uint16_t)CurRxBuf[16]);
  2089. DEBUG_INFO("Charge Breathe Led Fade In time: %d ms.\r\n",Charger.am3352.BreatheLedChargeFadeIn);
  2090. DEBUG_INFO("Charge Breathe Led Fade Out time: %d ms.\r\n",Charger.am3352.BreatheLedChargeFadeOut);
  2091. tx_len = 8;
  2092. tx[0] = 0xaa;
  2093. tx[1] = PROTOCOL_ADDR;
  2094. tx[2] = CurRxBuf[1];
  2095. tx[3] = PROTOCOL_MESSAGE_CONFIG_BREATHE_LED_TIMING;
  2096. tx[4] = 0x01;
  2097. tx[5] = 0x00;
  2098. tx[6] = 0x01;
  2099. tx[7] = 0x01;
  2100. break;
  2101. case PROTOCOL_MESSAGE_CONFIG_LED_BRIGHTNESS:
  2102. memcpy(Charger.Led_Brightness, &CurRxBuf[6], 12);
  2103. tx_len = 8;
  2104. tx[0] = 0xaa;
  2105. tx[1] = PROTOCOL_ADDR;
  2106. tx[2] = CurRxBuf[1];
  2107. tx[3] = PROTOCOL_MESSAGE_CONFIG_LED_BRIGHTNESS;
  2108. tx[4] = 0x01;
  2109. tx[5] = 0x00;
  2110. tx[6] = 0x01;
  2111. tx[7] = 0x01;
  2112. break;
  2113. case PROTOCOL_MESSAGE_CONFIG_EVSE_AUTH_MODE:
  2114. switch(CurRxBuf[6])
  2115. {
  2116. case AUTH_MODE_OCPP:
  2117. Charger.memory.EVSE_Config.data.item.AuthMode = AUTH_MODE_OCPP ;
  2118. break;
  2119. case AUTH_MODE_FREE:
  2120. Charger.memory.EVSE_Config.data.item.AuthMode = AUTH_MODE_FREE ;
  2121. break;
  2122. default:
  2123. Charger.memory.EVSE_Config.data.item.AuthMode = AUTH_MODE_FREE ;
  2124. Charger.memory.EVSE_Config.data.item.OfflinePolicy = RFID_USER_AUTH_FREE ;
  2125. break ;
  2126. }
  2127. switch(CurRxBuf[7])
  2128. {
  2129. case RFID_USER_AUTH_LOCAL_LIST:
  2130. Charger.memory.EVSE_Config.data.item.OfflinePolicy = RFID_USER_AUTH_LOCAL_LIST ;
  2131. break;
  2132. case RFID_USER_AUTH_PH:
  2133. Charger.memory.EVSE_Config.data.item.OfflinePolicy = RFID_USER_AUTH_PH ;
  2134. break;
  2135. case RFID_USER_AUTH_FREE:
  2136. Charger.memory.EVSE_Config.data.item.OfflinePolicy = RFID_USER_AUTH_FREE ;
  2137. break;
  2138. case RFID_USER_AUTH_NO_CHARGING:
  2139. Charger.memory.EVSE_Config.data.item.OfflinePolicy = RFID_USER_AUTH_NO_CHARGING ;
  2140. break;
  2141. default:
  2142. Charger.memory.EVSE_Config.data.item.AuthMode = AUTH_MODE_FREE ;
  2143. Charger.memory.EVSE_Config.data.item.OfflinePolicy = RFID_USER_AUTH_FREE ;
  2144. break ;
  2145. }
  2146. tx_len = 8;
  2147. tx[0] = 0xaa;
  2148. tx[1] = PROTOCOL_ADDR;
  2149. tx[2] = CurRxBuf[1];
  2150. tx[3] = PROTOCOL_MESSAGE_CONFIG_EVSE_AUTH_MODE;
  2151. tx[4] = 0x01;
  2152. tx[5] = 0x00;
  2153. tx[6] = 0x01;
  2154. tx[7] = 0x01;
  2155. Charger.memory.EVSE_Config.op_bits.update = ON;
  2156. break;
  2157. case PROTOCOL_MESSAGE_CONFIG_ERASE_MEMORY:
  2158. switch(CurRxBuf[6])
  2159. {
  2160. case 0:
  2161. Charger.memory.EVSE_Config.op_bits.clear = ON;
  2162. Charger.memory.whiteList.op_bits.clear = ON;
  2163. Charger.memory.hisCharging.op_bits.clear = ON;
  2164. Charger.memory.hisAlarm.op_bits.clear = ON;
  2165. Charger.memory.coldLoadPickUp.op_bits.clear = ON;
  2166. break;
  2167. case 1:
  2168. Charger.memory.EVSE_Config.op_bits.clear = ON;
  2169. break;
  2170. case 2:
  2171. Charger.memory.whiteList.op_bits.clear = ON;
  2172. break;
  2173. case 3:
  2174. Charger.memory.hisCharging.op_bits.clear = ON;
  2175. break;
  2176. case 4:
  2177. Charger.memory.hisAlarm.op_bits.clear = ON;
  2178. break;
  2179. case 5:
  2180. Charger.memory.coldLoadPickUp.op_bits.clear = ON;
  2181. break;
  2182. default:
  2183. break;
  2184. }
  2185. tx_len = 8;
  2186. tx[0] = 0xaa;
  2187. tx[1] = PROTOCOL_ADDR;
  2188. tx[2] = CurRxBuf[1];
  2189. tx[3] = PROTOCOL_MESSAGE_CONFIG_ERASE_MEMORY;
  2190. tx[4] = 0x01;
  2191. tx[5] = 0x00;
  2192. tx[6] = 0x01;
  2193. tx[7] = 0x01;
  2194. break;
  2195. /*
  2196. Firmware update message
  2197. */
  2198. case PROTOCOL_MESSAGE_UPGRADE_START:
  2199. tx_len = 8;
  2200. tx[0] = 0xaa;
  2201. tx[1] = PROTOCOL_ADDR;
  2202. tx[2] = CurRxBuf[1];
  2203. tx[3] = PROTOCOL_MESSAGE_UPGRADE_START;
  2204. tx[4] = 0x01;
  2205. tx[5] = 0x00;
  2206. binCRCTarget = (CurRxBuf[6] << 24) | (CurRxBuf[6] << 16) | (CurRxBuf[6] << 8) | (CurRxBuf[6] << 0);
  2207. flashdestination = NEW_CODE_ADDRESS;
  2208. if(FLASH_If_Erase(NEW_CODE_ADDRESS, 3)== FLASHIF_OK)
  2209. {
  2210. #if defined(DEBUG) || defined(RTOS_STAT)
  2211. DEBUG_INFO("Firmware transfer start, earase flash success....\n\r");
  2212. #endif
  2213. tx[6] = 0x01;
  2214. tx[7] = 0x01;
  2215. if (Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU)
  2216. {
  2217. setChargerMode(MODE_MAINTAIN);
  2218. timerEnable(TIMER_IDX_UPDATE, 10000);
  2219. }
  2220. }
  2221. else
  2222. {
  2223. #if defined(DEBUG) || defined(RTOS_STAT)
  2224. DEBUG_INFO("Firmware transfer start, earase flash fail....\n\r");
  2225. #endif
  2226. tx[6] = 0x00;
  2227. tx[7] = 0x00;
  2228. }
  2229. break;
  2230. case PROTOCOL_MESSAGE_UPGRADE_TRANS:
  2231. tx_len = 8;
  2232. tx[0] = 0xaa;
  2233. tx[1] = PROTOCOL_ADDR;
  2234. tx[2] = CurRxBuf[1];
  2235. tx[3] = PROTOCOL_MESSAGE_UPGRADE_TRANS;
  2236. tx[4] = 0x01;
  2237. tx[5] = 0x00;
  2238. flashdestination = NEW_CODE_ADDRESS + ((CurRxBuf[6]<<0) | (CurRxBuf[7]<<8) | (CurRxBuf[8]<<16) |(CurRxBuf[9]<<24));
  2239. if (FLASH_If_Write(flashdestination, (uint32_t*) &CurRxBuf[10], ((((CurRxBuf[4]) | (CurRxBuf[5])<<8)-4)>>2)) == FLASHIF_OK)
  2240. {
  2241. #if defined(DEBUG) || defined(RTOS_STAT)
  2242. //DEBUG_INFO("Firmware transfer start address: 0x%x, length: %d...Pass\n\r", flashdestination, (((CurRxBuf[4]) | (CurRxBuf[5])<<8)-4));
  2243. #endif
  2244. tx[6] = 0x01;
  2245. tx[7] = 0x01;
  2246. if (Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU)
  2247. {
  2248. setLedMotion(LED_ACTION_MAINTAIN);
  2249. timerRefresh(TIMER_IDX_UPDATE);
  2250. }
  2251. }
  2252. else
  2253. {
  2254. #if defined(DEBUG) || defined(RTOS_STAT)
  2255. DEBUG_INFO("Firmware transfer start address: 0x%x, length: %d...Fail\n\r", flashdestination, (((CurRxBuf[4]) | (CurRxBuf[5])<<8)-4));
  2256. #endif
  2257. tx[6] = 0x00;
  2258. tx[7] = 0x00;
  2259. }
  2260. break;
  2261. case PROTOCOL_MESSAGE_UPGRADE_STOP:
  2262. tx_len = 8;
  2263. tx[0] = 0xaa;
  2264. tx[1] = PROTOCOL_ADDR;
  2265. tx[2] = CurRxBuf[1];
  2266. tx[3] = PROTOCOL_MESSAGE_UPGRADE_STOP;
  2267. tx[4] = 0x01;
  2268. tx[5] = 0x00;
  2269. flash = NEW_CODE_ADDRESS;
  2270. checksum = HAL_CRC_Calculate(&hcrc, (uint32_t *)flash, ((FLASH_AP_LENGTH-4)>>2));
  2271. flash = ((uint32_t)(NEW_CODE_ADDRESS+FLASH_AP_LENGTH-4));
  2272. #if defined(DEBUG) || defined(RTOS_STAT)
  2273. DEBUG_INFO("Firmware transfer end, AP CRC checksum, flash: 0x%x, 0x%x\r\n", checksum, *((uint32_t *)flash) );
  2274. #endif
  2275. if(checksum == *((uint32_t *)flash))
  2276. {
  2277. if (FLASH_If_Write(UPGRADE_REQ_ADDRESS, (uint32_t *)&endFlag[0], 1) == FLASHIF_OK)
  2278. {
  2279. #if defined(DEBUG) || defined(RTOS_STAT)
  2280. DEBUG_INFO("Firmware Confirm Tag write ok..\n\r");
  2281. #endif
  2282. tx[6] = 0x01;
  2283. tx[7] = 0x01;
  2284. if(isUSB_CDC_IAP)
  2285. {
  2286. CDC_Transmit_FS((uint8_t *)tx, tx_len);
  2287. }
  2288. else
  2289. {
  2290. #ifdef MODIFY_DC_RS485_UPGRADE_ISSUE
  2291. Prefix_UartTX(tx, CurRxBuf);
  2292. HAL_UART_Transmit(pUartHandle, (uint8_t *)tx, tx_len , 0x0ffff);
  2293. Postfix_UartTX(tx, CurRxBuf);
  2294. #else
  2295. HAL_UART_Transmit(&IAP_USART, (uint8_t *)tx, tx_len , 0x0ffff);
  2296. #endif
  2297. }
  2298. osDelay(1000);
  2299. NVIC_SystemReset();
  2300. }
  2301. else
  2302. {
  2303. #if defined(DEBUG) || defined(RTOS_STAT)
  2304. DEBUG_INFO("Firmware Confirm Tag write fail...\n\r");
  2305. #endif
  2306. tx[6] = 0x00;
  2307. tx[7] = 0x00;
  2308. }
  2309. }
  2310. else
  2311. {
  2312. #if defined(DEBUG) || defined(RTOS_STAT)
  2313. DEBUG_INFO("Firmware checksum compare fail...\n\r");
  2314. #endif
  2315. tx[6] = 0x00;
  2316. tx[7] = 0x00;
  2317. }
  2318. if (Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU)
  2319. {
  2320. timerDisable(TIMER_IDX_UPDATE);
  2321. }
  2322. break;
  2323. case PROTOCOL_MESSAGE_UPGRADE_ABOARD:
  2324. #if defined(DEBUG) || defined(RTOS_STAT)
  2325. DEBUG_INFO("Firmware update transfer aboard...\n\r");
  2326. #endif
  2327. tx_len = 8;
  2328. tx[0] = 0xaa;
  2329. tx[1] = PROTOCOL_ADDR;
  2330. tx[2] = CurRxBuf[1];
  2331. tx[3] = PROTOCOL_MESSAGE_UPGRADE_ABOARD;
  2332. tx[4] = 0x01;
  2333. tx[5] = 0x00;
  2334. tx[6] = 0x01;
  2335. tx[7] = 0x01;
  2336. break;
  2337. default:
  2338. /* Todo: bin file receive aboard */
  2339. #if defined(DEBUG) || defined(RTOS_STAT)
  2340. #ifdef FUNC_RS485_SLAVE
  2341. DEBUG_INFO("Protocol message unknow (0x%02X)\n\r", CurRxBuf[3]);
  2342. #else
  2343. DEBUG_INFO("Protocol message unknow...\n\r");
  2344. #endif
  2345. #endif
  2346. tx_len = 8;
  2347. tx[0] = 0xaa;
  2348. tx[1] = PROTOCOL_ADDR;
  2349. tx[2] = CurRxBuf[1];
  2350. tx[3] = CurRxBuf[3];
  2351. tx[4] = 0x01;
  2352. tx[5] = 0x00;
  2353. tx[6] = 0x00;
  2354. tx[7] = 0x00;
  2355. break;
  2356. }
  2357. }
  2358. else
  2359. {
  2360. #ifndef FUNC_RS485_SLAVE
  2361. #if defined(DEBUG) || defined(RTOS_STAT)
  2362. DEBUG_INFO("Protocol check sum is wrong...\n\r");
  2363. #endif
  2364. tx_len = 8;
  2365. tx[0] = 0xaa;
  2366. tx[1] = PROTOCOL_ADDR;
  2367. tx[2] = CurRxBuf[1];
  2368. tx[3] = CurRxBuf[3];
  2369. tx[4] = 0x01;
  2370. tx[5] = 0x00;
  2371. tx[6] = 0x00;
  2372. tx[7] = 0x00;
  2373. #endif //FUNC_RS485_SLAVE
  2374. }
  2375. if(isUSB_CDC_IAP)
  2376. {
  2377. CDC_Transmit_FS((uint8_t *)tx, tx_len);
  2378. }
  2379. else
  2380. {
  2381. #ifdef FUNC_RS485_SLAVE
  2382. #ifdef MODIFY_DC_RS485_UPGRADE_ISSUE
  2383. if (CurRxBuf != NULL)
  2384. {
  2385. #ifdef FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  2386. if (CurRxBuf == UART_IAP_rx_buffer)
  2387. {
  2388. Prefix_UartTX(tx, CurRxBuf);
  2389. HAL_UART_Transmit(pUartHandle, (uint8_t *)tx, tx_len , 0x0ffff);
  2390. Postfix_UartTX(tx, CurRxBuf);
  2391. }
  2392. else if (CurRxBuf == UART_RS485_rx_buffer)
  2393. {
  2394. if (CurRxBuf[IMCP_FM_DSTADDR_IDX] == Charger.m_EMPX10_RS485_ClientAddr)
  2395. {
  2396. Prefix_UartTX(tx, CurRxBuf);
  2397. HAL_UART_Transmit(pUartHandle, (uint8_t *)tx, tx_len , 0x0ffff);
  2398. Postfix_UartTX(tx, CurRxBuf);
  2399. }
  2400. // else if (CurRxBuf[IMCP_FM_DSTADDR_IDX] == EMPX10_RS485_ADDR_BROADCAST)
  2401. // {
  2402. // Prefix_UartTX(tx, CurRxBuf);
  2403. // HAL_UART_Transmit(pUartHandle, (uint8_t *)tx, tx_len , 0x0ffff);
  2404. // Postfix_UartTX(tx, CurRxBuf);
  2405. // }
  2406. }
  2407. #else //FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  2408. Prefix_UartTX(tx, CurRxBuf);
  2409. HAL_UART_Transmit(pUartHandle, (uint8_t *)tx, tx_len , 0x0ffff);
  2410. Postfix_UartTX(tx, CurRxBuf);
  2411. #endif //FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  2412. }
  2413. #else //MODIFY_DC_RS485_UPGRADE_ISSUE
  2414. if (CurRxBuf != NULL)
  2415. {
  2416. tx[1] = Charger.m_RS485SlaveAddr; //Update Slave Address
  2417. HAL_UART_Transmit(pUartHandle, (uint8_t *)tx, tx_len, 0xffff);
  2418. }
  2419. #endif //MODIFY_DC_RS485_UPGRADE_ISSUE
  2420. #else //FUNC_RS485_SLAVE
  2421. HAL_UART_Transmit(&IAP_USART, (uint8_t *)tx, tx_len, 0xffff);
  2422. #endif //FUNC_RS485_SLAVE
  2423. }
  2424. #ifdef DEBUG_EMPX10_COMM
  2425. if (CurRxBuf == UART_RS485_rx_buffer)
  2426. {
  2427. HTK_ByteArray2HexStr_XP(NULL, tx, 0, tx_len);
  2428. }
  2429. #endif //DEBUG_EMPX10_COMM
  2430. #ifdef ENABLE_PRINT_IMCP_MSG
  2431. #ifdef FUNC_RS485_SLAVE
  2432. #ifdef PRINT_IMCP_MSG_ONLY_ON_UPDATEPORT
  2433. if (CurRxBuf == UART_IAP_rx_buffer && CheckPrintImcpMsg(CurRxBuf[3]))
  2434. {
  2435. HTK_ByteArray2HexStr_XP(NULL, tx, 0, tx_len);
  2436. }
  2437. #else
  2438. if (CheckPrintImcpMsg(CurRxBuf[3]))
  2439. {
  2440. HTK_ByteArray2HexStr_XP(NULL, tx, 0, tx_len);
  2441. }
  2442. #endif //PRINT_IMCP_MSG_ONLY_ON_UPDATEPORT
  2443. #else //FUNC_RS485_SLAVE
  2444. if (CheckPrintImcpMsg(UART_IAP_rx_buffer[3]))
  2445. {
  2446. HTK_ByteArray2HexStr_XP(NULL, tx, 0, tx_len);
  2447. }
  2448. #endif //FUNC_RS485_SLAVE
  2449. #endif //ENABLE_PRINT_IMCP_MSG
  2450. #ifdef FUNC_RS485_SLAVE
  2451. // Clear rx_buffer and related varaible
  2452. for(uint16_t i = 0; i < *pCurRxBuf_RxLen ; i++)
  2453. CurRxBuf[i]=0;
  2454. *pCurRxBuf_RxLen = 0;
  2455. *pCurRecvEndFlag = 0;
  2456. isUSB_CDC_IAP = OFF;
  2457. #else
  2458. // Clear rx_buffer and related varaible
  2459. for(uint16_t i = 0; i < UART_IAP_rx_len ; i++)
  2460. UART_IAP_rx_buffer[i]=0;
  2461. UART_IAP_rx_len=0;
  2462. UART_IAP_recv_end_flag=0;
  2463. isUSB_CDC_IAP = OFF;
  2464. #endif //FUNC_RS485_SLAVE
  2465. }
  2466. if(timer[TIMER_IDX_UPDATE].isAlarm == ON && Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU)
  2467. {
  2468. setChargerMode(MODE_IDLE);
  2469. setLedMotion(LED_ACTION_IDLE);
  2470. timerDisable(TIMER_IDX_UPDATE);
  2471. }
  2472. #ifdef FUNC_RS485_SLAVE
  2473. HAL_UART_Receive_DMA(&IAP_USART,(uint8_t*)UART_IAP_rx_buffer, UART_BUFFER_SIZE);
  2474. HAL_UART_Receive_DMA(&RS485_USART,(uint8_t*)UART_RS485_rx_buffer, UART_BUFFER_SIZE);
  2475. #else
  2476. HAL_UART_Receive_DMA(&IAP_USART,(uint8_t*)UART_IAP_rx_buffer, UART_BUFFER_SIZE);
  2477. #endif //FUNC_RS485_SLAVE
  2478. osDelay(1);
  2479. }
  2480. /* USER CODE END StartUartTask */
  2481. }
  2482. /* USER CODE BEGIN Header_StartAdcTask */
  2483. /**
  2484. * @brief Function implementing the adcTask thread.
  2485. * @param argument: Not used
  2486. * @retval None
  2487. */
  2488. /* USER CODE END Header_StartAdcTask */
  2489. #ifdef FUNC_DEBUG_MSG_CP_STATE
  2490. 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)"};
  2491. #endif
  2492. void StartAdcTask(void const * argument)
  2493. {
  2494. /* USER CODE BEGIN StartAdcTask */
  2495. //aadc
  2496. uint8_t previous_cp_state = SYSTEM_STATE_A;
  2497. #ifdef MODIFY_POWER_CONSUME_ACCU_TIMER
  2498. uint16_t updateMemReq = 0;
  2499. #else
  2500. uint8_t updateMemReq = 0;
  2501. #endif
  2502. struct TimeTick
  2503. {
  2504. uint32_t timeTick_Start;
  2505. uint32_t timeTick_End;
  2506. uint32_t timeTick_Delta;
  2507. }timeTick;
  2508. timeTick.timeTick_Start = HAL_GetTick();
  2509. #ifdef FUNC_CP_ADC_MODIFY
  2510. #ifdef NEW_CP_SPEC
  2511. while (!Charger.m_bCpDetectModuleInitOK)
  2512. {
  2513. osDelay(10);
  2514. }
  2515. #endif //NEW_CP_SPEC
  2516. #endif //FUNC_CP_ADC_MODIFY
  2517. #ifdef MODIFY_POWER_CONSUME_ACCU_TIMER
  2518. timerEnable(TIMER_IDX_POWER, POWER_CONSUME_ACCU_TIMEOUT_SPEC);
  2519. #else
  2520. timerEnable(TIMER_IDX_POWER, 1000);
  2521. #endif
  2522. /* Infinite loop */
  2523. for(;;)
  2524. {
  2525. Relay_Control_Function( ) ;
  2526. // ADC chanel 1
  2527. if(isDMAEnd_ADC1)
  2528. {
  2529. isDMAEnd_ADC1 = OFF;
  2530. CpCalculate(ADC1_Buffer, ADC1_CHANEL_COUNT*ADC1_SAMPLE_COUNT, &CpDetectionResult);
  2531. #ifdef FUNC_EMPX10_CUSTOMIZE_CP_STATE_OPERATION
  2532. if (Charger.am3352.isRequestOn == ON)
  2533. {
  2534. if (Charger.Alarm_Code == 0)
  2535. {
  2536. if (Charger.m_CP_CurPWM == PWM_DUTY_FULL)
  2537. {
  2538. Charger.CP_State = SYSTEM_STATE_B;
  2539. }
  2540. else
  2541. {
  2542. Charger.CP_State = SYSTEM_STATE_C;
  2543. }
  2544. }
  2545. else
  2546. {
  2547. Charger.CP_State = SYSTEM_STATE_B;
  2548. }
  2549. }
  2550. else
  2551. {
  2552. Charger.CP_State = SYSTEM_STATE_A;
  2553. }
  2554. #else
  2555. Charger.CP_State = CpDetectionResult.State;
  2556. #endif
  2557. #ifdef FUNC_DEBUG_MSG_CP_STATE
  2558. if (previous_cp_state != Charger.CP_State)
  2559. {
  2560. XP("#CP: [%s] >>> [%s] (%+f, %+f)\r\n",
  2561. CP_STATE_TAG[previous_cp_state],
  2562. CP_STATE_TAG[Charger.CP_State],
  2563. (CpDetectionResult.PositiveValue == 0 ? 0 : CP_GET_OPA_VIN_USE_ADC(CpDetectionResult.PositiveValue)),
  2564. (CpDetectionResult.NegativeValue == 0 ? 0 : CP_GET_OPA_VIN_USE_ADC(CpDetectionResult.NegativeValue))
  2565. );
  2566. }
  2567. #endif
  2568. if (Charger.Mode!= MODE_DEBUG)
  2569. {
  2570. #ifdef MODIFY_ADCTASK_CP_STATE_CHANGE
  2571. if((previous_cp_state != Charger.CP_State))
  2572. #else
  2573. if((previous_cp_state != Charger.CP_State) && (Charger.CP_State !=SYSTEM_STATE_UNKNOWN ))
  2574. #endif
  2575. {
  2576. previous_cp_state = Charger.CP_State;
  2577. if(Charger.CP_State == SYSTEM_STATE_A)
  2578. {
  2579. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL); // CP PWM duty set 100%
  2580. Charger.Relay_Action=GPIO_RELAY_ACTION_OFF;
  2581. Charger.memory.EVSE_Config.data.item.AcPlugInTimes++;
  2582. Charger.memory.EVSE_Config.op_bits.update = ON;
  2583. DEBUG_INFO("EVSE connector plug counts: %d\r\n", Charger.memory.EVSE_Config.data.item.AcPlugInTimes);
  2584. }
  2585. }
  2586. }
  2587. //DEBUG_INFO("HAL_GetTick() = %d\r\n",HAL_GetTick());
  2588. if(HAL_ADC_Start_DMA(&hadc1, (uint32_t*)&ADC1_Buffer, ADC1_CHANEL_COUNT*ADC1_SAMPLE_COUNT)!=HAL_OK)Error_Handler();
  2589. }
  2590. // ADC chanel 2
  2591. if(isDMAEnd_ADC2)
  2592. {
  2593. isDMAEnd_ADC2 = OFF;
  2594. for(uint16_t idx=0;idx<ADC2_SAMPLE_COUNT;idx++)
  2595. {
  2596. ADC2_Buffer_Each[0][idx] = ADC2_Buffer[(idx*ADC2_CHANEL_COUNT) + 0];
  2597. ADC2_Buffer_Each[1][idx] = ADC2_Buffer[(idx*ADC2_CHANEL_COUNT) + 1];
  2598. }
  2599. if (WatchDogLeakRawDataCheck_Event()) //clear leak WatchDogcount
  2600. {
  2601. Charger.counter.WatchDogLeak = 0 ;
  2602. }
  2603. //RlyVmax_cal
  2604. //filter_move_avg(&adc_value.ADC2_IN5_Welding, RlyVmax_cal(ADC2_Buffer_Each[0], ADC2_SAMPLE_COUNT));
  2605. if (Charger.GroundingSystem == GROUNGING_SYSTEM_LL)
  2606. {
  2607. filter_move_avg(&adc_value.ADC2_IN5_Welding, avg_v_cal(ADC2_Buffer_Each[0], ADC2_SAMPLE_COUNT)); //unit: 0.01V
  2608. }
  2609. else
  2610. {
  2611. filter_move_avg(&adc_value.ADC2_IN5_Welding, RlyVmax_cal(ADC2_Buffer_Each[0], ADC2_SAMPLE_COUNT)); //unit: 0.01V
  2612. }
  2613. //Leak
  2614. filter_move_avg(&adc_value.ADC2_IN6_GF, GFRms_cal(ADC2_Buffer_Each[1], ADC2_SAMPLE_COUNT));
  2615. if ((((adc_value.ADC2_IN6_GF.value)*Charger.coefficient.gain_leak[0])+Charger.coefficient.offset_leak[0]) <=0)
  2616. Charger.Leak_Current = adc_value.ADC2_IN6_GF.value;
  2617. else
  2618. Charger.Leak_Current = (uint16_t) (((adc_value.ADC2_IN6_GF.value)*Charger.coefficient.gain_leak[0])+Charger.coefficient.offset_leak[0]) ;
  2619. if(HAL_ADC_Start_DMA(&hadc2, (uint32_t*)&ADC2_Buffer, ADC2_CHANEL_COUNT*ADC2_SAMPLE_COUNT)!=HAL_OK)Error_Handler();
  2620. }
  2621. // ADC chanel 3
  2622. if(isDMAEnd_ADC3)
  2623. {
  2624. isDMAEnd_ADC3 = OFF;
  2625. for(uint16_t idx=0;idx<ADC3_SAMPLE_COUNT;idx++)
  2626. {
  2627. ADC3_Buffer_Each[0][idx] = ADC3_Buffer[(idx*ADC3_CHANEL_COUNT) + 0];
  2628. ADC3_Buffer_Each[1][idx] = ADC3_Buffer[(idx*ADC3_CHANEL_COUNT) + 1];
  2629. ADC3_Buffer_Each[2][idx] = ADC3_Buffer[(idx*ADC3_CHANEL_COUNT) + 2];
  2630. ADC3_Buffer_Each[3][idx] = ADC3_Buffer[(idx*ADC3_CHANEL_COUNT) + 3];
  2631. ADC3_Buffer_Each[4][idx] = ADC3_Buffer[(idx*ADC3_CHANEL_COUNT) + 4];
  2632. }
  2633. //VT
  2634. filter_move_avg(&adc_value.ADC3_IN9_Voltage_L1, vRms_cal2(ADC3_Buffer_Each[0], ADC3_SAMPLE_COUNT));
  2635. //SetADCWDGBoundary();
  2636. //MCU VDD
  2637. filter_move_avg(&adc_value.ADC3_IN14_UL_1998, avg_cal(ADC3_Buffer_Each[1], ADC3_SAMPLE_COUNT));
  2638. //TEMP
  2639. filter_move_avg(&adc_value.ADC3_IN15_Temp, avg_cal(ADC3_Buffer_Each[2], ADC3_SAMPLE_COUNT));
  2640. Charger.temperature.SystemAmbientTemp = getTemperature(adc_value.ADC3_IN15_Temp.value);
  2641. //PE
  2642. filter_move_avg(&adc_value.ADC3_IN4_GMI_VL1, avg_cal(ADC3_Buffer_Each[3], ADC3_SAMPLE_COUNT));
  2643. //CT
  2644. Current_filter_move_avg(&adc_value.ADC3_IN7_Current_L1, cRms_cal(ADC3_Buffer_Each[4], ADC3_SAMPLE_COUNT));
  2645. //Correction
  2646. //VT
  2647. Charger.Voltage[0] = (uint16_t) (((adc_value.ADC3_IN9_Voltage_L1.value)*Charger.coefficient.gain_v[0])+Charger.coefficient.offset_v[0]) ;
  2648. //CT
  2649. #ifdef FUNC_SIMU_CHARGE_CURRENT_OUTPUT
  2650. if (Charger.m_bEnableSimuChargeCurrent)
  2651. {
  2652. if (Charger.Relay_Action == GPIO_RELAY_ACTION_ON)
  2653. {
  2654. Charger.Current[0] = Charger.memory.EVSE_Config.data.item.MaxChargingCurrent * 100 - (rand() % 30);
  2655. }
  2656. else
  2657. {
  2658. Charger.Current[0] = 0;
  2659. }
  2660. }
  2661. else
  2662. {
  2663. Charger.Current[0] = (uint16_t)((adc_value.ADC3_IN7_Current_L1.value*Charger.coefficient.gain_c[0])+Charger.coefficient.offset_c[0]);
  2664. }
  2665. #else
  2666. Charger.Current[0] = (uint16_t)((adc_value.ADC3_IN7_Current_L1.value*Charger.coefficient.gain_c[0])+Charger.coefficient.offset_c[0]);
  2667. #endif
  2668. //GMI
  2669. Charger.Gmi_Voltage = (uint16_t) (((adc_value.ADC3_IN4_GMI_VL1.value)*Charger.coefficient.gain_gmi[0])+Charger.coefficient.offset_gmi[0]) ;
  2670. #ifdef COLD_LOAD_PICKUP
  2671. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  2672. // check Power drop is not alarm
  2673. // Power drop detection
  2674. if((vRms_cal2(ADC3_Buffer_Each[0], ADC3_SAMPLE_COUNT) <= ALARM_SPEC_POWER_DROP) && (!Charger.AC_DripisTure) && (Charger.Mode != MODE_INITIAL))
  2675. {
  2676. if(Charger.counter.AC_Drip.fail > 1000)
  2677. {
  2678. //Charger.memory.EVSE_Config.data.item.isDispDebug = OFF;
  2679. if((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))
  2680. {
  2681. Charger.memory.coldLoadPickUp.data.item.isEnable = ON;
  2682. Charger.memory.coldLoadPickUp.data.item.DateTime.year = Charger.cycle_info.startDateTime.year;
  2683. Charger.memory.coldLoadPickUp.data.item.DateTime.month = Charger.cycle_info.startDateTime.month;
  2684. Charger.memory.coldLoadPickUp.data.item.DateTime.day = Charger.cycle_info.startDateTime.day;
  2685. Charger.memory.coldLoadPickUp.data.item.DateTime.hour = Charger.cycle_info.startDateTime.hour;
  2686. Charger.memory.coldLoadPickUp.data.item.DateTime.min = Charger.cycle_info.startDateTime.min;
  2687. Charger.memory.coldLoadPickUp.data.item.DateTime.sec = Charger.cycle_info.startDateTime.sec;
  2688. Charger.memory.coldLoadPickUp.data.item.Duration = Charger.cycle_info.Duration;
  2689. sprintf(Charger.memory.coldLoadPickUp.data.item.PlugType, EVSE_CONNECTOR_TYPE);
  2690. Charger.memory.coldLoadPickUp.data.item.powerSum = Charger.cycle_info.Power_Consumption;
  2691. Charger.memory.coldLoadPickUp.data.item.startType = Charger.cycle_info.StartType;
  2692. //memcpy(Charger.memory.coldLoadPickUp.data.item.user, Charger.rfid.currentCard, 16);
  2693. Charger.memory.coldLoadPickUp.op_bits.update = ON;
  2694. }
  2695. Charger.AC_DripisTure = ON ;
  2696. }
  2697. else
  2698. Charger.counter.AC_Drip.fail ++ ;
  2699. }
  2700. else if((vRms_cal2(ADC3_Buffer_Each[0], ADC3_SAMPLE_COUNT) > (ALARM_SPEC_UV+ALARM_SPEC_OUV_HYSTERESIS)))
  2701. {
  2702. Charger.counter.AC_Drip.fail = 0 ;
  2703. if(Charger.AC_DripisTure)
  2704. {
  2705. Charger.AC_DripisTure = OFF ;
  2706. DEBUG_INFO("Alarm AC Drip recover.\r\n");
  2707. }
  2708. }
  2709. }
  2710. #endif //COLD_LOAD_PICKUP
  2711. // Power accumulate calculation
  2712. if(timer[TIMER_IDX_POWER].isAlarm)
  2713. {
  2714. timeTick.timeTick_End = HAL_GetTick();
  2715. timeTick.timeTick_Delta = timeTick.timeTick_End - timeTick.timeTick_Start;
  2716. timeTick.timeTick_Start = timeTick.timeTick_End;
  2717. if(Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative >= 0xFFFFFFFF)
  2718. {
  2719. Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative = 0;
  2720. }
  2721. else
  2722. {
  2723. #ifdef MODIFY_POWER_CONSUME_ACCU_TIMER
  2724. {
  2725. static double Energy = 0;
  2726. #ifdef FUNC_ZERO_CURRENT_WHEN_OUTP_OFF
  2727. u16 Curr = (Charger.Relay_Action == GPIO_RELAY_ACTION_ON) ? Charger.Current[0] : 0;
  2728. Energy += (((double)Charger.Voltage[0]/100.0)*((double)Curr/100.0)*((double)timeTick.timeTick_Delta/3600000.0)*10.0);
  2729. #else
  2730. Energy += (((double)Charger.Voltage[0]/100.0)*((double)Charger.Current[0]/100.0)*((double)timeTick.timeTick_Delta/3600000.0)*10.0);
  2731. #endif
  2732. if (Energy >= 1)
  2733. {
  2734. uint32_t EnergyIntPart = (uint32_t)Energy;
  2735. Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative += EnergyIntPart;
  2736. Energy -= EnergyIntPart;
  2737. }
  2738. }
  2739. #else
  2740. //unit : 0.0001kW , 0.1W *10 = W
  2741. 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);
  2742. #endif
  2743. }
  2744. #ifdef MODIFY_POWER_CONSUME_ACCU_TIMER
  2745. if((++updateMemReq) > (180 * (1000 / (double)POWER_CONSUME_ACCU_TIMEOUT_SPEC))) //Interval = POWER_CONSUME_ACCU_TIMEOUT_SPEC ms
  2746. #else
  2747. if(++updateMemReq>180) //Interval = 1000ms
  2748. #endif
  2749. {
  2750. Charger.memory.EVSE_Config.op_bits.update = ON;
  2751. updateMemReq = 0;
  2752. }
  2753. timerRefresh(TIMER_IDX_POWER);
  2754. }
  2755. if(HAL_ADC_Start_DMA(&hadc3, (uint32_t*)&ADC3_Buffer, ADC3_CHANEL_COUNT*ADC3_SAMPLE_COUNT)!=HAL_OK)Error_Handler();
  2756. }
  2757. osDelay(1);
  2758. }
  2759. /* USER CODE END StartAdcTask */
  2760. }
  2761. /* USER CODE BEGIN Header_StartTimeoutTask */
  2762. /**
  2763. * @brief Function implementing the timeoutTask thread.
  2764. * @param argument: Not used
  2765. * @retval None
  2766. */
  2767. /* USER CODE END Header_StartTimeoutTask */
  2768. void StartTimeoutTask(void const * argument)
  2769. {
  2770. /* USER CODE BEGIN StartTimeoutTask */
  2771. breathe.isUp = ON;
  2772. for(int idx=0;idx<BLINKER_COUNT;idx++)
  2773. blinker[idx].blinkCount = 0;
  2774. flickerTimeSet(FLICKER_IDX_BREATHE, 500, 500);
  2775. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 0, 1);
  2776. breatheTimeSet(1000, 1000, 1000, 1000, 1000, 0 );
  2777. Charger.am3352.BreatheLedChargeFadeIn = 2500;
  2778. Charger.am3352.BreatheLedChargeFadeOut = 2500;
  2779. Charger.am3352.BreatheLedConnectFadeIn = 1000;
  2780. Charger.am3352.BreatheLedConnectFadeOut = 1000;
  2781. Charger.am3352.BreatheLedAuthdFadeIn = 1000;
  2782. Charger.am3352.BreatheLedAuthdFadeOut = 1000;
  2783. /* Infinite loop */
  2784. for(;;)
  2785. {
  2786. if(Charger.Mode != MODE_INITIAL)
  2787. {
  2788. HAL_RTC_GetTime(&hrtc, &currentTime, RTC_FORMAT_BIN);
  2789. HAL_RTC_GetDate(&hrtc, &currentDate, RTC_FORMAT_BIN);
  2790. Charger.memory.EVSE_Config.data.item.SystemDateTime.year = currentDate.Year + 2000;
  2791. Charger.memory.EVSE_Config.data.item.SystemDateTime.month = currentDate.Month;
  2792. Charger.memory.EVSE_Config.data.item.SystemDateTime.day = currentDate.Date;
  2793. Charger.memory.EVSE_Config.data.item.SystemDateTime.hour = currentTime.Hours;
  2794. Charger.memory.EVSE_Config.data.item.SystemDateTime.min = currentTime.Minutes;
  2795. Charger.memory.EVSE_Config.data.item.SystemDateTime.sec = currentTime.Seconds;
  2796. }
  2797. /*
  2798. Timer check process
  2799. */
  2800. for(int idx=0;idx<TIMER_COUNT;idx++)
  2801. {
  2802. if(timer[idx].isEnable)
  2803. {
  2804. if((HAL_GetTick() - timer[idx].startTime) > timer[idx].timeoutSpec)
  2805. {
  2806. timer[idx].isAlarm = ON;
  2807. }
  2808. else
  2809. {
  2810. timer[idx].isAlarm = OFF;
  2811. }
  2812. }
  2813. else
  2814. {
  2815. timer[idx].startTime = HAL_GetTick();
  2816. timer[idx].isAlarm = OFF;
  2817. }
  2818. }
  2819. /*
  2820. Blink timer process
  2821. */
  2822. for(int idx=0;idx<BLINKER_COUNT;idx++)
  2823. {
  2824. if((HAL_GetTick() - blinker[idx].blinkStartTime) > (blinker[idx].isOn ? blinker[idx].blinkOnSpec : blinker[idx].blinkOffSpec))
  2825. {
  2826. if(blinker[idx].blinkCount >= blinker[idx].blinkCountSpec)
  2827. {
  2828. if((HAL_GetTick() - blinker[idx].resetStartTime) > blinker[idx].blinkRestSpec)
  2829. {
  2830. if(!blinker[idx].blinkRestSpec) //no rest case
  2831. blinker[idx].blinkisFinish = ON ;
  2832. blinker[idx].blinkCount = 0;
  2833. }
  2834. else
  2835. {
  2836. blinker[idx].isOn= OFF;
  2837. blinker[idx].blinkisFinish = ON ;
  2838. }
  2839. }
  2840. else
  2841. {
  2842. if(blinker[idx].isOn)
  2843. blinker[idx].blinkCount++;
  2844. blinker[idx].blinkStartTime = HAL_GetTick();
  2845. blinker[idx].resetStartTime = HAL_GetTick();
  2846. blinker[idx].isOn ^= ON;
  2847. if(blinker[idx].blinkRestSpec > 0) //had rest case
  2848. blinker[idx].blinkisFinish = OFF ;
  2849. }
  2850. }
  2851. }
  2852. /*
  2853. Flick timer process
  2854. */
  2855. for(int idx=0;idx<FLICKER_COUNT;idx++)
  2856. {
  2857. if((HAL_GetTick() - flicker[idx].StartTime) > (flicker[idx].isOn ? flicker[idx].OnSpec : flicker[idx].OffSpec))
  2858. {
  2859. flicker[idx].StartTime = HAL_GetTick();
  2860. flicker[idx].isOn ^= ON;
  2861. }
  2862. }
  2863. /*
  2864. Breathe timer process
  2865. */
  2866. if(breathe.isUp)
  2867. {
  2868. breathe.countPause = 0;
  2869. breathe.countDown = breathe.timeDown;
  2870. if(++breathe.countUp >= breathe.timeUp)
  2871. breathe.isUp = OFF;
  2872. breathe.duty[0] = breathe.RGB[0]*breathe.countUp/breathe.timeUp;
  2873. breathe.duty[1] = breathe.RGB[1]*breathe.countUp/breathe.timeUp;
  2874. breathe.duty[2] = breathe.RGB[2]*breathe.countUp/breathe.timeUp;
  2875. breathe.duty[3] = breathe.RGB[3]*breathe.countUp/breathe.timeUp;
  2876. }
  2877. else
  2878. {
  2879. breathe.countUp = 1000*BREATHE_LED_LOW_BOUNDARY/breathe.timeDown;
  2880. if(breathe.countDown <= 1000*BREATHE_LED_LOW_BOUNDARY/breathe.timeDown)
  2881. {
  2882. //if(++breathe.countPause>250) //breathe ON->oFF need delay nedd Uncomment annotations
  2883. breathe.isUp = ON;
  2884. }
  2885. else
  2886. {
  2887. if(breathe.countDown > 1000*BREATHE_LED_LOW_BOUNDARY/breathe.timeDown)
  2888. breathe.countDown -=1;
  2889. }
  2890. breathe.duty[0] = breathe.RGB[0]*breathe.countDown/breathe.timeDown;
  2891. breathe.duty[1] = breathe.RGB[1]*breathe.countDown/breathe.timeDown;
  2892. breathe.duty[2] = breathe.RGB[2]*breathe.countDown/breathe.timeDown;
  2893. breathe.duty[3] = breathe.RGB[3]*breathe.countDown/breathe.timeDown;
  2894. }
  2895. osDelay(1);
  2896. }
  2897. /* USER CODE END StartTimeoutTask */
  2898. }
  2899. /* USER CODE BEGIN Header_StartRfidTask */
  2900. /**
  2901. * @brief Function implementing the rfidTask thread.
  2902. * @param argument: Not used
  2903. * @retval None
  2904. */
  2905. /* USER CODE END Header_StartRfidTask */
  2906. void StartRfidTask(void const * argument)
  2907. {
  2908. /* USER CODE BEGIN StartRfidTask */
  2909. //rrfid
  2910. uint8_t cardType = 0;
  2911. uint8_t tmpBuf[32];
  2912. uint8_t tx[UART_BUFFER_SIZE>>4];
  2913. uint8_t tx_len;
  2914. HAL_GPIO_WritePin(OUT_RFID_Reset_GPIO_Port, OUT_RFID_Reset_Pin, GPIO_PIN_SET);
  2915. /* Infinite loop */
  2916. for(;;)
  2917. {
  2918. // Read card SN
  2919. if(!Charger.rfid.op_bits.reqHalt && !Charger.rfid.op_bits.reqBlockRead)
  2920. {
  2921. switch(cardType)
  2922. {
  2923. case 0x00:
  2924. tx_len = 4;
  2925. tx[0] = tx_len - 0x01;
  2926. tx[1] = RFID_CMD_REQUEST_SN_14443A;
  2927. tx[2] = 0x01;
  2928. tx[3] = rfidCheckSumCal(tx, tx_len-1);
  2929. cardType++;
  2930. break;
  2931. case 0x01:
  2932. tx_len = 5;
  2933. tx[0] = tx_len - 0x01;
  2934. tx[1] = RFID_CMD_REQUEST_SN_14443B;
  2935. tx[2] = 0x00;
  2936. tx[3] = 0x00;
  2937. tx[4] = rfidCheckSumCal(tx, tx_len-1);
  2938. cardType++;
  2939. break;
  2940. case 0x02:
  2941. tx_len = 9;
  2942. tx[0] = tx_len - 0x01;
  2943. tx[1] = RFID_CMD_REQUEST_SN_FELICA;
  2944. tx[2] = 0x06;
  2945. tx[3] = 0x00;
  2946. tx[4] = 0xff;
  2947. tx[5] = 0xff;
  2948. tx[6] = 0x01;
  2949. tx[7] = 0x00;
  2950. tx[8] = rfidCheckSumCal(tx, tx_len-1);
  2951. cardType++;
  2952. break;
  2953. default:
  2954. cardType = 0;
  2955. break;
  2956. }
  2957. HAL_UART_Transmit(&RFID_USART, (uint8_t *)tx, tx_len, 0xffff);
  2958. osDelay(100);
  2959. }
  2960. if(Charger.rfid.op_bits.reqHalt)
  2961. {
  2962. tx_len = 3;
  2963. tx[0] = tx_len - 0x01;
  2964. tx[1] = RFID_CMD_HALT_14443A;
  2965. tx[2] = rfidCheckSumCal(tx, tx_len-1);
  2966. HAL_UART_Transmit(&RFID_USART, (uint8_t *)tx, tx_len, 0xffff);
  2967. osDelay(100);
  2968. }
  2969. if(Charger.rfid.op_bits.reqBlockRead)
  2970. {
  2971. tx_len = 0x0b;
  2972. tx[0] = tx_len - 0x01;
  2973. tx[1] = RFID_CMD_BLOCK_READ;
  2974. tx[2] = Charger.rfid.keyType;
  2975. tx[3] = Charger.rfid.targetBlock;
  2976. tx[4] = Charger.rfid.key[0];
  2977. tx[5] = Charger.rfid.key[1];
  2978. tx[6] = Charger.rfid.key[2];
  2979. tx[7] = Charger.rfid.key[3];
  2980. tx[8] = Charger.rfid.key[4];
  2981. tx[9] = Charger.rfid.key[5];
  2982. tx[10] = rfidCheckSumCal(tx, tx_len-1);
  2983. HAL_UART_Transmit(&RFID_USART, (uint8_t *)tx, tx_len, 0xffff);
  2984. osDelay(100);
  2985. }
  2986. // RFID module feedback process
  2987. if(UART_RFID_recv_end_flag == ON)
  2988. {
  2989. 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))
  2990. {
  2991. switch(UART_RFID_rx_buffer[1])
  2992. {
  2993. case RFID_CMD_LED:
  2994. break;
  2995. case RFID_CMD_REQUEST_SN_14443A:
  2996. if(UART_RFID_rx_buffer[0] == 9)
  2997. {
  2998. Charger.rfid.snType = RFID_SN_TYPE_4BYTE;
  2999. memset(&Charger.rfid.data_Sn[0], 0x00, ARRAY_SIZE(Charger.rfid.data_Sn));
  3000. for(uint8_t idx=0;idx<16;idx++)
  3001. {
  3002. if(idx<4)
  3003. {
  3004. Charger.rfid.data_Sn[idx] = UART_RFID_rx_buffer[idx+2];
  3005. }
  3006. else
  3007. {
  3008. Charger.rfid.data_Sn[idx] = 0x00;
  3009. }
  3010. }
  3011. if(isMode(MODE_IDLE))
  3012. {
  3013. memset(&Charger.rfid.currentCard[0], 0x00, ARRAY_SIZE(Charger.rfid.currentCard));
  3014. }
  3015. if(Charger.memory.EVSE_Config.data.item.RFID_SN_Endian)
  3016. {
  3017. 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]);
  3018. 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]);
  3019. memcpy(&Charger.rfid.nowCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.currentCard));
  3020. }
  3021. else
  3022. {
  3023. 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]);
  3024. 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]);
  3025. memcpy(&Charger.rfid.nowCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.currentCard));
  3026. }
  3027. }
  3028. else if(UART_RFID_rx_buffer[0] == 0x0c)
  3029. {
  3030. Charger.rfid.snType = RFID_SN_TYPE_7BYTE;
  3031. memset(&Charger.rfid.data_Sn[0], 0x00, ARRAY_SIZE(Charger.rfid.data_Sn));
  3032. for(uint8_t idx=0;idx<16;idx++)
  3033. {
  3034. if(idx<7)
  3035. {
  3036. Charger.rfid.data_Sn[idx] = UART_RFID_rx_buffer[idx+2];
  3037. }
  3038. else
  3039. {
  3040. Charger.rfid.data_Sn[idx] = 0x00;
  3041. }
  3042. }
  3043. if(isMode(MODE_IDLE))
  3044. {
  3045. memset(&Charger.rfid.currentCard[0], 0x00, ARRAY_SIZE(Charger.rfid.currentCard));
  3046. }
  3047. if(Charger.memory.EVSE_Config.data.item.RFID_SN_Endian)
  3048. {
  3049. 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]);
  3050. 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]);
  3051. memcpy(&Charger.rfid.nowCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.currentCard));
  3052. }
  3053. else
  3054. {
  3055. 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]);
  3056. 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]);
  3057. memcpy(&Charger.rfid.nowCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.currentCard));
  3058. }
  3059. }
  3060. //PH
  3061. if (Charger.memory.EVSE_Config.data.item.OfflinePolicy == RFID_USER_AUTH_PH )
  3062. {
  3063. if(UART_RFID_rx_buffer[0] >= 9)
  3064. {
  3065. if(isMode(MODE_IDLE))
  3066. {
  3067. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU)
  3068. {
  3069. Charger.rfid.targetBlock = 0x02;
  3070. Charger.rfid.keyType = 0x00;
  3071. memset(&Charger.rfid.key[0], 0xff, 6);
  3072. Charger.rfid.op_bits.reqBlockRead = ON;
  3073. //control in RFID_CMD_BLOCK_READ to Valid_RFID_SN
  3074. }
  3075. }
  3076. else if(isMode(MODE_CHARGING) || isMode(MODE_STOP))
  3077. {
  3078. if ((Array_data_Check(Charger.rfid.currentCard,Charger.rfid.nowCard))&&(Charger.cycle_info.StartType != START_METHOD_BLE))
  3079. {
  3080. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU)
  3081. {
  3082. Charger.rfid.op_bits.reqStop = ON;
  3083. setLedMotion(LED_ACTION_RFID_PASS);
  3084. Charger.speaker.motion = SPEAKER_SHORT;
  3085. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3086. }
  3087. DEBUG_WARN("reqStop = ON.\r\n");
  3088. }
  3089. else
  3090. {
  3091. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU)
  3092. {
  3093. setLedMotion(LED_ACTION_RFID_FAIL);
  3094. Charger.speaker.motion = SPEAKER_BLINK;
  3095. blinkerTimeSet(BLINKER_IDX_SPEAKER, 100, 100, SPEAK_RESET_RFID_TIME, 3) ; // SPEAK_RESET_RFID_TIME -> Avoid repeating
  3096. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3097. }
  3098. DEBUG_WARN("reqStop = Fail\r\n");
  3099. }
  3100. Charger.rfid.op_bits.reqHalt = ON;
  3101. Charger.rfid.op_bits.reqBlockRead = OFF;
  3102. }
  3103. }
  3104. }
  3105. //White List
  3106. else if (Charger.memory.EVSE_Config.data.item.OfflinePolicy == RFID_USER_AUTH_LOCAL_LIST)
  3107. {
  3108. DEBUG_WARN("OfflinePolicy is White List\r\n");
  3109. }
  3110. //Free
  3111. else if (Charger.memory.EVSE_Config.data.item.OfflinePolicy == RFID_USER_AUTH_FREE)
  3112. {
  3113. Charger.rfid.op_bits.reqHalt = ON;
  3114. Charger.rfid.op_bits.reqBlockRead = OFF;
  3115. if(isMode(MODE_IDLE))
  3116. {
  3117. for(uint8_t idx=0;idx<16;idx++)
  3118. {
  3119. Charger.rfid.currentCard[idx] = Charger.rfid.data_Sn[idx];
  3120. }
  3121. Charger.rfid.op_bits.reqStart = ON;
  3122. memcpy(&Charger.rfid.currentCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.currentCard));
  3123. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  3124. setLedMotion(LED_ACTION_RFID_PASS);
  3125. Charger.speaker.motion = SPEAKER_SHORT;
  3126. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3127. }
  3128. DEBUG_WARN("RFID authorize pass.\r\n");
  3129. }
  3130. else if(isMode(MODE_CHARGING) || isMode(MODE_STOP))
  3131. {
  3132. DEBUG_WARN("RFID authorize pass.\r\n");
  3133. if (Array_data_Check(Charger.rfid.currentCard,Charger.rfid.nowCard))
  3134. {
  3135. Charger.rfid.op_bits.reqStop = ON;
  3136. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  3137. setLedMotion(LED_ACTION_RFID_PASS);
  3138. Charger.speaker.motion = SPEAKER_SHORT;
  3139. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3140. }
  3141. DEBUG_WARN("reqStop = ON.\r\n");
  3142. }
  3143. else
  3144. {
  3145. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  3146. setLedMotion(LED_ACTION_RFID_FAIL);
  3147. Charger.speaker.motion = SPEAKER_BLINK;
  3148. blinkerTimeSet(BLINKER_IDX_SPEAKER, 100, 100, SPEAK_RESET_RFID_TIME, 3) ; // SPEAK_RESET_RFID_TIME -> Avoid repeating
  3149. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3150. }
  3151. DEBUG_WARN("reqStop = Fail\r\n");
  3152. }
  3153. }
  3154. }
  3155. //NO charger
  3156. else // Charger.memory.EVSE_Config.data.item.OfflinePolicy == RFID_USER_AUTH_NO_CHARGING
  3157. {
  3158. setLedMotion(LED_ACTION_RFID_FAIL);
  3159. Charger.speaker.motion = SPEAKER_BLINK;
  3160. blinkerTimeSet(BLINKER_IDX_SPEAKER, 100, 100, SPEAK_RESET_RFID_TIME, 3) ; // SPEAK_RESET_RFID_TIME -> Avoid repeating
  3161. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3162. Charger.rfid.op_bits.reqHalt = ON;
  3163. Charger.rfid.op_bits.reqBlockRead = OFF;
  3164. DEBUG_WARN("It is RFID NO charger mode \r\n");
  3165. }
  3166. break;
  3167. case RFID_CMD_REQUEST_SN_14443B:
  3168. if(UART_RFID_rx_buffer[1] == RFID_CMD_REQUEST_SN_14443B)
  3169. {
  3170. Charger.rfid.snType = RFID_SN_TYPE_14443B;
  3171. memset(&Charger.rfid.data_Sn[0], 0x00, ARRAY_SIZE(Charger.rfid.data_Sn));
  3172. for(uint8_t idx=0;idx<16;idx++)
  3173. {
  3174. if(idx<13)
  3175. {
  3176. Charger.rfid.data_Sn[idx] = UART_RFID_rx_buffer[idx+2];
  3177. }
  3178. }
  3179. if(isMode(MODE_IDLE))
  3180. memset(&Charger.rfid.nowCard[0], 0x00, ARRAY_SIZE(Charger.rfid.nowCard));
  3181. if(Charger.memory.EVSE_Config.data.item.RFID_SN_Endian)
  3182. {
  3183. 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]);
  3184. 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]);
  3185. memcpy(&Charger.rfid.nowCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.nowCard));
  3186. }
  3187. else
  3188. {
  3189. 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]);
  3190. 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]);
  3191. memcpy(&Charger.rfid.nowCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.nowCard));
  3192. }
  3193. }
  3194. //White List
  3195. if (Charger.memory.EVSE_Config.data.item.OfflinePolicy == RFID_USER_AUTH_LOCAL_LIST)
  3196. {
  3197. DEBUG_WARN("OfflinePolicy is White List\r\n");
  3198. }
  3199. //Free
  3200. else if (Charger.memory.EVSE_Config.data.item.OfflinePolicy == RFID_USER_AUTH_FREE)
  3201. {
  3202. Charger.rfid.op_bits.reqHalt = ON;
  3203. Charger.rfid.op_bits.reqBlockRead = OFF;
  3204. if(isMode(MODE_IDLE))
  3205. {
  3206. for(uint8_t idx=0;idx<16;idx++)
  3207. {
  3208. Charger.rfid.currentCard[idx] = Charger.rfid.data_Sn[idx];
  3209. }
  3210. Charger.rfid.op_bits.reqStart = ON;
  3211. memcpy(&Charger.rfid.currentCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.currentCard));
  3212. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  3213. setLedMotion(LED_ACTION_RFID_PASS);
  3214. Charger.speaker.motion = SPEAKER_SHORT;
  3215. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3216. }
  3217. DEBUG_WARN("RFID authorize pass.\r\n");
  3218. }
  3219. else if(isMode(MODE_CHARGING) || isMode(MODE_STOP))
  3220. {
  3221. DEBUG_WARN("RFID authorize pass.\r\n");
  3222. if (Array_data_Check(Charger.rfid.currentCard,Charger.rfid.nowCard))
  3223. {
  3224. Charger.rfid.op_bits.reqStop = ON;
  3225. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  3226. setLedMotion(LED_ACTION_RFID_PASS);
  3227. Charger.speaker.motion = SPEAKER_SHORT;
  3228. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3229. }
  3230. DEBUG_WARN("reqStop = ON.\r\n");
  3231. }
  3232. else
  3233. {
  3234. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  3235. setLedMotion(LED_ACTION_RFID_FAIL);
  3236. Charger.speaker.motion = SPEAKER_BLINK;
  3237. blinkerTimeSet(BLINKER_IDX_SPEAKER, 100, 100, SPEAK_RESET_RFID_TIME, 3) ; // SPEAK_RESET_RFID_TIME -> Avoid repeating
  3238. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3239. }
  3240. DEBUG_WARN("reqStop = Fail\r\n");
  3241. }
  3242. }
  3243. }
  3244. //NO charger
  3245. else // Charger.memory.EVSE_Config.data.item.OfflinePolicy == RFID_USER_AUTH_NO_CHARGING
  3246. {
  3247. setLedMotion(LED_ACTION_RFID_FAIL);
  3248. Charger.speaker.motion = SPEAKER_BLINK;
  3249. blinkerTimeSet(BLINKER_IDX_SPEAKER, 100, 100, SPEAK_RESET_RFID_TIME, 3) ; // SPEAK_RESET_RFID_TIME -> Avoid repeating
  3250. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3251. Charger.rfid.op_bits.reqHalt = ON;
  3252. Charger.rfid.op_bits.reqBlockRead = OFF;
  3253. DEBUG_WARN("It is RFID NO charger mode \r\n");
  3254. }
  3255. break;
  3256. case RFID_CMD_REQUEST_SN_FELICA:
  3257. if(UART_RFID_rx_buffer[1] == RFID_CMD_REQUEST_SN_FELICA)
  3258. {
  3259. Charger.rfid.snType = RFID_SN_TYPE_FELICA;
  3260. memset(&Charger.rfid.data_Sn[0], 0x00, ARRAY_SIZE(Charger.rfid.data_Sn));
  3261. for(uint8_t idx=0;idx<16;idx++)
  3262. {
  3263. if(idx<6)
  3264. {
  3265. Charger.rfid.data_Sn[idx] = UART_RFID_rx_buffer[idx+6];
  3266. }
  3267. }
  3268. if(isMode(MODE_IDLE))
  3269. memset(&Charger.rfid.nowCard[0], 0x00, ARRAY_SIZE(Charger.rfid.nowCard));
  3270. if(Charger.memory.EVSE_Config.data.item.RFID_SN_Endian)
  3271. {
  3272. 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]);
  3273. 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]);
  3274. memcpy(&Charger.rfid.nowCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.nowCard));
  3275. }
  3276. else
  3277. {
  3278. 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]);
  3279. 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]);
  3280. memcpy(&Charger.rfid.nowCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.nowCard));
  3281. }
  3282. }
  3283. //White List
  3284. if (Charger.memory.EVSE_Config.data.item.OfflinePolicy == RFID_USER_AUTH_LOCAL_LIST)
  3285. {
  3286. DEBUG_WARN("OfflinePolicy is White List\r\n");
  3287. }
  3288. //Free
  3289. else if (Charger.memory.EVSE_Config.data.item.OfflinePolicy == RFID_USER_AUTH_FREE)
  3290. {
  3291. Charger.rfid.op_bits.reqHalt = ON;
  3292. Charger.rfid.op_bits.reqBlockRead = OFF;
  3293. if(isMode(MODE_IDLE))
  3294. {
  3295. for(uint8_t idx=0;idx<16;idx++)
  3296. {
  3297. Charger.rfid.currentCard[idx] = Charger.rfid.data_Sn[idx];
  3298. }
  3299. Charger.rfid.op_bits.reqStart = ON;
  3300. memcpy(&Charger.rfid.currentCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.currentCard));
  3301. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  3302. setLedMotion(LED_ACTION_RFID_PASS);
  3303. Charger.speaker.motion = SPEAKER_SHORT;
  3304. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3305. }
  3306. DEBUG_WARN("RFID authorize pass.\r\n");
  3307. }
  3308. else if(isMode(MODE_CHARGING) || isMode(MODE_STOP))
  3309. {
  3310. DEBUG_WARN("RFID authorize pass.\r\n");
  3311. if (Array_data_Check(Charger.rfid.currentCard,Charger.rfid.nowCard))
  3312. {
  3313. Charger.rfid.op_bits.reqStop = ON;
  3314. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  3315. setLedMotion(LED_ACTION_RFID_PASS);
  3316. Charger.speaker.motion = SPEAKER_SHORT;
  3317. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3318. }
  3319. DEBUG_WARN("reqStop = ON.\r\n");
  3320. }
  3321. else
  3322. {
  3323. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  3324. setLedMotion(LED_ACTION_RFID_FAIL);
  3325. Charger.speaker.motion = SPEAKER_BLINK;
  3326. blinkerTimeSet(BLINKER_IDX_SPEAKER, 100, 100, SPEAK_RESET_RFID_TIME, 3) ; // SPEAK_RESET_RFID_TIME -> Avoid repeating
  3327. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3328. }
  3329. DEBUG_WARN("reqStop = Fail\r\n");
  3330. }
  3331. }
  3332. }
  3333. //NO charger
  3334. else // Charger.memory.EVSE_Config.data.item.OfflinePolicy == RFID_USER_AUTH_NO_CHARGING
  3335. {
  3336. setLedMotion(LED_ACTION_RFID_FAIL);
  3337. Charger.speaker.motion = SPEAKER_BLINK;
  3338. blinkerTimeSet(BLINKER_IDX_SPEAKER, 100, 100, SPEAK_RESET_RFID_TIME, 3) ; // SPEAK_RESET_RFID_TIME -> Avoid repeating
  3339. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3340. Charger.rfid.op_bits.reqHalt = ON;
  3341. Charger.rfid.op_bits.reqBlockRead = OFF;
  3342. DEBUG_WARN("It is RFID NO charger mode \r\n");
  3343. }
  3344. osDelay(1000);
  3345. break;
  3346. case RFID_CMD_BLOCK_READ:
  3347. if(UART_RFID_rx_buffer[1] == RFID_CMD_BLOCK_READ)
  3348. {
  3349. memcpy(Charger.rfid.data_Block, &UART_RFID_rx_buffer[2], 0x10);
  3350. Charger.rfid.op_bits.reqHalt = ON;
  3351. Charger.rfid.op_bits.reqBlockRead = OFF;
  3352. //RFID_PH
  3353. if(Valid_RFID_SN())
  3354. {
  3355. if(isMode(MODE_IDLE))
  3356. {
  3357. Charger.rfid.op_bits.reqStart = ON;
  3358. setLedMotion(LED_ACTION_RFID_PASS);
  3359. Charger.speaker.motion = SPEAKER_SHORT;
  3360. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3361. memcpy(&Charger.rfid.currentCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.currentCard)); }
  3362. else if(isMode(MODE_CHARGING) || isMode(MODE_STOP))
  3363. {
  3364. Charger.rfid.op_bits.reqStop = ON;
  3365. }
  3366. DEBUG_WARN("RFID authorize pass.\r\n");
  3367. }
  3368. else
  3369. {
  3370. setLedMotion(LED_ACTION_RFID_FAIL);
  3371. Charger.speaker.motion = SPEAKER_BLINK;
  3372. blinkerTimeSet(BLINKER_IDX_SPEAKER, 100, 100, SPEAK_RESET_RFID_TIME, 3) ; // SPEAK_RESET_RFID_TIME -> Avoid repeating
  3373. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3374. DEBUG_WARN("RFID authorize fault.\r\n");
  3375. }
  3376. }
  3377. break;
  3378. case RFID_CMD_BLOCK_WRITE:
  3379. break;
  3380. case RFID_CMD_HALT_14443A:
  3381. if(UART_RFID_rx_buffer[1] == RFID_CMD_HALT_14443A)
  3382. {
  3383. Charger.rfid.op_bits.reqHalt = OFF;
  3384. DEBUG_INFO("RFID card(14443A) halt succcess.\r\n");
  3385. }
  3386. break;
  3387. case RFID_CMD_HALT_14443B:
  3388. if(UART_RFID_rx_buffer[1] == RFID_CMD_HALT_14443B)
  3389. {
  3390. Charger.rfid.op_bits.reqHalt = OFF;
  3391. DEBUG_INFO("RFID card(14443B) halt succcess.\r\n");
  3392. }
  3393. break;
  3394. case (RFID_CMD_BLOCK_READ ^ 0xff):
  3395. Charger.rfid.op_bits.reqBlockRead = OFF;
  3396. DEBUG_WARN("RFID cread block fail.\r\n");
  3397. break;
  3398. default:
  3399. break;
  3400. }
  3401. }
  3402. // Clear rx_buffer and related varaible
  3403. for(uint16_t i = 0; i < UART_RFID_rx_len ; i++)
  3404. UART_RFID_rx_buffer[i]=0;
  3405. UART_RFID_rx_len = 0;
  3406. UART_RFID_recv_end_flag = OFF;
  3407. }
  3408. HAL_UART_Receive_DMA(&RFID_USART,(uint8_t*)UART_RFID_rx_buffer, (UART_BUFFER_SIZE>>4));
  3409. osDelay(300);
  3410. }
  3411. /* USER CODE END StartRfidTask */
  3412. }
  3413. /* USER CODE BEGIN Header_StartLedSpeakerTask */
  3414. /**
  3415. * @brief Function implementing the led_speakerTask thread.
  3416. * @param argument: Not used
  3417. * @retval None
  3418. */
  3419. /* USER CODE END Header_StartLedSpeakerTask */
  3420. void StartLedSpeakerTask(void const * argument)
  3421. {
  3422. /* USER CODE BEGIN StartLedSpeakerTask */
  3423. uint16_t led_pwm_duty_now[4];
  3424. uint16_t breathe_duty_now[4];
  3425. //standard default
  3426. for(uint8_t idx=0;idx<12;idx++)
  3427. Charger.Led_Brightness[idx] = 0x33;
  3428. /*
  3429. //Night mode starndard default brightness
  3430. Charger.Led_Brightness[0] = 0x11; //00:00~ 01:59
  3431. Charger.Led_Brightness[1] = 0x11; //02:00~ 03:59
  3432. Charger.Led_Brightness[2] = 0x11; //04:00~ 05:59
  3433. Charger.Led_Brightness[3] = 0x33; //06:00~ 07:59
  3434. Charger.Led_Brightness[4] = 0x33; //08:00~ 09:59
  3435. Charger.Led_Brightness[5] = 0x33; //10:00~ 11:59
  3436. Charger.Led_Brightness[6] = 0x33; //12:00~ 13:59
  3437. Charger.Led_Brightness[7] = 0x33; //14:00~ 15:59
  3438. Charger.Led_Brightness[8] = 0x33; //16:00~ 17:59
  3439. Charger.Led_Brightness[9] = 0x11; //18:00~ 19:59
  3440. Charger.Led_Brightness[10] = 0x11; //20:00~ 21:59
  3441. Charger.Led_Brightness[11] = 0x11; //22:00~ 23:59
  3442. */
  3443. /* Infinite loop */
  3444. for(;;)
  3445. {
  3446. led_pwm_duty_now[0] = getBrightnessDuty(Charger.led_pwm_duty[0]);
  3447. led_pwm_duty_now[1] = getBrightnessDuty(Charger.led_pwm_duty[1]);
  3448. led_pwm_duty_now[2] = getBrightnessDuty(Charger.led_pwm_duty[2]);
  3449. led_pwm_duty_now[3] = getBrightnessDuty(Charger.led_pwm_duty[3]);
  3450. breathe_duty_now[0] = getBrightnessDuty(breathe.duty[0]);
  3451. breathe_duty_now[1] = getBrightnessDuty(breathe.duty[1]);
  3452. breathe_duty_now[2] = getBrightnessDuty(breathe.duty[2]);
  3453. breathe_duty_now[3] = getBrightnessDuty(breathe.duty[3]);
  3454. if(timer[TIMER_IDX_LED_TEMP].isAlarm)
  3455. {
  3456. timerDisable(TIMER_IDX_LED_TEMP);
  3457. setLedMotion(Charger.Led_Mode);
  3458. }
  3459. /*
  3460. LED red process
  3461. */
  3462. switch(Charger.led_R.motion)
  3463. {
  3464. case LED_MOTION_ON:
  3465. user_pwm_setvalue(PWM_CH_LED_R, led_pwm_duty_now[0]);
  3466. break;
  3467. case LED_MOTION_BLINK:
  3468. if(blinker[BLINKER_IDX_LED].isOn)
  3469. user_pwm_setvalue(PWM_CH_LED_R, led_pwm_duty_now[0]);
  3470. else
  3471. user_pwm_setvalue(PWM_CH_LED_R, PWM_DUTY_OFF);
  3472. break;
  3473. case LED_MOTION_BREATHE:
  3474. user_pwm_setvalue(PWM_CH_LED_R, breathe_duty_now[0]);
  3475. break;
  3476. case LED_MOTION_OFF:
  3477. default:
  3478. user_pwm_setvalue(PWM_CH_LED_R, PWM_DUTY_OFF);
  3479. break;
  3480. }
  3481. /*
  3482. LED green process
  3483. */
  3484. switch(Charger.led_G.motion)
  3485. {
  3486. case LED_MOTION_ON:
  3487. user_pwm_setvalue(PWM_CH_LED_G, led_pwm_duty_now[1]);
  3488. break;
  3489. case LED_MOTION_BLINK:
  3490. if(blinker[BLINKER_IDX_LED].isOn)
  3491. user_pwm_setvalue(PWM_CH_LED_G, led_pwm_duty_now[1]);
  3492. else
  3493. user_pwm_setvalue(PWM_CH_LED_G, PWM_DUTY_OFF);
  3494. break;
  3495. case LED_MOTION_BREATHE:
  3496. user_pwm_setvalue(PWM_CH_LED_G, breathe_duty_now[1]);
  3497. break;
  3498. case LED_MOTION_OFF:
  3499. default:
  3500. user_pwm_setvalue(PWM_CH_LED_G, PWM_DUTY_OFF);
  3501. break;
  3502. }
  3503. /*
  3504. LED blue process
  3505. */
  3506. switch(Charger.led_B.motion)
  3507. {
  3508. case LED_MOTION_ON:
  3509. user_pwm_setvalue(PWM_CH_LED_B, led_pwm_duty_now[2]);
  3510. break;
  3511. case LED_MOTION_BLINK:
  3512. if(blinker[BLINKER_IDX_LED].isOn)
  3513. user_pwm_setvalue(PWM_CH_LED_B, led_pwm_duty_now[2]);
  3514. else
  3515. user_pwm_setvalue(PWM_CH_LED_B, PWM_DUTY_OFF);
  3516. break;
  3517. case LED_MOTION_BREATHE:
  3518. user_pwm_setvalue(PWM_CH_LED_B, breathe_duty_now[2]);
  3519. break;
  3520. case LED_MOTION_OFF:
  3521. default:
  3522. user_pwm_setvalue(PWM_CH_LED_B, PWM_DUTY_OFF);
  3523. break;
  3524. }
  3525. /*
  3526. LED white process
  3527. */
  3528. switch(Charger.led_W.motion)
  3529. {
  3530. case LED_MOTION_ON:
  3531. user_pwm_setvalue(PWM_CH_LED_W, led_pwm_duty_now[3]);
  3532. break;
  3533. case LED_MOTION_BLINK:
  3534. if(blinker[BLINKER_IDX_LED].isOn)
  3535. user_pwm_setvalue(PWM_CH_LED_W, led_pwm_duty_now[3]);
  3536. else
  3537. user_pwm_setvalue(PWM_CH_LED_W, PWM_DUTY_OFF);
  3538. break;
  3539. case LED_MOTION_BREATHE:
  3540. user_pwm_setvalue(PWM_CH_LED_W, breathe_duty_now[3]);
  3541. break;
  3542. case LED_MOTION_OFF:
  3543. default:
  3544. user_pwm_setvalue(PWM_CH_LED_W, PWM_DUTY_OFF);
  3545. break;
  3546. }
  3547. /*
  3548. LED breathe process
  3549. */
  3550. if(flicker[FLICKER_IDX_BREATHE].isOn)
  3551. HAL_GPIO_WritePin(OUT_LED_Breathe_GPIO_Port, OUT_LED_Breathe_Pin, GPIO_PIN_SET);
  3552. else
  3553. HAL_GPIO_WritePin(OUT_LED_Breathe_GPIO_Port, OUT_LED_Breathe_Pin, GPIO_PIN_RESET);
  3554. /*
  3555. Spearker process
  3556. */
  3557. switch(Charger.speaker.motion)
  3558. {
  3559. case SPEAKER_LONG:
  3560. timerEnable(TIMER_IDX_SPEAKER, 600);
  3561. HAL_GPIO_WritePin(OUT_Speaker_GPIO_Port, OUT_Speaker_Pin, GPIO_PIN_SET);
  3562. if(timer[TIMER_IDX_SPEAKER].isAlarm)
  3563. {
  3564. timerDisable(TIMER_IDX_SPEAKER);
  3565. Charger.speaker.motion = SPEAKER_NONE;
  3566. }
  3567. break;
  3568. case SPEAKER_SHORT:
  3569. timerEnable(TIMER_IDX_SPEAKER, 200);
  3570. HAL_GPIO_WritePin(OUT_Speaker_GPIO_Port, OUT_Speaker_Pin, GPIO_PIN_SET);
  3571. if(timer[TIMER_IDX_SPEAKER].isAlarm)
  3572. {
  3573. timerDisable(TIMER_IDX_SPEAKER);
  3574. Charger.speaker.motion = SPEAKER_NONE;
  3575. }
  3576. break;
  3577. case SPEAKER_BLINK:
  3578. if(blinker[BLINKER_IDX_SPEAKER].isOn)
  3579. HAL_GPIO_WritePin(OUT_Speaker_GPIO_Port, OUT_Speaker_Pin, GPIO_PIN_SET);
  3580. else
  3581. HAL_GPIO_WritePin(OUT_Speaker_GPIO_Port, OUT_Speaker_Pin, GPIO_PIN_RESET);
  3582. if(blinker[BLINKER_IDX_SPEAKER].blinkisFinish)
  3583. {
  3584. disblinkerTime(BLINKER_IDX_SPEAKER);
  3585. }
  3586. break;
  3587. case SPEAKER_NONE:
  3588. default:
  3589. HAL_GPIO_WritePin(OUT_Speaker_GPIO_Port, OUT_Speaker_Pin, GPIO_PIN_RESET);
  3590. break;
  3591. }
  3592. osDelay(1);
  3593. }
  3594. /* USER CODE END StartLedSpeakerTask */
  3595. }
  3596. /* USER CODE BEGIN Header_StartCpTask */
  3597. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  3598. void CpTask_CtrlCpPwm_P0(void)
  3599. {
  3600. //#ifdef FUNC_CCS //======================================================================
  3601. uint16_t MaxCurr = Charger.memory.EVSE_Config.data.item.MaxChargingCurrent;
  3602. // if (Charger.m_bRunCCS) //CCS Charge
  3603. // {
  3604. // user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_5);
  3605. // Charger.MaxChargingCurrent_Previous = MaxCurr;
  3606. // }
  3607. // else //Normal Charge
  3608. {
  3609. #ifdef FUNC_MODIFY_CP_PWM_CTRL_20231225
  3610. if (Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU || Charger.m_bModelNameDC)
  3611. {
  3612. if ((MaxCurr >= 6) && (MaxCurr <= Charger.maxRatingCurrent))
  3613. {
  3614. user_pwm_setvalue(PWM_CH_CP, GetCpPwmDuty(MaxCurr));
  3615. Charger.MaxChargingCurrent_Previous = MaxCurr;
  3616. }
  3617. else
  3618. {
  3619. user_pwm_setvalue(PWM_CH_CP, GetCpPwmDuty(Charger.maxRatingCurrent));
  3620. Charger.MaxChargingCurrent_Previous = Charger.maxRatingCurrent;
  3621. }
  3622. }
  3623. else
  3624. {
  3625. if ((Charger.AC_MaxChargingCurrentOrDuty >= 6) && (Charger.AC_MaxChargingCurrentOrDuty <= Charger.maxRatingCurrent))
  3626. {
  3627. user_pwm_setvalue(PWM_CH_CP, GetCpPwmDuty(Charger.AC_MaxChargingCurrentOrDuty));
  3628. }
  3629. else
  3630. {
  3631. if (Charger.AC_MaxChargingCurrentOrDuty == 0)
  3632. {
  3633. user_pwm_setvalue(PWM_CH_CP, 0);
  3634. }
  3635. else if (Charger.AC_MaxChargingCurrentOrDuty == 1)
  3636. {
  3637. user_pwm_setvalue(PWM_CH_CP, 0);
  3638. }
  3639. else if (Charger.AC_MaxChargingCurrentOrDuty == 5)
  3640. {
  3641. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_5);
  3642. }
  3643. else if (Charger.AC_MaxChargingCurrentOrDuty == 100)
  3644. {
  3645. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  3646. }
  3647. else
  3648. {
  3649. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  3650. }
  3651. }
  3652. Charger.MaxChargingCurrent_Previous = Charger.AC_MaxChargingCurrentOrDuty;
  3653. }
  3654. #else //FUNC_MODIFY_CP_PWM_CTRL_20231225
  3655. if ((MaxCurr >= 6) && (MaxCurr <= Charger.maxRatingCurrent))
  3656. {
  3657. user_pwm_setvalue(PWM_CH_CP, GetCpPwmDuty(MaxCurr));
  3658. Charger.MaxChargingCurrent_Previous = MaxCurr;
  3659. }
  3660. else
  3661. {
  3662. user_pwm_setvalue(PWM_CH_CP, GetCpPwmDuty(Charger.maxRatingCurrent));
  3663. Charger.MaxChargingCurrent_Previous = Charger.maxRatingCurrent;
  3664. }
  3665. #endif //FUNC_MODIFY_CP_PWM_CTRL_20231225
  3666. }
  3667. //#else //FUNC_CCS //======================================================================
  3668. // uint16_t MaxCurr = Charger.memory.EVSE_Config.data.item.MaxChargingCurrent;
  3669. // if (MaxCurr < 6)
  3670. // {
  3671. //#ifdef MODIFY_CP_TASK_CTRL_CP_PWM_BELOW_SIX
  3672. // if (MaxCurr == 0)
  3673. // {
  3674. // user_pwm_setvalue(PWM_CH_CP, 0);
  3675. // Charger.MaxChargingCurrent_Previous = MaxCurr;
  3676. // }
  3677. // else
  3678. // {
  3679. // user_pwm_setvalue(PWM_CH_CP, GetCpPwmDuty(6));
  3680. // Charger.MaxChargingCurrent_Previous = MaxCurr;
  3681. // }
  3682. //#else //MODIFY_CP_TASK_CTRL_CP_PWM_BELOW_SIX
  3683. // user_pwm_setvalue(PWM_CH_CP, GetCpPwmDuty(6));
  3684. // Charger.MaxChargingCurrent_Previous = MaxCurr;
  3685. //#endif //MODIFY_CP_TASK_CTRL_CP_PWM_BELOW_SIX
  3686. // }
  3687. // else if ((MaxCurr >= 6) && (MaxCurr <= Charger.maxRatingCurrent))
  3688. // {
  3689. // user_pwm_setvalue(PWM_CH_CP, GetCpPwmDuty(MaxCurr));
  3690. // Charger.MaxChargingCurrent_Previous = MaxCurr;
  3691. // }
  3692. // else
  3693. // {
  3694. // user_pwm_setvalue(PWM_CH_CP, GetCpPwmDuty(Charger.maxRatingCurrent));
  3695. // Charger.MaxChargingCurrent_Previous = Charger.maxRatingCurrent;
  3696. // }
  3697. //#endif //FUNC_CCS //======================================================================
  3698. }
  3699. void CpTask_CtrlCpPwm_P1(void)
  3700. {
  3701. if (
  3702. (Charger.memory.EVSE_Config.data.item.MaxChargingCurrent != Charger.MaxChargingCurrent_Previous) &&
  3703. (Charger.memory.EVSE_Config.data.item.MaxChargingCurrent <= Charger.maxRatingCurrent)
  3704. )
  3705. {
  3706. CpTask_CtrlCpPwm_P0();
  3707. }
  3708. }
  3709. #endif //MODIFY_CP_TASK_CTRL_CP_PWM
  3710. #ifdef TRIM_CCID_SELFTEST
  3711. void Proc_CCID_SelfTest(void)
  3712. {
  3713. // check Leak Module do CCID selftset.
  3714. if (Charger.CCID_Module_Type == CCID_MODULE_CORMEX)
  3715. {
  3716. Charger.Test_LeakModuleisPass = Test_LeakModule();
  3717. }
  3718. else if (Charger.CCID_Module_Type == CCID_MODULE_VAC)
  3719. {
  3720. Charger.Test_LeakModuleisPass = PASS;
  3721. }
  3722. else
  3723. {
  3724. DEBUG_INFO("Did not judge Leak module can't self test before charging.\r\n");
  3725. }
  3726. XP("Proc CCID Self-Test(%d): %s\r\n", Charger.CCID_Module_Type, Charger.Test_LeakModuleisPass == PASS ? "OK" : "NG");
  3727. }
  3728. #endif
  3729. #ifdef FUNC_AUTO_MODIFY_CCID_MODULE
  3730. uint8_t AutoModifyCCID(void)
  3731. {
  3732. uint8_t old = Charger.CCID_Module_Type;
  3733. if (Test_LeakModule() == PASS)
  3734. {
  3735. Charger.m_bCCID_MustModify = HTK_FALSE;
  3736. Charger.m_CCID_ModuleTestOK = Charger.CCID_Module_Type;
  3737. return PASS;
  3738. }
  3739. else
  3740. {
  3741. Charger.CCID_Module_Type = (Charger.CCID_Module_Type == CCID_MODULE_CORMEX ? CCID_MODULE_VAC : CCID_MODULE_CORMEX);
  3742. XP("#AutoModifyCCID: %s\r\n", CCID_MODULE_TYPE_STR);
  3743. if (Test_LeakModule() == PASS)
  3744. {
  3745. Charger.m_bCCID_MustModify = HTK_TRUE;
  3746. Charger.m_CCID_ModuleTestOK = Charger.CCID_Module_Type;
  3747. return PASS;
  3748. }
  3749. else
  3750. {
  3751. Charger.m_bCCID_MustModify = HTK_FALSE;
  3752. Charger.m_CCID_ModuleTestOK = CCID_MODULE_UNKNOW;
  3753. Charger.CCID_Module_Type = old;
  3754. return FAIL;
  3755. }
  3756. }
  3757. }
  3758. #endif
  3759. /**
  3760. * @brief Function implementing the cpTask thread.
  3761. * @param argument: Not used
  3762. * @retval None
  3763. */
  3764. /* USER CODE END Header_StartCpTask */
  3765. void StartCpTask(void const * argument)
  3766. {
  3767. /* USER CODE BEGIN StartCpTask */
  3768. //ccp
  3769. uint32_t duration_delta;
  3770. Charger.Mode = MODE_INITIAL;
  3771. Charger.Relay_Action=GPIO_RELAY_ACTION_OFF;
  3772. setLedMotion(LED_ACTION_INIT);
  3773. //Check MCU control mode depend on GPIO state
  3774. #ifdef FUNC_FORCE_RUN_CSU_MODE_WITH_DC_MODELNAME
  3775. Update_McuCtrlMode();
  3776. #else
  3777. if(HAL_GPIO_ReadPin(IN_ACT_REQ_GPIO_Port, IN_ACT_REQ_Pin) == GPIO_PIN_RESET)
  3778. {
  3779. Charger.memory.EVSE_Config.data.item.MCU_Control_Mode = MCU_CONTROL_MODE_CSU;
  3780. DEBUG_INFO(" Check MCU_Control_Mode is CSU in CP task\r\n");
  3781. }
  3782. else
  3783. {
  3784. Charger.memory.EVSE_Config.data.item.MCU_Control_Mode = MCU_CONTROL_MODE_NO_CSU;
  3785. DEBUG_INFO(" MCU_Control_Mode is NO NO NO CSU in CP task\r\n");
  3786. }
  3787. #endif
  3788. //Charger.ModelReadisOK = FAIL ;
  3789. /* Infinite loop */
  3790. for(;;)
  3791. {
  3792. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU)
  3793. {
  3794. #ifdef MODIFY_CPTASK_HEAD
  3795. Proc_CpTaskHead(HTK_FALSE);
  3796. #endif
  3797. // Cancel EV ready restart cp logic
  3798. if (Charger.Mode != MODE_HANDSHAKE)
  3799. {
  3800. timerDisable(TIMER_IDX_SIMU_DISCONNECT);
  3801. }
  3802. switch(Charger.Mode)
  3803. {
  3804. case MODE_INITIAL:
  3805. setLedMotion(LED_ACTION_INIT);
  3806. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  3807. HAL_GPIO_WritePin(OUT_Leak_Test_GPIO_Port, OUT_Leak_Test_Pin, GPIO_PIN_SET); // for VAC LeakModule use
  3808. if (Charger.ModelReadisOK ==PASS)
  3809. {
  3810. getRotarySwitchSetting( );
  3811. getGridTypeSwitchSetting( );
  3812. Charger.isTestLeakModule = ON ;
  3813. #ifdef FUNC_AUTO_MODIFY_CCID_MODULE
  3814. if (AutoModifyCCID() == PASS)
  3815. #else
  3816. if(Test_LeakModule() == PASS)
  3817. #endif
  3818. {
  3819. DEBUG_INFO("Leak module self test pass when initial.\r\n");
  3820. osDelay(1000);
  3821. if(Charger.isDebugEnable)
  3822. setChargerMode(MODE_DEBUG);
  3823. else
  3824. {
  3825. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  3826. // Cold load pick up check
  3827. if((Charger.CP_State == SYSTEM_STATE_A) && Charger.memory.coldLoadPickUp.data.item.isEnable)
  3828. {
  3829. recordChargingHis(OFF, END_STATUS_CODE_DROP);
  3830. Charger.memory.coldLoadPickUp.data.item.isEnable = OFF;
  3831. Charger.memory.coldLoadPickUp.op_bits.update = ON;
  3832. }
  3833. }
  3834. osDelay(5000) ; //wait hardware PE feedback data so delay some time
  3835. setChargerMode(MODE_IDLE);
  3836. }
  3837. }
  3838. else
  3839. {
  3840. Charger.Alarm_Code |= ALARM_LEAK_MODULE_FAIL;
  3841. DEBUG_INFO("Leak module self test fail when initial.\r\n");
  3842. Charger.counter.LEAK_MODULE.isLatch = ON ;
  3843. }
  3844. Charger.isTestLeakModule = OFF ;
  3845. }
  3846. break;
  3847. case MODE_IDLE:
  3848. if(isModeChange())
  3849. {
  3850. setLedMotion(LED_ACTION_IDLE);
  3851. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  3852. Charger.memory.EVSE_Config.data.item.MaxChargingCurrent = Charger.maxRatingCurrent;
  3853. #ifdef MODIFY_AC_EVSE_STATUS_MAX_CHARGING_CURRENT_VARIABLE
  3854. Charger.AC_MaxChargingCurrentOrDuty = Charger.maxRatingCurrent;
  3855. #endif
  3856. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  3857. Charger.isSetStartTime = ON ;
  3858. }
  3859. if (Charger.memory.EVSE_Config.data.item.AuthMode == AUTH_MODE_OCPP)
  3860. {
  3861. if( Charger.memory.coldLoadPickUp.data.item.isEnable || Charger.rfid.op_bits.reqStart)
  3862. {
  3863. osDelay(LED_RFID_TIME); // for Buzzer and RFID /Fail LED use
  3864. setLedMotion(LED_ACTION_AUTHED);
  3865. // Check start method type
  3866. memset(&Charger.cycle_info.userId[0], 0x00, ARRAY_SIZE(Charger.cycle_info.userId));
  3867. if(Charger.rfid.op_bits.reqStart)
  3868. {
  3869. Charger.cycle_info.StartType = START_METHOD_RFID;
  3870. memcpy(&Charger.cycle_info.userId[0], &Charger.rfid.currentCard[0], ARRAY_SIZE(Charger.rfid.currentCard));
  3871. Charger.istoHandShakeMode = ON ;
  3872. }
  3873. Charger.rfid.op_bits.reqStart = OFF;
  3874. if(Charger.memory.coldLoadPickUp.data.item.isEnable)
  3875. {
  3876. Charger.memory.coldLoadPickUp.data.item.isEnable = OFF;
  3877. Charger.memory.coldLoadPickUp.op_bits.update = ON;
  3878. DEBUG_INFO("coldLoadPickUp ON to rand wait sec.\r\n");
  3879. #ifdef MODIFY_COLD_LOAD_PICKUP_DELAY
  3880. ColdLoadPickup_Delay("coldLoadPickUp ON to rand wait sec.");
  3881. #else
  3882. osDelay((rand()%175000)+5000);
  3883. //osDelay((rand()%5000)+5000);
  3884. #endif
  3885. Charger.istoHandShakeMode = ON ;
  3886. }
  3887. }
  3888. if (Charger.istoHandShakeMode == ON) //charger can to HandShakeMode
  3889. {
  3890. if ((Charger.CP_State == SYSTEM_STATE_B)||(Charger.CP_State == SYSTEM_STATE_C))
  3891. {
  3892. timerDisable(TIMER_IDX_CP);
  3893. Charger.istoHandShakeMode = OFF ;
  3894. setChargerMode(MODE_HANDSHAKE);
  3895. if (Charger.memory.EVSE_Config.data.item.ModelName[3] == 'E')
  3896. timerEnable(TIMER_IDX_SIMU_DISCONNECT, 30000);
  3897. }
  3898. else
  3899. {
  3900. timerEnable(TIMER_IDX_CP, 180000);
  3901. }
  3902. }
  3903. }
  3904. else if (Charger.memory.EVSE_Config.data.item.AuthMode == AUTH_MODE_FREE)
  3905. {
  3906. if((Charger.CP_State == SYSTEM_STATE_B) ||
  3907. (Charger.CP_State == SYSTEM_STATE_C) ||
  3908. Charger.memory.coldLoadPickUp.data.item.isEnable )
  3909. {
  3910. setLedMotion(LED_ACTION_AUTHED);
  3911. // Check start method type
  3912. memset(&Charger.cycle_info.userId[0], 0x00, ARRAY_SIZE(Charger.cycle_info.userId));
  3913. if(Charger.rfid.op_bits.reqStart)
  3914. {
  3915. Charger.cycle_info.StartType = START_METHOD_RFID;
  3916. memcpy(&Charger.cycle_info.userId[0], &Charger.rfid.currentCard[0], ARRAY_SIZE(Charger.rfid.currentCard));
  3917. }
  3918. else
  3919. Charger.cycle_info.StartType = START_METHOD_CP;
  3920. Charger.ble.isRequestOn = OFF;
  3921. Charger.rfid.op_bits.reqStart = OFF;
  3922. if(Charger.memory.coldLoadPickUp.data.item.isEnable)
  3923. {
  3924. Charger.memory.coldLoadPickUp.data.item.isEnable = OFF;
  3925. Charger.memory.coldLoadPickUp.op_bits.update = ON;
  3926. DEBUG_INFO("coldLoadPickUp ON to rand wait sec.\r\n");
  3927. #ifdef MODIFY_COLD_LOAD_PICKUP_DELAY
  3928. ColdLoadPickup_Delay("coldLoadPickUp ON to rand wait sec.");
  3929. #else
  3930. osDelay((rand()%175000)+5000);
  3931. #endif
  3932. }
  3933. setChargerMode(MODE_HANDSHAKE);
  3934. if (Charger.memory.EVSE_Config.data.item.ModelName[3] == 'E')
  3935. timerEnable(TIMER_IDX_SIMU_DISCONNECT, 30000);
  3936. }
  3937. }
  3938. break;
  3939. case MODE_HANDSHAKE:
  3940. if(isModeChange())
  3941. {
  3942. Charger.isCP_in_B = OFF ;
  3943. Charger.isTestLeakModule = ON ;
  3944. #ifdef TRIM_CCID_SELFTEST
  3945. Proc_CCID_SelfTest();
  3946. #endif
  3947. //check Leak test pass
  3948. if (Charger.Test_LeakModuleisPass)
  3949. {
  3950. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  3951. CpTask_CtrlCpPwm_P0();
  3952. #endif
  3953. DEBUG_INFO("Leak module self test pass before charging.\r\n");
  3954. }
  3955. else
  3956. {
  3957. Charger.Alarm_Code |= ALARM_LEAK_MODULE_FAIL;
  3958. DEBUG_INFO("Leak module self test fail before charging.\r\n");
  3959. Charger.counter.LEAK_MODULE.retry ++ ;
  3960. if (Charger.counter.LEAK_MODULE.retry >=2)
  3961. {
  3962. Charger.counter.LEAK_MODULE.isLatch = ON ;
  3963. }
  3964. }
  3965. osDelay(300);
  3966. Charger.isTestLeakModule = OFF ;
  3967. }
  3968. // to chaging moder
  3969. if(Charger.CP_State == SYSTEM_STATE_C)
  3970. {
  3971. setChargerMode(MODE_CHARGING);
  3972. if (Charger.isSetStartTime == ON)
  3973. {
  3974. Charger.cycle_info.startTimeTick = HAL_GetTick();
  3975. Charger.cycle_info.startDateTime.year = Charger.memory.EVSE_Config.data.item.SystemDateTime.year;
  3976. Charger.cycle_info.startDateTime.month = Charger.memory.EVSE_Config.data.item.SystemDateTime.month;
  3977. Charger.cycle_info.startDateTime.day = Charger.memory.EVSE_Config.data.item.SystemDateTime.day;
  3978. Charger.cycle_info.startDateTime.hour = Charger.memory.EVSE_Config.data.item.SystemDateTime.hour;
  3979. Charger.cycle_info.startDateTime.min = Charger.memory.EVSE_Config.data.item.SystemDateTime.min;
  3980. Charger.cycle_info.startDateTime.sec = Charger.memory.EVSE_Config.data.item.SystemDateTime.sec;
  3981. Charger.cycle_info.meterStart = Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative;
  3982. }
  3983. }
  3984. if(Charger.CP_State == SYSTEM_STATE_B)
  3985. {
  3986. Charger.isCP_in_B = ON ;
  3987. setLedMotion(LED_ACTION_CONNECTED);
  3988. }
  3989. //RFID remove gun
  3990. if ((Charger.cycle_info.StartType == START_METHOD_RFID) && (Charger.CP_State == SYSTEM_STATE_A) && (Charger.isCP_in_B == ON))
  3991. {
  3992. Charger.isCP_in_B = OFF ;
  3993. setChargerMode(MODE_IDLE);
  3994. }
  3995. //FREE remove gun
  3996. if ((Charger.cycle_info.StartType == START_METHOD_CP) && Charger.CP_State == SYSTEM_STATE_A)
  3997. {
  3998. setChargerMode(MODE_IDLE);
  3999. }
  4000. // EV ready restart cp logic
  4001. if (Charger.memory.EVSE_Config.data.item.ModelName[3] == 'E')
  4002. {
  4003. if(timer[TIMER_IDX_SIMU_DISCONNECT].isAlarm == ON)
  4004. {
  4005. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_ZERO);
  4006. osDelay(4000);
  4007. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  4008. osDelay(600);
  4009. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4010. CpTask_CtrlCpPwm_P0();
  4011. #endif
  4012. timerDisable(TIMER_IDX_SIMU_DISCONNECT);
  4013. }
  4014. }
  4015. break;
  4016. case MODE_CHARGING:
  4017. if(isModeChange())
  4018. {
  4019. #ifdef MODIFY_CPTASK_CSU_0_1_CHARGE_MODE
  4020. CpTask_CtrlCpPwm_P0();
  4021. #endif
  4022. setLedMotion(LED_ACTION_CHARGING);
  4023. }
  4024. if (Charger.memory.EVSE_Config.data.item.AuthMode == AUTH_MODE_OCPP)
  4025. {
  4026. #ifdef MODIFY_AW_CP_TASK_CSU_0_1_CHARGING_MODE_STOP_WITHOUT_STATE_C
  4027. if(Charger.CP_State!=SYSTEM_STATE_C || Charger.rfid.op_bits.reqStop )
  4028. #else
  4029. if(((Charger.CP_State!=SYSTEM_STATE_C)&&(Charger.CP_State!=SYSTEM_STATE_UNKNOWN)) || Charger.rfid.op_bits.reqStop )
  4030. #endif
  4031. {
  4032. setChargerMode(MODE_STOP);
  4033. }
  4034. else
  4035. {
  4036. Charger.Relay_Action = GPIO_RELAY_ACTION_ON;
  4037. duration_delta = (HAL_GetTick() - Charger.cycle_info.startTimeTick);
  4038. Charger.cycle_info.Duration = duration_delta;
  4039. Charger.cycle_info.Power_Consumption = Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative - Charger.cycle_info.meterStart;
  4040. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4041. CpTask_CtrlCpPwm_P1();
  4042. #endif
  4043. }
  4044. }
  4045. else if (Charger.memory.EVSE_Config.data.item.AuthMode == AUTH_MODE_FREE)
  4046. {
  4047. #ifdef MODIFY_AW_CP_TASK_CSU_0_1_CHARGING_MODE_STOP_WITHOUT_STATE_C
  4048. if(Charger.CP_State!=SYSTEM_STATE_C)
  4049. #else
  4050. if((Charger.CP_State != SYSTEM_STATE_C) && (Charger.CP_State != SYSTEM_STATE_UNKNOWN))
  4051. #endif
  4052. {
  4053. setChargerMode(MODE_STOP);
  4054. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4055. }
  4056. else
  4057. {
  4058. Charger.Relay_Action = GPIO_RELAY_ACTION_ON;
  4059. duration_delta = (HAL_GetTick() - Charger.cycle_info.startTimeTick);
  4060. Charger.cycle_info.Duration = duration_delta;
  4061. Charger.cycle_info.Power_Consumption = Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative - Charger.cycle_info.meterStart;
  4062. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4063. CpTask_CtrlCpPwm_P1();
  4064. #endif
  4065. }
  4066. }
  4067. break;
  4068. case MODE_STOP:
  4069. if(isModeChange())
  4070. {
  4071. if ((Charger.memory.EVSE_Config.data.item.AuthMode == AUTH_MODE_OCPP) && Charger.rfid.op_bits.reqStop)
  4072. {
  4073. osDelay(LED_RFID_TIME); // for Buzzer and RFID /Fail LED use
  4074. }
  4075. setLedMotion(LED_ACTION_STOP);
  4076. Charger.isCP_in_B = OFF ;
  4077. Charger.is_RFIDKeepCharger = ON ;
  4078. Charger.isSetStartTime = OFF ;
  4079. }
  4080. if (Charger.memory.EVSE_Config.data.item.AuthMode == AUTH_MODE_OCPP)
  4081. {
  4082. //GB
  4083. if(Charger.CP_State == SYSTEM_STATE_A)
  4084. {
  4085. Charger.cycle_info.endTimeTick = HAL_GetTick();
  4086. recordChargingHis(OFF, END_STATUS_CODE_NORMAL);
  4087. Charger.rfid.op_bits.reqStop = OFF;
  4088. setChargerMode(MODE_IDLE);
  4089. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4090. timerDisable(TIMER_IDX_RFID_RELAY_OFF);
  4091. Charger.isCP_in_B = OFF ;
  4092. Charger.is_RFIDKeepCharger = OFF ;
  4093. Charger.cycle_info.StartType = START_METHOD_NONE;
  4094. }
  4095. else
  4096. {
  4097. //rfid close PWM and relay
  4098. if ((Charger.rfid.op_bits.reqStop == ON)&&Charger.cycle_info.StartType == START_METHOD_RFID)
  4099. {
  4100. Charger.rfid.op_bits.reqStop = OFF;
  4101. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL); // close pwm
  4102. timerEnable( TIMER_IDX_RFID_RELAY_OFF, 3000); //is set wait 3 sec
  4103. Charger.is_RFIDKeepCharger = OFF ;
  4104. }
  4105. if (timer[TIMER_IDX_RFID_RELAY_OFF].isAlarm == ON) //is wait 3 sec
  4106. {
  4107. timerDisable(TIMER_IDX_RFID_RELAY_OFF);
  4108. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4109. setChargerMode(MODE_IDLE);
  4110. Charger.cycle_info.StartType = START_METHOD_NONE;
  4111. }
  4112. //cp close or open PWM
  4113. if((Charger.CP_State == SYSTEM_STATE_B))
  4114. {
  4115. timerDisable(TIMER_IDX_RFID_RELAY_OFF);
  4116. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4117. Charger.isCP_in_B = ON ;
  4118. }
  4119. else if (Charger.CP_State == SYSTEM_STATE_C )
  4120. {
  4121. if ( Charger.isCP_in_B == ON && Charger.is_RFIDKeepCharger == ON)
  4122. {
  4123. Charger.is_RFIDKeepCharger = OFF ;
  4124. Charger.isCP_in_B = OFF ;
  4125. Charger.isTestLeakModule = ON ;
  4126. #ifdef TRIM_CCID_SELFTEST
  4127. Proc_CCID_SelfTest();
  4128. #endif
  4129. //check Leak test pass
  4130. if (Charger.Test_LeakModuleisPass)
  4131. {
  4132. timerEnable(TIMER_IDX_CP, 180000);
  4133. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4134. CpTask_CtrlCpPwm_P0();
  4135. #endif
  4136. setChargerMode(MODE_CHARGING);
  4137. DEBUG_INFO("Leak module self test pass before charging.\r\n");
  4138. }
  4139. else
  4140. {
  4141. Charger.Alarm_Code |= ALARM_LEAK_MODULE_FAIL;
  4142. DEBUG_INFO("Leak module self test fail before charging.\r\n");
  4143. Charger.counter.LEAK_MODULE.retry ++ ;
  4144. if (Charger.counter.LEAK_MODULE.retry >=2)
  4145. {
  4146. Charger.counter.LEAK_MODULE.isLatch = ON ;
  4147. }
  4148. }
  4149. osDelay(300);
  4150. Charger.isTestLeakModule = OFF ;
  4151. }
  4152. }
  4153. }
  4154. }
  4155. else if (Charger.memory.EVSE_Config.data.item.AuthMode == AUTH_MODE_FREE)
  4156. {
  4157. if (Charger.CP_State == SYSTEM_STATE_A)
  4158. {
  4159. Charger.cycle_info.endTimeTick = HAL_GetTick();
  4160. recordChargingHis(OFF, END_STATUS_CODE_NORMAL);
  4161. Charger.ble.isRequestOff = OFF;
  4162. Charger.rfid.op_bits.reqStop = OFF;
  4163. setChargerMode(MODE_IDLE);
  4164. }
  4165. else
  4166. {
  4167. if (Charger.CP_State == SYSTEM_STATE_B)
  4168. {
  4169. Charger.isCP_in_B = ON ;
  4170. }
  4171. if((Charger.CP_State == SYSTEM_STATE_C))
  4172. {
  4173. setChargerMode(MODE_CHARGING);
  4174. }
  4175. }
  4176. }
  4177. break;
  4178. case MODE_ALARM:
  4179. if(isModeChange())
  4180. {
  4181. Charger.isSetStartTime = OFF ;
  4182. //clear blink time , Prevent red light from being snatched by others
  4183. disblinkerTime (BLINKER_IDX_LED) ;
  4184. }
  4185. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4186. //Set alarm led
  4187. if ((Charger.Alarm_Code != Charger.Alarm_Code_before) && (blinker[BLINKER_IDX_LED].blinkisFinish))
  4188. {
  4189. setLedMotion(LED_ACTION_ALARM);
  4190. Charger.Alarm_Code_before = Charger.Alarm_Code ;
  4191. }
  4192. if(Charger.Alarm_Code == 0x00)
  4193. {
  4194. Charger.Alarm_Code_before = Charger.Alarm_Code ;
  4195. if (Charger.Mode_Before_Alarm == MODE_CHARGING) // in charging mode
  4196. {
  4197. if(Charger.CP_State == SYSTEM_STATE_A) // to idle
  4198. {
  4199. Charger.cycle_info.endTimeTick = HAL_GetTick();
  4200. recordChargingHis(OFF, END_STATUS_CODE_NORMAL);
  4201. Charger.rfid.op_bits.reqStop = OFF;
  4202. Charger.ble.isRequestOff = OFF;
  4203. setChargerMode(MODE_IDLE);
  4204. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4205. timerDisable(TIMER_IDX_RFID_RELAY_OFF);
  4206. Charger.isCP_in_B = OFF ;
  4207. Charger.is_RFIDKeepCharger = OFF ;
  4208. Charger.cycle_info.StartType = START_METHOD_NONE;
  4209. }
  4210. else // to handshake
  4211. {
  4212. setChargerMode(MODE_HANDSHAKE);
  4213. Charger.isCP_in_B = ON ;
  4214. setLedMotion(LED_ACTION_CONNECTED);
  4215. }
  4216. }
  4217. else if(Charger.Mode_Before_Alarm == MODE_STOP) // in stop mode
  4218. {
  4219. if(Charger.CP_State == SYSTEM_STATE_A) // to idle
  4220. {
  4221. Charger.cycle_info.endTimeTick = HAL_GetTick();
  4222. recordChargingHis(OFF, END_STATUS_CODE_NORMAL);
  4223. Charger.rfid.op_bits.reqStop = OFF;
  4224. setChargerMode(MODE_IDLE);
  4225. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4226. timerDisable(TIMER_IDX_RFID_RELAY_OFF);
  4227. Charger.isCP_in_B = OFF ;
  4228. Charger.is_RFIDKeepCharger = OFF ;
  4229. Charger.cycle_info.StartType = START_METHOD_NONE;
  4230. }
  4231. else if ((Charger.CP_State == SYSTEM_STATE_B) && (Charger.memory.EVSE_Config.data.item.AuthMode == AUTH_MODE_OCPP)) // OCPP
  4232. {
  4233. if ( ( ((Charger.cycle_info.StartType == START_METHOD_BLE) && (Charger.is_BLEKeepCharger == ON))|| // to handshake
  4234. ((Charger.cycle_info.StartType == START_METHOD_RFID) && (Charger.is_RFIDKeepCharger == ON)) ) &&
  4235. ( Charger.isCP_in_B == ON ) )
  4236. {
  4237. setChargerMode(MODE_HANDSHAKE);
  4238. setLedMotion(LED_ACTION_CONNECTED);
  4239. }
  4240. else //to stop
  4241. {
  4242. setChargerMode(MODE_STOP);
  4243. }
  4244. }
  4245. else if ((Charger.CP_State == SYSTEM_STATE_B) && (Charger.memory.EVSE_Config.data.item.AuthMode == AUTH_MODE_FREE)) //Free
  4246. {
  4247. if ( Charger.isCP_in_B == ON ) // to handshake
  4248. {
  4249. setChargerMode(MODE_HANDSHAKE);
  4250. setLedMotion(LED_ACTION_CONNECTED);
  4251. }
  4252. else //to stop
  4253. {
  4254. setChargerMode(MODE_STOP);
  4255. }
  4256. }
  4257. else //to stop
  4258. {
  4259. setChargerMode(MODE_STOP);
  4260. }
  4261. }
  4262. else // idle
  4263. {
  4264. if(timer[TIMER_IDX_STATE_E].isAlarm == ON)
  4265. {
  4266. setChargerMode(MODE_IDLE);
  4267. }
  4268. }
  4269. }
  4270. break;
  4271. case MODE_MAINTAIN:
  4272. if(isModeChange())
  4273. {}
  4274. break;
  4275. case MODE_DEBUG:
  4276. if(isModeChange())
  4277. {
  4278. setLedMotion(LED_ACTION_DEBUG);
  4279. Charger.Relay_Action = GPIO_RELAY_ACTION_ON;
  4280. timerEnable(TIMER_IDX_DEBUG, 1000);
  4281. }
  4282. if(timer[TIMER_IDX_DEBUG].isAlarm || (Charger.Alarm_Code>0))
  4283. {
  4284. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4285. Charger.isDebugModeCPtest = ON;
  4286. }
  4287. if(Charger.isDebugModeCPtest)
  4288. {
  4289. if(Charger.CP_State == SYSTEM_STATE_A)
  4290. {
  4291. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4292. Charger.memory.EVSE_Config.data.item.MaxChargingCurrent = Charger.maxRatingCurrent;
  4293. #ifdef MODIFY_AC_EVSE_STATUS_MAX_CHARGING_CURRENT_VARIABLE
  4294. Charger.AC_MaxChargingCurrentOrDuty = Charger.maxRatingCurrent;
  4295. #endif
  4296. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  4297. }
  4298. else if (Charger.CP_State == SYSTEM_STATE_B)
  4299. {
  4300. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4301. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4302. CpTask_CtrlCpPwm_P1();
  4303. #endif
  4304. }
  4305. else if (Charger.CP_State == SYSTEM_STATE_C)
  4306. {
  4307. Charger.Relay_Action = GPIO_RELAY_ACTION_ON;
  4308. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4309. CpTask_CtrlCpPwm_P1();
  4310. #endif
  4311. }
  4312. }
  4313. break;
  4314. }
  4315. }
  4316. else if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_CSU) //CP_CSU
  4317. {
  4318. /*
  4319. MCU control mdoe with CSU
  4320. */
  4321. #ifdef MODIFY_CPTASK_HEAD
  4322. Proc_CpTaskHead(HTK_TRUE);
  4323. #endif
  4324. // Cancel EV ready restart cp logic
  4325. if (Charger.Mode != MODE_HANDSHAKE)
  4326. {
  4327. timerDisable(TIMER_IDX_SIMU_DISCONNECT);
  4328. }
  4329. switch(Charger.Mode)
  4330. {
  4331. case MODE_INITIAL:
  4332. setLedMotion(LED_ACTION_INIT);
  4333. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  4334. HAL_GPIO_WritePin(OUT_Leak_Test_GPIO_Port, OUT_Leak_Test_Pin, GPIO_PIN_SET); // for VAC LeakModule use
  4335. #ifdef FUNC_RELAY_OFF_WHEN_CSU_CMD_TIMEOUT
  4336. Charger.m_CSU_RxTick = HAL_GetTick();
  4337. #endif
  4338. if (Charger.ModelReadisOK ==PASS)
  4339. {
  4340. getRotarySwitchSetting( );
  4341. getGridTypeSwitchSetting( );
  4342. Charger.isTestLeakModule = ON ;
  4343. #ifdef FUNC_AUTO_MODIFY_CCID_MODULE
  4344. if (AutoModifyCCID() == PASS)
  4345. #else
  4346. if(Test_LeakModule() == PASS)
  4347. #endif
  4348. {
  4349. DEBUG_INFO("Leak module self test pass when initial.\r\n");
  4350. osDelay(1000);
  4351. if(Charger.isDebugEnable)
  4352. setChargerMode(MODE_DEBUG);
  4353. else
  4354. {
  4355. setChargerMode(MODE_IDLE);
  4356. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  4357. // Cold load pick up check
  4358. if((Charger.CP_State == SYSTEM_STATE_A) && Charger.memory.coldLoadPickUp.data.item.isEnable)
  4359. {
  4360. recordChargingHis(OFF, END_STATUS_CODE_DROP);
  4361. Charger.memory.coldLoadPickUp.data.item.isEnable = OFF;
  4362. Charger.memory.coldLoadPickUp.op_bits.update = ON;
  4363. }
  4364. }
  4365. }
  4366. }
  4367. else
  4368. {
  4369. Charger.Alarm_Code |= ALARM_LEAK_MODULE_FAIL;
  4370. DEBUG_INFO("Leak module self test fail when initial.\r\n");
  4371. Charger.counter.LEAK_MODULE.isLatch = ON ;
  4372. }
  4373. Charger.isTestLeakModule = OFF ;
  4374. }
  4375. break;
  4376. case MODE_IDLE:
  4377. if(isModeChange())
  4378. {
  4379. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4380. Charger.memory.EVSE_Config.data.item.MaxChargingCurrent = Charger.maxRatingCurrent;
  4381. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  4382. Charger.isSetStartTime = ON ;
  4383. #ifdef FUNC_BLOCK_CSU_CONFIG_CP_PWM_DUTY
  4384. Charger.m_bBlockCsuCpPwmDuty = HTK_DISABLE;
  4385. #endif
  4386. }
  4387. if(Charger.am3352.isRequestOn &&
  4388. (Charger.CP_State != SYSTEM_STATE_A))
  4389. {
  4390. setChargerMode(MODE_HANDSHAKE);
  4391. if (Charger.memory.EVSE_Config.data.item.ModelName[3] == 'E')
  4392. timerEnable(TIMER_IDX_SIMU_DISCONNECT, 30000);
  4393. }
  4394. break;
  4395. case MODE_HANDSHAKE:
  4396. if(isModeChange())
  4397. {
  4398. Charger.isCP_in_B = OFF ;
  4399. Charger.isTestLeakModule = ON ;
  4400. #ifdef TRIM_CCID_SELFTEST
  4401. Proc_CCID_SelfTest();
  4402. #endif
  4403. //check Leak test pass
  4404. if (Charger.Test_LeakModuleisPass)
  4405. {
  4406. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4407. #ifdef MODIFY_MODE_HANDSHAKE_STOP_PWM_WITHOUT_OK_STATE
  4408. if (Charger.m_bSafetyRegulationGB)
  4409. {
  4410. if(Charger.CP_State == SYSTEM_STATE_B || Charger.CP_State == SYSTEM_STATE_C)
  4411. {
  4412. CpTask_CtrlCpPwm_P0();
  4413. }
  4414. }
  4415. else
  4416. {
  4417. CpTask_CtrlCpPwm_P0();
  4418. }
  4419. #endif //MODIFY_MODE_HANDSHAKE_STOP_PWM_WITHOUT_OK_STATE
  4420. #else //MODIFY_CP_TASK_CTRL_CP_PWM
  4421. CpTask_CtrlCpPwm_P0();
  4422. #endif //MODIFY_CP_TASK_CTRL_CP_PWM
  4423. osDelay(300);
  4424. DEBUG_INFO("Leak module self test pass before charging.\r\n");
  4425. }
  4426. else
  4427. {
  4428. Charger.Alarm_Code |= ALARM_LEAK_MODULE_FAIL;
  4429. DEBUG_INFO("Leak module self test fail before charging.\r\n");
  4430. }
  4431. Charger.isTestLeakModule = OFF ;
  4432. }
  4433. if(Charger.CP_State == SYSTEM_STATE_C)
  4434. {
  4435. #ifdef MODIFY_MODE_HANDSHAKE_STOP_PWM_WITHOUT_OK_STATE
  4436. if (Charger.m_bSafetyRegulationGB)
  4437. {
  4438. Charger.m_bBlockCsuCpPwmDuty = HTK_DISABLE;
  4439. if (Charger.m_CP_CurPWM == PWM_DUTY_FULL)
  4440. {
  4441. CpTask_CtrlCpPwm_P0();
  4442. }
  4443. }
  4444. #endif
  4445. setChargerMode(MODE_CHARGING);
  4446. timerDisable(TIMER_IDX_CP);
  4447. if (Charger.isSetStartTime == ON)
  4448. {
  4449. Charger.cycle_info.startTimeTick = HAL_GetTick();
  4450. Charger.cycle_info.startDateTime.year = Charger.memory.EVSE_Config.data.item.SystemDateTime.year;
  4451. Charger.cycle_info.startDateTime.month = Charger.memory.EVSE_Config.data.item.SystemDateTime.month;
  4452. Charger.cycle_info.startDateTime.day = Charger.memory.EVSE_Config.data.item.SystemDateTime.day;
  4453. Charger.cycle_info.startDateTime.hour = Charger.memory.EVSE_Config.data.item.SystemDateTime.hour;
  4454. Charger.cycle_info.startDateTime.min = Charger.memory.EVSE_Config.data.item.SystemDateTime.min;
  4455. Charger.cycle_info.startDateTime.sec = Charger.memory.EVSE_Config.data.item.SystemDateTime.sec;
  4456. Charger.cycle_info.meterStart = Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative;
  4457. }
  4458. }
  4459. if(Charger.CP_State == SYSTEM_STATE_B)
  4460. {
  4461. #ifdef MODIFY_MODE_HANDSHAKE_STOP_PWM_WITHOUT_OK_STATE
  4462. if (Charger.m_bSafetyRegulationGB)
  4463. {
  4464. Charger.m_bBlockCsuCpPwmDuty = HTK_DISABLE;
  4465. if (Charger.m_CP_CurPWM == PWM_DUTY_FULL)
  4466. {
  4467. CpTask_CtrlCpPwm_P0();
  4468. }
  4469. }
  4470. #endif
  4471. Charger.isCP_in_B = ON ;
  4472. }
  4473. if(((Charger.CP_State == SYSTEM_STATE_A) && Charger.isCP_in_B) || !Charger.am3352.isRequestOn)
  4474. {
  4475. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL); // close pwm
  4476. setChargerMode(MODE_IDLE);
  4477. timerDisable(TIMER_IDX_CP);
  4478. }
  4479. // EV ready restart cp logic
  4480. if (Charger.memory.EVSE_Config.data.item.ModelName[3] == 'E')
  4481. {
  4482. if(timer[TIMER_IDX_SIMU_DISCONNECT].isAlarm == ON)
  4483. {
  4484. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_ZERO);
  4485. osDelay(4000);
  4486. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  4487. osDelay(600);
  4488. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4489. CpTask_CtrlCpPwm_P0();
  4490. #endif
  4491. timerDisable(TIMER_IDX_SIMU_DISCONNECT);
  4492. timer[TIMER_IDX_SIMU_DISCONNECT].isAlarm = OFF ;
  4493. }
  4494. }
  4495. #ifdef MODIFY_MODE_HANDSHAKE_STOP_PWM_WITHOUT_OK_STATE
  4496. if (Charger.m_bSafetyRegulationGB)
  4497. {
  4498. if (
  4499. (Charger.CP_State == SYSTEM_STATE_D || Charger.CP_State == SYSTEM_STATE_E) &&
  4500. (Charger.am3352.isRequestOn)
  4501. )
  4502. {
  4503. Charger.m_bBlockCsuCpPwmDuty = HTK_ENABLE;
  4504. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  4505. }
  4506. }
  4507. #endif
  4508. break;
  4509. case MODE_CHARGING:
  4510. if(isModeChange())
  4511. {
  4512. #ifdef MODIFY_CPTASK_CSU_0_1_CHARGE_MODE
  4513. CpTask_CtrlCpPwm_P0();
  4514. #endif
  4515. }
  4516. #ifdef MODIFY_AW_CP_TASK_CSU_0_1_CHARGING_MODE_STOP_WITHOUT_STATE_C
  4517. if(Charger.CP_State != SYSTEM_STATE_C || !Charger.am3352.isRequestOn)
  4518. #else
  4519. if(!Charger.am3352.isRequestOn)
  4520. #endif
  4521. {
  4522. setChargerMode(MODE_STOP);
  4523. if(Charger.CP_State != SYSTEM_STATE_C)
  4524. {
  4525. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4526. #ifdef MODIFY_MODE_CHARGING_STOP_PWM_WITHOUT_OK_STATE
  4527. if (Charger.m_bSafetyRegulationGB)
  4528. {
  4529. if (
  4530. (Charger.CP_State == SYSTEM_STATE_D || Charger.CP_State == SYSTEM_STATE_E) &&
  4531. (Charger.am3352.isRequestOn)
  4532. )
  4533. {
  4534. Charger.m_bBlockCsuCpPwmDuty = HTK_ENABLE;
  4535. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  4536. }
  4537. }
  4538. #endif //MODIFY_MODE_CHARGING_STOP_PWM_WITHOUT_OK_STATE
  4539. }
  4540. }
  4541. #ifdef FUNC_RELAY_OFF_WHEN_CSU_CMD_TIMEOUT
  4542. else if (HTK_IsTimeout(Charger.m_CSU_RxTick, CSU_CMD_TIMEOUT_SEC * 1000))
  4543. {
  4544. if (Charger.Relay_Action != GPIO_RELAY_ACTION_OFF)
  4545. {
  4546. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4547. XP("#<CSU_TIMEOUT> RELAY => OFF\r\n");
  4548. }
  4549. #ifdef FUNC_REQUEST_OFF_WHEN_CSU_CMD_TIMEOUT
  4550. if (Charger.am3352.isRequestOn != OFF)
  4551. {
  4552. Charger.am3352.isRequestOn = OFF;
  4553. XP("#<CSU_TIMEOUT> REQUEST => OFF\r\n");
  4554. }
  4555. #endif
  4556. }
  4557. #endif //FUNC_RELAY_OFF_WHEN_CSU_CMD_TIMEOUT
  4558. else
  4559. {
  4560. #ifdef USE_OLD_ONE_STEP_CHARGE_FOR_AW_NOODOE
  4561. Charger.Relay_Action = GPIO_RELAY_ACTION_ON;
  4562. #endif
  4563. duration_delta = (HAL_GetTick() - Charger.cycle_info.startTimeTick);
  4564. Charger.cycle_info.Duration = duration_delta;
  4565. Charger.cycle_info.Power_Consumption = Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative - Charger.cycle_info.meterStart;
  4566. }
  4567. break;
  4568. case MODE_STOP:
  4569. if(isModeChange())
  4570. {
  4571. Charger.isSetStartTime = OFF ;
  4572. Charger.isCP_in_B = OFF ;
  4573. if (!Charger.am3352.isRequestOn) //represent swip card stop charge in 6V
  4574. {
  4575. Charger.rfid.op_bits.reqStop = ON ;
  4576. }
  4577. }
  4578. if((Charger.CP_State == SYSTEM_STATE_A) || !Charger.am3352.isRequestOn)
  4579. {
  4580. if( !Charger.am3352.isRequestOn)
  4581. {
  4582. 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
  4583. {
  4584. if(Charger.CP_State == SYSTEM_STATE_B)
  4585. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4586. Charger.rfid.op_bits.reqStop = OFF;
  4587. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL); // close pwm
  4588. timerEnable( TIMER_IDX_RFID_RELAY_OFF, 3000); //is set wait 3 sec
  4589. Charger.cycle_info.endTimeTick = HAL_GetTick();
  4590. recordChargingHis(OFF, END_STATUS_CODE_NORMAL);
  4591. Charger.isCP_in_B = OFF ;
  4592. }
  4593. if (timer[TIMER_IDX_RFID_RELAY_OFF].isAlarm == ON ) //is wait 3 sec
  4594. {
  4595. timerDisable(TIMER_IDX_RFID_RELAY_OFF);
  4596. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4597. #ifdef FUNC_BLOCK_CSU_CONFIG_CP_PWM_DUTY
  4598. Charger.m_bBlockCsuCpPwmDuty = HTK_DISABLE;
  4599. #endif
  4600. setChargerMode(MODE_IDLE);
  4601. Charger.cycle_info.StartType = START_METHOD_NONE;
  4602. }
  4603. }
  4604. else //CP_State == SYSTEM_STATE_A
  4605. {
  4606. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL); // close pwm
  4607. Charger.cycle_info.endTimeTick = HAL_GetTick();
  4608. recordChargingHis(OFF, END_STATUS_CODE_NORMAL);
  4609. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4610. #ifdef FUNC_BLOCK_CSU_CONFIG_CP_PWM_DUTY
  4611. Charger.m_bBlockCsuCpPwmDuty = HTK_DISABLE;
  4612. #endif
  4613. setChargerMode(MODE_IDLE);
  4614. Charger.isCP_in_B = OFF ;
  4615. }
  4616. }
  4617. else // Charger.am3352.isRequestOn == 1
  4618. {
  4619. if((Charger.CP_State == SYSTEM_STATE_B))
  4620. {
  4621. timerDisable(TIMER_IDX_RFID_RELAY_OFF);
  4622. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4623. Charger.isCP_in_B = ON ;
  4624. }
  4625. if((Charger.CP_State == SYSTEM_STATE_C) && (!Charger.am3352.isRequestOff) )
  4626. {
  4627. #ifdef FUNC_BLOCK_CSU_CONFIG_CP_PWM_DUTY
  4628. Charger.m_bBlockCsuCpPwmDuty = HTK_DISABLE;
  4629. #endif
  4630. #ifdef MODIFY_MODE_STOP_TO_CHARGE_PROCESS
  4631. setChargerMode(MODE_CHARGING);
  4632. #else
  4633. setChargerMode(MODE_HANDSHAKE);
  4634. #endif
  4635. }
  4636. #ifdef MODIFY_MODE_STOP_STOP_PWM_WITHOUT_OK_STATE
  4637. if (Charger.m_bSafetyRegulationGB)
  4638. {
  4639. if (
  4640. (Charger.CP_State == SYSTEM_STATE_D || Charger.CP_State == SYSTEM_STATE_E) &&
  4641. (Charger.am3352.isRequestOn)
  4642. )
  4643. {
  4644. Charger.m_bBlockCsuCpPwmDuty = HTK_ENABLE;
  4645. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  4646. }
  4647. if ((Charger.CP_State == SYSTEM_STATE_B) && (Charger.am3352.isRequestOn))
  4648. {
  4649. Charger.m_bBlockCsuCpPwmDuty = HTK_DISABLE;
  4650. if (Charger.m_CP_CurPWM == PWM_DUTY_FULL)
  4651. {
  4652. CpTask_CtrlCpPwm_P0();
  4653. }
  4654. }
  4655. }
  4656. #endif //MODIFY_MODE_STOP_STOP_PWM_WITHOUT_OK_STATE
  4657. }
  4658. break;
  4659. case MODE_ALARM:
  4660. if(isModeChange())
  4661. {
  4662. Charger.isSetStartTime = OFF ;
  4663. if (Charger.CSUisReady == ON )
  4664. {
  4665. //clear blink time , Prevent red light from being snatched by others
  4666. disblinkerTime (BLINKER_IDX_LED) ;
  4667. //clear led Action
  4668. Charger.am3352.LedActionState = 0 ;
  4669. }
  4670. }
  4671. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4672. if(Charger.Alarm_Code == 0x00)
  4673. {
  4674. Charger.am3352.LedActionState = 0 ;
  4675. if (Charger.Mode_Before_Alarm == MODE_CHARGING) // in charging mode
  4676. {
  4677. if(Charger.CP_State == SYSTEM_STATE_B ) // to handshake
  4678. {
  4679. setChargerMode(MODE_HANDSHAKE);
  4680. }
  4681. else // to idle
  4682. {
  4683. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  4684. setChargerMode(MODE_IDLE);
  4685. }
  4686. }
  4687. else if(Charger.Mode_Before_Alarm == MODE_STOP)
  4688. {
  4689. if (Charger.isCP_in_B == ON) // to handshake
  4690. {
  4691. setChargerMode(MODE_HANDSHAKE);
  4692. }
  4693. else // to stop
  4694. {
  4695. setChargerMode(MODE_STOP);
  4696. }
  4697. }
  4698. else // to idle
  4699. {
  4700. if(timer[TIMER_IDX_STATE_E].isAlarm == ON)
  4701. {
  4702. setChargerMode(MODE_IDLE);
  4703. }
  4704. }
  4705. }
  4706. break;
  4707. case MODE_DEBUG:
  4708. if(isModeChange())
  4709. {
  4710. setLedMotion(LED_ACTION_DEBUG);
  4711. Charger.Relay_Action = GPIO_RELAY_ACTION_ON;
  4712. timerEnable(TIMER_IDX_DEBUG, 1000);
  4713. }
  4714. if(timer[TIMER_IDX_DEBUG].isAlarm || (Charger.Alarm_Code>0))
  4715. {
  4716. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4717. Charger.isDebugModeCPtest = ON;
  4718. }
  4719. if(Charger.isDebugModeCPtest)
  4720. {
  4721. if(Charger.CP_State == SYSTEM_STATE_A)
  4722. {
  4723. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4724. Charger.memory.EVSE_Config.data.item.MaxChargingCurrent = Charger.maxRatingCurrent;
  4725. #ifdef MODIFY_AC_EVSE_STATUS_MAX_CHARGING_CURRENT_VARIABLE
  4726. Charger.AC_MaxChargingCurrentOrDuty = Charger.maxRatingCurrent;
  4727. #endif
  4728. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  4729. }
  4730. else if (Charger.CP_State == SYSTEM_STATE_B)
  4731. {
  4732. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4733. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4734. CpTask_CtrlCpPwm_P1();
  4735. #endif
  4736. }
  4737. else if (Charger.CP_State == SYSTEM_STATE_C)
  4738. {
  4739. Charger.Relay_Action = GPIO_RELAY_ACTION_ON;
  4740. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4741. CpTask_CtrlCpPwm_P1();
  4742. #endif
  4743. }
  4744. }
  4745. break;
  4746. default:
  4747. setChargerMode(MODE_IDLE);
  4748. break;
  4749. }
  4750. }
  4751. else
  4752. {
  4753. /*By Pass Mode*/
  4754. }
  4755. osDelay(1);
  4756. }
  4757. /* USER CODE END StartCpTask */
  4758. }
  4759. /* USER CODE BEGIN Header_StartAlarmTask */
  4760. /**
  4761. * @brief Function implementing the alarmTask thread.
  4762. * @param argument: Not used
  4763. * @retval None
  4764. */
  4765. /* USER CODE END Header_StartAlarmTask */
  4766. void StartAlarmTask(void const * argument)
  4767. {
  4768. /* USER CODE BEGIN StartAlarmTask */
  4769. //aalarm
  4770. timerEnable(TIMER_IDX_PE_DETECT, 30000);
  4771. /* Infinite loop */
  4772. for(;;)
  4773. {
  4774. if(Charger.Mode != MODE_INITIAL && Charger.Mode != MODE_DEBUG)
  4775. {
  4776. recordAlarmHis();
  4777. //================================
  4778. // CP fail alarm detect
  4779. //================================
  4780. #ifdef CP_ALARM_PROTECT
  4781. if ((Charger.CP_State == SYSTEM_STATE_UNKNOWN)&&(!(Charger.Alarm_Code & ALARM_MCU_TESTFAIL)))
  4782. {
  4783. if(Charger.counter.CP.fail >1000)
  4784. {
  4785. if(!(Charger.Alarm_Code & ALARM_CP_ERROR))
  4786. {
  4787. Charger.counter.CP.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  4788. Charger.Alarm_Code |= ALARM_CP_ERROR;
  4789. DEBUG_ERROR("Alarm CP occur.\r\n");
  4790. }
  4791. }
  4792. else
  4793. Charger.counter.CP.fail++;
  4794. }
  4795. #ifdef MODIFY_ALARMTASK_CP_ERROR_RECOVER
  4796. else if (Charger.CP_State != SYSTEM_STATE_UNKNOWN)
  4797. #else
  4798. else if ((Charger.CP_State != SYSTEM_STATE_UNKNOWN)&&(Charger.CP_State!=SYSTEM_STATE_D)&&
  4799. (Charger.CP_State != SYSTEM_STATE_E)&&(Charger.CP_State != SYSTEM_STATE_F))
  4800. #endif
  4801. {
  4802. Charger.counter.CP.fail = 0;
  4803. if((Charger.Alarm_Code & ALARM_CP_ERROR))
  4804. {
  4805. osDelay(1000);
  4806. Charger.counter.CP.retry++;
  4807. Charger.Alarm_Code &= ~ALARM_CP_ERROR;
  4808. DEBUG_INFO("Alarm CP recover.\r\n");
  4809. }
  4810. }
  4811. #endif //CP_ALARM_PROTECT
  4812. //================================
  4813. // Over voltage alarm detect
  4814. //================================
  4815. #ifdef OVP_PROTECT
  4816. if(Charger.Voltage[0] > ALARM_SPEC_OV)
  4817. {
  4818. if(Charger.counter.OV.fail > 1000)
  4819. {
  4820. Charger.counter.OV.restore = 0 ;
  4821. if(!(Charger.Alarm_Code & ALARM_OVER_VOLTAGE))
  4822. {
  4823. Charger.Alarm_Code |= ALARM_OVER_VOLTAGE;
  4824. DEBUG_ERROR("Alarm OV occur.\r\n");
  4825. }
  4826. }
  4827. else
  4828. Charger.counter.OV.fail++;
  4829. }
  4830. else if(Charger.Voltage[0] < (ALARM_SPEC_OV-ALARM_SPEC_OUV_HYSTERESIS))
  4831. {
  4832. if((Charger.Alarm_Code & ALARM_OVER_VOLTAGE))
  4833. {
  4834. if (Charger.counter.OV.restore > 1000 && blinker[BLINKER_IDX_LED].blinkisFinish)
  4835. {
  4836. Charger.counter.OV.fail = 0 ;
  4837. DEBUG_INFO("Alarm OV is coldpickup ing..... \r\n");
  4838. Charger.counter.OV.isOccurInCharging = ((Charger.CP_State == SYSTEM_STATE_B)?ON:OFF);
  4839. if (Charger.counter.OV.isOccurInCharging)
  4840. {
  4841. #ifdef MODIFY_COLD_LOAD_PICKUP_DELAY
  4842. ColdLoadPickup_Delay("Alarm OV is coldpickup ing..... ");
  4843. #else
  4844. osDelay((rand()%175000)+5000);
  4845. #endif
  4846. }
  4847. Charger.Alarm_Code &= ~ALARM_OVER_VOLTAGE;
  4848. DEBUG_INFO("Alarm OV recover.\r\n");
  4849. }
  4850. else
  4851. {
  4852. Charger.counter.OV.restore++;
  4853. }
  4854. }
  4855. else
  4856. {
  4857. Charger.counter.OV.fail = 0 ;
  4858. Charger.counter.OV.restore = 0 ;
  4859. }
  4860. }
  4861. #endif //OVP_PROTECT
  4862. //================================
  4863. // Under voltage alarm detect
  4864. //================================
  4865. #ifdef UVP_PROTECT
  4866. if(Charger.Voltage[0] < ALARM_SPEC_UV)
  4867. {
  4868. if(Charger.counter.UV.fail > 1000)
  4869. {
  4870. Charger.counter.UV.restore = 0 ;
  4871. if(!(Charger.Alarm_Code & ALARM_UNDER_VOLTAGE))
  4872. {
  4873. Charger.Alarm_Code |= ALARM_UNDER_VOLTAGE;
  4874. DEBUG_ERROR("Alarm UV occur.\r\n");
  4875. }
  4876. }
  4877. else
  4878. Charger.counter.UV.fail++;
  4879. }
  4880. else if(Charger.Voltage[0] > (ALARM_SPEC_UV+ALARM_SPEC_OUV_HYSTERESIS))
  4881. {
  4882. if(Charger.Alarm_Code & ALARM_UNDER_VOLTAGE)
  4883. {
  4884. if (Charger.counter.UV.restore > 1000 && blinker[BLINKER_IDX_LED].blinkisFinish)
  4885. {
  4886. Charger.counter.UV.fail = 0 ;
  4887. DEBUG_INFO("Alarm UV is coldpickup ing..... \r\n");
  4888. Charger.counter.UV.isOccurInCharging = ((Charger.CP_State == SYSTEM_STATE_B)?ON:OFF);
  4889. if (Charger.counter.UV.isOccurInCharging)
  4890. {
  4891. #ifdef MODIFY_COLD_LOAD_PICKUP_DELAY
  4892. ColdLoadPickup_Delay("Alarm UV is coldpickup ing..... ");
  4893. #else
  4894. osDelay((rand()%175000)+5000);
  4895. #endif
  4896. }
  4897. Charger.Alarm_Code &= ~ALARM_UNDER_VOLTAGE;
  4898. DEBUG_INFO("Alarm UV recover.\r\n");
  4899. }
  4900. else
  4901. {
  4902. Charger.counter.UV.restore++ ;
  4903. }
  4904. }
  4905. else
  4906. {
  4907. Charger.counter.UV.restore = 0 ;
  4908. Charger.counter.UV.fail = 0 ;
  4909. }
  4910. }
  4911. #endif //UVP_PROTECT
  4912. //================================
  4913. // Over current alarm detect
  4914. //================================
  4915. #ifdef OCP_PROTECT
  4916. #ifdef FUNC_OCP_WITH_PP
  4917. {
  4918. uint16_t CurMaxCurr = 0; //A
  4919. uint16_t OC_BegThreshold = 0; //Occur: 0.01A
  4920. uint16_t OC_EndThreshold = 0; //Recover: 0.01A
  4921. CurMaxCurr = HTK_GET_VAL_MIN(Charger.memory.EVSE_Config.data.item.MaxChargingCurrent, Charger.maxRatingCurrent);
  4922. //// if (Charger.m_bDetectPP)
  4923. //// {
  4924. //// CurMaxCurr = HTK_GET_VAL_MIN(CurMaxCurr, Charger.m_PPInfo.m_CurCurr);
  4925. //// }
  4926. #ifdef MODIFY_OCP_PROC_WITH_MODELNAME_IDX_3_EURO_SPEC
  4927. if (Charger.m_bModelNameWithEuroSpecOCP)
  4928. #else
  4929. if (Charger.memory.EVSE_Config.data.item.ModelName[3] == 'E')
  4930. #endif
  4931. {
  4932. OC_BegThreshold = (CurMaxCurr * Charger.OCP_Magnification) + 0;
  4933. OC_EndThreshold = (CurMaxCurr * Charger.OCP_Magnification) - 200;
  4934. }
  4935. else
  4936. {
  4937. if (CurMaxCurr > 20)
  4938. {
  4939. OC_BegThreshold = (CurMaxCurr * Charger.OCP_Magnification) + 0;
  4940. OC_EndThreshold = (CurMaxCurr * Charger.OCP_Magnification) - 200;
  4941. }
  4942. else
  4943. {
  4944. OC_BegThreshold = (CurMaxCurr * 100) + 200;
  4945. OC_EndThreshold = (CurMaxCurr * 100) + 0;
  4946. }
  4947. }
  4948. Charger.m_OCP_CurMaxCurr = CurMaxCurr;
  4949. Charger.m_OCP_BegThreshold = OC_BegThreshold;
  4950. Charger.m_OCP_EndThreshold = OC_EndThreshold;
  4951. //--------------------------------
  4952. // L1 Over current
  4953. if(Charger.Current[0] >= 4800)
  4954. {
  4955. if(Charger.counter.OC.fail > 5)
  4956. {
  4957. if(!(Charger.Alarm_Code & ALARM_CIRCUIT_SHORT))
  4958. {
  4959. Charger.counter.OC.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  4960. Charger.Alarm_Code |= ALARM_CIRCUIT_SHORT;
  4961. if (Charger.counter.OC.isLatch == OFF)
  4962. {
  4963. Charger.counter.OC.isLatch = ON ;
  4964. setLedMotion(LED_ACTION_ALARM);
  4965. timerDisable(TIMER_IDX_RETRY_OC);
  4966. }
  4967. DEBUG_INFO("Correction Current[0]: %f\r\n", Charger.Current[0]/100.0);
  4968. DEBUG_ERROR("Alarm SCP occur.\r\n");
  4969. }
  4970. }
  4971. else
  4972. Charger.counter.OC.fail++;
  4973. }
  4974. //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))
  4975. else if (Charger.Current[0] > OC_BegThreshold)
  4976. {
  4977. if(Charger.counter.OC.fail > 6000)
  4978. {
  4979. if(!(Charger.Alarm_Code & ALARM_OVER_CURRENT))
  4980. {
  4981. Charger.counter.OC.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  4982. Charger.Alarm_Code |= ALARM_OVER_CURRENT;
  4983. DEBUG_INFO("Correction Current[0]: %f\r\n", Charger.Current[0]/100.0);
  4984. DEBUG_ERROR("Alarm OC occur.\r\n");
  4985. timerEnable(TIMER_IDX_RETRY_OC, ALARM_RETRY_INTERVAL_OC);
  4986. Charger.counter.OC.retry++;
  4987. }
  4988. }
  4989. else
  4990. Charger.counter.OC.fail++;
  4991. }
  4992. //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)))
  4993. else if (Charger.Current[0] < OC_EndThreshold)
  4994. {
  4995. Charger.counter.OC.fail = 0;
  4996. if ((Charger.counter.OC.retry > ALARM_OC_RETRY_COUNT))
  4997. {
  4998. if (Charger.counter.OC.isLatch == OFF)
  4999. {
  5000. Charger.counter.OC.isLatch = ON ;
  5001. setLedMotion(LED_ACTION_ALARM);
  5002. timerDisable(TIMER_IDX_RETRY_OC);
  5003. }
  5004. }
  5005. else if((Charger.Alarm_Code & ALARM_OVER_CURRENT) &&
  5006. (Charger.counter.OC.retry <= ALARM_OC_RETRY_COUNT) &&
  5007. (timer[TIMER_IDX_RETRY_OC].isAlarm == ON) &&
  5008. blinker[BLINKER_IDX_LED].blinkisFinish )
  5009. {
  5010. timerDisable(TIMER_IDX_RETRY_OC);
  5011. osDelay(1000);
  5012. Charger.Alarm_Code &= ~ALARM_OVER_CURRENT;
  5013. Charger.Alarm_Code &= ~ALARM_CIRCUIT_SHORT;
  5014. DEBUG_INFO("Alarm OC recover.\r\n");
  5015. }
  5016. }
  5017. }
  5018. #else //FUNC_OCP_WITH_PP
  5019. //Old Procedure (Deleted)
  5020. #endif //FUNC_OCP_WITH_PP
  5021. #endif //OCP_PROTECT
  5022. //================================
  5023. // Over temperature alarm detect
  5024. //================================
  5025. #ifdef OTP_PROTECT
  5026. if (Charger.temperature.SystemAmbientTemp > ALARM_SPEC_OT_2)
  5027. {
  5028. if(Charger.counter.OT.fail > 2000)
  5029. {
  5030. if (Charger.counter.OT.isLatch == OFF)
  5031. {
  5032. Charger.counter.OT.isLatch = ON ;
  5033. setLedMotion(LED_ACTION_ALARM);
  5034. }
  5035. //Charger.counter.OT.isLatch = ON ;
  5036. if(!(Charger.Alarm_Code & ALARM_OVER_TEMPERATURE))
  5037. {
  5038. Charger.counter.OT.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5039. Charger.Alarm_Code |= ALARM_OVER_TEMPERATURE;
  5040. DEBUG_ERROR("Latch OT2 occur.\r\n");
  5041. DEBUG_ERROR("Tmp = %d.\r\n",Charger.temperature.SystemAmbientTemp);
  5042. }
  5043. }
  5044. else
  5045. Charger.counter.OT.fail++;
  5046. }
  5047. else if((Charger.temperature.SystemAmbientTemp > ALARM_SPEC_OT_1) && (!Charger.counter.OT.isLatch))
  5048. {
  5049. if(Charger.counter.OT.fail > 1000)
  5050. {
  5051. if(!(Charger.Alarm_Code & ALARM_OVER_TEMPERATURE))
  5052. {
  5053. Charger.counter.OT.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5054. Charger.Alarm_Code |= ALARM_OVER_TEMPERATURE;
  5055. DEBUG_ERROR("Alarm OT occur.\r\n");
  5056. }
  5057. }
  5058. else
  5059. Charger.counter.OT.fail++;
  5060. }
  5061. else if((Charger.temperature.SystemAmbientTemp < (ALARM_SPEC_OT_1-ALARM_SPEC_OT_HYSTERESIS)) && (!Charger.counter.OT.isLatch))
  5062. {
  5063. Charger.counter.OT.fail = 0;
  5064. if((Charger.Alarm_Code & ALARM_OVER_TEMPERATURE) && blinker[BLINKER_IDX_LED].blinkisFinish)
  5065. {
  5066. Charger.counter.OT.retry++;
  5067. Charger.Alarm_Code &= ~ALARM_OVER_TEMPERATURE;
  5068. DEBUG_INFO("Alarm OT recover.\r\n");
  5069. }
  5070. }
  5071. /*
  5072. // For multi step OTP logic
  5073. if(Charger.temperature.SystemAmbientTemp > ALARM_SPEC_OT_2)
  5074. {
  5075. if(Charger.counter.OT.fail > 1000)
  5076. {
  5077. if(!(Charger.Alarm_Code & ALARM_OVER_TEMPERATURE))
  5078. {
  5079. Charger.counter.OT.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5080. Charger.Alarm_Code |= ALARM_OVER_TEMPERATURE;
  5081. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  5082. blinkerTimeSet(500, 500, 3000, 3);
  5083. }
  5084. DEBUG_ERROR("Alarm OT occur.\r\n");
  5085. }
  5086. }
  5087. else
  5088. Charger.counter.OT.fail++;
  5089. }
  5090. else if(Charger.temperature.SystemAmbientTemp > ALARM_SPEC_OT_1)
  5091. {
  5092. if(Charger.counter.OT.fail > 1000)
  5093. {
  5094. if(!timer[TIMER_IDX_RETRY_OT].isEnable || timer[TIMER_IDX_RETRY_OT].isAlarm)
  5095. {
  5096. Charger.memory.EVSE_Config.data.item.MaxChargingCurrent *= 0.75;
  5097. }
  5098. timerEnable(TIMER_IDX_RETRY_OT, ALARM_RETRY_INTERVAL_OT);
  5099. }
  5100. else
  5101. Charger.counter.OT.fail++;
  5102. }
  5103. else if(Charger.temperature.SystemAmbientTemp < (ALARM_SPEC_OT_1-ALARM_SPEC_OT_HYSTERESIS))
  5104. {
  5105. Charger.counter.OT.fail = 0;
  5106. if((Charger.Alarm_Code & ALARM_OVER_TEMPERATURE) &&
  5107. timer[TIMER_IDX_RETRY_OT].isAlarm)
  5108. {
  5109. Charger.memory.EVSE_Config.data.item.MaxChargingCurrent = Charger.maxRatingCurrent;
  5110. timerDisable(TIMER_IDX_RETRY_OT);
  5111. osDelay(3000);
  5112. Charger.counter.OT.retry++;
  5113. Charger.Alarm_Code &= ~ALARM_OVER_TEMPERATURE;
  5114. DEBUG_INFO("Alarm OT recover.\r\n");
  5115. }
  5116. }*/
  5117. #endif //OTP_PROTECT
  5118. //================================
  5119. // Current leakage alarm detect
  5120. // TODO: ADC channel & spec need to check for other detection method
  5121. //================================
  5122. #ifdef CCID_PROTECT
  5123. if (Charger.CCID_Module_Type == CCID_MODULE_VAC)
  5124. {
  5125. //module test
  5126. if((HAL_GPIO_ReadPin(IN_Leak_Error_GPIO_Port, IN_Leak_Error_Pin) == GPIO_PIN_SET) && (!Charger.isTestLeakModule))
  5127. {
  5128. if(Charger.counter.LEAK.fail > 1000)
  5129. {
  5130. if(!(Charger.Alarm_Code & ALARM_LEAK_MODULE_FAIL))
  5131. {
  5132. Charger.counter.LEAK.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5133. Charger.Alarm_Code |= ALARM_LEAK_MODULE_FAIL;
  5134. DEBUG_ERROR("Alarm leakage module error occur.\r\n");
  5135. timerEnable(TIMER_IDX_RETRY_LEAK, ALARM_RETRY_INTERVAL_LEAK);
  5136. }
  5137. }
  5138. else
  5139. Charger.counter.LEAK.fail++;
  5140. }
  5141. else
  5142. {
  5143. Charger.counter.LEAK.fail = 0;
  5144. if((Charger.Alarm_Code & ALARM_LEAK_MODULE_FAIL) &&
  5145. (Charger.counter.LEAK.retry < ALARM_RETRY_COUNT) &&
  5146. (timer[TIMER_IDX_RETRY_LEAK].isAlarm == ON) &&
  5147. blinker[BLINKER_IDX_LED].blinkisFinish)
  5148. {
  5149. timerDisable(TIMER_IDX_RETRY_LEAK);
  5150. Charger.Alarm_Code &= ~ALARM_LEAK_MODULE_FAIL;
  5151. osDelay(1000);
  5152. #ifdef DEBUG
  5153. DEBUG_INFO("Alarm leakage module error recover.\r\n");
  5154. #endif
  5155. }
  5156. }
  5157. //DC leakage
  5158. if((HAL_GPIO_ReadPin(IN_Leak_DC_GPIO_Port, IN_Leak_DC_Pin) == GPIO_PIN_SET) && (!Charger.isTestLeakModule))
  5159. {
  5160. if(Charger.counter.LEAK_DC.fail > 10)
  5161. {
  5162. if(!(Charger.Alarm_Code & ALARM_CURRENT_LEAK_DC))
  5163. {
  5164. Charger.counter.LEAK_DC.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5165. Charger.Alarm_Code |= ALARM_CURRENT_LEAK_DC;
  5166. DEBUG_ERROR("Alarm DC leakage occur.\r\n");
  5167. timerEnable(TIMER_IDX_RETRY_LEAK_DC, ALARM_RETRY_INTERVAL_LEAK);
  5168. Charger.counter.LEAK_DC.retry++;
  5169. }
  5170. if (timer[TIMER_IDX_RETRY_LEAK_DC].isAlarm == ON || (Charger.counter.LEAK_DC.retry >ALARM_RETRY_COUNT))
  5171. {
  5172. timerDisable(TIMER_IDX_RETRY_LEAK_DC);
  5173. //Charger.counter.LEAK.isLatch = ON ;
  5174. if (Charger.counter.LEAK.isLatch == OFF)
  5175. {
  5176. Charger.counter.LEAK.isLatch = ON ;
  5177. setLedMotion(LED_ACTION_ALARM);
  5178. }
  5179. DEBUG_ERROR("Alarm leakage DC isLatch.\r\n");
  5180. }
  5181. }
  5182. else
  5183. Charger.counter.LEAK_DC.fail++;
  5184. }
  5185. else
  5186. {
  5187. Charger.counter.LEAK_DC.fail = 0;
  5188. if((Charger.Alarm_Code & ALARM_CURRENT_LEAK_DC) &&
  5189. (Charger.counter.LEAK_DC.retry <= ALARM_RETRY_COUNT) &&
  5190. (timer[TIMER_IDX_RETRY_LEAK_DC].isAlarm == ON) &&
  5191. blinker[BLINKER_IDX_LED].blinkisFinish)
  5192. {
  5193. timerDisable(TIMER_IDX_RETRY_LEAK_DC);
  5194. Charger.Alarm_Code &= ~ALARM_CURRENT_LEAK_DC;
  5195. DEBUG_INFO("Alarm DC leakage recover.\r\n");
  5196. }
  5197. }
  5198. //AC leakage
  5199. if((HAL_GPIO_ReadPin(IN_Leak_AC_GPIO_Port, IN_Leak_AC_Pin) == GPIO_PIN_SET) && (!Charger.isTestLeakModule))
  5200. {
  5201. if(Charger.counter.LEAK_AC.fail > 10)
  5202. {
  5203. if(!(Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC))
  5204. {
  5205. Charger.counter.LEAK_AC.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5206. Charger.Alarm_Code |= ALARM_CURRENT_LEAK_AC;
  5207. DEBUG_ERROR("Alarm AC leakage occur.\r\n");
  5208. timerEnable(TIMER_IDX_RETRY_LEAK_AC, ALARM_RETRY_INTERVAL_LEAK);
  5209. Charger.counter.LEAK_AC.retry++;
  5210. }
  5211. if (timer[TIMER_IDX_RETRY_LEAK_AC].isAlarm == ON || (Charger.counter.LEAK_AC.retry > ALARM_RETRY_COUNT))
  5212. {
  5213. timerDisable(TIMER_IDX_RETRY_LEAK_AC);
  5214. //Charger.counter.LEAK.isLatch = ON ;
  5215. if (Charger.counter.LEAK.isLatch == OFF)
  5216. {
  5217. Charger.counter.LEAK.isLatch = ON ;
  5218. setLedMotion(LED_ACTION_ALARM);
  5219. }
  5220. DEBUG_ERROR("Alarm leakage AC isLatch.\r\n");
  5221. }
  5222. }
  5223. else
  5224. Charger.counter.LEAK_AC.fail++;
  5225. }
  5226. else
  5227. {
  5228. Charger.counter.LEAK_AC.fail = 0;
  5229. if((Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC) &&
  5230. (Charger.counter.LEAK_AC.retry <= ALARM_RETRY_COUNT) &&
  5231. (timer[TIMER_IDX_RETRY_LEAK_AC].isAlarm == ON) &&
  5232. blinker[BLINKER_IDX_LED].blinkisFinish )
  5233. {
  5234. timerDisable(TIMER_IDX_RETRY_LEAK_AC);
  5235. Charger.Alarm_Code &= ~ALARM_CURRENT_LEAK_AC;
  5236. DEBUG_INFO("Alarm AC leakage recover.\r\n");
  5237. }
  5238. }
  5239. }
  5240. else // Charger.CCID_Module_Type is CCID_MODULE_CORMEX
  5241. {
  5242. if((Charger.Leak_Current >= Charger.alarm_spec.Current_LEAK_AC) && (!Charger.isTestLeakModule)) // adc correction data to check
  5243. {
  5244. if(Charger.counter.LEAK.fail >= 5)
  5245. {
  5246. if(!(Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC))
  5247. {
  5248. #ifdef FUNC_MODIFY_LEAK_AC_DISPLAY
  5249. XP("#Leak_Current (%d >= %d)\r\n", Charger.Leak_Current, Charger.alarm_spec.Current_LEAK_AC);
  5250. #else
  5251. XP("************************************************************\r\n");
  5252. XP("adc_value.ADC2_IN6_GF.value = %d\r\n", adc_value.ADC2_IN6_GF.value);
  5253. XP("Charger.Leak_Current(mA) = %d\r\n", Charger.Leak_Current);
  5254. XP("Charger.alarm_spec.Current_LEAK_AC(mA) = %d\r\n", Charger.alarm_spec.Current_LEAK_AC);
  5255. XP("Charger.isTestLeakModule = %s\r\n", Charger.isTestLeakModule ? "ON" : "OFF");
  5256. XP("************************************************************\r\n");
  5257. #endif
  5258. Charger.counter.LEAK.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5259. Charger.Alarm_Code |= ALARM_CURRENT_LEAK_AC; //Hao##### CSU Charge error here
  5260. DEBUG_ERROR("Alarm leakage occur.\r\n");
  5261. timerEnable(TIMER_IDX_RETRY_LEAK_AC, ALARM_RETRY_INTERVAL_LEAK);
  5262. Charger.counter.LEAK.retry ++ ;
  5263. }
  5264. }
  5265. else
  5266. {
  5267. Charger.counter.LEAK.fail++;
  5268. }
  5269. }
  5270. else if (Charger.Leak_Current < (Charger.alarm_spec.Current_LEAK_AC - ALARM_SPEC_CURRENT_LEAK_HYSTERESIS)) // adc correction data to check
  5271. {
  5272. Charger.counter.LEAK.fail = 0;
  5273. if (Charger.counter.LEAK.retry > ALARM_LEAK_RETRY_COUNT)
  5274. {
  5275. if (Charger.counter.LEAK.isLatch == OFF)
  5276. {
  5277. Charger.counter.LEAK.isLatch = ON ;
  5278. setLedMotion(LED_ACTION_ALARM);
  5279. }
  5280. }
  5281. else
  5282. {
  5283. if(((Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC)||(Charger.Alarm_Code & ALARM_CURRENT_LEAK_DC)) &&
  5284. (Charger.counter.LEAK.retry <= ALARM_LEAK_RETRY_COUNT) &&
  5285. (timer[TIMER_IDX_RETRY_LEAK_AC].isAlarm == ON) &&
  5286. blinker[BLINKER_IDX_LED].blinkisFinish )
  5287. {
  5288. timerDisable(TIMER_IDX_RETRY_LEAK_AC);
  5289. osDelay(1000);
  5290. Charger.Alarm_Code &= ~ALARM_CURRENT_LEAK_AC;
  5291. DEBUG_INFO("Alarm leakage recover.\r\n");
  5292. }
  5293. }
  5294. }
  5295. }
  5296. #endif //CCID_PROTECT
  5297. //================================
  5298. // Ground fail alarm detect
  5299. // TODO: Spec need to check
  5300. //================================
  5301. #ifdef GROUND_FAULT_PROTECT
  5302. if(Charger.GroundingDetect==ON){
  5303. #ifdef FUNC_RELAXSPEC_GROUND_FAULT_PROTECT
  5304. static double GF_SPEC_OCCUR = 0;
  5305. static double GF_SPEC_RECOVER = 0;
  5306. if (GF_SPEC_OCCUR == 0 || GF_SPEC_RECOVER == 0)
  5307. {
  5308. GF_SPEC_OCCUR = 3.0 / 220.0; //GF_SPEC_OCCUR = 0.013636(3.0)
  5309. GF_SPEC_RECOVER = 2.0 / 220.0; //GF_SPEC_RECOVER = 0.009091(2.0)
  5310. XP("GF_SPEC_OCCUR = %lf, GF_SPEC_RECOVER = %lf\r\n", GF_SPEC_OCCUR, GF_SPEC_RECOVER);
  5311. }
  5312. #endif
  5313. if (timer[TIMER_IDX_PE_DETECT].isAlarm == ON) // after 30 sec check (GMI adc_value > spec) and (GMI adc_value <= 1 )
  5314. {
  5315. #ifdef FUNC_RELAXSPEC_GROUND_FAULT_PROTECT
  5316. 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))
  5317. #else
  5318. 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))
  5319. #endif
  5320. {
  5321. if(Charger.counter.GF.fail > 4000)
  5322. {
  5323. if(!(Charger.Alarm_Code & ALARM_GROUND_FAIL))
  5324. {
  5325. Charger.counter.GF.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5326. Charger.Alarm_Code |= ALARM_GROUND_FAIL;
  5327. DEBUG_ERROR("Alarm GF occur.\r\n");
  5328. }
  5329. }
  5330. else
  5331. Charger.counter.GF.fail++;
  5332. }
  5333. #ifdef FUNC_RELAXSPEC_GROUND_FAULT_PROTECT
  5334. else if((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) < (Charger.Voltage[0]*GF_SPEC_RECOVER))
  5335. #else
  5336. else if((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) < (Charger.Voltage[0]*0.00566))
  5337. #endif
  5338. {
  5339. Charger.counter.GF.fail = 0;
  5340. if((Charger.Alarm_Code & ALARM_GROUND_FAIL) && blinker[BLINKER_IDX_LED].blinkisFinish)
  5341. {
  5342. //osDelay(3000);
  5343. Charger.counter.GF.retry++;
  5344. Charger.Alarm_Code &= ~ALARM_GROUND_FAIL;
  5345. DEBUG_INFO("Alarm GF recover.\r\n");
  5346. }
  5347. }
  5348. }
  5349. else // before 30 sec check (GMI adc_value > spec)
  5350. {
  5351. #ifdef FUNC_RELAXSPEC_GROUND_FAULT_PROTECT
  5352. if((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) > (Charger.Voltage[0]*GF_SPEC_OCCUR))
  5353. #else
  5354. if((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) > (Charger.Voltage[0]*0.011))
  5355. #endif
  5356. {
  5357. if(Charger.counter.GF.fail > 4000)
  5358. {
  5359. if(!(Charger.Alarm_Code & ALARM_GROUND_FAIL))
  5360. {
  5361. Charger.counter.GF.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5362. Charger.Alarm_Code |= ALARM_GROUND_FAIL;
  5363. DEBUG_ERROR("Alarm GF occur.\r\n");
  5364. }
  5365. }
  5366. else
  5367. Charger.counter.GF.fail++;
  5368. }
  5369. #ifdef FUNC_RELAXSPEC_GROUND_FAULT_PROTECT
  5370. else if((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) < (Charger.Voltage[0]*GF_SPEC_RECOVER))
  5371. #else
  5372. else if((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) < (Charger.Voltage[0]*0.00566))
  5373. #endif
  5374. {
  5375. Charger.counter.GF.fail = 0;
  5376. if((Charger.Alarm_Code & ALARM_GROUND_FAIL) && blinker[BLINKER_IDX_LED].blinkisFinish)
  5377. {
  5378. //osDelay(3000);
  5379. Charger.counter.GF.retry++;
  5380. Charger.Alarm_Code &= ~ALARM_GROUND_FAIL;
  5381. DEBUG_INFO("Alarm GF recover.\r\n");
  5382. }
  5383. }
  5384. }
  5385. }
  5386. #endif //GROUND_FAULT_PROTECT
  5387. //================================
  5388. // Relay status error detect
  5389. // TODO: ADC channel & spec need to check
  5390. //================================
  5391. #ifdef RELAY_WELDING_PROTECT
  5392. //GROUNGING_SYSTEM_LL
  5393. if (Charger.GroundingSystem == GROUNGING_SYSTEM_LL)
  5394. {
  5395. 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)) )
  5396. {
  5397. if(adc_value.ADC2_IN5_Welding.value < ((Charger.Voltage[0]*0.0048)+3)) // for UL dloule L syste a=0.0048 b=0.03V
  5398. {
  5399. if(Charger.counter.RELAY_Drive_Fualt.fail > 1000)
  5400. {
  5401. if(!(Charger.Alarm_Code & ALARM_RELAY_DRIVE_FUALT))
  5402. {
  5403. Charger.counter.RELAY.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5404. Charger.Alarm_Code |= ALARM_RELAY_DRIVE_FUALT;
  5405. DEBUG_ERROR("Alarm Welding Voltage = %d.\r\n",adc_value.ADC2_IN5_Welding.value);
  5406. DEBUG_ERROR("Alarm relay ON status fault occur.\r\n");
  5407. }
  5408. }
  5409. else
  5410. Charger.counter.RELAY_Drive_Fualt.fail++;
  5411. }
  5412. else
  5413. {
  5414. Charger.counter.RELAY_Drive_Fualt.fail = 0 ;
  5415. }
  5416. }
  5417. 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)) )
  5418. {
  5419. //if(adc_value.ADC2_IN5_Welding.value > 24) // 0.24 V
  5420. if(adc_value.ADC2_IN5_Welding.value > ALARM_SPEC_RELAY_LL)
  5421. {
  5422. if(Charger.counter.RELAY.fail > 1000)
  5423. {
  5424. if(!(Charger.Alarm_Code & ALARM_RELAY_STATUS))
  5425. {
  5426. Charger.counter.RELAY.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5427. Charger.Alarm_Code |= ALARM_RELAY_STATUS;
  5428. DEBUG_ERROR("Alarm Welding Voltage = %d.\r\n",adc_value.ADC2_IN5_Welding.value);
  5429. DEBUG_ERROR("Alarm relay OFF status fault occur.\r\n");
  5430. }
  5431. }
  5432. else
  5433. Charger.counter.RELAY.fail++;
  5434. }
  5435. else
  5436. {
  5437. Charger.counter.RELAY.fail = 0 ;
  5438. }
  5439. }
  5440. }
  5441. else // GROUNGING_SYSTEM_LN
  5442. {
  5443. 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)) )
  5444. {
  5445. if(adc_value.ADC2_IN5_Welding.value < ALARM_SPEC_RELAY_LN_DRIVE_FAULT ) // old spec use
  5446. {
  5447. if(Charger.counter.RELAY.fail > 1000)
  5448. {
  5449. if(!(Charger.Alarm_Code & ALARM_RELAY_DRIVE_FUALT))
  5450. {
  5451. Charger.counter.RELAY.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5452. Charger.Alarm_Code |= ALARM_RELAY_DRIVE_FUALT;
  5453. DEBUG_ERROR("Alarm Welding Voltage = %d.\r\n",adc_value.ADC2_IN5_Welding.value);
  5454. DEBUG_ERROR("Alarm relay ON status fault occur.\r\n");
  5455. }
  5456. }
  5457. else
  5458. Charger.counter.RELAY.fail++;
  5459. }
  5460. else
  5461. {
  5462. Charger.counter.RELAY.fail = 0 ;
  5463. }
  5464. }
  5465. 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)) )
  5466. {
  5467. if(adc_value.ADC2_IN5_Welding.value > ALARM_SPEC_RELAY_LN_WELDING)
  5468. {
  5469. if(Charger.counter.RELAY.fail > 1000)
  5470. {
  5471. if(!(Charger.Alarm_Code & ALARM_RELAY_STATUS))
  5472. {
  5473. Charger.counter.RELAY.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5474. Charger.Alarm_Code |= ALARM_RELAY_STATUS;
  5475. DEBUG_ERROR("Alarm Welding Voltage = %d.\r\n",adc_value.ADC2_IN5_Welding.value);
  5476. DEBUG_ERROR("Alarm relay OFF status fault occur.\r\n");
  5477. }
  5478. }
  5479. else
  5480. Charger.counter.RELAY.fail++;
  5481. }
  5482. else
  5483. {
  5484. Charger.counter.RELAY.fail = 0 ;
  5485. }
  5486. }
  5487. }
  5488. #endif //RELAY_WELDING_PROTECT
  5489. //================================
  5490. // Emergency stop alarm detect
  5491. //================================
  5492. #ifdef EMC_BUTTON_PROTECT
  5493. if(HAL_GPIO_ReadPin(IN_Emergency_GPIO_Port, IN_Emergency_Pin) == GPIO_PIN_RESET)
  5494. {
  5495. if(Charger.counter.EMO.fail > 100)
  5496. {
  5497. if(!(Charger.Alarm_Code & ALARM_EMERGENCY_STOP))
  5498. {
  5499. Charger.counter.EMO.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5500. Charger.Alarm_Code |= ALARM_EMERGENCY_STOP;
  5501. DEBUG_ERROR("Alarm EMO occur.\r\n");
  5502. }
  5503. }
  5504. else
  5505. Charger.counter.EMO.fail++;
  5506. }
  5507. else
  5508. {
  5509. Charger.counter.EMO.fail = 0;
  5510. if((Charger.Alarm_Code & ALARM_EMERGENCY_STOP) && blinker[BLINKER_IDX_LED].blinkisFinish)
  5511. {
  5512. Charger.counter.EMO.retry++;
  5513. Charger.Alarm_Code &= ~ALARM_EMERGENCY_STOP;
  5514. DEBUG_INFO("Alarm EMO recover.\r\n");
  5515. }
  5516. }
  5517. #endif //EMC_BUTTON_PROTECT
  5518. //================================
  5519. // Handshaking timeout detect
  5520. //================================
  5521. #ifdef HANDSHAKE_PROTECT
  5522. if(timer[TIMER_IDX_CP].isAlarm == ON)
  5523. {
  5524. if(!(Charger.Alarm_Code & ALARM_HANDSHAKE_TIMEOUT))
  5525. {
  5526. Charger.Alarm_Code |= ALARM_HANDSHAKE_TIMEOUT;
  5527. timerDisable(TIMER_IDX_CP);
  5528. Charger.rfid.op_bits.reqStart = OFF;
  5529. Charger.istoHandShakeMode = OFF ;
  5530. DEBUG_ERROR("Alarm handshake overtime occur.\r\n");
  5531. }
  5532. }
  5533. else
  5534. {
  5535. if((Charger.Alarm_Code & ALARM_HANDSHAKE_TIMEOUT))
  5536. {
  5537. osDelay(10000);
  5538. Charger.Alarm_Code &= ~ALARM_HANDSHAKE_TIMEOUT;
  5539. DEBUG_INFO("Alarm handshake overtime recover.\r\n");
  5540. }
  5541. }
  5542. #endif //HANDSHAKE_PROTECT
  5543. //================================
  5544. // Rotary Switch Maxium Current Error detect
  5545. //================================
  5546. #ifdef ROTATE_SWITCH_CHECK
  5547. if (Charger.isRotarySwitchError)
  5548. {
  5549. if(!(Charger.Alarm_Code & ALARM_ROTATORY_SWITCH_FAULT))
  5550. {
  5551. Charger.Alarm_Code |= ALARM_ROTATORY_SWITCH_FAULT;
  5552. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  5553. blinkerTimeSet(BLINKER_IDX_LED, 200, 200, 0, 1);
  5554. }
  5555. DEBUG_INFO("Rotary Switch Maxium Current Error occur.\r\n");
  5556. }
  5557. }
  5558. #endif //ROTATE_SWITCH_CHECK
  5559. //================================
  5560. // Recover alarm info in state A
  5561. //================================
  5562. if(Charger.CP_State == SYSTEM_STATE_A)
  5563. {
  5564. //SCP Recover
  5565. if(Charger.Alarm_Code & ALARM_CIRCUIT_SHORT)
  5566. {
  5567. Charger.Alarm_Code &= ~ALARM_CIRCUIT_SHORT;
  5568. DEBUG_INFO("Alarm SCP recover.\r\n");
  5569. }
  5570. //OCP Recover
  5571. if(Charger.Alarm_Code & ALARM_OVER_CURRENT)
  5572. {
  5573. Charger.Alarm_Code &= ~ALARM_OVER_CURRENT;
  5574. DEBUG_INFO("Alarm OC recover.\r\n");
  5575. }
  5576. Charger.counter.OC.fail = 0 ;
  5577. timerDisable(TIMER_IDX_RETRY_OC);
  5578. Charger.counter.OC.retry = 0 ; // clear retry times
  5579. Charger.counter.OC.isLatch = OFF ;
  5580. //CCID Recover
  5581. if (Charger.CCID_Module_Type == CCID_MODULE_VAC)
  5582. {
  5583. if ((HAL_GPIO_ReadPin(IN_Leak_AC_GPIO_Port, IN_Leak_AC_Pin) == GPIO_PIN_RESET) && (Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC))
  5584. {
  5585. Charger.Alarm_Code &= ~ALARM_CURRENT_LEAK_AC;
  5586. DEBUG_INFO("Alarm leakage AC recover for VAC.\r\n");
  5587. }
  5588. if ((HAL_GPIO_ReadPin(IN_Leak_DC_GPIO_Port, IN_Leak_DC_Pin) == GPIO_PIN_RESET) && (Charger.Alarm_Code & ALARM_CURRENT_LEAK_DC))
  5589. {
  5590. Charger.Alarm_Code &= ~ALARM_CURRENT_LEAK_DC;
  5591. DEBUG_INFO("Alarm leakage DC recover for VAC.\r\n");
  5592. }
  5593. }
  5594. else //Charger.CCID_Module_Type is CCID_MODULE_CORMEX
  5595. {
  5596. #ifdef MODIFY_CCID_MODULE_CORMEX_RECOVER_AC_DC
  5597. if (Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC)
  5598. {
  5599. Charger.Alarm_Code &= ~ALARM_CURRENT_LEAK_AC;
  5600. DEBUG_INFO("Alarm leakage AC recover.\r\n");
  5601. }
  5602. if (Charger.Alarm_Code & ALARM_CURRENT_LEAK_DC)
  5603. {
  5604. Charger.Alarm_Code &= ~ALARM_CURRENT_LEAK_DC;
  5605. DEBUG_INFO("Alarm leakage DC recover.\r\n");
  5606. }
  5607. #else //MODIFY_CCID_MODULE_CORMEX_RECOVER_AC_DC
  5608. if((Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC)||(Charger.Alarm_Code & ALARM_CURRENT_LEAK_DC))
  5609. {
  5610. Charger.Alarm_Code &= ~ALARM_CURRENT_LEAK_AC;
  5611. DEBUG_INFO("Alarm leakage recover.\r\n");
  5612. }
  5613. #endif //MODIFY_CCID_MODULE_CORMEX_RECOVER_AC_DC
  5614. }
  5615. Charger.counter.LEAK.fail = 0;
  5616. timerDisable(TIMER_IDX_RETRY_LEAK) ;
  5617. timerDisable(TIMER_IDX_RETRY_LEAK_AC) ;
  5618. timerDisable(TIMER_IDX_RETRY_LEAK_DC) ;
  5619. Charger.counter.LEAK.retry = 0 ; // clear latch times
  5620. Charger.counter.LEAK_DC.retry = 0 ; // clear latch times
  5621. Charger.counter.LEAK_AC.retry = 0 ; // clear latch times
  5622. Charger.counter.LEAK.isLatch = OFF ;
  5623. //LEAK_MODULE selfrest Recover
  5624. if (Charger.CCID_Module_Type == CCID_MODULE_VAC)
  5625. {
  5626. 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
  5627. {
  5628. if ((Charger.counter.LEAK_MODULE.retry < 2) && (!Charger.counter.LEAK_MODULE.isLatch))
  5629. {
  5630. Charger.Alarm_Code &= ~ALARM_LEAK_MODULE_FAIL;
  5631. DEBUG_INFO("Alarm leakage module recover.\r\n");
  5632. }
  5633. }
  5634. }
  5635. else //Charger.CCID_Module_Type is CCID_MODULE_CORMEX
  5636. {
  5637. if(Charger.Alarm_Code & ALARM_LEAK_MODULE_FAIL) // leak module is CORMEX
  5638. {
  5639. if ((Charger.counter.LEAK_MODULE.retry < 2) && (!Charger.counter.LEAK_MODULE.isLatch))
  5640. {
  5641. Charger.Alarm_Code &= ~ALARM_LEAK_MODULE_FAIL;
  5642. DEBUG_INFO("Alarm leakage module recover.\r\n");
  5643. }
  5644. }
  5645. }
  5646. }
  5647. }
  5648. else if (Charger.Mode == MODE_DEBUG)
  5649. {
  5650. //================================
  5651. // Relay status error detect
  5652. // TODO: ADC channel & spec need to check
  5653. //================================
  5654. #ifdef RELAY_WELDING_PROTECT
  5655. //GROUNGING_SYSTEM_LL
  5656. if (Charger.GroundingSystem == GROUNGING_SYSTEM_LL)
  5657. {
  5658. 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)) )
  5659. {
  5660. if(adc_value.ADC2_IN5_Welding.value < ((Charger.Voltage[0]*0.0048)+3)) // for UL dloule L syste a=0.0048 b=0.03V
  5661. {
  5662. if(Charger.counter.RELAY_Drive_Fualt.fail > 1000)
  5663. {
  5664. if(!(Charger.Alarm_Code & ALARM_RELAY_DRIVE_FUALT))
  5665. {
  5666. Charger.counter.RELAY.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5667. Charger.Alarm_Code |= ALARM_RELAY_DRIVE_FUALT;
  5668. DEBUG_ERROR("Alarm Welding Voltage = %d.\r\n",adc_value.ADC2_IN5_Welding.value);
  5669. DEBUG_ERROR("Alarm relay ON status fault occur.\r\n");
  5670. }
  5671. }
  5672. else
  5673. Charger.counter.RELAY_Drive_Fualt.fail++;
  5674. }
  5675. else
  5676. {
  5677. Charger.counter.RELAY_Drive_Fualt.fail = 0 ;
  5678. }
  5679. }
  5680. 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)) )
  5681. {
  5682. //if(adc_value.ADC2_IN5_Welding.value > 24) // 0.24 V
  5683. if(adc_value.ADC2_IN5_Welding.value > ALARM_SPEC_RELAY_LL)
  5684. {
  5685. if(Charger.counter.RELAY.fail > 1000)
  5686. {
  5687. if(!(Charger.Alarm_Code & ALARM_RELAY_STATUS))
  5688. {
  5689. Charger.counter.RELAY.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5690. Charger.Alarm_Code |= ALARM_RELAY_STATUS;
  5691. DEBUG_ERROR("Alarm Welding Voltage = %d.\r\n",adc_value.ADC2_IN5_Welding.value);
  5692. DEBUG_ERROR("Alarm relay OFF status fault occur.\r\n");
  5693. }
  5694. }
  5695. else
  5696. Charger.counter.RELAY.fail++;
  5697. }
  5698. else
  5699. {
  5700. Charger.counter.RELAY.fail = 0 ;
  5701. }
  5702. }
  5703. }
  5704. else // GROUNGING_SYSTEM_LN
  5705. {
  5706. 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)) )
  5707. {
  5708. if(adc_value.ADC2_IN5_Welding.value < ALARM_SPEC_RELAY_LN_DRIVE_FAULT ) // old spec use
  5709. {
  5710. if(Charger.counter.RELAY.fail > 1000)
  5711. {
  5712. if(!(Charger.Alarm_Code & ALARM_RELAY_DRIVE_FUALT))
  5713. {
  5714. Charger.counter.RELAY.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5715. Charger.Alarm_Code |= ALARM_RELAY_DRIVE_FUALT;
  5716. DEBUG_ERROR("Alarm Welding Voltage = %d.\r\n",adc_value.ADC2_IN5_Welding.value);
  5717. DEBUG_ERROR("Alarm relay ON status fault occur.\r\n");
  5718. }
  5719. }
  5720. else
  5721. Charger.counter.RELAY.fail++;
  5722. }
  5723. else
  5724. {
  5725. Charger.counter.RELAY.fail = 0 ;
  5726. }
  5727. }
  5728. 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)) )
  5729. {
  5730. if(adc_value.ADC2_IN5_Welding.value > ALARM_SPEC_RELAY_LN_WELDING)
  5731. {
  5732. if(Charger.counter.RELAY.fail > 1000)
  5733. {
  5734. if(!(Charger.Alarm_Code & ALARM_RELAY_STATUS))
  5735. {
  5736. Charger.counter.RELAY.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5737. Charger.Alarm_Code |= ALARM_RELAY_STATUS;
  5738. DEBUG_ERROR("Alarm Welding Voltage = %d.\r\n",adc_value.ADC2_IN5_Welding.value);
  5739. DEBUG_ERROR("Alarm relay OFF status fault occur.\r\n");
  5740. }
  5741. }
  5742. else
  5743. Charger.counter.RELAY.fail++;
  5744. }
  5745. else
  5746. {
  5747. Charger.counter.RELAY.fail = 0 ;
  5748. }
  5749. }
  5750. }
  5751. #endif //RELAY_WELDING_PROTECT
  5752. }
  5753. osDelay(1);
  5754. }
  5755. /* USER CODE END StartAlarmTask */
  5756. }
  5757. /* USER CODE BEGIN Header_StartBleTask */
  5758. /**
  5759. * @brief Function implementing the bleTask thread.
  5760. * @param argument: Not used
  5761. * @retval None
  5762. */
  5763. /* USER CODE END Header_StartBleTask */
  5764. void StartBleTask(void const * argument)
  5765. {
  5766. /* USER CODE BEGIN StartBleTask */
  5767. char tmpBuf[128];
  5768. uint8_t endFlag[4]={0x55,0xaa,0x55,0xaa};
  5769. __IO uint32_t flash;
  5770. uint32_t checksum;
  5771. uint32_t rndNumber;
  5772. //=====================================
  5773. // Module reset
  5774. //=====================================
  5775. HAL_GPIO_WritePin(OUT_BLE_RESET_GPIO_Port, OUT_BLE_RESET_Pin, GPIO_PIN_RESET);
  5776. osDelay(60);
  5777. HAL_GPIO_WritePin(OUT_BLE_RESET_GPIO_Port, OUT_BLE_RESET_Pin, GPIO_PIN_SET);
  5778. HAL_GPIO_WritePin(OUT_BLE_DSR_GPIO_Port, OUT_BLE_DSR_Pin, GPIO_PIN_SET);
  5779. Charger.ble.initSeq = 0;
  5780. Charger.ble.isDataMode = OFF;
  5781. UART_BLE_Init_OK = OFF;
  5782. osDelay(1000);
  5783. /* Infinite loop */
  5784. for(;;)
  5785. {
  5786. //=====================================
  5787. // BLE module initialization process
  5788. //=====================================
  5789. switch(Charger.ble.initSeq)
  5790. {
  5791. case 0x00:
  5792. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_RESTORE], strlen(BLE_CMD[BLE_CMD_SET_RESTORE]), 0xffff);
  5793. osDelay(BLE_INIT_DELAY);
  5794. break;
  5795. case 0x01:
  5796. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_ROLE], strlen(BLE_CMD[BLE_CMD_SET_ROLE]), 0xffff);
  5797. osDelay(BLE_INIT_DELAY);
  5798. break;
  5799. case 0x02:
  5800. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_DTR], strlen(BLE_CMD[BLE_CMD_SET_DTR]), 0xffff);
  5801. osDelay(BLE_INIT_DELAY);
  5802. break;
  5803. case 0x03:
  5804. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_DSR], strlen(BLE_CMD[BLE_CMD_SET_DSR]), 0xffff);
  5805. osDelay(BLE_INIT_DELAY);
  5806. break;
  5807. case 0x04:
  5808. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_PAIRING_MODE], strlen(BLE_CMD[BLE_CMD_SET_PAIRING_MODE]), 0xffff);
  5809. osDelay(BLE_INIT_DELAY);
  5810. break;
  5811. case 0x05:
  5812. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_SECURITY_MODE], strlen(BLE_CMD[BLE_CMD_SET_SECURITY_MODE]), 0xffff);
  5813. osDelay(BLE_INIT_DELAY);
  5814. break;
  5815. case 0x06:
  5816. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_SECURITY_TYPE], strlen(BLE_CMD[BLE_CMD_SET_SECURITY_TYPE]), 0xffff);
  5817. osDelay(BLE_INIT_DELAY);
  5818. break;
  5819. case 0x07:
  5820. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_DISCOVERABILITY], strlen(BLE_CMD[BLE_CMD_SET_DISCOVERABILITY]), 0xffff);
  5821. osDelay(BLE_INIT_DELAY);
  5822. break;
  5823. case 0x08:
  5824. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_CONNECTABILITY], strlen(BLE_CMD[BLE_CMD_SET_CONNECTABILITY]), 0xffff);
  5825. osDelay(BLE_INIT_DELAY);
  5826. break;
  5827. case 0x09:
  5828. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_GET_ADDR], strlen(BLE_CMD[BLE_CMD_GET_ADDR]), 0xffff);
  5829. osDelay(BLE_INIT_DELAY);
  5830. break;
  5831. case 0x0a:
  5832. sprintf(tmpBuf, "%s\"BYTON_EVSE_%s\"\r\n", BLE_CMD[BLE_CMD_SET_NAME], Charger.ble.peripheral_mac);
  5833. DEBUG_INFO("BLE module set name:BYTON_EVSE_%s\r\n", Charger.ble.peripheral_mac);
  5834. HAL_UART_Transmit(&BLE_USART, (uint8_t *)tmpBuf, strlen(tmpBuf), 0xffff);
  5835. osDelay(BLE_INIT_DELAY);
  5836. break;
  5837. case 0x0b:
  5838. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_MAX_POWER], strlen(BLE_CMD[BLE_CMD_SET_MAX_POWER]), 0xffff);
  5839. osDelay(BLE_INIT_DELAY);
  5840. break;
  5841. case 0x0c:
  5842. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_MIN_INTERVAL], strlen(BLE_CMD[BLE_CMD_SET_MIN_INTERVAL]), 0xffff);
  5843. osDelay(BLE_INIT_DELAY);
  5844. break;
  5845. case 0x0d:
  5846. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_MAX_INTERVAL], strlen(BLE_CMD[BLE_CMD_SET_MAX_INTERVAL]), 0xffff);
  5847. osDelay(BLE_INIT_DELAY);
  5848. break;
  5849. case 0x0e:
  5850. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_TX_PHY], strlen(BLE_CMD[BLE_CMD_SET_TX_PHY]), 0xffff);
  5851. osDelay(BLE_INIT_DELAY);
  5852. break;
  5853. case 0x0f:
  5854. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_RX_PHY], strlen(BLE_CMD[BLE_CMD_SET_RX_PHY]), 0xffff);
  5855. osDelay(BLE_INIT_DELAY);
  5856. break;
  5857. case 0x10:
  5858. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_DATA_MODE], strlen(BLE_CMD[BLE_CMD_SET_DATA_MODE]), 0xffff);
  5859. DEBUG_INFO("BLE module switch to data mode.\r\n");
  5860. osDelay(BLE_INIT_DELAY);
  5861. default:
  5862. UART_BLE_Init_OK = ON;
  5863. break;
  5864. }
  5865. //=====================================
  5866. // BLE central device connect status
  5867. //=====================================
  5868. if((HAL_GPIO_ReadPin(IN_BLE_DTR_GPIO_Port, IN_BLE_DTR_Pin) == GPIO_PIN_SET) && (UART_BLE_Init_OK == ON))
  5869. {
  5870. // Device disconnect
  5871. timerRefresh(TIMER_IDX_BLE);
  5872. if(Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT || Charger.ble.loginRole == BLE_LOGIN_ROLE_USER)
  5873. {
  5874. DEBUG_INFO("BLE central device disconnect.\r\n.");
  5875. Charger.ble.loginRole = BLE_LOGIN_ROLE_UNKOWN;
  5876. }
  5877. }
  5878. else if((HAL_GPIO_ReadPin(IN_BLE_DTR_GPIO_Port, IN_BLE_DTR_Pin) == GPIO_PIN_RESET) && (UART_BLE_Init_OK == ON))
  5879. {
  5880. // Device connect in
  5881. if(Charger.ble.loginRole == BLE_LOGIN_ROLE_UNKOWN)
  5882. {
  5883. DEBUG_INFO("BLE central device connect.....wait sign in.\r\n.");
  5884. osDelay(1000);
  5885. }
  5886. }
  5887. //=====================================
  5888. // Communication timeout process
  5889. //=====================================
  5890. if(timer[TIMER_IDX_BLE].isAlarm)
  5891. {
  5892. Charger.ble.loginRole = BLE_LOGIN_ROLE_UNKOWN;
  5893. DEBUG_INFO("BLE no communication over time, disconnect central device.\r\n");
  5894. timerRefresh(TIMER_IDX_BLE);
  5895. HAL_GPIO_WritePin(OUT_BLE_DSR_GPIO_Port, OUT_BLE_DSR_Pin, GPIO_PIN_RESET);
  5896. osDelay(100);
  5897. HAL_GPIO_WritePin(OUT_BLE_DSR_GPIO_Port, OUT_BLE_DSR_Pin, GPIO_PIN_SET);
  5898. }
  5899. //=====================================
  5900. // BLE uart recieve data process
  5901. //=====================================
  5902. if(UART_BLE_recv_end_flag == ON)
  5903. {
  5904. if((HAL_GPIO_ReadPin(IN_BLE_DTR_GPIO_Port, IN_BLE_DTR_Pin) == GPIO_PIN_SET) || !Charger.ble.isDataMode)
  5905. {
  5906. // AT command rx process
  5907. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_RESTORE])!=NULL)
  5908. {
  5909. Charger.ble.initSeq++;
  5910. }
  5911. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_ROLE])!=NULL)
  5912. {
  5913. Charger.ble.initSeq++;
  5914. }
  5915. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_DTR])!=NULL)
  5916. {
  5917. Charger.ble.initSeq++;
  5918. }
  5919. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_DSR])!=NULL)
  5920. {
  5921. Charger.ble.initSeq++;
  5922. }
  5923. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_PAIRING_MODE])!=NULL)
  5924. {
  5925. Charger.ble.initSeq++;
  5926. }
  5927. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_SECURITY_MODE])!=NULL)
  5928. {
  5929. Charger.ble.initSeq++;
  5930. }
  5931. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_SECURITY_TYPE])!=NULL)
  5932. {
  5933. Charger.ble.initSeq++;
  5934. }
  5935. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_DISCOVERABILITY])!=NULL)
  5936. {
  5937. Charger.ble.initSeq++;
  5938. }
  5939. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_CONNECTABILITY])!=NULL)
  5940. {
  5941. Charger.ble.initSeq++;
  5942. }
  5943. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_GET_ADDR])!=NULL)
  5944. {
  5945. memcpy(Charger.ble.peripheral_mac, &UART_BLE_rx_buffer[strcspn((char *)UART_BLE_rx_buffer, ":")+1], 12);
  5946. Charger.ble.peripheral_mac[12] = '\0';
  5947. DEBUG_INFO("Peripheral mac address: %s\r\n", Charger.ble.peripheral_mac);
  5948. Charger.ble.initSeq++;
  5949. }
  5950. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_NAME])!=NULL)
  5951. {
  5952. Charger.ble.initSeq++;
  5953. }
  5954. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_MAX_POWER])!=NULL)
  5955. {
  5956. Charger.ble.initSeq++;
  5957. }
  5958. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_MIN_INTERVAL])!=NULL)
  5959. {
  5960. Charger.ble.initSeq++;
  5961. }
  5962. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_MAX_INTERVAL])!=NULL)
  5963. {
  5964. Charger.ble.initSeq++;
  5965. }
  5966. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_TX_PHY])!=NULL)
  5967. {
  5968. Charger.ble.initSeq++;
  5969. }
  5970. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_RX_PHY])!=NULL)
  5971. {
  5972. Charger.ble.initSeq++;
  5973. }
  5974. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_DATA_MODE])!=NULL)
  5975. {
  5976. Charger.ble.isDataMode = ON;
  5977. Charger.ble.initSeq++;
  5978. }
  5979. }
  5980. else
  5981. {
  5982. // Application protocol rx process
  5983. uint8_t tx[UART_BUFFER_SIZE];
  5984. uint8_t tx_len;
  5985. uint8_t chksum = 0;
  5986. uint32_t tmp;
  5987. //HTK_ByteArray2HexStr_XP("BLE_RX: ", UART_BLE_rx_buffer, 0, 16);
  5988. 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))))
  5989. {
  5990. // Timer reset
  5991. timerRefresh(TIMER_IDX_BLE);
  5992. 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));
  5993. switch(UART_BLE_rx_buffer[2])
  5994. {
  5995. case BLE_PROTOCOL_MESSAGE_SIGN_IN:
  5996. DEBUG_INFO("BLE device sign request...%d\r\n", UART_BLE_rx_buffer[7]);
  5997. tx_len = 5;
  5998. tx[0] = (tx_len>>8)&0xff;
  5999. tx[1] = (tx_len&0xff);
  6000. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6001. tx[3] = 0x00;
  6002. if(Charger.memory.EVSE_Config.data.item.bleConfig.isRegPuk)
  6003. {
  6004. if(UART_BLE_rx_buffer[7] == 0x00)
  6005. {
  6006. 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)
  6007. {
  6008. // Root sign in
  6009. tx[3] = 0x02;
  6010. Charger.ble.loginRole = BLE_LOGIN_ROLE_ROOT;
  6011. DEBUG_INFO("Sign in as root.\r\n");
  6012. }
  6013. }
  6014. else
  6015. {
  6016. 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)
  6017. {
  6018. // User sign in
  6019. tx[3] = 0x03;
  6020. Charger.ble.loginRole = BLE_LOGIN_ROLE_USER;
  6021. DEBUG_INFO("Sign in as user.\r\n");
  6022. }
  6023. }
  6024. }
  6025. else
  6026. {
  6027. if(UART_BLE_rx_buffer[7] == 0x00)
  6028. {
  6029. // Visitor
  6030. tx[3] = 0x01;
  6031. Charger.ble.loginRole = BLE_LOGIN_ROLE_VISITOR;
  6032. memcpy(&Charger.ble.loginId[0], &UART_BLE_rx_buffer[8], ((UART_BLE_rx_buffer[0]<<8) | (UART_BLE_rx_buffer[1]<<0)-9) );
  6033. DEBUG_WARN("Sign in as visitor.\r\n");
  6034. }
  6035. }
  6036. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6037. {
  6038. chksum ^= tx[2 + idx];
  6039. }
  6040. tx[4] = chksum;
  6041. break;
  6042. case BLE_PROTOCOL_MESSAGE_PUK_REG:
  6043. 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]);
  6044. tx_len = 11;
  6045. tx[0] = (tx_len>>8)&0xff;
  6046. tx[1] = (tx_len&0xff);
  6047. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6048. tx[3] = 0x00;
  6049. if(Charger.memory.EVSE_Config.data.item.bleConfig.isRegPuk)
  6050. {
  6051. if(memcmp(&Charger.ble.loginId[0], &Charger.memory.EVSE_Config.data.item.bleConfig.idRoot[0], ARRAY_SIZE(Charger.ble.loginId)))
  6052. {
  6053. tx[3] = 0x02;
  6054. DEBUG_INFO("ID registered exist.\r\n");
  6055. }
  6056. }
  6057. else
  6058. {
  6059. if(Charger.ble.loginRole == BLE_LOGIN_ROLE_VISITOR)
  6060. {
  6061. if(memcmp(&UART_BLE_rx_buffer[7], &Charger.memory.EVSE_Config.data.item.bleConfig.puk[0], 8) == 0x00)
  6062. {
  6063. tx[3] = 0x01;
  6064. // PIN list generation
  6065. Charger.memory.EVSE_Config.data.item.bleConfig.isGenPin = ON;
  6066. for(uint8_t idx=0;idx<20;idx++)
  6067. {
  6068. Charger.memory.whiteList.data.item[idx].listType = WHITE_LIST_TYPE_BLE;
  6069. Charger.memory.whiteList.data.item[idx].pin[0] = (rand()%0x2b)+0x30;
  6070. Charger.memory.whiteList.data.item[idx].pin[1] = (rand()%0x2b)+0x30;
  6071. Charger.memory.whiteList.data.item[idx].pin[2] = (rand()%0x2b)+0x30;
  6072. Charger.memory.whiteList.data.item[idx].pin[3] = (rand()%0x2b)+0x30;
  6073. Charger.memory.whiteList.data.item[idx].pin[4] = (rand()%0x2b)+0x30;
  6074. Charger.memory.whiteList.data.item[idx].pin[5] = (rand()%0x2b)+0x30;
  6075. Charger.memory.whiteList.data.item[idx].isReg = OFF;
  6076. }
  6077. Charger.memory.whiteList.op_bits.update = ON;
  6078. Charger.memory.EVSE_Config.data.item.bleConfig.isRegPuk = ON;
  6079. memcpy(&Charger.memory.EVSE_Config.data.item.bleConfig.idRoot[0], &Charger.ble.loginId[0], ARRAY_SIZE(Charger.ble.loginId));
  6080. Charger.memory.EVSE_Config.op_bits.update = ON;
  6081. DEBUG_INFO("Root registered and PIN list generate success.\r\n");
  6082. }
  6083. }
  6084. }
  6085. memcpy(&tx[4], &UART_BLE_rx_buffer[7], 6);
  6086. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6087. {
  6088. chksum ^= tx[2 + idx];
  6089. }
  6090. tx[10] = chksum;
  6091. break;
  6092. case BLE_PROTOCOL_MESSAGE_PIN_GET:
  6093. DEBUG_INFO("PIN get request.\r\n");
  6094. tx_len = 11;
  6095. tx[0] = (tx_len>>8)&0xff;
  6096. tx[1] = (tx_len&0xff);
  6097. tx[2] = UART_BLE_rx_buffer[2] + 0x80;;
  6098. tx[3] = 0x00;
  6099. if(Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT)
  6100. {
  6101. tx[3] = 0x01;
  6102. memcpy(&tx[4], &Charger.memory.whiteList.data.item[0].pin[0], 6);
  6103. DEBUG_INFO("PIN: %s\r\n", Charger.memory.whiteList.data.item[0].pin);
  6104. }
  6105. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6106. {
  6107. chksum ^= tx[2 + idx];
  6108. }
  6109. tx[10] = chksum;
  6110. break;
  6111. case BLE_PROTOCOL_MESSAGE_PIN_RENEW:
  6112. DEBUG_INFO("BLE device PIN renew request.\r\n");
  6113. tx_len = 13;
  6114. tx[0] = (tx_len>>8)&0xff;
  6115. tx[1] = (tx_len&0xff);
  6116. tx[2] = UART_BLE_rx_buffer[2] + 0x80;;
  6117. tx[3] = 0x00;
  6118. if((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT))
  6119. {
  6120. for(uint8_t idx=0;idx<20;idx++)
  6121. {
  6122. Charger.memory.whiteList.data.item[idx].listType = WHITE_LIST_TYPE_BLE;
  6123. Charger.memory.whiteList.data.item[idx].pin[0] = (rand()%0x2b)+0x30;
  6124. Charger.memory.whiteList.data.item[idx].pin[1] = (rand()%0x2b)+0x30;
  6125. Charger.memory.whiteList.data.item[idx].pin[2] = (rand()%0x2b)+0x30;
  6126. Charger.memory.whiteList.data.item[idx].pin[3] = (rand()%0x2b)+0x30;
  6127. Charger.memory.whiteList.data.item[idx].pin[4] = (rand()%0x2b)+0x30;
  6128. Charger.memory.whiteList.data.item[idx].pin[5] = (rand()%0x2b)+0x30;
  6129. Charger.memory.whiteList.data.item[idx].isReg = OFF;
  6130. }
  6131. Charger.memory.whiteList.op_bits.update = ON;
  6132. tx[3] = 0x01;
  6133. memcpy(&tx[4], &Charger.memory.whiteList.data.item[0].pin[0], 6);
  6134. DEBUG_INFO("PIN list renew: %s.\r\n", Charger.memory.whiteList.data.item[0].pin);
  6135. }
  6136. else
  6137. DEBUG_WARN("Login id not root.\r\n");
  6138. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6139. {
  6140. chksum ^= tx[2 + idx];
  6141. }
  6142. tx[4] = chksum;
  6143. break;
  6144. case BLE_PROTOCOL_MESSAGE_CHARGING_START:
  6145. DEBUG_INFO("BLE device request charging start.\r\n");
  6146. tx_len = 0x08;
  6147. tx[0] = (tx_len>>8)&0xff;
  6148. tx[1] = (tx_len&0xff);
  6149. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6150. if(Charger.Alarm_Code>0)
  6151. {
  6152. tx[3] = 0x02;
  6153. if((Charger.Alarm_Code & ALARM_OVER_VOLTAGE))
  6154. {
  6155. tx[4] = 0x01;
  6156. tx[5] = 0x22;
  6157. tx[6] = 0x00;
  6158. }
  6159. else if((Charger.Alarm_Code & ALARM_UNDER_VOLTAGE))
  6160. {
  6161. tx[4] = 0x01;
  6162. tx[5] = 0x22;
  6163. tx[6] = 0x03;
  6164. }
  6165. else if((Charger.Alarm_Code & ALARM_OVER_CURRENT))
  6166. {
  6167. tx[4] = 0x01;
  6168. tx[5] = 0x21;
  6169. tx[6] = 0x16;
  6170. }
  6171. else if((Charger.Alarm_Code & ALARM_OVER_TEMPERATURE))
  6172. {
  6173. tx[4] = 0x01;
  6174. tx[5] = 0x22;
  6175. tx[6] = 0x23;
  6176. }
  6177. else if((Charger.Alarm_Code & ALARM_GROUND_FAIL))
  6178. {
  6179. tx[4] = 0x01;
  6180. tx[5] = 0x22;
  6181. tx[6] = 0x56;
  6182. }
  6183. else if((Charger.Alarm_Code & ALARM_CP_ERROR))
  6184. {
  6185. tx[4] = 0x02;
  6186. tx[5] = 0x37;
  6187. tx[6] = 0x03;
  6188. }
  6189. else if((Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC)||(Charger.Alarm_Code & ALARM_CURRENT_LEAK_DC))
  6190. {
  6191. tx[4] = 0x01;
  6192. tx[5] = 0x22;
  6193. tx[6] = 0x33;
  6194. }
  6195. else if((Charger.Alarm_Code & ALARM_MCU_TESTFAIL))
  6196. {
  6197. tx[4] = 0x01;
  6198. tx[5] = 0x22;
  6199. tx[6] = 0x57;
  6200. }
  6201. else if((Charger.Alarm_Code & ALARM_EMERGENCY_STOP))
  6202. {
  6203. tx[4] = 0x01;
  6204. tx[5] = 0x22;
  6205. tx[6] = 0x51;
  6206. }
  6207. else if((Charger.Alarm_Code & ALARM_LEAK_MODULE_FAIL))
  6208. {
  6209. tx[4] = 0x01;
  6210. tx[5] = 0x10;
  6211. tx[6] = 0x04;
  6212. }
  6213. else if((Charger.Alarm_Code & ALARM_RELAY_STATUS))
  6214. {
  6215. tx[4] = 0x01;
  6216. tx[5] = 0x22;
  6217. tx[6] = 0x58;
  6218. }
  6219. DEBUG_WARN("EVSE in alarm state, can not start charging.\r\n");
  6220. }
  6221. else
  6222. {
  6223. if((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))
  6224. {
  6225. if(Charger.Mode == MODE_IDLE)
  6226. {
  6227. Charger.ble.isRequestOn = ON;
  6228. tx[3] = 0x01;
  6229. DEBUG_INFO("Start OK.\r\n");
  6230. }
  6231. else
  6232. {
  6233. tx[3] = 0x00;
  6234. DEBUG_WARN("EVSE not in IDLE.\r\n");
  6235. }
  6236. }
  6237. else
  6238. {
  6239. tx[3] = 0x00;
  6240. DEBUG_WARN("Login first\r\n");
  6241. }
  6242. tx[4] = 0x00;
  6243. tx[5] = 0x00;
  6244. tx[6] = 0x00;
  6245. }
  6246. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6247. {
  6248. chksum ^= tx[2 + idx];
  6249. }
  6250. tx[7] = chksum;
  6251. break;
  6252. case BLE_PROTOCOL_MESSAGE_CHARGING_STOP:
  6253. DEBUG_INFO("BLE device request charging stop.\r\n");
  6254. tx_len = 0x08;
  6255. tx[0] = (tx_len>>8)&0xff;
  6256. tx[1] = (tx_len&0xff);
  6257. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6258. if(Charger.Alarm_Code>0)
  6259. {
  6260. tx[3] = 0x02;
  6261. if((Charger.Alarm_Code & ALARM_OVER_VOLTAGE))
  6262. {
  6263. tx[4] = 0x01;
  6264. tx[5] = 0x22;
  6265. tx[6] = 0x00;
  6266. }
  6267. else if((Charger.Alarm_Code & ALARM_UNDER_VOLTAGE))
  6268. {
  6269. tx[4] = 0x01;
  6270. tx[5] = 0x22;
  6271. tx[6] = 0x03;
  6272. }
  6273. else if((Charger.Alarm_Code & ALARM_OVER_CURRENT))
  6274. {
  6275. tx[4] = 0x01;
  6276. tx[5] = 0x21;
  6277. tx[6] = 0x16;
  6278. }
  6279. else if((Charger.Alarm_Code & ALARM_OVER_TEMPERATURE))
  6280. {
  6281. tx[4] = 0x01;
  6282. tx[5] = 0x22;
  6283. tx[6] = 0x23;
  6284. }
  6285. else if((Charger.Alarm_Code & ALARM_GROUND_FAIL))
  6286. {
  6287. tx[4] = 0x01;
  6288. tx[5] = 0x22;
  6289. tx[6] = 0x56;
  6290. }
  6291. else if((Charger.Alarm_Code & ALARM_CP_ERROR))
  6292. {
  6293. tx[4] = 0x02;
  6294. tx[5] = 0x37;
  6295. tx[6] = 0x03;
  6296. }
  6297. else if((Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC)||(Charger.Alarm_Code & ALARM_CURRENT_LEAK_DC))
  6298. {
  6299. tx[4] = 0x01;
  6300. tx[5] = 0x22;
  6301. tx[6] = 0x33;
  6302. }
  6303. else if((Charger.Alarm_Code & ALARM_MCU_TESTFAIL))
  6304. {
  6305. tx[4] = 0x01;
  6306. tx[5] = 0x22;
  6307. tx[6] = 0x57;
  6308. }
  6309. else if((Charger.Alarm_Code & ALARM_EMERGENCY_STOP))
  6310. {
  6311. tx[4] = 0x01;
  6312. tx[5] = 0x22;
  6313. tx[6] = 0x51;
  6314. }
  6315. else if((Charger.Alarm_Code & ALARM_LEAK_MODULE_FAIL))
  6316. {
  6317. tx[4] = 0x01;
  6318. tx[5] = 0x10;
  6319. tx[6] = 0x04;
  6320. }
  6321. else if((Charger.Alarm_Code & ALARM_RELAY_STATUS))
  6322. {
  6323. tx[4] = 0x01;
  6324. tx[5] = 0x22;
  6325. tx[6] = 0x58;
  6326. }
  6327. DEBUG_WARN("EVSE in alarm state, can not stop charging.\r\n");
  6328. }
  6329. else
  6330. {
  6331. if((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))
  6332. {
  6333. if((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))
  6334. {
  6335. Charger.ble.isRequestOff = ON;
  6336. tx[3] = 0x01;
  6337. DEBUG_INFO("Stop OK.\r\n");
  6338. }
  6339. else
  6340. {
  6341. tx[3] = 0x00;
  6342. DEBUG_WARN("EVSE not in CHARGING.\r\n");
  6343. }
  6344. }
  6345. else
  6346. {
  6347. tx[3] = 0x00;
  6348. DEBUG_WARN("Login first\r\n");
  6349. }
  6350. tx[4] = 0x00;
  6351. tx[5] = 0x00;
  6352. tx[6] = 0x00;
  6353. }
  6354. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6355. {
  6356. chksum ^= tx[2 + idx];
  6357. }
  6358. tx[7] = chksum;
  6359. break;
  6360. case BLE_PROTOCOL_MESSAGE_STATUS_QUERY:
  6361. DEBUG_INFO("BLE device query EVSE status request.\r\n");
  6362. tx_len = 0x13;
  6363. tx[0] = (tx_len>>8)&0xff;
  6364. tx[1] = (tx_len&0xff);
  6365. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6366. tx[3] = (((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))?Charger.Mode:0x00);
  6367. 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);
  6368. 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);
  6369. 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);
  6370. 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);
  6371. 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);
  6372. 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);
  6373. tx[10] = (((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))?(((Charger.cycle_info.Duration/60000)>>0) & 0xff):0x00);
  6374. tx[11] = (((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))?(((Charger.cycle_info.Duration/60000)>>8) & 0xff):0x00);
  6375. if((Charger.Alarm_Code>0) && ((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER)))
  6376. {
  6377. if((Charger.Alarm_Code & ALARM_OVER_VOLTAGE))
  6378. {
  6379. tx[12] = 0x01;
  6380. tx[13] = 0x22;
  6381. tx[14] = 0x00;
  6382. }
  6383. else if((Charger.Alarm_Code & ALARM_UNDER_VOLTAGE))
  6384. {
  6385. tx[12] = 0x01;
  6386. tx[13] = 0x22;
  6387. tx[14] = 0x03;
  6388. }
  6389. else if((Charger.Alarm_Code & ALARM_OVER_CURRENT))
  6390. {
  6391. tx[12] = 0x01;
  6392. tx[13] = 0x21;
  6393. tx[14] = 0x16;
  6394. }
  6395. else if((Charger.Alarm_Code & ALARM_OVER_TEMPERATURE))
  6396. {
  6397. tx[12] = 0x01;
  6398. tx[13] = 0x22;
  6399. tx[14] = 0x23;
  6400. }
  6401. else if((Charger.Alarm_Code & ALARM_GROUND_FAIL))
  6402. {
  6403. tx[12] = 0x01;
  6404. tx[13] = 0x22;
  6405. tx[14] = 0x56;
  6406. }
  6407. else if((Charger.Alarm_Code & ALARM_CP_ERROR))
  6408. {
  6409. tx[12] = 0x02;
  6410. tx[13] = 0x37;
  6411. tx[14] = 0x03;
  6412. }
  6413. else if((Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC)||(Charger.Alarm_Code & ALARM_CURRENT_LEAK_DC))
  6414. {
  6415. tx[12] = 0x01;
  6416. tx[13] = 0x22;
  6417. tx[14] = 0x33;
  6418. }
  6419. else if((Charger.Alarm_Code & ALARM_MCU_TESTFAIL))
  6420. {
  6421. tx[12] = 0x01;
  6422. tx[13] = 0x22;
  6423. tx[14] = 0x57;
  6424. }
  6425. else if((Charger.Alarm_Code & ALARM_EMERGENCY_STOP))
  6426. {
  6427. tx[12] = 0x01;
  6428. tx[13] = 0x22;
  6429. tx[14] = 0x51;
  6430. }
  6431. else if((Charger.Alarm_Code & ALARM_LEAK_MODULE_FAIL))
  6432. {
  6433. tx[12] = 0x01;
  6434. tx[13] = 0x10;
  6435. tx[14] = 0x04;
  6436. }
  6437. else if((Charger.Alarm_Code & ALARM_RELAY_STATUS))
  6438. {
  6439. tx[12] = 0x01;
  6440. tx[13] = 0x22;
  6441. tx[14] = 0x58;
  6442. }
  6443. }
  6444. else
  6445. {
  6446. tx[12] = 0x00;
  6447. tx[13] = 0x00;
  6448. tx[14] = 0x00;
  6449. }
  6450. tx[15] = Charger.memory.EVSE_Config.data.item.reservation.mode;
  6451. tx[16] = Charger.memory.EVSE_Config.data.item.reservation.hour;
  6452. tx[17] = Charger.memory.EVSE_Config.data.item.reservation.min;
  6453. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6454. {
  6455. chksum ^= tx[2 + idx];
  6456. }
  6457. tx[18] = chksum;
  6458. break;
  6459. case BLE_PROTOCOL_MESSAGE_RESERVATION:
  6460. switch(UART_BLE_rx_buffer[7])
  6461. {
  6462. case RESERVATION_DISABLE:
  6463. DEBUG_INFO("BLE device reservation request disable.\r\n");
  6464. break;
  6465. case RESERVATION_ONCE:
  6466. DEBUG_INFO("BLE device reservation request once on %d:%d.\r\n", UART_BLE_rx_buffer[8], UART_BLE_rx_buffer[9]);
  6467. break;
  6468. case RESERVATION_ALWAYS:
  6469. DEBUG_INFO("BLE device reservation request always on %d:%d.\r\n", UART_BLE_rx_buffer[8], UART_BLE_rx_buffer[9]);
  6470. break;
  6471. }
  6472. tx_len = 0x05;
  6473. tx[0] = (tx_len>>8)&0xff;
  6474. tx[1] = (tx_len&0xff);
  6475. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6476. Charger.memory.EVSE_Config.data.item.reservation.mode = UART_BLE_rx_buffer[7];
  6477. Charger.memory.EVSE_Config.data.item.reservation.hour = UART_BLE_rx_buffer[8];
  6478. Charger.memory.EVSE_Config.data.item.reservation.min = UART_BLE_rx_buffer[9];
  6479. Charger.memory.EVSE_Config.op_bits.update = ON;
  6480. tx[3] = 0x01;
  6481. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6482. {
  6483. chksum ^= tx[2 + idx];
  6484. }
  6485. tx[4] = chksum;
  6486. break;
  6487. case BLE_PROTOCOL_MESSAGE_TIME_QUERY:
  6488. DEBUG_INFO("BLE device query date time request.\r\n");
  6489. tx_len = 0x08;
  6490. tx[0] = (tx_len>>8)&0xff;
  6491. tx[1] = (tx_len&0xff);
  6492. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6493. struct tm gm_date;
  6494. gm_date.tm_year = Charger.memory.EVSE_Config.data.item.SystemDateTime.year-1900;
  6495. gm_date.tm_mon = Charger.memory.EVSE_Config.data.item.SystemDateTime.month-1;
  6496. gm_date.tm_mday = Charger.memory.EVSE_Config.data.item.SystemDateTime.day;
  6497. gm_date.tm_hour = Charger.memory.EVSE_Config.data.item.SystemDateTime.hour;
  6498. gm_date.tm_min = Charger.memory.EVSE_Config.data.item.SystemDateTime.min;
  6499. gm_date.tm_sec = Charger.memory.EVSE_Config.data.item.SystemDateTime.sec;
  6500. tmp = mktime(&gm_date);
  6501. tx[3] = ((tmp >> 0)&0xff);
  6502. tx[4] = ((tmp >> 8)&0xff);
  6503. tx[5] = ((tmp >> 16)&0xff);
  6504. tx[6] = ((tmp >> 24)&0xff);
  6505. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6506. {
  6507. chksum ^= tx[2 + idx];
  6508. }
  6509. tx[7] = chksum;
  6510. break;
  6511. case BLE_PROTOCOL_MESSAGE_TIME_CONFIG:
  6512. DEBUG_INFO("BLE device config date time.\r\n");
  6513. tx_len = 0x09;
  6514. tx[0] = (tx_len>>8)&0xff;
  6515. tx[1] = (tx_len&0xff);
  6516. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6517. if((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))
  6518. {
  6519. struct tm *sm_date;
  6520. 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);
  6521. sm_date = gmtime(&tmp) ;
  6522. setTime.Hours = sm_date->tm_hour;
  6523. setTime.Minutes = sm_date->tm_min;
  6524. setTime.Seconds = sm_date->tm_sec;
  6525. setTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  6526. setTime.StoreOperation = RTC_STOREOPERATION_RESET;
  6527. if (HAL_RTC_SetTime(&hrtc, &setTime, RTC_FORMAT_BIN) != HAL_OK)
  6528. {
  6529. Error_Handler();
  6530. }
  6531. setDate.Year = (sm_date->tm_year + 1900)-2000;
  6532. setDate.Month = sm_date->tm_mon + 1;
  6533. setDate.Date = sm_date->tm_mday;
  6534. if (HAL_RTC_SetDate(&hrtc, &setDate, RTC_FORMAT_BIN) != HAL_OK)
  6535. {
  6536. Error_Handler();
  6537. }
  6538. tx[3] = 0x01;
  6539. DEBUG_INFO("RTC config OK.\r\n");
  6540. }
  6541. else
  6542. {
  6543. tx[3] = 0x00;
  6544. DEBUG_WARN("User con not config RTC.\r\n");
  6545. }
  6546. tx[4] = UART_BLE_rx_buffer[7];
  6547. tx[5] = UART_BLE_rx_buffer[8];
  6548. tx[6] = UART_BLE_rx_buffer[9];
  6549. tx[7] = UART_BLE_rx_buffer[10];
  6550. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6551. {
  6552. chksum ^= tx[2 + idx];
  6553. }
  6554. tx[8] = chksum;
  6555. break;
  6556. case BLE_PROTOCOL_MESSAGE_LED_QUERY:
  6557. DEBUG_INFO("BLE device query led setting.\r\n");
  6558. tx_len = 0x08;
  6559. tx[0] = (tx_len>>8)&0xff;
  6560. tx[1] = (tx_len&0xff);
  6561. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6562. /*
  6563. TODO: Query led result here
  6564. */
  6565. if((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))
  6566. {
  6567. tx[3] = 0xff;
  6568. tx[4] = 0xff;
  6569. tx[5] = 0xff;
  6570. tx[6] = 0xff;
  6571. }
  6572. else
  6573. {
  6574. tx[3] = 0x00;
  6575. tx[4] = 0x00;
  6576. tx[5] = 0x00;
  6577. tx[6] = 0x00;
  6578. }
  6579. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6580. {
  6581. chksum ^= tx[2 + idx];
  6582. }
  6583. tx[7] = chksum;
  6584. break;
  6585. case BLE_PROTOCOL_MESSAGE_LED_CONFIG:
  6586. DEBUG_INFO("BLE device config led setting.\r\n");
  6587. tx_len = 0x09;
  6588. tx[0] = (tx_len>>8)&0xff;
  6589. tx[1] = (tx_len&0xff);
  6590. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6591. /*
  6592. TODO: Config led result here
  6593. */
  6594. if((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))
  6595. tx[3] = 0x01;
  6596. else
  6597. tx[3] = 0x00;
  6598. tx[4] = UART_BLE_rx_buffer[7];
  6599. tx[5] = UART_BLE_rx_buffer[8];
  6600. tx[6] = UART_BLE_rx_buffer[9];
  6601. tx[7] = UART_BLE_rx_buffer[10];
  6602. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6603. {
  6604. chksum ^= tx[2 + idx];
  6605. }
  6606. tx[8] = chksum;
  6607. break;
  6608. case BLE_PROTOCOL_MESSAGE_CURRENT_LIMIT_QUERY:
  6609. DEBUG_INFO("BLE device query current limit request.\r\n");
  6610. tx_len = 0x05;
  6611. tx[0] = (tx_len>>8)&0xff;
  6612. tx[1] = (tx_len&0xff);
  6613. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6614. if((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))
  6615. {
  6616. tx[3] = Charger.memory.EVSE_Config.data.item.MaxChargingCurrent;
  6617. DEBUG_INFO("Query current limit: %02dA.\r\n", Charger.memory.EVSE_Config.data.item.MaxChargingCurrent);
  6618. }
  6619. else
  6620. {
  6621. tx[3] = 0x00;
  6622. DEBUG_WARN("Please login first, before query current limit.\r\n");
  6623. }
  6624. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6625. {
  6626. chksum ^= tx[2 + idx];
  6627. }
  6628. tx[8] = chksum;
  6629. break;
  6630. case BLE_PROTOCOL_MESSAGE_CURRENT_LIMIT_CONFIG:
  6631. DEBUG_INFO("BLE device config current limit.\r\n");
  6632. tx_len = 0x06;
  6633. tx[0] = (tx_len>>8)&0xff;
  6634. tx[1] = (tx_len&0xff);
  6635. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6636. if((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))
  6637. {
  6638. if(UART_BLE_rx_buffer[7]>Charger.maxRatingCurrent)
  6639. {
  6640. tx[3] = 0x02;
  6641. Charger.memory.EVSE_Config.data.item.MaxChargingCurrent = Charger.maxRatingCurrent;
  6642. DEBUG_INFO("Setting value over max rating value, set as max rating .\r\n");
  6643. }
  6644. else
  6645. {
  6646. tx[3] = 0x01;
  6647. Charger.memory.EVSE_Config.data.item.MaxChargingCurrent = UART_BLE_rx_buffer[7];
  6648. }
  6649. DEBUG_INFO("Config current limit: %02dA\r\n", Charger.memory.EVSE_Config.data.item.MaxChargingCurrent);
  6650. }
  6651. else
  6652. {
  6653. tx[3] = 0x00;
  6654. DEBUG_WARN("Please login first, before setting current limit.\r\n");
  6655. }
  6656. tx[4] = UART_BLE_rx_buffer[7];
  6657. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6658. {
  6659. chksum ^= tx[2 + idx];
  6660. }
  6661. tx[5] = chksum;
  6662. break;
  6663. case BLE_PROTOCOL_MESSAGE_FIRMWARE_VERSION_QUERY:
  6664. DEBUG_INFO("BLE device query firmware version.\r\n");
  6665. tx_len = 4 + strlen(Charger.Ver_FW);
  6666. tx[0] = (tx_len>>8)&0xff;
  6667. tx[1] = (tx_len&0xff);
  6668. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6669. for(uint16_t idx=0;idx<strlen(Charger.Ver_FW);idx++)
  6670. {
  6671. if((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))
  6672. tx[3+idx] = Charger.Ver_FW[idx];
  6673. else
  6674. tx[3+idx] = 0x00;
  6675. }
  6676. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6677. {
  6678. chksum ^= tx[2 + idx];
  6679. }
  6680. tx[3+strlen(Charger.Ver_FW)] = chksum;
  6681. break;
  6682. case BLE_PROTOCOL_MESSAGE_FIRMWARE_UPDATE_REQ:
  6683. tx_len = 5;
  6684. tx[0] = 0x00;
  6685. tx[1] = 0x05;
  6686. tx[2] = UART_BLE_rx_buffer[2] + 0x80;;
  6687. if(Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT)
  6688. {
  6689. 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);
  6690. flashdestination = NEW_CODE_ADDRESS;
  6691. if(FLASH_If_Erase(NEW_CODE_ADDRESS, 3)== FLASHIF_OK)
  6692. {
  6693. DEBUG_INFO("Firmware transfer start, earase flash success....\n\r");
  6694. tx[3] = 0x01;
  6695. }
  6696. else
  6697. {
  6698. DEBUG_INFO("Firmware transfer start, earase flash fail....\n\r");
  6699. tx[3] = 0x00;
  6700. }
  6701. }
  6702. else
  6703. {
  6704. tx[3] = 0x00;
  6705. DEBUG_WARN("User can not upgrade start.\r\n");
  6706. }
  6707. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6708. {
  6709. chksum ^= tx[2 + idx];
  6710. }
  6711. tx[4] = chksum;
  6712. break;
  6713. case BLE_PROTOCOL_MESSAGE_FIRMWARE_TRANS_REQ:
  6714. tx_len = 5;
  6715. tx[0] = 0x00;
  6716. tx[1] = 0x05;
  6717. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6718. if(Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT)
  6719. {
  6720. 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));
  6721. 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)
  6722. {
  6723. 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));
  6724. tx[3] = 0x01;
  6725. }
  6726. else
  6727. {
  6728. 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));
  6729. tx[3] = 0x00;
  6730. }
  6731. }
  6732. else
  6733. {
  6734. tx[3] = 0x00;
  6735. DEBUG_WARN("User can not upgrade transfer.\r\n");
  6736. }
  6737. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6738. {
  6739. chksum ^= tx[2 + idx];
  6740. }
  6741. tx[4] = chksum;
  6742. break;
  6743. case BLE_PROTOCOL_MESSAGE_FIRMWARE_END_REQ:
  6744. tx_len = 5;
  6745. tx[0] = 0x00;
  6746. tx[1] = 0x05;
  6747. tx[2] = UART_BLE_rx_buffer[2] + 0x80;;
  6748. if(Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT)
  6749. {
  6750. flash = NEW_CODE_ADDRESS;
  6751. checksum = HAL_CRC_Calculate(&hcrc, (uint32_t *)flash, ((FLASH_AP_LENGTH-4)>>2));
  6752. flash = ((uint32_t)(NEW_CODE_ADDRESS+FLASH_AP_LENGTH-4));
  6753. DEBUG_INFO("Firmware transfer end, AP CRC checksum, flash: 0x%x, 0x%x\r\n", checksum, *((uint32_t *)flash) );
  6754. if(checksum == *((uint32_t *)flash))
  6755. {
  6756. if (FLASH_If_Write(UPGRADE_REQ_ADDRESS, (uint32_t *)&endFlag[0], 1) == FLASHIF_OK)
  6757. {
  6758. DEBUG_INFO("Firmware Confirm Tag write ok..\n\r");
  6759. tx[3] = 0x01;
  6760. tx[4] = 0xB5;
  6761. HAL_UART_Transmit(&IAP_USART, (uint8_t *)tx, sizeof(tx), 0xffff);
  6762. osDelay(1000);
  6763. NVIC_SystemReset();
  6764. }
  6765. else
  6766. {
  6767. DEBUG_INFO("Firmware Confirm Tag write fail...\n\r");
  6768. tx[3] = 0x00;
  6769. }
  6770. }
  6771. else
  6772. {
  6773. DEBUG_INFO("Firmware checksum compare fail...\n\r");
  6774. tx[3] = 0x00;
  6775. }
  6776. }
  6777. else
  6778. {
  6779. tx[3] = 0x00;
  6780. DEBUG_WARN("User can not upgrade stop.\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[4] = chksum;
  6787. break;
  6788. default:
  6789. /*
  6790. TODO: Unknow command response here
  6791. */
  6792. tx_len = 5;
  6793. tx[0] = 0x00;
  6794. tx[1] = 0x05;
  6795. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6796. tx[3] = 0x00;
  6797. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6798. {
  6799. chksum ^= tx[2 + idx];
  6800. }
  6801. tx[4] = chksum;
  6802. DEBUG_ERROR("BLE unknow command.\r\n");
  6803. break;
  6804. }
  6805. }
  6806. else
  6807. {
  6808. /*
  6809. TODO: Protocol check sum error response here
  6810. */
  6811. tx_len = 5;
  6812. tx[0] = 0x00;
  6813. tx[1] = 0x05;
  6814. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6815. tx[3] = 0x00;
  6816. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6817. {
  6818. chksum ^= tx[2 + idx];
  6819. }
  6820. tx[4] = chksum;
  6821. DEBUG_ERROR("BLE check sum validation error.\r\n");
  6822. }
  6823. HAL_UART_Transmit(&BLE_USART, (uint8_t *)tx, tx_len, 0xffff);
  6824. }
  6825. UART_BLE_rx_len=0;
  6826. UART_BLE_recv_end_flag = OFF;
  6827. }
  6828. HAL_UART_Receive_DMA(&BLE_USART, (uint8_t*)UART_BLE_rx_buffer, UART_BUFFER_SIZE);
  6829. osDelay(1);
  6830. }
  6831. /* USER CODE END StartBleTask */
  6832. }
  6833. /* USER CODE BEGIN Header_StartMemoryTask */
  6834. /**
  6835. * @brief Function implementing the memoryTask thread.
  6836. * @param argument: Not used
  6837. * @retval None
  6838. */
  6839. /* USER CODE END Header_StartMemoryTask */
  6840. void StartMemoryTask(void const * argument)
  6841. {
  6842. /* USER CODE BEGIN StartMemoryTask */
  6843. //mmemory
  6844. //uint8_t tmpBuf[64];
  6845. uint8_t isSuccess;
  6846. uint8_t ID[2];
  6847. BSP_W25Qx_Init();
  6848. BSP_W25Qx_Read_ID(ID);
  6849. DEBUG_INFO("W25Qxxx ID is : 0x%02X 0x%02X \r\n",ID[0],ID[1]);
  6850. Charger.memory.EVSE_Config.op_bits.read = ON;
  6851. Charger.memory.coldLoadPickUp.op_bits.read = ON;
  6852. Charger.memory.whiteList.op_bits.read = ON;
  6853. Charger.memory.hisAlarm.op_bits.read = ON;
  6854. Charger.memory.hisCharging.op_bits.read = ON;
  6855. /*
  6856. Charger.memory.EVSE_Config.op_bits.clear = ON ;
  6857. Charger.memory.EVSE_Config.op_bits.clear = ON;
  6858. Charger.memory.coldLoadPickUp.op_bits.clear = ON;
  6859. Charger.memory.whiteList.op_bits.clear = ON;
  6860. Charger.memory.hisAlarm.op_bits.clear = ON;
  6861. Charger.memory.hisCharging.op_bits.clear = ON;
  6862. */
  6863. /* Infinite loop */
  6864. for(;;)
  6865. {
  6866. /*
  6867. EVSE config operation
  6868. */
  6869. if(Charger.memory.EVSE_Config.op_bits.read)
  6870. {
  6871. osDelay(100);
  6872. // Read data from block
  6873. if(BSP_W25Qx_Read(Charger.memory.EVSE_Config.data.value, MEM_ADDR_EVSE_CONFIG, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  6874. {
  6875. #ifdef FUNC_FLASH_RESTORE_CONFIG_WHEN_PRIMARY_LOSS
  6876. #ifdef FUNC_FLASH_CONFIG_APPEND_CRC
  6877. u32 crc32 = HAL_CRC_Calculate(&hcrc, (u32*)Charger.memory.EVSE_Config.data.verify.buf, ((W25Q16FV_BLOCK_SIZE-4)>>2));
  6878. if ((Charger.memory.EVSE_Config.data.verify.crc32 == crc32 || Charger.memory.EVSE_Config.data.verify.crc32 == HTK_U32_MAX))
  6879. #else
  6880. if (1)
  6881. #endif
  6882. {
  6883. if(BSP_W25Qx_Erase_Block(MEM_ADDR_BACKUP_EVSE_CONFIG) == W25Qx_OK)
  6884. {
  6885. u8 isSuccess = PASS;
  6886. // Write data to block
  6887. for(uint16_t idx=0;idx<((W25Q16FV_BLOCK_SIZE)>>8);idx++)
  6888. {
  6889. 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)
  6890. {
  6891. isSuccess = FAIL;
  6892. }
  6893. }
  6894. if(isSuccess)
  6895. DEBUG_INFO("Backup MEM_ADDR_BACKUP_EVSE_CONFIG success.\r\n");
  6896. else
  6897. DEBUG_WARN("Backup MEM_ADDR_BACKUP_EVSE_CONFIG fail.\r\n");
  6898. }
  6899. else
  6900. {
  6901. DEBUG_INFO("Backup MEM_ADDR_BACKUP_EVSE_CONFIG block(4k byte) fail.\r\n");
  6902. }
  6903. }
  6904. else
  6905. {
  6906. XP("[FLASH] Primary config area data loss => restore from backup area *****\r\n");
  6907. // Restore data from backup block
  6908. if(BSP_W25Qx_Read(Charger.memory.EVSE_Config.data.value, MEM_ADDR_BACKUP_EVSE_CONFIG, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  6909. {
  6910. DEBUG_INFO("Restore MEM_ADDR_BACKUP_EVSE_CONFIG block(4k byte) pass.\r\n");
  6911. }
  6912. else
  6913. {
  6914. DEBUG_INFO("Restore MEM_ADDR_BACKUP_EVSE_CONFIG block(4k byte) fail.\r\n");
  6915. }
  6916. }
  6917. #endif //FUNC_FLASH_RESTORE_CONFIG_WHEN_PRIMARY_LOSS
  6918. #ifdef MODIFY_UNDEFINED_MODEL_NAME_AND_SN_ISSUE
  6919. if (Charger.memory.EVSE_Config.data.item.ModelName[0] == 0xFF ||
  6920. Charger.memory.EVSE_Config.data.item.SerialNumber[0] == 0xFF)
  6921. {
  6922. Charger.memory.EVSE_Config.data.item.ModelName[MODEL_NAME_LEN - 1] = '\0';
  6923. Charger.memory.EVSE_Config.data.item.SerialNumber[SERIAL_NUM_LEN - 1] = '\0';
  6924. Charger.memory.EVSE_Config.op_bits.update = ON;
  6925. }
  6926. #endif //MODIFY_UNDEFINED_MODEL_NAME_AND_SN_ISSUE
  6927. Charger.memory.EVSE_Config.data.item.isDispDebug = ON;
  6928. 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);
  6929. //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)));
  6930. //memcpy(&Charger.memory.EVSE_Config.data.item.bleConfig.puk[0], &tmpBuf[0], ARRAY_SIZE(Charger.memory.EVSE_Config.data.item.bleConfig.puk));
  6931. //DEBUG_INFO("BLE PUK: %s\r\n", Charger.memory.EVSE_Config.data.item.bleConfig.puk);
  6932. srand(Charger.memory.EVSE_Config.data.item.bleConfig.puk[0] | (Charger.memory.EVSE_Config.data.item.bleConfig.puk[1]<<8));
  6933. setTime.Hours = Charger.memory.EVSE_Config.data.item.SystemDateTime.hour;
  6934. setTime.Minutes = Charger.memory.EVSE_Config.data.item.SystemDateTime.min;
  6935. setTime.Seconds = Charger.memory.EVSE_Config.data.item.SystemDateTime.sec;
  6936. setTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  6937. setTime.StoreOperation = RTC_STOREOPERATION_RESET;
  6938. if (HAL_RTC_SetTime(&hrtc, &setTime, RTC_FORMAT_BIN) != HAL_OK)
  6939. {
  6940. Error_Handler();
  6941. }
  6942. setDate.Year = Charger.memory.EVSE_Config.data.item.SystemDateTime.year-2000;
  6943. setDate.Month = Charger.memory.EVSE_Config.data.item.SystemDateTime.month;
  6944. setDate.Date = Charger.memory.EVSE_Config.data.item.SystemDateTime.day;
  6945. if (HAL_RTC_SetDate(&hrtc, &setDate, RTC_FORMAT_BIN) != HAL_OK)
  6946. {
  6947. Error_Handler();
  6948. }
  6949. //CCLC_Corr_Gain_Par
  6950. //VT
  6951. #ifdef MODIFY_CORRECTION_CHECK
  6952. if (
  6953. (Charger.memory.EVSE_Config.data.item.Correction_VL1[1][MCU_DATA] > Charger.memory.EVSE_Config.data.item.Correction_VL1[2][MCU_DATA]) &&
  6954. (Charger.memory.EVSE_Config.data.item.Correction_VL1[1][METER_DATA] > Charger.memory.EVSE_Config.data.item.Correction_VL1[2][METER_DATA])
  6955. )
  6956. #else
  6957. if(Charger.memory.EVSE_Config.data.item.Correction_VL1[1][MCU_DATA] > Charger.memory.EVSE_Config.data.item.Correction_VL1[2][MCU_DATA])
  6958. #endif
  6959. {
  6960. 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] ,
  6961. Charger.memory.EVSE_Config.data.item.Correction_VL1[1][MCU_DATA],Charger.memory.EVSE_Config.data.item.Correction_VL1[2][MCU_DATA] ,
  6962. &Charger.coefficient.gain_v[0], &Charger.coefficient.offset_v[0]);
  6963. }
  6964. else
  6965. {
  6966. Charger.coefficient.gain_v[0] = 1;
  6967. Charger.coefficient.offset_v[0] = 0;
  6968. }
  6969. //CT
  6970. #ifdef MODIFY_CORRECTION_CHECK
  6971. if (
  6972. (Charger.memory.EVSE_Config.data.item.Correction_CL1[1][MCU_DATA] > Charger.memory.EVSE_Config.data.item.Correction_CL1[2][MCU_DATA]) &&
  6973. (Charger.memory.EVSE_Config.data.item.Correction_CL1[1][METER_DATA] > Charger.memory.EVSE_Config.data.item.Correction_CL1[2][METER_DATA])
  6974. )
  6975. #else
  6976. if(Charger.memory.EVSE_Config.data.item.Correction_CL1[1][MCU_DATA] > Charger.memory.EVSE_Config.data.item.Correction_CL1[2][MCU_DATA])
  6977. #endif
  6978. {
  6979. 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] ,
  6980. Charger.memory.EVSE_Config.data.item.Correction_CL1[1][MCU_DATA],Charger.memory.EVSE_Config.data.item.Correction_CL1[2][MCU_DATA] ,
  6981. &Charger.coefficient.gain_c[0], &Charger.coefficient.offset_c[0]);
  6982. }
  6983. else
  6984. {
  6985. Charger.coefficient.gain_c[0] = 1;
  6986. Charger.coefficient.offset_c[0] = 0;
  6987. }
  6988. //LEAK
  6989. if(Charger.memory.EVSE_Config.data.item.Correction_Leak[1][MCU_DATA] > Charger.memory.EVSE_Config.data.item.Correction_Leak[2][MCU_DATA])
  6990. {
  6991. 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] ,
  6992. Charger.memory.EVSE_Config.data.item.Correction_Leak[1][MCU_DATA],Charger.memory.EVSE_Config.data.item.Correction_Leak[2][MCU_DATA] ,
  6993. &Charger.coefficient.gain_leak[0], &Charger.coefficient.offset_leak[0]);
  6994. }
  6995. else
  6996. {
  6997. Charger.coefficient.gain_leak[0] = 1;
  6998. Charger.coefficient.offset_leak[0] = 0;
  6999. }
  7000. DEBUG_INFO("Charger.coefficient.gain_leak[0] = %f\r\n",Charger.coefficient.gain_leak[0]);
  7001. DEBUG_INFO("Charger.coefficient.offset_leak[0] = %f\r\n",Charger.coefficient.offset_leak[0]);
  7002. //GMI
  7003. if(Charger.memory.EVSE_Config.data.item.Correction_GMI[1][MCU_DATA] > Charger.memory.EVSE_Config.data.item.Correction_GMI[2][MCU_DATA])
  7004. {
  7005. 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] ,
  7006. Charger.memory.EVSE_Config.data.item.Correction_GMI[1][MCU_DATA],Charger.memory.EVSE_Config.data.item.Correction_GMI[2][MCU_DATA] ,
  7007. &Charger.coefficient.gain_gmi[0], &Charger.coefficient.offset_gmi[0], &Charger.coefficient.offsetisPosOrNeg_gmi[0]);
  7008. }
  7009. else
  7010. {
  7011. Charger.coefficient.gain_gmi[0] = 1;
  7012. Charger.coefficient.offset_gmi[0] = 0;
  7013. }
  7014. //DEBUG_INFO("Charger.coefficient.gain_gmi[0] = %f\r\n",Charger.coefficient.gain_gmi[0]);
  7015. //DEBUG_INFO("Charger.coefficient.offset_gmi[0] = %f\r\n",Charger.coefficient.offset_gmi[0]);
  7016. #ifdef FUNC_SHOW_CORRECTION_DATA
  7017. {
  7018. XP("[CORRECT DATA]======================\r\n");
  7019. XP("<Correction_VL1>\r\n");
  7020. for (int i = 0; i < 4; i++)
  7021. {
  7022. XP("[%d][0] = 0x%04X, [%d][1] = 0x%04X\r\n",
  7023. i, Charger.memory.EVSE_Config.data.item.Correction_VL1[i][0],
  7024. i, Charger.memory.EVSE_Config.data.item.Correction_VL1[i][1]);
  7025. }
  7026. XP("<Correction_VL2>\r\n");
  7027. for (int i = 0; i < 2; i++)
  7028. {
  7029. XP("[%d][0] = 0x%04X, [%d][1] = 0x%04X\r\n",
  7030. i, Charger.memory.EVSE_Config.data.item.Correction_VL2[i][0],
  7031. i, Charger.memory.EVSE_Config.data.item.Correction_VL2[i][1]);
  7032. }
  7033. XP("<Correction_VL3>\r\n");
  7034. for (int i = 0; i < 2; i++)
  7035. {
  7036. XP("[%d][0] = 0x%04X, [%d][1] = 0x%04X\r\n",
  7037. i, Charger.memory.EVSE_Config.data.item.Correction_VL3[i][0],
  7038. i, Charger.memory.EVSE_Config.data.item.Correction_VL3[i][1]);
  7039. }
  7040. XP("<Correction_CL1>\r\n");
  7041. for (int i = 0; i < 4; i++)
  7042. {
  7043. XP("[%d][0] = 0x%04X, [%d][1] = 0x%04X\r\n",
  7044. i, Charger.memory.EVSE_Config.data.item.Correction_CL1[i][0],
  7045. i, Charger.memory.EVSE_Config.data.item.Correction_CL1[i][1]);
  7046. }
  7047. XP("<Correction_CL2>\r\n");
  7048. for (int i = 0; i < 2; i++)
  7049. {
  7050. XP("[%d][0] = 0x%04X, [%d][1] = 0x%04X\r\n",
  7051. i, Charger.memory.EVSE_Config.data.item.Correction_CL2[i][0],
  7052. i, Charger.memory.EVSE_Config.data.item.Correction_CL2[i][1]);
  7053. }
  7054. XP("<Correction_CL3>\r\n");
  7055. for (int i = 0; i < 2; i++)
  7056. {
  7057. XP("[%d][0] = 0x%04X, [%d][1] = 0x%04X\r\n",
  7058. i, Charger.memory.EVSE_Config.data.item.Correction_CL3[i][0],
  7059. i, Charger.memory.EVSE_Config.data.item.Correction_CL3[i][1]);
  7060. }
  7061. XP("<Correction_Leak>\r\n");
  7062. for (int i = 0; i < 4; i++)
  7063. {
  7064. XP("[%d][0] = 0x%04X, [%d][1] = 0x%04X\r\n",
  7065. i, Charger.memory.EVSE_Config.data.item.Correction_Leak[i][0],
  7066. i, Charger.memory.EVSE_Config.data.item.Correction_Leak[i][1]);
  7067. }
  7068. XP("<Correction_GMI>\r\n");
  7069. for (int i = 0; i < 4; i++)
  7070. {
  7071. XP("[%d][0] = 0x%04X, [%d][1] = 0x%04X\r\n",
  7072. i, Charger.memory.EVSE_Config.data.item.Correction_GMI[i][0],
  7073. i, Charger.memory.EVSE_Config.data.item.Correction_GMI[i][1]);
  7074. }
  7075. XP("====================================\r\n");
  7076. }
  7077. #endif //FUNC_SHOW_CORRECTION_DATA
  7078. DEBUG_INFO("Read MEM_ADDR_EVSE_CONFIG block(4k byte) pass.\r\n");
  7079. // Check MCU control mode depend on GPIO state ~again~ , Prevent W25Qx from being empty(0xFF)
  7080. if(HAL_GPIO_ReadPin(IN_ACT_REQ_GPIO_Port, IN_ACT_REQ_Pin) == GPIO_PIN_RESET)
  7081. {
  7082. Charger.memory.EVSE_Config.data.item.MCU_Control_Mode = MCU_CONTROL_MODE_CSU;
  7083. DEBUG_INFO("Check MCU_Control_Mode is CSU in memmeory task\r\n");
  7084. }
  7085. else
  7086. {
  7087. Charger.memory.EVSE_Config.data.item.MCU_Control_Mode = MCU_CONTROL_MODE_NO_CSU;
  7088. if (Charger.memory.EVSE_Config.data.item.AuthMode > AUTH_MODE_FREE)
  7089. {
  7090. Charger.memory.EVSE_Config.data.item.AuthMode = AUTH_MODE_FREE ;
  7091. DEBUG_INFO("AuthMode is defualt AUTH_MODE_FREE \r\n");
  7092. }
  7093. if (Charger.memory.EVSE_Config.data.item.OfflinePolicy > RFID_USER_AUTH_NO_CHARGING)
  7094. {
  7095. Charger.memory.EVSE_Config.data.item.OfflinePolicy = RFID_USER_AUTH_FREE ;
  7096. DEBUG_INFO("OfflinePolicy is defualt RFID_USER_AUTH_FREE \r\n");
  7097. }
  7098. DEBUG_INFO("Check MCU_Control_Mode is NO NO NO CSU in memmeory task\r\n");
  7099. }
  7100. // Clean update & clear flag when model name not configured
  7101. if(strlen(Charger.memory.EVSE_Config.data.item.ModelName)>20)
  7102. {
  7103. Charger.memory.EVSE_Config.op_bits.update = OFF;
  7104. Charger.memory.EVSE_Config.op_bits.clear = OFF;
  7105. Charger.memory.EVSE_Config.op_bits.backup = OFF;
  7106. Charger.memory.EVSE_Config.op_bits.restore = OFF;
  7107. }
  7108. parseVersionInfoFromModelname( );
  7109. Charger.ModelReadisOK = PASS ;
  7110. Charger.memory.EVSE_Config.op_bits.read = OFF;
  7111. }
  7112. else
  7113. {
  7114. DEBUG_INFO("Read MEM_ADDR_EVSE_CONFIG block(4k byte) fail.\r\n");
  7115. }
  7116. }
  7117. if(Charger.memory.EVSE_Config.op_bits.update)
  7118. {
  7119. #ifdef FUNC_AUTO_MODIFY_CCID_MODULE
  7120. Charger.memory.EVSE_Config.op_bits.update = OFF;
  7121. #endif
  7122. isSuccess = PASS;
  7123. #ifdef FUNC_FLASH_CONFIG_APPEND_CRC
  7124. Charger.memory.EVSE_Config.data.verify.crc32 = HAL_CRC_Calculate(&hcrc, (u32*)Charger.memory.EVSE_Config.data.verify.buf, ((W25Q16FV_BLOCK_SIZE-4)>>2));
  7125. #endif
  7126. // Erase block data
  7127. if(BSP_W25Qx_Erase_Block(MEM_ADDR_EVSE_CONFIG) == W25Qx_OK)
  7128. {
  7129. // Write data to block
  7130. for(uint16_t idx=0;idx<((W25Q16FV_BLOCK_SIZE)>>8);idx++)
  7131. {
  7132. 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)
  7133. {
  7134. isSuccess = FAIL;
  7135. }
  7136. }
  7137. if(isSuccess)
  7138. DEBUG_INFO("Update MEM_ADDR_EVSE_CONFIG success.\r\n");
  7139. else
  7140. DEBUG_WARN("Update MEM_ADDR_EVSE_CONFIG fail.\r\n");
  7141. #ifndef FUNC_AUTO_MODIFY_CCID_MODULE
  7142. Charger.memory.EVSE_Config.op_bits.update = OFF;
  7143. #endif
  7144. #ifdef FUNC_FLASH_UPDATE_CONFIG_BOTH_PRIMARY_AND_BACKUP
  7145. Charger.memory.EVSE_Config.op_bits.backup = ON;
  7146. #endif
  7147. }
  7148. else
  7149. {
  7150. DEBUG_INFO("Update MEM_ADDR_EVSE_CONFIG block(4k byte) fail.\r\n");
  7151. }
  7152. }
  7153. if(Charger.memory.EVSE_Config.op_bits.clear)
  7154. {
  7155. // Erase block data
  7156. if(BSP_W25Qx_Erase_Block(MEM_ADDR_EVSE_CONFIG) == W25Qx_OK)
  7157. {
  7158. // Read data from block
  7159. if(BSP_W25Qx_Read(Charger.memory.EVSE_Config.data.value, MEM_ADDR_EVSE_CONFIG, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7160. {
  7161. DEBUG_INFO("Erase MEM_ADDR_EVSE_CONFIG block(4k byte) pass.\r\n");
  7162. Charger.memory.EVSE_Config.op_bits.clear = OFF;
  7163. }
  7164. else
  7165. {
  7166. DEBUG_INFO("Erase MEM_ADDR_EVSE_CONFIG block(4k byte) fail.\r\n");
  7167. }
  7168. }
  7169. else
  7170. {
  7171. DEBUG_INFO("Erase MEM_ADDR_EVSE_CONFIG block(4k byte) fail.\r\n");
  7172. }
  7173. }
  7174. if(Charger.memory.EVSE_Config.op_bits.backup)
  7175. {
  7176. isSuccess = PASS;
  7177. // Erase block data
  7178. if(BSP_W25Qx_Erase_Block(MEM_ADDR_BACKUP_EVSE_CONFIG) == W25Qx_OK)
  7179. {
  7180. // Write data to block
  7181. for(uint16_t idx=0;idx<((W25Q16FV_BLOCK_SIZE)>>8);idx++)
  7182. {
  7183. 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)
  7184. {
  7185. isSuccess = FAIL;
  7186. }
  7187. }
  7188. if(isSuccess)
  7189. DEBUG_INFO("Backup MEM_ADDR_BACKUP_EVSE_CONFIG success.\r\n");
  7190. else
  7191. DEBUG_WARN("Backup MEM_ADDR_BACKUP_EVSE_CONFIG fail.\r\n");
  7192. Charger.memory.EVSE_Config.op_bits.backup = OFF;
  7193. #ifndef FUNC_FLASH_UPDATE_CONFIG_BOTH_PRIMARY_AND_BACKUP
  7194. //Charger.memory.EVSE_Config.op_bits.clear = ON;
  7195. #endif
  7196. }
  7197. else
  7198. {
  7199. DEBUG_INFO("Backup MEM_ADDR_BACKUP_EVSE_CONFIG block(4k byte) fail.\r\n");
  7200. }
  7201. }
  7202. if(Charger.memory.EVSE_Config.op_bits.restore)
  7203. {
  7204. // Restore data from backup block
  7205. if(BSP_W25Qx_Read(Charger.memory.EVSE_Config.data.value, MEM_ADDR_BACKUP_EVSE_CONFIG, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7206. {
  7207. DEBUG_INFO("Restore MEM_ADDR_BACKUP_EVSE_CONFIG block(4k byte) pass.\r\n");
  7208. Charger.memory.EVSE_Config.op_bits.restore = OFF;
  7209. }
  7210. else
  7211. {
  7212. DEBUG_INFO("Restore MEM_ADDR_BACKUP_EVSE_CONFIG block(4k byte) fail.\r\n");
  7213. }
  7214. }
  7215. if((HAL_GPIO_ReadPin(IN_MEM_Erase_GPIO_Port, IN_MEM_Erase_Pin) == GPIO_PIN_RESET) && (Charger.Mode == MODE_IDLE) )
  7216. {
  7217. //prevent idiot to pree this button too long to cause watchdog reset
  7218. HAL_IWDG_Refresh(&hiwdg);
  7219. if(Charger.counter.RST_CFG>3000)
  7220. {
  7221. // Reset EVSE configuration to factory default value
  7222. Charger.memory.EVSE_Config.data.item.bleConfig.isGenPin = OFF;
  7223. Charger.memory.EVSE_Config.data.item.bleConfig.isRegPuk = OFF;
  7224. Charger.memory.EVSE_Config.data.item.AuthMode = AUTH_MODE_OCPP;
  7225. Charger.memory.EVSE_Config.data.item.RFID_SN_Endian = RFID_ENDIAN_LITTLE;
  7226. //Charger.memory.EVSE_Config.data.item.isAuthEnable = OFF;
  7227. Charger.memory.EVSE_Config.data.item.OfflinePolicy = RFID_USER_AUTH_FREE ;
  7228. //Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative = 0;
  7229. Charger.memory.EVSE_Config.data.item.reservation.mode = RESERVATION_DISABLE;
  7230. Charger.memory.EVSE_Config.op_bits.update = ON;
  7231. // Reset cold load pick up to factory default value
  7232. Charger.memory.coldLoadPickUp.data.item.isEnable = OFF;
  7233. Charger.memory.coldLoadPickUp.op_bits.update = ON;
  7234. // Reset white list to factory default value
  7235. for(uint8_t idx=0;idx<20;idx++)
  7236. {
  7237. Charger.memory.whiteList.data.item[idx].listType = 0xff;
  7238. Charger.memory.whiteList.data.item[idx].isReg = OFF;
  7239. memset(&Charger.memory.whiteList.data.item[idx].pin[0], 0x00, 6);
  7240. memset(&Charger.memory.whiteList.data.item[idx].listID[0], 0x00, 6);
  7241. }
  7242. Charger.memory.whiteList.op_bits.update = ON;
  7243. //restore led
  7244. setLedMotion(LED_ACTION_RESTORE_SETTING) ;
  7245. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  7246. setChargerMode(MODE_IDLE);
  7247. osDelay(1000);
  7248. }
  7249. else
  7250. Charger.counter.RST_CFG++;
  7251. }
  7252. else
  7253. Charger.counter.RST_CFG = 0;
  7254. /*
  7255. Cold load pick up operation
  7256. */
  7257. if(Charger.memory.coldLoadPickUp.op_bits.read)
  7258. {
  7259. osDelay(100);
  7260. // Read data from block
  7261. if(BSP_W25Qx_Read(Charger.memory.coldLoadPickUp.data.value, MEM_ADDR_COLD_LOAD_PICKUP, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7262. {
  7263. DEBUG_INFO("Read MEM_ADDR_COLD_LOAD_PICKUP block(4k byte) pass.\r\n");
  7264. Charger.memory.coldLoadPickUp.op_bits.read = OFF;
  7265. }
  7266. else
  7267. {
  7268. DEBUG_INFO("Read MEM_ADDR_COLD_LOAD_PICKUP block(4k byte) fail.\r\n");
  7269. }
  7270. }
  7271. if(Charger.memory.coldLoadPickUp.op_bits.update)
  7272. {
  7273. isSuccess = PASS;
  7274. // Erase block data
  7275. if(BSP_W25Qx_Erase_Block(MEM_ADDR_COLD_LOAD_PICKUP) == W25Qx_OK)
  7276. {
  7277. // Write data to block
  7278. for(uint16_t idx=0;idx<((W25Q16FV_BLOCK_SIZE)>>8);idx++)
  7279. {
  7280. 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)
  7281. {
  7282. isSuccess = FAIL;
  7283. }
  7284. }
  7285. if(isSuccess)
  7286. DEBUG_INFO("Update MEM_ADDR_COLD_LOAD_PICKUP success.\r\n");
  7287. else
  7288. DEBUG_WARN("Update MEM_ADDR_COLD_LOAD_PICKUP fail.\r\n");
  7289. Charger.memory.coldLoadPickUp.op_bits.update = OFF;
  7290. }
  7291. else
  7292. {
  7293. DEBUG_INFO("Update MEM_ADDR_COLD_LOAD_PICKUP block(4k byte) fail.\r\n");
  7294. }
  7295. }
  7296. if(Charger.memory.coldLoadPickUp.op_bits.clear)
  7297. {
  7298. // Erase block data
  7299. if(BSP_W25Qx_Erase_Block(MEM_ADDR_COLD_LOAD_PICKUP) == W25Qx_OK)
  7300. {
  7301. // Read data from block
  7302. if(BSP_W25Qx_Read(Charger.memory.coldLoadPickUp.data.value, MEM_ADDR_COLD_LOAD_PICKUP, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7303. {
  7304. DEBUG_INFO("Erase MEM_ADDR_COLD_LOAD_PICKUP block(4k byte) pass.\r\n");
  7305. Charger.memory.coldLoadPickUp.op_bits.clear = OFF;
  7306. }
  7307. else
  7308. {
  7309. DEBUG_INFO("Erase MEM_ADDR_COLD_LOAD_PICKUP block(4k byte) fail.\r\n");
  7310. }
  7311. }
  7312. else
  7313. {
  7314. DEBUG_INFO("Erase MEM_ADDR_COLD_LOAD_PICKUP block(4k byte) fail.\r\n");
  7315. }
  7316. }
  7317. if(Charger.memory.coldLoadPickUp.op_bits.backup)
  7318. {
  7319. isSuccess = PASS;
  7320. // Erase block data
  7321. if(BSP_W25Qx_Erase_Block(MEM_ADDR_BACKUP_COLD_LOAD_PICKUP) == W25Qx_OK)
  7322. {
  7323. // Write data to block
  7324. for(uint16_t idx=0;idx<((W25Q16FV_BLOCK_SIZE)>>8);idx++)
  7325. {
  7326. 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)
  7327. {
  7328. isSuccess = FAIL;
  7329. }
  7330. }
  7331. if(isSuccess)
  7332. DEBUG_INFO("Backup MEM_ADDR_BACKUP_COLD_LOAD_PICKUP success.\r\n");
  7333. else
  7334. DEBUG_WARN("Backup MEM_ADDR_BACKUP_COLD_LOAD_PICKUP fail.\r\n");
  7335. Charger.memory.coldLoadPickUp.op_bits.backup = OFF;
  7336. Charger.memory.coldLoadPickUp.op_bits.clear = ON;
  7337. }
  7338. else
  7339. {
  7340. DEBUG_INFO("Backup MEM_ADDR_BACKUP_COLD_LOAD_PICKUP block(4k byte) fail.\r\n");
  7341. }
  7342. }
  7343. if(Charger.memory.coldLoadPickUp.op_bits.restore)
  7344. {
  7345. // Restore data from backup block
  7346. if(BSP_W25Qx_Read(Charger.memory.coldLoadPickUp.data.value, MEM_ADDR_BACKUP_COLD_LOAD_PICKUP, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7347. {
  7348. DEBUG_INFO("Restore MEM_ADDR_BACKUP_COLD_LOAD_PICKUP block(4k byte) pass.\r\n");
  7349. Charger.memory.coldLoadPickUp.op_bits.restore = OFF;
  7350. }
  7351. else
  7352. {
  7353. DEBUG_INFO("Restore MEM_ADDR_BACKUP_COLD_LOAD_PICKUP block(4k byte) fail.\r\n");
  7354. }
  7355. }
  7356. /*
  7357. White list operation
  7358. */
  7359. if(Charger.memory.whiteList.op_bits.read)
  7360. {
  7361. osDelay(100);
  7362. // Read data from block
  7363. if(BSP_W25Qx_Read(Charger.memory.whiteList.data.value, MEM_ADDR_WHITE_LIST, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7364. {
  7365. DEBUG_INFO("Read MEM_ADDR_WHITE_LIST block(4k byte) pass.\r\n");
  7366. Charger.memory.whiteList.op_bits.read = OFF;
  7367. }
  7368. else
  7369. {
  7370. DEBUG_INFO("Read MEM_ADDR_WHITE_LIST block(4k byte) fail.\r\n");
  7371. }
  7372. }
  7373. if(Charger.memory.whiteList.op_bits.update)
  7374. {
  7375. isSuccess = PASS;
  7376. // Erase block data
  7377. if(BSP_W25Qx_Erase_Block(MEM_ADDR_WHITE_LIST) == W25Qx_OK)
  7378. {
  7379. // Write data to block
  7380. for(uint16_t idx=0;idx<((W25Q16FV_BLOCK_SIZE)>>8);idx++)
  7381. {
  7382. 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)
  7383. {
  7384. isSuccess = FAIL;
  7385. }
  7386. }
  7387. if(isSuccess)
  7388. DEBUG_INFO("Update MEM_ADDR_WHITE_LIST success.\r\n");
  7389. else
  7390. DEBUG_WARN("Update MEM_ADDR_WHITE_LIST fail.\r\n");
  7391. Charger.memory.whiteList.op_bits.update = OFF;
  7392. }
  7393. else
  7394. {
  7395. DEBUG_INFO("Update MEM_ADDR_WHITE_LIST block(4k byte) fail.\r\n");
  7396. }
  7397. }
  7398. if(Charger.memory.whiteList.op_bits.clear)
  7399. {
  7400. // Erase block data
  7401. if(BSP_W25Qx_Erase_Block(MEM_ADDR_WHITE_LIST) == W25Qx_OK)
  7402. {
  7403. // Read data from block
  7404. if(BSP_W25Qx_Read(Charger.memory.whiteList.data.value, MEM_ADDR_WHITE_LIST, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7405. {
  7406. DEBUG_INFO("Erase MEM_ADDR_WHITE_LIST block(4k byte) pass.\r\n");
  7407. Charger.memory.whiteList.op_bits.clear = OFF;
  7408. }
  7409. else
  7410. {
  7411. DEBUG_INFO("Erase MEM_ADDR_WHITE_LIST block(4k byte) fail.\r\n");
  7412. }
  7413. }
  7414. else
  7415. {
  7416. DEBUG_INFO("Erase MEM_ADDR_WHITE_LIST block(4k byte) fail.\r\n");
  7417. }
  7418. }
  7419. /*
  7420. Alarm history operation
  7421. */
  7422. if(Charger.memory.hisAlarm.op_bits.read)
  7423. {
  7424. osDelay(100);
  7425. // Read data from block
  7426. if(BSP_W25Qx_Read(Charger.memory.hisAlarm.data.value, MEM_ADDR_HIS_ALARM, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7427. {
  7428. DEBUG_INFO("Read MEM_ADDR_HIS_ALARM block(4k byte) pass.\r\n");
  7429. Charger.memory.hisAlarm.op_bits.read = OFF;
  7430. }
  7431. else
  7432. {
  7433. DEBUG_INFO("Read MEM_ADDR_HIS_ALARM block(4k byte) fail.\r\n");
  7434. }
  7435. }
  7436. if(Charger.memory.hisAlarm.op_bits.update)
  7437. {
  7438. isSuccess = PASS;
  7439. // Write data to block
  7440. for(uint16_t idx=0;idx<((W25Q16FV_BLOCK_SIZE)>>8);idx++)
  7441. {
  7442. 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)
  7443. {
  7444. isSuccess = FAIL;
  7445. }
  7446. }
  7447. if(isSuccess)
  7448. DEBUG_INFO("Update MEM_ADDR_HIS_ALARM success.\r\n");
  7449. else
  7450. DEBUG_WARN("Update MEM_ADDR_HIS_ALARM fail.\r\n");
  7451. Charger.memory.hisAlarm.op_bits.update = OFF;
  7452. }
  7453. if(Charger.memory.hisAlarm.op_bits.clear)
  7454. {
  7455. // Erase block data
  7456. if(BSP_W25Qx_Erase_Block(MEM_ADDR_HIS_ALARM) == W25Qx_OK)
  7457. {
  7458. // Read data from block
  7459. if(BSP_W25Qx_Read(Charger.memory.hisAlarm.data.value, MEM_ADDR_HIS_ALARM, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7460. {
  7461. DEBUG_INFO("Erase MEM_ADDR_HIS_ALARM block(4k byte) pass.\r\n");
  7462. Charger.memory.hisAlarm.op_bits.clear = OFF;
  7463. }
  7464. else
  7465. {
  7466. DEBUG_INFO("Erase MEM_ADDR_HIS_ALARM block(4k byte) fail.\r\n");
  7467. }
  7468. }
  7469. else
  7470. {
  7471. DEBUG_INFO("Erase MEM_ADDR_HIS_ALARM block(4k byte) fail.\r\n");
  7472. }
  7473. }
  7474. if(Charger.memory.hisAlarm.op_bits.backup)
  7475. {
  7476. isSuccess = PASS;
  7477. // Erase block data
  7478. if(BSP_W25Qx_Erase_Block(MEM_ADDR_BACKUP_HIS_ALARM) == W25Qx_OK)
  7479. {
  7480. // Write data to block
  7481. for(uint16_t idx=0;idx<((W25Q16FV_BLOCK_SIZE)>>8);idx++)
  7482. {
  7483. 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)
  7484. {
  7485. isSuccess = FAIL;
  7486. }
  7487. }
  7488. if(isSuccess)
  7489. DEBUG_INFO("Backup MEM_ADDR_BACKUP_HIS_ALARM success.\r\n");
  7490. else
  7491. DEBUG_WARN("Backup MEM_ADDR_BACKUP_HIS_ALARM fail.\r\n");
  7492. Charger.memory.hisAlarm.op_bits.backup = OFF;
  7493. Charger.memory.hisAlarm.op_bits.clear = ON;
  7494. }
  7495. else
  7496. {
  7497. DEBUG_INFO("Backup MEM_ADDR_BACKUP_HIS_ALARM block(4k byte) fail.\r\n");
  7498. }
  7499. }
  7500. if(Charger.memory.hisAlarm.op_bits.restore)
  7501. {
  7502. // Restore data from backup block
  7503. if(BSP_W25Qx_Read(Charger.memory.hisAlarm.data.value, MEM_ADDR_BACKUP_HIS_ALARM, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7504. {
  7505. DEBUG_INFO("Restore MEM_ADDR_BACKUP_HIS_ALARM block(4k byte) pass.\r\n");
  7506. Charger.memory.hisAlarm.op_bits.restore = OFF;
  7507. }
  7508. else
  7509. {
  7510. DEBUG_INFO("Restore MEM_ADDR_BACKUP_HIS_ALARM block(4k byte) fail.\r\n");
  7511. }
  7512. }
  7513. /*
  7514. Charging history operation
  7515. */
  7516. if(Charger.memory.hisCharging.op_bits.read)
  7517. {
  7518. osDelay(100);
  7519. // Read data from block
  7520. if(BSP_W25Qx_Read(Charger.memory.hisCharging.data.value, MEM_ADDR_HIS_CHARGING, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7521. {
  7522. DEBUG_INFO("Read MEM_ADDR_HIS_CHARGING block(4k byte) pass.\r\n");
  7523. Charger.memory.hisCharging.op_bits.read = OFF;
  7524. }
  7525. else
  7526. {
  7527. DEBUG_INFO("Read MEM_ADDR_HIS_CHARGING block(4k byte) fail.\r\n");
  7528. }
  7529. }
  7530. if(Charger.memory.hisCharging.op_bits.update)
  7531. {
  7532. isSuccess = PASS;
  7533. // Write data to block
  7534. for(uint16_t idx=0;idx<((W25Q16FV_BLOCK_SIZE)>>8);idx++)
  7535. {
  7536. 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)
  7537. {
  7538. isSuccess = FAIL;
  7539. }
  7540. }
  7541. if(isSuccess)
  7542. DEBUG_INFO("Update MEM_ADDR_HIS_CHARGING success.\r\n");
  7543. else
  7544. DEBUG_WARN("Update MEM_ADDR_HIS_CHARGING fail.\r\n");
  7545. Charger.memory.hisCharging.op_bits.update = OFF;
  7546. }
  7547. if(Charger.memory.hisCharging.op_bits.clear)
  7548. {
  7549. // Erase block data
  7550. if(BSP_W25Qx_Erase_Block(MEM_ADDR_HIS_CHARGING) == W25Qx_OK)
  7551. {
  7552. // Read data from block
  7553. if(BSP_W25Qx_Read(Charger.memory.hisCharging.data.value, MEM_ADDR_HIS_CHARGING, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7554. {
  7555. DEBUG_INFO("Erase MEM_ADDR_HIS_CHARGING block(4k byte) pass.\r\n");
  7556. Charger.memory.hisCharging.op_bits.clear = OFF;
  7557. }
  7558. else
  7559. {
  7560. DEBUG_INFO("Erase MEM_ADDR_HIS_CHARGING block(4k byte) fail.\r\n");
  7561. }
  7562. }
  7563. else
  7564. {
  7565. DEBUG_INFO("Erase MEM_ADDR_HIS_CHARGING block(4k byte) fail.\r\n");
  7566. }
  7567. }
  7568. if(Charger.memory.hisCharging.op_bits.backup)
  7569. {
  7570. isSuccess = PASS;
  7571. // Erase block data
  7572. if(BSP_W25Qx_Erase_Block(MEM_ADDR_BACKUP_HIS_CHARGING) == W25Qx_OK)
  7573. {
  7574. // Write data to block
  7575. for(uint16_t idx=0;idx<((W25Q16FV_BLOCK_SIZE)>>8);idx++)
  7576. {
  7577. 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)
  7578. {
  7579. isSuccess = FAIL;
  7580. }
  7581. }
  7582. if(isSuccess)
  7583. DEBUG_INFO("Backup MEM_ADDR_BACKUP_HIS_CHARGING success.\r\n");
  7584. else
  7585. DEBUG_WARN("Backup MEM_ADDR_BACKUP_HIS_CHARGING fail.\r\n");
  7586. Charger.memory.hisCharging.op_bits.backup = OFF;
  7587. Charger.memory.hisCharging.op_bits.clear = ON;
  7588. }
  7589. else
  7590. {
  7591. DEBUG_INFO("Backup MEM_ADDR_BACKUP_HIS_CHARGING block(4k byte) fail.\r\n");
  7592. }
  7593. }
  7594. if(Charger.memory.hisCharging.op_bits.restore)
  7595. {
  7596. // Restore data from backup block
  7597. if(BSP_W25Qx_Read(Charger.memory.hisCharging.data.value, MEM_ADDR_BACKUP_HIS_CHARGING, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7598. {
  7599. DEBUG_INFO("Restore MEM_ADDR_BACKUP_HIS_CHARGING block(4k byte) pass.\r\n");
  7600. Charger.memory.hisCharging.op_bits.restore = OFF;
  7601. }
  7602. else
  7603. {
  7604. DEBUG_INFO("Restore MEM_ADDR_BACKUP_HIS_CHARGING block(4k byte) fail.\r\n");
  7605. }
  7606. }
  7607. osDelay(1);
  7608. }
  7609. /* USER CODE END StartMemoryTask */
  7610. }
  7611. /* USER CODE BEGIN Header_StartWifiTask */
  7612. /**
  7613. * @brief Function implementing the wifiTask thread.
  7614. * @param argument: Not used
  7615. * @retval None
  7616. */
  7617. /* USER CODE END Header_StartWifiTask */
  7618. void StartWifiTask(void const * argument)
  7619. {
  7620. /* USER CODE BEGIN StartWifiTask */
  7621. /* Infinite loop */
  7622. char tmpBuf[128];
  7623. //uint8_t endFlag[4]={0x55,0xaa,0x55,0xaa};
  7624. __IO uint32_t flash;
  7625. //uint32_t checksum;
  7626. uint32_t rndNumber;
  7627. //=====================================
  7628. // Module reset
  7629. //=====================================
  7630. HAL_GPIO_WritePin(OUT_BLE_RESET_GPIO_Port, OUT_BLE_RESET_Pin, GPIO_PIN_RESET);
  7631. osDelay(60);
  7632. HAL_GPIO_WritePin(OUT_BLE_RESET_GPIO_Port, OUT_BLE_RESET_Pin, GPIO_PIN_SET);
  7633. HAL_GPIO_WritePin(OUT_BLE_DSR_GPIO_Port, OUT_BLE_DSR_Pin, GPIO_PIN_SET);
  7634. Charger.wifi.initSeq = 0;
  7635. Charger.wifi.isDataMode = OFF;
  7636. UART_WIFI_Init_OK = OFF;
  7637. osDelay(1000);
  7638. /* Infinite loop */
  7639. for(;;)
  7640. {
  7641. //=====================================
  7642. // WIFI module initialization process
  7643. //=====================================
  7644. switch(Charger.wifi.initSeq)
  7645. {
  7646. case 0x00:
  7647. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)WIFI_CMD[WIFI_CMD_SET_RESTORE], strlen(WIFI_CMD[WIFI_CMD_SET_RESTORE]), 0xffff);
  7648. osDelay(WIFI_INIT_DELAY);
  7649. break;
  7650. case 0x01:
  7651. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)WIFI_CMD[WIFI_CMD_SET_DTR], strlen(WIFI_CMD[WIFI_CMD_SET_DTR]), 0xffff);
  7652. osDelay(WIFI_INIT_DELAY);
  7653. break;
  7654. case 0x02:
  7655. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)WIFI_CMD[WIFI_CMD_SET_DSR], strlen(WIFI_CMD[WIFI_CMD_SET_DSR]), 0xffff);
  7656. osDelay(WIFI_INIT_DELAY);
  7657. break;
  7658. case 0x03:
  7659. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)WIFI_CMD[WIFI_CMD_GET_ADDR], strlen(WIFI_CMD[WIFI_CMD_GET_ADDR]), 0xffff);
  7660. osDelay(WIFI_INIT_DELAY);
  7661. break;
  7662. case 0x04:
  7663. sprintf(tmpBuf, "%s\"EVSE_AP_%sP\"\r\n", WIFI_CMD[WIFI_CMD_SET_SSID], Charger.wifi.module_mac);
  7664. DEBUG_INFO("WIFI module set name: EVSE_AP_%s\r\n", Charger.wifi.module_mac);
  7665. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)tmpBuf, strlen(tmpBuf), 0xffff);
  7666. osDelay(WIFI_INIT_DELAY);
  7667. break;
  7668. case 0x05:
  7669. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)WIFI_CMD[WIFI_CMD_SET_CHANNEL], strlen(WIFI_CMD[WIFI_CMD_SET_CHANNEL]), 0xffff);
  7670. osDelay(WIFI_INIT_DELAY);
  7671. break;
  7672. case 0x06:
  7673. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)WIFI_CMD[WIFI_CMD_SET_SECURITY_MODE], strlen(WIFI_CMD[WIFI_CMD_SET_SECURITY_MODE]), 0xffff);
  7674. osDelay(WIFI_INIT_DELAY);
  7675. break;
  7676. case 0x07:
  7677. sprintf(tmpBuf, "%s\"%s\"\r\n", WIFI_CMD[WIFI_CMD_SET_PASSWORD], Charger.memory.EVSE_Config.data.item.wifiConfig.passwd);
  7678. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)tmpBuf, strlen(tmpBuf), 0xffff);
  7679. osDelay(WIFI_INIT_DELAY);
  7680. break;
  7681. case 0x08:
  7682. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)WIFI_CMD[WIFI_CMD_SET_DHCPSERVER], strlen(WIFI_CMD[WIFI_CMD_SET_DHCPSERVER]), 0xffff);
  7683. osDelay(WIFI_INIT_DELAY);
  7684. break;
  7685. case 0x09:
  7686. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)WIFI_CMD[WIFI_CMD_SET_TCPSETVER_CONFIG], strlen(WIFI_CMD[WIFI_CMD_SET_TCPSETVER_CONFIG]), 0xffff);
  7687. osDelay(WIFI_INIT_DELAY);
  7688. break;
  7689. case 0x0a:
  7690. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)WIFI_CMD[WIFI_CMD_SET_AP_ACTIVE], strlen(WIFI_CMD[WIFI_CMD_SET_AP_ACTIVE]), 0xffff);
  7691. osDelay(WIFI_INIT_DELAY);
  7692. break;
  7693. case 0x0b:
  7694. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)WIFI_CMD[WIFI_CMD_SET_DATA_MODE], strlen(WIFI_CMD[WIFI_CMD_SET_DATA_MODE]), 0xffff);
  7695. DEBUG_INFO("WIFI module switch to data mode.\r\n");
  7696. osDelay(WIFI_INIT_DELAY);
  7697. default:
  7698. UART_WIFI_Init_OK = ON;
  7699. break;
  7700. }
  7701. //=====================================
  7702. // WIFI central device connect status
  7703. //=====================================
  7704. if((HAL_GPIO_ReadPin(IN_BLE_DTR_GPIO_Port, IN_BLE_DTR_Pin) == GPIO_PIN_SET) && (UART_WIFI_Init_OK == ON))
  7705. {
  7706. // Device disconnect
  7707. timerRefresh(TIMER_IDX_WIFI);
  7708. if(Charger.wifi.loginRole == WIFI_LOGIN_ROLE_ROOT || Charger.wifi.loginRole == WIFI_LOGIN_ROLE_USER)
  7709. {
  7710. DEBUG_INFO("WIFI client device disconnect.\r\n.");
  7711. Charger.wifi.loginRole = WIFI_LOGIN_ROLE_UNKOWN;
  7712. }
  7713. }
  7714. else if((HAL_GPIO_ReadPin(IN_BLE_DTR_GPIO_Port, IN_BLE_DTR_Pin) == GPIO_PIN_RESET) && (UART_WIFI_Init_OK == ON))
  7715. {
  7716. // Device connect in
  7717. if(Charger.wifi.loginRole == WIFI_LOGIN_ROLE_UNKOWN)
  7718. {
  7719. DEBUG_INFO("WIFI client device connect.....wait sign in.\r\n.");
  7720. osDelay(1000);
  7721. }
  7722. }
  7723. //=====================================
  7724. // Communication timeout process
  7725. //=====================================
  7726. if(timer[TIMER_IDX_WIFI].isAlarm)
  7727. {
  7728. Charger.wifi.loginRole = WIFI_LOGIN_ROLE_UNKOWN;
  7729. DEBUG_INFO("WIFI no communication over time, disconnect client device.\r\n");
  7730. timerRefresh(TIMER_IDX_WIFI);
  7731. HAL_GPIO_WritePin(OUT_BLE_DSR_GPIO_Port, OUT_BLE_DSR_Pin, GPIO_PIN_RESET);
  7732. osDelay(100);
  7733. HAL_GPIO_WritePin(OUT_BLE_DSR_GPIO_Port, OUT_BLE_DSR_Pin, GPIO_PIN_SET);
  7734. }
  7735. //=====================================
  7736. // WIFI uart recieve data process
  7737. //=====================================
  7738. if(UART_WIFI_recv_end_flag == ON)
  7739. {
  7740. if((HAL_GPIO_ReadPin(IN_BLE_DTR_GPIO_Port, IN_BLE_DTR_Pin) == GPIO_PIN_SET) || !Charger.wifi.isDataMode)
  7741. {
  7742. // AT command rx process
  7743. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_RESTORE])!=NULL)
  7744. {
  7745. Charger.wifi.initSeq++;
  7746. }
  7747. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_DTR])!=NULL)
  7748. {
  7749. Charger.wifi.initSeq++;
  7750. }
  7751. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_DSR])!=NULL)
  7752. {
  7753. Charger.wifi.initSeq++;
  7754. }
  7755. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_GET_ADDR])!=NULL)
  7756. {
  7757. memcpy(Charger.wifi.module_mac, &UART_WIFI_rx_buffer[strcspn((char *)UART_WIFI_rx_buffer, ":")+1], 12);
  7758. Charger.wifi.module_mac[12] = '\0';
  7759. DEBUG_INFO("Wifi module mac address: %s\r\n", Charger.wifi.module_mac);
  7760. Charger.wifi.initSeq++;
  7761. }
  7762. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_SSID])!=NULL)
  7763. {
  7764. Charger.wifi.initSeq++;
  7765. }
  7766. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_CHANNEL])!=NULL)
  7767. {
  7768. Charger.wifi.initSeq++;
  7769. }
  7770. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_SECURITY_MODE])!=NULL)
  7771. {
  7772. Charger.wifi.initSeq++;
  7773. }
  7774. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_PASSWORD])!=NULL)
  7775. {
  7776. Charger.wifi.initSeq++;
  7777. }
  7778. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_DHCPSERVER])!=NULL)
  7779. {
  7780. Charger.wifi.initSeq++;
  7781. }
  7782. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_TCPSETVER_CONFIG])!=NULL)
  7783. {
  7784. Charger.wifi.initSeq++;
  7785. }
  7786. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_AP_ACTIVE])!=NULL)
  7787. {
  7788. Charger.wifi.initSeq++;
  7789. }
  7790. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_DATA_MODE])!=NULL)
  7791. {
  7792. Charger.wifi.isDataMode = ON;
  7793. Charger.wifi.initSeq++;
  7794. }
  7795. }
  7796. else
  7797. {
  7798. // Application protocol rx process
  7799. uint8_t tx[UART_BUFFER_SIZE];
  7800. uint8_t tx_len;
  7801. uint8_t chksum = 0;
  7802. 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))))
  7803. {
  7804. // Timer reset
  7805. timerRefresh(TIMER_IDX_WIFI);
  7806. 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));
  7807. switch(UART_WIFI_rx_buffer[2])
  7808. {
  7809. case WIFI_PROTOCOL_MESSAGE_SIGN_IN:
  7810. DEBUG_INFO("WIFI device sign request...%d\r\n", UART_WIFI_rx_buffer[7]);
  7811. tx_len = 5;
  7812. tx[0] = (tx_len>>8)&0xff;
  7813. tx[1] = (tx_len&0xff);
  7814. tx[2] = UART_WIFI_rx_buffer[2] + 0x80;
  7815. tx[3] = 0x00;
  7816. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  7817. {
  7818. chksum ^= tx[2 + idx];
  7819. }
  7820. tx[4] = chksum;
  7821. break;
  7822. default:
  7823. /*
  7824. TODO: Unknow command response here
  7825. */
  7826. tx_len = 5;
  7827. tx[0] = 0x00;
  7828. tx[1] = 0x05;
  7829. tx[2] = UART_WIFI_rx_buffer[2] + 0x80;
  7830. tx[3] = 0x00;
  7831. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  7832. {
  7833. chksum ^= tx[2 + idx];
  7834. }
  7835. tx[4] = chksum;
  7836. DEBUG_ERROR("WIFI unknow command.\r\n");
  7837. break;
  7838. }
  7839. }
  7840. else
  7841. {
  7842. /*
  7843. TODO: Protocol check sum error response here
  7844. */
  7845. tx_len = 5;
  7846. tx[0] = 0x00;
  7847. tx[1] = 0x05;
  7848. tx[2] = UART_WIFI_rx_buffer[2] + 0x80;
  7849. tx[3] = 0x00;
  7850. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  7851. {
  7852. chksum ^= tx[2 + idx];
  7853. }
  7854. tx[4] = chksum;
  7855. DEBUG_ERROR("WIFI check sum validation error.\r\n");
  7856. }
  7857. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)tx, tx_len, 0xffff);
  7858. }
  7859. UART_WIFI_rx_len=0;
  7860. UART_WIFI_recv_end_flag=0;
  7861. }
  7862. HAL_UART_Receive_DMA(&WIFI_USART, (uint8_t*)UART_WIFI_rx_buffer, UART_BUFFER_SIZE);
  7863. osDelay(1);
  7864. }
  7865. /* USER CODE END StartWifiTask */
  7866. }
  7867. /* Private application code --------------------------------------------------*/
  7868. /* USER CODE BEGIN Application */
  7869. #ifdef MODIFY_IMCP
  7870. void UpdateCheckSum(uint8_t *Buf, uint16_t Len) //Len: Total package length
  7871. {
  7872. uint16_t DataLen = Len - (IMCP_FM_HEAD_LEN + IMCP_FM_TAIL_LEN);
  7873. uint8_t CheckSum = 0;
  7874. //Update DataLen
  7875. HTK_U16(Buf[IMCP_FM_DATALEN_LSB_IDX]) = DataLen;
  7876. //Update CheckSum
  7877. for (uint16_t i = 0; i < DataLen; i++)
  7878. {
  7879. CheckSum ^= Buf[IMCP_FM_DATA_BEG_IDX + i];
  7880. }
  7881. Buf[Len - 1] = CheckSum;
  7882. }
  7883. #ifdef FUNC_RS485_SLAVE
  7884. uint8_t isValidCheckSum_IAP(uint8_t *RxBuf)
  7885. {
  7886. uint8_t result = OFF;
  7887. uint8_t chksum = 0x00;
  7888. #ifdef FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  7889. if (RxBuf[IMCP_FM_FRAMEID_IDX] != PROTOCOL_HEAD)
  7890. {
  7891. return FAIL;
  7892. }
  7893. else if (RxBuf == UART_IAP_rx_buffer)
  7894. {
  7895. if (RxBuf[IMCP_FM_DSTADDR_IDX] != PROTOCOL_ADDR &&
  7896. RxBuf[IMCP_FM_DSTADDR_IDX] != PROTOCOL_ADDR_AC_MAINBOARD &&
  7897. RxBuf[IMCP_FM_DSTADDR_IDX] != PROTOCOL_ADDR_BROADCAST)
  7898. {
  7899. return FAIL;
  7900. }
  7901. }
  7902. else if (RxBuf == UART_RS485_rx_buffer)
  7903. {
  7904. if (RxBuf[IMCP_FM_SRCADDR_IDX] != EMPX10_RS485_ADDR_CSU)
  7905. {
  7906. return FAIL;
  7907. }
  7908. else if (
  7909. RxBuf[IMCP_FM_DSTADDR_IDX] != Charger.m_EMPX10_RS485_ClientAddr &&
  7910. RxBuf[IMCP_FM_DSTADDR_IDX] != EMPX10_RS485_ADDR_BROADCAST)
  7911. {
  7912. return FAIL;
  7913. }
  7914. }
  7915. #else //FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  7916. if (
  7917. (RxBuf[IMCP_FM_FRAMEID_IDX] == PROTOCOL_HEAD) &&
  7918. (
  7919. (RxBuf[IMCP_FM_DSTADDR_IDX] == PROTOCOL_ADDR)
  7920. || (RxBuf[IMCP_FM_DSTADDR_IDX] == PROTOCOL_ADDR_AC_MAINBOARD)
  7921. || (RxBuf[IMCP_FM_DSTADDR_IDX] == PROTOCOL_ADDR_BROADCAST)
  7922. )
  7923. )
  7924. #endif //FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  7925. {
  7926. uint16_t nCheckSumIdx = IMCP_FM_DATA_BEG_IDX + HTK_U16(RxBuf[IMCP_FM_DATALEN_LSB_IDX]);
  7927. for(uint16_t i = IMCP_FM_DATA_BEG_IDX; i < nCheckSumIdx; i++)
  7928. {
  7929. chksum ^= RxBuf[i];
  7930. }
  7931. if(chksum == RxBuf[nCheckSumIdx])
  7932. result = ON;
  7933. }
  7934. return result;
  7935. }
  7936. #else //FUNC_RS485_SLAVE
  7937. uint8_t isValidCheckSum_IAP(void)
  7938. {
  7939. uint8_t result = OFF;
  7940. uint8_t chksum = 0x00;
  7941. if (
  7942. (UART_IAP_rx_buffer[IMCP_FM_FRAMEID_IDX] == PROTOCOL_HEAD) &&
  7943. (
  7944. (UART_IAP_rx_buffer[IMCP_FM_DSTADDR_IDX] == PROTOCOL_ADDR)
  7945. || (UART_IAP_rx_buffer[IMCP_FM_DSTADDR_IDX] == PROTOCOL_ADDR_BROADCAST)
  7946. )
  7947. )
  7948. {
  7949. uint16_t nCheckSumIdx = IMCP_FM_DATA_BEG_IDX + HTK_U16(UART_IAP_rx_buffer[IMCP_FM_DATALEN_LSB_IDX]);
  7950. for(uint16_t i = IMCP_FM_DATA_BEG_IDX; i < nCheckSumIdx; i++)
  7951. {
  7952. chksum ^= UART_IAP_rx_buffer[i];
  7953. }
  7954. if(chksum == UART_IAP_rx_buffer[nCheckSumIdx])
  7955. result = ON;
  7956. }
  7957. return result;
  7958. }
  7959. #endif //FUNC_RS485_SLAVE
  7960. #else //MODIFY_IMCP
  7961. uint8_t isValidCheckSum_IAP(void)
  7962. {
  7963. uint8_t result = OFF;
  7964. uint8_t chksum = 0x00;
  7965. if((UART_IAP_rx_buffer[0] == PROTOCOL_HEAD) &&
  7966. ((UART_IAP_rx_buffer[2] == PROTOCOL_ADDR) | (UART_IAP_rx_buffer[2] == PROTOCOL_ADDR_BROADCAST)))
  7967. {
  7968. for(int idx=0;idx<(UART_IAP_rx_buffer[4] | (UART_IAP_rx_buffer[5]<<8));idx++)
  7969. {
  7970. chksum ^= UART_IAP_rx_buffer[6+ idx];
  7971. }
  7972. if(chksum == UART_IAP_rx_buffer[6+(UART_IAP_rx_buffer[4] | (UART_IAP_rx_buffer[5]<<8))])
  7973. result = ON;
  7974. }
  7975. return result;
  7976. }
  7977. #endif //MODIFY_IMCP
  7978. //-------------------------------------------------------------------------------------------------
  7979. uint8_t isValidCheckSum_BLE(void)
  7980. {
  7981. uint8_t result = OFF;
  7982. uint8_t chksum = 0x00;
  7983. if(((UART_BLE_rx_buffer[1] | (UART_BLE_rx_buffer[0]<<8))-1) < UART_BUFFER_SIZE)
  7984. {
  7985. for(int idx=2;idx<((UART_BLE_rx_buffer[1] | (UART_BLE_rx_buffer[0]<<8))-1);idx++)
  7986. {
  7987. chksum ^= UART_BLE_rx_buffer[idx];
  7988. }
  7989. if(chksum == UART_BLE_rx_buffer[(UART_BLE_rx_buffer[1] | (UART_BLE_rx_buffer[0]<<8))-1])
  7990. result = ON;
  7991. }
  7992. return result;
  7993. }
  7994. //-------------------------------------------------------------------------------------------------
  7995. uint8_t isValidCheckSum_WIFI(void)
  7996. {
  7997. uint8_t result = OFF;
  7998. uint8_t chksum = 0x00;
  7999. if(((UART_WIFI_rx_buffer[1] | (UART_WIFI_rx_buffer[0]<<8))-1) < UART_BUFFER_SIZE)
  8000. {
  8001. for(int idx=2;idx<((UART_WIFI_rx_buffer[1] | (UART_WIFI_rx_buffer[0]<<8))-1);idx++)
  8002. {
  8003. chksum ^= UART_WIFI_rx_buffer[idx];
  8004. }
  8005. if(chksum == UART_WIFI_rx_buffer[(UART_WIFI_rx_buffer[1] | (UART_WIFI_rx_buffer[0]<<8))-1])
  8006. result = ON;
  8007. }
  8008. return result;
  8009. }
  8010. //-------------------------------------------------------------------------------------------------
  8011. #ifdef FUNC_DEBUG_MSG_SET_CHARGER_MODE
  8012. char* getModeName(uint8_t mode);
  8013. #endif
  8014. void setChargerMode(uint8_t mode)
  8015. {
  8016. #ifdef FUNC_DEBUG_MSG_SET_CHARGER_MODE
  8017. XP("#SET CHARGER MODE: [%s] >>> [%s]\r\n", getModeName(Charger.Mode_Previous), getModeName(mode));
  8018. #endif
  8019. Charger.Mode_Previous = Charger.Mode;
  8020. Charger.Mode = mode;
  8021. }
  8022. //-------------------------------------------------------------------------------------------------
  8023. char* getModeName(uint8_t mode)
  8024. {
  8025. switch(mode)
  8026. {
  8027. case MODE_INITIAL:
  8028. return "INITIAL";
  8029. case MODE_IDLE:
  8030. return "IDLE";
  8031. case MODE_HANDSHAKE:
  8032. return "HANDSHAKING";
  8033. case MODE_CHARGING:
  8034. return "CHARGING";
  8035. case MODE_STOP:
  8036. return "STOP";
  8037. case MODE_ALARM:
  8038. return "ALARM";
  8039. case MODE_MAINTAIN:
  8040. return "MAINTAIN";
  8041. case MODE_DEBUG:
  8042. return "DEBUG";
  8043. default:
  8044. return "UNKNOW";
  8045. }
  8046. }
  8047. //-------------------------------------------------------------------------------------------------
  8048. uint8_t isModeChange(void)
  8049. {
  8050. uint8_t result = NO;
  8051. if(!isMode(Charger.Mode_Previous))
  8052. {
  8053. DEBUG_INFO("#MODE from %s to %s\r\n", getModeName(Charger.Mode_Previous), getModeName(Charger.Mode));
  8054. result |= YES;
  8055. if(Charger.Mode == MODE_ALARM)
  8056. {
  8057. Charger.Mode_Before_Alarm = Charger.Mode_Previous;
  8058. }
  8059. Charger.Mode_Previous = Charger.Mode;
  8060. }
  8061. return result;
  8062. }
  8063. //-------------------------------------------------------------------------------------------------
  8064. uint8_t isMode(uint8_t mode)
  8065. {
  8066. return ((Charger.Mode == mode)? YES : NO);
  8067. }
  8068. //-------------------------------------------------------------------------------------------------
  8069. uint8_t rfidCheckSumCal(uint8_t *data, uint8_t length)
  8070. {
  8071. uint8_t result= 0x00;
  8072. for(uint8_t idx=0;idx<length;idx++)
  8073. result ^= data[idx];
  8074. return result;
  8075. }
  8076. //-------------------------------------------------------------------------------------------------
  8077. void HAL_PWR_PVDCallback(void)
  8078. {
  8079. HAL_Delay(100);
  8080. NVIC_SystemReset();
  8081. }
  8082. //-------------------------------------------------------------------------------------------------
  8083. uint8_t Test_LeakModule(void)
  8084. {
  8085. // uint8_t result = PASS, DC_check = false, AC_check=false;
  8086. // uint16_t time_out_count;
  8087. // uint8_t retry_cnt = 0 ;
  8088. uint8_t result = PASS;
  8089. #ifdef CCID_PROTECT
  8090. uint8_t DC_check = false;
  8091. uint8_t AC_check=false;
  8092. uint16_t time_out_count;
  8093. uint8_t retry_cnt = 0 ;
  8094. #ifdef FUNC_AUTO_MODIFY_CCID_MODULE
  8095. XP("#Test_LeakModule use CCID Module: %s\r\n", CCID_MODULE_TYPE_STR);
  8096. #endif
  8097. if (Charger.CCID_Module_Type == CCID_MODULE_VAC)
  8098. {
  8099. osDelay(500);
  8100. if(HAL_GPIO_ReadPin(IN_Leak_Error_GPIO_Port, IN_Leak_Error_Pin) == GPIO_PIN_SET)
  8101. {
  8102. DEBUG_INFO("VAC Leak Error pin ON.\r\n");
  8103. result = FAIL;
  8104. }
  8105. else
  8106. {
  8107. HAL_Delay(270);
  8108. HAL_GPIO_WritePin(OUT_Leak_Test_GPIO_Port , OUT_Leak_Test_Pin , GPIO_PIN_RESET);
  8109. HAL_Delay(50); //40ms~1.2sec
  8110. HAL_GPIO_WritePin(OUT_Leak_Test_GPIO_Port , OUT_Leak_Test_Pin , GPIO_PIN_SET);
  8111. //HAL_Delay(40); //36ms
  8112. time_out_count = 0;
  8113. while(1)
  8114. {
  8115. if(HAL_GPIO_ReadPin(IN_Leak_DC_GPIO_Port, IN_Leak_DC_Pin) == GPIO_PIN_SET)
  8116. {
  8117. DC_check = true;
  8118. }
  8119. if(HAL_GPIO_ReadPin(IN_Leak_AC_GPIO_Port, IN_Leak_AC_Pin) == GPIO_PIN_SET)
  8120. {
  8121. AC_check = true;
  8122. }
  8123. //if(DC_check&&AC_check)
  8124. 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))
  8125. {
  8126. break;
  8127. }
  8128. if(time_out_count>2000)
  8129. {
  8130. //result = FAIL;
  8131. if(retry_cnt < 1 )
  8132. {
  8133. HAL_Delay(270);
  8134. HAL_GPIO_WritePin(OUT_Leak_Test_GPIO_Port , OUT_Leak_Test_Pin , GPIO_PIN_RESET);
  8135. HAL_Delay(50); //40ms~1.2sec
  8136. HAL_GPIO_WritePin(OUT_Leak_Test_GPIO_Port , OUT_Leak_Test_Pin , GPIO_PIN_SET);
  8137. //HAL_Delay(40); //36ms
  8138. retry_cnt ++ ;
  8139. time_out_count = 0 ;
  8140. }
  8141. else
  8142. {
  8143. DEBUG_INFO("Leak module self test Fail in timeout.\r\n");
  8144. result = FAIL;
  8145. break;
  8146. }
  8147. }
  8148. else
  8149. {
  8150. time_out_count++;
  8151. HAL_Delay(1);
  8152. }
  8153. }
  8154. }
  8155. }
  8156. else // Charger.CCID_Module_Type is CCID_MODULE_CORMEX
  8157. {
  8158. SPWM_Switch(ON);
  8159. HAL_Delay(1000);
  8160. XP("#[SPWM] adc_value.ADC2_IN6_GF.value = %d\r\n",adc_value.ADC2_IN6_GF.value);
  8161. if ((adc_value.ADC2_IN6_GF.value<3000)&&(adc_value.ADC2_IN6_GF.value>2000)) //25mA
  8162. {
  8163. DEBUG_INFO("#Test_LeakModule(): OK\r\n");
  8164. result = PASS ;
  8165. }
  8166. else
  8167. {
  8168. DEBUG_INFO("Test_LeakModule(): NG\r\n");
  8169. //DEBUG_INFO("adc_value.ADC2_IN6_GF.value = %d\n\r",adc_value.ADC2_IN6_GF.value);
  8170. result = FAIL;
  8171. }
  8172. SPWM_Switch(OFF);
  8173. }
  8174. #endif //CCID_PROTECT
  8175. return result;
  8176. }
  8177. //-------------------------------------------------------------------------------------------------
  8178. uint8_t Valid_RFID_SN(void)
  8179. {
  8180. /*
  8181. TODO: Validation logic check
  8182. */
  8183. uint8_t result = FAIL;
  8184. uint32_t crc_result;
  8185. uint8_t sn_len;
  8186. uint8_t *buf;
  8187. if(Charger.rfid.snType == RFID_SN_TYPE_4BYTE)
  8188. {
  8189. // SN length: 4
  8190. sn_len = 4;
  8191. }
  8192. else if(Charger.rfid.snType == RFID_SN_TYPE_7BYTE)
  8193. {
  8194. // SN length: 7
  8195. sn_len = 7;
  8196. }
  8197. else
  8198. {
  8199. // SN length: 10
  8200. sn_len = 10;
  8201. }
  8202. buf = malloc(25 + sn_len);
  8203. memcpy(buf, Charger.memory.EVSE_Config.data.item.System_ID, 25);
  8204. memcpy(&buf[25], Charger.rfid.data_Sn, sn_len);
  8205. crc_result = crc32(buf, 25 + sn_len);
  8206. 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)
  8207. {
  8208. result = PASS;
  8209. }
  8210. free(buf);
  8211. return result;
  8212. }
  8213. //-------------------------------------------------------------------------------------------------
  8214. void recordAlarmHis(void)
  8215. {
  8216. uint8_t isSuccess = FAIL;
  8217. // Check record alarm request, if alarm code bit different with previous
  8218. for(uint8_t errIdx=0;errIdx<32;errIdx++)
  8219. {
  8220. if(((Charger.Alarm_Code>>errIdx)&0x01) != ((Charger.Alarm_Code_Previous>>errIdx)&0x01))
  8221. {
  8222. // Find empty memory space
  8223. for(uint16_t idx=0;idx<(W25Q16FV_BLOCK_SIZE>>4);idx++)
  8224. {
  8225. if((Charger.memory.hisAlarm.data.item[idx].alarmCode == 0xffffffff) &&
  8226. (Charger.memory.hisAlarm.data.item[idx].mode == 0xff))
  8227. {
  8228. isSuccess = PASS;
  8229. Charger.memory.hisAlarm.data.item[idx].DateTime.year = Charger.memory.EVSE_Config.data.item.SystemDateTime.year;
  8230. Charger.memory.hisAlarm.data.item[idx].DateTime.month = Charger.memory.EVSE_Config.data.item.SystemDateTime.month;
  8231. Charger.memory.hisAlarm.data.item[idx].DateTime.day = Charger.memory.EVSE_Config.data.item.SystemDateTime.day;
  8232. Charger.memory.hisAlarm.data.item[idx].DateTime.hour = Charger.memory.EVSE_Config.data.item.SystemDateTime.hour;
  8233. Charger.memory.hisAlarm.data.item[idx].DateTime.min = Charger.memory.EVSE_Config.data.item.SystemDateTime.min;
  8234. Charger.memory.hisAlarm.data.item[idx].DateTime.sec = Charger.memory.EVSE_Config.data.item.SystemDateTime.sec;
  8235. Charger.memory.hisAlarm.data.item[idx].mode = Charger.Mode;
  8236. if((Charger.Alarm_Code>>errIdx)&0x01)
  8237. {
  8238. // Record alarm occur
  8239. Charger.memory.hisAlarm.data.item[idx].alarmCode = alarm_record_code[errIdx];
  8240. Charger.memory.hisAlarm.data.item[idx].isRecover = OFF;
  8241. Charger.Alarm_Code_Previous |= (0x00000001<<errIdx);
  8242. }
  8243. else
  8244. {
  8245. // Record alarm recover
  8246. Charger.memory.hisAlarm.data.item[idx].alarmCode = alarm_record_code[errIdx];
  8247. Charger.memory.hisAlarm.data.item[idx].isRecover = ON;
  8248. Charger.Alarm_Code_Previous &= ~(0x00000001<<errIdx);
  8249. }
  8250. Charger.memory.hisAlarm.op_bits.update = ON;
  8251. // If speace is last item, move data to backup area
  8252. if(idx == ((W25Q16FV_BLOCK_SIZE>>4)-1))
  8253. {
  8254. osDelay(500);
  8255. Charger.memory.hisAlarm.op_bits.backup = ON;
  8256. osDelay(500);
  8257. }
  8258. break;
  8259. }
  8260. }
  8261. // If there is not any free space
  8262. if(!isSuccess)
  8263. {
  8264. osDelay(500);
  8265. Charger.memory.hisAlarm.op_bits.backup = ON;
  8266. osDelay(500);
  8267. }
  8268. }
  8269. }
  8270. }
  8271. //-------------------------------------------------------------------------------------------------
  8272. uint8_t recordChargingHis(uint8_t isColdLoadPickUp, uint32_t statusCode)
  8273. {
  8274. uint8_t isSuccess = FAIL;
  8275. if(isColdLoadPickUp)
  8276. {
  8277. for(uint16_t idx=0;idx<(W25Q16FV_BLOCK_SIZE>>6);idx++)
  8278. {
  8279. if((Charger.memory.hisCharging.data.item[idx].Duration == 0xffffffff) &&
  8280. (Charger.memory.hisCharging.data.item[idx].powerSum == 0xffff) &&
  8281. (Charger.memory.hisCharging.data.item[idx].stopStatusCode == 0xffffffff) )
  8282. {
  8283. isSuccess = PASS;
  8284. Charger.memory.hisCharging.data.item[idx].DateTime.year = Charger.memory.coldLoadPickUp.data.item.DateTime.year;
  8285. Charger.memory.hisCharging.data.item[idx].DateTime.month = Charger.memory.coldLoadPickUp.data.item.DateTime.month;
  8286. Charger.memory.hisCharging.data.item[idx].DateTime.day = Charger.memory.coldLoadPickUp.data.item.DateTime.day;
  8287. Charger.memory.hisCharging.data.item[idx].DateTime.hour = Charger.memory.coldLoadPickUp.data.item.DateTime.hour;
  8288. Charger.memory.hisCharging.data.item[idx].DateTime.min = Charger.memory.coldLoadPickUp.data.item.DateTime.min;
  8289. Charger.memory.hisCharging.data.item[idx].DateTime.sec = Charger.memory.coldLoadPickUp.data.item.DateTime.sec;
  8290. Charger.memory.hisCharging.data.item[idx].Duration = Charger.memory.coldLoadPickUp.data.item.Duration;
  8291. sprintf(Charger.memory.hisCharging.data.item[idx].PlugType, EVSE_CONNECTOR_TYPE);
  8292. Charger.memory.hisCharging.data.item[idx].powerSum = Charger.memory.coldLoadPickUp.data.item.powerSum;
  8293. Charger.memory.hisCharging.data.item[idx].startType = Charger.memory.coldLoadPickUp.data.item.startType;
  8294. Charger.memory.hisCharging.data.item[idx].stopStatusCode = statusCode;
  8295. memcpy(Charger.memory.hisCharging.data.item[idx].user, Charger.rfid.currentCard, 16);
  8296. Charger.memory.hisCharging.op_bits.update = ON;
  8297. // If speace is last item, move data to backup area
  8298. if(idx == ((W25Q16FV_BLOCK_SIZE>>4)-1))
  8299. {
  8300. osDelay(500);
  8301. Charger.memory.hisCharging.op_bits.backup = ON;
  8302. }
  8303. break;
  8304. }
  8305. }
  8306. }
  8307. else
  8308. {
  8309. for(uint16_t idx=0;idx<(W25Q16FV_BLOCK_SIZE>>6);idx++)
  8310. {
  8311. if((Charger.memory.hisCharging.data.item[idx].Duration == 0xffffffff) &&
  8312. (Charger.memory.hisCharging.data.item[idx].powerSum == 0xffff) &&
  8313. (Charger.memory.hisCharging.data.item[idx].stopStatusCode == 0xffffffff) )
  8314. {
  8315. isSuccess = PASS;
  8316. Charger.memory.hisCharging.data.item[idx].DateTime.year = Charger.cycle_info.startDateTime.year;
  8317. Charger.memory.hisCharging.data.item[idx].DateTime.month = Charger.cycle_info.startDateTime.month;
  8318. Charger.memory.hisCharging.data.item[idx].DateTime.day = Charger.cycle_info.startDateTime.day;
  8319. Charger.memory.hisCharging.data.item[idx].DateTime.hour = Charger.cycle_info.startDateTime.hour;
  8320. Charger.memory.hisCharging.data.item[idx].DateTime.min = Charger.cycle_info.startDateTime.min;
  8321. Charger.memory.hisCharging.data.item[idx].DateTime.sec = Charger.cycle_info.startDateTime.sec;
  8322. Charger.memory.hisCharging.data.item[idx].Duration = Charger.cycle_info.Duration;
  8323. sprintf(Charger.memory.hisCharging.data.item[idx].PlugType, EVSE_CONNECTOR_TYPE);
  8324. Charger.memory.hisCharging.data.item[idx].powerSum = Charger.cycle_info.Power_Consumption;
  8325. Charger.memory.hisCharging.data.item[idx].startType = Charger.cycle_info.StartType;
  8326. Charger.memory.hisCharging.data.item[idx].stopStatusCode = statusCode;
  8327. memcpy(Charger.memory.hisCharging.data.item[idx].user, Charger.rfid.currentCard, 16);
  8328. Charger.memory.hisCharging.op_bits.update = ON;
  8329. // If speace is last item, move data to backup area
  8330. if(idx == ((W25Q16FV_BLOCK_SIZE>>4)-1))
  8331. {
  8332. osDelay(500);
  8333. Charger.memory.hisCharging.op_bits.backup = ON;
  8334. }
  8335. break;
  8336. }
  8337. }
  8338. }
  8339. // If there is not any free space
  8340. if(!isSuccess)
  8341. {
  8342. osDelay(500);
  8343. Charger.memory.hisCharging.op_bits.backup = ON;
  8344. }
  8345. return isSuccess;
  8346. }
  8347. //-------------------------------------------------------------------------------------------------
  8348. void setLedMotion(uint8_t action)
  8349. {//ssetled
  8350. //standard
  8351. switch(action)
  8352. {
  8353. case LED_ACTION_INIT:
  8354. Charger.led_R.motion = LED_MOTION_ON;
  8355. Charger.led_G.motion = LED_MOTION_ON;
  8356. Charger.led_B.motion = LED_MOTION_ON;
  8357. Charger.led_W.motion = LED_MOTION_OFF;
  8358. LedOnRGBSet(1000, 1000, 1000, 0);
  8359. break;
  8360. case LED_ACTION_IDLE:
  8361. Charger.led_R.motion = LED_MOTION_OFF;
  8362. Charger.led_G.motion = LED_MOTION_ON;
  8363. Charger.led_B.motion = LED_MOTION_OFF;
  8364. Charger.led_W.motion = LED_MOTION_OFF;
  8365. LedOnRGBSet(0, 1000, 0, 0);
  8366. break;
  8367. case LED_ACTION_AUTHED:
  8368. Charger.led_R.motion = LED_MOTION_OFF;
  8369. Charger.led_G.motion = LED_MOTION_OFF;
  8370. Charger.led_B.motion = LED_MOTION_ON;
  8371. Charger.led_W.motion = LED_MOTION_OFF;
  8372. LedOnRGBSet(0, 0, 1000, 0);
  8373. break;
  8374. case LED_ACTION_CONNECTED:
  8375. Charger.led_R.motion = LED_MOTION_OFF;
  8376. Charger.led_G.motion = LED_MOTION_OFF;
  8377. Charger.led_B.motion = LED_MOTION_ON;
  8378. Charger.led_W.motion = LED_MOTION_OFF;
  8379. LedOnRGBSet(0, 0, 1000, 0);
  8380. break;
  8381. case LED_ACTION_CHARGING:
  8382. Charger.led_R.motion = LED_MOTION_OFF;
  8383. Charger.led_G.motion = LED_MOTION_OFF;
  8384. Charger.led_B.motion = LED_MOTION_BLINK;
  8385. Charger.led_W.motion = LED_MOTION_OFF;
  8386. LedOnRGBSet(0, 0, 1000, 0);
  8387. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 0, 1);
  8388. break;
  8389. case LED_ACTION_STOP:
  8390. Charger.led_R.motion = LED_MOTION_OFF;
  8391. Charger.led_G.motion = LED_MOTION_OFF;
  8392. Charger.led_B.motion = LED_MOTION_ON;
  8393. Charger.led_W.motion = LED_MOTION_OFF;
  8394. LedOnRGBSet(0, 0, 1000, 0);
  8395. break;
  8396. case LED_ACTION_ALARM:
  8397. Charger.led_R.motion = LED_MOTION_BLINK;
  8398. Charger.led_G.motion = LED_MOTION_OFF;
  8399. Charger.led_B.motion = LED_MOTION_OFF;
  8400. Charger.led_W.motion = LED_MOTION_OFF;
  8401. LedOnRGBSet(1000, 0, 0, 0);
  8402. //set alarm led blink duty
  8403. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  8404. Charger.Alarm_LED = Charger.Alarm_Code;
  8405. }else{
  8406. Charger.Alarm_LED = Charger.am3352.LedAlarmState;
  8407. }
  8408. if(Charger.Alarm_LED & ALARM_CP_ERROR)
  8409. blinkerTimeSet(BLINKER_IDX_LED, 500 , 500, 0, 1);
  8410. if(Charger.Alarm_LED & ALARM_OVER_VOLTAGE)
  8411. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 3000, 1);
  8412. if(Charger.Alarm_LED & ALARM_UNDER_VOLTAGE)
  8413. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 3000, 2);
  8414. if(Charger.Alarm_LED & ALARM_OVER_CURRENT)
  8415. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 3000, 3);
  8416. if(Charger.Alarm_LED & ALARM_OVER_TEMPERATURE)
  8417. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 3000, 4);
  8418. if(Charger.Alarm_LED & ALARM_CURRENT_LEAK_AC)
  8419. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 3000, 5);
  8420. if(Charger.Alarm_LED & ALARM_CURRENT_LEAK_DC)
  8421. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 3000, 5);
  8422. if(Charger.Alarm_LED & ALARM_GROUND_FAIL)
  8423. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 3000, 6);
  8424. if(Charger.Alarm_LED & ALARM_HANDSHAKE_TIMEOUT)
  8425. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 0, 1);
  8426. if(Charger.Alarm_LED & ALARM_EMERGENCY_STOP)
  8427. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 3000, 7);
  8428. if(Charger.Alarm_LED & ALARM_ROTATORY_SWITCH_FAULT)
  8429. blinkerTimeSet(BLINKER_IDX_LED, 200, 200, 0, 1);
  8430. //OCP Latch
  8431. if (Charger.counter.OC.isLatch == ON)
  8432. {
  8433. Charger.led_R.motion = LED_MOTION_ON ;
  8434. Charger.led_B.motion = LED_MOTION_ON ;
  8435. LedOnRGBSet(1000, 0, 1000, 0);
  8436. }
  8437. //CCID Latch
  8438. if (Charger.counter.LEAK.isLatch == ON)
  8439. {
  8440. Charger.led_R.motion = LED_MOTION_ON ;
  8441. Charger.led_G.motion = LED_MOTION_ON ;
  8442. LedOnRGBSet(1000, 1000, 0, 0);
  8443. }
  8444. //OTP
  8445. if (Charger.counter.OT.isLatch == ON)
  8446. {
  8447. Charger.led_R.motion = LED_MOTION_ON;
  8448. Charger.led_G.motion = LED_MOTION_BLINK;
  8449. Charger.led_B.motion = LED_MOTION_BLINK;
  8450. Charger.led_W.motion = LED_MOTION_OFF;
  8451. LedOnRGBSet(1000, 1000, 1000, 0);
  8452. blinkerTimeSet(BLINKER_IDX_LED, 500 , 500, 0, 1);
  8453. }
  8454. //self test error
  8455. if ((Charger.Alarm_Code & ALARM_MCU_TESTFAIL) ||
  8456. (Charger.Alarm_Code & ALARM_RELAY_STATUS) ||
  8457. (Charger.Alarm_Code & ALARM_RELAY_DRIVE_FUALT ) ||
  8458. (Charger.Alarm_Code & ALARM_LEAK_MODULE_FAIL))
  8459. {
  8460. Charger.led_R.motion = LED_MOTION_ON;
  8461. Charger.led_G.motion = LED_MOTION_OFF;
  8462. Charger.led_B.motion = LED_MOTION_OFF;
  8463. Charger.led_W.motion = LED_MOTION_OFF;
  8464. LedOnRGBSet(1000, 0, 0, 0);
  8465. }
  8466. break;
  8467. case LED_ACTION_MAINTAIN:
  8468. Charger.led_R.motion = LED_MOTION_OFF;
  8469. Charger.led_G.motion = LED_MOTION_BLINK;
  8470. Charger.led_B.motion = LED_MOTION_OFF;
  8471. Charger.led_W.motion = LED_MOTION_OFF;
  8472. LedOnRGBSet(0, 1000, 0, 0);
  8473. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 0, 1);
  8474. break;
  8475. case LED_ACTION_RFID_PASS:
  8476. Charger.led_R.motion = LED_MOTION_OFF;
  8477. Charger.led_G.motion = LED_MOTION_BLINK;
  8478. Charger.led_B.motion = LED_MOTION_OFF;
  8479. Charger.led_W.motion = LED_MOTION_OFF;
  8480. LedOnRGBSet(0, 1000, 0, 0);
  8481. blinkerTimeSet(BLINKER_IDX_LED, 200, 200, 0, 1);
  8482. break;
  8483. case LED_ACTION_RFID_FAIL:
  8484. Charger.led_R.motion = LED_MOTION_BLINK;
  8485. Charger.led_G.motion = LED_MOTION_OFF;
  8486. Charger.led_B.motion = LED_MOTION_OFF;
  8487. Charger.led_W.motion = LED_MOTION_OFF;
  8488. LedOnRGBSet(1000, 0, 0, 0);
  8489. blinkerTimeSet(BLINKER_IDX_LED, 200, 200, 0, 1);
  8490. break;
  8491. case LED_ACTION_DEBUG:
  8492. Charger.led_R.motion = LED_MOTION_BLINK;
  8493. Charger.led_G.motion = LED_MOTION_BLINK;
  8494. Charger.led_B.motion = LED_MOTION_BLINK;
  8495. Charger.led_W.motion = LED_MOTION_BLINK;
  8496. LedOnRGBSet(1000, 1000, 1000, 1000);
  8497. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 0, 1);
  8498. break;
  8499. case LED_ACTION_ALL_OFF:
  8500. Charger.led_R.motion = LED_MOTION_OFF;
  8501. Charger.led_G.motion = LED_MOTION_OFF;
  8502. Charger.led_B.motion = LED_MOTION_OFF;
  8503. Charger.led_W.motion = LED_MOTION_OFF;
  8504. LedOnRGBSet(0, 0, 0, 0);
  8505. break ;
  8506. case LED_ACTION_HANDSHAKE_FAIL:
  8507. Charger.led_R.motion = LED_MOTION_BLINK;
  8508. Charger.led_G.motion = LED_MOTION_OFF;
  8509. Charger.led_B.motion = LED_MOTION_OFF;
  8510. Charger.led_W.motion = LED_MOTION_OFF;
  8511. LedOnRGBSet(1000, 0, 0, 0);
  8512. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 0, 1);
  8513. break;
  8514. case LED_ACTION_INTERNET_DISCONNECT:
  8515. Charger.led_R.motion = LED_MOTION_ON;
  8516. Charger.led_G.motion = LED_MOTION_ON;
  8517. Charger.led_B.motion = LED_MOTION_OFF;
  8518. Charger.led_W.motion = LED_MOTION_OFF;
  8519. LedOnRGBSet(1000, 1000, 0, 0);
  8520. break;
  8521. case LED_ACTION_RESTORE_SETTING:
  8522. Charger.led_R.motion = LED_MOTION_OFF;
  8523. Charger.led_G.motion = LED_MOTION_OFF;
  8524. Charger.led_B.motion = LED_MOTION_OFF;
  8525. Charger.led_W.motion = LED_MOTION_BLINK;
  8526. LedOnRGBSet(0, 0, 0, 1000);
  8527. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 0, 1);
  8528. break;
  8529. default :
  8530. Charger.led_R.motion = LED_MOTION_OFF;
  8531. Charger.led_G.motion = LED_MOTION_OFF;
  8532. Charger.led_B.motion = LED_MOTION_OFF;
  8533. Charger.led_W.motion = LED_MOTION_OFF;
  8534. LedOnRGBSet(0, 0, 0, 0);
  8535. break;
  8536. }
  8537. if((action != LED_ACTION_RFID_PASS) &&
  8538. (action != LED_ACTION_RFID_FAIL) &&
  8539. (action != LED_ACTION_BLE_CONNECT) &&
  8540. (action != LED_ACTION_BLE_DISABLE) &&
  8541. (action != LED_ACTION_HANDSHAKE_FAIL) &&
  8542. (action != LED_ACTION_RESTORE_SETTING))
  8543. {
  8544. Charger.Led_Mode = action;
  8545. }
  8546. }
  8547. //-------------------------------------------------------------------------------------------------
  8548. void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef* hadc)
  8549. {
  8550. #ifdef CCID_PROTECT
  8551. if (Charger.CCID_Module_Type == CCID_MODULE_CORMEX)
  8552. {
  8553. if(hadc->Instance == ADC2) //use watch dog to check ccid level
  8554. {
  8555. if (!Charger.isTestLeakModule)
  8556. {
  8557. if(Charger.counter.WatchDogLeak >= 5)
  8558. {
  8559. if(!(Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC))
  8560. {
  8561. DEBUG_INFO("leak adc watch happen\r\n");
  8562. Charger.counter.LEAK.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  8563. Charger.Alarm_Code |= ALARM_CURRENT_LEAK_AC;
  8564. timerEnable(TIMER_IDX_RETRY_LEAK_AC, ALARM_RETRY_INTERVAL_LEAK);
  8565. Charger.counter.LEAK.retry++ ;
  8566. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF ;
  8567. HAL_GPIO_WritePin(OUT_RelayDrv_L1_GPIO_Port, OUT_RelayDrv_L1_Pin, GPIO_PIN_RESET);
  8568. HAL_GPIO_WritePin(OUT_RelayHold_L1_GPIO_Port, OUT_RelayHold_L1_Pin, GPIO_PIN_RESET);
  8569. HAL_GPIO_WritePin(OUT_RelayDrv_L2_GPIO_Port, OUT_RelayDrv_L2_Pin, GPIO_PIN_RESET);
  8570. HAL_GPIO_WritePin(OUT_RelayHold_L2_GPIO_Port, OUT_RelayHold_L2_Pin, GPIO_PIN_RESET);
  8571. Charger.counter.WatchDogLeak = 0 ;
  8572. }
  8573. }
  8574. else
  8575. {
  8576. Charger.counter.WatchDogLeak ++ ;
  8577. }
  8578. }
  8579. }
  8580. }
  8581. #endif //CCID_PROTECT
  8582. }
  8583. //-------------------------------------------------------------------------------------------------
  8584. void SetADCWDGBoundary(void)
  8585. {
  8586. if(!ADCWDGHighThreshold)
  8587. {
  8588. //220V 3200 820
  8589. //110V 2640 1400
  8590. ADCWDGHighThreshold = 3200;
  8591. ADCWDGLowThreshold = 820;
  8592. }
  8593. ADC_AnalogWDGConfTypeDef AnalogWDGConfig = {0};
  8594. /*
  8595. * Configure the analog watchdog
  8596. */
  8597. AnalogWDGConfig.WatchdogMode = ADC_ANALOGWATCHDOG_SINGLE_REG;
  8598. AnalogWDGConfig.HighThreshold = ADCWDGHighThreshold;
  8599. AnalogWDGConfig.LowThreshold = ADCWDGLowThreshold;
  8600. AnalogWDGConfig.Channel = ADC_CHANNEL_9;
  8601. AnalogWDGConfig.ITMode = ENABLE;
  8602. if (HAL_ADC_AnalogWDGConfig(&hadc3, &AnalogWDGConfig) != HAL_OK)
  8603. {
  8604. Error_Handler();
  8605. }
  8606. }
  8607. //-------------------------------------------------------------------------------------------------
  8608. void vApplicationStackOverflowHook( TaskHandle_t xTask, signed char *pcTaskName )
  8609. {
  8610. DEBUG_ERROR("Stack overflow: %s\r\n", pcTaskName);
  8611. osDelay(500);
  8612. }
  8613. //-------------------------------------------------------------------------------------------------
  8614. void getlastRecord(void)
  8615. {
  8616. for(uint16_t idx=0;idx<(W25Q16FV_BLOCK_SIZE>>4);idx++)
  8617. {
  8618. if((Charger.memory.hisCharging.data.item[idx].Duration == 0xffffffff) &&
  8619. (Charger.memory.hisCharging.data.item[idx].powerSum == 0xffff) &&
  8620. (Charger.memory.hisCharging.data.item[idx].stopStatusCode == 0xffffffff) )
  8621. {
  8622. DEBUG_INFO("Charging record last index: %d\r\n", (idx));
  8623. break;
  8624. }
  8625. }
  8626. for(uint16_t idx=0;idx<(W25Q16FV_BLOCK_SIZE>>6);idx++)
  8627. {
  8628. if((Charger.memory.hisAlarm.data.item[idx].alarmCode == 0xffffffff) &&
  8629. (Charger.memory.hisAlarm.data.item[idx].mode == 0xff))
  8630. {
  8631. DEBUG_INFO("Alarm record last index: %d\r\n", (idx));
  8632. break;
  8633. }
  8634. }
  8635. }
  8636. //-------------------------------------------------------------------------------------------------
  8637. void getRotarySwitchSetting(void)
  8638. {
  8639. uint8_t tmpBuf = 0;
  8640. tmpBuf += (HAL_GPIO_ReadPin(IN_AC_Current_Set1_GPIO_Port, IN_AC_Current_Set1_Pin)?0:1)<<0;
  8641. tmpBuf += (HAL_GPIO_ReadPin(IN_AC_Current_Set2_GPIO_Port, IN_AC_Current_Set2_Pin)?0:1)<<1;
  8642. tmpBuf += (HAL_GPIO_ReadPin(IN_AC_Current_Set3_GPIO_Port, IN_AC_Current_Set3_Pin)?0:1)<<2;
  8643. tmpBuf += (HAL_GPIO_ReadPin(IN_AC_Current_Set4_GPIO_Port, IN_AC_Current_Set4_Pin)?0:1)<<3;
  8644. Charger.isDebugEnable = OFF;
  8645. #ifdef FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  8646. if (!(((sbyte)tmpBuf) >= EMPX10_RS485_ADDR_MIN && ((sbyte)tmpBuf) <= EMPX10_RS485_ADDR_MAX))
  8647. {
  8648. Charger.isRotarySwitchError = ON;
  8649. }
  8650. DEBUG_INFO("Rotary switch (RS485 Addr) Setting: %d (%s)\r\n", tmpBuf, Charger.isRotarySwitchError ? "NG" : "OK");
  8651. Charger.m_EMPX10_RS485_ClientAddr = tmpBuf;
  8652. #ifdef FUNC_EMPX10_SET_FIXED_CURRENT_20A
  8653. Charger.maxRatingCurrent = 20;
  8654. #else
  8655. Charger.maxRatingCurrent = EVSE_MODEL_CURRENT_RATING;
  8656. #endif
  8657. #else //FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  8658. switch(tmpBuf)
  8659. {
  8660. case 0x00:
  8661. Charger.maxRatingCurrent = EVSE_MODEL_CURRENT_RATING;
  8662. DEBUG_INFO("Set maxRatingCurrent is Test mode\r\n");
  8663. Charger.isDebugEnable = ON;
  8664. break;
  8665. case 0x01:
  8666. Charger.maxRatingCurrent = (6>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:6);
  8667. Charger.isDebugEnable = OFF;
  8668. break;
  8669. case 0x02:
  8670. Charger.maxRatingCurrent = (8>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:8);
  8671. Charger.isDebugEnable = OFF;
  8672. break;
  8673. case 0x03:
  8674. Charger.maxRatingCurrent = (10>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:10);
  8675. Charger.isDebugEnable = OFF;
  8676. break;
  8677. case 0x04:
  8678. Charger.maxRatingCurrent = (13>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:13);
  8679. Charger.isDebugEnable = OFF;
  8680. break;
  8681. case 0x05:
  8682. Charger.maxRatingCurrent = (16>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:16);
  8683. Charger.isDebugEnable = OFF;
  8684. break;
  8685. case 0x06:
  8686. Charger.maxRatingCurrent = (20>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:20);
  8687. Charger.isDebugEnable = OFF;
  8688. break;
  8689. case 0x07:
  8690. Charger.maxRatingCurrent = (25>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:25);
  8691. Charger.isDebugEnable = OFF;
  8692. break;
  8693. case 0x08:
  8694. Charger.maxRatingCurrent = (32>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:32);
  8695. Charger.isDebugEnable = OFF;
  8696. break;
  8697. case 0x09:
  8698. Charger.maxRatingCurrent = (32>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:32);
  8699. Charger.isDebugEnable = OFF;
  8700. break ;
  8701. case 0x0a:
  8702. Charger.maxRatingCurrent = (30>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:30); // for JARI
  8703. Charger.isDebugEnable = OFF;
  8704. break ;
  8705. case 0x0b:
  8706. case 0x0c:
  8707. case 0x0d:
  8708. case 0x0e:
  8709. Charger.maxRatingCurrent = (32>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:32);
  8710. Charger.isRotarySwitchError = ON ;
  8711. Charger.isDebugEnable = OFF;
  8712. break ;
  8713. case 0x0f:
  8714. Charger.maxRatingCurrent = EVSE_MODEL_CURRENT_RATING;
  8715. DEBUG_INFO("Knob error Set maxRatingCurrent defualt 32A\r\n");
  8716. DEBUG_INFO("EVSE is Set Slave Mode\r\n");
  8717. Charger.isDebugEnable = OFF;
  8718. break;
  8719. default:
  8720. Charger.maxRatingCurrent = EVSE_MODEL_CURRENT_RATING;
  8721. DEBUG_INFO("knob default Set maxRatingCurrent defualt 32A\r\n");
  8722. Charger.isDebugEnable = OFF;
  8723. break;
  8724. }
  8725. #endif //FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  8726. DEBUG_INFO("Current rating value: %d A\r\n", Charger.maxRatingCurrent);
  8727. Charger.memory.EVSE_Config.data.item.MaxChargingCurrent = Charger.maxRatingCurrent;
  8728. #ifdef MODIFY_AC_EVSE_STATUS_MAX_CHARGING_CURRENT_VARIABLE
  8729. Charger.AC_MaxChargingCurrentOrDuty = Charger.maxRatingCurrent;
  8730. #endif
  8731. }
  8732. //------------------------------------------------------------------------------//
  8733. 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 )
  8734. {
  8735. *GainA = (float)((float)(SpecData_H -SpecData_L) / (float)( MCUData_H - MCUData_L)) ;
  8736. *GainB = (float)(SpecData_H - (float)(*GainA * MCUData_H)) ;
  8737. }
  8738. //------------------------------------------------------------------------------//
  8739. 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 )
  8740. {
  8741. *GainA = (float)((float)(SpecData_H -SpecData_L) / (float)( MCUData_H - MCUData_L)) ;
  8742. if (SpecData_H > (*GainA * MCUData_H))
  8743. {
  8744. *GainB = SpecData_H - (*GainA * MCUData_H) ;
  8745. *PosorNeg = ON ;
  8746. }
  8747. else
  8748. {
  8749. *GainB = (*GainA * MCUData_H) - SpecData_H ;
  8750. *PosorNeg = OFF ;
  8751. }
  8752. }
  8753. //------------------------------------------------------------------------------
  8754. uint8_t Array_data_Check ( uint8_t *s1, uint8_t *s2 )
  8755. {
  8756. uint8_t compare = 0;
  8757. uint8_t result = PASS;
  8758. for(compare=0;compare<sizeof(s1);compare++)
  8759. {
  8760. if(s1[compare]!=s2[compare])
  8761. {
  8762. result = FAIL;
  8763. break;
  8764. }
  8765. }
  8766. return result;
  8767. }
  8768. //------------------------------------------------------------------------------
  8769. void Relay_Control_Function (void)
  8770. {
  8771. switch(Charger.Relay_Action)
  8772. {
  8773. case GPIO_RELAY_ACTION_ON:
  8774. if(Relay1_Action_Get() == GPIO_RELAY1_ACTION_OFF && Relay2_Action_Get() == GPIO_RELAY2_ACTION_OFF)
  8775. {
  8776. Relay_Action_Set(GPIO_RELAY1_ACTION_ON);
  8777. Relay_Action_Set(GPIO_RELAY2_ACTION_ON);
  8778. }
  8779. if(timer[TIMER_IDX_RELAY_1].isAlarm == ON && timer[TIMER_IDX_RELAY_2].isAlarm == ON)
  8780. {
  8781. Relay_Action_Set(GPIO_RELAY1_ACTION_HOLD);
  8782. Relay_Action_Set(GPIO_RELAY2_ACTION_HOLD);
  8783. timerDisable(TIMER_IDX_RELAY_1);
  8784. timerDisable(TIMER_IDX_RELAY_2);
  8785. Charger.Relay_isOperationCompleted |= 0x11 ;
  8786. }
  8787. break;
  8788. case GPIO_RELAY_ACTION_OFF:
  8789. timer[TIMER_IDX_RELAY_1].isEnable = OFF;
  8790. timer[TIMER_IDX_RELAY_2].isEnable = OFF;
  8791. if(Relay1_Action_Get() != GPIO_RELAY1_ACTION_OFF)
  8792. {
  8793. Relay_Action_Set(GPIO_RELAY1_ACTION_OFF);
  8794. Relay_Action_Set(GPIO_RELAY2_ACTION_OFF);
  8795. }
  8796. osDelay(10);
  8797. Charger.Relay_isOperationCompleted &= 0x00 ;
  8798. #ifdef FUNC_GET_TRIP_TIME
  8799. if (Charger.m_TripBeg > 0 && Charger.m_TripEnd == HTK_U32_MAX)
  8800. {
  8801. Charger.m_TripEnd = HAL_GetTick();
  8802. }
  8803. #endif
  8804. break;
  8805. default:
  8806. break;
  8807. }
  8808. }
  8809. //--------------------------------------------------------------------------------
  8810. uint8_t WatchDogLeakRawDataCheck_Event(void)
  8811. {
  8812. uint8_t result = PASS ;
  8813. for(uint16_t idx = 0 ; idx < ADC2_SAMPLE_COUNT; idx++)
  8814. {
  8815. if (ADC2_Buffer_Each[1][idx] >= 4000)
  8816. {
  8817. result = FAIL;
  8818. break;
  8819. }
  8820. }
  8821. return result ;
  8822. }
  8823. //--------------------------------------------------------------------------------
  8824. uint16_t getBrightnessDuty(uint16_t max_duty)
  8825. {
  8826. uint8_t Lv_Brightness = 3;
  8827. uint16_t duty = 1000;
  8828. 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;
  8829. switch(Lv_Brightness)
  8830. {
  8831. case 0:
  8832. duty = max_duty/4;
  8833. break;
  8834. case 1:
  8835. duty = max_duty/3;
  8836. break;
  8837. case 2:
  8838. duty = max_duty/2;
  8839. break;
  8840. case 3:
  8841. duty = max_duty;
  8842. break;
  8843. default:
  8844. duty = max_duty;
  8845. break;
  8846. }
  8847. return duty;
  8848. }
  8849. //--------------------------------------------------------------------------------
  8850. void parseVersionInfoFromModelname(void)
  8851. {
  8852. //--------------------------------------
  8853. // Version Naming Rule "tx.yz.ab.cddd.vv"
  8854. //Version Naming Rule "a"
  8855. for(uint8_t idx=0;idx<3;idx++)
  8856. {
  8857. switch(Charger.memory.EVSE_Config.data.item.ModelName[7+idx])
  8858. {
  8859. case '1':
  8860. // J1772 Plug
  8861. Charger.Ver_FW[6] = '4';
  8862. break;
  8863. case '2':
  8864. // J1772 Socket
  8865. Charger.Ver_FW[6] = '1';
  8866. break;
  8867. case '3':
  8868. // CE Plug
  8869. Charger.Ver_FW[6] = '5';
  8870. break;
  8871. case '4':
  8872. // CE Socket
  8873. Charger.Ver_FW[6] = '2';
  8874. break;
  8875. case '5':
  8876. // GB Plug
  8877. Charger.Ver_FW[6] = '6';
  8878. break;
  8879. case '6':
  8880. // GB Socket
  8881. Charger.Ver_FW[6] = '3';
  8882. break;
  8883. #ifdef FUNC_ADD_FW_VER_WITH_GUN_TYPE_NACS
  8884. case '9':
  8885. // NACS
  8886. Charger.Ver_FW[6] = '9';
  8887. break;
  8888. #endif
  8889. }
  8890. }
  8891. //Version Naming Rule "c"
  8892. switch(Charger.memory.EVSE_Config.data.item.ModelName[10])
  8893. {
  8894. case 'B':
  8895. case 'U':
  8896. //Blue tooth
  8897. Charger.Ver_FW[9] = '3';
  8898. break;
  8899. case 'W':
  8900. // WIFI
  8901. Charger.Ver_FW[9] = '1';
  8902. break;
  8903. case 'T':
  8904. // 3G/4G
  8905. Charger.Ver_FW[9] = '2';
  8906. break;
  8907. #ifdef FUNC_ADD_FW_VER_WITH_NET_TYPE_WIFI_AND_4G
  8908. case 'D':
  8909. // WIFI + 4G
  8910. Charger.Ver_FW[9] = '5';
  8911. break;
  8912. #endif
  8913. default:
  8914. // LAN
  8915. Charger.Ver_FW[9] = '0';
  8916. break;
  8917. }
  8918. //Version Naming Rule "d3" , Get rating power from model name
  8919. memcpy(&Charger.Ver_FW[10], &Charger.memory.EVSE_Config.data.item.ModelName[4], 0x03);
  8920. // Get vender code from model name
  8921. memcpy(&Charger.Ver_FW[14], &Charger.memory.EVSE_Config.data.item.ModelName[12], 0x02);
  8922. //--------------------------------------
  8923. #ifdef MODIFY_OCP_PROC_WITH_MODELNAME_IDX_3_EURO_SPEC
  8924. HTK_BOOL bModelNameWithEuroSpecOCP = HTK_FALSE;
  8925. #endif
  8926. // CCID_Module_Type check
  8927. Charger.CCID_Module_Type = CCID_MODULE_CORMEX ;
  8928. switch(Charger.memory.EVSE_Config.data.item.ModelName[3])
  8929. {
  8930. case 'E': //CE / European Market
  8931. case 'T': //TR25 / Singapor Market
  8932. case 'Z': //EV ready / French Market
  8933. #ifdef MODIFY_OCP_PROC_WITH_MODELNAME_IDX_3_EURO_SPEC
  8934. case 'M': //E + MID Meter
  8935. case 'P': //E + PTB certification
  8936. case 'I': //Z + TIC Meter pcb
  8937. case 'F': //Z + MID Meter
  8938. bModelNameWithEuroSpecOCP = HTK_TRUE;
  8939. #endif
  8940. Charger.CCID_Module_Type = CCID_MODULE_VAC ;
  8941. ////#ifdef MODIFY_OCP_MAGNIFICATION
  8942. //// Charger.OCP_Magnification = 125 ;
  8943. ////#else
  8944. Charger.OCP_Magnification = 115 ;
  8945. ////#endif
  8946. break;
  8947. case 'U':
  8948. //UL / North America Market
  8949. Charger.CCID_Module_Type = CCID_MODULE_CORMEX ;
  8950. Charger.alarm_spec.Current_LEAK_AC = 1800 ; // unit 0.01mA
  8951. Charger.OCP_Magnification = 110 ;
  8952. break;
  8953. case 'G':
  8954. //GB / China Market
  8955. Charger.CCID_Module_Type = CCID_MODULE_CORMEX ;
  8956. Charger.alarm_spec.Current_LEAK_AC = 2500 ; // unit 0.01mA
  8957. Charger.OCP_Magnification = 110 ;
  8958. break;
  8959. case 'C':
  8960. //CNS / Taiwan Market
  8961. #ifdef FUNC_REPLACE_CORMEX_WITH_VAC_FOR_CNS_MODEL
  8962. Charger.CCID_Module_Type = CCID_MODULE_VAC;
  8963. #else
  8964. Charger.CCID_Module_Type = CCID_MODULE_CORMEX ;
  8965. #endif
  8966. Charger.alarm_spec.Current_LEAK_AC = 2500 ; // unit 0.01mA
  8967. Charger.OCP_Magnification = 110 ;
  8968. break;
  8969. case 'J':
  8970. //JARI / Japan Market
  8971. Charger.CCID_Module_Type = CCID_MODULE_CORMEX ;
  8972. Charger.alarm_spec.Current_LEAK_AC = 2500 ; // unit 0.01mA
  8973. Charger.OCP_Magnification = 110 ;
  8974. break;
  8975. case 'K':
  8976. //KC / Korea Market
  8977. Charger.CCID_Module_Type = CCID_MODULE_CORMEX ;
  8978. Charger.alarm_spec.Current_LEAK_AC = 1800 ; // unit 0.01mA
  8979. Charger.OCP_Magnification = 110 ;
  8980. break;
  8981. case 'B':
  8982. //British / UK Market
  8983. Charger.CCID_Module_Type = CCID_MODULE_CORMEX ;
  8984. Charger.alarm_spec.Current_LEAK_AC = 1800 ; // unit 0.01mA
  8985. Charger.OCP_Magnification = 110 ;
  8986. break;
  8987. default:
  8988. Charger.CCID_Module_Type = CCID_MODULE_CORMEX ;
  8989. Charger.alarm_spec.Current_LEAK_AC = 1800 ; // unit 0.01mA
  8990. Charger.OCP_Magnification = 110 ;
  8991. break;
  8992. }
  8993. #ifdef MODIFY_OCP_PROC_WITH_MODELNAME_IDX_3_EURO_SPEC
  8994. Charger.m_bModelNameWithEuroSpecOCP = bModelNameWithEuroSpecOCP;
  8995. XP("#Use EuropeanSpecOCP: %s\r\n", Charger.m_bModelNameWithEuroSpecOCP ? "True" : "False");
  8996. #endif
  8997. #ifdef FUNC_AUTO_MODIFY_CCID_MODULE
  8998. XP("#Use CCID Module (ModelName: %s): %s\r\n", Charger.memory.EVSE_Config.data.item.ModelName, CCID_MODULE_TYPE_STR);
  8999. if (Charger.m_bCCID_MustModify && Charger.m_CCID_ModuleTestOK != CCID_MODULE_UNKNOW)
  9000. {
  9001. Charger.CCID_Module_Type = Charger.m_CCID_ModuleTestOK;
  9002. XP("#Use CCID Module (AutoModify): %s\r\n", CCID_MODULE_TYPE_STR);
  9003. }
  9004. #endif
  9005. #ifdef FUNC_RS485_SLAVE
  9006. Charger.m_bRS485SlaveForDCModel =
  9007. (
  9008. (Charger.memory.EVSE_Config.data.item.ModelName[0] == 'D') //response to DC Model
  9009. ) ? 1 : 0;
  9010. Charger.m_RS485SlaveAddr = (Charger.m_bRS485SlaveForDCModel ? PROTOCOL_ADDR_AC_MAINBOARD : PROTOCOL_ADDR);
  9011. #endif
  9012. #ifdef FUNC_FORCE_RUN_CSU_MODE_WITH_DC_MODELNAME
  9013. Charger.m_bModelNameDC =
  9014. (
  9015. (Charger.memory.EVSE_Config.data.item.ModelName[0] == 'D') //response to DC Model
  9016. ) ? 1 : 0;
  9017. Charger.m_bModelNameDCReadOK = 1;
  9018. XP("#ModelNameDC: %s\r\n", Charger.m_bModelNameDC ? "True" : "False");
  9019. Update_McuCtrlMode();
  9020. #endif
  9021. #ifdef FUNC_CP_ADC_MODIFY
  9022. #ifdef NEW_CP_SPEC
  9023. CpDetectModuleInitialize(Charger.memory.EVSE_Config.data.item.ModelName[3]);
  9024. Charger.m_bCpDetectModuleInitOK = 1;
  9025. #endif //NEW_CP_SPEC
  9026. #endif //FUNC_CP_ADC_MODIFY
  9027. #ifdef FUNC_RULE_GB_202108
  9028. Charger.m_bSafetyRegulationGB = (Charger.memory.EVSE_Config.data.item.ModelName[3] == 'G');
  9029. #endif
  9030. #ifdef FUNC_UPDATE_ROTARY_SWITCH_SETTING_AFTER_PARSE_MODELNAME
  9031. getRotarySwitchSetting();
  9032. #endif
  9033. }
  9034. void HTK_ByteArray2HexStr(const u8* buf, const u16 BegIdx, const u16 len, char* OutStr, u16 nMaxBytesPerLine, HTK_BOOL bSpace)
  9035. {
  9036. char sTmp[8] = { 0 };
  9037. //char sTmp[8] = { 0 };
  9038. strcpy(OutStr, "\0");
  9039. for (u16 i = BegIdx; i < (BegIdx + len); i++)
  9040. {
  9041. sprintf(sTmp, bSpace ? "%02X " : "%02X", buf[i]);
  9042. strcat(OutStr, sTmp);
  9043. if ((i - BegIdx) % nMaxBytesPerLine == (nMaxBytesPerLine - 1))
  9044. {
  9045. strcat(OutStr, "\r\n");
  9046. }
  9047. }
  9048. }
  9049. void HTK_ByteArray2HexStr_XP(const char* Title, const u8* buf, const u16 BegIdx, const u16 len)
  9050. {
  9051. //char OutStr[256] = { 0 };
  9052. static char OutStr[1024 * 3 + 8] = { 0 };
  9053. HTK_ByteArray2HexStr(buf, BegIdx, len, OutStr, 0, HTK_TRUE);
  9054. XP("[%s] %s(%d)\r\n", (Title == NULL ? "" : Title), OutStr, len);
  9055. }
  9056. void HTK_ByteArray2HexStr_XT(const char* Title, const u8* buf, const u16 BegIdx, const u16 len)
  9057. {
  9058. //char OutStr[256] = { 0 };
  9059. static char OutStr[1024 * 3 + 8] = { 0 };
  9060. HTK_ByteArray2HexStr(buf, BegIdx, len, OutStr, 0, HTK_TRUE);
  9061. XT("[%s] %s(%d)\r\n", (Title == NULL ? "" : Title), OutStr, len);
  9062. }
  9063. #ifdef ENABLE_PRINT_IMCP_MSG
  9064. int CheckPrintImcpMsg(uint8_t MsgID)
  9065. {
  9066. #ifdef ENABLE_PRINT_IMCP_SPEC_MSG
  9067. return
  9068. (
  9069. // MsgID == PROTOCOL_MESSAGE_CONFIG_PARAMETER
  9070. // || MsgID == PROTOCOL_MESSAGE_QUERY_METER_IC_MEAS_PARAMETER
  9071. // || MsgID == PROTOCOL_MESSAGE_QUERY_METER_IC_CALI_PARAMETER
  9072. // || MsgID == PROTOCOL_MESSAGE_CONFIG_METER_IC_CALI_PARAMETER
  9073. MsgID == PROTOCOL_MESSAGE_CONFIG_RELAY_OUTPUT
  9074. || MsgID == PROTOCOL_MESSAGE_CONFIG_MAX_CURRENT_PWM_DUTY
  9075. || MsgID == PROTOCOL_MESSAGE_CONFIG_LEGACY_REQUEST
  9076. );
  9077. #else
  9078. return true;
  9079. #endif //ENABLE_PRINT_IMCP_SPEC_MSG
  9080. }
  9081. #endif //ENABLE_PRINT_IMCP_MSG
  9082. #ifdef MODIFY_COLD_LOAD_PICKUP_DELAY
  9083. void ColdLoadPickup_Delay(const char* Description)
  9084. {
  9085. //osDelay((rand()%175000)+5000);
  9086. int r = rand();
  9087. if (r < 0)
  9088. {
  9089. r = (-1) * r;
  9090. }
  9091. //uint32_t dt = (r % 175000) + 5000; //ms
  9092. uint32_t dt = (r % (COLD_LOAD_PICKUP_DELAY_TIME_MAX_RANGE_SEC * 1000)) + 5000; //ms
  9093. XP("ColdLoadPickup_Delay(ms) [%s]: %d\r\n", Description, dt);
  9094. osDelay(dt);
  9095. }
  9096. #endif
  9097. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  9098. uint16_t GetCpPwmDuty(uint16_t Curr)
  9099. {
  9100. uint16_t rtn = (uint16_t)((Curr / 0.6) * 10);
  9101. XP("#GetCpPwmDuty(%d) => %d\r\n", Curr, rtn);
  9102. return rtn;
  9103. }
  9104. #endif
  9105. #ifdef HTK
  9106. HTK_BOOL HTK_IsInTime(u32 BegTick, u32 MaxTick)
  9107. {
  9108. u32 EndTick = HAL_GetTick();
  9109. u32 ElapseTick = (EndTick >= BegTick ? EndTick - BegTick : HTK_U32_MAX - BegTick + EndTick);
  9110. return ElapseTick <= MaxTick ? HTK_TRUE : HTK_FALSE;
  9111. }
  9112. HTK_BOOL HTK_IsTimeout(u32 BegTick, u32 MaxTick)
  9113. {
  9114. u32 EndTick = HAL_GetTick();
  9115. u32 ElapseTick = (EndTick >= BegTick ? EndTick - BegTick : HTK_U32_MAX - BegTick + EndTick);
  9116. return ElapseTick > MaxTick ? HTK_TRUE : HTK_FALSE;
  9117. }
  9118. void HTK_Timer_Reset(PHTK_Timer p)
  9119. {
  9120. XP("#<Timer reset>\r\n\r\n");
  9121. p->m_bStart = HTK_FALSE;
  9122. p->m_bTimeout = HTK_FALSE;
  9123. p->m_BegTick = 0;
  9124. p->m_MaxTick = 0;
  9125. }
  9126. void HTK_Timer_Start(PHTK_Timer p, u32 MaxTick)
  9127. {
  9128. XP("#<Timer start>\r\n\r\n");
  9129. p->m_bStart = HTK_TRUE;
  9130. p->m_BegTick = HAL_GetTick();
  9131. p->m_MaxTick = MaxTick;
  9132. }
  9133. HTK_BOOL HTK_Timer_CheckTimeout(PHTK_Timer p)
  9134. {
  9135. // return HTK_IsTimeout(p->m_BegTick, p->m_MaxTick);
  9136. HTK_BOOL rtn = HTK_IsTimeout(p->m_BegTick, p->m_MaxTick);
  9137. if (rtn == HTK_TRUE)
  9138. {
  9139. XP("#<Timer timeout>\r\n\r\n");
  9140. p->m_bTimeout = HTK_TRUE;
  9141. }
  9142. return rtn;
  9143. }
  9144. uint8_t HTK_CheckSum_Calulate(uint8_t* pBeg, uint32_t len)
  9145. {
  9146. if (pBeg != NULL)
  9147. {
  9148. uint8_t CheckSum = 0;
  9149. for (uint32_t i = 0; i < len; i++)
  9150. {
  9151. CheckSum ^= *(pBeg + i);
  9152. }
  9153. return CheckSum;
  9154. }
  9155. else
  9156. {
  9157. return 0;
  9158. }
  9159. }
  9160. HTK_BOOL HTK_CheckSum_Update(uint8_t* pBeg, uint32_t len, uint8_t* pCheckSum)
  9161. {
  9162. if (pBeg != NULL && pCheckSum != NULL)
  9163. {
  9164. uint8_t CheckSum = HTK_CheckSum_Calulate(pBeg, len);
  9165. *pCheckSum = CheckSum;
  9166. return HTK_TRUE;
  9167. }
  9168. else
  9169. {
  9170. return HTK_FALSE;
  9171. }
  9172. }
  9173. HTK_BOOL HTK_CheckSum_IsOK(uint8_t* pBeg, uint32_t len, uint8_t* pCheckSum)
  9174. {
  9175. if (pBeg != NULL && pCheckSum != NULL)
  9176. {
  9177. uint8_t CheckSum = HTK_CheckSum_Calulate(pBeg, len);
  9178. return (*pCheckSum == CheckSum) ? HTK_TRUE : HTK_FALSE;
  9179. }
  9180. else
  9181. {
  9182. return HTK_FALSE;
  9183. }
  9184. }
  9185. static HTK_MallocHandle s_MallocHandle;
  9186. void HTK_Malloc_Init(void)
  9187. {
  9188. memset(&s_MallocHandle, 0, sizeof(s_MallocHandle));
  9189. //XP("HTK_Malloc_Init(): %d\r\n", HTK_Malloc_BlockTest(1024));
  9190. XP("HTK_Malloc_Init(): %d\r\n", HTK_Malloc_BlockTest(8000));
  9191. }
  9192. void* HTK_Malloc(s32 size)
  9193. {
  9194. void* p = malloc(size);
  9195. if (p != NULL)
  9196. {
  9197. s_MallocHandle.m_Allocated += size;
  9198. s_MallocHandle.m_Reserved -= size;
  9199. memset(p, 0, size);
  9200. }
  9201. XP("HTK_Malloc(%d) %s\r\n", size, (p == NULL) ? "*** NG ***" : "OK");
  9202. return p;
  9203. }
  9204. s32 HTK_Malloc_BlockTest(s32 size)
  9205. {
  9206. int Total = 0;
  9207. for (int i = 1; ; i++)
  9208. {
  9209. void* p = HTK_Malloc(size);
  9210. if (p == NULL)
  9211. {
  9212. break;
  9213. }
  9214. else
  9215. {
  9216. Total += size;
  9217. }
  9218. }
  9219. s_MallocHandle.m_Total = Total;
  9220. s_MallocHandle.m_Allocated = 0;
  9221. s_MallocHandle.m_Reserved = Total;
  9222. return Total;
  9223. }
  9224. #endif //HTK
  9225. #ifdef MODIFY_CPTASK_HEAD
  9226. void Proc_CpTaskHead(HTK_BOOL bCSU)
  9227. {
  9228. if(Charger.Alarm_Code > 0) // alarm occur
  9229. {
  9230. if(Charger.Mode != MODE_ALARM)
  9231. {
  9232. #ifdef FUNC_BLOCK_CSU_CMD_SET_DUTY_WHEN_ALARM_CP_F_STATE
  9233. Charger.m_bBlockCsuCpPwmDutyWhenAlarmCpF = HTK_TRUE;
  9234. #endif
  9235. HAL_GPIO_WritePin(OUT_StateE_GPIO_Port, OUT_StateE_Pin, GPIO_PIN_SET);
  9236. setChargerMode(MODE_ALARM);
  9237. user_pwm_setvalue(PWM_CH_CP, 0);
  9238. timerEnable(TIMER_IDX_STATE_E, 4000);
  9239. Charger.isAlarmSetPWM = OFF ;
  9240. }
  9241. // CP E or F to X1
  9242. if(timer[TIMER_IDX_STATE_E].isAlarm == ON)
  9243. {
  9244. HAL_GPIO_WritePin(OUT_StateE_GPIO_Port, OUT_StateE_Pin, GPIO_PIN_RESET);
  9245. if ( Charger.isAlarmSetPWM == OFF )
  9246. {
  9247. Charger.isAlarmSetPWM = ON ;
  9248. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  9249. }
  9250. #ifdef FUNC_BLOCK_CSU_CMD_SET_DUTY_WHEN_ALARM_CP_F_STATE
  9251. if (Charger.m_bBlockCsuCpPwmDutyWhenAlarmCpF)
  9252. {
  9253. Charger.m_bBlockCsuCpPwmDutyWhenAlarmCpF = HTK_FALSE;
  9254. }
  9255. #endif
  9256. }
  9257. }
  9258. else
  9259. {
  9260. if(Charger.Mode != MODE_ALARM)
  9261. {
  9262. //if (!bCSU) //only in AX code
  9263. {
  9264. HAL_GPIO_WritePin(OUT_StateE_GPIO_Port, OUT_StateE_Pin, GPIO_PIN_RESET); //CCS communication, so skip this line
  9265. }
  9266. timerDisable(TIMER_IDX_STATE_E);
  9267. }
  9268. #ifdef FIX_OUTP_PWM_WHEN_ALARM_RECOVER_IN_4SEC
  9269. #ifdef FUNC_BLOCK_CSU_CMD_SET_DUTY_WHEN_ALARM_CP_F_STATE
  9270. if (Charger.m_bBlockCsuCpPwmDutyWhenAlarmCpF)
  9271. {
  9272. Charger.m_bBlockCsuCpPwmDutyWhenAlarmCpF = HTK_FALSE;
  9273. }
  9274. #endif
  9275. #endif
  9276. }
  9277. }
  9278. #endif //MODIFY_CPTASK_HEAD
  9279. #ifdef FUNC_FORCE_RUN_CSU_MODE_WITH_DC_MODELNAME
  9280. void Update_McuCtrlMode(void)
  9281. {
  9282. while (!Charger.m_bModelNameDCReadOK)
  9283. {
  9284. osDelay(100);
  9285. }
  9286. #ifdef FUNC_EMPX10_CUSTOMIZE_RUN_CSU_MODE
  9287. {
  9288. Charger.memory.EVSE_Config.data.item.MCU_Control_Mode = MCU_CONTROL_MODE_CSU;
  9289. DEBUG_INFO("#MCU_CONTROL_MODE_CSU\r\n");
  9290. }
  9291. #else
  9292. if (HAL_GPIO_ReadPin(IN_ACT_REQ_GPIO_Port, IN_ACT_REQ_Pin) == GPIO_PIN_RESET ||
  9293. Charger.m_bModelNameDC == 1)
  9294. {
  9295. Charger.memory.EVSE_Config.data.item.MCU_Control_Mode = MCU_CONTROL_MODE_CSU;
  9296. DEBUG_INFO("#MCU_CONTROL_MODE_CSU\r\n");
  9297. }
  9298. else
  9299. {
  9300. Charger.memory.EVSE_Config.data.item.MCU_Control_Mode = MCU_CONTROL_MODE_NO_CSU;
  9301. DEBUG_INFO("#MCU_CONTROL_MODE_NO_CSU\r\n");
  9302. // if (Charger.memory.EVSE_Config.data.item.AuthMode > AUTH_MODE_FREE)
  9303. // {
  9304. // Charger.memory.EVSE_Config.data.item.AuthMode = AUTH_MODE_FREE ;
  9305. // DEBUG_INFO("#AuthMode set defualt AUTH_MODE_FREE \r\n");
  9306. // }
  9307. //
  9308. // if (Charger.memory.EVSE_Config.data.item.OfflinePolicy > RFID_USER_AUTH_NO_CHARGING)
  9309. // {
  9310. // Charger.memory.EVSE_Config.data.item.OfflinePolicy = RFID_USER_AUTH_FREE ;
  9311. // DEBUG_INFO("#OfflinePolicy set defualt RFID_USER_AUTH_FREE \r\n");
  9312. // }
  9313. }
  9314. #endif //FUNC_EMPX10_CUSTOMIZE_RUN_CSU_MODE
  9315. }
  9316. #endif //FUNC_FORCE_RUN_CSU_MODE_WITH_DC_MODELNAME
  9317. #ifdef MODIFY_DC_RS485_UPGRADE_ISSUE
  9318. void RS485_TX_Enable(void)
  9319. {
  9320. HAL_GPIO_WritePin(OUT_Meter_485_DE_GPIO_Port, OUT_Meter_485_DE_Pin, GPIO_PIN_SET);
  9321. HAL_GPIO_WritePin(OUT_Meter_485_RE_GPIO_Port, OUT_Meter_485_RE_Pin, GPIO_PIN_SET);
  9322. osDelay(1);
  9323. }
  9324. void RS485_TX_Disable(void)
  9325. {
  9326. HAL_GPIO_WritePin(OUT_Meter_485_DE_GPIO_Port, OUT_Meter_485_DE_Pin, GPIO_PIN_RESET);
  9327. HAL_GPIO_WritePin(OUT_Meter_485_RE_GPIO_Port, OUT_Meter_485_RE_Pin, GPIO_PIN_RESET);
  9328. }
  9329. void Prefix_UartTX(uint8_t* TxBuf, uint8_t* RxBuf)
  9330. {
  9331. #ifdef FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  9332. if (RxBuf == UART_IAP_rx_buffer)
  9333. {
  9334. TxBuf[1] = RxBuf[2];
  9335. }
  9336. else
  9337. #endif
  9338. if (RxBuf == UART_RS485_rx_buffer)
  9339. {
  9340. RS485_TX_Enable();
  9341. #ifdef FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  9342. //TxBuf[1] = RxBuf[2]; //Charger.m_EMPX10_RS485_ClientAddr;
  9343. TxBuf[1] = Charger.m_EMPX10_RS485_ClientAddr;
  9344. #else
  9345. TxBuf[1] = Charger.m_RS485SlaveAddr; //Update Slave Address
  9346. #endif
  9347. }
  9348. }
  9349. void Postfix_UartTX(uint8_t* TxBuf, uint8_t* RxBuf)
  9350. {
  9351. if (RxBuf == UART_RS485_rx_buffer)
  9352. {
  9353. RS485_TX_Disable();
  9354. }
  9355. }
  9356. #endif //MODIFY_DC_RS485_UPGRADE_ISSUE
  9357. /* USER CODE END Application */
  9358. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/