ds3231
Fork of ds3231 by
ds3231.h@15:16ca4c747252, 2016-03-28 (annotated)
- Committer:
- j3
- Date:
- Mon Mar 28 19:17:34 2016 +0000
- Revision:
- 15:16ca4c747252
- Parent:
- 14:11630748e2f2
- Child:
- 16:e1c0f63595d9
removed inheritance of I2C class and used private member for I2C bus
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 | 14:11630748e2f2 | 154 | |
j3 | 15:16ca4c747252 | 155 | class Ds3231 |
j3 | 0:b00c4699ae6f | 156 | { |
j3 | 0:b00c4699ae6f | 157 | public: |
j3 | 13:aa3eb69b25bb | 158 | |
j3 | 13:aa3eb69b25bb | 159 | /** |
j3 | 13:aa3eb69b25bb | 160 | * ds3231_regs_t - enumerated DS3231 registers |
j3 | 13:aa3eb69b25bb | 161 | */ |
j3 | 13:aa3eb69b25bb | 162 | typedef enum |
j3 | 13:aa3eb69b25bb | 163 | { |
j3 | 13:aa3eb69b25bb | 164 | SECONDS, |
j3 | 13:aa3eb69b25bb | 165 | MINUTES, |
j3 | 13:aa3eb69b25bb | 166 | HOURS, |
j3 | 13:aa3eb69b25bb | 167 | DAY, |
j3 | 13:aa3eb69b25bb | 168 | DATE, |
j3 | 13:aa3eb69b25bb | 169 | MONTH, |
j3 | 13:aa3eb69b25bb | 170 | YEAR, |
j3 | 13:aa3eb69b25bb | 171 | ALRM1_SECONDS, |
j3 | 13:aa3eb69b25bb | 172 | ALRM1_MINUTES, |
j3 | 13:aa3eb69b25bb | 173 | ALRM1_HOURS, |
j3 | 13:aa3eb69b25bb | 174 | ALRM1_DAY_DATE, |
j3 | 13:aa3eb69b25bb | 175 | ALRM2_MINUTES, |
j3 | 13:aa3eb69b25bb | 176 | ALRM2_HOURS, |
j3 | 13:aa3eb69b25bb | 177 | ALRM2_DAY_DATE, |
j3 | 13:aa3eb69b25bb | 178 | CONTROL, |
j3 | 13:aa3eb69b25bb | 179 | STATUS, |
j3 | 13:aa3eb69b25bb | 180 | AGING_OFFSET, //don't touch this register |
j3 | 13:aa3eb69b25bb | 181 | MSB_TEMP, |
j3 | 13:aa3eb69b25bb | 182 | LSB_TEMP |
j3 | 13:aa3eb69b25bb | 183 | }ds3231_regs_t; |
j3 | 14:11630748e2f2 | 184 | |
j3 | 9:e57201ee8921 | 185 | |
j3 | 0:b00c4699ae6f | 186 | /**********************************************************//** |
j3 | 0:b00c4699ae6f | 187 | * Constructor for Ds3231 Class |
j3 | 0:b00c4699ae6f | 188 | * |
j3 | 0:b00c4699ae6f | 189 | * On Entry: |
j3 | 1:c814af60fdbf | 190 | * @param[in] sda - sda pin of I2C bus |
j3 | 1:c814af60fdbf | 191 | * @param[in] scl - scl pin of I2C bus |
j3 | 0:b00c4699ae6f | 192 | * |
j3 | 0:b00c4699ae6f | 193 | * On Exit: |
j3 | 0:b00c4699ae6f | 194 | * @return none |
j3 | 0:b00c4699ae6f | 195 | * |
j3 | 0:b00c4699ae6f | 196 | * Example: |
j3 | 0:b00c4699ae6f | 197 | * @code |
j3 | 0:b00c4699ae6f | 198 | * |
j3 | 0:b00c4699ae6f | 199 | * //instantiate rtc object |
j3 | 1:c814af60fdbf | 200 | * Ds3231 rtc(D14, D15); |
j3 | 0:b00c4699ae6f | 201 | * |
j3 | 0:b00c4699ae6f | 202 | * @endcode |
j3 | 0:b00c4699ae6f | 203 | **************************************************************/ |
j3 | 1:c814af60fdbf | 204 | Ds3231(PinName sda, PinName scl); |
j3 | 0:b00c4699ae6f | 205 | |
j3 | 0:b00c4699ae6f | 206 | |
j3 | 0:b00c4699ae6f | 207 | /**********************************************************//** |
j3 | 15:16ca4c747252 | 208 | * Constructor for Ds3231 Class |
j3 | 15:16ca4c747252 | 209 | * |
j3 | 15:16ca4c747252 | 210 | * On Entry: |
j3 | 15:16ca4c747252 | 211 | * @param[in] i2c_bus - reference to existing bus |
j3 | 15:16ca4c747252 | 212 | * |
j3 | 15:16ca4c747252 | 213 | * On Exit: |
j3 | 15:16ca4c747252 | 214 | * |
j3 | 15:16ca4c747252 | 215 | * @return none |
j3 | 15:16ca4c747252 | 216 | **************************************************************/ |
j3 | 15:16ca4c747252 | 217 | Ds3231(I2C & i2c_bus); |
j3 | 15:16ca4c747252 | 218 | |
j3 | 15:16ca4c747252 | 219 | |
j3 | 15:16ca4c747252 | 220 | ~Ds3231(); |
j3 | 15:16ca4c747252 | 221 | |
j3 | 15:16ca4c747252 | 222 | |
j3 | 15:16ca4c747252 | 223 | /**********************************************************//** |
j3 | 0:b00c4699ae6f | 224 | * Sets the time on DS3231 |
j3 | 2:4e6e761c60f2 | 225 | * Struct data is in integrer format, not BCD. Fx will convert |
j3 | 2:4e6e761c60f2 | 226 | * to BCD for you. |
j3 | 0:b00c4699ae6f | 227 | * |
j3 | 0:b00c4699ae6f | 228 | * On Entry: |
j3 | 0:b00c4699ae6f | 229 | * @param[in] time - struct cotaining time data; |
j3 | 0:b00c4699ae6f | 230 | * |
j3 | 0:b00c4699ae6f | 231 | * On Exit: |
j3 | 0:b00c4699ae6f | 232 | * @return return value = 0 on success, non-0 on failure |
j3 | 0:b00c4699ae6f | 233 | * |
j3 | 0:b00c4699ae6f | 234 | * Example: |
j3 | 0:b00c4699ae6f | 235 | * @code |
j3 | 0:b00c4699ae6f | 236 | * |
j3 | 0:b00c4699ae6f | 237 | * //instantiate rtc object |
j3 | 1:c814af60fdbf | 238 | * Ds3231 rtc(D14, D15); |
j3 | 0:b00c4699ae6f | 239 | * |
j3 | 4:0beb5e9ac927 | 240 | * //time = 12:00:00 AM 12hr mode |
j3 | 4:0beb5e9ac927 | 241 | * ds3231_time_t time = {12, 0, 0, 0, 1} |
j3 | 0:b00c4699ae6f | 242 | * uint16_t rtn_val; |
j3 | 0:b00c4699ae6f | 243 | * |
j3 | 0:b00c4699ae6f | 244 | * rtn_val = rtc.set_time(time); |
j3 | 0:b00c4699ae6f | 245 | * |
j3 | 0:b00c4699ae6f | 246 | * @endcode |
j3 | 0:b00c4699ae6f | 247 | **************************************************************/ |
j3 | 0:b00c4699ae6f | 248 | uint16_t set_time(ds3231_time_t time); |
j3 | 0:b00c4699ae6f | 249 | |
j3 | 0:b00c4699ae6f | 250 | |
j3 | 0:b00c4699ae6f | 251 | /**********************************************************//** |
j3 | 0:b00c4699ae6f | 252 | * Sets the calendar on DS3231 |
j3 | 0:b00c4699ae6f | 253 | * |
j3 | 0:b00c4699ae6f | 254 | * On Entry: |
j3 | 3:312589d8185c | 255 | * @param[in] calendar - struct cotaining calendar data |
j3 | 0:b00c4699ae6f | 256 | * |
j3 | 0:b00c4699ae6f | 257 | * On Exit: |
j3 | 0:b00c4699ae6f | 258 | * @return return value = 0 on success, non-0 on failure |
j3 | 0:b00c4699ae6f | 259 | * |
j3 | 0:b00c4699ae6f | 260 | * Example: |
j3 | 0:b00c4699ae6f | 261 | * @code |
j3 | 0:b00c4699ae6f | 262 | * |
j3 | 0:b00c4699ae6f | 263 | * //instantiate rtc object |
j3 | 1:c814af60fdbf | 264 | * Ds3231 rtc(D14, D15); |
j3 | 0:b00c4699ae6f | 265 | * |
j3 | 0:b00c4699ae6f | 266 | * //see datasheet for calendar format |
j3 | 0:b00c4699ae6f | 267 | * ds3231_calendar_t calendar = {1, 1, 1, 0}; |
j3 | 0:b00c4699ae6f | 268 | * uint16_t rtn_val; |
j3 | 0:b00c4699ae6f | 269 | * |
j3 | 0:b00c4699ae6f | 270 | * rtn_val = rtc.set_calendar(calendar); |
j3 | 0:b00c4699ae6f | 271 | * |
j3 | 0:b00c4699ae6f | 272 | * @endcode |
j3 | 0:b00c4699ae6f | 273 | **************************************************************/ |
j3 | 0:b00c4699ae6f | 274 | uint16_t set_calendar(ds3231_calendar_t calendar); |
j3 | 0:b00c4699ae6f | 275 | |
j3 | 0:b00c4699ae6f | 276 | |
j3 | 0:b00c4699ae6f | 277 | /**********************************************************//** |
j3 | 0:b00c4699ae6f | 278 | * Set either Alarm1 or Alarm2 of DS3231 |
j3 | 0:b00c4699ae6f | 279 | * |
j3 | 0:b00c4699ae6f | 280 | * On Entry: |
j3 | 0:b00c4699ae6f | 281 | * @param[in] alarm - struct cotaining alarm data |
j3 | 3:312589d8185c | 282 | * |
j3 | 0:b00c4699ae6f | 283 | * @param[in] one_r_two - TRUE for Alarm1 and FALSE for |
j3 | 0:b00c4699ae6f | 284 | * Alarm2 |
j3 | 0:b00c4699ae6f | 285 | * |
j3 | 0:b00c4699ae6f | 286 | * On Exit: |
j3 | 0:b00c4699ae6f | 287 | * @return return value = 0 on success, non-0 on failure |
j3 | 0:b00c4699ae6f | 288 | * |
j3 | 0:b00c4699ae6f | 289 | * Example: |
j3 | 0:b00c4699ae6f | 290 | * @code |
j3 | 0:b00c4699ae6f | 291 | * |
j3 | 0:b00c4699ae6f | 292 | * //instantiate rtc object |
j3 | 1:c814af60fdbf | 293 | * Ds3231 rtc(D14, D15); |
j3 | 0:b00c4699ae6f | 294 | * |
j3 | 4:0beb5e9ac927 | 295 | * //see ds3231.h for .members and datasheet for alarm format |
j3 | 4:0beb5e9ac927 | 296 | * ds3231_alrm_t alarm; |
j3 | 0:b00c4699ae6f | 297 | * uint16_t rtn_val; |
j3 | 0:b00c4699ae6f | 298 | * |
j3 | 0:b00c4699ae6f | 299 | * rtn_val = rtc.set_alarm(alarm, FALSE); |
j3 | 0:b00c4699ae6f | 300 | * |
j3 | 0:b00c4699ae6f | 301 | * @endcode |
j3 | 0:b00c4699ae6f | 302 | **************************************************************/ |
j3 | 0:b00c4699ae6f | 303 | uint16_t set_alarm(ds3231_alrm_t alarm, bool one_r_two); |
j3 | 0:b00c4699ae6f | 304 | |
j3 | 0:b00c4699ae6f | 305 | |
j3 | 0:b00c4699ae6f | 306 | /**********************************************************//** |
j3 | 0:b00c4699ae6f | 307 | * Set control and status registers of DS3231 |
j3 | 0:b00c4699ae6f | 308 | * |
j3 | 0:b00c4699ae6f | 309 | * On Entry: |
j3 | 0:b00c4699ae6f | 310 | * @param[in] data - Struct containing control and status |
j3 | 0:b00c4699ae6f | 311 | * register data |
j3 | 0:b00c4699ae6f | 312 | * |
j3 | 0:b00c4699ae6f | 313 | * On Exit: |
j3 | 0:b00c4699ae6f | 314 | * @return return value = 0 on success, non-0 on failure |
j3 | 0:b00c4699ae6f | 315 | * |
j3 | 0:b00c4699ae6f | 316 | * Example: |
j3 | 0:b00c4699ae6f | 317 | * @code |
j3 | 0:b00c4699ae6f | 318 | * |
j3 | 0:b00c4699ae6f | 319 | * //instantiate rtc object |
j3 | 1:c814af60fdbf | 320 | * Ds3231 rtc(D14, D15); |
j3 | 0:b00c4699ae6f | 321 | * |
j3 | 0:b00c4699ae6f | 322 | * //do not use 0xAA, see datasheet for appropriate data |
j3 | 0:b00c4699ae6f | 323 | * ds3231_cntl_stat_t data = {0xAA, 0xAA}; |
j3 | 0:b00c4699ae6f | 324 | * |
j3 | 0:b00c4699ae6f | 325 | * rtn_val = rtc.set_cntl_stat_reg(data); |
j3 | 0:b00c4699ae6f | 326 | * |
j3 | 0:b00c4699ae6f | 327 | * @endcode |
j3 | 0:b00c4699ae6f | 328 | **************************************************************/ |
j3 | 0:b00c4699ae6f | 329 | uint16_t set_cntl_stat_reg(ds3231_cntl_stat_t data); |
j3 | 0:b00c4699ae6f | 330 | |
j3 | 0:b00c4699ae6f | 331 | |
j3 | 0:b00c4699ae6f | 332 | /**********************************************************//** |
j3 | 0:b00c4699ae6f | 333 | * Gets the time on DS3231 |
j3 | 0:b00c4699ae6f | 334 | * |
j3 | 0:b00c4699ae6f | 335 | * On Entry: |
j3 | 4:0beb5e9ac927 | 336 | * @param[in] time - pointer to struct for storing time data |
j3 | 0:b00c4699ae6f | 337 | * |
j3 | 0:b00c4699ae6f | 338 | * On Exit: |
j3 | 2:4e6e761c60f2 | 339 | * @param[out] time - contains current integrer rtc time |
j3 | 2:4e6e761c60f2 | 340 | * data |
j3 | 0:b00c4699ae6f | 341 | * @return return value = 0 on success, non-0 on failure |
j3 | 0:b00c4699ae6f | 342 | * |
j3 | 0:b00c4699ae6f | 343 | * Example: |
j3 | 0:b00c4699ae6f | 344 | * @code |
j3 | 0:b00c4699ae6f | 345 | * |
j3 | 0:b00c4699ae6f | 346 | * //instantiate rtc object |
j3 | 1:c814af60fdbf | 347 | * Ds3231 rtc(D14, D15); |
j3 | 0:b00c4699ae6f | 348 | * |
j3 | 4:0beb5e9ac927 | 349 | * //time = 12:00:00 AM 12hr mode |
j3 | 4:0beb5e9ac927 | 350 | * ds3231_time_t time = {12, 0, 0, 0, 1} |
j3 | 0:b00c4699ae6f | 351 | * uint16_t rtn_val; |
j3 | 0:b00c4699ae6f | 352 | * |
j3 | 3:312589d8185c | 353 | * rtn_val = rtc.get_time(&time); |
j3 | 0:b00c4699ae6f | 354 | * |
j3 | 0:b00c4699ae6f | 355 | * @endcode |
j3 | 0:b00c4699ae6f | 356 | **************************************************************/ |
j3 | 2:4e6e761c60f2 | 357 | uint16_t get_time(ds3231_time_t* time); |
j3 | 0:b00c4699ae6f | 358 | |
j3 | 0:b00c4699ae6f | 359 | |
j3 | 0:b00c4699ae6f | 360 | /**********************************************************//** |
j3 | 0:b00c4699ae6f | 361 | * Gets the calendar on DS3231 |
j3 | 0:b00c4699ae6f | 362 | * |
j3 | 0:b00c4699ae6f | 363 | * On Entry: |
j3 | 2:4e6e761c60f2 | 364 | * @param[in] calendar - pointer to struct for storing |
j3 | 4:0beb5e9ac927 | 365 | * calendar data |
j3 | 0:b00c4699ae6f | 366 | * |
j3 | 0:b00c4699ae6f | 367 | * On Exit: |
j3 | 2:4e6e761c60f2 | 368 | * @param[out] calendar - contains current integer rtc |
j3 | 2:4e6e761c60f2 | 369 | * calendar data |
j3 | 0:b00c4699ae6f | 370 | * @return return value = 0 on success, non-0 on failure |
j3 | 0:b00c4699ae6f | 371 | * |
j3 | 0:b00c4699ae6f | 372 | * Example: |
j3 | 0:b00c4699ae6f | 373 | * @code |
j3 | 0:b00c4699ae6f | 374 | * |
j3 | 0:b00c4699ae6f | 375 | * //instantiate rtc object |
j3 | 1:c814af60fdbf | 376 | * Ds3231 rtc(D14, D15); |
j3 | 0:b00c4699ae6f | 377 | * |
j3 | 0:b00c4699ae6f | 378 | * //see datasheet for calendar format |
j3 | 0:b00c4699ae6f | 379 | * ds3231_calendar_t calendar = {1, 1, 1, 0}; |
j3 | 0:b00c4699ae6f | 380 | * uint16_t rtn_val; |
j3 | 0:b00c4699ae6f | 381 | * |
j3 | 3:312589d8185c | 382 | * rtn_val = rtc.get_calendar(&calendar); |
j3 | 0:b00c4699ae6f | 383 | * |
j3 | 0:b00c4699ae6f | 384 | * @endcode |
j3 | 0:b00c4699ae6f | 385 | **************************************************************/ |
j3 | 2:4e6e761c60f2 | 386 | uint16_t get_calendar(ds3231_calendar_t* calendar); |
j3 | 0:b00c4699ae6f | 387 | |
j3 | 0:b00c4699ae6f | 388 | |
j3 | 0:b00c4699ae6f | 389 | /**********************************************************//** |
j3 | 0:b00c4699ae6f | 390 | * Get either Alarm1 or Alarm2 of DS3231 |
j3 | 0:b00c4699ae6f | 391 | * |
j3 | 0:b00c4699ae6f | 392 | * On Entry: |
j3 | 2:4e6e761c60f2 | 393 | * @param[in] alarm - pointer to struct for storing alarm |
j3 | 2:4e6e761c60f2 | 394 | * data; |
j3 | 2:4e6e761c60f2 | 395 | * |
j3 | 0:b00c4699ae6f | 396 | * @param[in] one_r_two - TRUE for Alarm1 and FALSE for |
j3 | 0:b00c4699ae6f | 397 | * Alarm2 |
j3 | 0:b00c4699ae6f | 398 | * |
j3 | 0:b00c4699ae6f | 399 | * On Exit: |
j3 | 2:4e6e761c60f2 | 400 | * @param[out] alarm - contains integer alarm data |
j3 | 0:b00c4699ae6f | 401 | * @return return value = 0 on success, non-0 on failure |
j3 | 0:b00c4699ae6f | 402 | * |
j3 | 0:b00c4699ae6f | 403 | * Example: |
j3 | 0:b00c4699ae6f | 404 | * @code |
j3 | 0:b00c4699ae6f | 405 | * |
j3 | 0:b00c4699ae6f | 406 | * //instantiate rtc object |
j3 | 1:c814af60fdbf | 407 | * Ds3231 rtc(D14, D15); |
j3 | 0:b00c4699ae6f | 408 | * |
j3 | 4:0beb5e9ac927 | 409 | * //see ds3231.h for .members and datasheet for alarm format |
j3 | 4:0beb5e9ac927 | 410 | * ds3231_alrm_t alarm; |
j3 | 0:b00c4699ae6f | 411 | * uint16_t rtn_val; |
j3 | 0:b00c4699ae6f | 412 | * |
j3 | 3:312589d8185c | 413 | * rtn_val = rtc.get_alarm(&alarm, FALSE); |
j3 | 0:b00c4699ae6f | 414 | * |
j3 | 0:b00c4699ae6f | 415 | * @endcode |
j3 | 0:b00c4699ae6f | 416 | **************************************************************/ |
j3 | 2:4e6e761c60f2 | 417 | uint16_t get_alarm(ds3231_alrm_t* alarm, bool one_r_two); |
j3 | 0:b00c4699ae6f | 418 | |
j3 | 0:b00c4699ae6f | 419 | |
j3 | 0:b00c4699ae6f | 420 | /**********************************************************//** |
j3 | 0:b00c4699ae6f | 421 | * Get control and status registers of DS3231 |
j3 | 0:b00c4699ae6f | 422 | * |
j3 | 0:b00c4699ae6f | 423 | * On Entry: |
j3 | 2:4e6e761c60f2 | 424 | * @param[in] data - pointer to struct for storing control |
j3 | 4:0beb5e9ac927 | 425 | * and status register data |
j3 | 0:b00c4699ae6f | 426 | * |
j3 | 0:b00c4699ae6f | 427 | * On Exit: |
j3 | 0:b00c4699ae6f | 428 | * @param[out] data - contains control and status registers |
j3 | 0:b00c4699ae6f | 429 | * data |
j3 | 0:b00c4699ae6f | 430 | * @return return value = 0 on success, non-0 on failure |
j3 | 0:b00c4699ae6f | 431 | * |
j3 | 0:b00c4699ae6f | 432 | * Example: |
j3 | 0:b00c4699ae6f | 433 | * @code |
j3 | 0:b00c4699ae6f | 434 | * |
j3 | 0:b00c4699ae6f | 435 | * //instantiate rtc object |
j3 | 1:c814af60fdbf | 436 | * Ds3231 rtc(D14, D15); |
j3 | 0:b00c4699ae6f | 437 | * |
j3 | 0:b00c4699ae6f | 438 | * //do not use 0xAA, see datasheet for appropriate data |
j3 | 0:b00c4699ae6f | 439 | * ds3231_cntl_stat_t data = {0xAA, 0xAA}; |
j3 | 0:b00c4699ae6f | 440 | * |
j3 | 3:312589d8185c | 441 | * rtn_val = rtc.get_cntl_stat_reg(&data); |
j3 | 0:b00c4699ae6f | 442 | * |
j3 | 0:b00c4699ae6f | 443 | * @endcode |
j3 | 0:b00c4699ae6f | 444 | **************************************************************/ |
j3 | 2:4e6e761c60f2 | 445 | uint16_t get_cntl_stat_reg(ds3231_cntl_stat_t* data); |
j3 | 0:b00c4699ae6f | 446 | |
j3 | 0:b00c4699ae6f | 447 | |
j3 | 0:b00c4699ae6f | 448 | /**********************************************************//** |
j3 | 0:b00c4699ae6f | 449 | * Get temperature data of DS3231 |
j3 | 0:b00c4699ae6f | 450 | * |
j3 | 0:b00c4699ae6f | 451 | * On Entry: |
j3 | 0:b00c4699ae6f | 452 | * |
j3 | 0:b00c4699ae6f | 453 | * On Exit: |
j3 | 2:4e6e761c60f2 | 454 | * @return return value = raw temperature data |
j3 | 0:b00c4699ae6f | 455 | * |
j3 | 0:b00c4699ae6f | 456 | * Example: |
j3 | 0:b00c4699ae6f | 457 | * @code |
j3 | 0:b00c4699ae6f | 458 | * |
j3 | 0:b00c4699ae6f | 459 | * //instantiate rtc object |
j3 | 1:c814af60fdbf | 460 | * Ds3231 rtc(D14, D15); |
j3 | 0:b00c4699ae6f | 461 | * |
j3 | 0:b00c4699ae6f | 462 | * uint16_t temp; |
j3 | 0:b00c4699ae6f | 463 | * |
j3 | 2:4e6e761c60f2 | 464 | * temp = rtc.get_temperature(); |
j3 | 0:b00c4699ae6f | 465 | * |
j3 | 0:b00c4699ae6f | 466 | * @endcode |
j3 | 0:b00c4699ae6f | 467 | **************************************************************/ |
j3 | 2:4e6e761c60f2 | 468 | uint16_t get_temperature(void); |
j3 | 0:b00c4699ae6f | 469 | |
j3 | 5:61dfe2690360 | 470 | |
j3 | 5:61dfe2690360 | 471 | /**********************************************************//** |
j3 | 5:61dfe2690360 | 472 | * Get epoch time based on current RTC time and date. |
j3 | 5:61dfe2690360 | 473 | * DS3231 must be configured and running before this fx is |
j3 | 5:61dfe2690360 | 474 | * called |
j3 | 5:61dfe2690360 | 475 | * |
j3 | 5:61dfe2690360 | 476 | * On Entry: |
j3 | 5:61dfe2690360 | 477 | * |
j3 | 5:61dfe2690360 | 478 | * On Exit: |
j3 | 5:61dfe2690360 | 479 | * @return return value = epoch time |
j3 | 5:61dfe2690360 | 480 | * |
j3 | 5:61dfe2690360 | 481 | * Example: |
j3 | 5:61dfe2690360 | 482 | * @code |
j3 | 5:61dfe2690360 | 483 | * |
j3 | 5:61dfe2690360 | 484 | * //instantiate rtc object |
j3 | 5:61dfe2690360 | 485 | * Ds3231 rtc(D14, D15); |
j3 | 5:61dfe2690360 | 486 | * |
j3 | 5:61dfe2690360 | 487 | * time_t epoch_time; |
j3 | 5:61dfe2690360 | 488 | * |
j3 | 5:61dfe2690360 | 489 | * epoch_time = rtc.get_epoch(); |
j3 | 5:61dfe2690360 | 490 | * |
j3 | 5:61dfe2690360 | 491 | * @endcode |
j3 | 5:61dfe2690360 | 492 | **************************************************************/ |
j3 | 5:61dfe2690360 | 493 | time_t get_epoch(void); |
j3 | 5:61dfe2690360 | 494 | |
j3 | 15:16ca4c747252 | 495 | private: |
j3 | 15:16ca4c747252 | 496 | |
j3 | 15:16ca4c747252 | 497 | I2C * _p_i2c; |
j3 | 15:16ca4c747252 | 498 | |
j3 | 15:16ca4c747252 | 499 | bool _i2c_owner; |
j3 | 15:16ca4c747252 | 500 | uint8_t _w_adrs, _r_adrs; |
j3 | 15:16ca4c747252 | 501 | |
j3 | 15:16ca4c747252 | 502 | /**********************************************************//** |
j3 | 15:16ca4c747252 | 503 | * Private mmber fx, converts unsigned char to BCD |
j3 | 15:16ca4c747252 | 504 | * |
j3 | 15:16ca4c747252 | 505 | * On Entry: |
j3 | 15:16ca4c747252 | 506 | * @param[in] data - 0-255 |
j3 | 15:16ca4c747252 | 507 | * |
j3 | 15:16ca4c747252 | 508 | * On Exit: |
j3 | 15:16ca4c747252 | 509 | * @return bcd_result = BCD representation of data |
j3 | 15:16ca4c747252 | 510 | * |
j3 | 15:16ca4c747252 | 511 | **************************************************************/ |
j3 | 15:16ca4c747252 | 512 | uint16_t uchar_2_bcd(uint8_t data); |
j3 | 15:16ca4c747252 | 513 | |
j3 | 15:16ca4c747252 | 514 | |
j3 | 15:16ca4c747252 | 515 | /**********************************************************//** |
j3 | 15:16ca4c747252 | 516 | * Private mmber fx, converts BCD to a uint8_t |
j3 | 15:16ca4c747252 | 517 | * |
j3 | 15:16ca4c747252 | 518 | * On Entry: |
j3 | 15:16ca4c747252 | 519 | * @param[in] bcd - 0-99 |
j3 | 15:16ca4c747252 | 520 | * |
j3 | 15:16ca4c747252 | 521 | * On Exit: |
j3 | 15:16ca4c747252 | 522 | * @return rtn_val = integer rep. of BCD |
j3 | 15:16ca4c747252 | 523 | * |
j3 | 15:16ca4c747252 | 524 | **************************************************************/ |
j3 | 15:16ca4c747252 | 525 | uint8_t bcd_2_uchar(uint8_t bcd); |
j3 | 15:16ca4c747252 | 526 | |
j3 | 0:b00c4699ae6f | 527 | }; |
j3 | 0:b00c4699ae6f | 528 | #endif /* DS3231_H*/ |