1 /**************************************************************************/
4 @author K. Townsend (microBuilder.eu)
8 Software License Agreement (BSD License)
10 Copyright (c) 2010, microBuilder SARL
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.
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.
35 /**************************************************************************/
38 #include "projectconfig.h"
41 // Currently only the MCP24AA I2C EEPROM is used
42 #include "drivers/storage/eeprom/mcp24aa/mcp24aa.h"
44 static uint8_t buf
[32];
46 /**************************************************************************/
48 @brief Checks whether the supplied address is within the valid range
51 The 16-bit address to check
53 @return Zero if the address is valid, otherwise 1
55 /**************************************************************************/
56 bool eepromCheckAddress(uint16_t addr
)
58 // Check for invalid values
59 return addr
<= MCP24AA_MAXADDR
? FALSE
: TRUE
;
62 /**************************************************************************/
64 @brief Reads 1 byte from EEPROM
67 The 16-bit address to read from in EEPROM
69 @return An unsigned 8-bit value (uint8_t)
71 /**************************************************************************/
72 uint8_t eepromReadU8(uint16_t addr
)
74 mcp24aaError_e error
= MCP24AA_ERROR_OK
;
75 error
= mcp24aaReadBuffer(addr
, buf
, sizeof(uint8_t));
77 // ToDo: Handle any errors
83 /**************************************************************************/
85 @brief Reads 1 byte from EEPROM
88 The 16-bit address to read from in EEPROM
90 @return A signed 8-bit value (int8_t)
92 /**************************************************************************/
93 int8_t eepromReadS8(uint16_t addr
)
97 mcp24aaError_e error
= MCP24AA_ERROR_OK
;
98 error
= mcp24aaReadBuffer(addr
, buf
, sizeof(int8_t));
100 // ToDo: Handle any errors
103 memcpy(&results
, buf
, sizeof(int8_t));
107 /**************************************************************************/
109 @brief Reads 2 bytes from EEPROM
112 The 16-bit address to read from in EEPROM
114 @return A unsigned 16-bit value (uint16_t)
116 /**************************************************************************/
117 uint16_t eepromReadU16(uint16_t addr
)
121 mcp24aaError_e error
= MCP24AA_ERROR_OK
;
122 error
= mcp24aaReadBuffer(addr
, buf
, sizeof(uint16_t));
124 // ToDo: Handle any errors
127 memcpy(&results
, buf
, sizeof(uint16_t));
132 /**************************************************************************/
134 @brief Reads 2 bytes from EEPROM
137 The 16-bit address to read from in EEPROM
139 @return A signed 16-bit value (int16_t)
141 /**************************************************************************/
142 int16_t eepromReadS16(uint16_t addr
)
146 mcp24aaError_e error
= MCP24AA_ERROR_OK
;
147 error
= mcp24aaReadBuffer(addr
, buf
, sizeof(int16_t));
149 // ToDo: Handle any errors
152 memcpy(&results
, buf
, sizeof(int16_t));
156 /**************************************************************************/
158 @brief Reads 4 bytes from EEPROM
161 The 16-bit address to read from in EEPROM
163 @return A unsigned 32-bit value (uint32_t)
165 /**************************************************************************/
166 uint32_t eepromReadU32(uint16_t addr
)
170 mcp24aaError_e error
= MCP24AA_ERROR_OK
;
171 error
= mcp24aaReadBuffer(addr
, buf
, sizeof(uint32_t));
173 // ToDo: Handle any errors
176 memcpy(&results
, buf
, sizeof(uint32_t));
180 /**************************************************************************/
182 @brief Reads 4 bytes from EEPROM
185 The 16-bit address to read from in EEPROM
187 @return A signed 32-bit value (int32_t)
189 /**************************************************************************/
190 int32_t eepromReadS32(uint16_t addr
)
194 mcp24aaError_e error
= MCP24AA_ERROR_OK
;
195 error
= mcp24aaReadBuffer(addr
, buf
, sizeof(int32_t));
197 // ToDo: Handle any errors
200 memcpy(&results
, buf
, sizeof(int32_t));
204 /**************************************************************************/
206 @brief Reads 8 bytes from EEPROM
209 The 16-bit address to read from in EEPROM
211 @return A unsigned 64-bit value (uint64_t)
213 /**************************************************************************/
214 uint64_t eepromReadU64(uint16_t addr
)
218 mcp24aaError_e error
= MCP24AA_ERROR_OK
;
219 error
= mcp24aaReadBuffer(addr
, buf
, sizeof(uint64_t));
221 // ToDo: Handle any errors
224 memcpy(&results
, buf
, sizeof(uint64_t));
228 /**************************************************************************/
230 @brief Reads 8 bytes from EEPROM
233 The 16-bit address to read from in EEPROM
235 @return A signed 64-bit value (int64_t)
237 /**************************************************************************/
238 int64_t eepromReadS64(uint16_t addr
)
242 mcp24aaError_e error
= MCP24AA_ERROR_OK
;
243 error
= mcp24aaReadBuffer(addr
, buf
, sizeof(int64_t));
245 // ToDo: Handle any errors
248 memcpy(&results
, buf
, sizeof(int64_t));
252 /**************************************************************************/
254 @brief Reads a variabls length buffer from EEPROM
257 The 16-bit address to write to in EEPROM
259 Pointer to the buffer that will store any retrieved bytes
260 @param[in] bufferLength
261 The number of bytes to read
263 /**************************************************************************/
264 void eepromReadBuffer(uint16_t addr
, uint8_t *buffer
, uint32_t bufferLength
)
266 // Instantiate error message placeholder
267 mcp24aaError_e error
= MCP24AA_ERROR_OK
;
269 // Read the contents of address
270 error
= mcp24aaReadBuffer(addr
, buffer
, bufferLength
);
272 // ToDo: Handle any errors
276 /**************************************************************************/
278 @brief Writes 1 byte to EEPROM
281 The 16-bit address to write to in EEPROM
283 /**************************************************************************/
284 void eepromWriteU8(uint16_t addr
, uint8_t value
)
286 mcp24aaError_e error
= MCP24AA_ERROR_OK
;
287 error
= mcp24aaWriteBuffer(addr
, (uint8_t *)&value
, sizeof(value
));
289 // ToDo: Handle any errors
293 /**************************************************************************/
295 @brief Writes 1 signed byte to EEPROM
298 The 16-bit address to write to in EEPROM
300 /**************************************************************************/
301 void eepromWriteS8(uint16_t addr
, int8_t value
)
303 mcp24aaError_e error
= MCP24AA_ERROR_OK
;
304 error
= mcp24aaWriteBuffer(addr
, (uint8_t *)&value
, sizeof(value
));
306 // ToDo: Handle any errors
310 /**************************************************************************/
312 @brief Writes an unsigned 16-bit integer to EEPROM
315 The 16-bit address to write to in EEPROM
317 /**************************************************************************/
318 void eepromWriteU16(uint16_t addr
, uint16_t value
)
320 mcp24aaError_e error
= MCP24AA_ERROR_OK
;
321 error
= mcp24aaWriteBuffer(addr
, (uint8_t *)&value
, sizeof(value
));
323 // ToDo: Handle any errors
327 /**************************************************************************/
329 @brief Writes a signed 16-bit integer to EEPROM
332 The 16-bit address to write to in EEPROM
334 /**************************************************************************/
335 void eepromWriteS16(uint16_t addr
, int16_t value
)
337 mcp24aaError_e error
= MCP24AA_ERROR_OK
;
338 error
= mcp24aaWriteBuffer(addr
, (uint8_t *)&value
, sizeof(value
));
340 // ToDo: Handle any errors
344 /**************************************************************************/
346 @brief Writes an unsigned 32-bit integer to EEPROM
349 The 16-bit address to write to in EEPROM
351 /**************************************************************************/
352 void eepromWriteU32(uint16_t addr
, uint32_t value
)
354 mcp24aaError_e error
= MCP24AA_ERROR_OK
;
355 error
= mcp24aaWriteBuffer(addr
, (uint8_t *)&value
, sizeof(value
));
357 // ToDo: Handle any errors
361 /**************************************************************************/
363 @brief Writes a signed 32-bit integer to EEPROM
366 The 16-bit address to write to in EEPROM
368 /**************************************************************************/
369 void eepromWriteS32(uint16_t addr
, int32_t value
)
371 mcp24aaError_e error
= MCP24AA_ERROR_OK
;
372 error
= mcp24aaWriteBuffer(addr
, (uint8_t *)&value
, sizeof(value
));
374 // ToDo: Handle any errors
378 /**************************************************************************/
380 @brief Writes an unsigned 64-bit integer to EEPROM
383 The 16-bit address to write to in EEPROM
385 /**************************************************************************/
386 void eepromWriteU64(uint16_t addr
, uint64_t value
)
388 mcp24aaError_e error
= MCP24AA_ERROR_OK
;
389 error
= mcp24aaWriteBuffer(addr
, (uint8_t *)&value
, sizeof(value
));
391 // ToDo: Handle any errors
395 /**************************************************************************/
397 @brief Writes a signed 64-bit integer to EEPROM
400 The 16-bit address to write to in EEPROM
402 /**************************************************************************/
403 void eepromWriteS64(uint16_t addr
, int64_t value
)
405 mcp24aaError_e error
= MCP24AA_ERROR_OK
;
406 error
= mcp24aaWriteBuffer(addr
, (uint8_t *)&value
, sizeof(value
));
408 // ToDo: Handle any errors
This page took 0.080268 seconds and 5 git commands to generate.