DC2100A  1.2.0
Bi-Directional Cell Balancer Using the LTC3300-1 and the LTC6804-2
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
24AA64.c
Go to the documentation of this file.
1 /*
2  Linear Technology DC2100A Demonstration Board.
3  Reference Application File for Interface to 24AA64 EEPROM through the LTC6804-2 Battery Monitor on the DC2100A PCB.
4  All datasheet references in this file refer to Microchip Technology Inc. document 21711J.pdf.
5 
6  @verbatim
7  The Microchip Technology Inc. 24AA64/24LC64
8  (24XX64*) is a 64 Kbit Electrically Erasable PROM.
9  The device is organized as a single block of 8K x 8-bit
10  memory with a 2-wire serial interface. Low-voltage
11  design permits operation down to 1.8V, with standby
12  and active currents of only 1 uA and 1 mA,
13  respectively. It has been developed for advanced, lowpower
14  applications such as personal communications
15  or data acquisition. The 24XX64 also has a page write
16  capability for up to 32 bytes of data. Functional address
17  lines allow up to eight devices on the same bus, for up
18  to 512 Kbits address space. The 24XX64 is available in
19  the standard 8-pin PDIP, surface mount SOIC, TSSOP
20  and MSOP packages.
21  @endverbatim
22 
23  http://www.linear.com/solutions/5126
24 
25  REVISION HISTORY
26  $Revision: 542 $
27  $Date: 2014-07-31 11:57:59 -0400 (Thu, 31 Jul 2014) $
28 
29  Copyright (c) 2013, Linear Technology Corp.(LTC)
30  All rights reserved.
31 
32  Redistribution and use in source and binary forms, with or without
33  modification, are permitted provided that the following conditions are met:
34 
35  1. Redistributions of source code must retain the above copyright notice, this
36  list of conditions and the following disclaimer.
37  2. Redistributions in binary form must reproduce the above copyright notice,
38  this list of conditions and the following disclaimer in the documentation
39  and/or other materials provided with the distribution.
40 
41  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
42  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
43  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
45  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
46  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
50  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 
52  The views and conclusions contained in the software and documentation are those
53  of the authors and should not be interpreted as representing official policies,
54  either expressed or implied, of Linear Technology Corp.
55 
56 */
57 
58 /*! @file
59  @ingroup EEPROM
60  Reference Application File for Interface to 24AA64 EEPROM through the LTC6804-2 Battery Monitor on the DC2100A PCB.
61 */
62 
63 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
64 // Includes
65 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
66 #include "Typedefs.h"
67 #include "24AA64.h"
68 #include "LTC6804-2.h"
69 #include <string.h>
70 
71 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
72 // Definitions
73 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
74 
75 // I2C address and format from datasheet Figure 3-3
76 #define EEPROM_24AA64_BASE_ADDRESS 0x50
77 #define EEPROM_24AA64_WRITE_BIT 0
78 #define EEPROM_24AA64_READ_BIT 1
79 
80 typedef struct
81 {
82  int8 control_byte;
83  int8 word_address[2];
85 
86 // Timing characteristics from datasheet Table 1-2
87 #define EEPROM_24AA64_BAUD_RATE 400 // in kHz, Max Clock Frequency when 2.5V <= VCC <= 5.5V (FCLK in datasheet)
88 
89 // Timing characteristics from datasheet Table 1-2
90 #define EEPROM_24AA64_TWC 5 // in ms, max write cycle time
91 
92 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
93 // Global Data
94 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
95 
96 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
97 // Local Data
98 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
99 
100 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
101 // Local Prototypes
102 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
103 
104 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
105 // Global Functions
106 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
107 
108 // Writes a series of bytes to the 24AA64 EEPROM, one page at a time, following datasheet Figure 6-2
109 void Eeprom_24AA64_Write(int8 board_num, int16 address, int8* data_ptr, int16 num_bytes)
110 {
112  unsigned int8 bytes_to_send;
113 
114  while (num_bytes)
115  {
116  // Send control byte to EEPROM
117  command.control_byte = (EEPROM_24AA64_BASE_ADDRESS << 1) | EEPROM_24AA64_WRITE_BIT;
118  command.word_address[0] = UPPER_BYTE(address);
119  command.word_address[1] = LOWER_BYTE(address);
120 
121  LTC6804_I2C_Write(board_num, TRUE, FALSE, &command, sizeof(command), EEPROM_24AA64_BAUD_RATE);
122 
123  // Write either as much data as necessary to fill an EEPROM page, or the rest of the bytes
124  bytes_to_send = MIN(EEPROM_24AA64_PAGE_SIZE - (address % EEPROM_24AA64_PAGE_SIZE), num_bytes);
125 
126  // Send the data to the EEPROM.
127  LTC6804_I2C_Write(board_num, FALSE, TRUE, data_ptr, bytes_to_send, EEPROM_24AA64_BAUD_RATE);
128 
129  // Update counters to reflect data sent.
130  num_bytes -= bytes_to_send;
131  address += bytes_to_send;
132  data_ptr += bytes_to_send;
133 
134  // Wait for the data to be written
135  delay_ms(EEPROM_24AA64_TWC);
136  }
137  return;
138 }
139 
140 // Reads a series of bytes to the 24AA64 EEPROM, following datasheet Figure 8-3
141 void Eeprom_24AA64_Read(int8 board_num, int16 address, int8* data_ptr, int16 num_bytes)
142 {
144 
145  while(num_bytes)
146  {
147  // Send control byte to EEPROM to write the start address.
148  command.control_byte = (EEPROM_24AA64_BASE_ADDRESS << 1) | EEPROM_24AA64_WRITE_BIT;
149  command.word_address[0] = UPPER_BYTE(address);
150  command.word_address[1] = LOWER_BYTE(address);
151 
152  LTC6804_I2C_Write(board_num, TRUE, FALSE, &command, sizeof(command), EEPROM_24AA64_BAUD_RATE);
153 
154  // You can not read from I2C without first writing an address to read from.
155  // Use first location where data is to be stored for the address
156  *data_ptr = (EEPROM_24AA64_BASE_ADDRESS << 1) | EEPROM_24AA64_READ_BIT;
157 
158  // Read the data from the EEPROM.
159  LTC6804_I2C_Read(board_num, TRUE, TRUE, data_ptr, num_bytes+1, EEPROM_24AA64_BAUD_RATE);
160 
161  // We can read all at once; no page limitations for reading
162  num_bytes = 0;
163  }
164  return;
165 }
166 
167 // Erases the full contents of the 24AA64 EEPROM
168 void Eeprom_24AA64_Erase(int8 board_num)
169 {
170  int8 erase_data[EEPROM_24AA64_PAGE_SIZE];
171  int16 erase_address = 0;
172 
173  memset(erase_data, 0xFF, sizeof(erase_data));
174 
175  while (erase_address < EEPROM_24AA64_SIZE)
176  {
177  Eeprom_24AA64_Write(board_num, erase_address, erase_data, sizeof(erase_data));
178  erase_address += EEPROM_24AA64_PAGE_SIZE;
179  }
180 }
181 
182 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
183 // Local Functions
184 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#define EEPROM_24AA64_PAGE_SIZE
Number of bytes that can be operated on at a time.
Definition: 24AA64.h:80
void LTC6804_I2C_Write(int8 board_num, BOOLEAN start, BOOLEAN stop, int8 *data_ptr, int16 num_bytes, int16 baud_khz)
Writes a string of bytes to the LTC6804 I2C port implemented on its GPIO pins.
Definition: LTC6804-2.c:703
void Eeprom_24AA64_Erase(int8 board_num)
Erases the full contents of the 24AA64 EEPROM.
Definition: 24AA64.c:168
#define EEPROM_24AA64_SIZE
Number of bytes in the EEPROM.
Definition: 24AA64.h:79
BOOLEAN LTC6804_I2C_Read(int8 board_num, BOOLEAN start, BOOLEAN stop, int8 *data_ptr, int16 num_bytes, int16 baud_khz)
Writes one byte, and then reads a string of bytes to the LTC6804 I2C port implemented on its GPIO pin...
Definition: LTC6804-2.c:763
API Header File for LTC6804-2 Multicell Battery Monitors.
Reference Application File for Interface to 24AA64 EEPROM through the LTC6804-2 Battery Monitor on th...
void Eeprom_24AA64_Read(int8 board_num, int16 address, int8 *data_ptr, int16 num_bytes)
Reads a series of bytes to the 24AA64 EEPROM.
Definition: 24AA64.c:141
void Eeprom_24AA64_Write(int8 board_num, int16 address, int8 *data_ptr, int16 num_bytes)
Writes a series of bytes to the 24AA64 EEPROM.
Definition: 24AA64.c:109