f7f981df060cb2f5c8bf85b42ba1f5f0ce264e69
[hackover2013-badge-firmware.git] / drivers / eeprom / mcp24aa / mcp24aa.c
1 /**************************************************************************/
2 /*!
3 @file mcp24aa.c
4 @author K. Townsend (microBuilder.eu)
5 @date 22 March 2010
6 @version 0.10
7
8 @section DESCRIPTION
9
10 Driver for Microchip's 24AA32AF serial EEPROM. This driver assumes
11 that the address is set to 1010 000.
12
13 @section Example
14
15 @code
16 #include "core/cpu/cpu.h"
17 #include "drivers/eeprom/mcp24aa/mcp24aa.h"
18
19 int main(void)
20 {
21 cpuInit();
22
23 mcp24aaInit();
24
25 // Instantiate error message placeholder
26 mcp24aaError_e error = MCP24AA_ERROR_OK;
27
28 // Create read buffer (1 byte)
29 uint8_t buffer[1] = { 0x00 };
30
31 // Write 0xCC at address 0x125
32 error = mcp24aaWriteByte(0x0125, 0xCC);
33 if (error)
34 {
35 // Handle any errors
36 switch (error)
37 {
38 case (MCP24AA_ERROR_I2CINIT):
39 // Unable to initialise I2C
40 break;
41 case (MCP24AA_ERROR_ADDRERR):
42 // Address out of range
43 break;
44 default:
45 break;
46 }
47 }
48
49 // Read the contents of address 0x0125
50 error = MCP24AA_ERROR_OK;
51 error = mcp24aaReadByte(0x0125, buffer);
52 if (error)
53 {
54 // Handle any errors
55 switch (error)
56 {
57 case (MCP24AA_ERROR_I2CINIT):
58 // Unable to initialise I2C
59 break;
60 case (MCP24AA_ERROR_ADDRERR):
61 // Address out of range
62 break;
63 default:
64 break;
65 }
66 }
67
68 uint8_t results = buffer[0];
69 }
70 @endcode
71
72 @section LICENSE
73
74 Software License Agreement (BSD License)
75
76 Copyright (c) 2010, microBuilder SARL
77 All rights reserved.
78
79 Redistribution and use in source and binary forms, with or without
80 modification, are permitted provided that the following conditions are met:
81 1. Redistributions of source code must retain the above copyright
82 notice, this list of conditions and the following disclaimer.
83 2. Redistributions in binary form must reproduce the above copyright
84 notice, this list of conditions and the following disclaimer in the
85 documentation and/or other materials provided with the distribution.
86 3. Neither the name of the copyright holders nor the
87 names of its contributors may be used to endorse or promote products
88 derived from this software without specific prior written permission.
89
90 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
91 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
92 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
93 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
94 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
95 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
96 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
97 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
98 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
99 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
100 */
101 /**************************************************************************/
102
103 #include "mcp24aa.h"
104 #include "core/systick/systick.h"
105 #include "core/i2c/i2c.h"
106
107 extern volatile uint8_t I2CMasterBuffer[I2C_BUFSIZE];
108 extern volatile uint8_t I2CSlaveBuffer[I2C_BUFSIZE];
109 extern volatile uint32_t I2CReadLength, I2CWriteLength;
110
111 uint32_t i, timeout;
112
113 static bool _mcp24aaInitialised = false;
114
115 /**************************************************************************/
116 /*!
117 @brief Initialises the I2C block
118 */
119 /**************************************************************************/
120 mcp24aaError_e mcp24aaInit()
121 {
122 // Initialise I2C
123 if (i2cInit(I2CMASTER) == false)
124 {
125 return MCP24AA_ERROR_I2CINIT; /* Fatal error */
126 }
127
128 // Set initialisation flag
129 _mcp24aaInitialised = true;
130
131 return MCP24AA_ERROR_OK;
132 }
133
134 /**************************************************************************/
135 /*!
136 @brief Reads the specified number of bytes from the supplied address.
137
138 This function will read one or more bytes starting at the supplied
139 address. A maximum of 8 bytes can be read in one operation.
140
141 @param[in] address
142 The 16-bit address where the read will start. The maximum
143 value for the address depends on the size of the EEPROM
144 @param[in] *buffer
145 Pointer to the buffer that will store the read results
146 @param[in] bufferLength
147 Length of the buffer
148 */
149 /**************************************************************************/
150 mcp24aaError_e mcp24aaReadBuffer (uint16_t address, uint8_t *buffer, uint32_t bufferLength)
151 {
152 if (!_mcp24aaInitialised) mcp24aaInit();
153
154 if (address >= MCP24AA_MAXADDR)
155 {
156 return MCP24AA_ERROR_ADDRERR;
157 }
158
159 if (bufferLength > 8)
160 {
161 return MCP24AA_ERROR_BUFFEROVERFLOW;
162 }
163
164 // ToDo: Check if I2C is ready
165
166 // Clear buffers
167 for ( i = 0; i < I2C_BUFSIZE; i++ )
168 {
169 I2CMasterBuffer[i] = 0x00;
170 I2CSlaveBuffer[i] = 0x00;
171 }
172
173 // Write address bits to enable random read
174 I2CWriteLength = 3;
175 I2CReadLength = bufferLength;
176 I2CMasterBuffer[0] = MCP24AA_ADDR; // I2C device address
177 I2CMasterBuffer[1] = (address >> 8); // Address (high byte)
178 I2CMasterBuffer[2] = (address & 0xFF); // Address (low byte)
179 // If you wish to read, you need to append the address w/read bit, though this
180 // needs to be placed one bit higher than the size of I2CWriteLength which
181 // may be unexpected
182 I2CMasterBuffer[3] = MCP24AA_ADDR | MCP24AA_READBIT;
183
184 // Transmit command
185 i2cEngine();
186
187 // Fill response buffer
188 for (i = 0; i < bufferLength; i++)
189 {
190 buffer[i] = I2CSlaveBuffer[i];
191 }
192
193 return MCP24AA_ERROR_OK;
194 }
195
196 /**************************************************************************/
197 /*!
198 @brief Writes the supplied bytes at a specified address.
199
200 This function will write one or more bytes starting at the supplied
201 address. A maximum of 8 bytes can be written in one operation.
202
203 @param[in] address
204 The 16-bit address where the write will start. The
205 maximum value for the address depends on the size of the
206 EEPROM
207 @param[in] *buffer
208 Pointer to the buffer that contains the values to write.
209 @param[in] bufferLength
210 Length of the buffer
211 */
212 /**************************************************************************/
213 mcp24aaError_e mcp24aaWriteBuffer (uint16_t address, uint8_t *buffer, uint32_t bufferLength)
214 {
215 if (!_mcp24aaInitialised) mcp24aaInit();
216
217 if (address >= MCP24AA_MAXADDR)
218 {
219 return MCP24AA_ERROR_ADDRERR;
220 }
221
222 if (bufferLength > 8)
223 {
224 return MCP24AA_ERROR_BUFFEROVERFLOW;
225 }
226
227 // ToDo: Check if I2C is ready
228
229 // Clear write buffer
230 for ( i = 0; i < I2C_BUFSIZE; i++ )
231 {
232 I2CMasterBuffer[i] = 0x00;
233 }
234
235 // Write address bits and data to the master buffer
236 I2CWriteLength = 3 + bufferLength;
237 I2CReadLength = 0;
238 I2CMasterBuffer[0] = MCP24AA_ADDR; // I2C device address
239 I2CMasterBuffer[1] = (address >> 8); // Address (high byte)
240 I2CMasterBuffer[2] = (address & 0xFF); // Address (low byte)
241 for (i = 0; i < bufferLength; i++)
242 {
243 I2CMasterBuffer[i+3] = buffer[i];
244 }
245
246 // Transmit command
247 i2cEngine();
248
249 // Wait at least 10ms
250 systickDelay(10);
251
252 return MCP24AA_ERROR_OK;
253 }
254
255 /**************************************************************************/
256 /*!
257 @brief Reads one byte from the supplied address.
258
259 This function will read one byte starting at the supplied address.
260
261 @param[in] address
262 The 16-bit address where the read will start. The maximum
263 value for the address depends on the size of the EEPROM
264 @param[in] *buffer
265 Pointer to the buffer that will store the read results
266
267 @code
268 #include "core/cpu/cpu/h"
269 #include "drivers/eeprom/mcp24aa/mcp24aa.h"
270 ...
271 cpuInit();
272 mcp24aaInit();
273
274 // Create read buffer (1 byte)
275 uint8_t buffer[1] = { 0x00 };
276
277 // Write 0xEE and address 0x0120
278 mcp24aaWriteByte(0x0120, 0xEE);
279
280 // Populate buffer with contents of 0x0120
281 mcp24aaReadByte(0x0120, buffer);
282
283 // results should equal 0xEE
284 uint8_t results = buffer[0];
285 @endcode
286 */
287 /**************************************************************************/
288 mcp24aaError_e mcp24aaReadByte (uint16_t address, uint8_t *buffer)
289 {
290 if (!_mcp24aaInitialised) mcp24aaInit();
291
292 return mcp24aaReadBuffer(address, buffer, 1);
293 }
294
295 /**************************************************************************/
296 /*!
297 @brief Writes one byte to the supplied address.
298
299 This function will write one byte at the supplied address.
300
301 @param[in] address
302 The 16-bit address where the write will start. The maximum
303 value for the address depends on the size of the EEPROM
304 @param[in] value
305 The data to be written to the EEPROM
306
307 @code
308 #include "core/cpu/cpu/h"
309 #include "drivers/eeprom/mcp24aa/mcp24aa.h"
310 ...
311 cpuInit();
312 mcp24aaInit();
313
314 // Create read buffer (1 byte)
315 uint8_t buffer[1] = { 0x00 };
316
317 // Write 0xEE and address 0x0120
318 mcp24aaWriteByte(0x0120, 0xEE);
319
320 // Populate buffer with contents of 0x0120
321 mcp24aaReadByte(0x0120, buffer);
322
323 // results should equal 0xEE
324 uint8_t results = buffer[0];
325 @endcode
326 */
327 /**************************************************************************/
328 mcp24aaError_e mcp24aaWriteByte (uint16_t address, uint8_t value)
329 {
330 if (!_mcp24aaInitialised) mcp24aaInit();
331
332 // Set read buffer
333 uint8_t wBuffer[1];
334
335 // Write byte to EEPROM at specified address
336 wBuffer[0] = value;
337 return mcp24aaWriteBuffer(address, wBuffer, 1);
338 }
339
This page took 0.061745 seconds and 3 git commands to generate.