j

Dependents:   LPC-SD-35

Fork of ds3231 by Maxim Integrated

Committer:
j3
Date:
Wed Nov 19 04:16:33 2014 +0000
Revision:
1:c814af60fdbf
Parent:
0:b00c4699ae6f
Child:
2:4e6e761c60f2
inherited I2C

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j3 0:b00c4699ae6f 1 /******************************************************************//**
j3 0:b00c4699ae6f 2 * @file ds3231.cpp
j3 0:b00c4699ae6f 3 *
j3 0:b00c4699ae6f 4 * @author Justin Jordan
j3 0:b00c4699ae6f 5 *
j3 0:b00c4699ae6f 6 * @version 0.0
j3 0:b00c4699ae6f 7 *
j3 0:b00c4699ae6f 8 * Started: 11NOV14
j3 0:b00c4699ae6f 9 *
j3 0:b00c4699ae6f 10 * Updated:
j3 0:b00c4699ae6f 11 *
j3 0:b00c4699ae6f 12 * @brief Source file for DS3231 class
j3 0:b00c4699ae6f 13 *
j3 0:b00c4699ae6f 14 ***********************************************************************
j3 0:b00c4699ae6f 15 *
j3 0:b00c4699ae6f 16 * @copyright
j3 0:b00c4699ae6f 17 * Copyright (C) 2013 Maxim Integrated Products, Inc., All Rights Reserved.
j3 0:b00c4699ae6f 18 *
j3 0:b00c4699ae6f 19 * Permission is hereby granted, free of charge, to any person obtaining a
j3 0:b00c4699ae6f 20 * copy of this software and associated documentation files (the "Software"),
j3 0:b00c4699ae6f 21 * to deal in the Software without restriction, including without limitation
j3 0:b00c4699ae6f 22 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
j3 0:b00c4699ae6f 23 * and/or sell copies of the Software, and to permit persons to whom the
j3 0:b00c4699ae6f 24 * Software is furnished to do so, subject to the following conditions:
j3 0:b00c4699ae6f 25 *
j3 0:b00c4699ae6f 26 * The above copyright notice and this permission notice shall be included
j3 0:b00c4699ae6f 27 * in all copies or substantial portions of the Software.
j3 0:b00c4699ae6f 28 *
j3 0:b00c4699ae6f 29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
j3 0:b00c4699ae6f 30 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
j3 0:b00c4699ae6f 31 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
j3 0:b00c4699ae6f 32 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
j3 0:b00c4699ae6f 33 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
j3 0:b00c4699ae6f 34 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
j3 0:b00c4699ae6f 35 * OTHER DEALINGS IN THE SOFTWARE.
j3 0:b00c4699ae6f 36 *
j3 0:b00c4699ae6f 37 * Except as contained in this notice, the name of Maxim Integrated
j3 0:b00c4699ae6f 38 * Products, Inc. shall not be used except as stated in the Maxim Integrated
j3 0:b00c4699ae6f 39 * Products, Inc. Branding Policy.
j3 0:b00c4699ae6f 40 *
j3 0:b00c4699ae6f 41 * The mere transfer of this software does not imply any licenses
j3 0:b00c4699ae6f 42 * of trade secrets, proprietary technology, copyrights, patents,
j3 0:b00c4699ae6f 43 * trademarks, maskwork rights, or any other form of intellectual
j3 0:b00c4699ae6f 44 * property whatsoever. Maxim Integrated Products, Inc. retains all
j3 0:b00c4699ae6f 45 * ownership rights.
j3 0:b00c4699ae6f 46 **********************************************************************/
j3 0:b00c4699ae6f 47
j3 0:b00c4699ae6f 48
j3 0:b00c4699ae6f 49 #include "ds3231.h"
j3 0:b00c4699ae6f 50
j3 0:b00c4699ae6f 51
j3 0:b00c4699ae6f 52 /**********************************************************//**
j3 0:b00c4699ae6f 53 * Constructor for Ds3231 Class
j3 0:b00c4699ae6f 54 *
j3 0:b00c4699ae6f 55 * On Entry:
j3 1:c814af60fdbf 56 * @param[in] sda - sda pin of I2C bus
j3 1:c814af60fdbf 57 * @param[in] scl - scl pin of I2C bus
j3 0:b00c4699ae6f 58 *
j3 0:b00c4699ae6f 59 * On Exit:
j3 0:b00c4699ae6f 60 * @return none
j3 0:b00c4699ae6f 61 *
j3 0:b00c4699ae6f 62 * Example:
j3 0:b00c4699ae6f 63 * @code
j3 0:b00c4699ae6f 64 *
j3 0:b00c4699ae6f 65 * //instantiate rtc object
j3 1:c814af60fdbf 66 * Ds3231 rtc(D14, D15);
j3 0:b00c4699ae6f 67 *
j3 0:b00c4699ae6f 68 * @endcode
j3 0:b00c4699ae6f 69 **************************************************************/
j3 1:c814af60fdbf 70 Ds3231::Ds3231(PinName sda, PinName scl) : I2C(sda, scl)
j3 0:b00c4699ae6f 71 {
j3 0:b00c4699ae6f 72 w_adrs = ((DS3231_I2C_ADRS << 1) | I2C_WRITE);
j3 0:b00c4699ae6f 73 r_adrs = ((DS3231_I2C_ADRS << 1) | I2C_READ);
j3 0:b00c4699ae6f 74 }
j3 0:b00c4699ae6f 75
j3 0:b00c4699ae6f 76
j3 0:b00c4699ae6f 77 /**********************************************************//**
j3 0:b00c4699ae6f 78 * Sets the time on DS3231
j3 0:b00c4699ae6f 79 *
j3 0:b00c4699ae6f 80 * On Entry:
j3 0:b00c4699ae6f 81 * @param[in] time - struct cotaining time data;
j3 0:b00c4699ae6f 82 * seconds, minutes and hours
j3 0:b00c4699ae6f 83 *
j3 0:b00c4699ae6f 84 * On Exit:
j3 0:b00c4699ae6f 85 * @return return value = 0 on success, non-0 on failure
j3 0:b00c4699ae6f 86 *
j3 0:b00c4699ae6f 87 * Example:
j3 0:b00c4699ae6f 88 * @code
j3 0:b00c4699ae6f 89 *
j3 0:b00c4699ae6f 90 * //instantiate rtc object
j3 1:c814af60fdbf 91 * Ds3231 rtc(D14, D15);
j3 0:b00c4699ae6f 92 *
j3 0:b00c4699ae6f 93 * ds3231_time_t time = {0, 0, 0} // time = 0:0:0 24hr format
j3 0:b00c4699ae6f 94 * uint16_t rtn_val;
j3 0:b00c4699ae6f 95 *
j3 0:b00c4699ae6f 96 * rtn_val = rtc.set_time(time);
j3 0:b00c4699ae6f 97 *
j3 0:b00c4699ae6f 98 * @endcode
j3 0:b00c4699ae6f 99 **************************************************************/
j3 0:b00c4699ae6f 100 uint16_t Ds3231::set_time(ds3231_time_t time)
j3 0:b00c4699ae6f 101 {
j3 0:b00c4699ae6f 102
j3 0:b00c4699ae6f 103 return 0;
j3 0:b00c4699ae6f 104 }
j3 0:b00c4699ae6f 105
j3 0:b00c4699ae6f 106
j3 0:b00c4699ae6f 107 /**********************************************************//**
j3 0:b00c4699ae6f 108 * Sets the calendar on DS3231
j3 0:b00c4699ae6f 109 *
j3 0:b00c4699ae6f 110 * On Entry:
j3 0:b00c4699ae6f 111 * @param[in] calendar - struct cotaining calendar data;
j3 0:b00c4699ae6f 112 * day, date, month, year
j3 0:b00c4699ae6f 113 *
j3 0:b00c4699ae6f 114 * On Exit:
j3 0:b00c4699ae6f 115 * @return return value = 0 on success, non-0 on failure
j3 0:b00c4699ae6f 116 *
j3 0:b00c4699ae6f 117 * Example:
j3 0:b00c4699ae6f 118 * @code
j3 0:b00c4699ae6f 119 *
j3 0:b00c4699ae6f 120 * //instantiate rtc object
j3 1:c814af60fdbf 121 * Ds3231 rtc(D14, D15);
j3 0:b00c4699ae6f 122 *
j3 0:b00c4699ae6f 123 * //see datasheet for calendar format
j3 0:b00c4699ae6f 124 * ds3231_calendar_t calendar = {1, 1, 1, 0};
j3 0:b00c4699ae6f 125 * uint16_t rtn_val;
j3 0:b00c4699ae6f 126 *
j3 0:b00c4699ae6f 127 * rtn_val = rtc.set_calendar(calendar);
j3 0:b00c4699ae6f 128 *
j3 0:b00c4699ae6f 129 * @endcode
j3 0:b00c4699ae6f 130 **************************************************************/
j3 0:b00c4699ae6f 131 uint16_t Ds3231::set_calendar(ds3231_calendar_t calendar)
j3 0:b00c4699ae6f 132 {
j3 0:b00c4699ae6f 133
j3 0:b00c4699ae6f 134 return 0;
j3 0:b00c4699ae6f 135 }
j3 0:b00c4699ae6f 136
j3 0:b00c4699ae6f 137
j3 0:b00c4699ae6f 138 /**********************************************************//**
j3 0:b00c4699ae6f 139 * Set either Alarm1 or Alarm2 of DS3231
j3 0:b00c4699ae6f 140 *
j3 0:b00c4699ae6f 141 * On Entry:
j3 0:b00c4699ae6f 142 * @param[in] alarm - struct cotaining alarm data
j3 0:b00c4699ae6f 143 * seconds, minutes, hours, day, date
j3 0:b00c4699ae6f 144 * seconds used on Alarm1 only
j3 0:b00c4699ae6f 145 * @param[in] one_r_two - TRUE for Alarm1 and FALSE for
j3 0:b00c4699ae6f 146 * Alarm2
j3 0:b00c4699ae6f 147 *
j3 0:b00c4699ae6f 148 * On Exit:
j3 0:b00c4699ae6f 149 * @return return value = 0 on success, non-0 on failure
j3 0:b00c4699ae6f 150 *
j3 0:b00c4699ae6f 151 * Example:
j3 0:b00c4699ae6f 152 * @code
j3 0:b00c4699ae6f 153 *
j3 0:b00c4699ae6f 154 * //instantiate rtc object
j3 1:c814af60fdbf 155 * Ds3231 rtc(D14, D15);
j3 0:b00c4699ae6f 156 *
j3 0:b00c4699ae6f 157 * //see datasheet for alarm format
j3 0:b00c4699ae6f 158 * ds3231_alrm_t alarm = {0, 0, 0, 0, 0};
j3 0:b00c4699ae6f 159 * uint16_t rtn_val;
j3 0:b00c4699ae6f 160 *
j3 0:b00c4699ae6f 161 * rtn_val = rtc.set_alarm(alarm, FALSE);
j3 0:b00c4699ae6f 162 *
j3 0:b00c4699ae6f 163 * @endcode
j3 0:b00c4699ae6f 164 **************************************************************/
j3 0:b00c4699ae6f 165 uint16_t Ds3231::set_alarm(ds3231_alrm_t alarm, bool one_r_two)
j3 0:b00c4699ae6f 166 {
j3 0:b00c4699ae6f 167
j3 0:b00c4699ae6f 168 return 0;
j3 0:b00c4699ae6f 169 }
j3 0:b00c4699ae6f 170
j3 0:b00c4699ae6f 171
j3 0:b00c4699ae6f 172 /**********************************************************//**
j3 0:b00c4699ae6f 173 * Set control and status registers of DS3231
j3 0:b00c4699ae6f 174 *
j3 0:b00c4699ae6f 175 * On Entry:
j3 0:b00c4699ae6f 176 * @param[in] data - Struct containing control and status
j3 0:b00c4699ae6f 177 * register data
j3 0:b00c4699ae6f 178 *
j3 0:b00c4699ae6f 179 * On Exit:
j3 0:b00c4699ae6f 180 * @return return value = 0 on success, non-0 on failure
j3 0:b00c4699ae6f 181 *
j3 0:b00c4699ae6f 182 * Example:
j3 0:b00c4699ae6f 183 * @code
j3 0:b00c4699ae6f 184 *
j3 0:b00c4699ae6f 185 * //instantiate rtc object
j3 1:c814af60fdbf 186 * Ds3231 rtc(D14, D15);
j3 0:b00c4699ae6f 187 *
j3 0:b00c4699ae6f 188 * //do not use 0xAA, see datasheet for appropriate data
j3 0:b00c4699ae6f 189 * ds3231_cntl_stat_t data = {0xAA, 0xAA};
j3 0:b00c4699ae6f 190 *
j3 0:b00c4699ae6f 191 * rtn_val = rtc.set_cntl_stat_reg(data);
j3 0:b00c4699ae6f 192 *
j3 0:b00c4699ae6f 193 * @endcode
j3 0:b00c4699ae6f 194 **************************************************************/
j3 0:b00c4699ae6f 195 uint16_t Ds3231::set_cntl_stat_reg(ds3231_cntl_stat_t data)
j3 0:b00c4699ae6f 196 {
j3 0:b00c4699ae6f 197
j3 0:b00c4699ae6f 198 return 0;
j3 0:b00c4699ae6f 199 }
j3 0:b00c4699ae6f 200
j3 0:b00c4699ae6f 201
j3 0:b00c4699ae6f 202 /**********************************************************//**
j3 0:b00c4699ae6f 203 * Gets the time on DS3231
j3 0:b00c4699ae6f 204 *
j3 0:b00c4699ae6f 205 * On Entry:
j3 0:b00c4699ae6f 206 * @param[in] time - struct for storing time data;
j3 0:b00c4699ae6f 207 * seconds, minutes and hours
j3 0:b00c4699ae6f 208 *
j3 0:b00c4699ae6f 209 * On Exit:
j3 0:b00c4699ae6f 210 * @param[out] time - contains current rtc time data
j3 0:b00c4699ae6f 211 * @return return value = 0 on success, non-0 on failure
j3 0:b00c4699ae6f 212 *
j3 0:b00c4699ae6f 213 * Example:
j3 0:b00c4699ae6f 214 * @code
j3 0:b00c4699ae6f 215 *
j3 0:b00c4699ae6f 216 * //instantiate rtc object
j3 1:c814af60fdbf 217 * Ds3231 rtc(D14, D15);
j3 0:b00c4699ae6f 218 *
j3 0:b00c4699ae6f 219 * ds3231_time_t time = {0, 0, 0} // time = 0:0:0 24hr format
j3 0:b00c4699ae6f 220 * uint16_t rtn_val;
j3 0:b00c4699ae6f 221 *
j3 0:b00c4699ae6f 222 * rtn_val = rtc.get_time(time);
j3 0:b00c4699ae6f 223 *
j3 0:b00c4699ae6f 224 * @endcode
j3 0:b00c4699ae6f 225 **************************************************************/
j3 0:b00c4699ae6f 226 uint16_t Ds3231::get_time(ds3231_time_t time)
j3 0:b00c4699ae6f 227 {
j3 0:b00c4699ae6f 228
j3 0:b00c4699ae6f 229 return 0;
j3 0:b00c4699ae6f 230 }
j3 0:b00c4699ae6f 231
j3 0:b00c4699ae6f 232
j3 0:b00c4699ae6f 233 /**********************************************************//**
j3 0:b00c4699ae6f 234 * Gets the calendar on DS3231
j3 0:b00c4699ae6f 235 *
j3 0:b00c4699ae6f 236 * On Entry:
j3 0:b00c4699ae6f 237 * @param[in] calendar - struct for storing calendar data;
j3 0:b00c4699ae6f 238 * day, date, month, year
j3 0:b00c4699ae6f 239 *
j3 0:b00c4699ae6f 240 * On Exit:
j3 0:b00c4699ae6f 241 * @param[out] calendar - contains current rtc calendar
j3 0:b00c4699ae6f 242 * @return return value = 0 on success, non-0 on failure
j3 0:b00c4699ae6f 243 *
j3 0:b00c4699ae6f 244 * Example:
j3 0:b00c4699ae6f 245 * @code
j3 0:b00c4699ae6f 246 *
j3 0:b00c4699ae6f 247 * //instantiate rtc object
j3 1:c814af60fdbf 248 * Ds3231 rtc(D14, D15);
j3 0:b00c4699ae6f 249 *
j3 0:b00c4699ae6f 250 * //see datasheet for calendar format
j3 0:b00c4699ae6f 251 * ds3231_calendar_t calendar = {1, 1, 1, 0};
j3 0:b00c4699ae6f 252 * uint16_t rtn_val;
j3 0:b00c4699ae6f 253 *
j3 0:b00c4699ae6f 254 * rtn_val = rtc.get_calendar(calendar);
j3 0:b00c4699ae6f 255 *
j3 0:b00c4699ae6f 256 * @endcode
j3 0:b00c4699ae6f 257 **************************************************************/
j3 0:b00c4699ae6f 258 uint16_t Ds3231::get_calendar(ds3231_calendar_t calendar)
j3 0:b00c4699ae6f 259 {
j3 0:b00c4699ae6f 260
j3 0:b00c4699ae6f 261 return 0;
j3 0:b00c4699ae6f 262 }
j3 0:b00c4699ae6f 263
j3 0:b00c4699ae6f 264
j3 0:b00c4699ae6f 265 /**********************************************************//**
j3 0:b00c4699ae6f 266 * Get either Alarm1 or Alarm2 of DS3231
j3 0:b00c4699ae6f 267 *
j3 0:b00c4699ae6f 268 * On Entry:
j3 0:b00c4699ae6f 269 * @param[in] alarm - struct for storing alarm data
j3 0:b00c4699ae6f 270 * seconds, minutes, hours, day, date
j3 0:b00c4699ae6f 271 * seconds used on Alarm1 only
j3 0:b00c4699ae6f 272 * @param[in] one_r_two - TRUE for Alarm1 and FALSE for
j3 0:b00c4699ae6f 273 * Alarm2
j3 0:b00c4699ae6f 274 *
j3 0:b00c4699ae6f 275 * On Exit:
j3 0:b00c4699ae6f 276 * @param[out] alarm - contains alarm register data
j3 0:b00c4699ae6f 277 * @return return value = 0 on success, non-0 on failure
j3 0:b00c4699ae6f 278 *
j3 0:b00c4699ae6f 279 * Example:
j3 0:b00c4699ae6f 280 * @code
j3 0:b00c4699ae6f 281 *
j3 0:b00c4699ae6f 282 * //instantiate rtc object
j3 1:c814af60fdbf 283 * Ds3231 rtc(D14, D15);
j3 0:b00c4699ae6f 284 *
j3 0:b00c4699ae6f 285 * //see datasheet for alarm format
j3 0:b00c4699ae6f 286 * ds3231_alrm_t alarm = {0, 0, 0, 0, 0};
j3 0:b00c4699ae6f 287 * uint16_t rtn_val;
j3 0:b00c4699ae6f 288 *
j3 0:b00c4699ae6f 289 * rtn_val = rtc.get_alarm(alarm, FALSE);
j3 0:b00c4699ae6f 290 *
j3 0:b00c4699ae6f 291 * @endcode
j3 0:b00c4699ae6f 292 **************************************************************/
j3 0:b00c4699ae6f 293 uint16_t Ds3231::get_alarm(ds3231_alrm_t alarm, bool one_r_two)
j3 0:b00c4699ae6f 294 {
j3 0:b00c4699ae6f 295
j3 0:b00c4699ae6f 296 return 0;
j3 0:b00c4699ae6f 297 }
j3 0:b00c4699ae6f 298
j3 0:b00c4699ae6f 299
j3 0:b00c4699ae6f 300 /**********************************************************//**
j3 0:b00c4699ae6f 301 * Get control and status registers of DS3231
j3 0:b00c4699ae6f 302 *
j3 0:b00c4699ae6f 303 * On Entry:
j3 0:b00c4699ae6f 304 * @param[in] data - Struct for storing control and status
j3 0:b00c4699ae6f 305 * register data
j3 0:b00c4699ae6f 306 *
j3 0:b00c4699ae6f 307 * On Exit:
j3 0:b00c4699ae6f 308 * @param[out] data - contains control and status registers
j3 0:b00c4699ae6f 309 * data
j3 0:b00c4699ae6f 310 * @return return value = 0 on success, non-0 on failure
j3 0:b00c4699ae6f 311 *
j3 0:b00c4699ae6f 312 * Example:
j3 0:b00c4699ae6f 313 * @code
j3 0:b00c4699ae6f 314 *
j3 0:b00c4699ae6f 315 * //instantiate rtc object
j3 1:c814af60fdbf 316 * Ds3231 rtc(D14, D15);
j3 0:b00c4699ae6f 317 *
j3 0:b00c4699ae6f 318 * //do not use 0xAA, see datasheet for appropriate data
j3 0:b00c4699ae6f 319 * ds3231_cntl_stat_t data = {0xAA, 0xAA};
j3 0:b00c4699ae6f 320 *
j3 0:b00c4699ae6f 321 * rtn_val = rtc.get_cntl_stat_reg(data);
j3 0:b00c4699ae6f 322 *
j3 0:b00c4699ae6f 323 * @endcode
j3 0:b00c4699ae6f 324 **************************************************************/
j3 0:b00c4699ae6f 325 uint16_t Ds3231::get_cntl_stat_reg(ds3231_cntl_stat_t data)
j3 0:b00c4699ae6f 326 {
j3 0:b00c4699ae6f 327
j3 0:b00c4699ae6f 328 return 0;
j3 0:b00c4699ae6f 329 }
j3 0:b00c4699ae6f 330
j3 0:b00c4699ae6f 331
j3 0:b00c4699ae6f 332 /**********************************************************//**
j3 0:b00c4699ae6f 333 * Get temperature data of DS3231
j3 0:b00c4699ae6f 334 *
j3 0:b00c4699ae6f 335 * On Entry:
j3 0:b00c4699ae6f 336 * @param[in] temp - Integer for storing temperature data
j3 0:b00c4699ae6f 337 *
j3 0:b00c4699ae6f 338 * On Exit:
j3 0:b00c4699ae6f 339 * @param[out] temp - contains temperature data
j3 0:b00c4699ae6f 340 * @return return value = 0 on success, non-0 on failure
j3 0:b00c4699ae6f 341 *
j3 0:b00c4699ae6f 342 * Example:
j3 0:b00c4699ae6f 343 * @code
j3 0:b00c4699ae6f 344 *
j3 0:b00c4699ae6f 345 * //instantiate rtc object
j3 1:c814af60fdbf 346 * Ds3231 rtc(D14, D15);
j3 0:b00c4699ae6f 347 *
j3 0:b00c4699ae6f 348 * //do not use 0xAA, see datasheet for appropriate data
j3 0:b00c4699ae6f 349 * uint16_t temp;
j3 0:b00c4699ae6f 350 *
j3 1:c814af60fdbf 351 * rtn_val = rtc.get_temperature(temp);
j3 0:b00c4699ae6f 352 *
j3 0:b00c4699ae6f 353 * @endcode
j3 0:b00c4699ae6f 354 **************************************************************/
j3 0:b00c4699ae6f 355 uint16_t Ds3231::get_temperature(uint16_t temp)
j3 0:b00c4699ae6f 356 {
j3 0:b00c4699ae6f 357
j3 0:b00c4699ae6f 358 return 0;
j3 0:b00c4699ae6f 359 }
j3 0:b00c4699ae6f 360