flash.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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 =0; //新小屏
  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 = 0x01;
  21. uint8_t WeightConst_TQ = 0x01;//0x05;
  22. uint8_t WeightConst_BLE = 0;
  23. uint8_t WeightConst_ChanCheShu = 0x02;
  24. uint8_t WeightConst_SBType = 0x01;
  25. int16_t WeightConst_WeightBegin = 0; //按键开始重量
  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. rt_sem_release(kv_db_lock);
  80. __enable_irq();
  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, 0xFFFFFFFF, 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, 0xFFFFFFFF, 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 = 0xFFFFFFFF;
  248. fdb_tsl_iter_by_time(&ts_keydb, read_key, 0xFFFFFFFF, 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)<0xFFFFFFFF){
  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, 0xFFFFFFFF, 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, 0xFFFFFFFF, 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, 0xFFFFFFFF, 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, 0xFFFFFFFF, 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. result = fdb_tsl_append(db, fdb_blob_make(&blob, buf, length));
  414. if (result != FDB_NO_ERR)
  415. {
  416. // rt_kprintf("ts_pushArrary:%02X \n", ts_planTime);
  417. return 0;
  418. }
  419. }
  420. return 1;
  421. }
  422. extern uint8_t isNsmall;
  423. void read_Flash(void)
  424. {
  425. struct fdb_blob blob;
  426. fdb_kv_get_blob(&kvdb, "CH", fdb_blob_make(&blob, &XbeeConst_DeviceAddress, sizeof(XbeeConst_DeviceAddress)));
  427. fdb_kv_get_blob(&kvdb, "TQ", fdb_blob_make(&blob, &WeightConst_TQ, sizeof(WeightConst_TQ)));
  428. fdb_kv_get_blob(&kvdb, "PM", fdb_blob_make(&blob, &WeightConst_SBType, sizeof(WeightConst_SBType)));
  429. fdb_kv_get_blob(&kvdb, "WL", fdb_blob_make(&blob, &XbeeConst_NetAddress, sizeof(XbeeConst_NetAddress)));
  430. fdb_kv_get_blob(&kvdb, "API", fdb_blob_make(&blob, &XbeeConst_UseAPI, sizeof(XbeeConst_UseAPI)));
  431. fdb_kv_get_blob(&kvdb, "CC", fdb_blob_make(&blob, &WeightConst_ChanCheShu, sizeof(WeightConst_ChanCheShu)));
  432. fdb_kv_get_blob(&kvdb, "BLE", fdb_blob_make(&blob, &WeightConst_BLE, sizeof(WeightConst_BLE)));
  433. if (WeightConst_BLE>1) isCC=1;
  434. fdb_kv_get_blob(&kvdb, "ad_Percent", fdb_blob_make(&blob, &ad_Percent, sizeof(ad_Percent)));
  435. fdb_kv_get_blob(&kvdb, "ad_Maxval", fdb_blob_make(&blob, &ad_Maxval, sizeof(ad_Maxval)));
  436. fdb_kv_get_blob(&kvdb, "ad_FS", fdb_blob_make(&blob, &ad_FS, sizeof(ad_FS)));
  437. fdb_kv_get_blob(&kvdb, "ad_Point", fdb_blob_make(&blob, &ad_Point, sizeof(ad_Point)));
  438. fdb_kv_get_blob(&kvdb, "ad_Inteval", fdb_blob_make(&blob, &ad_Inteval, sizeof(ad_Inteval)));
  439. fdb_kv_get_blob(&kvdb, "ad_Spanz", fdb_blob_make(&blob, &ad_Spanz, sizeof(ad_Spanz)));
  440. fdb_kv_get_blob(&kvdb, "ad_Zero", fdb_blob_make(&blob, &ad_Zero, sizeof(ad_Zero)));
  441. fdb_kv_get_blob(&kvdb, "ad_Tare", fdb_blob_make(&blob, &ad_Tare, sizeof(ad_Tare)));
  442. fdb_kv_get_blob(&kvdb, "read_key", fdb_blob_make(&blob, &read_key, sizeof(read_key)));
  443. fdb_kv_get_blob(&kvdb, "read_plan", fdb_blob_make(&blob, &read_plan, sizeof(read_plan)));
  444. fdb_kv_get_blob(&kvdb, "read_weight", fdb_blob_make(&blob, &read_weight, sizeof(read_weight)));
  445. fdb_kv_get_blob(&kvdb, "beginweight", fdb_blob_make(&blob, &WeightConst_WeightBegin, sizeof(WeightConst_WeightBegin)));
  446. fdb_kv_get_blob(&kvdb, "begintime", fdb_blob_make(&blob, &TimeConst, sizeof(TimeConst)));
  447. fdb_kv_get_blob(&kvdb, "completeorder", fdb_blob_make(&blob, &CompleteTarget_order, sizeof(CompleteTarget_order)));
  448. fdb_kv_get_blob(&kvdb, "TimeConst", fdb_blob_make(&blob, &TimeConst, sizeof(TimeConst)));
  449. if (isNsmall){
  450. WeightConst_SBType = 3;
  451. }
  452. }
  453. void onbordflash_init(void)
  454. {
  455. fdb_err_t result;
  456. struct fdb_default_kv default_kv;
  457. kv_db_lock = rt_sem_create("kv_db_lock", 1, RT_IPC_FLAG_FIFO);
  458. default_kv.kvs = default_kv_table;
  459. default_kv.num = sizeof(default_kv_table) / sizeof(default_kv_table[0]);
  460. fdb_kvdb_control(&kvdb, FDB_KVDB_CTRL_SET_LOCK, lock);
  461. fdb_kvdb_control(&kvdb, FDB_KVDB_CTRL_SET_UNLOCK, unlock);
  462. result = fdb_kvdb_init(&kvdb, "env", "kv", &default_kv, NULL);
  463. if (result != FDB_NO_ERR) {
  464. return ;
  465. }
  466. /////////////
  467. fdb_tsdb_control(&ts_keydb, FDB_TSDB_CTRL_SET_LOCK, lock);
  468. fdb_tsdb_control(&ts_keydb, FDB_TSDB_CTRL_SET_UNLOCK, unlock);
  469. result = fdb_tsdb_init(&ts_keydb, "key", "ts_key", get_keydbtime, 48, NULL);
  470. if (result != FDB_NO_ERR) {
  471. return ;
  472. }
  473. /* read last saved time for simulated timestamp */
  474. fdb_tsdb_control(&ts_keydb, FDB_TSDB_CTRL_GET_LAST_TIME, &ts_keyTime);
  475. /////////////////
  476. /////////////
  477. fdb_tsdb_control(&ts_plandb, FDB_TSDB_CTRL_SET_LOCK, lock);
  478. fdb_tsdb_control(&ts_plandb, FDB_TSDB_CTRL_SET_UNLOCK, unlock);
  479. result = fdb_tsdb_init(&ts_plandb, "plan", "ts_plan", get_plandbtime, 48, NULL);
  480. if (result != FDB_NO_ERR) {
  481. return ;
  482. }
  483. /* read last saved time for simulated timestamp */
  484. fdb_tsdb_control(&ts_plandb, FDB_TSDB_CTRL_GET_LAST_TIME, &ts_planTime);
  485. /////////////////
  486. /////////////
  487. fdb_tsdb_control(&ts_weightdb, FDB_TSDB_CTRL_SET_LOCK, lock);
  488. fdb_tsdb_control(&ts_weightdb, FDB_TSDB_CTRL_SET_UNLOCK, unlock);
  489. result = fdb_tsdb_init(&ts_weightdb, "weight", "ts_weight", get_weightdbtime, 7, NULL);
  490. if (result != FDB_NO_ERR) {
  491. return ;
  492. }
  493. /* read last saved time for simulated timestamp */
  494. fdb_tsdb_control(&ts_weightdb, FDB_TSDB_CTRL_GET_LAST_TIME, &ts_weightTime);
  495. /////////////////
  496. read_Flash();
  497. }
  498. void saveApi(rt_sem_t lock_sem,UART_HandleTypeDef *huart, uint8_t *pData)
  499. {
  500. //保存命令
  501. pData[0] = 0x41;
  502. pData[1] = 0x54;
  503. pData[2] = 0x57;
  504. pData[3] = 0x52;
  505. pData[4] = 0x0D;
  506. HAL_UART_Transmit_DMA(huart, pData, 5);
  507. if (rt_sem_take(lock_sem, 2000) == RT_EOK) {
  508. //退出命令
  509. pData[0] = 0x41;
  510. pData[1] = 0x54;
  511. pData[2] = 0x43;
  512. pData[3] = 0x4E;
  513. pData[4] = 0x0D;
  514. HAL_UART_Transmit_DMA(huart, pData, 5);
  515. if (rt_sem_take(lock_sem, 2000) != RT_EOK)
  516. rt_sem_release(lock_sem);
  517. else rt_sem_release(lock_sem);
  518. } else rt_sem_release(lock_sem);
  519. }
  520. void enterApi(rt_sem_t lock_sem, UART_HandleTypeDef *huart, uint8_t *pData)
  521. {
  522. unsigned char i;
  523. for (i = 0; i < 3; i++)
  524. pData[i] = 0x2B;
  525. rt_sem_take(lock_sem, RT_WAITING_FOREVER);
  526. HAL_UART_Transmit_DMA(huart, pData, 3);
  527. }
  528. void setAPI(void)
  529. {
  530. if (!XbeeConst_UseAPI && iscowweight<1)
  531. {
  532. enterApi(uart3_lock, &huart3, usart3_send_data);
  533. if (rt_sem_take(uart3_lock, 2000) == RT_EOK) {
  534. usart3_send_data[0] = 0x41;
  535. usart3_send_data[1] = 0x54;
  536. usart3_send_data[2] = 0x41;
  537. usart3_send_data[3] = 0x50;
  538. usart3_send_data[4] = 0x30;
  539. usart3_send_data[5] = 0x0D;
  540. HAL_UART_Transmit_DMA(&huart3, usart3_send_data, 6);
  541. if (rt_sem_take(uart3_lock, 2000) == RT_EOK) {
  542. saveApi(uart3_lock, &huart3, usart3_send_data);
  543. } else rt_sem_release(uart3_lock);
  544. } else rt_sem_release(uart3_lock);
  545. }
  546. enterApi(uart2_lock, &huart2, usart2_send_data);
  547. if (rt_sem_take(uart2_lock, 2000) == RT_EOK) {
  548. //设置API Enable =2
  549. usart2_send_data[0] = 0x41;
  550. usart2_send_data[1] = 0x54;
  551. usart2_send_data[2] = 0x41;
  552. usart2_send_data[3] = 0x50;
  553. if (XbeeConst_UseAPI>0)
  554. usart2_send_data[4] = 0x32;
  555. else
  556. usart2_send_data[4] = 0x30;
  557. usart2_send_data[5] = 0x0D;
  558. HAL_UART_Transmit_DMA(&huart2, usart2_send_data, 6);
  559. if (rt_sem_take(uart2_lock, 2000) == RT_EOK) {
  560. saveApi(uart2_lock, &huart2, usart2_send_data);
  561. beep();
  562. rt_thread_mdelay(500);
  563. beep();
  564. return;
  565. } else rt_sem_release(uart2_lock);
  566. } else rt_sem_release(uart2_lock);
  567. beep();
  568. }
  569. void setNetAddress(void)
  570. {
  571. if (!XbeeConst_UseAPI && iscowweight<1)
  572. {
  573. enterApi(uart3_lock, &huart3, usart3_send_data);
  574. if (rt_sem_take(uart3_lock, 2000) == RT_EOK) {
  575. usart3_send_data[0] = 0x41;
  576. usart3_send_data[1] = 0x54;
  577. usart3_send_data[2] = 0x49;
  578. usart3_send_data[3] = 0x44;
  579. usart3_send_data[4] = 0x37;
  580. usart3_send_data[5] = 0x46;
  581. usart3_send_data[6] = 0x46;
  582. usart3_send_data[7] = 0x45;
  583. usart3_send_data[8] = 0x0D;
  584. HAL_UART_Transmit_DMA(&huart3, usart3_send_data, 9);
  585. if (rt_sem_take(uart3_lock, 2000) == RT_EOK) {
  586. saveApi(uart3_lock, &huart3, usart3_send_data);
  587. } else rt_sem_release(uart3_lock);
  588. } else rt_sem_release(uart3_lock);
  589. }
  590. enterApi(uart2_lock, &huart2, usart2_send_data);
  591. if (rt_sem_take(uart2_lock, 2000) == RT_EOK) {
  592. //设置API Enable =2
  593. usart2_send_data[0] = 0x41;
  594. usart2_send_data[1] = 0x54;
  595. usart2_send_data[2] = 0x49;
  596. usart2_send_data[3] = 0x44;
  597. usart2_send_data[4] = 0x37;
  598. usart2_send_data[5] = 0x46;
  599. usart2_send_data[6] = 0x46;
  600. if(XbeeConst_NetAddress>0)
  601. {
  602. if (XbeeConst_NetAddress>9) usart2_send_data[7] = (XbeeConst_NetAddress-10)+0x41;
  603. else usart2_send_data[7] = XbeeConst_NetAddress+0x30;
  604. }
  605. else usart2_send_data[7] = 0x46;
  606. usart2_send_data[8] = 0x0D;
  607. HAL_UART_Transmit_DMA(&huart2, usart2_send_data, 9);
  608. if (rt_sem_take(uart2_lock, 2000) == RT_EOK) {
  609. saveApi(uart2_lock, &huart2, usart2_send_data);
  610. beep();
  611. rt_thread_mdelay(500);
  612. beep();
  613. return;
  614. } else rt_sem_release(uart2_lock);
  615. } else rt_sem_release(uart2_lock);
  616. beep();
  617. }
  618. /*
  619. void setBT(void)
  620. {
  621. //进入命令
  622. // WeightConst_Weight_display[7] = 0x20;WeightConst_Weight_display[6] = 0x20;
  623. // WeightConst_Weight_display[5] = 0x20;WeightConst_Weight_display[4] = 0x20;
  624. unsigned char sendCount=0,i;
  625. for (int SendDisplay_i = 0; SendDisplay_i < 8; SendDisplay_i++) //显示重量
  626. WeightConst_Weight_display[SendDisplay_i]=0x20;
  627. USART3_Configuration(0);
  628. sendCount = 0;
  629. while(sendCount<2)
  630. {
  631. setBT_timer = 1;
  632. //AT+CLRBAND AT+HOSTEN1
  633. usart3_send_data[0] = 0x41;
  634. usart3_send_data[1] = 0x54;
  635. usart3_send_data[2] = 0x2B;
  636. usart3_send_data[3] = 0x43;
  637. usart3_send_data[4] = 0x4C;
  638. usart3_send_data[5] = 0x52;
  639. usart3_send_data[6] = 0x42;
  640. usart3_send_data[7] = 0x41;
  641. usart3_send_data[8] = 0x4E;
  642. usart3_send_data[9] = 0x44;
  643. USART_Send_DMA(remoteSerial, 0x0A);
  644. while(setBT_timer>0 && setBT_timer<500);
  645. if (setBT_timer>=500)
  646. sendCount++;
  647. else if(setBT_timer==0)
  648. sendCount=4;
  649. }
  650. sendCount = 0;
  651. while(sendCount<2)
  652. {
  653. setBT_timer = 1;
  654. //41 54 2B 48 4F 53 54 45 4E 31 AT+HOSTEN1
  655. usart3_send_data[0] = 0x41;
  656. usart3_send_data[1] = 0x54;
  657. usart3_send_data[2] = 0x2B;
  658. usart3_send_data[3] = 0x48;
  659. usart3_send_data[4] = 0x4F;
  660. usart3_send_data[5] = 0x53;
  661. usart3_send_data[6] = 0x54;
  662. usart3_send_data[7] = 0x45;
  663. usart3_send_data[8] = 0x4E;
  664. usart3_send_data[9] = 0x31;
  665. USART_Send_DMA(remoteSerial, 0x0A);
  666. while(setBT_timer>0 && setBT_timer<500);
  667. if (setBT_timer>=500)
  668. sendCount++;
  669. else if(setBT_timer==0)
  670. sendCount=4;
  671. }
  672. sendCount = 0;
  673. while(sendCount<2)
  674. {
  675. setBT_timer = 1;
  676. //AT+RST
  677. usart3_send_data[0] = 0x41;
  678. usart3_send_data[1] = 0x54;
  679. usart3_send_data[2] = 0x2B;
  680. usart3_send_data[3] = 0x52;
  681. usart3_send_data[4] = 0x53;
  682. usart3_send_data[5] = 0x54;
  683. USART_Send_DMA(remoteSerial, 0x06);
  684. while(setBT_timer>0 && setBT_timer<500);
  685. if (setBT_timer>=500)
  686. sendCount++;
  687. else if(setBT_timer==0)
  688. {
  689. setBT_timer=1;
  690. while(setBT_timer>0 && setBT_timer<1000);
  691. sendCount=4;
  692. }
  693. }
  694. sendCount = 0;
  695. // while(sendCount<2)
  696. {
  697. setBT_timer = 1;
  698. //AT+SCAN1
  699. usart3_send_data[0] = 0x41;
  700. usart3_send_data[1] = 0x54;
  701. usart3_send_data[2] = 0x2B;
  702. usart3_send_data[3] = 0x53;
  703. usart3_send_data[4] = 0x43;
  704. usart3_send_data[5] = 0x41;
  705. usart3_send_data[6] = 0x4E;
  706. usart3_send_data[7] = 0x31;
  707. USART_Send_DMA(remoteSerial, 0x08);
  708. while(setBT_timer>0 && setBT_timer<500);
  709. if (setBT_timer>=500)
  710. sendCount++;
  711. else if(setBT_timer==0)
  712. {
  713. setBT_timer=1;
  714. while(setBT_timer>0 && setBT_timer<5000);
  715. if (setBT_timer>=5000)
  716. {
  717. WeightConst_Weight_display[7] = 0xCA;
  718. WeightConst_Weight_display[6] = 0xA7;
  719. WeightConst_Weight_display[5] = 0xB0;
  720. WeightConst_Weight_display[4] = 0xDC;
  721. sendCount=4;
  722. WeightConst_BLE = 0;
  723. TMRWatchConst[Const_BLE].Data = WeightConst_BLE;
  724. EE_WriteVariable(TMRWatchConst_EEPROM_START_ADDRESS, TMRWatchConst[Const_BLE].VirtAddress,
  725. TMRWatchConst, &TMRWatchConst_CurWrAddress);
  726. } //失败CAA7 B0DC //失败
  727. else
  728. {
  729. setBT_timer = 1;
  730. //AT+BAND0
  731. usart3_send_data[0] = 0x41;
  732. usart3_send_data[1] = 0x54;
  733. usart3_send_data[2] = 0x2B;
  734. usart3_send_data[3] = 0x42;
  735. usart3_send_data[4] = 0x41;
  736. usart3_send_data[5] = 0x4E;
  737. usart3_send_data[6] = 0x44;
  738. usart3_send_data[7] = 0x30;
  739. USART_Send_DMA(remoteSerial, 0x08);
  740. while(setBT_timer>0 && setBT_timer<500);
  741. if (setBT_timer>=500)
  742. {
  743. WeightConst_Weight_display[7] = 0xCA;
  744. WeightConst_Weight_display[6] = 0xA7;
  745. WeightConst_Weight_display[5] = 0xB0;
  746. WeightConst_Weight_display[4] = 0xDC;
  747. WeightConst_BLE = 0;
  748. TMRWatchConst[Const_BLE].Data = WeightConst_BLE;
  749. EE_WriteVariable(TMRWatchConst_EEPROM_START_ADDRESS, TMRWatchConst[Const_BLE].VirtAddress,
  750. TMRWatchConst, &TMRWatchConst_CurWrAddress);
  751. } //失败CAA7 B0DC
  752. else if(setBT_timer==0)
  753. {
  754. WeightConst_Weight_display[7] = 0xB3;
  755. WeightConst_Weight_display[6] = 0xC9;
  756. WeightConst_Weight_display[5] = 0xB9;
  757. WeightConst_Weight_display[4] = 0xA6;
  758. WeightConst_BLE = 1;
  759. TMRWatchConst[Const_BLE].Data = WeightConst_BLE;
  760. EE_WriteVariable(TMRWatchConst_EEPROM_START_ADDRESS, TMRWatchConst[Const_BLE].VirtAddress,
  761. TMRWatchConst, &TMRWatchConst_CurWrAddress);
  762. } //成功B3C9 B9A6
  763. }
  764. }
  765. }
  766. USART3_Configuration(1);
  767. }
  768. void setNetAddress_byapi(u8 netid)
  769. { //7E 00 06 08 01 49 44 7F FE EC
  770. unsigned char sendCount=0,i;
  771. usart2_send_data[0] = 0x7E;
  772. usart2_send_data[1] = 0x00;
  773. usart2_send_data[2] = 0x06;
  774. usart2_send_data[3] = 0x08;
  775. usart2_send_data[4] = 0x01;
  776. usart2_send_data[5] = 0x49;
  777. usart2_send_data[6] = 0x44;
  778. usart2_send_data[7] = 0x7F;
  779. if(netid)
  780. usart2_send_data[8] = 0xF0 + netid;
  781. else usart2_send_data[8] = 0xFF;
  782. for (i = 3; i < 9; i++)
  783. sendCount = sendCount + usart2_send_data[i];
  784. usart2_send_data[9] = 0xFF-sendCount;
  785. USART_Send_DMA(xbeeSerial, 0x0A);
  786. }
  787. */