func_queue_record.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*********************************************************
  2. //file :hd_dev_gpio.c
  3. //author :libo
  4. //date :2020/05/10
  5. //version :V1.0
  6. //brief :GSP HARD层GPIO接口C文件
  7. *********************************************************/
  8. /* Includes-----------------------------------------------------------------------------------*/
  9. #include "main.h"
  10. #ifdef USE_QUEUE_RECORD
  11. /* Includes-----------------------------------------------------------------------------------*/
  12. #include <stdint.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <stdbool.h>
  16. #include "func_queue_record.h"
  17. #include "func_spi_w25qxx.h"
  18. #include "obj_hal_w25qxx.h"
  19. #include "obj_soft_w25qxx.h"
  20. #include "lib_ringfs_func.h"
  21. #if USE_RAM_RECORD==1
  22. #include "func_ram_record.h"
  23. #endif
  24. /* Private macro------------------------------------------------------------------------------*/
  25. /* Private typedef----------------------------------------------------------------------------*/
  26. record_queue_object lifo_queue_obj = {0};
  27. record_queue_object fifo_queue_obj = {0};
  28. /* Private define-----------------------------------------------------------------------------*/
  29. /* Private variables--------------------------------------------------------------------------*/
  30. /* Private function prototypes----------------------------------------------------------------*/
  31. queue_record_object queue_record_obj = {
  32. .link_ok = false,
  33. .flash_ok = false,
  34. .wr_cnt = 0,
  35. .rd_cnt = 0,
  36. .flash_pop = NULL,
  37. .flash_push= NULL,
  38. };
  39. void func_record_queue_test(void)
  40. {
  41. uint8_t data = 0;
  42. uint32_t *p_data = 0;
  43. UNUSED(data);
  44. //sprintf(queue_record_obj.buf,"XX%02xXX",queue_record_obj.running%0x100);
  45. switch(queue_record_obj.opt_flag)
  46. {
  47. case 0:
  48. {
  49. queue_record_obj.wr_idx++;
  50. //queue_record_obj.wr_idx = queue_record_obj.wr_cnt%0x100;
  51. //data = (uint8_t) queue_record_obj.wr_idx ;
  52. p_data = (uint32_t *)&queue_record_obj.buf[0];
  53. for(uint32_t i = 0; i < sizeof(queue_record_obj.buf)/4; i++ )
  54. {
  55. *p_data = queue_record_obj.wr_idx ;
  56. p_data++;
  57. }
  58. //memset((uint8_t *)queue_record_obj.buf, data ,sizeof(queue_record_obj.buf));
  59. func_record_queue_write((uint8_t *)queue_record_obj.buf ,sizeof(queue_record_obj.buf));
  60. }break;
  61. default:
  62. {
  63. func_record_queue_read((void *)queue_record_obj.buf ,sizeof(queue_record_obj.buf));
  64. }break;
  65. }
  66. }
  67. //Message_Queue队列初始化
  68. void func_record_queue_init(void)
  69. {
  70. taskENTER_CRITICAL(); //进入临界区
  71. lifo_queue_obj.hQueue = xQueueCreate(QUEUE_MSG_Q_NUM,QUEUE_MSG_ITEM_SIZE); //创建消息Message_Queue,队列项长度是串口接收缓冲区长度
  72. fifo_queue_obj.hQueue = xQueueCreate(QUEUE_MSG_Q_NUM,QUEUE_MSG_ITEM_SIZE); //创建消息Message_Queue,队列项长度是串口接收缓冲区长度
  73. taskEXIT_CRITICAL(); //退出临界区
  74. #if USE_RFS_RECORD==1
  75. queue_record_obj.flash_pop = lib_ringfs_pop;
  76. queue_record_obj.flash_push = lib_ringfs_push;
  77. #endif
  78. }
  79. //查询Queue队列中的总队列数量和剩余队列数量
  80. void func_record_queue_check(record_queue_object * queue_obj)
  81. {
  82. if(queue_obj->hQueue == NULL)
  83. {
  84. return;
  85. }
  86. QueueHandle_t xQueue = queue_obj->hQueue;
  87. taskENTER_CRITICAL(); //进入临界区
  88. queue_obj->remain_size = uxQueueSpacesAvailable(xQueue);//得到队列剩余大小
  89. queue_obj->used_size = uxQueueMessagesWaiting(xQueue);//得到队列剩余大小
  90. queue_obj->total_size=uxQueueMessagesWaiting(xQueue)+uxQueueSpacesAvailable(xQueue);//得到队列总大小,总大小=使用+剩余的。
  91. taskEXIT_CRITICAL(); //退出临界区
  92. }
  93. void func_record_queue_empty(record_queue_object * queue_obj)
  94. {
  95. if(queue_obj->hQueue == NULL)
  96. {
  97. return;
  98. }
  99. taskENTER_CRITICAL(); //进入临界区
  100. #if 0
  101. int32_t num_cnt = QUEUE_MSG_Q_NUM;
  102. BaseType_t err = pdPASS;
  103. do
  104. {
  105. num_cnt--;
  106. if(num_cnt<=0)
  107. {
  108. break;
  109. }
  110. err = xQueueReceive(queue_obj->hQueue, (uint8_t *)queue_record_obj.buf, 0);
  111. }while(err == pdPASS);
  112. #endif
  113. xQueueReset(queue_obj->hQueue);
  114. taskEXIT_CRITICAL(); //退出临界区
  115. }
  116. void func_record_queue_update(void)
  117. {
  118. func_record_queue_check(&lifo_queue_obj);
  119. func_record_queue_check(&fifo_queue_obj);
  120. if(fifo_queue_obj.remain_size == 0) //如果缓冲队列无空闲测请理缓冲区;
  121. {
  122. func_record_queue_empty(&fifo_queue_obj);
  123. }
  124. if(lifo_queue_obj.remain_size == 0)
  125. {
  126. func_record_queue_empty(&lifo_queue_obj);
  127. }
  128. return;
  129. }
  130. //将产生的数据写入内存缓冲区队列
  131. uint32_t func_record_queue_write(void *buf ,uint32_t len)
  132. {
  133. BaseType_t err;
  134. //有数据写入队列,则说明当前连接断开
  135. func_record_queue_link_set(false);
  136. if((lifo_queue_obj.hQueue == NULL)||(fifo_queue_obj.hQueue == NULL))
  137. {
  138. return 0;
  139. }
  140. if(lifo_queue_obj.used_size == 0)
  141. {
  142. err=xQueueSend(fifo_queue_obj.hQueue,(uint8_t *)buf,10);
  143. }
  144. else
  145. {
  146. err=xQueueSendToFront(lifo_queue_obj.hQueue,(uint8_t *)buf,10);
  147. }
  148. //写入个数累加
  149. queue_record_obj.wr_cnt++;
  150. func_record_queue_update();
  151. if(err != pdPASS)
  152. {
  153. return 0;
  154. }
  155. return len;
  156. }
  157. void func_record_queue_cpy(record_queue_object * des, record_queue_object * src)
  158. {
  159. BaseType_t xResult = pdPASS;
  160. do{
  161. xResult = xQueueReceive(src->hQueue, (uint8_t *)queue_record_obj.buf, 0);
  162. if(xResult == pdPASS)
  163. {
  164. xResult = xQueueSendToFront(des->hQueue, (uint8_t *)queue_record_obj.buf, 0);
  165. }
  166. }while(xResult == pdPASS);
  167. func_record_queue_update();
  168. return;
  169. }
  170. //从队列中取出数据,发送;
  171. uint32_t func_record_queue_read(void *buf ,uint32_t len)
  172. {
  173. //从队列读取,则说明当前连接正常
  174. func_record_queue_link_set(true);
  175. BaseType_t xResult = pdPASS;
  176. if((lifo_queue_obj.hQueue == NULL)||(fifo_queue_obj.hQueue == NULL))
  177. {
  178. return 0;
  179. }
  180. if(lifo_queue_obj.used_size == 0)
  181. {
  182. if(fifo_queue_obj.used_size > 0)
  183. {
  184. func_record_queue_cpy(&lifo_queue_obj, &fifo_queue_obj);
  185. }
  186. }
  187. xResult = xQueueReceive(lifo_queue_obj.hQueue, (uint8_t *)buf, 1);
  188. //读取个数累加
  189. queue_record_obj.rd_cnt++;
  190. func_record_queue_update();
  191. if(xResult != pdPASS)
  192. {
  193. return 0;
  194. }
  195. return len;
  196. }
  197. //将缓存队列中的数据写入文件
  198. void func_record_queue_to_flash(record_queue_object * queue_obj)
  199. {
  200. BaseType_t xResult = pdPASS;
  201. do{
  202. xResult = xQueueReceive(queue_obj->hQueue,queue_record_obj.buf,1);
  203. if(xResult == pdPASS)
  204. {
  205. if(queue_record_obj.flash_push != 0)
  206. {
  207. queue_record_obj.flash_push((uint8_t *)queue_record_obj.buf ,QUEUE_MSG_ITEM_SIZE);
  208. }
  209. }
  210. }while(xResult == pdPASS);
  211. return;
  212. }
  213. void func_record_queue_link_set(uint8_t stat)
  214. {
  215. queue_record_obj.link_ok = stat;
  216. return ;
  217. }
  218. uint8_t func_record_queue_link_get(void)
  219. {
  220. return queue_record_obj.link_ok;
  221. }
  222. uint8_t func_record_queue_flash_set(uint8_t stat)
  223. {
  224. queue_record_obj.flash_ok = stat;
  225. return queue_record_obj.flash_ok;
  226. }
  227. uint8_t func_record_queue_flash_get(void)
  228. {
  229. //queue_record_obj.flash_ok = func_w25q_stat();
  230. return queue_record_obj.flash_ok;
  231. }
  232. uint32_t func_record_queue_obj_cnt(void)
  233. {
  234. uint32_t cnt_estimate = 0;
  235. if(queue_record_obj.wr_cnt > queue_record_obj.rd_cnt)
  236. {
  237. queue_record_obj.msg_cnt = queue_record_obj.wr_cnt - queue_record_obj.rd_cnt;
  238. }
  239. else
  240. {
  241. queue_record_obj.wr_cnt = 0;
  242. queue_record_obj.rd_cnt = 0;
  243. queue_record_obj.msg_cnt = 0;
  244. }
  245. //修正估算值
  246. cnt_estimate = lib_ringfs_obj_cnt_estimate();
  247. if(queue_record_obj.msg_cnt > cnt_estimate)
  248. {
  249. queue_record_obj.msg_cnt = cnt_estimate;
  250. }
  251. return queue_record_obj.msg_cnt ;
  252. }
  253. void func_record_queue_work(void)
  254. {
  255. BaseType_t xResult;
  256. //更新队列状态
  257. func_record_queue_update();
  258. if(func_record_queue_link_get() == true)
  259. {
  260. //如果连接正常
  261. if((lifo_queue_obj.used_size == 0)&&(fifo_queue_obj.used_size == 0))
  262. {
  263. if(queue_record_obj.flash_pop != NULL)
  264. {
  265. queue_record_obj.buf_len = queue_record_obj.flash_pop((uint8_t *)queue_record_obj.buf ,QUEUE_MSG_ITEM_SIZE);
  266. }
  267. if(queue_record_obj.buf_len == QUEUE_MSG_ITEM_SIZE)
  268. {
  269. xResult = xQueueSend(lifo_queue_obj.hQueue,queue_record_obj.buf,10);
  270. }
  271. }
  272. }
  273. else
  274. {
  275. //如果连接断开
  276. if(lifo_queue_obj.used_size >= (lifo_queue_obj.total_size/2))
  277. {
  278. if(fifo_queue_obj.used_size > 0)
  279. {
  280. func_record_queue_to_flash(&fifo_queue_obj);
  281. func_record_queue_check(&fifo_queue_obj);
  282. }
  283. if(fifo_queue_obj.used_size == 0)
  284. {
  285. func_record_queue_cpy(&fifo_queue_obj, &lifo_queue_obj);
  286. }
  287. }
  288. if(fifo_queue_obj.used_size >= (fifo_queue_obj.total_size/2))
  289. {
  290. func_record_queue_to_flash(&fifo_queue_obj);
  291. }
  292. }
  293. func_record_queue_update();
  294. UNUSED(xResult);
  295. return;
  296. }
  297. void func_record_queue_main(void const *argument)
  298. {
  299. func_record_queue_init();
  300. func_record_queue_update();
  301. #if USE_RAM_RECORD==1
  302. func_ram_record_init();
  303. #endif
  304. #if USE_RFS_RECORD==1
  305. func_w25q_init();
  306. if((func_w25q_stat() == false) //flash ID 异常
  307. ||(func_w25q_bootcnt() == false)) //启动次数异常;
  308. {
  309. //设置flash状态为异常
  310. func_record_queue_flash_set(false);
  311. while(1)
  312. {
  313. osDelay(10000);
  314. }
  315. }
  316. //设置flash状态为正常
  317. func_record_queue_flash_set(true);
  318. lib_ringfs_init(QUEUE_MSG_ITEM_SIZE);
  319. queue_record_obj.wr_cnt = lib_ringfs_obj_cnt_exact();
  320. #endif
  321. while(1)
  322. {
  323. queue_record_obj.running++;
  324. if(queue_record_obj.running%(60) == 0)
  325. {
  326. if(func_w25q_stat() == false) //读取FLASH ID,如果ID异常,则说明FLASH状态异常
  327. {
  328. //设置flash状态为异常
  329. func_record_queue_flash_set(false);
  330. }
  331. }
  332. if(queue_record_obj.flash_ok == true) //flash正常,且处于读取状态时进行数量计算;
  333. {
  334. func_record_queue_obj_cnt();
  335. func_record_queue_work();
  336. osDelay(1000);
  337. }
  338. else
  339. {
  340. osDelay(10000);
  341. }
  342. if(strcmp(queue_record_obj.cmd,"erase") == 0){
  343. W25QXX_Erase_Chip();
  344. Reboot_System();
  345. }
  346. }
  347. }
  348. #endif /*************USE_QUEUE_RECORD*******************/
  349. //------------------------the end of file-------------------------//