PN532 updates
[hackover2013-badge-firmware.git] / drivers / eeprom / eeprom.c
1 /**************************************************************************/
2 /*!
3 @file eeprom.c
4 @author K. Townsend (microBuilder.eu)
5
6 @section LICENSE
7
8 Software License Agreement (BSD License)
9
10 Copyright (c) 2010, microBuilder SARL
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are met:
15 1. Redistributions of source code must retain the above copyright
16 notice, this list of conditions and the following disclaimer.
17 2. Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in the
19 documentation and/or other materials provided with the distribution.
20 3. Neither the name of the copyright holders nor the
21 names of its contributors may be used to endorse or promote products
22 derived from this software without specific prior written permission.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35 /**************************************************************************/
36 #include <string.h>
37
38 #include "projectconfig.h"
39 #include "eeprom.h"
40
41 // Currently only the MCP24AA I2C EEPROM is used
42 #include "drivers/eeprom/mcp24aa/mcp24aa.h"
43
44 static uint8_t buf[32];
45
46 /**************************************************************************/
47 /*!
48 @brief Checks whether the supplied address is within the valid range
49
50 @param[in] addr
51 The 16-bit address to check
52
53 @return Zero if the address is valid, otherwise 1
54 */
55 /**************************************************************************/
56 bool eepromCheckAddress(uint16_t addr)
57 {
58 // Check for invalid values
59 return addr <= MCP24AA_MAXADDR ? FALSE : TRUE;
60 }
61
62 /**************************************************************************/
63 /*!
64 @brief Reads 1 byte from EEPROM
65
66 @param[in] addr
67 The 16-bit address to read from in EEPROM
68
69 @return An unsigned 8-bit value (uint8_t)
70 */
71 /**************************************************************************/
72 uint8_t eepromReadU8(uint16_t addr)
73 {
74 mcp24aaError_e error = MCP24AA_ERROR_OK;
75 error = mcp24aaReadBuffer(addr, buf, sizeof(uint8_t));
76
77 // ToDo: Handle any errors
78 if (error) { };
79
80 return buf[0];
81 }
82
83 /**************************************************************************/
84 /*!
85 @brief Reads 1 byte from EEPROM
86
87 @param[in] addr
88 The 16-bit address to read from in EEPROM
89
90 @return A signed 8-bit value (int8_t)
91 */
92 /**************************************************************************/
93 int8_t eepromReadS8(uint16_t addr)
94 {
95 int8_t results;
96
97 mcp24aaError_e error = MCP24AA_ERROR_OK;
98 error = mcp24aaReadBuffer(addr, buf, sizeof(int8_t));
99
100 // ToDo: Handle any errors
101 if (error) { };
102
103 memcpy(&results, buf, sizeof(int8_t));
104 return results;
105 }
106
107 /**************************************************************************/
108 /*!
109 @brief Reads 2 bytes from EEPROM
110
111 @param[in] addr
112 The 16-bit address to read from in EEPROM
113
114 @return A unsigned 16-bit value (uint16_t)
115 */
116 /**************************************************************************/
117 uint16_t eepromReadU16(uint16_t addr)
118 {
119 uint16_t results;
120
121 mcp24aaError_e error = MCP24AA_ERROR_OK;
122 error = mcp24aaReadBuffer(addr, buf, sizeof(uint16_t));
123
124 // ToDo: Handle any errors
125 if (error) { };
126
127 memcpy(&results, buf, sizeof(uint16_t));
128
129 return results;
130 }
131
132 /**************************************************************************/
133 /*!
134 @brief Reads 2 bytes from EEPROM
135
136 @param[in] addr
137 The 16-bit address to read from in EEPROM
138
139 @return A signed 16-bit value (int16_t)
140 */
141 /**************************************************************************/
142 int16_t eepromReadS16(uint16_t addr)
143 {
144 int16_t results;
145
146 mcp24aaError_e error = MCP24AA_ERROR_OK;
147 error = mcp24aaReadBuffer(addr, buf, sizeof(int16_t));
148
149 // ToDo: Handle any errors
150 if (error) { };
151
152 memcpy(&results, buf, sizeof(int16_t));
153 return results;
154 }
155
156 /**************************************************************************/
157 /*!
158 @brief Reads 4 bytes from EEPROM
159
160 @param[in] addr
161 The 16-bit address to read from in EEPROM
162
163 @return A unsigned 32-bit value (uint32_t)
164 */
165 /**************************************************************************/
166 uint32_t eepromReadU32(uint16_t addr)
167 {
168 uint32_t results;
169
170 mcp24aaError_e error = MCP24AA_ERROR_OK;
171 error = mcp24aaReadBuffer(addr, buf, sizeof(uint32_t));
172
173 // ToDo: Handle any errors
174 if (error) { };
175
176 memcpy(&results, buf, sizeof(uint32_t));
177 return results;
178 }
179
180 /**************************************************************************/
181 /*!
182 @brief Reads 4 bytes from EEPROM
183
184 @param[in] addr
185 The 16-bit address to read from in EEPROM
186
187 @return A signed 32-bit value (int32_t)
188 */
189 /**************************************************************************/
190 int32_t eepromReadS32(uint16_t addr)
191 {
192 int32_t results;
193
194 mcp24aaError_e error = MCP24AA_ERROR_OK;
195 error = mcp24aaReadBuffer(addr, buf, sizeof(int32_t));
196
197 // ToDo: Handle any errors
198 if (error) { };
199
200 memcpy(&results, buf, sizeof(int32_t));
201 return results;
202 }
203
204 /**************************************************************************/
205 /*!
206 @brief Reads 8 bytes from EEPROM
207
208 @param[in] addr
209 The 16-bit address to read from in EEPROM
210
211 @return A unsigned 64-bit value (uint64_t)
212 */
213 /**************************************************************************/
214 uint64_t eepromReadU64(uint16_t addr)
215 {
216 uint64_t results;
217
218 mcp24aaError_e error = MCP24AA_ERROR_OK;
219 error = mcp24aaReadBuffer(addr, buf, sizeof(uint64_t));
220
221 // ToDo: Handle any errors
222 if (error) { };
223
224 memcpy(&results, buf, sizeof(uint64_t));
225 return results;
226 }
227
228 /**************************************************************************/
229 /*!
230 @brief Reads 8 bytes from EEPROM
231
232 @param[in] addr
233 The 16-bit address to read from in EEPROM
234
235 @return A signed 64-bit value (int64_t)
236 */
237 /**************************************************************************/
238 int64_t eepromReadS64(uint16_t addr)
239 {
240 int64_t results;
241
242 mcp24aaError_e error = MCP24AA_ERROR_OK;
243 error = mcp24aaReadBuffer(addr, buf, sizeof(int64_t));
244
245 // ToDo: Handle any errors
246 if (error) { };
247
248 memcpy(&results, buf, sizeof(int64_t));
249 return results;
250 }
251
252 /**************************************************************************/
253 /*!
254 @brief Reads a variabls length buffer from EEPROM
255
256 @param[in] addr
257 The 16-bit address to write to in EEPROM
258 @param[out] buffer
259 Pointer to the buffer that will store any retrieved bytes
260 @param[in] bufferLength
261 The number of bytes to read
262 */
263 /**************************************************************************/
264 void eepromReadBuffer(uint16_t addr, uint8_t *buffer, uint32_t bufferLength)
265 {
266 // Instantiate error message placeholder
267 mcp24aaError_e error = MCP24AA_ERROR_OK;
268
269 // Read the contents of address
270 error = mcp24aaReadBuffer(addr, buffer, bufferLength);
271
272 // ToDo: Handle any errors
273 if (error) { };
274 }
275
276 /**************************************************************************/
277 /*!
278 @brief Writes 1 byte to EEPROM
279
280 @param[in] addr
281 The 16-bit address to write to in EEPROM
282 */
283 /**************************************************************************/
284 void eepromWriteU8(uint16_t addr, uint8_t value)
285 {
286 mcp24aaError_e error = MCP24AA_ERROR_OK;
287 error = mcp24aaWriteBuffer(addr, (uint8_t *)&value, sizeof(value));
288
289 // ToDo: Handle any errors
290 if (error) { };
291 }
292
293 /**************************************************************************/
294 /*!
295 @brief Writes 1 signed byte to EEPROM
296
297 @param[in] addr
298 The 16-bit address to write to in EEPROM
299 */
300 /**************************************************************************/
301 void eepromWriteS8(uint16_t addr, int8_t value)
302 {
303 mcp24aaError_e error = MCP24AA_ERROR_OK;
304 error = mcp24aaWriteBuffer(addr, (uint8_t *)&value, sizeof(value));
305
306 // ToDo: Handle any errors
307 if (error) { };
308 }
309
310 /**************************************************************************/
311 /*!
312 @brief Writes an unsigned 16-bit integer to EEPROM
313
314 @param[in] addr
315 The 16-bit address to write to in EEPROM
316 */
317 /**************************************************************************/
318 void eepromWriteU16(uint16_t addr, uint16_t value)
319 {
320 mcp24aaError_e error = MCP24AA_ERROR_OK;
321 error = mcp24aaWriteBuffer(addr, (uint8_t *)&value, sizeof(value));
322
323 // ToDo: Handle any errors
324 if (error) { };
325 }
326
327 /**************************************************************************/
328 /*!
329 @brief Writes a signed 16-bit integer to EEPROM
330
331 @param[in] addr
332 The 16-bit address to write to in EEPROM
333 */
334 /**************************************************************************/
335 void eepromWriteS16(uint16_t addr, int16_t value)
336 {
337 mcp24aaError_e error = MCP24AA_ERROR_OK;
338 error = mcp24aaWriteBuffer(addr, (uint8_t *)&value, sizeof(value));
339
340 // ToDo: Handle any errors
341 if (error) { };
342 }
343
344 /**************************************************************************/
345 /*!
346 @brief Writes an unsigned 32-bit integer to EEPROM
347
348 @param[in] addr
349 The 16-bit address to write to in EEPROM
350 */
351 /**************************************************************************/
352 void eepromWriteU32(uint16_t addr, uint32_t value)
353 {
354 mcp24aaError_e error = MCP24AA_ERROR_OK;
355 error = mcp24aaWriteBuffer(addr, (uint8_t *)&value, sizeof(value));
356
357 // ToDo: Handle any errors
358 if (error) { };
359 }
360
361 /**************************************************************************/
362 /*!
363 @brief Writes a signed 32-bit integer to EEPROM
364
365 @param[in] addr
366 The 16-bit address to write to in EEPROM
367 */
368 /**************************************************************************/
369 void eepromWriteS32(uint16_t addr, int32_t value)
370 {
371 mcp24aaError_e error = MCP24AA_ERROR_OK;
372 error = mcp24aaWriteBuffer(addr, (uint8_t *)&value, sizeof(value));
373
374 // ToDo: Handle any errors
375 if (error) { };
376 }
377
378 /**************************************************************************/
379 /*!
380 @brief Writes an unsigned 64-bit integer to EEPROM
381
382 @param[in] addr
383 The 16-bit address to write to in EEPROM
384 */
385 /**************************************************************************/
386 void eepromWriteU64(uint16_t addr, uint64_t value)
387 {
388 mcp24aaError_e error = MCP24AA_ERROR_OK;
389 error = mcp24aaWriteBuffer(addr, (uint8_t *)&value, sizeof(value));
390
391 // ToDo: Handle any errors
392 if (error) { };
393 }
394
395 /**************************************************************************/
396 /*!
397 @brief Writes a signed 64-bit integer to EEPROM
398
399 @param[in] addr
400 The 16-bit address to write to in EEPROM
401 */
402 /**************************************************************************/
403 void eepromWriteS64(uint16_t addr, int64_t value)
404 {
405 mcp24aaError_e error = MCP24AA_ERROR_OK;
406 error = mcp24aaWriteBuffer(addr, (uint8_t *)&value, sizeof(value));
407
408 // ToDo: Handle any errors
409 if (error) { };
410 }
This page took 0.060345 seconds and 5 git commands to generate.