Passed tests on Raven 1284p in 3 seconds with 56KB program memory disk
[contiki-2.x.git] / platform / sky / checkpoint-arch.c
blob10ec563e9fa0317eba3126df8df8cb1880b7bc89
1 /*
2 * Copyright (c) 2009, Swedish Institute of Computer Science
3 * All rights reserved.
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.
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.
29 * This file is part of the Contiki operating system.
33 /**
34 * \file
35 * Checkpoint library implementation for the Tmote Sky platform.
36 * \author
37 * Fredrik Osterlind <fros@sics.se>
40 #include "contiki.h"
41 #include "lib/checkpoint.h"
43 #include "sys/rtimer.h"
44 #include "sys/mt.h"
45 #include "cfs/cfs.h"
46 #include "cfs/cfs-coffee.h"
47 #include "dev/leds.h"
48 #include "dev/watchdog.h"
50 #include <io.h>
51 #include <signal.h>
52 #include <stdio.h>
54 #define DEBUG 1
55 #if DEBUG
56 #define PRINTF(...) printf(__VA_ARGS__)
57 #else
58 #define PRINTF(...)
59 #endif
61 #define COMMAND_ROLLBACK 1
62 #define COMMAND_CHECKPOINT 2
63 #define COMMAND_TBR 3
65 #define DATA_AS_HEX 0 /* If false, store binary data */
67 #define INCLUDE_RAM 1 /* Less then 10240 bytes */
68 #define INCLUDE_TIMERS 1 /* 16 bytes */
69 #define INCLUDE_LEDS 1 /* 1 bytes */
71 /* 10kb memory */
72 #define RAM_START 0x1100
73 #define RAM_END 0x3900
75 #define STOP_TIMERS() TACTL &= ~(MC1); TBCTL &= ~(MC1); watchdog_stop();
76 #define START_TIMERS() watchdog_start(); TACTL |= MC1; TBCTL |= MC1;
78 static struct mt_thread checkpoint_thread;
79 static uint8_t preset_cmd;
80 static int preset_fd;
82 typedef union {
83 unsigned char u8[2];
84 unsigned short u16;
85 } word_union_t;
87 /*---------------------------------------------------------------------------*/
88 static void
89 write_byte(int fd, uint8_t c)
91 #if DATA_AS_HEX
92 uint8_t hex[2];
93 sprintf(hex, "%02x", c);
94 if(cfs_write(fd, hex, 2) != 2) {
95 printf("err #1\n");
97 #else /* DATA_AS_HEX */
98 if(cfs_write(fd, &c, 1) != 1) {
99 printf("err #2\n");
101 #endif /* DATA_AS_HEX */
102 }/*---------------------------------------------------------------------------*/
103 #if 0
104 static void
105 write_array(int fd, unsigned char *mem, uint16_t len)
107 #if DATA_AS_HEX
108 int i;
109 for(i = 0; i < len; i++) {
110 write_byte(fd, mem[i]);
112 #else /* DATA_AS_HEX */
113 cfs_write(fd, mem, len);
114 #endif /* DATA_AS_HEX */
116 #endif /* 0 */
117 /*---------------------------------------------------------------------------*/
118 static void
119 write_word(int fd, uint16_t w)
121 word_union_t tmp;
122 tmp.u16 = w;
123 write_byte(fd, tmp.u8[0]);
124 write_byte(fd, tmp.u8[1]);
126 /*---------------------------------------------------------------------------*/
127 static uint8_t
128 read_byte(int fd)
130 #if DATA_AS_HEX
131 uint8_t hex[2];
133 cfs_read(fd, hex, 2);
135 if(hex[0] >= 'A' && hex[0] <= 'F') {
136 hex[0] = (hex[0] - 'A' + 0xa);
137 } else if(hex[0] >= 'a' && hex[0] <= 'f') {
138 hex[0] = (hex[0] - 'a' + 0xa);
139 } else {
140 hex[0] = (hex[0] - '0');
142 if(hex[1] >= 'A' && hex[1] <= 'F') {
143 hex[1] = (hex[1] - 'A' + 0xa);
144 } else if(hex[1] >= 'a' && hex[1] <= 'f') {
145 hex[1] = (hex[1] - 'a' + 0xa);
146 } else {
147 hex[1] = (hex[1] - '0');
149 return (uint8_t)((hex[0]<<4)&0xf0) | (hex[1]&0x0f);
150 #else /* DATA_AS_HEX */
151 uint8_t c;
152 cfs_read(fd, &c, 1);
153 return c;
154 #endif /* DATA_AS_HEX */
156 /*---------------------------------------------------------------------------*/
157 static uint16_t
158 read_word(int fd)
160 word_union_t tmp;
161 tmp.u8[0] = read_byte(fd);
162 tmp.u8[1] = read_byte(fd);
163 return tmp.u16;
165 /*---------------------------------------------------------------------------*/
166 static void
167 thread_checkpoint(int fd)
169 #if INCLUDE_RAM
170 unsigned char *addr;
171 uint16_t size = 0;
172 unsigned char *thread_mem_start = (unsigned char *)&checkpoint_thread.thread.stack;
173 unsigned char *thread_mem_end = thread_mem_start + sizeof(checkpoint_thread.thread.stack) - 1;
174 unsigned char *coffee_mem_start = cfs_coffee_get_protected_mem(&size);
175 unsigned char *coffee_mem_end = coffee_mem_start + size - 1;
176 #endif /* INCLUDE_RAM */
178 /*printf("protected thread memory: %u, size=%u\n", (uint16_t) thread_mem_start, sizeof(checkpoint_thread.thread.stack));*/
179 /*printf("protected coffee memory: %u, size=%u\n", (uint16_t) coffee_mem_start, size);*/
181 /* RAM */
182 #if INCLUDE_RAM
183 for(addr = (unsigned char *)RAM_START;
184 addr < (unsigned char *)RAM_END;
185 addr++) {
187 if((addr >= thread_mem_start && addr <= thread_mem_end)) {
188 /* Writing dummy memory */
189 /*write_byte(fd, 1);*/
190 continue;
193 if((addr >= coffee_mem_start && addr <= coffee_mem_end)) {
194 /* Writing dummy memory */
195 /*write_byte(fd, 2);*/
196 continue;
199 /* TODO Use write_array() */
200 write_byte(fd, *addr);
202 if(((int)addr % 512) == 0) {
203 PRINTF(".");
207 #endif /* INCLUDE_RAM */
209 /* Timers */
210 #if INCLUDE_TIMERS
211 write_word(fd, TACTL);
212 write_word(fd, TACCTL1);
213 write_word(fd, TACCR1);
214 write_word(fd, TAR);
216 write_word(fd, TBCTL);
217 write_word(fd, TBCCTL1);
218 write_word(fd, TBCCR1);
219 write_word(fd, TBR);
220 #endif /* INCLUDE_TIMERS */
222 /* LEDs */
223 #if INCLUDE_LEDS
224 write_byte(fd, leds_arch_get());
225 #endif /* INCLUDE_LEDS */
227 /* Radio */
228 /* ADC */
229 /* ... */
231 write_byte(fd, -1); /* Coffee padding byte */
233 /*---------------------------------------------------------------------------*/
234 static void
235 thread_rollback(int fd)
237 #if INCLUDE_RAM
238 unsigned char *addr;
239 uint16_t size = 0;
240 unsigned char *thread_mem_start = (unsigned char *)&checkpoint_thread.thread.stack;
241 unsigned char *thread_mem_end = thread_mem_start + sizeof(checkpoint_thread.thread.stack) - 1;
242 unsigned char *coffee_mem_start = cfs_coffee_get_protected_mem(&size);
243 unsigned char *coffee_mem_end = coffee_mem_start + size - 1;
244 #endif /* INCLUDE_RAM */
246 /*printf("protected thread memory: %u, size=%u\n", (uint16_t) thread_mem_start, sizeof(checkpoint_thread.thread.stack));*/
247 /*printf("protected coffee memory: %u, size=%u\n", (uint16_t) coffee_mem_start, size);*/
249 /* RAM */
250 #if INCLUDE_RAM
251 for(addr = (unsigned char *)RAM_START;
252 addr < (unsigned char *)RAM_END;
253 addr++) {
254 if((addr >= thread_mem_start && addr <= thread_mem_end)) {
255 /* Ignoring incoming memory */
256 /*read_byte(fd);*/
257 continue;
260 if((addr >= coffee_mem_start && addr <= coffee_mem_end)) {
261 /* Ignoring incoming memory */
262 /*read_byte(fd);*/
263 continue;
266 *addr = read_byte(fd);
268 if(((int)addr % 512) == 0) {
269 PRINTF(".");
273 #endif /* INCLUDE_RAM */
275 /* Timers */
276 #if INCLUDE_TIMERS
277 TACTL = read_word(fd);
278 TACCTL1 = read_word(fd);
279 TACCR1 = read_word(fd);
280 TAR = read_word(fd);
282 TBCTL = read_word(fd);
283 TBCCTL1 = read_word(fd);
284 TBCCR1 = read_word(fd);
285 TBR = read_word(fd);
286 #endif /* INCLUDE_TIMERS */
288 /* LEDs */
289 #if INCLUDE_LEDS
290 leds_arch_set(read_byte(fd));
291 #endif /* INCLUDE_LEDS */
293 /* Radio */
294 /* ADC */
295 /* ... */
297 read_byte(fd); /* Coffee padding byte */
299 /*---------------------------------------------------------------------------*/
300 static void
301 thread_loop(void *data)
303 uint8_t cmd;
304 int fd;
306 while(1) {
307 /* Store command and file descriptor on stack */
308 cmd = preset_cmd;
309 fd = preset_fd;
311 /* Handle command */
312 if(cmd == COMMAND_ROLLBACK) {
313 PRINTF("Rolling back");
314 thread_rollback(fd);
315 PRINTF(" done!\n");
316 } else if(cmd == COMMAND_CHECKPOINT) {
317 PRINTF("Checkpointing");
318 thread_checkpoint(fd);
319 PRINTF(" done!\n");
320 } else if(cmd == COMMAND_TBR) {
321 PRINTF("Writing TBR");
322 write_word(fd, TBR);
323 PRINTF(" done!\n");
324 } else {
325 printf("Error: unknown command: %u\n", cmd);
328 /* Return to main Contiki thread */
329 mt_yield();
332 /*---------------------------------------------------------------------------*/
334 checkpoint_arch_size()
336 return 10258;
338 /*---------------------------------------------------------------------------*/
339 void
340 checkpoint_arch_checkpoint(int fd)
342 STOP_TIMERS();
344 preset_cmd = COMMAND_CHECKPOINT;
345 preset_fd = fd;
346 mt_exec(&checkpoint_thread);
348 START_TIMERS();
350 /*---------------------------------------------------------------------------*/
351 void
352 checkpoint_arch_rollback(int fd)
354 STOP_TIMERS();
356 preset_cmd = COMMAND_ROLLBACK;
357 preset_fd = fd;
358 mt_exec(&checkpoint_thread);
360 START_TIMERS();
362 /*---------------------------------------------------------------------------*/
363 void
364 checkpoint_arch_init(void)
366 mt_init();
367 mt_start(&checkpoint_thread, thread_loop, NULL);
369 /*mt_stop(&checkpoint_thread);*/
370 /*mt_remove();*/
372 /*---------------------------------------------------------------------------*/