LTlib LaurTec Library  4.0.3
Open Source C Library for Microchip Microcontrollers based on XC8 Compiler
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
I2C_EEPROM.c
Go to the documentation of this file.
1 /*******************************************************************************
2 
3 Author : Mauro Laurenti
4 Version : 1.5
5 
6 Created on Date : 04/09/2006
7 Last update : 16/03/2016
8 
9 CopyRight 2006-2015 all rights are reserved
10 
11 ********************************************************
12 SOFTWARE LICENSE AGREEMENT
13 ********************************************************
14 
15 The usage of the supplied software imply the acceptance of the following license.
16 
17 The software supplied herewith by Mauro Laurenti (the Author) is intended for
18 use solely and exclusively on Microchip PIC Microcontroller (registered mark).
19 The software is owned by the Author, and is protected under applicable
20 copyright laws. All rights are reserved.
21 Any use in violation of the foregoing restrictions may subject the
22 user to criminal sanctions under applicable laws, as well as to civil liability
23 for the breach of the terms and conditions of this license.
24 Commercial use is forbidden without a written acknowledgement with the Author.
25 Personal or educational use is allowed if the application containing the
26 following software doesn't aim to commercial use or monetary earning of any kind.
27 
28 THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES,
29 WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
30 TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
31 PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE AUTHOR SHALL NOT,
32 IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
33 CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
34 
35 
36 ********************************************************
37 PURPOSES
38 ********************************************************
39 
40 These functions allow the user to read/write inside the eeprom
41 that requires 2 address bytes such as the 24LC512.
42 
43 ********************************************************************************/
44 
45 #ifdef __XC8
46  #include <xc.h>
47 #endif
48 
49 #include "I2C_EEPROM.h"
50 
51 //************************************************************
52 // I2C_EEPROM_initialize function implementation
53 //************************************************************
54 void I2C_EEPROM_initialize (unsigned int baud_rate_KHz){
55 
57  EEPROM_I2C_baud_rate (baud_rate_KHz);
58 }
59 
60 
61 
62 //************************************************************
63 // I2C_EEPROM_write_byte function implementation
64 //************************************************************
65 
66 signed char I2C_EEPROM_write_byte ( unsigned char control, unsigned int address, unsigned char data ){
67 
68  unsigned char addressH;
69  unsigned char addressL;
70 
71  addressH = (unsigned char) ((address & 0xFF00) >> 8);
72  addressL = (unsigned char) (address & 0x00FF);
73 
74 //*****************************
75 // Start Condition and control
76 // Byte are sent
77 //*****************************
78 
81 
82  // Check for bus collision
84  return (-1);
85  }
86 
87  // Write control byte - R/W bit should be 0
88  if (EEPROM_I2C_write_byte(control)){
89  // Return with write Collision error
90  return (-3);
91  }
92 
93 //*****************************
94 // Address High is sent
95 //*****************************
97 
98  if (!EEPROM_I2C_check_ACK()){
99 
100  if (EEPROM_I2C_write_byte(addressH)){
101  // Return with write Collision error
102  return (-3);
103  }
104  } else {
105  // Return with Not Ack error condition
106  return (-2);
107  }
108 
109 //*****************************
110 // Address Low is sent
111 //*****************************
112 
114 
115  // Check if ACK condition has been received
116  if (!EEPROM_I2C_check_ACK()) {
117 
118  // Write address low to the EEPROM
119  if (EEPROM_I2C_write_byte(addressL)) {
120  // Return with write Collision error
121  return (-3);
122  }
123  } else {
124  // Return with Not Ack error condition
125  return (-2);
126  }
127 
128 //*****************************
129 // Data Byte is sent
130 //*****************************
131 
133 
134  if (!EEPROM_I2C_check_ACK()){
135 
136  if (EEPROM_I2C_write_byte(data)) {
137  // Return with write Collision error
138  return (-3);
139  }
140  } else {
141  // Return with Not Ack error condition
142  return (-2);
143  }
144 
145 //*****************************
146 // Stop command is sent
147 //*****************************
148 
150 
151  if (!EEPROM_I2C_check_ACK()) {
152 
154 
155  } else {
156  // Return with Not Ack error condition
157  return (-2);
158  }
159 
161  return (-1);
162  }
163 
164  // Return with no error
165  return (0);
166 }
167 
168 
169 //************************************************************
170 // I2C_EEPROM_write_byte_check function implementation
171 //************************************************************
172 
173 signed char I2C_EEPROM_write_byte_check (unsigned char control, unsigned int address, unsigned char data ) {
174 
175  // Store the returned error from the read and write function call
176  signed char error;
177 
178  // Store the data read back from the EEPROM
179  unsigned char dataReadback;
180 
181  error = I2C_EEPROM_write_byte(control, address, data);
182 
183  if (error < 0)
184  return (error);
185 
186  // wait for the write process to be completed
187  delay_ms (6);
188 
189  error = I2C_EEPROM_read_byte (control, address, &dataReadback);
190 
191  if (error < 0)
192  return (error);
193 
194  if (dataReadback == data)
195  return (1);
196  else
197  return(-4);
198 }
199 
200 
201 //************************************************************
202 // I2C_EEPROM_read_byte function implementation
203 //************************************************************
204 
205 signed char I2C_EEPROM_read_byte (unsigned char control, unsigned int address, unsigned char *data) {
206 
207  unsigned char addressH;
208  unsigned char addressL;
209 
210  addressH = (unsigned char) ((address & 0xFF00) >> 8);
211  addressL = (unsigned char) (address & 0x00FF);
212 
213 
214 //*****************************
215 // Start Condition and control
216 // Byte are sent
217 //*****************************
218 
219 
221 
223 
225  return (-1);
226  }
227 
228  if (EEPROM_I2C_write_byte(control)){
229  // Return with write collision error
230  return (-3);
231  }
232 
233 //*****************************
234 // Address High is sent
235 //*****************************
236 
238 
239  // Check if ACK condition has been received
240  if (!EEPROM_I2C_check_ACK()) {
241 
242  if (EEPROM_I2C_write_byte(addressH)){
243  // Return with write collision error
244  return (-3);
245  }
246  }else {
247  // Return with Not Ack error condition
248  return (-2);
249  }
250 
251 //*****************************
252 // Address Low is sent
253 //*****************************
255 
256  // Check if ACK condition has been received
257  if (!EEPROM_I2C_check_ACK()) {
258 
259  if (EEPROM_I2C_write_byte(addressL)) {
260  // Return with write collision error
261  return (-3);
262  }
263  }else {
264  // Return with Not Ack error condition
265  return (-2);
266  }
267 
268 
269 //*****************************
270 // Restart condition
271 //*****************************
273 
274  if (!EEPROM_I2C_check_ACK()){
275 
277 
279  return (-1);
280  }
281 
282  // Write Control byte - This time R/W bit should be 1
283  if (EEPROM_I2C_write_byte(control+1)) {
284  // Return with write collision error
285  return (-3);
286  }
287 
288 //*****************************
289 // Data is Read
290 //*****************************
291 
293 
294  // Check if ACK condition has been received
295  if (!EEPROM_I2C_check_ACK()){
296 
298 
300 
302 
303  // Check if Bus collision happened
305  return (-1);
306  }
307 
308  } else {
309  // Return with Not Ack error
310  return (-2);
311  }
312 
313  } else {
314  // Return with Not Ack error
315  return (-2);
316  }
317 
318  // Data is read from the buffer
319  *data = EEPROM_I2C_read_byte();
320 
321  // No error occurred
322  return (0);
323 }
324 
325