ISL1208 library based on original ISL1208 library, modified to suit our needs.

Fork of ISL1208 by Neil Thiessen

Committer:
goranirnas
Date:
Tue Apr 03 09:08:36 2018 +0000
Revision:
7:00b8ead188f8
Parent:
6:c0635401a37f
i2c object in library converted to a pointer to i2c object to more easily add several devices on i2c bus.

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