recordlog.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. #define LOG_TAG "recordlog" // 该模块对应的标签。不定义时,默认:NO_TAG
  2. #define LOG_LVL LOG_LVL_DBG // 该模块对应的日志输出级别。不定义时,默认:调试级别
  3. #include <ulog.h> // 必须在 LOG_TAG 与 LOG_LVL 下面
  4. #include <rtthread.h>
  5. #include <rtdevice.h>
  6. #include "stdio.h"
  7. #include "string.h"
  8. #include <board.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <easyflash.h>
  12. #include "sys_config.h"
  13. #include "CH376HFT.h"
  14. uint16_t OneFile_Length = 3000; // 每个文件的数据条数 会在定义的范围内波动
  15. // uint8_t Log_Export_Percent = 0; /*数据导出U盘百分比*/
  16. // int32_t Log_Export_Time = 0; /*数据导出需要时间估算*/
  17. extern uint32_t Uflash_status;
  18. static void read_flash_record(size_t index, size_t size);
  19. static void read_all_flash_record(void);
  20. static void read_recent_flash_log(size_t size);
  21. rt_bool_t record_clean(void);
  22. static void write_flash_record(const char *log, size_t len);
  23. rt_uint8_t export_logdata_flag = 0;
  24. /* 数据记录邮箱控制块 */
  25. struct rt_mailbox record_mb;
  26. /* 用于放邮件的内存池 */
  27. static char mb_pool[32];
  28. /* 数据记录过程中保护的互斥量 */
  29. rt_mutex_t record_mutex = RT_NULL;
  30. /*获取flash的使用量*/
  31. uint64_t record_used_size(void)
  32. {
  33. return ef_log_get_used_size();
  34. }
  35. static uint16_t Log_Timer_ms = 0;
  36. static uint32_t Log_Timer_1s = 0;
  37. void ELOG_Timer(void)
  38. {
  39. Log_Timer_ms++;
  40. if (Log_Timer_ms > (999 - 4))
  41. {
  42. Log_Timer_ms = 0;
  43. Log_Timer_1s++;
  44. }
  45. }
  46. #define TIMED_EVENT (1 << 0) // 计时事件
  47. /* 定时事件控制块 */
  48. static struct rt_event timed_event;
  49. /* 定时器的控制块 */
  50. static rt_timer_t record_timed_task;
  51. /* 定时器 超时函数 */
  52. // static void record_timer(void *parameter)
  53. // {
  54. // static rt_uint32_t cnt = 0;
  55. // cnt++;
  56. // if (cnt % ((rt_uint32_t)parameter * qxz.Param.save_interval) == 0) //*qxz.Param.upload_interval
  57. // {
  58. // // // log_i("mqtt_timed_task time out %d min\r\n",1);
  59. // rt_event_send(&timed_event, TIMED_EVENT); // 定时时间到,发送事件
  60. // }
  61. // }
  62. #define STATUS_DIS_LEN 100 // 显示缓冲区长度
  63. /*
  64. #define DIS_POS1 "interval.t9.txt=\"%s\""
  65. #define DIS_POS_val1_val2 "interval.t9.txt=\"%s%d%% 约耗时%dS\""
  66. #define DIS_POS_val1 "interval.t9.txt=\"%s%d%%\""
  67. */
  68. // #define DIS_POS_val1_val2 "main.t36.txt=\"%s%d%% 仍需%dS\""
  69. #define DIS_POS "main.t36.txt=\"%s\""
  70. #define DIS_POS_val1_val2 "main.t36.txt=\"%s%d/%d\""
  71. #define DIS_POS_val1 "main.t36.txt=\"%s%d%%\""
  72. #define DIS_POS1 "interval.t9.txt=\"%s\""
  73. #define export_succe "导出成功"
  74. #define exporting_per "已存文件:"
  75. #define record_del "数据删除中,请稍后"
  76. #define record_del_succe "数据删除成功"
  77. #define record_del_fail "数据删除失败"
  78. #define u_release "U盘成功移除"
  79. #define u_retry_1 "U盘检查失败"
  80. #define u_retry_2 "请在5秒内重试"
  81. #define u_retry_3 "请在10秒内重试"
  82. #define u_reboot "导出失败,请重试"
  83. #define u_hint "准备导出 确保U盘已接入"
  84. #define u_wait "正在导出中..."
  85. extern rt_err_t HMI_Display(void *Data, rt_size_t length);
  86. /*屏幕显示相应的操作状态*/
  87. void Status_Display(const char *content, uint16_t value1, int16_t value2)
  88. {
  89. char *dis_buf = RT_NULL;
  90. dis_buf = rt_malloc(STATUS_DIS_LEN);
  91. rt_memset(dis_buf, '\0', STATUS_DIS_LEN);
  92. if ((value1 == RT_NULL) && (value2 == RT_NULL) && (content != RT_NULL))
  93. {
  94. rt_sprintf(dis_buf, DIS_POS, content);
  95. }
  96. else if ((value1 != RT_NULL) && (value2 != RT_NULL) && (content != RT_NULL))
  97. {
  98. rt_sprintf(dis_buf, DIS_POS_val1_val2, content, value1, value2);
  99. }
  100. else if ((value1 != RT_NULL) && (value2 == RT_NULL) && (content == RT_NULL))
  101. {
  102. rt_sprintf(dis_buf, DIS_POS_val1, value1);
  103. }
  104. else if ((value1 == RT_NULL) && (value2 == 0xFF) && (content != RT_NULL))
  105. {
  106. rt_sprintf(dis_buf, DIS_POS1, content);
  107. }
  108. HMI_Display(dis_buf, rt_strlen(dis_buf)); // 存入队列
  109. rt_free(dis_buf);
  110. }
  111. /*输出全部flash中的日志*/
  112. /*非阻塞,能输出进度百分比*/
  113. /*5分钟存储1条,1万条约为34天*/
  114. /*转存1万*148byte条约需要2分37秒,1s约转存9kbyte*/
  115. // 导出Log
  116. uint32_t excnt = 0;
  117. uint8_t exfail = 0;
  118. volatile uint8_t off_ef_env = 0;
  119. /*目前可用容量14M,148byte1条数据,约能存储100,000条数据*/
  120. unsigned char record_export_Uflash(void) // 已经自动记录到SD
  121. {
  122. // static uint16_t i, save_lenth_flag = 0;
  123. // static uint16_t export_percent = 500; /*转存500次*/
  124. // static uint16_t export_percent_big = 3000; /*转存3000次*/
  125. // static uint16_t export_percent_super_big = 5000; /*转存5000次*/
  126. // static uint32_t export_size = 0; /*1次输出数据大小*/
  127. // static uint32_t record_count = 0; /*存储的记录数量*/
  128. // extern rt_timer_t HIM_POWER_PIN_TIMER; // 屏幕关闭倒计时计时器
  129. // static uint32_t current_used_size = 0;
  130. // static uint16_t current_num_level = 0;
  131. // static uint8_t file_num_ctl = 0;
  132. // static uint16_t save_level = 0;
  133. // char cache[96] = {0};
  134. // record_count = record_used_size() / sys_info.record_len; /*获得存储的记录数量*/
  135. // current_used_size = record_used_size();
  136. // // rt_kprintf("\r\delay to test\r\n");
  137. // rt_thread_mdelay(10);
  138. // log_i("\r\rt_mutex_take waitting exdata \r\n");
  139. // log_i("record_count is %d", record_count);
  140. // // list_mem();
  141. // save_lenth_flag++;
  142. // rt_mutex_take(record_mutex, RT_WAITING_FOREVER); /*互斥量数据记录*/
  143. // Status_Display(u_wait, RT_NULL, RT_NULL);
  144. // current_num_level = 16000 / sys_info.record_len; /* 次数的缓存取其实比8000更大 后面保留冗余 */
  145. // if (sys_info.record_len > 200)
  146. // OneFile_Length = OneFile_Length / 2;
  147. // if (record_count < export_percent) /*小于100条记录,直接转存*/
  148. // {
  149. // LOG_I(" current current_num_level is 100 , record_count :%d", record_count);
  150. // read_flash_record(export_size, current_used_size);
  151. // rt_kprintf("use once save mode\n\n");
  152. // }
  153. // /* 分500次转存 */
  154. // else if (record_count / export_percent < current_num_level) /*判断当前转存一次的大小是否超过缓冲区*/
  155. // {
  156. // save_level = export_percent;
  157. // rt_kprintf("use 500times save mode\n\n");
  158. // }
  159. // /*
  160. // record_count : 当前导出时的数据总量(一共有多少条)
  161. // export_percent_big : 此次分包转存的分割次数(分几个csv文件导出)
  162. // current_num_level : 当前缓存区和通道数量下 一次能够转存的最大条数
  163. // */
  164. // /* 分3000次转存时 */
  165. // else if (record_count / export_percent_big < current_num_level) /*判断当前转存一次的大小是否超过缓冲区*/
  166. // {
  167. // save_level = export_percent_big;
  168. // rt_kprintf("use 3000times save mode\n\n");
  169. // }
  170. // /* 分5000次转存 */
  171. // else if (record_count / export_percent_super_big < current_num_level) /*判断当前转存一次的大小是否超过缓冲区*/
  172. // {
  173. // save_level = export_percent_super_big;
  174. // rt_kprintf("use 5000times save mode\n\n");
  175. // }
  176. // if (HIM_POWER_PIN_TIMER != RT_NULL)
  177. // rt_timer_start(HIM_POWER_PIN_TIMER); /*设置屏幕关闭时间为0,操作期间常亮*/
  178. // LOG_I(" current current_num_level is :%d ,save times is: %d OneFile_Length is : %d", save_level, (record_count / save_level), OneFile_Length);
  179. // for (i = 0; i < save_level; i++)
  180. // {
  181. // // Log_Export_Time = (current_used_size - export_size) / 1024 / 9; /*估算耗时*/
  182. // // Log_Export_Time = record_count/150;
  183. // if (export_size + ((record_count / save_level) * sys_info.record_len) < ef_log_get_used_size())
  184. // read_flash_record(export_size, (record_count / save_level) * sys_info.record_len); /*得到的记录总数/100,乘以每条记录长度*/
  185. // else
  186. // read_flash_record(export_size, ef_log_get_used_size() - export_size);
  187. // export_size += (record_count / save_level) * sys_info.record_len;
  188. // if (i % (save_level / (record_count / OneFile_Length)) == 0 && i != 0)
  189. // {
  190. // Save_file();
  191. // rt_thread_mdelay(10);
  192. // if (CH376_check() == 1)
  193. // {
  194. // Status_Display(exporting_per, save_lenth_flag, (record_count / OneFile_Length) + 1);
  195. // LOG_I("save logfile onec and creat new file success , file is %d i is %d", save_lenth_flag++, i);
  196. // }
  197. // }
  198. // if (i % 10 == 0)
  199. // rt_kprintf("ex pre > %d/%d \n", i + 1, save_level + 1);
  200. // if (i > 0 && i % 50 == 0)
  201. // {
  202. // sprintf(cache, "正在导出 %d%%", (uint8_t)((i * 100) / save_level));
  203. // Status_Display(cache, RT_NULL, RT_NULL); /*HMI显示数据导出成功*/
  204. // }
  205. // if (exfail != 0)
  206. // {
  207. // sprintf(cache, "导出失败 %02X", exfail);
  208. // Status_Display(cache, RT_NULL, RT_NULL);
  209. // rt_thread_mdelay(2000);
  210. // break;
  211. // }
  212. // }
  213. // i = 0;
  214. // save_lenth_flag = 0;
  215. // if (exfail != 0)
  216. // {
  217. // read_flash_record(export_size, current_used_size - export_size);
  218. // rt_kprintf("ex pre > %d/%d \n", save_level + 1, save_level + 1);
  219. // }
  220. // Save_file(); /*最后保存文件*/
  221. // if (excnt > 144 && exfail == 0)
  222. // {
  223. // rt_kprintf("ex succ cnt %d \n", excnt);
  224. // char cache[96] = {0};
  225. // sprintf(cache, "导出成功 %dK", excnt / 1024);
  226. // Status_Display(cache, RT_NULL, RT_NULL); /*HMI显示数据导出成功*/
  227. // }
  228. // else
  229. // {
  230. // rt_kprintf("ex fail please try again \n");
  231. // Status_Display(u_reboot, RT_NULL, RT_NULL); /*HMI显示数据导出成功*/
  232. // }
  233. // i = 0; /*相关静态变量清零*/
  234. // // Log_Export_Time = 0;
  235. // export_size = 0;
  236. // record_count = 0;
  237. // if (HIM_POWER_PIN_TIMER != RT_NULL)
  238. // rt_timer_start(HIM_POWER_PIN_TIMER); //
  239. // LOG_W("\r\n exdata finshed please take out Udisk \r\n");
  240. // rt_mutex_release(record_mutex); /*释放互斥量数据记录*/
  241. // // 复原flag = 0;
  242. // off_ef_env = 200;
  243. // export_logdata_flag = 0;
  244. // ef_set_env_blob("export_logdata_flag", &export_logdata_flag, sizeof(export_logdata_flag));
  245. // ef_get_env_blob("export_logdata_flag", &export_logdata_flag, sizeof(export_logdata_flag), NULL);
  246. // if (export_logdata_flag == 0)
  247. // {
  248. // LOG_I("Restore export flag");
  249. // log_w("logdata export finish ,system rebooting............");
  250. // // Status_Display(export_succe, RT_NULL, RT_NULL);
  251. // rt_thread_mdelay(100);
  252. // rt_hw_cpu_reset();
  253. // }
  254. return 0;
  255. }
  256. extern uint32_t month;
  257. extern uint32_t day;
  258. extern uint32_t year;
  259. extern uint32_t hour;
  260. extern uint32_t min;
  261. extern uint32_t sec;
  262. #include "ch376_driver.h"
  263. #include <CH376/bsp/bsp_csv.h>
  264. #define RECODE_FLASH CH376_MODE_SD_CARD // 251210 换了新板子使用sd卡
  265. //#define RECODE_FLASH CH376_MODE_USB_DISK
  266. rt_sem_t datatoSD = RT_NULL; // 记录数据信号量
  267. uint32_t RecodeDataToSD_M(void)
  268. {
  269. if (datatoSD != NULL)
  270. {
  271. rt_sem_release(datatoSD); // 通知进行补录
  272. }
  273. else
  274. {
  275. rt_kprintf("recode_edataM FAIL\n");
  276. }
  277. return 0;
  278. }
  279. MSH_CMD_EXPORT(RecodeDataToSD_M ,RecodeDataToSD_M);
  280. static uint32_t RecodedatatoSD(void)
  281. {
  282. static char once = 1;
  283. char filename[12] = {0};
  284. sprintf((char *)filename, (const char *)"%02d%02d%02d.CSV", year - 2000, month, day);
  285. char title_name[1024] = {0};
  286. sprintf(title_name, "序号,时间");
  287. for (char a = 0; a < qxz.Param.Num_sensors; a++)
  288. {
  289. strcat((char *)title_name, ",");
  290. strcat((char *)title_name, (const char *)sensor[a].cName);
  291. strcat((char *)title_name, (const char *)sensor[a].units);
  292. }
  293. strcat((char *)title_name, (const char *)"\n");
  294. // if(once)
  295. // {
  296. // rt_kprintf("title_name <%s>\n" , title_name);
  297. // once = 0;
  298. // }
  299. if (CSV_INIT(RECODE_FLASH) != CSV_SUCCESS)
  300. {
  301. rt_kprintf("? Initialization failed\n");
  302. return 0;
  303. }
  304. int line_c = CSV_COUNT_FILE_LINES(filename);
  305. if (line_c == 0)
  306. {
  307. line_c = 1;
  308. }
  309. CSV_OPEN(filename, CSV_MODE_APPEND, title_name);
  310. sprintf(title_name, "%d,%04d-%02d-%02dT%02d:%02d:%02d", line_c,
  311. year,
  312. month,
  313. day,
  314. hour,
  315. min,
  316. sec);
  317. for (char a = 0; a < qxz.Param.Num_sensors; a++)
  318. {
  319. strcat((char *)title_name, ",");
  320. if (sensor[a].online_flag)
  321. {
  322. strcat((char *)title_name, (const char *)sensor[a].eValue);
  323. }
  324. else
  325. {
  326. strcat((char *)title_name, (const char *)"NA");
  327. }
  328. }
  329. // rt_kprintf("filename <%s> title_name <%s> line %d\n", filename, title_name, line_c);
  330. CSV_APPEND(title_name);
  331. CSV_CLOSE();
  332. CSV_DEINIT();
  333. return (line_c+1);
  334. }
  335. /**
  336. * @brief Get the Today Recode object 获取今天记录了多少条
  337. *
  338. * @return uint16_t
  339. */
  340. uint16_t GetTodayRecode(void)
  341. {
  342. char filename[12] = {0};
  343. sprintf((char *)filename, (const char *)"%02d%02d%02d.CSV", year - 2000, month, day);
  344. if (CSV_INIT(RECODE_FLASH) != CSV_SUCCESS)
  345. {
  346. rt_kprintf("? Initialization failed\n");
  347. return;
  348. }
  349. uint16_t line_c = CSV_COUNT_FILE_LINES(filename);
  350. CSV_DEINIT();
  351. return line_c;
  352. }
  353. // 记录Log
  354. /*按照目前的配置14Mflash,16个传感器共计每条数据140byte,约能存储100,000条数据*/
  355. /**
  356. * @brief 251124 更新数据存储到外置SD卡,几乎实现无限制存储(每天一个csv)
  357. *
  358. * @return unsigned char
  359. */
  360. unsigned char Record_log(void)
  361. {
  362. // if (qxz.Param.save_flash_flag == 1 || 1) //保存到flash标志位使能
  363. // if (rt_event_recv(&timed_event, TIMED_EVENT,
  364. // RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
  365. // 0, &e) == RT_EOK) //定时时
  366. static uint32_t recode_timeout = 0;
  367. recode_timeout++;
  368. //rt_kprintf("Record_log >> %d > %d\n" ,recode_timeout, (qxz.Param.save_interval * 60));
  369. if ((recode_timeout >= (qxz.Param.save_interval * 60 - 8)) || (rt_sem_take(datatoSD, RT_WAITING_NO) == RT_EOK))
  370. {
  371. recode_timeout = 0;
  372. rt_mutex_take(record_mutex, RT_WAITING_FOREVER); /*互斥量数据记录*/
  373. uint32_t cnt = RecodedatatoSD();
  374. if(cnt == 0)
  375. {
  376. cnt = RecodedatatoSD();
  377. }
  378. LOG_I("Record_log = %lu\n",cnt);
  379. rt_mutex_release(record_mutex); /*释放互斥量数据记录*/
  380. return true;
  381. }
  382. return 0;
  383. }
  384. MSH_CMD_EXPORT(Record_log, Record_log);
  385. static void read_flash_record(size_t index, size_t size)
  386. {
  387. /* 64 bytes buffer */
  388. char buf[300] = {0};
  389. size_t log_total_size = ef_log_get_used_size();
  390. size_t buf_size = sizeof(buf);
  391. size_t read_size = 0;
  392. /* word alignment for index and size */
  393. index = RT_ALIGN_DOWN(index, 4);
  394. size = RT_ALIGN_DOWN(size, 4);
  395. if (index + size > log_total_size)
  396. {
  397. rt_kprintf("The output position and size is out of bound. The max size is %d.\n", log_total_size);
  398. LOG_I("index is :%d , size is :%d", index, size);
  399. return;
  400. }
  401. while (1)
  402. {
  403. rt_memset(buf, 0, buf_size);
  404. if (read_size + buf_size < size)
  405. {
  406. ef_log_read(index + read_size, (uint32_t *)buf, buf_size);
  407. Recordfile((char *)buf, buf_size); // 存入U盘缓冲区
  408. // else LOG_I(" cannot find '20' in buf, this is a bad data \n ");
  409. // rt_kprintf("%.*s", buf_size, buf);
  410. read_size += buf_size;
  411. rt_thread_mdelay(10); // 适当延时调度,用于显现导出状态
  412. }
  413. else
  414. {
  415. ef_log_read(index + read_size, (uint32_t *)buf, size - read_size);
  416. Recordfile((char *)buf, buf_size); // 存入U盘缓冲区
  417. // else LOG_I(" cannot find '20' in buf, this is a bad data \n ");
  418. rt_thread_mdelay(10); // 适当延时调度,用于显现导出状态
  419. break;
  420. }
  421. }
  422. }
  423. /**
  424. * Read and output all log which saved in flash.
  425. */
  426. static void read_all_flash_record(void)
  427. {
  428. read_flash_record(0, ef_log_get_used_size());
  429. }
  430. /**
  431. * Read and output recent log which saved in flash.
  432. *
  433. * @param size recent log size
  434. */
  435. static void read_recent_flash_log(size_t size)
  436. {
  437. size_t max_size = ef_log_get_used_size();
  438. if (size == 0)
  439. {
  440. return;
  441. }
  442. if (size > max_size)
  443. {
  444. rt_kprintf("The output size is out of bound. The max size is %d.\n", max_size);
  445. }
  446. else
  447. {
  448. read_flash_record(max_size - size, size);
  449. }
  450. }
  451. /**
  452. * clean all log which in flash
  453. */
  454. rt_bool_t record_clean(void)
  455. {
  456. EfErrCode clean_result = EF_NO_ERR;
  457. /* clean all log which in flash */
  458. clean_result = ef_log_clean();
  459. if (clean_result == EF_NO_ERR)
  460. {
  461. LOG_I("All logs which in flash is clean OK.");
  462. return RT_TRUE;
  463. }
  464. else
  465. {
  466. LOG_E("Clean logs which in flash has an error!");
  467. return RT_TRUE;
  468. }
  469. }
  470. static void write_flash_record(const char *log, size_t len)
  471. {
  472. /* write some '\r' for word alignment */
  473. char write_overage_c[4] = {'\r', '\r', '\r', '\r'};
  474. size_t write_size_temp = 0;
  475. EfErrCode result = EF_NO_ERR;
  476. /* calculate the word alignment write size */
  477. write_size_temp = RT_ALIGN_DOWN(len, 4);
  478. // log_i("record_flash read:%d",write_size_temp);
  479. result = ef_log_write((uint32_t *)log, write_size_temp);
  480. /* write last word alignment data */
  481. if ((result == EF_NO_ERR) && (write_size_temp != len))
  482. {
  483. memcpy(write_overage_c, log + write_size_temp, len - write_size_temp);
  484. ef_log_write((uint32_t *)write_overage_c, sizeof(write_overage_c));
  485. }
  486. }
  487. #if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH)
  488. #include <finsh.h>
  489. static void record_flash(uint8_t argc, char **argv)
  490. {
  491. if (argc >= 2)
  492. {
  493. if (!strcmp(argv[1], "read"))
  494. {
  495. if (argc >= 3)
  496. {
  497. read_recent_flash_log(atol(argv[2]));
  498. }
  499. else
  500. {
  501. read_all_flash_record();
  502. }
  503. }
  504. else if (!strcmp(argv[1], "clean"))
  505. {
  506. record_clean();
  507. }
  508. else
  509. {
  510. rt_kprintf("Please input ulog_flash <read|clean>.\n");
  511. }
  512. }
  513. else
  514. {
  515. rt_kprintf("Please input ulog_flash <read|clean>.\n");
  516. }
  517. }
  518. MSH_CMD_EXPORT(record_flash, record<read | clean> flash log by EasyFlash backend);
  519. #endif /* defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) */
  520. // record_flash read
  521. // record_flash clean
  522. rt_uint32_t record_status; // 从邮箱中获取到的操作状态
  523. /*数据记录任务,包含记录及导出U盘*/
  524. static void recordlog_thread(void *param)
  525. {
  526. rt_thread_mdelay(3300);
  527. uint8_t x = 0, i = 0;
  528. uint8_t write_overage_size = 0;
  529. uint32_t elog_len = 0;
  530. uint32_t total = 0; /* 记录当前缓冲在elog_buff中的数据长度 */
  531. rt_uint32_t e;
  532. uint32_t ex_cnt = 0;
  533. datatoSD = rt_sem_create("datatoSD", 0, RT_IPC_FLAG_PRIO);
  534. while (1)
  535. {
  536. /* 从邮箱中收取邮件 */
  537. if (rt_mb_recv(&record_mb, (rt_uint32_t *)&record_status, 150) == RT_EOK)
  538. {
  539. switch (record_status)
  540. {
  541. case 4:
  542. // 当得知要导出数据 首先给标志位赋1
  543. Status_Display(u_hint, RT_NULL, 0xFF);
  544. rt_thread_mdelay(3000);
  545. Status_Display(u_wait, RT_NULL, 0xFF);
  546. ex_cnt = transfer_csv_all_known_exists();
  547. rt_thread_mdelay(1000);
  548. char cache[32] = {0};
  549. sprintf(cache , "成功导出%d份表格" ,ex_cnt);
  550. Status_Display(cache, RT_NULL, 0xFF);
  551. ex_cnt = 0;
  552. break;
  553. case 5:
  554. rt_thread_mdelay(1000); // 延时避开屏幕刷新清屏的时间
  555. Status_Display(record_del, RT_NULL, RT_NULL);
  556. if (record_clean() == RT_TRUE)
  557. Status_Display(record_del_succe, RT_NULL, RT_NULL);
  558. else
  559. Status_Display(record_del_fail, RT_NULL, RT_NULL);
  560. break;
  561. case 6:
  562. // Save_file();
  563. Status_Display(u_release, RT_NULL, RT_NULL);
  564. log_i("CH376_SafeRemoveDis");
  565. break;
  566. default:
  567. break;
  568. }
  569. }
  570. Record_log();
  571. rt_thread_mdelay(1000);
  572. Uflash_status = 9; /*恢复初始状态 */
  573. export_logdata_flag = 0;
  574. }
  575. }
  576. static int recordlog(void)
  577. {
  578. rt_thread_t tid;
  579. rt_err_t result;
  580. /* 初始化一个 mailbox */
  581. result = rt_mb_init(&record_mb,
  582. "re_mb", /* 名称是 mbt */
  583. &mb_pool[0], /* 邮箱用到的内存池是 mb_pool */
  584. sizeof(mb_pool) / 4, /* 邮箱中的邮件数目,因为一封邮件占 4 字节 */
  585. RT_IPC_FLAG_FIFO); /* 采用 FIFO 方式进行线程等待 */
  586. if (result != RT_EOK)
  587. {
  588. log_e("init mailbox failed.\n");
  589. return -1;
  590. }
  591. record_mutex = rt_mutex_create("rmutex", RT_IPC_FLAG_FIFO);
  592. if (record_mutex == RT_NULL)
  593. {
  594. log_e("create record dynamic mutex failed.\n");
  595. return -1;
  596. }
  597. /* 初始化事件对象 */
  598. // result = rt_event_init(&timed_event, "event", RT_IPC_FLAG_FIFO);
  599. // if (result != RT_EOK)
  600. // {
  601. // log_e("init event failed.\n");
  602. // return -1;
  603. // }
  604. /* 创建定时器 1 周期定时器 */ //&qxz.Param.save_interval
  605. // record_timed_task = rt_timer_create("recordlog_timer", record_timer,
  606. // (void *)60, 1000,
  607. // RT_TIMER_FLAG_PERIODIC);
  608. /* 启动定时器 1 */
  609. if (record_timed_task != RT_NULL)
  610. rt_timer_start(record_timed_task);
  611. // rt_timer_stop(record_timed_task);
  612. // rt_timer_start(record_timed_task);
  613. tid = rt_thread_create("recordlog",
  614. recordlog_thread, RT_NULL,
  615. 1024 + 1024 + 512,
  616. 0, 10); // 优先级要比刷新显示线程低,实时显示操作状态
  617. if (tid != RT_NULL)
  618. rt_thread_startup(tid);
  619. // // 检测是否需要导出数据
  620. // if (ef_get_env_blob("export_logdata_flag", &export_logdata_flag, sizeof(export_logdata_flag), NULL))
  621. // {
  622. // if (export_logdata_flag == 1)
  623. // {
  624. // off_ef_env = 100;
  625. // rt_kprintf("ef_env off \n");
  626. // if (CH376_check() == 1)
  627. // {
  628. // log_w("start Log_Export_Uflash ");
  629. // record_export_Uflash();
  630. // }
  631. // else
  632. // {
  633. // Status_Display(u_retry_1, RT_NULL, RT_NULL); /*HMI显示U盘检查失败*/
  634. // log_e("CH376 check failed, please try again within five seconds ");
  635. // rt_thread_mdelay(2000);
  636. // Status_Display(u_retry_2, RT_NULL, RT_NULL); /*HMI显示U盘检查失败*/
  637. // rt_thread_mdelay(4000);
  638. // if (CH376_check() == 1)
  639. // {
  640. // Status_Display(u_wait, RT_NULL, RT_NULL);
  641. // log_w("start Log_Export_Uflash ");
  642. // record_export_Uflash();
  643. // }
  644. // else
  645. // {
  646. // Status_Display(u_retry_1, RT_NULL, RT_NULL); /*HMI显示U盘检查失败*/
  647. // log_e("CH376 check failed, please try again within five seconds ");
  648. // rt_thread_mdelay(2000);
  649. // Status_Display(u_retry_3, RT_NULL, RT_NULL); /*HMI显示U盘检查失败*/
  650. // rt_thread_mdelay(9000);
  651. // if (CH376_check() == 1)
  652. // {
  653. // Status_Display(u_wait, RT_NULL, RT_NULL);
  654. // log_w("start Log_Export_Uflash ");
  655. // record_export_Uflash();
  656. // }
  657. // else
  658. // {
  659. // export_logdata_flag = 0;
  660. // off_ef_env = 200;
  661. // ef_set_env_blob("export_logdata_flag", &export_logdata_flag, sizeof(export_logdata_flag));
  662. // Status_Display(u_reboot, RT_NULL, RT_NULL); /*HMI显示U盘检查失败*/
  663. // rt_thread_mdelay(1000);
  664. // log_w("logdata export failed ,system rebooting............");
  665. // rt_hw_cpu_reset();
  666. // }
  667. // }
  668. // }
  669. // off_ef_env = 200;
  670. // rt_kprintf("ef_env on \n");
  671. // }
  672. // }
  673. return RT_EOK;
  674. }
  675. INIT_APP_EXPORT(recordlog);
  676. // void exdata(void)
  677. // {
  678. // export_logdata_flag = 1;
  679. // ef_set_env_blob("export_logdata_flag", &export_logdata_flag, sizeof(export_logdata_flag));
  680. // // 再读出来确认一边
  681. // ef_get_env_blob("export_logdata_flag", &export_logdata_flag, sizeof(export_logdata_flag), NULL);
  682. // if (export_logdata_flag == 1)
  683. // {
  684. // log_w("prepare to Log_Export_Uflash ,Please plug in the USB flash drive");
  685. // rt_thread_mdelay(100);
  686. // Status_Display(u_hint, RT_NULL, 0xFF);
  687. // log_w("system rebooting............");
  688. // rt_hw_cpu_reset();
  689. // }
  690. // }
  691. // MSH_CMD_EXPORT(exdata, "exdata");
  692. UINT8 CH376GetVerStatus(void) /* 获取中断状态并取消中断请求 */
  693. {
  694. UINT8 s;
  695. xWriteCH376Cmd(CMD01_GET_IC_VER);
  696. s = xReadCH376Data();
  697. xEndCH376Cmd();
  698. rt_kprintf("CH376 Get Ver 0x%02X\n", s);
  699. return (s);
  700. }
  701. MSH_CMD_EXPORT(CH376GetVerStatus, "CH376GetVerStatus");