| Linduino 1.3.0 Linear Technology Arduino-Compatible Demonstration Board

Serial_EEPROM_Test.ino
Go to the documentation of this file.
1 /*
2 Serial EEPROM Test
3
4 This program will demonstrate how to communicate with a serial EEPROM
5 such as the 24LC025 device found on all QuikEval compatible demo board.
6
7 The routines require the use of the Arduino's hardware I2C port.
8
9 Pin Assignments:
10 SDA PINC4 ATMega328 pin 27. Arduino pin A4. QuikEval pin 9.
11 SCL PINC5 ATMega328 pin 28. Arduino pin A5. QuikEval pin 11.
12
13 DATA TYPES:
14 char = 1 byte
15 int = 2 bytes
16 long = 4 bytes
17 float = 4 bytes
18
19
20 Copyright 2018(c) Analog Devices, Inc.
21
22 All rights reserved.
23
24 Redistribution and use in source and binary forms, with or without
25 modification, are permitted provided that the following conditions are met:
26 - Redistributions of source code must retain the above copyright
27 notice, this list of conditions and the following disclaimer.
28 - Redistributions in binary form must reproduce the above copyright
29 notice, this list of conditions and the following disclaimer in
30 the documentation and/or other materials provided with the
31 distribution.
32 - Neither the name of Analog Devices, Inc. nor the names of its
33 contributors may be used to endorse or promote products derived
34 from this software without specific prior written permission.
35 - The use of this software may or may not infringe the patent rights
36 of one or more patent holders. This license does not release you
37 from the requirement that you obtain separate licenses from these
38 patent holders to use this software.
39 - Use of the software either in source or binary form, must be run
40 on or directly connected to an Analog Devices Inc. component.
41
42 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
43 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
44 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
45 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
46 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
48 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
51 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 */
53
54 #define EEPROM_PAGE_SIZE 16 // EEPROM Page size in bytes
55 #define EEPROM_DATA_SIZE 256 // EEPROM size in bytes
56
57 #include <Arduino.h> // Arduino specific header file
58 #include <stdint.h>
59 #include "Linduino.h"
60 #include "LT_I2C.h" // Hardware I2C driver
61 #include "QuikEval_EEPROM.h"
62 #include "UserInterface.h" // serial interface routines to communicate with the user
63
64 // global variables
65 uint8_t i2c_address = 0xA0; // The I2C address of the QuikEval EEPROM
66 unsigned char id_string_size = 50; // The QuikEval ID string size
67 uint16_t eeprom_address = 0x00; // The eeprom data address
68 byte terminator = 0x0a; // The termination char for buffer reads
69 byte read_count = id_string_size; // Byte read count for buffer reads
70 int int_data; // Integer test variable
71 long long_data; // Long test variable
72 float float_data; // Float test variable
73
74 // user command
75 unsigned char user_command;
76
77 void print_title()
78 // Print the title block
79 {
80 Serial.print(F("\n*****************************************************************\n"));
81 Serial.print(F("* QuikEval Demo Board EEPROM Reader *\n"));
82 Serial.print(F("* *\n"));
83 Serial.print(F("* This program will read the ID string from a QuikEval *\n"));
84 Serial.print(F("* compatible demo board using the Arduino's Hardware I2C port. *\n"));
85 Serial.print(F("* *\n"));
86 Serial.print(F("* Input Data Format : *\n"));
87 Serial.print(F("* decimal : 1024 *\n"));
88 Serial.print(F("* hex : 0x400 *\n"));
89 Serial.print(F("* octal : 02000 (leading 0 'Zero') *\n"));
90 Serial.print(F("* binary : B10000000000 *\n"));
91 Serial.print(F("* float : 1024.0 *\n"));
92 Serial.print(F("* *\n"));
93 Serial.print(F("* Set the baud rate to 115200 select the newline terminator. *\n"));
94 Serial.print(F("* *\n"));
95 }
96
97 void print_prompt()
98 // Prompt the user for an input command
99 {
100 Serial.print(F("*****************************************************************\n"));
101 Serial.println("Present Values:");
102 // I2C address
103 Serial.print(" I2C Address: 0x");
104 Serial.println(i2c_address, HEX);
105 // EEPROM data address
106 Serial.print(" EEPROM Address: 0x");
107 Serial.println(eeprom_address, HEX);
108 // EEPROM terminator
109 Serial.print(" Buffer Read Terminator: 0x");
110 Serial.println(terminator, HEX);
111 // EEPROM buffer Read Count
112 Serial.print(" Buffer Read Count: ");
113 Serial.println(read_count);
114 // Print the Command Summary
115 Serial.println("\nCommand Summary:");
116 Serial.println(" 0-Buffer Read");
117 Serial.println(" 1-Buffer Read with Terminator");
118 Serial.println(" 2-Byte Read");
119 Serial.println(" 3-Buffer Write");
120 Serial.println(" 4-Byte Write");
121 Serial.println(" 5-Set I2C Address");
122 Serial.println(" 6-Set EEPROM Address");
123 Serial.println(" 7-Set Terminator");
124 Serial.println(" 8-Set Read Count");
125 Serial.println(" 9-Integer Write");
126 Serial.println(" 10-Integer Read");
127 Serial.println(" 11-Long Write");
128 Serial.println(" 12-Long Read");
129 Serial.println(" 13-Float Write");
130 Serial.println(" 14-Float Read");
131 Serial.println(" 15-Clear Calibration Settings On Demo Boards");
132 Serial.println("");
133 Serial.print("Enter a command: ");
134 }
135
136 void setup()
137 // Setup the program
138 {
139 i2c_enable(); // Configure the Linduino for 100kHz I2C communications
140 Serial.begin(115200); // Initialize the serial port to the PC
141 print_title();
142 print_prompt();
143 }
144
145 void loop()
146 {
147 unsigned char user_command;
148 char data;
149 byte byte_count;
150 if (Serial.available())
151 {
152 user_command = read_int();
153 Serial.println(user_command);
154 switch (user_command)
155 {
156 case 0:
157 // buffer read
158 byte_count = eeprom_read_buffer(i2c_address, &ui_buffer[0], eeprom_address, read_count);
159 if (byte_count != 0)
160 {
161 Serial.print("Data Received: ");
162 Serial.println(ui_buffer);
163 }
164 else
165 Serial.println("Error: No Data Received");
166 break;
167 case 1:
168 // buffer read with terminator
169 byte_count = eeprom_read_buffer_with_terminator(i2c_address, &ui_buffer[0], eeprom_address, terminator, read_count);
170 if (byte_count != 0)
171 {
172 Serial.print("Data Received: ");
173 Serial.println(ui_buffer);
174 }
175 else
176 Serial.println("Error: No Data Received");
177 break;
178 case 2:
179 // byte read
180 byte_count = eeprom_read_byte(i2c_address, &data, eeprom_address);
181 Serial.println("test");
182 if (byte_count != 0)
183 {
184 Serial.print("Byte Received: 0x");
185 Serial.print(data, HEX);
186 Serial.print("(");
187 Serial.print(char(ui_buffer[0]));
188 Serial.println(")");
189 }
190 else
191 Serial.println("Error: No Data Received");
192 break;
193 case 3:
194 // buffer write
195 Serial.print("Enter string to Write to EEPROM: ");
196 read_string();
197 byte_count = eeprom_write_buffer(i2c_address, &ui_buffer[0], eeprom_address);
198 if (byte_count == strlen(ui_buffer))
199 {
200 Serial.println(ui_buffer);
201 }
202 else
203 Serial.println("Data Write Error");
204 break;
205 case 4:
206 // byte write
207 Serial.print("Enter Data Byte to Write to EEPROM: ");
208 data = read_int();
209 byte_count = eeprom_write_byte(i2c_address, data, eeprom_address);
210 if (byte_count != 0)
211 {
212 Serial.print("0x");
213 Serial.println(data, HEX);
214 }
215 else
216 Serial.println("Data Write Error");
217 break;
218 case 5:
219 // I2C Address
220 Serial.print("Enter I2C Address: ");
221 i2c_address = read_int();
222 Serial.println(i2c_address, HEX);
223 break;
224 case 6:
225 // EEPROM Address
226 Serial.print("Enter EEPROM Address: ");
227 eeprom_address = read_int();
228 if (eeprom_address >= EEPROM_DATA_SIZE) eeprom_address = EEPROM_DATA_SIZE-1;
229 Serial.println(eeprom_address, HEX);
230 break;
231 case 7:
232 // Terminator
233 Serial.print("Enter Terminator: ");
234 terminator = read_int();
235 Serial.println(terminator, HEX);
236 break;
237 case 8:
238 // Read Count
239 Serial.print("Enter Read Count: ");
240 read_count = read_int();
241 Serial.println(read_count);
242 break;
243 case 9:
244 // Write Integer
245 Serial.print("Enter Integer To Write (-32,768 to 32,767): ");
246 int_data = read_int();
247 byte_count = eeprom_write_int16(i2c_address, int_data, eeprom_address);
248 if (byte_count != 0)
249 {
250 Serial.println(int_data);
251 }
252 else
253 Serial.println("Data Write Error");
254 break;
255 case 10:
256 // Read Integer from EEPROM
257 byte_count = eeprom_read_int16(i2c_address, &int_data, eeprom_address);
258 if (byte_count != 0)
259 {
260 Serial.print("Integer Read From EEPROM:");
261 Serial.println(int_data);
262 }
263 else
264 Serial.println("Data Read Error");
265 break;
266 case 11:
267 // Write Long
268 Serial.print("Enter Long To Write (-2,147,483,648 to 2,147,483,647): ");
269 long_data = read_int();
270 byte_count = eeprom_write_int32(i2c_address, long_data, eeprom_address);
271 if (byte_count != 0)
272 {
273 Serial.println(long_data);
274 }
275 else
276 Serial.println("Data Write Error");
277 break;
278 case 12:
279 // Read Long from EEPROM
280 byte_count = eeprom_read_int32(i2c_address, &long_data, eeprom_address);
281 if (byte_count != 0)
282 {
283 Serial.print("Long Read From EEPROM:");
284 Serial.println(long_data);
285 }
286 else
287 Serial.println("Data Read Error");
288 break;
289 case 13:
290 // Write Float
291 Serial.print("Enter Float To Write: ");
292 float_data = read_float();
293 byte_count = eeprom_write_float(i2c_address, float_data, eeprom_address);
294 if (byte_count != 0)
295 {
296 Serial.println(float_data, 6);
297 }
298 else
299 Serial.println("Data Write Error");
300 break;
301 case 14:
302 // Read Float from EEPROM
303 byte_count = eeprom_read_float(i2c_address, &float_data, eeprom_address);
304 if (byte_count != 0)
305 {
306 Serial.print("Float Read From EEPROM: ");
307 Serial.println(float_data, 6);
308 }
309 else
310 Serial.println("Data Read Error");
311 break;
312 case 15:
313 // Clear Calibration Settings That are stored on the Demo boards EEPROM
314 Serial.println("Do You Want to Clear Calibration on the Demo Board?");
315 Serial.println("1-Clear Calibration\n");
316 Serial.println("0-Keep Calibration\n");
317 Serial.print("Enter a Command: ");
318 user_command = read_int();
319
320 if (user_command == 1)
321 {
322 // Clear ALL
323 for (int i = 0; i < 0x7FF; i++)
324 eeprom_write_byte(EEPROM_I2C_ADDRESS, 0x0FF, (EEPROM_CAL_STATUS_ADDRESS + i));
325 Serial.print("Calibration Cleared\n");
326 }
327 else
328 {
329 Serial.print("Calibration NOT Cleared\n");
330 }
331 }
332 print_prompt();
333 }
334 }
eeprom_read_int16
uint8_t eeprom_read_int16(uint8_t i2c_address, int16_t *read_data, uint16_t address)
Read the two byte integer data from the EEPROM starting at address.
Definition: QuikEval_EEPROM.cpp:395
user_command
unsigned char user_command
Definition: Serial_EEPROM_Test.ino:75
EEPROM_I2C_ADDRESS
#define EEPROM_I2C_ADDRESS
Definition: QuikEval_EEPROM.h:60
eeprom_write_byte
uint8_t eeprom_write_byte(uint8_t i2c_address, char data, uint16_t address)
Write the data byte to the EEPROM with i2c_address starting at EEPROM address.
Definition: QuikEval_EEPROM.cpp:214
int_data
static int int_data
Definition: Serial_EEPROM_Test.ino:70
long_data
long long_data
Definition: Serial_EEPROM_Test.ino:71
Linduino.h
Header File for Linduino Libraries and Demo Code.
i2c_address
static uint8_t i2c_address
Definition: Serial_EEPROM_Test.ino:65
eeprom_read_float
uint8_t eeprom_read_float(uint8_t i2c_address, float *read_data, uint16_t address)
Read the four byte float data from the EEPROM starting at address.
Definition: QuikEval_EEPROM.cpp:435
EEPROM_DATA_SIZE
#define EEPROM_DATA_SIZE
Definition: Serial_EEPROM_Test.ino:55
eeprom_write_int32
uint8_t eeprom_write_int32(uint8_t i2c_address, int32_t write_data, uint16_t address)
Write the 4 byte long data to the EEPROM starting at address.
Definition: QuikEval_EEPROM.cpp:455
print_prompt
static void print_prompt()
Definition: Serial_EEPROM_Test.ino:97
loop
static void loop()
Definition: Serial_EEPROM_Test.ino:145
i2c_enable
void i2c_enable()
i2c_enable or quikeval_I2C_init must be called before using any of the other I2C routines.
Definition: LT_I2C.cpp:414
data
union LT_union_int32_4bytes data
Definition: DC2094A.ino:138
eeprom_write_buffer
uint8_t eeprom_write_buffer(uint8_t i2c_address, char *buffer, uint16_t address)
Write the buffer to the EEPROM with i2c_address starting at EEPROM address in blocks of EEPROM_PAGE_S...
Definition: QuikEval_EEPROM.cpp:246
setup
static void setup()
Definition: Serial_EEPROM_Test.ino:136
eeprom_write_float
uint8_t eeprom_write_float(uint8_t i2c_address, float write_data, uint16_t address)
Write the 4 byte float data to the EEPROM starting at address.
Definition: QuikEval_EEPROM.cpp:412
eeprom_write_int16
uint8_t eeprom_write_int16(uint8_t i2c_address, int16_t write_data, uint16_t address)
Write the 2 byte integer data to the EEPROM starting at address.
Definition: QuikEval_EEPROM.cpp:377
eeprom_read_byte
uint8_t eeprom_read_byte(uint8_t i2c_address, char *data, uint16_t address)
Read a data byte at address from the EEPROM with i2c_address.
Definition: QuikEval_EEPROM.cpp:282
id_string_size
unsigned char id_string_size
Definition: Serial_EEPROM_Test.ino:66
QuikEval_EEPROM.h
QuikEval EEPROM Library.
read_string
char * read_string()
Definition: UserInterface.cpp:109
EEPROM_CAL_STATUS_ADDRESS
#define EEPROM_CAL_STATUS_ADDRESS
Definition: QuikEval_EEPROM.h:58
eeprom_read_buffer_with_terminator
uint8_t eeprom_read_buffer_with_terminator(uint8_t i2c_address, char *buffer, uint16_t address, char terminator, uint8_t count)
Read data bytes from the EEPROM starting at address until the terminator is read or the number bytes ...
Definition: QuikEval_EEPROM.cpp:347
float_data
static float float_data
Definition: Serial_EEPROM_Test.ino:72
LT_I2C.h
LT_I2C: Routines to communicate with ATmega328P&#39;s hardware I2C port.
eeprom_read_int32
uint8_t eeprom_read_int32(uint8_t i2c_address, int32_t *read_data, uint16_t address)
Read the four byte long data from the EEPROM starting at address.
Definition: QuikEval_EEPROM.cpp:478
UserInterface.h
read_int
int32_t read_int()
Definition: UserInterface.cpp:93
read_float
float read_float()
Definition: UserInterface.cpp:78
terminator
byte terminator
Definition: Serial_EEPROM_Test.ino:68
i
static int i
Definition: DC2430A.ino:184
eeprom_address
static uint16_t eeprom_address
Definition: Serial_EEPROM_Test.ino:67
eeprom_read_buffer
uint8_t eeprom_read_buffer(uint8_t i2c_address, char *buffer, uint16_t address, uint8_t count)
Read data bytes from the EEPROM starting at address until number bytes read equals count...
Definition: QuikEval_EEPROM.cpp:317
ui_buffer
char ui_buffer[UI_BUFFER_SIZE]
Definition: UserInterface.cpp:46
read_count
byte read_count
Definition: Serial_EEPROM_Test.ino:69
print_title
static void print_title()
Definition: Serial_EEPROM_Test.ino:77