Contiki 3.x
ms-io-arch.c
1 #include "dev/uart1_i2c_master.h"
2 #include "dev/ds3231-sensor.h" // Clock
3 #include "dev/adc1-sensor.h" // ADC 1
4 #include "dev/adc2-sensor.h" // ADC 2
5 #include "dev/batv-sensor.h" // Batt
6 #include "adxl345.h" // Accel
7 #include "dev/event-sensor.h" //event sensor (rain)
8 #include "mountainsensing/common/ms-io.h"
9 #include "utc_time.h"
10 #include "contiki-conf.h"
11 #include "contiki.h"
12 #include "dev/reset-sensor.h"
13 
14 #define DEBUG_ON
16 
17 #define ADC_ACTIVATE_DELAY 10 //delay in ticks of the rtimer PLATFORM DEPENDANT!
18 
19 /**
20  * Earliest time supported by the rtc - 2000/01/01 00:00:00
21  */
22 #define EARLIEST_EPOCH 946684800
23 
24 static void ms_radio_on(void);
25 static void ms_radio_off(void);
26 
27 void ms_init(void) {
28  // Set up sense control pin
29  SENSE_EN_PORT(SEL) &= ~BV(SENSE_EN_PIN);
30  SENSE_EN_PORT(DIR) |= BV(SENSE_EN_PIN);
31  SENSE_EN_PORT(REN) &= ~BV(SENSE_EN_PIN);
32  SENSE_EN_PORT(OUT) &= ~BV(SENSE_EN_PIN);
33 
34  //Make sure all analogue input pins are inputs.
35  P6DIR = 0x00;
36  P6SEL = 0x00;
37 
38  /* Ensure that rain bucket is input. */
39  P2DIR &= ~BV(0);
40 
41  // Set up radio control pin
42  RADIO_EN_PORT(SEL) &= ~BV(RADIO_EN_PIN);
43  RADIO_EN_PORT(DIR) |= BV(RADIO_EN_PIN);
44  RADIO_EN_PORT(REN) &= ~BV(RADIO_EN_PIN);
45  DEBUG("\tTurning on radio\n");
46  //Turn on by default
47  ms_radio_on();
48  ms_sense_off(); //turn off sensors by default
49 
50  // Turn the rain sensor on.
51  // It needs to be permanently on to count the rain ticks.
52  SENSORS_ACTIVATE(event_sensor);
53 }
54 
55 void ms_radio_on(void) {
56  DEBUG("Turning on radio\n");
57  RADIO_EN_PORT(OUT) |= BV(RADIO_EN_PIN);
58 }
59 
60 void ms_radio_off(void) {
61  DEBUG("Turning off radio\n");
62  RADIO_EN_PORT(OUT) &= ~BV(RADIO_EN_PIN);
63 }
64 
65 void ms_sense_on(void){
66  DEBUG("Turning on sense\n");
67  SENSE_EN_PORT(OUT) |= BV(SENSE_EN_PIN);
68 }
69 
70 void ms_sense_off(void){
71  DEBUG("Turning off sense\n");
72  SENSE_EN_PORT(OUT) &= ~BV(SENSE_EN_PIN);
73 }
74 
75 bool ms_get_temp(float *temp) {
76  *temp = ((float) ds3231_temperature()) / 100;
77  return true;
78 }
79 
80 bool ms_get_batt(float *batt) {
81  ms_sense_on();
82  rtimer_clock_t t0;
83  SENSORS_ACTIVATE(batv_sensor);
84  t0 = RTIMER_NOW();
85  while(RTIMER_CLOCK_LT(RTIMER_NOW(), (t0 + (uint32_t) ADC_ACTIVATE_DELAY)));
86  *batt = (float)(batv_sensor.value(0)) / 184.06 - 0.2532;
87  SENSORS_DEACTIVATE(batv_sensor);
88  return true;
89 }
90 
91 bool ms_get_time(uint32_t *seconds) {
92  struct tm t;
93  ds3231_get_time(&t);
94 
95  *seconds = (uint32_t) tm_to_epoch(&t);
96 
97  //DEBUG("years %d, months %d, days %d, hours %d, minutes %d, seconds %d\n", t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
98  //DEBUG("epoch is %" PRIu32 "\n", seconds);
99  return true;
100 }
101 
102 bool ms_set_time(uint32_t seconds) {
103  if (seconds < EARLIEST_EPOCH) {
104  return false;
105  }
106 
107  struct tm t;
108  epoch_to_tm((time_t *) &seconds, &t);
109 
110  //DEBUG("years %d, months %d, days %d, hours %d, minutes %d, seconds %d\n", t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
111  //DEBUG("epoch %" PRIu32 "\n", seconds);
112 
113  return ds3231_set_time(&t) == 0;
114 }
115 
116 bool ms_get_rain(uint32_t *rain) {
117  *rain = (uint32_t) event_sensor.value(1);
118  return true;
119 }
120 
121 bool ms_get_adc1(uint32_t *adc1) {
122  rtimer_clock_t t0;
123  SENSORS_ACTIVATE(adc1_sensor);
124  t0 = RTIMER_NOW();
125  while(RTIMER_CLOCK_LT(RTIMER_NOW(), (t0 + (uint32_t) ADC_ACTIVATE_DELAY)));
126  *adc1 = adc1_sensor.value(0);
127  SENSORS_DEACTIVATE(adc1_sensor);
128  return true;
129 }
130 
131 bool ms_get_adc2(uint32_t *adc2) {
132  rtimer_clock_t t0;
133  SENSORS_ACTIVATE(adc2_sensor);
134  t0 = RTIMER_NOW();
135  while(RTIMER_CLOCK_LT(RTIMER_NOW(), (t0 + (uint32_t) ADC_ACTIVATE_DELAY)));
136  *adc2 = adc2_sensor.value(0);
137  SENSORS_DEACTIVATE(adc2_sensor);
138  return true;
139 }
140 
141 bool ms_get_acc(int32_t *x, int32_t *y, int32_t *z) {
142  // Unsupported
143  return false;
144 }
145 
146 bool ms_get_humid(float *humid) {
147  // Unsupported
148  return false;
149 }
150 
151 bool ms_get_reboot(uint16_t *reboot) {
152  // Param is ignored by the reset sensor
153  *reboot = reset_sensor.value(0);
154  return true;
155 }
156 
157 bool ms_reset_reboot(void) {
158  reset_counter_reset();
159  return true;
160 }
#define EARLIEST_EPOCH
Earliest time supported by the rtc - 2000/01/01 00:00:00.
Definition: ds3231-sensor.h:49
Debug helpers.
#define RTIMER_NOW()
Get the current clock time.
Definition: rtimer.h:135
Sensors for DS3231 (RTC with Temperature Sensor).
int ds3231_get_time(struct tm *t)
ds3231_get_time
Utility functions for operating on UTC times.
I2C communication device driver header file for Zolertia Z1 sensor node.
time_t tm_to_epoch(struct tm *const tmp)
Convert a tm struct to a UNIX epoch.
Definition: tm_to_epoch.c:112
int ds3231_set_time(struct tm *t)
ds3231_set_time
int ds3231_temperature(void)
ds3231_temperature
tm
Definition: utc_time.h:20
void epoch_to_tm(const time_t *timer, struct tm *const tmp)
Convert a UNIX epoch to a tm struct.