Spieler splodiert.
[hackover2013-badge-firmware.git] / drivers / storage / 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/storage/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 static bool _mcp24aaInitialised = false;
112
113 /**************************************************************************/
114 /*!
115 @brief Initialises the I2C block
116 */
117 /**************************************************************************/
118 mcp24aaError_e mcp24aaInit()
119 {
120 // Initialise I2C
121 if (i2cInit(I2CMASTER) == false)
122 {
123 return MCP24AA_ERROR_I2CINIT; /* Fatal error */
124 }
125
126 // Set initialisation flag
127 _mcp24aaInitialised = true;
128
129 return MCP24AA_ERROR_OK;
130 }
131
132 /**************************************************************************/
133 /*!
134 @brief Reads the specified number of bytes from the supplied address.
135
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.
138
139 @param[in] address
140 The 16-bit address where the read will start. The maximum
141 value for the address depends on the size of the EEPROM
142 @param[in] *buffer
143 Pointer to the buffer that will store the read results
144 @param[in] bufferLength
145 Length of the buffer
146 */
147 /**************************************************************************/
148 mcp24aaError_e mcp24aaReadBuffer (uint16_t address, uint8_t *buffer, uint32_t bufferLength)
149 {
150 if (!_mcp24aaInitialised) mcp24aaInit();
151
152 if (address >= MCP24AA_MAXADDR)
153 {
154 return MCP24AA_ERROR_ADDRERR;
155 }
156
157 if (bufferLength > 8)
158 {
159 return MCP24AA_ERROR_BUFFEROVERFLOW;
160 }
161
162 // ToDo: Check if I2C is ready
163
164 // Clear buffers
165 uint32_t i;
166 for ( i = 0; i < I2C_BUFSIZE; i++ )
167 {
168 I2CMasterBuffer[i] = 0x00;
169 I2CSlaveBuffer[i] = 0x00;
170 }
171
172 // Write address bits to enable random read
173 I2CWriteLength = 3;
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
180 // may be unexpected
181 I2CMasterBuffer[3] = MCP24AA_ADDR | MCP24AA_READBIT;
182
183 // Transmit command
184 i2cEngine();
185
186 // Fill response buffer
187 for (i = 0; i < bufferLength; i++)
188 {
189 buffer[i] = I2CSlaveBuffer[i];
190 }
191
192 return MCP24AA_ERROR_OK;
193 }
194
195 /**************************************************************************/
196 /*!
197 @brief Writes the supplied bytes at a specified address.
198
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.
201
202 @param[in] address
203 The 16-bit address where the write will start. The
204 maximum value for the address depends on the size of the
205 EEPROM
206 @param[in] *buffer
207 Pointer to the buffer that contains the values to write.
208 @param[in] bufferLength
209 Length of the buffer
210 */
211 /**************************************************************************/
212 mcp24aaError_e mcp24aaWriteBuffer (uint16_t address, uint8_t *buffer, uint32_t bufferLength)
213 {
214 if (!_mcp24aaInitialised) mcp24aaInit();
215
216 if (address >= MCP24AA_MAXADDR)
217 {
218 return MCP24AA_ERROR_ADDRERR;
219 }
220
221 if (bufferLength > 8)
222 {
223 return MCP24AA_ERROR_BUFFEROVERFLOW;
224 }
225
226 // ToDo: Check if I2C is ready
227
228 // Clear write buffer
229 uint32_t i;
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/storage/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/storage/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.050768 seconds and 5 git commands to generate.