rtc7  2.0.0.0
rtc7.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 RTC7_H
36 #define RTC7_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 RTC7_MAP_MIKROBUS( cfg, mikrobus ) \
54  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
55  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
56  cfg.itb = MIKROBUS( mikrobus, MIKROBUS_AN ); \
57  cfg.ita = MIKROBUS( mikrobus, MIKROBUS_INT );
58 
64 #define RTC7_RETVAL uint8_t
65 
66 #define RTC7_OK 0x00
67 #define RTC7_INIT_ERROR 0xFF
68 
74 #define RTC7_SECONDS_ADDR 0x06
75 #define RTC7_MINUTES_ADDR 0x07
76 #define RTC7_HOURS_ADDR 0x08
77 #define RTC7_DAY_ADDR 0x09
78 #define RTC7_DATE_ADDR 0x0A
79 #define RTC7_MONTH_ADDR 0x0B
80 #define RTC7_YEAR_ADDR 0x0C
81 
87 #define RTC7_TIMEDATE_BYTES 7
88 #define RTC7_SECONDS_BYTE 0
89 #define RTC7_MINUTES_BYTE 1
90 #define RTC7_HOURS_BYTE 2
91 #define RTC7_DAY_BYTE 3
92 #define RTC7_DATE_BYTE 4
93 #define RTC7_MONTH_BYTE 5
94 #define RTC7_YEAR_BYTE 6
95 
101 #define RTC7_SECONDS_MASK 0x7F
102 #define RTC7_MINUTES_MASK 0x7F
103 #define RTC7_HOURS_MASK 0x1F
104 #define RTC7_DAY_MASK 0x07
105 #define RTC7_DATE_MASK 0x3F
106 #define RTC7_MONTH_MASK 0x1F
107 #define RTC7_YEAR_MASK 0xFF
108 
114 #define RTC7_JULIAN_DAY_1970 2440588
115 #define RTC7_TIME_SEC_IN_MIN 60
116 #define RTC7_TIME_SEC_IN_HOUR 3600
117 #define RTC7_TIME_SEC_IN_24_HOURS 86400
118 
124 #define RTC7_CONFIG1_REG 0x00
125 #define RTC7_CONFIG2_REG 0x01
126 #define RTC7_SLEEP_CONFIG_REG 0x02
127 #define RTC7_TIMER_CONFIG_REG 0x03
128 #define RTC7_INT_EN_REG 0x04
129 #define RTC7_INT_STATUS_REG 0x05
130 #define RTC7_ALM1_SEC_REG 0x0D
131 #define RTC7_ALM1_MIN_REG 0x0E
132 #define RTC7_ALM1_HRS_REG 0x0F
133 #define RTC7_ALM1_DAY_DATE_REG 0x10
134 #define RTC7_ALM2_MIN_REG 0x11
135 #define RTC7_ALM2_HRS_REG 0x12
136 #define RTC7_ALM2_DAY_DATE_REG 0x13
137 #define RTC7_TIMER_COUNT_REG 0x14
138 #define RTC7_TIMER_INIT_REG 0x15
139 #define RTC7_RAM0_REG_ADDR16H 0x16
140 #define RTC7_RAM63_REG_ADDR55H 0x55
141 #define RTC7_PWR_MGMT_REG 0x56
142 #define RTC7_TRICKLE_REG 0x57
143 #define RTC7_SPARE_REG 0x58
144 #define RTC7_SPARE_ST_REG 0x59
145 #define RTC7_TEST1_REG 0x64
146 #define RTC7_TEST2_REG 0x65
147 #define RTC7_TEST3_REG 0x66
148 #define RTC7_TCFG1_REG 0x67
149 #define RTC7_TCFG2_REG 0x68
150 #define RTC7_TCFG3_REG 0x69
151 #define RTC7_OTP1_B1_REG 0x6A
152 #define RTC7_OTP1_B2_REG 0x6B
153 #define RTC7_OTP1_B3_REG 0x6C
154 #define RTC7_OTP1_B4_REG 0x6D
155 #define RTC7_OTP2_B1_REG 0x6E
156 #define RTC7_OTP2_B2_REG 0x6F
157 #define RTC7_OTP2_B3_REG 0x70
158 #define RTC7_OTP2_B4_REG 0x71
159 #define RTC7_TCFG4_REG 0x72
160 #define RTC7_TCFG5_REG 0x73
161 #define RTC7_TEST_DIG_REG 0x74
162 
168 #define RTC7_RESET_DIGITAL_BLOCK 0x00
169 #define RTC7_NOT_RESET_DIGITAL_BLOCK 0x01
170 #define RTC7_OUTPUT_FREQ_1HZ 0x00
171 #define RTC7_OUTPUT_FREQ_4098HZ 0x02
172 #define RTC7_OUTPUT_FREQ_8192HZ 0x04
173 #define RTC7_OUTPUT_FREQ_32768HZ 0x06
174 #define RTC7_ENABLE_OSC 0x00
175 #define RTC7_DISABLE_OSC 0x08
176 #define RTC7_INPUT_FREQ_1HZ 0x00
177 #define RTC7_INPUT_FREQ_50HZ 0x10
178 #define RTC7_INPUT_FREQ_60HZ 0x20
179 #define RTC7_INPUT_FREQ_32768HZ 0x30
180 #define RTC7_OUTPUT_SQUARE_WAVE 0x00
181 #define RTC7_OUTPUT_INTERRUPT 0x40
182 #define RTC7_DISABLE_EXT_CLOCK 0x00
183 #define RTC7_ENABLE_EXT_CLOCK 0x80
184 
190 #define RTC7_SOLAR_MODE 0x00
191 #define RTC7_LUNAR_MODE 0x01
192 #define RTC7_WRITE_NOT_ALLOW 0x00
193 #define RTC7_WRITE_ALLOW 0x02
194 #define RTC7_READ_NOT_ALLOW 0x00
195 #define RTC7_READ_ALLOW 0x04
196 #define RTC7_I2C_TIMEOUT_DIS 0x00
197 #define RTC7_I2C_TIMEOUT_EN 0x08
198 #define RTC7_VOLTAGE_THRESH_1400MV 0x00
199 #define RTC7_VOLTAGE_THRESH_1800MV 0x10
200 #define RTC7_VOLTAGE_THRESH_2100MV 0x20
201 #define RTC7_VOLTAGE_THRESH_2500MV 0x30
202 #define RTC7_NORMAL_OP_MODE 0x00
203 #define RTC7_DATA_RETENTION_MODE 0x40
204 
210 #define RTC7_SLEEP_PERIOD_NO_DELAY 0x00
211 #define RTC7_SLEEP_PERIOD_8_TO_16MS 0x01
212 #define RTC7_SLEEP_PERIOD_16_TO_24MS 0x02
213 #define RTC7_SLEEP_PERIOD_24_TO_32MS 0x03
214 #define RTC7_SLEEP_PERIOD_32_TO_40MS 0x04
215 #define RTC7_SLEEP_PERIOD_40_TO_48MS 0x05
216 #define RTC7_SLEEP_PERIOD_48_TO_56MS 0x06
217 #define RTC7_SLEEP_PERIOD_56_TO_64MS 0x07
218 #define RTC7_SLEEP_MANAGER_DIS 0x00
219 #define RTC7_SLEEP_MANAGER_EN 0x08
220 #define RTC7_DIN0_INTERR_FALL_EDGE 0x00
221 #define RTC7_DIN0_INTERR_RISE_EDGE 0x10
222 #define RTC7_DIN1_INTERR_FALL_EDGE 0x00
223 #define RTC7_DIN1_INTERR_RISE_EDGE 0x20
224 #define RTC7_AN_INTERR_FALL_EDGE 0x00
225 #define RTC7_AN_INTERR_RISE_EDGE 0x40
226 #define RTC7_SLEEP_MODE_DIS 0x00
227 #define RTC7_SLEEP_MODE_EN 0x80
228 
234 #define RTC7_TIMER_FREQ_1024HZ 0x00
235 #define RTC7_TIMER_FREQ_256HZ 0x01
236 #define RTC7_TIMER_FREQ_64HZ 0x02
237 #define RTC7_TIMER_FREQ_16HZ 0x03
238 #define RTC7_TIMER_WILL_HALT 0x00
239 #define RTC7_TIMER_RELOADS_VALUE 0x04
240 #define RTC7_PULSE_INTERR_TM_IS_0 0x00
241 #define RTC7_PULSE_INTERR_TM_IS_1 0x08
242 #define RTC7_TIMER_DIS 0x00
243 #define RTC7_TIMER_EN 0x10
244 
250 #define RTC7_ALARM1_INTERR_DIS 0x00
251 #define RTC7_ALARM1_INTERR_EN 0x01
252 #define RTC7_ALARM2_INTERR_DIS 0x00
253 #define RTC7_ALARM2_INTERR_EN 0x02
254 #define RTC7_TIMER_INTERR_DIS 0x00
255 #define RTC7_TIMER_INTERR_EN 0x04
256 #define RTC7_EXT_INTERR0_DIS 0x00
257 #define RTC7_EXT_INTERR0_EN 0x08
258 #define RTC7_EXT_INTERR1_DIS 0x00
259 #define RTC7_EXT_INTERR1_EN 0x10
260 #define RTC7_AN_INTERR_DIS 0x00
261 #define RTC7_AN_INTERR_EN 0x20
262 
268 #define RTC7_INDICATES_AM 0x00
269 #define RTC7_INDICATES_PM 0x20
270 #define RTC7_24HR_FORMAT 0x00
271 #define RTC7_12HR_FORMAT 0x40
272  // End group macro
275 // --------------------------------------------------------------- PUBLIC TYPES
281 typedef struct
282 {
283  uint8_t seconds;
284  uint8_t minutes;
285  uint8_t hours;
286  uint8_t weekdays;
287  uint8_t monthday;
288  uint8_t month;
289  uint8_t year;
290 }rtc7_time_t;
291 
292 
296 typedef struct
297 {
298  // Input pins
299 
300  digital_in_t itb;
301  digital_in_t rst;
302  digital_in_t ita;
303 
304  // Modules
305 
306  i2c_master_t i2c;
307 
308  // ctx variable
309 
310  uint8_t slave_address;
311 
314  uint8_t dst_enabled;
315  uint8_t hours_check;
316  uint8_t val_pom;
317  uint8_t val_pom1;
318  uint8_t current_zone;
319  uint8_t cycle_check;
320  uint8_t rtc7_am_pm;
322  uint8_t rtc7_century;
324 
325 } rtc7_t;
326 
330 typedef struct
331 {
332  // Communication gpio pins
333 
334  pin_name_t scl;
335  pin_name_t sda;
336 
337  // Additional gpio pins
338 
339  pin_name_t itb;
340  pin_name_t rst;
341  pin_name_t ita;
342 
343  // static variable
344 
345  uint32_t i2c_speed;
346  uint8_t i2c_address;
347 
348 } rtc7_cfg_t;
349  // End types group
351 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
352 
358 #ifdef __cplusplus
359 extern "C"{
360 #endif
361 
370 void rtc7_cfg_setup ( rtc7_cfg_t *cfg );
371 
380 RTC7_RETVAL rtc7_init ( rtc7_t *ctx, rtc7_cfg_t *cfg );
381 
392 void rtc7_generic_write ( rtc7_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
393 
404 void rtc7_generic_read ( rtc7_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
405 
415 uint8_t rtc7_write_reg ( rtc7_t *ctx, uint8_t register_address, uint8_t transfer_data );
416 
427 uint8_t rtc7_read_reg ( rtc7_t *ctx, uint8_t register_address, uint8_t n_bytes, uint8_t *data_out );
428 
438 uint8_t rtc7_init_time ( rtc7_t *ctx, int8_t time_zone, uint8_t time_mode );
439 
447 void rtc7_enable_dst ( rtc7_t *ctx );
448 
456 void rtc7_disable_dst ( rtc7_t *ctx );
457 
468 uint8_t rtc7_set_osc ( rtc7_t *ctx, uint8_t enable_osc, uint8_t clk_in, uint8_t clk_out );
469 
479 uint8_t rtc7_set_timer ( rtc7_t *ctx, uint8_t enable_timer, uint8_t timer_freq );
480 
488 void rtc7_reset ( rtc7_t *ctx );
489 
498 void rtc7_get_gmt_time ( rtc7_t *ctx, rtc7_time_t *gmt_time );
499 
509 void rtc7_get_local_time ( rtc7_t *ctx, rtc7_time_t *local_time );
510 
519 uint8_t rtc7_set_gmt_time( rtc7_t *ctx, rtc7_time_t *time );
520 
529 uint8_t rtc7_check_interr ( rtc7_t *ctx );
530 
538 uint8_t rtc7_check_psw ( rtc7_t *ctx );
539 
540 #ifdef __cplusplus
541 }
542 #endif
543 #endif // _RTC7_H_
544  // End public_function group
547 
548 // ------------------------------------------------------------------------- END
uint8_t i2c_address
Definition: rtc7.h:346
uint8_t rtc7_am_pm
Definition: rtc7.h:320
uint8_t rtc7_set_timer(rtc7_t *ctx, uint8_t enable_timer, uint8_t timer_freq)
Timer Set function.
digital_in_t itb
Definition: rtc7.h:300
pin_name_t sda
Definition: rtc7.h:335
uint8_t current_zone
Definition: rtc7.h:318
uint8_t rtc7_set_osc(rtc7_t *ctx, uint8_t enable_osc, uint8_t clk_in, uint8_t clk_out)
Oscillator Set function.
uint8_t seconds
Definition: rtc7.h:283
uint8_t slave_address
Definition: rtc7.h:310
#define RTC7_RETVAL
Definition: rtc7.h:64
void rtc7_enable_dst(rtc7_t *ctx)
Distance Enable function.
Click configuration structure definition.
Definition: rtc7.h:330
void rtc7_get_gmt_time(rtc7_t *ctx, rtc7_time_t *gmt_time)
Get Gmt Time function.
void rtc7_generic_read(rtc7_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
uint8_t minutes
Definition: rtc7.h:284
void rtc7_get_local_time(rtc7_t *ctx, rtc7_time_t *local_time)
Get Local Time function.
int8_t current_time_zone
Definition: rtc7.h:313
uint8_t hours
Definition: rtc7.h:285
pin_name_t ita
Definition: rtc7.h:341
rtc7_time_t current_local_time
Definition: rtc7.h:312
uint8_t year
Definition: rtc7.h:289
uint8_t rtc7_set_gmt_time(rtc7_t *ctx, rtc7_time_t *time)
Set Gmt Time function.
uint8_t rtc7_read_reg(rtc7_t *ctx, uint8_t register_address, uint8_t n_bytes, uint8_t *data_out)
Read one byte function.
Definition: rtc7.h:281
uint8_t rtc7_write_reg(rtc7_t *ctx, uint8_t register_address, uint8_t transfer_data)
Write one byte function.
uint8_t monthday
Definition: rtc7.h:287
void rtc7_disable_dst(rtc7_t *ctx)
Distance Disable function.
Click ctx object definition.
Definition: rtc7.h:296
RTC7_RETVAL rtc7_init(rtc7_t *ctx, rtc7_cfg_t *cfg)
Initialization function.
void rtc7_generic_write(rtc7_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
uint32_t i2c_speed
Definition: rtc7.h:345
digital_in_t ita
Definition: rtc7.h:302
uint8_t cycle_check
Definition: rtc7.h:319
uint8_t rtc7_check_interr(rtc7_t *ctx)
Interrupt Check function.
i2c_master_t i2c
Definition: rtc7.h:306
void rtc7_reset(rtc7_t *ctx)
Reset function.
uint8_t val_pom1
Definition: rtc7.h:317
pin_name_t rst
Definition: rtc7.h:340
void rtc7_cfg_setup(rtc7_cfg_t *cfg)
Config Object Initialization function.
digital_in_t rst
Definition: rtc7.h:301
uint8_t month
Definition: rtc7.h:288
uint8_t weekdays
Definition: rtc7.h:286
uint8_t rtc7_check_psw(rtc7_t *ctx)
Power Switch Control function.
uint8_t rtc7_century
Definition: rtc7.h:322
uint8_t hours_check
Definition: rtc7.h:315
pin_name_t scl
Definition: rtc7.h:334
uint8_t rtc7_month_mode
Definition: rtc7.h:321
uint8_t val_pom
Definition: rtc7.h:316
rtc7_time_t * time_set_tmp
Definition: rtc7.h:323
uint8_t dst_enabled
Definition: rtc7.h:314
pin_name_t itb
Definition: rtc7.h:339
uint8_t rtc7_init_time(rtc7_t *ctx, int8_t time_zone, uint8_t time_mode)
Initialization function.