ds3231
Fork of ds3231 by
ds3231.h@16:e1c0f63595d9, 2016-08-21 (annotated)
- Committer:
- j3
- Date:
- Sun Aug 21 01:39:50 2016 +0000
- Revision:
- 16:e1c0f63595d9
- Parent:
- 15:16ca4c747252
fixed reading of alarm 2 hour parameter
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
j3 | 0:b00c4699ae6f | 1 | /******************************************************************//** |
j3 | 12:b9f13fd8c1b6 | 2 | * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved. |
j3 | 0:b00c4699ae6f | 3 | * |
j3 | 0:b00c4699ae6f | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
j3 | 0:b00c4699ae6f | 5 | * copy of this software and associated documentation files (the "Software"), |
j3 | 0:b00c4699ae6f | 6 | * to deal in the Software without restriction, including without limitation |
j3 | 0:b00c4699ae6f | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
j3 | 0:b00c4699ae6f | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
j3 | 0:b00c4699ae6f | 9 | * Software is furnished to do so, subject to the following conditions: |
j3 | 0:b00c4699ae6f | 10 | * |
j3 | 0:b00c4699ae6f | 11 | * The above copyright notice and this permission notice shall be included |
j3 | 0:b00c4699ae6f | 12 | * in all copies or substantial portions of the Software. |
j3 | 0:b00c4699ae6f | 13 | * |
j3 | 0:b00c4699ae6f | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
j3 | 0:b00c4699ae6f | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
j3 | 0:b00c4699ae6f | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
j3 | 0:b00c4699ae6f | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
j3 | 0:b00c4699ae6f | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
j3 | 0:b00c4699ae6f | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
j3 | 0:b00c4699ae6f | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
j3 | 0:b00c4699ae6f | 21 | * |
j3 | 0:b00c4699ae6f | 22 | * Except as contained in this notice, the name of Maxim Integrated |
j3 | 0:b00c4699ae6f | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
j3 | 0:b00c4699ae6f | 24 | * Products, Inc. Branding Policy. |
j3 | 0:b00c4699ae6f | 25 | * |
j3 | 0:b00c4699ae6f | 26 | * The mere transfer of this software does not imply any licenses |
j3 | 0:b00c4699ae6f | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
j3 | 0:b00c4699ae6f | 28 | * trademarks, maskwork rights, or any other form of intellectual |
j3 | 0:b00c4699ae6f | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
j3 | 0:b00c4699ae6f | 30 | * ownership rights. |
j3 | 0:b00c4699ae6f | 31 | **********************************************************************/ |
j3 | 0:b00c4699ae6f | 32 | |
j3 | 0:b00c4699ae6f | 33 | |
j3 | 0:b00c4699ae6f | 34 | #ifndef DS3231_H |
j3 | 0:b00c4699ae6f | 35 | #define DS3231_H |
j3 | 0:b00c4699ae6f | 36 | |
j3 | 0:b00c4699ae6f | 37 | |
j3 | 0:b00c4699ae6f | 38 | #include "mbed.h" |
j3 | 0:b00c4699ae6f | 39 | |
j3 | 0:b00c4699ae6f | 40 | |
j3 | 0:b00c4699ae6f | 41 | |
j3 | 9:e57201ee8921 | 42 | |
j3 | 14:11630748e2f2 | 43 | |
j3 | 14:11630748e2f2 | 44 | /** |
j3 | 14:11630748e2f2 | 45 | * ds3231_time_t - Struct for containing time data. |
j3 | 14:11630748e2f2 | 46 | * |
j3 | 14:11630748e2f2 | 47 | * Members: |
j3 | 14:11630748e2f2 | 48 | * |
j3 | 14:11630748e2f2 | 49 | * - uint32_t seconds - Use decimal value. Member fx's convert to BCD |
j3 | 14:11630748e2f2 | 50 | * |
j3 | 14:11630748e2f2 | 51 | * - uint32_t minutes - Use decimal value. Member fx's convert to BCD |
j3 | 14:11630748e2f2 | 52 | * |
j3 | 14:11630748e2f2 | 53 | * - uint32_t hours - Use decimal value. Member fx's convert to BCD |
j3 | 14:11630748e2f2 | 54 | * |
j3 | 14:11630748e2f2 | 55 | * - bool am_pm - TRUE for PM, same logic as datasheet |
j3 | 14:11630748e2f2 | 56 | * |
j3 | 14:11630748e2f2 | 57 | * - bool mode - TRUE for 12 hour, same logic as datasheet |
j3 | 14:11630748e2f2 | 58 | */ |
j3 | 14:11630748e2f2 | 59 | typedef struct |
j3 | 14:11630748e2f2 | 60 | { |
j3 | 14:11630748e2f2 | 61 | uint32_t seconds; |
j3 | 14:11630748e2f2 | 62 | uint32_t minutes; |
j3 | 14:11630748e2f2 | 63 | uint32_t hours; |
j3 | 14:11630748e2f2 | 64 | bool am_pm; |
j3 | 14:11630748e2f2 | 65 | bool mode; |
j3 | 14:11630748e2f2 | 66 | }ds3231_time_t; |
j3 | 14:11630748e2f2 | 67 | |
j3 | 14:11630748e2f2 | 68 | |
j3 | 14:11630748e2f2 | 69 | /** |
j3 | 14:11630748e2f2 | 70 | * ds3231_calendar_t - Struct for containing calendar data. |
j3 | 14:11630748e2f2 | 71 | * |
j3 | 14:11630748e2f2 | 72 | * Members: |
j3 | 14:11630748e2f2 | 73 | * |
j3 | 14:11630748e2f2 | 74 | * - uint32_t day - Use decimal value. Member fx's convert to BCD |
j3 | 14:11630748e2f2 | 75 | * |
j3 | 14:11630748e2f2 | 76 | * - uint32_t date - Use decimal value. Member fx's convert to BCD |
j3 | 14:11630748e2f2 | 77 | * |
j3 | 14:11630748e2f2 | 78 | * - uint32_t month - Use decimal value. Member fx's convert to BCD |
j3 | 14:11630748e2f2 | 79 | * |
j3 | 14:11630748e2f2 | 80 | * - uint32_t year - Use decimal value. Member fx's convert to BCD |
j3 | 14:11630748e2f2 | 81 | */ |
j3 | 14:11630748e2f2 | 82 | typedef struct |
j3 | 14:11630748e2f2 | 83 | { |
j3 | 14:11630748e2f2 | 84 | uint32_t day; |
j3 | 14:11630748e2f2 | 85 | uint32_t date; |
j3 | 14:11630748e2f2 | 86 | uint32_t month; |
j3 | 14:11630748e2f2 | 87 | uint32_t year; |
j3 | 14:11630748e2f2 | 88 | }ds3231_calendar_t; |
j3 | 14:11630748e2f2 | 89 | |
j3 | 14:11630748e2f2 | 90 | |
j3 | 14:11630748e2f2 | 91 | /** |
j3 | 14:11630748e2f2 | 92 | * ds3231_alrm_t - Struct for containing alarm data. |
j3 | 14:11630748e2f2 | 93 | * |
j3 | 14:11630748e2f2 | 94 | * Members: |
j3 | 14:11630748e2f2 | 95 | * |
j3 | 14:11630748e2f2 | 96 | * - uint32_t seconds - Use decimal value. Member fx's convert to BCD |
j3 | 14:11630748e2f2 | 97 | * |
j3 | 14:11630748e2f2 | 98 | * - uint32_t minutes - Use decimal value. Member fx's convert to BCD |
j3 | 14:11630748e2f2 | 99 | * |
j3 | 14:11630748e2f2 | 100 | * - uint32_t hours - Use decimal value. Member fx's convert to BCD |
j3 | 14:11630748e2f2 | 101 | * |
j3 | 14:11630748e2f2 | 102 | * - uint32_t day - Use decimal value. Member fx's convert to BCD |
j3 | 14:11630748e2f2 | 103 | * |
j3 | 14:11630748e2f2 | 104 | * - uint32_t date - Use decimal value. Member fx's convert to BCD |
j3 | 14:11630748e2f2 | 105 | * |
j3 | 14:11630748e2f2 | 106 | * - bool am1 - Flag for setting alarm rate |
j3 | 14:11630748e2f2 | 107 | * |
j3 | 14:11630748e2f2 | 108 | * - bool am2 - Flag for setting alarm rate |
j3 | 14:11630748e2f2 | 109 | * |
j3 | 14:11630748e2f2 | 110 | * - bool am3 - Flag for setting alarm rate |
j3 | 14:11630748e2f2 | 111 | * |
j3 | 14:11630748e2f2 | 112 | * - bool am4 - Flag for setting alarm rate |
j3 | 14:11630748e2f2 | 113 | * |
j3 | 14:11630748e2f2 | 114 | * - bool am_pm - TRUE for PM, same logic as datasheet |
j3 | 14:11630748e2f2 | 115 | * |
j3 | 14:11630748e2f2 | 116 | * - bool mode - TRUE for 12 hour, same logic as datasheet |
j3 | 14:11630748e2f2 | 117 | * |
j3 | 14:11630748e2f2 | 118 | * - bool dy_dt - TRUE for Day, same logic as datasheet |
j3 | 14:11630748e2f2 | 119 | */ |
j3 | 14:11630748e2f2 | 120 | typedef struct |
j3 | 14:11630748e2f2 | 121 | { |
j3 | 14:11630748e2f2 | 122 | //Seconds and am1 not used for alarm2 |
j3 | 14:11630748e2f2 | 123 | uint32_t seconds; |
j3 | 14:11630748e2f2 | 124 | uint32_t minutes; |
j3 | 14:11630748e2f2 | 125 | uint32_t hours; |
j3 | 14:11630748e2f2 | 126 | uint32_t day; |
j3 | 14:11630748e2f2 | 127 | uint32_t date; |
j3 | 14:11630748e2f2 | 128 | bool am1; |
j3 | 14:11630748e2f2 | 129 | bool am2; |
j3 | 14:11630748e2f2 | 130 | bool am3; |
j3 | 14:11630748e2f2 | 131 | bool am4; |
j3 | 14:11630748e2f2 | 132 | bool am_pm; |
j3 | 14:11630748e2f2 | 133 | bool mode; |
j3 | 14:11630748e2f2 | 134 | bool dy_dt; |
j3 | 14:11630748e2f2 | 135 | }ds3231_alrm_t; |
j3 | 14:11630748e2f2 | 136 | |
j3 | 14:11630748e2f2 | 137 | |
j3 | 14:11630748e2f2 | 138 | /** |
j3 | 14:11630748e2f2 | 139 | * ds3231_cntl_stat_t - Struct for containing control and status |
j3 | 14:11630748e2f2 | 140 | * register data. |
j3 | 14:11630748e2f2 | 141 | * |
j3 | 14:11630748e2f2 | 142 | * Members: |
j3 | 14:11630748e2f2 | 143 | * |
j3 | 14:11630748e2f2 | 144 | * - uint8_t control - Binary data for read/write of control register |
j3 | 14:11630748e2f2 | 145 | * |
j3 | 14:11630748e2f2 | 146 | * - uint8_t status - Binary data for read/write of status register |
j3 | 14:11630748e2f2 | 147 | */ |
j3 | 14:11630748e2f2 | 148 | typedef struct |
j3 | 14:11630748e2f2 | 149 | { |
j3 | 14:11630748e2f2 | 150 | uint8_t control; |
j3 | 14:11630748e2f2 | 151 | uint8_t status; |
j3 | 14:11630748e2f2 | 152 | }ds3231_cntl_stat_t; |
j3 | 9:e57201ee8921 | 153 | |
j3 | 16:e1c0f63595d9 | 154 | |
j3 | 16:e1c0f63595d9 | 155 | /** |
j3 | 16:e1c0f63595d9 | 156 | * @brief DS3231 Class |
j3 | 16:e1c0f63595d9 | 157 | */ |
j3 | 15:16ca4c747252 | 158 | class Ds3231 |
j3 | 0:b00c4699ae6f | 159 | { |
j3 | 0:b00c4699ae6f | 160 | public: |
j3 | 13:aa3eb69b25bb | 161 | |
j3 | 13:aa3eb69b25bb | 162 | /** |
j3 | 13:aa3eb69b25bb | 163 | * ds3231_regs_t - enumerated DS3231 registers |
j3 | 13:aa3eb69b25bb | 164 | */ |
j3 | 13:aa3eb69b25bb | 165 | typedef enum |
j3 | 13:aa3eb69b25bb | 166 | { |
j3 | 13:aa3eb69b25bb | 167 | SECONDS, |
j3 | 13:aa3eb69b25bb | 168 | MINUTES, |
j3 | 13:aa3eb69b25bb | 169 | HOURS, |
j3 | 13:aa3eb69b25bb | 170 | DAY, |
j3 | 13:aa3eb69b25bb | 171 | DATE, |
j3 | 13:aa3eb69b25bb | 172 | MONTH, |
j3 | 13:aa3eb69b25bb | 173 | YEAR, |
j3 | 13:aa3eb69b25bb | 174 | ALRM1_SECONDS, |
j3 | 13:aa3eb69b25bb | 175 | ALRM1_MINUTES, |
j3 | 13:aa3eb69b25bb | 176 | ALRM1_HOURS, |
j3 | 13:aa3eb69b25bb | 177 | ALRM1_DAY_DATE, |
j3 | 13:aa3eb69b25bb | 178 | ALRM2_MINUTES, |
j3 | 13:aa3eb69b25bb | 179 | ALRM2_HOURS, |
j3 | 13:aa3eb69b25bb | 180 | ALRM2_DAY_DATE, |
j3 | 13:aa3eb69b25bb | 181 | CONTROL, |
j3 | 13:aa3eb69b25bb | 182 | STATUS, |
j3 | 13:aa3eb69b25bb | 183 | AGING_OFFSET, //don't touch this register |
j3 | 13:aa3eb69b25bb | 184 | MSB_TEMP, |
j3 | 13:aa3eb69b25bb | 185 | LSB_TEMP |
j3 | 13:aa3eb69b25bb | 186 | }ds3231_regs_t; |
j3 | 14:11630748e2f2 | 187 | |
j3 | 9:e57201ee8921 | 188 | |
j3 | 0:b00c4699ae6f | 189 | /**********************************************************//** |
j3 | 0:b00c4699ae6f | 190 | * Constructor for Ds3231 Class |
j3 | 0:b00c4699ae6f | 191 | * |
j3 | 0:b00c4699ae6f | 192 | * On Entry: |
j3 | 1:c814af60fdbf | 193 | * @param[in] sda - sda pin of I2C bus |
j3 | 1:c814af60fdbf | 194 | * @param[in] scl - scl pin of I2C bus |
j3 | 0:b00c4699ae6f | 195 | * |
j3 | 0:b00c4699ae6f | 196 | * On Exit: |
j3 | 0:b00c4699ae6f | 197 | * @return none |
j3 | 0:b00c4699ae6f | 198 | * |
j3 | 0:b00c4699ae6f | 199 | * Example: |
j3 | 0:b00c4699ae6f | 200 | * @code |
j3 | 0:b00c4699ae6f | 201 | * |
j3 | 0:b00c4699ae6f | 202 | * //instantiate rtc object |
j3 | 1:c814af60fdbf | 203 | * Ds3231 rtc(D14, D15); |
j3 | 0:b00c4699ae6f | 204 | * |
j3 | 0:b00c4699ae6f | 205 | * @endcode |
j3 | 0:b00c4699ae6f | 206 | **************************************************************/ |
j3 | 1:c814af60fdbf | 207 | Ds3231(PinName sda, PinName scl); |
j3 | 0:b00c4699ae6f | 208 | |
j3 | 0:b00c4699ae6f | 209 | |
j3 | 0:b00c4699ae6f | 210 | /**********************************************************//** |
j3 | 15:16ca4c747252 | 211 | * Constructor for Ds3231 Class |
j3 | 15:16ca4c747252 | 212 | * |
j3 | 15:16ca4c747252 | 213 | * On Entry: |
j3 | 15:16ca4c747252 | 214 | * @param[in] i2c_bus - reference to existing bus |
j3 | 15:16ca4c747252 | 215 | * |
j3 | 15:16ca4c747252 | 216 | * On Exit: |
j3 | 15:16ca4c747252 | 217 | * |
j3 | 15:16ca4c747252 | 218 | * @return none |
j3 | 15:16ca4c747252 | 219 | **************************************************************/ |
j3 | 15:16ca4c747252 | 220 | Ds3231(I2C & i2c_bus); |
j3 | 15:16ca4c747252 | 221 | |
j3 | 15:16ca4c747252 | 222 | |
j3 | 15:16ca4c747252 | 223 | ~Ds3231(); |
j3 | 15:16ca4c747252 | 224 | |
j3 | 15:16ca4c747252 | 225 | |
j3 | 15:16ca4c747252 | 226 | /**********************************************************//** |
j3 | 0:b00c4699ae6f | 227 | * Sets the time on DS3231 |
j3 | 2:4e6e761c60f2 | 228 | * Struct data is in integrer format, not BCD. Fx will convert |
j3 | 2:4e6e761c60f2 | 229 | * to BCD for you. |
j3 | 0:b00c4699ae6f | 230 | * |
j3 | 0:b00c4699ae6f | 231 | * On Entry: |
j3 | 0:b00c4699ae6f | 232 | * @param[in] time - struct cotaining time data; |
j3 | 0:b00c4699ae6f | 233 | * |
j3 | 0:b00c4699ae6f | 234 | * On Exit: |
j3 | 0:b00c4699ae6f | 235 | * @return return value = 0 on success, non-0 on failure |
j3 | 0:b00c4699ae6f | 236 | * |
j3 | 0:b00c4699ae6f | 237 | * Example: |
j3 | 0:b00c4699ae6f | 238 | * @code |
j3 | 0:b00c4699ae6f | 239 | * |
j3 | 0:b00c4699ae6f | 240 | * //instantiate rtc object |
j3 | 1:c814af60fdbf | 241 | * Ds3231 rtc(D14, D15); |
j3 | 0:b00c4699ae6f | 242 | * |
j3 | 4:0beb5e9ac927 | 243 | * //time = 12:00:00 AM 12hr mode |
j3 | 4:0beb5e9ac927 | 244 | * ds3231_time_t time = {12, 0, 0, 0, 1} |
j3 | 0:b00c4699ae6f | 245 | * uint16_t rtn_val; |
j3 | 0:b00c4699ae6f | 246 | * |
j3 | 0:b00c4699ae6f | 247 | * rtn_val = rtc.set_time(time); |
j3 | 0:b00c4699ae6f | 248 | * |
j3 | 0:b00c4699ae6f | 249 | * @endcode |
j3 | 0:b00c4699ae6f | 250 | **************************************************************/ |
j3 | 0:b00c4699ae6f | 251 | uint16_t set_time(ds3231_time_t time); |
j3 | 0:b00c4699ae6f | 252 | |
j3 | 0:b00c4699ae6f | 253 | |
j3 | 0:b00c4699ae6f | 254 | /**********************************************************//** |
j3 | 0:b00c4699ae6f | 255 | * Sets the calendar on DS3231 |
j3 | 0:b00c4699ae6f | 256 | * |
j3 | 0:b00c4699ae6f | 257 | * On Entry: |
j3 | 3:312589d8185c | 258 | * @param[in] calendar - struct cotaining calendar data |
j3 | 0:b00c4699ae6f | 259 | * |
j3 | 0:b00c4699ae6f | 260 | * On Exit: |
j3 | 0:b00c4699ae6f | 261 | * @return return value = 0 on success, non-0 on failure |
j3 | 0:b00c4699ae6f | 262 | * |
j3 | 0:b00c4699ae6f | 263 | * Example: |
j3 | 0:b00c4699ae6f | 264 | * @code |
j3 | 0:b00c4699ae6f | 265 | * |
j3 | 0:b00c4699ae6f | 266 | * //instantiate rtc object |
j3 | 1:c814af60fdbf | 267 | * Ds3231 rtc(D14, D15); |
j3 | 0:b00c4699ae6f | 268 | * |
j3 | 0:b00c4699ae6f | 269 | * //see datasheet for calendar format |
j3 | 0:b00c4699ae6f | 270 | * ds3231_calendar_t calendar = {1, 1, 1, 0}; |
j3 | 0:b00c4699ae6f | 271 | * uint16_t rtn_val; |
j3 | 0:b00c4699ae6f | 272 | * |
j3 | 0:b00c4699ae6f | 273 | * rtn_val = rtc.set_calendar(calendar); |
j3 | 0:b00c4699ae6f | 274 | * |
j3 | 0:b00c4699ae6f | 275 | * @endcode |
j3 | 0:b00c4699ae6f | 276 | **************************************************************/ |
j3 | 0:b00c4699ae6f | 277 | uint16_t set_calendar(ds3231_calendar_t calendar); |
j3 | 0:b00c4699ae6f | 278 | |
j3 | 0:b00c4699ae6f | 279 | |
j3 | 0:b00c4699ae6f | 280 | /**********************************************************//** |
j3 | 0:b00c4699ae6f | 281 | * Set either Alarm1 or Alarm2 of DS3231 |
j3 | 0:b00c4699ae6f | 282 | * |
j3 | 0:b00c4699ae6f | 283 | * On Entry: |
j3 | 0:b00c4699ae6f | 284 | * @param[in] alarm - struct cotaining alarm data |
j3 | 3:312589d8185c | 285 | * |
j3 | 0:b00c4699ae6f | 286 | * @param[in] one_r_two - TRUE for Alarm1 and FALSE for |
j3 | 0:b00c4699ae6f | 287 | * Alarm2 |
j3 | 0:b00c4699ae6f | 288 | * |
j3 | 0:b00c4699ae6f | 289 | * On Exit: |
j3 | 0:b00c4699ae6f | 290 | * @return return value = 0 on success, non-0 on failure |
j3 | 0:b00c4699ae6f | 291 | * |
j3 | 0:b00c4699ae6f | 292 | * Example: |
j3 | 0:b00c4699ae6f | 293 | * @code |
j3 | 0:b00c4699ae6f | 294 | * |
j3 | 0:b00c4699ae6f | 295 | * //instantiate rtc object |
j3 | 1:c814af60fdbf | 296 | * Ds3231 rtc(D14, D15); |
j3 | 0:b00c4699ae6f | 297 | * |
j3 | 4:0beb5e9ac927 | 298 | * //see ds3231.h for .members and datasheet for alarm format |
j3 | 4:0beb5e9ac927 | 299 | * ds3231_alrm_t alarm; |
j3 | 0:b00c4699ae6f | 300 | * uint16_t rtn_val; |
j3 | 0:b00c4699ae6f | 301 | * |
j3 | 0:b00c4699ae6f | 302 | * rtn_val = rtc.set_alarm(alarm, FALSE); |
j3 | 0:b00c4699ae6f | 303 | * |
j3 | 0:b00c4699ae6f | 304 | * @endcode |
j3 | 0:b00c4699ae6f | 305 | **************************************************************/ |
j3 | 0:b00c4699ae6f | 306 | uint16_t set_alarm(ds3231_alrm_t alarm, bool one_r_two); |
j3 | 0:b00c4699ae6f | 307 | |
j3 | 0:b00c4699ae6f | 308 | |
j3 | 0:b00c4699ae6f | 309 | /**********************************************************//** |
j3 | 0:b00c4699ae6f | 310 | * Set control and status registers of DS3231 |
j3 | 0:b00c4699ae6f | 311 | * |
j3 | 0:b00c4699ae6f | 312 | * On Entry: |
j3 | 0:b00c4699ae6f | 313 | * @param[in] data - Struct containing control and status |
j3 | 0:b00c4699ae6f | 314 | * register data |
j3 | 0:b00c4699ae6f | 315 | * |
j3 | 0:b00c4699ae6f | 316 | * On Exit: |
j3 | 0:b00c4699ae6f | 317 | * @return return value = 0 on success, non-0 on failure |
j3 | 0:b00c4699ae6f | 318 | * |
j3 | 0:b00c4699ae6f | 319 | * Example: |
j3 | 0:b00c4699ae6f | 320 | * @code |
j3 | 0:b00c4699ae6f | 321 | * |
j3 | 0:b00c4699ae6f | 322 | * //instantiate rtc object |
j3 | 1:c814af60fdbf | 323 | * Ds3231 rtc(D14, D15); |
j3 | 0:b00c4699ae6f | 324 | * |
j3 | 0:b00c4699ae6f | 325 | * //do not use 0xAA, see datasheet for appropriate data |
j3 | 0:b00c4699ae6f | 326 | * ds3231_cntl_stat_t data = {0xAA, 0xAA}; |
j3 | 0:b00c4699ae6f | 327 | * |
j3 | 0:b00c4699ae6f | 328 | * rtn_val = rtc.set_cntl_stat_reg(data); |
j3 | 0:b00c4699ae6f | 329 | * |
j3 | 0:b00c4699ae6f | 330 | * @endcode |
j3 | 0:b00c4699ae6f | 331 | **************************************************************/ |
j3 | 0:b00c4699ae6f | 332 | uint16_t set_cntl_stat_reg(ds3231_cntl_stat_t data); |
j3 | 0:b00c4699ae6f | 333 | |
j3 | 0:b00c4699ae6f | 334 | |
j3 | 0:b00c4699ae6f | 335 | /**********************************************************//** |
j3 | 0:b00c4699ae6f | 336 | * Gets the time on DS3231 |
j3 | 0:b00c4699ae6f | 337 | * |
j3 | 0:b00c4699ae6f | 338 | * On Entry: |
j3 | 4:0beb5e9ac927 | 339 | * @param[in] time - pointer to struct for storing time data |
j3 | 0:b00c4699ae6f | 340 | * |
j3 | 0:b00c4699ae6f | 341 | * On Exit: |
j3 | 2:4e6e761c60f2 | 342 | * @param[out] time - contains current integrer rtc time |
j3 | 2:4e6e761c60f2 | 343 | * data |
j3 | 0:b00c4699ae6f | 344 | * @return return value = 0 on success, non-0 on failure |
j3 | 0:b00c4699ae6f | 345 | * |
j3 | 0:b00c4699ae6f | 346 | * Example: |
j3 | 0:b00c4699ae6f | 347 | * @code |
j3 | 0:b00c4699ae6f | 348 | * |
j3 | 0:b00c4699ae6f | 349 | * //instantiate rtc object |
j3 | 1:c814af60fdbf | 350 | * Ds3231 rtc(D14, D15); |
j3 | 0:b00c4699ae6f | 351 | * |
j3 | 4:0beb5e9ac927 | 352 | * //time = 12:00:00 AM 12hr mode |
j3 | 4:0beb5e9ac927 | 353 | * ds3231_time_t time = {12, 0, 0, 0, 1} |
j3 | 0:b00c4699ae6f | 354 | * uint16_t rtn_val; |
j3 | 0:b00c4699ae6f | 355 | * |
j3 | 3:312589d8185c | 356 | * rtn_val = rtc.get_time(&time); |
j3 | 0:b00c4699ae6f | 357 | * |
j3 | 0:b00c4699ae6f | 358 | * @endcode |
j3 | 0:b00c4699ae6f | 359 | **************************************************************/ |
j3 | 2:4e6e761c60f2 | 360 | uint16_t get_time(ds3231_time_t* time); |
j3 | 0:b00c4699ae6f | 361 | |
j3 | 0:b00c4699ae6f | 362 | |
j3 | 0:b00c4699ae6f | 363 | /**********************************************************//** |
j3 | 0:b00c4699ae6f | 364 | * Gets the calendar on DS3231 |
j3 | 0:b00c4699ae6f | 365 | * |
j3 | 0:b00c4699ae6f | 366 | * On Entry: |
j3 | 2:4e6e761c60f2 | 367 | * @param[in] calendar - pointer to struct for storing |
j3 | 4:0beb5e9ac927 | 368 | * calendar data |
j3 | 0:b00c4699ae6f | 369 | * |
j3 | 0:b00c4699ae6f | 370 | * On Exit: |
j3 | 2:4e6e761c60f2 | 371 | * @param[out] calendar - contains current integer rtc |
j3 | 2:4e6e761c60f2 | 372 | * calendar data |
j3 | 0:b00c4699ae6f | 373 | * @return return value = 0 on success, non-0 on failure |
j3 | 0:b00c4699ae6f | 374 | * |
j3 | 0:b00c4699ae6f | 375 | * Example: |
j3 | 0:b00c4699ae6f | 376 | * @code |
j3 | 0:b00c4699ae6f | 377 | * |
j3 | 0:b00c4699ae6f | 378 | * //instantiate rtc object |
j3 | 1:c814af60fdbf | 379 | * Ds3231 rtc(D14, D15); |
j3 | 0:b00c4699ae6f | 380 | * |
j3 | 0:b00c4699ae6f | 381 | * //see datasheet for calendar format |
j3 | 0:b00c4699ae6f | 382 | * ds3231_calendar_t calendar = {1, 1, 1, 0}; |
j3 | 0:b00c4699ae6f | 383 | * uint16_t rtn_val; |
j3 | 0:b00c4699ae6f | 384 | * |
j3 | 3:312589d8185c | 385 | * rtn_val = rtc.get_calendar(&calendar); |
j3 | 0:b00c4699ae6f | 386 | * |
j3 | 0:b00c4699ae6f | 387 | * @endcode |
j3 | 0:b00c4699ae6f | 388 | **************************************************************/ |
j3 | 2:4e6e761c60f2 | 389 | uint16_t get_calendar(ds3231_calendar_t* calendar); |
j3 | 0:b00c4699ae6f | 390 | |
j3 | 0:b00c4699ae6f | 391 | |
j3 | 0:b00c4699ae6f | 392 | /**********************************************************//** |
j3 | 0:b00c4699ae6f | 393 | * Get either Alarm1 or Alarm2 of DS3231 |
j3 | 0:b00c4699ae6f | 394 | * |
j3 | 0:b00c4699ae6f | 395 | * On Entry: |
j3 | 2:4e6e761c60f2 | 396 | * @param[in] alarm - pointer to struct for storing alarm |
j3 | 2:4e6e761c60f2 | 397 | * data; |
j3 | 2:4e6e761c60f2 | 398 | * |
j3 | 0:b00c4699ae6f | 399 | * @param[in] one_r_two - TRUE for Alarm1 and FALSE for |
j3 | 0:b00c4699ae6f | 400 | * Alarm2 |
j3 | 0:b00c4699ae6f | 401 | * |
j3 | 0:b00c4699ae6f | 402 | * On Exit: |
j3 | 2:4e6e761c60f2 | 403 | * @param[out] alarm - contains integer alarm data |
j3 | 0:b00c4699ae6f | 404 | * @return return value = 0 on success, non-0 on failure |
j3 | 0:b00c4699ae6f | 405 | * |
j3 | 0:b00c4699ae6f | 406 | * Example: |
j3 | 0:b00c4699ae6f | 407 | * @code |
j3 | 0:b00c4699ae6f | 408 | * |
j3 | 0:b00c4699ae6f | 409 | * //instantiate rtc object |
j3 | 1:c814af60fdbf | 410 | * Ds3231 rtc(D14, D15); |
j3 | 0:b00c4699ae6f | 411 | * |
j3 | 4:0beb5e9ac927 | 412 | * //see ds3231.h for .members and datasheet for alarm format |
j3 | 4:0beb5e9ac927 | 413 | * ds3231_alrm_t alarm; |
j3 | 0:b00c4699ae6f | 414 | * uint16_t rtn_val; |
j3 | 0:b00c4699ae6f | 415 | * |
j3 | 3:312589d8185c | 416 | * rtn_val = rtc.get_alarm(&alarm, FALSE); |
j3 | 0:b00c4699ae6f | 417 | * |
j3 | 0:b00c4699ae6f | 418 | * @endcode |
j3 | 0:b00c4699ae6f | 419 | **************************************************************/ |
j3 | 2:4e6e761c60f2 | 420 | uint16_t get_alarm(ds3231_alrm_t* alarm, bool one_r_two); |
j3 | 0:b00c4699ae6f | 421 | |
j3 | 0:b00c4699ae6f | 422 | |
j3 | 0:b00c4699ae6f | 423 | /**********************************************************//** |
j3 | 0:b00c4699ae6f | 424 | * Get control and status registers of DS3231 |
j3 | 0:b00c4699ae6f | 425 | * |
j3 | 0:b00c4699ae6f | 426 | * On Entry: |
j3 | 2:4e6e761c60f2 | 427 | * @param[in] data - pointer to struct for storing control |
j3 | 4:0beb5e9ac927 | 428 | * and status register data |
j3 | 0:b00c4699ae6f | 429 | * |
j3 | 0:b00c4699ae6f | 430 | * On Exit: |
j3 | 0:b00c4699ae6f | 431 | * @param[out] data - contains control and status registers |
j3 | 0:b00c4699ae6f | 432 | * data |
j3 | 0:b00c4699ae6f | 433 | * @return return value = 0 on success, non-0 on failure |
j3 | 0:b00c4699ae6f | 434 | * |
j3 | 0:b00c4699ae6f | 435 | * Example: |
j3 | 0:b00c4699ae6f | 436 | * @code |
j3 | 0:b00c4699ae6f | 437 | * |
j3 | 0:b00c4699ae6f | 438 | * //instantiate rtc object |
j3 | 1:c814af60fdbf | 439 | * Ds3231 rtc(D14, D15); |
j3 | 0:b00c4699ae6f | 440 | * |
j3 | 0:b00c4699ae6f | 441 | * //do not use 0xAA, see datasheet for appropriate data |
j3 | 0:b00c4699ae6f | 442 | * ds3231_cntl_stat_t data = {0xAA, 0xAA}; |
j3 | 0:b00c4699ae6f | 443 | * |
j3 | 3:312589d8185c | 444 | * rtn_val = rtc.get_cntl_stat_reg(&data); |
j3 | 0:b00c4699ae6f | 445 | * |
j3 | 0:b00c4699ae6f | 446 | * @endcode |
j3 | 0:b00c4699ae6f | 447 | **************************************************************/ |
j3 | 2:4e6e761c60f2 | 448 | uint16_t get_cntl_stat_reg(ds3231_cntl_stat_t* data); |
j3 | 0:b00c4699ae6f | 449 | |
j3 | 0:b00c4699ae6f | 450 | |
j3 | 0:b00c4699ae6f | 451 | /**********************************************************//** |
j3 | 0:b00c4699ae6f | 452 | * Get temperature data of DS3231 |
j3 | 0:b00c4699ae6f | 453 | * |
j3 | 0:b00c4699ae6f | 454 | * On Entry: |
j3 | 0:b00c4699ae6f | 455 | * |
j3 | 0:b00c4699ae6f | 456 | * On Exit: |
j3 | 2:4e6e761c60f2 | 457 | * @return return value = raw temperature data |
j3 | 0:b00c4699ae6f | 458 | * |
j3 | 0:b00c4699ae6f | 459 | * Example: |
j3 | 0:b00c4699ae6f | 460 | * @code |
j3 | 0:b00c4699ae6f | 461 | * |
j3 | 0:b00c4699ae6f | 462 | * //instantiate rtc object |
j3 | 1:c814af60fdbf | 463 | * Ds3231 rtc(D14, D15); |
j3 | 0:b00c4699ae6f | 464 | * |
j3 | 0:b00c4699ae6f | 465 | * uint16_t temp; |
j3 | 0:b00c4699ae6f | 466 | * |
j3 | 2:4e6e761c60f2 | 467 | * temp = rtc.get_temperature(); |
j3 | 0:b00c4699ae6f | 468 | * |
j3 | 0:b00c4699ae6f | 469 | * @endcode |
j3 | 0:b00c4699ae6f | 470 | **************************************************************/ |
j3 | 2:4e6e761c60f2 | 471 | uint16_t get_temperature(void); |
j3 | 0:b00c4699ae6f | 472 | |
j3 | 5:61dfe2690360 | 473 | |
j3 | 5:61dfe2690360 | 474 | /**********************************************************//** |
j3 | 5:61dfe2690360 | 475 | * Get epoch time based on current RTC time and date. |
j3 | 5:61dfe2690360 | 476 | * DS3231 must be configured and running before this fx is |
j3 | 5:61dfe2690360 | 477 | * called |
j3 | 5:61dfe2690360 | 478 | * |
j3 | 5:61dfe2690360 | 479 | * On Entry: |
j3 | 5:61dfe2690360 | 480 | * |
j3 | 5:61dfe2690360 | 481 | * On Exit: |
j3 | 5:61dfe2690360 | 482 | * @return return value = epoch time |
j3 | 5:61dfe2690360 | 483 | * |
j3 | 5:61dfe2690360 | 484 | * Example: |
j3 | 5:61dfe2690360 | 485 | * @code |
j3 | 5:61dfe2690360 | 486 | * |
j3 | 5:61dfe2690360 | 487 | * //instantiate rtc object |
j3 | 5:61dfe2690360 | 488 | * Ds3231 rtc(D14, D15); |
j3 | 5:61dfe2690360 | 489 | * |
j3 | 5:61dfe2690360 | 490 | * time_t epoch_time; |
j3 | 5:61dfe2690360 | 491 | * |
j3 | 5:61dfe2690360 | 492 | * epoch_time = rtc.get_epoch(); |
j3 | 5:61dfe2690360 | 493 | * |
j3 | 5:61dfe2690360 | 494 | * @endcode |
j3 | 5:61dfe2690360 | 495 | **************************************************************/ |
j3 | 5:61dfe2690360 | 496 | time_t get_epoch(void); |
j3 | 5:61dfe2690360 | 497 | |
j3 | 15:16ca4c747252 | 498 | private: |
j3 | 15:16ca4c747252 | 499 | |
j3 | 15:16ca4c747252 | 500 | I2C * _p_i2c; |
j3 | 15:16ca4c747252 | 501 | |
j3 | 15:16ca4c747252 | 502 | bool _i2c_owner; |
j3 | 15:16ca4c747252 | 503 | uint8_t _w_adrs, _r_adrs; |
j3 | 15:16ca4c747252 | 504 | |
j3 | 15:16ca4c747252 | 505 | /**********************************************************//** |
j3 | 15:16ca4c747252 | 506 | * Private mmber fx, converts unsigned char to BCD |
j3 | 15:16ca4c747252 | 507 | * |
j3 | 15:16ca4c747252 | 508 | * On Entry: |
j3 | 15:16ca4c747252 | 509 | * @param[in] data - 0-255 |
j3 | 15:16ca4c747252 | 510 | * |
j3 | 15:16ca4c747252 | 511 | * On Exit: |
j3 | 15:16ca4c747252 | 512 | * @return bcd_result = BCD representation of data |
j3 | 15:16ca4c747252 | 513 | * |
j3 | 15:16ca4c747252 | 514 | **************************************************************/ |
j3 | 15:16ca4c747252 | 515 | uint16_t uchar_2_bcd(uint8_t data); |
j3 | 15:16ca4c747252 | 516 | |
j3 | 15:16ca4c747252 | 517 | |
j3 | 15:16ca4c747252 | 518 | /**********************************************************//** |
j3 | 15:16ca4c747252 | 519 | * Private mmber fx, converts BCD to a uint8_t |
j3 | 15:16ca4c747252 | 520 | * |
j3 | 15:16ca4c747252 | 521 | * On Entry: |
j3 | 15:16ca4c747252 | 522 | * @param[in] bcd - 0-99 |
j3 | 15:16ca4c747252 | 523 | * |
j3 | 15:16ca4c747252 | 524 | * On Exit: |
j3 | 15:16ca4c747252 | 525 | * @return rtn_val = integer rep. of BCD |
j3 | 15:16ca4c747252 | 526 | * |
j3 | 15:16ca4c747252 | 527 | **************************************************************/ |
j3 | 15:16ca4c747252 | 528 | uint8_t bcd_2_uchar(uint8_t bcd); |
j3 | 15:16ca4c747252 | 529 | |
j3 | 0:b00c4699ae6f | 530 | }; |
j3 | 0:b00c4699ae6f | 531 | #endif /* DS3231_H*/ |