main.c 6.6 KB

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