1 /**************************************************************************/
4 @author K. Townsend (microBuilder.eu)
10 Driver for Microchip's 24AA32AF serial EEPROM. This driver assumes
11 that the address is set to 1010 000.
16 #include "core/cpu/cpu.h"
17 #include "drivers/eeprom/mcp24aa/mcp24aa.h"
25 // Instantiate error message placeholder
26 mcp24aaError_e error = MCP24AA_ERROR_OK;
28 // Create read buffer (1 byte)
29 uint8_t buffer[1] = { 0x00 };
31 // Write 0xCC at address 0x125
32 error = mcp24aaWriteByte(0x0125, 0xCC);
38 case (MCP24AA_ERROR_I2CINIT):
39 // Unable to initialise I2C
41 case (MCP24AA_ERROR_ADDRERR):
42 // Address out of range
49 // Read the contents of address 0x0125
50 error = MCP24AA_ERROR_OK;
51 error = mcp24aaReadByte(0x0125, buffer);
57 case (MCP24AA_ERROR_I2CINIT):
58 // Unable to initialise I2C
60 case (MCP24AA_ERROR_ADDRERR):
61 // Address out of range
68 uint8_t results = buffer[0];
74 Software License Agreement (BSD License)
76 Copyright (c) 2010, microBuilder SARL
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.
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.
101 /**************************************************************************/
104 #include "core/systick/systick.h"
105 #include "core/i2c/i2c.h"
107 extern volatile uint8_t I2CMasterBuffer
[I2C_BUFSIZE
];
108 extern volatile uint8_t I2CSlaveBuffer
[I2C_BUFSIZE
];
109 extern volatile uint32_t I2CReadLength
, I2CWriteLength
;
111 static bool _mcp24aaInitialised
= false;
113 /**************************************************************************/
115 @brief Initialises the I2C block
117 /**************************************************************************/
118 mcp24aaError_e
mcp24aaInit()
121 if (i2cInit(I2CMASTER
) == false)
123 return MCP24AA_ERROR_I2CINIT
; /* Fatal error */
126 // Set initialisation flag
127 _mcp24aaInitialised
= true;
129 return MCP24AA_ERROR_OK
;
132 /**************************************************************************/
134 @brief Reads the specified number of bytes from the supplied address.
136 This function will read one or more bytes starting at the supplied
137 address. A maximum of 8 bytes can be read in one operation.
140 The 16-bit address where the read will start. The maximum
141 value for the address depends on the size of the EEPROM
143 Pointer to the buffer that will store the read results
144 @param[in] bufferLength
147 /**************************************************************************/
148 mcp24aaError_e
mcp24aaReadBuffer (uint16_t address
, uint8_t *buffer
, uint32_t bufferLength
)
150 if (!_mcp24aaInitialised
) mcp24aaInit();
152 if (address
>= MCP24AA_MAXADDR
)
154 return MCP24AA_ERROR_ADDRERR
;
157 if (bufferLength
> 8)
159 return MCP24AA_ERROR_BUFFEROVERFLOW
;
162 // ToDo: Check if I2C is ready
166 for ( i
= 0; i
< I2C_BUFSIZE
; i
++ )
168 I2CMasterBuffer
[i
] = 0x00;
169 I2CSlaveBuffer
[i
] = 0x00;
172 // Write address bits to enable random read
174 I2CReadLength
= bufferLength
;
175 I2CMasterBuffer
[0] = MCP24AA_ADDR
; // I2C device address
176 I2CMasterBuffer
[1] = (address
>> 8); // Address (high byte)
177 I2CMasterBuffer
[2] = (address
& 0xFF); // Address (low byte)
178 // If you wish to read, you need to append the address w/read bit, though this
179 // needs to be placed one bit higher than the size of I2CWriteLength which
181 I2CMasterBuffer
[3] = MCP24AA_ADDR
| MCP24AA_READBIT
;
186 // Fill response buffer
187 for (i
= 0; i
< bufferLength
; i
++)
189 buffer
[i
] = I2CSlaveBuffer
[i
];
192 return MCP24AA_ERROR_OK
;
195 /**************************************************************************/
197 @brief Writes the supplied bytes at a specified address.
199 This function will write one or more bytes starting at the supplied
200 address. A maximum of 8 bytes can be written in one operation.
203 The 16-bit address where the write will start. The
204 maximum value for the address depends on the size of the
207 Pointer to the buffer that contains the values to write.
208 @param[in] bufferLength
211 /**************************************************************************/
212 mcp24aaError_e
mcp24aaWriteBuffer (uint16_t address
, uint8_t *buffer
, uint32_t bufferLength
)
214 if (!_mcp24aaInitialised
) mcp24aaInit();
216 if (address
>= MCP24AA_MAXADDR
)
218 return MCP24AA_ERROR_ADDRERR
;
221 if (bufferLength
> 8)
223 return MCP24AA_ERROR_BUFFEROVERFLOW
;
226 // ToDo: Check if I2C is ready
228 // Clear write buffer
230 for ( i
= 0; i
< I2C_BUFSIZE
; i
++ )
232 I2CMasterBuffer
[i
] = 0x00;
235 // Write address bits and data to the master buffer
236 I2CWriteLength
= 3 + bufferLength
;
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
++)
243 I2CMasterBuffer
[i
+3] = buffer
[i
];
249 // Wait at least 10ms
252 return MCP24AA_ERROR_OK
;
255 /**************************************************************************/
257 @brief Reads one byte from the supplied address.
259 This function will read one byte starting at the supplied 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
265 Pointer to the buffer that will store the read results
268 #include "core/cpu/cpu/h"
269 #include "drivers/eeprom/mcp24aa/mcp24aa.h"
274 // Create read buffer (1 byte)
275 uint8_t buffer[1] = { 0x00 };
277 // Write 0xEE and address 0x0120
278 mcp24aaWriteByte(0x0120, 0xEE);
280 // Populate buffer with contents of 0x0120
281 mcp24aaReadByte(0x0120, buffer);
283 // results should equal 0xEE
284 uint8_t results = buffer[0];
287 /**************************************************************************/
288 mcp24aaError_e
mcp24aaReadByte (uint16_t address
, uint8_t *buffer
)
290 if (!_mcp24aaInitialised
) mcp24aaInit();
292 return mcp24aaReadBuffer(address
, buffer
, 1);
295 /**************************************************************************/
297 @brief Writes one byte to the supplied address.
299 This function will write one byte at the supplied 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
305 The data to be written to the EEPROM
308 #include "core/cpu/cpu/h"
309 #include "drivers/eeprom/mcp24aa/mcp24aa.h"
314 // Create read buffer (1 byte)
315 uint8_t buffer[1] = { 0x00 };
317 // Write 0xEE and address 0x0120
318 mcp24aaWriteByte(0x0120, 0xEE);
320 // Populate buffer with contents of 0x0120
321 mcp24aaReadByte(0x0120, buffer);
323 // results should equal 0xEE
324 uint8_t results = buffer[0];
327 /**************************************************************************/
328 mcp24aaError_e
mcp24aaWriteByte (uint16_t address
, uint8_t value
)
330 if (!_mcp24aaInitialised
) mcp24aaInit();
335 // Write byte to EEPROM at specified address
337 return mcp24aaWriteBuffer(address
, wBuffer
, 1);