main.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. // 系统秒,从程序启动时刻算起
  22. u32 g_tick_secs;
  23. // 系统重启定时器
  24. u32 g_restart_count;
  25. #if defined CPU_AM335X || defined CPU_FUXI
  26. extern bool SystemInit_finish_flag __attribute__((aligned(8)));
  27. #endif
  28. /********************************************************************
  29. 版权所有:
  30. 文件版本: V1.00
  31. 文件名称: main.c
  32. 生成日期: 2008年08月26日
  33. 作 者:
  34. 使用范围:
  35. 功 能:主函数
  36. 更新信息:
  37. 更新日志1:
  38. 修改者:
  39. 修改日期:
  40. 修改内容:
  41. 修改原因:
  42. *********************************************************************/
  43. int app_main (void *arg);
  44. int kernel_callback(void);
  45. int dt_cmd_rt_reset(void);
  46. int uart_test(void);
  47. int test_round(void);
  48. int ds18b20_init(void);
  49. int sntp_init(void);
  50. int rtc_test(void);
  51. int adc_test(void);
  52. int bkpt_disable(void);
  53. #ifdef CPU_FUXI
  54. uint32_t wdt_id_mainloop;
  55. extern int printf_e907_init(void);
  56. #endif
  57. void bkpt_isr(void)
  58. {
  59. rt_printf("x\r\n");
  60. }
  61. #if defined(CPU_AM335X)
  62. static void check_app_folder_recover(void)
  63. {
  64. //应用启动后,需要判断 /tmp/app_empty_flag 文件是否存在,如果存在,则说明这次启动 app 分区进行了恢复,你那边记录后把该文件删除掉。
  65. struct file * pfile = rt_file_open("/tmp/app_empty_flag",O_RDONLY ,0);
  66. if(IS_ERR(pfile))
  67. {
  68. ;
  69. }
  70. else
  71. {
  72. rt_printf("临时文件/tmp/app_empty_flag不存在,之前发生过app分区恢复操作.\r\n");
  73. soe_record_opt(EV_APP_FOLDER_RECOVER,0);
  74. rt_file_del("/tmp/app_empty_flag");
  75. }
  76. }
  77. #endif
  78. int app_main (void *unused)
  79. {
  80. int ret;
  81. uint64_t us0,us0_sec;
  82. // 将BKPT管脚设置为中断,防止由于EMC干扰,导致系统复位。
  83. #ifdef CPU_COLDFIRE
  84. bkpt_disable();
  85. rt_request_irq(12,4,bkpt_isr,"bkpt_isr");
  86. #endif
  87. //登记bsp回调函数
  88. rt_irq_level(0);
  89. SystemInit();
  90. #ifdef CPU_FUXI
  91. SystemInit_finish_flag = true;
  92. #endif
  93. #ifdef CPU_FUXI
  94. printf_e907_init();
  95. #endif
  96. #ifdef RCD_STRAN_S
  97. _rcd_get_waveno(); /*装置地址要读出参数才有效*/
  98. #endif
  99. #ifndef CPU_FUXI
  100. watchdog_register_all();
  101. #endif
  102. dt_cmd_rt_reset();
  103. rt_stat_other_init(RT_STAT_BIG_LOOP,"主循环");
  104. rt_stat_other_init(1,"5ms_2");
  105. rt_stat_other_init(2,"5ms_3");
  106. rt_stat_other_init(3,"测量计算1");
  107. rt_stat_other_init(4,"测量计算2");
  108. rt_stat_other_init(5,"SOE保存");
  109. rt_stat_other_init(6,"临时测试");
  110. rt_stat_other_init(7,"主循环间隔");
  111. #ifdef SNTP_FUNC
  112. sntp_init();
  113. #endif
  114. {
  115. // 记录关键文件CRC
  116. char name[256];
  117. char log_info[128];
  118. sprintf(name,"上电完成。校验CRC:程序0x%04x, 资源表0x%04x, 板卡资源0x%04x, 通道配置0x%04x, 点表0x%04x。",
  119. m_CodeCrc,g_crc_rsc, g_crc_brd_res, g_crc_equ_cfg, g_crc_rectable);
  120. log_str_time(LOG_ERR,name,0,1);
  121. 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",
  122. m_CodeCrc,g_crc_rsc, g_crc_brd_res, g_crc_equ_cfg, g_crc_rectable);
  123. load_hs_log_rcd(TYPE_DEV_RESTART,true,NULL,log_info,1);
  124. }
  125. ret = get_mt_port_proc();
  126. if(ret != 1) {
  127. close_inet_port();
  128. }
  129. #if defined(CPU_AM335X)
  130. check_app_folder_recover();
  131. #endif
  132. #ifdef CPU_FUXI
  133. // noted by sunxi: 经测试发现,装置运行10多分钟后,tmp文件里的rsc.bin和set_desc.bin会莫名消失。
  134. // 经分析得,伏羲系统会周期(十多分钟)删除掉tmp文件夹中的旧文件(rsc.bin和set_desc.bin 文件是在
  135. // 系统对时前产生的,所以时间默认比较早)。因此在这里统一对tmp文件夹的文件更新一下。
  136. system("touch /tmp/*");
  137. prctl(PR_SET_NAME, "app_main_loop");
  138. #endif
  139. #ifdef RT_THREAD_DEBUG
  140. rt_printf("Thread created successfully: %s, PID: %d LWP: %d\r\n",
  141. "app_main_loop", (int)getpid(), (int)syscall(SYS_gettid));
  142. #endif
  143. dp_info_nt("最大定值个数 MAX_SET_NUMBER = %d\r\n", MAX_SET_NUMBER);
  144. dp_info_nt("装置参数 EQUIP_PARA_NUMBER = %d, PARA_NUM = %d\r\n", EQUIP_PARA_NUMBER, PARA_NUM);
  145. dp_info_nt("保护定值 SW_SET_NUMBER = %d, SW_SET_NUMBER = %d\r\n", SW_SET_NUMBER, SW_SET_NUMBER);
  146. dp_info_nt("开关个数 SWITCH_NUM_MAX = %d, 全部开关保护定值总个数 SET_NUMBER = %d\r\n", SWITCH_NUM_MAX, SET_NUMBER);
  147. dp_info_nt("公共定值 PUB_SET_NUMBER = %d, SET_PUB_NUM = %d\r\n", PUB_SET_NUMBER, SET_PUB_NUM);
  148. dp_info_nt("内部定值 CSTSET_NUMBER = %d, CSTSET_NUM = %d\r\n", CSTSET_NUMBER, CSTSET_NUM);
  149. dp_info_nt("固有参数 FIXEDSET_TABLE_NUMBER = %d\r\n", FIXEDSET_TABLE_NUMBER);
  150. {
  151. struct timespec ts;
  152. struct rtc_time_t ct;
  153. gps_get_time(&ts);
  154. timespec_to_rtc(ts, &ct, 1);
  155. rt_printf("当前时间: %04d-%02d-%02d %02d:%02d:%02d\r\n", ct.year+2000, ct.month, ct.day, ct.hour, ct.min, ct.ms/1000);
  156. }
  157. #if defined(CPU_AM335X)
  158. watchdog_unregister_module();
  159. #endif
  160. #if defined(BSP_CAN_ENABLE)
  161. // 复位所有子板
  162. can_app_reset(0);
  163. #endif
  164. #if defined(CAN_SLAVE_BOARD)
  165. // 复位所有子板
  166. can_app_reset(0);
  167. #endif
  168. #ifdef CPU_FUXI
  169. int rc = watchdog_add_item("mainloop", &wdt_id_mainloop,60);
  170. if(rc != 0) {
  171. printf("can not add mainloop task to wdt\r\n");
  172. return -1;
  173. }
  174. #endif
  175. us0_sec = bsp_ustimer_get_origin();
  176. rt_printf("主循环启动...\r\n");
  177. while(1)
  178. {
  179. us0 = bsp_ustimer_get_origin();
  180. // test_round();
  181. // uart_test();
  182. // flash_test();
  183. // adc_test();
  184. // gps_test();
  185. // dido_test();
  186. // rtc_test();
  187. #ifdef CPU_FUXI
  188. msleep(100);
  189. // shm_comm_test();
  190. #endif
  191. // 系统的tick秒
  192. if(us0 - us0_sec > USTIMER_SEC)
  193. {
  194. g_tick_secs++;
  195. us0_sec += USTIMER_SEC;
  196. if(g_restart_count)
  197. {
  198. g_restart_count--;
  199. if(g_restart_count == 0)
  200. {
  201. rt_printf("装置重启\r\n");
  202. watchdog_reset_cpu(2); //装置复位
  203. }
  204. }
  205. }
  206. #if defined(BSP_CAN_ENABLE)
  207. equ_check_period();
  208. can_task();
  209. #else
  210. #ifdef CAN_SLAVE_BOARD
  211. can_task();
  212. #endif
  213. #endif
  214. #ifdef CPU_FUXI
  215. sys_time_check();
  216. #endif
  217. rt_err_app();
  218. soe_save();
  219. IECCommTask();
  220. Maintain_Applay();
  221. net_debug_app();
  222. IEC104_Ether_App();
  223. FA_status_write();
  224. sec_app();
  225. clk_app();
  226. AppPolling(); // 设备状态处理,如点灯等
  227. create_set_file();
  228. #ifdef TMP_CHIP_AHT20
  229. aht20_task();
  230. #endif
  231. #ifdef METERING_ENERGY
  232. metering_yc_refresh();
  233. DD_Calc();
  234. #endif
  235. #ifdef FUNC_DRIVE
  236. quit_drive();
  237. #endif
  238. #ifdef FUNC_RESET_EQU
  239. equ_PowerRest();
  240. #endif
  241. // 喂主循环软件狗
  242. watchdog_feed_mainloop();
  243. //统计大循环时间
  244. rt_stat_other_in(RT_STAT_BIG_LOOP,bsp_ustimer_get_duration(us0)/USTIMER_US);
  245. #ifdef LINUX_KERNEL_APP
  246. kernel_callback();
  247. #endif
  248. //us0 = ustimer_get_origin();
  249. rt_stat_other_in(7,bsp_ustimer_get_duration(us0)/USTIMER_US);
  250. }
  251. #ifdef CPU_FUXI
  252. watchdog_remove_item(wdt_id_mainloop);
  253. #endif
  254. return 0;
  255. }
  256. #if 0 // sunxi 20191018
  257. #ifndef LIB_61850_SW
  258. int gi_platelib_state=2;
  259. int app_goose_init(int fn)
  260. {
  261. return 0;
  262. }
  263. void app_goose_pub_data_handle(char *gse_data, int gseoutnum)
  264. {
  265. return;
  266. }
  267. int app_goose_recv_data_handle(unsigned int index, char *buf, const int buflen, char *outdata)
  268. {
  269. return 0;
  270. }
  271. void app_goose_timer_data_handle(const uint32_t us_diff)
  272. {
  273. return;
  274. }
  275. void goose_pub_init(char *gse_data, int gseoutnum)
  276. {
  277. return;
  278. }
  279. void app_goose_exit(void)
  280. {
  281. return;
  282. }
  283. void app_goose_inf_print(void)
  284. {
  285. }
  286. void lib61850_set_task_exit(void)
  287. {
  288. }
  289. int lib61850_start_task(void *arg)
  290. { return 0;
  291. }
  292. #endif
  293. #endif // sunxi 20191018