1 /* vim:ts=4 sts=4 et tw=80
5 * for additional information please
6 * see http://lochraster.org/fnordlichtmini
8 * (c) by Alexander Neumann <alexander@bumpern.de>
10 * This program is free software: you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 3 as published by
12 * the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "../common/pt/pt.h"
26 #include <avr/eeprom.h>
27 #include <util/crc16.h>
30 /* global structures */
31 struct storage_config_t startup_config
;
32 EEMEM
struct storage_t eeprom_storage
;
35 struct storage_internal_t
39 /* buffer for nonblocking read/write */
40 uint8_t buf
[STORAGE_BUFFER_SIZE
];
43 uint8_t bytes_remaining
;
52 static struct storage_internal_t storage
;
54 static uint16_t eeprom_checksum(void)
56 uint16_t checksum
= 0;
57 uint8_t *start
= NULL
;
59 for (uint16_t i
= 0; i
< sizeof(struct storage_t
)-2; i
++)
60 checksum
= _crc16_update(checksum
, eeprom_read_byte(start
++));
65 void storage_init(void)
67 /* compare checksum and eeprom configuration content */
68 uint16_t checksum1
= eeprom_checksum();
69 uint16_t checksum2
= eeprom_read_word(&eeprom_storage
.checksum
);
70 storage
.eeprom_good
= (checksum1
== checksum2
);
71 storage_load_config();
74 /* initialize state structure */
75 storage
.state
= STORAGE_IDLE
;
76 PT_INIT(&storage
.thread
);
80 static PT_THREAD(storage_thread(struct pt
*thread
))
85 if (storage
.state
== STORAGE_WRITE
) {
90 while (storage
.bytes_remaining
--) {
91 eeprom_write_byte(storage
.address
++, storage
.buf
[storage
.index
++]);
93 while (!eeprom_is_ready())
98 eeprom_write_word(&eeprom_storage
.checksum
, eeprom_checksum());
99 while (!eeprom_is_ready())
102 /* release int line */
103 remote_release_int();
105 storage
.state
= STORAGE_IDLE
;
114 void storage_poll(void)
116 /* call storage thread */
117 if (storage
.state
!= STORAGE_IDLE
)
118 PT_SCHEDULE(storage_thread(&storage
.thread
));
121 void storage_save_config(void)
123 if (storage
.state
!= STORAGE_IDLE
)
126 /* set magic byte and save startup_config to EEPROM */
127 startup_config
.magic
= EEPROM_MAGIC_BYTE
;
129 /* copy data to buffer */
130 memcpy(&storage
.buf
[0], &startup_config
, sizeof(struct storage_config_t
));
131 /* set address and bytes_remaining */
133 storage
.bytes_remaining
= sizeof(struct storage_config_t
);
134 storage
.address
= (uint8_t *)&eeprom_storage
.config
;
136 storage
.state
= STORAGE_WRITE
;
138 /* reset magic config and mark EEPROM as good */
139 startup_config
.magic
= 0;
140 storage
.eeprom_good
= true;
143 void storage_load_config(void)
145 if (!storage
.eeprom_good
)
149 eeprom_read_block(&startup_config
, &eeprom_storage
.config
, sizeof(struct storage_config_t
));
152 void storage_save_color(uint8_t position
, struct storage_color_t
*color
)
154 if (storage
.state
!= STORAGE_IDLE
)
157 /* copy data to buffer */
158 memcpy(&storage
.buf
[0], color
, sizeof(struct storage_color_t
));
159 /* set address and bytes_remaining */
161 storage
.bytes_remaining
= sizeof(struct storage_color_t
);
162 storage
.address
= (uint8_t *)&eeprom_storage
.color
[position
];
164 storage
.state
= STORAGE_WRITE
;
167 void storage_load_color(uint8_t position
, struct storage_color_t
*color
)
169 eeprom_read_block(color
, &eeprom_storage
.color
[position
], sizeof(struct storage_color_t
));
172 bool storage_valid_config(void)
174 return (storage
.eeprom_good
&& startup_config
.magic
== EEPROM_MAGIC_BYTE
);