rtc  2.0.0.0
rtc.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  */
24 
33 // ----------------------------------------------------------------------------
34 
35 #ifndef RTC_H
36 #define RTC_H
37 
38 #include "drv_digital_out.h"
39 #include "drv_digital_in.h"
40 #include "drv_i2c_master.h"
41 
42 
43 // -------------------------------------------------------------- PUBLIC MACROS
53 #define RTC_MAP_MIKROBUS( cfg, mikrobus ) \
54  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
55  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
56  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT );
57 
63 #define RTC_OK 0
64 #define RTC_ERROR -1
65 
71 #define RTC_I2C_ADDRESS_0 0x50
72 #define RTC_I2C_ADDRESS_1 0x51
73 
79 #define RTC_REG_CONTROL 0x00
80 #define RTC_REG_TIME_HUN_SEC 0x01
81 #define RTC_REG_TIME_SEC 0x02
82 #define RTC_REG_TIME_MIN 0x03
83 #define RTC_REG_TIME_HOUR 0x04
84 #define RTC_REG_TIME_DATE_DAY_AND_YEAR 0x05
85 #define RTC_REG_TIME_DAY_OF_THE_WEEK_AND_MONTH 0x06
86 #define RTC_REG_TIMER 0x07
87 #define RTC_REG_TIME_ALARM_CONTROL 0x08
88 #define RTC_REG_TIME_ALARM_HUN_SEC 0x09
89 #define RTC_REG_TIME_ALARM_SEC 0x0A
90 #define RTC_REG_TIME_ALARM_MIN 0x0B
91 #define RTC_REG_TIME_ALARM_HOUR 0x0C
92 #define RTC_REG_TIME_ALARM_DATE_DAY_AND_YEAR 0x0D
93 #define RTC_REG_TIME_ALARM_DAY_OF_THE_WEEK_AND_MONTH 0x0E
94 #define RTC_REG_TIME_ALARM_TIMER 0x0F
95  // End group macro
98 // --------------------------------------------------------------- PUBLIC TYPES
107 typedef struct
108 {
110  uint8_t date_day;
111  uint8_t date_month;
112  uint16_t date_year;
113 
114 } rtc_date_t;
115 
119 typedef struct
120 {
121  uint8_t time_hours;
122  uint8_t time_minutes;
123  uint8_t time_seconds;
124  int8_t time_hun_sec;
125 
126 } rtc_time_t;
127 
131 typedef struct
132 {
133  // Input pins
134  digital_in_t int_pin;
135 
136  // Modules
137  i2c_master_t i2c;
138 
139  // I2C slave address
140  uint8_t slave_address;
142  // time and date data
145 
146 } rtc_t;
147 
151 typedef struct
152 {
153  // Communication gpio pins
154  pin_name_t scl;
155  pin_name_t sda;
156 
157  // Additional gpio pins
158  pin_name_t int_pin;
159 
160  // static variable
161  uint32_t i2c_speed;
162  uint8_t i2c_address;
164 } rtc_cfg_t;
165 
166 
167  // End types group
169 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
170 
176 #ifdef __cplusplus
177 extern "C"{
178 #endif
179 
188 void rtc_cfg_setup ( rtc_cfg_t *cfg );
189 
197 err_t rtc_init ( rtc_t *ctx, rtc_cfg_t *cfg );
198 
213 err_t rtc_generic_write ( rtc_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
214 
229 err_t rtc_generic_read ( rtc_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
230 
239 void rtc_enable_disable_counting ( rtc_t *ctx, uint8_t en_dis );
240 
249 void rtc_read_time ( rtc_t *ctx );
250 
259 void rtc_read_date ( rtc_t *ctx );
260 
273 uint8_t rtc_get_time_value ( rtc_t *ctx, uint8_t time_part );
274 
285 void rtc_set_time_value ( rtc_t *ctx, uint8_t time_part, uint8_t time_addr );
286 
297 uint8_t rtc_get_date_day ( rtc_t *ctx );
298 
308 void rtc_set_date_day ( rtc_t *ctx, uint8_t date_day );
309 
320 uint8_t rtc_get_date_year ( rtc_t *ctx );
321 
331 void rtc_set_date_year ( rtc_t *ctx, uint16_t date_year );
332 
347 uint8_t rtc_check_leap_year ( rtc_t *ctx );
348 
360 
379 void rtc_set_day_of_the_week ( rtc_t *ctx, uint8_t w_day );
380 
391 uint8_t rtc_get_date_month ( rtc_t *ctx );
392 
402 void rtc_set_date_month ( rtc_t *ctx, uint8_t date_month );
403 
412 void rtc_set_time ( rtc_t *ctx );
413 
422 void rtc_get_time ( rtc_t *ctx );
423 
432 void rtc_set_date ( rtc_t *ctx );
433 
442 void rtc_get_date ( rtc_t *ctx );
443 
453 void rtc_enable_disable_alarm ( rtc_t *ctx, uint8_t en_dis );
454 
465 void rtc_set_alarm_value ( rtc_t *ctx, uint8_t al_time_val, uint8_t al_time_type );
466 
479 uint8_t rtc_get_alarm_value ( rtc_t *ctx, uint8_t alarm_addr );
480 
489 void rtc_set_time_alarm ( rtc_t *ctx );
490 
499 void rtc_get_time_alarm ( rtc_t *ctx );
500 
510 void rtc_enable_disable_interrupt( rtc_t *ctx, uint8_t flag );
511 
524 uint8_t rtc_get_interrupt( rtc_t *ctx );
525 
526 #ifdef __cplusplus
527 }
528 #endif
529 #endif // _RTC_H_
530  // End public_function group
533 
534 // ------------------------------------------------------------------------- END
rtc_set_date_year
void rtc_set_date_year(rtc_t *ctx, uint16_t date_year)
Set year function.
rtc_get_alarm_value
uint8_t rtc_get_alarm_value(rtc_t *ctx, uint8_t alarm_addr)
Get alarm time value function.
rtc_t::time
rtc_time_t time
Definition: rtc.h:144
rtc_cfg_setup
void rtc_cfg_setup(rtc_cfg_t *cfg)
Config Object Initialization function.
rtc_enable_disable_alarm
void rtc_enable_disable_alarm(rtc_t *ctx, uint8_t en_dis)
Enable/Disable alarm function.
rtc_set_time_value
void rtc_set_time_value(rtc_t *ctx, uint8_t time_part, uint8_t time_addr)
Set time values function.
rtc_cfg_t::scl
pin_name_t scl
Definition: rtc.h:154
rtc_time_t
Time data structure.
Definition: rtc.h:120
rtc_cfg_t::sda
pin_name_t sda
Definition: rtc.h:155
rtc_enable_disable_counting
void rtc_enable_disable_counting(rtc_t *ctx, uint8_t en_dis)
Enable/Disable counting function.
rtc_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: rtc.h:161
rtc_time_t::time_hours
uint8_t time_hours
Definition: rtc.h:121
rtc_get_time_value
uint8_t rtc_get_time_value(rtc_t *ctx, uint8_t time_part)
Get time value function.
rtc_get_interrupt
uint8_t rtc_get_interrupt(rtc_t *ctx)
Get state of interrupt pin function.
rtc_time_t::time_hun_sec
int8_t time_hun_sec
Definition: rtc.h:124
rtc_init
err_t rtc_init(rtc_t *ctx, rtc_cfg_t *cfg)
Initialization function.
rtc_date_t
Date data structure.
Definition: rtc.h:108
rtc_date_t::date_month
uint8_t date_month
Definition: rtc.h:111
rtc_t
Click ctx object definition.
Definition: rtc.h:132
rtc_generic_read
err_t rtc_generic_read(rtc_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
RTC I2C reading function.
rtc_cfg_t::int_pin
pin_name_t int_pin
Definition: rtc.h:158
rtc_get_date_month
uint8_t rtc_get_date_month(rtc_t *ctx)
Get month function.
rtc_set_time
void rtc_set_time(rtc_t *ctx)
Set time hours, minutes, seconds and hundredth of a seconds function.
rtc_get_day_of_the_week
uint8_t rtc_get_day_of_the_week(rtc_t *ctx)
Get day of the week function.
rtc_cfg_t
Click configuration structure definition.
Definition: rtc.h:152
rtc_set_date_day
void rtc_set_date_day(rtc_t *ctx, uint8_t date_day)
Set day function.
rtc_date_t::date_year
uint16_t date_year
Definition: rtc.h:112
rtc_read_time
void rtc_read_time(rtc_t *ctx)
Set control register to read time function.
rtc_t::i2c
i2c_master_t i2c
Definition: rtc.h:137
rtc_get_date
void rtc_get_date(rtc_t *ctx)
Get time hours, minutes and seconds function.
rtc_cfg_t::i2c_address
uint8_t i2c_address
Definition: rtc.h:162
rtc_t::int_pin
digital_in_t int_pin
Definition: rtc.h:134
rtc_read_date
void rtc_read_date(rtc_t *ctx)
Set control register to read date function.
rtc_get_date_year
uint8_t rtc_get_date_year(rtc_t *ctx)
Get year function.
rtc_set_date
void rtc_set_date(rtc_t *ctx)
Set date hours, minutes and seconds function.
rtc_get_time
void rtc_get_time(rtc_t *ctx)
Get time hours, minutes, seconds and hundredth of a seconds function.
rtc_get_date_day
uint8_t rtc_get_date_day(rtc_t *ctx)
Get day function.
rtc_enable_disable_interrupt
void rtc_enable_disable_interrupt(rtc_t *ctx, uint8_t flag)
Enable alarm interrupt function.
rtc_set_time_alarm
void rtc_set_time_alarm(rtc_t *ctx)
Set alarm time - minutes function.
rtc_date_t::day_of_the_week
uint8_t day_of_the_week
Definition: rtc.h:109
rtc_t::slave_address
uint8_t slave_address
Definition: rtc.h:140
rtc_generic_write
err_t rtc_generic_write(rtc_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
RTC I2C writing function.
rtc_t::date
rtc_date_t date
Definition: rtc.h:143
rtc_time_t::time_minutes
uint8_t time_minutes
Definition: rtc.h:122
rtc_set_date_month
void rtc_set_date_month(rtc_t *ctx, uint8_t date_month)
Set month function.
rtc_get_time_alarm
void rtc_get_time_alarm(rtc_t *ctx)
Get the alarm time hours, minutes, seconds and hundredth of a seconds function.
rtc_check_leap_year
uint8_t rtc_check_leap_year(rtc_t *ctx)
Check if it's a leap year function.
rtc_set_day_of_the_week
void rtc_set_day_of_the_week(rtc_t *ctx, uint8_t w_day)
Set day of the week function.
rtc_date_t::date_day
uint8_t date_day
Definition: rtc.h:110
rtc_set_alarm_value
void rtc_set_alarm_value(rtc_t *ctx, uint8_t al_time_val, uint8_t al_time_type)
Set alarm time value.
rtc_time_t::time_seconds
uint8_t time_seconds
Definition: rtc.h:123