j
Fork of ds3231 by
ds3231.cpp@7:0551d7e7c13f, 2014-12-01 (annotated)
- Committer:
- j3
- Date:
- Mon Dec 01 20:31:03 2014 +0000
- Revision:
- 7:0551d7e7c13f
- Parent:
- 6:e8850ad15893
- Child:
- 10:3b55ed3f71d3
Updated all set fx's to test for data range
Who changed what in which revision?
User | Revision | Line number | New 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 | 4:0beb5e9ac927 | 94 | * //time = 12:00:00 AM 12hr mode |
j3 | 4:0beb5e9ac927 | 95 | * ds3231_time_t time = {12, 0, 0, 0, 1} |
j3 | 0:b00c4699ae6f | 96 | * uint16_t rtn_val; |
j3 | 0:b00c4699ae6f | 97 | * |
j3 | 0:b00c4699ae6f | 98 | * rtn_val = rtc.set_time(time); |
j3 | 0:b00c4699ae6f | 99 | * |
j3 | 0:b00c4699ae6f | 100 | * @endcode |
j3 | 0:b00c4699ae6f | 101 | **************************************************************/ |
j3 | 0:b00c4699ae6f | 102 | uint16_t Ds3231::set_time(ds3231_time_t time) |
j3 | 0:b00c4699ae6f | 103 | { |
j3 | 2:4e6e761c60f2 | 104 | uint8_t data[] = {0,0,0,0}; |
j3 | 2:4e6e761c60f2 | 105 | uint8_t data_length = 0; |
j3 | 2:4e6e761c60f2 | 106 | uint8_t max_hour = 24; |
j3 | 0:b00c4699ae6f | 107 | |
j3 | 2:4e6e761c60f2 | 108 | data[data_length++] = SECONDS; |
j3 | 2:4e6e761c60f2 | 109 | data[data_length++] = uchar_2_bcd(time.seconds); |
j3 | 2:4e6e761c60f2 | 110 | data[data_length++] = uchar_2_bcd(time.minutes); |
j3 | 2:4e6e761c60f2 | 111 | |
j3 | 2:4e6e761c60f2 | 112 | //format Hours register |
j3 | 2:4e6e761c60f2 | 113 | data[data_length] = uchar_2_bcd(time.hours); |
j3 | 2:4e6e761c60f2 | 114 | if(time.mode) |
j3 | 2:4e6e761c60f2 | 115 | { |
j3 | 2:4e6e761c60f2 | 116 | max_hour = max_hour/2; |
j3 | 2:4e6e761c60f2 | 117 | |
j3 | 2:4e6e761c60f2 | 118 | data[data_length] |= MODE; |
j3 | 2:4e6e761c60f2 | 119 | if(time.am_pm) |
j3 | 2:4e6e761c60f2 | 120 | { |
j3 | 2:4e6e761c60f2 | 121 | data[data_length] |= AM_PM; |
j3 | 2:4e6e761c60f2 | 122 | } |
j3 | 2:4e6e761c60f2 | 123 | |
j3 | 2:4e6e761c60f2 | 124 | } |
j3 | 2:4e6e761c60f2 | 125 | else |
j3 | 2:4e6e761c60f2 | 126 | { |
j3 | 2:4e6e761c60f2 | 127 | max_hour = max_hour - 1; |
j3 | 2:4e6e761c60f2 | 128 | } |
j3 | 2:4e6e761c60f2 | 129 | data_length++; |
j3 | 2:4e6e761c60f2 | 130 | |
j3 | 7:0551d7e7c13f | 131 | //Make sure data is within range. |
j3 | 2:4e6e761c60f2 | 132 | if((time.seconds > 59) || (time.minutes > 59) || (time.hours > max_hour)) |
j3 | 2:4e6e761c60f2 | 133 | { |
j3 | 2:4e6e761c60f2 | 134 | return(1); |
j3 | 2:4e6e761c60f2 | 135 | } |
j3 | 2:4e6e761c60f2 | 136 | else |
j3 | 2:4e6e761c60f2 | 137 | { |
j3 | 2:4e6e761c60f2 | 138 | return(write(w_adrs,(const char*) data, data_length)); |
j3 | 2:4e6e761c60f2 | 139 | } |
j3 | 0:b00c4699ae6f | 140 | } |
j3 | 0:b00c4699ae6f | 141 | |
j3 | 0:b00c4699ae6f | 142 | |
j3 | 0:b00c4699ae6f | 143 | /**********************************************************//** |
j3 | 0:b00c4699ae6f | 144 | * Sets the calendar on DS3231 |
j3 | 0:b00c4699ae6f | 145 | * |
j3 | 0:b00c4699ae6f | 146 | * On Entry: |
j3 | 3:312589d8185c | 147 | * @param[in] calendar - struct cotaining calendar data |
j3 | 0:b00c4699ae6f | 148 | * |
j3 | 0:b00c4699ae6f | 149 | * On Exit: |
j3 | 0:b00c4699ae6f | 150 | * @return return value = 0 on success, non-0 on failure |
j3 | 0:b00c4699ae6f | 151 | * |
j3 | 0:b00c4699ae6f | 152 | * Example: |
j3 | 0:b00c4699ae6f | 153 | * @code |
j3 | 0:b00c4699ae6f | 154 | * |
j3 | 0:b00c4699ae6f | 155 | * //instantiate rtc object |
j3 | 1:c814af60fdbf | 156 | * Ds3231 rtc(D14, D15); |
j3 | 0:b00c4699ae6f | 157 | * |
j3 | 0:b00c4699ae6f | 158 | * //see datasheet for calendar format |
j3 | 0:b00c4699ae6f | 159 | * ds3231_calendar_t calendar = {1, 1, 1, 0}; |
j3 | 0:b00c4699ae6f | 160 | * uint16_t rtn_val; |
j3 | 0:b00c4699ae6f | 161 | * |
j3 | 0:b00c4699ae6f | 162 | * rtn_val = rtc.set_calendar(calendar); |
j3 | 0:b00c4699ae6f | 163 | * |
j3 | 0:b00c4699ae6f | 164 | * @endcode |
j3 | 0:b00c4699ae6f | 165 | **************************************************************/ |
j3 | 0:b00c4699ae6f | 166 | uint16_t Ds3231::set_calendar(ds3231_calendar_t calendar) |
j3 | 0:b00c4699ae6f | 167 | { |
j3 | 2:4e6e761c60f2 | 168 | uint8_t data[] = {0,0,0,0,0}; |
j3 | 2:4e6e761c60f2 | 169 | uint8_t data_length = 0; |
j3 | 2:4e6e761c60f2 | 170 | |
j3 | 2:4e6e761c60f2 | 171 | data[data_length++] = DAY; |
j3 | 2:4e6e761c60f2 | 172 | data[data_length++] = uchar_2_bcd(calendar.day); |
j3 | 2:4e6e761c60f2 | 173 | data[data_length++] = uchar_2_bcd(calendar.date); |
j3 | 2:4e6e761c60f2 | 174 | data[data_length++] = uchar_2_bcd(calendar.month); |
j3 | 2:4e6e761c60f2 | 175 | data[data_length++] = uchar_2_bcd(calendar.year); |
j3 | 0:b00c4699ae6f | 176 | |
j3 | 7:0551d7e7c13f | 177 | //Make sure data is within range. |
j3 | 7:0551d7e7c13f | 178 | if(((calendar.day < 1) || (calendar.day > 7)) || |
j3 | 7:0551d7e7c13f | 179 | ((calendar.date < 1) || (calendar.date > 31)) || |
j3 | 7:0551d7e7c13f | 180 | ((calendar.month < 1) || (calendar.month > 12)) || |
j3 | 7:0551d7e7c13f | 181 | (calendar.year > 99)) |
j3 | 7:0551d7e7c13f | 182 | { |
j3 | 7:0551d7e7c13f | 183 | return(1); |
j3 | 7:0551d7e7c13f | 184 | } |
j3 | 7:0551d7e7c13f | 185 | else |
j3 | 7:0551d7e7c13f | 186 | { |
j3 | 7:0551d7e7c13f | 187 | return(write(w_adrs,(const char*) data, data_length)); |
j3 | 7:0551d7e7c13f | 188 | } |
j3 | 0:b00c4699ae6f | 189 | } |
j3 | 0:b00c4699ae6f | 190 | |
j3 | 0:b00c4699ae6f | 191 | |
j3 | 0:b00c4699ae6f | 192 | /**********************************************************//** |
j3 | 0:b00c4699ae6f | 193 | * Set either Alarm1 or Alarm2 of DS3231 |
j3 | 0:b00c4699ae6f | 194 | * |
j3 | 0:b00c4699ae6f | 195 | * On Entry: |
j3 | 0:b00c4699ae6f | 196 | * @param[in] alarm - struct cotaining alarm data |
j3 | 3:312589d8185c | 197 | * |
j3 | 0:b00c4699ae6f | 198 | * @param[in] one_r_two - TRUE for Alarm1 and FALSE for |
j3 | 0:b00c4699ae6f | 199 | * Alarm2 |
j3 | 0:b00c4699ae6f | 200 | * |
j3 | 0:b00c4699ae6f | 201 | * On Exit: |
j3 | 0:b00c4699ae6f | 202 | * @return return value = 0 on success, non-0 on failure |
j3 | 0:b00c4699ae6f | 203 | * |
j3 | 0:b00c4699ae6f | 204 | * Example: |
j3 | 0:b00c4699ae6f | 205 | * @code |
j3 | 0:b00c4699ae6f | 206 | * |
j3 | 0:b00c4699ae6f | 207 | * //instantiate rtc object |
j3 | 1:c814af60fdbf | 208 | * Ds3231 rtc(D14, D15); |
j3 | 0:b00c4699ae6f | 209 | * |
j3 | 4:0beb5e9ac927 | 210 | * //see ds3231.h for .members and datasheet for alarm format |
j3 | 4:0beb5e9ac927 | 211 | * ds3231_alrm_t alarm; |
j3 | 0:b00c4699ae6f | 212 | * uint16_t rtn_val; |
j3 | 0:b00c4699ae6f | 213 | * |
j3 | 0:b00c4699ae6f | 214 | * rtn_val = rtc.set_alarm(alarm, FALSE); |
j3 | 0:b00c4699ae6f | 215 | * |
j3 | 0:b00c4699ae6f | 216 | * @endcode |
j3 | 0:b00c4699ae6f | 217 | **************************************************************/ |
j3 | 0:b00c4699ae6f | 218 | uint16_t Ds3231::set_alarm(ds3231_alrm_t alarm, bool one_r_two) |
j3 | 0:b00c4699ae6f | 219 | { |
j3 | 2:4e6e761c60f2 | 220 | uint8_t data[] = {0,0,0,0,0}; |
j3 | 2:4e6e761c60f2 | 221 | uint8_t data_length = 0; |
j3 | 2:4e6e761c60f2 | 222 | uint8_t max_hour = 24; |
j3 | 2:4e6e761c60f2 | 223 | uint8_t mask_var = 0; |
j3 | 2:4e6e761c60f2 | 224 | |
j3 | 2:4e6e761c60f2 | 225 | //setting alarm 1 or 2? |
j3 | 2:4e6e761c60f2 | 226 | if(one_r_two) |
j3 | 2:4e6e761c60f2 | 227 | { |
j3 | 2:4e6e761c60f2 | 228 | data[data_length++] = ALRM1_SECONDS; |
j3 | 2:4e6e761c60f2 | 229 | |
j3 | 2:4e6e761c60f2 | 230 | //config seconds register |
j3 | 2:4e6e761c60f2 | 231 | if(alarm.am1) |
j3 | 2:4e6e761c60f2 | 232 | { |
j3 | 2:4e6e761c60f2 | 233 | mask_var |= ALRM_MASK; |
j3 | 2:4e6e761c60f2 | 234 | } |
j3 | 2:4e6e761c60f2 | 235 | data[data_length++] = (mask_var | uchar_2_bcd(alarm.seconds)); |
j3 | 2:4e6e761c60f2 | 236 | mask_var = 0; |
j3 | 2:4e6e761c60f2 | 237 | |
j3 | 2:4e6e761c60f2 | 238 | //config minutes register |
j3 | 2:4e6e761c60f2 | 239 | if(alarm.am2) |
j3 | 2:4e6e761c60f2 | 240 | { |
j3 | 2:4e6e761c60f2 | 241 | mask_var |= ALRM_MASK; |
j3 | 2:4e6e761c60f2 | 242 | } |
j3 | 2:4e6e761c60f2 | 243 | data[data_length++] = (mask_var | uchar_2_bcd(alarm.minutes)); |
j3 | 2:4e6e761c60f2 | 244 | mask_var = 0; |
j3 | 2:4e6e761c60f2 | 245 | |
j3 | 2:4e6e761c60f2 | 246 | //config hours register |
j3 | 2:4e6e761c60f2 | 247 | if(alarm.am3) |
j3 | 2:4e6e761c60f2 | 248 | { |
j3 | 2:4e6e761c60f2 | 249 | mask_var |= ALRM_MASK; |
j3 | 2:4e6e761c60f2 | 250 | } |
j3 | 2:4e6e761c60f2 | 251 | if(alarm.mode) |
j3 | 2:4e6e761c60f2 | 252 | { |
j3 | 2:4e6e761c60f2 | 253 | max_hour = max_hour/2; |
j3 | 2:4e6e761c60f2 | 254 | mask_var |= MODE; |
j3 | 2:4e6e761c60f2 | 255 | if(alarm.am_pm) |
j3 | 2:4e6e761c60f2 | 256 | { |
j3 | 2:4e6e761c60f2 | 257 | mask_var |= AM_PM; |
j3 | 2:4e6e761c60f2 | 258 | } |
j3 | 2:4e6e761c60f2 | 259 | } |
j3 | 2:4e6e761c60f2 | 260 | else |
j3 | 2:4e6e761c60f2 | 261 | { |
j3 | 2:4e6e761c60f2 | 262 | max_hour = max_hour - 1; |
j3 | 2:4e6e761c60f2 | 263 | } |
j3 | 2:4e6e761c60f2 | 264 | data[data_length++] = (mask_var | uchar_2_bcd(alarm.hours)); |
j3 | 2:4e6e761c60f2 | 265 | mask_var = 0; |
j3 | 2:4e6e761c60f2 | 266 | |
j3 | 2:4e6e761c60f2 | 267 | //config day/date register |
j3 | 2:4e6e761c60f2 | 268 | if(alarm.am4) |
j3 | 2:4e6e761c60f2 | 269 | { |
j3 | 2:4e6e761c60f2 | 270 | mask_var |= ALRM_MASK; |
j3 | 2:4e6e761c60f2 | 271 | } |
j3 | 2:4e6e761c60f2 | 272 | if(alarm.dy_dt) |
j3 | 2:4e6e761c60f2 | 273 | { |
j3 | 2:4e6e761c60f2 | 274 | mask_var |= DY_DT; |
j3 | 2:4e6e761c60f2 | 275 | data[data_length++] = (mask_var | uchar_2_bcd(alarm.day)); |
j3 | 2:4e6e761c60f2 | 276 | } |
j3 | 2:4e6e761c60f2 | 277 | else |
j3 | 2:4e6e761c60f2 | 278 | { |
j3 | 2:4e6e761c60f2 | 279 | data[data_length++] = (mask_var | uchar_2_bcd(alarm.date)); |
j3 | 2:4e6e761c60f2 | 280 | } |
j3 | 2:4e6e761c60f2 | 281 | mask_var = 0; |
j3 | 2:4e6e761c60f2 | 282 | } |
j3 | 2:4e6e761c60f2 | 283 | else |
j3 | 2:4e6e761c60f2 | 284 | { |
j3 | 2:4e6e761c60f2 | 285 | data[data_length++] = ALRM2_MINUTES; |
j3 | 2:4e6e761c60f2 | 286 | |
j3 | 2:4e6e761c60f2 | 287 | //config minutes register |
j3 | 2:4e6e761c60f2 | 288 | if(alarm.am2) |
j3 | 2:4e6e761c60f2 | 289 | { |
j3 | 2:4e6e761c60f2 | 290 | mask_var |= ALRM_MASK; |
j3 | 2:4e6e761c60f2 | 291 | } |
j3 | 2:4e6e761c60f2 | 292 | data[data_length++] = (mask_var | uchar_2_bcd(alarm.minutes)); |
j3 | 2:4e6e761c60f2 | 293 | mask_var = 0; |
j3 | 2:4e6e761c60f2 | 294 | |
j3 | 2:4e6e761c60f2 | 295 | //config hours register |
j3 | 2:4e6e761c60f2 | 296 | if(alarm.am3) |
j3 | 2:4e6e761c60f2 | 297 | { |
j3 | 2:4e6e761c60f2 | 298 | mask_var |= ALRM_MASK; |
j3 | 2:4e6e761c60f2 | 299 | } |
j3 | 2:4e6e761c60f2 | 300 | if(alarm.mode) |
j3 | 2:4e6e761c60f2 | 301 | { |
j3 | 2:4e6e761c60f2 | 302 | max_hour = max_hour/2; |
j3 | 2:4e6e761c60f2 | 303 | mask_var |= MODE; |
j3 | 2:4e6e761c60f2 | 304 | if(alarm.am_pm) |
j3 | 2:4e6e761c60f2 | 305 | { |
j3 | 2:4e6e761c60f2 | 306 | mask_var |= AM_PM; |
j3 | 2:4e6e761c60f2 | 307 | } |
j3 | 2:4e6e761c60f2 | 308 | } |
j3 | 2:4e6e761c60f2 | 309 | else |
j3 | 2:4e6e761c60f2 | 310 | { |
j3 | 2:4e6e761c60f2 | 311 | max_hour = max_hour - 1; |
j3 | 2:4e6e761c60f2 | 312 | } |
j3 | 2:4e6e761c60f2 | 313 | data[data_length++] = (mask_var | uchar_2_bcd(alarm.hours)); |
j3 | 2:4e6e761c60f2 | 314 | mask_var = 0; |
j3 | 2:4e6e761c60f2 | 315 | |
j3 | 2:4e6e761c60f2 | 316 | //config day/date register |
j3 | 2:4e6e761c60f2 | 317 | if(alarm.am4) |
j3 | 2:4e6e761c60f2 | 318 | { |
j3 | 2:4e6e761c60f2 | 319 | mask_var |= ALRM_MASK; |
j3 | 2:4e6e761c60f2 | 320 | } |
j3 | 2:4e6e761c60f2 | 321 | if(alarm.dy_dt) |
j3 | 2:4e6e761c60f2 | 322 | { |
j3 | 2:4e6e761c60f2 | 323 | mask_var |= DY_DT; |
j3 | 2:4e6e761c60f2 | 324 | data[data_length++] = (mask_var | uchar_2_bcd(alarm.day)); |
j3 | 2:4e6e761c60f2 | 325 | } |
j3 | 2:4e6e761c60f2 | 326 | else |
j3 | 2:4e6e761c60f2 | 327 | { |
j3 | 2:4e6e761c60f2 | 328 | data[data_length++] = (mask_var | uchar_2_bcd(alarm.date)); |
j3 | 2:4e6e761c60f2 | 329 | } |
j3 | 2:4e6e761c60f2 | 330 | mask_var = 0; |
j3 | 2:4e6e761c60f2 | 331 | } |
j3 | 2:4e6e761c60f2 | 332 | |
j3 | 7:0551d7e7c13f | 333 | //Make sure data is within range. |
j3 | 7:0551d7e7c13f | 334 | if((alarm.seconds > 59) || (alarm.minutes > 59) || (alarm.hours > max_hour) || |
j3 | 7:0551d7e7c13f | 335 | ((alarm.day < 1) || (alarm.day > 7)) || |
j3 | 7:0551d7e7c13f | 336 | ((alarm.date < 1) || (alarm.date > 31))) |
j3 | 2:4e6e761c60f2 | 337 | { |
j3 | 2:4e6e761c60f2 | 338 | return(1); |
j3 | 2:4e6e761c60f2 | 339 | } |
j3 | 2:4e6e761c60f2 | 340 | else |
j3 | 2:4e6e761c60f2 | 341 | { |
j3 | 2:4e6e761c60f2 | 342 | return(write(w_adrs,(const char*) data, data_length)); |
j3 | 2:4e6e761c60f2 | 343 | } |
j3 | 0:b00c4699ae6f | 344 | } |
j3 | 0:b00c4699ae6f | 345 | |
j3 | 0:b00c4699ae6f | 346 | |
j3 | 0:b00c4699ae6f | 347 | /**********************************************************//** |
j3 | 0:b00c4699ae6f | 348 | * Set control and status registers of DS3231 |
j3 | 0:b00c4699ae6f | 349 | * |
j3 | 0:b00c4699ae6f | 350 | * On Entry: |
j3 | 0:b00c4699ae6f | 351 | * @param[in] data - Struct containing control and status |
j3 | 0:b00c4699ae6f | 352 | * register data |
j3 | 0:b00c4699ae6f | 353 | * |
j3 | 0:b00c4699ae6f | 354 | * On Exit: |
j3 | 0:b00c4699ae6f | 355 | * @return return value = 0 on success, non-0 on failure |
j3 | 0:b00c4699ae6f | 356 | * |
j3 | 0:b00c4699ae6f | 357 | * Example: |
j3 | 0:b00c4699ae6f | 358 | * @code |
j3 | 0:b00c4699ae6f | 359 | * |
j3 | 0:b00c4699ae6f | 360 | * //instantiate rtc object |
j3 | 1:c814af60fdbf | 361 | * Ds3231 rtc(D14, D15); |
j3 | 0:b00c4699ae6f | 362 | * |
j3 | 0:b00c4699ae6f | 363 | * //do not use 0xAA, see datasheet for appropriate data |
j3 | 0:b00c4699ae6f | 364 | * ds3231_cntl_stat_t data = {0xAA, 0xAA}; |
j3 | 0:b00c4699ae6f | 365 | * |
j3 | 0:b00c4699ae6f | 366 | * rtn_val = rtc.set_cntl_stat_reg(data); |
j3 | 0:b00c4699ae6f | 367 | * |
j3 | 0:b00c4699ae6f | 368 | * @endcode |
j3 | 0:b00c4699ae6f | 369 | **************************************************************/ |
j3 | 0:b00c4699ae6f | 370 | uint16_t Ds3231::set_cntl_stat_reg(ds3231_cntl_stat_t data) |
j3 | 0:b00c4699ae6f | 371 | { |
j3 | 2:4e6e761c60f2 | 372 | uint8_t local_data[] = {0,0,0}; |
j3 | 2:4e6e761c60f2 | 373 | uint8_t data_length = 0; |
j3 | 2:4e6e761c60f2 | 374 | |
j3 | 2:4e6e761c60f2 | 375 | local_data[data_length++] = CONTROL; |
j3 | 2:4e6e761c60f2 | 376 | local_data[data_length++] = data.control; |
j3 | 2:4e6e761c60f2 | 377 | local_data[data_length++] = data.status; |
j3 | 0:b00c4699ae6f | 378 | |
j3 | 2:4e6e761c60f2 | 379 | //users responsibility to make sure data is logical |
j3 | 2:4e6e761c60f2 | 380 | return(write(w_adrs,(const char*) local_data, data_length)); |
j3 | 0:b00c4699ae6f | 381 | } |
j3 | 0:b00c4699ae6f | 382 | |
j3 | 0:b00c4699ae6f | 383 | |
j3 | 0:b00c4699ae6f | 384 | /**********************************************************//** |
j3 | 0:b00c4699ae6f | 385 | * Gets the time on DS3231 |
j3 | 0:b00c4699ae6f | 386 | * |
j3 | 0:b00c4699ae6f | 387 | * On Entry: |
j3 | 4:0beb5e9ac927 | 388 | * @param[in] time - pointer to struct for storing time data |
j3 | 0:b00c4699ae6f | 389 | * |
j3 | 0:b00c4699ae6f | 390 | * On Exit: |
j3 | 2:4e6e761c60f2 | 391 | * @param[out] time - contains current integrer rtc time |
j3 | 2:4e6e761c60f2 | 392 | * data |
j3 | 0:b00c4699ae6f | 393 | * @return return value = 0 on success, non-0 on failure |
j3 | 0:b00c4699ae6f | 394 | * |
j3 | 0:b00c4699ae6f | 395 | * Example: |
j3 | 0:b00c4699ae6f | 396 | * @code |
j3 | 0:b00c4699ae6f | 397 | * |
j3 | 0:b00c4699ae6f | 398 | * //instantiate rtc object |
j3 | 1:c814af60fdbf | 399 | * Ds3231 rtc(D14, D15); |
j3 | 0:b00c4699ae6f | 400 | * |
j3 | 4:0beb5e9ac927 | 401 | * //time = 12:00:00 AM 12hr mode |
j3 | 4:0beb5e9ac927 | 402 | * ds3231_time_t time = {12, 0, 0, 0, 1} |
j3 | 0:b00c4699ae6f | 403 | * uint16_t rtn_val; |
j3 | 0:b00c4699ae6f | 404 | * |
j3 | 3:312589d8185c | 405 | * rtn_val = rtc.get_time(&time); |
j3 | 0:b00c4699ae6f | 406 | * |
j3 | 0:b00c4699ae6f | 407 | * @endcode |
j3 | 0:b00c4699ae6f | 408 | **************************************************************/ |
j3 | 2:4e6e761c60f2 | 409 | uint16_t Ds3231::get_time(ds3231_time_t* time) |
j3 | 0:b00c4699ae6f | 410 | { |
j3 | 2:4e6e761c60f2 | 411 | uint16_t rtn_val = 1; |
j3 | 2:4e6e761c60f2 | 412 | uint8_t data[3]; |
j3 | 2:4e6e761c60f2 | 413 | |
j3 | 2:4e6e761c60f2 | 414 | data[0] = SECONDS; |
j3 | 2:4e6e761c60f2 | 415 | rtn_val = write(w_adrs, (const char*) data, 1); |
j3 | 2:4e6e761c60f2 | 416 | |
j3 | 2:4e6e761c60f2 | 417 | if(!rtn_val) |
j3 | 2:4e6e761c60f2 | 418 | { |
j3 | 2:4e6e761c60f2 | 419 | rtn_val = read(r_adrs,(char *) data, 3); |
j3 | 2:4e6e761c60f2 | 420 | |
j3 | 2:4e6e761c60f2 | 421 | time->seconds = bcd_2_uchar(data[0]); |
j3 | 2:4e6e761c60f2 | 422 | time->minutes = bcd_2_uchar(data[1]); |
j3 | 2:4e6e761c60f2 | 423 | time->hours = bcd_2_uchar((data[2]&0x1F)); |
j3 | 4:0beb5e9ac927 | 424 | time->am_pm = (data[2]&AM_PM); |
j3 | 4:0beb5e9ac927 | 425 | time->mode = (data[2]&MODE); |
j3 | 2:4e6e761c60f2 | 426 | } |
j3 | 2:4e6e761c60f2 | 427 | |
j3 | 2:4e6e761c60f2 | 428 | return(rtn_val); |
j3 | 0:b00c4699ae6f | 429 | } |
j3 | 0:b00c4699ae6f | 430 | |
j3 | 0:b00c4699ae6f | 431 | |
j3 | 0:b00c4699ae6f | 432 | /**********************************************************//** |
j3 | 0:b00c4699ae6f | 433 | * Gets the calendar on DS3231 |
j3 | 0:b00c4699ae6f | 434 | * |
j3 | 0:b00c4699ae6f | 435 | * On Entry: |
j3 | 2:4e6e761c60f2 | 436 | * @param[in] calendar - pointer to struct for storing |
j3 | 4:0beb5e9ac927 | 437 | * calendar data |
j3 | 0:b00c4699ae6f | 438 | * |
j3 | 0:b00c4699ae6f | 439 | * On Exit: |
j3 | 2:4e6e761c60f2 | 440 | * @param[out] calendar - contains current integer rtc |
j3 | 2:4e6e761c60f2 | 441 | * calendar data |
j3 | 0:b00c4699ae6f | 442 | * @return return value = 0 on success, non-0 on failure |
j3 | 0:b00c4699ae6f | 443 | * |
j3 | 0:b00c4699ae6f | 444 | * Example: |
j3 | 0:b00c4699ae6f | 445 | * @code |
j3 | 0:b00c4699ae6f | 446 | * |
j3 | 0:b00c4699ae6f | 447 | * //instantiate rtc object |
j3 | 1:c814af60fdbf | 448 | * Ds3231 rtc(D14, D15); |
j3 | 0:b00c4699ae6f | 449 | * |
j3 | 0:b00c4699ae6f | 450 | * //see datasheet for calendar format |
j3 | 0:b00c4699ae6f | 451 | * ds3231_calendar_t calendar = {1, 1, 1, 0}; |
j3 | 0:b00c4699ae6f | 452 | * uint16_t rtn_val; |
j3 | 0:b00c4699ae6f | 453 | * |
j3 | 3:312589d8185c | 454 | * rtn_val = rtc.get_calendar(&calendar); |
j3 | 0:b00c4699ae6f | 455 | * |
j3 | 0:b00c4699ae6f | 456 | * @endcode |
j3 | 0:b00c4699ae6f | 457 | **************************************************************/ |
j3 | 2:4e6e761c60f2 | 458 | uint16_t Ds3231::get_calendar(ds3231_calendar_t* calendar) |
j3 | 0:b00c4699ae6f | 459 | { |
j3 | 2:4e6e761c60f2 | 460 | uint16_t rtn_val = 1; |
j3 | 2:4e6e761c60f2 | 461 | uint8_t data[4]; |
j3 | 2:4e6e761c60f2 | 462 | |
j3 | 2:4e6e761c60f2 | 463 | data[0] = DAY; |
j3 | 2:4e6e761c60f2 | 464 | rtn_val = write(w_adrs, (const char*) data, 1); |
j3 | 2:4e6e761c60f2 | 465 | |
j3 | 2:4e6e761c60f2 | 466 | if(!rtn_val) |
j3 | 2:4e6e761c60f2 | 467 | { |
j3 | 2:4e6e761c60f2 | 468 | rtn_val = read(r_adrs,(char *) data, 4); |
j3 | 2:4e6e761c60f2 | 469 | |
j3 | 2:4e6e761c60f2 | 470 | calendar->day = bcd_2_uchar(data[0]); |
j3 | 2:4e6e761c60f2 | 471 | calendar->date = bcd_2_uchar(data[1]); |
j3 | 2:4e6e761c60f2 | 472 | calendar->month = bcd_2_uchar((data[2]&0x1F)); |
j3 | 2:4e6e761c60f2 | 473 | calendar->year = bcd_2_uchar(data[3]); |
j3 | 2:4e6e761c60f2 | 474 | } |
j3 | 2:4e6e761c60f2 | 475 | |
j3 | 2:4e6e761c60f2 | 476 | return(rtn_val); |
j3 | 0:b00c4699ae6f | 477 | } |
j3 | 0:b00c4699ae6f | 478 | |
j3 | 0:b00c4699ae6f | 479 | |
j3 | 0:b00c4699ae6f | 480 | /**********************************************************//** |
j3 | 0:b00c4699ae6f | 481 | * Get either Alarm1 or Alarm2 of DS3231 |
j3 | 0:b00c4699ae6f | 482 | * |
j3 | 0:b00c4699ae6f | 483 | * On Entry: |
j3 | 2:4e6e761c60f2 | 484 | * @param[in] alarm - pointer to struct for storing alarm |
j3 | 2:4e6e761c60f2 | 485 | * data; |
j3 | 2:4e6e761c60f2 | 486 | * |
j3 | 0:b00c4699ae6f | 487 | * @param[in] one_r_two - TRUE for Alarm1 and FALSE for |
j3 | 0:b00c4699ae6f | 488 | * Alarm2 |
j3 | 0:b00c4699ae6f | 489 | * |
j3 | 0:b00c4699ae6f | 490 | * On Exit: |
j3 | 2:4e6e761c60f2 | 491 | * @param[out] alarm - contains integer alarm data |
j3 | 0:b00c4699ae6f | 492 | * @return return value = 0 on success, non-0 on failure |
j3 | 0:b00c4699ae6f | 493 | * |
j3 | 0:b00c4699ae6f | 494 | * Example: |
j3 | 0:b00c4699ae6f | 495 | * @code |
j3 | 0:b00c4699ae6f | 496 | * |
j3 | 0:b00c4699ae6f | 497 | * //instantiate rtc object |
j3 | 1:c814af60fdbf | 498 | * Ds3231 rtc(D14, D15); |
j3 | 0:b00c4699ae6f | 499 | * |
j3 | 4:0beb5e9ac927 | 500 | * //see ds3231.h for .members and datasheet for alarm format |
j3 | 4:0beb5e9ac927 | 501 | * ds3231_alrm_t alarm; |
j3 | 0:b00c4699ae6f | 502 | * uint16_t rtn_val; |
j3 | 0:b00c4699ae6f | 503 | * |
j3 | 3:312589d8185c | 504 | * rtn_val = rtc.get_alarm(&alarm, FALSE); |
j3 | 0:b00c4699ae6f | 505 | * |
j3 | 0:b00c4699ae6f | 506 | * @endcode |
j3 | 0:b00c4699ae6f | 507 | **************************************************************/ |
j3 | 2:4e6e761c60f2 | 508 | uint16_t Ds3231::get_alarm(ds3231_alrm_t* alarm, bool one_r_two) |
j3 | 0:b00c4699ae6f | 509 | { |
j3 | 2:4e6e761c60f2 | 510 | uint16_t rtn_val = 1; |
j3 | 2:4e6e761c60f2 | 511 | uint8_t data[4]; |
j3 | 2:4e6e761c60f2 | 512 | |
j3 | 2:4e6e761c60f2 | 513 | if(one_r_two) |
j3 | 2:4e6e761c60f2 | 514 | { |
j3 | 2:4e6e761c60f2 | 515 | data[0] = ALRM1_SECONDS; |
j3 | 2:4e6e761c60f2 | 516 | rtn_val = write(w_adrs, (const char*) data, 1); |
j3 | 2:4e6e761c60f2 | 517 | |
j3 | 2:4e6e761c60f2 | 518 | if(!rtn_val) |
j3 | 2:4e6e761c60f2 | 519 | { |
j3 | 2:4e6e761c60f2 | 520 | rtn_val = read(r_adrs,(char *) data, 4); |
j3 | 2:4e6e761c60f2 | 521 | |
j3 | 2:4e6e761c60f2 | 522 | alarm->seconds = bcd_2_uchar(data[0]&0x7F); |
j3 | 4:0beb5e9ac927 | 523 | alarm->am1 = (data[0]&ALRM_MASK); |
j3 | 2:4e6e761c60f2 | 524 | alarm->minutes = bcd_2_uchar(data[1]&0x7F); |
j3 | 4:0beb5e9ac927 | 525 | alarm->am2 = (data[1]&ALRM_MASK); |
j3 | 2:4e6e761c60f2 | 526 | alarm->hours = bcd_2_uchar(data[2]&0x1F); |
j3 | 4:0beb5e9ac927 | 527 | alarm->am3 = (data[2]&ALRM_MASK); |
j3 | 4:0beb5e9ac927 | 528 | alarm->am_pm = (data[2]&AM_PM); |
j3 | 4:0beb5e9ac927 | 529 | alarm->mode = (data[2]&MODE); |
j3 | 2:4e6e761c60f2 | 530 | |
j3 | 2:4e6e761c60f2 | 531 | if(data[3] & DY_DT) |
j3 | 2:4e6e761c60f2 | 532 | { |
j3 | 4:0beb5e9ac927 | 533 | alarm->dy_dt = 1; |
j3 | 2:4e6e761c60f2 | 534 | alarm->day = bcd_2_uchar(data[3]&0x0F); |
j3 | 2:4e6e761c60f2 | 535 | } |
j3 | 2:4e6e761c60f2 | 536 | else |
j3 | 2:4e6e761c60f2 | 537 | { |
j3 | 2:4e6e761c60f2 | 538 | alarm->date = bcd_2_uchar(data[3]&0x3F); |
j3 | 2:4e6e761c60f2 | 539 | } |
j3 | 4:0beb5e9ac927 | 540 | alarm->am4 = (data[3]&ALRM_MASK); |
j3 | 2:4e6e761c60f2 | 541 | } |
j3 | 2:4e6e761c60f2 | 542 | } |
j3 | 2:4e6e761c60f2 | 543 | else |
j3 | 2:4e6e761c60f2 | 544 | { |
j3 | 2:4e6e761c60f2 | 545 | data[0] = ALRM2_MINUTES; |
j3 | 2:4e6e761c60f2 | 546 | rtn_val = write(w_adrs, (const char*) data, 1); |
j3 | 2:4e6e761c60f2 | 547 | |
j3 | 2:4e6e761c60f2 | 548 | if(!rtn_val) |
j3 | 2:4e6e761c60f2 | 549 | { |
j3 | 2:4e6e761c60f2 | 550 | rtn_val = read(r_adrs,(char *) data, 4); |
j3 | 2:4e6e761c60f2 | 551 | |
j3 | 2:4e6e761c60f2 | 552 | alarm->minutes = bcd_2_uchar(data[0]&0x7F); |
j3 | 4:0beb5e9ac927 | 553 | alarm->am2 = (data[0]&ALRM_MASK); |
j3 | 2:4e6e761c60f2 | 554 | alarm->hours = bcd_2_uchar(data[1]&0x1F); |
j3 | 4:0beb5e9ac927 | 555 | alarm->am3 = (data[1]&ALRM_MASK); |
j3 | 4:0beb5e9ac927 | 556 | alarm->am_pm = (data[1]&AM_PM); |
j3 | 4:0beb5e9ac927 | 557 | alarm->mode = (data[1]&MODE); |
j3 | 2:4e6e761c60f2 | 558 | |
j3 | 4:0beb5e9ac927 | 559 | if(data[2] & DY_DT) |
j3 | 2:4e6e761c60f2 | 560 | { |
j3 | 4:0beb5e9ac927 | 561 | alarm->dy_dt = 1; |
j3 | 2:4e6e761c60f2 | 562 | alarm->day = bcd_2_uchar(data[2]&0x0F); |
j3 | 2:4e6e761c60f2 | 563 | } |
j3 | 2:4e6e761c60f2 | 564 | else |
j3 | 2:4e6e761c60f2 | 565 | { |
j3 | 2:4e6e761c60f2 | 566 | alarm->date = bcd_2_uchar(data[2]&0x3F); |
j3 | 2:4e6e761c60f2 | 567 | } |
j3 | 4:0beb5e9ac927 | 568 | alarm->am4 = (data[2]&ALRM_MASK); |
j3 | 2:4e6e761c60f2 | 569 | } |
j3 | 2:4e6e761c60f2 | 570 | } |
j3 | 2:4e6e761c60f2 | 571 | |
j3 | 2:4e6e761c60f2 | 572 | return(rtn_val); |
j3 | 0:b00c4699ae6f | 573 | } |
j3 | 0:b00c4699ae6f | 574 | |
j3 | 0:b00c4699ae6f | 575 | |
j3 | 0:b00c4699ae6f | 576 | /**********************************************************//** |
j3 | 0:b00c4699ae6f | 577 | * Get control and status registers of DS3231 |
j3 | 0:b00c4699ae6f | 578 | * |
j3 | 0:b00c4699ae6f | 579 | * On Entry: |
j3 | 2:4e6e761c60f2 | 580 | * @param[in] data - pointer to struct for storing control |
j3 | 4:0beb5e9ac927 | 581 | * and status register data |
j3 | 0:b00c4699ae6f | 582 | * |
j3 | 0:b00c4699ae6f | 583 | * On Exit: |
j3 | 0:b00c4699ae6f | 584 | * @param[out] data - contains control and status registers |
j3 | 0:b00c4699ae6f | 585 | * data |
j3 | 0:b00c4699ae6f | 586 | * @return return value = 0 on success, non-0 on failure |
j3 | 0:b00c4699ae6f | 587 | * |
j3 | 0:b00c4699ae6f | 588 | * Example: |
j3 | 0:b00c4699ae6f | 589 | * @code |
j3 | 0:b00c4699ae6f | 590 | * |
j3 | 0:b00c4699ae6f | 591 | * //instantiate rtc object |
j3 | 1:c814af60fdbf | 592 | * Ds3231 rtc(D14, D15); |
j3 | 0:b00c4699ae6f | 593 | * |
j3 | 0:b00c4699ae6f | 594 | * //do not use 0xAA, see datasheet for appropriate data |
j3 | 0:b00c4699ae6f | 595 | * ds3231_cntl_stat_t data = {0xAA, 0xAA}; |
j3 | 0:b00c4699ae6f | 596 | * |
j3 | 3:312589d8185c | 597 | * rtn_val = rtc.get_cntl_stat_reg(&data); |
j3 | 0:b00c4699ae6f | 598 | * |
j3 | 0:b00c4699ae6f | 599 | * @endcode |
j3 | 0:b00c4699ae6f | 600 | **************************************************************/ |
j3 | 2:4e6e761c60f2 | 601 | uint16_t Ds3231::get_cntl_stat_reg(ds3231_cntl_stat_t* data) |
j3 | 0:b00c4699ae6f | 602 | { |
j3 | 2:4e6e761c60f2 | 603 | uint16_t rtn_val = 1; |
j3 | 2:4e6e761c60f2 | 604 | uint8_t local_data[2]; |
j3 | 2:4e6e761c60f2 | 605 | |
j3 | 2:4e6e761c60f2 | 606 | local_data[0] = CONTROL; |
j3 | 2:4e6e761c60f2 | 607 | rtn_val = write(w_adrs, (const char*) local_data, 1); |
j3 | 2:4e6e761c60f2 | 608 | |
j3 | 2:4e6e761c60f2 | 609 | if(!rtn_val) |
j3 | 2:4e6e761c60f2 | 610 | { |
j3 | 2:4e6e761c60f2 | 611 | rtn_val = read(r_adrs,(char *) local_data, 2); |
j3 | 2:4e6e761c60f2 | 612 | |
j3 | 2:4e6e761c60f2 | 613 | data->control = local_data[0]; |
j3 | 2:4e6e761c60f2 | 614 | data->status = local_data[1]; |
j3 | 2:4e6e761c60f2 | 615 | } |
j3 | 2:4e6e761c60f2 | 616 | |
j3 | 2:4e6e761c60f2 | 617 | return(rtn_val); |
j3 | 0:b00c4699ae6f | 618 | } |
j3 | 0:b00c4699ae6f | 619 | |
j3 | 0:b00c4699ae6f | 620 | |
j3 | 0:b00c4699ae6f | 621 | /**********************************************************//** |
j3 | 0:b00c4699ae6f | 622 | * Get temperature data of DS3231 |
j3 | 0:b00c4699ae6f | 623 | * |
j3 | 0:b00c4699ae6f | 624 | * On Entry: |
j3 | 0:b00c4699ae6f | 625 | * |
j3 | 0:b00c4699ae6f | 626 | * On Exit: |
j3 | 2:4e6e761c60f2 | 627 | * @return return value = raw temperature data |
j3 | 0:b00c4699ae6f | 628 | * |
j3 | 0:b00c4699ae6f | 629 | * Example: |
j3 | 0:b00c4699ae6f | 630 | * @code |
j3 | 0:b00c4699ae6f | 631 | * |
j3 | 0:b00c4699ae6f | 632 | * //instantiate rtc object |
j3 | 1:c814af60fdbf | 633 | * Ds3231 rtc(D14, D15); |
j3 | 0:b00c4699ae6f | 634 | * |
j3 | 0:b00c4699ae6f | 635 | * uint16_t temp; |
j3 | 0:b00c4699ae6f | 636 | * |
j3 | 2:4e6e761c60f2 | 637 | * temp = rtc.get_temperature(); |
j3 | 0:b00c4699ae6f | 638 | * |
j3 | 0:b00c4699ae6f | 639 | * @endcode |
j3 | 0:b00c4699ae6f | 640 | **************************************************************/ |
j3 | 2:4e6e761c60f2 | 641 | uint16_t Ds3231::get_temperature(void) |
j3 | 0:b00c4699ae6f | 642 | { |
j3 | 2:4e6e761c60f2 | 643 | uint16_t rtn_val = 1; |
j3 | 2:4e6e761c60f2 | 644 | uint8_t data[2]; |
j3 | 2:4e6e761c60f2 | 645 | |
j3 | 2:4e6e761c60f2 | 646 | data[0] = MSB_TEMP; |
j3 | 2:4e6e761c60f2 | 647 | rtn_val = write(w_adrs, (const char*) data, 1); |
j3 | 2:4e6e761c60f2 | 648 | |
j3 | 2:4e6e761c60f2 | 649 | if(!rtn_val) |
j3 | 2:4e6e761c60f2 | 650 | { |
j3 | 2:4e6e761c60f2 | 651 | read(r_adrs,(char *) data, 2); |
j3 | 2:4e6e761c60f2 | 652 | |
j3 | 2:4e6e761c60f2 | 653 | rtn_val = data[0] << 8; |
j3 | 2:4e6e761c60f2 | 654 | rtn_val |= data[1]; |
j3 | 2:4e6e761c60f2 | 655 | } |
j3 | 2:4e6e761c60f2 | 656 | |
j3 | 2:4e6e761c60f2 | 657 | return(rtn_val); |
j3 | 0:b00c4699ae6f | 658 | } |
j3 | 0:b00c4699ae6f | 659 | |
j3 | 2:4e6e761c60f2 | 660 | |
j3 | 2:4e6e761c60f2 | 661 | /**********************************************************//** |
j3 | 5:61dfe2690360 | 662 | * Get epoch time based on current RTC time and date. |
j3 | 5:61dfe2690360 | 663 | * DS3231 must be configured and running before this fx is |
j3 | 5:61dfe2690360 | 664 | * called |
j3 | 5:61dfe2690360 | 665 | * |
j3 | 5:61dfe2690360 | 666 | * On Entry: |
j3 | 5:61dfe2690360 | 667 | * |
j3 | 5:61dfe2690360 | 668 | * On Exit: |
j3 | 5:61dfe2690360 | 669 | * @return return value = epoch time |
j3 | 5:61dfe2690360 | 670 | * |
j3 | 5:61dfe2690360 | 671 | * Example: |
j3 | 5:61dfe2690360 | 672 | * @code |
j3 | 5:61dfe2690360 | 673 | * |
j3 | 5:61dfe2690360 | 674 | * //instantiate rtc object |
j3 | 5:61dfe2690360 | 675 | * Ds3231 rtc(D14, D15); |
j3 | 5:61dfe2690360 | 676 | * |
j3 | 5:61dfe2690360 | 677 | * time_t epoch_time; |
j3 | 5:61dfe2690360 | 678 | * |
j3 | 5:61dfe2690360 | 679 | * epoch_time = rtc.get_epoch(); |
j3 | 5:61dfe2690360 | 680 | * |
j3 | 5:61dfe2690360 | 681 | * @endcode |
j3 | 5:61dfe2690360 | 682 | **************************************************************/ |
j3 | 5:61dfe2690360 | 683 | time_t Ds3231::get_epoch(void) |
j3 | 5:61dfe2690360 | 684 | { |
j3 | 5:61dfe2690360 | 685 | //system vars |
j3 | 5:61dfe2690360 | 686 | struct tm sys_time; |
j3 | 5:61dfe2690360 | 687 | |
j3 | 5:61dfe2690360 | 688 | //RTC vars |
j3 | 6:e8850ad15893 | 689 | ds3231_time_t rtc_time = {0,0,0,0,0}; |
j3 | 6:e8850ad15893 | 690 | ds3231_calendar_t rtc_calendar = {0,0,0,0}; |
j3 | 5:61dfe2690360 | 691 | |
j3 | 5:61dfe2690360 | 692 | get_calendar(&rtc_calendar); |
j3 | 5:61dfe2690360 | 693 | get_time(&rtc_time); |
j3 | 5:61dfe2690360 | 694 | |
j3 | 5:61dfe2690360 | 695 | sys_time.tm_wday = rtc_calendar.day - 1; |
j3 | 5:61dfe2690360 | 696 | sys_time.tm_mday = rtc_calendar.date; |
j3 | 5:61dfe2690360 | 697 | sys_time.tm_mon = rtc_calendar.month - 1; |
j3 | 5:61dfe2690360 | 698 | sys_time.tm_year = rtc_calendar.year + 100; |
j3 | 5:61dfe2690360 | 699 | |
j3 | 5:61dfe2690360 | 700 | //check for 12hr or 24hr mode |
j3 | 5:61dfe2690360 | 701 | if(rtc_time.mode) |
j3 | 5:61dfe2690360 | 702 | { |
j3 | 5:61dfe2690360 | 703 | //check am/pm |
j3 | 5:61dfe2690360 | 704 | if(rtc_time.am_pm && (rtc_time.hours != 12)) |
j3 | 5:61dfe2690360 | 705 | { |
j3 | 5:61dfe2690360 | 706 | sys_time.tm_hour = rtc_time.hours + 12; |
j3 | 5:61dfe2690360 | 707 | } |
j3 | 5:61dfe2690360 | 708 | else |
j3 | 5:61dfe2690360 | 709 | { |
j3 | 5:61dfe2690360 | 710 | sys_time.tm_hour = rtc_time.hours; |
j3 | 5:61dfe2690360 | 711 | } |
j3 | 5:61dfe2690360 | 712 | |
j3 | 5:61dfe2690360 | 713 | } |
j3 | 5:61dfe2690360 | 714 | else |
j3 | 5:61dfe2690360 | 715 | { |
j3 | 5:61dfe2690360 | 716 | //24hr mode |
j3 | 5:61dfe2690360 | 717 | sys_time.tm_hour = rtc_time.hours; |
j3 | 5:61dfe2690360 | 718 | } |
j3 | 5:61dfe2690360 | 719 | |
j3 | 5:61dfe2690360 | 720 | sys_time.tm_min = rtc_time.minutes; |
j3 | 5:61dfe2690360 | 721 | sys_time.tm_sec = rtc_time.seconds; |
j3 | 5:61dfe2690360 | 722 | |
j3 | 5:61dfe2690360 | 723 | //make epoch time |
j3 | 5:61dfe2690360 | 724 | return(mktime(&sys_time)); |
j3 | 5:61dfe2690360 | 725 | } |
j3 | 5:61dfe2690360 | 726 | |
j3 | 5:61dfe2690360 | 727 | |
j3 | 5:61dfe2690360 | 728 | /**********************************************************//** |
j3 | 2:4e6e761c60f2 | 729 | * Private mmber fx, converts unsigned char to BCD |
j3 | 2:4e6e761c60f2 | 730 | * |
j3 | 2:4e6e761c60f2 | 731 | * On Entry: |
j3 | 2:4e6e761c60f2 | 732 | * @param[in] data - 0-255 |
j3 | 2:4e6e761c60f2 | 733 | * |
j3 | 2:4e6e761c60f2 | 734 | * On Exit: |
j3 | 2:4e6e761c60f2 | 735 | * @return bcd_result = BCD representation of data |
j3 | 2:4e6e761c60f2 | 736 | * |
j3 | 2:4e6e761c60f2 | 737 | **************************************************************/ |
j3 | 2:4e6e761c60f2 | 738 | uint16_t Ds3231::uchar_2_bcd(uint8_t data) |
j3 | 2:4e6e761c60f2 | 739 | { |
j3 | 2:4e6e761c60f2 | 740 | uint16_t bcd_result = 0; |
j3 | 2:4e6e761c60f2 | 741 | |
j3 | 2:4e6e761c60f2 | 742 | //Get hundreds |
j3 | 2:4e6e761c60f2 | 743 | bcd_result |= ((data/100) << 8); |
j3 | 2:4e6e761c60f2 | 744 | data = (data - (data/100)*100); |
j3 | 2:4e6e761c60f2 | 745 | |
j3 | 2:4e6e761c60f2 | 746 | //Get tens |
j3 | 2:4e6e761c60f2 | 747 | bcd_result |= ((data/10) << 4); |
j3 | 2:4e6e761c60f2 | 748 | data = (data - (data/10)*10); |
j3 | 2:4e6e761c60f2 | 749 | |
j3 | 2:4e6e761c60f2 | 750 | //Get ones |
j3 | 2:4e6e761c60f2 | 751 | bcd_result |= data; |
j3 | 2:4e6e761c60f2 | 752 | |
j3 | 2:4e6e761c60f2 | 753 | return(bcd_result); |
j3 | 2:4e6e761c60f2 | 754 | } |
j3 | 2:4e6e761c60f2 | 755 | |
j3 | 2:4e6e761c60f2 | 756 | |
j3 | 2:4e6e761c60f2 | 757 | /**********************************************************//** |
j3 | 2:4e6e761c60f2 | 758 | * Private mmber fx, converts BCD to a uint8_t |
j3 | 2:4e6e761c60f2 | 759 | * |
j3 | 2:4e6e761c60f2 | 760 | * On Entry: |
j3 | 2:4e6e761c60f2 | 761 | * @param[in] bcd - 0-99 |
j3 | 2:4e6e761c60f2 | 762 | * |
j3 | 2:4e6e761c60f2 | 763 | * On Exit: |
j3 | 2:4e6e761c60f2 | 764 | * @return rtn_val = integer rep. of BCD |
j3 | 2:4e6e761c60f2 | 765 | * |
j3 | 2:4e6e761c60f2 | 766 | **************************************************************/ |
j3 | 2:4e6e761c60f2 | 767 | uint8_t Ds3231::bcd_2_uchar(uint8_t bcd) |
j3 | 2:4e6e761c60f2 | 768 | { |
j3 | 2:4e6e761c60f2 | 769 | uint8_t rtn_val = 0; |
j3 | 2:4e6e761c60f2 | 770 | |
j3 | 2:4e6e761c60f2 | 771 | rtn_val += ((bcd&0xf0)>>4)*10; |
j3 | 2:4e6e761c60f2 | 772 | rtn_val += (bcd&0x000f); |
j3 | 2:4e6e761c60f2 | 773 | |
j3 | 2:4e6e761c60f2 | 774 | return rtn_val; |
j3 | 2:4e6e761c60f2 | 775 | } |
j3 | 2:4e6e761c60f2 | 776 | |
j3 | 2:4e6e761c60f2 | 777 |