-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlpc_i2c_m24256.c
278 lines (224 loc) · 7.29 KB
/
lpc_i2c_m24256.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
/******************************************************************//**
* @file lpc_i2c_m24256.c
* @brief Contains all functions support for I2C based E2PROM
* M24256-BWMN6P library on LPC17xx
* @version 1.0
* @date 11. Dec. 2013
* @author Dwijay.Edutech Learning Solutions
**********************************************************************/
/* Peripheral group ----------------------------------------------------------- */
/** @addtogroup EEPROM
* @{
*/
/* Includes ------------------------------------------------------------------- */
#include "lpc_i2c_m24256.h"
/* If this source file built with example, the LPC17xx FW library configuration
* file in each example directory ("lpc17xx_libcfg.h") must be included,
* otherwise the default FW library configuration file must be included instead
*/
/** @addtogroup EEPROM_Public_Functions
* @{
*/
/* Public Functions ----------------------------------------------------------- */
/*********************************************************************//**
* @brief Writes byte at given address
* @param[in] eep_address Word Address range[0000 - 4000]
* @param[in] byte_data Byte value
* @return status
**********************************************************************/
char I2C_IEeprom_Write_Byte (uint16_t eep_address, uint8_t byte_data)
{
/* Transmit setup */
I2C_M_SETUP_Type txsetup;
I2C_Tx_Buf[0] =(eep_address & 0x8FFF) >> 8; // 1st byte extract
// printf(LPC_UART0,"%x02",set_addr);
I2C_Tx_Buf[1] = (uchar)(eep_address & 0xFF); // 2st byte extract
I2C_Tx_Buf[2] = byte_data;
txsetup.sl_addr7bit = E2PM24256_ID;
txsetup.tx_data = I2C_Tx_Buf;
txsetup.tx_length = 3;
txsetup.rx_data = NULL;
txsetup.rx_length = 0;
txsetup.retransmissions_max = 50;
/* write byte to addr */
if(I2C_MasterTransferData(LPC_I2C0, &txsetup, I2C_TRANSFER_POLLING)==SUCCESS) //return status
{
return (0);
}
else
{
return (-1);
}
}
/*********************************************************************//**
* @brief Writes byte at given address
* @param[in] eep_address Word Address range[0000 - 4000]
* @param[in] byte_data Byte value
* @return status
**********************************************************************/
char I2C_IEeprom_Write (uint16_t eep_address, uint8_t* byte_data, uint16_t length)
{
/* Transmit setup */
I2C_M_SETUP_Type txsetup;
uint16_t ip_len,new_length,i;
uint16_t new_address;
/* Intern page length(ip_len) gives length from address that can be occupied in page */
ip_len = 0x40 - (eep_address % 0x40);
new_address = eep_address + ip_len;
I2C_Tx_Buf[0] =(eep_address & 0x8FFF) >> 8; // 1st byte extract
// printf(LPC_UART0,"%x02",I2C_Tx_Buf[0]);
I2C_Tx_Buf[1] = (uchar)(eep_address & 0xFF); // 2st byte extract
if(length > ip_len)
{
new_length = length - ip_len;
for(i=2;i<(ip_len+2);i++)
{
I2C_Tx_Buf[i]=*byte_data++;
}
txsetup.sl_addr7bit = E2PM24256_ID;
txsetup.tx_data = I2C_Tx_Buf;
txsetup.tx_length = ip_len+2;
txsetup.rx_data = NULL;
txsetup.rx_length = 0;
txsetup.retransmissions_max = 50;
if(I2C_MasterTransferData(LPC_I2C0, &txsetup, I2C_TRANSFER_POLLING)==ERROR) //return status
{
return (-1);
}
// Recursive function
delay_ms(5);
I2C_IEeprom_Write(new_address,byte_data,new_length);
}
if(length <= ip_len)
{
for(i=2;i<(length+2);i++)
{
I2C_Tx_Buf[i]=*byte_data++;
}
txsetup.sl_addr7bit = E2PM24256_ID;
txsetup.tx_data = I2C_Tx_Buf;
txsetup.tx_length = length+2;
txsetup.rx_data = NULL;
txsetup.rx_length = 0;
txsetup.retransmissions_max = 50;
if(I2C_MasterTransferData(LPC_I2C0, &txsetup, I2C_TRANSFER_POLLING)==SUCCESS) //return status
{
return (0);
}
else
{
return (-1);
}
}
}
/*********************************************************************//**
* @brief Reads byte from given address
* @param[in] eep_address Word Address range[0000 - 4000]
* @return Byte value
**********************************************************************/
uint8_t I2C_IEeprom_Read_Byte (uint16_t eep_address)
{
/* Receive setup */
I2C_M_SETUP_Type rxsetup;
I2C_Tx_Buf[0] = (eep_address & 0x8FFF) >> 8; // 1st byte extract
// printf(LPC_UART0,"%x02",set_addr);
I2C_Tx_Buf[1] = (uchar)(eep_address & 0xFF); // 2st byte extract
rxsetup.sl_addr7bit = E2PM24256_ID;
rxsetup.tx_data = I2C_Tx_Buf; // Get address to read at writing address
rxsetup.tx_length = 2;
rxsetup.rx_data = I2C_Rx_Buf;
rxsetup.rx_length = 1;
rxsetup.retransmissions_max = 3;
if (I2C_MasterTransferData(LPC_I2C0, &rxsetup, I2C_TRANSFER_POLLING) == SUCCESS)
{
return (I2C_Rx_Buf[0]);
}
else
{
return (-1);
}
}
/*********************************************************************//**
* @brief Reads array from given address
* @param[in] eep_address Word Address range[0000 - 4000]
* @return Byte value
**********************************************************************/
char I2C_IEeprom_Read (uint16_t eep_address, uint8_t* buf_data, uint16_t length)
{
/* Receive setup */
I2C_M_SETUP_Type rxsetup;
I2C_Tx_Buf[0] = (eep_address & 0x8FFF) >> 8;
// printf(LPC_UART0,"%x02",set_addr);
I2C_Tx_Buf[1] = (uchar)(eep_address & 0xFF); // 2st byte extract
rxsetup.sl_addr7bit = E2PM24256_ID;
rxsetup.tx_data = I2C_Tx_Buf; // Get address to read at writing address
rxsetup.tx_length = 2;
rxsetup.rx_data = buf_data;
rxsetup.rx_length = length;
rxsetup.retransmissions_max = 3;
if (I2C_MasterTransferData(LPC_I2C0, &rxsetup, I2C_TRANSFER_POLLING) == SUCCESS)
{
return (0);
}
else
{
return (-1);
}
}
/*********************************************************************//**
* @brief Display Read data stored in array
* @param[in] *dest_addr buffer address
* @param[in] length size of buffer
* @return None
**********************************************************************/
void Display_IEeprom_Array (uint8_t *string, uint16_t length)
{
while(length)
{
printf(LPC_UART0,"%x02 ",*string++);
length--;
}
}
/*********************************************************************//**
* @brief Ask for Address Range you want to display
* @param[in] *dest_addr buffer address
* @param[in] length size of buffer
* @return None
**********************************************************************/
void Display_IEeprom_Loc (uint16_t mem_start_address, uint16_t mem_end_address)
{
uint8_t line=0,count=0;
uint8_t dat;
uint16_t addr;
printf(LPC_UART0,"EEPROM Range = 0x000 - 0x7FF \r\n");
clr_scr_rst_cur(LPC_UART0);
printf(LPC_UART0,"Start: %x03 End: %x03 \r\n",mem_start_address,mem_end_address);
for(addr=mem_start_address; addr<mem_end_address+1; addr++)
{
if(count == 0) printf(LPC_UART0,"%x03 ",addr);
dat = I2C_IEeprom_Read_Byte(addr); // read byte from address
printf(LPC_UART0,"%x02 ",dat);
count++;
if(count == 16) // check for last digit entered
{
line++;
count = 0;
printf(LPC_UART0,"\r\n");
}
if(line == 16 || addr == mem_end_address)
{
printf(LPC_UART0,"\x1b[24;01HPress any key to continue.");
line = 0;
getche(LPC_UART0);
clr_scr_rst_cur(LPC_UART0);
}
}
}
/**
* @}
*/
/* End of Public Functions ---------------------------------------------------- */
/**
* @}
*/
/* --------------------------------- End Of File ------------------------------ */