freertos.c 422 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * File Name : freertos.c
  5. * Description : Code for freertos applications
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under Ultimate Liberty license
  13. * SLA0044, the "License"; You may not use this file except in compliance with
  14. * the License. You may obtain a copy of the License at:
  15. * www.st.com/SLA0044
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "FreeRTOS.h"
  22. #include "task.h"
  23. #include "main.h"
  24. #include "cmsis_os.h"
  25. /* Private includes ----------------------------------------------------------*/
  26. /* USER CODE BEGIN Includes */
  27. #include "adc.h"
  28. #include "crc.h"
  29. #include "flash_if.h"
  30. #include "gpio.h"
  31. #include "iwdg.h"
  32. #include "rtc.h"
  33. #include "tim.h"
  34. #include "usart.h"
  35. #include "cp_detection.h"
  36. #include "W25QXX.h"
  37. #include "usbd_cdc_if.h"
  38. #include "time.h"
  39. /* USER CODE END Includes */
  40. /* Private typedef -----------------------------------------------------------*/
  41. /* USER CODE BEGIN PTD */
  42. /* USER CODE END PTD */
  43. /* Private define ------------------------------------------------------------*/
  44. /* USER CODE BEGIN PD */
  45. /* USER CODE END PD */
  46. /* Private macro -------------------------------------------------------------*/
  47. /* USER CODE BEGIN PM */
  48. /* USER CODE END PM */
  49. /* Private variables ---------------------------------------------------------*/
  50. /* USER CODE BEGIN Variables */
  51. uint32_t alarm_record_code[32] =
  52. {
  53. 0x00012200, // Input OVP
  54. 0x00012203, // Input UVP
  55. 0x00012216, // Output OCP
  56. 0x00012223, // Over temperature
  57. 0x00012255, // Ground fault
  58. 0x00023703, // Pilot error
  59. 0x00012233, // AC CCID
  60. 0x00012233, // DC CCID
  61. 0x00012256, // MCU self test
  62. 0x00012258, // Hand shaking timeout
  63. 0x00012251, // Emergency stop
  64. 0x00011009, // Relay welding
  65. 0x00011032, // Leak module test fail
  66. 0x00011034, // shutter fault
  67. 0x00012212, // L1 Power drop
  68. 0x00012262, // Circuit short
  69. 0x00011033, // Maximum Output Current setup error
  70. 0x00011010, // AC output relay driving fault
  71. 0x00011035, // Ble module broken
  72. 0x00000000,
  73. 0x00000000,
  74. 0x00000000,
  75. 0x00000000,
  76. 0x00000000,
  77. 0x00000000,
  78. 0x00000000,
  79. 0x00000000,
  80. 0x00000000,
  81. 0x00000000,
  82. 0x00000000,
  83. 0x00000000
  84. };
  85. uint32_t binCRCTarget, binCRCCal;
  86. __IO uint32_t flashdestination;
  87. __IO uint32_t newdestination;
  88. volatile unsigned long ulHighFrequencyTimerTicks;
  89. sCPModuleResult_t CpDetectionResult;
  90. #ifndef FUNC_CP_ADC_MODIFY
  91. sCPVoltageBoundary_t CpBoundary = {
  92. CP_P12V_HIGH,
  93. CP_P12V_LOW,
  94. CP_P9V_HIGH,
  95. CP_P9V_LOW,
  96. CP_P6V_HIGH,
  97. CP_P6V_LOW,
  98. CP_P3V_HIGH,
  99. CP_P3V_LOW,
  100. CP_0V_HIGH,
  101. CP_0V_LOW,
  102. CP_N12V_HIGH,
  103. CP_N12V_LOW,
  104. };
  105. sCPVoltageBoundary_t SpecDefBoundary = {
  106. CP_SPEC_P12V_HIGH,
  107. CP_SPEC_P12V_LOW,
  108. CP_SPEC_P9V_HIGH,
  109. CP_SPEC_P9V_LOW,
  110. CP_SPEC_P6V_HIGH,
  111. CP_SPEC_P6V_LOW,
  112. CP_SPEC_P3V_HIGH,
  113. CP_SPEC_P3V_LOW,
  114. CP_SPEC_0V_HIGH,
  115. CP_SPEC_0V_LOW,
  116. CP_SPEC_N12V_HIGH,
  117. CP_SPEC_N12V_LOW,
  118. };
  119. sCPVoltageHysteresis_t CpHysteresis = {
  120. HYSTERESIS_P12V_HIGH,
  121. HYSTERESIS_P12V_LOW,
  122. HYSTERESIS_P9V_HIGH,
  123. HYSTERESIS_P9V_LOW,
  124. HYSTERESIS_P6V_HIGH,
  125. HYSTERESIS_P6V_LOW,
  126. HYSTERESIS_P3V_HIGH,
  127. HYSTERESIS_P3V_LOW,
  128. HYSTERESIS_0V_HIGH,
  129. HYSTERESIS_0V_LOW,
  130. };
  131. #endif //FUNC_CP_ADC_MODIFY
  132. struct EVSE Charger;
  133. const unsigned char CharacterArray[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  134. uint32_t test_logA = 0 ;
  135. uint32_t test_logB = 0 ;
  136. extern I2C_HandleTypeDef hi2c2;
  137. /* USER CODE END Variables */
  138. #ifndef DISABLE_OS_ETH_TASK
  139. osThreadId ethTaskHandle;
  140. #endif
  141. osThreadId uartTaskHandle;
  142. osThreadId adcTaskHandle;
  143. osThreadId timeoutTaskHandle;
  144. osThreadId rfidTaskHandle;
  145. osThreadId led_speakerTaskHandle;
  146. osThreadId cpTaskHandle;
  147. osThreadId alarmTaskHandle;
  148. osThreadId bleTaskHandle;
  149. osThreadId memoryTaskHandle;
  150. osThreadId wifiTaskHandle;
  151. /* Private function prototypes -----------------------------------------------*/
  152. /* USER CODE BEGIN FunctionPrototypes */
  153. #ifdef FUNC_RS485_SLAVE
  154. uint8_t isValidCheckSum_IAP(uint8_t *RxBuf);
  155. #else
  156. uint8_t isValidCheckSum_IAP(void);
  157. #endif
  158. uint8_t isValidCheckSum_BLE(void);
  159. uint8_t isValidCheckSum_WIFI(void);
  160. void setChargerMode(uint8_t mode);
  161. uint8_t isModeChange(void);
  162. uint8_t isMode(uint8_t mode);
  163. uint8_t rfidCheckSumCal(uint8_t *data, uint8_t length);
  164. uint8_t Test_LeakModule(void);
  165. uint8_t Valid_RFID_SN(void);
  166. void recordAlarmHis(void);
  167. uint8_t recordChargingHis(uint8_t isColdLoadPickUp, uint32_t statusCode);
  168. void setLedMotion(uint8_t action);
  169. void SetADCWDGBoundary( void);
  170. void getlastRecord(void);
  171. void getRotarySwitchSetting(void);
  172. void CLC_Corr_Gain_Par(uint16_t SpecData_H ,uint16_t SpecData_L ,uint16_t MCUData_H , uint16_t MCUData_L , float *GainA , float *GainB ) ;
  173. void CLC_Corr_Gain_Par2(uint16_t SpecData_H ,uint16_t SpecData_L ,uint16_t MCUData_H , uint16_t MCUData_L , float *GainA , float *GainB , uint8_t *PosorNeg ) ;
  174. uint8_t Array_data_Check ( uint8_t *s1, uint8_t *s2 ) ;
  175. void Relay_Control_Function (void) ;
  176. uint8_t WatchDogLeakRawDataCheck_Event(void) ;
  177. uint16_t getBrightnessDuty(uint16_t max_duty);
  178. void parseVersionInfoFromModelname(void);
  179. void HTK_GetBufferString(const u8* buf, const u8 len, char* szOutBuf);
  180. void HTK_PrintBufferString(const u8* buf, const u8 len);
  181. #ifdef ENABLE_PRINT_IMCP_MSG
  182. int CheckPrintImcpMsg(uint8_t MsgID);
  183. #endif
  184. #ifdef MODIFY_IMCP
  185. void UpdateCheckSum(uint8_t *Buf, uint16_t Len);
  186. #endif
  187. #ifdef MODIFY_COLD_LOAD_PICKUP_DELAY
  188. void ColdLoadPickup_Delay(const char* Description);
  189. #endif
  190. #ifdef FUNC_FORCE_RUN_CSU_MODE_WITH_DC_MODELNAME
  191. void Update_McuCtrlMode(void);
  192. #endif
  193. #ifdef MODIFY_DC_RS485_UPGRADE_ISSUE
  194. void RS485_TX_Enable(void);
  195. void RS485_TX_Disable(void);
  196. void Prefix_UartTX(uint8_t* TxBuf, uint8_t* RxBuf);
  197. void Postfix_UartTX(uint8_t* TxBuf, uint8_t* RxBuf);
  198. #endif
  199. /* USER CODE END FunctionPrototypes */
  200. #ifndef DISABLE_OS_ETH_TASK
  201. void StartEthTask(void const * argument);
  202. #endif
  203. void StartUartTask(void const * argument);
  204. void StartAdcTask(void const * argument);
  205. void StartTimeoutTask(void const * argument);
  206. void StartRfidTask(void const * argument);
  207. void StartLedSpeakerTask(void const * argument);
  208. void StartCpTask(void const * argument);
  209. void StartAlarmTask(void const * argument);
  210. void StartBleTask(void const * argument);
  211. void StartMemoryTask(void const * argument);
  212. void StartWifiTask(void const * argument);
  213. extern void MX_USB_DEVICE_Init(void);
  214. void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */
  215. /* GetIdleTaskMemory prototype (linked to static allocation support) */
  216. void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );
  217. /* Hook prototypes */
  218. void configureTimerForRunTimeStats(void);
  219. unsigned long getRunTimeCounterValue(void);
  220. /* USER CODE BEGIN 1 */
  221. /* Functions needed when configGENERATE_RUN_TIME_STATS is on */
  222. __weak void configureTimerForRunTimeStats(void)
  223. {
  224. ulHighFrequencyTimerTicks = 0ul;
  225. }
  226. __weak unsigned long getRunTimeCounterValue(void)
  227. {
  228. return ulHighFrequencyTimerTicks;
  229. }
  230. /* USER CODE END 1 */
  231. /* USER CODE BEGIN GET_IDLE_TASK_MEMORY */
  232. static StaticTask_t xIdleTaskTCBBuffer;
  233. static StackType_t xIdleStack[configMINIMAL_STACK_SIZE];
  234. void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
  235. {
  236. *ppxIdleTaskTCBBuffer = &xIdleTaskTCBBuffer;
  237. *ppxIdleTaskStackBuffer = &xIdleStack[0];
  238. *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
  239. /* place for user code */
  240. }
  241. /* USER CODE END GET_IDLE_TASK_MEMORY */
  242. /**
  243. * @brief FreeRTOS initialization
  244. * @param None
  245. * @retval None
  246. */
  247. void UpdateFirmwareVersion(void)
  248. {
  249. #ifdef FUNC_FW_VER_TRACE
  250. #if (FW_VER_TYPE == FW_VER_TYPE_DEVELOP)
  251. sprintf(Charger.Ver_FW, "D%4.2f.X0.0007.P0", FW_VER_NUM * 0.01);
  252. #elif (FW_VER_TYPE == FW_VER_TYPE_FORMAL)
  253. #ifdef FORCE_VER_WITH_T_PREFIX
  254. //---------------
  255. #ifdef FORCE_VER_WITH_F_PREFIX
  256. sprintf(Charger.Ver_FW, "F%4.2f.X0.0011.P0", FW_VER_NUM * 0.00001);
  257. #else
  258. sprintf(Charger.Ver_FW, "T%4.2f.X0.0011.P0", FW_VER_NUM * 0.00001);
  259. #endif
  260. //---------------
  261. //sprintf(Charger.Ver_FW, "T%4.2f.X0.0011.P0", FW_VER_NUM * 0.00001);
  262. #else //FORCE_VER_WITH_T_PREFIX
  263. sprintf(Charger.Ver_FW, "V%4.2f.X0.0007.P0", FW_VER_NUM * 0.00001);
  264. #endif //FORCE_VER_WITH_T_PREFIX
  265. #elif (FW_VER_TYPE == FW_VER_TYPE_TEST)
  266. sprintf(Charger.Ver_FW, "T%4.2f.X0.0007.P0", FW_VER_NUM * 0.00001);
  267. #endif
  268. #else //FUNC_FW_VER_TRACE
  269. //sprintf(Charger.Ver_FW, "V0.05.X0.0007.P0");
  270. //sprintf(Charger.Ver_FW, "V0.06.X0.0007.P0"); //20210611
  271. #endif //FUNC_FW_VER_TRACE
  272. #ifdef FUNC_VERSION_USE_NEW_SPEC_20240926
  273. //Indicate firmware belong which module board
  274. //L: LED board
  275. //R: Relay board
  276. //C: CHAdeMO board
  277. //G: GBT board
  278. //F: Fan board
  279. //D: DCM 407
  280. //A: AC main board
  281. //S: CCS board
  282. //M: CSU board
  283. Charger.Ver_FW[6] = 'A';
  284. //0: For formal release specification
  285. //1: For lab certificate specification
  286. #ifdef FUNC_VERSION_FOR_LAB_CERTIFICATE_SPEC
  287. Charger.Ver_FW[7] = '1';
  288. #else
  289. Charger.Ver_FW[7] = '0';
  290. #endif
  291. #endif //FUNC_VERSION_USE_NEW_SPEC_20240926
  292. }
  293. //---------------
  294. #ifdef FUNC_STM32_FLASH_INFO
  295. enum { STM32_UID_LEN = 12 }; //96 bits (12 bytes)
  296. typedef struct _STM32_Flash_Info
  297. {
  298. union
  299. {
  300. u8 m_UID[STM32_UID_LEN];
  301. u32 m_UIDw[3];
  302. };
  303. u32 m_DeviceID;
  304. u32 m_FlashSizeKB;
  305. }
  306. STM32_Flash_Info, *PSTM32_Flash_Info;
  307. void STM32_Flash_Info_Init(PSTM32_Flash_Info p)
  308. {
  309. memset(p, 0, sizeof(STM32_Flash_Info));
  310. }
  311. void STM32_Flash_Info_Read(PSTM32_Flash_Info p)
  312. {
  313. //UID
  314. memcpy(p->m_UID, (u8*)UID_BASE, STM32_UID_LEN);
  315. //Device ID
  316. p->m_DeviceID = (uint32_t)(READ_BIT(DBGMCU->IDCODE, DBGMCU_IDCODE_DEV_ID));
  317. //Flash size
  318. p->m_FlashSizeKB = (uint32_t)(READ_REG(*((uint32_t *)FLASHSIZE_BASE)) & 0xFFFF);
  319. }
  320. void STM32_Flash_Info_Disp(PSTM32_Flash_Info p)
  321. {
  322. XP("[FlashInfo]------------------------\r\n");
  323. //UID
  324. HTK_ByteArray2HexStr_XP("UID", p->m_UID, 0, STM32_UID_LEN);
  325. //Device ID
  326. XP("DeviceID: 0x%03X\r\n", p->m_DeviceID);
  327. {
  328. char *s = "DeviceFamily: ";
  329. if (p->m_DeviceID == 0x413)
  330. {
  331. XP("%sSTM32F40xxx/F41xxx\r\n", s);
  332. }
  333. else if (p->m_DeviceID == 0x419)
  334. {
  335. XP("%sSTM32F42xxx/F43xxx\r\n", s);
  336. }
  337. else
  338. {
  339. XP("%sUnknow\r\n", s);
  340. }
  341. }
  342. //Flash size
  343. XP("DeviceFlashSize: %d Kbytes\r\n", p->m_FlashSizeKB);
  344. }
  345. void STM32_Flash_Info_Proc(void)
  346. {
  347. PSTM32_Flash_Info p = HTK_Malloc(sizeof(STM32_Flash_Info));
  348. if (p != NULL)
  349. {
  350. STM32_Flash_Info_Init(p);
  351. STM32_Flash_Info_Read(p);
  352. STM32_Flash_Info_Disp(p);
  353. }
  354. else
  355. {
  356. XP("PSTM32_Flash_Info malloc NG\r\n");
  357. }
  358. }
  359. #endif //FUNC_STM32_FLASH_INFO
  360. #ifdef FUNC_STM32_FLASH_OPTION_BYTES
  361. typedef struct _STM32_Flash_OB
  362. {
  363. FLASH_OBProgramInitTypeDef m_SetOB;
  364. FLASH_OBProgramInitTypeDef m_CurOB;
  365. u8 m_nRST_STDBY: 1; //0: Reset generated when entering the Standby mode
  366. //1: No reset generated
  367. u8 m_nRST_STOP: 1; //0: Reset generated when entering the Stop mode
  368. //1: No reset generated
  369. u8 m_WDG_SW: 1; //0: Hardware independent watchdog
  370. //1: Software independent watchdog
  371. }
  372. STM32_Flash_OB, *PSTM32_Flash_OB;
  373. void STM32_Flash_OB_Init(PSTM32_Flash_OB p)
  374. {
  375. memset(p, 0, sizeof(STM32_Flash_OB));
  376. //Set default
  377. p->m_SetOB.OptionType = OPTIONBYTE_WRP /*| OPTIONBYTE_RDP*/ | OPTIONBYTE_USER | OPTIONBYTE_BOR;
  378. p->m_SetOB.WRPState = OB_WRPSTATE_ENABLE;
  379. p->m_SetOB.Banks = 0;
  380. p->m_SetOB.BORLevel = OB_BOR_LEVEL3;
  381. p->m_SetOB.USERConfig = OB_IWDG_SW | OB_STOP_NO_RST | OB_STDBY_NO_RST;
  382. //#ifdef FUNC_USE_STM32_SBSFU
  383. // //Sector_0~4: Write Protection; Sector_5~11: No Write Protection
  384. // p->m_SetOB.WRPSector = OB_WRP_SECTOR_0 | OB_WRP_SECTOR_1 | OB_WRP_SECTOR_2 | OB_WRP_SECTOR_3 | OB_WRP_SECTOR_4;
  385. // p->m_SetOB.RDPLevel = OB_RDP_LEVEL_1;
  386. //#else
  387. //Sector_0~11: No Write Protection
  388. p->m_SetOB.WRPSector = ~OB_WRP_SECTOR_All;
  389. p->m_SetOB.RDPLevel = OB_RDP_LEVEL_0;
  390. //#endif
  391. }
  392. void STM32_Flash_OB_Read(PSTM32_Flash_OB p)
  393. {
  394. __HAL_FLASH_PREFETCH_BUFFER_DISABLE();
  395. HAL_FLASHEx_OBGetConfig(&p->m_CurOB);
  396. __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
  397. p->m_nRST_STDBY = BIT_GET(p->m_CurOB.USERConfig, 7);
  398. p->m_nRST_STOP = BIT_GET(p->m_CurOB.USERConfig, 6);
  399. p->m_WDG_SW = BIT_GET(p->m_CurOB.USERConfig, 5);
  400. }
  401. void STM32_Flash_OB_WriteDefault(PSTM32_Flash_OB p)
  402. {
  403. XP("<Write default OB>\r\n");
  404. __HAL_FLASH_PREFETCH_BUFFER_DISABLE();
  405. HAL_FLASH_Unlock();
  406. HAL_FLASH_OB_Unlock();
  407. {
  408. {
  409. XP("<Set OB Default>\r\n");
  410. HAL_FLASHEx_OBProgram(&p->m_SetOB);
  411. HAL_FLASH_OB_Launch();
  412. }
  413. {
  414. FLASH_OBProgramInitTypeDef ob;
  415. memcpy(&ob, &p->m_SetOB, sizeof(p->m_SetOB));
  416. ob.OptionType = OPTIONBYTE_WRP;
  417. ob.WRPState = OB_WRPSTATE_DISABLE;
  418. ob.WRPSector = (~p->m_SetOB.WRPSector) & OB_WRP_SECTOR_All;
  419. XP("<Set OB.WRPSector => Disable>\r\n");
  420. HAL_FLASHEx_OBProgram(&ob);
  421. HAL_FLASH_OB_Launch();
  422. }
  423. if (p->m_CurOB.RDPLevel == OB_RDP_LEVEL_0 && p->m_SetOB.RDPLevel == OB_RDP_LEVEL_1)
  424. {
  425. FLASH_OBProgramInitTypeDef ob;
  426. memcpy(&ob, &p->m_SetOB, sizeof(p->m_SetOB));
  427. ob.OptionType = OPTIONBYTE_RDP;
  428. XP("<Set OB.RDPLevel => Level 1> Please power off and then power on to restart !\r\n");
  429. HAL_FLASHEx_OBProgram(&ob);
  430. HAL_FLASH_OB_Launch();
  431. /* Set the OPTSTRT bit in OPTCR register */
  432. // XP("*(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= FLASH_OPTCR_OPTSTRT [BEG]\r\n");
  433. // *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= FLASH_OPTCR_OPTSTRT;
  434. // XP("*(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= FLASH_OPTCR_OPTSTRT [BEG]\r\n");
  435. //HAL_Delay(1000);
  436. //NVIC_SystemReset();
  437. }
  438. }
  439. HAL_FLASH_OB_Lock();
  440. HAL_FLASH_Lock();
  441. __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
  442. }
  443. void STM32_Flash_OB_Disp(PSTM32_Flash_OB p)
  444. {
  445. XP("[OB]-------------------------------\r\n");
  446. XP("OptionType: 0x%08X\r\n", p->m_CurOB.OptionType);
  447. XP("WRPState : 0x%08X\r\n", p->m_CurOB.WRPState);
  448. XP("WRPSector : 0x%08X\r\n", p->m_CurOB.WRPSector);
  449. {
  450. for (int i = 0; i < 12; i++)
  451. {
  452. XP("\tSector_%d: %s Protection\r\n", i, BIT_GET(p->m_CurOB.WRPSector, i) == 1 ? "No" : "Write");
  453. }
  454. }
  455. XP("Banks : 0x%08X\r\n", p->m_CurOB.Banks);
  456. XP("RDPLevel : 0x%08X (0: 0xAA, 1: 0x55, 2: 0xCC)\r\n", p->m_CurOB.RDPLevel);
  457. XP("BORLevel : 0x%08X (3: 0x00, 2: 0x04, 1: 0x08, OFF: 0x0C)\r\n", p->m_CurOB.BORLevel);
  458. XP("USERConfig: 0x%02X\r\n", p->m_CurOB.USERConfig);
  459. {
  460. XP("\tnRST_STDBY = %d (%s)\r\n", p->m_nRST_STDBY, (p->m_nRST_STDBY == 0 ? "STDBY_RST(0)" : "STDBY_NO_RST(0x80)"));
  461. XP("\tnRST_STOP = %d (%s)\r\n", p->m_nRST_STOP, (p->m_nRST_STOP == 0 ? "STOP_RST(0)" : "STOP_NO_RST(0x40)"));
  462. XP("\tWDG_SW = %d (%s)\r\n", p->m_WDG_SW, (p->m_WDG_SW == 0 ? "IWDG_HW(0)" : "IWDG_SW(0x20)"));
  463. }
  464. }
  465. HTK_BOOL STM32_Flash_OB_IsDefault(PSTM32_Flash_OB p)
  466. {
  467. XP("m_CurOB.BORLevel = 0x%08X, m_SetOB.BORLevel = 0x%08X\r\n", p->m_CurOB.BORLevel, p->m_SetOB.BORLevel);
  468. XP("m_CurOB.RDPLevel = 0x%08X, m_SetOB.RDPLevel = 0x%08X\r\n", p->m_CurOB.RDPLevel, p->m_SetOB.RDPLevel);
  469. XP("m_CurOB.USERConfig = 0x%08X, m_SetOB.USERConfig = 0x%08X\r\n", p->m_CurOB.USERConfig, p->m_SetOB.USERConfig);
  470. XP("m_CurOB.WRPSector = 0x%08X, m_SetOB.WRPSector = 0x%08X\r\n", p->m_CurOB.WRPSector, p->m_SetOB.WRPSector);
  471. XP("(~p->m_SetOB.WRPSector) & OB_WRP_SECTOR_All = 0x%08X\r\n", (~p->m_SetOB.WRPSector) & OB_WRP_SECTOR_All);
  472. if (
  473. (p->m_CurOB.BORLevel == p->m_SetOB.BORLevel) &&
  474. (p->m_CurOB.RDPLevel == p->m_SetOB.RDPLevel) &&
  475. (p->m_CurOB.USERConfig == p->m_SetOB.USERConfig) &&
  476. (p->m_CurOB.WRPSector == ((~p->m_SetOB.WRPSector) & OB_WRP_SECTOR_All))
  477. )
  478. {
  479. XP("<OB is Default>\r\n");
  480. return HTK_TRUE;
  481. }
  482. else
  483. {
  484. XP("<OB is NOT Default>\r\n");
  485. return HTK_FALSE;
  486. }
  487. }
  488. void STM32_Flash_OB_ProcDefault(void)
  489. {
  490. PSTM32_Flash_OB p = HTK_Malloc(sizeof(STM32_Flash_OB));
  491. if (p != NULL)
  492. {
  493. STM32_Flash_OB_Init(p);
  494. STM32_Flash_OB_Read(p);
  495. STM32_Flash_OB_Disp(p);
  496. if (!STM32_Flash_OB_IsDefault(p))
  497. {
  498. STM32_Flash_OB_WriteDefault(p);
  499. STM32_Flash_OB_Read(p);
  500. STM32_Flash_OB_Disp(p);
  501. if (STM32_Flash_OB_IsDefault(p))
  502. {
  503. XP("<Write default OB success>\r\n");
  504. }
  505. else
  506. {
  507. XP("<Write default OB fail>\r\n");
  508. }
  509. }
  510. }
  511. else
  512. {
  513. XP("STM32_Flash_OB malloc NG\r\n");
  514. }
  515. }
  516. #endif //FUNC_STM32_FLASH_OPTION_BYTES
  517. void MX_FREERTOS_Init(void) {
  518. /* USER CODE BEGIN Init */
  519. #ifdef FUNC_BOOT_TICK
  520. Charger.m_BootTick = HAL_GetTick();
  521. #endif
  522. #ifdef FUNC_STM32_FLASH_INFO
  523. STM32_Flash_Info_Proc();
  524. #endif //FUNC_STM32_FLASH_INFO
  525. #ifdef FUNC_STM32_FLASH_OPTION_BYTES
  526. STM32_Flash_OB_ProcDefault();
  527. #endif
  528. UpdateFirmwareVersion();
  529. sprintf(Charger.Ver_HW, "RA2");
  530. HAL_GPIO_WritePin(OUT_PHY_Reset_GPIO_Port, OUT_PHY_Reset_Pin, GPIO_PIN_RESET);
  531. HAL_GPIO_WritePin(OUT_Leak_Test_GPIO_Port, OUT_Leak_Test_Pin, GPIO_PIN_SET);
  532. /* USER CODE END Init */
  533. /* USER CODE BEGIN RTOS_MUTEX */
  534. /* add mutexes, ... */
  535. /* USER CODE END RTOS_MUTEX */
  536. /* USER CODE BEGIN RTOS_SEMAPHORES */
  537. /* add semaphores, ... */
  538. /* USER CODE END RTOS_SEMAPHORES */
  539. /* USER CODE BEGIN RTOS_TIMERS */
  540. /* start timers, add new ones, ... */
  541. /* USER CODE END RTOS_TIMERS */
  542. /* USER CODE BEGIN RTOS_QUEUES */
  543. /* add queues, ... */
  544. /* USER CODE END RTOS_QUEUES */
  545. /* Create the thread(s) */
  546. /* definition and creation of ethTask */
  547. #ifndef DISABLE_OS_ETH_TASK
  548. osThreadDef(ethTask, StartEthTask, osPriorityIdle, 0, 1024);
  549. ethTaskHandle = osThreadCreate(osThread(ethTask), NULL);
  550. #endif
  551. /* definition and creation of uartTask */
  552. osThreadDef(uartTask, StartUartTask, osPriorityNormal, 0, 1024);
  553. uartTaskHandle = osThreadCreate(osThread(uartTask), NULL);
  554. /* definition and creation of adcTask */
  555. osThreadDef(adcTask, StartAdcTask, osPriorityRealtime, 0, 256);
  556. adcTaskHandle = osThreadCreate(osThread(adcTask), NULL);
  557. /* definition and creation of timeoutTask */
  558. osThreadDef(timeoutTask, StartTimeoutTask, osPriorityAboveNormal, 0, 128);
  559. timeoutTaskHandle = osThreadCreate(osThread(timeoutTask), NULL);
  560. /* definition and creation of rfidTask */
  561. osThreadDef(rfidTask, StartRfidTask, osPriorityIdle, 0, 256);
  562. rfidTaskHandle = osThreadCreate(osThread(rfidTask), NULL);
  563. /* definition and creation of led_speakerTask */
  564. osThreadDef(led_speakerTask, StartLedSpeakerTask, osPriorityAboveNormal, 0, 128);
  565. led_speakerTaskHandle = osThreadCreate(osThread(led_speakerTask), NULL);
  566. /* definition and creation of cpTask */
  567. osThreadDef(cpTask, StartCpTask, osPriorityHigh, 0, 256);
  568. cpTaskHandle = osThreadCreate(osThread(cpTask), NULL);
  569. /* definition and creation of alarmTask */
  570. osThreadDef(alarmTask, StartAlarmTask, osPriorityHigh, 0, 256);
  571. alarmTaskHandle = osThreadCreate(osThread(alarmTask), NULL);
  572. /* definition and creation of bleTask */
  573. #ifdef LOCAL_LINK_BLE
  574. osThreadDef(bleTask, StartBleTask, osPriorityHigh, 0, 1024);
  575. bleTaskHandle = osThreadCreate(osThread(bleTask), NULL);
  576. #endif
  577. /* definition and creation of memoryTask */
  578. osThreadDef(memoryTask, StartMemoryTask, osPriorityIdle, 0, 256);
  579. memoryTaskHandle = osThreadCreate(osThread(memoryTask), NULL);
  580. /* definition and creation of wifiTask */
  581. #ifdef LOCAL_LINK_WIFI
  582. osThreadDef(wifiTask, StartWifiTask, osPriorityIdle, 0, 1024);
  583. wifiTaskHandle = osThreadCreate(osThread(wifiTask), NULL);
  584. #endif
  585. /* USER CODE BEGIN RTOS_THREADS */
  586. /* add threads, ... */
  587. /* USER CODE END RTOS_THREADS */
  588. }
  589. #ifndef DISABLE_OS_ETH_TASK
  590. /* USER CODE BEGIN Header_StartEthTask */
  591. /**
  592. * @brief Function implementing the ethTask thread.
  593. * @param argument: Not used
  594. * @retval None
  595. */
  596. /* USER CODE END Header_StartEthTask */
  597. void StartEthTask(void const * argument)
  598. {
  599. /* init code for USB_DEVICE */
  600. MX_USB_DEVICE_Init();
  601. /* USER CODE BEGIN StartEthTask */
  602. /* Infinite loop */
  603. for(;;)
  604. {
  605. osDelay(1);
  606. }
  607. /* USER CODE END StartEthTask */
  608. }
  609. #endif //DISABLE_OS_ETH_TASK
  610. /* USER CODE BEGIN Header_StartUartTask */
  611. /**
  612. * @brief Function implementing the uartTask thread.
  613. * @param argument: Not used
  614. * @retval None
  615. */
  616. /* USER CODE END Header_StartUartTask */
  617. void StartUartTask(void const * argument)
  618. {
  619. /* USER CODE BEGIN StartUartTask */
  620. //uuart
  621. #ifndef DISABLE_PRINT_RTOS_MSG
  622. uint8_t pcWriteBuffer[1024];
  623. #endif
  624. uint8_t endFlag[4]={0x55,0xaa,0x55,0xaa};
  625. __IO uint32_t flash;
  626. uint32_t checksum;
  627. uint8_t dbgIdx;
  628. #if defined(DEBUG) || defined(RTOS_STAT)
  629. DEBUG_INFO("========== AP initial OK ==========\n\r");
  630. DEBUG_INFO(" Version: %s\n\r", Charger.Ver_FW);
  631. DEBUG_INFO("===================================\n\r");
  632. if (__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST) != RESET) DEBUG_INFO("Reset flag: Pin reset \r\n");
  633. if (__HAL_RCC_GET_FLAG(RCC_FLAG_PORRST) != RESET) DEBUG_INFO("Reset flag: POR reset \r\n");
  634. if (__HAL_RCC_GET_FLAG(RCC_FLAG_SFTRST) != RESET) DEBUG_INFO("Reset flag: SW reset \r\n");
  635. if (__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST) != RESET) DEBUG_INFO("Reset flag: IWDG reset \r\n");
  636. if (__HAL_RCC_GET_FLAG(RCC_FLAG_WWDGRST) != RESET) DEBUG_INFO("Reset flag: WWDG reset \r\n");
  637. if (__HAL_RCC_GET_FLAG(RCC_FLAG_LPWRRST) != RESET) DEBUG_INFO("Reset flag: LP reset \r\n");
  638. DEBUG_INFO("\r\n");
  639. __HAL_RCC_CLEAR_RESET_FLAGS();
  640. #endif
  641. Charger.CSUisReady = OFF ;
  642. #ifdef FUNC_RS485_SLAVE
  643. HAL_GPIO_WritePin(OUT_Meter_485_DE_GPIO_Port, OUT_Meter_485_DE_Pin, GPIO_PIN_RESET);
  644. HAL_GPIO_WritePin(OUT_Meter_485_RE_GPIO_Port, OUT_Meter_485_RE_Pin, GPIO_PIN_RESET);
  645. #endif
  646. #ifdef FUNC_RELAY_OFF_WHEN_GET_TWICE_OFF_CMD
  647. u8 CsuSetRelayOffCount = 0;
  648. #endif
  649. #ifdef FUNC_REQUEST_OFF_WHEN_GET_TWICE_OFF_CMD
  650. u8 CsuSetRequestOffCount = 0;
  651. #endif
  652. /* Infinite loop */
  653. for(;;)
  654. {
  655. timerEnable(TIMER_IDX_RTOS_STAT, 500);
  656. if(timer[TIMER_IDX_RTOS_STAT].isAlarm)
  657. {
  658. switch(dbgIdx)
  659. {
  660. case 0:
  661. #ifdef RTOS_STAT
  662. #ifndef DISABLE_PRINT_RTOS_MSG
  663. printf("=================================================\r\n");
  664. printf("Task_Name \tState \tPrior \tStack \tTask_Id\r\n");
  665. vTaskList((char *)&pcWriteBuffer);
  666. printf("%s\r\n", pcWriteBuffer);
  667. printf("-------------------------------------------------\r\n");
  668. printf("Task_Name \tRun_Time(10us) \tUsage_Percent\r\n");
  669. vTaskGetRunTimeStats((char *)&pcWriteBuffer);
  670. printf("%s\r\n", pcWriteBuffer);
  671. #endif //DISABLE_PRINT_RTOS_MSG
  672. #endif
  673. dbgIdx++;
  674. break;
  675. case 1:
  676. #ifdef FUNC_EMPX10_CUSTOMIZE
  677. DEBUG_INFO("Firmware version: %s (AWeMP/ID:%d)\r\n", Charger.Ver_FW, Charger.m_EMPX10_RS485_ClientAddr);
  678. #else
  679. DEBUG_INFO("Firmware version: %s\r\n", Charger.Ver_FW);
  680. #endif
  681. #ifdef FUNC_BUILD_DATE_TIME
  682. DEBUG_INFO("Build: %s %s\r\n", __DATE__, __TIME__); //ex: Build: Mar 10 2022 14:53:25
  683. #endif
  684. //DEBUG_INFO("MCU_Control_Mode: %x\r\n",Charger.memory.EVSE_Config.data.item.MCU_Control_Mode);
  685. DEBUG_INFO("MCU_Control_Mode: %x (%s)\r\n",Charger.memory.EVSE_Config.data.item.MCU_Control_Mode,
  686. Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_CSU ? "CSU" : "No CSU");
  687. DEBUG_INFO("ModelName: %s\r\n",Charger.memory.EVSE_Config.data.item.ModelName);
  688. DEBUG_INFO("SerialNumber: %s\r\n",Charger.memory.EVSE_Config.data.item.SerialNumber);
  689. #ifdef FUNC_BOOT_TICK
  690. DEBUG_INFO("UpTime(s): %d\r\n", (HAL_GetTick() - Charger.m_BootTick) / 1000);
  691. #endif
  692. dbgIdx++;
  693. break;
  694. case 2:
  695. if (Charger.CCID_Module_Type == CCID_MODULE_CORMEX)
  696. {
  697. DEBUG_INFO("Charger.CCID_Module_Type is CCID_MODULE_CORMEX\n\r");
  698. }
  699. else if (Charger.CCID_Module_Type == CCID_MODULE_VAC)
  700. {
  701. DEBUG_INFO("Charger.CCID_Module_Type is CCID_MODULE_VAC\n\r");
  702. }
  703. DEBUG_INFO("System mode: %d\r\n", Charger.Mode);
  704. DEBUG_INFO("Alarm code: 0x%08X\r\n", Charger.Alarm_Code);
  705. #ifdef FUNC_SHOW_RELAY_INFO
  706. DEBUG_INFO("Relay action: Main(%d)\r\n", Charger.Relay_Action);
  707. #endif
  708. dbgIdx++;
  709. break;
  710. case 3:
  711. DEBUG_INFO("Original Voltage[0]: %f\r\n", adc_value.ADC3_IN9_Voltage_L1.value/100.0);
  712. DEBUG_INFO("Original Current[0]: %f\r\n", adc_value.ADC3_IN7_Current_L1.value/100.0);
  713. dbgIdx++;
  714. break;
  715. case 4:
  716. DEBUG_INFO("Correction Voltage[0]: %f\r\n", Charger.Voltage[0]/100.0);
  717. DEBUG_INFO("Correction Current[0]: %f\r\n", Charger.Current[0]/100.0);
  718. dbgIdx++;
  719. break;
  720. case 5:
  721. DEBUG_INFO("PE(ADC): %d\r\n", adc_value.ADC3_IN4_GMI_VL1.value );
  722. DEBUG_INFO("PE(V): %f\r\n", adc_value.ADC3_IN4_GMI_VL1.value*3.3/4095 );
  723. DEBUG_INFO("adc_value.ADC2_IN5_Welding.value : %d(unit:0.01v)\r\n", adc_value.ADC2_IN5_Welding.value );
  724. DEBUG_INFO("Charger.temperature.SystemAmbientTemp : %d(unit:C)\r\n", Charger.temperature.SystemAmbientTemp);
  725. dbgIdx++;
  726. break;
  727. case 6:
  728. if (Charger.CCID_Module_Type == CCID_MODULE_CORMEX)
  729. {
  730. DEBUG_INFO("Original GF[0]: %fmA\r\n", adc_value.ADC2_IN6_GF.value/100.0);
  731. DEBUG_INFO("Charger.Leak_Current: %fmA\r\n", Charger.Leak_Current/100.0);
  732. }
  733. else
  734. {
  735. DEBUG_INFO("Temperature ADC raw data : %d\r\n", adc_value.ADC3_IN15_Temp.value);
  736. DEBUG_INFO("Temperature : %d\r\n", Charger.temperature.SystemAmbientTemp);
  737. DEBUG_INFO("ADC3_Buffer[1] (ADC): %d(unit:ADC)\r\n", ADC3_Buffer[1] );
  738. DEBUG_INFO("Charger.Led_Mode: %d\r\n", Charger.Led_Mode );
  739. }
  740. dbgIdx++;
  741. break;
  742. case 7:
  743. DEBUG_INFO("CP+(ADC): %d(unit:ADC)\r\n",CpDetectionResult.PositiveValue );
  744. DEBUG_INFO("CP-(ADC): %d(unit:ADC)\r\n",CpDetectionResult.NegativeValue );
  745. if(CpDetectionResult.PositiveValue>0)
  746. {
  747. #ifdef FUNC_CP_ADC_MODIFY
  748. DEBUG_INFO("CP +voltage: %f\r\n", CP_GET_OPA_VIN_USE_ADC(CpDetectionResult.PositiveValue));
  749. #else
  750. DEBUG_INFO("CP +voltage: %f\r\n", (float)((1.59131-((3.3*CpDetectionResult.PositiveValue)/4095))*(475/53.6)));
  751. #endif
  752. }
  753. else
  754. {
  755. DEBUG_INFO("CP +voltage: 0\r\n");
  756. }
  757. if(CpDetectionResult.NegativeValue>0)
  758. {
  759. #ifdef FUNC_CP_ADC_MODIFY
  760. DEBUG_INFO("CP -voltage: %f\r\n", CP_GET_OPA_VIN_USE_ADC(CpDetectionResult.NegativeValue));
  761. #else
  762. DEBUG_INFO("CP -voltage: %f\r\n", (float)((1.59131-((3.3*CpDetectionResult.NegativeValue)/4095))*(475/53.6)));
  763. #endif
  764. }else
  765. {
  766. DEBUG_INFO("CP -voltage: 0\r\n");
  767. }
  768. DEBUG_INFO("CP state: %d\r\n", Charger.CP_State);
  769. dbgIdx++;
  770. break;
  771. case 8:
  772. DEBUG_INFO("Power_Consumption_Cumulative: %d (0.0001KWH)\r\n", Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative);
  773. DEBUG_INFO("Current rating value: %d A\r\n", Charger.maxRatingCurrent);
  774. if (Charger.memory.EVSE_Config.data.item.MaxChargingCurrent>=20)
  775. {
  776. DEBUG_INFO("OCP tigger UL: %d\r\n", (Charger.memory.EVSE_Config.data.item.MaxChargingCurrent*110)-100 );
  777. DEBUG_INFO("OCP tigger IEC: %d\r\n", (Charger.memory.EVSE_Config.data.item.MaxChargingCurrent*125)-100 );
  778. }
  779. else
  780. {
  781. DEBUG_INFO("OCP tigger UL: %d\r\n", (Charger.memory.EVSE_Config.data.item.MaxChargingCurrent+1)*100 );
  782. DEBUG_INFO("OCP tigger IEC: %d\r\n", (Charger.memory.EVSE_Config.data.item.MaxChargingCurrent*125)-100 );
  783. }
  784. DEBUG_INFO("Charger.am3352.isRequestOn: %d\r\n", Charger.am3352.isRequestOn);
  785. #ifdef FUNC_OCP_WITH_PP
  786. #ifdef MODIFY_OCP_PROC_WITH_MODELNAME_IDX_3_EURO_SPEC
  787. DEBUG_INFO("#OCPInfo(%d, %d, %d, %d) [%s]\r\n",
  788. Charger.m_OCP_CurMaxCurr, Charger.OCP_Magnification,
  789. Charger.m_OCP_BegThreshold, Charger.m_OCP_EndThreshold,
  790. Charger.m_bModelNameWithEuroSpecOCP ? "EuroSpec" : "NotEuroSpec");
  791. #else
  792. DEBUG_INFO("#OCPInfo(%d, %d, %d, %d)\r\n",
  793. Charger.m_OCP_CurMaxCurr, Charger.OCP_Magnification,
  794. Charger.m_OCP_BegThreshold, Charger.m_OCP_EndThreshold);
  795. #endif //MODIFY_OCP_PROC_WITH_MODELNAME_IDX_3_EURO_SPEC
  796. #endif //FUNC_OCP_WITH_PP
  797. #ifdef FUNC_GET_TRIP_TIME
  798. if (Charger.m_TripBeg != 0 && Charger.m_TripBeg < Charger.m_TripEnd)
  799. {
  800. DEBUG_INFO("Trip period: %d\r\n", Charger.m_TripEnd - Charger.m_TripBeg);
  801. }
  802. #endif
  803. dbgIdx++;
  804. break;
  805. case 9:
  806. if (Charger.Alarm_Code & ALARM_OVER_VOLTAGE)
  807. DEBUG_INFO("ALARM_OVER_VOLTAGE \n\r");
  808. if (Charger.Alarm_Code & ALARM_UNDER_VOLTAGE)
  809. DEBUG_INFO("ALARM_UNDER_VOLTAGE \n\r");
  810. if (Charger.Alarm_Code & ALARM_OVER_CURRENT)
  811. DEBUG_INFO("ALARM_OVER_CURRENT \n\r");
  812. if (Charger.Alarm_Code & ALARM_OVER_TEMPERATURE)
  813. DEBUG_INFO("ALARM_OVER_TEMPERATURE \n\r");
  814. if (Charger.Alarm_Code & ALARM_GROUND_FAIL)
  815. DEBUG_INFO("ALARM_GROUND_FAIL \n\r");
  816. if (Charger.Alarm_Code & ALARM_CP_ERROR)
  817. DEBUG_INFO("ALARM_CP_ERROR \n\r");
  818. if (Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC)
  819. DEBUG_INFO("ALARM_CURRENT_LEAK_AC \n\r");
  820. if (Charger.Alarm_Code & ALARM_CURRENT_LEAK_DC)
  821. DEBUG_INFO("ALARM_CURRENT_LEAK_DC \n\r");
  822. if (Charger.Alarm_Code & ALARM_MCU_TESTFAIL)
  823. DEBUG_INFO("ALARM_MCU_TESTFAIL \n\r");
  824. dbgIdx++;
  825. break;
  826. case 10:
  827. if (Charger.Alarm_Code & ALARM_HANDSHAKE_TIMEOUT)
  828. DEBUG_INFO("ALARM_HANDSHAKE_TIMEOUT \n\r");
  829. if (Charger.Alarm_Code & ALARM_EMERGENCY_STOP)
  830. DEBUG_INFO("ALARM_EMERGENCY_STOP \n\r");
  831. if (Charger.Alarm_Code & ALARM_RELAY_STATUS)
  832. DEBUG_INFO("ALARM_RELAY_STATUS \n\r");
  833. if (Charger.Alarm_Code & ALARM_LEAK_MODULE_FAIL)
  834. DEBUG_INFO("ALARM_LEAK_MODULE_FAIL \n\r");
  835. if (Charger.Alarm_Code & ALARM_SHUTTER_FAULT)
  836. DEBUG_INFO("ALARM_SHUTTER_FAULT \n\r");
  837. if (Charger.Alarm_Code & ALARM_LOCKER_FAULT)
  838. DEBUG_INFO("ALARM_LOCKER_FAULT \n\r");
  839. if (Charger.Alarm_Code & ALARM_POWER_DROP)
  840. DEBUG_INFO("ALARM_POWER_DROP \n\r");
  841. if (Charger.Alarm_Code & ALARM_CIRCUIT_SHORT)
  842. DEBUG_INFO("ALARM_CIRCUIT_SHORT \n\r");
  843. if (Charger.Alarm_Code & ALARM_ROTATORY_SWITCH_FAULT)
  844. DEBUG_INFO("ALARM_ROTATORY_SWITCH_FAULT \n\r");
  845. if (Charger.Alarm_Code & ALARM_RELAY_DRIVE_FUALT)
  846. DEBUG_INFO("ALARM_RELAY_DRIVE_FUALT \n\r");
  847. dbgIdx++;
  848. break;
  849. default:
  850. getlastRecord();
  851. dbgIdx = 0;
  852. break;
  853. }
  854. timerRefresh(TIMER_IDX_RTOS_STAT);
  855. }
  856. HAL_IWDG_Refresh(&hiwdg);
  857. #ifdef FUNC_RS485_SLAVE
  858. if(UART_IAP_recv_end_flag == 1 || UART_RS485_recv_end_flag == 1)
  859. #else
  860. if(UART_IAP_recv_end_flag == 1)
  861. #endif
  862. {
  863. #ifdef FUNC_UART_IAP_TX_BUF_INIT_ZERO
  864. uint8_t tx[UART_BUFFER_SIZE] = { 0 };
  865. #else
  866. uint8_t tx[UART_BUFFER_SIZE];
  867. #endif
  868. #ifdef MODIFY_UART_TX_LEN_FROM_U8_TO_U16
  869. uint16_t tx_len;
  870. #else
  871. uint8_t tx_len;
  872. #endif
  873. uint8_t chksum = 0;
  874. uint8_t tmpBuf = 0 ;
  875. // Protocol process
  876. #ifdef FUNC_RS485_SLAVE
  877. uint8_t *CurRxBuf = NULL;
  878. uint16_t *pCurRxBuf_RxLen = NULL;
  879. uint8_t *pCurRecvEndFlag = NULL;
  880. UART_HandleTypeDef *pUartHandle = NULL;
  881. if (UART_IAP_recv_end_flag == 1 && isValidCheckSum_IAP(UART_IAP_rx_buffer) == ON)
  882. {
  883. CurRxBuf = UART_IAP_rx_buffer;
  884. pCurRxBuf_RxLen = &UART_IAP_rx_len;
  885. pCurRecvEndFlag = &UART_IAP_recv_end_flag;
  886. pUartHandle = &IAP_USART;
  887. #ifdef FUNC_RELAY_OFF_WHEN_CSU_CMD_TIMEOUT
  888. Charger.m_CSU_RxTick = HAL_GetTick();
  889. #endif
  890. }
  891. else if (UART_RS485_recv_end_flag == 1 && isValidCheckSum_IAP(UART_RS485_rx_buffer) == ON)
  892. {
  893. CurRxBuf = UART_RS485_rx_buffer;
  894. pCurRxBuf_RxLen = &UART_RS485_rx_len;
  895. pCurRecvEndFlag = &UART_RS485_recv_end_flag;
  896. pUartHandle = &RS485_USART;
  897. #ifdef FUNC_RELAY_OFF_WHEN_CSU_CMD_TIMEOUT //DC Model
  898. Charger.m_CSU_RxTick = HAL_GetTick();
  899. #endif
  900. }
  901. if (CurRxBuf != NULL)
  902. #else
  903. uint8_t *CurRxBuf = UART_IAP_rx_buffer;
  904. if(isValidCheckSum_IAP() == ON)
  905. #endif
  906. {
  907. Charger.CSUisReady = ON ;
  908. #ifdef DEBUG_EMPX10_COMM
  909. if (CurRxBuf == UART_RS485_rx_buffer)
  910. {
  911. XP("-------------------------------------------\r\n");
  912. HTK_ByteArray2HexStr_XP(NULL, CurRxBuf, 0, IMCP_FM_HEAD_LEN + HTK_U16(CurRxBuf[IMCP_FM_DATALEN_LSB_IDX]) + IMCP_FM_TAIL_LEN);
  913. }
  914. #endif //DEBUG_EMPX10_COMM
  915. #ifdef ENABLE_PRINT_IMCP_MSG
  916. #ifdef FUNC_RS485_SLAVE
  917. #ifdef PRINT_IMCP_MSG_ONLY_ON_UPDATEPORT
  918. if (CurRxBuf == UART_IAP_rx_buffer && CheckPrintImcpMsg(CurRxBuf[IMCP_FM_MSGID_IDX]))
  919. {
  920. XP("-------------------------------------------\r\n");
  921. HTK_ByteArray2HexStr_XP(NULL, CurRxBuf, 0, IMCP_FM_HEAD_LEN + HTK_U16(CurRxBuf[IMCP_FM_DATALEN_LSB_IDX]) + IMCP_FM_TAIL_LEN);
  922. }
  923. #else
  924. if (CheckPrintImcpMsg(CurRxBuf[IMCP_FM_MSGID_IDX]))
  925. {
  926. XP("-------------------------------------------\r\n");
  927. HTK_ByteArray2HexStr_XP(NULL, CurRxBuf, 0, IMCP_FM_HEAD_LEN + HTK_U16(CurRxBuf[IMCP_FM_DATALEN_LSB_IDX]) + IMCP_FM_TAIL_LEN);
  928. }
  929. #endif //PRINT_IMCP_MSG_ONLY_ON_UPDATEPORT
  930. #else //FUNC_RS485_SLAVE
  931. if (CheckPrintImcpMsg(UART_IAP_rx_buffer[IMCP_FM_MSGID_IDX]))
  932. {
  933. XP("-------------------------------------------\r\n");
  934. HTK_ByteArray2HexStr_XP(NULL, UART_IAP_rx_buffer, 0, IMCP_FM_HEAD_LEN + HTK_U16(UART_IAP_rx_buffer[IMCP_FM_DATALEN_LSB_IDX]) + IMCP_FM_TAIL_LEN);
  935. }
  936. #endif //FUNC_RS485_SLAVE
  937. #endif //ENABLE_PRINT_IMCP_MSG
  938. #ifdef FUNC_RS485_SLAVE
  939. switch (CurRxBuf[3])
  940. #else
  941. switch (UART_IAP_rx_buffer[3])
  942. #endif
  943. {
  944. /*
  945. Query message
  946. */
  947. case PROTOCOL_MESSAGE_QUERY_FW_VER:
  948. tx_len = 7 + (strlen(Charger.Ver_FW));
  949. tx[0] = 0xaa;
  950. tx[1] = PROTOCOL_ADDR;
  951. tx[2] = CurRxBuf[1];
  952. tx[3] = PROTOCOL_MESSAGE_QUERY_FW_VER;
  953. tx[4] = strlen(Charger.Ver_FW)&0xff;
  954. tx[5] = (strlen(Charger.Ver_FW)>>0x08) & 0xff;
  955. for(uint8_t idx=0;idx<strlen(Charger.Ver_FW);idx++)
  956. {
  957. tx[6+idx] = Charger.Ver_FW[idx];
  958. }
  959. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  960. {
  961. chksum ^= tx[6 + idx];
  962. }
  963. tx[6+(tx[4] | (tx[5]<<8))] = chksum;
  964. break;
  965. case PROTOCOL_MESSAGE_QUERY_HW_VER:
  966. tx_len = 7 + strlen(Charger.Ver_HW);
  967. tx[0] = 0xaa;
  968. tx[1] = PROTOCOL_ADDR;
  969. tx[2] = CurRxBuf[1];
  970. tx[3] = PROTOCOL_MESSAGE_QUERY_HW_VER;
  971. tx[4] = strlen(Charger.Ver_HW) & 0xff;
  972. tx[5] = (strlen(Charger.Ver_HW)>>0x08) & 0xff;
  973. for(uint8_t idx=0;idx<strlen(Charger.Ver_HW);idx++)
  974. {
  975. tx[6+idx] = Charger.Ver_HW[idx];
  976. }
  977. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  978. {
  979. chksum ^= tx[6 + idx];
  980. }
  981. tx[6+(tx[4] | (tx[5]<<8))] = chksum;
  982. break;
  983. case PROTOCOL_MESSAGE_QUERY_PRESENT_INPUT_VOLTAGE:
  984. tx_len = 14;
  985. tx[0] = 0xaa;
  986. tx[1] = PROTOCOL_ADDR;
  987. tx[2] = CurRxBuf[1];
  988. tx[3] = PROTOCOL_MESSAGE_QUERY_PRESENT_INPUT_VOLTAGE;
  989. tx[4] = 0x07;
  990. tx[5] = 0x00;
  991. tx[6] = 0x00;
  992. tx[7] = (((Charger.Voltage[0]/10)>>0) & 0xff);
  993. tx[8] = (((Charger.Voltage[0]/10)>>8) & 0xff);
  994. tx[9] = (((Charger.Voltage[1]/10)>>0) & 0xff);
  995. tx[10] = (((Charger.Voltage[1]/10)>>8) & 0xff);
  996. tx[11] = (((Charger.Voltage[2]/10)>>0) & 0xff);
  997. tx[12] = (((Charger.Voltage[2]/10)>>8) & 0xff);
  998. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  999. {
  1000. chksum ^= tx[6 + idx];
  1001. }
  1002. tx[13] = chksum;
  1003. break;
  1004. case PROTOCOL_MESSAGE_QUERY_PRESENT_OUTPUT_VOLTAGE:
  1005. tx_len = 15;
  1006. tx[0] = 0xaa;
  1007. tx[1] = PROTOCOL_ADDR;
  1008. tx[2] = CurRxBuf[1];
  1009. tx[3] = PROTOCOL_MESSAGE_QUERY_PRESENT_OUTPUT_VOLTAGE;
  1010. tx[4] = 0x08;
  1011. tx[5] = 0x00;
  1012. tx[6] = 0x00;
  1013. tx[7] = 0x00;
  1014. tx[8] = 0x00;
  1015. tx[9] = 0x00;
  1016. tx[10] = 0x00;
  1017. tx[11] = 0x00;
  1018. tx[12] = 0x00;
  1019. tx[13] = 0x00;
  1020. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1021. {
  1022. chksum ^= tx[6 + idx];
  1023. }
  1024. tx[14] = chksum;
  1025. break;
  1026. case PROTOCOL_MESSAGE_QUERY_FAN_SPEED:
  1027. tx_len = 15;
  1028. tx[0] = 0xaa;
  1029. tx[1] = PROTOCOL_ADDR;
  1030. tx[2] = CurRxBuf[1];
  1031. tx[3] = PROTOCOL_MESSAGE_QUERY_FAN_SPEED;
  1032. tx[4] = 0x08;
  1033. tx[5] = 0x00;
  1034. tx[6] = 0x00;
  1035. tx[7] = 0x00;
  1036. tx[8] = 0x00;
  1037. tx[9] = 0x00;
  1038. tx[10] = 0x00;
  1039. tx[11] = 0x00;
  1040. tx[12] = 0x00;
  1041. tx[13] = 0x00;
  1042. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1043. {
  1044. chksum ^= tx[6 + idx];
  1045. }
  1046. tx[14] = chksum;
  1047. break;
  1048. case PROTOCOL_MESSAGE_QUERY_TEMPERATURE:
  1049. tx_len = 15;
  1050. tx[0] = 0xaa;
  1051. tx[1] = PROTOCOL_ADDR;
  1052. tx[2] = CurRxBuf[1];
  1053. tx[3] = PROTOCOL_MESSAGE_QUERY_TEMPERATURE;
  1054. tx[4] = 0x08;
  1055. tx[5] = 0x00;
  1056. tx[6] = (((Charger.temperature.SystemAmbientTemp+60)>>0) & 0xff);
  1057. tx[7] = (((Charger.temperature.CcsConnectorTemp+60)>>0) & 0xff);
  1058. tx[8] = 0x00;
  1059. tx[9] = 0x00;
  1060. tx[10] = 0x00;
  1061. tx[11] = 0x00;
  1062. tx[12] = 0x00;
  1063. tx[13] = 0x00;
  1064. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1065. {
  1066. chksum ^= tx[6 + idx];
  1067. }
  1068. tx[14] = chksum;
  1069. break;
  1070. case PROTOCOL_MESSAGE_QUERY_AUX_POWER_VOLTAGE:
  1071. tx_len = 15;
  1072. tx[0] = 0xaa;
  1073. tx[1] = PROTOCOL_ADDR;
  1074. tx[2] = CurRxBuf[1];
  1075. tx[3] = PROTOCOL_MESSAGE_QUERY_AUX_POWER_VOLTAGE;
  1076. tx[4] = 0x08;
  1077. tx[5] = 0x00;
  1078. tx[6] = 0x00;
  1079. tx[7] = 0x00;
  1080. tx[8] = 0x00;
  1081. tx[9] = 0x00;
  1082. tx[10] = 0x00;
  1083. tx[11] = 0x00;
  1084. tx[12] = 0x00;
  1085. tx[13] = 0x00;
  1086. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1087. {
  1088. chksum ^= tx[6 + idx];
  1089. }
  1090. tx[14] = chksum;
  1091. break;
  1092. case PROTOCOL_MESSAGE_QUERY_RELAY_OUTPUT_STATUS:
  1093. tx_len = 9;
  1094. tx[0] = 0xaa;
  1095. tx[1] = PROTOCOL_ADDR;
  1096. tx[2] = CurRxBuf[1];
  1097. tx[3] = PROTOCOL_MESSAGE_QUERY_RELAY_OUTPUT_STATUS;
  1098. tx[4] = 0x02;
  1099. tx[5] = 0x00;
  1100. tx[6] = ((Relay1_Action_Get()==GPIO_RELAY1_ACTION_OFF)?0:1);
  1101. tx[7] = ((Relay2_Action_Get()==GPIO_RELAY2_ACTION_OFF)?0:1);
  1102. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1103. {
  1104. chksum ^= tx[6 + idx];
  1105. }
  1106. tx[8] = chksum;
  1107. break;
  1108. case PROTOCOL_MESSAGE_QUERY_GFD_ADC_VALUE:
  1109. tx_len = 15;
  1110. tx[0] = 0xaa;
  1111. tx[1] = PROTOCOL_ADDR;
  1112. tx[2] = CurRxBuf[1];
  1113. tx[3] = PROTOCOL_MESSAGE_QUERY_GFD_ADC_VALUE;
  1114. tx[4] = 0x08;
  1115. tx[5] = 0x00;
  1116. tx[6] = ((adc_value.ADC2_IN6_GF.value>>0) & 0xff);
  1117. tx[7] = ((adc_value.ADC2_IN6_GF.value>>8) & 0xff);
  1118. tx[8] = 0x00;
  1119. tx[9] = 0x00;
  1120. tx[10] = 0x00;
  1121. tx[11] = 0x00;
  1122. tx[12] = 0x00;
  1123. tx[13] = 0x00;
  1124. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1125. {
  1126. chksum ^= tx[6 + idx];
  1127. }
  1128. tx[14] = chksum;
  1129. break;
  1130. case PROTOCOL_MESSAGE_QUERY_INPUT_GPIO_STATUS:
  1131. tx_len = 9;
  1132. tx[0] = 0xaa;
  1133. tx[1] = PROTOCOL_ADDR;
  1134. tx[2] = CurRxBuf[1];
  1135. tx[3] = PROTOCOL_MESSAGE_QUERY_INPUT_GPIO_STATUS;
  1136. tx[4] = 0x02;
  1137. tx[5] = 0x00;
  1138. tx[6] = 0x00;
  1139. tx[7] = 0x00;
  1140. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1141. {
  1142. chksum ^= tx[6 + idx];
  1143. }
  1144. tx[8] = chksum;
  1145. break;
  1146. case PROTOCOL_MESSAGE_QUERY_ALARM_LOG:
  1147. tx_len = 17;
  1148. tx[0] = 0xaa;
  1149. tx[1] = PROTOCOL_ADDR;
  1150. tx[2] = CurRxBuf[1];
  1151. tx[3] = PROTOCOL_MESSAGE_QUERY_ALARM_LOG;
  1152. tx[4] = 0x0a;
  1153. tx[5] = 0x00;
  1154. tx[6] = CurRxBuf[7] ;
  1155. tx[7] = CurRxBuf[8] ;
  1156. tx[8] = CharacterArray[(Charger.memory.hisAlarm.data.item[CurRxBuf[7] | (CurRxBuf[8]>>8)].alarmCode & 0xF0000000)>>28];
  1157. tx[9] = CharacterArray[(Charger.memory.hisAlarm.data.item[CurRxBuf[7] | (CurRxBuf[8]>>8)].alarmCode & 0x0F000000)>>24];
  1158. tx[10] = CharacterArray[(Charger.memory.hisAlarm.data.item[CurRxBuf[7] | (CurRxBuf[8]>>8)].alarmCode & 0x00F00000)>>20];
  1159. tx[11] = CharacterArray[(Charger.memory.hisAlarm.data.item[CurRxBuf[7] | (CurRxBuf[8]>>8)].alarmCode & 0x000F0000)>>16];
  1160. tx[12] = CharacterArray[(Charger.memory.hisAlarm.data.item[CurRxBuf[7] | (CurRxBuf[8]>>8)].alarmCode & 0x0000F000)>>12];
  1161. tx[13] = CharacterArray[(Charger.memory.hisAlarm.data.item[CurRxBuf[7] | (CurRxBuf[8]>>8)].alarmCode & 0x00000F00)>>8];
  1162. tx[14] = CharacterArray[(Charger.memory.hisAlarm.data.item [CurRxBuf[7] | (CurRxBuf[8]>>8)].alarmCode & 0x000000F0)>>4];
  1163. tx[15] = CharacterArray[ Charger.memory.hisAlarm.data.item [CurRxBuf[7] | (CurRxBuf[8]>>8)].alarmCode & 0x0000000F ];
  1164. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1165. {
  1166. chksum ^= tx[6 + idx];
  1167. }
  1168. tx[16] = chksum;
  1169. /*
  1170. tx[8] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].DateTime.year>>0) & 0xff);
  1171. tx[9] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].DateTime.year>>8) & 0xff);
  1172. tx[10] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].DateTime.month>>0) & 0xff);;
  1173. tx[11] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].DateTime.day>>0) & 0xff);
  1174. tx[12] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].DateTime.hour>>0) & 0xff);
  1175. tx[13] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].DateTime.min>>0) & 0xff);
  1176. tx[14] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].DateTime.sec>>0) & 0xff);
  1177. tx[15] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].alarmCode>>0) & 0xff);
  1178. tx[16] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].alarmCode>>8) & 0xff);
  1179. tx[17] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].alarmCode>>16) & 0xff);
  1180. tx[18] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].alarmCode>>24) & 0xff);
  1181. tx[19] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].mode>>0) & 0xff);
  1182. tx[20] = ((Charger.memory.hisAlarm.data.item[UART_IAP_rx_buffer[7] | (UART_IAP_rx_buffer[8]<<8)].isRecover>0) & 0xff);
  1183. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1184. {
  1185. chksum ^= tx[6 + idx];
  1186. }
  1187. tx[21] = chksum;
  1188. */
  1189. break;
  1190. case PROTOCOL_MESSAGE_QUERY_SN:
  1191. // head +checksum = 7 , SystemDateTimeyear month day = 8 , SerialNumber
  1192. tx_len = 15 + strlen(Charger.memory.EVSE_Config.data.item.SerialNumber);
  1193. tx[0] = 0xaa;
  1194. tx[1] = PROTOCOL_ADDR;
  1195. tx[2] = CurRxBuf[1];
  1196. tx[3] = PROTOCOL_MESSAGE_QUERY_SN;
  1197. tx[4] = (strlen(Charger.memory.EVSE_Config.data.item.SerialNumber)+8)&0xff;
  1198. tx[5] = ((strlen(Charger.memory.EVSE_Config.data.item.SerialNumber)+8)>>0x08) & 0xff;
  1199. tx[6] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.year/1000)+'0';
  1200. tx[7] = ((Charger.memory.EVSE_Config.data.item.SystemDateTime.year%1000)/100)+'0';
  1201. tx[8] = ((Charger.memory.EVSE_Config.data.item.SystemDateTime.year%100)/10)+'0';
  1202. tx[9] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.year%10) +'0';
  1203. tx[10] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.month/10) +'0';
  1204. tx[11] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.month%10) +'0';
  1205. tx[12] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.day/10) +'0';
  1206. tx[13] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.day%10) +'0';
  1207. for(uint8_t idx=0;idx<strlen(Charger.memory.EVSE_Config.data.item.SerialNumber);idx++)
  1208. {
  1209. tx[14+idx] = Charger.memory.EVSE_Config.data.item.SerialNumber[idx];
  1210. }
  1211. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1212. {
  1213. chksum ^= tx[6 + idx];
  1214. }
  1215. tx[6+(tx[4] | (tx[5]<<8))] = chksum;
  1216. break;
  1217. case PROTOCOL_MESSAGE_QUERY_MODEL_NAME:
  1218. tx_len = 7 + strlen(Charger.memory.EVSE_Config.data.item.ModelName);;
  1219. tx[0] = 0xaa;
  1220. tx[1] = PROTOCOL_ADDR;
  1221. tx[2] = CurRxBuf[1];
  1222. tx[3] = PROTOCOL_MESSAGE_QUERY_MODEL_NAME;
  1223. tx[4] = strlen(Charger.memory.EVSE_Config.data.item.ModelName)&0xff;
  1224. tx[5] = (strlen(Charger.memory.EVSE_Config.data.item.ModelName)>>0x08) & 0xff;
  1225. for(uint8_t idx=0;idx<strlen(Charger.memory.EVSE_Config.data.item.ModelName);idx++)
  1226. {
  1227. tx[6+idx] = Charger.memory.EVSE_Config.data.item.ModelName[idx];
  1228. }
  1229. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1230. {
  1231. chksum ^= tx[6 + idx];
  1232. }
  1233. tx[6+(tx[4] | (tx[5]<<8))] = chksum;
  1234. break;
  1235. case PROTOCOL_MESSAGE_QUERY_PARAMETER:
  1236. tx_len = 11;
  1237. tx[0] = 0xaa;
  1238. tx[1] = PROTOCOL_ADDR;
  1239. tx[2] = CurRxBuf[1];
  1240. tx[3] = PROTOCOL_MESSAGE_QUERY_PARAMETER;
  1241. tx[4] = 0x04;
  1242. tx[5] = 0x00;
  1243. tx[6] = CurRxBuf[6];
  1244. tx[7] = CurRxBuf[7];
  1245. switch(CurRxBuf[6])
  1246. {
  1247. case INPUT_L1_AC_VOLTAGE:
  1248. tx[8] = Charger.memory.EVSE_Config.data.item.Correction_VL1[CurRxBuf[7]][1] & 0xff;
  1249. tx[9] = (Charger.memory.EVSE_Config.data.item.Correction_VL1[CurRxBuf[7]][1]>>8) & 0xff;
  1250. break;
  1251. case INPUT_L2_AC_VOLTAGE:
  1252. tx[8] = Charger.memory.EVSE_Config.data.item.Correction_VL2[CurRxBuf[7]][1] & 0xff;
  1253. tx[9] = (Charger.memory.EVSE_Config.data.item.Correction_VL2[CurRxBuf[7]][1]>>8) & 0xff;
  1254. break;
  1255. case INPUT_L3_AC_VOLTAGE:
  1256. tx[8] = Charger.memory.EVSE_Config.data.item.Correction_VL3[CurRxBuf[7]][1] & 0xff;
  1257. tx[9] = (Charger.memory.EVSE_Config.data.item.Correction_VL3[CurRxBuf[7]][1]>>8) & 0xff;
  1258. break;
  1259. case OUTPUT_L1_CURRENT:
  1260. tx[8] = Charger.memory.EVSE_Config.data.item.Correction_CL1[CurRxBuf[7]][1] & 0xff;
  1261. tx[9] = (Charger.memory.EVSE_Config.data.item.Correction_CL1[CurRxBuf[7]][1]>>8) & 0xff;
  1262. break;
  1263. case OUTPUT_L2_CURRENT:
  1264. tx[8] = Charger.memory.EVSE_Config.data.item.Correction_CL2[CurRxBuf[7]][1] & 0xff;
  1265. tx[9] = (Charger.memory.EVSE_Config.data.item.Correction_CL2[CurRxBuf[7]][1]>>8) & 0xff;
  1266. break;
  1267. case OUTPUT_L3_CURRENT:
  1268. tx[8] = Charger.memory.EVSE_Config.data.item.Correction_CL3[CurRxBuf[7]][1] & 0xff;
  1269. tx[9] = (Charger.memory.EVSE_Config.data.item.Correction_CL3[CurRxBuf[7]][1]>>8) & 0xff;
  1270. break;
  1271. case LEAK_CURRENT:
  1272. tx[8] = (Charger.memory.EVSE_Config.data.item.Correction_Leak[CurRxBuf[7]][1]/100) & 0xff;
  1273. tx[9] = ((Charger.memory.EVSE_Config.data.item.Correction_Leak[CurRxBuf[7]][1]/100)>>8) & 0xff;
  1274. break;
  1275. }
  1276. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1277. {
  1278. chksum ^= tx[6 + idx];
  1279. }
  1280. tx[10] = chksum;
  1281. break;
  1282. case PROTOCOL_MESSAGE_QUERY_RTC:
  1283. tx_len = 21;
  1284. tx[0] = 0xaa;
  1285. tx[1] = PROTOCOL_ADDR;
  1286. tx[2] = CurRxBuf[1];
  1287. tx[3] = PROTOCOL_MESSAGE_QUERY_RTC;
  1288. tx[4] = 0x0e;
  1289. tx[5] = 0x00;
  1290. tx[6] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.year/1000)+'0';
  1291. tx[7] = ((Charger.memory.EVSE_Config.data.item.SystemDateTime.year%1000)/100)+'0';
  1292. tx[8] = ((Charger.memory.EVSE_Config.data.item.SystemDateTime.year%100)/10)+'0';
  1293. tx[9] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.year%10) +'0';
  1294. tx[10] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.month/10) +'0';
  1295. tx[11] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.month%10) +'0';
  1296. tx[12] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.day/10) +'0';
  1297. tx[13] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.day%10) +'0';
  1298. tx[14] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.hour/10) +'0';
  1299. tx[15] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.hour%10) +'0';
  1300. tx[16] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.min/10) +'0';
  1301. tx[17] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.min%10) +'0';
  1302. tx[18] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.sec/10) +'0';
  1303. tx[19] = (Charger.memory.EVSE_Config.data.item.SystemDateTime.sec%10) +'0';
  1304. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1305. {
  1306. chksum ^= tx[6 + idx];
  1307. }
  1308. tx[20] = chksum;
  1309. break;
  1310. case PROTOCOL_MESSAGE_QUERY_PRESENT_OUTPUT_CURRENT:
  1311. tx_len = 19;
  1312. tx[0] = 0xaa;
  1313. tx[1] = PROTOCOL_ADDR;
  1314. tx[2] = CurRxBuf[1];
  1315. tx[3] = PROTOCOL_MESSAGE_QUERY_PRESENT_OUTPUT_CURRENT;
  1316. tx[4] = 0x0c;
  1317. tx[5] = 0x00;
  1318. #ifdef FUNC_ZERO_CURRENT_WHEN_OUTP_OFF
  1319. if (Charger.Relay_Action == GPIO_RELAY_ACTION_ON)
  1320. {
  1321. tx[6] = (((Charger.Current[0]/10)>>0) & 0xff);
  1322. tx[7] = (((Charger.Current[0]/10)>>8) & 0xff);
  1323. tx[8] = (((Charger.Current[1]/10)>>0) & 0xff);
  1324. tx[9] = (((Charger.Current[1]/10)>>8) & 0xff);
  1325. tx[10] = (((Charger.Current[2]/10)>>0) & 0xff);
  1326. tx[11] = (((Charger.Current[2]/10)>>8) & 0xff);
  1327. }
  1328. else
  1329. {
  1330. tx[6] = 0;
  1331. tx[7] = 0;
  1332. tx[8] = 0;
  1333. tx[9] = 0;
  1334. tx[10] = 0;
  1335. tx[11] = 0;
  1336. }
  1337. #else
  1338. tx[6] = (((Charger.Current[0]/10)>>0) & 0xff);
  1339. tx[7] = (((Charger.Current[0]/10)>>8) & 0xff);
  1340. tx[8] = (((Charger.Current[1]/10)>>0) & 0xff);
  1341. tx[9] = (((Charger.Current[1]/10)>>8) & 0xff);
  1342. tx[10] = (((Charger.Current[2]/10)>>0) & 0xff);
  1343. tx[11] = (((Charger.Current[2]/10)>>8) & 0xff);
  1344. #endif
  1345. tx[12] = 0x00;
  1346. tx[13] = 0x00;
  1347. tx[14] = 0x00;
  1348. tx[15] = 0x00;
  1349. tx[16] = 0x00;
  1350. tx[17] = 0x00;
  1351. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1352. {
  1353. chksum ^= tx[6 + idx];
  1354. }
  1355. tx[18] = chksum;
  1356. break;
  1357. //edward 2019/08/27
  1358. case PROTOCOL_MESSAGE_QUERY_EVSE_STATE:
  1359. tx_len = 21;
  1360. tx[0] = 0xaa;
  1361. tx[1] = PROTOCOL_ADDR;
  1362. tx[2] = CurRxBuf[1];
  1363. tx[3] = PROTOCOL_MESSAGE_QUERY_EVSE_STATE;
  1364. tx[4] = 14;
  1365. tx[5] = 0x00;
  1366. tx[6] = Charger.CP_State;
  1367. #ifdef MODIFY_AC_EVSE_STATUS_MAX_CHARGING_CURRENT_VARIABLE
  1368. tx[7] = (Charger.AC_MaxChargingCurrentOrDuty&0xff);
  1369. tx[8] = Charger.AC_MaxChargingCurrentOrDuty>>8;
  1370. #else
  1371. tx[7] = (Charger.memory.EVSE_Config.data.item.MaxChargingCurrent&0xff);
  1372. tx[8] = Charger.memory.EVSE_Config.data.item.MaxChargingCurrent>>8;
  1373. #endif
  1374. uint16_t transfer_float;
  1375. {
  1376. #ifdef FUNC_CP_ADC_MODIFY
  1377. transfer_float = (uint16_t)(CpDetectionResult.PositiveValue == 0 ? 0 : HTK_GetPosFromZero(CP_GET_OPA_VIN_USE_ADC(CpDetectionResult.PositiveValue)) * 100);
  1378. #else
  1379. //transfer_float = (uint16_t)((float)(((1.59131-((3.3*CpDetectionResult.PositiveValue)/4095))*(475/53.6)))*100);
  1380. #endif
  1381. }
  1382. tx[9] = (transfer_float&0xff);
  1383. tx[10] = (transfer_float&0xff00)>>8;
  1384. {
  1385. #ifdef FUNC_CP_ADC_MODIFY
  1386. transfer_float = (uint16_t)(CpDetectionResult.NegativeValue == 0 ? 0 : HTK_GetNegFromZero(CP_GET_OPA_VIN_USE_ADC(CpDetectionResult.NegativeValue)) * 100);
  1387. #else
  1388. //transfer_float = (uint16_t)((float)(((((3.3*CpDetectionResult.NegativeValue)/4095)-1.59131)*(475/53.6)))*100);
  1389. #endif
  1390. }
  1391. tx[11] = (transfer_float&0xff);
  1392. tx[12] = (transfer_float&0xff00)>>8;
  1393. tx[13] = 0x00;
  1394. if(Charger.Relay_Action == GPIO_RELAY_ACTION_ON)
  1395. {
  1396. tx[14] = 0x01;
  1397. }
  1398. else
  1399. {
  1400. tx[14] = 0x00;
  1401. }
  1402. tx[15] = 0x00;
  1403. tx[16] = 0x00;
  1404. tx[17] = 0x00;
  1405. tx[18] = Charger.maxRatingCurrent;
  1406. tmpBuf += (HAL_GPIO_ReadPin(IN_AC_Current_Set1_GPIO_Port, IN_AC_Current_Set1_Pin)?0:1)<<0;
  1407. tmpBuf += (HAL_GPIO_ReadPin(IN_AC_Current_Set2_GPIO_Port, IN_AC_Current_Set2_Pin)?0:1)<<1;
  1408. tmpBuf += (HAL_GPIO_ReadPin(IN_AC_Current_Set3_GPIO_Port, IN_AC_Current_Set3_Pin)?0:1)<<2;
  1409. tmpBuf += (HAL_GPIO_ReadPin(IN_AC_Current_Set4_GPIO_Port, IN_AC_Current_Set4_Pin)?0:1)<<3;
  1410. tmpBuf += (HAL_GPIO_ReadPin(IN_GridType_Select_GPIO_Port, IN_GridType_Select_Pin)?0:1)<<4;
  1411. tmpBuf += (HAL_GPIO_ReadPin(IN_Grid_Groundding_GPIO_Port, IN_Grid_Groundding_Pin)?0:1)<<5;
  1412. tx[19] = tmpBuf;
  1413. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1414. {
  1415. chksum ^= tx[6 + idx];
  1416. }
  1417. tx[20] = chksum;
  1418. break;
  1419. case PROTOCOL_MESSAGE_QUERY_EVSE_ALARM:
  1420. tx_len = 11;
  1421. tx[0] = 0xaa;
  1422. tx[1] = PROTOCOL_ADDR;
  1423. tx[2] = CurRxBuf[1];
  1424. tx[3] = PROTOCOL_MESSAGE_QUERY_EVSE_ALARM;
  1425. tx[4] = 4;
  1426. tx[5] = 0x00;
  1427. tx[6] = (Charger.Alarm_Code&0xff);
  1428. tx[7] = (Charger.Alarm_Code&0xff00)>>8;
  1429. tx[8] = (Charger.Alarm_Code&0xff0000)>>16;
  1430. tx[9] = (Charger.Alarm_Code&0xff000000)>>24;
  1431. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1432. {
  1433. chksum ^= tx[6 + idx];
  1434. }
  1435. tx[10] = chksum;
  1436. break;
  1437. case PROTOCOL_MESSAGE_QUERY_BLE_STATE:
  1438. tx_len = 10;
  1439. tx[0] = 0xaa;
  1440. tx[1] = PROTOCOL_ADDR;
  1441. tx[2] = CurRxBuf[1];
  1442. tx[3] = PROTOCOL_MESSAGE_QUERY_BLE_STATE;
  1443. tx[4] = 3;
  1444. tx[5] = 0x00;
  1445. tx[6] = 0x00 ;
  1446. tx[7] = 0x00 ;
  1447. tx[8] = 0x00 ;
  1448. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1449. {
  1450. chksum ^= tx[6 + idx];
  1451. }
  1452. tx[9] = chksum;
  1453. break;
  1454. case PROTOCOL_MESSAGE_QUERY_BLE_LOGIC_CENTRAL_ID:
  1455. tx_len = 39;
  1456. tx[0] = 0xaa;
  1457. tx[1] = PROTOCOL_ADDR;
  1458. tx[2] = CurRxBuf[1];
  1459. tx[3] = PROTOCOL_MESSAGE_QUERY_BLE_LOGIC_CENTRAL_ID;
  1460. tx[4] = 32;
  1461. tx[5] = 0x00;
  1462. tx[6] = 'P';
  1463. tx[7] = 'H';
  1464. tx[8] = 'I';
  1465. tx[9] = 'H';
  1466. tx[10] = 'O';
  1467. tx[11] = 'N';
  1468. tx[12] = 'G';
  1469. tx[13] = ' ';
  1470. tx[14] = 'A';
  1471. tx[15] = 'C';
  1472. tx[16] = '-';
  1473. tx[17] = 'S';
  1474. tx[18] = 'i';
  1475. tx[19] = 'm';
  1476. tx[20] = 'p';
  1477. tx[21] = 'l';
  1478. tx[22] = 'e';
  1479. tx[23] = ' ';
  1480. tx[24] = '1';
  1481. tx[25] = '2';
  1482. tx[26] = '3';
  1483. tx[27] = '4';
  1484. tx[28] = '5';
  1485. tx[29] = '6';
  1486. tx[30] = '7';
  1487. tx[31] = '8';
  1488. tx[32] = '9';
  1489. tx[33] = 'A';
  1490. tx[34] = 'B';
  1491. tx[35] = 'C';
  1492. tx[36] = 'D';
  1493. tx[37] = 'E';
  1494. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1495. {
  1496. chksum ^= tx[6 + idx];
  1497. }
  1498. tx[38] = chksum;
  1499. break;
  1500. case PROTOCOL_MESSAGE_QUERY_POWER_CONSUME:
  1501. tx_len = 11;
  1502. tx[0] = 0xaa;
  1503. tx[1] = PROTOCOL_ADDR;
  1504. tx[2] = CurRxBuf[1];
  1505. tx[3] = PROTOCOL_MESSAGE_QUERY_POWER_CONSUME;
  1506. tx[4] = 4;
  1507. tx[5] = 0x00;
  1508. tx[6] = ((Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative/100)&0xff);
  1509. tx[7] = ((Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative/100)&0xff00)>>8;
  1510. tx[8] = ((Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative/100)&0xff0000)>>16;
  1511. tx[9] = ((Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative/100)&0xff000000)>>24;
  1512. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1513. {
  1514. chksum ^= tx[6 + idx];
  1515. }
  1516. tx[10] = chksum;
  1517. break;
  1518. case PROTOCOL_MESSAGE_QUERY_GUN_PLUGIN_TIMES:
  1519. tx_len = 11;
  1520. tx[0] = 0xaa;
  1521. tx[1] = PROTOCOL_ADDR;
  1522. tx[2] = CurRxBuf[1];
  1523. tx[3] = PROTOCOL_MESSAGE_QUERY_GUN_PLUGIN_TIMES;
  1524. tx[4] = 4;
  1525. tx[5] = 0x00;
  1526. tx[6] = (Charger.memory.EVSE_Config.data.item.AcPlugInTimes&0xff);
  1527. tx[7] = (Charger.memory.EVSE_Config.data.item.AcPlugInTimes&0xff00)>>8;
  1528. tx[8] = (Charger.memory.EVSE_Config.data.item.AcPlugInTimes&0xff0000)>>16;
  1529. tx[9] = (Charger.memory.EVSE_Config.data.item.AcPlugInTimes&0xff000000)>>24;
  1530. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1531. {
  1532. chksum ^= tx[6 + idx];
  1533. }
  1534. tx[10] = chksum;
  1535. break;
  1536. case PROTOCOL_MESSAGE_QUERY_EVSE_AUTH_MODE:
  1537. tx_len = 9;
  1538. tx[0] = 0xaa;
  1539. tx[1] = PROTOCOL_ADDR;
  1540. tx[2] = CurRxBuf[1];
  1541. tx[3] = PROTOCOL_MESSAGE_QUERY_EVSE_AUTH_MODE;
  1542. tx[4] = 2;
  1543. tx[5] = 0x00;
  1544. tx[6] = Charger.memory.EVSE_Config.data.item.AuthMode;
  1545. tx[7] = Charger.memory.EVSE_Config.data.item.OfflinePolicy;
  1546. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1547. {
  1548. chksum ^= tx[6 + idx];
  1549. }
  1550. tx[8] = chksum;
  1551. break;
  1552. case PROTOCOL_MESSAGE_QUERY_PRESENT_LEAK_CURRENT:
  1553. tx_len = 9;
  1554. tx[0] = 0xaa;
  1555. tx[1] = PROTOCOL_ADDR;
  1556. tx[2] = CurRxBuf[1];
  1557. tx[3] = PROTOCOL_MESSAGE_QUERY_PRESENT_LEAK_CURRENT;
  1558. tx[4] = 2;
  1559. tx[5] = 0x00;
  1560. tx[6] = Charger.Leak_Current/100;
  1561. tx[7] = 0;
  1562. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1563. {
  1564. chksum ^= tx[6 + idx];
  1565. }
  1566. tx[8] = chksum;
  1567. break;
  1568. /*
  1569. Config message
  1570. */
  1571. case PROTOCOL_MESSAGE_CONFIG_FAN_SPEED:
  1572. tx_len = 8;
  1573. tx[0] = 0xaa;
  1574. tx[1] = PROTOCOL_ADDR;
  1575. tx[2] = CurRxBuf[1];
  1576. tx[3] = PROTOCOL_MESSAGE_CONFIG_FAN_SPEED;
  1577. tx[4] = 0x01;
  1578. tx[5] = 0x00;
  1579. tx[6] = 0x01;
  1580. tx[7] = 0x01;
  1581. break;
  1582. case PROTOCOL_MESSAGE_CONFIG_SN:
  1583. tx_len = 8;
  1584. tx[0] = 0xaa;
  1585. tx[1] = PROTOCOL_ADDR;
  1586. tx[2] = CurRxBuf[1];
  1587. tx[3] = PROTOCOL_MESSAGE_CONFIG_SN;
  1588. tx[4] = 0x01;
  1589. tx[5] = 0x00;
  1590. #ifdef MODIFY_CONFIG_SN
  1591. {
  1592. uint16_t len = HTK_U16(CurRxBuf[4]);
  1593. if (len < sizeof(Charger.memory.EVSE_Config.data.item.ModelName) && HTK_IS_DISP_CHAR(CurRxBuf[14]))
  1594. {
  1595. //OK
  1596. tx[6] = 0x01;
  1597. tx[7] = 0x01;
  1598. memset(Charger.memory.EVSE_Config.data.item.SerialNumber, 0, ARRAY_SIZE(Charger.memory.EVSE_Config.data.item.SerialNumber));
  1599. memset(Charger.memory.EVSE_Config.data.item.System_ID, 0, ARRAY_SIZE(Charger.memory.EVSE_Config.data.item.System_ID));
  1600. for(uint16_t idx=0;idx<(len-8);idx++)
  1601. {
  1602. Charger.memory.EVSE_Config.data.item.SerialNumber[idx] = CurRxBuf[14+idx];
  1603. }
  1604. Charger.memory.EVSE_Config.data.item.SerialNumber[len-8] = '\0';
  1605. memcpy(Charger.memory.EVSE_Config.data.item.System_ID, Charger.memory.EVSE_Config.data.item.ModelName, strlen(Charger.memory.EVSE_Config.data.item.ModelName));
  1606. memcpy(&Charger.memory.EVSE_Config.data.item.System_ID[strlen(Charger.memory.EVSE_Config.data.item.ModelName)], Charger.memory.EVSE_Config.data.item.SerialNumber, len);
  1607. XP("#CONFIG_SN: %s\r\n", Charger.memory.EVSE_Config.data.item.SerialNumber);
  1608. //parseVersionInfoFromModelname() ;
  1609. Charger.memory.EVSE_Config.op_bits.update = ON;
  1610. break;
  1611. }
  1612. else
  1613. {
  1614. //NG
  1615. tx[6] = 0x00;
  1616. tx[7] = 0x00;
  1617. break;
  1618. }
  1619. }
  1620. #else //MODIFY_CONFIG_SN
  1621. // tx[6] = 0x01;
  1622. // tx[7] = 0x01;
  1623. //
  1624. // memset(Charger.memory.EVSE_Config.data.item.SerialNumber, 0x00, sizeof(Charger.memory.EVSE_Config.data.item.SerialNumber)/sizeof(Charger.memory.EVSE_Config.data.item.SerialNumber[0]));
  1625. // memset(Charger.memory.EVSE_Config.data.item.System_ID, 0x00, sizeof(Charger.memory.EVSE_Config.data.item.System_ID)/sizeof(Charger.memory.EVSE_Config.data.item.System_ID[0]));
  1626. // for(uint16_t idx=0;idx<((CurRxBuf[4] | (CurRxBuf[5]<<8))-8);idx++)
  1627. // {
  1628. // Charger.memory.EVSE_Config.data.item.SerialNumber[idx] = CurRxBuf[14+idx];
  1629. // }
  1630. // Charger.memory.EVSE_Config.data.item.SerialNumber[(CurRxBuf[4] | (CurRxBuf[5]<<8))-8] = '\0';
  1631. // memcpy(Charger.memory.EVSE_Config.data.item.System_ID, Charger.memory.EVSE_Config.data.item.ModelName, strlen(Charger.memory.EVSE_Config.data.item.ModelName));
  1632. // memcpy(&Charger.memory.EVSE_Config.data.item.System_ID[strlen(Charger.memory.EVSE_Config.data.item.ModelName)], Charger.memory.EVSE_Config.data.item.SerialNumber, (CurRxBuf[4] | (CurRxBuf[5]<<8)));
  1633. //
  1634. // parseVersionInfoFromModelname() ;
  1635. // Charger.memory.EVSE_Config.op_bits.update = ON;
  1636. // break;
  1637. #endif //MODIFY_CONFIG_SN
  1638. case PROTOCOL_MESSAGE_CONFIG_MODEL_NAME:
  1639. tx_len = 8;
  1640. tx[0] = 0xaa;
  1641. tx[1] = PROTOCOL_ADDR;
  1642. tx[2] = CurRxBuf[1];
  1643. tx[3] = PROTOCOL_MESSAGE_CONFIG_MODEL_NAME;
  1644. tx[4] = 0x01;
  1645. tx[5] = 0x00;
  1646. #ifdef MODIFY_CONFIG_MODEL_NAME
  1647. {
  1648. uint16_t len = HTK_U16(CurRxBuf[4]);
  1649. if (len < sizeof(Charger.memory.EVSE_Config.data.item.ModelName) && HTK_IS_DISP_CHAR(CurRxBuf[6]))
  1650. {
  1651. //OK
  1652. tx[6] = 0x01;
  1653. tx[7] = 0x01;
  1654. memset(Charger.memory.EVSE_Config.data.item.ModelName, 0, ARRAY_SIZE(Charger.memory.EVSE_Config.data.item.ModelName));
  1655. memset(Charger.memory.EVSE_Config.data.item.System_ID, 0, ARRAY_SIZE(Charger.memory.EVSE_Config.data.item.System_ID));
  1656. for(uint16_t idx=0;idx<len;idx++)
  1657. {
  1658. Charger.memory.EVSE_Config.data.item.ModelName[idx] = CurRxBuf[6+idx];
  1659. }
  1660. Charger.memory.EVSE_Config.data.item.ModelName[len] = '\0';
  1661. memcpy(Charger.memory.EVSE_Config.data.item.System_ID, Charger.memory.EVSE_Config.data.item.ModelName, strlen(Charger.memory.EVSE_Config.data.item.ModelName));
  1662. memcpy(&Charger.memory.EVSE_Config.data.item.System_ID[strlen(Charger.memory.EVSE_Config.data.item.ModelName)], Charger.memory.EVSE_Config.data.item.SerialNumber, len);
  1663. XP("#CONFIG_MODEL_NAME: %s\r\n", Charger.memory.EVSE_Config.data.item.ModelName);
  1664. #ifdef FUNC_AUTO_MODIFY_CCID_MODULE
  1665. Charger.m_bCCID_MustModify = HTK_TRUE;
  1666. #endif
  1667. parseVersionInfoFromModelname() ;
  1668. Charger.memory.EVSE_Config.op_bits.update = ON;
  1669. break;
  1670. }
  1671. else
  1672. {
  1673. //NG
  1674. tx[6] = 0x00;
  1675. tx[7] = 0x00;
  1676. break;
  1677. }
  1678. }
  1679. #else //MODIFY_CONFIG_MODEL_NAME
  1680. // tx[6] = 0x01;
  1681. // tx[7] = 0x01;
  1682. //
  1683. // memset(Charger.memory.EVSE_Config.data.item.ModelName, 0x00, sizeof(Charger.memory.EVSE_Config.data.item.ModelName)/sizeof(Charger.memory.EVSE_Config.data.item.ModelName[0]));
  1684. // memset(Charger.memory.EVSE_Config.data.item.System_ID, 0x00, sizeof(Charger.memory.EVSE_Config.data.item.System_ID)/sizeof(Charger.memory.EVSE_Config.data.item.System_ID[0]));
  1685. // for(uint16_t idx=0;idx<(CurRxBuf[4] | (CurRxBuf[5]<<8));idx++)
  1686. // {
  1687. // Charger.memory.EVSE_Config.data.item.ModelName[idx] = CurRxBuf[6+idx];
  1688. // }
  1689. // Charger.memory.EVSE_Config.data.item.ModelName[(CurRxBuf[4] | (CurRxBuf[5]<<8))] = '\0';
  1690. // memcpy(Charger.memory.EVSE_Config.data.item.System_ID, Charger.memory.EVSE_Config.data.item.ModelName, strlen(Charger.memory.EVSE_Config.data.item.ModelName));
  1691. // memcpy(&Charger.memory.EVSE_Config.data.item.System_ID[strlen(Charger.memory.EVSE_Config.data.item.ModelName)], Charger.memory.EVSE_Config.data.item.SerialNumber, (CurRxBuf[4] | (CurRxBuf[5]<<8)));
  1692. //
  1693. // parseVersionInfoFromModelname() ;
  1694. // Charger.memory.EVSE_Config.op_bits.update = ON;
  1695. // break;
  1696. #endif //MODIFY_CONFIG_MODEL_NAME
  1697. case PROTOCOL_MESSAGE_CONFIG_PARAMETER:
  1698. tx_len = 8;
  1699. tx[0] = 0xaa;
  1700. tx[1] = PROTOCOL_ADDR;
  1701. tx[2] = CurRxBuf[1];
  1702. tx[3] = PROTOCOL_MESSAGE_CONFIG_PARAMETER;
  1703. tx[4] = 0x01;
  1704. tx[5] = 0x00;
  1705. tx[6] = 0x01;
  1706. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1707. {
  1708. chksum ^= tx[6 + idx];
  1709. }
  1710. tx[7] = chksum; //checksum
  1711. switch(CurRxBuf[6])
  1712. {
  1713. case INPUT_L1_AC_VOLTAGE:
  1714. Charger.memory.EVSE_Config.data.item.Correction_VL1[CurRxBuf[7]][0] = adc_value.ADC3_IN9_Voltage_L1.value/10;
  1715. Charger.memory.EVSE_Config.data.item.Correction_VL1[CurRxBuf[7]][1] = CurRxBuf[8] | (CurRxBuf[9]<<8);
  1716. break;
  1717. case INPUT_L2_AC_VOLTAGE:
  1718. Charger.memory.EVSE_Config.data.item.Correction_VL2[CurRxBuf[7]][0] = Charger.Voltage[1];
  1719. Charger.memory.EVSE_Config.data.item.Correction_VL2[CurRxBuf[7]][1] = CurRxBuf[8] | (CurRxBuf[9]<<8);
  1720. break;
  1721. case INPUT_L3_AC_VOLTAGE:
  1722. Charger.memory.EVSE_Config.data.item.Correction_VL3[CurRxBuf[7]][0] = Charger.Voltage[2];
  1723. Charger.memory.EVSE_Config.data.item.Correction_VL3[CurRxBuf[7]][1] = CurRxBuf[8] | (CurRxBuf[9]<<8);
  1724. break;
  1725. case OUTPUT_L1_CURRENT:
  1726. Charger.memory.EVSE_Config.data.item.Correction_CL1[CurRxBuf[7]][0] = adc_value.ADC3_IN7_Current_L1.value/10;
  1727. Charger.memory.EVSE_Config.data.item.Correction_CL1[CurRxBuf[7]][1] = CurRxBuf[8] | (CurRxBuf[9]<<8);
  1728. break;
  1729. case OUTPUT_L2_CURRENT:
  1730. Charger.memory.EVSE_Config.data.item.Correction_CL2[CurRxBuf[7]][0] = Charger.Current[0];
  1731. Charger.memory.EVSE_Config.data.item.Correction_CL2[CurRxBuf[7]][1] = CurRxBuf[8] | (CurRxBuf[9]<<8);
  1732. break;
  1733. case OUTPUT_L3_CURRENT:
  1734. Charger.memory.EVSE_Config.data.item.Correction_CL3[CurRxBuf[7]][0] = Charger.Current[0];
  1735. Charger.memory.EVSE_Config.data.item.Correction_CL3[CurRxBuf[7]][1] = CurRxBuf[8] | (CurRxBuf[9]<<8);
  1736. break;
  1737. case LEAK_CURRENT:
  1738. Charger.memory.EVSE_Config.data.item.Correction_Leak[CurRxBuf[7]][0] = adc_value.ADC2_IN6_GF.value; // 2500->25mA
  1739. Charger.memory.EVSE_Config.data.item.Correction_Leak[CurRxBuf[7]][1] = ((CurRxBuf[8] | (CurRxBuf[9]<<8))*100); // mA
  1740. DEBUG_INFO("Charger.memory.EVSE_Config.data.item.Correction_Leak[[0] = %d\r\n",Charger.memory.EVSE_Config.data.item.Correction_Leak[CurRxBuf[7]][0]);
  1741. DEBUG_INFO("Charger.memory.EVSE_Config.data.item.Correction_Leak[[1] = %d\r\n",Charger.memory.EVSE_Config.data.item.Correction_Leak[CurRxBuf[7]][1]);
  1742. break;
  1743. case GROUND_FAULT_VOLTAGE: //max 3.3V
  1744. Charger.memory.EVSE_Config.data.item.Correction_GMI[CurRxBuf[7]][0] = (uint16_t)(adc_value.ADC3_IN4_GMI_VL1.value*10*3.3/4095) ;
  1745. Charger.memory.EVSE_Config.data.item.Correction_GMI[CurRxBuf[7]][1] = CurRxBuf[8] | (CurRxBuf[9]<<8);
  1746. break;
  1747. #ifdef FUNC_SIMU_CHARGE_CURRENT_OUTPUT
  1748. case SIMU_CHARGE_CURRENT_CMD:
  1749. Charger.m_bEnableSimuChargeCurrent =
  1750. (
  1751. (
  1752. CurRxBuf[7] == SIMU_CHARGE_CURRENT_ON &&
  1753. HTK_U16(CurRxBuf[8]) == SIMU_CHARGE_CURRENT_ON_PWD
  1754. ) ? 1 : 0
  1755. );
  1756. XP("#CSU_ParameterSimuCurr: %s\r\n", Charger.m_bEnableSimuChargeCurrent ? "ON" : "OFF");
  1757. break;
  1758. #endif //FUNC_SIMU_CHARGE_CURRENT_OUTPUT
  1759. #ifdef FUNC_SWITCH_RELAY_WELDING_PROTECT
  1760. case SIMU_SWITCH_RELAY_WELDING_CMD:
  1761. Charger.m_bDisableRelayWelding =
  1762. (
  1763. (
  1764. CurRxBuf[7] == SIMU_SWITCH_RELAY_WELDING_DISABLE &&
  1765. HTK_U16(CurRxBuf[8]) == SIMU_SWITCH_RELAY_WELDING_PWD
  1766. ) ? 1 : 0
  1767. );
  1768. XP("#CSU_ParameterDisableRelayWelding: %s\r\n", Charger.m_bDisableRelayWelding ? "ON" : "OFF");
  1769. Charger.m_bDisableCCID = Charger.m_bDisableRelayWelding;
  1770. XP("#CSU_ParameterDisableCCID: %s\r\n", Charger.m_bDisableCCID ? "ON" : "OFF");
  1771. Charger.m_bSetOCP_32A = Charger.m_bDisableRelayWelding;
  1772. XP("#CSU_ParameterSetOCP_32A: %s\r\n", Charger.m_bSetOCP_32A ? "ON" : "OFF");
  1773. break;
  1774. #endif //FUNC_SWITCH_RELAY_WELDING_PROTECT
  1775. }
  1776. #ifdef FUNC_SIMU_CHARGE_CURRENT_OUTPUT
  1777. #ifdef FUNC_SWITCH_RELAY_WELDING_PROTECT
  1778. if (CurRxBuf[6] != SIMU_CHARGE_CURRENT_CMD && CurRxBuf[6] != SIMU_SWITCH_RELAY_WELDING_CMD)
  1779. {
  1780. Charger.memory.EVSE_Config.op_bits.update = ON;
  1781. }
  1782. #else //FUNC_SWITCH_RELAY_WELDING_PROTECT
  1783. if (CurRxBuf[6] != SIMU_CHARGE_CURRENT_CMD)
  1784. {
  1785. Charger.memory.EVSE_Config.op_bits.update = ON;
  1786. }
  1787. #endif //FUNC_SWITCH_RELAY_WELDING_PROTECT
  1788. #else //FUNC_SIMU_CHARGE_CURRENT_OUTPUT
  1789. Charger.memory.EVSE_Config.op_bits.update = ON;
  1790. #endif //FUNC_SIMU_CHARGE_CURRENT_OUTPUT
  1791. break;
  1792. case PROTOCOL_MESSAGE_CONFIG_RELAY_OUTPUT:
  1793. tx_len = 8;
  1794. tx[0] = 0xaa;
  1795. tx[1] = PROTOCOL_ADDR;
  1796. tx[2] = CurRxBuf[1];
  1797. tx[3] = PROTOCOL_MESSAGE_CONFIG_RELAY_OUTPUT;
  1798. tx[4] = 0x01;
  1799. tx[5] = 0x00;
  1800. tx[6] = 0x01;
  1801. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  1802. {
  1803. chksum ^= tx[6 + idx];
  1804. }
  1805. tx[7] = chksum;
  1806. #ifndef USE_OLD_ONE_STEP_CHARGE_FOR_AW_NOODOE
  1807. if (
  1808. #ifdef MODIFY_PROTOCOL_MESSAGE_CONFIG_RELAY_OUTPUT_CHECK_LEGACY_REQUEST
  1809. Charger.am3352.isRequestOn &&
  1810. #endif
  1811. #ifdef MODIFY_PROTOCOL_MESSAGE_CONFIG_RELAY_OUTPUT_CHECK_ALARM_CODE
  1812. Charger.Alarm_Code == 0 &&
  1813. #endif
  1814. #ifdef MODIFY_PROTOCOL_MESSAGE_CONFIG_RELAY_OUTPUT_CHECK_CP_PWM
  1815. Charger.m_CP_CurPWM != PWM_DUTY_FULL &&
  1816. #endif
  1817. Charger.CP_State == SYSTEM_STATE_C &&
  1818. CurRxBuf[9] == 0x07 //Main relay (0111b)
  1819. )
  1820. {
  1821. Charger.Relay_Action = GPIO_RELAY_ACTION_ON;
  1822. #ifdef FUNC_RELAY_OFF_WHEN_GET_TWICE_OFF_CMD
  1823. CsuSetRelayOffCount = 0;
  1824. #endif
  1825. }
  1826. else
  1827. {
  1828. #ifdef FUNC_RELAY_OFF_WHEN_GET_TWICE_OFF_CMD
  1829. if (++CsuSetRelayOffCount >= 2)
  1830. {
  1831. CsuSetRelayOffCount = 0;
  1832. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF ;
  1833. }
  1834. #else //FUNC_RELAY_OFF_WHEN_GET_TWICE_OFF_CMD
  1835. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF ;
  1836. #endif //FUNC_RELAY_OFF_WHEN_GET_TWICE_OFF_CMD
  1837. }
  1838. #endif //USE_OLD_ONE_STEP_CHARGE_FOR_AW_NOODOE
  1839. #ifdef FUNC_CSU_CMD_DEBUG
  1840. XP("#CSU_Relay: %d\r\n", CurRxBuf[9]);
  1841. #endif
  1842. DEBUG_INFO("Set Relay Action By CSU: %d => %d\r\n", CurRxBuf[9], Charger.Relay_Action);
  1843. break;
  1844. case PROTOCOL_MESSAGE_CONFIG_GPIO_OUTPUT:
  1845. tx_len = 8;
  1846. tx[0] = 0xaa;
  1847. tx[1] = PROTOCOL_ADDR;
  1848. tx[2] = CurRxBuf[1];
  1849. tx[3] = PROTOCOL_MESSAGE_CONFIG_GPIO_OUTPUT;
  1850. tx[4] = 0x01;
  1851. tx[5] = 0x00;
  1852. tx[6] = CurRxBuf[6];
  1853. tx[7] = 0x00 ^ CurRxBuf[6];
  1854. break;
  1855. case PROTOCOL_MESSAGE_CONFIG_RTC:
  1856. tx_len = 8;
  1857. tx[0] = 0xaa;
  1858. tx[1] = PROTOCOL_ADDR;
  1859. tx[2] = CurRxBuf[1];
  1860. tx[3] = PROTOCOL_MESSAGE_CONFIG_RTC;
  1861. tx[4] = 0x01;
  1862. tx[5] = 0x00;
  1863. tx[6] = 0x01;
  1864. tx[7] = 0x01;
  1865. setTime.Hours = (CurRxBuf[14]-'0')*10 + (CurRxBuf[15]-'0');
  1866. setTime.Minutes = (CurRxBuf[16]-'0')*10 + (CurRxBuf[17]-'0');
  1867. setTime.Seconds = (CurRxBuf[18]-'0')*10 + (CurRxBuf[19]-'0');
  1868. setTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  1869. setTime.StoreOperation = RTC_STOREOPERATION_RESET;
  1870. if (HAL_RTC_SetTime(&hrtc, &setTime, RTC_FORMAT_BIN) != HAL_OK)
  1871. {
  1872. Error_Handler();
  1873. }
  1874. setDate.Year = (CurRxBuf[6]-'0')*1000 + (CurRxBuf[7]-'0')*100 + (CurRxBuf[8]-'0')*10 + (CurRxBuf[9]-'0') - 2000;
  1875. setDate.Month = (CurRxBuf[10]-'0')*10+ (CurRxBuf[11]-'0');
  1876. setDate.Date = (CurRxBuf[12]-'0')*10 + (CurRxBuf[13]-'0');
  1877. if (HAL_RTC_SetDate(&hrtc, &setDate, RTC_FORMAT_BIN) != HAL_OK)
  1878. {
  1879. Error_Handler();
  1880. }
  1881. break;
  1882. case PROTOCOL_MESSAGE_CONFIG_LED_ACTION_ALARM:
  1883. #ifdef FUNC_DISABLE_LED_CMD_CHECK
  1884. //do nothing
  1885. #else
  1886. if((Charger.am3352.LedActionState != CurRxBuf[6])||(Charger.am3352.LedAlarmState != (CurRxBuf[7]|(CurRxBuf[8]<<8)|(CurRxBuf[9]<<16)|(CurRxBuf[10]<<24))))
  1887. #endif
  1888. {
  1889. Charger.am3352.LedActionState = CurRxBuf[6];
  1890. Charger.am3352.LedAlarmState = (CurRxBuf[7]|(CurRxBuf[8]<<8)|(CurRxBuf[9]<<16)|(CurRxBuf[10]<<24));
  1891. #ifdef FUNC_SHOW_CSU_CONFIG_LED_ACTION_INFO
  1892. XP("#CSU_SetLed (%d, %d)\r\n", Charger.am3352.LedActionState, Charger.am3352.LedAlarmState);
  1893. #endif
  1894. switch(Charger.am3352.LedActionState)
  1895. {
  1896. case LED_ACTION_INIT:
  1897. setLedMotion(LED_ACTION_INIT);
  1898. break;
  1899. case LED_ACTION_IDLE:
  1900. setLedMotion(LED_ACTION_IDLE);
  1901. break;
  1902. case LED_ACTION_AUTHED:
  1903. setLedMotion(LED_ACTION_AUTHED);
  1904. break;
  1905. case LED_ACTION_CONNECTED:
  1906. setLedMotion(LED_ACTION_CONNECTED);
  1907. break;
  1908. case LED_ACTION_CHARGING:
  1909. setLedMotion(LED_ACTION_CHARGING);
  1910. break;
  1911. case LED_ACTION_STOP:
  1912. setLedMotion(LED_ACTION_STOP);
  1913. break;
  1914. case LED_ACTION_ALARM:
  1915. setLedMotion(LED_ACTION_ALARM);
  1916. break;
  1917. case LED_ACTION_MAINTAIN:
  1918. setLedMotion(LED_ACTION_MAINTAIN);
  1919. break;
  1920. case LED_ACTION_RFID_PASS:
  1921. setLedMotion(LED_ACTION_RFID_PASS);
  1922. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  1923. break;
  1924. case LED_ACTION_RFID_FAIL:
  1925. setLedMotion(LED_ACTION_RFID_FAIL);
  1926. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  1927. break;
  1928. case LED_ACTION_BLE_CONNECT:
  1929. setLedMotion(LED_ACTION_BLE_CONNECT);
  1930. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  1931. break;
  1932. case LED_ACTION_BLE_DISABLE:
  1933. setLedMotion(LED_ACTION_BLE_DISABLE);
  1934. break;
  1935. case LED_ACTION_ALL_OFF:
  1936. setLedMotion(LED_ACTION_ALL_OFF);
  1937. break;
  1938. case LED_ACTION_INTERNET_DISCONNECT:
  1939. setLedMotion(LED_ACTION_INTERNET_DISCONNECT);
  1940. break;
  1941. case LED_ACTION_HANDSHAKE_FAIL:
  1942. setLedMotion(LED_ACTION_HANDSHAKE_FAIL);
  1943. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  1944. break;
  1945. case LED_ACTION_RESTORE_SETTING:
  1946. setLedMotion(LED_ACTION_RESTORE_SETTING);
  1947. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  1948. break;
  1949. default:
  1950. break;
  1951. }
  1952. }
  1953. tx_len = 8;
  1954. tx[0] = 0xaa;
  1955. tx[1] = PROTOCOL_ADDR;
  1956. tx[2] = CurRxBuf[1];
  1957. tx[3] = PROTOCOL_MESSAGE_CONFIG_LED_ACTION_ALARM;
  1958. tx[4] = 0x01;
  1959. tx[5] = 0x00;
  1960. tx[6] = 0x01;
  1961. tx[7] = 0x01;
  1962. break;
  1963. case PROTOCOL_MESSAGE_CONFIG_MAX_CURRENT_PWM_DUTY:
  1964. tx_len = 8;
  1965. tx[0] = 0xaa;
  1966. tx[1] = PROTOCOL_ADDR;
  1967. tx[2] = CurRxBuf[1];
  1968. tx[3] = PROTOCOL_MESSAGE_CONFIG_MAX_CURRENT_PWM_DUTY;
  1969. tx[4] = 0x01;
  1970. tx[5] = 0x00;
  1971. #ifdef FUNC_CSU_CMD_DEBUG
  1972. XP("#CSU_SetCpPwmDuty: %d %s\r\n", CurRxBuf[6],
  1973. (Charger.m_bBlockCsuCpPwmDuty || Charger.m_bBlockCsuCpPwmDutyWhenAlarmCpF) ? "(Block)" : "");
  1974. #endif
  1975. if (
  1976. #ifdef FUNC_BLOCK_CSU_CONFIG_CP_PWM_DUTY
  1977. !Charger.m_bBlockCsuCpPwmDuty &&
  1978. #endif
  1979. #ifdef FUNC_BLOCK_CSU_CMD_SET_DUTY_WHEN_ALARM_CP_F_STATE
  1980. !Charger.m_bBlockCsuCpPwmDutyWhenAlarmCpF &&
  1981. #endif
  1982. (
  1983. (CurRxBuf[6] <= 32 && CurRxBuf[6] >= 6) ||
  1984. (CurRxBuf[6] == PWM_DUTY_OFF) ||
  1985. (CurRxBuf[6] == 1) ||
  1986. (CurRxBuf[6] == 5) ||
  1987. (CurRxBuf[6] == 100)
  1988. )
  1989. )
  1990. {
  1991. #ifdef MODIFY_AC_EVSE_STATUS_MAX_CHARGING_CURRENT_VARIABLE
  1992. Charger.AC_MaxChargingCurrentOrDuty = CurRxBuf[6];
  1993. #endif
  1994. tx[6] = 0x00;
  1995. if(CurRxBuf[6] <= 32 && CurRxBuf[6] >= 6)
  1996. {
  1997. Charger.memory.EVSE_Config.data.item.MaxChargingCurrent = CurRxBuf[6];
  1998. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  1999. user_pwm_setvalue(PWM_CH_CP, GetCpPwmDuty(Charger.memory.EVSE_Config.data.item.MaxChargingCurrent));
  2000. #else
  2001. //user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_651(Charger.memory.EVSE_Config.data.item.MaxChargingCurrent));
  2002. #endif
  2003. tx[6] = 0x01;
  2004. }
  2005. else if (CurRxBuf[6] == PWM_DUTY_OFF)
  2006. {
  2007. user_pwm_setvalue(PWM_CH_CP, 0); // State F
  2008. tx[6] = 0x01;
  2009. }
  2010. else if (CurRxBuf[6] == 5)
  2011. {
  2012. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_5);
  2013. tx[6] = 0x01;
  2014. }
  2015. else if (CurRxBuf[6] == 100)
  2016. {
  2017. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  2018. tx[6] = 0x01;
  2019. }
  2020. //control pilot E
  2021. if (CurRxBuf[6] == 1)
  2022. {
  2023. HAL_GPIO_WritePin(OUT_StateE_GPIO_Port, OUT_StateE_Pin, GPIO_PIN_SET); // ON // State E
  2024. user_pwm_setvalue(PWM_CH_CP, 0); //Temporarily, wait for EE to change the hardware and delete
  2025. tx[6] = 0x01;
  2026. }
  2027. else
  2028. {
  2029. HAL_GPIO_WritePin(OUT_StateE_GPIO_Port, OUT_StateE_Pin, GPIO_PIN_RESET); //OFF
  2030. }
  2031. XP("#CSU_SetCpPwmDuty_Final: %d\r\n", CurRxBuf[6]);
  2032. }
  2033. else
  2034. {
  2035. tx[6] = 0x00;
  2036. DEBUG_INFO("CSU_SetCpPwmDuty [NG] (%d) !!!!! \r\n", CurRxBuf[6]);
  2037. }
  2038. for(uint16_t idx=0;idx<(tx[4] | (tx[5]<<8));idx++)
  2039. {
  2040. chksum ^= tx[6 + idx];
  2041. }
  2042. tx[7] = chksum; //checksum
  2043. break;
  2044. case PROTOCOL_MESSAGE_CONFIG_LEGACY_REQUEST:
  2045. #ifdef FUNC_CSU_CMD_DEBUG
  2046. XP("#CSU_SetRequest: %d\r\n", CurRxBuf[6]);
  2047. #endif
  2048. if((CurRxBuf[6]&SETTING_LEGACY_REQUEST)>0)
  2049. {
  2050. #ifdef FUNC_REQUEST_OFF_WHEN_GET_TWICE_OFF_CMD
  2051. CsuSetRequestOffCount = 0;
  2052. #endif
  2053. Charger.am3352.isRequestOff = OFF;
  2054. Charger.rfid.op_bits.reqStart = ON;
  2055. if(!Charger.am3352.isRequestOn)
  2056. {
  2057. DEBUG_INFO("#Request Pin On.\r\n");
  2058. Charger.am3352.isRequestOn = ON;
  2059. }
  2060. }
  2061. else
  2062. {
  2063. #ifdef FUNC_REQUEST_OFF_WHEN_GET_TWICE_OFF_CMD
  2064. if (++CsuSetRequestOffCount >= 2)
  2065. {
  2066. CsuSetRequestOffCount = 0;
  2067. Charger.am3352.isRequestOn = OFF;
  2068. Charger.am3352.RequestPinDebounceCnt = 0;
  2069. Charger.rfid.op_bits.reqStart = OFF;
  2070. if(!Charger.am3352.isRequestOff)
  2071. {
  2072. DEBUG_INFO("#Request Pin Off.\r\n");
  2073. Charger.am3352.isRequestOff = ON;
  2074. }
  2075. }
  2076. #else //FUNC_REQUEST_OFF_WHEN_GET_TWICE_OFF_CMD
  2077. Charger.am3352.isRequestOn = OFF;
  2078. Charger.am3352.RequestPinDebounceCnt = 0;
  2079. Charger.rfid.op_bits.reqStart = OFF;
  2080. if(!Charger.am3352.isRequestOff)
  2081. {
  2082. DEBUG_INFO("#Request Pin Off.\r\n");
  2083. Charger.am3352.isRequestOff = ON;
  2084. }
  2085. #endif //FUNC_REQUEST_OFF_WHEN_GET_TWICE_OFF_CMD
  2086. }
  2087. tx_len = 8;
  2088. tx[0] = 0xaa;
  2089. tx[1] = PROTOCOL_ADDR;
  2090. tx[2] = CurRxBuf[1];
  2091. tx[3] = PROTOCOL_MESSAGE_CONFIG_LEGACY_REQUEST;
  2092. tx[4] = 0x01;
  2093. tx[5] = 0x00;
  2094. tx[6] = 0x01;
  2095. tx[7] = 0x01;
  2096. break;
  2097. case PROTOCOL_MESSAGE_CONFIG_MCU_RESET:
  2098. tx_len = 8;
  2099. tx[0] = 0xaa;
  2100. tx[1] = PROTOCOL_ADDR;
  2101. tx[2] = CurRxBuf[1];
  2102. tx[3] = PROTOCOL_MESSAGE_CONFIG_MCU_RESET;
  2103. tx[4] = 0x01;
  2104. tx[5] = 0x00;
  2105. tx[6] = 0x01;
  2106. tx[7] = 0x01;
  2107. if((CurRxBuf[6]&0x01)>0){
  2108. DEBUG_INFO("MCU reset request.\r\n");
  2109. #ifdef MODIFY_DC_RS485_UPGRADE_ISSUE
  2110. Prefix_UartTX(tx, CurRxBuf);
  2111. HAL_UART_Transmit(pUartHandle, (uint8_t *)tx, tx_len , 0x0ffff);
  2112. Postfix_UartTX(tx, CurRxBuf);
  2113. #else
  2114. HAL_UART_Transmit(&IAP_USART, (uint8_t *)tx, tx_len , 0x0ffff);
  2115. #endif
  2116. osDelay(1000);
  2117. NVIC_SystemReset();
  2118. }
  2119. else
  2120. {
  2121. tx[6] = 0x00;
  2122. tx[7] = 0x00;
  2123. }
  2124. break;
  2125. case PROTOCOL_MESSAGE_CONFIG_BREATHE_LED_TIMING:
  2126. Charger.am3352.BreatheLedConnectFadeIn = (((uint16_t)CurRxBuf[7]<<8)+(uint16_t)CurRxBuf[6]);
  2127. Charger.am3352.BreatheLedConnectFadeOut = (((uint16_t)CurRxBuf[9]<<8)+(uint16_t)CurRxBuf[8]);
  2128. DEBUG_INFO("Connect Breathe Led Fade In time: %d ms.\r\n",Charger.am3352.BreatheLedConnectFadeIn);
  2129. DEBUG_INFO("Connect Breathe Led Fade Out time: %d ms.\r\n",Charger.am3352.BreatheLedConnectFadeOut);
  2130. Charger.am3352.BreatheLedAuthdFadeIn = (((uint16_t)CurRxBuf[11]<<8)+(uint16_t)CurRxBuf[10]);
  2131. Charger.am3352.BreatheLedAuthdFadeOut = (((uint16_t)CurRxBuf[13]<<8)+(uint16_t)CurRxBuf[12]);
  2132. DEBUG_INFO("Authd Breathe Led Fade In time: %d ms.\r\n",Charger.am3352.BreatheLedAuthdFadeIn);
  2133. DEBUG_INFO("Authd Breathe Led Fade Out time: %d ms.\r\n",Charger.am3352.BreatheLedAuthdFadeOut);
  2134. Charger.am3352.BreatheLedChargeFadeIn = (((uint16_t)CurRxBuf[15]<<8)+(uint16_t)CurRxBuf[14]);
  2135. Charger.am3352.BreatheLedChargeFadeOut = (((uint16_t)CurRxBuf[17]<<8)+(uint16_t)CurRxBuf[16]);
  2136. DEBUG_INFO("Charge Breathe Led Fade In time: %d ms.\r\n",Charger.am3352.BreatheLedChargeFadeIn);
  2137. DEBUG_INFO("Charge Breathe Led Fade Out time: %d ms.\r\n",Charger.am3352.BreatheLedChargeFadeOut);
  2138. tx_len = 8;
  2139. tx[0] = 0xaa;
  2140. tx[1] = PROTOCOL_ADDR;
  2141. tx[2] = CurRxBuf[1];
  2142. tx[3] = PROTOCOL_MESSAGE_CONFIG_BREATHE_LED_TIMING;
  2143. tx[4] = 0x01;
  2144. tx[5] = 0x00;
  2145. tx[6] = 0x01;
  2146. tx[7] = 0x01;
  2147. break;
  2148. case PROTOCOL_MESSAGE_CONFIG_LED_BRIGHTNESS:
  2149. memcpy(Charger.Led_Brightness, &CurRxBuf[6], 12);
  2150. #ifdef FUNC_SHOW_CSU_CONFIG_LED_BRIGHTNESS
  2151. XP("#CSU_SetLedBrightness(%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d)\r\n",
  2152. Charger.Led_Brightness[0],
  2153. Charger.Led_Brightness[1],
  2154. Charger.Led_Brightness[2],
  2155. Charger.Led_Brightness[3],
  2156. Charger.Led_Brightness[4],
  2157. Charger.Led_Brightness[5],
  2158. Charger.Led_Brightness[6],
  2159. Charger.Led_Brightness[7],
  2160. Charger.Led_Brightness[8],
  2161. Charger.Led_Brightness[9],
  2162. Charger.Led_Brightness[10],
  2163. Charger.Led_Brightness[11]);
  2164. #endif
  2165. tx_len = 8;
  2166. tx[0] = 0xaa;
  2167. tx[1] = PROTOCOL_ADDR;
  2168. tx[2] = CurRxBuf[1];
  2169. tx[3] = PROTOCOL_MESSAGE_CONFIG_LED_BRIGHTNESS;
  2170. tx[4] = 0x01;
  2171. tx[5] = 0x00;
  2172. tx[6] = 0x01;
  2173. tx[7] = 0x01;
  2174. break;
  2175. case PROTOCOL_MESSAGE_CONFIG_EVSE_AUTH_MODE:
  2176. switch(CurRxBuf[6])
  2177. {
  2178. case AUTH_MODE_OCPP:
  2179. Charger.memory.EVSE_Config.data.item.AuthMode = AUTH_MODE_OCPP ;
  2180. break;
  2181. case AUTH_MODE_FREE:
  2182. Charger.memory.EVSE_Config.data.item.AuthMode = AUTH_MODE_FREE ;
  2183. break;
  2184. default:
  2185. Charger.memory.EVSE_Config.data.item.AuthMode = AUTH_MODE_FREE ;
  2186. Charger.memory.EVSE_Config.data.item.OfflinePolicy = RFID_USER_AUTH_FREE ;
  2187. break ;
  2188. }
  2189. switch(CurRxBuf[7])
  2190. {
  2191. case RFID_USER_AUTH_LOCAL_LIST:
  2192. Charger.memory.EVSE_Config.data.item.OfflinePolicy = RFID_USER_AUTH_LOCAL_LIST ;
  2193. break;
  2194. case RFID_USER_AUTH_PH:
  2195. Charger.memory.EVSE_Config.data.item.OfflinePolicy = RFID_USER_AUTH_PH ;
  2196. break;
  2197. case RFID_USER_AUTH_FREE:
  2198. Charger.memory.EVSE_Config.data.item.OfflinePolicy = RFID_USER_AUTH_FREE ;
  2199. break;
  2200. case RFID_USER_AUTH_NO_CHARGING:
  2201. Charger.memory.EVSE_Config.data.item.OfflinePolicy = RFID_USER_AUTH_NO_CHARGING ;
  2202. break;
  2203. default:
  2204. Charger.memory.EVSE_Config.data.item.AuthMode = AUTH_MODE_FREE ;
  2205. Charger.memory.EVSE_Config.data.item.OfflinePolicy = RFID_USER_AUTH_FREE ;
  2206. break ;
  2207. }
  2208. tx_len = 8;
  2209. tx[0] = 0xaa;
  2210. tx[1] = PROTOCOL_ADDR;
  2211. tx[2] = CurRxBuf[1];
  2212. tx[3] = PROTOCOL_MESSAGE_CONFIG_EVSE_AUTH_MODE;
  2213. tx[4] = 0x01;
  2214. tx[5] = 0x00;
  2215. tx[6] = 0x01;
  2216. tx[7] = 0x01;
  2217. Charger.memory.EVSE_Config.op_bits.update = ON;
  2218. break;
  2219. case PROTOCOL_MESSAGE_CONFIG_ERASE_MEMORY:
  2220. switch(CurRxBuf[6])
  2221. {
  2222. case 0:
  2223. Charger.memory.EVSE_Config.op_bits.clear = ON;
  2224. Charger.memory.whiteList.op_bits.clear = ON;
  2225. Charger.memory.hisCharging.op_bits.clear = ON;
  2226. Charger.memory.hisAlarm.op_bits.clear = ON;
  2227. Charger.memory.coldLoadPickUp.op_bits.clear = ON;
  2228. break;
  2229. case 1:
  2230. Charger.memory.EVSE_Config.op_bits.clear = ON;
  2231. break;
  2232. case 2:
  2233. Charger.memory.whiteList.op_bits.clear = ON;
  2234. break;
  2235. case 3:
  2236. Charger.memory.hisCharging.op_bits.clear = ON;
  2237. break;
  2238. case 4:
  2239. Charger.memory.hisAlarm.op_bits.clear = ON;
  2240. break;
  2241. case 5:
  2242. Charger.memory.coldLoadPickUp.op_bits.clear = ON;
  2243. break;
  2244. default:
  2245. break;
  2246. }
  2247. tx_len = 8;
  2248. tx[0] = 0xaa;
  2249. tx[1] = PROTOCOL_ADDR;
  2250. tx[2] = CurRxBuf[1];
  2251. tx[3] = PROTOCOL_MESSAGE_CONFIG_ERASE_MEMORY;
  2252. tx[4] = 0x01;
  2253. tx[5] = 0x00;
  2254. tx[6] = 0x01;
  2255. tx[7] = 0x01;
  2256. break;
  2257. #ifdef FUNC_RESET_CONNECTOR_PLUGIN_TIMES
  2258. case PROTOCOL_MESSAGE_CONFIG_RESET_CONNECTOR_PLUGIN_TIMES:
  2259. tx_len = 8;
  2260. tx[0] = 0xaa;
  2261. tx[1] = PROTOCOL_ADDR;
  2262. tx[2] = CurRxBuf[1];
  2263. tx[3] = PROTOCOL_MESSAGE_CONFIG_RESET_CONNECTOR_PLUGIN_TIMES;
  2264. tx[4] = 0x01;
  2265. tx[5] = 0x00;
  2266. tx[6] = 0x01;
  2267. tx[7] = 0x01;
  2268. if(CurRxBuf[6] == 0x01)
  2269. {
  2270. DEBUG_INFO("CSU reset connector plugin times.\r\n");
  2271. Charger.memory.EVSE_Config.data.item.AcPlugInTimes = 0;
  2272. Charger.memory.EVSE_Config.op_bits.update = ON;
  2273. }
  2274. else
  2275. {
  2276. tx[6] = 0x00;
  2277. tx[7] = 0x00;
  2278. }
  2279. break;
  2280. #endif //FUNC_RESET_CONNECTOR_PLUGIN_TIMES
  2281. /*
  2282. Firmware update message
  2283. */
  2284. case PROTOCOL_MESSAGE_UPGRADE_START:
  2285. tx_len = 8;
  2286. tx[0] = 0xaa;
  2287. tx[1] = PROTOCOL_ADDR;
  2288. tx[2] = CurRxBuf[1];
  2289. tx[3] = PROTOCOL_MESSAGE_UPGRADE_START;
  2290. tx[4] = 0x01;
  2291. tx[5] = 0x00;
  2292. binCRCTarget = (CurRxBuf[6] << 24) | (CurRxBuf[6] << 16) | (CurRxBuf[6] << 8) | (CurRxBuf[6] << 0);
  2293. flashdestination = NEW_CODE_ADDRESS;
  2294. if(FLASH_If_Erase(NEW_CODE_ADDRESS, 3)== FLASHIF_OK)
  2295. {
  2296. #if defined(DEBUG) || defined(RTOS_STAT)
  2297. DEBUG_INFO("Firmware transfer start, earase flash success....\n\r");
  2298. #endif
  2299. tx[6] = 0x01;
  2300. tx[7] = 0x01;
  2301. if (Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU)
  2302. {
  2303. setChargerMode(MODE_MAINTAIN);
  2304. timerEnable(TIMER_IDX_UPDATE, 10000);
  2305. }
  2306. }
  2307. else
  2308. {
  2309. #if defined(DEBUG) || defined(RTOS_STAT)
  2310. DEBUG_INFO("Firmware transfer start, earase flash fail....\n\r");
  2311. #endif
  2312. tx[6] = 0x00;
  2313. tx[7] = 0x00;
  2314. }
  2315. break;
  2316. case PROTOCOL_MESSAGE_UPGRADE_TRANS:
  2317. tx_len = 8;
  2318. tx[0] = 0xaa;
  2319. tx[1] = PROTOCOL_ADDR;
  2320. tx[2] = CurRxBuf[1];
  2321. tx[3] = PROTOCOL_MESSAGE_UPGRADE_TRANS;
  2322. tx[4] = 0x01;
  2323. tx[5] = 0x00;
  2324. flashdestination = NEW_CODE_ADDRESS + ((CurRxBuf[6]<<0) | (CurRxBuf[7]<<8) | (CurRxBuf[8]<<16) |(CurRxBuf[9]<<24));
  2325. if (FLASH_If_Write(flashdestination, (uint32_t*) &CurRxBuf[10], ((((CurRxBuf[4]) | (CurRxBuf[5])<<8)-4)>>2)) == FLASHIF_OK)
  2326. {
  2327. #if defined(DEBUG) || defined(RTOS_STAT)
  2328. //DEBUG_INFO("Firmware transfer start address: 0x%x, length: %d...Pass\n\r", flashdestination, (((CurRxBuf[4]) | (CurRxBuf[5])<<8)-4));
  2329. #endif
  2330. tx[6] = 0x01;
  2331. tx[7] = 0x01;
  2332. if (Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU)
  2333. {
  2334. setLedMotion(LED_ACTION_MAINTAIN);
  2335. timerRefresh(TIMER_IDX_UPDATE);
  2336. }
  2337. }
  2338. else
  2339. {
  2340. #if defined(DEBUG) || defined(RTOS_STAT)
  2341. DEBUG_INFO("Firmware transfer start address: 0x%x, length: %d...Fail\n\r", flashdestination, (((CurRxBuf[4]) | (CurRxBuf[5])<<8)-4));
  2342. #endif
  2343. tx[6] = 0x00;
  2344. tx[7] = 0x00;
  2345. }
  2346. break;
  2347. case PROTOCOL_MESSAGE_UPGRADE_STOP:
  2348. tx_len = 8;
  2349. tx[0] = 0xaa;
  2350. tx[1] = PROTOCOL_ADDR;
  2351. tx[2] = CurRxBuf[1];
  2352. tx[3] = PROTOCOL_MESSAGE_UPGRADE_STOP;
  2353. tx[4] = 0x01;
  2354. tx[5] = 0x00;
  2355. flash = NEW_CODE_ADDRESS;
  2356. checksum = HAL_CRC_Calculate(&hcrc, (uint32_t *)flash, ((FLASH_AP_LENGTH-4)>>2));
  2357. flash = ((uint32_t)(NEW_CODE_ADDRESS+FLASH_AP_LENGTH-4));
  2358. #if defined(DEBUG) || defined(RTOS_STAT)
  2359. DEBUG_INFO("Firmware transfer end, AP CRC checksum, flash: 0x%x, 0x%x\r\n", checksum, *((uint32_t *)flash) );
  2360. #endif
  2361. if(checksum == *((uint32_t *)flash))
  2362. {
  2363. if (FLASH_If_Write(UPGRADE_REQ_ADDRESS, (uint32_t *)&endFlag[0], 1) == FLASHIF_OK)
  2364. {
  2365. #if defined(DEBUG) || defined(RTOS_STAT)
  2366. DEBUG_INFO("Firmware Confirm Tag write ok..\n\r");
  2367. #endif
  2368. tx[6] = 0x01;
  2369. tx[7] = 0x01;
  2370. if(isUSB_CDC_IAP)
  2371. {
  2372. CDC_Transmit_FS((uint8_t *)tx, tx_len);
  2373. }
  2374. else
  2375. {
  2376. #ifdef MODIFY_DC_RS485_UPGRADE_ISSUE
  2377. Prefix_UartTX(tx, CurRxBuf);
  2378. HAL_UART_Transmit(pUartHandle, (uint8_t *)tx, tx_len , 0x0ffff);
  2379. Postfix_UartTX(tx, CurRxBuf);
  2380. #else
  2381. HAL_UART_Transmit(&IAP_USART, (uint8_t *)tx, tx_len , 0x0ffff);
  2382. #endif
  2383. }
  2384. osDelay(1000);
  2385. NVIC_SystemReset();
  2386. }
  2387. else
  2388. {
  2389. #if defined(DEBUG) || defined(RTOS_STAT)
  2390. DEBUG_INFO("Firmware Confirm Tag write fail...\n\r");
  2391. #endif
  2392. tx[6] = 0x00;
  2393. tx[7] = 0x00;
  2394. }
  2395. }
  2396. else
  2397. {
  2398. #if defined(DEBUG) || defined(RTOS_STAT)
  2399. DEBUG_INFO("Firmware checksum compare fail...\n\r");
  2400. #endif
  2401. tx[6] = 0x00;
  2402. tx[7] = 0x00;
  2403. }
  2404. if (Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU)
  2405. {
  2406. timerDisable(TIMER_IDX_UPDATE);
  2407. }
  2408. break;
  2409. case PROTOCOL_MESSAGE_UPGRADE_ABOARD:
  2410. #if defined(DEBUG) || defined(RTOS_STAT)
  2411. DEBUG_INFO("Firmware update transfer aboard...\n\r");
  2412. #endif
  2413. tx_len = 8;
  2414. tx[0] = 0xaa;
  2415. tx[1] = PROTOCOL_ADDR;
  2416. tx[2] = CurRxBuf[1];
  2417. tx[3] = PROTOCOL_MESSAGE_UPGRADE_ABOARD;
  2418. tx[4] = 0x01;
  2419. tx[5] = 0x00;
  2420. tx[6] = 0x01;
  2421. tx[7] = 0x01;
  2422. break;
  2423. default:
  2424. /* Todo: bin file receive aboard */
  2425. #if defined(DEBUG) || defined(RTOS_STAT)
  2426. #ifdef FUNC_RS485_SLAVE
  2427. DEBUG_INFO("Protocol message unknow (0x%02X)\n\r", CurRxBuf[3]);
  2428. #else
  2429. DEBUG_INFO("Protocol message unknow...\n\r");
  2430. #endif
  2431. #endif
  2432. tx_len = 8;
  2433. #ifdef FIX_RS485_UNKNOW_MSG
  2434. tx[0] = 0x55;
  2435. #else
  2436. tx[0] = 0xaa;
  2437. #endif
  2438. tx[1] = PROTOCOL_ADDR;
  2439. tx[2] = CurRxBuf[1];
  2440. tx[3] = CurRxBuf[3];
  2441. tx[4] = 0x01;
  2442. tx[5] = 0x00;
  2443. tx[6] = 0x00;
  2444. tx[7] = 0x00;
  2445. break;
  2446. }
  2447. }
  2448. else
  2449. {
  2450. #ifndef FUNC_RS485_SLAVE
  2451. #if defined(DEBUG) || defined(RTOS_STAT)
  2452. DEBUG_INFO("Protocol check sum is wrong...\n\r");
  2453. #endif
  2454. tx_len = 8;
  2455. #ifdef FIX_RS485_UNKNOW_MSG
  2456. tx[0] = 0x55;
  2457. #else
  2458. tx[0] = 0xaa;
  2459. #endif
  2460. tx[1] = PROTOCOL_ADDR;
  2461. tx[2] = CurRxBuf[1];
  2462. tx[3] = CurRxBuf[3];
  2463. tx[4] = 0x01;
  2464. tx[5] = 0x00;
  2465. tx[6] = 0x00;
  2466. tx[7] = 0x00;
  2467. #endif //FUNC_RS485_SLAVE
  2468. }
  2469. if(isUSB_CDC_IAP)
  2470. {
  2471. CDC_Transmit_FS((uint8_t *)tx, tx_len);
  2472. }
  2473. else
  2474. {
  2475. #ifdef FUNC_RS485_SLAVE
  2476. #ifdef MODIFY_DC_RS485_UPGRADE_ISSUE
  2477. if (CurRxBuf != NULL)
  2478. {
  2479. #ifdef FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  2480. if (CurRxBuf == UART_IAP_rx_buffer)
  2481. {
  2482. Prefix_UartTX(tx, CurRxBuf);
  2483. HAL_UART_Transmit(pUartHandle, (uint8_t *)tx, tx_len , 0x0ffff);
  2484. Postfix_UartTX(tx, CurRxBuf);
  2485. }
  2486. else if (CurRxBuf == UART_RS485_rx_buffer)
  2487. {
  2488. #ifdef FIX_RS485_UNKNOW_MSG
  2489. if (CurRxBuf[IMCP_FM_DSTADDR_IDX] == Charger.m_EMPX10_RS485_ClientAddr && tx[0] != 0x55)
  2490. #else
  2491. if (CurRxBuf[IMCP_FM_DSTADDR_IDX] == Charger.m_EMPX10_RS485_ClientAddr)
  2492. #endif
  2493. {
  2494. Prefix_UartTX(tx, CurRxBuf);
  2495. HAL_UART_Transmit(pUartHandle, (uint8_t *)tx, tx_len , 0x0ffff);
  2496. Postfix_UartTX(tx, CurRxBuf);
  2497. }
  2498. // else if (CurRxBuf[IMCP_FM_DSTADDR_IDX] == EMPX10_RS485_ADDR_BROADCAST)
  2499. // {
  2500. // Prefix_UartTX(tx, CurRxBuf);
  2501. // HAL_UART_Transmit(pUartHandle, (uint8_t *)tx, tx_len , 0x0ffff);
  2502. // Postfix_UartTX(tx, CurRxBuf);
  2503. // }
  2504. }
  2505. #else //FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  2506. Prefix_UartTX(tx, CurRxBuf);
  2507. HAL_UART_Transmit(pUartHandle, (uint8_t *)tx, tx_len , 0x0ffff);
  2508. Postfix_UartTX(tx, CurRxBuf);
  2509. #endif //FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  2510. }
  2511. #else //MODIFY_DC_RS485_UPGRADE_ISSUE
  2512. if (CurRxBuf != NULL)
  2513. {
  2514. tx[1] = Charger.m_RS485SlaveAddr; //Update Slave Address
  2515. HAL_UART_Transmit(pUartHandle, (uint8_t *)tx, tx_len, 0xffff);
  2516. }
  2517. #endif //MODIFY_DC_RS485_UPGRADE_ISSUE
  2518. #else //FUNC_RS485_SLAVE
  2519. HAL_UART_Transmit(&IAP_USART, (uint8_t *)tx, tx_len, 0xffff);
  2520. #endif //FUNC_RS485_SLAVE
  2521. }
  2522. #ifdef DEBUG_EMPX10_COMM
  2523. if (CurRxBuf == UART_RS485_rx_buffer)
  2524. {
  2525. HTK_ByteArray2HexStr_XP(NULL, tx, 0, tx_len);
  2526. }
  2527. #endif //DEBUG_EMPX10_COMM
  2528. #ifdef ENABLE_PRINT_IMCP_MSG
  2529. #ifdef FUNC_RS485_SLAVE
  2530. #ifdef PRINT_IMCP_MSG_ONLY_ON_UPDATEPORT
  2531. if (CurRxBuf == UART_IAP_rx_buffer && CheckPrintImcpMsg(CurRxBuf[3]))
  2532. {
  2533. HTK_ByteArray2HexStr_XP(NULL, tx, 0, tx_len);
  2534. }
  2535. #else
  2536. if (CheckPrintImcpMsg(CurRxBuf[3]))
  2537. {
  2538. HTK_ByteArray2HexStr_XP(NULL, tx, 0, tx_len);
  2539. }
  2540. #endif //PRINT_IMCP_MSG_ONLY_ON_UPDATEPORT
  2541. #else //FUNC_RS485_SLAVE
  2542. if (CheckPrintImcpMsg(UART_IAP_rx_buffer[3]))
  2543. {
  2544. HTK_ByteArray2HexStr_XP(NULL, tx, 0, tx_len);
  2545. }
  2546. #endif //FUNC_RS485_SLAVE
  2547. #endif //ENABLE_PRINT_IMCP_MSG
  2548. #ifdef FUNC_RS485_SLAVE
  2549. // Clear rx_buffer and related varaible
  2550. for(uint16_t i = 0; i < *pCurRxBuf_RxLen ; i++)
  2551. CurRxBuf[i]=0;
  2552. *pCurRxBuf_RxLen = 0;
  2553. *pCurRecvEndFlag = 0;
  2554. isUSB_CDC_IAP = OFF;
  2555. #else
  2556. // Clear rx_buffer and related varaible
  2557. for(uint16_t i = 0; i < UART_IAP_rx_len ; i++)
  2558. UART_IAP_rx_buffer[i]=0;
  2559. UART_IAP_rx_len=0;
  2560. UART_IAP_recv_end_flag=0;
  2561. isUSB_CDC_IAP = OFF;
  2562. #endif //FUNC_RS485_SLAVE
  2563. }
  2564. if(timer[TIMER_IDX_UPDATE].isAlarm == ON && Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU)
  2565. {
  2566. setChargerMode(MODE_IDLE);
  2567. setLedMotion(LED_ACTION_IDLE);
  2568. timerDisable(TIMER_IDX_UPDATE);
  2569. }
  2570. #ifdef FUNC_RS485_SLAVE
  2571. HAL_UART_Receive_DMA(&IAP_USART,(uint8_t*)UART_IAP_rx_buffer, UART_BUFFER_SIZE);
  2572. HAL_UART_Receive_DMA(&RS485_USART,(uint8_t*)UART_RS485_rx_buffer, UART_BUFFER_SIZE);
  2573. #else
  2574. HAL_UART_Receive_DMA(&IAP_USART,(uint8_t*)UART_IAP_rx_buffer, UART_BUFFER_SIZE);
  2575. #endif //FUNC_RS485_SLAVE
  2576. osDelay(1);
  2577. }
  2578. /* USER CODE END StartUartTask */
  2579. }
  2580. /* USER CODE BEGIN Header_StartAdcTask */
  2581. /**
  2582. * @brief Function implementing the adcTask thread.
  2583. * @param argument: Not used
  2584. * @retval None
  2585. */
  2586. /* USER CODE END Header_StartAdcTask */
  2587. #ifdef FUNC_DEBUG_MSG_CP_STATE
  2588. const char *CP_STATE_TAG[7] = {"0:UNKNOW", "1:A(12V)", "2:B(9V)", "3:C(6V)", "4:D(3V)", "5:E(0V)", "6:F(-12V)"};
  2589. #endif
  2590. void StartAdcTask(void const * argument)
  2591. {
  2592. /* USER CODE BEGIN StartAdcTask */
  2593. //aadc
  2594. uint8_t previous_cp_state = SYSTEM_STATE_A;
  2595. #ifdef MODIFY_POWER_CONSUME_ACCU_TIMER
  2596. uint16_t updateMemReq = 0;
  2597. #else
  2598. uint8_t updateMemReq = 0;
  2599. #endif
  2600. struct TimeTick
  2601. {
  2602. uint32_t timeTick_Start;
  2603. uint32_t timeTick_End;
  2604. uint32_t timeTick_Delta;
  2605. }timeTick;
  2606. timeTick.timeTick_Start = HAL_GetTick();
  2607. #ifdef FUNC_CP_ADC_MODIFY
  2608. #ifdef NEW_CP_SPEC
  2609. while (!Charger.m_bCpDetectModuleInitOK)
  2610. {
  2611. osDelay(10);
  2612. }
  2613. #endif //NEW_CP_SPEC
  2614. #endif //FUNC_CP_ADC_MODIFY
  2615. #ifdef MODIFY_POWER_CONSUME_ACCU_TIMER
  2616. timerEnable(TIMER_IDX_POWER, POWER_CONSUME_ACCU_TIMEOUT_SPEC);
  2617. #else
  2618. timerEnable(TIMER_IDX_POWER, 1000);
  2619. #endif
  2620. /* Infinite loop */
  2621. for(;;)
  2622. {
  2623. Relay_Control_Function( ) ;
  2624. // ADC chanel 1
  2625. if(isDMAEnd_ADC1)
  2626. {
  2627. isDMAEnd_ADC1 = OFF;
  2628. CpCalculate(ADC1_Buffer, ADC1_CHANEL_COUNT*ADC1_SAMPLE_COUNT, &CpDetectionResult);
  2629. #ifdef FUNC_EMPX10_CUSTOMIZE_CP_STATE_OPERATION
  2630. if (Charger.am3352.isRequestOn == ON)
  2631. {
  2632. if (Charger.Alarm_Code == 0)
  2633. {
  2634. if (Charger.m_CP_CurPWM == PWM_DUTY_FULL)
  2635. {
  2636. Charger.CP_State = SYSTEM_STATE_B;
  2637. }
  2638. else
  2639. {
  2640. Charger.CP_State = SYSTEM_STATE_C;
  2641. }
  2642. }
  2643. else
  2644. {
  2645. Charger.CP_State = SYSTEM_STATE_B;
  2646. }
  2647. }
  2648. else
  2649. {
  2650. Charger.CP_State = SYSTEM_STATE_A;
  2651. }
  2652. #else
  2653. Charger.CP_State = CpDetectionResult.State;
  2654. #endif
  2655. #ifdef FUNC_RELAY_OFF_WHEN_CP_NOT_STATE_C
  2656. if (Charger.CP_State != SYSTEM_STATE_C)
  2657. {
  2658. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  2659. }
  2660. #endif
  2661. #ifdef FUNC_DEBUG_MSG_CP_STATE
  2662. if (previous_cp_state != Charger.CP_State)
  2663. {
  2664. XP("#CP: [%s] >>> [%s] (%+f, %+f)\r\n",
  2665. CP_STATE_TAG[previous_cp_state],
  2666. CP_STATE_TAG[Charger.CP_State],
  2667. (CpDetectionResult.PositiveValue == 0 ? 0 : CP_GET_OPA_VIN_USE_ADC(CpDetectionResult.PositiveValue)),
  2668. (CpDetectionResult.NegativeValue == 0 ? 0 : CP_GET_OPA_VIN_USE_ADC(CpDetectionResult.NegativeValue))
  2669. );
  2670. }
  2671. #endif
  2672. if (Charger.Mode!= MODE_DEBUG)
  2673. {
  2674. #ifdef MODIFY_ADCTASK_CP_STATE_CHANGE
  2675. if((previous_cp_state != Charger.CP_State))
  2676. #else
  2677. if((previous_cp_state != Charger.CP_State) && (Charger.CP_State !=SYSTEM_STATE_UNKNOWN ))
  2678. #endif
  2679. {
  2680. previous_cp_state = Charger.CP_State;
  2681. if(Charger.CP_State == SYSTEM_STATE_A)
  2682. {
  2683. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL); // CP PWM duty set 100%
  2684. Charger.Relay_Action=GPIO_RELAY_ACTION_OFF;
  2685. Charger.memory.EVSE_Config.data.item.AcPlugInTimes++;
  2686. Charger.memory.EVSE_Config.op_bits.update = ON;
  2687. DEBUG_INFO("EVSE connector plug counts: %d\r\n", Charger.memory.EVSE_Config.data.item.AcPlugInTimes);
  2688. }
  2689. }
  2690. }
  2691. //DEBUG_INFO("HAL_GetTick() = %d\r\n",HAL_GetTick());
  2692. if(HAL_ADC_Start_DMA(&hadc1, (uint32_t*)&ADC1_Buffer, ADC1_CHANEL_COUNT*ADC1_SAMPLE_COUNT)!=HAL_OK)Error_Handler();
  2693. }
  2694. // ADC chanel 2
  2695. if(isDMAEnd_ADC2)
  2696. {
  2697. isDMAEnd_ADC2 = OFF;
  2698. for(uint16_t idx=0;idx<ADC2_SAMPLE_COUNT;idx++)
  2699. {
  2700. ADC2_Buffer_Each[0][idx] = ADC2_Buffer[(idx*ADC2_CHANEL_COUNT) + 0];
  2701. ADC2_Buffer_Each[1][idx] = ADC2_Buffer[(idx*ADC2_CHANEL_COUNT) + 1];
  2702. }
  2703. if (WatchDogLeakRawDataCheck_Event()) //clear leak WatchDogcount
  2704. {
  2705. Charger.counter.WatchDogLeak = 0 ;
  2706. }
  2707. //RlyVmax_cal
  2708. //filter_move_avg(&adc_value.ADC2_IN5_Welding, RlyVmax_cal(ADC2_Buffer_Each[0], ADC2_SAMPLE_COUNT));
  2709. if (Charger.GroundingSystem == GROUNGING_SYSTEM_LL)
  2710. {
  2711. filter_move_avg(&adc_value.ADC2_IN5_Welding, avg_v_cal(ADC2_Buffer_Each[0], ADC2_SAMPLE_COUNT)); //unit: 0.01V
  2712. }
  2713. else
  2714. {
  2715. filter_move_avg(&adc_value.ADC2_IN5_Welding, RlyVmax_cal(ADC2_Buffer_Each[0], ADC2_SAMPLE_COUNT)); //unit: 0.01V
  2716. }
  2717. //Leak
  2718. filter_move_avg(&adc_value.ADC2_IN6_GF, GFRms_cal(ADC2_Buffer_Each[1], ADC2_SAMPLE_COUNT));
  2719. if ((((adc_value.ADC2_IN6_GF.value)*Charger.coefficient.gain_leak[0])+Charger.coefficient.offset_leak[0]) <=0)
  2720. Charger.Leak_Current = adc_value.ADC2_IN6_GF.value;
  2721. else
  2722. Charger.Leak_Current = (uint16_t) (((adc_value.ADC2_IN6_GF.value)*Charger.coefficient.gain_leak[0])+Charger.coefficient.offset_leak[0]) ;
  2723. if(HAL_ADC_Start_DMA(&hadc2, (uint32_t*)&ADC2_Buffer, ADC2_CHANEL_COUNT*ADC2_SAMPLE_COUNT)!=HAL_OK)Error_Handler();
  2724. }
  2725. // ADC chanel 3
  2726. if(isDMAEnd_ADC3)
  2727. {
  2728. isDMAEnd_ADC3 = OFF;
  2729. for(uint16_t idx=0;idx<ADC3_SAMPLE_COUNT;idx++)
  2730. {
  2731. ADC3_Buffer_Each[0][idx] = ADC3_Buffer[(idx*ADC3_CHANEL_COUNT) + 0];
  2732. ADC3_Buffer_Each[1][idx] = ADC3_Buffer[(idx*ADC3_CHANEL_COUNT) + 1];
  2733. ADC3_Buffer_Each[2][idx] = ADC3_Buffer[(idx*ADC3_CHANEL_COUNT) + 2];
  2734. ADC3_Buffer_Each[3][idx] = ADC3_Buffer[(idx*ADC3_CHANEL_COUNT) + 3];
  2735. ADC3_Buffer_Each[4][idx] = ADC3_Buffer[(idx*ADC3_CHANEL_COUNT) + 4];
  2736. }
  2737. //VT
  2738. filter_move_avg(&adc_value.ADC3_IN9_Voltage_L1, vRms_cal2(ADC3_Buffer_Each[0], ADC3_SAMPLE_COUNT));
  2739. //SetADCWDGBoundary();
  2740. //MCU VDD
  2741. filter_move_avg(&adc_value.ADC3_IN14_UL_1998, avg_cal(ADC3_Buffer_Each[1], ADC3_SAMPLE_COUNT));
  2742. //TEMP
  2743. filter_move_avg(&adc_value.ADC3_IN15_Temp, avg_cal(ADC3_Buffer_Each[2], ADC3_SAMPLE_COUNT));
  2744. Charger.temperature.SystemAmbientTemp = getTemperature(adc_value.ADC3_IN15_Temp.value);
  2745. //PE
  2746. filter_move_avg(&adc_value.ADC3_IN4_GMI_VL1, avg_cal(ADC3_Buffer_Each[3], ADC3_SAMPLE_COUNT));
  2747. //CT
  2748. Current_filter_move_avg(&adc_value.ADC3_IN7_Current_L1, cRms_cal(ADC3_Buffer_Each[4], ADC3_SAMPLE_COUNT));
  2749. //Correction
  2750. //VT
  2751. Charger.Voltage[0] = (uint16_t) (((adc_value.ADC3_IN9_Voltage_L1.value)*Charger.coefficient.gain_v[0])+Charger.coefficient.offset_v[0]) ;
  2752. //CT
  2753. #ifdef FUNC_SIMU_CHARGE_CURRENT_OUTPUT
  2754. if (Charger.m_bEnableSimuChargeCurrent)
  2755. {
  2756. if (Charger.Relay_Action == GPIO_RELAY_ACTION_ON)
  2757. {
  2758. Charger.Current[0] = Charger.memory.EVSE_Config.data.item.MaxChargingCurrent * 100 - (rand() % 30);
  2759. }
  2760. else
  2761. {
  2762. Charger.Current[0] = 0;
  2763. }
  2764. }
  2765. else
  2766. {
  2767. Charger.Current[0] = (uint16_t)((adc_value.ADC3_IN7_Current_L1.value*Charger.coefficient.gain_c[0])+Charger.coefficient.offset_c[0]);
  2768. }
  2769. #else //FUNC_SIMU_CHARGE_CURRENT_OUTPUT
  2770. Charger.Current[0] = (uint16_t)((adc_value.ADC3_IN7_Current_L1.value*Charger.coefficient.gain_c[0])+Charger.coefficient.offset_c[0]);
  2771. #endif //FUNC_SIMU_CHARGE_CURRENT_OUTPUT
  2772. //GMI
  2773. Charger.Gmi_Voltage = (uint16_t) (((adc_value.ADC3_IN4_GMI_VL1.value)*Charger.coefficient.gain_gmi[0])+Charger.coefficient.offset_gmi[0]) ;
  2774. #ifdef COLD_LOAD_PICKUP
  2775. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  2776. // check Power drop is not alarm
  2777. // Power drop detection
  2778. if((vRms_cal2(ADC3_Buffer_Each[0], ADC3_SAMPLE_COUNT) <= ALARM_SPEC_POWER_DROP) && (!Charger.AC_DripisTure) && (Charger.Mode != MODE_INITIAL))
  2779. {
  2780. if(Charger.counter.AC_Drip.fail > 1000)
  2781. {
  2782. //Charger.memory.EVSE_Config.data.item.isDispDebug = OFF;
  2783. if((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))
  2784. {
  2785. Charger.memory.coldLoadPickUp.data.item.isEnable = ON;
  2786. Charger.memory.coldLoadPickUp.data.item.DateTime.year = Charger.cycle_info.startDateTime.year;
  2787. Charger.memory.coldLoadPickUp.data.item.DateTime.month = Charger.cycle_info.startDateTime.month;
  2788. Charger.memory.coldLoadPickUp.data.item.DateTime.day = Charger.cycle_info.startDateTime.day;
  2789. Charger.memory.coldLoadPickUp.data.item.DateTime.hour = Charger.cycle_info.startDateTime.hour;
  2790. Charger.memory.coldLoadPickUp.data.item.DateTime.min = Charger.cycle_info.startDateTime.min;
  2791. Charger.memory.coldLoadPickUp.data.item.DateTime.sec = Charger.cycle_info.startDateTime.sec;
  2792. Charger.memory.coldLoadPickUp.data.item.Duration = Charger.cycle_info.Duration;
  2793. sprintf(Charger.memory.coldLoadPickUp.data.item.PlugType, EVSE_CONNECTOR_TYPE);
  2794. Charger.memory.coldLoadPickUp.data.item.powerSum = Charger.cycle_info.Power_Consumption;
  2795. Charger.memory.coldLoadPickUp.data.item.startType = Charger.cycle_info.StartType;
  2796. //memcpy(Charger.memory.coldLoadPickUp.data.item.user, Charger.rfid.currentCard, 16);
  2797. Charger.memory.coldLoadPickUp.op_bits.update = ON;
  2798. }
  2799. Charger.AC_DripisTure = ON ;
  2800. }
  2801. else
  2802. Charger.counter.AC_Drip.fail ++ ;
  2803. }
  2804. else if((vRms_cal2(ADC3_Buffer_Each[0], ADC3_SAMPLE_COUNT) > (ALARM_SPEC_UV+ALARM_SPEC_OUV_HYSTERESIS)))
  2805. {
  2806. Charger.counter.AC_Drip.fail = 0 ;
  2807. if(Charger.AC_DripisTure)
  2808. {
  2809. Charger.AC_DripisTure = OFF ;
  2810. DEBUG_INFO("Alarm AC Drip recover.\r\n");
  2811. }
  2812. }
  2813. }
  2814. #endif //COLD_LOAD_PICKUP
  2815. // Power accumulate calculation
  2816. if(timer[TIMER_IDX_POWER].isAlarm)
  2817. {
  2818. timeTick.timeTick_End = HAL_GetTick();
  2819. timeTick.timeTick_Delta = timeTick.timeTick_End - timeTick.timeTick_Start;
  2820. timeTick.timeTick_Start = timeTick.timeTick_End;
  2821. if(Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative >= 0xFFFFFFFF)
  2822. {
  2823. Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative = 0;
  2824. }
  2825. else
  2826. {
  2827. #ifdef MODIFY_POWER_CONSUME_ACCU_TIMER
  2828. {
  2829. static double Energy = 0;
  2830. #ifdef FUNC_ZERO_CURRENT_WHEN_OUTP_OFF
  2831. u16 Curr = (Charger.Relay_Action == GPIO_RELAY_ACTION_ON) ? Charger.Current[0] : 0;
  2832. Energy += (((double)Charger.Voltage[0]/100.0)*((double)Curr/100.0)*((double)timeTick.timeTick_Delta/3600000.0)*10.0);
  2833. #else
  2834. Energy += (((double)Charger.Voltage[0]/100.0)*((double)Charger.Current[0]/100.0)*((double)timeTick.timeTick_Delta/3600000.0)*10.0);
  2835. #endif
  2836. if (Energy >= 1)
  2837. {
  2838. uint32_t EnergyIntPart = (uint32_t)Energy;
  2839. Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative += EnergyIntPart;
  2840. Energy -= EnergyIntPart;
  2841. }
  2842. }
  2843. #else //MODIFY_POWER_CONSUME_ACCU_TIMER
  2844. //unit : 0.0001kW , 0.1W *10 = W
  2845. Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative += (uint32_t)((Charger.Voltage[0]/100.0)*(Charger.Current[0]/100.0)*(timeTick.timeTick_Delta/3600000.0)*10.0);
  2846. #endif //MODIFY_POWER_CONSUME_ACCU_TIMER
  2847. }
  2848. #ifdef MODIFY_POWER_CONSUME_ACCU_TIMER
  2849. if((++updateMemReq) > (180 * (1000 / (double)POWER_CONSUME_ACCU_TIMEOUT_SPEC))) //Interval = POWER_CONSUME_ACCU_TIMEOUT_SPEC ms
  2850. #else
  2851. if(++updateMemReq>180) //Interval = 1000ms
  2852. #endif
  2853. {
  2854. Charger.memory.EVSE_Config.op_bits.update = ON;
  2855. updateMemReq = 0;
  2856. }
  2857. timerRefresh(TIMER_IDX_POWER);
  2858. }
  2859. if(HAL_ADC_Start_DMA(&hadc3, (uint32_t*)&ADC3_Buffer, ADC3_CHANEL_COUNT*ADC3_SAMPLE_COUNT)!=HAL_OK)Error_Handler();
  2860. }
  2861. osDelay(1);
  2862. }
  2863. /* USER CODE END StartAdcTask */
  2864. }
  2865. /* USER CODE BEGIN Header_StartTimeoutTask */
  2866. /**
  2867. * @brief Function implementing the timeoutTask thread.
  2868. * @param argument: Not used
  2869. * @retval None
  2870. */
  2871. /* USER CODE END Header_StartTimeoutTask */
  2872. void StartTimeoutTask(void const * argument)
  2873. {
  2874. /* USER CODE BEGIN StartTimeoutTask */
  2875. breathe.isUp = ON;
  2876. for(int idx=0;idx<BLINKER_COUNT;idx++)
  2877. blinker[idx].blinkCount = 0;
  2878. flickerTimeSet(FLICKER_IDX_BREATHE, 500, 500);
  2879. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 0, 1);
  2880. breatheTimeSet(1000, 1000, 1000, 1000, 1000, 0 );
  2881. Charger.am3352.BreatheLedChargeFadeIn = 2500;
  2882. Charger.am3352.BreatheLedChargeFadeOut = 2500;
  2883. Charger.am3352.BreatheLedConnectFadeIn = 1000;
  2884. Charger.am3352.BreatheLedConnectFadeOut = 1000;
  2885. Charger.am3352.BreatheLedAuthdFadeIn = 1000;
  2886. Charger.am3352.BreatheLedAuthdFadeOut = 1000;
  2887. /* Infinite loop */
  2888. for(;;)
  2889. {
  2890. if(Charger.Mode != MODE_INITIAL)
  2891. {
  2892. HAL_RTC_GetTime(&hrtc, &currentTime, RTC_FORMAT_BIN);
  2893. HAL_RTC_GetDate(&hrtc, &currentDate, RTC_FORMAT_BIN);
  2894. Charger.memory.EVSE_Config.data.item.SystemDateTime.year = currentDate.Year + 2000;
  2895. Charger.memory.EVSE_Config.data.item.SystemDateTime.month = currentDate.Month;
  2896. Charger.memory.EVSE_Config.data.item.SystemDateTime.day = currentDate.Date;
  2897. Charger.memory.EVSE_Config.data.item.SystemDateTime.hour = currentTime.Hours;
  2898. Charger.memory.EVSE_Config.data.item.SystemDateTime.min = currentTime.Minutes;
  2899. Charger.memory.EVSE_Config.data.item.SystemDateTime.sec = currentTime.Seconds;
  2900. }
  2901. /*
  2902. Timer check process
  2903. */
  2904. for(int idx=0;idx<TIMER_COUNT;idx++)
  2905. {
  2906. if(timer[idx].isEnable)
  2907. {
  2908. if((HAL_GetTick() - timer[idx].startTime) > timer[idx].timeoutSpec)
  2909. {
  2910. timer[idx].isAlarm = ON;
  2911. }
  2912. else
  2913. {
  2914. timer[idx].isAlarm = OFF;
  2915. }
  2916. }
  2917. else
  2918. {
  2919. timer[idx].startTime = HAL_GetTick();
  2920. timer[idx].isAlarm = OFF;
  2921. }
  2922. }
  2923. /*
  2924. Blink timer process
  2925. */
  2926. for(int idx=0;idx<BLINKER_COUNT;idx++)
  2927. {
  2928. if((HAL_GetTick() - blinker[idx].blinkStartTime) > (blinker[idx].isOn ? blinker[idx].blinkOnSpec : blinker[idx].blinkOffSpec))
  2929. {
  2930. if(blinker[idx].blinkCount >= blinker[idx].blinkCountSpec)
  2931. {
  2932. if((HAL_GetTick() - blinker[idx].resetStartTime) > blinker[idx].blinkRestSpec)
  2933. {
  2934. if(!blinker[idx].blinkRestSpec) //no rest case
  2935. blinker[idx].blinkisFinish = ON ;
  2936. blinker[idx].blinkCount = 0;
  2937. }
  2938. else
  2939. {
  2940. blinker[idx].isOn= OFF;
  2941. blinker[idx].blinkisFinish = ON ;
  2942. }
  2943. }
  2944. else
  2945. {
  2946. if(blinker[idx].isOn)
  2947. blinker[idx].blinkCount++;
  2948. blinker[idx].blinkStartTime = HAL_GetTick();
  2949. blinker[idx].resetStartTime = HAL_GetTick();
  2950. blinker[idx].isOn ^= ON;
  2951. if(blinker[idx].blinkRestSpec > 0) //had rest case
  2952. blinker[idx].blinkisFinish = OFF ;
  2953. }
  2954. }
  2955. }
  2956. /*
  2957. Flick timer process
  2958. */
  2959. for(int idx=0;idx<FLICKER_COUNT;idx++)
  2960. {
  2961. if((HAL_GetTick() - flicker[idx].StartTime) > (flicker[idx].isOn ? flicker[idx].OnSpec : flicker[idx].OffSpec))
  2962. {
  2963. flicker[idx].StartTime = HAL_GetTick();
  2964. flicker[idx].isOn ^= ON;
  2965. }
  2966. }
  2967. /*
  2968. Breathe timer process
  2969. */
  2970. if(breathe.isUp)
  2971. {
  2972. breathe.countPause = 0;
  2973. breathe.countDown = breathe.timeDown;
  2974. if(++breathe.countUp >= breathe.timeUp)
  2975. breathe.isUp = OFF;
  2976. breathe.duty[0] = breathe.RGB[0]*breathe.countUp/breathe.timeUp;
  2977. breathe.duty[1] = breathe.RGB[1]*breathe.countUp/breathe.timeUp;
  2978. breathe.duty[2] = breathe.RGB[2]*breathe.countUp/breathe.timeUp;
  2979. breathe.duty[3] = breathe.RGB[3]*breathe.countUp/breathe.timeUp;
  2980. }
  2981. else
  2982. {
  2983. breathe.countUp = 1000*BREATHE_LED_LOW_BOUNDARY/breathe.timeDown;
  2984. if(breathe.countDown <= 1000*BREATHE_LED_LOW_BOUNDARY/breathe.timeDown)
  2985. {
  2986. //if(++breathe.countPause>250) //breathe ON->oFF need delay nedd Uncomment annotations
  2987. breathe.isUp = ON;
  2988. }
  2989. else
  2990. {
  2991. if(breathe.countDown > 1000*BREATHE_LED_LOW_BOUNDARY/breathe.timeDown)
  2992. breathe.countDown -=1;
  2993. }
  2994. breathe.duty[0] = breathe.RGB[0]*breathe.countDown/breathe.timeDown;
  2995. breathe.duty[1] = breathe.RGB[1]*breathe.countDown/breathe.timeDown;
  2996. breathe.duty[2] = breathe.RGB[2]*breathe.countDown/breathe.timeDown;
  2997. breathe.duty[3] = breathe.RGB[3]*breathe.countDown/breathe.timeDown;
  2998. }
  2999. osDelay(1);
  3000. }
  3001. /* USER CODE END StartTimeoutTask */
  3002. }
  3003. /* USER CODE BEGIN Header_StartRfidTask */
  3004. /**
  3005. * @brief Function implementing the rfidTask thread.
  3006. * @param argument: Not used
  3007. * @retval None
  3008. */
  3009. /* USER CODE END Header_StartRfidTask */
  3010. void StartRfidTask(void const * argument)
  3011. {
  3012. /* USER CODE BEGIN StartRfidTask */
  3013. //rrfid
  3014. uint8_t cardType = 0;
  3015. uint8_t tmpBuf[32];
  3016. uint8_t tx[UART_BUFFER_SIZE>>4];
  3017. uint8_t tx_len;
  3018. HAL_GPIO_WritePin(OUT_RFID_Reset_GPIO_Port, OUT_RFID_Reset_Pin, GPIO_PIN_SET);
  3019. /* Infinite loop */
  3020. for(;;)
  3021. {
  3022. // Read card SN
  3023. if(!Charger.rfid.op_bits.reqHalt && !Charger.rfid.op_bits.reqBlockRead)
  3024. {
  3025. switch(cardType)
  3026. {
  3027. case 0x00:
  3028. tx_len = 4;
  3029. tx[0] = tx_len - 0x01;
  3030. tx[1] = RFID_CMD_REQUEST_SN_14443A;
  3031. tx[2] = 0x01;
  3032. tx[3] = rfidCheckSumCal(tx, tx_len-1);
  3033. cardType++;
  3034. break;
  3035. case 0x01:
  3036. tx_len = 5;
  3037. tx[0] = tx_len - 0x01;
  3038. tx[1] = RFID_CMD_REQUEST_SN_14443B;
  3039. tx[2] = 0x00;
  3040. tx[3] = 0x00;
  3041. tx[4] = rfidCheckSumCal(tx, tx_len-1);
  3042. cardType++;
  3043. break;
  3044. case 0x02:
  3045. tx_len = 9;
  3046. tx[0] = tx_len - 0x01;
  3047. tx[1] = RFID_CMD_REQUEST_SN_FELICA;
  3048. tx[2] = 0x06;
  3049. tx[3] = 0x00;
  3050. tx[4] = 0xff;
  3051. tx[5] = 0xff;
  3052. tx[6] = 0x01;
  3053. tx[7] = 0x00;
  3054. tx[8] = rfidCheckSumCal(tx, tx_len-1);
  3055. cardType++;
  3056. break;
  3057. default:
  3058. cardType = 0;
  3059. break;
  3060. }
  3061. HAL_UART_Transmit(&RFID_USART, (uint8_t *)tx, tx_len, 0xffff);
  3062. osDelay(100);
  3063. }
  3064. if(Charger.rfid.op_bits.reqHalt)
  3065. {
  3066. tx_len = 3;
  3067. tx[0] = tx_len - 0x01;
  3068. tx[1] = RFID_CMD_HALT_14443A;
  3069. tx[2] = rfidCheckSumCal(tx, tx_len-1);
  3070. HAL_UART_Transmit(&RFID_USART, (uint8_t *)tx, tx_len, 0xffff);
  3071. osDelay(100);
  3072. }
  3073. if(Charger.rfid.op_bits.reqBlockRead)
  3074. {
  3075. tx_len = 0x0b;
  3076. tx[0] = tx_len - 0x01;
  3077. tx[1] = RFID_CMD_BLOCK_READ;
  3078. tx[2] = Charger.rfid.keyType;
  3079. tx[3] = Charger.rfid.targetBlock;
  3080. tx[4] = Charger.rfid.key[0];
  3081. tx[5] = Charger.rfid.key[1];
  3082. tx[6] = Charger.rfid.key[2];
  3083. tx[7] = Charger.rfid.key[3];
  3084. tx[8] = Charger.rfid.key[4];
  3085. tx[9] = Charger.rfid.key[5];
  3086. tx[10] = rfidCheckSumCal(tx, tx_len-1);
  3087. HAL_UART_Transmit(&RFID_USART, (uint8_t *)tx, tx_len, 0xffff);
  3088. osDelay(100);
  3089. }
  3090. // RFID module feedback process
  3091. if(UART_RFID_recv_end_flag == ON)
  3092. {
  3093. if((rfidCheckSumCal(UART_RFID_rx_buffer, UART_RFID_rx_buffer[0]) == UART_RFID_rx_buffer[UART_RFID_rx_buffer[0]]) && (Charger.Mode != MODE_INITIAL) && (Charger.Mode != MODE_ALARM))
  3094. {
  3095. switch(UART_RFID_rx_buffer[1])
  3096. {
  3097. case RFID_CMD_LED:
  3098. break;
  3099. case RFID_CMD_REQUEST_SN_14443A:
  3100. if(UART_RFID_rx_buffer[0] == 9)
  3101. {
  3102. Charger.rfid.snType = RFID_SN_TYPE_4BYTE;
  3103. memset(&Charger.rfid.data_Sn[0], 0x00, ARRAY_SIZE(Charger.rfid.data_Sn));
  3104. for(uint8_t idx=0;idx<16;idx++)
  3105. {
  3106. if(idx<4)
  3107. {
  3108. Charger.rfid.data_Sn[idx] = UART_RFID_rx_buffer[idx+2];
  3109. }
  3110. else
  3111. {
  3112. Charger.rfid.data_Sn[idx] = 0x00;
  3113. }
  3114. }
  3115. if(isMode(MODE_IDLE))
  3116. {
  3117. memset(&Charger.rfid.currentCard[0], 0x00, ARRAY_SIZE(Charger.rfid.currentCard));
  3118. }
  3119. if(Charger.memory.EVSE_Config.data.item.RFID_SN_Endian)
  3120. {
  3121. DEBUG_INFO("RFID SN: %02X%02X%02X%02X\r\n", Charger.rfid.data_Sn[3], Charger.rfid.data_Sn[2], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[0]);
  3122. sprintf((char *)tmpBuf, "%02X%02X%02X%02X", Charger.rfid.data_Sn[3], Charger.rfid.data_Sn[2], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[0]);
  3123. memcpy(&Charger.rfid.nowCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.currentCard));
  3124. }
  3125. else
  3126. {
  3127. DEBUG_INFO("RFID SN: %02X%02X%02X%02X\r\n", Charger.rfid.data_Sn[0], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[2], Charger.rfid.data_Sn[3]);
  3128. sprintf((char *)tmpBuf, "%02X%02X%02X%02X", Charger.rfid.data_Sn[0], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[2], Charger.rfid.data_Sn[3]);
  3129. memcpy(&Charger.rfid.nowCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.currentCard));
  3130. }
  3131. }
  3132. else if(UART_RFID_rx_buffer[0] == 0x0c)
  3133. {
  3134. Charger.rfid.snType = RFID_SN_TYPE_7BYTE;
  3135. memset(&Charger.rfid.data_Sn[0], 0x00, ARRAY_SIZE(Charger.rfid.data_Sn));
  3136. for(uint8_t idx=0;idx<16;idx++)
  3137. {
  3138. if(idx<7)
  3139. {
  3140. Charger.rfid.data_Sn[idx] = UART_RFID_rx_buffer[idx+2];
  3141. }
  3142. else
  3143. {
  3144. Charger.rfid.data_Sn[idx] = 0x00;
  3145. }
  3146. }
  3147. if(isMode(MODE_IDLE))
  3148. {
  3149. memset(&Charger.rfid.currentCard[0], 0x00, ARRAY_SIZE(Charger.rfid.currentCard));
  3150. }
  3151. if(Charger.memory.EVSE_Config.data.item.RFID_SN_Endian)
  3152. {
  3153. DEBUG_INFO("RFID SN: %02X%02X%02X%02X%02X%02X%02X\r\n", Charger.rfid.data_Sn[6], Charger.rfid.data_Sn[5], Charger.rfid.data_Sn[4], Charger.rfid.data_Sn[3], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[0]);
  3154. sprintf((char *)tmpBuf, "%02X%02X%02X%02X%02X%02X%02X", Charger.rfid.data_Sn[6], Charger.rfid.data_Sn[5], Charger.rfid.data_Sn[4], Charger.rfid.data_Sn[3], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[0]);
  3155. memcpy(&Charger.rfid.nowCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.currentCard));
  3156. }
  3157. else
  3158. {
  3159. DEBUG_INFO("RFID SN: %02X%02X%02X%02X%02X%02X%02X\r\n", Charger.rfid.data_Sn[0], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[2], Charger.rfid.data_Sn[3], Charger.rfid.data_Sn[4], Charger.rfid.data_Sn[5], Charger.rfid.data_Sn[6]);
  3160. sprintf((char *)tmpBuf, "%02X%02X%02X%02X%02X%02X%02X", Charger.rfid.data_Sn[0], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[2], Charger.rfid.data_Sn[3], Charger.rfid.data_Sn[4], Charger.rfid.data_Sn[5], Charger.rfid.data_Sn[6]);
  3161. memcpy(&Charger.rfid.nowCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.currentCard));
  3162. }
  3163. }
  3164. //PH
  3165. if (Charger.memory.EVSE_Config.data.item.OfflinePolicy == RFID_USER_AUTH_PH )
  3166. {
  3167. if(UART_RFID_rx_buffer[0] >= 9)
  3168. {
  3169. if(isMode(MODE_IDLE))
  3170. {
  3171. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU)
  3172. {
  3173. Charger.rfid.targetBlock = 0x02;
  3174. Charger.rfid.keyType = 0x00;
  3175. memset(&Charger.rfid.key[0], 0xff, 6);
  3176. Charger.rfid.op_bits.reqBlockRead = ON;
  3177. //control in RFID_CMD_BLOCK_READ to Valid_RFID_SN
  3178. }
  3179. }
  3180. else if(isMode(MODE_CHARGING) || isMode(MODE_STOP))
  3181. {
  3182. if ((Array_data_Check(Charger.rfid.currentCard,Charger.rfid.nowCard))&&(Charger.cycle_info.StartType != START_METHOD_BLE))
  3183. {
  3184. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU)
  3185. {
  3186. Charger.rfid.op_bits.reqStop = ON;
  3187. setLedMotion(LED_ACTION_RFID_PASS);
  3188. Charger.speaker.motion = SPEAKER_SHORT;
  3189. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3190. }
  3191. DEBUG_WARN("reqStop = ON.\r\n");
  3192. }
  3193. else
  3194. {
  3195. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU)
  3196. {
  3197. setLedMotion(LED_ACTION_RFID_FAIL);
  3198. Charger.speaker.motion = SPEAKER_BLINK;
  3199. blinkerTimeSet(BLINKER_IDX_SPEAKER, 100, 100, SPEAK_RESET_RFID_TIME, 3) ; // SPEAK_RESET_RFID_TIME -> Avoid repeating
  3200. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3201. }
  3202. DEBUG_WARN("reqStop = Fail\r\n");
  3203. }
  3204. Charger.rfid.op_bits.reqHalt = ON;
  3205. Charger.rfid.op_bits.reqBlockRead = OFF;
  3206. }
  3207. }
  3208. }
  3209. //White List
  3210. else if (Charger.memory.EVSE_Config.data.item.OfflinePolicy == RFID_USER_AUTH_LOCAL_LIST)
  3211. {
  3212. DEBUG_WARN("OfflinePolicy is White List\r\n");
  3213. }
  3214. //Free
  3215. else if (Charger.memory.EVSE_Config.data.item.OfflinePolicy == RFID_USER_AUTH_FREE)
  3216. {
  3217. Charger.rfid.op_bits.reqHalt = ON;
  3218. Charger.rfid.op_bits.reqBlockRead = OFF;
  3219. if(isMode(MODE_IDLE))
  3220. {
  3221. for(uint8_t idx=0;idx<16;idx++)
  3222. {
  3223. Charger.rfid.currentCard[idx] = Charger.rfid.data_Sn[idx];
  3224. }
  3225. Charger.rfid.op_bits.reqStart = ON;
  3226. memcpy(&Charger.rfid.currentCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.currentCard));
  3227. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  3228. setLedMotion(LED_ACTION_RFID_PASS);
  3229. Charger.speaker.motion = SPEAKER_SHORT;
  3230. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3231. }
  3232. DEBUG_WARN("RFID authorize pass.\r\n");
  3233. }
  3234. else if(isMode(MODE_CHARGING) || isMode(MODE_STOP))
  3235. {
  3236. DEBUG_WARN("RFID authorize pass.\r\n");
  3237. if (Array_data_Check(Charger.rfid.currentCard,Charger.rfid.nowCard))
  3238. {
  3239. Charger.rfid.op_bits.reqStop = ON;
  3240. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  3241. setLedMotion(LED_ACTION_RFID_PASS);
  3242. Charger.speaker.motion = SPEAKER_SHORT;
  3243. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3244. }
  3245. DEBUG_WARN("reqStop = ON.\r\n");
  3246. }
  3247. else
  3248. {
  3249. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  3250. setLedMotion(LED_ACTION_RFID_FAIL);
  3251. Charger.speaker.motion = SPEAKER_BLINK;
  3252. blinkerTimeSet(BLINKER_IDX_SPEAKER, 100, 100, SPEAK_RESET_RFID_TIME, 3) ; // SPEAK_RESET_RFID_TIME -> Avoid repeating
  3253. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3254. }
  3255. DEBUG_WARN("reqStop = Fail\r\n");
  3256. }
  3257. }
  3258. }
  3259. //NO charger
  3260. else // Charger.memory.EVSE_Config.data.item.OfflinePolicy == RFID_USER_AUTH_NO_CHARGING
  3261. {
  3262. setLedMotion(LED_ACTION_RFID_FAIL);
  3263. Charger.speaker.motion = SPEAKER_BLINK;
  3264. blinkerTimeSet(BLINKER_IDX_SPEAKER, 100, 100, SPEAK_RESET_RFID_TIME, 3) ; // SPEAK_RESET_RFID_TIME -> Avoid repeating
  3265. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3266. Charger.rfid.op_bits.reqHalt = ON;
  3267. Charger.rfid.op_bits.reqBlockRead = OFF;
  3268. DEBUG_WARN("It is RFID NO charger mode \r\n");
  3269. }
  3270. break;
  3271. case RFID_CMD_REQUEST_SN_14443B:
  3272. if(UART_RFID_rx_buffer[1] == RFID_CMD_REQUEST_SN_14443B)
  3273. {
  3274. Charger.rfid.snType = RFID_SN_TYPE_14443B;
  3275. memset(&Charger.rfid.data_Sn[0], 0x00, ARRAY_SIZE(Charger.rfid.data_Sn));
  3276. for(uint8_t idx=0;idx<16;idx++)
  3277. {
  3278. if(idx<13)
  3279. {
  3280. Charger.rfid.data_Sn[idx] = UART_RFID_rx_buffer[idx+2];
  3281. }
  3282. }
  3283. if(isMode(MODE_IDLE))
  3284. memset(&Charger.rfid.nowCard[0], 0x00, ARRAY_SIZE(Charger.rfid.nowCard));
  3285. if(Charger.memory.EVSE_Config.data.item.RFID_SN_Endian)
  3286. {
  3287. DEBUG_INFO("RFID SN: %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\r\n", Charger.rfid.data_Sn[12], Charger.rfid.data_Sn[11], Charger.rfid.data_Sn[10], Charger.rfid.data_Sn[9], Charger.rfid.data_Sn[8], Charger.rfid.data_Sn[7], Charger.rfid.data_Sn[6], Charger.rfid.data_Sn[5], Charger.rfid.data_Sn[4], Charger.rfid.data_Sn[3], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[0]);
  3288. sprintf((char *)tmpBuf, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", Charger.rfid.data_Sn[12], Charger.rfid.data_Sn[11], Charger.rfid.data_Sn[10], Charger.rfid.data_Sn[9], Charger.rfid.data_Sn[8], Charger.rfid.data_Sn[7], Charger.rfid.data_Sn[6], Charger.rfid.data_Sn[5], Charger.rfid.data_Sn[4], Charger.rfid.data_Sn[3], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[0]);
  3289. memcpy(&Charger.rfid.nowCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.nowCard));
  3290. }
  3291. else
  3292. {
  3293. DEBUG_INFO("RFID SN: %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\r\n", Charger.rfid.data_Sn[0], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[2], Charger.rfid.data_Sn[3], Charger.rfid.data_Sn[4], Charger.rfid.data_Sn[5], Charger.rfid.data_Sn[6], Charger.rfid.data_Sn[7], Charger.rfid.data_Sn[8], Charger.rfid.data_Sn[9], Charger.rfid.data_Sn[10], Charger.rfid.data_Sn[11], Charger.rfid.data_Sn[12]);
  3294. sprintf((char *)tmpBuf, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", Charger.rfid.data_Sn[0], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[2], Charger.rfid.data_Sn[3], Charger.rfid.data_Sn[4], Charger.rfid.data_Sn[5], Charger.rfid.data_Sn[6], Charger.rfid.data_Sn[7], Charger.rfid.data_Sn[8], Charger.rfid.data_Sn[9], Charger.rfid.data_Sn[10], Charger.rfid.data_Sn[11], Charger.rfid.data_Sn[12]);
  3295. memcpy(&Charger.rfid.nowCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.nowCard));
  3296. }
  3297. }
  3298. //White List
  3299. if (Charger.memory.EVSE_Config.data.item.OfflinePolicy == RFID_USER_AUTH_LOCAL_LIST)
  3300. {
  3301. DEBUG_WARN("OfflinePolicy is White List\r\n");
  3302. }
  3303. //Free
  3304. else if (Charger.memory.EVSE_Config.data.item.OfflinePolicy == RFID_USER_AUTH_FREE)
  3305. {
  3306. Charger.rfid.op_bits.reqHalt = ON;
  3307. Charger.rfid.op_bits.reqBlockRead = OFF;
  3308. if(isMode(MODE_IDLE))
  3309. {
  3310. for(uint8_t idx=0;idx<16;idx++)
  3311. {
  3312. Charger.rfid.currentCard[idx] = Charger.rfid.data_Sn[idx];
  3313. }
  3314. Charger.rfid.op_bits.reqStart = ON;
  3315. memcpy(&Charger.rfid.currentCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.currentCard));
  3316. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  3317. setLedMotion(LED_ACTION_RFID_PASS);
  3318. Charger.speaker.motion = SPEAKER_SHORT;
  3319. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3320. }
  3321. DEBUG_WARN("RFID authorize pass.\r\n");
  3322. }
  3323. else if(isMode(MODE_CHARGING) || isMode(MODE_STOP))
  3324. {
  3325. DEBUG_WARN("RFID authorize pass.\r\n");
  3326. if (Array_data_Check(Charger.rfid.currentCard,Charger.rfid.nowCard))
  3327. {
  3328. Charger.rfid.op_bits.reqStop = ON;
  3329. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  3330. setLedMotion(LED_ACTION_RFID_PASS);
  3331. Charger.speaker.motion = SPEAKER_SHORT;
  3332. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3333. }
  3334. DEBUG_WARN("reqStop = ON.\r\n");
  3335. }
  3336. else
  3337. {
  3338. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  3339. setLedMotion(LED_ACTION_RFID_FAIL);
  3340. Charger.speaker.motion = SPEAKER_BLINK;
  3341. blinkerTimeSet(BLINKER_IDX_SPEAKER, 100, 100, SPEAK_RESET_RFID_TIME, 3) ; // SPEAK_RESET_RFID_TIME -> Avoid repeating
  3342. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3343. }
  3344. DEBUG_WARN("reqStop = Fail\r\n");
  3345. }
  3346. }
  3347. }
  3348. //NO charger
  3349. else // Charger.memory.EVSE_Config.data.item.OfflinePolicy == RFID_USER_AUTH_NO_CHARGING
  3350. {
  3351. setLedMotion(LED_ACTION_RFID_FAIL);
  3352. Charger.speaker.motion = SPEAKER_BLINK;
  3353. blinkerTimeSet(BLINKER_IDX_SPEAKER, 100, 100, SPEAK_RESET_RFID_TIME, 3) ; // SPEAK_RESET_RFID_TIME -> Avoid repeating
  3354. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3355. Charger.rfid.op_bits.reqHalt = ON;
  3356. Charger.rfid.op_bits.reqBlockRead = OFF;
  3357. DEBUG_WARN("It is RFID NO charger mode \r\n");
  3358. }
  3359. break;
  3360. case RFID_CMD_REQUEST_SN_FELICA:
  3361. if(UART_RFID_rx_buffer[1] == RFID_CMD_REQUEST_SN_FELICA)
  3362. {
  3363. Charger.rfid.snType = RFID_SN_TYPE_FELICA;
  3364. memset(&Charger.rfid.data_Sn[0], 0x00, ARRAY_SIZE(Charger.rfid.data_Sn));
  3365. for(uint8_t idx=0;idx<16;idx++)
  3366. {
  3367. if(idx<6)
  3368. {
  3369. Charger.rfid.data_Sn[idx] = UART_RFID_rx_buffer[idx+6];
  3370. }
  3371. }
  3372. if(isMode(MODE_IDLE))
  3373. memset(&Charger.rfid.nowCard[0], 0x00, ARRAY_SIZE(Charger.rfid.nowCard));
  3374. if(Charger.memory.EVSE_Config.data.item.RFID_SN_Endian)
  3375. {
  3376. DEBUG_INFO("RFID SN: %02X%02X%02X%02X%02X%02X\r\n", Charger.rfid.data_Sn[5], Charger.rfid.data_Sn[4], Charger.rfid.data_Sn[3], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[0]);
  3377. sprintf((char *)tmpBuf, "%02X%02X%02X%02X%02X%02X", Charger.rfid.data_Sn[5], Charger.rfid.data_Sn[4], Charger.rfid.data_Sn[3], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[0]);
  3378. memcpy(&Charger.rfid.nowCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.nowCard));
  3379. }
  3380. else
  3381. {
  3382. DEBUG_INFO("RFID SN: %02X%02X%02X%02X%02X%02X\r\n", Charger.rfid.data_Sn[0], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[2], Charger.rfid.data_Sn[3], Charger.rfid.data_Sn[4], Charger.rfid.data_Sn[5]);
  3383. sprintf((char *)tmpBuf, "%02X%02X%02X%02X%02X%02X", Charger.rfid.data_Sn[0], Charger.rfid.data_Sn[1], Charger.rfid.data_Sn[2], Charger.rfid.data_Sn[3], Charger.rfid.data_Sn[4], Charger.rfid.data_Sn[5]);
  3384. memcpy(&Charger.rfid.nowCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.nowCard));
  3385. }
  3386. }
  3387. //White List
  3388. if (Charger.memory.EVSE_Config.data.item.OfflinePolicy == RFID_USER_AUTH_LOCAL_LIST)
  3389. {
  3390. DEBUG_WARN("OfflinePolicy is White List\r\n");
  3391. }
  3392. //Free
  3393. else if (Charger.memory.EVSE_Config.data.item.OfflinePolicy == RFID_USER_AUTH_FREE)
  3394. {
  3395. Charger.rfid.op_bits.reqHalt = ON;
  3396. Charger.rfid.op_bits.reqBlockRead = OFF;
  3397. if(isMode(MODE_IDLE))
  3398. {
  3399. for(uint8_t idx=0;idx<16;idx++)
  3400. {
  3401. Charger.rfid.currentCard[idx] = Charger.rfid.data_Sn[idx];
  3402. }
  3403. Charger.rfid.op_bits.reqStart = ON;
  3404. memcpy(&Charger.rfid.currentCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.currentCard));
  3405. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  3406. setLedMotion(LED_ACTION_RFID_PASS);
  3407. Charger.speaker.motion = SPEAKER_SHORT;
  3408. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3409. }
  3410. DEBUG_WARN("RFID authorize pass.\r\n");
  3411. }
  3412. else if(isMode(MODE_CHARGING) || isMode(MODE_STOP))
  3413. {
  3414. DEBUG_WARN("RFID authorize pass.\r\n");
  3415. if (Array_data_Check(Charger.rfid.currentCard,Charger.rfid.nowCard))
  3416. {
  3417. Charger.rfid.op_bits.reqStop = ON;
  3418. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  3419. setLedMotion(LED_ACTION_RFID_PASS);
  3420. Charger.speaker.motion = SPEAKER_SHORT;
  3421. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3422. }
  3423. DEBUG_WARN("reqStop = ON.\r\n");
  3424. }
  3425. else
  3426. {
  3427. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  3428. setLedMotion(LED_ACTION_RFID_FAIL);
  3429. Charger.speaker.motion = SPEAKER_BLINK;
  3430. blinkerTimeSet(BLINKER_IDX_SPEAKER, 100, 100, SPEAK_RESET_RFID_TIME, 3) ; // SPEAK_RESET_RFID_TIME -> Avoid repeating
  3431. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3432. }
  3433. DEBUG_WARN("reqStop = Fail\r\n");
  3434. }
  3435. }
  3436. }
  3437. //NO charger
  3438. else // Charger.memory.EVSE_Config.data.item.OfflinePolicy == RFID_USER_AUTH_NO_CHARGING
  3439. {
  3440. setLedMotion(LED_ACTION_RFID_FAIL);
  3441. Charger.speaker.motion = SPEAKER_BLINK;
  3442. blinkerTimeSet(BLINKER_IDX_SPEAKER, 100, 100, SPEAK_RESET_RFID_TIME, 3) ; // SPEAK_RESET_RFID_TIME -> Avoid repeating
  3443. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3444. Charger.rfid.op_bits.reqHalt = ON;
  3445. Charger.rfid.op_bits.reqBlockRead = OFF;
  3446. DEBUG_WARN("It is RFID NO charger mode \r\n");
  3447. }
  3448. osDelay(1000);
  3449. break;
  3450. case RFID_CMD_BLOCK_READ:
  3451. if(UART_RFID_rx_buffer[1] == RFID_CMD_BLOCK_READ)
  3452. {
  3453. memcpy(Charger.rfid.data_Block, &UART_RFID_rx_buffer[2], 0x10);
  3454. Charger.rfid.op_bits.reqHalt = ON;
  3455. Charger.rfid.op_bits.reqBlockRead = OFF;
  3456. //RFID_PH
  3457. if(Valid_RFID_SN())
  3458. {
  3459. if(isMode(MODE_IDLE))
  3460. {
  3461. Charger.rfid.op_bits.reqStart = ON;
  3462. setLedMotion(LED_ACTION_RFID_PASS);
  3463. Charger.speaker.motion = SPEAKER_SHORT;
  3464. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3465. memcpy(&Charger.rfid.currentCard[0], &tmpBuf[0], ARRAY_SIZE(Charger.rfid.currentCard)); }
  3466. else if(isMode(MODE_CHARGING) || isMode(MODE_STOP))
  3467. {
  3468. Charger.rfid.op_bits.reqStop = ON;
  3469. }
  3470. DEBUG_WARN("RFID authorize pass.\r\n");
  3471. }
  3472. else
  3473. {
  3474. setLedMotion(LED_ACTION_RFID_FAIL);
  3475. Charger.speaker.motion = SPEAKER_BLINK;
  3476. blinkerTimeSet(BLINKER_IDX_SPEAKER, 100, 100, SPEAK_RESET_RFID_TIME, 3) ; // SPEAK_RESET_RFID_TIME -> Avoid repeating
  3477. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  3478. DEBUG_WARN("RFID authorize fault.\r\n");
  3479. }
  3480. }
  3481. break;
  3482. case RFID_CMD_BLOCK_WRITE:
  3483. break;
  3484. case RFID_CMD_HALT_14443A:
  3485. if(UART_RFID_rx_buffer[1] == RFID_CMD_HALT_14443A)
  3486. {
  3487. Charger.rfid.op_bits.reqHalt = OFF;
  3488. DEBUG_INFO("RFID card(14443A) halt succcess.\r\n");
  3489. }
  3490. break;
  3491. case RFID_CMD_HALT_14443B:
  3492. if(UART_RFID_rx_buffer[1] == RFID_CMD_HALT_14443B)
  3493. {
  3494. Charger.rfid.op_bits.reqHalt = OFF;
  3495. DEBUG_INFO("RFID card(14443B) halt succcess.\r\n");
  3496. }
  3497. break;
  3498. case (RFID_CMD_BLOCK_READ ^ 0xff):
  3499. Charger.rfid.op_bits.reqBlockRead = OFF;
  3500. DEBUG_WARN("RFID cread block fail.\r\n");
  3501. break;
  3502. default:
  3503. break;
  3504. }
  3505. }
  3506. // Clear rx_buffer and related varaible
  3507. for(uint16_t i = 0; i < UART_RFID_rx_len ; i++)
  3508. UART_RFID_rx_buffer[i]=0;
  3509. UART_RFID_rx_len = 0;
  3510. UART_RFID_recv_end_flag = OFF;
  3511. }
  3512. HAL_UART_Receive_DMA(&RFID_USART,(uint8_t*)UART_RFID_rx_buffer, (UART_BUFFER_SIZE>>4));
  3513. osDelay(300);
  3514. }
  3515. /* USER CODE END StartRfidTask */
  3516. }
  3517. /* USER CODE BEGIN Header_StartLedSpeakerTask */
  3518. /**
  3519. * @brief Function implementing the led_speakerTask thread.
  3520. * @param argument: Not used
  3521. * @retval None
  3522. */
  3523. /* USER CODE END Header_StartLedSpeakerTask */
  3524. void StartLedSpeakerTask(void const * argument)
  3525. {
  3526. /* USER CODE BEGIN StartLedSpeakerTask */
  3527. uint16_t led_pwm_duty_now[4];
  3528. uint16_t breathe_duty_now[4];
  3529. //standard default
  3530. for(uint8_t idx=0;idx<12;idx++)
  3531. Charger.Led_Brightness[idx] = 0x33;
  3532. /*
  3533. //Night mode starndard default brightness
  3534. Charger.Led_Brightness[0] = 0x11; //00:00~ 01:59
  3535. Charger.Led_Brightness[1] = 0x11; //02:00~ 03:59
  3536. Charger.Led_Brightness[2] = 0x11; //04:00~ 05:59
  3537. Charger.Led_Brightness[3] = 0x33; //06:00~ 07:59
  3538. Charger.Led_Brightness[4] = 0x33; //08:00~ 09:59
  3539. Charger.Led_Brightness[5] = 0x33; //10:00~ 11:59
  3540. Charger.Led_Brightness[6] = 0x33; //12:00~ 13:59
  3541. Charger.Led_Brightness[7] = 0x33; //14:00~ 15:59
  3542. Charger.Led_Brightness[8] = 0x33; //16:00~ 17:59
  3543. Charger.Led_Brightness[9] = 0x11; //18:00~ 19:59
  3544. Charger.Led_Brightness[10] = 0x11; //20:00~ 21:59
  3545. Charger.Led_Brightness[11] = 0x11; //22:00~ 23:59
  3546. */
  3547. /* Infinite loop */
  3548. for(;;)
  3549. {
  3550. led_pwm_duty_now[0] = getBrightnessDuty(Charger.led_pwm_duty[0]);
  3551. led_pwm_duty_now[1] = getBrightnessDuty(Charger.led_pwm_duty[1]);
  3552. led_pwm_duty_now[2] = getBrightnessDuty(Charger.led_pwm_duty[2]);
  3553. led_pwm_duty_now[3] = getBrightnessDuty(Charger.led_pwm_duty[3]);
  3554. breathe_duty_now[0] = getBrightnessDuty(breathe.duty[0]);
  3555. breathe_duty_now[1] = getBrightnessDuty(breathe.duty[1]);
  3556. breathe_duty_now[2] = getBrightnessDuty(breathe.duty[2]);
  3557. breathe_duty_now[3] = getBrightnessDuty(breathe.duty[3]);
  3558. if(timer[TIMER_IDX_LED_TEMP].isAlarm)
  3559. {
  3560. timerDisable(TIMER_IDX_LED_TEMP);
  3561. setLedMotion(Charger.Led_Mode);
  3562. }
  3563. /*
  3564. LED red process
  3565. */
  3566. switch(Charger.led_R.motion)
  3567. {
  3568. case LED_MOTION_ON:
  3569. user_pwm_setvalue(PWM_CH_LED_R, led_pwm_duty_now[0]);
  3570. break;
  3571. case LED_MOTION_BLINK:
  3572. if(blinker[BLINKER_IDX_LED].isOn)
  3573. user_pwm_setvalue(PWM_CH_LED_R, led_pwm_duty_now[0]);
  3574. else
  3575. user_pwm_setvalue(PWM_CH_LED_R, PWM_DUTY_OFF);
  3576. break;
  3577. case LED_MOTION_BREATHE:
  3578. user_pwm_setvalue(PWM_CH_LED_R, breathe_duty_now[0]);
  3579. break;
  3580. case LED_MOTION_OFF:
  3581. default:
  3582. user_pwm_setvalue(PWM_CH_LED_R, PWM_DUTY_OFF);
  3583. break;
  3584. }
  3585. /*
  3586. LED green process
  3587. */
  3588. switch(Charger.led_G.motion)
  3589. {
  3590. case LED_MOTION_ON:
  3591. user_pwm_setvalue(PWM_CH_LED_G, led_pwm_duty_now[1]);
  3592. break;
  3593. case LED_MOTION_BLINK:
  3594. if(blinker[BLINKER_IDX_LED].isOn)
  3595. user_pwm_setvalue(PWM_CH_LED_G, led_pwm_duty_now[1]);
  3596. else
  3597. user_pwm_setvalue(PWM_CH_LED_G, PWM_DUTY_OFF);
  3598. break;
  3599. case LED_MOTION_BREATHE:
  3600. user_pwm_setvalue(PWM_CH_LED_G, breathe_duty_now[1]);
  3601. break;
  3602. case LED_MOTION_OFF:
  3603. default:
  3604. user_pwm_setvalue(PWM_CH_LED_G, PWM_DUTY_OFF);
  3605. break;
  3606. }
  3607. /*
  3608. LED blue process
  3609. */
  3610. switch(Charger.led_B.motion)
  3611. {
  3612. case LED_MOTION_ON:
  3613. user_pwm_setvalue(PWM_CH_LED_B, led_pwm_duty_now[2]);
  3614. break;
  3615. case LED_MOTION_BLINK:
  3616. if(blinker[BLINKER_IDX_LED].isOn)
  3617. user_pwm_setvalue(PWM_CH_LED_B, led_pwm_duty_now[2]);
  3618. else
  3619. user_pwm_setvalue(PWM_CH_LED_B, PWM_DUTY_OFF);
  3620. break;
  3621. case LED_MOTION_BREATHE:
  3622. user_pwm_setvalue(PWM_CH_LED_B, breathe_duty_now[2]);
  3623. break;
  3624. case LED_MOTION_OFF:
  3625. default:
  3626. user_pwm_setvalue(PWM_CH_LED_B, PWM_DUTY_OFF);
  3627. break;
  3628. }
  3629. /*
  3630. LED white process
  3631. */
  3632. switch(Charger.led_W.motion)
  3633. {
  3634. case LED_MOTION_ON:
  3635. user_pwm_setvalue(PWM_CH_LED_W, led_pwm_duty_now[3]);
  3636. break;
  3637. case LED_MOTION_BLINK:
  3638. if(blinker[BLINKER_IDX_LED].isOn)
  3639. user_pwm_setvalue(PWM_CH_LED_W, led_pwm_duty_now[3]);
  3640. else
  3641. user_pwm_setvalue(PWM_CH_LED_W, PWM_DUTY_OFF);
  3642. break;
  3643. case LED_MOTION_BREATHE:
  3644. user_pwm_setvalue(PWM_CH_LED_W, breathe_duty_now[3]);
  3645. break;
  3646. case LED_MOTION_OFF:
  3647. default:
  3648. user_pwm_setvalue(PWM_CH_LED_W, PWM_DUTY_OFF);
  3649. break;
  3650. }
  3651. /*
  3652. LED breathe process
  3653. */
  3654. if(flicker[FLICKER_IDX_BREATHE].isOn)
  3655. HAL_GPIO_WritePin(OUT_LED_Breathe_GPIO_Port, OUT_LED_Breathe_Pin, GPIO_PIN_SET);
  3656. else
  3657. HAL_GPIO_WritePin(OUT_LED_Breathe_GPIO_Port, OUT_LED_Breathe_Pin, GPIO_PIN_RESET);
  3658. /*
  3659. Spearker process
  3660. */
  3661. switch(Charger.speaker.motion)
  3662. {
  3663. case SPEAKER_LONG:
  3664. timerEnable(TIMER_IDX_SPEAKER, 600);
  3665. HAL_GPIO_WritePin(OUT_Speaker_GPIO_Port, OUT_Speaker_Pin, GPIO_PIN_SET);
  3666. if(timer[TIMER_IDX_SPEAKER].isAlarm)
  3667. {
  3668. timerDisable(TIMER_IDX_SPEAKER);
  3669. Charger.speaker.motion = SPEAKER_NONE;
  3670. }
  3671. break;
  3672. case SPEAKER_SHORT:
  3673. timerEnable(TIMER_IDX_SPEAKER, 200);
  3674. HAL_GPIO_WritePin(OUT_Speaker_GPIO_Port, OUT_Speaker_Pin, GPIO_PIN_SET);
  3675. if(timer[TIMER_IDX_SPEAKER].isAlarm)
  3676. {
  3677. timerDisable(TIMER_IDX_SPEAKER);
  3678. Charger.speaker.motion = SPEAKER_NONE;
  3679. }
  3680. break;
  3681. case SPEAKER_BLINK:
  3682. if(blinker[BLINKER_IDX_SPEAKER].isOn)
  3683. HAL_GPIO_WritePin(OUT_Speaker_GPIO_Port, OUT_Speaker_Pin, GPIO_PIN_SET);
  3684. else
  3685. HAL_GPIO_WritePin(OUT_Speaker_GPIO_Port, OUT_Speaker_Pin, GPIO_PIN_RESET);
  3686. if(blinker[BLINKER_IDX_SPEAKER].blinkisFinish)
  3687. {
  3688. disblinkerTime(BLINKER_IDX_SPEAKER);
  3689. }
  3690. break;
  3691. case SPEAKER_NONE:
  3692. default:
  3693. HAL_GPIO_WritePin(OUT_Speaker_GPIO_Port, OUT_Speaker_Pin, GPIO_PIN_RESET);
  3694. break;
  3695. }
  3696. osDelay(1);
  3697. }
  3698. /* USER CODE END StartLedSpeakerTask */
  3699. }
  3700. /* USER CODE BEGIN Header_StartCpTask */
  3701. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  3702. void CpTask_CtrlCpPwm_P0(void)
  3703. {
  3704. //#ifdef FUNC_CCS //======================================================================
  3705. uint16_t MaxCurr = Charger.memory.EVSE_Config.data.item.MaxChargingCurrent;
  3706. // if (Charger.m_bRunCCS) //CCS Charge
  3707. // {
  3708. // user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_5);
  3709. // Charger.MaxChargingCurrent_Previous = MaxCurr;
  3710. // }
  3711. // else //Normal Charge
  3712. {
  3713. #ifdef FUNC_MODIFY_CP_PWM_CTRL_20231225
  3714. if (Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU || Charger.m_bModelNameDC)
  3715. {
  3716. if ((MaxCurr >= 6) && (MaxCurr <= Charger.maxRatingCurrent))
  3717. {
  3718. user_pwm_setvalue(PWM_CH_CP, GetCpPwmDuty(MaxCurr));
  3719. Charger.MaxChargingCurrent_Previous = MaxCurr;
  3720. }
  3721. else
  3722. {
  3723. user_pwm_setvalue(PWM_CH_CP, GetCpPwmDuty(Charger.maxRatingCurrent));
  3724. Charger.MaxChargingCurrent_Previous = Charger.maxRatingCurrent;
  3725. }
  3726. }
  3727. else
  3728. {
  3729. if ((Charger.AC_MaxChargingCurrentOrDuty >= 6) && (Charger.AC_MaxChargingCurrentOrDuty <= Charger.maxRatingCurrent))
  3730. {
  3731. user_pwm_setvalue(PWM_CH_CP, GetCpPwmDuty(Charger.AC_MaxChargingCurrentOrDuty));
  3732. }
  3733. else
  3734. {
  3735. if (Charger.AC_MaxChargingCurrentOrDuty == 0)
  3736. {
  3737. user_pwm_setvalue(PWM_CH_CP, 0);
  3738. }
  3739. else if (Charger.AC_MaxChargingCurrentOrDuty == 1)
  3740. {
  3741. user_pwm_setvalue(PWM_CH_CP, 0);
  3742. }
  3743. else if (Charger.AC_MaxChargingCurrentOrDuty == 5)
  3744. {
  3745. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_5);
  3746. }
  3747. else if (Charger.AC_MaxChargingCurrentOrDuty == 100)
  3748. {
  3749. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  3750. }
  3751. else
  3752. {
  3753. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  3754. }
  3755. }
  3756. Charger.MaxChargingCurrent_Previous = Charger.AC_MaxChargingCurrentOrDuty;
  3757. }
  3758. #else //FUNC_MODIFY_CP_PWM_CTRL_20231225
  3759. if ((MaxCurr >= 6) && (MaxCurr <= Charger.maxRatingCurrent))
  3760. {
  3761. user_pwm_setvalue(PWM_CH_CP, GetCpPwmDuty(MaxCurr));
  3762. Charger.MaxChargingCurrent_Previous = MaxCurr;
  3763. }
  3764. else
  3765. {
  3766. user_pwm_setvalue(PWM_CH_CP, GetCpPwmDuty(Charger.maxRatingCurrent));
  3767. Charger.MaxChargingCurrent_Previous = Charger.maxRatingCurrent;
  3768. }
  3769. #endif //FUNC_MODIFY_CP_PWM_CTRL_20231225
  3770. }
  3771. //#else //FUNC_CCS //======================================================================
  3772. // uint16_t MaxCurr = Charger.memory.EVSE_Config.data.item.MaxChargingCurrent;
  3773. // if (MaxCurr < 6)
  3774. // {
  3775. //#ifdef MODIFY_CP_TASK_CTRL_CP_PWM_BELOW_SIX
  3776. // if (MaxCurr == 0)
  3777. // {
  3778. // user_pwm_setvalue(PWM_CH_CP, 0);
  3779. // Charger.MaxChargingCurrent_Previous = MaxCurr;
  3780. // }
  3781. // else
  3782. // {
  3783. // user_pwm_setvalue(PWM_CH_CP, GetCpPwmDuty(6));
  3784. // Charger.MaxChargingCurrent_Previous = MaxCurr;
  3785. // }
  3786. //#else //MODIFY_CP_TASK_CTRL_CP_PWM_BELOW_SIX
  3787. // user_pwm_setvalue(PWM_CH_CP, GetCpPwmDuty(6));
  3788. // Charger.MaxChargingCurrent_Previous = MaxCurr;
  3789. //#endif //MODIFY_CP_TASK_CTRL_CP_PWM_BELOW_SIX
  3790. // }
  3791. // else if ((MaxCurr >= 6) && (MaxCurr <= Charger.maxRatingCurrent))
  3792. // {
  3793. // user_pwm_setvalue(PWM_CH_CP, GetCpPwmDuty(MaxCurr));
  3794. // Charger.MaxChargingCurrent_Previous = MaxCurr;
  3795. // }
  3796. // else
  3797. // {
  3798. // user_pwm_setvalue(PWM_CH_CP, GetCpPwmDuty(Charger.maxRatingCurrent));
  3799. // Charger.MaxChargingCurrent_Previous = Charger.maxRatingCurrent;
  3800. // }
  3801. //#endif //FUNC_CCS //======================================================================
  3802. }
  3803. void CpTask_CtrlCpPwm_P1(void)
  3804. {
  3805. if (
  3806. (Charger.memory.EVSE_Config.data.item.MaxChargingCurrent != Charger.MaxChargingCurrent_Previous) &&
  3807. (Charger.memory.EVSE_Config.data.item.MaxChargingCurrent <= Charger.maxRatingCurrent)
  3808. )
  3809. {
  3810. CpTask_CtrlCpPwm_P0();
  3811. }
  3812. }
  3813. #endif //MODIFY_CP_TASK_CTRL_CP_PWM
  3814. #ifdef TRIM_CCID_SELFTEST
  3815. void Proc_CCID_SelfTest(void)
  3816. {
  3817. // check Leak Module do CCID selftset.
  3818. if (Charger.CCID_Module_Type == CCID_MODULE_CORMEX)
  3819. {
  3820. Charger.Test_LeakModuleisPass = Test_LeakModule();
  3821. }
  3822. else if (Charger.CCID_Module_Type == CCID_MODULE_VAC)
  3823. {
  3824. Charger.Test_LeakModuleisPass = PASS;
  3825. }
  3826. else
  3827. {
  3828. DEBUG_INFO("Did not judge Leak module can't self test before charging.\r\n");
  3829. }
  3830. XP("Proc CCID Self-Test(%d): %s\r\n", Charger.CCID_Module_Type, Charger.Test_LeakModuleisPass == PASS ? "OK" : "NG");
  3831. }
  3832. #endif
  3833. #ifdef FUNC_AUTO_MODIFY_CCID_MODULE
  3834. uint8_t AutoModifyCCID(void)
  3835. {
  3836. uint8_t old = Charger.CCID_Module_Type;
  3837. if (Test_LeakModule() == PASS)
  3838. {
  3839. Charger.m_bCCID_MustModify = HTK_FALSE;
  3840. Charger.m_CCID_ModuleTestOK = Charger.CCID_Module_Type;
  3841. return PASS;
  3842. }
  3843. else
  3844. {
  3845. Charger.CCID_Module_Type = (Charger.CCID_Module_Type == CCID_MODULE_CORMEX ? CCID_MODULE_VAC : CCID_MODULE_CORMEX);
  3846. XP("#AutoModifyCCID: %s\r\n", CCID_MODULE_TYPE_STR);
  3847. if (Test_LeakModule() == PASS)
  3848. {
  3849. Charger.m_bCCID_MustModify = HTK_TRUE;
  3850. Charger.m_CCID_ModuleTestOK = Charger.CCID_Module_Type;
  3851. return PASS;
  3852. }
  3853. else
  3854. {
  3855. Charger.m_bCCID_MustModify = HTK_FALSE;
  3856. Charger.m_CCID_ModuleTestOK = CCID_MODULE_UNKNOW;
  3857. Charger.CCID_Module_Type = old;
  3858. return FAIL;
  3859. }
  3860. }
  3861. }
  3862. #endif
  3863. /**
  3864. * @brief Function implementing the cpTask thread.
  3865. * @param argument: Not used
  3866. * @retval None
  3867. */
  3868. /* USER CODE END Header_StartCpTask */
  3869. void StartCpTask(void const * argument)
  3870. {
  3871. /* USER CODE BEGIN StartCpTask */
  3872. //ccp
  3873. uint32_t duration_delta;
  3874. Charger.Mode = MODE_INITIAL;
  3875. Charger.Relay_Action=GPIO_RELAY_ACTION_OFF;
  3876. setLedMotion(LED_ACTION_INIT);
  3877. //Check MCU control mode depend on GPIO state
  3878. #ifdef FUNC_FORCE_RUN_CSU_MODE_WITH_DC_MODELNAME
  3879. Update_McuCtrlMode();
  3880. #else
  3881. if(HAL_GPIO_ReadPin(IN_ACT_REQ_GPIO_Port, IN_ACT_REQ_Pin) == GPIO_PIN_RESET)
  3882. {
  3883. Charger.memory.EVSE_Config.data.item.MCU_Control_Mode = MCU_CONTROL_MODE_CSU;
  3884. DEBUG_INFO(" Check MCU_Control_Mode is CSU in CP task\r\n");
  3885. }
  3886. else
  3887. {
  3888. Charger.memory.EVSE_Config.data.item.MCU_Control_Mode = MCU_CONTROL_MODE_NO_CSU;
  3889. DEBUG_INFO(" MCU_Control_Mode is NO NO NO CSU in CP task\r\n");
  3890. }
  3891. #endif
  3892. //Charger.ModelReadisOK = FAIL ;
  3893. /* Infinite loop */
  3894. for(;;)
  3895. {
  3896. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU)
  3897. {
  3898. #ifdef MODIFY_CPTASK_HEAD
  3899. Proc_CpTaskHead(HTK_FALSE);
  3900. #endif
  3901. // Cancel EV ready restart cp logic
  3902. if (Charger.Mode != MODE_HANDSHAKE)
  3903. {
  3904. timerDisable(TIMER_IDX_SIMU_DISCONNECT);
  3905. }
  3906. switch(Charger.Mode)
  3907. {
  3908. case MODE_INITIAL:
  3909. setLedMotion(LED_ACTION_INIT);
  3910. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  3911. HAL_GPIO_WritePin(OUT_Leak_Test_GPIO_Port, OUT_Leak_Test_Pin, GPIO_PIN_SET); // for VAC LeakModule use
  3912. if (Charger.ModelReadisOK ==PASS)
  3913. {
  3914. getRotarySwitchSetting( );
  3915. getGridTypeSwitchSetting( );
  3916. Charger.isTestLeakModule = ON ;
  3917. #ifdef FUNC_AUTO_MODIFY_CCID_MODULE
  3918. if (AutoModifyCCID() == PASS)
  3919. #else
  3920. if(Test_LeakModule() == PASS)
  3921. #endif
  3922. {
  3923. DEBUG_INFO("Leak module self test pass when initial.\r\n");
  3924. osDelay(1000);
  3925. if(Charger.isDebugEnable)
  3926. setChargerMode(MODE_DEBUG);
  3927. else
  3928. {
  3929. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  3930. // Cold load pick up check
  3931. if((Charger.CP_State == SYSTEM_STATE_A) && Charger.memory.coldLoadPickUp.data.item.isEnable)
  3932. {
  3933. recordChargingHis(OFF, END_STATUS_CODE_DROP);
  3934. Charger.memory.coldLoadPickUp.data.item.isEnable = OFF;
  3935. Charger.memory.coldLoadPickUp.op_bits.update = ON;
  3936. }
  3937. }
  3938. osDelay(5000) ; //wait hardware PE feedback data so delay some time
  3939. /*
  3940. TEST CALI VAL
  3941. Charger.memory.EVSE_Config.data.item.Correction_VL1[1][0] = 0x0A59;
  3942. Charger.memory.EVSE_Config.data.item.Correction_VL1[1][1] = 0x0A91;
  3943. Charger.memory.EVSE_Config.data.item.Correction_VL1[2][0] = 0x0684;
  3944. Charger.memory.EVSE_Config.data.item.Correction_VL1[2][1] = 0x06A7;
  3945. Charger.memory.EVSE_Config.data.item.Correction_CL1[1][0] = 0x0139;
  3946. Charger.memory.EVSE_Config.data.item.Correction_CL1[1][1] = 0x0140;
  3947. Charger.memory.EVSE_Config.data.item.Correction_CL1[2][0] = 0x0062;
  3948. Charger.memory.EVSE_Config.data.item.Correction_CL1[2][1] = 0x0064;
  3949. Charger.memory.EVSE_Config.data.item.Correction_Leak[1][0] = 0x0632;
  3950. Charger.memory.EVSE_Config.data.item.Correction_Leak[1][1] = 0x05DC;
  3951. Charger.memory.EVSE_Config.data.item.Correction_Leak[2][0] = 0x0488;
  3952. Charger.memory.EVSE_Config.data.item.Correction_Leak[2][1] = 0x03E8;
  3953. Charger.memory.EVSE_Config.op_bits.update = ON;
  3954. */
  3955. setChargerMode(MODE_IDLE);
  3956. }
  3957. }
  3958. else
  3959. {
  3960. Charger.Alarm_Code |= ALARM_LEAK_MODULE_FAIL;
  3961. DEBUG_INFO("Leak module self test fail when initial.\r\n");
  3962. Charger.counter.LEAK_MODULE.isLatch = ON ;
  3963. }
  3964. Charger.isTestLeakModule = OFF ;
  3965. }
  3966. break;
  3967. case MODE_IDLE:
  3968. if(isModeChange())
  3969. {
  3970. setLedMotion(LED_ACTION_IDLE);
  3971. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  3972. Charger.memory.EVSE_Config.data.item.MaxChargingCurrent = Charger.maxRatingCurrent;
  3973. #ifdef MODIFY_AC_EVSE_STATUS_MAX_CHARGING_CURRENT_VARIABLE
  3974. Charger.AC_MaxChargingCurrentOrDuty = Charger.maxRatingCurrent;
  3975. #endif
  3976. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  3977. Charger.isSetStartTime = ON ;
  3978. }
  3979. if (Charger.memory.EVSE_Config.data.item.AuthMode == AUTH_MODE_OCPP)
  3980. {
  3981. if( Charger.memory.coldLoadPickUp.data.item.isEnable || Charger.rfid.op_bits.reqStart)
  3982. {
  3983. osDelay(LED_RFID_TIME); // for Buzzer and RFID /Fail LED use
  3984. setLedMotion(LED_ACTION_AUTHED);
  3985. // Check start method type
  3986. memset(&Charger.cycle_info.userId[0], 0x00, ARRAY_SIZE(Charger.cycle_info.userId));
  3987. if(Charger.rfid.op_bits.reqStart)
  3988. {
  3989. Charger.cycle_info.StartType = START_METHOD_RFID;
  3990. memcpy(&Charger.cycle_info.userId[0], &Charger.rfid.currentCard[0], ARRAY_SIZE(Charger.rfid.currentCard));
  3991. Charger.istoHandShakeMode = ON ;
  3992. }
  3993. Charger.rfid.op_bits.reqStart = OFF;
  3994. if(Charger.memory.coldLoadPickUp.data.item.isEnable)
  3995. {
  3996. Charger.memory.coldLoadPickUp.data.item.isEnable = OFF;
  3997. Charger.memory.coldLoadPickUp.op_bits.update = ON;
  3998. DEBUG_INFO("coldLoadPickUp ON to rand wait sec.\r\n");
  3999. #ifdef MODIFY_COLD_LOAD_PICKUP_DELAY
  4000. ColdLoadPickup_Delay("coldLoadPickUp ON to rand wait sec.");
  4001. #else
  4002. osDelay((rand()%175000)+5000);
  4003. //osDelay((rand()%5000)+5000);
  4004. #endif
  4005. Charger.istoHandShakeMode = ON ;
  4006. }
  4007. }
  4008. if (Charger.istoHandShakeMode == ON) //charger can to HandShakeMode
  4009. {
  4010. if ((Charger.CP_State == SYSTEM_STATE_B)||(Charger.CP_State == SYSTEM_STATE_C))
  4011. {
  4012. timerDisable(TIMER_IDX_CP);
  4013. Charger.istoHandShakeMode = OFF ;
  4014. setChargerMode(MODE_HANDSHAKE);
  4015. if (Charger.memory.EVSE_Config.data.item.ModelName[3] == 'E')
  4016. timerEnable(TIMER_IDX_SIMU_DISCONNECT, 30000);
  4017. }
  4018. else
  4019. {
  4020. timerEnable(TIMER_IDX_CP, 180000);
  4021. }
  4022. }
  4023. }
  4024. else if (Charger.memory.EVSE_Config.data.item.AuthMode == AUTH_MODE_FREE)
  4025. {
  4026. if((Charger.CP_State == SYSTEM_STATE_B) ||
  4027. (Charger.CP_State == SYSTEM_STATE_C) ||
  4028. Charger.memory.coldLoadPickUp.data.item.isEnable )
  4029. {
  4030. setLedMotion(LED_ACTION_AUTHED);
  4031. // Check start method type
  4032. memset(&Charger.cycle_info.userId[0], 0x00, ARRAY_SIZE(Charger.cycle_info.userId));
  4033. if(Charger.rfid.op_bits.reqStart)
  4034. {
  4035. Charger.cycle_info.StartType = START_METHOD_RFID;
  4036. memcpy(&Charger.cycle_info.userId[0], &Charger.rfid.currentCard[0], ARRAY_SIZE(Charger.rfid.currentCard));
  4037. }
  4038. else
  4039. Charger.cycle_info.StartType = START_METHOD_CP;
  4040. Charger.ble.isRequestOn = OFF;
  4041. Charger.rfid.op_bits.reqStart = OFF;
  4042. if(Charger.memory.coldLoadPickUp.data.item.isEnable)
  4043. {
  4044. Charger.memory.coldLoadPickUp.data.item.isEnable = OFF;
  4045. Charger.memory.coldLoadPickUp.op_bits.update = ON;
  4046. DEBUG_INFO("coldLoadPickUp ON to rand wait sec.\r\n");
  4047. #ifdef MODIFY_COLD_LOAD_PICKUP_DELAY
  4048. ColdLoadPickup_Delay("coldLoadPickUp ON to rand wait sec.");
  4049. #else
  4050. osDelay((rand()%175000)+5000);
  4051. #endif
  4052. }
  4053. setChargerMode(MODE_HANDSHAKE);
  4054. if (Charger.memory.EVSE_Config.data.item.ModelName[3] == 'E')
  4055. timerEnable(TIMER_IDX_SIMU_DISCONNECT, 30000);
  4056. }
  4057. }
  4058. break;
  4059. case MODE_HANDSHAKE:
  4060. if(isModeChange())
  4061. {
  4062. Charger.isCP_in_B = OFF ;
  4063. Charger.isTestLeakModule = ON ;
  4064. #ifdef TRIM_CCID_SELFTEST
  4065. Proc_CCID_SelfTest();
  4066. #endif
  4067. //check Leak test pass
  4068. if (Charger.Test_LeakModuleisPass)
  4069. {
  4070. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4071. CpTask_CtrlCpPwm_P0();
  4072. #endif
  4073. DEBUG_INFO("Leak module self test pass before charging.\r\n");
  4074. }
  4075. else
  4076. {
  4077. Charger.Alarm_Code |= ALARM_LEAK_MODULE_FAIL;
  4078. DEBUG_INFO("Leak module self test fail before charging.\r\n");
  4079. Charger.counter.LEAK_MODULE.retry ++ ;
  4080. if (Charger.counter.LEAK_MODULE.retry >=2)
  4081. {
  4082. Charger.counter.LEAK_MODULE.isLatch = ON ;
  4083. }
  4084. }
  4085. osDelay(300);
  4086. Charger.isTestLeakModule = OFF ;
  4087. }
  4088. // to chaging moder
  4089. if(Charger.CP_State == SYSTEM_STATE_C)
  4090. {
  4091. setChargerMode(MODE_CHARGING);
  4092. if (Charger.isSetStartTime == ON)
  4093. {
  4094. Charger.cycle_info.startTimeTick = HAL_GetTick();
  4095. Charger.cycle_info.startDateTime.year = Charger.memory.EVSE_Config.data.item.SystemDateTime.year;
  4096. Charger.cycle_info.startDateTime.month = Charger.memory.EVSE_Config.data.item.SystemDateTime.month;
  4097. Charger.cycle_info.startDateTime.day = Charger.memory.EVSE_Config.data.item.SystemDateTime.day;
  4098. Charger.cycle_info.startDateTime.hour = Charger.memory.EVSE_Config.data.item.SystemDateTime.hour;
  4099. Charger.cycle_info.startDateTime.min = Charger.memory.EVSE_Config.data.item.SystemDateTime.min;
  4100. Charger.cycle_info.startDateTime.sec = Charger.memory.EVSE_Config.data.item.SystemDateTime.sec;
  4101. Charger.cycle_info.meterStart = Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative;
  4102. }
  4103. }
  4104. if(Charger.CP_State == SYSTEM_STATE_B)
  4105. {
  4106. Charger.isCP_in_B = ON ;
  4107. setLedMotion(LED_ACTION_CONNECTED);
  4108. }
  4109. //RFID remove gun
  4110. if ((Charger.cycle_info.StartType == START_METHOD_RFID) && (Charger.CP_State == SYSTEM_STATE_A) && (Charger.isCP_in_B == ON))
  4111. {
  4112. Charger.isCP_in_B = OFF ;
  4113. setChargerMode(MODE_IDLE);
  4114. }
  4115. //FREE remove gun
  4116. if ((Charger.cycle_info.StartType == START_METHOD_CP) && Charger.CP_State == SYSTEM_STATE_A)
  4117. {
  4118. setChargerMode(MODE_IDLE);
  4119. }
  4120. // EV ready restart cp logic
  4121. if (Charger.memory.EVSE_Config.data.item.ModelName[3] == 'E')
  4122. {
  4123. if(timer[TIMER_IDX_SIMU_DISCONNECT].isAlarm == ON)
  4124. {
  4125. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_ZERO);
  4126. osDelay(4000);
  4127. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  4128. osDelay(600);
  4129. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4130. CpTask_CtrlCpPwm_P0();
  4131. #endif
  4132. timerDisable(TIMER_IDX_SIMU_DISCONNECT);
  4133. }
  4134. }
  4135. break;
  4136. case MODE_CHARGING:
  4137. if(isModeChange())
  4138. {
  4139. #ifdef MODIFY_CPTASK_CSU_0_1_CHARGE_MODE
  4140. CpTask_CtrlCpPwm_P0();
  4141. #endif
  4142. setLedMotion(LED_ACTION_CHARGING);
  4143. }
  4144. if (Charger.memory.EVSE_Config.data.item.AuthMode == AUTH_MODE_OCPP)
  4145. {
  4146. #ifdef MODIFY_AW_CP_TASK_CSU_0_1_CHARGING_MODE_STOP_WITHOUT_STATE_C
  4147. if(Charger.CP_State!=SYSTEM_STATE_C || Charger.rfid.op_bits.reqStop )
  4148. #else
  4149. if(((Charger.CP_State!=SYSTEM_STATE_C)&&(Charger.CP_State!=SYSTEM_STATE_UNKNOWN)) || Charger.rfid.op_bits.reqStop )
  4150. #endif
  4151. {
  4152. setChargerMode(MODE_STOP);
  4153. }
  4154. else
  4155. {
  4156. Charger.Relay_Action = GPIO_RELAY_ACTION_ON;
  4157. duration_delta = (HAL_GetTick() - Charger.cycle_info.startTimeTick);
  4158. Charger.cycle_info.Duration = duration_delta;
  4159. Charger.cycle_info.Power_Consumption = Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative - Charger.cycle_info.meterStart;
  4160. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4161. CpTask_CtrlCpPwm_P1();
  4162. #endif
  4163. }
  4164. }
  4165. else if (Charger.memory.EVSE_Config.data.item.AuthMode == AUTH_MODE_FREE)
  4166. {
  4167. #ifdef MODIFY_AW_CP_TASK_CSU_0_1_CHARGING_MODE_STOP_WITHOUT_STATE_C
  4168. if(Charger.CP_State!=SYSTEM_STATE_C)
  4169. #else
  4170. if((Charger.CP_State != SYSTEM_STATE_C) && (Charger.CP_State != SYSTEM_STATE_UNKNOWN))
  4171. #endif
  4172. {
  4173. setChargerMode(MODE_STOP);
  4174. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4175. }
  4176. else
  4177. {
  4178. Charger.Relay_Action = GPIO_RELAY_ACTION_ON;
  4179. duration_delta = (HAL_GetTick() - Charger.cycle_info.startTimeTick);
  4180. Charger.cycle_info.Duration = duration_delta;
  4181. Charger.cycle_info.Power_Consumption = Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative - Charger.cycle_info.meterStart;
  4182. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4183. CpTask_CtrlCpPwm_P1();
  4184. #endif
  4185. }
  4186. }
  4187. break;
  4188. case MODE_STOP:
  4189. if(isModeChange())
  4190. {
  4191. if ((Charger.memory.EVSE_Config.data.item.AuthMode == AUTH_MODE_OCPP) && Charger.rfid.op_bits.reqStop)
  4192. {
  4193. osDelay(LED_RFID_TIME); // for Buzzer and RFID /Fail LED use
  4194. }
  4195. setLedMotion(LED_ACTION_STOP);
  4196. Charger.isCP_in_B = OFF ;
  4197. Charger.is_RFIDKeepCharger = ON ;
  4198. Charger.isSetStartTime = OFF ;
  4199. }
  4200. if (Charger.memory.EVSE_Config.data.item.AuthMode == AUTH_MODE_OCPP)
  4201. {
  4202. //GB
  4203. if(Charger.CP_State == SYSTEM_STATE_A)
  4204. {
  4205. Charger.cycle_info.endTimeTick = HAL_GetTick();
  4206. recordChargingHis(OFF, END_STATUS_CODE_NORMAL);
  4207. Charger.rfid.op_bits.reqStop = OFF;
  4208. setChargerMode(MODE_IDLE);
  4209. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4210. timerDisable(TIMER_IDX_RFID_RELAY_OFF);
  4211. Charger.isCP_in_B = OFF ;
  4212. Charger.is_RFIDKeepCharger = OFF ;
  4213. Charger.cycle_info.StartType = START_METHOD_NONE;
  4214. }
  4215. else
  4216. {
  4217. //rfid close PWM and relay
  4218. if ((Charger.rfid.op_bits.reqStop == ON)&&Charger.cycle_info.StartType == START_METHOD_RFID)
  4219. {
  4220. Charger.rfid.op_bits.reqStop = OFF;
  4221. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL); // close pwm
  4222. timerEnable( TIMER_IDX_RFID_RELAY_OFF, 3000); //is set wait 3 sec
  4223. Charger.is_RFIDKeepCharger = OFF ;
  4224. }
  4225. if (timer[TIMER_IDX_RFID_RELAY_OFF].isAlarm == ON) //is wait 3 sec
  4226. {
  4227. timerDisable(TIMER_IDX_RFID_RELAY_OFF);
  4228. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4229. setChargerMode(MODE_IDLE);
  4230. Charger.cycle_info.StartType = START_METHOD_NONE;
  4231. }
  4232. //cp close or open PWM
  4233. if((Charger.CP_State == SYSTEM_STATE_B))
  4234. {
  4235. timerDisable(TIMER_IDX_RFID_RELAY_OFF);
  4236. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4237. Charger.isCP_in_B = ON ;
  4238. }
  4239. else if (Charger.CP_State == SYSTEM_STATE_C )
  4240. {
  4241. if ( Charger.isCP_in_B == ON && Charger.is_RFIDKeepCharger == ON)
  4242. {
  4243. Charger.is_RFIDKeepCharger = OFF ;
  4244. Charger.isCP_in_B = OFF ;
  4245. Charger.isTestLeakModule = ON ;
  4246. #ifdef TRIM_CCID_SELFTEST
  4247. Proc_CCID_SelfTest();
  4248. #endif
  4249. //check Leak test pass
  4250. if (Charger.Test_LeakModuleisPass)
  4251. {
  4252. timerEnable(TIMER_IDX_CP, 180000);
  4253. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4254. CpTask_CtrlCpPwm_P0();
  4255. #endif
  4256. setChargerMode(MODE_CHARGING);
  4257. DEBUG_INFO("Leak module self test pass before charging.\r\n");
  4258. }
  4259. else
  4260. {
  4261. Charger.Alarm_Code |= ALARM_LEAK_MODULE_FAIL;
  4262. DEBUG_INFO("Leak module self test fail before charging.\r\n");
  4263. Charger.counter.LEAK_MODULE.retry ++ ;
  4264. if (Charger.counter.LEAK_MODULE.retry >=2)
  4265. {
  4266. Charger.counter.LEAK_MODULE.isLatch = ON ;
  4267. }
  4268. }
  4269. osDelay(300);
  4270. Charger.isTestLeakModule = OFF ;
  4271. }
  4272. }
  4273. }
  4274. }
  4275. else if (Charger.memory.EVSE_Config.data.item.AuthMode == AUTH_MODE_FREE)
  4276. {
  4277. if (Charger.CP_State == SYSTEM_STATE_A)
  4278. {
  4279. Charger.cycle_info.endTimeTick = HAL_GetTick();
  4280. recordChargingHis(OFF, END_STATUS_CODE_NORMAL);
  4281. Charger.ble.isRequestOff = OFF;
  4282. Charger.rfid.op_bits.reqStop = OFF;
  4283. setChargerMode(MODE_IDLE);
  4284. }
  4285. else
  4286. {
  4287. if (Charger.CP_State == SYSTEM_STATE_B)
  4288. {
  4289. Charger.isCP_in_B = ON ;
  4290. }
  4291. if((Charger.CP_State == SYSTEM_STATE_C))
  4292. {
  4293. setChargerMode(MODE_CHARGING);
  4294. }
  4295. }
  4296. }
  4297. break;
  4298. case MODE_ALARM:
  4299. if(isModeChange())
  4300. {
  4301. Charger.isSetStartTime = OFF ;
  4302. //clear blink time , Prevent red light from being snatched by others
  4303. disblinkerTime (BLINKER_IDX_LED) ;
  4304. }
  4305. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4306. //Set alarm led
  4307. if ((Charger.Alarm_Code != Charger.Alarm_Code_before) && (blinker[BLINKER_IDX_LED].blinkisFinish))
  4308. {
  4309. setLedMotion(LED_ACTION_ALARM);
  4310. Charger.Alarm_Code_before = Charger.Alarm_Code ;
  4311. }
  4312. if(Charger.Alarm_Code == 0x00)
  4313. {
  4314. Charger.Alarm_Code_before = Charger.Alarm_Code ;
  4315. if (Charger.Mode_Before_Alarm == MODE_CHARGING) // in charging mode
  4316. {
  4317. if(Charger.CP_State == SYSTEM_STATE_A) // to idle
  4318. {
  4319. Charger.cycle_info.endTimeTick = HAL_GetTick();
  4320. recordChargingHis(OFF, END_STATUS_CODE_NORMAL);
  4321. Charger.rfid.op_bits.reqStop = OFF;
  4322. Charger.ble.isRequestOff = OFF;
  4323. setChargerMode(MODE_IDLE);
  4324. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4325. timerDisable(TIMER_IDX_RFID_RELAY_OFF);
  4326. Charger.isCP_in_B = OFF ;
  4327. Charger.is_RFIDKeepCharger = OFF ;
  4328. Charger.cycle_info.StartType = START_METHOD_NONE;
  4329. }
  4330. else // to handshake
  4331. {
  4332. setChargerMode(MODE_HANDSHAKE);
  4333. Charger.isCP_in_B = ON ;
  4334. setLedMotion(LED_ACTION_CONNECTED);
  4335. }
  4336. }
  4337. else if(Charger.Mode_Before_Alarm == MODE_STOP) // in stop mode
  4338. {
  4339. if(Charger.CP_State == SYSTEM_STATE_A) // to idle
  4340. {
  4341. Charger.cycle_info.endTimeTick = HAL_GetTick();
  4342. recordChargingHis(OFF, END_STATUS_CODE_NORMAL);
  4343. Charger.rfid.op_bits.reqStop = OFF;
  4344. setChargerMode(MODE_IDLE);
  4345. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4346. timerDisable(TIMER_IDX_RFID_RELAY_OFF);
  4347. Charger.isCP_in_B = OFF ;
  4348. Charger.is_RFIDKeepCharger = OFF ;
  4349. Charger.cycle_info.StartType = START_METHOD_NONE;
  4350. }
  4351. else if ((Charger.CP_State == SYSTEM_STATE_B) && (Charger.memory.EVSE_Config.data.item.AuthMode == AUTH_MODE_OCPP)) // OCPP
  4352. {
  4353. if ( ( ((Charger.cycle_info.StartType == START_METHOD_BLE) && (Charger.is_BLEKeepCharger == ON))|| // to handshake
  4354. ((Charger.cycle_info.StartType == START_METHOD_RFID) && (Charger.is_RFIDKeepCharger == ON)) ) &&
  4355. ( Charger.isCP_in_B == ON ) )
  4356. {
  4357. setChargerMode(MODE_HANDSHAKE);
  4358. setLedMotion(LED_ACTION_CONNECTED);
  4359. }
  4360. else //to stop
  4361. {
  4362. setChargerMode(MODE_STOP);
  4363. }
  4364. }
  4365. else if ((Charger.CP_State == SYSTEM_STATE_B) && (Charger.memory.EVSE_Config.data.item.AuthMode == AUTH_MODE_FREE)) //Free
  4366. {
  4367. if ( Charger.isCP_in_B == ON ) // to handshake
  4368. {
  4369. setChargerMode(MODE_HANDSHAKE);
  4370. setLedMotion(LED_ACTION_CONNECTED);
  4371. }
  4372. else //to stop
  4373. {
  4374. setChargerMode(MODE_STOP);
  4375. }
  4376. }
  4377. else //to stop
  4378. {
  4379. setChargerMode(MODE_STOP);
  4380. }
  4381. }
  4382. else // idle
  4383. {
  4384. if(timer[TIMER_IDX_STATE_E].isAlarm == ON)
  4385. {
  4386. setChargerMode(MODE_IDLE);
  4387. }
  4388. }
  4389. }
  4390. break;
  4391. case MODE_MAINTAIN:
  4392. if(isModeChange())
  4393. {}
  4394. break;
  4395. case MODE_DEBUG:
  4396. if(isModeChange())
  4397. {
  4398. setLedMotion(LED_ACTION_DEBUG);
  4399. Charger.Relay_Action = GPIO_RELAY_ACTION_ON;
  4400. timerEnable(TIMER_IDX_DEBUG, 1000);
  4401. }
  4402. if(timer[TIMER_IDX_DEBUG].isAlarm || (Charger.Alarm_Code>0))
  4403. {
  4404. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4405. Charger.isDebugModeCPtest = ON;
  4406. }
  4407. if(Charger.isDebugModeCPtest)
  4408. {
  4409. if(Charger.CP_State == SYSTEM_STATE_A)
  4410. {
  4411. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4412. Charger.memory.EVSE_Config.data.item.MaxChargingCurrent = Charger.maxRatingCurrent;
  4413. #ifdef MODIFY_AC_EVSE_STATUS_MAX_CHARGING_CURRENT_VARIABLE
  4414. Charger.AC_MaxChargingCurrentOrDuty = Charger.maxRatingCurrent;
  4415. #endif
  4416. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  4417. }
  4418. else if (Charger.CP_State == SYSTEM_STATE_B)
  4419. {
  4420. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4421. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4422. CpTask_CtrlCpPwm_P1();
  4423. #endif
  4424. }
  4425. else if (Charger.CP_State == SYSTEM_STATE_C)
  4426. {
  4427. Charger.Relay_Action = GPIO_RELAY_ACTION_ON;
  4428. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4429. CpTask_CtrlCpPwm_P1();
  4430. #endif
  4431. }
  4432. }
  4433. break;
  4434. }
  4435. }
  4436. else if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_CSU) //CP_CSU
  4437. {
  4438. /*
  4439. MCU control mdoe with CSU
  4440. */
  4441. #ifdef MODIFY_CPTASK_HEAD
  4442. Proc_CpTaskHead(HTK_TRUE);
  4443. #endif
  4444. // Cancel EV ready restart cp logic
  4445. if (Charger.Mode != MODE_HANDSHAKE)
  4446. {
  4447. timerDisable(TIMER_IDX_SIMU_DISCONNECT);
  4448. }
  4449. switch(Charger.Mode)
  4450. {
  4451. case MODE_INITIAL:
  4452. setLedMotion(LED_ACTION_INIT);
  4453. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  4454. HAL_GPIO_WritePin(OUT_Leak_Test_GPIO_Port, OUT_Leak_Test_Pin, GPIO_PIN_SET); // for VAC LeakModule use
  4455. #ifdef FUNC_RELAY_OFF_WHEN_CSU_CMD_TIMEOUT
  4456. Charger.m_CSU_RxTick = HAL_GetTick();
  4457. #endif
  4458. if (Charger.ModelReadisOK ==PASS)
  4459. {
  4460. getRotarySwitchSetting( );
  4461. getGridTypeSwitchSetting( );
  4462. Charger.isTestLeakModule = ON ;
  4463. #ifdef FUNC_AUTO_MODIFY_CCID_MODULE
  4464. if (AutoModifyCCID() == PASS)
  4465. #else
  4466. if(Test_LeakModule() == PASS)
  4467. #endif
  4468. {
  4469. DEBUG_INFO("Leak module self test pass when initial.\r\n");
  4470. osDelay(1000);
  4471. if(Charger.isDebugEnable)
  4472. setChargerMode(MODE_DEBUG);
  4473. else
  4474. {
  4475. setChargerMode(MODE_IDLE);
  4476. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  4477. // Cold load pick up check
  4478. if((Charger.CP_State == SYSTEM_STATE_A) && Charger.memory.coldLoadPickUp.data.item.isEnable)
  4479. {
  4480. recordChargingHis(OFF, END_STATUS_CODE_DROP);
  4481. Charger.memory.coldLoadPickUp.data.item.isEnable = OFF;
  4482. Charger.memory.coldLoadPickUp.op_bits.update = ON;
  4483. }
  4484. }
  4485. }
  4486. }
  4487. else
  4488. {
  4489. Charger.Alarm_Code |= ALARM_LEAK_MODULE_FAIL;
  4490. DEBUG_INFO("Leak module self test fail when initial.\r\n");
  4491. Charger.counter.LEAK_MODULE.isLatch = ON ;
  4492. }
  4493. Charger.isTestLeakModule = OFF ;
  4494. }
  4495. break;
  4496. case MODE_IDLE:
  4497. if(isModeChange())
  4498. {
  4499. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4500. Charger.memory.EVSE_Config.data.item.MaxChargingCurrent = Charger.maxRatingCurrent;
  4501. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  4502. Charger.isSetStartTime = ON ;
  4503. #ifdef FUNC_BLOCK_CSU_CONFIG_CP_PWM_DUTY
  4504. Charger.m_bBlockCsuCpPwmDuty = HTK_DISABLE;
  4505. #endif
  4506. }
  4507. if(Charger.am3352.isRequestOn &&
  4508. (Charger.CP_State != SYSTEM_STATE_A))
  4509. {
  4510. setChargerMode(MODE_HANDSHAKE);
  4511. if (Charger.memory.EVSE_Config.data.item.ModelName[3] == 'E')
  4512. timerEnable(TIMER_IDX_SIMU_DISCONNECT, 30000);
  4513. }
  4514. break;
  4515. case MODE_HANDSHAKE:
  4516. if(isModeChange())
  4517. {
  4518. Charger.isCP_in_B = OFF ;
  4519. Charger.isTestLeakModule = ON ;
  4520. #ifdef TRIM_CCID_SELFTEST
  4521. Proc_CCID_SelfTest();
  4522. #endif
  4523. //check Leak test pass
  4524. if (Charger.Test_LeakModuleisPass)
  4525. {
  4526. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4527. #ifdef MODIFY_MODE_HANDSHAKE_STOP_PWM_WITHOUT_OK_STATE
  4528. if (Charger.m_bSafetyRegulationGB)
  4529. {
  4530. if(Charger.CP_State == SYSTEM_STATE_B || Charger.CP_State == SYSTEM_STATE_C)
  4531. {
  4532. CpTask_CtrlCpPwm_P0();
  4533. }
  4534. }
  4535. else
  4536. {
  4537. CpTask_CtrlCpPwm_P0();
  4538. }
  4539. #endif //MODIFY_MODE_HANDSHAKE_STOP_PWM_WITHOUT_OK_STATE
  4540. #else //MODIFY_CP_TASK_CTRL_CP_PWM
  4541. CpTask_CtrlCpPwm_P0();
  4542. #endif //MODIFY_CP_TASK_CTRL_CP_PWM
  4543. osDelay(300);
  4544. DEBUG_INFO("Leak module self test pass before charging.\r\n");
  4545. }
  4546. else
  4547. {
  4548. Charger.Alarm_Code |= ALARM_LEAK_MODULE_FAIL;
  4549. DEBUG_INFO("Leak module self test fail before charging.\r\n");
  4550. }
  4551. Charger.isTestLeakModule = OFF ;
  4552. }
  4553. if(Charger.CP_State == SYSTEM_STATE_C)
  4554. {
  4555. #ifdef MODIFY_MODE_HANDSHAKE_STOP_PWM_WITHOUT_OK_STATE
  4556. if (Charger.m_bSafetyRegulationGB)
  4557. {
  4558. Charger.m_bBlockCsuCpPwmDuty = HTK_DISABLE;
  4559. if (Charger.m_CP_CurPWM == PWM_DUTY_FULL)
  4560. {
  4561. CpTask_CtrlCpPwm_P0();
  4562. }
  4563. }
  4564. #endif
  4565. setChargerMode(MODE_CHARGING);
  4566. timerDisable(TIMER_IDX_CP);
  4567. if (Charger.isSetStartTime == ON)
  4568. {
  4569. Charger.cycle_info.startTimeTick = HAL_GetTick();
  4570. Charger.cycle_info.startDateTime.year = Charger.memory.EVSE_Config.data.item.SystemDateTime.year;
  4571. Charger.cycle_info.startDateTime.month = Charger.memory.EVSE_Config.data.item.SystemDateTime.month;
  4572. Charger.cycle_info.startDateTime.day = Charger.memory.EVSE_Config.data.item.SystemDateTime.day;
  4573. Charger.cycle_info.startDateTime.hour = Charger.memory.EVSE_Config.data.item.SystemDateTime.hour;
  4574. Charger.cycle_info.startDateTime.min = Charger.memory.EVSE_Config.data.item.SystemDateTime.min;
  4575. Charger.cycle_info.startDateTime.sec = Charger.memory.EVSE_Config.data.item.SystemDateTime.sec;
  4576. Charger.cycle_info.meterStart = Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative;
  4577. }
  4578. }
  4579. if(Charger.CP_State == SYSTEM_STATE_B)
  4580. {
  4581. #ifdef MODIFY_MODE_HANDSHAKE_STOP_PWM_WITHOUT_OK_STATE
  4582. if (Charger.m_bSafetyRegulationGB)
  4583. {
  4584. Charger.m_bBlockCsuCpPwmDuty = HTK_DISABLE;
  4585. if (Charger.m_CP_CurPWM == PWM_DUTY_FULL)
  4586. {
  4587. CpTask_CtrlCpPwm_P0();
  4588. }
  4589. }
  4590. #endif
  4591. Charger.isCP_in_B = ON ;
  4592. }
  4593. if(((Charger.CP_State == SYSTEM_STATE_A) && Charger.isCP_in_B) || !Charger.am3352.isRequestOn)
  4594. {
  4595. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL); // close pwm
  4596. setChargerMode(MODE_IDLE);
  4597. timerDisable(TIMER_IDX_CP);
  4598. }
  4599. // EV ready restart cp logic
  4600. if (Charger.memory.EVSE_Config.data.item.ModelName[3] == 'E')
  4601. {
  4602. if(timer[TIMER_IDX_SIMU_DISCONNECT].isAlarm == ON)
  4603. {
  4604. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_ZERO);
  4605. osDelay(4000);
  4606. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  4607. osDelay(600);
  4608. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4609. CpTask_CtrlCpPwm_P0();
  4610. #endif
  4611. timerDisable(TIMER_IDX_SIMU_DISCONNECT);
  4612. timer[TIMER_IDX_SIMU_DISCONNECT].isAlarm = OFF ;
  4613. }
  4614. }
  4615. #ifdef MODIFY_MODE_HANDSHAKE_STOP_PWM_WITHOUT_OK_STATE
  4616. if (Charger.m_bSafetyRegulationGB)
  4617. {
  4618. if (
  4619. (Charger.CP_State == SYSTEM_STATE_D || Charger.CP_State == SYSTEM_STATE_E) &&
  4620. (Charger.am3352.isRequestOn)
  4621. )
  4622. {
  4623. Charger.m_bBlockCsuCpPwmDuty = HTK_ENABLE;
  4624. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  4625. }
  4626. }
  4627. #endif
  4628. break;
  4629. case MODE_CHARGING:
  4630. if(isModeChange())
  4631. {
  4632. #ifdef MODIFY_CPTASK_CSU_0_1_CHARGE_MODE
  4633. CpTask_CtrlCpPwm_P0();
  4634. #endif
  4635. }
  4636. #ifdef MODIFY_AW_CP_TASK_CSU_0_1_CHARGING_MODE_STOP_WITHOUT_STATE_C
  4637. if(Charger.CP_State != SYSTEM_STATE_C || !Charger.am3352.isRequestOn)
  4638. #else
  4639. if(!Charger.am3352.isRequestOn)
  4640. #endif
  4641. {
  4642. setChargerMode(MODE_STOP);
  4643. if(Charger.CP_State != SYSTEM_STATE_C)
  4644. {
  4645. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4646. #ifdef MODIFY_MODE_CHARGING_STOP_PWM_WITHOUT_OK_STATE
  4647. if (Charger.m_bSafetyRegulationGB)
  4648. {
  4649. if (
  4650. (Charger.CP_State == SYSTEM_STATE_D || Charger.CP_State == SYSTEM_STATE_E) &&
  4651. (Charger.am3352.isRequestOn)
  4652. )
  4653. {
  4654. Charger.m_bBlockCsuCpPwmDuty = HTK_ENABLE;
  4655. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  4656. }
  4657. }
  4658. #endif //MODIFY_MODE_CHARGING_STOP_PWM_WITHOUT_OK_STATE
  4659. }
  4660. }
  4661. #ifdef FUNC_RELAY_OFF_WHEN_CSU_CMD_TIMEOUT
  4662. else if (HTK_IsTimeout(Charger.m_CSU_RxTick, CSU_CMD_TIMEOUT_SEC * 1000))
  4663. {
  4664. if (Charger.Relay_Action != GPIO_RELAY_ACTION_OFF)
  4665. {
  4666. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4667. XP("#<CSU_TIMEOUT> RELAY => OFF\r\n");
  4668. }
  4669. #ifdef FUNC_REQUEST_OFF_WHEN_CSU_CMD_TIMEOUT
  4670. if (Charger.am3352.isRequestOn != OFF)
  4671. {
  4672. Charger.am3352.isRequestOn = OFF;
  4673. XP("#<CSU_TIMEOUT> REQUEST => OFF\r\n");
  4674. }
  4675. #endif
  4676. }
  4677. #endif //FUNC_RELAY_OFF_WHEN_CSU_CMD_TIMEOUT
  4678. else
  4679. {
  4680. #ifdef USE_OLD_ONE_STEP_CHARGE_FOR_AW_NOODOE
  4681. Charger.Relay_Action = GPIO_RELAY_ACTION_ON;
  4682. #endif
  4683. duration_delta = (HAL_GetTick() - Charger.cycle_info.startTimeTick);
  4684. Charger.cycle_info.Duration = duration_delta;
  4685. Charger.cycle_info.Power_Consumption = Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative - Charger.cycle_info.meterStart;
  4686. }
  4687. break;
  4688. case MODE_STOP:
  4689. if(isModeChange())
  4690. {
  4691. Charger.isSetStartTime = OFF ;
  4692. Charger.isCP_in_B = OFF ;
  4693. if (!Charger.am3352.isRequestOn) //represent swip card stop charge in 6V
  4694. {
  4695. Charger.rfid.op_bits.reqStop = ON ;
  4696. }
  4697. }
  4698. if((Charger.CP_State == SYSTEM_STATE_A) || !Charger.am3352.isRequestOn)
  4699. {
  4700. if( !Charger.am3352.isRequestOn)
  4701. {
  4702. if (Charger.rfid.op_bits.reqStop == ON || (Charger.CP_State == SYSTEM_STATE_B)) //1. swip card stop charge in 6V. , 2.cp-> B then swip card stop charge
  4703. {
  4704. if(Charger.CP_State == SYSTEM_STATE_B)
  4705. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4706. Charger.rfid.op_bits.reqStop = OFF;
  4707. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL); // close pwm
  4708. timerEnable( TIMER_IDX_RFID_RELAY_OFF, 3000); //is set wait 3 sec
  4709. Charger.cycle_info.endTimeTick = HAL_GetTick();
  4710. recordChargingHis(OFF, END_STATUS_CODE_NORMAL);
  4711. Charger.isCP_in_B = OFF ;
  4712. }
  4713. if (timer[TIMER_IDX_RFID_RELAY_OFF].isAlarm == ON ) //is wait 3 sec
  4714. {
  4715. timerDisable(TIMER_IDX_RFID_RELAY_OFF);
  4716. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4717. #ifdef FUNC_BLOCK_CSU_CONFIG_CP_PWM_DUTY
  4718. Charger.m_bBlockCsuCpPwmDuty = HTK_DISABLE;
  4719. #endif
  4720. setChargerMode(MODE_IDLE);
  4721. Charger.cycle_info.StartType = START_METHOD_NONE;
  4722. }
  4723. }
  4724. else //CP_State == SYSTEM_STATE_A
  4725. {
  4726. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL); // close pwm
  4727. Charger.cycle_info.endTimeTick = HAL_GetTick();
  4728. recordChargingHis(OFF, END_STATUS_CODE_NORMAL);
  4729. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4730. #ifdef FUNC_BLOCK_CSU_CONFIG_CP_PWM_DUTY
  4731. Charger.m_bBlockCsuCpPwmDuty = HTK_DISABLE;
  4732. #endif
  4733. setChargerMode(MODE_IDLE);
  4734. Charger.isCP_in_B = OFF ;
  4735. }
  4736. }
  4737. else // Charger.am3352.isRequestOn == 1
  4738. {
  4739. if((Charger.CP_State == SYSTEM_STATE_B))
  4740. {
  4741. timerDisable(TIMER_IDX_RFID_RELAY_OFF);
  4742. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4743. Charger.isCP_in_B = ON ;
  4744. }
  4745. if((Charger.CP_State == SYSTEM_STATE_C) && (!Charger.am3352.isRequestOff) )
  4746. {
  4747. #ifdef FUNC_BLOCK_CSU_CONFIG_CP_PWM_DUTY
  4748. Charger.m_bBlockCsuCpPwmDuty = HTK_DISABLE;
  4749. #endif
  4750. #ifdef MODIFY_MODE_STOP_TO_CHARGE_PROCESS
  4751. setChargerMode(MODE_CHARGING);
  4752. #else
  4753. setChargerMode(MODE_HANDSHAKE);
  4754. #endif
  4755. }
  4756. #ifdef MODIFY_MODE_STOP_STOP_PWM_WITHOUT_OK_STATE
  4757. if (Charger.m_bSafetyRegulationGB)
  4758. {
  4759. if (
  4760. (Charger.CP_State == SYSTEM_STATE_D || Charger.CP_State == SYSTEM_STATE_E) &&
  4761. (Charger.am3352.isRequestOn)
  4762. )
  4763. {
  4764. Charger.m_bBlockCsuCpPwmDuty = HTK_ENABLE;
  4765. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  4766. }
  4767. if ((Charger.CP_State == SYSTEM_STATE_B) && (Charger.am3352.isRequestOn))
  4768. {
  4769. Charger.m_bBlockCsuCpPwmDuty = HTK_DISABLE;
  4770. if (Charger.m_CP_CurPWM == PWM_DUTY_FULL)
  4771. {
  4772. CpTask_CtrlCpPwm_P0();
  4773. }
  4774. }
  4775. }
  4776. #endif //MODIFY_MODE_STOP_STOP_PWM_WITHOUT_OK_STATE
  4777. }
  4778. break;
  4779. case MODE_ALARM:
  4780. if(isModeChange())
  4781. {
  4782. Charger.isSetStartTime = OFF ;
  4783. if (Charger.CSUisReady == ON )
  4784. {
  4785. //clear blink time , Prevent red light from being snatched by others
  4786. disblinkerTime (BLINKER_IDX_LED) ;
  4787. //clear led Action
  4788. Charger.am3352.LedActionState = 0 ;
  4789. }
  4790. }
  4791. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4792. if(Charger.Alarm_Code == 0x00)
  4793. {
  4794. Charger.am3352.LedActionState = 0 ;
  4795. if (Charger.Mode_Before_Alarm == MODE_CHARGING) // in charging mode
  4796. {
  4797. if(Charger.CP_State == SYSTEM_STATE_B ) // to handshake
  4798. {
  4799. setChargerMode(MODE_HANDSHAKE);
  4800. }
  4801. else // to idle
  4802. {
  4803. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  4804. setChargerMode(MODE_IDLE);
  4805. }
  4806. }
  4807. else if(Charger.Mode_Before_Alarm == MODE_STOP)
  4808. {
  4809. if (Charger.isCP_in_B == ON) // to handshake
  4810. {
  4811. setChargerMode(MODE_HANDSHAKE);
  4812. }
  4813. else // to stop
  4814. {
  4815. setChargerMode(MODE_STOP);
  4816. }
  4817. }
  4818. else // to idle
  4819. {
  4820. if(timer[TIMER_IDX_STATE_E].isAlarm == ON)
  4821. {
  4822. setChargerMode(MODE_IDLE);
  4823. }
  4824. }
  4825. }
  4826. break;
  4827. case MODE_DEBUG:
  4828. if(isModeChange())
  4829. {
  4830. setLedMotion(LED_ACTION_DEBUG);
  4831. Charger.Relay_Action = GPIO_RELAY_ACTION_ON;
  4832. timerEnable(TIMER_IDX_DEBUG, 1000);
  4833. }
  4834. if(timer[TIMER_IDX_DEBUG].isAlarm || (Charger.Alarm_Code>0))
  4835. {
  4836. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4837. Charger.isDebugModeCPtest = ON;
  4838. }
  4839. if(Charger.isDebugModeCPtest)
  4840. {
  4841. if(Charger.CP_State == SYSTEM_STATE_A)
  4842. {
  4843. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4844. Charger.memory.EVSE_Config.data.item.MaxChargingCurrent = Charger.maxRatingCurrent;
  4845. #ifdef MODIFY_AC_EVSE_STATUS_MAX_CHARGING_CURRENT_VARIABLE
  4846. Charger.AC_MaxChargingCurrentOrDuty = Charger.maxRatingCurrent;
  4847. #endif
  4848. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  4849. }
  4850. else if (Charger.CP_State == SYSTEM_STATE_B)
  4851. {
  4852. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF;
  4853. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4854. CpTask_CtrlCpPwm_P1();
  4855. #endif
  4856. }
  4857. else if (Charger.CP_State == SYSTEM_STATE_C)
  4858. {
  4859. Charger.Relay_Action = GPIO_RELAY_ACTION_ON;
  4860. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  4861. CpTask_CtrlCpPwm_P1();
  4862. #endif
  4863. }
  4864. }
  4865. break;
  4866. default:
  4867. setChargerMode(MODE_IDLE);
  4868. break;
  4869. }
  4870. }
  4871. else
  4872. {
  4873. /*By Pass Mode*/
  4874. }
  4875. osDelay(1);
  4876. }
  4877. /* USER CODE END StartCpTask */
  4878. }
  4879. /* USER CODE BEGIN Header_StartAlarmTask */
  4880. /**
  4881. * @brief Function implementing the alarmTask thread.
  4882. * @param argument: Not used
  4883. * @retval None
  4884. */
  4885. /* USER CODE END Header_StartAlarmTask */
  4886. void StartAlarmTask(void const * argument)
  4887. {
  4888. /* USER CODE BEGIN StartAlarmTask */
  4889. //aalarm
  4890. timerEnable(TIMER_IDX_PE_DETECT, 30000);
  4891. /* Infinite loop */
  4892. for(;;)
  4893. {
  4894. if(Charger.Mode != MODE_INITIAL && Charger.Mode != MODE_DEBUG)
  4895. {
  4896. recordAlarmHis();
  4897. //================================
  4898. // CP fail alarm detect
  4899. //================================
  4900. #ifdef CP_ALARM_PROTECT
  4901. if ((Charger.CP_State == SYSTEM_STATE_UNKNOWN)&&(!(Charger.Alarm_Code & ALARM_MCU_TESTFAIL)))
  4902. {
  4903. if(Charger.counter.CP.fail >1000)
  4904. {
  4905. if(!(Charger.Alarm_Code & ALARM_CP_ERROR))
  4906. {
  4907. Charger.counter.CP.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  4908. Charger.Alarm_Code |= ALARM_CP_ERROR;
  4909. DEBUG_ERROR("Alarm CP occur.\r\n");
  4910. }
  4911. }
  4912. else
  4913. Charger.counter.CP.fail++;
  4914. }
  4915. #ifdef MODIFY_ALARMTASK_CP_ERROR_RECOVER
  4916. else if (Charger.CP_State != SYSTEM_STATE_UNKNOWN)
  4917. #else
  4918. else if ((Charger.CP_State != SYSTEM_STATE_UNKNOWN)&&(Charger.CP_State!=SYSTEM_STATE_D)&&
  4919. (Charger.CP_State != SYSTEM_STATE_E)&&(Charger.CP_State != SYSTEM_STATE_F))
  4920. #endif
  4921. {
  4922. Charger.counter.CP.fail = 0;
  4923. if((Charger.Alarm_Code & ALARM_CP_ERROR))
  4924. {
  4925. osDelay(1000);
  4926. Charger.counter.CP.retry++;
  4927. Charger.Alarm_Code &= ~ALARM_CP_ERROR;
  4928. DEBUG_INFO("Alarm CP recover.\r\n");
  4929. }
  4930. }
  4931. #endif //CP_ALARM_PROTECT
  4932. //================================
  4933. // Over voltage alarm detect
  4934. //================================
  4935. #ifdef OVP_PROTECT
  4936. if(Charger.Voltage[0] > ALARM_SPEC_OV)
  4937. {
  4938. if(Charger.counter.OV.fail > 1000)
  4939. {
  4940. Charger.counter.OV.restore = 0 ;
  4941. if(!(Charger.Alarm_Code & ALARM_OVER_VOLTAGE))
  4942. {
  4943. Charger.Alarm_Code |= ALARM_OVER_VOLTAGE;
  4944. DEBUG_ERROR("Alarm OV occur.\r\n");
  4945. }
  4946. }
  4947. else
  4948. Charger.counter.OV.fail++;
  4949. }
  4950. else if(Charger.Voltage[0] < (ALARM_SPEC_OV-ALARM_SPEC_OUV_HYSTERESIS))
  4951. {
  4952. if((Charger.Alarm_Code & ALARM_OVER_VOLTAGE))
  4953. {
  4954. if (Charger.counter.OV.restore > 1000 && blinker[BLINKER_IDX_LED].blinkisFinish)
  4955. {
  4956. Charger.counter.OV.fail = 0 ;
  4957. DEBUG_INFO("Alarm OV is coldpickup ing..... \r\n");
  4958. Charger.counter.OV.isOccurInCharging = ((Charger.CP_State == SYSTEM_STATE_B)?ON:OFF);
  4959. if (Charger.counter.OV.isOccurInCharging)
  4960. {
  4961. #ifdef MODIFY_COLD_LOAD_PICKUP_DELAY
  4962. ColdLoadPickup_Delay("Alarm OV is coldpickup ing..... ");
  4963. #else
  4964. osDelay((rand()%175000)+5000);
  4965. #endif
  4966. }
  4967. Charger.Alarm_Code &= ~ALARM_OVER_VOLTAGE;
  4968. DEBUG_INFO("Alarm OV recover.\r\n");
  4969. }
  4970. else
  4971. {
  4972. Charger.counter.OV.restore++;
  4973. }
  4974. }
  4975. else
  4976. {
  4977. Charger.counter.OV.fail = 0 ;
  4978. Charger.counter.OV.restore = 0 ;
  4979. }
  4980. }
  4981. #endif //OVP_PROTECT
  4982. //================================
  4983. // Under voltage alarm detect
  4984. //================================
  4985. #ifdef UVP_PROTECT
  4986. if(Charger.Voltage[0] < ALARM_SPEC_UV)
  4987. {
  4988. if(Charger.counter.UV.fail > 1000)
  4989. {
  4990. Charger.counter.UV.restore = 0 ;
  4991. if(!(Charger.Alarm_Code & ALARM_UNDER_VOLTAGE))
  4992. {
  4993. Charger.Alarm_Code |= ALARM_UNDER_VOLTAGE;
  4994. DEBUG_ERROR("Alarm UV occur.\r\n");
  4995. }
  4996. }
  4997. else
  4998. Charger.counter.UV.fail++;
  4999. }
  5000. else if(Charger.Voltage[0] > (ALARM_SPEC_UV+ALARM_SPEC_OUV_HYSTERESIS))
  5001. {
  5002. if(Charger.Alarm_Code & ALARM_UNDER_VOLTAGE)
  5003. {
  5004. if (Charger.counter.UV.restore > 1000 && blinker[BLINKER_IDX_LED].blinkisFinish)
  5005. {
  5006. Charger.counter.UV.fail = 0 ;
  5007. DEBUG_INFO("Alarm UV is coldpickup ing..... \r\n");
  5008. Charger.counter.UV.isOccurInCharging = ((Charger.CP_State == SYSTEM_STATE_B)?ON:OFF);
  5009. if (Charger.counter.UV.isOccurInCharging)
  5010. {
  5011. #ifdef MODIFY_COLD_LOAD_PICKUP_DELAY
  5012. ColdLoadPickup_Delay("Alarm UV is coldpickup ing..... ");
  5013. #else
  5014. osDelay((rand()%175000)+5000);
  5015. #endif
  5016. }
  5017. Charger.Alarm_Code &= ~ALARM_UNDER_VOLTAGE;
  5018. DEBUG_INFO("Alarm UV recover.\r\n");
  5019. }
  5020. else
  5021. {
  5022. Charger.counter.UV.restore++ ;
  5023. }
  5024. }
  5025. else
  5026. {
  5027. Charger.counter.UV.restore = 0 ;
  5028. Charger.counter.UV.fail = 0 ;
  5029. }
  5030. }
  5031. #endif //UVP_PROTECT
  5032. //================================
  5033. // Over current alarm detect
  5034. //================================
  5035. #ifdef OCP_PROTECT
  5036. #ifdef FUNC_OCP_WITH_PP
  5037. {
  5038. uint16_t CurMaxCurr = 0; //A
  5039. uint16_t OC_BegThreshold = 0; //Occur: 0.01A
  5040. uint16_t OC_EndThreshold = 0; //Recover: 0.01A
  5041. CurMaxCurr = HTK_GET_VAL_MIN(Charger.memory.EVSE_Config.data.item.MaxChargingCurrent, Charger.maxRatingCurrent);
  5042. #ifdef FUNC_SWITCH_RELAY_WELDING_PROTECT
  5043. if (Charger.m_bSetOCP_32A == 1)
  5044. {
  5045. CurMaxCurr = 32;
  5046. }
  5047. #endif
  5048. //// if (Charger.m_bDetectPP)
  5049. //// {
  5050. //// CurMaxCurr = HTK_GET_VAL_MIN(CurMaxCurr, Charger.m_PPInfo.m_CurCurr);
  5051. //// }
  5052. #ifdef MODIFY_OCP_PROC_WITH_MODELNAME_IDX_3_EURO_SPEC
  5053. if (Charger.m_bModelNameWithEuroSpecOCP)
  5054. #else
  5055. if (Charger.memory.EVSE_Config.data.item.ModelName[3] == 'E')
  5056. #endif
  5057. {
  5058. OC_BegThreshold = (CurMaxCurr * Charger.OCP_Magnification) + 0;
  5059. OC_EndThreshold = (CurMaxCurr * Charger.OCP_Magnification) - 200;
  5060. }
  5061. else
  5062. {
  5063. if (CurMaxCurr > 20)
  5064. {
  5065. OC_BegThreshold = (CurMaxCurr * Charger.OCP_Magnification) + 0;
  5066. OC_EndThreshold = (CurMaxCurr * Charger.OCP_Magnification) - 200;
  5067. }
  5068. else
  5069. {
  5070. OC_BegThreshold = (CurMaxCurr * 100) + 200;
  5071. OC_EndThreshold = (CurMaxCurr * 100) + 0;
  5072. }
  5073. }
  5074. Charger.m_OCP_CurMaxCurr = CurMaxCurr;
  5075. Charger.m_OCP_BegThreshold = OC_BegThreshold;
  5076. Charger.m_OCP_EndThreshold = OC_EndThreshold;
  5077. //--------------------------------
  5078. // L1 Over current
  5079. if(Charger.Current[0] >= 4800)
  5080. {
  5081. if(Charger.counter.OC.fail > 5)
  5082. {
  5083. if(!(Charger.Alarm_Code & ALARM_CIRCUIT_SHORT))
  5084. {
  5085. Charger.counter.OC.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5086. Charger.Alarm_Code |= ALARM_CIRCUIT_SHORT;
  5087. if (Charger.counter.OC.isLatch == OFF)
  5088. {
  5089. Charger.counter.OC.isLatch = ON ;
  5090. setLedMotion(LED_ACTION_ALARM);
  5091. timerDisable(TIMER_IDX_RETRY_OC);
  5092. }
  5093. DEBUG_INFO("Correction Current[0]: %f\r\n", Charger.Current[0]/100.0);
  5094. DEBUG_ERROR("Alarm SCP occur.\r\n");
  5095. }
  5096. }
  5097. else
  5098. Charger.counter.OC.fail++;
  5099. }
  5100. //else if(Charger.Current[0] > (Charger.memory.EVSE_Config.data.item.MaxChargingCurrent>=Charger.maxRatingCurrent ? Charger.maxRatingCurrent*Charger.OCP_Magnification : Charger.memory.EVSE_Config.data.item.MaxChargingCurrent*Charger.OCP_Magnification))
  5101. else if (Charger.Current[0] > OC_BegThreshold)
  5102. {
  5103. if(Charger.counter.OC.fail > 6000)
  5104. {
  5105. if(!(Charger.Alarm_Code & ALARM_OVER_CURRENT))
  5106. {
  5107. Charger.counter.OC.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5108. Charger.Alarm_Code |= ALARM_OVER_CURRENT;
  5109. DEBUG_INFO("Correction Current[0]: %f\r\n", Charger.Current[0]/100.0);
  5110. DEBUG_ERROR("Alarm OC occur.\r\n");
  5111. timerEnable(TIMER_IDX_RETRY_OC, ALARM_RETRY_INTERVAL_OC);
  5112. Charger.counter.OC.retry++;
  5113. }
  5114. }
  5115. else
  5116. Charger.counter.OC.fail++;
  5117. }
  5118. //else if(Charger.Current[0] < (Charger.memory.EVSE_Config.data.item.MaxChargingCurrent>=Charger.maxRatingCurrent ? ((Charger.maxRatingCurrent*Charger.OCP_Magnification)-200) : ((Charger.memory.EVSE_Config.data.item.MaxChargingCurrent*Charger.OCP_Magnification)-200)))
  5119. else if (Charger.Current[0] < OC_EndThreshold)
  5120. {
  5121. Charger.counter.OC.fail = 0;
  5122. if ((Charger.counter.OC.retry > ALARM_OC_RETRY_COUNT))
  5123. {
  5124. if (Charger.counter.OC.isLatch == OFF)
  5125. {
  5126. Charger.counter.OC.isLatch = ON ;
  5127. setLedMotion(LED_ACTION_ALARM);
  5128. timerDisable(TIMER_IDX_RETRY_OC);
  5129. }
  5130. }
  5131. else if((Charger.Alarm_Code & ALARM_OVER_CURRENT) &&
  5132. (Charger.counter.OC.retry <= ALARM_OC_RETRY_COUNT) &&
  5133. (timer[TIMER_IDX_RETRY_OC].isAlarm == ON) &&
  5134. blinker[BLINKER_IDX_LED].blinkisFinish )
  5135. {
  5136. timerDisable(TIMER_IDX_RETRY_OC);
  5137. osDelay(1000);
  5138. Charger.Alarm_Code &= ~ALARM_OVER_CURRENT;
  5139. Charger.Alarm_Code &= ~ALARM_CIRCUIT_SHORT;
  5140. DEBUG_INFO("Alarm OC recover.\r\n");
  5141. }
  5142. }
  5143. }
  5144. #else //FUNC_OCP_WITH_PP
  5145. //Old Procedure (Deleted)
  5146. #endif //FUNC_OCP_WITH_PP
  5147. #endif //OCP_PROTECT
  5148. //================================
  5149. // Over temperature alarm detect
  5150. //================================
  5151. #ifdef OTP_PROTECT
  5152. if (Charger.temperature.SystemAmbientTemp > ALARM_SPEC_OT_2)
  5153. {
  5154. if(Charger.counter.OT.fail > 2000)
  5155. {
  5156. if (Charger.counter.OT.isLatch == OFF)
  5157. {
  5158. Charger.counter.OT.isLatch = ON ;
  5159. setLedMotion(LED_ACTION_ALARM);
  5160. }
  5161. //Charger.counter.OT.isLatch = ON ;
  5162. if(!(Charger.Alarm_Code & ALARM_OVER_TEMPERATURE))
  5163. {
  5164. Charger.counter.OT.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5165. Charger.Alarm_Code |= ALARM_OVER_TEMPERATURE;
  5166. DEBUG_ERROR("Latch OT2 occur.\r\n");
  5167. DEBUG_ERROR("Tmp = %d.\r\n",Charger.temperature.SystemAmbientTemp);
  5168. }
  5169. }
  5170. else
  5171. Charger.counter.OT.fail++;
  5172. }
  5173. else if((Charger.temperature.SystemAmbientTemp > ALARM_SPEC_OT_1) && (!Charger.counter.OT.isLatch))
  5174. {
  5175. if(Charger.counter.OT.fail > 1000)
  5176. {
  5177. if(!(Charger.Alarm_Code & ALARM_OVER_TEMPERATURE))
  5178. {
  5179. Charger.counter.OT.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5180. Charger.Alarm_Code |= ALARM_OVER_TEMPERATURE;
  5181. DEBUG_ERROR("Alarm OT occur.\r\n");
  5182. }
  5183. }
  5184. else
  5185. Charger.counter.OT.fail++;
  5186. }
  5187. else if((Charger.temperature.SystemAmbientTemp < (ALARM_SPEC_OT_1-ALARM_SPEC_OT_HYSTERESIS)) && (!Charger.counter.OT.isLatch))
  5188. {
  5189. Charger.counter.OT.fail = 0;
  5190. if((Charger.Alarm_Code & ALARM_OVER_TEMPERATURE) && blinker[BLINKER_IDX_LED].blinkisFinish)
  5191. {
  5192. Charger.counter.OT.retry++;
  5193. Charger.Alarm_Code &= ~ALARM_OVER_TEMPERATURE;
  5194. DEBUG_INFO("Alarm OT recover.\r\n");
  5195. }
  5196. }
  5197. /*
  5198. // For multi step OTP logic
  5199. if(Charger.temperature.SystemAmbientTemp > ALARM_SPEC_OT_2)
  5200. {
  5201. if(Charger.counter.OT.fail > 1000)
  5202. {
  5203. if(!(Charger.Alarm_Code & ALARM_OVER_TEMPERATURE))
  5204. {
  5205. Charger.counter.OT.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5206. Charger.Alarm_Code |= ALARM_OVER_TEMPERATURE;
  5207. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  5208. blinkerTimeSet(500, 500, 3000, 3);
  5209. }
  5210. DEBUG_ERROR("Alarm OT occur.\r\n");
  5211. }
  5212. }
  5213. else
  5214. Charger.counter.OT.fail++;
  5215. }
  5216. else if(Charger.temperature.SystemAmbientTemp > ALARM_SPEC_OT_1)
  5217. {
  5218. if(Charger.counter.OT.fail > 1000)
  5219. {
  5220. if(!timer[TIMER_IDX_RETRY_OT].isEnable || timer[TIMER_IDX_RETRY_OT].isAlarm)
  5221. {
  5222. Charger.memory.EVSE_Config.data.item.MaxChargingCurrent *= 0.75;
  5223. }
  5224. timerEnable(TIMER_IDX_RETRY_OT, ALARM_RETRY_INTERVAL_OT);
  5225. }
  5226. else
  5227. Charger.counter.OT.fail++;
  5228. }
  5229. else if(Charger.temperature.SystemAmbientTemp < (ALARM_SPEC_OT_1-ALARM_SPEC_OT_HYSTERESIS))
  5230. {
  5231. Charger.counter.OT.fail = 0;
  5232. if((Charger.Alarm_Code & ALARM_OVER_TEMPERATURE) &&
  5233. timer[TIMER_IDX_RETRY_OT].isAlarm)
  5234. {
  5235. Charger.memory.EVSE_Config.data.item.MaxChargingCurrent = Charger.maxRatingCurrent;
  5236. timerDisable(TIMER_IDX_RETRY_OT);
  5237. osDelay(3000);
  5238. Charger.counter.OT.retry++;
  5239. Charger.Alarm_Code &= ~ALARM_OVER_TEMPERATURE;
  5240. DEBUG_INFO("Alarm OT recover.\r\n");
  5241. }
  5242. }*/
  5243. #endif //OTP_PROTECT
  5244. #ifdef FUNC_EMP_FAN_CTRL_USE_LOCKER_IO
  5245. if (Charger.temperature.SystemAmbientTemp > 30)
  5246. {
  5247. if (GET_EMP_FAN == OFF)
  5248. {
  5249. DEBUG_ERROR("<SET_FAN_ON>\r\n");
  5250. SET_EMP_FAN(ON);
  5251. }
  5252. }
  5253. else if (Charger.temperature.SystemAmbientTemp < 25)
  5254. {
  5255. if (GET_EMP_FAN == ON)
  5256. {
  5257. DEBUG_ERROR("<SET_FAN_OFF>\r\n");
  5258. SET_EMP_FAN(OFF);
  5259. }
  5260. }
  5261. #endif //FUNC_EMP_FAN_CTRL_USE_LOCKER_IO
  5262. //================================
  5263. // Current leakage alarm detect
  5264. // TODO: ADC channel & spec need to check for other detection method
  5265. //================================
  5266. #ifdef CCID_PROTECT
  5267. #ifdef FUNC_SWITCH_RELAY_WELDING_PROTECT
  5268. if (Charger.m_bDisableCCID == 0 && ((HAL_GetTick() - Charger.m_BootTick) / 1000) > 30)
  5269. {
  5270. #endif
  5271. if (Charger.CCID_Module_Type == CCID_MODULE_VAC)
  5272. {
  5273. //module test
  5274. if((HAL_GPIO_ReadPin(IN_Leak_Error_GPIO_Port, IN_Leak_Error_Pin) == GPIO_PIN_SET) && (!Charger.isTestLeakModule))
  5275. {
  5276. if(Charger.counter.LEAK.fail > 1000)
  5277. {
  5278. if(!(Charger.Alarm_Code & ALARM_LEAK_MODULE_FAIL))
  5279. {
  5280. Charger.counter.LEAK.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5281. Charger.Alarm_Code |= ALARM_LEAK_MODULE_FAIL;
  5282. DEBUG_ERROR("Alarm leakage module error occur.\r\n");
  5283. timerEnable(TIMER_IDX_RETRY_LEAK, ALARM_RETRY_INTERVAL_LEAK);
  5284. }
  5285. }
  5286. else
  5287. Charger.counter.LEAK.fail++;
  5288. }
  5289. else
  5290. {
  5291. Charger.counter.LEAK.fail = 0;
  5292. if((Charger.Alarm_Code & ALARM_LEAK_MODULE_FAIL) &&
  5293. (Charger.counter.LEAK.retry < ALARM_RETRY_COUNT) &&
  5294. (timer[TIMER_IDX_RETRY_LEAK].isAlarm == ON) &&
  5295. blinker[BLINKER_IDX_LED].blinkisFinish)
  5296. {
  5297. timerDisable(TIMER_IDX_RETRY_LEAK);
  5298. Charger.Alarm_Code &= ~ALARM_LEAK_MODULE_FAIL;
  5299. osDelay(1000);
  5300. #ifdef DEBUG
  5301. DEBUG_INFO("Alarm leakage module error recover.\r\n");
  5302. #endif
  5303. }
  5304. }
  5305. //DC leakage
  5306. if((HAL_GPIO_ReadPin(IN_Leak_DC_GPIO_Port, IN_Leak_DC_Pin) == GPIO_PIN_SET) && (!Charger.isTestLeakModule))
  5307. {
  5308. if(Charger.counter.LEAK_DC.fail > 10)
  5309. {
  5310. if(!(Charger.Alarm_Code & ALARM_CURRENT_LEAK_DC))
  5311. {
  5312. Charger.counter.LEAK_DC.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5313. Charger.Alarm_Code |= ALARM_CURRENT_LEAK_DC;
  5314. DEBUG_ERROR("Alarm DC leakage occur.\r\n");
  5315. timerEnable(TIMER_IDX_RETRY_LEAK_DC, ALARM_RETRY_INTERVAL_LEAK);
  5316. Charger.counter.LEAK_DC.retry++;
  5317. }
  5318. if (timer[TIMER_IDX_RETRY_LEAK_DC].isAlarm == ON || (Charger.counter.LEAK_DC.retry >ALARM_RETRY_COUNT))
  5319. {
  5320. timerDisable(TIMER_IDX_RETRY_LEAK_DC);
  5321. //Charger.counter.LEAK.isLatch = ON ;
  5322. if (Charger.counter.LEAK.isLatch == OFF)
  5323. {
  5324. Charger.counter.LEAK.isLatch = ON ;
  5325. setLedMotion(LED_ACTION_ALARM);
  5326. }
  5327. DEBUG_ERROR("Alarm leakage DC isLatch.\r\n");
  5328. }
  5329. }
  5330. else
  5331. Charger.counter.LEAK_DC.fail++;
  5332. }
  5333. else
  5334. {
  5335. Charger.counter.LEAK_DC.fail = 0;
  5336. if((Charger.Alarm_Code & ALARM_CURRENT_LEAK_DC) &&
  5337. (Charger.counter.LEAK_DC.retry <= ALARM_RETRY_COUNT) &&
  5338. (timer[TIMER_IDX_RETRY_LEAK_DC].isAlarm == ON) &&
  5339. blinker[BLINKER_IDX_LED].blinkisFinish)
  5340. {
  5341. timerDisable(TIMER_IDX_RETRY_LEAK_DC);
  5342. Charger.Alarm_Code &= ~ALARM_CURRENT_LEAK_DC;
  5343. DEBUG_INFO("Alarm DC leakage recover.\r\n");
  5344. }
  5345. }
  5346. //AC leakage
  5347. if((HAL_GPIO_ReadPin(IN_Leak_AC_GPIO_Port, IN_Leak_AC_Pin) == GPIO_PIN_SET) && (!Charger.isTestLeakModule))
  5348. {
  5349. if(Charger.counter.LEAK_AC.fail > 10)
  5350. {
  5351. if(!(Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC))
  5352. {
  5353. Charger.counter.LEAK_AC.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5354. Charger.Alarm_Code |= ALARM_CURRENT_LEAK_AC;
  5355. DEBUG_ERROR("Alarm AC leakage occur.\r\n");
  5356. timerEnable(TIMER_IDX_RETRY_LEAK_AC, ALARM_RETRY_INTERVAL_LEAK);
  5357. Charger.counter.LEAK_AC.retry++;
  5358. }
  5359. if (timer[TIMER_IDX_RETRY_LEAK_AC].isAlarm == ON || (Charger.counter.LEAK_AC.retry > ALARM_RETRY_COUNT))
  5360. {
  5361. timerDisable(TIMER_IDX_RETRY_LEAK_AC);
  5362. //Charger.counter.LEAK.isLatch = ON ;
  5363. if (Charger.counter.LEAK.isLatch == OFF)
  5364. {
  5365. Charger.counter.LEAK.isLatch = ON ;
  5366. setLedMotion(LED_ACTION_ALARM);
  5367. }
  5368. DEBUG_ERROR("Alarm leakage AC isLatch.\r\n");
  5369. }
  5370. }
  5371. else
  5372. Charger.counter.LEAK_AC.fail++;
  5373. }
  5374. else
  5375. {
  5376. Charger.counter.LEAK_AC.fail = 0;
  5377. if((Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC) &&
  5378. (Charger.counter.LEAK_AC.retry <= ALARM_RETRY_COUNT) &&
  5379. (timer[TIMER_IDX_RETRY_LEAK_AC].isAlarm == ON) &&
  5380. blinker[BLINKER_IDX_LED].blinkisFinish )
  5381. {
  5382. timerDisable(TIMER_IDX_RETRY_LEAK_AC);
  5383. Charger.Alarm_Code &= ~ALARM_CURRENT_LEAK_AC;
  5384. DEBUG_INFO("Alarm AC leakage recover.\r\n");
  5385. }
  5386. }
  5387. }
  5388. else // Charger.CCID_Module_Type is CCID_MODULE_CORMEX
  5389. {
  5390. if((Charger.Leak_Current >= Charger.alarm_spec.Current_LEAK_AC) && (!Charger.isTestLeakModule)) // adc correction data to check
  5391. {
  5392. if(Charger.counter.LEAK.fail >= 5)
  5393. {
  5394. if(!(Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC))
  5395. {
  5396. #ifdef FUNC_MODIFY_LEAK_AC_DISPLAY
  5397. XP("#Leak_Current (%d >= %d)\r\n", Charger.Leak_Current, Charger.alarm_spec.Current_LEAK_AC);
  5398. #else
  5399. XP("************************************************************\r\n");
  5400. XP("adc_value.ADC2_IN6_GF.value = %d\r\n", adc_value.ADC2_IN6_GF.value);
  5401. XP("Charger.Leak_Current(mA) = %d\r\n", Charger.Leak_Current);
  5402. XP("Charger.alarm_spec.Current_LEAK_AC(mA) = %d\r\n", Charger.alarm_spec.Current_LEAK_AC);
  5403. XP("Charger.isTestLeakModule = %s\r\n", Charger.isTestLeakModule ? "ON" : "OFF");
  5404. XP("************************************************************\r\n");
  5405. #endif
  5406. Charger.counter.LEAK.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5407. Charger.Alarm_Code |= ALARM_CURRENT_LEAK_AC; //Hao##### CSU Charge error here
  5408. DEBUG_ERROR("Alarm leakage occur.\r\n");
  5409. timerEnable(TIMER_IDX_RETRY_LEAK_AC, ALARM_RETRY_INTERVAL_LEAK);
  5410. Charger.counter.LEAK.retry ++ ;
  5411. }
  5412. }
  5413. else
  5414. {
  5415. Charger.counter.LEAK.fail++;
  5416. }
  5417. }
  5418. else if (Charger.Leak_Current < (Charger.alarm_spec.Current_LEAK_AC - ALARM_SPEC_CURRENT_LEAK_HYSTERESIS)) // adc correction data to check
  5419. {
  5420. Charger.counter.LEAK.fail = 0;
  5421. if (Charger.counter.LEAK.retry > ALARM_LEAK_RETRY_COUNT)
  5422. {
  5423. if (Charger.counter.LEAK.isLatch == OFF)
  5424. {
  5425. Charger.counter.LEAK.isLatch = ON ;
  5426. setLedMotion(LED_ACTION_ALARM);
  5427. }
  5428. }
  5429. else
  5430. {
  5431. if(((Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC)||(Charger.Alarm_Code & ALARM_CURRENT_LEAK_DC)) &&
  5432. (Charger.counter.LEAK.retry <= ALARM_LEAK_RETRY_COUNT) &&
  5433. (timer[TIMER_IDX_RETRY_LEAK_AC].isAlarm == ON) &&
  5434. blinker[BLINKER_IDX_LED].blinkisFinish )
  5435. {
  5436. timerDisable(TIMER_IDX_RETRY_LEAK_AC);
  5437. osDelay(1000);
  5438. Charger.Alarm_Code &= ~ALARM_CURRENT_LEAK_AC;
  5439. DEBUG_INFO("Alarm leakage recover.\r\n");
  5440. }
  5441. }
  5442. }
  5443. }
  5444. #ifdef FUNC_SWITCH_RELAY_WELDING_PROTECT
  5445. }
  5446. #endif
  5447. #endif //CCID_PROTECT
  5448. //================================
  5449. // Ground fail alarm detect
  5450. // TODO: Spec need to check
  5451. //================================
  5452. #ifdef GROUND_FAULT_PROTECT
  5453. if(Charger.GroundingDetect==ON){
  5454. #ifdef FUNC_RELAXSPEC_GROUND_FAULT_PROTECT
  5455. static double GF_SPEC_OCCUR = 0;
  5456. static double GF_SPEC_RECOVER = 0;
  5457. if (GF_SPEC_OCCUR == 0 || GF_SPEC_RECOVER == 0)
  5458. {
  5459. GF_SPEC_OCCUR = 3.0 / 220.0; //GF_SPEC_OCCUR = 0.013636(3.0)
  5460. GF_SPEC_RECOVER = 2.0 / 220.0; //GF_SPEC_RECOVER = 0.009091(2.0)
  5461. XP("GF_SPEC_OCCUR = %lf, GF_SPEC_RECOVER = %lf\r\n", GF_SPEC_OCCUR, GF_SPEC_RECOVER);
  5462. }
  5463. #endif
  5464. if (timer[TIMER_IDX_PE_DETECT].isAlarm == ON) // after 30 sec check (GMI adc_value > spec) and (GMI adc_value <= 1 )
  5465. {
  5466. #ifdef FUNC_RELAXSPEC_GROUND_FAULT_PROTECT
  5467. if(((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) > (Charger.Voltage[0]*GF_SPEC_OCCUR)) || (adc_value.ADC3_IN4_GMI_VL1.value <= 1))
  5468. #else
  5469. if(((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) > (Charger.Voltage[0]*0.011)) || (adc_value.ADC3_IN4_GMI_VL1.value <= 1))
  5470. #endif
  5471. {
  5472. if(Charger.counter.GF.fail > 4000)
  5473. {
  5474. if(!(Charger.Alarm_Code & ALARM_GROUND_FAIL))
  5475. {
  5476. Charger.counter.GF.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5477. Charger.Alarm_Code |= ALARM_GROUND_FAIL;
  5478. DEBUG_ERROR("Alarm GF occur.\r\n");
  5479. }
  5480. }
  5481. else
  5482. Charger.counter.GF.fail++;
  5483. }
  5484. #ifdef FUNC_RELAXSPEC_GROUND_FAULT_PROTECT
  5485. else if((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) < (Charger.Voltage[0]*GF_SPEC_RECOVER))
  5486. #else
  5487. else if((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) < (Charger.Voltage[0]*0.00566))
  5488. #endif
  5489. {
  5490. Charger.counter.GF.fail = 0;
  5491. if((Charger.Alarm_Code & ALARM_GROUND_FAIL) && blinker[BLINKER_IDX_LED].blinkisFinish)
  5492. {
  5493. //osDelay(3000);
  5494. Charger.counter.GF.retry++;
  5495. Charger.Alarm_Code &= ~ALARM_GROUND_FAIL;
  5496. DEBUG_INFO("Alarm GF recover.\r\n");
  5497. }
  5498. }
  5499. }
  5500. else // before 30 sec check (GMI adc_value > spec)
  5501. {
  5502. #ifdef FUNC_RELAXSPEC_GROUND_FAULT_PROTECT
  5503. if((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) > (Charger.Voltage[0]*GF_SPEC_OCCUR))
  5504. #else
  5505. if((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) > (Charger.Voltage[0]*0.011))
  5506. #endif
  5507. {
  5508. if(Charger.counter.GF.fail > 4000)
  5509. {
  5510. if(!(Charger.Alarm_Code & ALARM_GROUND_FAIL))
  5511. {
  5512. Charger.counter.GF.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5513. Charger.Alarm_Code |= ALARM_GROUND_FAIL;
  5514. DEBUG_ERROR("Alarm GF occur.\r\n");
  5515. }
  5516. }
  5517. else
  5518. Charger.counter.GF.fail++;
  5519. }
  5520. #ifdef FUNC_RELAXSPEC_GROUND_FAULT_PROTECT
  5521. else if((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) < (Charger.Voltage[0]*GF_SPEC_RECOVER))
  5522. #else
  5523. else if((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) < (Charger.Voltage[0]*0.00566))
  5524. #endif
  5525. {
  5526. Charger.counter.GF.fail = 0;
  5527. if((Charger.Alarm_Code & ALARM_GROUND_FAIL) && blinker[BLINKER_IDX_LED].blinkisFinish)
  5528. {
  5529. //osDelay(3000);
  5530. Charger.counter.GF.retry++;
  5531. Charger.Alarm_Code &= ~ALARM_GROUND_FAIL;
  5532. DEBUG_INFO("Alarm GF recover.\r\n");
  5533. }
  5534. }
  5535. }
  5536. }
  5537. #endif //GROUND_FAULT_PROTECT
  5538. //================================
  5539. // Relay status error detect
  5540. // TODO: ADC channel & spec need to check
  5541. //================================
  5542. #ifdef RELAY_WELDING_PROTECT
  5543. #ifdef FUNC_SWITCH_RELAY_WELDING_PROTECT
  5544. if (Charger.m_bDisableRelayWelding == 0 && ((HAL_GetTick() - Charger.m_BootTick) / 1000) > 30)
  5545. {
  5546. #endif
  5547. //GROUNGING_SYSTEM_LL
  5548. if (Charger.GroundingSystem == GROUNGING_SYSTEM_LL)
  5549. {
  5550. if((Charger.Relay_Action == GPIO_RELAY_ACTION_ON) && (Charger.Relay_isOperationCompleted == 0x11 ) && ((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) < (Charger.Voltage[0]*0.011)) )
  5551. {
  5552. if(adc_value.ADC2_IN5_Welding.value < ((Charger.Voltage[0]*0.0048)+3)) // for UL dloule L syste a=0.0048 b=0.03V
  5553. {
  5554. if(Charger.counter.RELAY_Drive_Fualt.fail > 1000)
  5555. {
  5556. if(!(Charger.Alarm_Code & ALARM_RELAY_DRIVE_FUALT))
  5557. {
  5558. Charger.counter.RELAY.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5559. Charger.Alarm_Code |= ALARM_RELAY_DRIVE_FUALT;
  5560. DEBUG_ERROR("Alarm Welding Voltage = %d.\r\n",adc_value.ADC2_IN5_Welding.value);
  5561. DEBUG_ERROR("Alarm relay ON status fault occur.\r\n");
  5562. }
  5563. }
  5564. else
  5565. Charger.counter.RELAY_Drive_Fualt.fail++;
  5566. }
  5567. else
  5568. {
  5569. Charger.counter.RELAY_Drive_Fualt.fail = 0 ;
  5570. }
  5571. }
  5572. else if ((Charger.Relay_Action == GPIO_RELAY_ACTION_OFF) && (Charger.Relay_isOperationCompleted == 0x00) && ((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) < (Charger.Voltage[0]*0.011)) )
  5573. {
  5574. //if(adc_value.ADC2_IN5_Welding.value > 24) // 0.24 V
  5575. if(adc_value.ADC2_IN5_Welding.value > ALARM_SPEC_RELAY_LL)
  5576. {
  5577. if(Charger.counter.RELAY.fail > 1000)
  5578. {
  5579. if(!(Charger.Alarm_Code & ALARM_RELAY_STATUS))
  5580. {
  5581. Charger.counter.RELAY.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5582. Charger.Alarm_Code |= ALARM_RELAY_STATUS;
  5583. DEBUG_ERROR("Alarm Welding Voltage = %d.\r\n",adc_value.ADC2_IN5_Welding.value);
  5584. DEBUG_ERROR("Alarm relay OFF status fault occur.\r\n");
  5585. }
  5586. }
  5587. else
  5588. Charger.counter.RELAY.fail++;
  5589. }
  5590. else
  5591. {
  5592. Charger.counter.RELAY.fail = 0 ;
  5593. }
  5594. }
  5595. }
  5596. else // GROUNGING_SYSTEM_LN
  5597. {
  5598. if((Charger.Relay_Action == GPIO_RELAY_ACTION_ON) && (Charger.Relay_isOperationCompleted == 0x11) && ((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) < (Charger.Voltage[0]*0.011)) )
  5599. {
  5600. if(adc_value.ADC2_IN5_Welding.value < ALARM_SPEC_RELAY_LN_DRIVE_FAULT ) // old spec use
  5601. {
  5602. if(Charger.counter.RELAY.fail > 1000)
  5603. {
  5604. if(!(Charger.Alarm_Code & ALARM_RELAY_DRIVE_FUALT))
  5605. {
  5606. Charger.counter.RELAY.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5607. Charger.Alarm_Code |= ALARM_RELAY_DRIVE_FUALT;
  5608. DEBUG_ERROR("Alarm Welding Voltage = %d.\r\n",adc_value.ADC2_IN5_Welding.value);
  5609. DEBUG_ERROR("Alarm relay ON status fault occur.\r\n");
  5610. }
  5611. }
  5612. else
  5613. Charger.counter.RELAY.fail++;
  5614. }
  5615. else
  5616. {
  5617. Charger.counter.RELAY.fail = 0 ;
  5618. }
  5619. }
  5620. else if ((Charger.Relay_Action == GPIO_RELAY_ACTION_OFF) && (Charger.Relay_isOperationCompleted == 0x00) && ((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) < (Charger.Voltage[0]*0.011)) )
  5621. {
  5622. if(adc_value.ADC2_IN5_Welding.value > ALARM_SPEC_RELAY_LN_WELDING)
  5623. {
  5624. if(Charger.counter.RELAY.fail > 1000)
  5625. {
  5626. if(!(Charger.Alarm_Code & ALARM_RELAY_STATUS))
  5627. {
  5628. Charger.counter.RELAY.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5629. Charger.Alarm_Code |= ALARM_RELAY_STATUS;
  5630. DEBUG_ERROR("Alarm Welding Voltage = %d.\r\n",adc_value.ADC2_IN5_Welding.value);
  5631. DEBUG_ERROR("Alarm relay OFF status fault occur.\r\n");
  5632. }
  5633. }
  5634. else
  5635. Charger.counter.RELAY.fail++;
  5636. }
  5637. else
  5638. {
  5639. Charger.counter.RELAY.fail = 0 ;
  5640. }
  5641. }
  5642. }
  5643. #ifdef FUNC_SWITCH_RELAY_WELDING_PROTECT
  5644. }
  5645. #endif
  5646. #endif //RELAY_WELDING_PROTECT
  5647. //================================
  5648. // Emergency stop alarm detect
  5649. //================================
  5650. #ifdef EMC_BUTTON_PROTECT
  5651. if(HAL_GPIO_ReadPin(IN_Emergency_GPIO_Port, IN_Emergency_Pin) == GPIO_PIN_RESET)
  5652. {
  5653. if(Charger.counter.EMO.fail > 100)
  5654. {
  5655. if(!(Charger.Alarm_Code & ALARM_EMERGENCY_STOP))
  5656. {
  5657. Charger.counter.EMO.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5658. Charger.Alarm_Code |= ALARM_EMERGENCY_STOP;
  5659. DEBUG_ERROR("Alarm EMO occur.\r\n");
  5660. }
  5661. }
  5662. else
  5663. Charger.counter.EMO.fail++;
  5664. }
  5665. else
  5666. {
  5667. Charger.counter.EMO.fail = 0;
  5668. if((Charger.Alarm_Code & ALARM_EMERGENCY_STOP) && blinker[BLINKER_IDX_LED].blinkisFinish)
  5669. {
  5670. Charger.counter.EMO.retry++;
  5671. Charger.Alarm_Code &= ~ALARM_EMERGENCY_STOP;
  5672. DEBUG_INFO("Alarm EMO recover.\r\n");
  5673. }
  5674. }
  5675. #endif //EMC_BUTTON_PROTECT
  5676. //================================
  5677. // Handshaking timeout detect
  5678. //================================
  5679. #ifdef HANDSHAKE_PROTECT
  5680. if(timer[TIMER_IDX_CP].isAlarm == ON)
  5681. {
  5682. if(!(Charger.Alarm_Code & ALARM_HANDSHAKE_TIMEOUT))
  5683. {
  5684. Charger.Alarm_Code |= ALARM_HANDSHAKE_TIMEOUT;
  5685. timerDisable(TIMER_IDX_CP);
  5686. Charger.rfid.op_bits.reqStart = OFF;
  5687. Charger.istoHandShakeMode = OFF ;
  5688. DEBUG_ERROR("Alarm handshake overtime occur.\r\n");
  5689. }
  5690. }
  5691. else
  5692. {
  5693. if((Charger.Alarm_Code & ALARM_HANDSHAKE_TIMEOUT))
  5694. {
  5695. osDelay(10000);
  5696. Charger.Alarm_Code &= ~ALARM_HANDSHAKE_TIMEOUT;
  5697. DEBUG_INFO("Alarm handshake overtime recover.\r\n");
  5698. }
  5699. }
  5700. #endif //HANDSHAKE_PROTECT
  5701. //================================
  5702. // Rotary Switch Maxium Current Error detect
  5703. //================================
  5704. #ifdef ROTATE_SWITCH_CHECK
  5705. if (Charger.isRotarySwitchError)
  5706. {
  5707. if(!(Charger.Alarm_Code & ALARM_ROTATORY_SWITCH_FAULT))
  5708. {
  5709. Charger.Alarm_Code |= ALARM_ROTATORY_SWITCH_FAULT;
  5710. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  5711. blinkerTimeSet(BLINKER_IDX_LED, 200, 200, 0, 1);
  5712. }
  5713. DEBUG_INFO("Rotary Switch Maxium Current Error occur.\r\n");
  5714. }
  5715. }
  5716. #endif //ROTATE_SWITCH_CHECK
  5717. //================================
  5718. // Recover alarm info in state A
  5719. //================================
  5720. if(Charger.CP_State == SYSTEM_STATE_A)
  5721. {
  5722. //SCP Recover
  5723. if(Charger.Alarm_Code & ALARM_CIRCUIT_SHORT)
  5724. {
  5725. Charger.Alarm_Code &= ~ALARM_CIRCUIT_SHORT;
  5726. DEBUG_INFO("Alarm SCP recover.\r\n");
  5727. }
  5728. //OCP Recover
  5729. if(Charger.Alarm_Code & ALARM_OVER_CURRENT)
  5730. {
  5731. Charger.Alarm_Code &= ~ALARM_OVER_CURRENT;
  5732. DEBUG_INFO("Alarm OC recover.\r\n");
  5733. }
  5734. Charger.counter.OC.fail = 0 ;
  5735. timerDisable(TIMER_IDX_RETRY_OC);
  5736. Charger.counter.OC.retry = 0 ; // clear retry times
  5737. Charger.counter.OC.isLatch = OFF ;
  5738. //CCID Recover
  5739. if (Charger.CCID_Module_Type == CCID_MODULE_VAC)
  5740. {
  5741. if ((HAL_GPIO_ReadPin(IN_Leak_AC_GPIO_Port, IN_Leak_AC_Pin) == GPIO_PIN_RESET) && (Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC))
  5742. {
  5743. Charger.Alarm_Code &= ~ALARM_CURRENT_LEAK_AC;
  5744. DEBUG_INFO("Alarm leakage AC recover for VAC.\r\n");
  5745. }
  5746. if ((HAL_GPIO_ReadPin(IN_Leak_DC_GPIO_Port, IN_Leak_DC_Pin) == GPIO_PIN_RESET) && (Charger.Alarm_Code & ALARM_CURRENT_LEAK_DC))
  5747. {
  5748. Charger.Alarm_Code &= ~ALARM_CURRENT_LEAK_DC;
  5749. DEBUG_INFO("Alarm leakage DC recover for VAC.\r\n");
  5750. }
  5751. }
  5752. else //Charger.CCID_Module_Type is CCID_MODULE_CORMEX
  5753. {
  5754. #ifdef MODIFY_CCID_MODULE_CORMEX_RECOVER_AC_DC
  5755. if (Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC)
  5756. {
  5757. Charger.Alarm_Code &= ~ALARM_CURRENT_LEAK_AC;
  5758. DEBUG_INFO("Alarm leakage AC recover.\r\n");
  5759. }
  5760. if (Charger.Alarm_Code & ALARM_CURRENT_LEAK_DC)
  5761. {
  5762. Charger.Alarm_Code &= ~ALARM_CURRENT_LEAK_DC;
  5763. DEBUG_INFO("Alarm leakage DC recover.\r\n");
  5764. }
  5765. #else //MODIFY_CCID_MODULE_CORMEX_RECOVER_AC_DC
  5766. if((Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC)||(Charger.Alarm_Code & ALARM_CURRENT_LEAK_DC))
  5767. {
  5768. Charger.Alarm_Code &= ~ALARM_CURRENT_LEAK_AC;
  5769. DEBUG_INFO("Alarm leakage recover.\r\n");
  5770. }
  5771. #endif //MODIFY_CCID_MODULE_CORMEX_RECOVER_AC_DC
  5772. }
  5773. Charger.counter.LEAK.fail = 0;
  5774. timerDisable(TIMER_IDX_RETRY_LEAK) ;
  5775. timerDisable(TIMER_IDX_RETRY_LEAK_AC) ;
  5776. timerDisable(TIMER_IDX_RETRY_LEAK_DC) ;
  5777. Charger.counter.LEAK.retry = 0 ; // clear latch times
  5778. Charger.counter.LEAK_DC.retry = 0 ; // clear latch times
  5779. Charger.counter.LEAK_AC.retry = 0 ; // clear latch times
  5780. Charger.counter.LEAK.isLatch = OFF ;
  5781. //LEAK_MODULE selfrest Recover
  5782. if (Charger.CCID_Module_Type == CCID_MODULE_VAC)
  5783. {
  5784. if(Charger.Alarm_Code & ALARM_LEAK_MODULE_FAIL & (HAL_GPIO_ReadPin(IN_Leak_Error_GPIO_Port, IN_Leak_Error_Pin) == GPIO_PIN_RESET)) // leak module is VAC
  5785. {
  5786. if ((Charger.counter.LEAK_MODULE.retry < 2) && (!Charger.counter.LEAK_MODULE.isLatch))
  5787. {
  5788. Charger.Alarm_Code &= ~ALARM_LEAK_MODULE_FAIL;
  5789. DEBUG_INFO("Alarm leakage module recover.\r\n");
  5790. }
  5791. }
  5792. }
  5793. else //Charger.CCID_Module_Type is CCID_MODULE_CORMEX
  5794. {
  5795. if(Charger.Alarm_Code & ALARM_LEAK_MODULE_FAIL) // leak module is CORMEX
  5796. {
  5797. if ((Charger.counter.LEAK_MODULE.retry < 2) && (!Charger.counter.LEAK_MODULE.isLatch))
  5798. {
  5799. Charger.Alarm_Code &= ~ALARM_LEAK_MODULE_FAIL;
  5800. DEBUG_INFO("Alarm leakage module recover.\r\n");
  5801. }
  5802. }
  5803. }
  5804. }
  5805. }
  5806. else if (Charger.Mode == MODE_DEBUG)
  5807. {
  5808. //================================
  5809. // Relay status error detect
  5810. // TODO: ADC channel & spec need to check
  5811. //================================
  5812. #ifdef RELAY_WELDING_PROTECT
  5813. //GROUNGING_SYSTEM_LL
  5814. if (Charger.GroundingSystem == GROUNGING_SYSTEM_LL)
  5815. {
  5816. if((Charger.Relay_Action == GPIO_RELAY_ACTION_ON) && (Charger.Relay_isOperationCompleted == 0x11 ) && ((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) < (Charger.Voltage[0]*0.011)) )
  5817. {
  5818. if(adc_value.ADC2_IN5_Welding.value < ((Charger.Voltage[0]*0.0048)+3)) // for UL dloule L syste a=0.0048 b=0.03V
  5819. {
  5820. if(Charger.counter.RELAY_Drive_Fualt.fail > 1000)
  5821. {
  5822. if(!(Charger.Alarm_Code & ALARM_RELAY_DRIVE_FUALT))
  5823. {
  5824. Charger.counter.RELAY.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5825. Charger.Alarm_Code |= ALARM_RELAY_DRIVE_FUALT;
  5826. DEBUG_ERROR("Alarm Welding Voltage = %d.\r\n",adc_value.ADC2_IN5_Welding.value);
  5827. DEBUG_ERROR("Alarm relay ON status fault occur.\r\n");
  5828. }
  5829. }
  5830. else
  5831. Charger.counter.RELAY_Drive_Fualt.fail++;
  5832. }
  5833. else
  5834. {
  5835. Charger.counter.RELAY_Drive_Fualt.fail = 0 ;
  5836. }
  5837. }
  5838. else if ((Charger.Relay_Action == GPIO_RELAY_ACTION_OFF) && (Charger.Relay_isOperationCompleted == 0x00) && ((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) < (Charger.Voltage[0]*0.011)) )
  5839. {
  5840. //if(adc_value.ADC2_IN5_Welding.value > 24) // 0.24 V
  5841. if(adc_value.ADC2_IN5_Welding.value > ALARM_SPEC_RELAY_LL)
  5842. {
  5843. if(Charger.counter.RELAY.fail > 1000)
  5844. {
  5845. if(!(Charger.Alarm_Code & ALARM_RELAY_STATUS))
  5846. {
  5847. Charger.counter.RELAY.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5848. Charger.Alarm_Code |= ALARM_RELAY_STATUS;
  5849. DEBUG_ERROR("Alarm Welding Voltage = %d.\r\n",adc_value.ADC2_IN5_Welding.value);
  5850. DEBUG_ERROR("Alarm relay OFF status fault occur.\r\n");
  5851. }
  5852. }
  5853. else
  5854. Charger.counter.RELAY.fail++;
  5855. }
  5856. else
  5857. {
  5858. Charger.counter.RELAY.fail = 0 ;
  5859. }
  5860. }
  5861. }
  5862. else // GROUNGING_SYSTEM_LN
  5863. {
  5864. if((Charger.Relay_Action == GPIO_RELAY_ACTION_ON) && (Charger.Relay_isOperationCompleted == 0x11) && ((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) < (Charger.Voltage[0]*0.011)) )
  5865. {
  5866. if(adc_value.ADC2_IN5_Welding.value < ALARM_SPEC_RELAY_LN_DRIVE_FAULT ) // old spec use
  5867. {
  5868. if(Charger.counter.RELAY.fail > 1000)
  5869. {
  5870. if(!(Charger.Alarm_Code & ALARM_RELAY_DRIVE_FUALT))
  5871. {
  5872. Charger.counter.RELAY.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5873. Charger.Alarm_Code |= ALARM_RELAY_DRIVE_FUALT;
  5874. DEBUG_ERROR("Alarm Welding Voltage = %d.\r\n",adc_value.ADC2_IN5_Welding.value);
  5875. DEBUG_ERROR("Alarm relay ON status fault occur.\r\n");
  5876. }
  5877. }
  5878. else
  5879. Charger.counter.RELAY.fail++;
  5880. }
  5881. else
  5882. {
  5883. Charger.counter.RELAY.fail = 0 ;
  5884. }
  5885. }
  5886. else if ((Charger.Relay_Action == GPIO_RELAY_ACTION_OFF) && (Charger.Relay_isOperationCompleted == 0x00) && ((adc_value.ADC3_IN4_GMI_VL1.value*100*3.3/4095) < (Charger.Voltage[0]*0.011)) )
  5887. {
  5888. if(adc_value.ADC2_IN5_Welding.value > ALARM_SPEC_RELAY_LN_WELDING)
  5889. {
  5890. if(Charger.counter.RELAY.fail > 1000)
  5891. {
  5892. if(!(Charger.Alarm_Code & ALARM_RELAY_STATUS))
  5893. {
  5894. Charger.counter.RELAY.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  5895. Charger.Alarm_Code |= ALARM_RELAY_STATUS;
  5896. DEBUG_ERROR("Alarm Welding Voltage = %d.\r\n",adc_value.ADC2_IN5_Welding.value);
  5897. DEBUG_ERROR("Alarm relay OFF status fault occur.\r\n");
  5898. }
  5899. }
  5900. else
  5901. Charger.counter.RELAY.fail++;
  5902. }
  5903. else
  5904. {
  5905. Charger.counter.RELAY.fail = 0 ;
  5906. }
  5907. }
  5908. }
  5909. #endif //RELAY_WELDING_PROTECT
  5910. }
  5911. osDelay(1);
  5912. }
  5913. /* USER CODE END StartAlarmTask */
  5914. }
  5915. /* USER CODE BEGIN Header_StartBleTask */
  5916. /**
  5917. * @brief Function implementing the bleTask thread.
  5918. * @param argument: Not used
  5919. * @retval None
  5920. */
  5921. /* USER CODE END Header_StartBleTask */
  5922. void StartBleTask(void const * argument)
  5923. {
  5924. /* USER CODE BEGIN StartBleTask */
  5925. char tmpBuf[128];
  5926. uint8_t endFlag[4]={0x55,0xaa,0x55,0xaa};
  5927. __IO uint32_t flash;
  5928. uint32_t checksum;
  5929. uint32_t rndNumber;
  5930. //=====================================
  5931. // Module reset
  5932. //=====================================
  5933. HAL_GPIO_WritePin(OUT_BLE_RESET_GPIO_Port, OUT_BLE_RESET_Pin, GPIO_PIN_RESET);
  5934. osDelay(60);
  5935. HAL_GPIO_WritePin(OUT_BLE_RESET_GPIO_Port, OUT_BLE_RESET_Pin, GPIO_PIN_SET);
  5936. HAL_GPIO_WritePin(OUT_BLE_DSR_GPIO_Port, OUT_BLE_DSR_Pin, GPIO_PIN_SET);
  5937. Charger.ble.initSeq = 0;
  5938. Charger.ble.isDataMode = OFF;
  5939. UART_BLE_Init_OK = OFF;
  5940. osDelay(1000);
  5941. /* Infinite loop */
  5942. for(;;)
  5943. {
  5944. //=====================================
  5945. // BLE module initialization process
  5946. //=====================================
  5947. switch(Charger.ble.initSeq)
  5948. {
  5949. case 0x00:
  5950. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_RESTORE], strlen(BLE_CMD[BLE_CMD_SET_RESTORE]), 0xffff);
  5951. osDelay(BLE_INIT_DELAY);
  5952. break;
  5953. case 0x01:
  5954. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_ROLE], strlen(BLE_CMD[BLE_CMD_SET_ROLE]), 0xffff);
  5955. osDelay(BLE_INIT_DELAY);
  5956. break;
  5957. case 0x02:
  5958. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_DTR], strlen(BLE_CMD[BLE_CMD_SET_DTR]), 0xffff);
  5959. osDelay(BLE_INIT_DELAY);
  5960. break;
  5961. case 0x03:
  5962. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_DSR], strlen(BLE_CMD[BLE_CMD_SET_DSR]), 0xffff);
  5963. osDelay(BLE_INIT_DELAY);
  5964. break;
  5965. case 0x04:
  5966. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_PAIRING_MODE], strlen(BLE_CMD[BLE_CMD_SET_PAIRING_MODE]), 0xffff);
  5967. osDelay(BLE_INIT_DELAY);
  5968. break;
  5969. case 0x05:
  5970. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_SECURITY_MODE], strlen(BLE_CMD[BLE_CMD_SET_SECURITY_MODE]), 0xffff);
  5971. osDelay(BLE_INIT_DELAY);
  5972. break;
  5973. case 0x06:
  5974. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_SECURITY_TYPE], strlen(BLE_CMD[BLE_CMD_SET_SECURITY_TYPE]), 0xffff);
  5975. osDelay(BLE_INIT_DELAY);
  5976. break;
  5977. case 0x07:
  5978. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_DISCOVERABILITY], strlen(BLE_CMD[BLE_CMD_SET_DISCOVERABILITY]), 0xffff);
  5979. osDelay(BLE_INIT_DELAY);
  5980. break;
  5981. case 0x08:
  5982. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_CONNECTABILITY], strlen(BLE_CMD[BLE_CMD_SET_CONNECTABILITY]), 0xffff);
  5983. osDelay(BLE_INIT_DELAY);
  5984. break;
  5985. case 0x09:
  5986. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_GET_ADDR], strlen(BLE_CMD[BLE_CMD_GET_ADDR]), 0xffff);
  5987. osDelay(BLE_INIT_DELAY);
  5988. break;
  5989. case 0x0a:
  5990. sprintf(tmpBuf, "%s\"BYTON_EVSE_%s\"\r\n", BLE_CMD[BLE_CMD_SET_NAME], Charger.ble.peripheral_mac);
  5991. DEBUG_INFO("BLE module set name:BYTON_EVSE_%s\r\n", Charger.ble.peripheral_mac);
  5992. HAL_UART_Transmit(&BLE_USART, (uint8_t *)tmpBuf, strlen(tmpBuf), 0xffff);
  5993. osDelay(BLE_INIT_DELAY);
  5994. break;
  5995. case 0x0b:
  5996. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_MAX_POWER], strlen(BLE_CMD[BLE_CMD_SET_MAX_POWER]), 0xffff);
  5997. osDelay(BLE_INIT_DELAY);
  5998. break;
  5999. case 0x0c:
  6000. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_MIN_INTERVAL], strlen(BLE_CMD[BLE_CMD_SET_MIN_INTERVAL]), 0xffff);
  6001. osDelay(BLE_INIT_DELAY);
  6002. break;
  6003. case 0x0d:
  6004. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_MAX_INTERVAL], strlen(BLE_CMD[BLE_CMD_SET_MAX_INTERVAL]), 0xffff);
  6005. osDelay(BLE_INIT_DELAY);
  6006. break;
  6007. case 0x0e:
  6008. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_TX_PHY], strlen(BLE_CMD[BLE_CMD_SET_TX_PHY]), 0xffff);
  6009. osDelay(BLE_INIT_DELAY);
  6010. break;
  6011. case 0x0f:
  6012. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_RX_PHY], strlen(BLE_CMD[BLE_CMD_SET_RX_PHY]), 0xffff);
  6013. osDelay(BLE_INIT_DELAY);
  6014. break;
  6015. case 0x10:
  6016. HAL_UART_Transmit(&BLE_USART, (uint8_t *)BLE_CMD[BLE_CMD_SET_DATA_MODE], strlen(BLE_CMD[BLE_CMD_SET_DATA_MODE]), 0xffff);
  6017. DEBUG_INFO("BLE module switch to data mode.\r\n");
  6018. osDelay(BLE_INIT_DELAY);
  6019. default:
  6020. UART_BLE_Init_OK = ON;
  6021. break;
  6022. }
  6023. //=====================================
  6024. // BLE central device connect status
  6025. //=====================================
  6026. if((HAL_GPIO_ReadPin(IN_BLE_DTR_GPIO_Port, IN_BLE_DTR_Pin) == GPIO_PIN_SET) && (UART_BLE_Init_OK == ON))
  6027. {
  6028. // Device disconnect
  6029. timerRefresh(TIMER_IDX_BLE);
  6030. if(Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT || Charger.ble.loginRole == BLE_LOGIN_ROLE_USER)
  6031. {
  6032. DEBUG_INFO("BLE central device disconnect.\r\n.");
  6033. Charger.ble.loginRole = BLE_LOGIN_ROLE_UNKOWN;
  6034. }
  6035. }
  6036. else if((HAL_GPIO_ReadPin(IN_BLE_DTR_GPIO_Port, IN_BLE_DTR_Pin) == GPIO_PIN_RESET) && (UART_BLE_Init_OK == ON))
  6037. {
  6038. // Device connect in
  6039. if(Charger.ble.loginRole == BLE_LOGIN_ROLE_UNKOWN)
  6040. {
  6041. DEBUG_INFO("BLE central device connect.....wait sign in.\r\n.");
  6042. osDelay(1000);
  6043. }
  6044. }
  6045. //=====================================
  6046. // Communication timeout process
  6047. //=====================================
  6048. if(timer[TIMER_IDX_BLE].isAlarm)
  6049. {
  6050. Charger.ble.loginRole = BLE_LOGIN_ROLE_UNKOWN;
  6051. DEBUG_INFO("BLE no communication over time, disconnect central device.\r\n");
  6052. timerRefresh(TIMER_IDX_BLE);
  6053. HAL_GPIO_WritePin(OUT_BLE_DSR_GPIO_Port, OUT_BLE_DSR_Pin, GPIO_PIN_RESET);
  6054. osDelay(100);
  6055. HAL_GPIO_WritePin(OUT_BLE_DSR_GPIO_Port, OUT_BLE_DSR_Pin, GPIO_PIN_SET);
  6056. }
  6057. //=====================================
  6058. // BLE uart recieve data process
  6059. //=====================================
  6060. if(UART_BLE_recv_end_flag == ON)
  6061. {
  6062. if((HAL_GPIO_ReadPin(IN_BLE_DTR_GPIO_Port, IN_BLE_DTR_Pin) == GPIO_PIN_SET) || !Charger.ble.isDataMode)
  6063. {
  6064. // AT command rx process
  6065. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_RESTORE])!=NULL)
  6066. {
  6067. Charger.ble.initSeq++;
  6068. }
  6069. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_ROLE])!=NULL)
  6070. {
  6071. Charger.ble.initSeq++;
  6072. }
  6073. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_DTR])!=NULL)
  6074. {
  6075. Charger.ble.initSeq++;
  6076. }
  6077. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_DSR])!=NULL)
  6078. {
  6079. Charger.ble.initSeq++;
  6080. }
  6081. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_PAIRING_MODE])!=NULL)
  6082. {
  6083. Charger.ble.initSeq++;
  6084. }
  6085. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_SECURITY_MODE])!=NULL)
  6086. {
  6087. Charger.ble.initSeq++;
  6088. }
  6089. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_SECURITY_TYPE])!=NULL)
  6090. {
  6091. Charger.ble.initSeq++;
  6092. }
  6093. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_DISCOVERABILITY])!=NULL)
  6094. {
  6095. Charger.ble.initSeq++;
  6096. }
  6097. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_CONNECTABILITY])!=NULL)
  6098. {
  6099. Charger.ble.initSeq++;
  6100. }
  6101. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_GET_ADDR])!=NULL)
  6102. {
  6103. memcpy(Charger.ble.peripheral_mac, &UART_BLE_rx_buffer[strcspn((char *)UART_BLE_rx_buffer, ":")+1], 12);
  6104. Charger.ble.peripheral_mac[12] = '\0';
  6105. DEBUG_INFO("Peripheral mac address: %s\r\n", Charger.ble.peripheral_mac);
  6106. Charger.ble.initSeq++;
  6107. }
  6108. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_NAME])!=NULL)
  6109. {
  6110. Charger.ble.initSeq++;
  6111. }
  6112. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_MAX_POWER])!=NULL)
  6113. {
  6114. Charger.ble.initSeq++;
  6115. }
  6116. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_MIN_INTERVAL])!=NULL)
  6117. {
  6118. Charger.ble.initSeq++;
  6119. }
  6120. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_MAX_INTERVAL])!=NULL)
  6121. {
  6122. Charger.ble.initSeq++;
  6123. }
  6124. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_TX_PHY])!=NULL)
  6125. {
  6126. Charger.ble.initSeq++;
  6127. }
  6128. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_RX_PHY])!=NULL)
  6129. {
  6130. Charger.ble.initSeq++;
  6131. }
  6132. if(strstr((char *)UART_BLE_rx_buffer, BLE_CMD[BLE_CMD_SET_DATA_MODE])!=NULL)
  6133. {
  6134. Charger.ble.isDataMode = ON;
  6135. Charger.ble.initSeq++;
  6136. }
  6137. }
  6138. else
  6139. {
  6140. // Application protocol rx process
  6141. uint8_t tx[UART_BUFFER_SIZE];
  6142. uint8_t tx_len;
  6143. uint8_t chksum = 0;
  6144. uint32_t tmp;
  6145. //HTK_ByteArray2HexStr_XP("BLE_RX: ", UART_BLE_rx_buffer, 0, 16);
  6146. if(isValidCheckSum_BLE() && (rndNumber != ((UART_BLE_rx_buffer[3]<<0) | (UART_BLE_rx_buffer[4]<<8) | (UART_BLE_rx_buffer[5]<<16) | (UART_BLE_rx_buffer[6]<<24))))
  6147. {
  6148. // Timer reset
  6149. timerRefresh(TIMER_IDX_BLE);
  6150. rndNumber = ((UART_BLE_rx_buffer[3]<<0) | (UART_BLE_rx_buffer[4]<<8) | (UART_BLE_rx_buffer[5]<<16) | (UART_BLE_rx_buffer[6]<<24));
  6151. switch(UART_BLE_rx_buffer[2])
  6152. {
  6153. case BLE_PROTOCOL_MESSAGE_SIGN_IN:
  6154. DEBUG_INFO("BLE device sign request...%d\r\n", UART_BLE_rx_buffer[7]);
  6155. tx_len = 5;
  6156. tx[0] = (tx_len>>8)&0xff;
  6157. tx[1] = (tx_len&0xff);
  6158. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6159. tx[3] = 0x00;
  6160. if(Charger.memory.EVSE_Config.data.item.bleConfig.isRegPuk)
  6161. {
  6162. if(UART_BLE_rx_buffer[7] == 0x00)
  6163. {
  6164. if(memcmp(&UART_BLE_rx_buffer[8], &Charger.memory.EVSE_Config.data.item.bleConfig.idRoot[0], ((UART_BLE_rx_buffer[0]<<8) | (UART_BLE_rx_buffer[1]<<0)-9)) == 0)
  6165. {
  6166. // Root sign in
  6167. tx[3] = 0x02;
  6168. Charger.ble.loginRole = BLE_LOGIN_ROLE_ROOT;
  6169. DEBUG_INFO("Sign in as root.\r\n");
  6170. }
  6171. }
  6172. else
  6173. {
  6174. if(memcmp(&UART_BLE_rx_buffer[8], &Charger.memory.whiteList.data.item[0].pin[0], ((UART_BLE_rx_buffer[0]<<8) | (UART_BLE_rx_buffer[1]<<0)-9)) == 0)
  6175. {
  6176. // User sign in
  6177. tx[3] = 0x03;
  6178. Charger.ble.loginRole = BLE_LOGIN_ROLE_USER;
  6179. DEBUG_INFO("Sign in as user.\r\n");
  6180. }
  6181. }
  6182. }
  6183. else
  6184. {
  6185. if(UART_BLE_rx_buffer[7] == 0x00)
  6186. {
  6187. // Visitor
  6188. tx[3] = 0x01;
  6189. Charger.ble.loginRole = BLE_LOGIN_ROLE_VISITOR;
  6190. memcpy(&Charger.ble.loginId[0], &UART_BLE_rx_buffer[8], ((UART_BLE_rx_buffer[0]<<8) | (UART_BLE_rx_buffer[1]<<0)-9) );
  6191. DEBUG_WARN("Sign in as visitor.\r\n");
  6192. }
  6193. }
  6194. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6195. {
  6196. chksum ^= tx[2 + idx];
  6197. }
  6198. tx[4] = chksum;
  6199. break;
  6200. case BLE_PROTOCOL_MESSAGE_PUK_REG:
  6201. DEBUG_INFO("BLE device PUK registe.%02x%02x%02x%02x%02x%02x%02x%02x\r\n", UART_BLE_rx_buffer[7], UART_BLE_rx_buffer[8], UART_BLE_rx_buffer[9], UART_BLE_rx_buffer[10], UART_BLE_rx_buffer[11], UART_BLE_rx_buffer[12], UART_BLE_rx_buffer[13], UART_BLE_rx_buffer[14]);
  6202. tx_len = 11;
  6203. tx[0] = (tx_len>>8)&0xff;
  6204. tx[1] = (tx_len&0xff);
  6205. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6206. tx[3] = 0x00;
  6207. if(Charger.memory.EVSE_Config.data.item.bleConfig.isRegPuk)
  6208. {
  6209. if(memcmp(&Charger.ble.loginId[0], &Charger.memory.EVSE_Config.data.item.bleConfig.idRoot[0], ARRAY_SIZE(Charger.ble.loginId)))
  6210. {
  6211. tx[3] = 0x02;
  6212. DEBUG_INFO("ID registered exist.\r\n");
  6213. }
  6214. }
  6215. else
  6216. {
  6217. if(Charger.ble.loginRole == BLE_LOGIN_ROLE_VISITOR)
  6218. {
  6219. if(memcmp(&UART_BLE_rx_buffer[7], &Charger.memory.EVSE_Config.data.item.bleConfig.puk[0], 8) == 0x00)
  6220. {
  6221. tx[3] = 0x01;
  6222. // PIN list generation
  6223. Charger.memory.EVSE_Config.data.item.bleConfig.isGenPin = ON;
  6224. for(uint8_t idx=0;idx<20;idx++)
  6225. {
  6226. Charger.memory.whiteList.data.item[idx].listType = WHITE_LIST_TYPE_BLE;
  6227. Charger.memory.whiteList.data.item[idx].pin[0] = (rand()%0x2b)+0x30;
  6228. Charger.memory.whiteList.data.item[idx].pin[1] = (rand()%0x2b)+0x30;
  6229. Charger.memory.whiteList.data.item[idx].pin[2] = (rand()%0x2b)+0x30;
  6230. Charger.memory.whiteList.data.item[idx].pin[3] = (rand()%0x2b)+0x30;
  6231. Charger.memory.whiteList.data.item[idx].pin[4] = (rand()%0x2b)+0x30;
  6232. Charger.memory.whiteList.data.item[idx].pin[5] = (rand()%0x2b)+0x30;
  6233. Charger.memory.whiteList.data.item[idx].isReg = OFF;
  6234. }
  6235. Charger.memory.whiteList.op_bits.update = ON;
  6236. Charger.memory.EVSE_Config.data.item.bleConfig.isRegPuk = ON;
  6237. memcpy(&Charger.memory.EVSE_Config.data.item.bleConfig.idRoot[0], &Charger.ble.loginId[0], ARRAY_SIZE(Charger.ble.loginId));
  6238. Charger.memory.EVSE_Config.op_bits.update = ON;
  6239. DEBUG_INFO("Root registered and PIN list generate success.\r\n");
  6240. }
  6241. }
  6242. }
  6243. memcpy(&tx[4], &UART_BLE_rx_buffer[7], 6);
  6244. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6245. {
  6246. chksum ^= tx[2 + idx];
  6247. }
  6248. tx[10] = chksum;
  6249. break;
  6250. case BLE_PROTOCOL_MESSAGE_PIN_GET:
  6251. DEBUG_INFO("PIN get request.\r\n");
  6252. tx_len = 11;
  6253. tx[0] = (tx_len>>8)&0xff;
  6254. tx[1] = (tx_len&0xff);
  6255. tx[2] = UART_BLE_rx_buffer[2] + 0x80;;
  6256. tx[3] = 0x00;
  6257. if(Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT)
  6258. {
  6259. tx[3] = 0x01;
  6260. memcpy(&tx[4], &Charger.memory.whiteList.data.item[0].pin[0], 6);
  6261. DEBUG_INFO("PIN: %s\r\n", Charger.memory.whiteList.data.item[0].pin);
  6262. }
  6263. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6264. {
  6265. chksum ^= tx[2 + idx];
  6266. }
  6267. tx[10] = chksum;
  6268. break;
  6269. case BLE_PROTOCOL_MESSAGE_PIN_RENEW:
  6270. DEBUG_INFO("BLE device PIN renew request.\r\n");
  6271. tx_len = 13;
  6272. tx[0] = (tx_len>>8)&0xff;
  6273. tx[1] = (tx_len&0xff);
  6274. tx[2] = UART_BLE_rx_buffer[2] + 0x80;;
  6275. tx[3] = 0x00;
  6276. if((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT))
  6277. {
  6278. for(uint8_t idx=0;idx<20;idx++)
  6279. {
  6280. Charger.memory.whiteList.data.item[idx].listType = WHITE_LIST_TYPE_BLE;
  6281. Charger.memory.whiteList.data.item[idx].pin[0] = (rand()%0x2b)+0x30;
  6282. Charger.memory.whiteList.data.item[idx].pin[1] = (rand()%0x2b)+0x30;
  6283. Charger.memory.whiteList.data.item[idx].pin[2] = (rand()%0x2b)+0x30;
  6284. Charger.memory.whiteList.data.item[idx].pin[3] = (rand()%0x2b)+0x30;
  6285. Charger.memory.whiteList.data.item[idx].pin[4] = (rand()%0x2b)+0x30;
  6286. Charger.memory.whiteList.data.item[idx].pin[5] = (rand()%0x2b)+0x30;
  6287. Charger.memory.whiteList.data.item[idx].isReg = OFF;
  6288. }
  6289. Charger.memory.whiteList.op_bits.update = ON;
  6290. tx[3] = 0x01;
  6291. memcpy(&tx[4], &Charger.memory.whiteList.data.item[0].pin[0], 6);
  6292. DEBUG_INFO("PIN list renew: %s.\r\n", Charger.memory.whiteList.data.item[0].pin);
  6293. }
  6294. else
  6295. DEBUG_WARN("Login id not root.\r\n");
  6296. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6297. {
  6298. chksum ^= tx[2 + idx];
  6299. }
  6300. tx[4] = chksum;
  6301. break;
  6302. case BLE_PROTOCOL_MESSAGE_CHARGING_START:
  6303. DEBUG_INFO("BLE device request charging start.\r\n");
  6304. tx_len = 0x08;
  6305. tx[0] = (tx_len>>8)&0xff;
  6306. tx[1] = (tx_len&0xff);
  6307. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6308. if(Charger.Alarm_Code>0)
  6309. {
  6310. tx[3] = 0x02;
  6311. if((Charger.Alarm_Code & ALARM_OVER_VOLTAGE))
  6312. {
  6313. tx[4] = 0x01;
  6314. tx[5] = 0x22;
  6315. tx[6] = 0x00;
  6316. }
  6317. else if((Charger.Alarm_Code & ALARM_UNDER_VOLTAGE))
  6318. {
  6319. tx[4] = 0x01;
  6320. tx[5] = 0x22;
  6321. tx[6] = 0x03;
  6322. }
  6323. else if((Charger.Alarm_Code & ALARM_OVER_CURRENT))
  6324. {
  6325. tx[4] = 0x01;
  6326. tx[5] = 0x21;
  6327. tx[6] = 0x16;
  6328. }
  6329. else if((Charger.Alarm_Code & ALARM_OVER_TEMPERATURE))
  6330. {
  6331. tx[4] = 0x01;
  6332. tx[5] = 0x22;
  6333. tx[6] = 0x23;
  6334. }
  6335. else if((Charger.Alarm_Code & ALARM_GROUND_FAIL))
  6336. {
  6337. tx[4] = 0x01;
  6338. tx[5] = 0x22;
  6339. tx[6] = 0x56;
  6340. }
  6341. else if((Charger.Alarm_Code & ALARM_CP_ERROR))
  6342. {
  6343. tx[4] = 0x02;
  6344. tx[5] = 0x37;
  6345. tx[6] = 0x03;
  6346. }
  6347. else if((Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC)||(Charger.Alarm_Code & ALARM_CURRENT_LEAK_DC))
  6348. {
  6349. tx[4] = 0x01;
  6350. tx[5] = 0x22;
  6351. tx[6] = 0x33;
  6352. }
  6353. else if((Charger.Alarm_Code & ALARM_MCU_TESTFAIL))
  6354. {
  6355. tx[4] = 0x01;
  6356. tx[5] = 0x22;
  6357. tx[6] = 0x57;
  6358. }
  6359. else if((Charger.Alarm_Code & ALARM_EMERGENCY_STOP))
  6360. {
  6361. tx[4] = 0x01;
  6362. tx[5] = 0x22;
  6363. tx[6] = 0x51;
  6364. }
  6365. else if((Charger.Alarm_Code & ALARM_LEAK_MODULE_FAIL))
  6366. {
  6367. tx[4] = 0x01;
  6368. tx[5] = 0x10;
  6369. tx[6] = 0x04;
  6370. }
  6371. else if((Charger.Alarm_Code & ALARM_RELAY_STATUS))
  6372. {
  6373. tx[4] = 0x01;
  6374. tx[5] = 0x22;
  6375. tx[6] = 0x58;
  6376. }
  6377. DEBUG_WARN("EVSE in alarm state, can not start charging.\r\n");
  6378. }
  6379. else
  6380. {
  6381. if((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))
  6382. {
  6383. if(Charger.Mode == MODE_IDLE)
  6384. {
  6385. Charger.ble.isRequestOn = ON;
  6386. tx[3] = 0x01;
  6387. DEBUG_INFO("Start OK.\r\n");
  6388. }
  6389. else
  6390. {
  6391. tx[3] = 0x00;
  6392. DEBUG_WARN("EVSE not in IDLE.\r\n");
  6393. }
  6394. }
  6395. else
  6396. {
  6397. tx[3] = 0x00;
  6398. DEBUG_WARN("Login first\r\n");
  6399. }
  6400. tx[4] = 0x00;
  6401. tx[5] = 0x00;
  6402. tx[6] = 0x00;
  6403. }
  6404. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6405. {
  6406. chksum ^= tx[2 + idx];
  6407. }
  6408. tx[7] = chksum;
  6409. break;
  6410. case BLE_PROTOCOL_MESSAGE_CHARGING_STOP:
  6411. DEBUG_INFO("BLE device request charging stop.\r\n");
  6412. tx_len = 0x08;
  6413. tx[0] = (tx_len>>8)&0xff;
  6414. tx[1] = (tx_len&0xff);
  6415. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6416. if(Charger.Alarm_Code>0)
  6417. {
  6418. tx[3] = 0x02;
  6419. if((Charger.Alarm_Code & ALARM_OVER_VOLTAGE))
  6420. {
  6421. tx[4] = 0x01;
  6422. tx[5] = 0x22;
  6423. tx[6] = 0x00;
  6424. }
  6425. else if((Charger.Alarm_Code & ALARM_UNDER_VOLTAGE))
  6426. {
  6427. tx[4] = 0x01;
  6428. tx[5] = 0x22;
  6429. tx[6] = 0x03;
  6430. }
  6431. else if((Charger.Alarm_Code & ALARM_OVER_CURRENT))
  6432. {
  6433. tx[4] = 0x01;
  6434. tx[5] = 0x21;
  6435. tx[6] = 0x16;
  6436. }
  6437. else if((Charger.Alarm_Code & ALARM_OVER_TEMPERATURE))
  6438. {
  6439. tx[4] = 0x01;
  6440. tx[5] = 0x22;
  6441. tx[6] = 0x23;
  6442. }
  6443. else if((Charger.Alarm_Code & ALARM_GROUND_FAIL))
  6444. {
  6445. tx[4] = 0x01;
  6446. tx[5] = 0x22;
  6447. tx[6] = 0x56;
  6448. }
  6449. else if((Charger.Alarm_Code & ALARM_CP_ERROR))
  6450. {
  6451. tx[4] = 0x02;
  6452. tx[5] = 0x37;
  6453. tx[6] = 0x03;
  6454. }
  6455. else if((Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC)||(Charger.Alarm_Code & ALARM_CURRENT_LEAK_DC))
  6456. {
  6457. tx[4] = 0x01;
  6458. tx[5] = 0x22;
  6459. tx[6] = 0x33;
  6460. }
  6461. else if((Charger.Alarm_Code & ALARM_MCU_TESTFAIL))
  6462. {
  6463. tx[4] = 0x01;
  6464. tx[5] = 0x22;
  6465. tx[6] = 0x57;
  6466. }
  6467. else if((Charger.Alarm_Code & ALARM_EMERGENCY_STOP))
  6468. {
  6469. tx[4] = 0x01;
  6470. tx[5] = 0x22;
  6471. tx[6] = 0x51;
  6472. }
  6473. else if((Charger.Alarm_Code & ALARM_LEAK_MODULE_FAIL))
  6474. {
  6475. tx[4] = 0x01;
  6476. tx[5] = 0x10;
  6477. tx[6] = 0x04;
  6478. }
  6479. else if((Charger.Alarm_Code & ALARM_RELAY_STATUS))
  6480. {
  6481. tx[4] = 0x01;
  6482. tx[5] = 0x22;
  6483. tx[6] = 0x58;
  6484. }
  6485. DEBUG_WARN("EVSE in alarm state, can not stop charging.\r\n");
  6486. }
  6487. else
  6488. {
  6489. if((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))
  6490. {
  6491. if((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))
  6492. {
  6493. Charger.ble.isRequestOff = ON;
  6494. tx[3] = 0x01;
  6495. DEBUG_INFO("Stop OK.\r\n");
  6496. }
  6497. else
  6498. {
  6499. tx[3] = 0x00;
  6500. DEBUG_WARN("EVSE not in CHARGING.\r\n");
  6501. }
  6502. }
  6503. else
  6504. {
  6505. tx[3] = 0x00;
  6506. DEBUG_WARN("Login first\r\n");
  6507. }
  6508. tx[4] = 0x00;
  6509. tx[5] = 0x00;
  6510. tx[6] = 0x00;
  6511. }
  6512. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6513. {
  6514. chksum ^= tx[2 + idx];
  6515. }
  6516. tx[7] = chksum;
  6517. break;
  6518. case BLE_PROTOCOL_MESSAGE_STATUS_QUERY:
  6519. DEBUG_INFO("BLE device query EVSE status request.\r\n");
  6520. tx_len = 0x13;
  6521. tx[0] = (tx_len>>8)&0xff;
  6522. tx[1] = (tx_len&0xff);
  6523. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6524. tx[3] = (((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))?Charger.Mode:0x00);
  6525. tx[4] = (((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))?(((Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative/10000)>>0) & 0xff):0x00);
  6526. tx[5] = (((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))?(((Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative/10000)>>8) & 0xff):0x00);
  6527. tx[6] = (((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))?(((Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative/10000)>>16) & 0xff):0x00);
  6528. tx[7] = (((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))?(((Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative/10000)>>24) & 0xff):0x00);
  6529. tx[8] = (((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))?(((Charger.cycle_info.Power_Consumption/10)>>0) & 0xff):0x00);
  6530. tx[9] = (((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))?(((Charger.cycle_info.Power_Consumption/10)>>8) & 0xff):0x00);
  6531. tx[10] = (((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))?(((Charger.cycle_info.Duration/60000)>>0) & 0xff):0x00);
  6532. tx[11] = (((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))?(((Charger.cycle_info.Duration/60000)>>8) & 0xff):0x00);
  6533. if((Charger.Alarm_Code>0) && ((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER)))
  6534. {
  6535. if((Charger.Alarm_Code & ALARM_OVER_VOLTAGE))
  6536. {
  6537. tx[12] = 0x01;
  6538. tx[13] = 0x22;
  6539. tx[14] = 0x00;
  6540. }
  6541. else if((Charger.Alarm_Code & ALARM_UNDER_VOLTAGE))
  6542. {
  6543. tx[12] = 0x01;
  6544. tx[13] = 0x22;
  6545. tx[14] = 0x03;
  6546. }
  6547. else if((Charger.Alarm_Code & ALARM_OVER_CURRENT))
  6548. {
  6549. tx[12] = 0x01;
  6550. tx[13] = 0x21;
  6551. tx[14] = 0x16;
  6552. }
  6553. else if((Charger.Alarm_Code & ALARM_OVER_TEMPERATURE))
  6554. {
  6555. tx[12] = 0x01;
  6556. tx[13] = 0x22;
  6557. tx[14] = 0x23;
  6558. }
  6559. else if((Charger.Alarm_Code & ALARM_GROUND_FAIL))
  6560. {
  6561. tx[12] = 0x01;
  6562. tx[13] = 0x22;
  6563. tx[14] = 0x56;
  6564. }
  6565. else if((Charger.Alarm_Code & ALARM_CP_ERROR))
  6566. {
  6567. tx[12] = 0x02;
  6568. tx[13] = 0x37;
  6569. tx[14] = 0x03;
  6570. }
  6571. else if((Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC)||(Charger.Alarm_Code & ALARM_CURRENT_LEAK_DC))
  6572. {
  6573. tx[12] = 0x01;
  6574. tx[13] = 0x22;
  6575. tx[14] = 0x33;
  6576. }
  6577. else if((Charger.Alarm_Code & ALARM_MCU_TESTFAIL))
  6578. {
  6579. tx[12] = 0x01;
  6580. tx[13] = 0x22;
  6581. tx[14] = 0x57;
  6582. }
  6583. else if((Charger.Alarm_Code & ALARM_EMERGENCY_STOP))
  6584. {
  6585. tx[12] = 0x01;
  6586. tx[13] = 0x22;
  6587. tx[14] = 0x51;
  6588. }
  6589. else if((Charger.Alarm_Code & ALARM_LEAK_MODULE_FAIL))
  6590. {
  6591. tx[12] = 0x01;
  6592. tx[13] = 0x10;
  6593. tx[14] = 0x04;
  6594. }
  6595. else if((Charger.Alarm_Code & ALARM_RELAY_STATUS))
  6596. {
  6597. tx[12] = 0x01;
  6598. tx[13] = 0x22;
  6599. tx[14] = 0x58;
  6600. }
  6601. }
  6602. else
  6603. {
  6604. tx[12] = 0x00;
  6605. tx[13] = 0x00;
  6606. tx[14] = 0x00;
  6607. }
  6608. tx[15] = Charger.memory.EVSE_Config.data.item.reservation.mode;
  6609. tx[16] = Charger.memory.EVSE_Config.data.item.reservation.hour;
  6610. tx[17] = Charger.memory.EVSE_Config.data.item.reservation.min;
  6611. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6612. {
  6613. chksum ^= tx[2 + idx];
  6614. }
  6615. tx[18] = chksum;
  6616. break;
  6617. case BLE_PROTOCOL_MESSAGE_RESERVATION:
  6618. switch(UART_BLE_rx_buffer[7])
  6619. {
  6620. case RESERVATION_DISABLE:
  6621. DEBUG_INFO("BLE device reservation request disable.\r\n");
  6622. break;
  6623. case RESERVATION_ONCE:
  6624. DEBUG_INFO("BLE device reservation request once on %d:%d.\r\n", UART_BLE_rx_buffer[8], UART_BLE_rx_buffer[9]);
  6625. break;
  6626. case RESERVATION_ALWAYS:
  6627. DEBUG_INFO("BLE device reservation request always on %d:%d.\r\n", UART_BLE_rx_buffer[8], UART_BLE_rx_buffer[9]);
  6628. break;
  6629. }
  6630. tx_len = 0x05;
  6631. tx[0] = (tx_len>>8)&0xff;
  6632. tx[1] = (tx_len&0xff);
  6633. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6634. Charger.memory.EVSE_Config.data.item.reservation.mode = UART_BLE_rx_buffer[7];
  6635. Charger.memory.EVSE_Config.data.item.reservation.hour = UART_BLE_rx_buffer[8];
  6636. Charger.memory.EVSE_Config.data.item.reservation.min = UART_BLE_rx_buffer[9];
  6637. Charger.memory.EVSE_Config.op_bits.update = ON;
  6638. tx[3] = 0x01;
  6639. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6640. {
  6641. chksum ^= tx[2 + idx];
  6642. }
  6643. tx[4] = chksum;
  6644. break;
  6645. case BLE_PROTOCOL_MESSAGE_TIME_QUERY:
  6646. DEBUG_INFO("BLE device query date time request.\r\n");
  6647. tx_len = 0x08;
  6648. tx[0] = (tx_len>>8)&0xff;
  6649. tx[1] = (tx_len&0xff);
  6650. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6651. struct tm gm_date;
  6652. gm_date.tm_year = Charger.memory.EVSE_Config.data.item.SystemDateTime.year-1900;
  6653. gm_date.tm_mon = Charger.memory.EVSE_Config.data.item.SystemDateTime.month-1;
  6654. gm_date.tm_mday = Charger.memory.EVSE_Config.data.item.SystemDateTime.day;
  6655. gm_date.tm_hour = Charger.memory.EVSE_Config.data.item.SystemDateTime.hour;
  6656. gm_date.tm_min = Charger.memory.EVSE_Config.data.item.SystemDateTime.min;
  6657. gm_date.tm_sec = Charger.memory.EVSE_Config.data.item.SystemDateTime.sec;
  6658. tmp = mktime(&gm_date);
  6659. tx[3] = ((tmp >> 0)&0xff);
  6660. tx[4] = ((tmp >> 8)&0xff);
  6661. tx[5] = ((tmp >> 16)&0xff);
  6662. tx[6] = ((tmp >> 24)&0xff);
  6663. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6664. {
  6665. chksum ^= tx[2 + idx];
  6666. }
  6667. tx[7] = chksum;
  6668. break;
  6669. case BLE_PROTOCOL_MESSAGE_TIME_CONFIG:
  6670. DEBUG_INFO("BLE device config date time.\r\n");
  6671. tx_len = 0x09;
  6672. tx[0] = (tx_len>>8)&0xff;
  6673. tx[1] = (tx_len&0xff);
  6674. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6675. if((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))
  6676. {
  6677. struct tm *sm_date;
  6678. tmp = (UART_BLE_rx_buffer[7]<<0) | (UART_BLE_rx_buffer[8]<<8) | (UART_BLE_rx_buffer[9]<<16) | (UART_BLE_rx_buffer[10]<<24);
  6679. sm_date = gmtime(&tmp) ;
  6680. setTime.Hours = sm_date->tm_hour;
  6681. setTime.Minutes = sm_date->tm_min;
  6682. setTime.Seconds = sm_date->tm_sec;
  6683. setTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  6684. setTime.StoreOperation = RTC_STOREOPERATION_RESET;
  6685. if (HAL_RTC_SetTime(&hrtc, &setTime, RTC_FORMAT_BIN) != HAL_OK)
  6686. {
  6687. Error_Handler();
  6688. }
  6689. setDate.Year = (sm_date->tm_year + 1900)-2000;
  6690. setDate.Month = sm_date->tm_mon + 1;
  6691. setDate.Date = sm_date->tm_mday;
  6692. if (HAL_RTC_SetDate(&hrtc, &setDate, RTC_FORMAT_BIN) != HAL_OK)
  6693. {
  6694. Error_Handler();
  6695. }
  6696. tx[3] = 0x01;
  6697. DEBUG_INFO("RTC config OK.\r\n");
  6698. }
  6699. else
  6700. {
  6701. tx[3] = 0x00;
  6702. DEBUG_WARN("User con not config RTC.\r\n");
  6703. }
  6704. tx[4] = UART_BLE_rx_buffer[7];
  6705. tx[5] = UART_BLE_rx_buffer[8];
  6706. tx[6] = UART_BLE_rx_buffer[9];
  6707. tx[7] = UART_BLE_rx_buffer[10];
  6708. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6709. {
  6710. chksum ^= tx[2 + idx];
  6711. }
  6712. tx[8] = chksum;
  6713. break;
  6714. case BLE_PROTOCOL_MESSAGE_LED_QUERY:
  6715. DEBUG_INFO("BLE device query led setting.\r\n");
  6716. tx_len = 0x08;
  6717. tx[0] = (tx_len>>8)&0xff;
  6718. tx[1] = (tx_len&0xff);
  6719. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6720. /*
  6721. TODO: Query led result here
  6722. */
  6723. if((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))
  6724. {
  6725. tx[3] = 0xff;
  6726. tx[4] = 0xff;
  6727. tx[5] = 0xff;
  6728. tx[6] = 0xff;
  6729. }
  6730. else
  6731. {
  6732. tx[3] = 0x00;
  6733. tx[4] = 0x00;
  6734. tx[5] = 0x00;
  6735. tx[6] = 0x00;
  6736. }
  6737. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6738. {
  6739. chksum ^= tx[2 + idx];
  6740. }
  6741. tx[7] = chksum;
  6742. break;
  6743. case BLE_PROTOCOL_MESSAGE_LED_CONFIG:
  6744. DEBUG_INFO("BLE device config led setting.\r\n");
  6745. tx_len = 0x09;
  6746. tx[0] = (tx_len>>8)&0xff;
  6747. tx[1] = (tx_len&0xff);
  6748. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6749. /*
  6750. TODO: Config led result here
  6751. */
  6752. if((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))
  6753. tx[3] = 0x01;
  6754. else
  6755. tx[3] = 0x00;
  6756. tx[4] = UART_BLE_rx_buffer[7];
  6757. tx[5] = UART_BLE_rx_buffer[8];
  6758. tx[6] = UART_BLE_rx_buffer[9];
  6759. tx[7] = UART_BLE_rx_buffer[10];
  6760. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6761. {
  6762. chksum ^= tx[2 + idx];
  6763. }
  6764. tx[8] = chksum;
  6765. break;
  6766. case BLE_PROTOCOL_MESSAGE_CURRENT_LIMIT_QUERY:
  6767. DEBUG_INFO("BLE device query current limit request.\r\n");
  6768. tx_len = 0x05;
  6769. tx[0] = (tx_len>>8)&0xff;
  6770. tx[1] = (tx_len&0xff);
  6771. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6772. if((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))
  6773. {
  6774. tx[3] = Charger.memory.EVSE_Config.data.item.MaxChargingCurrent;
  6775. DEBUG_INFO("Query current limit: %02dA.\r\n", Charger.memory.EVSE_Config.data.item.MaxChargingCurrent);
  6776. }
  6777. else
  6778. {
  6779. tx[3] = 0x00;
  6780. DEBUG_WARN("Please login first, before query current limit.\r\n");
  6781. }
  6782. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6783. {
  6784. chksum ^= tx[2 + idx];
  6785. }
  6786. tx[8] = chksum;
  6787. break;
  6788. case BLE_PROTOCOL_MESSAGE_CURRENT_LIMIT_CONFIG:
  6789. DEBUG_INFO("BLE device config current limit.\r\n");
  6790. tx_len = 0x06;
  6791. tx[0] = (tx_len>>8)&0xff;
  6792. tx[1] = (tx_len&0xff);
  6793. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6794. if((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))
  6795. {
  6796. if(UART_BLE_rx_buffer[7]>Charger.maxRatingCurrent)
  6797. {
  6798. tx[3] = 0x02;
  6799. Charger.memory.EVSE_Config.data.item.MaxChargingCurrent = Charger.maxRatingCurrent;
  6800. DEBUG_INFO("Setting value over max rating value, set as max rating .\r\n");
  6801. }
  6802. else
  6803. {
  6804. tx[3] = 0x01;
  6805. Charger.memory.EVSE_Config.data.item.MaxChargingCurrent = UART_BLE_rx_buffer[7];
  6806. }
  6807. DEBUG_INFO("Config current limit: %02dA\r\n", Charger.memory.EVSE_Config.data.item.MaxChargingCurrent);
  6808. }
  6809. else
  6810. {
  6811. tx[3] = 0x00;
  6812. DEBUG_WARN("Please login first, before setting current limit.\r\n");
  6813. }
  6814. tx[4] = UART_BLE_rx_buffer[7];
  6815. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6816. {
  6817. chksum ^= tx[2 + idx];
  6818. }
  6819. tx[5] = chksum;
  6820. break;
  6821. case BLE_PROTOCOL_MESSAGE_FIRMWARE_VERSION_QUERY:
  6822. DEBUG_INFO("BLE device query firmware version.\r\n");
  6823. tx_len = 4 + strlen(Charger.Ver_FW);
  6824. tx[0] = (tx_len>>8)&0xff;
  6825. tx[1] = (tx_len&0xff);
  6826. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6827. for(uint16_t idx=0;idx<strlen(Charger.Ver_FW);idx++)
  6828. {
  6829. if((Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT) || (Charger.ble.loginRole == BLE_LOGIN_ROLE_USER))
  6830. tx[3+idx] = Charger.Ver_FW[idx];
  6831. else
  6832. tx[3+idx] = 0x00;
  6833. }
  6834. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6835. {
  6836. chksum ^= tx[2 + idx];
  6837. }
  6838. tx[3+strlen(Charger.Ver_FW)] = chksum;
  6839. break;
  6840. case BLE_PROTOCOL_MESSAGE_FIRMWARE_UPDATE_REQ:
  6841. tx_len = 5;
  6842. tx[0] = 0x00;
  6843. tx[1] = 0x05;
  6844. tx[2] = UART_BLE_rx_buffer[2] + 0x80;;
  6845. if(Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT)
  6846. {
  6847. binCRCTarget = (UART_BLE_rx_buffer[7] << 24) | (UART_BLE_rx_buffer[8] << 16) | (UART_BLE_rx_buffer[9] << 8) | (UART_BLE_rx_buffer[10] << 0);
  6848. flashdestination = NEW_CODE_ADDRESS;
  6849. if(FLASH_If_Erase(NEW_CODE_ADDRESS, 3)== FLASHIF_OK)
  6850. {
  6851. DEBUG_INFO("Firmware transfer start, earase flash success....\n\r");
  6852. tx[3] = 0x01;
  6853. }
  6854. else
  6855. {
  6856. DEBUG_INFO("Firmware transfer start, earase flash fail....\n\r");
  6857. tx[3] = 0x00;
  6858. }
  6859. }
  6860. else
  6861. {
  6862. tx[3] = 0x00;
  6863. DEBUG_WARN("User can not upgrade start.\r\n");
  6864. }
  6865. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6866. {
  6867. chksum ^= tx[2 + idx];
  6868. }
  6869. tx[4] = chksum;
  6870. break;
  6871. case BLE_PROTOCOL_MESSAGE_FIRMWARE_TRANS_REQ:
  6872. tx_len = 5;
  6873. tx[0] = 0x00;
  6874. tx[1] = 0x05;
  6875. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6876. if(Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT)
  6877. {
  6878. flashdestination = NEW_CODE_ADDRESS + ((UART_BLE_rx_buffer[7]<<24) | (UART_BLE_rx_buffer[8]<<16) | (UART_BLE_rx_buffer[9]<<8) |(UART_BLE_rx_buffer[10]<<0));
  6879. if (FLASH_If_Write(flashdestination, (uint32_t*) &UART_BLE_rx_buffer[11], ((((UART_BLE_rx_buffer[1]) | (UART_BLE_rx_buffer[0])<<8)-12)>>2)) == FLASHIF_OK)
  6880. {
  6881. DEBUG_INFO("Firmware transfer start address: 0x%x ,length: %d...Pass\n\r", flashdestination, (((UART_BLE_rx_buffer[1]) | (UART_BLE_rx_buffer[0])<<8)-12));
  6882. tx[3] = 0x01;
  6883. }
  6884. else
  6885. {
  6886. DEBUG_INFO("Firmware transfer start address: 0x%x, length: %d...Fail\n\r", flashdestination, (((UART_BLE_rx_buffer[1]) | (UART_BLE_rx_buffer[0])<<8)-12));
  6887. tx[3] = 0x00;
  6888. }
  6889. }
  6890. else
  6891. {
  6892. tx[3] = 0x00;
  6893. DEBUG_WARN("User can not upgrade transfer.\r\n");
  6894. }
  6895. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6896. {
  6897. chksum ^= tx[2 + idx];
  6898. }
  6899. tx[4] = chksum;
  6900. break;
  6901. case BLE_PROTOCOL_MESSAGE_FIRMWARE_END_REQ:
  6902. tx_len = 5;
  6903. tx[0] = 0x00;
  6904. tx[1] = 0x05;
  6905. tx[2] = UART_BLE_rx_buffer[2] + 0x80;;
  6906. if(Charger.ble.loginRole == BLE_LOGIN_ROLE_ROOT)
  6907. {
  6908. flash = NEW_CODE_ADDRESS;
  6909. checksum = HAL_CRC_Calculate(&hcrc, (uint32_t *)flash, ((FLASH_AP_LENGTH-4)>>2));
  6910. flash = ((uint32_t)(NEW_CODE_ADDRESS+FLASH_AP_LENGTH-4));
  6911. DEBUG_INFO("Firmware transfer end, AP CRC checksum, flash: 0x%x, 0x%x\r\n", checksum, *((uint32_t *)flash) );
  6912. if(checksum == *((uint32_t *)flash))
  6913. {
  6914. if (FLASH_If_Write(UPGRADE_REQ_ADDRESS, (uint32_t *)&endFlag[0], 1) == FLASHIF_OK)
  6915. {
  6916. DEBUG_INFO("Firmware Confirm Tag write ok..\n\r");
  6917. tx[3] = 0x01;
  6918. tx[4] = 0xB5;
  6919. HAL_UART_Transmit(&IAP_USART, (uint8_t *)tx, sizeof(tx), 0xffff);
  6920. osDelay(1000);
  6921. NVIC_SystemReset();
  6922. }
  6923. else
  6924. {
  6925. DEBUG_INFO("Firmware Confirm Tag write fail...\n\r");
  6926. tx[3] = 0x00;
  6927. }
  6928. }
  6929. else
  6930. {
  6931. DEBUG_INFO("Firmware checksum compare fail...\n\r");
  6932. tx[3] = 0x00;
  6933. }
  6934. }
  6935. else
  6936. {
  6937. tx[3] = 0x00;
  6938. DEBUG_WARN("User can not upgrade stop.\r\n");
  6939. }
  6940. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6941. {
  6942. chksum ^= tx[2 + idx];
  6943. }
  6944. tx[4] = chksum;
  6945. break;
  6946. default:
  6947. /*
  6948. TODO: Unknow command response here
  6949. */
  6950. tx_len = 5;
  6951. tx[0] = 0x00;
  6952. tx[1] = 0x05;
  6953. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6954. tx[3] = 0x00;
  6955. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6956. {
  6957. chksum ^= tx[2 + idx];
  6958. }
  6959. tx[4] = chksum;
  6960. DEBUG_ERROR("BLE unknow command.\r\n");
  6961. break;
  6962. }
  6963. }
  6964. else
  6965. {
  6966. /*
  6967. TODO: Protocol check sum error response here
  6968. */
  6969. tx_len = 5;
  6970. tx[0] = 0x00;
  6971. tx[1] = 0x05;
  6972. tx[2] = UART_BLE_rx_buffer[2] + 0x80;
  6973. tx[3] = 0x00;
  6974. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  6975. {
  6976. chksum ^= tx[2 + idx];
  6977. }
  6978. tx[4] = chksum;
  6979. DEBUG_ERROR("BLE check sum validation error.\r\n");
  6980. }
  6981. HAL_UART_Transmit(&BLE_USART, (uint8_t *)tx, tx_len, 0xffff);
  6982. }
  6983. UART_BLE_rx_len=0;
  6984. UART_BLE_recv_end_flag = OFF;
  6985. }
  6986. HAL_UART_Receive_DMA(&BLE_USART, (uint8_t*)UART_BLE_rx_buffer, UART_BUFFER_SIZE);
  6987. osDelay(1);
  6988. }
  6989. /* USER CODE END StartBleTask */
  6990. }
  6991. #ifdef MODIFY_FLASH_OPERATION_20241104
  6992. void Proc_EVSE_Config(void)
  6993. {
  6994. #ifdef MODIFY_UNDEFINED_MODEL_NAME_AND_SN_ISSUE
  6995. if (Charger.memory.EVSE_Config.data.item.ModelName[0] == 0xFF ||
  6996. Charger.memory.EVSE_Config.data.item.SerialNumber[0] == 0xFF)
  6997. {
  6998. Charger.memory.EVSE_Config.data.item.ModelName[MODEL_NAME_LEN - 1] = '\0';
  6999. Charger.memory.EVSE_Config.data.item.SerialNumber[SERIAL_NUM_LEN - 1] = '\0';
  7000. Charger.memory.EVSE_Config.op_bits.update = ON;
  7001. }
  7002. #endif //MODIFY_UNDEFINED_MODEL_NAME_AND_SN_ISSUE
  7003. Charger.memory.EVSE_Config.data.item.isDispDebug = ON;
  7004. sprintf(Charger.memory.EVSE_Config.data.item.System_ID, "%s%s", Charger.memory.EVSE_Config.data.item.ModelName, Charger.memory.EVSE_Config.data.item.SerialNumber);
  7005. //sprintf((char *)tmpBuf, "%08x", ~HAL_CRC_Calculate(&hcrc, (uint32_t *)&Charger.memory.EVSE_Config.data.item.System_ID[0], (ARRAY_SIZE(Charger.memory.EVSE_Config.data.item.System_ID)>>2)));
  7006. //memcpy(&Charger.memory.EVSE_Config.data.item.bleConfig.puk[0], &tmpBuf[0], ARRAY_SIZE(Charger.memory.EVSE_Config.data.item.bleConfig.puk));
  7007. //DEBUG_INFO("BLE PUK: %s\r\n", Charger.memory.EVSE_Config.data.item.bleConfig.puk);
  7008. srand(Charger.memory.EVSE_Config.data.item.bleConfig.puk[0] | (Charger.memory.EVSE_Config.data.item.bleConfig.puk[1]<<8));
  7009. setTime.Hours = Charger.memory.EVSE_Config.data.item.SystemDateTime.hour;
  7010. setTime.Minutes = Charger.memory.EVSE_Config.data.item.SystemDateTime.min;
  7011. setTime.Seconds = Charger.memory.EVSE_Config.data.item.SystemDateTime.sec;
  7012. setTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  7013. setTime.StoreOperation = RTC_STOREOPERATION_RESET;
  7014. if (HAL_RTC_SetTime(&hrtc, &setTime, RTC_FORMAT_BIN) != HAL_OK)
  7015. {
  7016. Error_Handler();
  7017. }
  7018. setDate.Year = Charger.memory.EVSE_Config.data.item.SystemDateTime.year-2000;
  7019. setDate.Month = Charger.memory.EVSE_Config.data.item.SystemDateTime.month;
  7020. setDate.Date = Charger.memory.EVSE_Config.data.item.SystemDateTime.day;
  7021. if (HAL_RTC_SetDate(&hrtc, &setDate, RTC_FORMAT_BIN) != HAL_OK)
  7022. {
  7023. Error_Handler();
  7024. }
  7025. //CCLC_Corr_Gain_Par
  7026. //VT
  7027. #ifdef MODIFY_CORRECTION_CHECK
  7028. if (
  7029. (Charger.memory.EVSE_Config.data.item.Correction_VL1[1][MCU_DATA] > Charger.memory.EVSE_Config.data.item.Correction_VL1[2][MCU_DATA]) &&
  7030. (Charger.memory.EVSE_Config.data.item.Correction_VL1[1][METER_DATA] > Charger.memory.EVSE_Config.data.item.Correction_VL1[2][METER_DATA])
  7031. )
  7032. #else
  7033. if(Charger.memory.EVSE_Config.data.item.Correction_VL1[1][MCU_DATA] > Charger.memory.EVSE_Config.data.item.Correction_VL1[2][MCU_DATA])
  7034. #endif
  7035. {
  7036. CLC_Corr_Gain_Par( Charger.memory.EVSE_Config.data.item.Correction_VL1[1][METER_DATA],Charger.memory.EVSE_Config.data.item.Correction_VL1[2][METER_DATA] ,
  7037. Charger.memory.EVSE_Config.data.item.Correction_VL1[1][MCU_DATA],Charger.memory.EVSE_Config.data.item.Correction_VL1[2][MCU_DATA] ,
  7038. &Charger.coefficient.gain_v[0], &Charger.coefficient.offset_v[0]);
  7039. }
  7040. else
  7041. {
  7042. Charger.coefficient.gain_v[0] = 1;
  7043. Charger.coefficient.offset_v[0] = 0;
  7044. }
  7045. //CT
  7046. #ifdef MODIFY_CORRECTION_CHECK
  7047. if (
  7048. (Charger.memory.EVSE_Config.data.item.Correction_CL1[1][MCU_DATA] > Charger.memory.EVSE_Config.data.item.Correction_CL1[2][MCU_DATA]) &&
  7049. (Charger.memory.EVSE_Config.data.item.Correction_CL1[1][METER_DATA] > Charger.memory.EVSE_Config.data.item.Correction_CL1[2][METER_DATA])
  7050. )
  7051. #else
  7052. if(Charger.memory.EVSE_Config.data.item.Correction_CL1[1][MCU_DATA] > Charger.memory.EVSE_Config.data.item.Correction_CL1[2][MCU_DATA])
  7053. #endif
  7054. {
  7055. CLC_Corr_Gain_Par(Charger.memory.EVSE_Config.data.item.Correction_CL1[1][METER_DATA],Charger.memory.EVSE_Config.data.item.Correction_CL1[2][METER_DATA] ,
  7056. Charger.memory.EVSE_Config.data.item.Correction_CL1[1][MCU_DATA],Charger.memory.EVSE_Config.data.item.Correction_CL1[2][MCU_DATA] ,
  7057. &Charger.coefficient.gain_c[0], &Charger.coefficient.offset_c[0]);
  7058. }
  7059. else
  7060. {
  7061. Charger.coefficient.gain_c[0] = 1;
  7062. Charger.coefficient.offset_c[0] = 0;
  7063. }
  7064. //LEAK
  7065. if(Charger.memory.EVSE_Config.data.item.Correction_Leak[1][MCU_DATA] > Charger.memory.EVSE_Config.data.item.Correction_Leak[2][MCU_DATA])
  7066. {
  7067. CLC_Corr_Gain_Par(Charger.memory.EVSE_Config.data.item.Correction_Leak[1][METER_DATA],Charger.memory.EVSE_Config.data.item.Correction_Leak[2][METER_DATA] ,
  7068. Charger.memory.EVSE_Config.data.item.Correction_Leak[1][MCU_DATA],Charger.memory.EVSE_Config.data.item.Correction_Leak[2][MCU_DATA] ,
  7069. &Charger.coefficient.gain_leak[0], &Charger.coefficient.offset_leak[0]);
  7070. }
  7071. else
  7072. {
  7073. Charger.coefficient.gain_leak[0] = 1;
  7074. Charger.coefficient.offset_leak[0] = 0;
  7075. }
  7076. DEBUG_INFO("Charger.coefficient.gain_leak[0] = %f\r\n",Charger.coefficient.gain_leak[0]);
  7077. DEBUG_INFO("Charger.coefficient.offset_leak[0] = %f\r\n",Charger.coefficient.offset_leak[0]);
  7078. //GMI
  7079. if(Charger.memory.EVSE_Config.data.item.Correction_GMI[1][MCU_DATA] > Charger.memory.EVSE_Config.data.item.Correction_GMI[2][MCU_DATA])
  7080. {
  7081. CLC_Corr_Gain_Par2(Charger.memory.EVSE_Config.data.item.Correction_GMI[1][METER_DATA],Charger.memory.EVSE_Config.data.item.Correction_GMI[2][METER_DATA] ,
  7082. Charger.memory.EVSE_Config.data.item.Correction_GMI[1][MCU_DATA],Charger.memory.EVSE_Config.data.item.Correction_GMI[2][MCU_DATA] ,
  7083. &Charger.coefficient.gain_gmi[0], &Charger.coefficient.offset_gmi[0], &Charger.coefficient.offsetisPosOrNeg_gmi[0]);
  7084. }
  7085. else
  7086. {
  7087. Charger.coefficient.gain_gmi[0] = 1;
  7088. Charger.coefficient.offset_gmi[0] = 0;
  7089. }
  7090. //DEBUG_INFO("Charger.coefficient.gain_gmi[0] = %f\r\n",Charger.coefficient.gain_gmi[0]);
  7091. //DEBUG_INFO("Charger.coefficient.offset_gmi[0] = %f\r\n",Charger.coefficient.offset_gmi[0]);
  7092. #ifdef FUNC_SHOW_CORRECTION_DATA
  7093. {
  7094. XP("[CORRECT DATA]======================\r\n");
  7095. XP("<Correction_VL1>\r\n");
  7096. for (int i = 0; i < 4; i++)
  7097. {
  7098. XP("[%d][0] = 0x%04X, [%d][1] = 0x%04X\r\n",
  7099. i, Charger.memory.EVSE_Config.data.item.Correction_VL1[i][0],
  7100. i, Charger.memory.EVSE_Config.data.item.Correction_VL1[i][1]);
  7101. }
  7102. XP("<Correction_VL2>\r\n");
  7103. for (int i = 0; i < 2; i++)
  7104. {
  7105. XP("[%d][0] = 0x%04X, [%d][1] = 0x%04X\r\n",
  7106. i, Charger.memory.EVSE_Config.data.item.Correction_VL2[i][0],
  7107. i, Charger.memory.EVSE_Config.data.item.Correction_VL2[i][1]);
  7108. }
  7109. XP("<Correction_VL3>\r\n");
  7110. for (int i = 0; i < 2; i++)
  7111. {
  7112. XP("[%d][0] = 0x%04X, [%d][1] = 0x%04X\r\n",
  7113. i, Charger.memory.EVSE_Config.data.item.Correction_VL3[i][0],
  7114. i, Charger.memory.EVSE_Config.data.item.Correction_VL3[i][1]);
  7115. }
  7116. XP("<Correction_CL1>\r\n");
  7117. for (int i = 0; i < 4; i++)
  7118. {
  7119. XP("[%d][0] = 0x%04X, [%d][1] = 0x%04X\r\n",
  7120. i, Charger.memory.EVSE_Config.data.item.Correction_CL1[i][0],
  7121. i, Charger.memory.EVSE_Config.data.item.Correction_CL1[i][1]);
  7122. }
  7123. XP("<Correction_CL2>\r\n");
  7124. for (int i = 0; i < 2; i++)
  7125. {
  7126. XP("[%d][0] = 0x%04X, [%d][1] = 0x%04X\r\n",
  7127. i, Charger.memory.EVSE_Config.data.item.Correction_CL2[i][0],
  7128. i, Charger.memory.EVSE_Config.data.item.Correction_CL2[i][1]);
  7129. }
  7130. XP("<Correction_CL3>\r\n");
  7131. for (int i = 0; i < 2; i++)
  7132. {
  7133. XP("[%d][0] = 0x%04X, [%d][1] = 0x%04X\r\n",
  7134. i, Charger.memory.EVSE_Config.data.item.Correction_CL3[i][0],
  7135. i, Charger.memory.EVSE_Config.data.item.Correction_CL3[i][1]);
  7136. }
  7137. XP("<Correction_Leak>\r\n");
  7138. for (int i = 0; i < 4; i++)
  7139. {
  7140. XP("[%d][0] = 0x%04X, [%d][1] = 0x%04X\r\n",
  7141. i, Charger.memory.EVSE_Config.data.item.Correction_Leak[i][0],
  7142. i, Charger.memory.EVSE_Config.data.item.Correction_Leak[i][1]);
  7143. }
  7144. XP("<Correction_GMI>\r\n");
  7145. for (int i = 0; i < 4; i++)
  7146. {
  7147. XP("[%d][0] = 0x%04X, [%d][1] = 0x%04X\r\n",
  7148. i, Charger.memory.EVSE_Config.data.item.Correction_GMI[i][0],
  7149. i, Charger.memory.EVSE_Config.data.item.Correction_GMI[i][1]);
  7150. }
  7151. XP("====================================\r\n");
  7152. }
  7153. #endif //FUNC_SHOW_CORRECTION_DATA
  7154. //DEBUG_INFO("Read MEM_ADDR_EVSE_CONFIG block(4k byte) pass.\r\n");
  7155. // Check MCU control mode depend on GPIO state ~again~ , Prevent W25Qx from being empty(0xFF)
  7156. if(HAL_GPIO_ReadPin(IN_ACT_REQ_GPIO_Port, IN_ACT_REQ_Pin) == GPIO_PIN_RESET)
  7157. {
  7158. Charger.memory.EVSE_Config.data.item.MCU_Control_Mode = MCU_CONTROL_MODE_CSU;
  7159. DEBUG_INFO("Check MCU_Control_Mode is CSU in memmeory task\r\n");
  7160. }
  7161. else
  7162. {
  7163. Charger.memory.EVSE_Config.data.item.MCU_Control_Mode = MCU_CONTROL_MODE_NO_CSU;
  7164. if (Charger.memory.EVSE_Config.data.item.AuthMode > AUTH_MODE_FREE)
  7165. {
  7166. Charger.memory.EVSE_Config.data.item.AuthMode = AUTH_MODE_FREE ;
  7167. DEBUG_INFO("AuthMode is defualt AUTH_MODE_FREE \r\n");
  7168. }
  7169. if (Charger.memory.EVSE_Config.data.item.OfflinePolicy > RFID_USER_AUTH_NO_CHARGING)
  7170. {
  7171. Charger.memory.EVSE_Config.data.item.OfflinePolicy = RFID_USER_AUTH_FREE ;
  7172. DEBUG_INFO("OfflinePolicy is defualt RFID_USER_AUTH_FREE \r\n");
  7173. }
  7174. DEBUG_INFO("Check MCU_Control_Mode is NO NO NO CSU in memmeory task\r\n");
  7175. }
  7176. // Clean update & clear flag when model name not configured
  7177. /*
  7178. if(strlen(Charger.memory.EVSE_Config.data.item.ModelName)>20)
  7179. {
  7180. Charger.memory.EVSE_Config.op_bits.update = OFF;
  7181. Charger.memory.EVSE_Config.op_bits.clear = OFF;
  7182. Charger.memory.EVSE_Config.op_bits.backup = OFF;
  7183. Charger.memory.EVSE_Config.op_bits.restore = OFF;
  7184. }
  7185. */
  7186. parseVersionInfoFromModelname();
  7187. }
  7188. #endif //MODIFY_FLASH_OPERATION_20241104
  7189. /* USER CODE BEGIN Header_StartMemoryTask */
  7190. /**
  7191. * @brief Function implementing the memoryTask thread.
  7192. * @param argument: Not used
  7193. * @retval None
  7194. */
  7195. /* USER CODE END Header_StartMemoryTask */
  7196. void StartMemoryTask(void const * argument)
  7197. {
  7198. /* USER CODE BEGIN StartMemoryTask */
  7199. //mmemory
  7200. //uint8_t tmpBuf[64];
  7201. uint8_t isSuccess;
  7202. uint8_t ID[2];
  7203. BSP_W25Qx_Init();
  7204. BSP_W25Qx_Read_ID(ID);
  7205. DEBUG_INFO("W25Qxxx ID is : 0x%02X 0x%02X \r\n",ID[0],ID[1]);
  7206. Charger.memory.EVSE_Config.op_bits.read = ON;
  7207. Charger.memory.coldLoadPickUp.op_bits.read = ON;
  7208. Charger.memory.whiteList.op_bits.read = ON;
  7209. Charger.memory.hisAlarm.op_bits.read = ON;
  7210. Charger.memory.hisCharging.op_bits.read = ON;
  7211. /*
  7212. Charger.memory.EVSE_Config.op_bits.clear = ON ;
  7213. Charger.memory.EVSE_Config.op_bits.clear = ON;
  7214. Charger.memory.coldLoadPickUp.op_bits.clear = ON;
  7215. Charger.memory.whiteList.op_bits.clear = ON;
  7216. Charger.memory.hisAlarm.op_bits.clear = ON;
  7217. Charger.memory.hisCharging.op_bits.clear = ON;
  7218. */
  7219. /* Infinite loop */
  7220. for(;;)
  7221. {
  7222. /*
  7223. EVSE config operation
  7224. */
  7225. if(Charger.memory.EVSE_Config.op_bits.read)
  7226. {
  7227. #ifdef FUNC_CALC_FLASH_CRC_IN_CRITICAL
  7228. taskENTER_CRITICAL();
  7229. #endif
  7230. #ifndef FUNC_CALC_FLASH_CRC_IN_CRITICAL
  7231. osDelay(100);
  7232. #endif
  7233. // Read data from block
  7234. if(BSP_W25Qx_Read(Charger.memory.EVSE_Config.data.value, MEM_ADDR_EVSE_CONFIG, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7235. {
  7236. #ifdef FUNC_FLASH_RESTORE_CONFIG_WHEN_PRIMARY_LOSS
  7237. #ifdef FUNC_FLASH_CONFIG_APPEND_CRC
  7238. u32 crc32 = HAL_CRC_Calculate(&hcrc, (u32*)Charger.memory.EVSE_Config.data.verify.buf, ((W25Q16FV_BLOCK_SIZE-4)>>2));
  7239. if ((Charger.memory.EVSE_Config.data.verify.crc32 == crc32 /*|| Charger.memory.EVSE_Config.data.verify.crc32 == HTK_U32_MAX*/))
  7240. #else
  7241. if (1)
  7242. #endif
  7243. {
  7244. DEBUG_INFO("Read MEM_ADDR_EVSE_CONFIG block(4k byte) pass.\r\n");
  7245. Proc_EVSE_Config();
  7246. Charger.ModelReadisOK = PASS;
  7247. }
  7248. else
  7249. {
  7250. static u8 bProc = 0;
  7251. if (!bProc)
  7252. {
  7253. bProc = 1;
  7254. DEBUG_WARN("MEM_ADDR_EVSE_CONFIG data area check sum error => restore from backup area.\r\n");
  7255. Charger.memory.EVSE_Config.op_bits.restore = ON;
  7256. }
  7257. else
  7258. {
  7259. Proc_EVSE_Config();
  7260. Charger.ModelReadisOK = PASS;
  7261. }
  7262. }
  7263. #endif //FUNC_FLASH_RESTORE_CONFIG_WHEN_PRIMARY_LOSS
  7264. Charger.memory.EVSE_Config.op_bits.read = OFF;
  7265. }
  7266. else
  7267. {
  7268. DEBUG_INFO("Read MEM_ADDR_EVSE_CONFIG block(4k byte) fail.\r\n");
  7269. }
  7270. #ifdef FUNC_CALC_FLASH_CRC_IN_CRITICAL
  7271. taskEXIT_CRITICAL();
  7272. #endif
  7273. }
  7274. if(Charger.memory.EVSE_Config.op_bits.update)
  7275. {
  7276. #ifdef FUNC_CALC_FLASH_CRC_IN_CRITICAL
  7277. taskENTER_CRITICAL();
  7278. #endif
  7279. #ifdef FUNC_AUTO_MODIFY_CCID_MODULE
  7280. Charger.memory.EVSE_Config.op_bits.update = OFF;
  7281. #endif
  7282. isSuccess = PASS;
  7283. #ifdef FUNC_FLASH_CONFIG_APPEND_CRC
  7284. #ifndef FUNC_CALC_FLASH_CRC_IN_CRITICAL
  7285. osDelay(100);
  7286. #endif
  7287. Charger.memory.EVSE_Config.data.verify.crc32 = HAL_CRC_Calculate(&hcrc, (u32*)Charger.memory.EVSE_Config.data.verify.buf, ((W25Q16FV_BLOCK_SIZE-4)>>2));
  7288. #endif
  7289. // Erase block data
  7290. if(BSP_W25Qx_Erase_Block(MEM_ADDR_EVSE_CONFIG) == W25Qx_OK)
  7291. {
  7292. // Write data to block
  7293. for(uint16_t idx=0;idx<((W25Q16FV_BLOCK_SIZE)>>8);idx++)
  7294. {
  7295. if(BSP_W25Qx_Write(&Charger.memory.EVSE_Config.data.value[idx*W25Q16FV_PAGE_SIZE], MEM_ADDR_EVSE_CONFIG+(idx*W25Q16FV_PAGE_SIZE), W25Q16FV_PAGE_SIZE) != W25Qx_OK)
  7296. {
  7297. isSuccess = FAIL;
  7298. }
  7299. }
  7300. if(isSuccess)
  7301. DEBUG_INFO("Update MEM_ADDR_EVSE_CONFIG success.\r\n");
  7302. else
  7303. DEBUG_WARN("Update MEM_ADDR_EVSE_CONFIG fail.\r\n");
  7304. #ifndef FUNC_AUTO_MODIFY_CCID_MODULE
  7305. Charger.memory.EVSE_Config.op_bits.update = OFF;
  7306. #endif
  7307. #ifdef FUNC_FLASH_UPDATE_CONFIG_BOTH_PRIMARY_AND_BACKUP
  7308. Charger.memory.EVSE_Config.op_bits.backup = ON;
  7309. #endif
  7310. }
  7311. else
  7312. {
  7313. DEBUG_WARN("Update MEM_ADDR_EVSE_CONFIG block(4k byte) fail.\r\n");
  7314. }
  7315. #ifdef FUNC_CALC_FLASH_CRC_IN_CRITICAL
  7316. taskEXIT_CRITICAL();
  7317. #endif
  7318. }
  7319. if(Charger.memory.EVSE_Config.op_bits.clear)
  7320. {
  7321. // Erase block data
  7322. if(BSP_W25Qx_Erase_Block(MEM_ADDR_EVSE_CONFIG) == W25Qx_OK)
  7323. {
  7324. // Read data from block
  7325. if(BSP_W25Qx_Read(Charger.memory.EVSE_Config.data.value, MEM_ADDR_EVSE_CONFIG, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7326. {
  7327. DEBUG_INFO("Erase MEM_ADDR_EVSE_CONFIG block(4k byte) pass.\r\n");
  7328. Charger.memory.EVSE_Config.op_bits.clear = OFF;
  7329. }
  7330. else
  7331. {
  7332. DEBUG_WARN("Erase MEM_ADDR_EVSE_CONFIG block(4k byte) fail.\r\n");
  7333. }
  7334. }
  7335. else
  7336. {
  7337. DEBUG_WARN("Erase MEM_ADDR_EVSE_CONFIG block(4k byte) fail.\r\n");
  7338. }
  7339. }
  7340. if(Charger.memory.EVSE_Config.op_bits.backup)
  7341. {
  7342. #ifdef FUNC_CALC_FLASH_CRC_IN_CRITICAL
  7343. taskENTER_CRITICAL();
  7344. #endif
  7345. isSuccess = PASS;
  7346. #ifndef FUNC_CALC_FLASH_CRC_IN_CRITICAL
  7347. osDelay(100);
  7348. #endif
  7349. Charger.memory.EVSE_Config.data.verify.crc32 = HAL_CRC_Calculate(&hcrc, (u32*)Charger.memory.EVSE_Config.data.verify.buf, ((W25Q16FV_BLOCK_SIZE-4)>>2));
  7350. // Erase block data
  7351. if(BSP_W25Qx_Erase_Block(MEM_ADDR_BACKUP_EVSE_CONFIG) == W25Qx_OK)
  7352. {
  7353. // Write data to block
  7354. for(uint16_t idx=0;idx<((W25Q16FV_BLOCK_SIZE)>>8);idx++)
  7355. {
  7356. 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)
  7357. {
  7358. isSuccess = FAIL;
  7359. }
  7360. }
  7361. if(isSuccess)
  7362. DEBUG_INFO("Backup MEM_ADDR_BACKUP_EVSE_CONFIG success.\r\n");
  7363. else
  7364. DEBUG_WARN("Backup MEM_ADDR_BACKUP_EVSE_CONFIG fail.\r\n");
  7365. Charger.memory.EVSE_Config.op_bits.backup = OFF;
  7366. #ifndef FUNC_FLASH_UPDATE_CONFIG_BOTH_PRIMARY_AND_BACKUP
  7367. //Charger.memory.EVSE_Config.op_bits.clear = ON;
  7368. #endif
  7369. }
  7370. else
  7371. {
  7372. DEBUG_WARN("Backup MEM_ADDR_BACKUP_EVSE_CONFIG block(4k byte) fail.\r\n");
  7373. }
  7374. #ifdef FUNC_CALC_FLASH_CRC_IN_CRITICAL
  7375. taskEXIT_CRITICAL();
  7376. #endif
  7377. }
  7378. if(Charger.memory.EVSE_Config.op_bits.restore)
  7379. {
  7380. // Restore data from backup block
  7381. if(BSP_W25Qx_Read(Charger.memory.EVSE_Config.data.value, MEM_ADDR_BACKUP_EVSE_CONFIG, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7382. {
  7383. u32 crc32 = HAL_CRC_Calculate(&hcrc, (u32*)Charger.memory.EVSE_Config.data.verify.buf, ((W25Q16FV_BLOCK_SIZE-4)>>2));
  7384. if(Charger.memory.EVSE_Config.data.verify.crc32 == crc32)
  7385. {
  7386. DEBUG_INFO("Restore MEM_ADDR_BACKUP_EVSE_CONFIG block(4k byte) pass.\r\n");
  7387. // Update data to primary area
  7388. Charger.memory.EVSE_Config.op_bits.update = ON;
  7389. Proc_EVSE_Config();
  7390. Charger.ModelReadisOK = PASS;
  7391. }
  7392. else
  7393. {
  7394. DEBUG_WARN("[FLASH] MEM_ADDR_BACKUP_EVSE_CONFIG block check sum error.\r\n");
  7395. // TODO:
  7396. static u8 bProc = 0;
  7397. if (!bProc)
  7398. {
  7399. bProc = 1;
  7400. setLedMotion(LED_ACTION_ALARM);
  7401. Charger.memory.EVSE_Config.op_bits.read = ON;
  7402. }
  7403. }
  7404. Charger.memory.EVSE_Config.op_bits.restore = OFF;
  7405. }
  7406. else
  7407. {
  7408. DEBUG_WARN("Restore MEM_ADDR_BACKUP_EVSE_CONFIG block(4k byte) fail.\r\n");
  7409. }
  7410. }
  7411. #ifdef FUNC_TEST_SIMU_LOAD_BUTTON
  7412. {
  7413. static int buttonDownCount = 0;
  7414. static HTK_BOOL bExec = HTK_FALSE;
  7415. if((HAL_GPIO_ReadPin(IN_MEM_Erase_GPIO_Port, IN_MEM_Erase_Pin) == GPIO_PIN_RESET))
  7416. {
  7417. if (buttonDownCount > 100 && !bExec)
  7418. {
  7419. bExec = HTK_TRUE;
  7420. Charger.m_bEnableSimuChargeCurrent = !Charger.m_bEnableSimuChargeCurrent;
  7421. XP("\r\n\r\n#BUTTON:ParameterSimuCurr: %s\r\n\r\n\r\n", Charger.m_bEnableSimuChargeCurrent ? "ON" : "OFF");
  7422. }
  7423. else
  7424. {
  7425. buttonDownCount++;
  7426. }
  7427. }
  7428. else
  7429. {
  7430. bExec = HTK_FALSE;
  7431. buttonDownCount = 0;
  7432. }
  7433. }
  7434. #else //FUNC_TEST_SIMU_LOAD_BUTTON
  7435. if((HAL_GPIO_ReadPin(IN_MEM_Erase_GPIO_Port, IN_MEM_Erase_Pin) == GPIO_PIN_RESET) && (Charger.Mode == MODE_IDLE) )
  7436. {
  7437. //prevent idiot to pree this button too long to cause watchdog reset
  7438. HAL_IWDG_Refresh(&hiwdg);
  7439. if(Charger.counter.RST_CFG>3000)
  7440. {
  7441. // Reset EVSE configuration to factory default value
  7442. Charger.memory.EVSE_Config.data.item.bleConfig.isGenPin = OFF;
  7443. Charger.memory.EVSE_Config.data.item.bleConfig.isRegPuk = OFF;
  7444. Charger.memory.EVSE_Config.data.item.AuthMode = AUTH_MODE_OCPP;
  7445. Charger.memory.EVSE_Config.data.item.RFID_SN_Endian = RFID_ENDIAN_LITTLE;
  7446. //Charger.memory.EVSE_Config.data.item.isAuthEnable = OFF;
  7447. Charger.memory.EVSE_Config.data.item.OfflinePolicy = RFID_USER_AUTH_FREE ;
  7448. //Charger.memory.EVSE_Config.data.item.Power_Consumption_Cumulative = 0;
  7449. Charger.memory.EVSE_Config.data.item.reservation.mode = RESERVATION_DISABLE;
  7450. Charger.memory.EVSE_Config.op_bits.update = ON;
  7451. // Reset cold load pick up to factory default value
  7452. Charger.memory.coldLoadPickUp.data.item.isEnable = OFF;
  7453. Charger.memory.coldLoadPickUp.op_bits.update = ON;
  7454. // Reset white list to factory default value
  7455. for(uint8_t idx=0;idx<20;idx++)
  7456. {
  7457. Charger.memory.whiteList.data.item[idx].listType = 0xff;
  7458. Charger.memory.whiteList.data.item[idx].isReg = OFF;
  7459. memset(&Charger.memory.whiteList.data.item[idx].pin[0], 0x00, 6);
  7460. memset(&Charger.memory.whiteList.data.item[idx].listID[0], 0x00, 6);
  7461. }
  7462. Charger.memory.whiteList.op_bits.update = ON;
  7463. //restore led
  7464. setLedMotion(LED_ACTION_RESTORE_SETTING) ;
  7465. timerEnable(TIMER_IDX_LED_TEMP, 3000);
  7466. setChargerMode(MODE_IDLE);
  7467. osDelay(1000);
  7468. }
  7469. else
  7470. Charger.counter.RST_CFG++;
  7471. }
  7472. else
  7473. Charger.counter.RST_CFG = 0;
  7474. #endif //FUNC_TEST_SIMU_LOAD_BUTTON
  7475. /*
  7476. Cold load pick up operation
  7477. */
  7478. if(Charger.memory.coldLoadPickUp.op_bits.read)
  7479. {
  7480. osDelay(100);
  7481. // Read data from block
  7482. if(BSP_W25Qx_Read(Charger.memory.coldLoadPickUp.data.value, MEM_ADDR_COLD_LOAD_PICKUP, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7483. {
  7484. DEBUG_INFO("Read MEM_ADDR_COLD_LOAD_PICKUP block(4k byte) pass.\r\n");
  7485. Charger.memory.coldLoadPickUp.op_bits.read = OFF;
  7486. }
  7487. else
  7488. {
  7489. DEBUG_WARN("Read MEM_ADDR_COLD_LOAD_PICKUP block(4k byte) fail.\r\n");
  7490. }
  7491. }
  7492. if(Charger.memory.coldLoadPickUp.op_bits.update)
  7493. {
  7494. isSuccess = PASS;
  7495. // Erase block data
  7496. if(BSP_W25Qx_Erase_Block(MEM_ADDR_COLD_LOAD_PICKUP) == W25Qx_OK)
  7497. {
  7498. // Write data to block
  7499. for(uint16_t idx=0;idx<((W25Q16FV_BLOCK_SIZE)>>8);idx++)
  7500. {
  7501. 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)
  7502. {
  7503. isSuccess = FAIL;
  7504. }
  7505. }
  7506. if(isSuccess)
  7507. DEBUG_INFO("Update MEM_ADDR_COLD_LOAD_PICKUP success.\r\n");
  7508. else
  7509. DEBUG_WARN("Update MEM_ADDR_COLD_LOAD_PICKUP fail.\r\n");
  7510. Charger.memory.coldLoadPickUp.op_bits.update = OFF;
  7511. }
  7512. else
  7513. {
  7514. DEBUG_WARN("Update MEM_ADDR_COLD_LOAD_PICKUP block(4k byte) fail.\r\n");
  7515. }
  7516. }
  7517. if(Charger.memory.coldLoadPickUp.op_bits.clear)
  7518. {
  7519. // Erase block data
  7520. if(BSP_W25Qx_Erase_Block(MEM_ADDR_COLD_LOAD_PICKUP) == W25Qx_OK)
  7521. {
  7522. // Read data from block
  7523. if(BSP_W25Qx_Read(Charger.memory.coldLoadPickUp.data.value, MEM_ADDR_COLD_LOAD_PICKUP, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7524. {
  7525. DEBUG_INFO("Erase MEM_ADDR_COLD_LOAD_PICKUP block(4k byte) pass.\r\n");
  7526. Charger.memory.coldLoadPickUp.op_bits.clear = OFF;
  7527. }
  7528. else
  7529. {
  7530. DEBUG_WARN("Erase MEM_ADDR_COLD_LOAD_PICKUP block(4k byte) fail.\r\n");
  7531. }
  7532. }
  7533. else
  7534. {
  7535. DEBUG_WARN("Erase MEM_ADDR_COLD_LOAD_PICKUP block(4k byte) fail.\r\n");
  7536. }
  7537. }
  7538. if(Charger.memory.coldLoadPickUp.op_bits.backup)
  7539. {
  7540. isSuccess = PASS;
  7541. // Erase block data
  7542. if(BSP_W25Qx_Erase_Block(MEM_ADDR_BACKUP_COLD_LOAD_PICKUP) == W25Qx_OK)
  7543. {
  7544. // Write data to block
  7545. for(uint16_t idx=0;idx<((W25Q16FV_BLOCK_SIZE)>>8);idx++)
  7546. {
  7547. 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)
  7548. {
  7549. isSuccess = FAIL;
  7550. }
  7551. }
  7552. if(isSuccess)
  7553. DEBUG_INFO("Backup MEM_ADDR_BACKUP_COLD_LOAD_PICKUP success.\r\n");
  7554. else
  7555. DEBUG_WARN("Backup MEM_ADDR_BACKUP_COLD_LOAD_PICKUP fail.\r\n");
  7556. Charger.memory.coldLoadPickUp.op_bits.backup = OFF;
  7557. Charger.memory.coldLoadPickUp.op_bits.clear = ON;
  7558. }
  7559. else
  7560. {
  7561. DEBUG_WARN("Backup MEM_ADDR_BACKUP_COLD_LOAD_PICKUP block(4k byte) fail.\r\n");
  7562. }
  7563. }
  7564. if(Charger.memory.coldLoadPickUp.op_bits.restore)
  7565. {
  7566. // Restore data from backup block
  7567. if(BSP_W25Qx_Read(Charger.memory.coldLoadPickUp.data.value, MEM_ADDR_BACKUP_COLD_LOAD_PICKUP, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7568. {
  7569. DEBUG_INFO("Restore MEM_ADDR_BACKUP_COLD_LOAD_PICKUP block(4k byte) pass.\r\n");
  7570. Charger.memory.coldLoadPickUp.op_bits.restore = OFF;
  7571. }
  7572. else
  7573. {
  7574. DEBUG_WARN("Restore MEM_ADDR_BACKUP_COLD_LOAD_PICKUP block(4k byte) fail.\r\n");
  7575. }
  7576. }
  7577. /*
  7578. White list operation
  7579. */
  7580. if(Charger.memory.whiteList.op_bits.read)
  7581. {
  7582. osDelay(100);
  7583. // Read data from block
  7584. if(BSP_W25Qx_Read(Charger.memory.whiteList.data.value, MEM_ADDR_WHITE_LIST, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7585. {
  7586. DEBUG_INFO("Read MEM_ADDR_WHITE_LIST block(4k byte) pass.\r\n");
  7587. Charger.memory.whiteList.op_bits.read = OFF;
  7588. }
  7589. else
  7590. {
  7591. DEBUG_WARN("Read MEM_ADDR_WHITE_LIST block(4k byte) fail.\r\n");
  7592. }
  7593. }
  7594. if(Charger.memory.whiteList.op_bits.update)
  7595. {
  7596. isSuccess = PASS;
  7597. // Erase block data
  7598. if(BSP_W25Qx_Erase_Block(MEM_ADDR_WHITE_LIST) == W25Qx_OK)
  7599. {
  7600. // Write data to block
  7601. for(uint16_t idx=0;idx<((W25Q16FV_BLOCK_SIZE)>>8);idx++)
  7602. {
  7603. 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)
  7604. {
  7605. isSuccess = FAIL;
  7606. }
  7607. }
  7608. if(isSuccess)
  7609. DEBUG_INFO("Update MEM_ADDR_WHITE_LIST success.\r\n");
  7610. else
  7611. DEBUG_WARN("Update MEM_ADDR_WHITE_LIST fail.\r\n");
  7612. Charger.memory.whiteList.op_bits.update = OFF;
  7613. }
  7614. else
  7615. {
  7616. DEBUG_WARN("Update MEM_ADDR_WHITE_LIST block(4k byte) fail.\r\n");
  7617. }
  7618. }
  7619. if(Charger.memory.whiteList.op_bits.clear)
  7620. {
  7621. // Erase block data
  7622. if(BSP_W25Qx_Erase_Block(MEM_ADDR_WHITE_LIST) == W25Qx_OK)
  7623. {
  7624. // Read data from block
  7625. if(BSP_W25Qx_Read(Charger.memory.whiteList.data.value, MEM_ADDR_WHITE_LIST, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7626. {
  7627. DEBUG_INFO("Erase MEM_ADDR_WHITE_LIST block(4k byte) pass.\r\n");
  7628. Charger.memory.whiteList.op_bits.clear = OFF;
  7629. }
  7630. else
  7631. {
  7632. DEBUG_WARN("Erase MEM_ADDR_WHITE_LIST block(4k byte) fail.\r\n");
  7633. }
  7634. }
  7635. else
  7636. {
  7637. DEBUG_WARN("Erase MEM_ADDR_WHITE_LIST block(4k byte) fail.\r\n");
  7638. }
  7639. }
  7640. /*
  7641. Alarm history operation
  7642. */
  7643. if(Charger.memory.hisAlarm.op_bits.read)
  7644. {
  7645. osDelay(100);
  7646. // Read data from block
  7647. if(BSP_W25Qx_Read(Charger.memory.hisAlarm.data.value, MEM_ADDR_HIS_ALARM, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7648. {
  7649. DEBUG_INFO("Read MEM_ADDR_HIS_ALARM block(4k byte) pass.\r\n");
  7650. Charger.memory.hisAlarm.op_bits.read = OFF;
  7651. }
  7652. else
  7653. {
  7654. DEBUG_WARN("Read MEM_ADDR_HIS_ALARM block(4k byte) fail.\r\n");
  7655. }
  7656. }
  7657. if(Charger.memory.hisAlarm.op_bits.update)
  7658. {
  7659. isSuccess = PASS;
  7660. // Write data to block
  7661. for(uint16_t idx=0;idx<((W25Q16FV_BLOCK_SIZE)>>8);idx++)
  7662. {
  7663. 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)
  7664. {
  7665. isSuccess = FAIL;
  7666. }
  7667. }
  7668. if(isSuccess)
  7669. DEBUG_INFO("Update MEM_ADDR_HIS_ALARM success.\r\n");
  7670. else
  7671. DEBUG_WARN("Update MEM_ADDR_HIS_ALARM fail.\r\n");
  7672. Charger.memory.hisAlarm.op_bits.update = OFF;
  7673. }
  7674. if(Charger.memory.hisAlarm.op_bits.clear)
  7675. {
  7676. // Erase block data
  7677. if(BSP_W25Qx_Erase_Block(MEM_ADDR_HIS_ALARM) == W25Qx_OK)
  7678. {
  7679. // Read data from block
  7680. if(BSP_W25Qx_Read(Charger.memory.hisAlarm.data.value, MEM_ADDR_HIS_ALARM, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7681. {
  7682. DEBUG_INFO("Erase MEM_ADDR_HIS_ALARM block(4k byte) pass.\r\n");
  7683. Charger.memory.hisAlarm.op_bits.clear = OFF;
  7684. }
  7685. else
  7686. {
  7687. DEBUG_WARN("Erase MEM_ADDR_HIS_ALARM block(4k byte) fail.\r\n");
  7688. }
  7689. }
  7690. else
  7691. {
  7692. DEBUG_WARN("Erase MEM_ADDR_HIS_ALARM block(4k byte) fail.\r\n");
  7693. }
  7694. }
  7695. if(Charger.memory.hisAlarm.op_bits.backup)
  7696. {
  7697. isSuccess = PASS;
  7698. // Erase block data
  7699. if(BSP_W25Qx_Erase_Block(MEM_ADDR_BACKUP_HIS_ALARM) == W25Qx_OK)
  7700. {
  7701. // Write data to block
  7702. for(uint16_t idx=0;idx<((W25Q16FV_BLOCK_SIZE)>>8);idx++)
  7703. {
  7704. 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)
  7705. {
  7706. isSuccess = FAIL;
  7707. }
  7708. }
  7709. if(isSuccess)
  7710. DEBUG_INFO("Backup MEM_ADDR_BACKUP_HIS_ALARM success.\r\n");
  7711. else
  7712. DEBUG_WARN("Backup MEM_ADDR_BACKUP_HIS_ALARM fail.\r\n");
  7713. Charger.memory.hisAlarm.op_bits.backup = OFF;
  7714. Charger.memory.hisAlarm.op_bits.clear = ON;
  7715. }
  7716. else
  7717. {
  7718. DEBUG_WARN("Backup MEM_ADDR_BACKUP_HIS_ALARM block(4k byte) fail.\r\n");
  7719. }
  7720. }
  7721. if(Charger.memory.hisAlarm.op_bits.restore)
  7722. {
  7723. // Restore data from backup block
  7724. if(BSP_W25Qx_Read(Charger.memory.hisAlarm.data.value, MEM_ADDR_BACKUP_HIS_ALARM, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7725. {
  7726. DEBUG_INFO("Restore MEM_ADDR_BACKUP_HIS_ALARM block(4k byte) pass.\r\n");
  7727. Charger.memory.hisAlarm.op_bits.restore = OFF;
  7728. }
  7729. else
  7730. {
  7731. DEBUG_WARN("Restore MEM_ADDR_BACKUP_HIS_ALARM block(4k byte) fail.\r\n");
  7732. }
  7733. }
  7734. /*
  7735. Charging history operation
  7736. */
  7737. if(Charger.memory.hisCharging.op_bits.read)
  7738. {
  7739. osDelay(100);
  7740. // Read data from block
  7741. if(BSP_W25Qx_Read(Charger.memory.hisCharging.data.value, MEM_ADDR_HIS_CHARGING, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7742. {
  7743. DEBUG_INFO("Read MEM_ADDR_HIS_CHARGING block(4k byte) pass.\r\n");
  7744. Charger.memory.hisCharging.op_bits.read = OFF;
  7745. }
  7746. else
  7747. {
  7748. DEBUG_WARN("Read MEM_ADDR_HIS_CHARGING block(4k byte) fail.\r\n");
  7749. }
  7750. }
  7751. if(Charger.memory.hisCharging.op_bits.update)
  7752. {
  7753. isSuccess = PASS;
  7754. // Write data to block
  7755. for(uint16_t idx=0;idx<((W25Q16FV_BLOCK_SIZE)>>8);idx++)
  7756. {
  7757. 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)
  7758. {
  7759. isSuccess = FAIL;
  7760. }
  7761. }
  7762. if(isSuccess)
  7763. DEBUG_INFO("Update MEM_ADDR_HIS_CHARGING success.\r\n");
  7764. else
  7765. DEBUG_WARN("Update MEM_ADDR_HIS_CHARGING fail.\r\n");
  7766. Charger.memory.hisCharging.op_bits.update = OFF;
  7767. }
  7768. if(Charger.memory.hisCharging.op_bits.clear)
  7769. {
  7770. // Erase block data
  7771. if(BSP_W25Qx_Erase_Block(MEM_ADDR_HIS_CHARGING) == W25Qx_OK)
  7772. {
  7773. // Read data from block
  7774. if(BSP_W25Qx_Read(Charger.memory.hisCharging.data.value, MEM_ADDR_HIS_CHARGING, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7775. {
  7776. DEBUG_INFO("Erase MEM_ADDR_HIS_CHARGING block(4k byte) pass.\r\n");
  7777. Charger.memory.hisCharging.op_bits.clear = OFF;
  7778. }
  7779. else
  7780. {
  7781. DEBUG_WARN("Erase MEM_ADDR_HIS_CHARGING block(4k byte) fail.\r\n");
  7782. }
  7783. }
  7784. else
  7785. {
  7786. DEBUG_WARN("Erase MEM_ADDR_HIS_CHARGING block(4k byte) fail.\r\n");
  7787. }
  7788. }
  7789. if(Charger.memory.hisCharging.op_bits.backup)
  7790. {
  7791. isSuccess = PASS;
  7792. // Erase block data
  7793. if(BSP_W25Qx_Erase_Block(MEM_ADDR_BACKUP_HIS_CHARGING) == W25Qx_OK)
  7794. {
  7795. // Write data to block
  7796. for(uint16_t idx=0;idx<((W25Q16FV_BLOCK_SIZE)>>8);idx++)
  7797. {
  7798. 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)
  7799. {
  7800. isSuccess = FAIL;
  7801. }
  7802. }
  7803. if(isSuccess)
  7804. DEBUG_INFO("Backup MEM_ADDR_BACKUP_HIS_CHARGING success.\r\n");
  7805. else
  7806. DEBUG_WARN("Backup MEM_ADDR_BACKUP_HIS_CHARGING fail.\r\n");
  7807. Charger.memory.hisCharging.op_bits.backup = OFF;
  7808. Charger.memory.hisCharging.op_bits.clear = ON;
  7809. }
  7810. else
  7811. {
  7812. DEBUG_WARN("Backup MEM_ADDR_BACKUP_HIS_CHARGING block(4k byte) fail.\r\n");
  7813. }
  7814. }
  7815. if(Charger.memory.hisCharging.op_bits.restore)
  7816. {
  7817. // Restore data from backup block
  7818. if(BSP_W25Qx_Read(Charger.memory.hisCharging.data.value, MEM_ADDR_BACKUP_HIS_CHARGING, W25Q16FV_BLOCK_SIZE)== W25Qx_OK)
  7819. {
  7820. DEBUG_INFO("Restore MEM_ADDR_BACKUP_HIS_CHARGING block(4k byte) pass.\r\n");
  7821. Charger.memory.hisCharging.op_bits.restore = OFF;
  7822. }
  7823. else
  7824. {
  7825. DEBUG_WARN("Restore MEM_ADDR_BACKUP_HIS_CHARGING block(4k byte) fail.\r\n");
  7826. }
  7827. }
  7828. osDelay(1);
  7829. }
  7830. /* USER CODE END StartMemoryTask */
  7831. }
  7832. /* USER CODE BEGIN Header_StartWifiTask */
  7833. /**
  7834. * @brief Function implementing the wifiTask thread.
  7835. * @param argument: Not used
  7836. * @retval None
  7837. */
  7838. /* USER CODE END Header_StartWifiTask */
  7839. void StartWifiTask(void const * argument)
  7840. {
  7841. /* USER CODE BEGIN StartWifiTask */
  7842. /* Infinite loop */
  7843. char tmpBuf[128];
  7844. //uint8_t endFlag[4]={0x55,0xaa,0x55,0xaa};
  7845. __IO uint32_t flash;
  7846. //uint32_t checksum;
  7847. uint32_t rndNumber;
  7848. //=====================================
  7849. // Module reset
  7850. //=====================================
  7851. HAL_GPIO_WritePin(OUT_BLE_RESET_GPIO_Port, OUT_BLE_RESET_Pin, GPIO_PIN_RESET);
  7852. osDelay(60);
  7853. HAL_GPIO_WritePin(OUT_BLE_RESET_GPIO_Port, OUT_BLE_RESET_Pin, GPIO_PIN_SET);
  7854. HAL_GPIO_WritePin(OUT_BLE_DSR_GPIO_Port, OUT_BLE_DSR_Pin, GPIO_PIN_SET);
  7855. Charger.wifi.initSeq = 0;
  7856. Charger.wifi.isDataMode = OFF;
  7857. UART_WIFI_Init_OK = OFF;
  7858. osDelay(1000);
  7859. /* Infinite loop */
  7860. for(;;)
  7861. {
  7862. //=====================================
  7863. // WIFI module initialization process
  7864. //=====================================
  7865. switch(Charger.wifi.initSeq)
  7866. {
  7867. case 0x00:
  7868. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)WIFI_CMD[WIFI_CMD_SET_RESTORE], strlen(WIFI_CMD[WIFI_CMD_SET_RESTORE]), 0xffff);
  7869. osDelay(WIFI_INIT_DELAY);
  7870. break;
  7871. case 0x01:
  7872. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)WIFI_CMD[WIFI_CMD_SET_DTR], strlen(WIFI_CMD[WIFI_CMD_SET_DTR]), 0xffff);
  7873. osDelay(WIFI_INIT_DELAY);
  7874. break;
  7875. case 0x02:
  7876. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)WIFI_CMD[WIFI_CMD_SET_DSR], strlen(WIFI_CMD[WIFI_CMD_SET_DSR]), 0xffff);
  7877. osDelay(WIFI_INIT_DELAY);
  7878. break;
  7879. case 0x03:
  7880. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)WIFI_CMD[WIFI_CMD_GET_ADDR], strlen(WIFI_CMD[WIFI_CMD_GET_ADDR]), 0xffff);
  7881. osDelay(WIFI_INIT_DELAY);
  7882. break;
  7883. case 0x04:
  7884. sprintf(tmpBuf, "%s\"EVSE_AP_%sP\"\r\n", WIFI_CMD[WIFI_CMD_SET_SSID], Charger.wifi.module_mac);
  7885. DEBUG_INFO("WIFI module set name: EVSE_AP_%s\r\n", Charger.wifi.module_mac);
  7886. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)tmpBuf, strlen(tmpBuf), 0xffff);
  7887. osDelay(WIFI_INIT_DELAY);
  7888. break;
  7889. case 0x05:
  7890. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)WIFI_CMD[WIFI_CMD_SET_CHANNEL], strlen(WIFI_CMD[WIFI_CMD_SET_CHANNEL]), 0xffff);
  7891. osDelay(WIFI_INIT_DELAY);
  7892. break;
  7893. case 0x06:
  7894. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)WIFI_CMD[WIFI_CMD_SET_SECURITY_MODE], strlen(WIFI_CMD[WIFI_CMD_SET_SECURITY_MODE]), 0xffff);
  7895. osDelay(WIFI_INIT_DELAY);
  7896. break;
  7897. case 0x07:
  7898. sprintf(tmpBuf, "%s\"%s\"\r\n", WIFI_CMD[WIFI_CMD_SET_PASSWORD], Charger.memory.EVSE_Config.data.item.wifiConfig.passwd);
  7899. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)tmpBuf, strlen(tmpBuf), 0xffff);
  7900. osDelay(WIFI_INIT_DELAY);
  7901. break;
  7902. case 0x08:
  7903. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)WIFI_CMD[WIFI_CMD_SET_DHCPSERVER], strlen(WIFI_CMD[WIFI_CMD_SET_DHCPSERVER]), 0xffff);
  7904. osDelay(WIFI_INIT_DELAY);
  7905. break;
  7906. case 0x09:
  7907. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)WIFI_CMD[WIFI_CMD_SET_TCPSETVER_CONFIG], strlen(WIFI_CMD[WIFI_CMD_SET_TCPSETVER_CONFIG]), 0xffff);
  7908. osDelay(WIFI_INIT_DELAY);
  7909. break;
  7910. case 0x0a:
  7911. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)WIFI_CMD[WIFI_CMD_SET_AP_ACTIVE], strlen(WIFI_CMD[WIFI_CMD_SET_AP_ACTIVE]), 0xffff);
  7912. osDelay(WIFI_INIT_DELAY);
  7913. break;
  7914. case 0x0b:
  7915. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)WIFI_CMD[WIFI_CMD_SET_DATA_MODE], strlen(WIFI_CMD[WIFI_CMD_SET_DATA_MODE]), 0xffff);
  7916. DEBUG_INFO("WIFI module switch to data mode.\r\n");
  7917. osDelay(WIFI_INIT_DELAY);
  7918. default:
  7919. UART_WIFI_Init_OK = ON;
  7920. break;
  7921. }
  7922. //=====================================
  7923. // WIFI central device connect status
  7924. //=====================================
  7925. if((HAL_GPIO_ReadPin(IN_BLE_DTR_GPIO_Port, IN_BLE_DTR_Pin) == GPIO_PIN_SET) && (UART_WIFI_Init_OK == ON))
  7926. {
  7927. // Device disconnect
  7928. timerRefresh(TIMER_IDX_WIFI);
  7929. if(Charger.wifi.loginRole == WIFI_LOGIN_ROLE_ROOT || Charger.wifi.loginRole == WIFI_LOGIN_ROLE_USER)
  7930. {
  7931. DEBUG_INFO("WIFI client device disconnect.\r\n.");
  7932. Charger.wifi.loginRole = WIFI_LOGIN_ROLE_UNKOWN;
  7933. }
  7934. }
  7935. else if((HAL_GPIO_ReadPin(IN_BLE_DTR_GPIO_Port, IN_BLE_DTR_Pin) == GPIO_PIN_RESET) && (UART_WIFI_Init_OK == ON))
  7936. {
  7937. // Device connect in
  7938. if(Charger.wifi.loginRole == WIFI_LOGIN_ROLE_UNKOWN)
  7939. {
  7940. DEBUG_INFO("WIFI client device connect.....wait sign in.\r\n.");
  7941. osDelay(1000);
  7942. }
  7943. }
  7944. //=====================================
  7945. // Communication timeout process
  7946. //=====================================
  7947. if(timer[TIMER_IDX_WIFI].isAlarm)
  7948. {
  7949. Charger.wifi.loginRole = WIFI_LOGIN_ROLE_UNKOWN;
  7950. DEBUG_INFO("WIFI no communication over time, disconnect client device.\r\n");
  7951. timerRefresh(TIMER_IDX_WIFI);
  7952. HAL_GPIO_WritePin(OUT_BLE_DSR_GPIO_Port, OUT_BLE_DSR_Pin, GPIO_PIN_RESET);
  7953. osDelay(100);
  7954. HAL_GPIO_WritePin(OUT_BLE_DSR_GPIO_Port, OUT_BLE_DSR_Pin, GPIO_PIN_SET);
  7955. }
  7956. //=====================================
  7957. // WIFI uart recieve data process
  7958. //=====================================
  7959. if(UART_WIFI_recv_end_flag == ON)
  7960. {
  7961. if((HAL_GPIO_ReadPin(IN_BLE_DTR_GPIO_Port, IN_BLE_DTR_Pin) == GPIO_PIN_SET) || !Charger.wifi.isDataMode)
  7962. {
  7963. // AT command rx process
  7964. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_RESTORE])!=NULL)
  7965. {
  7966. Charger.wifi.initSeq++;
  7967. }
  7968. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_DTR])!=NULL)
  7969. {
  7970. Charger.wifi.initSeq++;
  7971. }
  7972. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_DSR])!=NULL)
  7973. {
  7974. Charger.wifi.initSeq++;
  7975. }
  7976. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_GET_ADDR])!=NULL)
  7977. {
  7978. memcpy(Charger.wifi.module_mac, &UART_WIFI_rx_buffer[strcspn((char *)UART_WIFI_rx_buffer, ":")+1], 12);
  7979. Charger.wifi.module_mac[12] = '\0';
  7980. DEBUG_INFO("Wifi module mac address: %s\r\n", Charger.wifi.module_mac);
  7981. Charger.wifi.initSeq++;
  7982. }
  7983. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_SSID])!=NULL)
  7984. {
  7985. Charger.wifi.initSeq++;
  7986. }
  7987. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_CHANNEL])!=NULL)
  7988. {
  7989. Charger.wifi.initSeq++;
  7990. }
  7991. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_SECURITY_MODE])!=NULL)
  7992. {
  7993. Charger.wifi.initSeq++;
  7994. }
  7995. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_PASSWORD])!=NULL)
  7996. {
  7997. Charger.wifi.initSeq++;
  7998. }
  7999. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_DHCPSERVER])!=NULL)
  8000. {
  8001. Charger.wifi.initSeq++;
  8002. }
  8003. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_TCPSETVER_CONFIG])!=NULL)
  8004. {
  8005. Charger.wifi.initSeq++;
  8006. }
  8007. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_AP_ACTIVE])!=NULL)
  8008. {
  8009. Charger.wifi.initSeq++;
  8010. }
  8011. if(strstr((char *)UART_WIFI_rx_buffer, WIFI_CMD[WIFI_CMD_SET_DATA_MODE])!=NULL)
  8012. {
  8013. Charger.wifi.isDataMode = ON;
  8014. Charger.wifi.initSeq++;
  8015. }
  8016. }
  8017. else
  8018. {
  8019. // Application protocol rx process
  8020. uint8_t tx[UART_BUFFER_SIZE];
  8021. uint8_t tx_len;
  8022. uint8_t chksum = 0;
  8023. 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))))
  8024. {
  8025. // Timer reset
  8026. timerRefresh(TIMER_IDX_WIFI);
  8027. 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));
  8028. switch(UART_WIFI_rx_buffer[2])
  8029. {
  8030. case WIFI_PROTOCOL_MESSAGE_SIGN_IN:
  8031. DEBUG_INFO("WIFI device sign request...%d\r\n", UART_WIFI_rx_buffer[7]);
  8032. tx_len = 5;
  8033. tx[0] = (tx_len>>8)&0xff;
  8034. tx[1] = (tx_len&0xff);
  8035. tx[2] = UART_WIFI_rx_buffer[2] + 0x80;
  8036. tx[3] = 0x00;
  8037. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  8038. {
  8039. chksum ^= tx[2 + idx];
  8040. }
  8041. tx[4] = chksum;
  8042. break;
  8043. default:
  8044. /*
  8045. TODO: Unknow command response here
  8046. */
  8047. tx_len = 5;
  8048. tx[0] = 0x00;
  8049. tx[1] = 0x05;
  8050. tx[2] = UART_WIFI_rx_buffer[2] + 0x80;
  8051. tx[3] = 0x00;
  8052. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  8053. {
  8054. chksum ^= tx[2 + idx];
  8055. }
  8056. tx[4] = chksum;
  8057. DEBUG_ERROR("WIFI unknow command.\r\n");
  8058. break;
  8059. }
  8060. }
  8061. else
  8062. {
  8063. /*
  8064. TODO: Protocol check sum error response here
  8065. */
  8066. tx_len = 5;
  8067. tx[0] = 0x00;
  8068. tx[1] = 0x05;
  8069. tx[2] = UART_WIFI_rx_buffer[2] + 0x80;
  8070. tx[3] = 0x00;
  8071. for(uint16_t idx=0;idx<((tx[1] | (tx[0]<<8))-3);idx++)
  8072. {
  8073. chksum ^= tx[2 + idx];
  8074. }
  8075. tx[4] = chksum;
  8076. DEBUG_ERROR("WIFI check sum validation error.\r\n");
  8077. }
  8078. HAL_UART_Transmit(&WIFI_USART, (uint8_t *)tx, tx_len, 0xffff);
  8079. }
  8080. UART_WIFI_rx_len=0;
  8081. UART_WIFI_recv_end_flag=0;
  8082. }
  8083. HAL_UART_Receive_DMA(&WIFI_USART, (uint8_t*)UART_WIFI_rx_buffer, UART_BUFFER_SIZE);
  8084. osDelay(1);
  8085. }
  8086. /* USER CODE END StartWifiTask */
  8087. }
  8088. /* Private application code --------------------------------------------------*/
  8089. /* USER CODE BEGIN Application */
  8090. #ifdef MODIFY_IMCP
  8091. void UpdateCheckSum(uint8_t *Buf, uint16_t Len) //Len: Total package length
  8092. {
  8093. uint16_t DataLen = Len - (IMCP_FM_HEAD_LEN + IMCP_FM_TAIL_LEN);
  8094. uint8_t CheckSum = 0;
  8095. //Update DataLen
  8096. HTK_U16(Buf[IMCP_FM_DATALEN_LSB_IDX]) = DataLen;
  8097. //Update CheckSum
  8098. for (uint16_t i = 0; i < DataLen; i++)
  8099. {
  8100. CheckSum ^= Buf[IMCP_FM_DATA_BEG_IDX + i];
  8101. }
  8102. Buf[Len - 1] = CheckSum;
  8103. }
  8104. #ifdef FUNC_RS485_SLAVE
  8105. uint8_t isValidCheckSum_IAP(uint8_t *RxBuf)
  8106. {
  8107. uint8_t result = OFF;
  8108. uint8_t chksum = 0x00;
  8109. #ifdef FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  8110. if (RxBuf[IMCP_FM_FRAMEID_IDX] != PROTOCOL_HEAD)
  8111. {
  8112. return FAIL;
  8113. }
  8114. else if (RxBuf == UART_IAP_rx_buffer)
  8115. {
  8116. if (RxBuf[IMCP_FM_DSTADDR_IDX] != PROTOCOL_ADDR &&
  8117. RxBuf[IMCP_FM_DSTADDR_IDX] != PROTOCOL_ADDR_AC_MAINBOARD &&
  8118. RxBuf[IMCP_FM_DSTADDR_IDX] != PROTOCOL_ADDR_BROADCAST)
  8119. {
  8120. return FAIL;
  8121. }
  8122. }
  8123. else if (RxBuf == UART_RS485_rx_buffer)
  8124. {
  8125. if (RxBuf[IMCP_FM_SRCADDR_IDX] != EMPX10_RS485_ADDR_CSU)
  8126. {
  8127. return FAIL;
  8128. }
  8129. else if (
  8130. RxBuf[IMCP_FM_DSTADDR_IDX] != Charger.m_EMPX10_RS485_ClientAddr &&
  8131. RxBuf[IMCP_FM_DSTADDR_IDX] != EMPX10_RS485_ADDR_BROADCAST)
  8132. {
  8133. return FAIL;
  8134. }
  8135. }
  8136. #else //FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  8137. if (
  8138. (RxBuf[IMCP_FM_FRAMEID_IDX] == PROTOCOL_HEAD) &&
  8139. (
  8140. (RxBuf[IMCP_FM_DSTADDR_IDX] == PROTOCOL_ADDR)
  8141. || (RxBuf[IMCP_FM_DSTADDR_IDX] == PROTOCOL_ADDR_AC_MAINBOARD)
  8142. || (RxBuf[IMCP_FM_DSTADDR_IDX] == PROTOCOL_ADDR_BROADCAST)
  8143. )
  8144. )
  8145. #endif //FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  8146. {
  8147. uint16_t nCheckSumIdx = IMCP_FM_DATA_BEG_IDX + HTK_U16(RxBuf[IMCP_FM_DATALEN_LSB_IDX]);
  8148. for(uint16_t i = IMCP_FM_DATA_BEG_IDX; i < nCheckSumIdx; i++)
  8149. {
  8150. chksum ^= RxBuf[i];
  8151. }
  8152. if(chksum == RxBuf[nCheckSumIdx])
  8153. result = ON;
  8154. }
  8155. return result;
  8156. }
  8157. #else //FUNC_RS485_SLAVE
  8158. uint8_t isValidCheckSum_IAP(void)
  8159. {
  8160. uint8_t result = OFF;
  8161. uint8_t chksum = 0x00;
  8162. if (
  8163. (UART_IAP_rx_buffer[IMCP_FM_FRAMEID_IDX] == PROTOCOL_HEAD) &&
  8164. (
  8165. (UART_IAP_rx_buffer[IMCP_FM_DSTADDR_IDX] == PROTOCOL_ADDR)
  8166. || (UART_IAP_rx_buffer[IMCP_FM_DSTADDR_IDX] == PROTOCOL_ADDR_BROADCAST)
  8167. )
  8168. )
  8169. {
  8170. uint16_t nCheckSumIdx = IMCP_FM_DATA_BEG_IDX + HTK_U16(UART_IAP_rx_buffer[IMCP_FM_DATALEN_LSB_IDX]);
  8171. for(uint16_t i = IMCP_FM_DATA_BEG_IDX; i < nCheckSumIdx; i++)
  8172. {
  8173. chksum ^= UART_IAP_rx_buffer[i];
  8174. }
  8175. if(chksum == UART_IAP_rx_buffer[nCheckSumIdx])
  8176. result = ON;
  8177. }
  8178. return result;
  8179. }
  8180. #endif //FUNC_RS485_SLAVE
  8181. #else //MODIFY_IMCP
  8182. uint8_t isValidCheckSum_IAP(void)
  8183. {
  8184. uint8_t result = OFF;
  8185. uint8_t chksum = 0x00;
  8186. if((UART_IAP_rx_buffer[0] == PROTOCOL_HEAD) &&
  8187. ((UART_IAP_rx_buffer[2] == PROTOCOL_ADDR) | (UART_IAP_rx_buffer[2] == PROTOCOL_ADDR_BROADCAST)))
  8188. {
  8189. for(int idx=0;idx<(UART_IAP_rx_buffer[4] | (UART_IAP_rx_buffer[5]<<8));idx++)
  8190. {
  8191. chksum ^= UART_IAP_rx_buffer[6+ idx];
  8192. }
  8193. if(chksum == UART_IAP_rx_buffer[6+(UART_IAP_rx_buffer[4] | (UART_IAP_rx_buffer[5]<<8))])
  8194. result = ON;
  8195. }
  8196. return result;
  8197. }
  8198. #endif //MODIFY_IMCP
  8199. //-------------------------------------------------------------------------------------------------
  8200. uint8_t isValidCheckSum_BLE(void)
  8201. {
  8202. uint8_t result = OFF;
  8203. uint8_t chksum = 0x00;
  8204. if(((UART_BLE_rx_buffer[1] | (UART_BLE_rx_buffer[0]<<8))-1) < UART_BUFFER_SIZE)
  8205. {
  8206. for(int idx=2;idx<((UART_BLE_rx_buffer[1] | (UART_BLE_rx_buffer[0]<<8))-1);idx++)
  8207. {
  8208. chksum ^= UART_BLE_rx_buffer[idx];
  8209. }
  8210. if(chksum == UART_BLE_rx_buffer[(UART_BLE_rx_buffer[1] | (UART_BLE_rx_buffer[0]<<8))-1])
  8211. result = ON;
  8212. }
  8213. return result;
  8214. }
  8215. //-------------------------------------------------------------------------------------------------
  8216. uint8_t isValidCheckSum_WIFI(void)
  8217. {
  8218. uint8_t result = OFF;
  8219. uint8_t chksum = 0x00;
  8220. if(((UART_WIFI_rx_buffer[1] | (UART_WIFI_rx_buffer[0]<<8))-1) < UART_BUFFER_SIZE)
  8221. {
  8222. for(int idx=2;idx<((UART_WIFI_rx_buffer[1] | (UART_WIFI_rx_buffer[0]<<8))-1);idx++)
  8223. {
  8224. chksum ^= UART_WIFI_rx_buffer[idx];
  8225. }
  8226. if(chksum == UART_WIFI_rx_buffer[(UART_WIFI_rx_buffer[1] | (UART_WIFI_rx_buffer[0]<<8))-1])
  8227. result = ON;
  8228. }
  8229. return result;
  8230. }
  8231. //-------------------------------------------------------------------------------------------------
  8232. #ifdef FUNC_DEBUG_MSG_SET_CHARGER_MODE
  8233. char* getModeName(uint8_t mode);
  8234. #endif
  8235. void setChargerMode(uint8_t mode)
  8236. {
  8237. #ifdef FUNC_DEBUG_MSG_SET_CHARGER_MODE
  8238. XP("#SET CHARGER MODE: [%s] >>> [%s]\r\n", getModeName(Charger.Mode_Previous), getModeName(mode));
  8239. #endif
  8240. Charger.Mode_Previous = Charger.Mode;
  8241. Charger.Mode = mode;
  8242. }
  8243. //-------------------------------------------------------------------------------------------------
  8244. char* getModeName(uint8_t mode)
  8245. {
  8246. switch(mode)
  8247. {
  8248. case MODE_INITIAL:
  8249. return "INITIAL";
  8250. case MODE_IDLE:
  8251. return "IDLE";
  8252. case MODE_HANDSHAKE:
  8253. return "HANDSHAKING";
  8254. case MODE_CHARGING:
  8255. return "CHARGING";
  8256. case MODE_STOP:
  8257. return "STOP";
  8258. case MODE_ALARM:
  8259. return "ALARM";
  8260. case MODE_MAINTAIN:
  8261. return "MAINTAIN";
  8262. case MODE_DEBUG:
  8263. return "DEBUG";
  8264. default:
  8265. return "UNKNOW";
  8266. }
  8267. }
  8268. //-------------------------------------------------------------------------------------------------
  8269. uint8_t isModeChange(void)
  8270. {
  8271. uint8_t result = NO;
  8272. if(!isMode(Charger.Mode_Previous))
  8273. {
  8274. DEBUG_INFO("#MODE from %s to %s\r\n", getModeName(Charger.Mode_Previous), getModeName(Charger.Mode));
  8275. result |= YES;
  8276. if(Charger.Mode == MODE_ALARM)
  8277. {
  8278. Charger.Mode_Before_Alarm = Charger.Mode_Previous;
  8279. }
  8280. Charger.Mode_Previous = Charger.Mode;
  8281. }
  8282. return result;
  8283. }
  8284. //-------------------------------------------------------------------------------------------------
  8285. uint8_t isMode(uint8_t mode)
  8286. {
  8287. return ((Charger.Mode == mode)? YES : NO);
  8288. }
  8289. //-------------------------------------------------------------------------------------------------
  8290. uint8_t rfidCheckSumCal(uint8_t *data, uint8_t length)
  8291. {
  8292. uint8_t result= 0x00;
  8293. for(uint8_t idx=0;idx<length;idx++)
  8294. result ^= data[idx];
  8295. return result;
  8296. }
  8297. //-------------------------------------------------------------------------------------------------
  8298. void HAL_PWR_PVDCallback(void)
  8299. {
  8300. HAL_Delay(100);
  8301. NVIC_SystemReset();
  8302. }
  8303. //-------------------------------------------------------------------------------------------------
  8304. uint8_t Test_LeakModule(void)
  8305. {
  8306. // uint8_t result = PASS, DC_check = false, AC_check=false;
  8307. // uint16_t time_out_count;
  8308. // uint8_t retry_cnt = 0 ;
  8309. uint8_t result = PASS;
  8310. #ifdef CCID_PROTECT
  8311. #ifdef FUNC_SWITCH_RELAY_WELDING_PROTECT
  8312. if (Charger.m_bDisableCCID == 0 && ((HAL_GetTick() - Charger.m_BootTick) / 1000) > 30)
  8313. {
  8314. #endif
  8315. uint8_t DC_check = false;
  8316. uint8_t AC_check=false;
  8317. uint16_t time_out_count;
  8318. uint8_t retry_cnt = 0 ;
  8319. #ifdef FUNC_AUTO_MODIFY_CCID_MODULE
  8320. XP("#Test_LeakModule use CCID Module: %s\r\n", CCID_MODULE_TYPE_STR);
  8321. #endif
  8322. if (Charger.CCID_Module_Type == CCID_MODULE_VAC)
  8323. {
  8324. osDelay(500);
  8325. if(HAL_GPIO_ReadPin(IN_Leak_Error_GPIO_Port, IN_Leak_Error_Pin) == GPIO_PIN_SET)
  8326. {
  8327. DEBUG_INFO("VAC Leak Error pin ON.\r\n");
  8328. result = FAIL;
  8329. }
  8330. else
  8331. {
  8332. HAL_Delay(270);
  8333. HAL_GPIO_WritePin(OUT_Leak_Test_GPIO_Port , OUT_Leak_Test_Pin , GPIO_PIN_RESET);
  8334. HAL_Delay(50); //40ms~1.2sec
  8335. HAL_GPIO_WritePin(OUT_Leak_Test_GPIO_Port , OUT_Leak_Test_Pin , GPIO_PIN_SET);
  8336. //HAL_Delay(40); //36ms
  8337. time_out_count = 0;
  8338. while(1)
  8339. {
  8340. if(HAL_GPIO_ReadPin(IN_Leak_DC_GPIO_Port, IN_Leak_DC_Pin) == GPIO_PIN_SET)
  8341. {
  8342. DC_check = true;
  8343. }
  8344. if(HAL_GPIO_ReadPin(IN_Leak_AC_GPIO_Port, IN_Leak_AC_Pin) == GPIO_PIN_SET)
  8345. {
  8346. AC_check = true;
  8347. }
  8348. //if(DC_check&&AC_check)
  8349. 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))
  8350. {
  8351. break;
  8352. }
  8353. if(time_out_count>2000)
  8354. {
  8355. //result = FAIL;
  8356. if(retry_cnt < 1 )
  8357. {
  8358. HAL_Delay(270);
  8359. HAL_GPIO_WritePin(OUT_Leak_Test_GPIO_Port , OUT_Leak_Test_Pin , GPIO_PIN_RESET);
  8360. HAL_Delay(50); //40ms~1.2sec
  8361. HAL_GPIO_WritePin(OUT_Leak_Test_GPIO_Port , OUT_Leak_Test_Pin , GPIO_PIN_SET);
  8362. //HAL_Delay(40); //36ms
  8363. retry_cnt ++ ;
  8364. time_out_count = 0 ;
  8365. }
  8366. else
  8367. {
  8368. DEBUG_INFO("Leak module self test Fail in timeout.\r\n");
  8369. result = FAIL;
  8370. break;
  8371. }
  8372. }
  8373. else
  8374. {
  8375. time_out_count++;
  8376. HAL_Delay(1);
  8377. }
  8378. }
  8379. }
  8380. }
  8381. else // Charger.CCID_Module_Type is CCID_MODULE_CORMEX
  8382. {
  8383. SPWM_Switch(ON);
  8384. HAL_Delay(1000);
  8385. XP("#[SPWM] adc_value.ADC2_IN6_GF.value = %d\r\n",adc_value.ADC2_IN6_GF.value);
  8386. if ((adc_value.ADC2_IN6_GF.value<3000)&&(adc_value.ADC2_IN6_GF.value>2000)) //25mA
  8387. {
  8388. DEBUG_INFO("#Test_LeakModule(): OK\r\n");
  8389. result = PASS ;
  8390. }
  8391. else
  8392. {
  8393. DEBUG_INFO("Test_LeakModule(): NG\r\n");
  8394. //DEBUG_INFO("adc_value.ADC2_IN6_GF.value = %d\n\r",adc_value.ADC2_IN6_GF.value);
  8395. result = FAIL;
  8396. }
  8397. SPWM_Switch(OFF);
  8398. }
  8399. #ifdef FUNC_SWITCH_RELAY_WELDING_PROTECT
  8400. }
  8401. #endif
  8402. #endif //CCID_PROTECT
  8403. return result;
  8404. }
  8405. //-------------------------------------------------------------------------------------------------
  8406. uint8_t Valid_RFID_SN(void)
  8407. {
  8408. /*
  8409. TODO: Validation logic check
  8410. */
  8411. uint8_t result = FAIL;
  8412. uint32_t crc_result;
  8413. uint8_t sn_len;
  8414. uint8_t *buf;
  8415. if(Charger.rfid.snType == RFID_SN_TYPE_4BYTE)
  8416. {
  8417. // SN length: 4
  8418. sn_len = 4;
  8419. }
  8420. else if(Charger.rfid.snType == RFID_SN_TYPE_7BYTE)
  8421. {
  8422. // SN length: 7
  8423. sn_len = 7;
  8424. }
  8425. else
  8426. {
  8427. // SN length: 10
  8428. sn_len = 10;
  8429. }
  8430. buf = malloc(25 + sn_len);
  8431. memcpy(buf, Charger.memory.EVSE_Config.data.item.System_ID, 25);
  8432. memcpy(&buf[25], Charger.rfid.data_Sn, sn_len);
  8433. crc_result = crc32(buf, 25 + sn_len);
  8434. 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)
  8435. {
  8436. result = PASS;
  8437. }
  8438. free(buf);
  8439. return result;
  8440. }
  8441. //-------------------------------------------------------------------------------------------------
  8442. void recordAlarmHis(void)
  8443. {
  8444. uint8_t isSuccess = FAIL;
  8445. // Check record alarm request, if alarm code bit different with previous
  8446. for(uint8_t errIdx=0;errIdx<32;errIdx++)
  8447. {
  8448. if(((Charger.Alarm_Code>>errIdx)&0x01) != ((Charger.Alarm_Code_Previous>>errIdx)&0x01))
  8449. {
  8450. // Find empty memory space
  8451. for(uint16_t idx=0;idx<(W25Q16FV_BLOCK_SIZE>>4);idx++)
  8452. {
  8453. if((Charger.memory.hisAlarm.data.item[idx].alarmCode == 0xffffffff) &&
  8454. (Charger.memory.hisAlarm.data.item[idx].mode == 0xff))
  8455. {
  8456. isSuccess = PASS;
  8457. Charger.memory.hisAlarm.data.item[idx].DateTime.year = Charger.memory.EVSE_Config.data.item.SystemDateTime.year;
  8458. Charger.memory.hisAlarm.data.item[idx].DateTime.month = Charger.memory.EVSE_Config.data.item.SystemDateTime.month;
  8459. Charger.memory.hisAlarm.data.item[idx].DateTime.day = Charger.memory.EVSE_Config.data.item.SystemDateTime.day;
  8460. Charger.memory.hisAlarm.data.item[idx].DateTime.hour = Charger.memory.EVSE_Config.data.item.SystemDateTime.hour;
  8461. Charger.memory.hisAlarm.data.item[idx].DateTime.min = Charger.memory.EVSE_Config.data.item.SystemDateTime.min;
  8462. Charger.memory.hisAlarm.data.item[idx].DateTime.sec = Charger.memory.EVSE_Config.data.item.SystemDateTime.sec;
  8463. Charger.memory.hisAlarm.data.item[idx].mode = Charger.Mode;
  8464. if((Charger.Alarm_Code>>errIdx)&0x01)
  8465. {
  8466. // Record alarm occur
  8467. Charger.memory.hisAlarm.data.item[idx].alarmCode = alarm_record_code[errIdx];
  8468. Charger.memory.hisAlarm.data.item[idx].isRecover = OFF;
  8469. Charger.Alarm_Code_Previous |= (0x00000001<<errIdx);
  8470. }
  8471. else
  8472. {
  8473. // Record alarm recover
  8474. Charger.memory.hisAlarm.data.item[idx].alarmCode = alarm_record_code[errIdx];
  8475. Charger.memory.hisAlarm.data.item[idx].isRecover = ON;
  8476. Charger.Alarm_Code_Previous &= ~(0x00000001<<errIdx);
  8477. }
  8478. Charger.memory.hisAlarm.op_bits.update = ON;
  8479. // If speace is last item, move data to backup area
  8480. if(idx == ((W25Q16FV_BLOCK_SIZE>>4)-1))
  8481. {
  8482. osDelay(500);
  8483. Charger.memory.hisAlarm.op_bits.backup = ON;
  8484. osDelay(500);
  8485. }
  8486. break;
  8487. }
  8488. }
  8489. // If there is not any free space
  8490. if(!isSuccess)
  8491. {
  8492. osDelay(500);
  8493. Charger.memory.hisAlarm.op_bits.backup = ON;
  8494. osDelay(500);
  8495. }
  8496. }
  8497. }
  8498. }
  8499. //-------------------------------------------------------------------------------------------------
  8500. uint8_t recordChargingHis(uint8_t isColdLoadPickUp, uint32_t statusCode)
  8501. {
  8502. uint8_t isSuccess = FAIL;
  8503. if(isColdLoadPickUp)
  8504. {
  8505. for(uint16_t idx=0;idx<(W25Q16FV_BLOCK_SIZE>>6);idx++)
  8506. {
  8507. if((Charger.memory.hisCharging.data.item[idx].Duration == 0xffffffff) &&
  8508. (Charger.memory.hisCharging.data.item[idx].powerSum == 0xffff) &&
  8509. (Charger.memory.hisCharging.data.item[idx].stopStatusCode == 0xffffffff) )
  8510. {
  8511. isSuccess = PASS;
  8512. Charger.memory.hisCharging.data.item[idx].DateTime.year = Charger.memory.coldLoadPickUp.data.item.DateTime.year;
  8513. Charger.memory.hisCharging.data.item[idx].DateTime.month = Charger.memory.coldLoadPickUp.data.item.DateTime.month;
  8514. Charger.memory.hisCharging.data.item[idx].DateTime.day = Charger.memory.coldLoadPickUp.data.item.DateTime.day;
  8515. Charger.memory.hisCharging.data.item[idx].DateTime.hour = Charger.memory.coldLoadPickUp.data.item.DateTime.hour;
  8516. Charger.memory.hisCharging.data.item[idx].DateTime.min = Charger.memory.coldLoadPickUp.data.item.DateTime.min;
  8517. Charger.memory.hisCharging.data.item[idx].DateTime.sec = Charger.memory.coldLoadPickUp.data.item.DateTime.sec;
  8518. Charger.memory.hisCharging.data.item[idx].Duration = Charger.memory.coldLoadPickUp.data.item.Duration;
  8519. sprintf(Charger.memory.hisCharging.data.item[idx].PlugType, EVSE_CONNECTOR_TYPE);
  8520. Charger.memory.hisCharging.data.item[idx].powerSum = Charger.memory.coldLoadPickUp.data.item.powerSum;
  8521. Charger.memory.hisCharging.data.item[idx].startType = Charger.memory.coldLoadPickUp.data.item.startType;
  8522. Charger.memory.hisCharging.data.item[idx].stopStatusCode = statusCode;
  8523. memcpy(Charger.memory.hisCharging.data.item[idx].user, Charger.rfid.currentCard, 16);
  8524. Charger.memory.hisCharging.op_bits.update = ON;
  8525. // If speace is last item, move data to backup area
  8526. if(idx == ((W25Q16FV_BLOCK_SIZE>>4)-1))
  8527. {
  8528. osDelay(500);
  8529. Charger.memory.hisCharging.op_bits.backup = ON;
  8530. }
  8531. break;
  8532. }
  8533. }
  8534. }
  8535. else
  8536. {
  8537. for(uint16_t idx=0;idx<(W25Q16FV_BLOCK_SIZE>>6);idx++)
  8538. {
  8539. if((Charger.memory.hisCharging.data.item[idx].Duration == 0xffffffff) &&
  8540. (Charger.memory.hisCharging.data.item[idx].powerSum == 0xffff) &&
  8541. (Charger.memory.hisCharging.data.item[idx].stopStatusCode == 0xffffffff) )
  8542. {
  8543. isSuccess = PASS;
  8544. Charger.memory.hisCharging.data.item[idx].DateTime.year = Charger.cycle_info.startDateTime.year;
  8545. Charger.memory.hisCharging.data.item[idx].DateTime.month = Charger.cycle_info.startDateTime.month;
  8546. Charger.memory.hisCharging.data.item[idx].DateTime.day = Charger.cycle_info.startDateTime.day;
  8547. Charger.memory.hisCharging.data.item[idx].DateTime.hour = Charger.cycle_info.startDateTime.hour;
  8548. Charger.memory.hisCharging.data.item[idx].DateTime.min = Charger.cycle_info.startDateTime.min;
  8549. Charger.memory.hisCharging.data.item[idx].DateTime.sec = Charger.cycle_info.startDateTime.sec;
  8550. Charger.memory.hisCharging.data.item[idx].Duration = Charger.cycle_info.Duration;
  8551. sprintf(Charger.memory.hisCharging.data.item[idx].PlugType, EVSE_CONNECTOR_TYPE);
  8552. Charger.memory.hisCharging.data.item[idx].powerSum = Charger.cycle_info.Power_Consumption;
  8553. Charger.memory.hisCharging.data.item[idx].startType = Charger.cycle_info.StartType;
  8554. Charger.memory.hisCharging.data.item[idx].stopStatusCode = statusCode;
  8555. memcpy(Charger.memory.hisCharging.data.item[idx].user, Charger.rfid.currentCard, 16);
  8556. Charger.memory.hisCharging.op_bits.update = ON;
  8557. // If speace is last item, move data to backup area
  8558. if(idx == ((W25Q16FV_BLOCK_SIZE>>4)-1))
  8559. {
  8560. osDelay(500);
  8561. Charger.memory.hisCharging.op_bits.backup = ON;
  8562. }
  8563. break;
  8564. }
  8565. }
  8566. }
  8567. // If there is not any free space
  8568. if(!isSuccess)
  8569. {
  8570. osDelay(500);
  8571. Charger.memory.hisCharging.op_bits.backup = ON;
  8572. }
  8573. return isSuccess;
  8574. }
  8575. //-------------------------------------------------------------------------------------------------
  8576. void setLedMotion(uint8_t action)
  8577. {//ssetled
  8578. //standard
  8579. switch(action)
  8580. {
  8581. case LED_ACTION_INIT:
  8582. Charger.led_R.motion = LED_MOTION_ON;
  8583. Charger.led_G.motion = LED_MOTION_ON;
  8584. Charger.led_B.motion = LED_MOTION_ON;
  8585. Charger.led_W.motion = LED_MOTION_OFF;
  8586. LedOnRGBSet(1000, 1000, 1000, 0);
  8587. break;
  8588. case LED_ACTION_IDLE: //eMP: IDLE BACKEND ONLINE
  8589. Charger.led_R.motion = LED_MOTION_OFF;
  8590. Charger.led_G.motion = LED_MOTION_ON;
  8591. Charger.led_B.motion = LED_MOTION_OFF;
  8592. Charger.led_W.motion = LED_MOTION_OFF;
  8593. LedOnRGBSet(0, 1000, 0, 0);
  8594. break;
  8595. case LED_ACTION_AUTHED:
  8596. Charger.led_R.motion = LED_MOTION_OFF;
  8597. Charger.led_G.motion = LED_MOTION_OFF;
  8598. Charger.led_B.motion = LED_MOTION_ON;
  8599. Charger.led_W.motion = LED_MOTION_OFF;
  8600. LedOnRGBSet(0, 0, 1000, 0);
  8601. break;
  8602. case LED_ACTION_CONNECTED: //eMP: IDLE BACKEND OFFLINE
  8603. #ifdef FUNC_EMPX10_LED
  8604. Charger.led_R.motion = LED_MOTION_ON;
  8605. Charger.led_G.motion = LED_MOTION_ON;
  8606. Charger.led_B.motion = LED_MOTION_OFF;
  8607. Charger.led_W.motion = LED_MOTION_OFF;
  8608. LedOnRGBSet(1000, 500, 0, 0);
  8609. #else
  8610. Charger.led_R.motion = LED_MOTION_OFF;
  8611. Charger.led_G.motion = LED_MOTION_OFF;
  8612. Charger.led_B.motion = LED_MOTION_ON;
  8613. Charger.led_W.motion = LED_MOTION_OFF;
  8614. LedOnRGBSet(0, 0, 1000, 0);
  8615. #endif
  8616. break;
  8617. case LED_ACTION_CHARGING:
  8618. Charger.led_R.motion = LED_MOTION_OFF;
  8619. Charger.led_G.motion = LED_MOTION_OFF;
  8620. Charger.led_B.motion = LED_MOTION_BLINK;
  8621. Charger.led_W.motion = LED_MOTION_OFF;
  8622. LedOnRGBSet(0, 0, 1000, 0);
  8623. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 0, 1);
  8624. break;
  8625. case LED_ACTION_STOP:
  8626. Charger.led_R.motion = LED_MOTION_OFF;
  8627. Charger.led_G.motion = LED_MOTION_OFF;
  8628. Charger.led_B.motion = LED_MOTION_ON;
  8629. Charger.led_W.motion = LED_MOTION_OFF;
  8630. LedOnRGBSet(0, 0, 1000, 0);
  8631. break;
  8632. case LED_ACTION_ALARM:
  8633. #ifdef FUNC_EMPX10_LED_ALARM_FIX_20240730
  8634. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  8635. Charger.Alarm_LED = Charger.Alarm_Code;
  8636. }else{
  8637. Charger.Alarm_LED = Charger.am3352.LedAlarmState;
  8638. }
  8639. if(Charger.Alarm_LED & ALARM_EMERGENCY_STOP)
  8640. {
  8641. Charger.led_R.motion = LED_MOTION_BLINK;
  8642. Charger.led_G.motion = LED_MOTION_OFF;
  8643. Charger.led_B.motion = LED_MOTION_OFF;
  8644. Charger.led_W.motion = LED_MOTION_OFF;
  8645. LedOnRGBSet(1000, 0, 0, 0);
  8646. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 3000, 7);
  8647. }
  8648. else
  8649. {
  8650. Charger.led_R.motion = LED_MOTION_ON;
  8651. Charger.led_G.motion = LED_MOTION_OFF;
  8652. Charger.led_B.motion = LED_MOTION_OFF;
  8653. Charger.led_W.motion = LED_MOTION_OFF;
  8654. LedOnRGBSet(1000, 0, 0, 0);
  8655. }
  8656. #else //FUNC_EMPX10_LED_ALARM_FIX_20240730
  8657. Charger.led_R.motion = LED_MOTION_BLINK;
  8658. Charger.led_G.motion = LED_MOTION_OFF;
  8659. Charger.led_B.motion = LED_MOTION_OFF;
  8660. Charger.led_W.motion = LED_MOTION_OFF;
  8661. LedOnRGBSet(1000, 0, 0, 0);
  8662. //set alarm led blink duty
  8663. if(Charger.memory.EVSE_Config.data.item.MCU_Control_Mode == MCU_CONTROL_MODE_NO_CSU){
  8664. Charger.Alarm_LED = Charger.Alarm_Code;
  8665. }else{
  8666. Charger.Alarm_LED = Charger.am3352.LedAlarmState;
  8667. }
  8668. if(Charger.Alarm_LED & ALARM_CP_ERROR)
  8669. blinkerTimeSet(BLINKER_IDX_LED, 500 , 500, 0, 1);
  8670. if(Charger.Alarm_LED & ALARM_OVER_VOLTAGE)
  8671. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 3000, 1);
  8672. if(Charger.Alarm_LED & ALARM_UNDER_VOLTAGE)
  8673. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 3000, 2);
  8674. if(Charger.Alarm_LED & ALARM_OVER_CURRENT)
  8675. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 3000, 3);
  8676. if(Charger.Alarm_LED & ALARM_OVER_TEMPERATURE)
  8677. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 3000, 4);
  8678. if(Charger.Alarm_LED & ALARM_CURRENT_LEAK_AC)
  8679. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 3000, 5);
  8680. if(Charger.Alarm_LED & ALARM_CURRENT_LEAK_DC)
  8681. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 3000, 5);
  8682. if(Charger.Alarm_LED & ALARM_GROUND_FAIL)
  8683. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 3000, 6);
  8684. if(Charger.Alarm_LED & ALARM_HANDSHAKE_TIMEOUT)
  8685. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 0, 1);
  8686. if(Charger.Alarm_LED & ALARM_EMERGENCY_STOP)
  8687. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 3000, 7);
  8688. if(Charger.Alarm_LED & ALARM_ROTATORY_SWITCH_FAULT)
  8689. blinkerTimeSet(BLINKER_IDX_LED, 200, 200, 0, 1);
  8690. //OCP Latch
  8691. if (Charger.counter.OC.isLatch == ON)
  8692. {
  8693. Charger.led_R.motion = LED_MOTION_ON ;
  8694. Charger.led_B.motion = LED_MOTION_ON ;
  8695. LedOnRGBSet(1000, 0, 1000, 0);
  8696. }
  8697. //CCID Latch
  8698. if (Charger.counter.LEAK.isLatch == ON)
  8699. {
  8700. Charger.led_R.motion = LED_MOTION_ON ;
  8701. Charger.led_G.motion = LED_MOTION_ON ;
  8702. LedOnRGBSet(1000, 1000, 0, 0);
  8703. }
  8704. //OTP
  8705. if (Charger.counter.OT.isLatch == ON)
  8706. {
  8707. Charger.led_R.motion = LED_MOTION_ON;
  8708. Charger.led_G.motion = LED_MOTION_BLINK;
  8709. Charger.led_B.motion = LED_MOTION_BLINK;
  8710. Charger.led_W.motion = LED_MOTION_OFF;
  8711. LedOnRGBSet(1000, 1000, 1000, 0);
  8712. blinkerTimeSet(BLINKER_IDX_LED, 500 , 500, 0, 1);
  8713. }
  8714. //self test error
  8715. if ((Charger.Alarm_Code & ALARM_MCU_TESTFAIL) ||
  8716. (Charger.Alarm_Code & ALARM_RELAY_STATUS) ||
  8717. (Charger.Alarm_Code & ALARM_RELAY_DRIVE_FUALT ) ||
  8718. (Charger.Alarm_Code & ALARM_LEAK_MODULE_FAIL))
  8719. {
  8720. Charger.led_R.motion = LED_MOTION_ON;
  8721. Charger.led_G.motion = LED_MOTION_OFF;
  8722. Charger.led_B.motion = LED_MOTION_OFF;
  8723. Charger.led_W.motion = LED_MOTION_OFF;
  8724. LedOnRGBSet(1000, 0, 0, 0);
  8725. }
  8726. #endif //FUNC_EMPX10_LED_ALARM_FIX_20240730
  8727. break;
  8728. case LED_ACTION_MAINTAIN:
  8729. #ifdef FUNC_EMPX10_LED
  8730. Charger.led_R.motion = LED_MOTION_BLINK;
  8731. Charger.led_G.motion = LED_MOTION_BLINK;
  8732. Charger.led_B.motion = LED_MOTION_OFF;
  8733. Charger.led_W.motion = LED_MOTION_OFF;
  8734. LedOnRGBSet(1000, 500, 0, 0);
  8735. #else
  8736. Charger.led_R.motion = LED_MOTION_OFF;
  8737. Charger.led_G.motion = LED_MOTION_BLINK;
  8738. Charger.led_B.motion = LED_MOTION_OFF;
  8739. Charger.led_W.motion = LED_MOTION_OFF;
  8740. LedOnRGBSet(0, 1000, 0, 0);
  8741. #endif
  8742. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 0, 1);
  8743. break;
  8744. case LED_ACTION_RFID_PASS:
  8745. Charger.led_R.motion = LED_MOTION_OFF;
  8746. Charger.led_G.motion = LED_MOTION_BLINK;
  8747. Charger.led_B.motion = LED_MOTION_OFF;
  8748. Charger.led_W.motion = LED_MOTION_OFF;
  8749. LedOnRGBSet(0, 1000, 0, 0);
  8750. blinkerTimeSet(BLINKER_IDX_LED, 200, 200, 0, 1);
  8751. break;
  8752. case LED_ACTION_RFID_FAIL:
  8753. Charger.led_R.motion = LED_MOTION_BLINK;
  8754. Charger.led_G.motion = LED_MOTION_OFF;
  8755. Charger.led_B.motion = LED_MOTION_OFF;
  8756. Charger.led_W.motion = LED_MOTION_OFF;
  8757. LedOnRGBSet(1000, 0, 0, 0);
  8758. blinkerTimeSet(BLINKER_IDX_LED, 200, 200, 0, 1);
  8759. break;
  8760. case LED_ACTION_DEBUG:
  8761. Charger.led_R.motion = LED_MOTION_BLINK;
  8762. Charger.led_G.motion = LED_MOTION_BLINK;
  8763. Charger.led_B.motion = LED_MOTION_BLINK;
  8764. Charger.led_W.motion = LED_MOTION_BLINK;
  8765. LedOnRGBSet(1000, 1000, 1000, 1000);
  8766. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 0, 1);
  8767. break;
  8768. case LED_ACTION_ALL_OFF:
  8769. Charger.led_R.motion = LED_MOTION_OFF;
  8770. Charger.led_G.motion = LED_MOTION_OFF;
  8771. Charger.led_B.motion = LED_MOTION_OFF;
  8772. Charger.led_W.motion = LED_MOTION_OFF;
  8773. LedOnRGBSet(0, 0, 0, 0);
  8774. break ;
  8775. case LED_ACTION_HANDSHAKE_FAIL:
  8776. Charger.led_R.motion = LED_MOTION_BLINK;
  8777. Charger.led_G.motion = LED_MOTION_OFF;
  8778. Charger.led_B.motion = LED_MOTION_OFF;
  8779. Charger.led_W.motion = LED_MOTION_OFF;
  8780. LedOnRGBSet(1000, 0, 0, 0);
  8781. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 0, 1);
  8782. break;
  8783. case LED_ACTION_INTERNET_DISCONNECT:
  8784. Charger.led_R.motion = LED_MOTION_ON;
  8785. Charger.led_G.motion = LED_MOTION_ON;
  8786. Charger.led_B.motion = LED_MOTION_OFF;
  8787. Charger.led_W.motion = LED_MOTION_OFF;
  8788. LedOnRGBSet(1000, 1000, 0, 0);
  8789. break;
  8790. case LED_ACTION_RESTORE_SETTING:
  8791. Charger.led_R.motion = LED_MOTION_OFF;
  8792. Charger.led_G.motion = LED_MOTION_OFF;
  8793. Charger.led_B.motion = LED_MOTION_OFF;
  8794. Charger.led_W.motion = LED_MOTION_BLINK;
  8795. LedOnRGBSet(0, 0, 0, 1000);
  8796. blinkerTimeSet(BLINKER_IDX_LED, 500, 500, 0, 1);
  8797. break;
  8798. default :
  8799. Charger.led_R.motion = LED_MOTION_OFF;
  8800. Charger.led_G.motion = LED_MOTION_OFF;
  8801. Charger.led_B.motion = LED_MOTION_OFF;
  8802. Charger.led_W.motion = LED_MOTION_OFF;
  8803. LedOnRGBSet(0, 0, 0, 0);
  8804. break;
  8805. }
  8806. if((action != LED_ACTION_RFID_PASS) &&
  8807. (action != LED_ACTION_RFID_FAIL) &&
  8808. (action != LED_ACTION_BLE_CONNECT) &&
  8809. (action != LED_ACTION_BLE_DISABLE) &&
  8810. (action != LED_ACTION_HANDSHAKE_FAIL) &&
  8811. (action != LED_ACTION_RESTORE_SETTING))
  8812. {
  8813. Charger.Led_Mode = action;
  8814. }
  8815. }
  8816. //-------------------------------------------------------------------------------------------------
  8817. void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef* hadc)
  8818. {
  8819. #ifdef CCID_PROTECT
  8820. #ifdef FUNC_SWITCH_RELAY_WELDING_PROTECT
  8821. if (Charger.m_bDisableCCID == 0 && ((HAL_GetTick() - Charger.m_BootTick) / 1000) > 30)
  8822. {
  8823. #endif
  8824. if (Charger.CCID_Module_Type == CCID_MODULE_CORMEX)
  8825. {
  8826. if(hadc->Instance == ADC2) //use watch dog to check ccid level
  8827. {
  8828. if (!Charger.isTestLeakModule)
  8829. {
  8830. if(Charger.counter.WatchDogLeak >= 5)
  8831. {
  8832. if(!(Charger.Alarm_Code & ALARM_CURRENT_LEAK_AC))
  8833. {
  8834. DEBUG_INFO("leak adc watch happen\r\n");
  8835. Charger.counter.LEAK.isOccurInCharging = (((Charger.Mode == MODE_CHARGING) || (Charger.Mode == MODE_STOP))?ON:OFF);
  8836. Charger.Alarm_Code |= ALARM_CURRENT_LEAK_AC;
  8837. timerEnable(TIMER_IDX_RETRY_LEAK_AC, ALARM_RETRY_INTERVAL_LEAK);
  8838. Charger.counter.LEAK.retry++ ;
  8839. Charger.Relay_Action = GPIO_RELAY_ACTION_OFF ;
  8840. HAL_GPIO_WritePin(OUT_RelayDrv_L1_GPIO_Port, OUT_RelayDrv_L1_Pin, GPIO_PIN_RESET);
  8841. HAL_GPIO_WritePin(OUT_RelayHold_L1_GPIO_Port, OUT_RelayHold_L1_Pin, GPIO_PIN_RESET);
  8842. HAL_GPIO_WritePin(OUT_RelayDrv_L2_GPIO_Port, OUT_RelayDrv_L2_Pin, GPIO_PIN_RESET);
  8843. HAL_GPIO_WritePin(OUT_RelayHold_L2_GPIO_Port, OUT_RelayHold_L2_Pin, GPIO_PIN_RESET);
  8844. Charger.counter.WatchDogLeak = 0 ;
  8845. }
  8846. }
  8847. else
  8848. {
  8849. Charger.counter.WatchDogLeak ++ ;
  8850. }
  8851. }
  8852. }
  8853. }
  8854. #ifdef FUNC_SWITCH_RELAY_WELDING_PROTECT
  8855. }
  8856. #endif
  8857. #endif //CCID_PROTECT
  8858. }
  8859. //-------------------------------------------------------------------------------------------------
  8860. void SetADCWDGBoundary(void)
  8861. {
  8862. if(!ADCWDGHighThreshold)
  8863. {
  8864. //220V 3200 820
  8865. //110V 2640 1400
  8866. ADCWDGHighThreshold = 3200;
  8867. ADCWDGLowThreshold = 820;
  8868. }
  8869. ADC_AnalogWDGConfTypeDef AnalogWDGConfig = {0};
  8870. /*
  8871. * Configure the analog watchdog
  8872. */
  8873. AnalogWDGConfig.WatchdogMode = ADC_ANALOGWATCHDOG_SINGLE_REG;
  8874. AnalogWDGConfig.HighThreshold = ADCWDGHighThreshold;
  8875. AnalogWDGConfig.LowThreshold = ADCWDGLowThreshold;
  8876. AnalogWDGConfig.Channel = ADC_CHANNEL_9;
  8877. AnalogWDGConfig.ITMode = ENABLE;
  8878. if (HAL_ADC_AnalogWDGConfig(&hadc3, &AnalogWDGConfig) != HAL_OK)
  8879. {
  8880. Error_Handler();
  8881. }
  8882. }
  8883. //-------------------------------------------------------------------------------------------------
  8884. void vApplicationStackOverflowHook( TaskHandle_t xTask, signed char *pcTaskName )
  8885. {
  8886. DEBUG_ERROR("Stack overflow: %s\r\n", pcTaskName);
  8887. osDelay(500);
  8888. }
  8889. //-------------------------------------------------------------------------------------------------
  8890. void getlastRecord(void)
  8891. {
  8892. for(uint16_t idx=0;idx<(W25Q16FV_BLOCK_SIZE>>4);idx++)
  8893. {
  8894. if((Charger.memory.hisCharging.data.item[idx].Duration == 0xffffffff) &&
  8895. (Charger.memory.hisCharging.data.item[idx].powerSum == 0xffff) &&
  8896. (Charger.memory.hisCharging.data.item[idx].stopStatusCode == 0xffffffff) )
  8897. {
  8898. DEBUG_INFO("Charging record last index: %d\r\n", (idx));
  8899. break;
  8900. }
  8901. }
  8902. for(uint16_t idx=0;idx<(W25Q16FV_BLOCK_SIZE>>6);idx++)
  8903. {
  8904. if((Charger.memory.hisAlarm.data.item[idx].alarmCode == 0xffffffff) &&
  8905. (Charger.memory.hisAlarm.data.item[idx].mode == 0xff))
  8906. {
  8907. DEBUG_INFO("Alarm record last index: %d\r\n", (idx));
  8908. break;
  8909. }
  8910. }
  8911. }
  8912. //-------------------------------------------------------------------------------------------------
  8913. void getRotarySwitchSetting(void)
  8914. {
  8915. uint8_t tmpBuf = 0;
  8916. tmpBuf += (HAL_GPIO_ReadPin(IN_AC_Current_Set1_GPIO_Port, IN_AC_Current_Set1_Pin)?0:1)<<0;
  8917. tmpBuf += (HAL_GPIO_ReadPin(IN_AC_Current_Set2_GPIO_Port, IN_AC_Current_Set2_Pin)?0:1)<<1;
  8918. tmpBuf += (HAL_GPIO_ReadPin(IN_AC_Current_Set3_GPIO_Port, IN_AC_Current_Set3_Pin)?0:1)<<2;
  8919. tmpBuf += (HAL_GPIO_ReadPin(IN_AC_Current_Set4_GPIO_Port, IN_AC_Current_Set4_Pin)?0:1)<<3;
  8920. Charger.isDebugEnable = OFF;
  8921. #ifdef FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  8922. if (!(((sbyte)tmpBuf) >= EMPX10_RS485_ADDR_MIN && ((sbyte)tmpBuf) <= EMPX10_RS485_ADDR_MAX))
  8923. {
  8924. Charger.isRotarySwitchError = ON;
  8925. }
  8926. DEBUG_INFO("Rotary switch (RS485 Addr) Setting: %d (%s)\r\n", tmpBuf, Charger.isRotarySwitchError ? "NG" : "OK");
  8927. Charger.m_EMPX10_RS485_ClientAddr = tmpBuf;
  8928. #ifdef FUNC_EMPX10_SET_FIXED_CURRENT_16A
  8929. Charger.maxRatingCurrent = 16;
  8930. #else //FUNC_EMPX10_SET_FIXED_CURRENT_16A
  8931. #ifdef FUNC_EMPX10_SET_FIXED_CURRENT_20A
  8932. Charger.maxRatingCurrent = 20;
  8933. #else
  8934. Charger.maxRatingCurrent = EVSE_MODEL_CURRENT_RATING;
  8935. #endif
  8936. #endif //FUNC_EMPX10_SET_FIXED_CURRENT_16A
  8937. #else //FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  8938. switch(tmpBuf)
  8939. {
  8940. case 0x00:
  8941. Charger.maxRatingCurrent = EVSE_MODEL_CURRENT_RATING;
  8942. DEBUG_INFO("Set maxRatingCurrent is Test mode\r\n");
  8943. Charger.isDebugEnable = ON;
  8944. break;
  8945. case 0x01:
  8946. Charger.maxRatingCurrent = (6>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:6);
  8947. Charger.isDebugEnable = OFF;
  8948. break;
  8949. case 0x02:
  8950. Charger.maxRatingCurrent = (8>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:8);
  8951. Charger.isDebugEnable = OFF;
  8952. break;
  8953. case 0x03:
  8954. Charger.maxRatingCurrent = (10>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:10);
  8955. Charger.isDebugEnable = OFF;
  8956. break;
  8957. case 0x04:
  8958. Charger.maxRatingCurrent = (13>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:13);
  8959. Charger.isDebugEnable = OFF;
  8960. break;
  8961. case 0x05:
  8962. Charger.maxRatingCurrent = (16>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:16);
  8963. Charger.isDebugEnable = OFF;
  8964. break;
  8965. case 0x06:
  8966. Charger.maxRatingCurrent = (20>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:20);
  8967. Charger.isDebugEnable = OFF;
  8968. break;
  8969. case 0x07:
  8970. Charger.maxRatingCurrent = (25>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:25);
  8971. Charger.isDebugEnable = OFF;
  8972. break;
  8973. case 0x08:
  8974. Charger.maxRatingCurrent = (32>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:32);
  8975. Charger.isDebugEnable = OFF;
  8976. break;
  8977. case 0x09:
  8978. Charger.maxRatingCurrent = (32>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:32);
  8979. Charger.isDebugEnable = OFF;
  8980. break ;
  8981. case 0x0a:
  8982. Charger.maxRatingCurrent = (30>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:30); // for JARI
  8983. Charger.isDebugEnable = OFF;
  8984. break ;
  8985. case 0x0b:
  8986. case 0x0c:
  8987. case 0x0d:
  8988. case 0x0e:
  8989. Charger.maxRatingCurrent = (32>EVSE_MODEL_CURRENT_RATING?EVSE_MODEL_CURRENT_RATING:32);
  8990. Charger.isRotarySwitchError = ON ;
  8991. Charger.isDebugEnable = OFF;
  8992. break ;
  8993. case 0x0f:
  8994. Charger.maxRatingCurrent = EVSE_MODEL_CURRENT_RATING;
  8995. DEBUG_INFO("Knob error Set maxRatingCurrent defualt 32A\r\n");
  8996. DEBUG_INFO("EVSE is Set Slave Mode\r\n");
  8997. Charger.isDebugEnable = OFF;
  8998. break;
  8999. default:
  9000. Charger.maxRatingCurrent = EVSE_MODEL_CURRENT_RATING;
  9001. DEBUG_INFO("knob default Set maxRatingCurrent defualt 32A\r\n");
  9002. Charger.isDebugEnable = OFF;
  9003. break;
  9004. }
  9005. #endif //FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  9006. DEBUG_INFO("Current rating value: %d A\r\n", Charger.maxRatingCurrent);
  9007. Charger.memory.EVSE_Config.data.item.MaxChargingCurrent = Charger.maxRatingCurrent;
  9008. #ifdef MODIFY_AC_EVSE_STATUS_MAX_CHARGING_CURRENT_VARIABLE
  9009. Charger.AC_MaxChargingCurrentOrDuty = Charger.maxRatingCurrent;
  9010. #endif
  9011. }
  9012. //------------------------------------------------------------------------------//
  9013. 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 )
  9014. {
  9015. *GainA = (float)((float)(SpecData_H -SpecData_L) / (float)( MCUData_H - MCUData_L)) ;
  9016. *GainB = (float)(SpecData_H - (float)(*GainA * MCUData_H)) ;
  9017. }
  9018. //------------------------------------------------------------------------------//
  9019. 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 )
  9020. {
  9021. *GainA = (float)((float)(SpecData_H -SpecData_L) / (float)( MCUData_H - MCUData_L)) ;
  9022. if (SpecData_H > (*GainA * MCUData_H))
  9023. {
  9024. *GainB = SpecData_H - (*GainA * MCUData_H) ;
  9025. *PosorNeg = ON ;
  9026. }
  9027. else
  9028. {
  9029. *GainB = (*GainA * MCUData_H) - SpecData_H ;
  9030. *PosorNeg = OFF ;
  9031. }
  9032. }
  9033. //------------------------------------------------------------------------------
  9034. uint8_t Array_data_Check ( uint8_t *s1, uint8_t *s2 )
  9035. {
  9036. uint8_t compare = 0;
  9037. uint8_t result = PASS;
  9038. for(compare=0;compare<sizeof(s1);compare++)
  9039. {
  9040. if(s1[compare]!=s2[compare])
  9041. {
  9042. result = FAIL;
  9043. break;
  9044. }
  9045. }
  9046. return result;
  9047. }
  9048. //------------------------------------------------------------------------------
  9049. void Relay_Control_Function (void)
  9050. {
  9051. switch(Charger.Relay_Action)
  9052. {
  9053. #ifdef FUNC_TEST_RELAY_ALWAYS_ON
  9054. case GPIO_RELAY_ACTION_OFF:
  9055. #endif
  9056. case GPIO_RELAY_ACTION_ON:
  9057. if(Relay1_Action_Get() == GPIO_RELAY1_ACTION_OFF && Relay2_Action_Get() == GPIO_RELAY2_ACTION_OFF)
  9058. {
  9059. Relay_Action_Set(GPIO_RELAY1_ACTION_ON);
  9060. Relay_Action_Set(GPIO_RELAY2_ACTION_ON);
  9061. }
  9062. if(timer[TIMER_IDX_RELAY_1].isAlarm == ON && timer[TIMER_IDX_RELAY_2].isAlarm == ON)
  9063. {
  9064. Relay_Action_Set(GPIO_RELAY1_ACTION_HOLD);
  9065. Relay_Action_Set(GPIO_RELAY2_ACTION_HOLD);
  9066. timerDisable(TIMER_IDX_RELAY_1);
  9067. timerDisable(TIMER_IDX_RELAY_2);
  9068. Charger.Relay_isOperationCompleted |= 0x11 ;
  9069. }
  9070. break;
  9071. #ifndef FUNC_TEST_RELAY_ALWAYS_ON
  9072. case GPIO_RELAY_ACTION_OFF:
  9073. timer[TIMER_IDX_RELAY_1].isEnable = OFF;
  9074. timer[TIMER_IDX_RELAY_2].isEnable = OFF;
  9075. if(Relay1_Action_Get() != GPIO_RELAY1_ACTION_OFF)
  9076. {
  9077. Relay_Action_Set(GPIO_RELAY1_ACTION_OFF);
  9078. Relay_Action_Set(GPIO_RELAY2_ACTION_OFF);
  9079. }
  9080. osDelay(10);
  9081. Charger.Relay_isOperationCompleted &= 0x00 ;
  9082. #ifdef FUNC_GET_TRIP_TIME
  9083. if (Charger.m_TripBeg > 0 && Charger.m_TripEnd == HTK_U32_MAX)
  9084. {
  9085. Charger.m_TripEnd = HAL_GetTick();
  9086. }
  9087. #endif
  9088. break;
  9089. #endif //FUNC_TEST_RELAY_ALWAYS_ON
  9090. default:
  9091. break;
  9092. }
  9093. }
  9094. //--------------------------------------------------------------------------------
  9095. uint8_t WatchDogLeakRawDataCheck_Event(void)
  9096. {
  9097. uint8_t result = PASS ;
  9098. for(uint16_t idx = 0 ; idx < ADC2_SAMPLE_COUNT; idx++)
  9099. {
  9100. if (ADC2_Buffer_Each[1][idx] >= 4000)
  9101. {
  9102. result = FAIL;
  9103. break;
  9104. }
  9105. }
  9106. return result ;
  9107. }
  9108. //--------------------------------------------------------------------------------
  9109. uint16_t getBrightnessDuty(uint16_t max_duty)
  9110. {
  9111. uint8_t Lv_Brightness = 3;
  9112. uint16_t duty = 1000;
  9113. 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;
  9114. switch(Lv_Brightness)
  9115. {
  9116. case 0:
  9117. duty = max_duty/4;
  9118. break;
  9119. case 1:
  9120. duty = max_duty/3;
  9121. break;
  9122. case 2:
  9123. duty = max_duty/2;
  9124. break;
  9125. case 3:
  9126. duty = max_duty;
  9127. break;
  9128. default:
  9129. duty = max_duty;
  9130. break;
  9131. }
  9132. return duty;
  9133. }
  9134. //--------------------------------------------------------------------------------
  9135. void parseVersionInfoFromModelname(void)
  9136. {
  9137. #ifdef FUNC_VERSION_USE_NEW_SPEC_20240926
  9138. //do nothing
  9139. #else //FUNC_VERSION_USE_NEW_SPEC_20240926
  9140. //--------------------------------------
  9141. // Version Naming Rule "tx.yz.ab.cddd.vv"
  9142. //Version Naming Rule "a"
  9143. for(uint8_t idx=0;idx<3;idx++)
  9144. {
  9145. switch(Charger.memory.EVSE_Config.data.item.ModelName[7+idx])
  9146. {
  9147. case '1':
  9148. // J1772 Plug
  9149. Charger.Ver_FW[6] = '4';
  9150. break;
  9151. case '2':
  9152. // J1772 Socket
  9153. Charger.Ver_FW[6] = '1';
  9154. break;
  9155. case '3':
  9156. // CE Plug
  9157. Charger.Ver_FW[6] = '5';
  9158. break;
  9159. case '4':
  9160. // CE Socket
  9161. Charger.Ver_FW[6] = '2';
  9162. break;
  9163. case '5':
  9164. // GB Plug
  9165. Charger.Ver_FW[6] = '6';
  9166. break;
  9167. case '6':
  9168. // GB Socket
  9169. Charger.Ver_FW[6] = '3';
  9170. break;
  9171. #ifdef FUNC_ADD_FW_VER_WITH_GUN_TYPE_NACS
  9172. case '9':
  9173. // NACS
  9174. Charger.Ver_FW[6] = '9';
  9175. break;
  9176. #endif
  9177. }
  9178. }
  9179. #endif //FUNC_VERSION_USE_NEW_SPEC_20240926
  9180. //Version Naming Rule "c"
  9181. switch(Charger.memory.EVSE_Config.data.item.ModelName[10])
  9182. {
  9183. case 'B':
  9184. case 'U':
  9185. //Blue tooth
  9186. Charger.Ver_FW[9] = '3';
  9187. break;
  9188. case 'W':
  9189. // WIFI
  9190. Charger.Ver_FW[9] = '1';
  9191. break;
  9192. case 'T':
  9193. // 3G/4G
  9194. Charger.Ver_FW[9] = '2';
  9195. break;
  9196. #ifdef FUNC_ADD_FW_VER_WITH_NET_TYPE_WIFI_AND_4G
  9197. case 'D':
  9198. // WIFI + 4G
  9199. Charger.Ver_FW[9] = '5';
  9200. break;
  9201. #endif
  9202. default:
  9203. // LAN
  9204. Charger.Ver_FW[9] = '0';
  9205. break;
  9206. }
  9207. //Version Naming Rule "d3" , Get rating power from model name
  9208. memcpy(&Charger.Ver_FW[10], &Charger.memory.EVSE_Config.data.item.ModelName[4], 0x03);
  9209. // Get vender code from model name
  9210. memcpy(&Charger.Ver_FW[14], &Charger.memory.EVSE_Config.data.item.ModelName[12], 0x02);
  9211. //--------------------------------------
  9212. #ifdef MODIFY_OCP_PROC_WITH_MODELNAME_IDX_3_EURO_SPEC
  9213. HTK_BOOL bModelNameWithEuroSpecOCP = HTK_FALSE;
  9214. #endif
  9215. // CCID_Module_Type check
  9216. Charger.CCID_Module_Type = CCID_MODULE_CORMEX ;
  9217. switch(Charger.memory.EVSE_Config.data.item.ModelName[3])
  9218. {
  9219. case 'E': //CE / European Market
  9220. case 'T': //TR25 / Singapor Market
  9221. case 'Z': //EV ready / French Market
  9222. #ifdef MODIFY_OCP_PROC_WITH_MODELNAME_IDX_3_EURO_SPEC
  9223. case 'M': //E + MID Meter
  9224. case 'P': //E + PTB certification
  9225. case 'I': //Z + TIC Meter pcb
  9226. case 'F': //Z + MID Meter
  9227. bModelNameWithEuroSpecOCP = HTK_TRUE;
  9228. #endif
  9229. Charger.CCID_Module_Type = CCID_MODULE_VAC ;
  9230. ////#ifdef MODIFY_OCP_MAGNIFICATION
  9231. //// Charger.OCP_Magnification = 125 ;
  9232. ////#else
  9233. Charger.OCP_Magnification = 115 ;
  9234. ////#endif
  9235. break;
  9236. case 'U':
  9237. //UL / North America Market
  9238. Charger.CCID_Module_Type = CCID_MODULE_CORMEX ;
  9239. Charger.alarm_spec.Current_LEAK_AC = 1800 ; // unit 0.01mA
  9240. Charger.OCP_Magnification = 110 ;
  9241. break;
  9242. case 'G':
  9243. //GB / China Market
  9244. Charger.CCID_Module_Type = CCID_MODULE_CORMEX ;
  9245. Charger.alarm_spec.Current_LEAK_AC = 2500 ; // unit 0.01mA
  9246. Charger.OCP_Magnification = 110 ;
  9247. break;
  9248. case 'C':
  9249. //CNS / Taiwan Market
  9250. #ifdef FUNC_REPLACE_CORMEX_WITH_VAC_FOR_CNS_MODEL
  9251. Charger.CCID_Module_Type = CCID_MODULE_VAC;
  9252. #else
  9253. Charger.CCID_Module_Type = CCID_MODULE_CORMEX ;
  9254. #endif
  9255. Charger.alarm_spec.Current_LEAK_AC = 2500 ; // unit 0.01mA
  9256. Charger.OCP_Magnification = 110 ;
  9257. break;
  9258. case 'J':
  9259. //JARI / Japan Market
  9260. Charger.CCID_Module_Type = CCID_MODULE_CORMEX ;
  9261. Charger.alarm_spec.Current_LEAK_AC = 2500 ; // unit 0.01mA
  9262. Charger.OCP_Magnification = 110 ;
  9263. break;
  9264. case 'K':
  9265. //KC / Korea Market
  9266. Charger.CCID_Module_Type = CCID_MODULE_CORMEX ;
  9267. Charger.alarm_spec.Current_LEAK_AC = 1800 ; // unit 0.01mA
  9268. Charger.OCP_Magnification = 110 ;
  9269. break;
  9270. case 'B':
  9271. //British / UK Market
  9272. Charger.CCID_Module_Type = CCID_MODULE_CORMEX ;
  9273. Charger.alarm_spec.Current_LEAK_AC = 1800 ; // unit 0.01mA
  9274. Charger.OCP_Magnification = 110 ;
  9275. break;
  9276. default:
  9277. Charger.CCID_Module_Type = CCID_MODULE_CORMEX ;
  9278. Charger.alarm_spec.Current_LEAK_AC = 1800 ; // unit 0.01mA
  9279. Charger.OCP_Magnification = 110 ;
  9280. break;
  9281. }
  9282. #ifdef MODIFY_OCP_PROC_WITH_MODELNAME_IDX_3_EURO_SPEC
  9283. Charger.m_bModelNameWithEuroSpecOCP = bModelNameWithEuroSpecOCP;
  9284. XP("#Use EuropeanSpecOCP: %s\r\n", Charger.m_bModelNameWithEuroSpecOCP ? "True" : "False");
  9285. #endif
  9286. #ifdef FUNC_AUTO_MODIFY_CCID_MODULE
  9287. XP("#Use CCID Module (ModelName: %s): %s\r\n", Charger.memory.EVSE_Config.data.item.ModelName, CCID_MODULE_TYPE_STR);
  9288. if (Charger.m_bCCID_MustModify && Charger.m_CCID_ModuleTestOK != CCID_MODULE_UNKNOW)
  9289. {
  9290. Charger.CCID_Module_Type = Charger.m_CCID_ModuleTestOK;
  9291. XP("#Use CCID Module (AutoModify): %s\r\n", CCID_MODULE_TYPE_STR);
  9292. }
  9293. #endif
  9294. #ifdef FUNC_RS485_SLAVE
  9295. Charger.m_bRS485SlaveForDCModel =
  9296. (
  9297. (Charger.memory.EVSE_Config.data.item.ModelName[0] == 'D') //response to DC Model
  9298. ) ? 1 : 0;
  9299. Charger.m_RS485SlaveAddr = (Charger.m_bRS485SlaveForDCModel ? PROTOCOL_ADDR_AC_MAINBOARD : PROTOCOL_ADDR);
  9300. #endif
  9301. #ifdef FUNC_FORCE_RUN_CSU_MODE_WITH_DC_MODELNAME
  9302. Charger.m_bModelNameDC =
  9303. (
  9304. (Charger.memory.EVSE_Config.data.item.ModelName[0] == 'D') //response to DC Model
  9305. ) ? 1 : 0;
  9306. Charger.m_bModelNameDCReadOK = 1;
  9307. XP("#ModelNameDC: %s\r\n", Charger.m_bModelNameDC ? "True" : "False");
  9308. Update_McuCtrlMode();
  9309. #endif
  9310. #ifdef FUNC_CP_ADC_MODIFY
  9311. #ifdef NEW_CP_SPEC
  9312. CpDetectModuleInitialize(Charger.memory.EVSE_Config.data.item.ModelName[3]);
  9313. Charger.m_bCpDetectModuleInitOK = 1;
  9314. #endif //NEW_CP_SPEC
  9315. #endif //FUNC_CP_ADC_MODIFY
  9316. #ifdef FUNC_RULE_GB_202108
  9317. Charger.m_bSafetyRegulationGB = (Charger.memory.EVSE_Config.data.item.ModelName[3] == 'G');
  9318. #endif
  9319. #ifdef FUNC_UPDATE_ROTARY_SWITCH_SETTING_AFTER_PARSE_MODELNAME
  9320. getRotarySwitchSetting();
  9321. #endif
  9322. }
  9323. void HTK_ByteArray2HexStr(const u8* buf, const u16 BegIdx, const u16 len, char* OutStr, u16 nMaxBytesPerLine, HTK_BOOL bSpace)
  9324. {
  9325. char sTmp[8] = { 0 };
  9326. //char sTmp[8] = { 0 };
  9327. strcpy(OutStr, "\0");
  9328. for (u16 i = BegIdx; i < (BegIdx + len); i++)
  9329. {
  9330. sprintf(sTmp, bSpace ? "%02X " : "%02X", buf[i]);
  9331. strcat(OutStr, sTmp);
  9332. if ((i - BegIdx) % nMaxBytesPerLine == (nMaxBytesPerLine - 1))
  9333. {
  9334. strcat(OutStr, "\r\n");
  9335. }
  9336. }
  9337. }
  9338. void HTK_ByteArray2HexStr_XP(const char* Title, const u8* buf, const u16 BegIdx, const u16 len)
  9339. {
  9340. //char OutStr[256] = { 0 };
  9341. static char OutStr[1024 * 3 + 8] = { 0 };
  9342. HTK_ByteArray2HexStr(buf, BegIdx, len, OutStr, 0, HTK_TRUE);
  9343. XP("[%s] %s(%d)\r\n", (Title == NULL ? "" : Title), OutStr, len);
  9344. }
  9345. void HTK_ByteArray2HexStr_XT(const char* Title, const u8* buf, const u16 BegIdx, const u16 len)
  9346. {
  9347. //char OutStr[256] = { 0 };
  9348. static char OutStr[1024 * 3 + 8] = { 0 };
  9349. HTK_ByteArray2HexStr(buf, BegIdx, len, OutStr, 0, HTK_TRUE);
  9350. XT("[%s] %s(%d)\r\n", (Title == NULL ? "" : Title), OutStr, len);
  9351. }
  9352. #ifdef ENABLE_PRINT_IMCP_MSG
  9353. int CheckPrintImcpMsg(uint8_t MsgID)
  9354. {
  9355. #ifdef ENABLE_PRINT_IMCP_SPEC_MSG
  9356. return
  9357. (
  9358. // MsgID == PROTOCOL_MESSAGE_CONFIG_PARAMETER
  9359. // || MsgID == PROTOCOL_MESSAGE_QUERY_METER_IC_MEAS_PARAMETER
  9360. // || MsgID == PROTOCOL_MESSAGE_QUERY_METER_IC_CALI_PARAMETER
  9361. // || MsgID == PROTOCOL_MESSAGE_CONFIG_METER_IC_CALI_PARAMETER
  9362. MsgID == PROTOCOL_MESSAGE_CONFIG_RELAY_OUTPUT
  9363. || MsgID == PROTOCOL_MESSAGE_CONFIG_MAX_CURRENT_PWM_DUTY
  9364. || MsgID == PROTOCOL_MESSAGE_CONFIG_LEGACY_REQUEST
  9365. );
  9366. #else
  9367. return true;
  9368. #endif //ENABLE_PRINT_IMCP_SPEC_MSG
  9369. }
  9370. #endif //ENABLE_PRINT_IMCP_MSG
  9371. #ifdef MODIFY_COLD_LOAD_PICKUP_DELAY
  9372. void ColdLoadPickup_Delay(const char* Description)
  9373. {
  9374. //osDelay((rand()%175000)+5000);
  9375. int r = rand();
  9376. if (r < 0)
  9377. {
  9378. r = (-1) * r;
  9379. }
  9380. //uint32_t dt = (r % 175000) + 5000; //ms
  9381. uint32_t dt = (r % (COLD_LOAD_PICKUP_DELAY_TIME_MAX_RANGE_SEC * 1000)) + 5000; //ms
  9382. XP("ColdLoadPickup_Delay(ms) [%s]: %d\r\n", Description, dt);
  9383. osDelay(dt);
  9384. }
  9385. #endif
  9386. #ifdef MODIFY_CP_TASK_CTRL_CP_PWM
  9387. uint16_t GetCpPwmDuty(uint16_t Curr)
  9388. {
  9389. uint16_t rtn = (uint16_t)((Curr / 0.6) * 10);
  9390. XP("#GetCpPwmDuty(%d) => %d\r\n", Curr, rtn);
  9391. return rtn;
  9392. }
  9393. #endif
  9394. #ifdef HTK
  9395. HTK_BOOL HTK_IsInTime(u32 BegTick, u32 MaxTick)
  9396. {
  9397. u32 EndTick = HAL_GetTick();
  9398. u32 ElapseTick = (EndTick >= BegTick ? EndTick - BegTick : HTK_U32_MAX - BegTick + EndTick);
  9399. return ElapseTick <= MaxTick ? HTK_TRUE : HTK_FALSE;
  9400. }
  9401. HTK_BOOL HTK_IsTimeout(u32 BegTick, u32 MaxTick)
  9402. {
  9403. u32 EndTick = HAL_GetTick();
  9404. u32 ElapseTick = (EndTick >= BegTick ? EndTick - BegTick : HTK_U32_MAX - BegTick + EndTick);
  9405. return ElapseTick > MaxTick ? HTK_TRUE : HTK_FALSE;
  9406. }
  9407. void HTK_Timer_Reset(PHTK_Timer p)
  9408. {
  9409. XP("#<Timer reset>\r\n\r\n");
  9410. p->m_bStart = HTK_FALSE;
  9411. p->m_bTimeout = HTK_FALSE;
  9412. p->m_BegTick = 0;
  9413. p->m_MaxTick = 0;
  9414. }
  9415. void HTK_Timer_Start(PHTK_Timer p, u32 MaxTick)
  9416. {
  9417. XP("#<Timer start>\r\n\r\n");
  9418. p->m_bStart = HTK_TRUE;
  9419. p->m_BegTick = HAL_GetTick();
  9420. p->m_MaxTick = MaxTick;
  9421. }
  9422. HTK_BOOL HTK_Timer_CheckTimeout(PHTK_Timer p)
  9423. {
  9424. // return HTK_IsTimeout(p->m_BegTick, p->m_MaxTick);
  9425. HTK_BOOL rtn = HTK_IsTimeout(p->m_BegTick, p->m_MaxTick);
  9426. if (rtn == HTK_TRUE)
  9427. {
  9428. XP("#<Timer timeout>\r\n\r\n");
  9429. p->m_bTimeout = HTK_TRUE;
  9430. }
  9431. return rtn;
  9432. }
  9433. uint8_t HTK_CheckSum_Calulate(uint8_t* pBeg, uint32_t len)
  9434. {
  9435. if (pBeg != NULL)
  9436. {
  9437. uint8_t CheckSum = 0;
  9438. for (uint32_t i = 0; i < len; i++)
  9439. {
  9440. CheckSum ^= *(pBeg + i);
  9441. }
  9442. return CheckSum;
  9443. }
  9444. else
  9445. {
  9446. return 0;
  9447. }
  9448. }
  9449. HTK_BOOL HTK_CheckSum_Update(uint8_t* pBeg, uint32_t len, uint8_t* pCheckSum)
  9450. {
  9451. if (pBeg != NULL && pCheckSum != NULL)
  9452. {
  9453. uint8_t CheckSum = HTK_CheckSum_Calulate(pBeg, len);
  9454. *pCheckSum = CheckSum;
  9455. return HTK_TRUE;
  9456. }
  9457. else
  9458. {
  9459. return HTK_FALSE;
  9460. }
  9461. }
  9462. HTK_BOOL HTK_CheckSum_IsOK(uint8_t* pBeg, uint32_t len, uint8_t* pCheckSum)
  9463. {
  9464. if (pBeg != NULL && pCheckSum != NULL)
  9465. {
  9466. uint8_t CheckSum = HTK_CheckSum_Calulate(pBeg, len);
  9467. return (*pCheckSum == CheckSum) ? HTK_TRUE : HTK_FALSE;
  9468. }
  9469. else
  9470. {
  9471. return HTK_FALSE;
  9472. }
  9473. }
  9474. static HTK_MallocHandle s_MallocHandle;
  9475. void HTK_Malloc_Init(void)
  9476. {
  9477. memset(&s_MallocHandle, 0, sizeof(s_MallocHandle));
  9478. //XP("HTK_Malloc_Init(): %d\r\n", HTK_Malloc_BlockTest(1024));
  9479. XP("HTK_Malloc_Init(): %d\r\n", HTK_Malloc_BlockTest(8000));
  9480. }
  9481. void* HTK_Malloc(s32 size)
  9482. {
  9483. void* p = malloc(size);
  9484. if (p != NULL)
  9485. {
  9486. s_MallocHandle.m_Allocated += size;
  9487. s_MallocHandle.m_Reserved -= size;
  9488. memset(p, 0, size);
  9489. }
  9490. XP("HTK_Malloc(%d) %s\r\n", size, (p == NULL) ? "*** NG ***" : "OK");
  9491. return p;
  9492. }
  9493. s32 HTK_Malloc_BlockTest(s32 size)
  9494. {
  9495. int Total = 0;
  9496. for (int i = 1; ; i++)
  9497. {
  9498. void* p = HTK_Malloc(size);
  9499. if (p == NULL)
  9500. {
  9501. break;
  9502. }
  9503. else
  9504. {
  9505. Total += size;
  9506. }
  9507. }
  9508. s_MallocHandle.m_Total = Total;
  9509. s_MallocHandle.m_Allocated = 0;
  9510. s_MallocHandle.m_Reserved = Total;
  9511. return Total;
  9512. }
  9513. #endif //HTK
  9514. #ifdef MODIFY_CPTASK_HEAD
  9515. void Proc_CpTaskHead(HTK_BOOL bCSU)
  9516. {
  9517. if(Charger.Alarm_Code > 0) // alarm occur
  9518. {
  9519. if(Charger.Mode != MODE_ALARM)
  9520. {
  9521. #ifdef FUNC_BLOCK_CSU_CMD_SET_DUTY_WHEN_ALARM_CP_F_STATE
  9522. Charger.m_bBlockCsuCpPwmDutyWhenAlarmCpF = HTK_TRUE;
  9523. #endif
  9524. HAL_GPIO_WritePin(OUT_StateE_GPIO_Port, OUT_StateE_Pin, GPIO_PIN_SET);
  9525. setChargerMode(MODE_ALARM);
  9526. user_pwm_setvalue(PWM_CH_CP, 0);
  9527. timerEnable(TIMER_IDX_STATE_E, 4000);
  9528. Charger.isAlarmSetPWM = OFF ;
  9529. }
  9530. // CP E or F to X1
  9531. if(timer[TIMER_IDX_STATE_E].isAlarm == ON)
  9532. {
  9533. HAL_GPIO_WritePin(OUT_StateE_GPIO_Port, OUT_StateE_Pin, GPIO_PIN_RESET);
  9534. if ( Charger.isAlarmSetPWM == OFF )
  9535. {
  9536. Charger.isAlarmSetPWM = ON ;
  9537. user_pwm_setvalue(PWM_CH_CP, PWM_DUTY_FULL);
  9538. }
  9539. #ifdef FUNC_BLOCK_CSU_CMD_SET_DUTY_WHEN_ALARM_CP_F_STATE
  9540. if (Charger.m_bBlockCsuCpPwmDutyWhenAlarmCpF)
  9541. {
  9542. Charger.m_bBlockCsuCpPwmDutyWhenAlarmCpF = HTK_FALSE;
  9543. }
  9544. #endif
  9545. }
  9546. }
  9547. else
  9548. {
  9549. if(Charger.Mode != MODE_ALARM)
  9550. {
  9551. //if (!bCSU) //only in AX code
  9552. {
  9553. HAL_GPIO_WritePin(OUT_StateE_GPIO_Port, OUT_StateE_Pin, GPIO_PIN_RESET); //CCS communication, so skip this line
  9554. }
  9555. timerDisable(TIMER_IDX_STATE_E);
  9556. }
  9557. #ifdef FIX_OUTP_PWM_WHEN_ALARM_RECOVER_IN_4SEC
  9558. #ifdef FUNC_BLOCK_CSU_CMD_SET_DUTY_WHEN_ALARM_CP_F_STATE
  9559. if (Charger.m_bBlockCsuCpPwmDutyWhenAlarmCpF)
  9560. {
  9561. Charger.m_bBlockCsuCpPwmDutyWhenAlarmCpF = HTK_FALSE;
  9562. }
  9563. #endif
  9564. #endif
  9565. }
  9566. }
  9567. #endif //MODIFY_CPTASK_HEAD
  9568. #ifdef FUNC_FORCE_RUN_CSU_MODE_WITH_DC_MODELNAME
  9569. void Update_McuCtrlMode(void)
  9570. {
  9571. while (!Charger.m_bModelNameDCReadOK)
  9572. {
  9573. osDelay(100);
  9574. }
  9575. #ifdef FUNC_EMPX10_CUSTOMIZE_RUN_CSU_MODE
  9576. {
  9577. Charger.memory.EVSE_Config.data.item.MCU_Control_Mode = MCU_CONTROL_MODE_CSU;
  9578. DEBUG_INFO("#MCU_CONTROL_MODE_CSU\r\n");
  9579. }
  9580. #else
  9581. if (HAL_GPIO_ReadPin(IN_ACT_REQ_GPIO_Port, IN_ACT_REQ_Pin) == GPIO_PIN_RESET ||
  9582. Charger.m_bModelNameDC == 1)
  9583. {
  9584. Charger.memory.EVSE_Config.data.item.MCU_Control_Mode = MCU_CONTROL_MODE_CSU;
  9585. DEBUG_INFO("#MCU_CONTROL_MODE_CSU\r\n");
  9586. }
  9587. else
  9588. {
  9589. Charger.memory.EVSE_Config.data.item.MCU_Control_Mode = MCU_CONTROL_MODE_NO_CSU;
  9590. DEBUG_INFO("#MCU_CONTROL_MODE_NO_CSU\r\n");
  9591. // if (Charger.memory.EVSE_Config.data.item.AuthMode > AUTH_MODE_FREE)
  9592. // {
  9593. // Charger.memory.EVSE_Config.data.item.AuthMode = AUTH_MODE_FREE ;
  9594. // DEBUG_INFO("#AuthMode set defualt AUTH_MODE_FREE \r\n");
  9595. // }
  9596. //
  9597. // if (Charger.memory.EVSE_Config.data.item.OfflinePolicy > RFID_USER_AUTH_NO_CHARGING)
  9598. // {
  9599. // Charger.memory.EVSE_Config.data.item.OfflinePolicy = RFID_USER_AUTH_FREE ;
  9600. // DEBUG_INFO("#OfflinePolicy set defualt RFID_USER_AUTH_FREE \r\n");
  9601. // }
  9602. }
  9603. #endif //FUNC_EMPX10_CUSTOMIZE_RUN_CSU_MODE
  9604. }
  9605. #endif //FUNC_FORCE_RUN_CSU_MODE_WITH_DC_MODELNAME
  9606. #ifdef MODIFY_DC_RS485_UPGRADE_ISSUE
  9607. void RS485_TX_Enable(void)
  9608. {
  9609. HAL_GPIO_WritePin(OUT_Meter_485_DE_GPIO_Port, OUT_Meter_485_DE_Pin, GPIO_PIN_SET);
  9610. HAL_GPIO_WritePin(OUT_Meter_485_RE_GPIO_Port, OUT_Meter_485_RE_Pin, GPIO_PIN_SET);
  9611. osDelay(1);
  9612. }
  9613. void RS485_TX_Disable(void)
  9614. {
  9615. HAL_GPIO_WritePin(OUT_Meter_485_DE_GPIO_Port, OUT_Meter_485_DE_Pin, GPIO_PIN_RESET);
  9616. HAL_GPIO_WritePin(OUT_Meter_485_RE_GPIO_Port, OUT_Meter_485_RE_Pin, GPIO_PIN_RESET);
  9617. }
  9618. void Prefix_UartTX(uint8_t* TxBuf, uint8_t* RxBuf)
  9619. {
  9620. #ifdef FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  9621. if (RxBuf == UART_IAP_rx_buffer)
  9622. {
  9623. TxBuf[1] = RxBuf[2];
  9624. }
  9625. else
  9626. #endif
  9627. if (RxBuf == UART_RS485_rx_buffer)
  9628. {
  9629. RS485_TX_Enable();
  9630. #ifdef FUNC_EMPX10_CUSTOMIZE_RS485_CLIENT_ADDRESS
  9631. //TxBuf[1] = RxBuf[2]; //Charger.m_EMPX10_RS485_ClientAddr;
  9632. TxBuf[1] = Charger.m_EMPX10_RS485_ClientAddr;
  9633. #else
  9634. TxBuf[1] = Charger.m_RS485SlaveAddr; //Update Slave Address
  9635. #endif
  9636. }
  9637. }
  9638. void Postfix_UartTX(uint8_t* TxBuf, uint8_t* RxBuf)
  9639. {
  9640. if (RxBuf == UART_RS485_rx_buffer)
  9641. {
  9642. RS485_TX_Disable();
  9643. }
  9644. }
  9645. #endif //MODIFY_DC_RS485_UPGRADE_ISSUE
  9646. /* USER CODE END Application */
  9647. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/