Library for DS3231 RTC

Dependents:   ard2pmod DS3231demo DS3231demo_COM_Port_Output MAXREFDES99_RTC_Display ... more

DS3231 Component Page

Committer:
j3
Date:
Thu Nov 20 21:06:57 2014 +0000
Revision:
3:312589d8185c
Parent:
2:4e6e761c60f2
Child:
4:0beb5e9ac927
updated comments

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 2:4e6e761c60f2 79 * Struct data is in integrer format, not BCD. Fx will convert
j3 2:4e6e761c60f2 80 * to BCD for you.
j3 0:b00c4699ae6f 81 *
j3 0:b00c4699ae6f 82 * On Entry:
j3 0:b00c4699ae6f 83 * @param[in] time - struct cotaining time data;
j3 0:b00c4699ae6f 84 *
j3 0:b00c4699ae6f 85 * On Exit:
j3 0:b00c4699ae6f 86 * @return return value = 0 on success, non-0 on failure
j3 0:b00c4699ae6f 87 *
j3 0:b00c4699ae6f 88 * Example:
j3 0:b00c4699ae6f 89 * @code
j3 0:b00c4699ae6f 90 *
j3 0:b00c4699ae6f 91 * //instantiate rtc object
j3 1:c814af60fdbf 92 * Ds3231 rtc(D14, D15);
j3 0:b00c4699ae6f 93 *
j3 0:b00c4699ae6f 94 * ds3231_time_t time = {0, 0, 0} // time = 0:0:0 24hr format
j3 0:b00c4699ae6f 95 * uint16_t rtn_val;
j3 0:b00c4699ae6f 96 *
j3 0:b00c4699ae6f 97 * rtn_val = rtc.set_time(time);
j3 0:b00c4699ae6f 98 *
j3 0:b00c4699ae6f 99 * @endcode
j3 0:b00c4699ae6f 100 **************************************************************/
j3 0:b00c4699ae6f 101 uint16_t Ds3231::set_time(ds3231_time_t time)
j3 0:b00c4699ae6f 102 {
j3 2:4e6e761c60f2 103 uint8_t data[] = {0,0,0,0};
j3 2:4e6e761c60f2 104 uint8_t data_length = 0;
j3 2:4e6e761c60f2 105 uint8_t max_hour = 24;
j3 0:b00c4699ae6f 106
j3 2:4e6e761c60f2 107 data[data_length++] = SECONDS;
j3 2:4e6e761c60f2 108 data[data_length++] = uchar_2_bcd(time.seconds);
j3 2:4e6e761c60f2 109 data[data_length++] = uchar_2_bcd(time.minutes);
j3 2:4e6e761c60f2 110
j3 2:4e6e761c60f2 111 //format Hours register
j3 2:4e6e761c60f2 112 data[data_length] = uchar_2_bcd(time.hours);
j3 2:4e6e761c60f2 113 if(time.mode)
j3 2:4e6e761c60f2 114 {
j3 2:4e6e761c60f2 115 max_hour = max_hour/2;
j3 2:4e6e761c60f2 116
j3 2:4e6e761c60f2 117 data[data_length] |= MODE;
j3 2:4e6e761c60f2 118 if(time.am_pm)
j3 2:4e6e761c60f2 119 {
j3 2:4e6e761c60f2 120 data[data_length] |= AM_PM;
j3 2:4e6e761c60f2 121 }
j3 2:4e6e761c60f2 122
j3 2:4e6e761c60f2 123 }
j3 2:4e6e761c60f2 124 else
j3 2:4e6e761c60f2 125 {
j3 2:4e6e761c60f2 126 max_hour = max_hour - 1;
j3 2:4e6e761c60f2 127 }
j3 2:4e6e761c60f2 128 data_length++;
j3 2:4e6e761c60f2 129
j3 2:4e6e761c60f2 130 //Test for good data.
j3 2:4e6e761c60f2 131 if((time.seconds > 59) || (time.minutes > 59) || (time.hours > max_hour))
j3 2:4e6e761c60f2 132 {
j3 2:4e6e761c60f2 133 return(1);
j3 2:4e6e761c60f2 134 }
j3 2:4e6e761c60f2 135 else
j3 2:4e6e761c60f2 136 {
j3 2:4e6e761c60f2 137 return(write(w_adrs,(const char*) data, data_length));
j3 2:4e6e761c60f2 138 }
j3 0:b00c4699ae6f 139 }
j3 0:b00c4699ae6f 140
j3 0:b00c4699ae6f 141
j3 0:b00c4699ae6f 142 /**********************************************************//**
j3 0:b00c4699ae6f 143 * Sets the calendar on DS3231
j3 0:b00c4699ae6f 144 *
j3 0:b00c4699ae6f 145 * On Entry:
j3 3:312589d8185c 146 * @param[in] calendar - struct cotaining calendar data
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 calendar format
j3 0:b00c4699ae6f 158 * ds3231_calendar_t calendar = {1, 1, 1, 0};
j3 0:b00c4699ae6f 159 * uint16_t rtn_val;
j3 0:b00c4699ae6f 160 *
j3 0:b00c4699ae6f 161 * rtn_val = rtc.set_calendar(calendar);
j3 0:b00c4699ae6f 162 *
j3 0:b00c4699ae6f 163 * @endcode
j3 0:b00c4699ae6f 164 **************************************************************/
j3 0:b00c4699ae6f 165 uint16_t Ds3231::set_calendar(ds3231_calendar_t calendar)
j3 0:b00c4699ae6f 166 {
j3 2:4e6e761c60f2 167 uint8_t data[] = {0,0,0,0,0};
j3 2:4e6e761c60f2 168 uint8_t data_length = 0;
j3 2:4e6e761c60f2 169
j3 2:4e6e761c60f2 170 data[data_length++] = DAY;
j3 2:4e6e761c60f2 171 data[data_length++] = uchar_2_bcd(calendar.day);
j3 2:4e6e761c60f2 172 data[data_length++] = uchar_2_bcd(calendar.date);
j3 2:4e6e761c60f2 173 data[data_length++] = uchar_2_bcd(calendar.month);
j3 2:4e6e761c60f2 174 data[data_length++] = uchar_2_bcd(calendar.year);
j3 0:b00c4699ae6f 175
j3 2:4e6e761c60f2 176 //users responsibility to make sure calendar is logical
j3 2:4e6e761c60f2 177 return(write(w_adrs,(const char*) data, data_length));
j3 0:b00c4699ae6f 178 }
j3 0:b00c4699ae6f 179
j3 0:b00c4699ae6f 180
j3 0:b00c4699ae6f 181 /**********************************************************//**
j3 0:b00c4699ae6f 182 * Set either Alarm1 or Alarm2 of DS3231
j3 0:b00c4699ae6f 183 *
j3 0:b00c4699ae6f 184 * On Entry:
j3 0:b00c4699ae6f 185 * @param[in] alarm - struct cotaining alarm data
j3 3:312589d8185c 186 *
j3 0:b00c4699ae6f 187 * @param[in] one_r_two - TRUE for Alarm1 and FALSE for
j3 0:b00c4699ae6f 188 * Alarm2
j3 0:b00c4699ae6f 189 *
j3 0:b00c4699ae6f 190 * On Exit:
j3 0:b00c4699ae6f 191 * @return return value = 0 on success, non-0 on failure
j3 0:b00c4699ae6f 192 *
j3 0:b00c4699ae6f 193 * Example:
j3 0:b00c4699ae6f 194 * @code
j3 0:b00c4699ae6f 195 *
j3 0:b00c4699ae6f 196 * //instantiate rtc object
j3 1:c814af60fdbf 197 * Ds3231 rtc(D14, D15);
j3 0:b00c4699ae6f 198 *
j3 0:b00c4699ae6f 199 * //see datasheet for alarm format
j3 0:b00c4699ae6f 200 * ds3231_alrm_t alarm = {0, 0, 0, 0, 0};
j3 0:b00c4699ae6f 201 * uint16_t rtn_val;
j3 0:b00c4699ae6f 202 *
j3 0:b00c4699ae6f 203 * rtn_val = rtc.set_alarm(alarm, FALSE);
j3 0:b00c4699ae6f 204 *
j3 0:b00c4699ae6f 205 * @endcode
j3 0:b00c4699ae6f 206 **************************************************************/
j3 0:b00c4699ae6f 207 uint16_t Ds3231::set_alarm(ds3231_alrm_t alarm, bool one_r_two)
j3 0:b00c4699ae6f 208 {
j3 2:4e6e761c60f2 209 uint8_t data[] = {0,0,0,0,0};
j3 2:4e6e761c60f2 210 uint8_t data_length = 0;
j3 2:4e6e761c60f2 211 uint8_t max_hour = 24;
j3 2:4e6e761c60f2 212 uint8_t mask_var = 0;
j3 2:4e6e761c60f2 213
j3 2:4e6e761c60f2 214 //setting alarm 1 or 2?
j3 2:4e6e761c60f2 215 if(one_r_two)
j3 2:4e6e761c60f2 216 {
j3 2:4e6e761c60f2 217 data[data_length++] = ALRM1_SECONDS;
j3 2:4e6e761c60f2 218
j3 2:4e6e761c60f2 219 //config seconds register
j3 2:4e6e761c60f2 220 if(alarm.am1)
j3 2:4e6e761c60f2 221 {
j3 2:4e6e761c60f2 222 mask_var |= ALRM_MASK;
j3 2:4e6e761c60f2 223 }
j3 2:4e6e761c60f2 224 data[data_length++] = (mask_var | uchar_2_bcd(alarm.seconds));
j3 2:4e6e761c60f2 225 mask_var = 0;
j3 2:4e6e761c60f2 226
j3 2:4e6e761c60f2 227 //config minutes register
j3 2:4e6e761c60f2 228 if(alarm.am2)
j3 2:4e6e761c60f2 229 {
j3 2:4e6e761c60f2 230 mask_var |= ALRM_MASK;
j3 2:4e6e761c60f2 231 }
j3 2:4e6e761c60f2 232 data[data_length++] = (mask_var | uchar_2_bcd(alarm.minutes));
j3 2:4e6e761c60f2 233 mask_var = 0;
j3 2:4e6e761c60f2 234
j3 2:4e6e761c60f2 235 //config hours register
j3 2:4e6e761c60f2 236 if(alarm.am3)
j3 2:4e6e761c60f2 237 {
j3 2:4e6e761c60f2 238 mask_var |= ALRM_MASK;
j3 2:4e6e761c60f2 239 }
j3 2:4e6e761c60f2 240 if(alarm.mode)
j3 2:4e6e761c60f2 241 {
j3 2:4e6e761c60f2 242 max_hour = max_hour/2;
j3 2:4e6e761c60f2 243 mask_var |= MODE;
j3 2:4e6e761c60f2 244 if(alarm.am_pm)
j3 2:4e6e761c60f2 245 {
j3 2:4e6e761c60f2 246 mask_var |= AM_PM;
j3 2:4e6e761c60f2 247 }
j3 2:4e6e761c60f2 248 }
j3 2:4e6e761c60f2 249 else
j3 2:4e6e761c60f2 250 {
j3 2:4e6e761c60f2 251 max_hour = max_hour - 1;
j3 2:4e6e761c60f2 252 }
j3 2:4e6e761c60f2 253 data[data_length++] = (mask_var | uchar_2_bcd(alarm.hours));
j3 2:4e6e761c60f2 254 mask_var = 0;
j3 2:4e6e761c60f2 255
j3 2:4e6e761c60f2 256 //config day/date register
j3 2:4e6e761c60f2 257 if(alarm.am4)
j3 2:4e6e761c60f2 258 {
j3 2:4e6e761c60f2 259 mask_var |= ALRM_MASK;
j3 2:4e6e761c60f2 260 }
j3 2:4e6e761c60f2 261 if(alarm.dy_dt)
j3 2:4e6e761c60f2 262 {
j3 2:4e6e761c60f2 263 mask_var |= DY_DT;
j3 2:4e6e761c60f2 264 data[data_length++] = (mask_var | uchar_2_bcd(alarm.day));
j3 2:4e6e761c60f2 265 }
j3 2:4e6e761c60f2 266 else
j3 2:4e6e761c60f2 267 {
j3 2:4e6e761c60f2 268 data[data_length++] = (mask_var | uchar_2_bcd(alarm.date));
j3 2:4e6e761c60f2 269 }
j3 2:4e6e761c60f2 270 mask_var = 0;
j3 2:4e6e761c60f2 271 }
j3 2:4e6e761c60f2 272 else
j3 2:4e6e761c60f2 273 {
j3 2:4e6e761c60f2 274 data[data_length++] = ALRM2_MINUTES;
j3 2:4e6e761c60f2 275
j3 2:4e6e761c60f2 276 //config minutes register
j3 2:4e6e761c60f2 277 if(alarm.am2)
j3 2:4e6e761c60f2 278 {
j3 2:4e6e761c60f2 279 mask_var |= ALRM_MASK;
j3 2:4e6e761c60f2 280 }
j3 2:4e6e761c60f2 281 data[data_length++] = (mask_var | uchar_2_bcd(alarm.minutes));
j3 2:4e6e761c60f2 282 mask_var = 0;
j3 2:4e6e761c60f2 283
j3 2:4e6e761c60f2 284 //config hours register
j3 2:4e6e761c60f2 285 if(alarm.am3)
j3 2:4e6e761c60f2 286 {
j3 2:4e6e761c60f2 287 mask_var |= ALRM_MASK;
j3 2:4e6e761c60f2 288 }
j3 2:4e6e761c60f2 289 if(alarm.mode)
j3 2:4e6e761c60f2 290 {
j3 2:4e6e761c60f2 291 max_hour = max_hour/2;
j3 2:4e6e761c60f2 292 mask_var |= MODE;
j3 2:4e6e761c60f2 293 if(alarm.am_pm)
j3 2:4e6e761c60f2 294 {
j3 2:4e6e761c60f2 295 mask_var |= AM_PM;
j3 2:4e6e761c60f2 296 }
j3 2:4e6e761c60f2 297 }
j3 2:4e6e761c60f2 298 else
j3 2:4e6e761c60f2 299 {
j3 2:4e6e761c60f2 300 max_hour = max_hour - 1;
j3 2:4e6e761c60f2 301 }
j3 2:4e6e761c60f2 302 data[data_length++] = (mask_var | uchar_2_bcd(alarm.hours));
j3 2:4e6e761c60f2 303 mask_var = 0;
j3 2:4e6e761c60f2 304
j3 2:4e6e761c60f2 305 //config day/date register
j3 2:4e6e761c60f2 306 if(alarm.am4)
j3 2:4e6e761c60f2 307 {
j3 2:4e6e761c60f2 308 mask_var |= ALRM_MASK;
j3 2:4e6e761c60f2 309 }
j3 2:4e6e761c60f2 310 if(alarm.dy_dt)
j3 2:4e6e761c60f2 311 {
j3 2:4e6e761c60f2 312 mask_var |= DY_DT;
j3 2:4e6e761c60f2 313 data[data_length++] = (mask_var | uchar_2_bcd(alarm.day));
j3 2:4e6e761c60f2 314 }
j3 2:4e6e761c60f2 315 else
j3 2:4e6e761c60f2 316 {
j3 2:4e6e761c60f2 317 data[data_length++] = (mask_var | uchar_2_bcd(alarm.date));
j3 2:4e6e761c60f2 318 }
j3 2:4e6e761c60f2 319 mask_var = 0;
j3 2:4e6e761c60f2 320 }
j3 2:4e6e761c60f2 321
j3 2:4e6e761c60f2 322 //Test for good data.
j3 2:4e6e761c60f2 323 if((alarm.seconds > 59) || (alarm.minutes > 59) || (alarm.hours > max_hour) || (alarm.day > 7) || (alarm.date > 31))
j3 2:4e6e761c60f2 324 {
j3 2:4e6e761c60f2 325 return(1);
j3 2:4e6e761c60f2 326 }
j3 2:4e6e761c60f2 327 else
j3 2:4e6e761c60f2 328 {
j3 2:4e6e761c60f2 329 return(write(w_adrs,(const char*) data, data_length));
j3 2:4e6e761c60f2 330 }
j3 0:b00c4699ae6f 331 }
j3 0:b00c4699ae6f 332
j3 0:b00c4699ae6f 333
j3 0:b00c4699ae6f 334 /**********************************************************//**
j3 0:b00c4699ae6f 335 * Set control and status registers of DS3231
j3 0:b00c4699ae6f 336 *
j3 0:b00c4699ae6f 337 * On Entry:
j3 0:b00c4699ae6f 338 * @param[in] data - Struct containing control and status
j3 0:b00c4699ae6f 339 * register data
j3 0:b00c4699ae6f 340 *
j3 0:b00c4699ae6f 341 * On Exit:
j3 0:b00c4699ae6f 342 * @return return value = 0 on success, non-0 on failure
j3 0:b00c4699ae6f 343 *
j3 0:b00c4699ae6f 344 * Example:
j3 0:b00c4699ae6f 345 * @code
j3 0:b00c4699ae6f 346 *
j3 0:b00c4699ae6f 347 * //instantiate rtc object
j3 1:c814af60fdbf 348 * Ds3231 rtc(D14, D15);
j3 0:b00c4699ae6f 349 *
j3 0:b00c4699ae6f 350 * //do not use 0xAA, see datasheet for appropriate data
j3 0:b00c4699ae6f 351 * ds3231_cntl_stat_t data = {0xAA, 0xAA};
j3 0:b00c4699ae6f 352 *
j3 0:b00c4699ae6f 353 * rtn_val = rtc.set_cntl_stat_reg(data);
j3 0:b00c4699ae6f 354 *
j3 0:b00c4699ae6f 355 * @endcode
j3 0:b00c4699ae6f 356 **************************************************************/
j3 0:b00c4699ae6f 357 uint16_t Ds3231::set_cntl_stat_reg(ds3231_cntl_stat_t data)
j3 0:b00c4699ae6f 358 {
j3 2:4e6e761c60f2 359 uint8_t local_data[] = {0,0,0};
j3 2:4e6e761c60f2 360 uint8_t data_length = 0;
j3 2:4e6e761c60f2 361
j3 2:4e6e761c60f2 362 local_data[data_length++] = CONTROL;
j3 2:4e6e761c60f2 363 local_data[data_length++] = data.control;
j3 2:4e6e761c60f2 364 local_data[data_length++] = data.status;
j3 0:b00c4699ae6f 365
j3 2:4e6e761c60f2 366 //users responsibility to make sure data is logical
j3 2:4e6e761c60f2 367 return(write(w_adrs,(const char*) local_data, data_length));
j3 0:b00c4699ae6f 368 }
j3 0:b00c4699ae6f 369
j3 0:b00c4699ae6f 370
j3 0:b00c4699ae6f 371 /**********************************************************//**
j3 0:b00c4699ae6f 372 * Gets the time on DS3231
j3 0:b00c4699ae6f 373 *
j3 0:b00c4699ae6f 374 * On Entry:
j3 2:4e6e761c60f2 375 * @param[in] time - pointer to struct for storing time
j3 2:4e6e761c60f2 376 * data; seconds, minutes and hours
j3 0:b00c4699ae6f 377 *
j3 0:b00c4699ae6f 378 * On Exit:
j3 2:4e6e761c60f2 379 * @param[out] time - contains current integrer rtc time
j3 2:4e6e761c60f2 380 * data
j3 0:b00c4699ae6f 381 * @return return value = 0 on success, non-0 on failure
j3 0:b00c4699ae6f 382 *
j3 0:b00c4699ae6f 383 * Example:
j3 0:b00c4699ae6f 384 * @code
j3 0:b00c4699ae6f 385 *
j3 0:b00c4699ae6f 386 * //instantiate rtc object
j3 1:c814af60fdbf 387 * Ds3231 rtc(D14, D15);
j3 0:b00c4699ae6f 388 *
j3 0:b00c4699ae6f 389 * ds3231_time_t time = {0, 0, 0} // time = 0:0:0 24hr format
j3 0:b00c4699ae6f 390 * uint16_t rtn_val;
j3 0:b00c4699ae6f 391 *
j3 3:312589d8185c 392 * rtn_val = rtc.get_time(&time);
j3 0:b00c4699ae6f 393 *
j3 0:b00c4699ae6f 394 * @endcode
j3 0:b00c4699ae6f 395 **************************************************************/
j3 2:4e6e761c60f2 396 uint16_t Ds3231::get_time(ds3231_time_t* time)
j3 0:b00c4699ae6f 397 {
j3 2:4e6e761c60f2 398 uint16_t rtn_val = 1;
j3 2:4e6e761c60f2 399 uint8_t data[3];
j3 2:4e6e761c60f2 400
j3 2:4e6e761c60f2 401 data[0] = SECONDS;
j3 2:4e6e761c60f2 402 rtn_val = write(w_adrs, (const char*) data, 1);
j3 2:4e6e761c60f2 403
j3 2:4e6e761c60f2 404 if(!rtn_val)
j3 2:4e6e761c60f2 405 {
j3 2:4e6e761c60f2 406 rtn_val = read(r_adrs,(char *) data, 3);
j3 2:4e6e761c60f2 407
j3 2:4e6e761c60f2 408 time->seconds = bcd_2_uchar(data[0]);
j3 2:4e6e761c60f2 409 time->minutes = bcd_2_uchar(data[1]);
j3 2:4e6e761c60f2 410 time->hours = bcd_2_uchar((data[2]&0x1F));
j3 2:4e6e761c60f2 411 }
j3 2:4e6e761c60f2 412
j3 2:4e6e761c60f2 413 return(rtn_val);
j3 0:b00c4699ae6f 414 }
j3 0:b00c4699ae6f 415
j3 0:b00c4699ae6f 416
j3 0:b00c4699ae6f 417 /**********************************************************//**
j3 0:b00c4699ae6f 418 * Gets the calendar on DS3231
j3 0:b00c4699ae6f 419 *
j3 0:b00c4699ae6f 420 * On Entry:
j3 2:4e6e761c60f2 421 * @param[in] calendar - pointer to struct for storing
j3 2:4e6e761c60f2 422 * calendar data;
j3 0:b00c4699ae6f 423 * day, date, month, year
j3 0:b00c4699ae6f 424 *
j3 0:b00c4699ae6f 425 * On Exit:
j3 2:4e6e761c60f2 426 * @param[out] calendar - contains current integer rtc
j3 2:4e6e761c60f2 427 * calendar data
j3 0:b00c4699ae6f 428 * @return return value = 0 on success, non-0 on failure
j3 0:b00c4699ae6f 429 *
j3 0:b00c4699ae6f 430 * Example:
j3 0:b00c4699ae6f 431 * @code
j3 0:b00c4699ae6f 432 *
j3 0:b00c4699ae6f 433 * //instantiate rtc object
j3 1:c814af60fdbf 434 * Ds3231 rtc(D14, D15);
j3 0:b00c4699ae6f 435 *
j3 0:b00c4699ae6f 436 * //see datasheet for calendar format
j3 0:b00c4699ae6f 437 * ds3231_calendar_t calendar = {1, 1, 1, 0};
j3 0:b00c4699ae6f 438 * uint16_t rtn_val;
j3 0:b00c4699ae6f 439 *
j3 3:312589d8185c 440 * rtn_val = rtc.get_calendar(&calendar);
j3 0:b00c4699ae6f 441 *
j3 0:b00c4699ae6f 442 * @endcode
j3 0:b00c4699ae6f 443 **************************************************************/
j3 2:4e6e761c60f2 444 uint16_t Ds3231::get_calendar(ds3231_calendar_t* calendar)
j3 0:b00c4699ae6f 445 {
j3 2:4e6e761c60f2 446 uint16_t rtn_val = 1;
j3 2:4e6e761c60f2 447 uint8_t data[4];
j3 2:4e6e761c60f2 448
j3 2:4e6e761c60f2 449 data[0] = DAY;
j3 2:4e6e761c60f2 450 rtn_val = write(w_adrs, (const char*) data, 1);
j3 2:4e6e761c60f2 451
j3 2:4e6e761c60f2 452 if(!rtn_val)
j3 2:4e6e761c60f2 453 {
j3 2:4e6e761c60f2 454 rtn_val = read(r_adrs,(char *) data, 4);
j3 2:4e6e761c60f2 455
j3 2:4e6e761c60f2 456 calendar->day = bcd_2_uchar(data[0]);
j3 2:4e6e761c60f2 457 calendar->date = bcd_2_uchar(data[1]);
j3 2:4e6e761c60f2 458 calendar->month = bcd_2_uchar((data[2]&0x1F));
j3 2:4e6e761c60f2 459 calendar->year = bcd_2_uchar(data[3]);
j3 2:4e6e761c60f2 460 }
j3 2:4e6e761c60f2 461
j3 2:4e6e761c60f2 462 return(rtn_val);
j3 0:b00c4699ae6f 463 }
j3 0:b00c4699ae6f 464
j3 0:b00c4699ae6f 465
j3 0:b00c4699ae6f 466 /**********************************************************//**
j3 0:b00c4699ae6f 467 * Get either Alarm1 or Alarm2 of DS3231
j3 0:b00c4699ae6f 468 *
j3 0:b00c4699ae6f 469 * On Entry:
j3 2:4e6e761c60f2 470 * @param[in] alarm - pointer to struct for storing alarm
j3 2:4e6e761c60f2 471 * data;
j3 0:b00c4699ae6f 472 * seconds, minutes, hours, day, date
j3 0:b00c4699ae6f 473 * seconds used on Alarm1 only
j3 2:4e6e761c60f2 474 *
j3 0:b00c4699ae6f 475 * @param[in] one_r_two - TRUE for Alarm1 and FALSE for
j3 0:b00c4699ae6f 476 * Alarm2
j3 0:b00c4699ae6f 477 *
j3 0:b00c4699ae6f 478 * On Exit:
j3 2:4e6e761c60f2 479 * @param[out] alarm - contains integer alarm data
j3 0:b00c4699ae6f 480 * @return return value = 0 on success, non-0 on failure
j3 0:b00c4699ae6f 481 *
j3 0:b00c4699ae6f 482 * Example:
j3 0:b00c4699ae6f 483 * @code
j3 0:b00c4699ae6f 484 *
j3 0:b00c4699ae6f 485 * //instantiate rtc object
j3 1:c814af60fdbf 486 * Ds3231 rtc(D14, D15);
j3 0:b00c4699ae6f 487 *
j3 0:b00c4699ae6f 488 * //see datasheet for alarm format
j3 0:b00c4699ae6f 489 * ds3231_alrm_t alarm = {0, 0, 0, 0, 0};
j3 0:b00c4699ae6f 490 * uint16_t rtn_val;
j3 0:b00c4699ae6f 491 *
j3 3:312589d8185c 492 * rtn_val = rtc.get_alarm(&alarm, FALSE);
j3 0:b00c4699ae6f 493 *
j3 0:b00c4699ae6f 494 * @endcode
j3 0:b00c4699ae6f 495 **************************************************************/
j3 2:4e6e761c60f2 496 uint16_t Ds3231::get_alarm(ds3231_alrm_t* alarm, bool one_r_two)
j3 0:b00c4699ae6f 497 {
j3 2:4e6e761c60f2 498 uint16_t rtn_val = 1;
j3 2:4e6e761c60f2 499 uint8_t data[4];
j3 2:4e6e761c60f2 500
j3 2:4e6e761c60f2 501 if(one_r_two)
j3 2:4e6e761c60f2 502 {
j3 2:4e6e761c60f2 503 data[0] = ALRM1_SECONDS;
j3 2:4e6e761c60f2 504 rtn_val = write(w_adrs, (const char*) data, 1);
j3 2:4e6e761c60f2 505
j3 2:4e6e761c60f2 506 if(!rtn_val)
j3 2:4e6e761c60f2 507 {
j3 2:4e6e761c60f2 508 rtn_val = read(r_adrs,(char *) data, 4);
j3 2:4e6e761c60f2 509
j3 2:4e6e761c60f2 510 alarm->seconds = bcd_2_uchar(data[0]&0x7F);
j3 2:4e6e761c60f2 511 alarm->minutes = bcd_2_uchar(data[1]&0x7F);
j3 2:4e6e761c60f2 512 alarm->hours = bcd_2_uchar(data[2]&0x1F);
j3 2:4e6e761c60f2 513
j3 2:4e6e761c60f2 514 if(data[3] & DY_DT)
j3 2:4e6e761c60f2 515 {
j3 2:4e6e761c60f2 516 alarm->day = bcd_2_uchar(data[3]&0x0F);
j3 2:4e6e761c60f2 517 }
j3 2:4e6e761c60f2 518 else
j3 2:4e6e761c60f2 519 {
j3 2:4e6e761c60f2 520 alarm->date = bcd_2_uchar(data[3]&0x3F);
j3 2:4e6e761c60f2 521 }
j3 2:4e6e761c60f2 522 }
j3 2:4e6e761c60f2 523 }
j3 2:4e6e761c60f2 524 else
j3 2:4e6e761c60f2 525 {
j3 2:4e6e761c60f2 526 data[0] = ALRM2_MINUTES;
j3 2:4e6e761c60f2 527 rtn_val = write(w_adrs, (const char*) data, 1);
j3 2:4e6e761c60f2 528
j3 2:4e6e761c60f2 529 if(!rtn_val)
j3 2:4e6e761c60f2 530 {
j3 2:4e6e761c60f2 531 rtn_val = read(r_adrs,(char *) data, 4);
j3 2:4e6e761c60f2 532
j3 2:4e6e761c60f2 533 alarm->minutes = bcd_2_uchar(data[0]&0x7F);
j3 2:4e6e761c60f2 534 alarm->hours = bcd_2_uchar(data[1]&0x1F);
j3 2:4e6e761c60f2 535
j3 2:4e6e761c60f2 536 if(data[3] & DY_DT)
j3 2:4e6e761c60f2 537 {
j3 2:4e6e761c60f2 538 alarm->day = bcd_2_uchar(data[2]&0x0F);
j3 2:4e6e761c60f2 539 }
j3 2:4e6e761c60f2 540 else
j3 2:4e6e761c60f2 541 {
j3 2:4e6e761c60f2 542 alarm->date = bcd_2_uchar(data[2]&0x3F);
j3 2:4e6e761c60f2 543 }
j3 2:4e6e761c60f2 544 }
j3 2:4e6e761c60f2 545 }
j3 2:4e6e761c60f2 546
j3 2:4e6e761c60f2 547 return(rtn_val);
j3 0:b00c4699ae6f 548 }
j3 0:b00c4699ae6f 549
j3 0:b00c4699ae6f 550
j3 0:b00c4699ae6f 551 /**********************************************************//**
j3 0:b00c4699ae6f 552 * Get control and status registers of DS3231
j3 0:b00c4699ae6f 553 *
j3 0:b00c4699ae6f 554 * On Entry:
j3 2:4e6e761c60f2 555 * @param[in] data - pointer to struct for storing control
j3 2:4e6e761c60f2 556 * nd status register data
j3 0:b00c4699ae6f 557 *
j3 0:b00c4699ae6f 558 * On Exit:
j3 0:b00c4699ae6f 559 * @param[out] data - contains control and status registers
j3 0:b00c4699ae6f 560 * data
j3 0:b00c4699ae6f 561 * @return return value = 0 on success, non-0 on failure
j3 0:b00c4699ae6f 562 *
j3 0:b00c4699ae6f 563 * Example:
j3 0:b00c4699ae6f 564 * @code
j3 0:b00c4699ae6f 565 *
j3 0:b00c4699ae6f 566 * //instantiate rtc object
j3 1:c814af60fdbf 567 * Ds3231 rtc(D14, D15);
j3 0:b00c4699ae6f 568 *
j3 0:b00c4699ae6f 569 * //do not use 0xAA, see datasheet for appropriate data
j3 0:b00c4699ae6f 570 * ds3231_cntl_stat_t data = {0xAA, 0xAA};
j3 0:b00c4699ae6f 571 *
j3 3:312589d8185c 572 * rtn_val = rtc.get_cntl_stat_reg(&data);
j3 0:b00c4699ae6f 573 *
j3 0:b00c4699ae6f 574 * @endcode
j3 0:b00c4699ae6f 575 **************************************************************/
j3 2:4e6e761c60f2 576 uint16_t Ds3231::get_cntl_stat_reg(ds3231_cntl_stat_t* data)
j3 0:b00c4699ae6f 577 {
j3 2:4e6e761c60f2 578 uint16_t rtn_val = 1;
j3 2:4e6e761c60f2 579 uint8_t local_data[2];
j3 2:4e6e761c60f2 580
j3 2:4e6e761c60f2 581 local_data[0] = CONTROL;
j3 2:4e6e761c60f2 582 rtn_val = write(w_adrs, (const char*) local_data, 1);
j3 2:4e6e761c60f2 583
j3 2:4e6e761c60f2 584 if(!rtn_val)
j3 2:4e6e761c60f2 585 {
j3 2:4e6e761c60f2 586 rtn_val = read(r_adrs,(char *) local_data, 2);
j3 2:4e6e761c60f2 587
j3 2:4e6e761c60f2 588 data->control = local_data[0];
j3 2:4e6e761c60f2 589 data->status = local_data[1];
j3 2:4e6e761c60f2 590 }
j3 2:4e6e761c60f2 591
j3 2:4e6e761c60f2 592 return(rtn_val);
j3 0:b00c4699ae6f 593 }
j3 0:b00c4699ae6f 594
j3 0:b00c4699ae6f 595
j3 0:b00c4699ae6f 596 /**********************************************************//**
j3 0:b00c4699ae6f 597 * Get temperature data of DS3231
j3 0:b00c4699ae6f 598 *
j3 0:b00c4699ae6f 599 * On Entry:
j3 0:b00c4699ae6f 600 *
j3 0:b00c4699ae6f 601 * On Exit:
j3 2:4e6e761c60f2 602 * @return return value = raw temperature data
j3 0:b00c4699ae6f 603 *
j3 0:b00c4699ae6f 604 * Example:
j3 0:b00c4699ae6f 605 * @code
j3 0:b00c4699ae6f 606 *
j3 0:b00c4699ae6f 607 * //instantiate rtc object
j3 1:c814af60fdbf 608 * Ds3231 rtc(D14, D15);
j3 0:b00c4699ae6f 609 *
j3 0:b00c4699ae6f 610 * uint16_t temp;
j3 0:b00c4699ae6f 611 *
j3 2:4e6e761c60f2 612 * temp = rtc.get_temperature();
j3 0:b00c4699ae6f 613 *
j3 0:b00c4699ae6f 614 * @endcode
j3 0:b00c4699ae6f 615 **************************************************************/
j3 2:4e6e761c60f2 616 uint16_t Ds3231::get_temperature(void)
j3 0:b00c4699ae6f 617 {
j3 2:4e6e761c60f2 618 uint16_t rtn_val = 1;
j3 2:4e6e761c60f2 619 uint8_t data[2];
j3 2:4e6e761c60f2 620
j3 2:4e6e761c60f2 621 data[0] = MSB_TEMP;
j3 2:4e6e761c60f2 622 rtn_val = write(w_adrs, (const char*) data, 1);
j3 2:4e6e761c60f2 623
j3 2:4e6e761c60f2 624 if(!rtn_val)
j3 2:4e6e761c60f2 625 {
j3 2:4e6e761c60f2 626 read(r_adrs,(char *) data, 2);
j3 2:4e6e761c60f2 627
j3 2:4e6e761c60f2 628 rtn_val = data[0] << 8;
j3 2:4e6e761c60f2 629 rtn_val |= data[1];
j3 2:4e6e761c60f2 630 }
j3 2:4e6e761c60f2 631
j3 2:4e6e761c60f2 632 return(rtn_val);
j3 0:b00c4699ae6f 633 }
j3 0:b00c4699ae6f 634
j3 2:4e6e761c60f2 635
j3 2:4e6e761c60f2 636 /**********************************************************//**
j3 2:4e6e761c60f2 637 * Private mmber fx, converts unsigned char to BCD
j3 2:4e6e761c60f2 638 *
j3 2:4e6e761c60f2 639 * On Entry:
j3 2:4e6e761c60f2 640 * @param[in] data - 0-255
j3 2:4e6e761c60f2 641 *
j3 2:4e6e761c60f2 642 * On Exit:
j3 2:4e6e761c60f2 643 * @return bcd_result = BCD representation of data
j3 2:4e6e761c60f2 644 *
j3 2:4e6e761c60f2 645 **************************************************************/
j3 2:4e6e761c60f2 646 uint16_t Ds3231::uchar_2_bcd(uint8_t data)
j3 2:4e6e761c60f2 647 {
j3 2:4e6e761c60f2 648 uint16_t bcd_result = 0;
j3 2:4e6e761c60f2 649
j3 2:4e6e761c60f2 650 //Get hundreds
j3 2:4e6e761c60f2 651 bcd_result |= ((data/100) << 8);
j3 2:4e6e761c60f2 652 data = (data - (data/100)*100);
j3 2:4e6e761c60f2 653
j3 2:4e6e761c60f2 654 //Get tens
j3 2:4e6e761c60f2 655 bcd_result |= ((data/10) << 4);
j3 2:4e6e761c60f2 656 data = (data - (data/10)*10);
j3 2:4e6e761c60f2 657
j3 2:4e6e761c60f2 658 //Get ones
j3 2:4e6e761c60f2 659 bcd_result |= data;
j3 2:4e6e761c60f2 660
j3 2:4e6e761c60f2 661 return(bcd_result);
j3 2:4e6e761c60f2 662 }
j3 2:4e6e761c60f2 663
j3 2:4e6e761c60f2 664
j3 2:4e6e761c60f2 665 /**********************************************************//**
j3 2:4e6e761c60f2 666 * Private mmber fx, converts BCD to a uint8_t
j3 2:4e6e761c60f2 667 *
j3 2:4e6e761c60f2 668 * On Entry:
j3 2:4e6e761c60f2 669 * @param[in] bcd - 0-99
j3 2:4e6e761c60f2 670 *
j3 2:4e6e761c60f2 671 * On Exit:
j3 2:4e6e761c60f2 672 * @return rtn_val = integer rep. of BCD
j3 2:4e6e761c60f2 673 *
j3 2:4e6e761c60f2 674 **************************************************************/
j3 2:4e6e761c60f2 675 uint8_t Ds3231::bcd_2_uchar(uint8_t bcd)
j3 2:4e6e761c60f2 676 {
j3 2:4e6e761c60f2 677 uint8_t rtn_val = 0;
j3 2:4e6e761c60f2 678
j3 2:4e6e761c60f2 679 rtn_val += ((bcd&0xf0)>>4)*10;
j3 2:4e6e761c60f2 680 rtn_val += (bcd&0x000f);
j3 2:4e6e761c60f2 681
j3 2:4e6e761c60f2 682 return rtn_val;
j3 2:4e6e761c60f2 683 }
j3 2:4e6e761c60f2 684
j3 2:4e6e761c60f2 685