rtc4  2.0.0.0
rtc4.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2020 MikroElektronika d.o.o.
3 ** Contact: https://www.mikroe.com/contact
4 **
5 ** Permission is hereby granted, free of charge, to any person obtaining a copy
6 ** of this software and associated documentation files (the "Software"), to deal
7 ** in the Software without restriction, including without limitation the rights
8 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 ** copies of the Software, and to permit persons to whom the Software is
10 ** furnished to do so, subject to the following conditions:
11 ** The above copyright notice and this permission notice shall be
12 ** included in all copies or substantial portions of the Software.
13 **
14 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 ** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 ** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19 ** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 ** USE OR OTHER DEALINGS IN THE SOFTWARE.
21 ****************************************************************************/
22 
28 #ifndef RTC4_H
29 #define RTC4_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
35 #include "mikrosdk_version.h"
36 
37 #ifdef __GNUC__
38 #if mikroSDK_GET_VERSION < 20800ul
39 #include "rcu_delays.h"
40 #else
41 #include "delays.h"
42 #endif
43 #endif
44 
45 #include "drv_digital_out.h"
46 #include "drv_digital_in.h"
47 #include "drv_one_wire.h"
48 
69 #define RTC4_CMD_READ_CLOCK 0x66
70 #define RTC4_CMD_WRITE_CLOCK 0x99
71  // rtc4_cmd
73 
88 #define RTC4_SEC_PER_MINUTE 60
89 #define RTC4_SEC_PER_HOUR 3600
90 #define RTC4_SEC_PER_DAY 86400
91 #define RTC4_START_YEAR 1970
92 #define RTC4_DAYS_PER_LEAP_YEAR 366
93 #define RTC4_DAYS_PER_YEAR 365
94 #define RTC4_DAYS_IN_FEB 29
95  // rtc4_time
97 
112 #define RTC4_DCB_INT_ENABLE 0x80
113 #define RTC4_DCB_OSC_ENABLE 0x0C
114 #define RTC4_DCB_INTERVAL_1S 0x00
115 #define RTC4_DCB_INTERVAL_4S 0x10
116 #define RTC4_DCB_INTERVAL_32S 0x20
117 #define RTC4_DCB_INTERVAL_64S 0x30
118 #define RTC4_DCB_INTERVAL_2048S 0x40
119 #define RTC4_DCB_INTERVAL_4096S 0x50
120 #define RTC4_DCB_INTERVAL_65536S 0x60
121 #define RTC4_DCB_INTERVAL_131072S 0x70
122 
127 #define RTC4_FAMILY_CODE 0x27
128  // rtc4_set
130 
145 #define RTC4_MAP_MIKROBUS( cfg, mikrobus ) \
146  cfg.gp0 = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
147  cfg.gp1 = MIKROBUS( mikrobus, MIKROBUS_AN ); \
148  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
149  // rtc4_map
151  // rtc4
153 
158 typedef struct
159 {
160  uint32_t hours;
161  uint32_t min;
162  uint32_t sec;
163 
164 } rtc4_time_t;
165 
170 typedef struct
171 {
172  uint32_t year;
173  uint32_t month;
174  uint32_t day;
175 
176 } rtc4_date_t;
177 
182 typedef enum
183 {
185  RTC4_GPIO_1
188 
193 typedef struct
194 {
195  //Input pins
196  digital_in_t int_pin;
198  //Modules
199  one_wire_t ow;
201  one_wire_rom_address_t rom_addr;
202  uint8_t address;
204 } rtc4_t;
205 
210 typedef struct
211 {
212  // Communication gpio pins
213  pin_name_t gp0;
214  pin_name_t gp1;
216  // Additional gpio pins
217  pin_name_t int_pin;
221 } rtc4_cfg_t;
222 
227 typedef enum
228 {
229  RTC4_OK = 0,
230  RTC4_ERROR = -1
231 
233 
250 
266 
278 err_t rtc4_init ( rtc4_t *ctx, rtc4_cfg_t *cfg );
279 
292 
302 uint8_t rtc4_get_interrupt ( rtc4_t *ctx );
303 
315 err_t rtc4_set_unix_and_int ( rtc4_t *ctx, uint32_t unix_time, uint8_t int_interval );
316 
329 err_t rtc4_get_unix_time ( rtc4_t *ctx, uint32_t *unix_time );
330 
342 void rtc4_unix_to_date_time ( uint32_t unix_time, rtc4_time_t *time, rtc4_date_t *date );
343 
355 void rtc4_date_time_to_unix ( rtc4_date_t *date, rtc4_time_t *time, uint32_t *unix_time );
356 
370 err_t rtc4_set_date_time ( rtc4_t *ctx, rtc4_date_t *date, rtc4_time_t *time, uint8_t int_interval );
371 
385 
386 #ifdef __cplusplus
387 }
388 #endif
389 #endif // RTC4_H
390  // rtc4
392 
393 // ------------------------------------------------------------------------ END
rtc4_cfg_setup
void rtc4_cfg_setup(rtc4_cfg_t *cfg)
RTC 4 configuration object setup function.
rtc4_cfg_t::gp0
pin_name_t gp0
Definition: rtc4.h:213
rtc4_gpio_sel_t
rtc4_gpio_sel_t
RTC 4 Click gpio selector.
Definition: rtc4.h:183
rtc4_date_t::day
uint32_t day
Definition: rtc4.h:174
rtc4_time_t::sec
uint32_t sec
Definition: rtc4.h:162
rtc4_init
err_t rtc4_init(rtc4_t *ctx, rtc4_cfg_t *cfg)
RTC 4 initialization function.
rtc4_set_unix_and_int
err_t rtc4_set_unix_and_int(rtc4_t *ctx, uint32_t unix_time, uint8_t int_interval)
RTC 4 set unix and interrupt interval function.
rtc4_time_t
RTC 4 Click time object.
Definition: rtc4.h:159
rtc4_cfg_t::int_pin
pin_name_t int_pin
Definition: rtc4.h:217
rtc4_get_interrupt
uint8_t rtc4_get_interrupt(rtc4_t *ctx)
RTC 4 get interrupt function.
rtc4_unix_to_date_time
void rtc4_unix_to_date_time(uint32_t unix_time, rtc4_time_t *time, rtc4_date_t *date)
RTC 4 unix to date time function.
time
rtc4_time_t time
Definition: main.c:30
rtc4_check_communication
err_t rtc4_check_communication(rtc4_t *ctx)
RTC 4 check communication function.
rtc4_set_date_time
err_t rtc4_set_date_time(rtc4_t *ctx, rtc4_date_t *date, rtc4_time_t *time, uint8_t int_interval)
RTC 4 set date time function.
rtc4_cfg_t::gp1
pin_name_t gp1
Definition: rtc4.h:214
rtc4_get_unix_time
err_t rtc4_get_unix_time(rtc4_t *ctx, uint32_t *unix_time)
RTC 4 get unix time function.
rtc4_t::address
uint8_t address
Definition: rtc4.h:202
rtc4_time_t::min
uint32_t min
Definition: rtc4.h:161
RTC4_GPIO_0
@ RTC4_GPIO_0
Definition: rtc4.h:184
rtc4_cfg_t
RTC 4 Click configuration object.
Definition: rtc4.h:211
rtc4_date_t::month
uint32_t month
Definition: rtc4.h:173
RTC4_ERROR
@ RTC4_ERROR
Definition: rtc4.h:230
RTC4_GPIO_1
@ RTC4_GPIO_1
Definition: rtc4.h:185
RTC4_OK
@ RTC4_OK
Definition: rtc4.h:229
rtc4_date_t
RTC 4 Click date object.
Definition: rtc4.h:171
rtc4_cfg_t::gpio_sel
rtc4_gpio_sel_t gpio_sel
Definition: rtc4.h:219
rtc4_return_value_t
rtc4_return_value_t
RTC 4 Click return value data.
Definition: rtc4.h:228
date
rtc4_date_t date
Definition: main.c:31
rtc4_get_date_time
err_t rtc4_get_date_time(rtc4_t *ctx, rtc4_date_t *date, rtc4_time_t *time)
RTC 4 get date time function.
rtc4_t::int_pin
digital_in_t int_pin
Definition: rtc4.h:196
rtc4_t::ow
one_wire_t ow
Definition: rtc4.h:199
rtc4_date_t::year
uint32_t year
Definition: rtc4.h:172
rtc4_gpio_selection
void rtc4_gpio_selection(rtc4_cfg_t *cfg, rtc4_gpio_sel_t gpio_sel)
RTC 4 driver interface setup function.
rtc4_t
RTC 4 Click context object.
Definition: rtc4.h:194
rtc4_t::rom_addr
one_wire_rom_address_t rom_addr
Definition: rtc4.h:201
rtc4_time_t::hours
uint32_t hours
Definition: rtc4.h:160
rtc4_date_time_to_unix
void rtc4_date_time_to_unix(rtc4_date_t *date, rtc4_time_t *time, uint32_t *unix_time)
RTC 4 date time to unix function.