rtc6 2.0.0.0
rtc6.h
Go to the documentation of this file.
1/*
2 * MikroSDK - MikroE Software Development Kit
3 * Copyright© 2020 MikroElektronika d.o.o.
4 *
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge,
9 * publish, distribute, sublicense, and/or sell copies of the Software,
10 * and to permit persons to whom the Software is furnished to do so,
11 * subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22 * OR OTHER DEALINGS IN THE SOFTWARE.
23 */
32// ----------------------------------------------------------------------------
33
34#ifndef RTC6_H
35#define RTC6_H
36
41#ifdef PREINIT_SUPPORTED
42#include "preinit.h"
43#endif
44
45#ifdef MikroCCoreVersion
46 #if MikroCCoreVersion >= 1
47 #include "delays.h"
48 #endif
49#endif
50
51#include "drv_digital_out.h"
52#include "drv_digital_in.h"
53#include "drv_i2c_master.h"
54
55// -------------------------------------------------------------- PUBLIC MACROS
65#define RTC6_MAP_MIKROBUS( cfg, mikrobus ) \
66 cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
67 cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
68 cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
75#define RTC6_REG_SECONDS 0x00
76#define RTC6_REG_MINUTES 0x01
77#define RTC6_REG_HOURS 0x02
78#define RTC6_REG_DAY 0x03
79#define RTC6_REG_DATE 0x04
80#define RTC6_REG_MONTH 0x05
81#define RTC6_REG_YEARS 0x06
82#define RTC6_REG_CONTROL 0x07
83#define RTC6_REG_OSC_TRIM 0x08
84#define RTC6_REG_EEUNLOCK 0x09
85#define RTC6_REG_ALARM0_SECOND 0x0A
86#define RTC6_REG_ALARM0_MINUTE 0x0B
87#define RTC6_REG_ALARM0_HOUR 0x0C
88#define RTC6_REG_ALARM0_DAY 0x0D
89#define RTC6_REG_ALARM0_DATE 0x0E
90#define RTC6_REG_ALARM0_MONTH 0x0F
91#define RTC6_REG_ALARM1_SECOND 0x11
92#define RTC6_REG_ALARM1_MINUTE 0x12
93#define RTC6_REG_ALARM1_HOUR 0x13
94#define RTC6_REG_ALARM1_DAY 0x14
95#define RTC6_REG_ALARM1_DATE 0x15
96#define RTC6_REG_ALARM1_MONTH 0x16
97#define RTC6_REG_POWER_DOWN_MINUTE 0x18
98#define RTC6_REG_POWER_DOWN_HOUR 0x19
99#define RTC6_REG_POWER_DOWN_DATE 0x1A
100#define RTC6_REG_POWER_DOWN_MONTH 0x1B
101#define RTC6_REG_POWER_UP_MINUTE 0x1C
102#define RTC6_REG_POWER_UP_HOUR 0x1D
103#define RTC6_REG_POWER_UP_DATE 0x1E
104#define RTC6_REG_POWER_UP_MONTH 0x1F
111#define RTC6_OSCILLATOR_ENABLE 0x80
112#define RTC6_OSCILLATOR_DISABLE 0x00
119#define RTC6_FORMAT_12_HOURS 0x40
120#define RTC6_FORMAT_24_HOURS 0x00
127#define RTC6_SWO_1HZ 0x01
128#define RTC6_SWO_4_096KHZ 0x02
129#define RTC6_SWO_8_192KHZ 0x03
130#define RTC6_SWO_32_768KHZ 0x04
133/* Months in Year */
134#define RTC6_MONTH_JANUARY 0x00
135#define RTC6_MONTH_FEBRUARY 0x01
136#define RTC6_MONTH_MARCH 0x02
137#define RTC6_MONTH_APRIL 0x03
138#define RTC6_MONTH_MAY 0x04
139#define RTC6_MONTH_JUNE 0x05
140#define RTC6_MONTH_JULY 0x06
141#define RTC6_MONTH_AUGUST 0x07
142#define RTC6_MONTH_SEPTEMBER 0x08
143#define RTC6_MONTH_OCTOBER 0x09
144#define RTC6_MONTH_NOVEMBER 0x0A
145#define RTC6_MONTH_DECEMBER 0x0B
152#define RTC6_DAY_MONDAY 0x00
153#define RTC6_DAY_TUESDAY 0x01
154#define RTC6_DAY_WEDNESDAY 0x02
155#define RTC6_DAY_THURSDAY 0x03
156#define RTC6_DAY_FRIDAY 0x04
157#define RTC6_DAY_SATURDAY 0x05
158#define RTC6_DAY_SUNDAY 0x06
165#define RTC6_DEVICE_SLAVE_ADDR 0x6F
166#define RTC6_SRAM_SLAVE_ADDR 0xDE
167#define RTC6_EEPROM_SLAVE_ADDR 0x57
174#define RTC6_ALARM_0 0x00
175#define RTC6_ALARM_1 0x01
182#define RTC6_RETVAL uint8_t
183
184#define RTC6_OK 0x00
185#define RTC6_INIT_ERROR 0xFF
192#define SET_ERROR 0x02
193#define DEVICE_ERROR 0x01
194#define DEVICE_OK 0x00
201#define RTC6_TIMEDATE_BYTES 0x07
202#define RTC6_SECONDS_BYTE 0x00
203#define RTC6_MINUTES_BYTE 0x01
204#define RTC6_HOUR_BYTE 0x02
205#define RTC6_DAY_BYTE 0x03
206#define RTC6_DATE_BYTE 0x04
207#define RTC6_MONTH_BYTE 0x05
208#define RTC6_YEAR_BYTE 0x06
215#define RTC6_RAM_SIZE 64
216#define RTC6_RAM_START 0x20
217#define RTC6_RAM_END 0x5f
218#define RTC6_EEPROM_SIZE 1024
219#define RTC6_EEPROM_START 0
220#define RTC6_EEPROM_END 1024
221#define RTC6_EEPROM_PAGE_SIZE 8
222#define RTC6_EEPROM_STATUS 0xFF
225/* Seconds */
230#define TIME_SEC_IN_MIN 60
231#define TIME_SEC_IN_HOUR 3600
232#define TIME_SEC_IN_DAY 86400
233#define TIME_SEC_IN_MONTH 2629743
234#define TIME_SEC_IN_YEAR_365 31536000
235#define TIME_SEC_IN_LEAP_YEAR 31622400
236#define TIME_SEC_IN_YEAR_365_25 31557600
239 // End group macro
240// --------------------------------------------------------------- PUBLIC TYPES
249typedef struct
250{
251 uint8_t seconds;
252 uint8_t minutes;
253 uint8_t hours;
254 uint8_t weekdays;
255 uint8_t monthday;
256 uint8_t month;
257 uint16_t year;
258
260
264typedef struct
265{
266 // Input pins
267
268 digital_in_t int_pin;
269
270 // Modules
271
272 i2c_master_t i2c;
273
274 // ctx variable
275
277
278 int8_t time_zone;
279
280} rtc6_t;
281
285typedef struct
286{
287 // Communication gpio pins
288
289 pin_name_t scl;
290 pin_name_t sda;
291
292 // Additional gpio pins
293
294 pin_name_t int_pin;
295
296 // static variable
297
298 uint32_t i2c_speed;
299 uint8_t i2c_address;
300
301 // Time config value
302
304
305} rtc6_cfg_t;
306
307 // End types group
308// ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
309
315#ifdef __cplusplus
316extern "C"{
317#endif
318
328
338
346void rtc6_default_cfg ( rtc6_t *ctx, int8_t time_zone, rtc6_time_t *utc_time, rtc6_time_t *alarm_time );
347
358void rtc6_generic_write ( rtc6_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
359
370void rtc6_generic_read ( rtc6_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
371
372uint8_t rtc6_init_fun ( rtc6_t *ctx, int8_t time_zone );
373
389void rtc6_enable_swo ( rtc6_t *ctx, uint8_t swo );
390
399
408
417void rtc6_get_gmt_time ( rtc6_t *ctx, rtc6_time_t *gmt_time );
418
427void rtc6_set_gmt_time ( rtc6_t *ctx, rtc6_time_t *set_time );
428
437void rtc6_get_local_time ( rtc6_t *ctx, rtc6_time_t *local_time );
438
449
460
470uint8_t rtc6_is_leap_year ( rtc6_t *ctx );
471
482
492
503void rtc6_set_alarm ( rtc6_t *ctx, uint8_t alarm, uint8_t trigger, rtc6_time_t *time );
504
513void rtc6_disable_alarm ( rtc6_t *ctx, uint8_t alarm );
514
524void rtc6_read_alarm ( rtc6_t *ctx, uint8_t alarm, rtc6_time_t *get_alarm );
525
536
546void rtc6_repeat_alarm ( rtc6_t *ctx, uint8_t alarm, uint32_t sec );
547
557void rtc6_write_sram ( rtc6_t *ctx, uint8_t addr, uint8_t data_in );
558
569uint8_t rtc6_read_sram ( rtc6_t *ctx, uint8_t addr );
570
579
588
597
608void rtc6_write_eeprom ( rtc6_t *ctx, uint8_t addr, uint8_t *data_in, uint8_t data_size );
609
620void rtc6_read_eeprom ( rtc6_t *ctx, uint8_t addr, uint8_t *data_out, uint8_t data_size );
621
631void rtc6_read_unique_id ( rtc6_t *ctx, uint8_t *unique_id );
632
633#ifdef __cplusplus
634}
635#endif
636#endif // _RTC6_H_
637
638 // End public_function group
640
641// ------------------------------------------------------------------------- END
#define RTC6_RETVAL
Definition rtc6.h:182
void rtc6_generic_read(rtc6_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
void rtc6_get_last_power_failure(rtc6_t *ctx, rtc6_time_t *last_time)
Gets the time of the last power failure.
void rtc6_default_cfg(rtc6_t *ctx, int8_t time_zone, rtc6_time_t *utc_time, rtc6_time_t *alarm_time)
Click Default Configuration function.
void rtc6_write_eeprom(rtc6_t *ctx, uint8_t addr, uint8_t *data_in, uint8_t data_size)
Writes data to EEPROM.
void rtc6_generic_write(rtc6_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
void rtc6_cfg_setup(rtc6_cfg_t *cfg)
Config Object Initialization function.
uint8_t rtc6_eeprom_is_locked(rtc6_t *ctx)
Lock check function.
void rtc6_enable_swo(rtc6_t *ctx, uint8_t swo)
Enable Square Wave Output.
uint8_t rtc6_is_power_failure(rtc6_t *ctx)
Checks if a power failure has occured.
void rtc6_repeat_alarm(rtc6_t *ctx, uint8_t alarm, uint32_t sec)
Repeat Alarm.
void rtc6_get_local_time(rtc6_t *ctx, rtc6_time_t *local_time)
Calculates the current local time.
uint8_t rtc6_read_sram(rtc6_t *ctx, uint8_t addr)
Reads data from an SRAM.
void rtc6_get_gmt_time(rtc6_t *ctx, rtc6_time_t *gmt_time)
Gets the current GMT time set in the RTC.
uint8_t rtc6_is_leap_year(rtc6_t *ctx)
Checks if the current year is a leap one.
uint32_t rtc6_get_gmt_unix_time(rtc6_t *ctx)
Calculates the current GMT time in UNIX epoch time.
void rtc6_battery_enable(rtc6_t *ctx)
Enables automatic switch to battery.
void rtc6_read_unique_id(rtc6_t *ctx, uint8_t *unique_id)
Reads unique ID.
void rtc6_set_gmt_time(rtc6_t *ctx, rtc6_time_t *set_time)
Sets the UTC GMT time.
uint8_t rtc6_is_active_alarm(rtc6_t *ctx)
Check if the alarm is activated.
uint32_t rtc6_get_local_unix_time(rtc6_t *ctx)
Calculates the current local time in UNIX epoch time.
void rtc6_write_sram(rtc6_t *ctx, uint8_t addr, uint8_t data_in)
Writes data to the SRAM.
void rtc6_write_eeprom_protect_on(rtc6_t *ctx)
Write protect on.
void rtc6_read_alarm(rtc6_t *ctx, uint8_t alarm, rtc6_time_t *get_alarm)
Reads the alarm time.
void rtc6_read_eeprom(rtc6_t *ctx, uint8_t addr, uint8_t *data_out, uint8_t data_size)
Reads data from EEPROM.
uint8_t rtc6_init_fun(rtc6_t *ctx, int8_t time_zone)
void rtc6_disable_swo(rtc6_t *ctx)
Disables Square Wave Output.
void rtc6_disable_alarm(rtc6_t *ctx, uint8_t alarm)
Disables the alarm.
void rtc6_set_alarm(rtc6_t *ctx, uint8_t alarm, uint8_t trigger, rtc6_time_t *time)
Sets the desired alarm.
RTC6_RETVAL rtc6_init(rtc6_t *ctx, rtc6_cfg_t *cfg)
Initialization function.
void rtc6_write_eeprom_protect_off(rtc6_t *ctx)
Write protect off.
Click configuration structure definition.
Definition rtc6.h:286
int8_t time_zone_cfg
Definition rtc6.h:303
uint32_t i2c_speed
Definition rtc6.h:298
pin_name_t scl
Definition rtc6.h:289
pin_name_t int_pin
Definition rtc6.h:294
pin_name_t sda
Definition rtc6.h:290
uint8_t i2c_address
Definition rtc6.h:299
Click ctx object definition.
Definition rtc6.h:265
int8_t time_zone
Definition rtc6.h:278
digital_in_t int_pin
Definition rtc6.h:268
i2c_master_t i2c
Definition rtc6.h:272
uint8_t slave_address
Definition rtc6.h:276
Click time data object.
Definition rtc6.h:250
uint8_t hours
Definition rtc6.h:253
uint8_t monthday
Definition rtc6.h:255
uint8_t month
Definition rtc6.h:256
uint8_t seconds
Definition rtc6.h:251
uint16_t year
Definition rtc6.h:257
uint8_t minutes
Definition rtc6.h:252
uint8_t weekdays
Definition rtc6.h:254