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 
37 #include "mikrosdk_version.h"
38 
39 #ifdef __GNUC__
40 #if mikroSDK_GET_VERSION < 20800ul
41 #include "rcu_delays.h"
42 #else
43 #include "delays.h"
44 #endif
45 #endif
46 
47 #include "drv_digital_out.h"
48 #include "drv_digital_in.h"
49 #include "drv_i2c_master.h"
50 
51 // -------------------------------------------------------------- PUBLIC MACROS
61 #define RTC6_MAP_MIKROBUS( cfg, mikrobus ) \
62  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
63  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
64  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
65 
71 #define RTC6_REG_SECONDS 0x00
72 #define RTC6_REG_MINUTES 0x01
73 #define RTC6_REG_HOURS 0x02
74 #define RTC6_REG_DAY 0x03
75 #define RTC6_REG_DATE 0x04
76 #define RTC6_REG_MONTH 0x05
77 #define RTC6_REG_YEARS 0x06
78 #define RTC6_REG_CONTROL 0x07
79 #define RTC6_REG_OSC_TRIM 0x08
80 #define RTC6_REG_EEUNLOCK 0x09
81 #define RTC6_REG_ALARM0_SECOND 0x0A
82 #define RTC6_REG_ALARM0_MINUTE 0x0B
83 #define RTC6_REG_ALARM0_HOUR 0x0C
84 #define RTC6_REG_ALARM0_DAY 0x0D
85 #define RTC6_REG_ALARM0_DATE 0x0E
86 #define RTC6_REG_ALARM0_MONTH 0x0F
87 #define RTC6_REG_ALARM1_SECOND 0x11
88 #define RTC6_REG_ALARM1_MINUTE 0x12
89 #define RTC6_REG_ALARM1_HOUR 0x13
90 #define RTC6_REG_ALARM1_DAY 0x14
91 #define RTC6_REG_ALARM1_DATE 0x15
92 #define RTC6_REG_ALARM1_MONTH 0x16
93 #define RTC6_REG_POWER_DOWN_MINUTE 0x18
94 #define RTC6_REG_POWER_DOWN_HOUR 0x19
95 #define RTC6_REG_POWER_DOWN_DATE 0x1A
96 #define RTC6_REG_POWER_DOWN_MONTH 0x1B
97 #define RTC6_REG_POWER_UP_MINUTE 0x1C
98 #define RTC6_REG_POWER_UP_HOUR 0x1D
99 #define RTC6_REG_POWER_UP_DATE 0x1E
100 #define RTC6_REG_POWER_UP_MONTH 0x1F
101 
107 #define RTC6_OSCILLATOR_ENABLE 0x80
108 #define RTC6_OSCILLATOR_DISABLE 0x00
109 
115 #define RTC6_FORMAT_12_HOURS 0x40
116 #define RTC6_FORMAT_24_HOURS 0x00
117 
123 #define RTC6_SWO_1HZ 0x01
124 #define RTC6_SWO_4_096KHZ 0x02
125 #define RTC6_SWO_8_192KHZ 0x03
126 #define RTC6_SWO_32_768KHZ 0x04
127 
129 /* Months in Year */
130 #define RTC6_MONTH_JANUARY 0x00
131 #define RTC6_MONTH_FEBRUARY 0x01
132 #define RTC6_MONTH_MARCH 0x02
133 #define RTC6_MONTH_APRIL 0x03
134 #define RTC6_MONTH_MAY 0x04
135 #define RTC6_MONTH_JUNE 0x05
136 #define RTC6_MONTH_JULY 0x06
137 #define RTC6_MONTH_AUGUST 0x07
138 #define RTC6_MONTH_SEPTEMBER 0x08
139 #define RTC6_MONTH_OCTOBER 0x09
140 #define RTC6_MONTH_NOVEMBER 0x0A
141 #define RTC6_MONTH_DECEMBER 0x0B
142 
148 #define RTC6_DAY_MONDAY 0x00
149 #define RTC6_DAY_TUESDAY 0x01
150 #define RTC6_DAY_WEDNESDAY 0x02
151 #define RTC6_DAY_THURSDAY 0x03
152 #define RTC6_DAY_FRIDAY 0x04
153 #define RTC6_DAY_SATURDAY 0x05
154 #define RTC6_DAY_SUNDAY 0x06
155 
161 #define RTC6_DEVICE_SLAVE_ADDR 0x6F
162 #define RTC6_SRAM_SLAVE_ADDR 0xDE
163 #define RTC6_EEPROM_SLAVE_ADDR 0x57
164 
170 #define RTC6_ALARM_0 0x00
171 #define RTC6_ALARM_1 0x01
172 
178 #define RTC6_RETVAL uint8_t
179 
180 #define RTC6_OK 0x00
181 #define RTC6_INIT_ERROR 0xFF
182 
188 #define SET_ERROR 0x02
189 #define DEVICE_ERROR 0x01
190 #define DEVICE_OK 0x00
191 
197 #define RTC6_TIMEDATE_BYTES 0x07
198 #define RTC6_SECONDS_BYTE 0x00
199 #define RTC6_MINUTES_BYTE 0x01
200 #define RTC6_HOUR_BYTE 0x02
201 #define RTC6_DAY_BYTE 0x03
202 #define RTC6_DATE_BYTE 0x04
203 #define RTC6_MONTH_BYTE 0x05
204 #define RTC6_YEAR_BYTE 0x06
205 
211 #define RTC6_RAM_SIZE 64
212 #define RTC6_RAM_START 0x20
213 #define RTC6_RAM_END 0x5f
214 #define RTC6_EEPROM_SIZE 1024
215 #define RTC6_EEPROM_START 0
216 #define RTC6_EEPROM_END 1024
217 #define RTC6_EEPROM_PAGE_SIZE 8
218 #define RTC6_EEPROM_STATUS 0xFF
219 
221 /* Seconds */
226 #define TIME_SEC_IN_MIN 60
227 #define TIME_SEC_IN_HOUR 3600
228 #define TIME_SEC_IN_DAY 86400
229 #define TIME_SEC_IN_MONTH 2629743
230 #define TIME_SEC_IN_YEAR_365 31536000
231 #define TIME_SEC_IN_LEAP_YEAR 31622400
232 #define TIME_SEC_IN_YEAR_365_25 31557600
233  // End group macro
236 // --------------------------------------------------------------- PUBLIC TYPES
245 typedef struct
246 {
247  uint8_t seconds;
248  uint8_t minutes;
249  uint8_t hours;
250  uint8_t weekdays;
251  uint8_t monthday;
252  uint8_t month;
253  uint16_t year;
254 
255 } rtc6_time_t;
256 
260 typedef struct
261 {
262  // Input pins
263 
264  digital_in_t int_pin;
265 
266  // Modules
267 
268  i2c_master_t i2c;
269 
270  // ctx variable
271 
272  uint8_t slave_address;
273 
274  int8_t time_zone;
275 
276 } rtc6_t;
277 
281 typedef struct
282 {
283  // Communication gpio pins
284 
285  pin_name_t scl;
286  pin_name_t sda;
287 
288  // Additional gpio pins
289 
290  pin_name_t int_pin;
291 
292  // static variable
293 
294  uint32_t i2c_speed;
295  uint8_t i2c_address;
296 
297  // Time config value
298 
300 
301 } rtc6_cfg_t;
302  // End types group
304 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
305 
311 #ifdef __cplusplus
312 extern "C"{
313 #endif
314 
324 
334 
342 void rtc6_default_cfg ( rtc6_t *ctx, int8_t time_zone, rtc6_time_t *utc_time, rtc6_time_t *alarm_time );
343 
354 void rtc6_generic_write ( rtc6_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
355 
366 void rtc6_generic_read ( rtc6_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
367 
368 uint8_t rtc6_init_fun ( rtc6_t *ctx, int8_t time_zone );
369 
385 void rtc6_enable_swo ( rtc6_t *ctx, uint8_t swo );
386 
394 void rtc6_disable_swo ( rtc6_t *ctx );
395 
404 
413 void rtc6_get_gmt_time ( rtc6_t *ctx, rtc6_time_t *gmt_time );
414 
423 void rtc6_set_gmt_time ( rtc6_t *ctx, rtc6_time_t *set_time );
424 
433 void rtc6_get_local_time ( rtc6_t *ctx, rtc6_time_t *local_time );
434 
444 uint32_t rtc6_get_gmt_unix_time ( rtc6_t *ctx );
445 
456 
466 uint8_t rtc6_is_leap_year ( rtc6_t *ctx );
467 
477 uint8_t rtc6_is_power_failure ( rtc6_t *ctx );
478 
488 
499 void rtc6_set_alarm ( rtc6_t *ctx, uint8_t alarm, uint8_t trigger, rtc6_time_t *time );
500 
509 void rtc6_disable_alarm ( rtc6_t *ctx, uint8_t alarm );
510 
520 void rtc6_read_alarm ( rtc6_t *ctx, uint8_t alarm, rtc6_time_t *get_alarm );
521 
531 uint8_t rtc6_is_active_alarm( rtc6_t *ctx );
532 
542 void rtc6_repeat_alarm ( rtc6_t *ctx, uint8_t alarm, uint32_t sec );
543 
553 void rtc6_write_sram ( rtc6_t *ctx, uint8_t addr, uint8_t data_in );
554 
565 uint8_t rtc6_read_sram ( rtc6_t *ctx, uint8_t addr );
566 
575 
584 
592 uint8_t rtc6_eeprom_is_locked ( rtc6_t *ctx );
593 
604 void rtc6_write_eeprom ( rtc6_t *ctx, uint8_t addr, uint8_t *data_in, uint8_t data_size );
605 
616 void rtc6_read_eeprom ( rtc6_t *ctx, uint8_t addr, uint8_t *data_out, uint8_t data_size );
617 
627 void rtc6_read_unique_id ( rtc6_t *ctx, uint8_t *unique_id );
628 
629 #ifdef __cplusplus
630 }
631 #endif
632 #endif // _RTC6_H_
633  // End public_function group
636 
637 // ------------------------------------------------------------------------- END
rtc6_time_t::hours
uint8_t hours
Definition: rtc6.h:249
rtc6_write_sram
void rtc6_write_sram(rtc6_t *ctx, uint8_t addr, uint8_t data_in)
Writes data to the SRAM.
rtc6_cfg_t::time_zone_cfg
int8_t time_zone_cfg
Definition: rtc6.h:299
rtc6_is_power_failure
uint8_t rtc6_is_power_failure(rtc6_t *ctx)
Checks if a power failure has occured.
rtc6_t::i2c
i2c_master_t i2c
Definition: rtc6.h:268
rtc6_repeat_alarm
void rtc6_repeat_alarm(rtc6_t *ctx, uint8_t alarm, uint32_t sec)
Repeat Alarm.
rtc6_default_cfg
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.
rtc6_cfg_setup
void rtc6_cfg_setup(rtc6_cfg_t *cfg)
Config Object Initialization function.
rtc6_write_eeprom_protect_on
void rtc6_write_eeprom_protect_on(rtc6_t *ctx)
Write protect on.
rtc6_time_t::monthday
uint8_t monthday
Definition: rtc6.h:251
rtc6_t::time_zone
int8_t time_zone
Definition: rtc6.h:274
rtc6_init
RTC6_RETVAL rtc6_init(rtc6_t *ctx, rtc6_cfg_t *cfg)
Initialization function.
rtc6_eeprom_is_locked
uint8_t rtc6_eeprom_is_locked(rtc6_t *ctx)
Lock check function.
rtc6_generic_write
void rtc6_generic_write(rtc6_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
rtc6_read_alarm
void rtc6_read_alarm(rtc6_t *ctx, uint8_t alarm, rtc6_time_t *get_alarm)
Reads the alarm time.
RTC6_RETVAL
#define RTC6_RETVAL
Definition: rtc6.h:178
rtc6_write_eeprom
void rtc6_write_eeprom(rtc6_t *ctx, uint8_t addr, uint8_t *data_in, uint8_t data_size)
Writes data to EEPROM.
rtc6_time_t
Click time data object.
Definition: rtc6.h:246
rtc6_cfg_t::sda
pin_name_t sda
Definition: rtc6.h:286
rtc6_cfg_t::int_pin
pin_name_t int_pin
Definition: rtc6.h:290
rtc6_cfg_t
Click configuration structure definition.
Definition: rtc6.h:282
rtc6_read_eeprom
void rtc6_read_eeprom(rtc6_t *ctx, uint8_t addr, uint8_t *data_out, uint8_t data_size)
Reads data from EEPROM.
rtc6_disable_swo
void rtc6_disable_swo(rtc6_t *ctx)
Disables Square Wave Output.
rtc6_get_last_power_failure
void rtc6_get_last_power_failure(rtc6_t *ctx, rtc6_time_t *last_time)
Gets the time of the last power failure.
rtc6_get_local_time
void rtc6_get_local_time(rtc6_t *ctx, rtc6_time_t *local_time)
Calculates the current local time.
rtc6_disable_alarm
void rtc6_disable_alarm(rtc6_t *ctx, uint8_t alarm)
Disables the alarm.
rtc6_time_t::minutes
uint8_t minutes
Definition: rtc6.h:248
rtc6_init_fun
uint8_t rtc6_init_fun(rtc6_t *ctx, int8_t time_zone)
rtc6_is_leap_year
uint8_t rtc6_is_leap_year(rtc6_t *ctx)
Checks if the current year is a leap one.
rtc6_time_t::month
uint8_t month
Definition: rtc6.h:252
rtc6_get_gmt_unix_time
uint32_t rtc6_get_gmt_unix_time(rtc6_t *ctx)
Calculates the current GMT time in UNIX epoch time.
rtc6_time_t::seconds
uint8_t seconds
Definition: rtc6.h:247
rtc6_is_active_alarm
uint8_t rtc6_is_active_alarm(rtc6_t *ctx)
Check if the alarm is activated.
rtc6_cfg_t::i2c_address
uint8_t i2c_address
Definition: rtc6.h:295
rtc6_get_gmt_time
void rtc6_get_gmt_time(rtc6_t *ctx, rtc6_time_t *gmt_time)
Gets the current GMT time set in the RTC.
rtc6_set_gmt_time
void rtc6_set_gmt_time(rtc6_t *ctx, rtc6_time_t *set_time)
Sets the UTC GMT time.
rtc6_t::int_pin
digital_in_t int_pin
Definition: rtc6.h:264
rtc6_read_unique_id
void rtc6_read_unique_id(rtc6_t *ctx, uint8_t *unique_id)
Reads unique ID.
rtc6_time_t::year
uint16_t year
Definition: rtc6.h:253
rtc6_set_alarm
void rtc6_set_alarm(rtc6_t *ctx, uint8_t alarm, uint8_t trigger, rtc6_time_t *time)
Sets the desired alarm.
rtc6_t::slave_address
uint8_t slave_address
Definition: rtc6.h:272
rtc6_get_local_unix_time
uint32_t rtc6_get_local_unix_time(rtc6_t *ctx)
Calculates the current local time in UNIX epoch time.
rtc6_t
Click ctx object definition.
Definition: rtc6.h:261
rtc6_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: rtc6.h:294
rtc6_time_t::weekdays
uint8_t weekdays
Definition: rtc6.h:250
rtc6_enable_swo
void rtc6_enable_swo(rtc6_t *ctx, uint8_t swo)
Enable Square Wave Output.
rtc6_read_sram
uint8_t rtc6_read_sram(rtc6_t *ctx, uint8_t addr)
Reads data from an SRAM.
rtc6_write_eeprom_protect_off
void rtc6_write_eeprom_protect_off(rtc6_t *ctx)
Write protect off.
rtc6_generic_read
void rtc6_generic_read(rtc6_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
rtc6_battery_enable
void rtc6_battery_enable(rtc6_t *ctx)
Enables automatic switch to battery.
rtc6_cfg_t::scl
pin_name_t scl
Definition: rtc6.h:285