main.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. /*===========================================================================
  2. Combined Charging System (CCS): SECC
  3. main.c
  4. initiated by Vern, Joseph
  5. (since 2019/07/19)
  6. =============================================================================*/
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <linux/termios.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <time.h>
  14. #include <stdlib.h>
  15. #include <sys/ipc.h>
  16. #include <sys/shm.h>
  17. #include <sys/mman.h>
  18. #include <linux/sockios.h>
  19. #include <linux/socket.h>
  20. #include <sys/socket.h>
  21. #include <netinet/in.h>
  22. #include <sys/time.h>
  23. #include <sys/timeb.h>
  24. #include <math.h>//for pow
  25. #include <unistd.h>
  26. #include "define.h"
  27. #include "main.h"
  28. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  29. struct StatusCodeData *ShmStatusCodeData;
  30. struct CcsData *ShmCcsData;
  31. struct InternalComm *ShmInternalComm;
  32. //struct InternalCommAC *ShmInternalCommAC;
  33. unsigned char buf_log_main[SIZE_OF_LOG_BUFFER];
  34. #if SAVE_SYS_LOG_MSG_MAIN_SWITCH == ENABLE
  35. int StoreLogMsg(unsigned char *DataString);
  36. #endif
  37. void System_Init();
  38. void CreateShareMemory_Init();
  39. void GPIO_Init();
  40. void PWM_Init();
  41. void Confinguration_Init();
  42. double DiffTimeb(struct timeb ST, struct timeb ET);
  43. int CreateShareMemory();
  44. int LoadSysConfigAndInfo(struct SysConfigData *ptr);
  45. int SpawnTask();
  46. int StoreUsrConfigData(struct SysConfigData *UsrData);
  47. unsigned int isKernelSupportNAT()
  48. {
  49. unsigned int result = 0;
  50. unsigned int version = 0;
  51. FILE *fp;
  52. char cmd[256];
  53. char buf[512];
  54. // Get IP address & net mask
  55. strcpy(cmd, "uname -v");
  56. fp = popen(cmd, "r");
  57. if(fp != NULL)
  58. {
  59. if(fgets(buf, sizeof(buf), fp) != NULL)
  60. {
  61. sscanf(buf, "#%d", &version);
  62. // DEBUG_INFO("Kernel version: %d\n", version);
  63. if(version >= 30)
  64. result = 1;
  65. }
  66. }
  67. pclose(fp);
  68. return result;
  69. }
  70. float ReadAdcVolt(unsigned char AdcChannel)
  71. {
  72. //AIN0=CCS GUN Temp 1
  73. //AIN1=CCS GUN Temp 2
  74. //AIN2=CCS_Proximity/2
  75. //AIN3=pilot voltage
  76. if(AdcChannel == 3)
  77. {
  78. int fd,count,AvgTimes;
  79. unsigned char SampleBuf[4];
  80. float TmpVolt, MinSample, AvgSample = 0;
  81. fd = open("/sys/bus/iio/devices/iio\:device0/in_voltage3_raw", O_RDONLY);
  82. if(fd > 0)
  83. {
  84. //system("echo 1 > /sys/class/gpio/gpio89/value"); //for test
  85. for(AvgTimes = 0; AvgTimes < 3; AvgTimes++) //period = 60~91 ms(renice -10, , CurrentDemand()) /*+++ 20200909, vern, extend detection time for interference ---*/
  86. {
  87. count = 0;
  88. MinSample = 2306;
  89. //system("echo 1 > /sys/class/gpio/gpio89/value"); //for test
  90. while(count < 40) //period = 21~42ms (renice -10, CurrentDemand())/*+++ 20200909, vern, extend detection time for interference ---*/
  91. {
  92. //re-sampling period = 3~13ms (renice -10, SLAC())
  93. //system("echo 1 > /sys/class/gpio/gpio89/value"); //for test
  94. read(fd, SampleBuf, 4); //period = 3.2~10ms (renice -10, SLAC())
  95. //system("echo 0 > /sys/class/gpio/gpio89/value"); //for test
  96. TmpVolt = atoi(SampleBuf);
  97. if((TmpVolt < 2306) && (TmpVolt > 0))//positive voltage
  98. {
  99. if(TmpVolt < MinSample)
  100. {
  101. MinSample = TmpVolt;
  102. }
  103. count++;
  104. }
  105. lseek(fd, 0, SEEK_SET);
  106. }
  107. //system("echo 0 > /sys/class/gpio/gpio89/value"); //for test
  108. AvgSample += MinSample;
  109. }
  110. AvgSample /= AvgTimes;
  111. close(fd);
  112. //system("echo 0 > /sys/class/gpio/gpio89/value"); //for test
  113. return ((0.954-(1.8*AvgSample/4095))/0.06);
  114. }
  115. else
  116. {
  117. return -1;
  118. }
  119. }
  120. else
  121. {
  122. FILE *fp;
  123. unsigned char str[64];
  124. unsigned char AdcValue[8];
  125. if(AdcChannel > 7)
  126. {
  127. return -1;
  128. }
  129. memset(str,0,sizeof(str));
  130. memset(AdcValue,0,sizeof(AdcValue));
  131. sprintf(str, "cat /sys/bus/iio/devices/iio\\:device0/in_voltage%d_raw", AdcChannel);
  132. fp=popen(str, "r");
  133. if(fgets(AdcValue,sizeof(AdcValue),fp) == NULL)
  134. {
  135. pclose(fp);
  136. return -1;
  137. }
  138. pclose(fp);
  139. //Vin = Vref *D / (2^n - 1)
  140. //printf("ADC[%d]=%d\n",AdcChannel,atoi(AdcValue));
  141. return ((float)1.8*(float)atoi(AdcValue))/4095;
  142. }
  143. }
  144. /*===========================================================================
  145. FUNCTION: main
  146. DESCRIPTION:
  147. 1. This main routine will be executed by the "rcS" when linux is booting up.
  148. /opt/ti-processor-sdk-linux-am335x-evm-04.02.00.09/EVSE/rootfs/etc/init.d/rcS
  149. PRE-CONDITION:
  150. INPUT:
  151. OUTPUT:
  152. GLOBAL VARIABLES:
  153. =============================================================================*/
  154. int main(int argc, char *argv[])
  155. {
  156. #ifdef CONNECTOR_TEMP_DETECTION
  157. float CcsConnectorTemp1;
  158. int CcsConnectorTemp;
  159. #endif
  160. SAVE_SYS_LOG_MSG_MAIN("ccs: on");
  161. SAVE_SYS_LOG_MSG_MAIN("---------------------------------------------");
  162. SAVE_SYS_LOG_MSG_MAIN("-- MAIN --");
  163. SAVE_SYS_LOG_MSG_MAIN("---------------------------------------------\n");
  164. //int Rtn = 0;
  165. //unsigned int StartTime;
  166. //struct timeb StartChargingTime, CurrentChargingTime, ChargingLoopTime;
  167. //float ChargingPower, ChargingEnergy;
  168. //CSU Initialization
  169. System_Init();
  170. //task spawn
  171. SpawnTask();
  172. sleep(2);
  173. system("killall ntpd");
  174. /**************************************************************/
  175. /************** main Loop********************************/
  176. /*****************************************************************/
  177. while(1)
  178. {
  179. //Sleep to reduce CPU power comsumption
  180. sleep(5);
  181. #ifdef CONNECTOR_TEMP_DETECTION
  182. CcsConnectorTemp=0;
  183. for(unsigned char i=0;i<=1;i++)
  184. {
  185. CcsConnectorTemp1 = ReadAdcVolt(i);
  186. if((CcsConnectorTemp1<=0.9)&&(CcsConnectorTemp1>=0.8))//0 ~ -40
  187. CcsConnectorTemp1=(CcsConnectorTemp1-0.9)*500;
  188. else if((CcsConnectorTemp1<=1.07)&&(CcsConnectorTemp1>0.9))
  189. CcsConnectorTemp1=(CcsConnectorTemp1-0.9)*705.88;
  190. else
  191. CcsConnectorTemp1=195;//not available
  192. CcsConnectorTemp|=((unsigned int)(CcsConnectorTemp1+60)&0xFF)<<(i*8); //0x00(-60)~0xFE(194)
  193. //printf("CcsConnectorTemp1[%d]=%f\n ",i,CcsConnectorTemp1);
  194. }
  195. ShmSysConfigAndInfo->SysInfo.CcsConnectorTemp=CcsConnectorTemp;
  196. //printf("CcsConnectorTemp=0x%x\n ",CcsConnectorTemp);
  197. #endif
  198. }//main while
  199. }
  200. /*===========================================================================
  201. FUNCTION: Eth0_PortSetting
  202. DESCRIPTION:
  203. PRE-CONDITION:
  204. INPUT:
  205. OUTPUT:
  206. GLOBAL VARIABLES:
  207. =============================================================================*/
  208. int Eth0_PortSetting_Base(unsigned char addr)
  209. {
  210. SAVE_SYS_LOG_MSG_MAIN("[Eth0_PortSetting]start");
  211. unsigned char cmd[128];
  212. if ((addr >= 20) && (addr <= (255-20-1))) //preserved for dynamic IP and QCA7000
  213. {
  214. //Step 0: shutdown eth0
  215. #if 0
  216. SAVE_SYS_LOG_MSG_MAIN("[Eth0_PortSetting][eth0]down");
  217. system("/sbin/ifconfig eth0 down");
  218. sleep(1);
  219. #endif
  220. //Step 1: eth0 setting
  221. sprintf(buf_log_main,
  222. "[Eth0_PortSetting][eth0]IP >> 192.168.0.%d(up)",
  223. addr);
  224. SAVE_SYS_LOG_MSG_MAIN(buf_log_main);
  225. sprintf(cmd, "/sbin/ifconfig eth0 192.168.0.%d netmask 255.255.255.0 up", addr);
  226. system(cmd);
  227. memset(cmd, 0, sizeof(cmd));
  228. sleep(1);
  229. //Step 2: add route
  230. system("/sbin/route add default gw 192.168.0.1");
  231. //Step 3: done
  232. sprintf(buf_log_main,
  233. "[Eth0_PortSetting]done");
  234. SAVE_SYS_LOG_MSG_MAIN(buf_log_main);
  235. return PASS;
  236. }
  237. else
  238. {
  239. sprintf(buf_log_main,
  240. "[Eth0_PortSetting][Error]addr(%d) is out of range(20~234)",
  241. addr);
  242. SAVE_SYS_LOG_MSG_MAIN(buf_log_main);
  243. return FAIL;
  244. }
  245. }
  246. /*===========================================================================
  247. FUNCTION: System_Init
  248. DESCRIPTION:
  249. PRE-CONDITION:
  250. 1. Pin Assignment Configuration
  251. board-support/linux-4.9.59+gitAUTOINC+a75d8e9305-ga75d8e9305/
  252. arch/arm/boot/dts/[CCS]am335x-evm.dts
  253. INPUT:
  254. OUTPUT:
  255. GLOBAL VARIABLES:
  256. =============================================================================*/
  257. void System_Init()
  258. {
  259. //RTC Update
  260. time_t rtc_new;
  261. rtc_new = RTC_DEFAULT_TIME; //Epoch time
  262. stime(&rtc_new);
  263. system("date");
  264. sprintf(buf_log_main, "[RTC]%d (DEC)\n", rtc_new);
  265. SAVE_SYS_LOG_MSG_MAIN(buf_log_main);
  266. //-----------------------------------
  267. //Print Linux Kernel Version
  268. sprintf(buf_log_main, "\nLinux Image SHA1: %s\n", LINUX_IMAGE_VERSION);
  269. SAVE_SYS_LOG_MSG_MAIN(buf_log_main);
  270. //Print Hardware Version
  271. sprintf(buf_log_main, "HW: %s\n", HARDWARE_VERSION);
  272. SAVE_SYS_LOG_MSG_MAIN(buf_log_main);
  273. //Print Firmware Version
  274. sprintf(buf_log_main, "FW: %s\n", FIRMWARE_VERSION);
  275. SAVE_SYS_LOG_MSG_MAIN(buf_log_main);
  276. #if (FIRMWARE_VERSION_COMPILE_SETTING_RELEASE_MODE == ENABLE)
  277. {
  278. //Print Linux Kernel Version
  279. printf("\nLinux Image SHA1: %s\n", LINUX_IMAGE_VERSION);
  280. //Print Hardware Version
  281. printf("HW: %s\n", HARDWARE_VERSION);
  282. //Print Firmware Version
  283. printf("FW: %s\n", FIRMWARE_VERSION);
  284. }
  285. #endif
  286. //Init Eth0
  287. Eth0_PortSetting_Base(ETH0_PORT_BASE); //192.168.0.20
  288. // ---------- Hardware Init ---------
  289. if(isKernelSupportNAT() == 1)
  290. system("/sbin/insmod /lib/qcaspi_nat.ko");
  291. else
  292. system("/sbin/insmod /lib/qcaspi.ko");
  293. // system("/sbin/insmod /lib/qcaspi.ko");
  294. sleep(2);
  295. system("/sbin/ifconfig eth1 192.168.0.11 netmask 255.255.255.0 up");
  296. sleep(1);
  297. printf("Starting SSH : \n");
  298. system("/sbin/dropbear &");
  299. printf("Starting FTPD : \n");
  300. system("/usr/bin/tcpsvd -vE 0.0.0.0 21 ftpd -w -t 30 / &");
  301. //printf("Starting LIGHTTPD : \n");
  302. //system("/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf -m /lib");
  303. //Init GPIO
  304. GPIO_Init();
  305. //Init ecap0
  306. PWM_Init();
  307. // ---------- Software Init ----------
  308. //Create all share memory
  309. CreateShareMemory_Init();
  310. //Init System Confinguration
  311. //Confinguration_Init();
  312. SAVE_SYS_LOG_MSG_MAIN("--------------------------");
  313. SAVE_SYS_LOG_MSG_MAIN("System_Init(): DONE");
  314. }
  315. /*===========================================================================
  316. FUNCTION: CreateShareMemory_Init
  317. DESCRIPTION:
  318. PRE-CONDITION:
  319. INPUT:
  320. OUTPUT:
  321. GLOBAL VARIABLES:
  322. =============================================================================*/
  323. void CreateShareMemory_Init()
  324. {
  325. if(CreateShareMemory() == 0)
  326. {
  327. SAVE_SYS_LOG_MSG_MAIN("CreateShareMemory_Init(): NG");
  328. if(ShmStatusCodeData != NULL)
  329. {
  330. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  331. }
  332. sleep(5);
  333. system("reboot -f");
  334. sleep(5);
  335. system("reboot -f");
  336. }
  337. else
  338. {
  339. SAVE_SYS_LOG_MSG_MAIN("CreateShareMemory_Init(): DONE");
  340. }
  341. }
  342. /**************************************************************************************/
  343. /**************************Create all share memory *********************************/
  344. /**************************************************************************************/
  345. /*===========================================================================
  346. FUNCTION: CreateShareMemory
  347. DESCRIPTION:
  348. There are 4 share memory created here, which are listed as below.
  349. 1) ShmSysConfigAndInfo
  350. 2) ShmStatusCodeData
  351. 3) ShmCcsData
  352. 4) ShmInternalComm
  353. PRE-CONDITION:
  354. INPUT:
  355. OUTPUT:
  356. GLOBAL VARIABLES:
  357. =============================================================================*/
  358. int CreateShareMemory()
  359. {
  360. int MeterSMId;
  361. //create ShmSysConfigAndInfo
  362. if((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  363. {
  364. SAVE_SYS_LOG_MSG_MAIN("CreateShareMemory:shmget ShmSysConfigAndInfo NG");
  365. return 0;
  366. }
  367. else if((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *)-1)
  368. {
  369. SAVE_SYS_LOG_MSG_MAIN("CreateShareMemory:shmat ShmSysConfigAndInfo NG");
  370. return 0;
  371. }
  372. memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
  373. //create ShmStatusCodeData
  374. if((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  375. {
  376. SAVE_SYS_LOG_MSG_MAIN("CreateShareMemory:shmget ShmStatusCodeData NG");
  377. return 0;
  378. }
  379. else if((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *)-1)
  380. {
  381. SAVE_SYS_LOG_MSG_MAIN("CreateShareMemory:shmat ShmStatusCodeData NG");
  382. return 0;
  383. }
  384. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  385. //create ShmCcsData
  386. if((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  387. {
  388. SAVE_SYS_LOG_MSG_MAIN("CreateShareMemory:shmget ShmCcsData NG");
  389. return 0;
  390. }
  391. else if((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *)-1)
  392. {
  393. SAVE_SYS_LOG_MSG_MAIN("CreateShareMemory:shmat ShmCcsData NG");
  394. return 0;
  395. }
  396. memset(ShmCcsData, 0, sizeof(struct CcsData));
  397. //create ShmInternalComm
  398. if((MeterSMId = shmget(ShmInternalCommKey, sizeof(struct InternalComm), IPC_CREAT | 0777)) < 0)
  399. {
  400. SAVE_SYS_LOG_MSG_MAIN("CreateShareMemory:shmget ShmInternalComm NG");
  401. return 0;
  402. }
  403. else if((ShmInternalComm = shmat(MeterSMId, NULL, 0)) == (void *)-1)
  404. {
  405. SAVE_SYS_LOG_MSG_MAIN("CreateShareMemory:shmat ShmInternalComm NG");
  406. return 0;
  407. }
  408. memset(ShmInternalComm, 0, sizeof(struct InternalComm));
  409. #if 0
  410. //create ShmInternalCommAC
  411. if((MeterSMId = shmget(ShmInternalCommACKey, sizeof(struct InternalCommAC), IPC_CREAT | 0777)) < 0)
  412. {
  413. SAVE_SYS_LOG_MSG_MAIN("CreateShareMemory:shmget ShmInternalCommAC NG");
  414. return 0;
  415. }
  416. else if((ShmInternalCommAC = shmat(MeterSMId, NULL, 0)) == (void *)-1)
  417. {
  418. SAVE_SYS_LOG_MSG_MAIN("CreateShareMemory:shmat ShmInternalCommAC NG");
  419. return 0;
  420. }
  421. //[To-Do] The reset here should be removed, since AW-CCS CSU will reset it in its main.c.
  422. memset(ShmInternalCommAC, 0, sizeof(struct InternalCommAC));
  423. #endif
  424. return 1;
  425. }
  426. /*===========================================================================
  427. FUNCTION: GPIO_Init
  428. DESCRIPTION:
  429. 1. GPIO (input) x 1
  430. * gpio2_0(gpio64): QCA7000 interrupt
  431. * gpio2_23 (gpio87): CCS Board ID detection
  432. 2. GPIO (output) x 4
  433. * gpio2_25 (gpio89): reserved (default = 0)
  434. * gpio2_24 (gpio88): power reset QCA7000 (default = 0)
  435. * gpio2_22 (gpio86): Pilot Status E control (0V) (default = 0)
  436. PRE-CONDITION:
  437. INPUT:
  438. OUTPUT:
  439. GLOBAL VARIABLES:
  440. =============================================================================*/
  441. void GPIO_Init()
  442. {
  443. /*****************0~3, 4 bank, bank x 32+ num*********************/
  444. /*****************************************************************/
  445. /*************** INPUT PIN ***************************************/
  446. /*****************************************************************/
  447. /*GPIO2_0: QCA7000 interrupt */
  448. system("echo 64 > /sys/class/gpio/export");
  449. system("echo \"in\" > /sys/class/gpio/gpio64/direction");
  450. //IO port for CCS Board ID detection
  451. /*GPIO2_23: AM_IO_1, H: ?; L: ?*/
  452. system("echo 87 > /sys/class/gpio/export");
  453. system("echo \"in\" > /sys/class/gpio/gpio87/direction");
  454. /***************************************************************/
  455. /*************** OUTPUT PIN ************************************/
  456. /***************************************************************/
  457. //IO port for Communication Board.(reserved for emergency shutdown PSU, i.e. PSKILL)
  458. /*GPIO2_25: AM_IO_2, H: ?; L: ?*/
  459. system("echo 89 > /sys/class/gpio/export");
  460. system("echo \"out\" > /sys/class/gpio/gpio89/direction");
  461. system("echo 0 > /sys/class/gpio/gpio89/value");
  462. //Power Reset QCA7000
  463. /*GPIO2_24: AM_QCA_PWR_RST, H: reset, L: noraml*/
  464. system("echo 88 > /sys/class/gpio/export");
  465. system("echo \"out\" > /sys/class/gpio/gpio88/direction");
  466. system("echo 0 > /sys/class/gpio/gpio88/value");
  467. //Pilot Status E control (0V)
  468. /*GPIO2_22: Pilot_state E, H: state E, L: normal*/
  469. system("echo 86 > /sys/class/gpio/export");
  470. system("echo \"out\" > /sys/class/gpio/gpio86/direction");
  471. system("echo 0 > /sys/class/gpio/gpio86/value");
  472. SAVE_SYS_LOG_MSG_MAIN("GPIO_Init(): DONE");
  473. }
  474. /*===========================================================================
  475. FUNCTION: PWM_Init
  476. DESCRIPTION:
  477. - Initialize the PWM channel by the following configuration.
  478. * Frequency = 1KHz
  479. * Duty = 100%
  480. - The default voltage should be as below.
  481. * 12V: unplugged
  482. * 9V: plugged
  483. - Check Mechanism (to-do)
  484. * Read CP Voltage and PWM by AIN3:
  485. // cat /sys/bus/iio/devices/iio\\:device0/in_voltage3_raw;
  486. PRE-CONDITION:
  487. INPUT:
  488. OUTPUT:
  489. - PWM = 100% at 1KHz
  490. GLOBAL VARIABLES:
  491. =============================================================================*/
  492. void PWM_Init()
  493. {
  494. //unsigned char str[256];
  495. //init ecap0
  496. //memset(str, 0, sizeof(str));
  497. system("echo 0 > /sys/class/pwm/pwmchip0/export");
  498. system("echo 1000000 > /sys/class/pwm/pwmchip0/pwm0/period");// nano seconds =>1k Hz
  499. system("echo 1000000 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle"); //default 100%
  500. system("echo 1 > /sys/class/pwm/pwmchip0/pwm0/enable");
  501. //system("echo 1 > /sys /class/pwm/pwmchip0/pwm0/polarity");
  502. //PWM_Init_Check(); //Checking if the PWM analog signal is well-initialized
  503. //as 1KHz,100%, and 12V/9V.
  504. SAVE_SYS_LOG_MSG_MAIN("PWM_Init(): DONE");
  505. }
  506. /*===========================================================================
  507. FUNCTION:
  508. DESCRIPTION:
  509. PRE-CONDITION:
  510. INPUT:
  511. OUTPUT:
  512. GLOBAL VARIABLES:
  513. =============================================================================*/
  514. void Confinguration_Init()
  515. {
  516. //Load System Confinguration
  517. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  518. SAVE_SYS_LOG_MSG_MAIN("Confinguration_Init(): DONE");
  519. }
  520. /*===========================================================================
  521. FUNCTION: StoreLogMsg
  522. DESCRIPTION:
  523. PRE-CONDITION:
  524. INPUT:
  525. OUTPUT:
  526. GLOBAL VARIABLES:
  527. =============================================================================*/
  528. #if SAVE_SYS_LOG_MSG_MAIN_SWITCH == ENABLE
  529. int StoreLogMsg(unsigned char *DataString)
  530. {
  531. static unsigned char Buf[1024];
  532. static time_t CurrentTime;
  533. static struct tm *tm;
  534. static struct timeval tv;
  535. memset(Buf, 0, sizeof(Buf));
  536. CurrentTime = time(NULL);
  537. tm = localtime(&CurrentTime);
  538. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  539. sprintf(Buf, "echo \"[%04d%02d%02d: %02d:%02d:%02d.%06d][main]%s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  540. tm->tm_year + 1900,
  541. tm->tm_mon + 1,
  542. tm->tm_mday,
  543. tm->tm_hour,
  544. tm->tm_min,
  545. tm->tm_sec,
  546. tv.tv_usec,
  547. DataString,
  548. tm->tm_year + 1900,
  549. tm->tm_mon + 1);
  550. system(Buf);
  551. DEBUG_PRINTF_MAIN_SYSTEM_LOG("[%02d:%02d:%02d.%06d][main]%s \n",
  552. tm->tm_hour,
  553. tm->tm_min,
  554. tm->tm_sec,
  555. tv.tv_usec,
  556. DataString);
  557. //Reset the buf_log_main Buffer, i.e. DataString
  558. memset(buf_log_main, 0, SIZE_OF_LOG_BUFFER);
  559. }
  560. #endif
  561. /*===========================================================================
  562. FUNCTION: DiffTimeb
  563. DESCRIPTION:
  564. Caculating the time difference in ms precision (milli-second).
  565. PRE-CONDITION:
  566. INPUT:
  567. OUTPUT:
  568. GLOBAL VARIABLES:
  569. =============================================================================*/
  570. double DiffTimeb(struct timeb ST, struct timeb ET)
  571. {
  572. //return milli-second
  573. double StartTime, EndTime;
  574. double t_diff;
  575. StartTime = ((double)ST.time)*1000 + (double)ST.millitm;
  576. EndTime = ((double)ET.time)*1000 + (double)ET.millitm;
  577. t_diff = EndTime - StartTime;
  578. //printf("%.02lf - %.02lf = %.02lf\n", EndTime, StartTime, t_diff);
  579. if (t_diff < 0)
  580. {
  581. #if 0
  582. if (t_diff < -1000) //1000ms
  583. {
  584. sprintf(buf_log_main,
  585. "[Warning]StartTime(%.02lf) > EndTime(%.02lf), d(%.02lf)",
  586. StartTime,
  587. EndTime,
  588. t_diff);
  589. SAVE_SYS_LOG_MSG_MAIN(buf_log_main);
  590. }
  591. #endif
  592. return -1;
  593. }
  594. return t_diff;
  595. }
  596. /**************************************************************************************/
  597. /****************Following functions are CSU initialization***************************/
  598. /**************************************************************************************/
  599. /*===========================================================================
  600. FUNCTION:
  601. DESCRIPTION:
  602. PRE-CONDITION:
  603. INPUT:
  604. OUTPUT:
  605. GLOBAL VARIABLES:
  606. =============================================================================*/
  607. int SpawnTask()
  608. {
  609. //system("/root/EventLogging &");
  610. system("/root/CsuComm &");
  611. usleep(50000); //50ms
  612. //system("echo 50000 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle"); //5%, for test.
  613. system("/root/SeccComm &");
  614. }
  615. /*===========================================================================
  616. FUNCTION:
  617. DESCRIPTION:
  618. PRE-CONDITION:
  619. INPUT:
  620. OUTPUT:
  621. GLOBAL VARIABLES:
  622. =============================================================================*/
  623. int StoreUsrConfigData(struct SysConfigData *UsrData)
  624. {
  625. int fd, wrd;
  626. unsigned int i, Chk;
  627. unsigned char *ptr;
  628. Chk = 0;
  629. ptr = (unsigned char *)UsrData;
  630. for(i = 0; i < sizeof(struct SysConfigData) - 4; i++)
  631. {
  632. Chk += *(ptr + i);
  633. }
  634. UsrData->Checksum = Chk;
  635. fd = open("/dev/mtdblock10", O_RDWR);
  636. if(fd < 0)
  637. {
  638. SAVE_SYS_LOG_MSG_MAIN("StoreUsrConfigData: open /dev/mtdblock10 NG");
  639. return 0;
  640. }
  641. wrd = write(fd, UsrData, sizeof(struct SysConfigData));
  642. close(fd);
  643. if(wrd != (sizeof(struct SysConfigData)))
  644. {
  645. SAVE_SYS_LOG_MSG_MAIN("StoreUsrConfigData: write /dev/mtdblock10 NG");
  646. return 0;
  647. }
  648. fd = open("/dev/mtdblock11", O_RDWR);
  649. if(fd < 0)
  650. {
  651. SAVE_SYS_LOG_MSG_MAIN("StoreUsrConfigData: open /dev/mtdblock11(backup) NG");
  652. return 0;
  653. }
  654. wrd = write(fd, UsrData, sizeof(struct SysConfigData));
  655. close(fd);
  656. if(wrd != (sizeof(struct SysConfigData)))
  657. {
  658. SAVE_SYS_LOG_MSG_MAIN("StoreUsrConfigData: write /dev/mtdblock11(backup) NG");
  659. return 0;
  660. }
  661. return 1;
  662. }
  663. /*===========================================================================
  664. FUNCTION: LoadSysConfigAndInfo
  665. DESCRIPTION:
  666. PRE-CONDITION:
  667. INPUT:
  668. OUTPUT:
  669. GLOBAL VARIABLES:
  670. =============================================================================*/
  671. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  672. {
  673. int fd, wrd;
  674. struct SysConfigData *buf;
  675. unsigned char *PtrBuf;
  676. unsigned int ChkSum, ChkSumOrg;
  677. if((buf = malloc(sizeof(struct SysConfigData))) == NULL)
  678. {
  679. SAVE_SYS_LOG_MSG_MAIN("LoadSysConfigAndInfo(): malloc buffer NG,rebooting..");
  680. if(ShmStatusCodeData != NULL)
  681. {
  682. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  683. }
  684. sleep(5);
  685. system("reboot -f");
  686. sleep(5);
  687. system("reboot -f");
  688. }
  689. memset(buf, 0, sizeof(struct SysConfigData));
  690. fd = open("/dev/mtdblock10", O_RDWR);
  691. if(fd < 0)
  692. {
  693. free(buf);
  694. SAVE_SYS_LOG_MSG_MAIN("LoadSysConfigAndInfo():open mtdblock10 NG,rebooting..");
  695. if(ShmStatusCodeData != NULL)
  696. {
  697. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  698. }
  699. sleep(5);
  700. system("reboot -f");
  701. sleep(5);
  702. system("reboot -f");
  703. }
  704. wrd = read(fd, buf, sizeof(struct SysConfigData));
  705. close(fd);
  706. if(wrd != (sizeof(struct SysConfigData)))
  707. {
  708. free(buf);
  709. SAVE_SYS_LOG_MSG_MAIN("LoadSysConfigAndInfo(): read SysConfigData data NG,rebooting..");
  710. if(ShmStatusCodeData != NULL)
  711. {
  712. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  713. }
  714. sleep(5);
  715. system("reboot -f");
  716. sleep(5);
  717. system("reboot -f");
  718. }
  719. PtrBuf = (unsigned char *)buf;
  720. ChkSum = 0;
  721. for(wrd = 0; wrd < (sizeof(struct SysConfigData) - 4); wrd++)
  722. {
  723. ChkSum += PtrBuf[wrd];
  724. }
  725. ChkSumOrg = buf->Checksum;
  726. if(ChkSum != ChkSumOrg)
  727. {
  728. SAVE_SYS_LOG_MSG_MAIN("LoadSysConfigAndInfo(): Primary SysConfigData checksum NG, read backup");
  729. fd = open("/dev/mtdblock11", O_RDWR);
  730. if(fd < 0)
  731. {
  732. free(buf);
  733. SAVE_SYS_LOG_MSG_MAIN("LoadSysConfigAndInfo(): open mtdblock11 (backup) NG,rebooting..");
  734. if(ShmStatusCodeData != NULL)
  735. {
  736. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  737. }
  738. sleep(5);
  739. system("reboot -f");
  740. sleep(5);
  741. system("reboot -f");
  742. }
  743. memset(buf, 0, sizeof(struct SysConfigData));
  744. wrd = read(fd, buf, sizeof(struct SysConfigData));
  745. close(fd);
  746. if(wrd != sizeof(struct SysConfigData))
  747. {
  748. free(buf);
  749. SAVE_SYS_LOG_MSG_MAIN("LoadSysConfigAndInfo(): read backup SysConfigData data NG,rebooting..");
  750. if(ShmStatusCodeData != NULL)
  751. {
  752. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  753. }
  754. sleep(5);
  755. system("reboot -f");
  756. sleep(5);
  757. system("reboot -f");
  758. }
  759. PtrBuf = (unsigned char *)buf;
  760. ChkSum = 0;
  761. for(wrd = 0; wrd < (sizeof(struct SysConfigData) - 4); wrd++)
  762. {
  763. ChkSum += PtrBuf[wrd];
  764. }
  765. ChkSumOrg = buf->Checksum;
  766. if(ChkSum != ChkSumOrg)
  767. {
  768. SAVE_SYS_LOG_MSG_MAIN("LoadSysConfigAndInfo(): backup SysConfigData checksum NG, read Factory default");
  769. fd = open("/dev/mtdblock12", O_RDWR);
  770. if(fd < 0)
  771. {
  772. free(buf);
  773. SAVE_SYS_LOG_MSG_MAIN("LoadSysConfigAndInfo(): open mtdblock12 (Factory default) NG,rebooting..");
  774. if(ShmStatusCodeData != NULL)
  775. {
  776. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  777. }
  778. sleep(5);
  779. system("reboot -f");
  780. sleep(5);
  781. system("reboot -f");
  782. }
  783. memset(buf, 0, sizeof(struct SysConfigData));
  784. wrd = read(fd, buf, sizeof(struct SysConfigData));
  785. close(fd);
  786. if(wrd != sizeof(struct SysConfigData))
  787. {
  788. free(buf);
  789. SAVE_SYS_LOG_MSG_MAIN("LoadSysConfigAndInfo(): read factory default SysConfigData data NG,rebooting..");
  790. if(ShmStatusCodeData != NULL)
  791. {
  792. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  793. }
  794. sleep(5);
  795. system("reboot -f");
  796. sleep(5);
  797. system("reboot -f");
  798. }
  799. PtrBuf = (unsigned char *)buf;
  800. ChkSum = 0;
  801. for(wrd = 0; wrd < (sizeof(struct SysConfigData) - 4); wrd++)
  802. {
  803. ChkSum += PtrBuf[wrd];
  804. }
  805. ChkSumOrg = buf->Checksum;
  806. if(ChkSum != ChkSumOrg)
  807. {
  808. SAVE_SYS_LOG_MSG_MAIN("LoadSysConfigAndInfo(): factory default SysConfigData checksum NG, restore factory default");
  809. goto DefaultShm;
  810. }
  811. }
  812. }
  813. //load OK
  814. memcpy((struct SysConfigData *)ptr, (struct SysConfigData *)buf, sizeof(struct SysConfigData));
  815. free(buf);
  816. SAVE_SYS_LOG_MSG_MAIN("LoadSysConfigAndInfo(): Load SysConfigData OK");
  817. return 1;
  818. DefaultShm:
  819. system("cd /root;./FactoryConfig");
  820. #if (FIRMWARE_VERSION_COMPILE_SETTING_RELEASE_MODE == DISABLE)
  821. {
  822. SAVE_SYS_LOG_MSG_MAIN("sync...");
  823. system("sync");
  824. }
  825. #endif
  826. sleep(5);
  827. system("reboot -f");
  828. sleep(5);
  829. system("reboot -f");
  830. }