eeprom.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. #include "eeprom.h"
  2. #include "board_cfg.h"
  3. #include "gd32e23x.h"
  4. #include "common.h"
  5. #include "systick.h"
  6. #define ACK 1
  7. #define NACK 0
  8. #define IIC_WP_PIN GPIO_PIN_15
  9. #define IIC_DATA_IO GPIOF
  10. #define IIC_SDA_PIN GPIO_PIN_0
  11. #define IIC_SCL_PIN GPIO_PIN_1
  12. #define PAGE_SZIE_32 (32)
  13. #define PAGE_SIZE_16 (16)
  14. #define IIC_READ_SDA() gpio_input_bit_get(IIC_DATA_IO, IIC_SDA_PIN)
  15. #define IIC_SCL_SET gpio_bit_set(IIC_DATA_IO, IIC_SCL_PIN)
  16. #define IIC_SCL_CLR gpio_bit_reset(IIC_DATA_IO, IIC_SCL_PIN)
  17. #define IIC_SDA_SET gpio_bit_set(IIC_DATA_IO, IIC_SDA_PIN)
  18. #define IIC_SDA_CLR gpio_bit_reset(IIC_DATA_IO, IIC_SDA_PIN)
  19. #define AT24C01 127
  20. #define AT24C02 255
  21. #define AT24C04 511
  22. #define AT24C08 1023
  23. #define AT24C16 2047
  24. #define AT24C32 4095
  25. #define AT24C64 8191
  26. #define AT24C128 16383
  27. #define AT24C256 32767
  28. #define USER_DEFINE_EEPROM_TYPE AT24C64
  29. #define PAGE_SIZE PAGE_SZIE_32
  30. static void iic_init(void);
  31. static void iic_start_signal(void);
  32. static void iic_stop_signal(void);
  33. static void iic_send_byte(uint8_t data);
  34. static uint8_t iic_read_byte(uint8_t ack);
  35. static uint8_t iic_wait_ack(void);
  36. static void iic_send_ack(void);
  37. static void iic_send_nack(void);
  38. static uint8_t eeprom_read_1_byte(uint16_t addr);
  39. static void eeprom_write_1_byte(uint16_t addr,uint8_t data);
  40. static void eeprom_write(uint16_t w_addr,uint8_t *data,uint16_t lenth);
  41. static void eeprom_read(uint16_t r_addr,uint8_t *data,uint16_t lenth);
  42. static void check_threshold(uint8_t in_out,uint8_t channel);
  43. static void eeprom_page_read(uint16_t addr,uint8_t *data,uint16_t lenth);
  44. static void eeprom_write_page(uint16_t addr,uint8_t *data,uint16_t chuank);
  45. static void eeprom_auto_page_write(uint16_t addr,uint8_t *data,uint16_t lenth);
  46. static void eeprom_auto_page_read(uint16_t addr,uint8_t *data,uint16_t lenth);
  47. static inline void iic_set_sda_input()
  48. {
  49. gpio_mode_set(IIC_DATA_IO, GPIO_MODE_INPUT, GPIO_PUPD_NONE, IIC_SDA_PIN);
  50. }
  51. static inline void iic_set_sda_output()
  52. {
  53. gpio_mode_set(IIC_DATA_IO, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, IIC_SDA_PIN);
  54. gpio_output_options_set(IIC_DATA_IO, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, IIC_SDA_PIN);
  55. }
  56. void eeprom_init(void)
  57. {
  58. board_t *board = get_board();
  59. iic_init();
  60. board->read_eeprom = eeprom_read;
  61. board->write_eeprom = eeprom_write;
  62. board->check_threshold = check_threshold;
  63. board->read_eeprom_page = eeprom_auto_page_read;
  64. board->write_eeprom_page = eeprom_auto_page_write;
  65. }
  66. static void iic_init(void)
  67. {
  68. rcu_periph_clock_enable(RCU_GPIOC);
  69. gpio_mode_set(GPIOC, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_15);
  70. gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_15); /* WP */
  71. gpio_bit_reset(GPIOC, GPIO_PIN_15);
  72. rcu_periph_clock_enable(RCU_GPIOF);
  73. gpio_mode_set(IIC_DATA_IO, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_0);
  74. gpio_output_options_set(IIC_DATA_IO, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, IIC_SDA_PIN); /* SDA */
  75. gpio_mode_set(IIC_DATA_IO, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_1); /* SCL */
  76. gpio_output_options_set(IIC_DATA_IO, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, IIC_SCL_PIN);
  77. }
  78. /*****************************************************************************************
  79. * start data transport signal
  80. * 1 2 3 4
  81. * __________
  82. * SCL : __/ \_____
  83. * ________
  84. * SDA : \___________
  85. ******************************************************************************************/
  86. static void iic_start_signal(void)
  87. {
  88. iic_set_sda_output();
  89. IIC_SDA_SET;
  90. IIC_SCL_SET;
  91. DelayNus(4);
  92. IIC_SDA_CLR;
  93. DelayNus(4);
  94. IIC_SCL_CLR;
  95. }
  96. /*****************************************************************************************
  97. * stop data transport signal
  98. * 1 2 3 4
  99. * _______________
  100. * SCL : ______/
  101. * __ ____________
  102. * SDA : \______/
  103. * ^
  104. * |
  105. *
  106. ******************************************************************************************/
  107. static void iic_stop_signal(void)
  108. {
  109. iic_set_sda_output();
  110. IIC_SCL_CLR;
  111. IIC_SDA_CLR;
  112. DelayNus(4);
  113. IIC_SCL_SET;
  114. IIC_SDA_SET;
  115. DelayNus(4);
  116. }
  117. /*****************************************************************************************
  118. * iic send 1 byte
  119. * 1 2 3 4
  120. * ______
  121. * SCL: ________/ \______
  122. * ______________________
  123. * SDA: \\\___________________
  124. *
  125. ******************************************************************************************/
  126. static void iic_send_byte(uint8_t data)
  127. {
  128. iic_set_sda_output();
  129. IIC_SCL_CLR;
  130. uint8_t t;
  131. for(t = 0; t < 8; t++){
  132. if((data & 0x80) >> 7)
  133. {
  134. IIC_SDA_SET;
  135. }else
  136. {
  137. IIC_SDA_CLR;
  138. }
  139. data <<= 1;
  140. DelayNus(2);
  141. IIC_SCL_SET;
  142. DelayNus(2);
  143. IIC_SCL_CLR;
  144. DelayNus(2);
  145. }
  146. }
  147. /*****************************************************************************************
  148. * read 1 byte
  149. * 1 2 3 4
  150. * ______
  151. * SCL: ______/ \___
  152. * ____________________
  153. * SDA: \\\\______________\\\
  154. *
  155. ******************************************************************************************/
  156. static uint8_t iic_read_byte(uint8_t ack)
  157. {
  158. uint8_t i;
  159. uint8_t receive = 0;
  160. iic_set_sda_input();
  161. for(i = 0; i < 8; i++ ){
  162. IIC_SCL_CLR;
  163. DelayNus(2);
  164. IIC_SCL_SET;
  165. receive <<= 1;
  166. if(IIC_READ_SDA())
  167. {
  168. receive ++;
  169. }
  170. DelayNus(1);
  171. }
  172. if(NACK == ack)
  173. {
  174. iic_send_nack();
  175. }else{
  176. iic_send_ack();
  177. }
  178. return receive;
  179. }
  180. /*****************************************************************************************
  181. * wait ack signal
  182. * _______|____
  183. * SCL: | \_________
  184. * _______|
  185. * SDA: \_____________
  186. *
  187. ******************************************************************************************/
  188. static uint8_t iic_wait_ack(void)
  189. {
  190. uint8_t err_time = 0;
  191. iic_set_sda_input();
  192. IIC_SCL_SET;
  193. DelayNus(1);
  194. while(IIC_READ_SDA())
  195. {
  196. err_time++;
  197. if(err_time >250)
  198. {
  199. iic_stop_signal();
  200. return 1;
  201. }
  202. }
  203. IIC_SCL_CLR;
  204. return 0;
  205. }
  206. /*****************************************************************************************
  207. * send ack signal
  208. * 1 2 3 4
  209. * ______
  210. * SCL: ________/ \______
  211. * __
  212. * SDA: \___________________
  213. ******************************************************************************************/
  214. static void iic_send_ack(void)
  215. {
  216. IIC_SCL_CLR;
  217. iic_set_sda_output();
  218. IIC_SDA_CLR;
  219. DelayNus(2);
  220. IIC_SCL_SET;
  221. DelayNus(2);
  222. IIC_SCL_CLR;
  223. }
  224. /*****************************************************************************************
  225. * send nack signal
  226. * 1 2 3 4
  227. * ______
  228. * SCL: ________/ \______
  229. * ______________________
  230. * SDA: __/
  231. ******************************************************************************************/
  232. static void iic_send_nack(void)
  233. {
  234. IIC_SCL_CLR;
  235. iic_set_sda_output();
  236. IIC_SDA_SET;
  237. DelayNus(2);
  238. IIC_SCL_SET;
  239. DelayNus(2);
  240. IIC_SCL_CLR;
  241. }
  242. static uint8_t eeprom_read_1_byte(uint16_t addr)
  243. {
  244. uint8_t temp =0;
  245. iic_start_signal();
  246. if(USER_DEFINE_EEPROM_TYPE > AT24C16)
  247. {
  248. iic_send_byte(0XA0);
  249. iic_wait_ack();
  250. iic_send_byte(addr >> 8);
  251. }else
  252. {
  253. iic_send_byte(0XA0 + ((addr / 256) << 1));
  254. }
  255. iic_wait_ack();
  256. iic_send_byte(addr % 256);
  257. iic_wait_ack();
  258. iic_start_signal();
  259. iic_send_byte(0XA1);
  260. iic_wait_ack();
  261. temp = iic_read_byte(0);
  262. iic_stop_signal();
  263. return temp;
  264. }
  265. static void eeprom_write_1_byte(uint16_t addr,uint8_t data)
  266. {
  267. iic_start_signal();
  268. if(USER_DEFINE_EEPROM_TYPE > AT24C16)
  269. {
  270. iic_send_byte(0XA0);
  271. iic_wait_ack();
  272. iic_send_byte(addr >> 8);
  273. }else
  274. {
  275. iic_send_byte(0XA0 + ((addr / 256) << 1));
  276. }
  277. iic_wait_ack();
  278. iic_send_byte(addr % 256);
  279. iic_wait_ack();
  280. iic_send_byte(data);
  281. iic_wait_ack();
  282. iic_stop_signal();
  283. DelayNms(7);
  284. }
  285. static void eeprom_write(uint16_t w_addr,uint8_t *data,uint16_t lenth)
  286. {
  287. while(lenth)
  288. {
  289. eeprom_write_1_byte(w_addr,*data);
  290. w_addr++;
  291. data++;
  292. lenth--;
  293. }
  294. }
  295. static void eeprom_read(uint16_t r_addr,uint8_t *data,uint16_t lenth)
  296. {
  297. while(lenth)
  298. {
  299. *data = eeprom_read_1_byte(r_addr);
  300. data++;
  301. r_addr++;
  302. lenth--;
  303. }
  304. }
  305. static void check_threshold(uint8_t in_out,uint8_t channel)
  306. {
  307. if(in_out == THRESHOLD_IN)
  308. {
  309. board_t *board = get_board();
  310. if(channel < 3)
  311. {
  312. if(board->md_data.rw_data.rw_d.th_in[channel].th_voltage_max > Default_Voltage_Max) board->md_data.rw_data.rw_d.th_in[channel].th_voltage_max = 0;
  313. if(board->md_data.rw_data.rw_d.th_in[channel].th_voltage_min > Default_Voltage_Max) board->md_data.rw_data.rw_d.th_in[channel].th_voltage_min = 0;
  314. if(board->md_data.rw_data.rw_d.th_in[channel].th_current_max > Default_Current_Max) board->md_data.rw_data.rw_d.th_in[channel].th_current_max = 0;
  315. if(board->md_data.rw_data.rw_d.th_in[channel].th_power_max > Default_Power_Max) board->md_data.rw_data.rw_d.th_in[channel].th_power_max = 0;
  316. if(board->md_data.rw_data.rw_d.th_in[channel].th_consumer_max > Default_Consum_Max) board->md_data.rw_data.rw_d.th_in[channel].th_consumer_max = 0;
  317. if(board->md_data.rw_data.rw_d.th_in[channel].th_voltage_max <= THRESHOULD_JUDGMENT)
  318. UNSET_VOLTAGE_MAX_EN(board->in_wanning_en[channel]);
  319. else
  320. SET_VOLTAGE_MAX_EN(board->in_wanning_en[channel]);
  321. if(board->md_data.rw_data.rw_d.th_in[channel].th_voltage_min <= THRESHOULD_JUDGMENT)
  322. UNSET_VOLTAGE_MIN_EN(board->in_wanning_en[channel]);
  323. else
  324. SET_VOLTAGE_MIN_EN(board->in_wanning_en[channel]);
  325. if(board->md_data.rw_data.rw_d.th_in[channel].th_current_max <= THRESHOULD_JUDGMENT)
  326. UNSET_CURRENT_MAX_EN(board->in_wanning_en[channel]);
  327. else
  328. SET_CURRENT_MAX_EN(board->in_wanning_en[channel]);
  329. if(board->md_data.rw_data.rw_d.th_in[channel].th_power_max <= THRESHOULD_JUDGMENT)
  330. UNSET_POWER_MAX_EN(board->in_wanning_en[channel]);
  331. else
  332. SET_POWER_MAX_EN(board->in_wanning_en[channel]);
  333. if(board->md_data.rw_data.rw_d.th_in[channel].th_consumer_max <= THRESHOULD_JUDGMENT)
  334. UNSET_CONSUMER_MAX_EN(board->in_wanning_en[channel]);
  335. else
  336. SET_CONSUMER_MAX_EN(board->in_wanning_en[channel]);
  337. }
  338. }else if(in_out == THRESHOLD_OUT)
  339. {
  340. board_t *board = get_board();
  341. if(channel < RelaySlaveChaNum)
  342. {
  343. if(board->md_data.rw_data.rw_d.th_out[channel].th_voltage_max > Default_Voltage_Max) board->md_data.rw_data.rw_d.th_out[channel].th_voltage_max = 0;
  344. if(board->md_data.rw_data.rw_d.th_out[channel].th_voltage_min > Default_Voltage_Max) board->md_data.rw_data.rw_d.th_out[channel].th_voltage_min = 0;
  345. if(board->md_data.rw_data.rw_d.th_out[channel].th_current_max > Default_Current_Max) board->md_data.rw_data.rw_d.th_out[channel].th_current_max = 0;
  346. if(board->md_data.rw_data.rw_d.th_out[channel].th_power_max > Default_Power_Max) board->md_data.rw_data.rw_d.th_out[channel].th_power_max = 0;
  347. if(board->md_data.rw_data.rw_d.th_out[channel].th_consumer_max > Default_Consum_Max) board->md_data.rw_data.rw_d.th_out[channel].th_consumer_max = 0;
  348. if(board->md_data.rw_data.rw_d.th_out[channel].th_voltage_max <= THRESHOULD_JUDGMENT)
  349. UNSET_VOLTAGE_MAX_EN(board->out_wanning_en[channel]);
  350. else
  351. SET_VOLTAGE_MAX_EN(board->out_wanning_en[channel]);
  352. if(board->md_data.rw_data.rw_d.th_out[channel].th_voltage_min <= THRESHOULD_JUDGMENT)
  353. UNSET_VOLTAGE_MIN_EN(board->out_wanning_en[channel]);
  354. else
  355. SET_VOLTAGE_MIN_EN(board->out_wanning_en[channel]);
  356. if(board->md_data.rw_data.rw_d.th_out[channel].th_current_max <= THRESHOULD_JUDGMENT)
  357. UNSET_CURRENT_MAX_EN(board->out_wanning_en[channel]);
  358. else
  359. SET_CURRENT_MAX_EN(board->out_wanning_en[channel]);
  360. if(board->md_data.rw_data.rw_d.th_out[channel].th_power_max <= THRESHOULD_JUDGMENT)
  361. UNSET_POWER_MAX_EN(board->out_wanning_en[channel]);
  362. else
  363. SET_POWER_MAX_EN(board->out_wanning_en[channel]);
  364. if(board->md_data.rw_data.rw_d.th_out[channel].th_consumer_max <= THRESHOULD_JUDGMENT)
  365. UNSET_CONSUMER_MAX_EN(board->out_wanning_en[channel]);
  366. else
  367. SET_CONSUMER_MAX_EN(board->out_wanning_en[channel]);
  368. }
  369. }
  370. }
  371. static void eeprom_page_read(uint16_t addr,uint8_t *data,uint16_t lenth)
  372. {
  373. if(lenth == 0 || lenth > PAGE_SIZE || ((addr % PAGE_SIZE) + lenth > PAGE_SIZE))
  374. {
  375. return;
  376. }
  377. iic_start_signal();
  378. if(USER_DEFINE_EEPROM_TYPE > AT24C16)
  379. {
  380. iic_send_byte(0XA0);
  381. iic_wait_ack();
  382. iic_send_byte(addr >> 8);
  383. }else
  384. {
  385. iic_send_byte(0XA0 + ((addr / 256) << 1));
  386. }
  387. iic_wait_ack();
  388. iic_send_byte(addr % 256);
  389. iic_wait_ack();
  390. iic_start_signal();
  391. iic_send_byte(0XA1);
  392. iic_wait_ack();
  393. for(int i = 0; i < lenth;i++)
  394. {
  395. if(i == lenth -1)
  396. {
  397. data[i] = iic_read_byte(0);
  398. }else
  399. {
  400. data[i] = iic_read_byte(1);
  401. }
  402. }
  403. iic_stop_signal();
  404. }
  405. static void eeprom_write_page(uint16_t addr,uint8_t *data,uint16_t chuank)
  406. {
  407. if(chuank == 0 || chuank > PAGE_SIZE || ((addr % PAGE_SIZE) + chuank > PAGE_SIZE))
  408. {
  409. return;
  410. }
  411. iic_start_signal();
  412. if(USER_DEFINE_EEPROM_TYPE > AT24C16)
  413. {
  414. iic_send_byte(0XA0);
  415. iic_wait_ack();
  416. iic_send_byte(addr >> 8);
  417. }else
  418. {
  419. iic_send_byte(0XA0 + ((addr / 256) << 1));
  420. }
  421. iic_wait_ack();
  422. iic_send_byte(addr % 256);
  423. iic_wait_ack();
  424. for(uint16_t i = 0; i < chuank;i++)
  425. {
  426. iic_send_byte(data[i]);
  427. iic_wait_ack();
  428. }
  429. iic_stop_signal();
  430. DelayNms(7);
  431. }
  432. static void eeprom_auto_page_write(uint16_t addr,uint8_t *data,uint16_t lenth)
  433. {
  434. uint16_t remaining = lenth;
  435. uint8_t *p = data;
  436. while(remaining > 0)
  437. {
  438. uint8_t chunk = PAGE_SIZE - (addr % PAGE_SIZE);
  439. if(chunk > remaining) chunk = remaining;
  440. eeprom_write_page(addr, p,chunk);
  441. addr += chunk;
  442. p += chunk;
  443. remaining -= chunk;
  444. }
  445. }
  446. static void eeprom_auto_page_read(uint16_t addr,uint8_t *data,uint16_t lenth)
  447. {
  448. uint16_t remaining = lenth;
  449. uint8_t *p = data;
  450. while(remaining > 0)
  451. {
  452. uint8_t chunk = PAGE_SIZE - (addr % PAGE_SIZE);
  453. if(chunk > remaining) chunk = remaining;
  454. eeprom_page_read(addr, p, chunk);
  455. addr += chunk;
  456. p += chunk;
  457. remaining -= chunk;
  458. }
  459. }