func_queue_record.c 10.0 KB

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