freertos.c 410 KB

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