123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747 |
- /*===========================================================================
- Combined Charging System (CCS): SECC
- main.c
- initiated by Vern, Joseph
- (since 2019/07/19)
- =============================================================================*/
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <linux/termios.h>
- #include <stdio.h>
- #include <string.h>
- #include <time.h>
- #include <stdlib.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <sys/mman.h>
- #include <linux/sockios.h>
- #include <linux/socket.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <sys/time.h>
- #include <sys/timeb.h>
- #include <math.h>//for pow
- #include <unistd.h>
- #include "define.h"
- #include "main.h"
- #define Debug
- struct SysConfigAndInfo *ShmSysConfigAndInfo;
- struct StatusCodeData *ShmStatusCodeData;
- struct CcsData *ShmCcsData;
- struct InternalComm *ShmInternalComm;
- #ifdef SystemLogMessage
- int StoreLogMsg(unsigned char *DataString);
- #endif
- void System_Init();
- void CreateShareMemory_Init();
- void GPIO_Init();
- void PWM_Init();
- void Confinguration_Init();
- int DiffTimeb(struct timeb ST, struct timeb ET);
- int CreatShareMemory();
- int LoadSysConfigAndInfo(struct SysConfigData *ptr);
- int SpawnTask();
- int StoreUsrConfigData(struct SysConfigData *UsrData);
- /*===========================================================================
- FUNCTION: main
- DESCRIPTION:
- 1. This main routine will be executed by the "rcS" when linux is booting up.
- /opt/ti-processor-sdk-linux-am335x-evm-04.02.00.09/EVSE/rootfs/etc/init.d/rcS
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- int main(int argc, char *argv[])
- {
- //int Rtn = 0;
- //unsigned int StartTime;
- //struct timeb StartChargingTime, CurrentChargingTime, ChargingLoopTime;
- //float ChargingPower, ChargingEnergy;
- //CSU Initialization
- System_Init();
- //task spawn
- //SpawnTask();
- /**************************************************************/
- /************** main Loop********************************/
- /*****************************************************************/
- while(1)
- {
- sleep(5);
- }//main while
- }
- /*===========================================================================
- FUNCTION: System_Init
- DESCRIPTION:
- PRE-CONDITION:
- 1. Pin Assignment Configuration
- board-support/linux-4.9.59+gitAUTOINC+a75d8e9305-ga75d8e9305/
- arch/arm/boot/dts/[CCS]am335x-evm.dts
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- void System_Init()
- {
- // ---------- Hardware Init ----------
- //Init GPIO
- GPIO_Init();
- //Init ecap0
- PWM_Init();
- // ---------- Software Init ----------
- //Create all share memory
- CreateShareMemory_Init();
- //Init System Confinguration
- Confinguration_Init();
- #ifdef SystemLogMessage
- StoreLogMsg("--------------------------");
- StoreLogMsg("[main] System_Init(): DONE");
- #endif
- }
- /*===========================================================================
- FUNCTION: CreateShareMemory_Init
- DESCRIPTION:
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- void CreateShareMemory_Init()
- {
- if(CreatShareMemory() == 0)
- {
- #ifdef SystemLogMessage
- StoreLogMsg("[main] CreateShareMemory_Init(): NG");
- #endif
- if(ShmStatusCodeData != NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
- }
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- else
- {
- #ifdef SystemLogMessage
- StoreLogMsg("[main] CreateShareMemory_Init(): DONE");
- #endif
- }
- }
- /**************************************************************************************/
- /**************************Create all share memory *********************************/
- /**************************************************************************************/
- /*===========================================================================
- FUNCTION: CreatShareMemory
- DESCRIPTION:
- There are 4 share memory created here, which are listed as below.
- 1) ShmSysConfigAndInfo
- 2) ShmStatusCodeData
- 3) ShmCcsData
- 4) ShmInternalComm
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- int CreatShareMemory()
- {
- int MeterSMId;
- //creat ShmSysConfigAndInfo
- if((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
- {
- #ifdef SystemLogMessage
- StoreLogMsg("[main]CreatShareMemory:shmget ShmSysConfigAndInfo NG");
- #endif
- return 0;
- }
- else if((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *)-1)
- {
- #ifdef SystemLogMessage
- StoreLogMsg("[main]CreatShareMemory:shmat ShmSysConfigAndInfo NG");
- #endif
- return 0;
- }
- memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
- //creat ShmStatusCodeData
- if((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
- {
- #ifdef SystemLogMessage
- StoreLogMsg("[main]CreatShareMemory:shmget ShmStatusCodeData NG");
- #endif
- return 0;
- }
- else if((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *)-1)
- {
- #ifdef SystemLogMessage
- StoreLogMsg("[main]CreatShareMemory:shmat ShmStatusCodeData NG");
- #endif
- return 0;
- }
- memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
- //creat ShmCcsData
- if((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
- {
- #ifdef SystemLogMessage
- StoreLogMsg("[main]CreatShareMemory:shmget ShmCcsData NG");
- #endif
- return 0;
- }
- else if((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *)-1)
- {
- #ifdef SystemLogMessage
- StoreLogMsg("[main]CreatShareMemory:shmat ShmCcsData NG");
- #endif
- return 0;
- }
- memset(ShmCcsData, 0, sizeof(struct CcsData));
- //creat ShmInternalComm
- if((MeterSMId = shmget(ShmInternalCommKey, sizeof(struct InternalComm), IPC_CREAT | 0777)) < 0)
- {
- #ifdef SystemLogMessage
- StoreLogMsg("[main]CreatShareMemory:shmget ShmInternalComm NG");
- #endif
- return 0;
- }
- else if((ShmInternalComm = shmat(MeterSMId, NULL, 0)) == (void *)-1)
- {
- #ifdef SystemLogMessage
- StoreLogMsg("[main]CreatShareMemory:shmat ShmInternalComm NG");
- #endif
- return 0;
- }
- return 1;
- }
- /*===========================================================================
- FUNCTION: GPIO_Init
- DESCRIPTION:
- 1. GPIO (input) x 1
- * gpio2_0(gpio64): QCA7000 interrupt
- 2. GPIO (output) x 4
- * gpio2_25 (gpio89): reserved (default = 0)
- * gpio2_23 (gpio87): reserved (default = 0)
- * gpio2_24 (gpio88): power reset QCA7000 (default = 0)
- * gpio2_22 (gpio86): Pilot Status E control (0V) (default = 0)
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- void GPIO_Init()
- {
- /*****************0~3, 4 bank, bank x 32+ num*********************/
- /*****************************************************************/
- /*************** INPUT PIN ***************************************/
- /*****************************************************************/
- /*GPIO2_0: QCA7000 interrupt */
- system("echo 64 > /sys/class/gpio/export");
- system("echo \"in\" > /sys/class/gpio/gpio64/direction");
- /***************************************************************/
- /*************** OUTPUT PIN ************************************/
- /***************************************************************/
- //IO port for Communication Board.(reserved for emergency shutdown PSU, i.e. PSKILL)
- /*GPIO2_25: AM_IO_2, H: ?; L: ?*/
- system("echo 89 > /sys/class/gpio/export");
- system("echo \"out\" > /sys/class/gpio/gpio89/direction");
- system("echo 0 > /sys/class/gpio/gpio89/value");
- //IO port for Communication Board.(reserved for emergency shutdown PSU, i.e. PSKILL)
- /*GPIO2_23: AM_IO_1, H: ?; L: ?*/
- system("echo 87 > /sys/class/gpio/export");
- system("echo \"out\" > /sys/class/gpio/gpio87/direction");
- system("echo 0 > /sys/class/gpio/gpio87/value");
- //Power Reset QCA7000
- /*GPIO2_24: AM_QCA_PWR_RST, H: reset, L: noraml*/
- system("echo 88 > /sys/class/gpio/export");
- system("echo \"out\" > /sys/class/gpio/gpio88/direction");
- system("echo 0 > /sys/class/gpio/gpio88/value");
- //Pilot Status E control (0V)
- /*GPIO2_22: Pilot_state E, H: state E, L: normal*/
- system("echo 86 > /sys/class/gpio/export");
- system("echo \"out\" > /sys/class/gpio/gpio86/direction");
- system("echo 0 > /sys/class/gpio/gpio86/value");
- #ifdef SystemLogMessage
- StoreLogMsg("[main] GPIO_Init(): DONE");
- #endif
- }
- /*===========================================================================
- FUNCTION: PWM_Init
- DESCRIPTION:
- - Initialize the PWM channel by the following configuration.
- * Frequency = 1KHz
- * Duty = 100%
- - The default voltage should be as below.
- * 12V: unplugged
- * 9V: plugged
- - Check Mechanism (to-do)
- * Read CP Voltage and PWM by AIN3:
- // cat /sys/bus/iio/devices/iio\\:device0/in_voltage3_raw;
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- - PWM = 100% at 1KHz
- GLOBAL VARIABLES:
- =============================================================================*/
- void PWM_Init()
- {
- //unsigned char str[256];
- //init ecap0
- //memset(str, 0, sizeof(str));
- system("echo 0 > /sys/class/pwm/pwmchip0/export");
- system("echo 1000000 > /sys/class/pwm/pwmchip0/pwm0/period");// nano seconds =>1k Hz
- system("echo 1000000 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle"); //default 100%
- system("echo 1 > /sys/class/pwm/pwmchip0/pwm0/enable");
- //system("echo 1 > /sys /class/pwm/pwmchip0/pwm0/polarity");
- //PWM_Init_Check(); //Checking if the PWM analog signal is well-initialized
- //as 1KHz,100%, and 12V/9V.
- #ifdef SystemLogMessage
- StoreLogMsg("[main] PWM_Init(): DONE");
- #endif
- }
- /*===========================================================================
- FUNCTION:
- DESCRIPTION:
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- void Confinguration_Init()
- {
- //Load System Confinguration
- LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
- #ifdef SystemLogMessage
- StoreLogMsg("[main] Confinguration_Init(): DONE");
- #endif
- }
- /*===========================================================================
- FUNCTION:
- DESCRIPTION:
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- #ifdef SystemLogMessage
- int StoreLogMsg(unsigned char *DataString)
- {
- unsigned char Buf[256];
- time_t CurrentTime;
- struct tm *tm;
- struct timeval tv;
- gettimeofday(&tv, NULL); // get microseconds, 10^-6
- //Get uptime in nano-second precision
- //struct timespec elapsed_from_boot;
- //clock_gettime(CLOCK_BOOTTIME, &elapsed_from_boot);
- //Need GLIBC_2.17
- memset(Buf, 0, sizeof(Buf));
- CurrentTime = time(NULL);
- tm = localtime(&CurrentTime);
- sprintf(Buf, "echo \"[%04d.%02d.%02d-%02d:%02d:%02d.%06d] @ %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
- tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec,
- DataString,
- tm->tm_year + 1900, tm->tm_mon + 1);
- system(Buf);
- #ifdef Debug
- printf("[%04d.%02d.%02d-%02d:%02d:%02d.%06d] @ %s \n",
- tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec,
- DataString);
- #endif
- }
- #endif
- /*===========================================================================
- FUNCTION: DiffTimeb
- DESCRIPTION:
- Caculating the time difference in ms precision (milli-second).
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- int DiffTimeb(struct timeb ST, struct timeb ET)
- {
- //return milli-second
- unsigned int StartTime, StopTime;
- StartTime = (unsigned int)ST.time;
- StopTime = (unsigned int)ET.time;
- return (StopTime - StartTime) * 1000 + ET.millitm - ST.millitm;
- }
- /**************************************************************************************/
- /****************Following functions are CSU initialization***************************/
- /**************************************************************************************/
- /*===========================================================================
- FUNCTION:
- DESCRIPTION:
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- int SpawnTask()
- {
- system("/root/EventLogging &");
- system("/root/CsuComm &");
- system("/root/EvComm &");
- }
- /*===========================================================================
- FUNCTION:
- DESCRIPTION:
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- int StoreUsrConfigData(struct SysConfigData *UsrData)
- {
- int fd, wrd;
- unsigned int i, Chk;
- unsigned char *ptr;
- Chk = 0;
- ptr = (unsigned char *)UsrData;
- for(i = 0; i < sizeof(struct SysConfigData) - 4; i++)
- {
- Chk += *(ptr + i);
- }
- UsrData->Checksum = Chk;
- fd = open("/dev/mtdblock10", O_RDWR);
- if(fd < 0)
- {
- #ifdef SystemLogMessage
- StoreLogMsg("[main]StoreUsrConfigData: open /dev/mtdblock10 NG");
- #endif
- return 0;
- }
- wrd = write(fd, UsrData, sizeof(struct SysConfigData));
- close(fd);
- if(wrd != (sizeof(struct SysConfigData)))
- {
- #ifdef SystemLogMessage
- StoreLogMsg("[main]StoreUsrConfigData: write /dev/mtdblock10 NG");
- #endif
- return 0;
- }
- fd = open("/dev/mtdblock11", O_RDWR);
- if(fd < 0)
- {
- #ifdef SystemLogMessage
- StoreLogMsg("[main]StoreUsrConfigData: open /dev/mtdblock11(backup) NG");
- #endif
- return 0;
- }
- wrd = write(fd, UsrData, sizeof(struct SysConfigData));
- close(fd);
- if(wrd != (sizeof(struct SysConfigData)))
- {
- #ifdef SystemLogMessage
- StoreLogMsg("[main]StoreUsrConfigData: write /dev/mtdblock11(backup) NG");
- #endif
- return 0;
- }
- return 1;
- }
- /*===========================================================================
- FUNCTION: LoadSysConfigAndInfo
- DESCRIPTION:
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- int LoadSysConfigAndInfo(struct SysConfigData *ptr)
- {
- int fd, wrd;
- struct SysConfigData *buf;
- unsigned char *PtrBuf;
- unsigned int ChkSum, ChkSumOrg;
- if((buf = malloc(sizeof(struct SysConfigData))) == NULL)
- {
- #ifdef SystemLogMessage
- StoreLogMsg("[main] LoadSysConfigAndInfo(): malloc buffer NG,rebooting..");
- #endif
- if(ShmStatusCodeData != NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
- }
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- memset(buf, 0, sizeof(struct SysConfigData));
- fd = open("/dev/mtdblock10", O_RDWR);
- if(fd < 0)
- {
- free(buf);
- #ifdef SystemLogMessage
- StoreLogMsg("[main] LoadSysConfigAndInfo():open mtdblock10 NG,rebooting..");
- #endif
- if(ShmStatusCodeData != NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
- }
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- wrd = read(fd, buf, sizeof(struct SysConfigData));
- close(fd);
- if(wrd != (sizeof(struct SysConfigData)))
- {
- free(buf);
- #ifdef SystemLogMessage
- StoreLogMsg("[main] LoadSysConfigAndInfo(): read SysConfigData data NG,rebooting..");
- #endif
- if(ShmStatusCodeData != NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
- }
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- PtrBuf = (unsigned char *)buf;
- ChkSum = 0;
- for(wrd = 0; wrd < (sizeof(struct SysConfigData) - 4); wrd++)
- {
- ChkSum += PtrBuf[wrd];
- }
- ChkSumOrg = buf->Checksum;
- if(ChkSum != ChkSumOrg)
- {
- #ifdef SystemLogMessage
- StoreLogMsg("[main] LoadSysConfigAndInfo(): Primary SysConfigData checksum NG, read backup");
- #endif
- fd = open("/dev/mtdblock11", O_RDWR);
- if(fd < 0)
- {
- free(buf);
- #ifdef SystemLogMessage
- StoreLogMsg("[main] LoadSysConfigAndInfo(): open mtdblock11 (backup) NG,rebooting..");
- #endif
- if(ShmStatusCodeData != NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
- }
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- memset(buf, 0, sizeof(struct SysConfigData));
- wrd = read(fd, buf, sizeof(struct SysConfigData));
- close(fd);
- if(wrd != sizeof(struct SysConfigData))
- {
- free(buf);
- #ifdef SystemLogMessage
- StoreLogMsg("[main] LoadSysConfigAndInfo(): read backup SysConfigData data NG,rebooting..");
- #endif
- if(ShmStatusCodeData != NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
- }
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- PtrBuf = (unsigned char *)buf;
- ChkSum = 0;
- for(wrd = 0; wrd < (sizeof(struct SysConfigData) - 4); wrd++)
- {
- ChkSum += PtrBuf[wrd];
- }
- ChkSumOrg = buf->Checksum;
- if(ChkSum != ChkSumOrg)
- {
- #ifdef SystemLogMessage
- StoreLogMsg("[main] LoadSysConfigAndInfo(): backup SysConfigData checksum NG, read Factory default");
- #endif
- fd = open("/dev/mtdblock12", O_RDWR);
- if(fd < 0)
- {
- free(buf);
- #ifdef SystemLogMessage
- StoreLogMsg("[main] LoadSysConfigAndInfo(): open mtdblock12 (Factory default) NG,rebooting..");
- #endif
- if(ShmStatusCodeData != NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
- }
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- memset(buf, 0, sizeof(struct SysConfigData));
- wrd = read(fd, buf, sizeof(struct SysConfigData));
- close(fd);
- if(wrd != sizeof(struct SysConfigData))
- {
- free(buf);
- #ifdef SystemLogMessage
- StoreLogMsg("[main] LoadSysConfigAndInfo(): read factory default SysConfigData data NG,rebooting..");
- #endif
- if(ShmStatusCodeData != NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
- }
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- PtrBuf = (unsigned char *)buf;
- ChkSum = 0;
- for(wrd = 0; wrd < (sizeof(struct SysConfigData) - 4); wrd++)
- {
- ChkSum += PtrBuf[wrd];
- }
- ChkSumOrg = buf->Checksum;
- if(ChkSum != ChkSumOrg)
- {
- #ifdef SystemLogMessage
- StoreLogMsg("[main] LoadSysConfigAndInfo(): factory default SysConfigData checksum NG, restore factory default");
- #endif
- goto DefaultShm;
- }
- }
- }
- //load OK
- memcpy((struct SysConfigData *)ptr, (struct SysConfigData *)buf, sizeof(struct SysConfigData));
- free(buf);
- #ifdef SystemLogMessage
- StoreLogMsg("[main]LoadSysConfigAndInfo(): Load SysConfigData OK");
- #endif
- return 1;
- DefaultShm:
- system("cd /root;./FactoryConfig");
- system("sync");
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
|