flash.c 28 KB

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