-
Notifications
You must be signed in to change notification settings - Fork 28
/
eeprom.c
57 lines (44 loc) · 1.13 KB
/
eeprom.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "ch554_platform.h"
#include "i2c.h"
// For 24LC512, A0=0, A1=0, A2=0, WP=Floating
#define EEPROM_ADDR 0xA0
void EEPROM_Read(uint8_t* buf, uint8_t AddrH, uint8_t AddrL, uint8_t length) {
do {
I2C_Send_Start();
I2C_Buf = EEPROM_ADDR;
I2C_WriteByte();
} while (I2C_Buf); // Wait for pending operation to complete
I2C_Buf = AddrH;
I2C_WriteByte(); // Address H
I2C_Buf = AddrL;
I2C_WriteByte(); // Address L
I2C_Send_Start();
I2C_Buf = EEPROM_ADDR|1; //Read
I2C_WriteByte();
AddrH = length-1; // Length
for (AddrL=0; AddrL<AddrH; AddrL++) {
I2C_ReadByte();
buf[AddrL] = I2C_Buf;
I2C_Send_ACK();
}
I2C_ReadByte();
buf[AddrH] = I2C_Buf;
I2C_Send_NACK();
I2C_Send_Stop();
}
void EEPROM_Write(uint8_t* buf, uint8_t AddrH, uint8_t AddrL, uint8_t length) {
do {
I2C_Send_Start();
I2C_Buf = EEPROM_ADDR;
I2C_WriteByte();
} while (I2C_Buf); // Wait for pending operation to complete
I2C_Buf = AddrH;
I2C_WriteByte(); // Address H
I2C_Buf = AddrL;
I2C_WriteByte(); // Address L
for (AddrL=0; AddrL<length; AddrL++) {
I2C_Buf = buf[AddrL];
I2C_WriteByte(); // Data byte
}
I2C_Send_Stop();
}