A feature complete driver for the ISL1208 real time clock from Intersil.

Dependents:   ISL1208_HelloWorld

Committer:
neilt6
Date:
Tue Nov 12 17:15:20 2013 +0000
Revision:
4:42dc07f9ffb3
Parent:
3:115e4dacfe07
Child:
5:d67ac8351c76
open() no longer configures the oscillator

Who changed what in which revision?

UserRevisionLine numberNew contents of line
neilt6 0:697ca602e934 1 /* ISL1208 Driver Library
neilt6 0:697ca602e934 2 * Copyright (c) 2013 Neil Thiessen
neilt6 0:697ca602e934 3 *
neilt6 0:697ca602e934 4 * Licensed under the Apache License, Version 2.0 (the "License");
neilt6 0:697ca602e934 5 * you may not use this file except in compliance with the License.
neilt6 0:697ca602e934 6 * You may obtain a copy of the License at
neilt6 0:697ca602e934 7 *
neilt6 0:697ca602e934 8 * http://www.apache.org/licenses/LICENSE-2.0
neilt6 0:697ca602e934 9 *
neilt6 0:697ca602e934 10 * Unless required by applicable law or agreed to in writing, software
neilt6 0:697ca602e934 11 * distributed under the License is distributed on an "AS IS" BASIS,
neilt6 0:697ca602e934 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
neilt6 0:697ca602e934 13 * See the License for the specific language governing permissions and
neilt6 0:697ca602e934 14 * limitations under the License.
neilt6 0:697ca602e934 15 */
neilt6 0:697ca602e934 16
neilt6 0:697ca602e934 17 #include "ISL1208.h"
neilt6 0:697ca602e934 18
neilt6 0:697ca602e934 19 ISL1208::ISL1208(PinName sda, PinName scl) : m_I2C(sda, scl)
neilt6 0:697ca602e934 20 {
neilt6 2:f33dbb2535a3 21 //Set the I2C bus frequency to 400kHz
neilt6 2:f33dbb2535a3 22 m_I2C.frequency(400000);
neilt6 0:697ca602e934 23 }
neilt6 0:697ca602e934 24
neilt6 4:42dc07f9ffb3 25 bool ISL1208::open()
neilt6 0:697ca602e934 26 {
neilt6 0:697ca602e934 27 //Probe for the ISL1208 using a Zero Length Transfer
neilt6 0:697ca602e934 28 if (!m_I2C.write(m_ADDR, NULL, 0)) {
neilt6 0:697ca602e934 29 //Read the current status register
neilt6 0:697ca602e934 30 char sr = read8(REG_CTL_SR);
neilt6 0:697ca602e934 31
neilt6 0:697ca602e934 32 //Disable auto reset for BAT and ALM bits
neilt6 0:697ca602e934 33 sr &= ~(1 << 7);
neilt6 0:697ca602e934 34
neilt6 0:697ca602e934 35 //Write the new status register
neilt6 0:697ca602e934 36 write8(REG_CTL_SR, sr);
neilt6 0:697ca602e934 37
neilt6 0:697ca602e934 38 //Return success
neilt6 0:697ca602e934 39 return true;
neilt6 0:697ca602e934 40 } else {
neilt6 0:697ca602e934 41 //Return failure
neilt6 0:697ca602e934 42 return false;
neilt6 0:697ca602e934 43 }
neilt6 0:697ca602e934 44 }
neilt6 0:697ca602e934 45
neilt6 2:f33dbb2535a3 46 time_t ISL1208::time()
neilt6 0:697ca602e934 47 {
neilt6 0:697ca602e934 48 //Setup a tm structure based on the RTC
neilt6 0:697ca602e934 49 struct tm timeinfo;
neilt6 0:697ca602e934 50 timeinfo.tm_sec = bcd2bin(read8(REG_RTC_SC));
neilt6 0:697ca602e934 51 timeinfo.tm_min = bcd2bin(read8(REG_RTC_MN));
neilt6 0:697ca602e934 52
neilt6 0:697ca602e934 53 //Make sure we get the proper hour regardless of the mode
neilt6 0:697ca602e934 54 char hours = read8(REG_RTC_HR);
neilt6 0:697ca602e934 55 if (hours & (1 << 7)) {
neilt6 0:697ca602e934 56 //RTC is in 24-hour mode
neilt6 0:697ca602e934 57 timeinfo.tm_hour = bcd2bin(hours & 0x3F);
neilt6 0:697ca602e934 58 } else {
neilt6 0:697ca602e934 59 //RTC is in 12-hour mode
neilt6 0:697ca602e934 60 timeinfo.tm_hour = bcd2bin(hours & 0x1F);
neilt6 0:697ca602e934 61
neilt6 0:697ca602e934 62 //Check for the PM flag
neilt6 0:697ca602e934 63 if (hours & (1 << 5))
neilt6 0:697ca602e934 64 timeinfo.tm_hour += 12;
neilt6 0:697ca602e934 65 }
neilt6 0:697ca602e934 66
neilt6 0:697ca602e934 67 //Continue reading the registers
neilt6 0:697ca602e934 68 timeinfo.tm_mday = bcd2bin(read8(REG_RTC_DT));
neilt6 0:697ca602e934 69 timeinfo.tm_mon = bcd2bin(read8(REG_RTC_MO)) - 1;
neilt6 0:697ca602e934 70 timeinfo.tm_year = bcd2bin(read8(REG_RTC_YR)) + 100;
neilt6 0:697ca602e934 71 timeinfo.tm_wday = bcd2bin(read8(REG_RTC_DW));
neilt6 0:697ca602e934 72
neilt6 0:697ca602e934 73 //Return as a timestamp
neilt6 0:697ca602e934 74 return mktime(&timeinfo);
neilt6 0:697ca602e934 75 }
neilt6 0:697ca602e934 76
neilt6 0:697ca602e934 77 void ISL1208::time(time_t t)
neilt6 0:697ca602e934 78 {
neilt6 0:697ca602e934 79 //Convert the time to a tm
neilt6 0:697ca602e934 80 struct tm *timeinfo = localtime(&t);
neilt6 0:697ca602e934 81
neilt6 0:697ca602e934 82 /* The clock has an 8 bit wide bcd-coded register (they never learn)
neilt6 0:697ca602e934 83 * for the year. tm_year is an offset from 1900 and we are interested
neilt6 0:697ca602e934 84 * in the 2000-2099 range, so any value less than 100 is invalid.
neilt6 0:697ca602e934 85 */
neilt6 0:697ca602e934 86 if (timeinfo->tm_year < 100)
neilt6 0:697ca602e934 87 return;
neilt6 0:697ca602e934 88
neilt6 0:697ca602e934 89 //Read the old SR register value
neilt6 0:697ca602e934 90 char sr = read8(REG_CTL_SR);
neilt6 0:697ca602e934 91
neilt6 0:697ca602e934 92 //Enable RTC writing
neilt6 0:697ca602e934 93 write8(REG_CTL_SR, sr | (1 << 4));
neilt6 0:697ca602e934 94
neilt6 0:697ca602e934 95 //Write the current time
neilt6 0:697ca602e934 96 write8(REG_RTC_SC, bin2bcd(timeinfo->tm_sec));
neilt6 0:697ca602e934 97 write8(REG_RTC_MN, bin2bcd(timeinfo->tm_min));
neilt6 0:697ca602e934 98 write8(REG_RTC_HR, bin2bcd(timeinfo->tm_hour) | (1 << 7)); //24-hour mode
neilt6 0:697ca602e934 99 write8(REG_RTC_DT, bin2bcd(timeinfo->tm_mday));
neilt6 0:697ca602e934 100 write8(REG_RTC_MO, bin2bcd(timeinfo->tm_mon + 1));
neilt6 0:697ca602e934 101 write8(REG_RTC_YR, bin2bcd(timeinfo->tm_year - 100));
neilt6 0:697ca602e934 102 write8(REG_RTC_DW, bin2bcd(timeinfo->tm_wday & 7));
neilt6 0:697ca602e934 103
neilt6 0:697ca602e934 104 //Disable RTC writing
neilt6 0:697ca602e934 105 write8(REG_CTL_SR, sr);
neilt6 0:697ca602e934 106 }
neilt6 0:697ca602e934 107
neilt6 2:f33dbb2535a3 108 bool ISL1208::powerFailed()
neilt6 0:697ca602e934 109 {
neilt6 0:697ca602e934 110 //Read the 8-bit register value
neilt6 0:697ca602e934 111 char value = read8(REG_CTL_SR);
neilt6 0:697ca602e934 112
neilt6 0:697ca602e934 113 //Return the status of the RTCF bit
neilt6 0:697ca602e934 114 if (value & (1 << 0))
neilt6 0:697ca602e934 115 return true;
neilt6 0:697ca602e934 116 else
neilt6 0:697ca602e934 117 return false;
neilt6 0:697ca602e934 118 }
neilt6 0:697ca602e934 119
neilt6 2:f33dbb2535a3 120 bool ISL1208::batteryFlag()
neilt6 0:697ca602e934 121 {
neilt6 0:697ca602e934 122 //Read the 8-bit register value
neilt6 0:697ca602e934 123 char value = read8(REG_CTL_SR);
neilt6 0:697ca602e934 124
neilt6 0:697ca602e934 125 //Return the status of the BAT bit
neilt6 0:697ca602e934 126 if (value & (1 << 1))
neilt6 0:697ca602e934 127 return true;
neilt6 0:697ca602e934 128 else
neilt6 0:697ca602e934 129 return false;
neilt6 0:697ca602e934 130 }
neilt6 0:697ca602e934 131
neilt6 2:f33dbb2535a3 132 void ISL1208::clearBatteryFlag()
neilt6 0:697ca602e934 133 {
neilt6 0:697ca602e934 134 //Read the current 8-bit register value
neilt6 0:697ca602e934 135 char value = read8(REG_CTL_SR);
neilt6 0:697ca602e934 136
neilt6 0:697ca602e934 137 //Clear the BAT bit
neilt6 0:697ca602e934 138 value &= ~(1 << 1);
neilt6 0:697ca602e934 139
neilt6 0:697ca602e934 140 //Write the value back out
neilt6 0:697ca602e934 141 write8(REG_CTL_SR, value);
neilt6 0:697ca602e934 142 }
neilt6 0:697ca602e934 143
neilt6 2:f33dbb2535a3 144 bool ISL1208::alarmFlag()
neilt6 0:697ca602e934 145 {
neilt6 0:697ca602e934 146 //Read the 8-bit register value
neilt6 0:697ca602e934 147 char value = read8(REG_CTL_SR);
neilt6 0:697ca602e934 148
neilt6 0:697ca602e934 149 //Return the status of the ALM bit
neilt6 0:697ca602e934 150 if (value & (1 << 2))
neilt6 0:697ca602e934 151 return true;
neilt6 0:697ca602e934 152 else
neilt6 0:697ca602e934 153 return false;
neilt6 0:697ca602e934 154 }
neilt6 0:697ca602e934 155
neilt6 2:f33dbb2535a3 156 void ISL1208::clearAlarmFlag()
neilt6 0:697ca602e934 157 {
neilt6 0:697ca602e934 158 //Read the current 8-bit register value
neilt6 0:697ca602e934 159 char value = read8(REG_CTL_SR);
neilt6 0:697ca602e934 160
neilt6 0:697ca602e934 161 //Clear the ALM bit
neilt6 0:697ca602e934 162 value &= ~(1 << 2);
neilt6 0:697ca602e934 163
neilt6 0:697ca602e934 164 //Write the value back out
neilt6 0:697ca602e934 165 write8(REG_CTL_SR, value);
neilt6 0:697ca602e934 166 }
neilt6 0:697ca602e934 167
neilt6 4:42dc07f9ffb3 168 ISL1208::OscillatorMode ISL1208::oscillatorMode()
neilt6 4:42dc07f9ffb3 169 {
neilt6 4:42dc07f9ffb3 170 //Read the 8-bit register value
neilt6 4:42dc07f9ffb3 171 char value = read8(REG_CTL_SR);
neilt6 4:42dc07f9ffb3 172
neilt6 4:42dc07f9ffb3 173 //Return the status of the XTOSCB bit
neilt6 4:42dc07f9ffb3 174 if (value & (1 << 6))
neilt6 4:42dc07f9ffb3 175 return OSCILLATOR_EXTERNAL;
neilt6 4:42dc07f9ffb3 176 else
neilt6 4:42dc07f9ffb3 177 return OSCILLATOR_CRYSTAL;
neilt6 4:42dc07f9ffb3 178 }
neilt6 4:42dc07f9ffb3 179
neilt6 4:42dc07f9ffb3 180 void ISL1208::oscillatorMode(OscillatorMode mode)
neilt6 4:42dc07f9ffb3 181 {
neilt6 4:42dc07f9ffb3 182 //Read the current 8-bit register value
neilt6 4:42dc07f9ffb3 183 char value = read8(REG_CTL_SR);
neilt6 4:42dc07f9ffb3 184
neilt6 4:42dc07f9ffb3 185 //Set or clear the XTOSCB bit
neilt6 4:42dc07f9ffb3 186 if (mode == OSCILLATOR_EXTERNAL)
neilt6 4:42dc07f9ffb3 187 value |= (1 << 6);
neilt6 4:42dc07f9ffb3 188 else
neilt6 4:42dc07f9ffb3 189 value &= ~(1 << 6);
neilt6 4:42dc07f9ffb3 190
neilt6 4:42dc07f9ffb3 191 //Write the value back out
neilt6 4:42dc07f9ffb3 192 write8(REG_CTL_SR, value);
neilt6 4:42dc07f9ffb3 193 }
neilt6 4:42dc07f9ffb3 194
neilt6 2:f33dbb2535a3 195 ISL1208::OutputFrequency ISL1208::foutFrequency()
neilt6 0:697ca602e934 196 {
neilt6 0:697ca602e934 197 //Read the 8-bit register value
neilt6 0:697ca602e934 198 char value = read8(REG_CTL_INT);
neilt6 0:697ca602e934 199
neilt6 0:697ca602e934 200 //Return the lower nibble
neilt6 0:697ca602e934 201 return (OutputFrequency)(value & 0x0F);
neilt6 0:697ca602e934 202 }
neilt6 0:697ca602e934 203
neilt6 0:697ca602e934 204 void ISL1208::foutFrequency(OutputFrequency freq)
neilt6 0:697ca602e934 205 {
neilt6 0:697ca602e934 206 //Read the current 8-bit register value
neilt6 0:697ca602e934 207 char value = read8(REG_CTL_INT);
neilt6 0:697ca602e934 208
neilt6 0:697ca602e934 209 //Clear the old frequency bits
neilt6 0:697ca602e934 210 value &= 0xF0;
neilt6 0:697ca602e934 211
neilt6 0:697ca602e934 212 //Set the new frequency bits
neilt6 0:697ca602e934 213 value |= freq;
neilt6 0:697ca602e934 214
neilt6 0:697ca602e934 215 //Write the value back out
neilt6 0:697ca602e934 216 write8(REG_CTL_INT, value);
neilt6 0:697ca602e934 217 }
neilt6 0:697ca602e934 218
neilt6 2:f33dbb2535a3 219 bool ISL1208::outputOnBattery()
neilt6 0:697ca602e934 220 {
neilt6 0:697ca602e934 221 //Read the 8-bit register value
neilt6 0:697ca602e934 222 char value = read8(REG_CTL_INT);
neilt6 0:697ca602e934 223
neilt6 0:697ca602e934 224 //Return the status of the FOBATB bit
neilt6 0:697ca602e934 225 if (value & (1 << 4))
neilt6 0:697ca602e934 226 return false;
neilt6 0:697ca602e934 227 else
neilt6 0:697ca602e934 228 return true;
neilt6 0:697ca602e934 229 }
neilt6 0:697ca602e934 230
neilt6 0:697ca602e934 231 void ISL1208::outputOnBattery(bool output)
neilt6 0:697ca602e934 232 {
neilt6 0:697ca602e934 233 //Read the current 8-bit register value
neilt6 0:697ca602e934 234 char value = read8(REG_CTL_INT);
neilt6 0:697ca602e934 235
neilt6 0:697ca602e934 236 //Set or clear the FOBATB bit
neilt6 0:697ca602e934 237 if (output)
neilt6 0:697ca602e934 238 value &= ~(1 << 4);
neilt6 0:697ca602e934 239 else
neilt6 0:697ca602e934 240 value |= (1 << 4);
neilt6 0:697ca602e934 241
neilt6 0:697ca602e934 242 //Write the value back out
neilt6 0:697ca602e934 243 write8(REG_CTL_INT, value);
neilt6 0:697ca602e934 244 }
neilt6 0:697ca602e934 245
neilt6 2:f33dbb2535a3 246 ISL1208::PowerMode ISL1208::powerMode()
neilt6 0:697ca602e934 247 {
neilt6 0:697ca602e934 248 //Read the 8-bit register value
neilt6 0:697ca602e934 249 char value = read8(REG_CTL_INT);
neilt6 0:697ca602e934 250
neilt6 0:697ca602e934 251 //Return the status of the LPMODE bit
neilt6 0:697ca602e934 252 if (value & (1 << 5))
neilt6 0:697ca602e934 253 return POWER_LPMODE;
neilt6 0:697ca602e934 254 else
neilt6 0:697ca602e934 255 return POWER_NORMAL;
neilt6 0:697ca602e934 256 }
neilt6 0:697ca602e934 257
neilt6 0:697ca602e934 258 void ISL1208::powerMode(PowerMode mode)
neilt6 0:697ca602e934 259 {
neilt6 0:697ca602e934 260 //Read the current 8-bit register value
neilt6 0:697ca602e934 261 char value = read8(REG_CTL_INT);
neilt6 0:697ca602e934 262
neilt6 0:697ca602e934 263 //Set or clear the LPMODE bit
neilt6 0:697ca602e934 264 if (mode == POWER_LPMODE)
neilt6 0:697ca602e934 265 value |= (1 << 5);
neilt6 0:697ca602e934 266 else
neilt6 0:697ca602e934 267 value &= ~(1 << 5);
neilt6 0:697ca602e934 268
neilt6 0:697ca602e934 269 //Write the value back out
neilt6 0:697ca602e934 270 write8(REG_CTL_INT, value);
neilt6 0:697ca602e934 271 }
neilt6 0:697ca602e934 272
neilt6 2:f33dbb2535a3 273 ISL1208::AlarmMode ISL1208::alarmMode()
neilt6 0:697ca602e934 274 {
neilt6 0:697ca602e934 275 //Read the 8-bit register value
neilt6 0:697ca602e934 276 char value = read8(REG_CTL_INT);
neilt6 0:697ca602e934 277
neilt6 0:697ca602e934 278 //Return the status of the ALME and IM bits
neilt6 0:697ca602e934 279 if (value & (1 << 6)) {
neilt6 0:697ca602e934 280 if (value & (1 << 7))
neilt6 0:697ca602e934 281 return ALARM_INTERRUPT;
neilt6 0:697ca602e934 282 else
neilt6 0:697ca602e934 283 return ALARM_SINGLE;
neilt6 0:697ca602e934 284 } else
neilt6 0:697ca602e934 285 return ALARM_DISABLED;
neilt6 0:697ca602e934 286 }
neilt6 0:697ca602e934 287
neilt6 0:697ca602e934 288 void ISL1208::alarmMode(AlarmMode mode)
neilt6 0:697ca602e934 289 {
neilt6 0:697ca602e934 290 //Read the current 8-bit register value
neilt6 0:697ca602e934 291 char value = read8(REG_CTL_INT);
neilt6 0:697ca602e934 292
neilt6 0:697ca602e934 293 //Set or clear the ALME and IM bit
neilt6 0:697ca602e934 294 if (mode != ALARM_DISABLED) {
neilt6 0:697ca602e934 295 value |= (1 << 6);
neilt6 0:697ca602e934 296 if (mode == ALARM_INTERRUPT)
neilt6 0:697ca602e934 297 value |= (1 << 7);
neilt6 0:697ca602e934 298 else
neilt6 0:697ca602e934 299 value &= ~(1 << 7);
neilt6 0:697ca602e934 300 } else
neilt6 0:697ca602e934 301 value &= ~(1 << 6);
neilt6 0:697ca602e934 302
neilt6 0:697ca602e934 303 //Write the value back out
neilt6 0:697ca602e934 304 write8(REG_CTL_INT, value);
neilt6 0:697ca602e934 305 }
neilt6 0:697ca602e934 306
neilt6 2:f33dbb2535a3 307 float ISL1208::analogTrim()
neilt6 0:697ca602e934 308 {
neilt6 0:697ca602e934 309 //Read the 8-bit register value
neilt6 0:697ca602e934 310 char value = read8(REG_CTL_ATR);
neilt6 0:697ca602e934 311
neilt6 0:697ca602e934 312 //Mask off the top 2 bits
neilt6 0:697ca602e934 313 value &= 0x3F;
neilt6 0:697ca602e934 314
neilt6 0:697ca602e934 315 //Invert bit 5
neilt6 0:697ca602e934 316 value ^= 1 << 5;
neilt6 0:697ca602e934 317
neilt6 0:697ca602e934 318 //Add an offset of 4.5pF (unit[atr] = 0.25pF)
neilt6 0:697ca602e934 319 value += 2 * 9;
neilt6 0:697ca602e934 320
neilt6 0:697ca602e934 321 //Return the analog trim in pF
neilt6 0:697ca602e934 322 return value * 0.25;
neilt6 0:697ca602e934 323 }
neilt6 0:697ca602e934 324
neilt6 0:697ca602e934 325 void ISL1208::analogTrim(float trim)
neilt6 0:697ca602e934 326 {
neilt6 0:697ca602e934 327 //Range limit trim
neilt6 0:697ca602e934 328 if (trim < 4.5)
neilt6 0:697ca602e934 329 trim = 4.5;
neilt6 0:697ca602e934 330 else if (trim > 20.25)
neilt6 0:697ca602e934 331 trim = 20.25;
neilt6 0:697ca602e934 332
neilt6 0:697ca602e934 333 //Convert the analog trim value to a 6-bit integer
neilt6 0:697ca602e934 334 char value = (char)(trim / 0.25);
neilt6 0:697ca602e934 335
neilt6 0:697ca602e934 336 //Remove the offset of 4.5pF (unit[atr] = 0.25pF)
neilt6 0:697ca602e934 337 value -= 2 * 9;
neilt6 0:697ca602e934 338
neilt6 0:697ca602e934 339 //Invert bit 5
neilt6 0:697ca602e934 340 value ^= 1 << 5;
neilt6 0:697ca602e934 341
neilt6 0:697ca602e934 342 //Read the current 8-bit register value
neilt6 0:697ca602e934 343 char reg = read8(REG_CTL_ATR);
neilt6 0:697ca602e934 344
neilt6 0:697ca602e934 345 //Clear the old ATR bits
neilt6 0:697ca602e934 346 reg &= 0xC0;
neilt6 0:697ca602e934 347
neilt6 0:697ca602e934 348 //Add the new ATR bits
neilt6 0:697ca602e934 349 reg |= value;
neilt6 0:697ca602e934 350
neilt6 0:697ca602e934 351 //Write the value back out
neilt6 0:697ca602e934 352 write8(REG_CTL_ATR, reg);
neilt6 0:697ca602e934 353 }
neilt6 0:697ca602e934 354
neilt6 2:f33dbb2535a3 355 ISL1208::BatteryModeATR ISL1208::batteryModeATR()
neilt6 0:697ca602e934 356 {
neilt6 0:697ca602e934 357 //Read the 8-bit register value
neilt6 0:697ca602e934 358 char value = read8(REG_CTL_ATR);
neilt6 0:697ca602e934 359
neilt6 0:697ca602e934 360 //Shift out the ATR bits
neilt6 0:697ca602e934 361 value >>= 6;
neilt6 0:697ca602e934 362
neilt6 0:697ca602e934 363 //Return the value as a BatteryModeATR enum
neilt6 0:697ca602e934 364 return (BatteryModeATR)value;
neilt6 0:697ca602e934 365 }
neilt6 0:697ca602e934 366
neilt6 0:697ca602e934 367 void ISL1208::batteryModeATR(BatteryModeATR atr)
neilt6 0:697ca602e934 368 {
neilt6 0:697ca602e934 369 //Read the current 8-bit register value
neilt6 0:697ca602e934 370 char value = read8(REG_CTL_ATR);
neilt6 0:697ca602e934 371
neilt6 0:697ca602e934 372 //Clear the old battery mode ATR bits
neilt6 0:697ca602e934 373 value &= 0x3F;
neilt6 0:697ca602e934 374
neilt6 0:697ca602e934 375 //Add the new battery mode ATR bits
neilt6 0:697ca602e934 376 value |= (atr << 6);
neilt6 0:697ca602e934 377
neilt6 0:697ca602e934 378 //Write the value back out
neilt6 0:697ca602e934 379 write8(REG_CTL_ATR, value);
neilt6 0:697ca602e934 380 }
neilt6 0:697ca602e934 381
neilt6 2:f33dbb2535a3 382 ISL1208::DigitalTrim ISL1208::digitalTrim()
neilt6 0:697ca602e934 383 {
neilt6 0:697ca602e934 384 //Read the 8-bit register value
neilt6 0:697ca602e934 385 char value = read8(REG_CTL_DTR);
neilt6 0:697ca602e934 386
neilt6 0:697ca602e934 387 //Mask off the reserved bit
neilt6 0:697ca602e934 388 value &= ~(1 << 7);
neilt6 0:697ca602e934 389
neilt6 0:697ca602e934 390 //Return the value as a DigitalTrim enum
neilt6 0:697ca602e934 391 return (DigitalTrim)value;
neilt6 0:697ca602e934 392 }
neilt6 0:697ca602e934 393
neilt6 0:697ca602e934 394 void ISL1208::digitalTrim(DigitalTrim dtr)
neilt6 0:697ca602e934 395 {
neilt6 0:697ca602e934 396 //Read the current 8-bit register value (to preserve the reserved bit)
neilt6 0:697ca602e934 397 char value = read8(REG_CTL_DTR);
neilt6 0:697ca602e934 398
neilt6 0:697ca602e934 399 //Clear the old DTR bits
neilt6 0:697ca602e934 400 value &= 0xF8;
neilt6 0:697ca602e934 401
neilt6 0:697ca602e934 402 //Add the new DTR bits
neilt6 0:697ca602e934 403 value |= dtr;
neilt6 0:697ca602e934 404
neilt6 0:697ca602e934 405 //Write the value back out
neilt6 0:697ca602e934 406 write8(REG_CTL_DTR, value);
neilt6 0:697ca602e934 407 }
neilt6 0:697ca602e934 408
neilt6 2:f33dbb2535a3 409 time_t ISL1208::alarmTime()
neilt6 0:697ca602e934 410 {
neilt6 0:697ca602e934 411 //Setup a tm structure based on the RTC
neilt6 0:697ca602e934 412 struct tm timeinfo;
neilt6 0:697ca602e934 413
neilt6 0:697ca602e934 414 //MSB of each alarm register is an enable bit
neilt6 0:697ca602e934 415 timeinfo.tm_sec = bcd2bin(read8(REG_ALM_SCA) & 0x7F);
neilt6 0:697ca602e934 416 timeinfo.tm_min = bcd2bin(read8(REG_ALM_MNA) & 0x7F);
neilt6 0:697ca602e934 417 timeinfo.tm_hour = bcd2bin(read8(REG_ALM_HRA) & 0x3F);
neilt6 0:697ca602e934 418 timeinfo.tm_mday = bcd2bin(read8(REG_ALM_DTA) & 0x3F);
neilt6 0:697ca602e934 419 timeinfo.tm_mon = bcd2bin(read8(REG_ALM_MOA) & 0x1F) - 1;
neilt6 0:697ca602e934 420 timeinfo.tm_wday = bcd2bin(read8(REG_ALM_DWA) & 0x03);
neilt6 0:697ca602e934 421
neilt6 0:697ca602e934 422 //The alarm doesn't store the year, so get it from the RTC section
neilt6 0:697ca602e934 423 timeinfo.tm_year = bcd2bin(read8(REG_RTC_YR)) + 100;
neilt6 0:697ca602e934 424
neilt6 0:697ca602e934 425 //Return as a timestamp
neilt6 0:697ca602e934 426 return mktime(&timeinfo);
neilt6 0:697ca602e934 427 }
neilt6 0:697ca602e934 428
neilt6 0:697ca602e934 429 void ISL1208::alarmTime(time_t t, bool sc, bool mn, bool hr, bool dt, bool mo, bool dw)
neilt6 0:697ca602e934 430 {
neilt6 0:697ca602e934 431 //Convert the time to a tm
neilt6 0:697ca602e934 432 struct tm *timeinfo = localtime(&t);
neilt6 0:697ca602e934 433
neilt6 0:697ca602e934 434 //Write the new alarm time components (if enabled)
neilt6 0:697ca602e934 435 if (sc)
neilt6 0:697ca602e934 436 write8(REG_ALM_SCA, bin2bcd(timeinfo->tm_sec) | 0x80);
neilt6 0:697ca602e934 437 else
neilt6 0:697ca602e934 438 write8(REG_ALM_SCA, 0x0);
neilt6 0:697ca602e934 439 if (mn)
neilt6 0:697ca602e934 440 write8(REG_ALM_MNA, bin2bcd(timeinfo->tm_min) | 0x80);
neilt6 0:697ca602e934 441 else
neilt6 0:697ca602e934 442 write8(REG_ALM_MNA, 0x0);
neilt6 0:697ca602e934 443 if (hr)
neilt6 0:697ca602e934 444 write8(REG_ALM_HRA, bin2bcd(timeinfo->tm_hour) | 0x80);
neilt6 0:697ca602e934 445 else
neilt6 0:697ca602e934 446 write8(REG_ALM_HRA, 0x0);
neilt6 0:697ca602e934 447 if (hr)
neilt6 0:697ca602e934 448 write8(REG_ALM_DTA, bin2bcd(timeinfo->tm_mday) | 0x80);
neilt6 0:697ca602e934 449 else
neilt6 0:697ca602e934 450 write8(REG_ALM_DTA, 0x0);
neilt6 0:697ca602e934 451 if (mo)
neilt6 0:697ca602e934 452 write8(REG_ALM_MOA, bin2bcd(timeinfo->tm_mon + 1) | 0x80);
neilt6 0:697ca602e934 453 else
neilt6 0:697ca602e934 454 write8(REG_ALM_MOA, 0x0);
neilt6 0:697ca602e934 455 if (dw)
neilt6 0:697ca602e934 456 write8(REG_ALM_DWA, bin2bcd(timeinfo->tm_wday & 7) | 0x80);
neilt6 0:697ca602e934 457 else
neilt6 0:697ca602e934 458 write8(REG_ALM_DWA, 0x0);
neilt6 0:697ca602e934 459 }
neilt6 0:697ca602e934 460
neilt6 2:f33dbb2535a3 461 unsigned short ISL1208::sram()
neilt6 0:697ca602e934 462 {
neilt6 0:697ca602e934 463 //Return the complete contents of the SRAM
neilt6 0:697ca602e934 464 return read16(REG_USR_USR1);
neilt6 0:697ca602e934 465 }
neilt6 0:697ca602e934 466
neilt6 0:697ca602e934 467 void ISL1208::sram(unsigned short data)
neilt6 0:697ca602e934 468 {
neilt6 0:697ca602e934 469 //Write the complete contents of the SRAM
neilt6 0:697ca602e934 470 write16(REG_USR_USR1, data);
neilt6 0:697ca602e934 471 }
neilt6 0:697ca602e934 472
neilt6 0:697ca602e934 473 char ISL1208::read8(char reg)
neilt6 0:697ca602e934 474 {
neilt6 0:697ca602e934 475 //Select the register
neilt6 3:115e4dacfe07 476 m_I2C.write(m_ADDR, &reg, 1, true);
neilt6 0:697ca602e934 477
neilt6 0:697ca602e934 478 //Read the 8-bit register
neilt6 0:697ca602e934 479 m_I2C.read(m_ADDR, &reg, 1);
neilt6 0:697ca602e934 480
neilt6 0:697ca602e934 481 //Return the byte
neilt6 0:697ca602e934 482 return reg;
neilt6 0:697ca602e934 483 }
neilt6 0:697ca602e934 484
neilt6 0:697ca602e934 485 void ISL1208::write8(char reg, char data)
neilt6 0:697ca602e934 486 {
neilt6 0:697ca602e934 487 //Create a temporary buffer
neilt6 0:697ca602e934 488 char buff[2];
neilt6 0:697ca602e934 489
neilt6 0:697ca602e934 490 //Load the register address and 8-bit data
neilt6 0:697ca602e934 491 buff[0] = reg;
neilt6 0:697ca602e934 492 buff[1] = data;
neilt6 0:697ca602e934 493
neilt6 0:697ca602e934 494 //Write the data
neilt6 0:697ca602e934 495 m_I2C.write(m_ADDR, buff, 2);
neilt6 0:697ca602e934 496 }
neilt6 0:697ca602e934 497
neilt6 0:697ca602e934 498 unsigned short ISL1208::read16(char reg)
neilt6 0:697ca602e934 499 {
neilt6 0:697ca602e934 500 //Create a temporary buffer
neilt6 0:697ca602e934 501 char buff[2];
neilt6 0:697ca602e934 502
neilt6 0:697ca602e934 503 //Select the register
neilt6 3:115e4dacfe07 504 m_I2C.write(m_ADDR, &reg, 1, true);
neilt6 0:697ca602e934 505
neilt6 0:697ca602e934 506 //Read the 16-bit register
neilt6 0:697ca602e934 507 m_I2C.read(m_ADDR, buff, 2);
neilt6 0:697ca602e934 508
neilt6 0:697ca602e934 509 //Return the combined 16-bit value
neilt6 0:697ca602e934 510 return (buff[0] << 8) | buff[1];
neilt6 0:697ca602e934 511 }
neilt6 0:697ca602e934 512
neilt6 0:697ca602e934 513 void ISL1208::write16(char reg, unsigned short data)
neilt6 0:697ca602e934 514 {
neilt6 0:697ca602e934 515 //Create a temporary buffer
neilt6 0:697ca602e934 516 char buff[3];
neilt6 0:697ca602e934 517
neilt6 0:697ca602e934 518 //Load the register address and 16-bit data
neilt6 0:697ca602e934 519 buff[0] = reg;
neilt6 0:697ca602e934 520 buff[1] = data >> 8;
neilt6 0:697ca602e934 521 buff[2] = data;
neilt6 0:697ca602e934 522
neilt6 0:697ca602e934 523 //Write the data
neilt6 0:697ca602e934 524 m_I2C.write(m_ADDR, buff, 3);
neilt6 0:697ca602e934 525 }
neilt6 0:697ca602e934 526
neilt6 0:697ca602e934 527 unsigned int ISL1208::bcd2bin(unsigned char val)
neilt6 0:697ca602e934 528 {
neilt6 0:697ca602e934 529 return (val & 0x0F) + (val >> 4) * 10;
neilt6 0:697ca602e934 530 }
neilt6 0:697ca602e934 531
neilt6 0:697ca602e934 532 char ISL1208::bin2bcd(unsigned int val)
neilt6 0:697ca602e934 533 {
neilt6 0:697ca602e934 534 return ((val / 10) << 4) + val % 10;
neilt6 0:697ca602e934 535 }