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_C_H
36 #define RTC_C_H
37 
38 #include "mikrosdk_version.h"
39 
40 #ifdef __GNUC__
41 #if mikroSDK_GET_VERSION < 20800ul
42 #include "rcu_delays.h"
43 #else
44 #include "delays.h"
45 #endif
46 #endif
47 
48 #include "drv_digital_out.h"
49 #include "drv_digital_in.h"
50 #include "drv_i2c_master.h"
51 
52 
53 // -------------------------------------------------------------- PUBLIC MACROS
63 #define RTC_C_MAP_MIKROBUS( cfg, mikrobus ) \
64  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
65  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
66  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT );
67 
73 #define RTC_C_OK 0
74 #define RTC_C_ERROR -1
75 
81 #define RTC_C_I2C_ADDRESS_0 0x50
82 #define RTC_C_I2C_ADDRESS_1 0x51
83 
89 #define RTC_C_REG_CONTROL 0x00
90 #define RTC_C_REG_TIME_HUN_SEC 0x01
91 #define RTC_C_REG_TIME_SEC 0x02
92 #define RTC_C_REG_TIME_MIN 0x03
93 #define RTC_C_REG_TIME_HOUR 0x04
94 #define RTC_C_REG_TIME_DATE_DAY_AND_YEAR 0x05
95 #define RTC_C_REG_TIME_DAY_OF_THE_WEEK_AND_MONTH 0x06
96 #define RTC_C_REG_TIMER 0x07
97 #define RTC_C_REG_TIME_ALARM_CONTROL 0x08
98 #define RTC_C_REG_TIME_ALARM_HUN_SEC 0x09
99 #define RTC_C_REG_TIME_ALARM_SEC 0x0A
100 #define RTC_C_REG_TIME_ALARM_MIN 0x0B
101 #define RTC_C_REG_TIME_ALARM_HOUR 0x0C
102 #define RTC_C_REG_TIME_ALARM_DATE_DAY_AND_YEAR 0x0D
103 #define RTC_C_REG_TIME_ALARM_DAY_OF_THE_WEEK_AND_MONTH 0x0E
104 #define RTC_C_REG_TIME_ALARM_TIMER 0x0F
105  // End group macro
108 // --------------------------------------------------------------- PUBLIC TYPES
117 typedef struct
118 {
120  uint8_t date_day;
121  uint8_t date_month;
122  uint16_t date_year;
123 
124 } rtc_c_date_t;
125 
129 typedef struct
130 {
131  uint8_t time_hours;
132  uint8_t time_minutes;
133  uint8_t time_seconds;
134  int8_t time_hun_sec;
135 
136 } rtc_c_time_t;
137 
141 typedef struct
142 {
143  // Input pins
144  digital_in_t int_pin;
145 
146  // Modules
147  i2c_master_t i2c;
148 
149  // I2C slave address
150  uint8_t slave_address;
152  // time and date data
155 
156 } rtc_c_t;
157 
161 typedef struct
162 {
163  // Communication gpio pins
164  pin_name_t scl;
165  pin_name_t sda;
166 
167  // Additional gpio pins
168  pin_name_t int_pin;
169 
170  // static variable
171  uint32_t i2c_speed;
172  uint8_t i2c_address;
174 } rtc_c_cfg_t;
175 
176 
177  // End types group
179 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
180 
186 #ifdef __cplusplus
187 extern "C"{
188 #endif
189 
199 
207 err_t rtc_c_init ( rtc_c_t *ctx, rtc_c_cfg_t *cfg );
208 
223 err_t rtc_c_generic_write ( rtc_c_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
224 
239 err_t rtc_c_generic_read ( rtc_c_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
240 
249 void rtc_c_enable_disable_counting ( rtc_c_t *ctx, uint8_t en_dis );
250 
259 void rtc_c_read_time ( rtc_c_t *ctx );
260 
269 void rtc_c_read_date ( rtc_c_t *ctx );
270 
283 uint8_t rtc_c_get_time_value ( rtc_c_t *ctx, uint8_t time_part );
284 
295 void rtc_c_set_time_value ( rtc_c_t *ctx, uint8_t time_part, uint8_t time_addr );
296 
307 uint8_t rtc_c_get_date_day ( rtc_c_t *ctx );
308 
318 void rtc_c_set_date_day ( rtc_c_t *ctx, uint8_t date_day );
319 
330 uint8_t rtc_c_get_date_year ( rtc_c_t *ctx );
331 
341 void rtc_c_set_date_year ( rtc_c_t *ctx, uint16_t date_year );
342 
358 
370 
389 void rtc_c_set_day_of_the_week ( rtc_c_t *ctx, uint8_t w_day );
390 
401 uint8_t rtc_c_get_date_month ( rtc_c_t *ctx );
402 
412 void rtc_c_set_date_month ( rtc_c_t *ctx, uint8_t date_month );
413 
422 void rtc_c_set_time ( rtc_c_t *ctx );
423 
432 void rtc_c_get_time ( rtc_c_t *ctx );
433 
442 void rtc_c_set_date ( rtc_c_t *ctx );
443 
452 void rtc_c_get_date ( rtc_c_t *ctx );
453 
463 void rtc_c_enable_disable_alarm ( rtc_c_t *ctx, uint8_t en_dis );
464 
475 void rtc_c_set_alarm_value ( rtc_c_t *ctx, uint8_t al_time_val, uint8_t al_time_type );
476 
489 uint8_t rtc_c_get_alarm_value ( rtc_c_t *ctx, uint8_t alarm_addr );
490 
500 
510 
520 void rtc_c_enable_disable_interrupt( rtc_c_t *ctx, uint8_t flag );
521 
534 uint8_t rtc_c_get_interrupt( rtc_c_t *ctx );
535 
536 #ifdef __cplusplus
537 }
538 #endif
539 #endif // _RTC_C_H_
540  // End public_function group
543 
544 // ------------------------------------------------------------------------- END
rtc_c_get_time
void rtc_c_get_time(rtc_c_t *ctx)
Get time hours, minutes, seconds and hundredth of a seconds function.
rtc_c_cfg_t::int_pin
pin_name_t int_pin
Definition: rtc.h:168
rtc_c_set_day_of_the_week
void rtc_c_set_day_of_the_week(rtc_c_t *ctx, uint8_t w_day)
Set day of the week function.
rtc_c_get_date_month
uint8_t rtc_c_get_date_month(rtc_c_t *ctx)
Get month function.
rtc_c_date_t::date_month
uint8_t date_month
Definition: rtc.h:121
rtc_c_read_time
void rtc_c_read_time(rtc_c_t *ctx)
Set control register to read time function.
rtc_c_t::date
rtc_c_date_t date
Definition: rtc.h:153
rtc_c_set_date
void rtc_c_set_date(rtc_c_t *ctx)
Set date hours, minutes and seconds function.
rtc_c_set_date_month
void rtc_c_set_date_month(rtc_c_t *ctx, uint8_t date_month)
Set month function.
rtc_c_date_t::day_of_the_week
uint8_t day_of_the_week
Definition: rtc.h:119
rtc_c_set_alarm_value
void rtc_c_set_alarm_value(rtc_c_t *ctx, uint8_t al_time_val, uint8_t al_time_type)
Set alarm time value.
rtc_c_t::time
rtc_c_time_t time
Definition: rtc.h:154
rtc_c_set_date_year
void rtc_c_set_date_year(rtc_c_t *ctx, uint16_t date_year)
Set year function.
rtc_c_time_t::time_minutes
uint8_t time_minutes
Definition: rtc.h:132
rtc_c_time_t::time_hours
uint8_t time_hours
Definition: rtc.h:131
rtc_c_set_time_value
void rtc_c_set_time_value(rtc_c_t *ctx, uint8_t time_part, uint8_t time_addr)
Set time values function.
rtc_c_generic_write
err_t rtc_c_generic_write(rtc_c_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
RTC I2C writing function.
rtc_c_date_t
Date data structure.
Definition: rtc.h:118
rtc_c_t::slave_address
uint8_t slave_address
Definition: rtc.h:150
rtc_c_get_date_year
uint8_t rtc_c_get_date_year(rtc_c_t *ctx)
Get year function.
rtc_c_cfg_t::sda
pin_name_t sda
Definition: rtc.h:165
rtc_c_check_leap_year
uint8_t rtc_c_check_leap_year(rtc_c_t *ctx)
Check if it's a leap year function.
rtc_c_time_t::time_seconds
uint8_t time_seconds
Definition: rtc.h:133
rtc_c_date_t::date_day
uint8_t date_day
Definition: rtc.h:120
rtc_c_get_alarm_value
uint8_t rtc_c_get_alarm_value(rtc_c_t *ctx, uint8_t alarm_addr)
Get alarm time value function.
rtc_c_cfg_t
Click configuration structure definition.
Definition: rtc.h:162
rtc_c_init
err_t rtc_c_init(rtc_c_t *ctx, rtc_c_cfg_t *cfg)
Initialization function.
rtc_c_enable_disable_interrupt
void rtc_c_enable_disable_interrupt(rtc_c_t *ctx, uint8_t flag)
Enable alarm interrupt function.
rtc_c_get_interrupt
uint8_t rtc_c_get_interrupt(rtc_c_t *ctx)
Get state of interrupt pin function.
rtc_c_get_time_alarm
void rtc_c_get_time_alarm(rtc_c_t *ctx)
Get the alarm time hours, minutes, seconds and hundredth of a seconds function.
rtc_c_cfg_setup
void rtc_c_cfg_setup(rtc_c_cfg_t *cfg)
Config Object Initialization function.
rtc_c_cfg_t::i2c_address
uint8_t i2c_address
Definition: rtc.h:172
rtc_c_t::int_pin
digital_in_t int_pin
Definition: rtc.h:144
rtc_c_get_date_day
uint8_t rtc_c_get_date_day(rtc_c_t *ctx)
Get day function.
rtc_c_get_time_value
uint8_t rtc_c_get_time_value(rtc_c_t *ctx, uint8_t time_part)
Get time value function.
rtc_c_set_time
void rtc_c_set_time(rtc_c_t *ctx)
Set time hours, minutes, seconds and hundredth of a seconds function.
rtc_c_date_t::date_year
uint16_t date_year
Definition: rtc.h:122
rtc_c_time_t::time_hun_sec
int8_t time_hun_sec
Definition: rtc.h:134
rtc_c_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: rtc.h:171
rtc_c_get_date
void rtc_c_get_date(rtc_c_t *ctx)
Get time hours, minutes and seconds function.
rtc_c_enable_disable_counting
void rtc_c_enable_disable_counting(rtc_c_t *ctx, uint8_t en_dis)
Enable/Disable counting function.
rtc_c_t::i2c
i2c_master_t i2c
Definition: rtc.h:147
rtc_c_time_t
Time data structure.
Definition: rtc.h:130
rtc_c_t
Click ctx object definition.
Definition: rtc.h:142
rtc_c_enable_disable_alarm
void rtc_c_enable_disable_alarm(rtc_c_t *ctx, uint8_t en_dis)
Enable/Disable alarm function.
rtc_c_read_date
void rtc_c_read_date(rtc_c_t *ctx)
Set control register to read date function.
rtc_c_set_date_day
void rtc_c_set_date_day(rtc_c_t *ctx, uint8_t date_day)
Set day function.
rtc_c_set_time_alarm
void rtc_c_set_time_alarm(rtc_c_t *ctx)
Set alarm time - minutes function.
rtc_c_get_day_of_the_week
uint8_t rtc_c_get_day_of_the_week(rtc_c_t *ctx)
Get day of the week function.
rtc_c_cfg_t::scl
pin_name_t scl
Definition: rtc.h:164
rtc_c_generic_read
err_t rtc_c_generic_read(rtc_c_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
RTC I2C reading function.