Contiki 3.x
ds3231-sensor.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Timothy Rule <trule.github@nym.hush.com>
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 
30 /**
31  * @file
32  * Sensors for DS3231 (RTC with Temperature Sensor).
33  * @author
34  * Timothy Rule <trule.github@nym.hush.com>
35  * @note
36  * See http://www.maxim-ic.com/datasheet/index.mvp/id/4627
37  */
38 
39 #ifndef __DS3231_SENSOR__
40 #define __DS3231_SENSOR__
41 
42 #include <lib/sensors.h>
43 #include <stdbool.h>
44 #include "time.h"
45 
46 /**
47  * Earliest time supported by the rtc - 2000/01/01 00:00:00
48  */
49 #define EARLIEST_EPOCH 946684800
50 
51 /**
52  * DS3231 Sensor and Config Command List.
53  */
54 #define DS3231_SENSOR_TEMP 0
55 #define DS3231_SENSOR_GET_EPOCH_SECONDS_MSB 1
56 #define DS3231_SENSOR_GET_EPOCH_SECONDS_LSB 2
57 #define DS3231_CONFIG_SET_TIME 55
58 #define DS3231_CONFIG_SET_ALARM 56
59 #define DS3231_CONFIG_CLEAR_ALARM 57
60 
61 #define DS3231_MASTER 0x00
62 #define DS3231_ADDR 0x68 //0xd0 with shift
63 #define DS3231_CONTROL_A1IE_SET_MASK 0x05
64 #define DS3231_CONTROL_A1IE_CLEAR_MASK 0xfe
65 #define DS3231_STATUS_A1F_CLEAR_MASK 0xfe
66 
67 typedef union {
68  struct {
69  /* Start Address on RTC for time registers. */
70  uint8_t address;
71  /* Byte 0 */
72  uint8_t sec : 4;
73  uint8_t dsec : 3;
74  uint8_t : 1;
75  /* Byte 1 */
76  uint8_t min : 4;
77  uint8_t dmin : 3;
78  uint8_t : 1;
79  /* Byte 2 */
80  uint8_t hour : 4;
81  uint8_t dhour : 2;
82  uint8_t : 2; /* Runs 24 hour time (default). */
83  /* Byte 3 */
84  uint8_t day : 3;
85  uint8_t : 5;
86  /* Byte 4 */
87  uint8_t date : 4;
88  uint8_t ddate : 2;
89  uint8_t : 2;
90  /* Byte 5 */
91  uint8_t mon : 4;
92  uint8_t dmon : 1;
93  uint8_t : 2;
94  uint8_t cent : 1;
95  /* Byte 6 */
96  uint8_t year : 4;
97  uint8_t dyear : 4;
98  } tm;
99  uint8_t data[8];
100 } ds_3231_time_t;
101 
102 typedef union {
103  struct {
104  /* Start Address on RTC for alarm registers. */
105  uint8_t address;
106  /* Byte 0 */
107  uint8_t sec : 4;
108  uint8_t dsec : 3;
109  uint8_t a1m1 : 1;
110  /* Byte 1 */
111  uint8_t min : 4;
112  uint8_t dmin : 3;
113  uint8_t a1m2: 1;
114  /* Byte 2 */
115  uint8_t hour : 4;
116  uint8_t dhour : 2;
117  uint8_t : 1; /* Runs 24 hour time (default). */
118  uint8_t a1m3 : 1;
119  /* Byte 3 */
120  uint8_t date : 4;
121  uint8_t ddate : 2;
122  uint8_t : 1; /* Date based alarm (default) */
123  uint8_t a1m4 : 1;
124  } tm;
125  uint8_t data[5];
126 } ds_3231_alarm_t;
127 
128 /**
129  * Export the DS3231 sensor object.
130  *
131  * Can be called as follows:
132  *
133  * #include <dev/ds3231-sensor.h>
134  *
135  * SENSORS_ACTIVATE(ds3231_sensor);
136  * ds3231_sensor.configure(DS3231_CONFIG_SET_TIME, (int)&t);
137  * ds3231_sensor.configure(DS3231_CONFIG_SET_ALARM, (int)&t);
138  * ds3231_sensor.value(DS3231_SENSOR_TEMP);
139  * ds3231_sensor.value(DS3231_SENSOR_GET_EPOCH_SECONDS_LSB);
140  * ds3231_sensor.value(DS3231_SENSOR_GET_EPOCH_SECONDS_MSB);
141  * ds3231_sensor.configure(DS3231_CONFIG_CLEAR_ALARM, 0);
142  */
143 extern const struct sensors_sensor ds3231_sensor;
144 
145 int ds3231_get_time(struct tm *t);
146 int ds3231_set_time(struct tm *t);
147 int ds3231_set_alarm(struct tm *t);
148 int ds3231_clear_alarm(void);
149 int ds3231_get_temperature(int *temp);
150 int value(int type);
151 int configure(int type, int c);
152 int status(int type);
153 
154 #endif
int ds3231_get_temperature(int *temp)
ds3231_temperature
int ds3231_clear_alarm(void)
ds3231_clear_alarm
int ds3231_get_time(struct tm *t)
ds3231_get_time
int ds3231_set_time(struct tm *t)
ds3231_set_time
int configure(int type, int c)
configure
Definition: raven-lcd.c:482
int ds3231_set_alarm(struct tm *t)
ds3231_set_alarm
tm
Definition: utc_time.h:20
const struct sensors_sensor ds3231_sensor
Export the DS3231 sensor object.
Definition: ds3231-sensor.c:82
int status(int type)
status
Definition: raven-lcd.c:489