poweroff_save_app.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #include "global.h"
  2. #include "hw_flash_data.h"
  3. #include "poweroff_save_app.h"
  4. #include "variable.h"
  5. #include "board.h"
  6. #include <string.h>
  7. #include "MathHelper.h"
  8. #include "log_app.h"
  9. //下次保存地址
  10. uint32_t next_save_posi = POWEROFF_SAVE_ADDRESS;
  11. //上电标志
  12. static unsigned char power_on_flag = 0;
  13. //定时器
  14. static sw_timer_t timer;
  15. static int power_off_sample = 0;
  16. //记录对象实体
  17. //static tiny_log_t tinylog;
  18. //unsigned char log_data[256];
  19. /**
  20. * 掉电工作初始化
  21. * 每次上电时先调用这个函数检查下第一块是否有空的位置让参数去保存,没有就先擦除第二块,然后将第一块的有效参数保存进去
  22. * 然后擦除第一块
  23. *
  24. * @author lxz (2019/5/31/鍛ㄤ簲)
  25. *
  26. * @param void
  27. */
  28. void poweroff_app_init(void)
  29. {
  30. int block_size = POWEROFF_SAVE_BLOCK_SIZE;
  31. int block_number = POWEROFF_SAVE_SECTOR_SIZE / block_size;
  32. int date_NoEmpty = 0;
  33. int NoEmpty_index = 0;
  34. int i = 0;
  35. int retry=0;
  36. //while (hw_dma_uart_write_finish(2) == 0);
  37. //等待一断时间,检测下看电源是否稳定再操作
  38. sw_timer_start(&timer, POWER_OFF_FUNCTION_DELAY, 0);
  39. while (sw_timer_expire(&timer) == 0);
  40. power_on_flag = hw_power_is_on();
  41. i = block_number;
  42. do
  43. {
  44. i--;
  45. //查找下非空的flash地址
  46. if (hw_flash_data_check_empty(POWEROFF_SAVE_ADDRESS + i * block_size, block_size) != 0)
  47. {
  48. date_NoEmpty=1;
  49. NoEmpty_index=i;//数据非空的地址编号
  50. break;
  51. }
  52. } while (i > 0);
  53. //说明已经写满了,这时候,需要先清除第二块flash,并将第一块的数据存到第二块去
  54. if (i >= block_number - 1)
  55. {
  56. //检测下是否有24V供电,没供电不要进行这个操作
  57. if (hw_power_is_on())
  58. {
  59. //读取第一块中的有效数据
  60. do
  61. {
  62. //必须保证CRC校验成功
  63. hw_flash_data_read(POWEROFF_SAVE_ADDRESS + (i)*block_size, user_datas, block_size);
  64. if (mh_crc16_calc((unsigned char *)&user_datas[0], block_size) == 0)
  65. {
  66. //读取到有效数据,存到第二块去
  67. hw_flash_data_write(POWEROFF_BACKUP_ADDRESS, user_datas, block_size);
  68. break;
  69. }
  70. i--;
  71. }while (i >= 0);
  72. if(i<0) //没有有效数据
  73. {
  74. memset(user_datas, 0, block_size);
  75. }
  76. }
  77. //如果擦除完了后读取到是掉电的标志位,就不能进行存储工作,因为可能24V没电了
  78. //这时候还原工作交给下次上电比较好
  79. if (hw_power_is_on())
  80. {
  81. //擦除旧块
  82. hw_flash_data_erase(POWEROFF_SAVE_ADDRESS);
  83. }
  84. next_save_posi = POWEROFF_SAVE_ADDRESS;
  85. }
  86. else
  87. {
  88. //读取存储的数据
  89. //读取有效数据
  90. if (date_NoEmpty==0)//第一块数据都为空
  91. {
  92. //检测是否第二块是空的, 不是就读出来
  93. if (hw_flash_data_check_empty(POWEROFF_BACKUP_ADDRESS, block_size) != 0)
  94. {
  95. for(retry=0;retry<3;retry++)
  96. {
  97. hw_flash_data_read(POWEROFF_BACKUP_ADDRESS, user_datas, block_size);
  98. //校验数据是否有效
  99. if (mh_crc16_calc((unsigned char *)&user_datas[0], block_size) == 0)
  100. {
  101. break;
  102. }
  103. }
  104. if(retry>=3)//没有有效数据
  105. {
  106. memset(user_datas, 0, block_size);
  107. next_save_posi = POWEROFF_SAVE_ADDRESS;
  108. }
  109. }
  110. else
  111. {
  112. //如果没有数据就清零
  113. memset(user_datas, 0, block_size);
  114. next_save_posi = POWEROFF_SAVE_ADDRESS;
  115. }
  116. }
  117. else
  118. {
  119. //读取第一块中的有效数据
  120. do
  121. {
  122. //必须保证CRC校验成功
  123. hw_flash_data_read(POWEROFF_SAVE_ADDRESS + (i)*block_size, user_datas, block_size);
  124. if (mh_crc16_calc((unsigned char *)&user_datas[0], block_size) == 0)
  125. {
  126. break;
  127. }
  128. i--;
  129. }while (i >= 0);
  130. if(i<0) //没有有效数据
  131. {
  132. //检测是否第二块是空的, 不是就读出来
  133. if (hw_flash_data_check_empty(POWEROFF_BACKUP_ADDRESS, block_size) != 0)
  134. {
  135. for(retry=0;retry<3;retry++)
  136. {
  137. hw_flash_data_read(POWEROFF_BACKUP_ADDRESS, user_datas, block_size);
  138. //校验数据是否有效
  139. if (mh_crc16_calc((unsigned char *)&user_datas[0], block_size) == 0)
  140. {
  141. break;
  142. }
  143. }
  144. if(retry>=3)//没有有效数据
  145. {
  146. memset(user_datas, 0, block_size);
  147. }
  148. }
  149. else
  150. {
  151. memset(user_datas, 0, block_size);
  152. }
  153. }
  154. //下次存储在数据为空的地址,避免掉电保存时还要进行擦除操作
  155. next_save_posi = POWEROFF_SAVE_ADDRESS + (NoEmpty_index+1) * block_size;
  156. }
  157. }
  158. //将该位置定义成一个记录保存块,它的有效内容大小为128 - 18
  159. //tiny_log_init(&tinylog, &user_datas[512],128);
  160. }
  161. /**
  162. * 保存参数
  163. *
  164. * @author lxz (2019/6/12/鍛ㄤ笁)
  165. *
  166. * @param void
  167. */
  168. void app_save_parameter(void)
  169. {
  170. hw_board_enter_powerless();
  171. hw_run_status_off();
  172. //保存一次记录
  173. log_app_save();
  174. //计算校验码
  175. user_datas[POWEROFF_SAVE_BLOCK_SIZE / 2 - 1] = mh_crc16_calc((unsigned char *)&user_datas[0], POWEROFF_SAVE_BLOCK_SIZE - 2);
  176. //写拉flash
  177. hw_flash_data_write(next_save_posi, user_datas, POWEROFF_SAVE_BLOCK_SIZE);
  178. hw_run_status_on();
  179. }
  180. long testDisplay;
  181. /**
  182. * 掉电的处理函数
  183. *
  184. * @author lxz (2019/6/12/鍛ㄤ笁)
  185. *
  186. * @param void
  187. */
  188. void poweroff_app_run(void)
  189. {
  190. //当第一次上电有上电标志的时间,才检测掉电保存
  191. if (power_on_flag)
  192. {
  193. //周期检测,管脚滤波
  194. if (sw_timer_expire(&timer))
  195. {
  196. if (hw_power_is_off())
  197. {
  198. power_off_sample++;
  199. } else if (power_off_sample > 0)
  200. {
  201. power_off_sample--;
  202. }
  203. sw_timer_start(&timer, 0, POWER_OFF_SAMPLE_TIME);
  204. }
  205. //确定检测到断电
  206. if (power_off_sample >= POWER_OFF_SAMPLE_COUNT)
  207. {
  208. //这里需要调用一次保存
  209. testDisplay++;
  210. //保存参数
  211. app_save_parameter();
  212. //hw_run_status_set(3);
  213. while (1)
  214. {
  215. hw_run_status_show();
  216. }
  217. }
  218. } else
  219. {
  220. //当在掉电状态的时候,程序在运行则一直在执行上电的检测,检测时间为连续的200ms
  221. if (sw_timer_expire(&timer))
  222. {
  223. if (hw_power_is_on())
  224. {
  225. power_off_sample++;
  226. } else if (power_off_sample > 0)
  227. {
  228. power_off_sample--;
  229. }
  230. sw_timer_start(&timer, 0, POWER_OFF_SAMPLE_TIME);
  231. }
  232. if (power_off_sample >= POWER_ON_SAMPLE_COUNT)
  233. {
  234. power_on_flag = hw_power_is_on();
  235. }
  236. }
  237. }