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

Dependents:   ISL1208_HelloWorld

Committer:
neilt6
Date:
Tue Nov 12 21:42:44 2013 +0000
Revision:
5:d67ac8351c76
Parent:
4:42dc07f9ffb3
Child:
6:c0635401a37f
Minor C++ syntax improvements

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