flash.c 28 KB

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