flash.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. #include <flashdb.h>
  2. #include <rtthread.h>
  3. #include "plan.h"
  4. #include "buffer.h"
  5. #include "button.h"
  6. #include "display.h"
  7. #include <string.h>
  8. uint8_t isCC = 0 ;
  9. const uint8_t isTag = 0 ; //是否含识读标签
  10. unsigned char isn = 1; //是否新大屏
  11. unsigned char remote_sumweight = 0; //远程是否包含重量
  12. unsigned char iscowweight = 0; //是否奶牛秤
  13. unsigned char eid[10]; //电子耳标,0为长度
  14. uint8_t XbeeConst_DeviceAddress = 0x01;
  15. uint8_t XbeeConst_NetAddress = 0x00;
  16. uint8_t XbeeConst_UseAPI = 0x01;
  17. uint8_t WeightConst_TQ = 0x07;//0x05;
  18. uint8_t WeightConst_BLE = 0;
  19. uint8_t WeightConst_ChanCheShu = 0x02;
  20. uint8_t WeightConst_SBType = 0x04;
  21. int16_t WeightConst_WeightBegin = 0; //按键开始重量
  22. uint8_t ad_Percent= 0x00; // 7, 校正重量,1字节
  23. unsigned int ad_Maxval= 0x00007530; // 8, 最大称量,4字节
  24. uint8_t ad_FS= 0x04; // 12, 滤波深度,1字节
  25. uint8_t ad_Point= 0x00; // 13, 小数点, 1字节
  26. uint8_t ad_Inteval= 0x02; // 14, 分度值, 1字节
  27. float ad_Spanz = 0.9; // 15, 分度内码数, 4字节
  28. unsigned int ad_Zero = 0x00000000; // 19, 真零内码, 4字节
  29. unsigned int ad_Tare = 0x00002320; // 23, 皮零内码, 4字节
  30. static uint32_t read_key = 1;
  31. static uint32_t read_plan = 1;
  32. static uint32_t read_weight = 1;
  33. static struct fdb_default_kv_node default_kv_table[] = {
  34. {"CH", &XbeeConst_DeviceAddress, sizeof(XbeeConst_DeviceAddress)}, // 1, 车号, 1字节
  35. {"TQ", &WeightConst_TQ, sizeof(WeightConst_TQ)}, // 2, 提取方式,1字节
  36. {"PM", &WeightConst_SBType, sizeof(WeightConst_SBType)}, // 3, 屏幕类型,1字节 6、海安 7长屏铲车 8 新彩屏
  37. {"WL", &XbeeConst_NetAddress, sizeof(XbeeConst_NetAddress)}, // 4, 网络地址,1字节
  38. {"API", &XbeeConst_UseAPI, sizeof(XbeeConst_UseAPI)}, // 5, 使用API,1字节
  39. {"CC", &WeightConst_ChanCheShu, sizeof(WeightConst_ChanCheShu)}, // 6, TMR数量,1字节
  40. {"BLE", &WeightConst_BLE, sizeof(WeightConst_BLE)}, // 27, 皮零内码, 4字节
  41. ///////////////////////////////////////////////////////////////////////////////////////
  42. {"ad_Percent", &ad_Percent, sizeof(ad_Percent)}, // 7, 校正重量,1字节
  43. {"ad_Maxval", &ad_Maxval, sizeof(ad_Maxval)}, // 8, 最大称量,4字节
  44. {"ad_FS", &ad_FS, sizeof(ad_FS)}, // 12, 滤波深度,1字节
  45. {"ad_Point", &ad_Point, sizeof(ad_Point)}, // 13, 小数点, 1字节
  46. {"ad_Inteval", &ad_Inteval, sizeof(ad_Inteval)}, // 14, 分度值, 1字节
  47. {"ad_Spanz", &ad_Spanz, sizeof(ad_Spanz)}, // 15, 分度内码数, 4字节
  48. {"ad_Zero", &ad_Zero, sizeof(ad_Zero)}, // 19, 真零内码, 4字节
  49. {"ad_Tare", &ad_Tare, sizeof(ad_Tare)}, // 23, 皮零内码, 4字节
  50. {"read_key", &read_key, sizeof(read_key)},
  51. {"read_plan", &read_plan, sizeof(read_plan)},
  52. {"read_weight", &read_weight, sizeof(read_weight)},
  53. {"beginweight", &WeightConst_WeightBegin, sizeof(WeightConst_WeightBegin)},
  54. {"begintime", &TimeConst, sizeof(TimeConst)},
  55. {"completeorder", &CompleteTarget_order, sizeof(CompleteTarget_order)},
  56. {"TimeConst", &TimeConst, sizeof(TimeConst)},
  57. };
  58. #define FDB_LOG_TAG "[main]"
  59. struct fdb_tsdb ts_keydb = { 0 };
  60. struct fdb_tsdb ts_plandb = { 0 };
  61. struct fdb_tsdb ts_weightdb = { 0 };
  62. struct fdb_kvdb kvdb = { 0 };
  63. static uint32_t ts_keyTime = 0;
  64. static uint32_t ts_planTime = 0;
  65. static uint32_t ts_weightTime = 0;
  66. rt_sem_t kv_db_lock;
  67. static void lock(fdb_db_t db)
  68. {
  69. rt_sem_take(kv_db_lock, RT_WAITING_FOREVER);
  70. __disable_irq();
  71. }
  72. static void unlock(fdb_db_t db)
  73. {
  74. rt_sem_release(kv_db_lock);
  75. __enable_irq();
  76. }
  77. static fdb_time_t get_keydbtime(void)
  78. {
  79. return ++ts_keyTime;
  80. }
  81. void write_Flash(const char *key, const void *value_buf, size_t buf_len)
  82. {
  83. struct fdb_blob blob;
  84. fdb_kv_set_blob(&kvdb, key, fdb_blob_make(&blob, (void *)value_buf, buf_len));
  85. }
  86. void reset_read_ts(const char key)
  87. {
  88. if (key == 'k'){
  89. read_key = ts_keyTime+1;
  90. write_Flash("read_key", &read_key, sizeof(read_key));
  91. }else if (key == 'p'){
  92. read_plan = ts_planTime+1;
  93. write_Flash("read_plan", &read_plan, sizeof(read_plan));
  94. // rt_kprintf("read_plan:%02X \n",read_plan);
  95. }else {
  96. read_weight = ts_weightTime+1;
  97. write_Flash("read_weight", &read_weight, sizeof(read_weight));
  98. };
  99. }
  100. void init_allflash(void)
  101. {
  102. // fdb_kv_set_default(&kvdb);
  103. fdb_tsl_clean(&ts_weightdb);
  104. fdb_tsl_clean(&ts_plandb);
  105. fdb_tsl_clean(&ts_keydb);
  106. ts_keyTime=0;
  107. ts_planTime=0;
  108. ts_weightTime=0;
  109. reset_read_ts('k');
  110. reset_read_ts('p');
  111. reset_read_ts('w');
  112. }
  113. static fdb_time_t get_plandbtime(void)
  114. {
  115. return ++ts_planTime;
  116. }
  117. static fdb_time_t get_weightdbtime(void)
  118. {
  119. return ++ts_weightTime;
  120. }
  121. uint32_t tsdb_recordcount(const char key)
  122. {
  123. if (key== 'k'){
  124. if (read_key > ts_keyTime || ts_keyTime == 0 ) return 0;
  125. else
  126. return ts_keyTime-read_key+1;
  127. }
  128. else if (key== 'p'){
  129. if (read_plan > ts_planTime || ts_planTime == 0 ) return 0;
  130. else
  131. return ts_planTime-read_plan+1;
  132. }
  133. else if (read_weight > ts_weightTime || ts_weightTime == 0 ) return 0;
  134. else
  135. return ts_weightTime-read_weight+1;
  136. }
  137. static uint8_t foundF8 = 0;
  138. static bool query_f8_cb(fdb_tsl_t tsl, void *arg)
  139. {
  140. struct fdb_blob blob;
  141. unsigned char buf[tsl->log_len];
  142. fdb_tsdb_t db = arg;
  143. rt_memset(buf, 0, tsl->log_len);
  144. fdb_blob_read((fdb_db_t) db, fdb_tsl_to_blob(tsl, fdb_blob_make(&blob, &buf, tsl->log_len)));
  145. if ((buf[3]==0xf8 || buf[3]==0xE8) && tsl->status==FDB_TSL_WRITE)
  146. {
  147. foundF8 = 1;
  148. return true;
  149. }
  150. return false;
  151. //write_Flash("read_key", &ts_keyTime, sizeof(ts_keyTime));
  152. }
  153. void findF8(void)
  154. {
  155. fdb_tsl_iter_by_time(&ts_keydb, read_key, 0xFFFFFFFF, query_f8_cb, &ts_keydb);
  156. }
  157. static uint8_t foundF4 = 0;
  158. static bool query_f4_cb(fdb_tsl_t tsl, void *arg)
  159. {
  160. struct fdb_blob blob;
  161. unsigned char buf[tsl->log_len];
  162. fdb_tsdb_t db = arg;
  163. rt_memset(buf, 0, tsl->log_len);
  164. fdb_blob_read((fdb_db_t) db, fdb_tsl_to_blob(tsl, fdb_blob_make(&blob, &buf, tsl->log_len)));
  165. if ((buf[3]==0xf4 || buf[3]==0xE4) && tsl->status==FDB_TSL_WRITE)
  166. {
  167. foundF4 = 1;
  168. return true;
  169. }
  170. return false;
  171. //write_Flash("read_key", &ts_keyTime, sizeof(ts_keyTime));
  172. }
  173. void findF4(void)
  174. {
  175. fdb_tsl_iter_by_time(&ts_keydb, read_key, 0xFFFFFFFF, query_f4_cb, &ts_keydb);
  176. }
  177. static bool query_f12_cb(fdb_tsl_t tsl, void *arg)
  178. {
  179. struct fdb_blob blob;
  180. unsigned char buf[tsl->log_len];
  181. unsigned char len = 0;
  182. fdb_tsdb_t db = arg;
  183. if (tsl->log_len>40) len=40;
  184. else len=tsl->log_len;
  185. rt_memset(WeightConst_PlantempList, 0x20, tsl->log_len);
  186. fdb_blob_read((fdb_db_t) db, fdb_tsl_to_blob(tsl, fdb_blob_make(&blob, &buf, tsl->log_len)));
  187. rt_memcpy(WeightConst_PlantempList,buf, len);
  188. if (buf[3]==0x12 && tsl->status==FDB_TSL_WRITE)
  189. {
  190. WeightConst_Run = 1;
  191. rt_memset(WeightConst_product,0x20,40);
  192. rt_memset(WeightConst_feed,0x20,40);
  193. for (int i = 0; i <= buf[1]-10; i++) {
  194. WeightConst_product[i] = buf[i+4]; //产品名称
  195. if (WeightConst_SBType==1||WeightConst_SBType==6)
  196. WeightConst_feed[i] = WeightConst_product[i]; //显示一秒牛舍
  197. }
  198. plan_comp.Buffer[0] = buf[buf[1]-5+1]; //产品名称
  199. plan_comp.Buffer[1] = buf[buf[1]-5]; //产品名称
  200. for (int i = 2; i < 6; i++)
  201. plan_comp.Buffer[i] = buf[buf[1]-5+i]; //产品名称
  202. }
  203. return false;
  204. }
  205. void findF12(void)
  206. {
  207. uint8_t i = 0;
  208. if (read_plan>50) i = read_plan-50;
  209. fdb_tsl_iter_by_time(&ts_plandb, i, read_plan, query_f12_cb, &ts_plandb);
  210. }
  211. static uint32_t foundkey = 0;
  212. static bool query_key_cb(fdb_tsl_t tsl, void *arg)
  213. {
  214. struct fdb_blob blob;
  215. //unsigned char buf[tsl->log_len];
  216. fdb_tsdb_t db = arg;
  217. rt_memset(XbeeConst_FTxBuf0, 0, tsl->log_len);
  218. fdb_blob_read((fdb_db_t) db, fdb_tsl_to_blob(tsl, fdb_blob_make(&blob, &XbeeConst_FTxBuf0, tsl->log_len)));
  219. for (int i=tsl->log_len; i>0; i--)
  220. {
  221. XbeeConst_FTxBuf0[i] = XbeeConst_FTxBuf0[i-1];
  222. }
  223. XbeeConst_FTxBuf0[0] = tsl->log_len;
  224. if (tsl->status==FDB_TSL_WRITE)
  225. {
  226. foundkey = tsl->time;
  227. read_key = foundkey;
  228. return true;
  229. }
  230. return false;
  231. }
  232. uint32_t findkey(void)
  233. {
  234. foundkey = 0xFFFFFFFF;
  235. fdb_tsl_iter_by_time(&ts_keydb, read_key, 0xFFFFFFFF, query_key_cb, &ts_keydb);
  236. return foundkey;
  237. //return 0xFFFFFFFF;
  238. }
  239. void save_read_ts(const char key)
  240. {
  241. if (tsdb_recordcount(key)>0)
  242. {
  243. if (key == 'k'){
  244. if (findkey()<0xFFFFFFFF){
  245. if(XbeeConst_FrameNumLast == XbeeConst_FTxBuf0[XbeeConst_FTxBuf0[0]-1]){
  246. read_key++;
  247. write_Flash("read_key", &read_key, sizeof(read_key));
  248. }
  249. }
  250. }else if (key == 'p'){
  251. read_plan++;
  252. write_Flash("read_plan", &read_plan, sizeof(read_plan));
  253. // rt_kprintf("read_plan save_read_t:%02X \n",read_plan);
  254. }else {
  255. read_weight++;
  256. write_Flash("read_weight", &read_weight, sizeof(read_weight));
  257. };
  258. }
  259. }
  260. static uint32_t foundplan = 0;
  261. static bool query_plan_cb(fdb_tsl_t tsl, void *arg)
  262. {
  263. struct fdb_blob blob;
  264. //unsigned char buf[tsl->log_len];
  265. fdb_tsdb_t db = arg;
  266. rt_memset(WeightConst_PlantempList, 0, tsl->log_len);
  267. fdb_blob_read((fdb_db_t) db, fdb_tsl_to_blob(tsl, fdb_blob_make(&blob, &WeightConst_PlantempList, tsl->log_len)));
  268. if (tsl->status==FDB_TSL_WRITE)
  269. {
  270. foundplan = tsl->time;
  271. read_plan = tsl->time;
  272. return true;
  273. }
  274. return false;
  275. }
  276. uint32_t findplan(void)
  277. {
  278. foundplan = 0;
  279. // rt_kprintf("read_plan findplan:%02X \n",read_plan);
  280. fdb_tsl_iter_by_time(&ts_plandb, read_plan, 0xFFFFFFFF, query_plan_cb, &ts_plandb);
  281. return foundplan;
  282. }
  283. static uint8_t foundlastplan = 0;
  284. static bool query_lastplan_cb(fdb_tsl_t tsl, void *arg)
  285. {
  286. struct fdb_blob blob;
  287. //unsigned char buf[tsl->log_len];
  288. fdb_tsdb_t db = arg;
  289. rt_memset(WeightConst_PlantempList, 0, tsl->log_len);
  290. fdb_blob_read((fdb_db_t) db, fdb_tsl_to_blob(tsl, fdb_blob_make(&blob, &WeightConst_PlantempList, tsl->log_len)));
  291. if (tsl->status==FDB_TSL_WRITE)
  292. {
  293. if (WeightConst_PlantempList[3]==0x15)
  294. foundlastplan = WeightConst_PlantempList[WeightConst_PlantempList[1]-1];
  295. return true;
  296. }
  297. return false;
  298. }
  299. uint32_t findlastplan(void)
  300. {
  301. foundlastplan = 0xF0;
  302. // rt_kprintf("read_plan findplan:%02X \n",read_plan);
  303. fdb_tsl_iter_by_time(&ts_plandb, ts_planTime, 0xFFFFFFFF, query_lastplan_cb, &ts_plandb);
  304. return foundlastplan;
  305. }
  306. static uint32_t foundNextplan = 0;
  307. static bool query_Nextplan_cb(fdb_tsl_t tsl, void *arg)
  308. {
  309. struct fdb_blob blob;
  310. fdb_tsdb_t db = arg;
  311. uint8_t Nextfeedlength = 0;
  312. rt_memset(WeightConst_Nextfeed, 0x20, tsl->log_len);
  313. fdb_blob_read((fdb_db_t) db, fdb_tsl_to_blob(tsl, fdb_blob_make(&blob, &WeightConst_Nextfeed, tsl->log_len)));
  314. if (tsl->status==FDB_TSL_WRITE)
  315. {
  316. foundNextplan = tsl->time;
  317. if (WeightConst_Nextfeed[3]==0x15) {
  318. Nextfeedlength = WeightConst_Nextfeed[1];
  319. WeightConst_NextWeightTarget = WeightConst_Nextfeed[Nextfeedlength-6]<<8 ; //目标总量
  320. WeightConst_NextWeightTarget = WeightConst_NextWeightTarget | WeightConst_Nextfeed[Nextfeedlength-5];
  321. for (int i = 0; i <= Nextfeedlength-11; i++)
  322. WeightConst_Nextfeed[i] = WeightConst_Nextfeed[i+4];
  323. for (int i = Nextfeedlength-11; i <= tsl->log_len; i++)
  324. WeightConst_Nextfeed[i] = 0x20;
  325. }
  326. return true;
  327. }
  328. return false;
  329. }
  330. uint32_t findNextplan(void)
  331. {
  332. foundNextplan = 0;
  333. fdb_tsl_iter_by_time(&ts_plandb, read_plan+1, 0xFFFFFFFF, query_Nextplan_cb, &ts_plandb);
  334. return foundNextplan;
  335. }
  336. static uint32_t foundweight = 0;
  337. static uint32_t read_weight_temp = 0;
  338. static bool query_weight_cb(fdb_tsl_t tsl, void *arg)
  339. {
  340. struct fdb_blob blob;
  341. unsigned char buf[7];
  342. fdb_tsdb_t db = arg;
  343. fdb_blob_read((fdb_db_t) db, fdb_tsl_to_blob(tsl, fdb_blob_make(&blob, &buf, 7)));
  344. for (int i=7; i>0; i--)
  345. XbeeConst_FTxBuf0[4+foundweight*7+i] = buf[7-i];
  346. read_weight_temp = tsl->time;
  347. foundweight++;
  348. return foundweight>3;
  349. }
  350. uint32_t findweight(void)
  351. {
  352. unsigned char CRCNum=0;
  353. foundweight = 0;
  354. read_weight_temp = read_weight;
  355. rt_memset(XbeeConst_FTxBuf0, 0, 0x22);
  356. fdb_tsl_iter_by_time(&ts_weightdb, read_weight, 0xFFFFFFFF, query_weight_cb, &ts_weightdb);
  357. if (foundweight>3){
  358. XbeeConst_FTxBuf0[0] = 0x22;
  359. XbeeConst_FTxBuf0[1] = 0x7E;
  360. XbeeConst_FTxBuf0[2] = 0x20;
  361. XbeeConst_FTxBuf0[3] = XbeeConst_DeviceAddress;
  362. XbeeConst_FTxBuf0[4] = 0xF0;
  363. XbeeConst_FTxBuf0[33] = XbeeConst_FrameNum ++;
  364. for (int i=1; i< 34; i++) CRCNum+=XbeeConst_FTxBuf0[i];
  365. XbeeConst_FTxBuf0[34] = 0xFF - CRCNum;
  366. read_weight = read_weight_temp;
  367. }
  368. return foundweight;
  369. }
  370. uint8_t ts_pushArrary(fdb_tsdb_t db, uint8_t *buf, uint8_t length)
  371. {
  372. struct fdb_blob blob;
  373. fdb_err_t result = FDB_NO_ERR;
  374. foundF8 = 0;
  375. foundF4 = 0;
  376. if (strcmp(((fdb_db_t)db)->name, "key") == 0 && (buf[3] == 0xF8 || buf[3] == 0xE8))
  377. findF8();
  378. if (strcmp(((fdb_db_t)db)->name, "key") == 0 && (buf[3] == 0xF4 || buf[3] == 0xE4))
  379. findF4();
  380. if (foundF8==0 && foundF4==0){
  381. result = fdb_tsl_append(db, fdb_blob_make(&blob, buf, length));
  382. if (result != FDB_NO_ERR)
  383. {
  384. // rt_kprintf("ts_pushArrary:%02X \n", ts_planTime);
  385. return 0;
  386. }
  387. }
  388. return 1;
  389. }
  390. void read_Flash(void)
  391. {
  392. struct fdb_blob blob;
  393. fdb_kv_get_blob(&kvdb, "CH", fdb_blob_make(&blob, &XbeeConst_DeviceAddress, sizeof(XbeeConst_DeviceAddress)));
  394. fdb_kv_get_blob(&kvdb, "TQ", fdb_blob_make(&blob, &WeightConst_TQ, sizeof(WeightConst_TQ)));
  395. fdb_kv_get_blob(&kvdb, "PM", fdb_blob_make(&blob, &WeightConst_SBType, sizeof(WeightConst_SBType)));
  396. fdb_kv_get_blob(&kvdb, "WL", fdb_blob_make(&blob, &XbeeConst_NetAddress, sizeof(XbeeConst_NetAddress)));
  397. fdb_kv_get_blob(&kvdb, "API", fdb_blob_make(&blob, &XbeeConst_UseAPI, sizeof(XbeeConst_UseAPI)));
  398. fdb_kv_get_blob(&kvdb, "CC", fdb_blob_make(&blob, &WeightConst_ChanCheShu, sizeof(WeightConst_ChanCheShu)));
  399. fdb_kv_get_blob(&kvdb, "BLE", fdb_blob_make(&blob, &WeightConst_BLE, sizeof(WeightConst_BLE)));
  400. if (WeightConst_BLE>1) isCC=1;
  401. fdb_kv_get_blob(&kvdb, "ad_Percent", fdb_blob_make(&blob, &ad_Percent, sizeof(ad_Percent)));
  402. fdb_kv_get_blob(&kvdb, "ad_Maxval", fdb_blob_make(&blob, &ad_Maxval, sizeof(ad_Maxval)));
  403. fdb_kv_get_blob(&kvdb, "ad_FS", fdb_blob_make(&blob, &ad_FS, sizeof(ad_FS)));
  404. fdb_kv_get_blob(&kvdb, "ad_Point", fdb_blob_make(&blob, &ad_Point, sizeof(ad_Point)));
  405. fdb_kv_get_blob(&kvdb, "ad_Inteval", fdb_blob_make(&blob, &ad_Inteval, sizeof(ad_Inteval)));
  406. fdb_kv_get_blob(&kvdb, "ad_Spanz", fdb_blob_make(&blob, &ad_Spanz, sizeof(ad_Spanz)));
  407. fdb_kv_get_blob(&kvdb, "ad_Zero", fdb_blob_make(&blob, &ad_Zero, sizeof(ad_Zero)));
  408. fdb_kv_get_blob(&kvdb, "ad_Tare", fdb_blob_make(&blob, &ad_Tare, sizeof(ad_Tare)));
  409. fdb_kv_get_blob(&kvdb, "read_key", fdb_blob_make(&blob, &read_key, sizeof(read_key)));
  410. fdb_kv_get_blob(&kvdb, "read_plan", fdb_blob_make(&blob, &read_plan, sizeof(read_plan)));
  411. fdb_kv_get_blob(&kvdb, "read_weight", fdb_blob_make(&blob, &read_weight, sizeof(read_weight)));
  412. fdb_kv_get_blob(&kvdb, "beginweight", fdb_blob_make(&blob, &WeightConst_WeightBegin, sizeof(WeightConst_WeightBegin)));
  413. fdb_kv_get_blob(&kvdb, "begintime", fdb_blob_make(&blob, &TimeConst, sizeof(TimeConst)));
  414. fdb_kv_get_blob(&kvdb, "completeorder", fdb_blob_make(&blob, &CompleteTarget_order, sizeof(CompleteTarget_order)));
  415. fdb_kv_get_blob(&kvdb, "TimeConst", fdb_blob_make(&blob, &TimeConst, sizeof(TimeConst)));
  416. }
  417. void onbordflash_init(void)
  418. {
  419. fdb_err_t result;
  420. struct fdb_default_kv default_kv;
  421. kv_db_lock = rt_sem_create("kv_db_lock", 1, RT_IPC_FLAG_FIFO);
  422. default_kv.kvs = default_kv_table;
  423. default_kv.num = sizeof(default_kv_table) / sizeof(default_kv_table[0]);
  424. fdb_kvdb_control(&kvdb, FDB_KVDB_CTRL_SET_LOCK, lock);
  425. fdb_kvdb_control(&kvdb, FDB_KVDB_CTRL_SET_UNLOCK, unlock);
  426. result = fdb_kvdb_init(&kvdb, "env", "kv", &default_kv, NULL);
  427. if (result != FDB_NO_ERR) {
  428. return ;
  429. }
  430. /////////////
  431. fdb_tsdb_control(&ts_keydb, FDB_TSDB_CTRL_SET_LOCK, lock);
  432. fdb_tsdb_control(&ts_keydb, FDB_TSDB_CTRL_SET_UNLOCK, unlock);
  433. result = fdb_tsdb_init(&ts_keydb, "key", "ts_key", get_keydbtime, 48, NULL);
  434. if (result != FDB_NO_ERR) {
  435. return ;
  436. }
  437. /* read last saved time for simulated timestamp */
  438. fdb_tsdb_control(&ts_keydb, FDB_TSDB_CTRL_GET_LAST_TIME, &ts_keyTime);
  439. /////////////////
  440. /////////////
  441. fdb_tsdb_control(&ts_plandb, FDB_TSDB_CTRL_SET_LOCK, lock);
  442. fdb_tsdb_control(&ts_plandb, FDB_TSDB_CTRL_SET_UNLOCK, unlock);
  443. result = fdb_tsdb_init(&ts_plandb, "plan", "ts_plan", get_plandbtime, 48, NULL);
  444. if (result != FDB_NO_ERR) {
  445. return ;
  446. }
  447. /* read last saved time for simulated timestamp */
  448. fdb_tsdb_control(&ts_plandb, FDB_TSDB_CTRL_GET_LAST_TIME, &ts_planTime);
  449. /////////////////
  450. /////////////
  451. fdb_tsdb_control(&ts_weightdb, FDB_TSDB_CTRL_SET_LOCK, lock);
  452. fdb_tsdb_control(&ts_weightdb, FDB_TSDB_CTRL_SET_UNLOCK, unlock);
  453. result = fdb_tsdb_init(&ts_weightdb, "weight", "ts_weight", get_weightdbtime, 7, NULL);
  454. if (result != FDB_NO_ERR) {
  455. return ;
  456. }
  457. /* read last saved time for simulated timestamp */
  458. fdb_tsdb_control(&ts_weightdb, FDB_TSDB_CTRL_GET_LAST_TIME, &ts_weightTime);
  459. /////////////////
  460. read_Flash();
  461. }
  462. void saveApi(rt_sem_t lock_sem,UART_HandleTypeDef *huart, uint8_t *pData)
  463. {
  464. //保存命令
  465. pData[0] = 0x41;
  466. pData[1] = 0x54;
  467. pData[2] = 0x57;
  468. pData[3] = 0x52;
  469. pData[4] = 0x0D;
  470. HAL_UART_Transmit_DMA(huart, pData, 5);
  471. if (rt_sem_take(lock_sem, 2000) == RT_EOK) {
  472. //退出命令
  473. pData[0] = 0x41;
  474. pData[1] = 0x54;
  475. pData[2] = 0x43;
  476. pData[3] = 0x4E;
  477. pData[4] = 0x0D;
  478. HAL_UART_Transmit_DMA(huart, pData, 5);
  479. if (rt_sem_take(lock_sem, 2000) != RT_EOK)
  480. rt_sem_release(lock_sem);
  481. else rt_sem_release(lock_sem);
  482. } else rt_sem_release(lock_sem);
  483. }
  484. void enterApi(rt_sem_t lock_sem, UART_HandleTypeDef *huart, uint8_t *pData)
  485. {
  486. unsigned char i;
  487. for (i = 0; i < 3; i++)
  488. pData[i] = 0x2B;
  489. rt_sem_take(lock_sem, RT_WAITING_FOREVER);
  490. HAL_UART_Transmit_DMA(huart, pData, 3);
  491. }
  492. void setAPI(void)
  493. {
  494. if (!XbeeConst_UseAPI && iscowweight<1)
  495. {
  496. enterApi(uart3_lock, &huart3, usart3_send_data);
  497. if (rt_sem_take(uart3_lock, 2000) == RT_EOK) {
  498. usart3_send_data[0] = 0x41;
  499. usart3_send_data[1] = 0x54;
  500. usart3_send_data[2] = 0x41;
  501. usart3_send_data[3] = 0x50;
  502. usart3_send_data[4] = 0x30;
  503. usart3_send_data[5] = 0x0D;
  504. HAL_UART_Transmit_DMA(&huart3, usart3_send_data, 6);
  505. if (rt_sem_take(uart3_lock, 2000) == RT_EOK) {
  506. saveApi(uart3_lock, &huart3, usart3_send_data);
  507. } else rt_sem_release(uart3_lock);
  508. } else rt_sem_release(uart3_lock);
  509. }
  510. enterApi(uart2_lock, &huart2, usart2_send_data);
  511. if (rt_sem_take(uart2_lock, 2000) == RT_EOK) {
  512. //设置API Enable =2
  513. usart2_send_data[0] = 0x41;
  514. usart2_send_data[1] = 0x54;
  515. usart2_send_data[2] = 0x41;
  516. usart2_send_data[3] = 0x50;
  517. if (XbeeConst_UseAPI>0)
  518. usart2_send_data[4] = 0x32;
  519. else
  520. usart2_send_data[4] = 0x30;
  521. usart2_send_data[5] = 0x0D;
  522. HAL_UART_Transmit_DMA(&huart2, usart2_send_data, 6);
  523. if (rt_sem_take(uart2_lock, 2000) == RT_EOK) {
  524. saveApi(uart2_lock, &huart2, usart2_send_data);
  525. beep();
  526. rt_thread_mdelay(500);
  527. beep();
  528. return;
  529. } else rt_sem_release(uart2_lock);
  530. } else rt_sem_release(uart2_lock);
  531. beep();
  532. }
  533. void setNetAddress(void)
  534. {
  535. if (!XbeeConst_UseAPI && iscowweight<1)
  536. {
  537. enterApi(uart3_lock, &huart3, usart3_send_data);
  538. if (rt_sem_take(uart3_lock, 2000) == RT_EOK) {
  539. usart3_send_data[0] = 0x41;
  540. usart3_send_data[1] = 0x54;
  541. usart3_send_data[2] = 0x49;
  542. usart3_send_data[3] = 0x44;
  543. usart3_send_data[4] = 0x37;
  544. usart3_send_data[5] = 0x46;
  545. usart3_send_data[6] = 0x46;
  546. usart3_send_data[7] = 0x45;
  547. usart3_send_data[8] = 0x0D;
  548. HAL_UART_Transmit_DMA(&huart3, usart3_send_data, 9);
  549. if (rt_sem_take(uart3_lock, 2000) == RT_EOK) {
  550. saveApi(uart3_lock, &huart3, usart3_send_data);
  551. } else rt_sem_release(uart3_lock);
  552. } else rt_sem_release(uart3_lock);
  553. }
  554. enterApi(uart2_lock, &huart2, usart2_send_data);
  555. if (rt_sem_take(uart2_lock, 2000) == RT_EOK) {
  556. //设置API Enable =2
  557. usart2_send_data[0] = 0x41;
  558. usart2_send_data[1] = 0x54;
  559. usart2_send_data[2] = 0x49;
  560. usart2_send_data[3] = 0x44;
  561. usart2_send_data[4] = 0x37;
  562. usart2_send_data[5] = 0x46;
  563. usart2_send_data[6] = 0x46;
  564. if(XbeeConst_NetAddress>0)
  565. {
  566. if (XbeeConst_NetAddress>9) usart2_send_data[7] = (XbeeConst_NetAddress-10)+0x41;
  567. else usart2_send_data[7] = XbeeConst_NetAddress+0x30;
  568. }
  569. else usart2_send_data[7] = 0x46;
  570. usart2_send_data[8] = 0x0D;
  571. HAL_UART_Transmit_DMA(&huart2, usart2_send_data, 9);
  572. if (rt_sem_take(uart2_lock, 2000) == RT_EOK) {
  573. saveApi(uart2_lock, &huart2, usart2_send_data);
  574. beep();
  575. rt_thread_mdelay(500);
  576. beep();
  577. return;
  578. } else rt_sem_release(uart2_lock);
  579. } else rt_sem_release(uart2_lock);
  580. beep();
  581. }
  582. /*
  583. void setBT(void)
  584. {
  585. //进入命令
  586. // WeightConst_Weight_display[7] = 0x20;WeightConst_Weight_display[6] = 0x20;
  587. // WeightConst_Weight_display[5] = 0x20;WeightConst_Weight_display[4] = 0x20;
  588. unsigned char sendCount=0,i;
  589. for (int SendDisplay_i = 0; SendDisplay_i < 8; SendDisplay_i++) //显示重量
  590. WeightConst_Weight_display[SendDisplay_i]=0x20;
  591. USART3_Configuration(0);
  592. sendCount = 0;
  593. while(sendCount<2)
  594. {
  595. setBT_timer = 1;
  596. //AT+CLRBAND AT+HOSTEN1
  597. usart3_send_data[0] = 0x41;
  598. usart3_send_data[1] = 0x54;
  599. usart3_send_data[2] = 0x2B;
  600. usart3_send_data[3] = 0x43;
  601. usart3_send_data[4] = 0x4C;
  602. usart3_send_data[5] = 0x52;
  603. usart3_send_data[6] = 0x42;
  604. usart3_send_data[7] = 0x41;
  605. usart3_send_data[8] = 0x4E;
  606. usart3_send_data[9] = 0x44;
  607. USART_Send_DMA(remoteSerial, 0x0A);
  608. while(setBT_timer>0 && setBT_timer<500);
  609. if (setBT_timer>=500)
  610. sendCount++;
  611. else if(setBT_timer==0)
  612. sendCount=4;
  613. }
  614. sendCount = 0;
  615. while(sendCount<2)
  616. {
  617. setBT_timer = 1;
  618. //41 54 2B 48 4F 53 54 45 4E 31 AT+HOSTEN1
  619. usart3_send_data[0] = 0x41;
  620. usart3_send_data[1] = 0x54;
  621. usart3_send_data[2] = 0x2B;
  622. usart3_send_data[3] = 0x48;
  623. usart3_send_data[4] = 0x4F;
  624. usart3_send_data[5] = 0x53;
  625. usart3_send_data[6] = 0x54;
  626. usart3_send_data[7] = 0x45;
  627. usart3_send_data[8] = 0x4E;
  628. usart3_send_data[9] = 0x31;
  629. USART_Send_DMA(remoteSerial, 0x0A);
  630. while(setBT_timer>0 && setBT_timer<500);
  631. if (setBT_timer>=500)
  632. sendCount++;
  633. else if(setBT_timer==0)
  634. sendCount=4;
  635. }
  636. sendCount = 0;
  637. while(sendCount<2)
  638. {
  639. setBT_timer = 1;
  640. //AT+RST
  641. usart3_send_data[0] = 0x41;
  642. usart3_send_data[1] = 0x54;
  643. usart3_send_data[2] = 0x2B;
  644. usart3_send_data[3] = 0x52;
  645. usart3_send_data[4] = 0x53;
  646. usart3_send_data[5] = 0x54;
  647. USART_Send_DMA(remoteSerial, 0x06);
  648. while(setBT_timer>0 && setBT_timer<500);
  649. if (setBT_timer>=500)
  650. sendCount++;
  651. else if(setBT_timer==0)
  652. {
  653. setBT_timer=1;
  654. while(setBT_timer>0 && setBT_timer<1000);
  655. sendCount=4;
  656. }
  657. }
  658. sendCount = 0;
  659. // while(sendCount<2)
  660. {
  661. setBT_timer = 1;
  662. //AT+SCAN1
  663. usart3_send_data[0] = 0x41;
  664. usart3_send_data[1] = 0x54;
  665. usart3_send_data[2] = 0x2B;
  666. usart3_send_data[3] = 0x53;
  667. usart3_send_data[4] = 0x43;
  668. usart3_send_data[5] = 0x41;
  669. usart3_send_data[6] = 0x4E;
  670. usart3_send_data[7] = 0x31;
  671. USART_Send_DMA(remoteSerial, 0x08);
  672. while(setBT_timer>0 && setBT_timer<500);
  673. if (setBT_timer>=500)
  674. sendCount++;
  675. else if(setBT_timer==0)
  676. {
  677. setBT_timer=1;
  678. while(setBT_timer>0 && setBT_timer<5000);
  679. if (setBT_timer>=5000)
  680. {
  681. WeightConst_Weight_display[7] = 0xCA;
  682. WeightConst_Weight_display[6] = 0xA7;
  683. WeightConst_Weight_display[5] = 0xB0;
  684. WeightConst_Weight_display[4] = 0xDC;
  685. sendCount=4;
  686. WeightConst_BLE = 0;
  687. TMRWatchConst[Const_BLE].Data = WeightConst_BLE;
  688. EE_WriteVariable(TMRWatchConst_EEPROM_START_ADDRESS, TMRWatchConst[Const_BLE].VirtAddress,
  689. TMRWatchConst, &TMRWatchConst_CurWrAddress);
  690. } //失败CAA7 B0DC //失败
  691. else
  692. {
  693. setBT_timer = 1;
  694. //AT+BAND0
  695. usart3_send_data[0] = 0x41;
  696. usart3_send_data[1] = 0x54;
  697. usart3_send_data[2] = 0x2B;
  698. usart3_send_data[3] = 0x42;
  699. usart3_send_data[4] = 0x41;
  700. usart3_send_data[5] = 0x4E;
  701. usart3_send_data[6] = 0x44;
  702. usart3_send_data[7] = 0x30;
  703. USART_Send_DMA(remoteSerial, 0x08);
  704. while(setBT_timer>0 && setBT_timer<500);
  705. if (setBT_timer>=500)
  706. {
  707. WeightConst_Weight_display[7] = 0xCA;
  708. WeightConst_Weight_display[6] = 0xA7;
  709. WeightConst_Weight_display[5] = 0xB0;
  710. WeightConst_Weight_display[4] = 0xDC;
  711. WeightConst_BLE = 0;
  712. TMRWatchConst[Const_BLE].Data = WeightConst_BLE;
  713. EE_WriteVariable(TMRWatchConst_EEPROM_START_ADDRESS, TMRWatchConst[Const_BLE].VirtAddress,
  714. TMRWatchConst, &TMRWatchConst_CurWrAddress);
  715. } //失败CAA7 B0DC
  716. else if(setBT_timer==0)
  717. {
  718. WeightConst_Weight_display[7] = 0xB3;
  719. WeightConst_Weight_display[6] = 0xC9;
  720. WeightConst_Weight_display[5] = 0xB9;
  721. WeightConst_Weight_display[4] = 0xA6;
  722. WeightConst_BLE = 1;
  723. TMRWatchConst[Const_BLE].Data = WeightConst_BLE;
  724. EE_WriteVariable(TMRWatchConst_EEPROM_START_ADDRESS, TMRWatchConst[Const_BLE].VirtAddress,
  725. TMRWatchConst, &TMRWatchConst_CurWrAddress);
  726. } //成功B3C9 B9A6
  727. }
  728. }
  729. }
  730. USART3_Configuration(1);
  731. }
  732. void setNetAddress_byapi(u8 netid)
  733. { //7E 00 06 08 01 49 44 7F FE EC
  734. unsigned char sendCount=0,i;
  735. usart2_send_data[0] = 0x7E;
  736. usart2_send_data[1] = 0x00;
  737. usart2_send_data[2] = 0x06;
  738. usart2_send_data[3] = 0x08;
  739. usart2_send_data[4] = 0x01;
  740. usart2_send_data[5] = 0x49;
  741. usart2_send_data[6] = 0x44;
  742. usart2_send_data[7] = 0x7F;
  743. if(netid)
  744. usart2_send_data[8] = 0xF0 + netid;
  745. else usart2_send_data[8] = 0xFF;
  746. for (i = 3; i < 9; i++)
  747. sendCount = sendCount + usart2_send_data[i];
  748. usart2_send_data[9] = 0xFF-sendCount;
  749. USART_Send_DMA(xbeeSerial, 0x0A);
  750. }
  751. */