forked from mysensors/Raspberry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PiEEPROM.cpp
183 lines (150 loc) · 3.89 KB
/
PiEEPROM.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* PiEEPROM.cpp - AVR EEPROM like implementation for Raspberry Pi
*
* Copyright (C) 2014 Tomas Hozza <thozza@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <PiEEPROM.h>
#ifdef __cplusplus
extern "C" {
#endif
uint8_t _eeprom[EEPROM_SIZE] = {};
int eeprom_is_ready()
{
return 1;
}
/**
* Read one byte from EEPROM address __p.
*/
uint8_t eeprom_read_byte (const uint8_t *__p)
{
size_t addr = (size_t)__p;
if (addr < EEPROM_SIZE)
{
return *(_eeprom + addr);
}
return 0;
}
/**
* Read one 16-bit word (little endian) from EEPROM address __p.
*/
uint16_t eeprom_read_word (const uint16_t *__p)
{
size_t addr = (size_t)__p;
if (addr < EEPROM_SIZE - (sizeof(uint16_t) - sizeof(uint8_t)))
{
return (uint16_t)*(_eeprom + addr);
}
return 0;
}
/**
* Read one 32-bit double word (little endian) from EEPROM address __p.
*/
uint32_t eeprom_read_dword (const uint32_t *__p)
{
size_t addr = (size_t)__p;
if (addr < EEPROM_SIZE - (sizeof(uint32_t) - sizeof(uint8_t)))
{
return (uint32_t)*(_eeprom + addr);
}
return 0;
}
/**
* Read one float value (little endian) from EEPROM address __p.
*/
float eeprom_read_float (const float *__p)
{
size_t addr = (size_t)__p;
if (addr < EEPROM_SIZE - (sizeof(float) - sizeof(uint8_t)))
{
return (float)*(_eeprom + addr);
}
return 0;
}
/**
* Read a block of __n bytes from EEPROM address __src to SRAM __dst.
*/
void eeprom_read_block (void *__dst, const void *__src, size_t __n)
{
size_t addr = (size_t)__src;
if (addr < EEPROM_SIZE - (__n - sizeof(uint8_t)))
{
memcpy(__dst, (_eeprom + addr), __n);
}
}
/**
* Write a byte __value to EEPROM address __p.
*/
void eeprom_write_byte (uint8_t *__p, uint8_t __value)
{
size_t addr = (size_t)__p;
if (addr < EEPROM_SIZE)
{
memcpy((_eeprom + addr), &__value, sizeof(uint8_t));
}
}
/**
* Write a word __value to EEPROM address __p.
*/
void eeprom_write_word (uint16_t *__p, uint16_t __value)
{
size_t addr = (size_t)__p;
if (addr < EEPROM_SIZE - (sizeof(uint16_t) - sizeof(uint8_t)))
{
memcpy((_eeprom + addr), &__value, sizeof(uint16_t));
}
}
/**
* Write a 32-bit double word __value to EEPROM address __p.
*/
void eeprom_write_dword (uint32_t *__p, uint32_t __value)
{
size_t addr = (size_t)__p;
if (addr < EEPROM_SIZE - (sizeof(uint32_t) - sizeof(uint8_t)))
{
memcpy((_eeprom + addr), &__value, sizeof(uint32_t));
}
}
/**
* Write a float __value to EEPROM address __p.
*/
void eeprom_write_float (float *__p, float __value)
{
size_t addr = (size_t)__p;
if (addr < EEPROM_SIZE - (sizeof(float) - sizeof(uint8_t)))
{
memcpy((_eeprom + addr), &__value, sizeof(float));
}
}
/**
* Write a block of __n bytes to EEPROM address __dst from __src.
* The argument order is mismatch with common functions like strcpy().
*/
void eeprom_write_block (const void *__src, void *__dst, size_t __n)
{
size_t addr = (size_t)__dst;
if (addr < EEPROM_SIZE - (__n - sizeof(uint8_t)))
{
memcpy((_eeprom + addr), __src, __n);
}
}
#ifdef __cplusplus
}
#endif