flash.c 28 KB

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