rtc2  2.0.0.0
rtc2.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 RTC2_H
36 #define RTC2_H
37 
38 #include "drv_digital_out.h"
39 #include "drv_digital_in.h"
40 #include "drv_i2c_master.h"
41 
42 // -------------------------------------------------------------- PUBLIC MACROS
52 #define RTC2_MAP_MIKROBUS( cfg, mikrobus ) \
53  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
54  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
55  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
56 
62 #define RTC2_RETVAL uint8_t
63 
64 #define RTC2_OK 0x00
65 #define RTC2_INIT_ERROR 0xFF
66 
72 #define RTC2_I2C_ADDRESS 0x68
73 #define RTC2_REG_TIME_SEC 0x00
74 #define RTC2_REG_TIME_MIN 0x01
75 #define RTC2_REG_TIME_HOUR 0x02
76 #define RTC2_REG_TIME_DAY_OF_THE_WEEK 0x03
77 #define RTC2_REG_TIME_DATE_DAY 0x04
78 #define RTC2_REG_TIME_DATE_MONTH 0x05
79 #define RTC2_REG_TIME_DATE_YEAR 0x06
80 #define RTC2_REG_CONTROL 0x07
81 
87 #define RTC2_ENABLE_COUNTING 0x7F
88 #define RTC2_DISABLE_COUNTING 0x80
89 #define RTC2_CONFIG_RATE_LOW 0x00
90 #define RTC2_CONFIG_RATE_HIGH 0x80
91 #define RTC2_CONFIG_RATE_SELECT_1_HZ 0x10
92 #define RTC2_CONFIG_RATE_SELECT_4_096_kHZ 0x11
93 #define RTC2_CONFIG_RATE_SELECT_8_192_kHZ 0x12
94 #define RTC2_CONFIG_RATE_SELECT_32_768_kHZ 0x13
95  // End group macro
98 // --------------------------------------------------------------- PUBLIC TYPES
107 typedef struct
108 {
109  // Output pins
110 
111  // Input pins
112 
113  digital_in_t int_pin;
114 
115  // Modules
116 
117  i2c_master_t i2c;
118 
119  // ctx variable
120 
121  uint8_t slave_address;
122 
123 } rtc2_t;
124 
128 typedef struct
129 {
131  uint8_t date_day;
132  uint8_t date_month;
133  uint16_t date_year;
134 } rtc2_data_t;
135 
139 typedef struct
140 {
141  // Communication gpio pins
142 
143  pin_name_t scl;
144  pin_name_t sda;
145 
146  // Additional gpio pins
147 
148  pin_name_t int_pin;
149 
150  // static variable
151 
152  uint32_t i2c_speed;
153  uint8_t i2c_address;
154 
155 } rtc2_cfg_t;
156  // End types group
158 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
159 
165 #ifdef __cplusplus
166 extern "C"{
167 #endif
168 
177 void rtc2_cfg_setup ( rtc2_cfg_t *cfg );
178 
187 RTC2_RETVAL rtc2_init ( rtc2_t *ctx, rtc2_cfg_t *cfg );
188 
199 void rtc2_generic_write ( rtc2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
200 
211 void rtc2_generic_read ( rtc2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
212 
222  uint8_t rtc2_read_byte ( rtc2_t *ctx, uint8_t reg_address );
223 
234  void rtc2_write_byte ( rtc2_t *ctx, uint8_t reg_address, uint8_t write_data );
235 
242  void rtc2_enable_counting ( rtc2_t *ctx );
243 
251  void rtc2_disable_counting ( rtc2_t *ctx );
252 
261  uint8_t rtc2_get_time_seconds ( rtc2_t *ctx ) ;
262 
272  void rtc2_set_time_seconds ( rtc2_t *ctx, uint8_t seconds );
273 
282  uint8_t rtc2_get_time_minutes ( rtc2_t *ctx );
283 
293  void rtc2_set_time_minutes ( rtc2_t *ctx, uint8_t minutes );
294 
303  uint8_t rtc2_get_time_hours ( rtc2_t *ctx );
304 
314  void rtc2_set_time_hours ( rtc2_t *ctx, uint8_t hours );
315 
324  uint8_t rtc2_get_day_of_the_week ( rtc2_t *ctx );
325 
335  void rtc2_set_day_of_the_week ( rtc2_t *ctx, uint8_t w_day );
336 
345  uint8_t rtc2_get_date_day ( rtc2_t *ctx );
346 
356  void rtc2_set_date_day ( rtc2_t *ctx, uint8_t date_day );
357 
366  uint8_t rtc2_get_date_month ( rtc2_t *ctx );
367 
377  void rtc2_set_date_month ( rtc2_t *ctx, uint8_t date_month );
378 
387  uint8_t rtc2_get_date_year ( rtc2_t *ctx );
388 
398  void rtc2_set_date_year ( rtc2_t *ctx, uint16_t date_year );
399 
411  void rtc2_set_time ( rtc2_t *ctx, uint8_t time_hours, uint8_t time_minutes, uint8_t time_seconds );
412 
424  void rtc2_get_time ( rtc2_t *ctx, uint8_t *time_hours, uint8_t *time_minutes, uint8_t *time_seconds );
425 
435  void rtc2_set_date ( rtc2_t *ctx, rtc2_data_t *date );
436 
446  void rtc2_get_date ( rtc2_t *ctx, rtc2_data_t *date );
447 
456  void rtc2_set_frequency_sqwe ( rtc2_t *ctx, uint8_t rate_select );
457 
458 
459 #ifdef __cplusplus
460 }
461 #endif
462 #endif // _RTC2_H_
463  // End public_function group
466 
467 // ------------------------------------------------------------------------- END
rtc2_t::slave_address
uint8_t slave_address
Definition: rtc2.h:121
rtc2_set_frequency_sqwe
void rtc2_set_frequency_sqwe(rtc2_t *ctx, uint8_t rate_select)
Set frequency of square-wave output function.
rtc2_get_date_year
uint8_t rtc2_get_date_year(rtc2_t *ctx)
Get year function.
rtc2_get_time_hours
uint8_t rtc2_get_time_hours(rtc2_t *ctx)
Set hours function.
rtc2_get_time_seconds
uint8_t rtc2_get_time_seconds(rtc2_t *ctx)
Get seconds function.
rtc2_set_day_of_the_week
void rtc2_set_day_of_the_week(rtc2_t *ctx, uint8_t w_day)
Generic read function.
rtc2_get_time
void rtc2_get_time(rtc2_t *ctx, uint8_t *time_hours, uint8_t *time_minutes, uint8_t *time_seconds)
Get time hours, minutes and seconds function.
date_year
uint16_t date_year
Definition: main.c:48
rtc2_generic_read
void rtc2_generic_read(rtc2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
rtc2_write_byte
void rtc2_write_byte(rtc2_t *ctx, uint8_t reg_address, uint8_t write_data)
Generic write byte of data function.
date_day
uint8_t date_day
Definition: main.c:46
rtc2_generic_write
void rtc2_generic_write(rtc2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
rtc2_init
RTC2_RETVAL rtc2_init(rtc2_t *ctx, rtc2_cfg_t *cfg)
Initialization function.
rtc2_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: rtc2.h:152
rtc2_set_date_year
void rtc2_set_date_year(rtc2_t *ctx, uint16_t date_year)
Set year function.
rtc2_data_t::day_of_the_week
uint8_t day_of_the_week
Definition: rtc2.h:130
rtc2_set_time_minutes
void rtc2_set_time_minutes(rtc2_t *ctx, uint8_t minutes)
Set minutes function.
rtc2_cfg_t
Click configuration structure definition.
Definition: rtc2.h:139
rtc2_set_time_seconds
void rtc2_set_time_seconds(rtc2_t *ctx, uint8_t seconds)
Set seconds function.
rtc2_set_date
void rtc2_set_date(rtc2_t *ctx, rtc2_data_t *date)
Generic read function.
rtc2_get_date
void rtc2_get_date(rtc2_t *ctx, rtc2_data_t *date)
Generic read function.
rtc2_read_byte
uint8_t rtc2_read_byte(rtc2_t *ctx, uint8_t reg_address)
Generic read byte of data function.
rtc2_cfg_setup
void rtc2_cfg_setup(rtc2_cfg_t *cfg)
Config Object Initialization function.
rtc2_get_time_minutes
uint8_t rtc2_get_time_minutes(rtc2_t *ctx)
Get hours function.
rtc2_disable_counting
void rtc2_disable_counting(rtc2_t *ctx)
Disable counting function.
rtc2_cfg_t::scl
pin_name_t scl
Definition: rtc2.h:143
rtc2_t::int_pin
digital_in_t int_pin
Definition: rtc2.h:113
rtc2_enable_counting
void rtc2_enable_counting(rtc2_t *ctx)
Enable counting function.
rtc2_set_time
void rtc2_set_time(rtc2_t *ctx, uint8_t time_hours, uint8_t time_minutes, uint8_t time_seconds)
Set time hours, minutes and seconds function.
rtc2_data_t
Data structure definition.
Definition: rtc2.h:128
rtc2_get_date_month
uint8_t rtc2_get_date_month(rtc2_t *ctx)
Get month function.
rtc2_set_date_month
void rtc2_set_date_month(rtc2_t *ctx, uint8_t date_month)
Set month function.
time_seconds
uint8_t time_seconds
Definition: main.c:44
rtc2_get_day_of_the_week
uint8_t rtc2_get_day_of_the_week(rtc2_t *ctx)
Set day of the week function.
time_minutes
uint8_t time_minutes
Definition: main.c:43
rtc2_get_date_day
uint8_t rtc2_get_date_day(rtc2_t *ctx)
Get day function.
rtc2_set_date_day
void rtc2_set_date_day(rtc2_t *ctx, uint8_t date_day)
Set day function.
rtc2_cfg_t::int_pin
pin_name_t int_pin
Definition: rtc2.h:148
rtc2_data_t::date_month
uint8_t date_month
Definition: rtc2.h:132
time_hours
uint8_t time_hours
Definition: main.c:42
rtc2_set_time_hours
void rtc2_set_time_hours(rtc2_t *ctx, uint8_t hours)
Set hours function.
rtc2_t
Click ctx object definition.
Definition: rtc2.h:107
rtc2_cfg_t::i2c_address
uint8_t i2c_address
Definition: rtc2.h:153
date_month
uint8_t date_month
Definition: main.c:47
rtc2_t::i2c
i2c_master_t i2c
Definition: rtc2.h:117
rtc2_data_t::date_day
uint8_t date_day
Definition: rtc2.h:131
rtc2_cfg_t::sda
pin_name_t sda
Definition: rtc2.h:144
RTC2_RETVAL
#define RTC2_RETVAL
Definition: rtc2.h:62
rtc2_data_t::date_year
uint16_t date_year
Definition: rtc2.h:133