main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*************************************************************************
  2. * 版权所有:
  3. * 文件版本: V1.00
  4. * 文件名称: main.C主程序
  5. * 生成日期: 2008年8月8日
  6. * 作 者:
  7. * 功 能: 保护主函数处理,
  8. * 更新信息:
  9. * 更新日志1:
  10. * 日期:
  11. * 修改者:
  12. * 修改内容:
  13. * 修改原因:
  14. * 更新日志2:
  15. **************************************************************************/
  16. #include "head.h"
  17. #include "rt_printf.h"
  18. #include <sys/prctl.h>
  19. #include <unistd.h>
  20. #include <sys/syscall.h>
  21. #include <inttypes.h>
  22. #include <time.h>
  23. // 系统秒,从程序启动时刻算起
  24. u32 g_tick_secs;
  25. // 系统重启定时器
  26. u32 g_restart_count;
  27. #if defined CPU_AM335X || defined CPU_FUXI
  28. extern bool SystemInit_finish_flag __attribute__((aligned(8)));
  29. #endif
  30. /********************************************************************
  31. 版权所有:
  32. 文件版本: V1.00
  33. 文件名称: main.c
  34. 生成日期: 2008年08月26日
  35. 作 者:
  36. 使用范围:
  37. 功 能:主函数
  38. 更新信息:
  39. 更新日志1:
  40. 修改者:
  41. 修改日期:
  42. 修改内容:
  43. 修改原因:
  44. *********************************************************************/
  45. int app_main(void *arg);
  46. int kernel_callback(void);
  47. int dt_cmd_rt_reset(void);
  48. int uart_test(void);
  49. int test_round(void);
  50. int ds18b20_init(void);
  51. int sntp_init(void);
  52. int rtc_test(void);
  53. int adc_test(void);
  54. int bkpt_disable(void);
  55. #ifdef CPU_FUXI
  56. uint32_t wdt_id_mainloop;
  57. extern int printf_e907_init(void);
  58. #endif
  59. void bkpt_isr(void)
  60. {
  61. rt_printf("x\r\n");
  62. }
  63. #if defined(CPU_AM335X)
  64. static void check_app_folder_recover(void)
  65. {
  66. // 应用启动后,需要判断 /tmp/app_empty_flag 文件是否存在,如果存在,则说明这次启动 app 分区进行了恢复,你那边记录后把该文件删除掉。
  67. struct file *pfile = rt_file_open("/tmp/app_empty_flag", O_RDONLY, 0);
  68. if (IS_ERR(pfile))
  69. {
  70. ;
  71. }
  72. else
  73. {
  74. rt_printf("临时文件/tmp/app_empty_flag不存在,之前发生过app分区恢复操作.\r\n");
  75. soe_record_opt(EV_APP_FOLDER_RECOVER, 0);
  76. rt_file_del("/tmp/app_empty_flag");
  77. }
  78. }
  79. #endif
  80. int app_main(void *unused)
  81. {
  82. int ret;
  83. uint64_t us0, us0_sec;
  84. // 登记bsp回调函数
  85. rt_irq_level(0);
  86. SystemInit();
  87. #ifdef CPU_FUXI
  88. SystemInit_finish_flag = true;
  89. #endif
  90. #ifdef CPU_FUXI
  91. printf_e907_init();
  92. #endif
  93. #ifdef RCD_STRAN_S
  94. _rcd_get_waveno(); /*装置地址要读出参数才有效*/
  95. #endif
  96. #ifndef CPU_FUXI
  97. watchdog_register_all();
  98. #endif
  99. dt_cmd_rt_reset();
  100. rt_stat_other_init(RT_STAT_BIG_LOOP, "主循环");
  101. rt_stat_other_init(1, "5ms_2");
  102. rt_stat_other_init(2, "5ms_3");
  103. rt_stat_other_init(3, "测量计算1");
  104. rt_stat_other_init(4, "测量计算2");
  105. rt_stat_other_init(5, "SOE保存");
  106. rt_stat_other_init(6, "临时测试");
  107. rt_stat_other_init(7, "主循环间隔");
  108. #ifdef SNTP_FUNC
  109. sntp_init();
  110. #endif
  111. {
  112. // 记录关键文件CRC
  113. char name[256];
  114. char log_info[128];
  115. sprintf(name, "上电完成。校验CRC:程序0x%04x, 资源表0x%04x, 板卡资源0x%04x, 通道配置0x%04x, 点表0x%04x。",
  116. m_CodeCrc, g_crc_rsc, g_crc_brd_res, g_crc_equ_cfg, g_crc_rectable);
  117. log_str_time(LOG_ERR, name, 0, 1);
  118. sprintf(log_info, "Device is powered on,crc:code=0x%04x,rsc=0x%04x,brd_res=0x%04x,equ_cfg=0x%04x,rectable=0x%04x",
  119. m_CodeCrc, g_crc_rsc, g_crc_brd_res, g_crc_equ_cfg, g_crc_rectable);
  120. load_hs_log_rcd(TYPE_DEV_RESTART, true, NULL, log_info, 1);
  121. }
  122. ret = get_mt_port_proc();
  123. if (ret != 1)
  124. {
  125. close_inet_port();
  126. }
  127. #if defined(CPU_AM335X)
  128. check_app_folder_recover();
  129. #endif
  130. #ifdef CPU_FUXI
  131. // noted by sunxi: 经测试发现,装置运行10多分钟后,tmp文件里的rsc.bin和set_desc.bin会莫名消失。
  132. // 经分析得,伏羲系统会周期(十多分钟)删除掉tmp文件夹中的旧文件(rsc.bin和set_desc.bin 文件是在
  133. // 系统对时前产生的,所以时间默认比较早)。因此在这里统一对tmp文件夹的文件更新一下。
  134. system("touch /tmp/*");
  135. prctl(PR_SET_NAME, "app_main_loop");
  136. #endif
  137. #ifdef RT_THREAD_DEBUG
  138. rt_printf("Thread created successfully: %s, PID: %d LWP: %d\r\n",
  139. "app_main_loop", (int)getpid(), (int)syscall(SYS_gettid));
  140. #endif
  141. #if (1) /* 打印信息测试函数 */
  142. // extern int test_read_pin(void);
  143. // test_read_pin();
  144. dp_info_nt("开关个数 SWITCH_NUM_MAX = %d", SWITCH_NUM_MAX);
  145. dp_info_nt("最大定值个数 MAX_SET_NUMBER = %d", MAX_SET_NUMBER);
  146. dp_info_nt("装置参数 EQUIP_PARA_NUMBER = %d, PARA_NUM = %d", EQUIP_PARA_NUMBER, PARA_NUM);
  147. dp_info_nt("开关定值 SW_SET_NUMBER = %d, SET_NUM = %d", SW_SET_NUMBER, SET_NUM);
  148. dp_info_nt("公共定值 PUB_SET_NUMBER = %d, SET_PUB_NUM = %d", PUB_SET_NUMBER, SET_PUB_NUM);
  149. dp_info_nt("全部开关保护定值总个数 SET_NUMBER = %d", SET_NUMBER);
  150. dp_info_nt("内部定值 CSTSET_NUMBER = %d, CSTSET_NUM = %d", CSTSET_NUMBER, CSTSET_NUM);
  151. dp_info_nt("固有参数 FIXEDSET_TABLE_NUMBER = %d", FIXEDSET_TABLE_NUMBER);
  152. msleep(100);
  153. equ_info_printf();
  154. msleep(100);
  155. equ_info_printf_di();
  156. msleep(100);
  157. equ_info_printf_do();
  158. msleep(100);
  159. equ_info_printf_ac();
  160. msleep(100);
  161. factor_printf();
  162. msleep(500);
  163. led_info_printf();
  164. msleep(500);
  165. // msleep(500);
  166. tbl_printf();
  167. msleep(500);
  168. #endif
  169. rt_err_printf();
  170. #if (1)
  171. dp_err_nt_rt("JZS_ALL_EN(0) = %d", JZS_ALL_EN(0));
  172. dp_err_nt_rt("KZ_OUT_ALL_EN(0) = %d", KZ_OUT_ALL_EN(0));
  173. dp_err_nt_rt("FUN_ALL_EN(0) = %d(%d, %d, %d)", FUN_ALL_EN(0), g_run_stu.bhall, pRunSet->tSwSet[0].btt_yxyb_gnztt, g_tRelay[0].run_stu.bhztt);
  174. dp_err_nt_rt("BH_GOOSE_EN_YB(0) = %d", BH_GOOSE_EN_YB(0));
  175. dp_err_nt_rt("BH_GOOSE_EN(0) = %d", BH_GOOSE_EN(0));
  176. dp_err_nt_rt("BH_GOOSE_COMMERR(0) = %d", BH_GOOSE_COMMERR(0));
  177. dp_err_nt_rt("BH_ALL_2(0) = %d", BH_ALL_2(0));
  178. dp_err_nt_rt("BH_ALL_EN(0) = %d", BH_ALL_EN(0));
  179. dp_err_nt_rt("FA_ALL_EN(0) = %d", FA_ALL_EN(0));
  180. dp_err_nt_rt("CHZ_ALL_EN(0) = %d", CHZ_ALL_EN(0));
  181. dp_err_nt_rt("TYTQ_ALL_EN(0) = %d", TYTQ_ALL_EN(0));
  182. dp_err_nt_rt("JL_ALL_EN(0) = %d", JL_ALL_EN(0));
  183. dp_err_nt_rt("tRunPara.dTBatBegin = %d", tRunPara.dTBatBegin);
  184. dp_err_nt_rt("tRunPara.dTBatInterval = %d", tRunPara.dTBatInterval);
  185. dp_err_nt_rt("pRunSet->tSwSet[0].d_chz_times = %d", pRunSet->tSwSet[0].d_chz_times);
  186. dp_info_nt_rt("EEP_SET_ADDR = 0x%x", EEP_SET_ADDR);
  187. dp_info_nt_rt("EEP_FIXVAL_DATA_SIZE = 0x%x", EEP_FIXVAL_DATA_SIZE);
  188. dp_info_nt_rt("EEP_SET_ADDR_END = 0x%x", EEP_SET_ADDR_END);
  189. dp_info_nt_rt("EEP_SYSTEM_DATA_START = 0x%x", EEP_SYSTEM_DATA_START);
  190. dp_info_nt_rt("EEP_SOE_ADDR = 0x%x", EEP_SOE_ADDR);
  191. dp_info_nt_rt("EEP_OPERATE_ADDR = 0x%x", EEP_OPERATE_ADDR);
  192. dp_info_nt_rt("EEP_VIP_ADDR = 0x%x", EEP_VIP_ADDR);
  193. #endif
  194. {
  195. struct timespec ts;
  196. struct rtc_time_t ct;
  197. gps_get_time(&ts);
  198. timespec_to_rtc(ts, &ct, 1);
  199. rt_printf("当前时间: %04d-%02d-%02d %02d:%02d:%02d\r\n", ct.year + 2000, ct.month, ct.day, ct.hour, ct.min, ct.ms / 1000);
  200. #if (1)
  201. struct tm *local_time_gps = localtime(&ts.tv_sec);
  202. if (local_time_gps == NULL)
  203. {
  204. perror("localtime failed");
  205. return EXIT_FAILURE;
  206. }
  207. int ms_part_gps = ts.tv_nsec / 1000000;
  208. dp_info_nt_rt("%04d-%02d-%02d %02d:%02d:%02d %03dms(GPS)",
  209. local_time_gps->tm_year + 1900, local_time_gps->tm_mon + 1, local_time_gps->tm_mday,
  210. local_time_gps->tm_hour, local_time_gps->tm_min, local_time_gps->tm_sec, ms_part_gps);
  211. struct timespec t_ts;
  212. int l_ret_ct = clock_gettime(CLOCK_REALTIME, &t_ts);
  213. dp_info_nt_rt("l_ret_ct = %d, time %lu, %lu", l_ret_ct, t_ts.tv_sec, t_ts.tv_nsec);
  214. u64 ullreturntime = t_ts.tv_sec * 1000 + t_ts.tv_nsec / 1000000;
  215. dp_info_nt_rt("ullreturntime %" PRIu64 "", ullreturntime);
  216. struct tm *local_time = localtime(&t_ts.tv_sec);
  217. if (local_time == NULL)
  218. {
  219. perror("localtime failed");
  220. return EXIT_FAILURE;
  221. }
  222. int ms_part = t_ts.tv_nsec / 1000000;
  223. dp_info_nt_rt("%04d-%02d-%02d %02d:%02d:%02d %03dms",
  224. local_time->tm_year + 1900, local_time->tm_mon + 1, local_time->tm_mday,
  225. local_time->tm_hour, local_time->tm_min, local_time->tm_sec, ms_part);
  226. #endif
  227. }
  228. #if defined(CPU_AM335X)
  229. watchdog_unregister_module();
  230. #endif
  231. #if defined(BSP_CAN_ENABLE)
  232. // 复位所有子板
  233. can_app_reset(0);
  234. #endif
  235. #if defined(CAN_SLAVE_BOARD)
  236. // 复位所有子板
  237. can_app_reset(0);
  238. #endif
  239. #ifdef CPU_FUXI
  240. int rc = watchdog_add_item("mainloop", &wdt_id_mainloop, 60);
  241. if (rc != 0)
  242. {
  243. printf("can not add mainloop task to wdt\r\n");
  244. return -1;
  245. }
  246. #endif
  247. us0_sec = bsp_ustimer_get_origin();
  248. rt_printf("主循环启动...\r\n");
  249. while (1)
  250. {
  251. us0 = bsp_ustimer_get_origin();
  252. // test_round();
  253. // uart_test();
  254. // flash_test();
  255. // adc_test();
  256. // gps_test();
  257. // dido_test();
  258. // rtc_test();
  259. #ifdef CPU_FUXI
  260. msleep(10);
  261. #endif
  262. // 系统的tick秒
  263. if (us0 - us0_sec > USTIMER_SEC)
  264. {
  265. g_tick_secs++;
  266. us0_sec += USTIMER_SEC;
  267. if (g_restart_count)
  268. {
  269. g_restart_count--;
  270. if (g_restart_count == 0)
  271. {
  272. rt_printf("装置重启\r\n");
  273. watchdog_reset_cpu(2); // 装置复位
  274. }
  275. }
  276. }
  277. #if defined(BSP_CAN_ENABLE)
  278. equ_check_period();
  279. can_task();
  280. #else
  281. #ifdef CAN_SLAVE_BOARD
  282. can_task();
  283. #endif
  284. #endif
  285. #ifdef CPU_FUXI
  286. sys_time_check();
  287. #endif
  288. rt_err_app();
  289. soe_save();
  290. IECCommTask();
  291. Maintain_Applay();
  292. net_debug_app();
  293. IEC104_Ether_App();
  294. FA_status_write();
  295. sec_app();
  296. clk_app();
  297. AppPolling(); // 设备状态处理,如点灯等
  298. create_set_file();
  299. check_rtc_write();
  300. #ifdef TMP_CHIP_AHT20
  301. aht20_task();
  302. #endif
  303. #ifdef METERING_ENERGY
  304. metering_yc_refresh();
  305. DD_Calc();
  306. #endif
  307. #ifdef FUNC_DRIVE
  308. quit_drive();
  309. #endif
  310. #ifdef FUNC_RESET_EQU
  311. equ_PowerRest();
  312. #endif
  313. // 喂主循环软件狗
  314. watchdog_feed_mainloop();
  315. // 统计大循环时间
  316. rt_stat_other_in(RT_STAT_BIG_LOOP, bsp_ustimer_get_duration(us0) / USTIMER_US);
  317. #ifdef LINUX_KERNEL_APP
  318. kernel_callback();
  319. #endif
  320. // us0 = ustimer_get_origin();
  321. rt_stat_other_in(7, bsp_ustimer_get_duration(us0) / USTIMER_US);
  322. }
  323. #ifdef CPU_FUXI
  324. watchdog_remove_item(wdt_id_mainloop);
  325. #endif
  326. return 0;
  327. }
  328. #if 0 // sunxi 20191018
  329. #ifndef LIB_61850_SW
  330. int gi_platelib_state=2;
  331. int app_goose_init(int fn)
  332. {
  333. return 0;
  334. }
  335. void app_goose_pub_data_handle(char *gse_data, int gseoutnum)
  336. {
  337. return;
  338. }
  339. int app_goose_recv_data_handle(unsigned int index, char *buf, const int buflen, char *outdata)
  340. {
  341. return 0;
  342. }
  343. void app_goose_timer_data_handle(const uint32_t us_diff)
  344. {
  345. return;
  346. }
  347. void goose_pub_init(char *gse_data, int gseoutnum)
  348. {
  349. return;
  350. }
  351. void app_goose_exit(void)
  352. {
  353. return;
  354. }
  355. void app_goose_inf_print(void)
  356. {
  357. }
  358. void lib61850_set_task_exit(void)
  359. {
  360. }
  361. int lib61850_start_task(void *arg)
  362. { return 0;
  363. }
  364. #endif
  365. #endif // sunxi 20191018