Contiki 3.x
reset-sensor.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006, Swedish Institute of Computer Science.
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  */
32 
33 /**
34  * \file
35  * Utility to store a node id in the external flash
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 #include "contiki-conf.h"
41 #include "dev/xmem.h"
42 #include <string.h>
43 #include "lib/sensors.h"
44 #include "contiki.h"
45 
46 const struct sensors_sensor reset_sensor;
47 
48 /*---------------------------------------------------------------------------*/
49 static int reset_counter_get(int type)
50 {
51  unsigned char buf[12]; //create buffer
52  xmem_pread(buf, 12, RESET_COUNTER_XMEM_OFFSET); //read buffer from flash
53  if(buf[0] == 0x21 && //check id byte
54  buf[1] == 0x42) { //and again
55  return (buf[2] << 8) | buf[3]; //return buffer
56  } else {
57  return 0; //return no counter
58  }
59 }
60 /*---------------------------------------------------------------------------*/
61 static int reset_counter_update(int type, int c)
62 {
63  unsigned char buf[12]; //create a buffer to write to
64  int reset_counter = reset_counter_get(0); //get current reset counter
65  reset_counter++; //increment counter
66  buf[0] = 0x21; //id byte 1
67  buf[1] = 0x42; //id byte 2
68  buf[2] = reset_counter >> 8; //upper byte of counter
69  buf[3] = reset_counter & 0xff; //lower byte of counter
70  xmem_erase(XMEM_ERASE_UNIT_SIZE, RESET_COUNTER_XMEM_OFFSET); //erase
71  xmem_pwrite(buf, 12, RESET_COUNTER_XMEM_OFFSET); //write
72  return reset_counter;
73 }
74 /*---------------------------------------------------------------------------*/
75 void reset_counter_reset()
76 {
77  unsigned char buf[12]; //create a buffer to write to
78  unsigned int reset_counter = 0; //get current reset counter
79  buf[0] = 0x21; //id byte 1
80  buf[1] = 0x42; //id byte 2
81  buf[2] = reset_counter >> 8; //upper byte of counter
82  buf[3] = reset_counter & 0xff; //lower byte of counter
83  xmem_erase(XMEM_ERASE_UNIT_SIZE, RESET_COUNTER_XMEM_OFFSET); //erase
84  xmem_pwrite(buf, 12, RESET_COUNTER_XMEM_OFFSET); //write
85 }
86 /*---------------------------------------------------------------------------*/
87 static int reset_counter_status(int type)
88 {
89  return 1;
90 }
91 
92 SENSORS_SENSOR(reset_sensor, "Resets",
93  reset_counter_get , reset_counter_update , reset_counter_status);
94 
95