AT24C128Opt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. #include "AT24C128Opt.h"
  2. #include "../network/downlink.h"
  3. #include "../encode/encodeOpt.h"
  4. /*------------------------------------------------
  5. * 清空EEPROM的存储空间
  6. * ------------------------------------------------*/
  7. void fram_init(void)
  8. {
  9. uint8_t buff[64] = {0};
  10. int i = 0;
  11. for(i=0;i<256;i++){
  12. AT24CXX_Write(0+i*64,buff,sizeof(buff));
  13. }
  14. // fram_write_eeprom_powerUpFlag();
  15. }
  16. /*----------------------------------------------
  17. * 是否第一次上电 的写入
  18. * ---------------------------------------------*/
  19. void fram_write_eeprom_powerUpFlag(void)
  20. {
  21. uint16_t version = 0x55AA;
  22. AT24CXX_Write(FRAM_FIRST_POWER_ON,(uint8_t*)&version,sizeof(uint16_t));
  23. }
  24. /*----------------------------------------------
  25. * 是否第一次上电 读取
  26. * ---------------------------------------------*/
  27. uint16_t fram_read_eeprom_powerUpFlag(void)
  28. {
  29. uint16_t version = 0;
  30. AT24CXX_Read(FRAM_FIRST_POWER_ON,(uint8_t*)&version,sizeof(uint16_t));
  31. return version;
  32. }
  33. /*----------------------------------------------
  34. * EEPROM 版本号的写入
  35. * ---------------------------------------------*/
  36. void fram_write_eeprom_version(void)
  37. {
  38. uint8_t version = 0x01;
  39. AT24CXX_Write(FRAM_VERSION,&version,1);
  40. }
  41. /*----------------------------------------------
  42. * EEPROM 版本号的读取
  43. * ---------------------------------------------*/
  44. uint8_t fram_read_eeprom_version(void)
  45. {
  46. uint8_t version = 0;
  47. AT24CXX_Read(FRAM_VERSION,&version,1);
  48. return version;
  49. }
  50. /*----------------------------------------------
  51. * 升级标志的写入
  52. * --------------------------------------------*/
  53. void fram_write_update_flag(void)
  54. {
  55. uint8_t flag = GATEWAY_UPDATE_FLAG;
  56. AT24CXX_Write(FRAM_UPDATE_FLAG,&flag,1);
  57. }
  58. /*----------------------------------------------
  59. * 配置信息标志的初始化
  60. * ---------------------------------------------*/
  61. void fram_init_config_msg(void)
  62. {
  63. GateConfigMsg *gconfigmsg = &(g_firmwareMsg.gatewayMsg.configmsg);
  64. gconfigmsg->bInitMsg.value = 0;
  65. gconfigmsg->coll_num = 0;
  66. gconfigmsg->time_interval = 0;
  67. AT24CXX_Write(FRAM_CONFIG_MSG,(uint8_t*)gconfigmsg,sizeof(GateConfigMsg));
  68. }
  69. /*-----------------------------------------------
  70. * 配置信息标志的写入
  71. * ----------------------------------------------*/
  72. void fram_write_config_msg(uint16_t flag)
  73. {
  74. GateConfigMsg *gconfigmsg = &(g_firmwareMsg.gatewayMsg.configmsg);
  75. if(flag==1){
  76. g_firmwareMsg.gatewayMsg.configmsg.bInitMsg.bInit.bConfig_tab = flag;
  77. }
  78. AT24CXX_Write(FRAM_CONFIG_MSG,(uint8_t*)gconfigmsg,sizeof(GateConfigMsg));
  79. }
  80. /*------------------------------------------------
  81. * 配置信息标志的读取
  82. * -----------------------------------------------*/
  83. uint8_t fram_read_config_msg(void)
  84. {
  85. GateConfigMsg *gconfigmsg = &(g_firmwareMsg.gatewayMsg.configmsg);
  86. downlink_config.config_flag = 0;
  87. downlink_config.collect_num = 0;
  88. AT24CXX_Read(FRAM_CONFIG_MSG,(uint8_t*)gconfigmsg,sizeof(GateConfigMsg));
  89. downlink_config.config_flag = gconfigmsg->bInitMsg.bInit.bConfig_tab;
  90. g_encoderDa.bhaveEncoder = gconfigmsg->bInitMsg.bInit.bConfig_qei;
  91. g_encodeOpt.benable = gconfigmsg->bInitMsg.bInit.benable_qei;
  92. // if(gconfigmsg->coll_num==0xFF) gconfigmsg->coll_num = 0x00;
  93. downlink_config.collect_num = gconfigmsg->coll_num;
  94. // if(gconfigmsg->time_interval < 2) gconfigmsg->time_interval = 2;
  95. return downlink_config.config_flag;
  96. }
  97. /*-------------------------------------------------
  98. * mqqtt ip prot的初始化
  99. * ----------------------------------------------------*/
  100. void fram_init_mqtt_msg(void)
  101. {
  102. MqttIdPort *mqttMsg = &(g_firmwareMsg.mqttidport);
  103. mqttMsg->flag = 0;
  104. memset(mqttMsg,0,sizeof(MqttIdPort));
  105. AT24CXX_Write(FRAM_MQTT_MSG,(uint8_t*)mqttMsg,sizeof(MqttIdPort));
  106. }
  107. /*-------------------------------------------------
  108. * mqqtt ip prot的写 入
  109. * ----------------------------------------------------*/
  110. void fram_write_mqtt_msg(void)
  111. {
  112. MqttIdPort *mqttMsg = &(g_firmwareMsg.mqttidport);
  113. AT24CXX_Write(FRAM_MQTT_MSG,(uint8_t*)mqttMsg,sizeof(MqttIdPort));
  114. }
  115. /*-------------------------------------------------
  116. * mqqtt ip prot的初始化
  117. * ----------------------------------------------------*/
  118. void fram_read_mqtt_msg(void)
  119. {
  120. MqttIdPort *mqttMsg = &(g_firmwareMsg.mqttidport);
  121. AT24CXX_Read(FRAM_MQTT_MSG,(uint8_t*)mqttMsg,sizeof(MqttIdPort));
  122. }
  123. /*-----------------------------------------------
  124. * 网关的硬件信息读取
  125. * -----------------------------------------------*/
  126. void fram_read_gateway_hardware_msg(void)
  127. {
  128. GateHareWareMsg *msg = &(g_firmwareMsg.gatewayMsg.hardwareMsg);
  129. downlink_config.gateway_id = 0x000000;
  130. AT24CXX_Read(FRAM_GATWAY_HARDWARE_MSG,(uint8_t*)msg,sizeof(GateHareWareMsg));
  131. downlink_config.gateway_id = msg->gateway_sn;
  132. }
  133. /*-----------------------------------------------
  134. * 网关的硬件信息写入
  135. * -----------------------------------------------*/
  136. void fram_write_gateway_hardware_msg(void)
  137. {
  138. GateHareWareMsg *msg = &(g_firmwareMsg.gatewayMsg.hardwareMsg);
  139. AT24CXX_Write(FRAM_GATWAY_HARDWARE_MSG,(uint8_t*)msg,sizeof(GateHareWareMsg));
  140. }
  141. /*----------------------------------------------
  142. * 网关信息的初始化
  143. * ---------------------------------------------*/
  144. void fram_init_gateway_msg(void)
  145. {
  146. g_firmwareMsg.gatewayMsg.configmsg.coll_num = 0;
  147. g_firmwareMsg.gatewayMsg.configmsg.bInitMsg.value = 0;
  148. g_firmwareMsg.gatewayMsg.gate_resetnum = 0;
  149. fram_write_config_msg(0);
  150. fram_init_gateway_resetnum();
  151. }
  152. /*----------------------------------------------------
  153. * 网关复位次数的初始化
  154. * ---------------------------------------------------*/
  155. void fram_init_gateway_resetnum(void)
  156. {
  157. // uint32_t num = 0;
  158. return;
  159. // g_firmwareMsg.gatewayMsg.gate_resetnum = 0;
  160. // AT24CXX_Write(FRAM_GATWAY_RESETNUM,(uint8_t*)&num,sizeof(uint32_t));
  161. }
  162. /*----------------------------------------------------
  163. * 网关复位次数的写入
  164. * ---------------------------------------------------*/
  165. void fram_wirte_gateway_resetnum(void)
  166. {
  167. // uint32_t num = 0;
  168. return;
  169. // num = g_firmwareMsg.gatewayMsg.gate_resetnum;
  170. // AT24CXX_Write(FRAM_GATWAY_RESETNUM,(uint8_t*)&num,sizeof(uint32_t));
  171. }
  172. /*----------------------------------------------------
  173. * 网关复位次数的读取
  174. * ---------------------------------------------------*/
  175. void fram_read_gateway_resetnum(void)
  176. {
  177. uint32_t num = 0;
  178. AT24CXX_Read(FRAM_GATWAY_RESETNUM,(uint8_t*)&num,sizeof(uint32_t));
  179. g_firmwareMsg.gatewayMsg.gate_resetnum = num;
  180. }
  181. /*------------------------------------------------------
  182. * 网关软件版本的信息初始化
  183. * 固件信息格式 :DDDDFNNN:
  184. * DDDD(设备类型):0x0101:网关A 0x0102:网关B 0x0201:采集器1 0x0202:采集器2
  185. * F(固件类型):0x01:bootloader版本 0x02:pt版本 0x03:app版本 0xF:合成版本
  186. * NNN:0x001 表示第1个版本 0x002:表示第2个版本
  187. * -----------------------------------------------------*/
  188. void fram_init_gateway_version(void)
  189. {
  190. GateVersionMsg *msg = NULL;
  191. g_firmwareExpl.dd = GATEWAY_DEVICE_TYPE; //网关A
  192. g_firmwareExpl.ff = GATEWAY_APP_TYPE; //app版本
  193. g_firmwareExpl.nn = GATEWAY_APP_VERSION; // 第一个版本
  194. fram_read_gateway_version();
  195. msg = &(g_firmwareMsg.gatewayMsg.verMsg);
  196. if(msg->gate_appVr != (((uint32_t)g_firmwareExpl.dd<<16)|((uint16_t)g_firmwareExpl.ff<<12)|g_firmwareExpl.nn)){
  197. msg->gate_appVr = ((uint32_t)g_firmwareExpl.dd<<16)|((uint16_t)g_firmwareExpl.ff<<12)|g_firmwareExpl.nn;
  198. // msg->gate_appVr2 = msg->gate_appVr;
  199. // msg->gate_appVr3 = msg->gate_appVr;
  200. fram_write_gateway_version();
  201. }
  202. }
  203. /*----------------------------------------------------
  204. * 网关 版本信息的读取
  205. * ---------------------------------------------------*/
  206. void fram_write_gateway_version(void)
  207. {
  208. GateVersionMsg *msg = NULL;
  209. msg = &(g_firmwareMsg.gatewayMsg.verMsg);
  210. AT24CXX_Write(FRAM_GATWAY_VER_MSG,(uint8_t*)msg,sizeof(GateVersionMsg));
  211. }
  212. /*----------------------------------------------------
  213. * 网关 版本信息的读取
  214. * ---------------------------------------------------*/
  215. void fram_read_gateway_version(void)
  216. {
  217. GateVersionMsg *msg = NULL;
  218. msg = &(g_firmwareMsg.gatewayMsg.verMsg);
  219. AT24CXX_Read(FRAM_GATWAY_VER_MSG,(uint8_t*)msg,sizeof(GateVersionMsg));
  220. // fram_init_gateway_version();
  221. }
  222. /*----------------------------------------------
  223. * 采集器信息的初始化
  224. * ---------------------------------------------*/
  225. void fram_init_collect_msg(void)
  226. {
  227. uint8_t i = 0;
  228. uint16_t len = 0;
  229. uint8_t buff[1200] = {0};
  230. // collect_conf_t *collMsg = NULL;
  231. len = sizeof(collect_conf_t)*16;
  232. memset(buff,0,len);
  233. AT24CXX_Write(FRAM_COLLECT_MSG+i,buff,len);
  234. }
  235. /*----------------------------------------------
  236. * 采集器信息的写入
  237. * ---------------------------------------------*/
  238. void fram_write_collect_msg(void)
  239. {
  240. uint8_t i = 0;
  241. uint16_t len = 0;
  242. collect_conf_t *collMsg = NULL;
  243. len = sizeof(collect_conf_t);
  244. for(i = 0;i < 16;i++){
  245. collMsg = &(downlink_config.collect_conf[i]);
  246. AT24CXX_Write(FRAM_COLLECT_MSG+i*len,(uint8_t *)collMsg,len);
  247. }
  248. }
  249. /*----------------------------------------------
  250. * 采集器信息的读取
  251. * ---------------------------------------------*/
  252. void fram_read_collect_msg(void)
  253. {
  254. uint8_t i = 0;
  255. uint16_t len = 0;
  256. collect_conf_t *collMsg = NULL;
  257. len = sizeof(collect_conf_t);
  258. for(i = 0;i < 16;i++){
  259. collMsg = &(downlink_config.collect_conf[i]);
  260. AT24CXX_Read(FRAM_COLLECT_MSG+i*len,(uint8_t*)collMsg,len);
  261. collMsg->collect_status = 0x00;
  262. //memset(downlink_config.collect_conf[i],collMsg,len);
  263. }
  264. }
  265. /*----------------------------------------------
  266. * 写 lora 编号
  267. * ---------------------------------------------*/
  268. void fram_write_lora_index(uint8_t index)
  269. {
  270. uint16_t value = 0;
  271. if(index>3 ) return;//&& index <0
  272. value = (~index);
  273. value = value<<8;
  274. value = (value | index);
  275. AT24CXX_Write(FRAM_LORA_INDEX,(uint8_t*)&value,2);
  276. }
  277. /*----------------------------------------------
  278. * 读取 lora 编号
  279. * ---------------------------------------------*/
  280. uint8_t fram_read_lora_index(void)
  281. {
  282. uint16_t value = 0;
  283. uint8_t index = 0, nindex=0;
  284. AT24CXX_Read(FRAM_LORA_INDEX,(uint8_t*)&value,2);
  285. index = value &0xFF;
  286. nindex = (~(value >>8))&0xFF;
  287. if(index != nindex) return 0;
  288. else return index;
  289. }
  290. /*-----------------------------------------------------
  291. * 编码器的配置表的存储
  292. * ----------------------------------------------------*/
  293. void fram_init_encoder_configMsg(void)
  294. {
  295. uint8_t value = 0;
  296. AT24CXX_Write(FRAM_ENCODEINIT,(uint8_t *)&value,1);
  297. }
  298. void fram_write_encoder_configMsg(void)
  299. {
  300. uint8_t value = 0;
  301. value = g_encodeOpt.encodenum;
  302. AT24CXX_Write(FRAM_ENCODEINIT,(uint8_t *)&value,1);
  303. }
  304. /*-----------------------------------------------------
  305. * 编码器的配置表读取
  306. * ----------------------------------------------------*/
  307. void fram_read_encoder_configMsg(void)
  308. {
  309. uint8_t value = 0;
  310. AT24CXX_Read(FRAM_ENCODEINIT,(uint8_t*)&value,1);
  311. g_encodeOpt.encodenum = value;
  312. }
  313. /*------------------------------------------------------
  314. * 编码器信息表存储
  315. * ------------------------------------------------------*/
  316. void fram_init_encoder_msg(void)
  317. {
  318. uint8_t i=0;
  319. uint16_t len = 0;
  320. table_msg *msg = NULL;
  321. len = sizeof(table_msg);
  322. for(i=0;i<64;i++){
  323. msg = &(g_encodeOpt.tableDa[i]);
  324. memset((char*)msg,0x00,len);
  325. AT24CXX_Write(FRAM_ENCODEMSG+i*len,(uint8_t *)msg,len);
  326. }
  327. }
  328. void fram_write_encoder_msg(void)
  329. {
  330. uint8_t i=0;
  331. uint16_t len = 0;
  332. table_msg *msg = NULL;
  333. len = sizeof(table_msg);
  334. for(i=0;i<64;i++){
  335. msg = &(g_encodeOpt.tableDa[i]);
  336. AT24CXX_Write(FRAM_ENCODEMSG+i*len,(uint8_t *)msg,len);
  337. }
  338. }
  339. /*---------------------------------------------------------
  340. * 编码器信息表的读取
  341. * -------------------------------------------------------*/
  342. void fram_read_encoder_msg(void)
  343. {
  344. uint8_t i = 0;
  345. uint16_t len = 0;
  346. table_msg *msg = NULL;
  347. len = sizeof(table_msg);
  348. for(i = 0;i < 64;i++){
  349. msg = &(g_encodeOpt.tableDa[i]);
  350. AT24CXX_Read(FRAM_ENCODEMSG+i*len,(uint8_t*)msg,len);
  351. }
  352. }