1 /**************************************************************************/
4 @author K. Townsend (microBuilder.eu)
6 @brief Drivers for the ISL12022M RTC
10 The ISL12022M is an I2C RTC with 128 bytes battery-backed up SRAM.
14 Software License Agreement (BSD License)
16 Copyright (c) 2010, microBuilder SARL
19 Redistribution and use in source and binary forms, with or without
20 modification, are permitted provided that the following conditions are met:
21 1. Redistributions of source code must retain the above copyright
22 notice, this list of conditions and the following disclaimer.
23 2. Redistributions in binary form must reproduce the above copyright
24 notice, this list of conditions and the following disclaimer in the
25 documentation and/or other materials provided with the distribution.
26 3. Neither the name of the copyright holders nor the
27 names of its contributors may be used to endorse or promote products
28 derived from this software without specific prior written permission.
30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
31 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
34 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
37 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 /**************************************************************************/
42 #include "isl12022m.h"
43 #include "core/systick/systick.h"
45 extern volatile uint8_t I2CMasterBuffer
[I2C_BUFSIZE
];
46 extern volatile uint8_t I2CSlaveBuffer
[I2C_BUFSIZE
];
47 extern volatile uint32_t I2CReadLength
, I2CWriteLength
;
50 uint8_t monthday
[12]={31,28,31,30,31,30,31,31,30,31,30,31};
52 static bool _isl12022mInitialised
= false;
54 /**************************************************************************/
56 @brief Standard decimal to binary coded decimal
58 /**************************************************************************/
59 uint8_t isl12022mDecToBCD(uint8_t val
)
61 return ( (val
/10*16) + (val
%10) );
64 /**************************************************************************/
66 @brief Binary coded decimal to standard decimal
68 /**************************************************************************/
69 uint8_t isl12022mBCDToDec(uint8_t val
)
71 return ( (val
/16*10) + (val
%16) );
74 /**************************************************************************/
76 @brief Writes an 8 bit value
78 /**************************************************************************/
79 isl12022mError_t
isl12022mWrite8 (uint8_t address
, uint8_t reg
, uint32_t value
)
81 // Clear write buffers
82 for ( i
= 0; i
< I2C_BUFSIZE
; i
++ )
84 I2CMasterBuffer
[i
] = 0x00;
89 I2CMasterBuffer
[0] = address
;
90 I2CMasterBuffer
[1] = reg
; // Command register
91 I2CMasterBuffer
[2] = (value
& 0xFF); // Value to write
93 return ISL12022M_ERROR_OK
;
96 /**************************************************************************/
98 @brief Reads x bytes into a buffer
100 /**************************************************************************/
101 isl12022mError_t
isl12022mReadBuffer(uint8_t address
, uint8_t reg
, uint8_t *buffer
, uint32_t len
)
103 if (len
> I2C_BUFSIZE
)
104 return ISL12022M_ERROR_I2C_BUFFEROVERFLOW
;
106 // Clear write buffers
107 for ( i
= 0; i
< I2C_BUFSIZE
; i
++ )
109 I2CMasterBuffer
[i
] = 0x00;
114 I2CMasterBuffer
[0] = address
;
115 I2CMasterBuffer
[1] = reg
; // Command register
116 // Append address w/read bit
117 I2CMasterBuffer
[2] = address
| ISL12022M_READBIT
;
120 // Push response into buffer
121 for ( i
= 0; i
< len
; i
++ )
123 buffer
[i
] = I2CSlaveBuffer
[i
];
126 return ISL12022M_ERROR_OK
;
129 /**************************************************************************/
131 @brief Initialises the I2C block
133 /**************************************************************************/
134 isl12022mError_t
isl12022mInit(void)
136 isl12022mError_t error
= ISL12022M_ERROR_OK
;
140 if (i2cInit(I2CMASTER
) == false)
142 return ISL12022M_ERROR_I2C_INIT
; /* Fatal error */
145 // Make sure write is enabled on the ISL12202M (factory default = disabled)
146 error
= isl12022mReadBuffer(ISL12022M_RTC_ADDRESS
, ISL12022M_REG_CSR_INT
, buffer
, sizeof(buffer
));
149 if (!(buffer
[0] & ISL12022M_INT_WRITEENABLE
))
151 // Write is not enabled on the RTC ... enable it now
152 error
= isl12022mWrite8(ISL12022M_RTC_ADDRESS
, ISL12022M_REG_CSR_INT
, buffer
[0] | ISL12022M_INT_WRITEENABLE
);
154 _isl12022mInitialised
= true;
160 /**************************************************************************/
162 @brief Gets the current date/time from the RTC
167 #include "drivers/rtc/isl12022m.h"
171 // Set the time to 12 June 2011 @ 11:59:30
172 isl12022mSetTime(0, 11, 6, 12, 11, 59, 30);
177 // Display the current time
178 isl12022mTime_t time;
179 isl12022mGetTime(&time);
180 printf("DW:%d, Y:%d, M:%d, D:%d, H:%d, M:%d, S:%d\r\n",
190 /**************************************************************************/
191 isl12022mError_t
isl12022mGetTime(isl12022mTime_t
*time
)
193 isl12022mError_t error
= ISL12022M_ERROR_OK
;
196 if (!_isl12022mInitialised
)
198 error
= isl12022mInit();
199 if (error
) return error
;
202 // Read 9 bytes at once into buffer
203 error
= isl12022mReadBuffer(ISL12022M_RTC_ADDRESS
,
204 ISL12022M_REG_RTC_SC
,
205 buffer
, sizeof(buffer
));
209 // Check status register
210 if (buffer
[7] & (ISL12022M_STATUS_LOWBATT85
| ISL12022M_STATUS_LOWBATT75
))
212 // Set low battery flag to indicate that the RTC value may not be accurate
213 error
= ISL12022M_ERROR_RTC_LOWBATT
;
216 time
->second
= isl12022mBCDToDec(buffer
[0]);
217 time
->minute
= isl12022mBCDToDec(buffer
[1]);
218 time
->hour
= buffer
[2] & 0x1F; // 0x3F;
219 time
->day
= buffer
[3] & 0x3F;
220 time
->month
= buffer
[4] & 0x1F;
221 time
->year
= buffer
[5];
222 time
->dayofweek
= buffer
[6] & 0x07;
223 time
->status
= buffer
[7];
224 time
->interrupt
= buffer
[8];
230 /**************************************************************************/
232 @brief Sets the current date/time from the RTC
237 #include "drivers/rtc/isl12022m.h"
241 // Set the time to 12 June 2011 @ 11:59:30
242 isl12022mSetTime(0, 11, 6, 12, 11, 59, 30);
247 // Display the current time
248 isl12022mTime_t time;
249 isl12022mGetTime(&time);
250 printf("DW:%d, Y:%d, M:%d, D:%d, H:%d, M:%d, S:%d\r\n",
260 /**************************************************************************/
261 isl12022mError_t
isl12022mSetTime(uint8_t dayofweek
, uint8_t year
, uint8_t month
, uint8_t day
, uint8_t hour
, uint8_t minute
, uint8_t second
)
263 isl12022mError_t error
= ISL12022M_ERROR_OK
;
265 if (!_isl12022mInitialised
)
267 error
= isl12022mInit();
268 if (error
) return error
;
271 error
= isl12022mWrite8(ISL12022M_RTC_ADDRESS
, ISL12022M_REG_RTC_SC
, isl12022mDecToBCD(second
));
272 if (error
) return error
;
273 error
= isl12022mWrite8(ISL12022M_RTC_ADDRESS
, ISL12022M_REG_RTC_MN
, isl12022mDecToBCD(minute
));
274 if (error
) return error
;
275 // Always append military flag to hour (24 hour only)
276 error
= isl12022mWrite8(ISL12022M_RTC_ADDRESS
, ISL12022M_REG_RTC_HR
, hour
& 0x3F) | ISL12022M_HR_MILITARY
;
277 if (error
) return error
;
278 error
= isl12022mWrite8(ISL12022M_RTC_ADDRESS
, ISL12022M_REG_RTC_DT
, day
& 0x3F);
279 if (error
) return error
;
280 error
= isl12022mWrite8(ISL12022M_RTC_ADDRESS
, ISL12022M_REG_RTC_MO
, month
& 0x1F);
281 if (error
) return error
;
282 error
= isl12022mWrite8(ISL12022M_RTC_ADDRESS
, ISL12022M_REG_RTC_YR
, year
);
283 if (error
) return error
;
284 error
= isl12022mWrite8(ISL12022M_RTC_ADDRESS
, ISL12022M_REG_RTC_DW
, dayofweek
& 0x07);
285 if (error
) return error
;
290 /**************************************************************************/
292 @brief Reads the current temperature from the ISL12022
297 #include "drivers/rtc/isl12022m.h"
301 uint32_t temperature;
302 isl12022mGetTemp(&temperature);
303 printf("Temperature: %u C\r\n", temperature);
307 /**************************************************************************/
308 isl12022mError_t
isl12022mGetTemp(uint8_t *celsius
)
310 isl12022mError_t error
= ISL12022M_ERROR_OK
;
314 if (!_isl12022mInitialised
)
316 error
= isl12022mInit();
317 if (error
) return error
;
320 // Enable temperature sensing if required
321 error
= isl12022mReadBuffer(ISL12022M_RTC_ADDRESS
, ISL12022M_REG_CSR_BETA
, buffer
, 1);
324 if (!(buffer
[0] & ISL12022M_BETA_TEMPENABLE
))
326 // Temp sensor is not enabled ... enable it now
327 error
= isl12022mWrite8(ISL12022M_RTC_ADDRESS
, ISL12022M_REG_CSR_BETA
, buffer
[0] | ISL12022M_BETA_TEMPENABLE
);
333 // Wait 100ms for conversion to complete
335 // Read low and high temp bytes (0x28 and 0x29)
336 error
= isl12022mReadBuffer(ISL12022M_RTC_ADDRESS
, ISL12022M_REG_TEMP_TKOL
, buffer
, 2);
339 // Convert value to degrees celsius (value/2 - 273 = degrees C)
340 temp
= ((buffer
[0]) | (buffer
[1] << 8)) / 2 - 273;
341 *celsius
= (uint8_t)temp
& 0xFF;