1 /*
2  * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #pragma once
25 
26 #include <string>
27 #include <mraa/i2c.hpp>
28 
29 #define ADDR                    0x68 // device address
30 
31 // timekeeping registers
32 #define TIME_CAL_ADDR           0x00
33 #define ALARM1_ADDR             0x07
34 #define ALARM2_ADDR             0x0B
35 #define CONTROL_ADDR            0x0E
36 #define STATUS_ADDR             0x0F
37 #define AGING_OFFSET_ADDR       0x10
38 #define TEMPERATURE_ADDR        0x11
39 
40 // control register bits
41 #define A1IE                    0x1
42 #define A2IE                    0x2
43 #define INTCN                   0x4
44 
45 // status register bits
46 #define A1F                     0x1
47 #define A2F                     0x2
48 #define OSF                     0x80
49 
50 #define HIGH                    1
51 #define LOW                     0
52 
53 namespace upm {
54 
55 struct Time3231 {
56     uint8_t second;
57     uint8_t minute;
58     uint8_t hour;
59     uint8_t day;
60     uint8_t month;
61     int16_t year;
62     uint8_t weekDay;
63 };
64 
65 /**
66  * @brief MAXDS3231M Proximity Sensor library
67  * @defgroup maxds3231m libupm-maxds3231m
68  * @ingroup maxim i2c light
69  */
70 /**
71  * @library maxds3231m
72  * @sensor maxds3231m
73  * @comname MAXDS3231M Real-Time Clock
74  * @type light
75  * @man maxim
76  * @con i2c
77  *
78  * @brief API for the MAXDS3231M I2C Real-Time Clock
79  *
80  * This module defines the API for MAXDS3231M
81  *
82  * @snippet maxds3231m.cxx Interesting
83  */
84 class MAXDS3231M {
85     public:
86         /**
87          * Instantiates an MAXDS3231M object
88          *
89          * @param bus Number of the used bus
90          * @param devAddr Address of the used I2C device
91          */
92         MAXDS3231M (int bus=0, int devAddr=0x68);
93 
94         /**
95          * Sets the date and time on the chip.
96          *
97          * @param time Time structure
98          */
99         void setDate (Time3231 &time);
100 
101         /**
102          * Gets the date and time from the chip.
103          *
104          * @param time Time structure
105          */
106         bool getDate (Time3231 &time);
107 
108         /**
109          * Gets the on-board temperature.
110          */
111         uint16_t getTemperature ();
112 
113         /**
114          * Returns the name of the component
115          */
name()116         std::string name()
117         {
118             return m_name;
119         }
120     private:
121         std::string m_name;
122 
123         int m_i2cAddr;
124         int m_bus;
125         mraa::I2c m_i2Ctx;
126 
127         uint16_t i2cReadReg_N (int reg, unsigned int len, uint8_t * buffer);
128         mraa::Result i2cWriteReg_N (uint8_t reg, unsigned int len, uint8_t * buffer);
129         uint8_t DECtoBSD (uint8_t data);
130         uint8_t BCDtoDEC (uint8_t data);
131 };
132 
133 }
134