func_queue_record.c 9.8 KB

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