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 "utc_time.h"
45 
46 /**
47  * DS3231 Sensor and Config Command List.
48  */
49 #define DS3231_SENSOR_TEMP 0
50 #define DS3231_SENSOR_GET_EPOCH_SECONDS_MSB 1
51 #define DS3231_SENSOR_GET_EPOCH_SECONDS_LSB 2
52 #define DS3231_CONFIG_SET_TIME 55
53 #define DS3231_CONFIG_SET_ALARM 56
54 #define DS3231_CONFIG_CLEAR_ALARM 57
55 
56 #define DS3231_MASTER 0x00
57 #define DS3231_ADDR 0x68 //0xd0 with shift
58 #define DS3231_CONTROL_A1IE_SET_MASK 0x05
59 #define DS3231_CONTROL_A1IE_CLEAR_MASK 0xfe
60 #define DS3231_STATUS_A1F_CLEAR_MASK 0xfe
61 
62 typedef struct tm tm;
63 
64 typedef union {
65  struct {
66  /* Start Address on RTC for time registers. */
67  uint8_t address;
68  /* Byte 0 */
69  uint8_t sec : 4;
70  uint8_t dsec : 3;
71  uint8_t : 1;
72  /* Byte 1 */
73  uint8_t min : 4;
74  uint8_t dmin : 3;
75  uint8_t : 1;
76  /* Byte 2 */
77  uint8_t hour : 4;
78  uint8_t dhour : 2;
79  uint8_t : 2; /* Runs 24 hour time (default). */
80  /* Byte 3 */
81  uint8_t day : 3;
82  uint8_t : 5;
83  /* Byte 4 */
84  uint8_t date : 4;
85  uint8_t ddate : 2;
86  uint8_t : 2;
87  /* Byte 5 */
88  uint8_t mon : 4;
89  uint8_t dmon : 1;
90  uint8_t : 2;
91  uint8_t cent : 1;
92  /* Byte 6 */
93  uint8_t year : 4;
94  uint8_t dyear : 4;
95  } tm;
96  uint8_t data[8];
97 } ds_3231_time_t;
98 
99 typedef union {
100  struct {
101  /* Start Address on RTC for alarm registers. */
102  uint8_t address;
103  /* Byte 0 */
104  uint8_t sec : 4;
105  uint8_t dsec : 3;
106  uint8_t a1m1 : 1;
107  /* Byte 1 */
108  uint8_t min : 4;
109  uint8_t dmin : 3;
110  uint8_t a1m2: 1;
111  /* Byte 2 */
112  uint8_t hour : 4;
113  uint8_t dhour : 2;
114  uint8_t : 1; /* Runs 24 hour time (default). */
115  uint8_t a1m3 : 1;
116  /* Byte 3 */
117  uint8_t date : 4;
118  uint8_t ddate : 2;
119  uint8_t : 1; /* Date based alarm (default) */
120  uint8_t a1m4 : 1;
121  } tm;
122  uint8_t data[5];
123 } ds_3231_alarm_t;
124 
125 /**
126  * Export the DS3231 sensor object.
127  *
128  * Can be called as follows:
129  *
130  * #include <dev/ds3231-sensor.h>
131  *
132  * SENSORS_ACTIVATE(ds3231_sensor);
133  * ds3231_sensor.configure(DS3231_CONFIG_SET_TIME, (int)&t);
134  * ds3231_sensor.configure(DS3231_CONFIG_SET_ALARM, (int)&t);
135  * ds3231_sensor.value(DS3231_SENSOR_TEMP);
136  * ds3231_sensor.value(DS3231_SENSOR_GET_EPOCH_SECONDS_LSB);
137  * ds3231_sensor.value(DS3231_SENSOR_GET_EPOCH_SECONDS_MSB);
138  * ds3231_sensor.configure(DS3231_CONFIG_CLEAR_ALARM, 0);
139  */
140 extern const struct sensors_sensor ds3231_sensor;
141 
142 int ds3231_get_time(struct tm *t);
143 int ds3231_set_time(struct tm *t);
144 int ds3231_set_alarm(struct tm *t);
145 int ds3231_clear_alarm(void);
146 int ds3231_temperature(void);
147 int value(int type);
148 int configure(int type, int c);
149 int status(int type);
150 
151 #endif
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
Utility functions for operating on UTC times.
int ds3231_temperature(void)
ds3231_temperature
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