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 
42 #ifdef PREINIT_SUPPORTED
43 #include "preinit.h"
44 #endif
45 
46 #ifdef MikroCCoreVersion
47  #if MikroCCoreVersion >= 1
48  #include "delays.h"
49  #endif
50 #endif
51 
52 #include "drv_digital_out.h"
53 #include "drv_digital_in.h"
54 #include "drv_i2c_master.h"
55 
56 
57 // -------------------------------------------------------------- PUBLIC MACROS
67 #define RTC_C_MAP_MIKROBUS( cfg, mikrobus ) \
68  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
69  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
70  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT );
71 
77 #define RTC_C_OK 0
78 #define RTC_C_ERROR -1
79 
85 #define RTC_C_I2C_ADDRESS_0 0x50
86 #define RTC_C_I2C_ADDRESS_1 0x51
87 
93 #define RTC_C_REG_CONTROL 0x00
94 #define RTC_C_REG_TIME_HUN_SEC 0x01
95 #define RTC_C_REG_TIME_SEC 0x02
96 #define RTC_C_REG_TIME_MIN 0x03
97 #define RTC_C_REG_TIME_HOUR 0x04
98 #define RTC_C_REG_TIME_DATE_DAY_AND_YEAR 0x05
99 #define RTC_C_REG_TIME_DAY_OF_THE_WEEK_AND_MONTH 0x06
100 #define RTC_C_REG_TIMER 0x07
101 #define RTC_C_REG_TIME_ALARM_CONTROL 0x08
102 #define RTC_C_REG_TIME_ALARM_HUN_SEC 0x09
103 #define RTC_C_REG_TIME_ALARM_SEC 0x0A
104 #define RTC_C_REG_TIME_ALARM_MIN 0x0B
105 #define RTC_C_REG_TIME_ALARM_HOUR 0x0C
106 #define RTC_C_REG_TIME_ALARM_DATE_DAY_AND_YEAR 0x0D
107 #define RTC_C_REG_TIME_ALARM_DAY_OF_THE_WEEK_AND_MONTH 0x0E
108 #define RTC_C_REG_TIME_ALARM_TIMER 0x0F
109  // End group macro
112 // --------------------------------------------------------------- PUBLIC TYPES
121 typedef struct
122 {
124  uint8_t date_day;
125  uint8_t date_month;
126  uint16_t date_year;
127 
128 } rtc_c_date_t;
129 
133 typedef struct
134 {
135  uint8_t time_hours;
136  uint8_t time_minutes;
137  uint8_t time_seconds;
138  int8_t time_hun_sec;
139 
140 } rtc_c_time_t;
141 
145 typedef struct
146 {
147  // Input pins
148  digital_in_t int_pin;
149 
150  // Modules
151  i2c_master_t i2c;
152 
153  // I2C slave address
154  uint8_t slave_address;
156  // time and date data
159 
160 } rtc_c_t;
161 
165 typedef struct
166 {
167  // Communication gpio pins
168  pin_name_t scl;
169  pin_name_t sda;
170 
171  // Additional gpio pins
172  pin_name_t int_pin;
173 
174  // static variable
175  uint32_t i2c_speed;
176  uint8_t i2c_address;
178 } rtc_c_cfg_t;
179 
180 
181  // End types group
183 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
184 
190 #ifdef __cplusplus
191 extern "C"{
192 #endif
193 
203 
211 err_t rtc_c_init ( rtc_c_t *ctx, rtc_c_cfg_t *cfg );
212 
227 err_t rtc_c_generic_write ( rtc_c_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
228 
243 err_t rtc_c_generic_read ( rtc_c_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
244 
253 void rtc_c_enable_disable_counting ( rtc_c_t *ctx, uint8_t en_dis );
254 
263 void rtc_c_read_time ( rtc_c_t *ctx );
264 
273 void rtc_c_read_date ( rtc_c_t *ctx );
274 
287 uint8_t rtc_c_get_time_value ( rtc_c_t *ctx, uint8_t time_part );
288 
299 void rtc_c_set_time_value ( rtc_c_t *ctx, uint8_t time_part, uint8_t time_addr );
300 
311 uint8_t rtc_c_get_date_day ( rtc_c_t *ctx );
312 
322 void rtc_c_set_date_day ( rtc_c_t *ctx, uint8_t date_day );
323 
334 uint8_t rtc_c_get_date_year ( rtc_c_t *ctx );
335 
345 void rtc_c_set_date_year ( rtc_c_t *ctx, uint16_t date_year );
346 
362 
374 
393 void rtc_c_set_day_of_the_week ( rtc_c_t *ctx, uint8_t w_day );
394 
405 uint8_t rtc_c_get_date_month ( rtc_c_t *ctx );
406 
416 void rtc_c_set_date_month ( rtc_c_t *ctx, uint8_t date_month );
417 
426 void rtc_c_set_time ( rtc_c_t *ctx );
427 
436 void rtc_c_get_time ( rtc_c_t *ctx );
437 
446 void rtc_c_set_date ( rtc_c_t *ctx );
447 
456 void rtc_c_get_date ( rtc_c_t *ctx );
457 
467 void rtc_c_enable_disable_alarm ( rtc_c_t *ctx, uint8_t en_dis );
468 
479 void rtc_c_set_alarm_value ( rtc_c_t *ctx, uint8_t al_time_val, uint8_t al_time_type );
480 
493 uint8_t rtc_c_get_alarm_value ( rtc_c_t *ctx, uint8_t alarm_addr );
494 
504 
514 
524 void rtc_c_enable_disable_interrupt( rtc_c_t *ctx, uint8_t flag );
525 
538 uint8_t rtc_c_get_interrupt( rtc_c_t *ctx );
539 
540 #ifdef __cplusplus
541 }
542 #endif
543 #endif // _RTC_C_H_
544  // End public_function group
547 
548 // ------------------------------------------------------------------------- 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:172
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:125
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:157
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:123
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:158
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:136
rtc_c_time_t::time_hours
uint8_t time_hours
Definition: rtc.h:135
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:122
rtc_c_t::slave_address
uint8_t slave_address
Definition: rtc.h:154
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:169
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:137
rtc_c_date_t::date_day
uint8_t date_day
Definition: rtc.h:124
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:166
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:176
rtc_c_t::int_pin
digital_in_t int_pin
Definition: rtc.h:148
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:126
rtc_c_time_t::time_hun_sec
int8_t time_hun_sec
Definition: rtc.h:138
rtc_c_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: rtc.h:175
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:151
rtc_c_time_t
Time data structure.
Definition: rtc.h:134
rtc_c_t
Click ctx object definition.
Definition: rtc.h:146
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:168
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.