Contiki 3.x
reset-sensor.c
1 #include <string.h>
2 #include <stdint.h>
3 #include "contiki-conf.h"
4 #include "contiki.h"
5 #include "reset-sensor.h"
6 #include "dev/rom-util.h"
7 #include "dev/cc2538-dev.h"
8 
9 /**
10  * Reset counter in ROM
11  * On first boot after being flashed, with will be 0xFFFFFFFF (due to being erased by the flasher),
12  * which will naturally wrap around to 0.
13  */
14 uint32_t const volatile * const reset_counter = (uint32_t *) CC2538_DEV_FLASH_ADDR;
15 
16 // Ensure we have enough room in the reserved space for the reset_counter
17 _Static_assert(sizeof(*reset_counter) < RESET_SENSOR_SIZE, "Reset sensor size mismatch");
18 
19 const struct sensors_sensor reset_sensor;
20 
21 static int reset_counter_get(int type) {
22  return *reset_counter;
23 }
24 
25 static int reset_counter_update(int type, int c) {
26  uint32_t count = *reset_counter + 1;
27 
28  // We can only change bits from 1 -> 0 without erasing the whole page
29  reset_counter_reset();
30  rom_util_program_flash(&count, (uintptr_t) reset_counter, sizeof(*reset_counter));
31 
32  return reset_counter_get(0);
33 }
34 
35 void reset_counter_reset(void) {
36  rom_util_page_erase((uintptr_t) reset_counter, sizeof(*reset_counter));
37 }
38 
39 static int reset_counter_status(int type) {
40  return 1;
41 }
42 
43 SENSORS_SENSOR(reset_sensor, "Resets", reset_counter_get, reset_counter_update, reset_counter_status);
Header file for the cc2538 devices definitions.
Header file for the cc2538 ROM utility function library driver.
static volatile clock_time_t count
These routines define the AVR-specific calls declared in /core/sys/clock.h CLOCK_SECOND is the number...
Definition: clock.c:80
#define CC2538_DEV_FLASH_ADDR
Flash address.
Definition: cc2538-dev.h:96