Contiki-NG
Toggle main menu visibility
Loading...
Searching...
No Matches
eeprom.c
1
/*
2
* Copyright (c) 2013, Robert Quattlebaum.
3
* All rights reserved.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
7
* are met:
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
13
* 3. Neither the name of the Institute nor the names of its contributors
14
* may be used to endorse or promote products derived from this software
15
* without specific prior written permission.
16
*
17
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
* SUCH DAMAGE.
28
*
29
* This file is part of the Contiki operating system.
30
*
31
* Author: Robert Quattlebaum <darco@deepdarc.com>
32
*
33
*/
34
35
#include <errno.h>
36
#include <stdio.h>
37
#include <stdlib.h>
38
#include <string.h>
39
40
#include "contiki.h"
41
#include "
dev/eeprom.h
"
42
43
/* Log configuration */
44
#include "
sys/log.h
"
45
#define LOG_MODULE "EEPROM"
46
#define LOG_LEVEL LOG_LEVEL_MAIN
47
48
static
FILE *eeprom_file;
49
50
/*---------------------------------------------------------------------------*/
51
static
bool
52
eeprom_fill(eeprom_addr_t
addr
,
unsigned
char
value
,
size_t
size)
53
{
54
if
(!eeprom_file) {
55
return
false
;
56
}
57
58
if
(
addr
> EEPROM_END_ADDR ||
addr
+ size > EEPROM_END_ADDR + 1) {
59
LOG_ERR(
"Bad address and/or size (addr = %04x, size = %zu)\n"
,
addr
, size);
60
return
false
;
61
}
62
63
if
(fseek(eeprom_file,
addr
, SEEK_SET) < 0) {
64
LOG_ERR(
"fseek() failed: %s\n"
, strerror(errno));
65
return
false
;
66
}
67
68
while
(size--) {
69
if
(fputc(
value
, eeprom_file) !=
value
) {
70
LOG_ERR(
"fputc() failed\n"
);
71
return
false
;
72
}
73
}
74
return
true
;
75
}
76
77
/*---------------------------------------------------------------------------*/
78
bool
79
eeprom_init
(
void
)
80
{
81
if
(eeprom_file) {
82
LOG_WARN(
"Re-initializing EEPROM\n"
);
83
fclose(eeprom_file);
84
eeprom_file = NULL;
85
}
86
87
if
(EEPROM_SIZE == 0) {
88
LOG_ERR(
"Cannot initialize EEPROM when EEPROM_SIZE is 0\n"
);
89
return
false
;
90
}
91
92
char
*eeprom_filename = getenv(
"CONTIKI_EEPROM"
);
93
if
(eeprom_filename != NULL) {
94
LOG_INFO(
"Using EEPROM file \"%s\"\n"
, eeprom_filename);
95
}
else
{
96
LOG_INFO(
"CONTIKI_EEPROM env is not set; using a temp file instead\n"
);
97
}
98
99
eeprom_file = eeprom_filename ? fopen(eeprom_filename,
"w+"
) : tmpfile();
100
101
if
(eeprom_file == NULL) {
102
LOG_ERR(
"Unable to open the EEPROM file\n"
);
103
goto
error;
104
}
105
106
107
/* Make sure that the file is the correct size by seeking
108
* to the end and checking the file position. If it is
109
* less than what we expect, we pad out the rest of the file
110
* with 0xFF, just like a real EEPROM. */
111
112
if
(fseek(eeprom_file, 0, SEEK_END) == -1) {
113
LOG_ERR(
"fseek() failed: %s\n"
, strerror(errno));
114
goto
error;
115
}
116
117
off_t length = ftell(eeprom_file);
118
if
(length < 0) {
119
LOG_ERR(
"ftell() failed\n"
);
120
goto
error;
121
}
122
123
if
(length < EEPROM_END_ADDR) {
124
/* Fill with 0xFF, just like a real EEPROM. */
125
if
(eeprom_fill(length, 0xFF, EEPROM_SIZE - length) ==
false
) {
126
goto
error;
127
}
128
}
129
130
return
true
;
131
132
error:
133
if
(eeprom_file != NULL) {
134
fclose(eeprom_file);
135
eeprom_file = NULL;
136
}
137
return
false
;
138
}
139
140
/*---------------------------------------------------------------------------*/
141
bool
142
eeprom_write
(eeprom_addr_t
addr
,
const
unsigned
char
*buf,
size_t
size)
143
{
144
if
(!eeprom_file) {
145
return
false
;
146
}
147
148
if
(
addr
> EEPROM_END_ADDR ||
addr
+ size > EEPROM_END_ADDR + 1) {
149
LOG_ERR(
"Bad address and/or size (addr = %04x, size = %zu)\n"
,
addr
, size);
150
return
false
;
151
}
152
153
if
(fseek(eeprom_file,
addr
, SEEK_SET) < 0) {
154
LOG_ERR(
"fseek() failed: %s\n"
, strerror(errno));
155
return
false
;
156
}
157
158
if
(fwrite(buf, 1, size, eeprom_file) != size) {
159
LOG_ERR(
"fwrite() failed\n"
);
160
return
false
;
161
}
162
163
return
true
;
164
}
165
166
/*---------------------------------------------------------------------------*/
167
bool
168
eeprom_read
(eeprom_addr_t
addr
,
unsigned
char
*buf,
size_t
size)
169
{
170
if
(!eeprom_file) {
171
return
false
;
172
}
173
174
if
(
addr
> EEPROM_END_ADDR ||
addr
+ size > EEPROM_END_ADDR + 1) {
175
LOG_ERR(
"Bad address and/or size (addr = %04x, size = %zu)\n"
,
addr
, size);
176
return
false
;
177
}
178
179
if
(fseek(eeprom_file,
addr
, SEEK_SET) < 0) {
180
LOG_ERR(
"fseek() failed: %s\n"
, strerror(errno));
181
return
false
;
182
}
183
184
if
(fread(buf, 1, size, eeprom_file) != size) {
185
LOG_ERR(
"fread() failed\n"
);
186
return
false
;
187
}
188
189
return
true
;
190
}
eeprom.h
EEPROM functions.
value
static int value(int type)
Definition
cc2538-temp-sensor.c:49
eeprom_write
bool eeprom_write(eeprom_addr_t addr, const unsigned char *buf, size_t size)
Write a buffer into EEPROM.
Definition
eeprom.c:142
eeprom_init
bool eeprom_init(void)
Initialize the EEPROM module.
Definition
eeprom.c:79
eeprom_read
bool eeprom_read(eeprom_addr_t addr, unsigned char *buf, size_t size)
Read data from the EEPROM.
Definition
eeprom.c:168
log.h
Header file for the logging system.
addr
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
Definition
uip-nd6.c:107
arch
cpu
native
dev
eeprom.c
Generated on
for Contiki-NG by
1.17.0