func_queue_record.c 9.9 KB

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