-
Notifications
You must be signed in to change notification settings - Fork 0
/
m24m02.c
284 lines (235 loc) · 8.53 KB
/
m24m02.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
*Lib defining the M24m)@ eeeprom form ST for the Exeger Light band project .
*26/05/2014
*Copyright Exeger Systems AB 2014
*
*Author:Kartik karuna (Kartik.karuna@exeger.com)
*
*/
#include "m24m02.h"
#include "twi_master.h"
#include "nrf_delay.h"
#include "cy_io.h"
#include "defines.h"
//Device address
#define MEM_BASE_ADD 0xA0 //!< 6 MSBs of the MAX 44009 I2C ADD
uint8_t DEV_REVERSE_LOOKUP[] = { 0x08,0x0A,0x0C,0x0E };
/*
#define PAGE_0 0x08 // PAGE 0 (E2 =1 , A17=A16=0,W/R=D.C)
#define PAGE_1 0x0A // PAGE 1 (E2 =1 , A17=0 A16=1,W/R=D.C)
#define PAGE_2 0x0C // PAGE 2 (E2 =1 , A17=1 A16=0, W/R=D.C)
#define PAGE_3 0x0E // PAGE 3 (E2 =1 , A17=A16=1, W/R=D.C)
terms block and devices is used interchangeably
*/
//Memory Blocks
#define BLOCK_SIZE 0x10000
#define BLOCK_MAX 0x3FFF0 //((4*BLOCK_SIZE)-15) //0x40000 //saving last 15 bytes to save memory location.
#define BUS true //Bool valve to select bus
//static uint8_t m_device_address; //!< Device address in bits [7:1]
#define INT_STATUS 0x00
#define INT_ENABLE 0x01
#define PAGE_SIZE 0x80
extern float SCALE_FACTOR; // captures scaling factors to map from % brightness to PWM
#define WC MEM_WC
#define E2 MEM_EN
#define pointer_address 0xFFF5 //on AE device
void i2c_eeprom_init()
{
if (!twi_master_init())
{
//Wait for Initilization
}
nrf_gpio_pin_set(E2);
nrf_delay_ms(1);
}
bool i2c_eeprom_erase()
{
// initialize all bytes to 0
uint8_t data[128] = {0};
uint32_t addr = 0x0;
// Erasing EEPROM
while (addr < BLOCK_MAX)
{
i2c_eeprom_write(addr, data, sizeof(data));
addr += sizeof(data);
}
return true;
//Erase done
}
bool i2c_eeprom_write(uint32_t address, uint8_t* data, uint32_t length)
{
if (address > BLOCK_MAX) {return false;}
if (address + length > BLOCK_MAX) {return false;}
uint32_t start_byte = address;
uint32_t end_byte = address + length;
uint32_t curr_device_start = (address / BLOCK_SIZE) * BLOCK_SIZE;
uint32_t next_device_start = curr_device_start + BLOCK_SIZE;
bool success = true;
while (end_byte > next_device_start && success)
{
uint8_t dev_offset = start_byte / BLOCK_SIZE;
uint8_t dev_id = MEM_BASE_ADD | DEV_REVERSE_LOOKUP[dev_offset];
success = i2c_eeprom_write_buffer(dev_id, (uint16_t)(start_byte % BLOCK_SIZE),(uint8_t*)&(data[start_byte - address]), next_device_start - start_byte);
curr_device_start = next_device_start;
next_device_start = curr_device_start + BLOCK_SIZE;
start_byte = curr_device_start;
}
if (!success) {return false;}
uint8_t dev_offset = start_byte / BLOCK_SIZE;
uint8_t dev_id = MEM_BASE_ADD | DEV_REVERSE_LOOKUP[dev_offset];
uint16_t lenght_send = end_byte - start_byte;
return i2c_eeprom_write_buffer(dev_id, (uint16_t)(start_byte % BLOCK_SIZE),
(uint8_t*)&(data[start_byte - address]),lenght_send);
}
bool i2c_eeprom_write_buffer(uint8_t dev_id, uint16_t address, uint8_t* data, uint16_t length) {
uint16_t start_byte = address;
uint16_t end_byte = address + length;
uint16_t curr_page_start = (address / PAGE_SIZE) * PAGE_SIZE;
uint32_t next_page_start = curr_page_start + PAGE_SIZE;
print("i2c %d %d %d %d \r\n",start_byte,end_byte,curr_page_start,next_page_start );
bool success = true;
while (end_byte > next_page_start && success)
{
success = i2c_eeprom_write_page(dev_id, start_byte, &(data[start_byte - address]), next_page_start - start_byte);
print("i2c page overflow \r\n");
curr_page_start = next_page_start;
next_page_start = curr_page_start + PAGE_SIZE;
start_byte = curr_page_start;
}
if (!success) {return false; }
uint16_t lenght_send = end_byte - start_byte;
return i2c_eeprom_write_page(dev_id, start_byte, &(data[start_byte - address]), lenght_send);
}
bool i2c_eeprom_write_page(uint8_t dev_id, uint16_t eeaddress, uint8_t* data, uint8_t length )
{
nrf_gpio_pin_clear(WC);
uint8_t buffer_len=length+2;
uint8_t data_buffer[130];
uint8_t add_high=(uint8_t)((eeaddress >> 8) &0xFF);
data_buffer[0]=add_high;
uint8_t add_low=(uint8_t)(eeaddress & 0xFF);
data_buffer[1]=add_low;
uint16_t i;
for (i=0;i<length;i++)
{
data_buffer[i+2]= *(data+i);
}
if (twi_master_transfer(dev_id,data_buffer,buffer_len,TWI_ISSUE_STOP))
{
nrf_gpio_pin_set(WC);
nrf_delay_ms(10);
return true;
}
else
{
nrf_gpio_pin_set(WC);
return false;
}
}
/*
uint32_t i2c_eeprom_current_address()
{
uint8_t device_address,i; = MEM_BASE_ADD | DEV_REVERSE_LOOKUP[dev_offset];
uint16_t address_pointer[4];
uint32_t return_address;
for (i=0;1<4;i++)
{
device_address = MEM_BASE_ADD | DEV_REVERSE_LOOKUP[i];
address_pointer[i]=i2c_eeprom_address_read(device_address)
}
return return_address;
}
uint16_t i2c_eeprom_address_read(uint8_t dev_id)
{
if(twi_master_transfer(dev_id|TWI_READ_BIT,&read_byte,1,TWI_ISSUE_STOP))
{
}
}
*/
uint8_t i2c_eeprom_read_byte(uint8_t dev_id, uint16_t eeaddress )
{
nrf_gpio_pin_set(WC);
uint8_t read_byte = 0xDB;
uint8_t add_buffer[2];
add_buffer[0]=(uint8_t)((eeaddress >> 8) &0xFF);
add_buffer[1]=(uint8_t)(eeaddress & 0xFF);
if(twi_master_transfer(dev_id,add_buffer,2,TWI_DONT_ISSUE_STOP))
{
if(twi_master_transfer(dev_id|TWI_READ_BIT,&read_byte,1,TWI_ISSUE_STOP))
{
//Read successful
}
}
return read_byte;
}
bool i2c_eeprom_read(uint32_t address, uint8_t* data, uint32_t length) {
if (address > BLOCK_MAX) {return false;}
if (address + length > BLOCK_MAX) {return false;}
uint32_t start_byte = address;
uint32_t end_byte = address + length;
uint32_t curr_device_start = (address / BLOCK_SIZE) * BLOCK_SIZE;
uint32_t next_device_start = curr_device_start + BLOCK_SIZE;
bool success = true;
while (end_byte > next_device_start && success)
{
uint8_t dev_offset = start_byte / BLOCK_MAX;
uint8_t dev_id = (MEM_BASE_ADD | DEV_REVERSE_LOOKUP[dev_offset]);
success = i2c_eeprom_read_buffer(dev_id, (uint16_t)(start_byte % BLOCK_SIZE), (uint8_t*)&(data[start_byte - address]), next_device_start - start_byte);
curr_device_start = next_device_start;
next_device_start = curr_device_start + BLOCK_SIZE;
start_byte = curr_device_start;
}
if (!success)
{
return false;
}
uint8_t dev_offset = start_byte / BLOCK_SIZE;
uint8_t dev_id = MEM_BASE_ADD | DEV_REVERSE_LOOKUP[dev_offset];
return i2c_eeprom_read_buffer(dev_id, (uint16_t)(start_byte % BLOCK_SIZE), (uint8_t*)&(data[start_byte - address]), (uint16_t)(end_byte - start_byte));
}
bool i2c_eeprom_read_buffer(uint8_t dev_id, uint16_t address, uint8_t *buffer, uint16_t length )
{
bool success;
nrf_gpio_pin_set(WC);
nrf_delay_ms(5);
uint8_t add_buffer[2];
add_buffer[0]=(uint8_t)((address >> 8) &0xFF);
add_buffer[1]=(uint8_t)(address & 0xFF);
if(twi_master_transfer(dev_id,add_buffer,2,TWI_DONT_ISSUE_STOP))
{
if(twi_master_transfer(dev_id|TWI_READ_BIT,buffer,length,TWI_ISSUE_STOP))
{
success= true;
}
}
else
{
success = false;
}
return success;
}
uint32_t eeprom_find_add_pointer(void)
{
uint8_t dev_id = MEM_BASE_ADD | DEV_REVERSE_LOOKUP[3];
uint8_t pointer_address_buffer[4];
uint8_t length = 4;
if(i2c_eeprom_read_buffer(dev_id,(uint16_t)pointer_address,(uint8_t*)&pointer_address_buffer[0], (uint16_t)length))
{
uint32_t t_buffer = ((((uint32_t)pointer_address_buffer[0])<<24)|(((uint32_t)pointer_address_buffer[1])<<16)|(((uint32_t)pointer_address_buffer[2])<<8)|(((uint32_t)pointer_address_buffer[3]))); ;
// t_buffer = (uint32_t)pointer_address_buffer;
return t_buffer;
}
return ((uint32_t)0);
}
bool eeprom_updateadd_pointer(uint32_t address)
{
uint8_t dev_id = MEM_BASE_ADD | DEV_REVERSE_LOOKUP[3];
uint8_t pointer_address_buffer[4];
uint8_t lenght_send = sizeof(uint32_t);
//conver 32 bit address in to 8 bytes chunks.
pointer_address_buffer[0]=(uint8_t)((address >> 24) &0x000000FF);
pointer_address_buffer[1]=(uint8_t)((address >> 16) &0x000000FF);
pointer_address_buffer[2]=(uint8_t)((address >> 8) &0x000000FF);
pointer_address_buffer[3]=(uint8_t)(address & 0x000000FF);
return i2c_eeprom_write_page(dev_id,(uint16_t)pointer_address,pointer_address_buffer,lenght_send);
}