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  */
32 // ----------------------------------------------------------------------------
33 
34 #ifndef RTC7_H
35 #define RTC7_H
36 
37 #include "drv_digital_out.h"
38 #include "drv_digital_in.h"
39 #include "drv_i2c_master.h"
40 
41 
42 // -------------------------------------------------------------- PUBLIC MACROS
52 #define RTC7_MAP_MIKROBUS( cfg, mikrobus ) \
53  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
54  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
55  cfg.itb = MIKROBUS( mikrobus, MIKROBUS_AN ); \
56  cfg.ita = MIKROBUS( mikrobus, MIKROBUS_INT );
57 
63 #define RTC7_OK 0
64 #define RTC7_ERROR -1
65 
71 #define RTC7_SECONDS_ADDR 0x06
72 #define RTC7_MINUTES_ADDR 0x07
73 #define RTC7_HOURS_ADDR 0x08
74 #define RTC7_DAY_ADDR 0x09
75 #define RTC7_DATE_ADDR 0x0A
76 #define RTC7_MONTH_ADDR 0x0B
77 #define RTC7_YEAR_ADDR 0x0C
78 
84 #define RTC7_TIMEDATE_BYTES 7
85 #define RTC7_SECONDS_BYTE 0
86 #define RTC7_MINUTES_BYTE 1
87 #define RTC7_HOURS_BYTE 2
88 #define RTC7_DAY_BYTE 3
89 #define RTC7_DATE_BYTE 4
90 #define RTC7_MONTH_BYTE 5
91 #define RTC7_YEAR_BYTE 6
92 
98 #define RTC7_SECONDS_MASK 0x7F
99 #define RTC7_MINUTES_MASK 0x7F
100 #define RTC7_HOURS_MASK 0x1F
101 #define RTC7_DAY_MASK 0x07
102 #define RTC7_DATE_MASK 0x3F
103 #define RTC7_MONTH_MASK 0x1F
104 #define RTC7_YEAR_MASK 0xFF
105 
111 #define RTC7_JULIAN_DAY_1970 2440588
112 #define RTC7_TIME_SEC_IN_MIN 60
113 #define RTC7_TIME_SEC_IN_HOUR 3600
114 #define RTC7_TIME_SEC_IN_24_HOURS 86400
115 
121 #define RTC7_CONFIG1_REG 0x00
122 #define RTC7_CONFIG2_REG 0x01
123 #define RTC7_SLEEP_CONFIG_REG 0x02
124 #define RTC7_TIMER_CONFIG_REG 0x03
125 #define RTC7_INT_EN_REG 0x04
126 #define RTC7_INT_STATUS_REG 0x05
127 #define RTC7_ALM1_SEC_REG 0x0D
128 #define RTC7_ALM1_MIN_REG 0x0E
129 #define RTC7_ALM1_HRS_REG 0x0F
130 #define RTC7_ALM1_DAY_DATE_REG 0x10
131 #define RTC7_ALM2_MIN_REG 0x11
132 #define RTC7_ALM2_HRS_REG 0x12
133 #define RTC7_ALM2_DAY_DATE_REG 0x13
134 #define RTC7_TIMER_COUNT_REG 0x14
135 #define RTC7_TIMER_INIT_REG 0x15
136 #define RTC7_RAM0_REG_ADDR16H 0x16
137 #define RTC7_RAM63_REG_ADDR55H 0x55
138 #define RTC7_PWR_MGMT_REG 0x56
139 #define RTC7_TRICKLE_REG 0x57
140 #define RTC7_SPARE_REG 0x58
141 #define RTC7_SPARE_ST_REG 0x59
142 #define RTC7_TEST1_REG 0x64
143 #define RTC7_TEST2_REG 0x65
144 #define RTC7_TEST3_REG 0x66
145 #define RTC7_TCFG1_REG 0x67
146 #define RTC7_TCFG2_REG 0x68
147 #define RTC7_TCFG3_REG 0x69
148 #define RTC7_OTP1_B1_REG 0x6A
149 #define RTC7_OTP1_B2_REG 0x6B
150 #define RTC7_OTP1_B3_REG 0x6C
151 #define RTC7_OTP1_B4_REG 0x6D
152 #define RTC7_OTP2_B1_REG 0x6E
153 #define RTC7_OTP2_B2_REG 0x6F
154 #define RTC7_OTP2_B3_REG 0x70
155 #define RTC7_OTP2_B4_REG 0x71
156 #define RTC7_TCFG4_REG 0x72
157 #define RTC7_TCFG5_REG 0x73
158 #define RTC7_TEST_DIG_REG 0x74
159 
165 #define RTC7_RESET_DIGITAL_BLOCK 0x00
166 #define RTC7_NOT_RESET_DIGITAL_BLOCK 0x01
167 #define RTC7_OUTPUT_FREQ_1HZ 0x00
168 #define RTC7_OUTPUT_FREQ_4098HZ 0x02
169 #define RTC7_OUTPUT_FREQ_8192HZ 0x04
170 #define RTC7_OUTPUT_FREQ_32768HZ 0x06
171 #define RTC7_ENABLE_OSC 0x00
172 #define RTC7_DISABLE_OSC 0x08
173 #define RTC7_INPUT_FREQ_1HZ 0x00
174 #define RTC7_INPUT_FREQ_50HZ 0x10
175 #define RTC7_INPUT_FREQ_60HZ 0x20
176 #define RTC7_INPUT_FREQ_32768HZ 0x30
177 #define RTC7_OUTPUT_SQUARE_WAVE 0x00
178 #define RTC7_OUTPUT_INTERRUPT 0x40
179 #define RTC7_DISABLE_EXT_CLOCK 0x00
180 #define RTC7_ENABLE_EXT_CLOCK 0x80
181 
187 #define RTC7_SOLAR_MODE 0x00
188 #define RTC7_LUNAR_MODE 0x01
189 #define RTC7_WRITE_NOT_ALLOW 0x00
190 #define RTC7_WRITE_ALLOW 0x02
191 #define RTC7_READ_NOT_ALLOW 0x00
192 #define RTC7_READ_ALLOW 0x04
193 #define RTC7_I2C_TIMEOUT_DIS 0x00
194 #define RTC7_I2C_TIMEOUT_EN 0x08
195 #define RTC7_VOLTAGE_THRESH_1400MV 0x00
196 #define RTC7_VOLTAGE_THRESH_1800MV 0x10
197 #define RTC7_VOLTAGE_THRESH_2100MV 0x20
198 #define RTC7_VOLTAGE_THRESH_2500MV 0x30
199 #define RTC7_NORMAL_OP_MODE 0x00
200 #define RTC7_DATA_RETENTION_MODE 0x40
201 
207 #define RTC7_SLEEP_PERIOD_NO_DELAY 0x00
208 #define RTC7_SLEEP_PERIOD_8_TO_16MS 0x01
209 #define RTC7_SLEEP_PERIOD_16_TO_24MS 0x02
210 #define RTC7_SLEEP_PERIOD_24_TO_32MS 0x03
211 #define RTC7_SLEEP_PERIOD_32_TO_40MS 0x04
212 #define RTC7_SLEEP_PERIOD_40_TO_48MS 0x05
213 #define RTC7_SLEEP_PERIOD_48_TO_56MS 0x06
214 #define RTC7_SLEEP_PERIOD_56_TO_64MS 0x07
215 #define RTC7_SLEEP_MANAGER_DIS 0x00
216 #define RTC7_SLEEP_MANAGER_EN 0x08
217 #define RTC7_DIN0_INTERR_FALL_EDGE 0x00
218 #define RTC7_DIN0_INTERR_RISE_EDGE 0x10
219 #define RTC7_DIN1_INTERR_FALL_EDGE 0x00
220 #define RTC7_DIN1_INTERR_RISE_EDGE 0x20
221 #define RTC7_AN_INTERR_FALL_EDGE 0x00
222 #define RTC7_AN_INTERR_RISE_EDGE 0x40
223 #define RTC7_SLEEP_MODE_DIS 0x00
224 #define RTC7_SLEEP_MODE_EN 0x80
225 
231 #define RTC7_TIMER_FREQ_1024HZ 0x00
232 #define RTC7_TIMER_FREQ_256HZ 0x01
233 #define RTC7_TIMER_FREQ_64HZ 0x02
234 #define RTC7_TIMER_FREQ_16HZ 0x03
235 #define RTC7_TIMER_WILL_HALT 0x00
236 #define RTC7_TIMER_RELOADS_VALUE 0x04
237 #define RTC7_PULSE_INTERR_TM_IS_0 0x00
238 #define RTC7_PULSE_INTERR_TM_IS_1 0x08
239 #define RTC7_TIMER_DIS 0x00
240 #define RTC7_TIMER_EN 0x10
241 
247 #define RTC7_ALARM1_INTERR_DIS 0x00
248 #define RTC7_ALARM1_INTERR_EN 0x01
249 #define RTC7_ALARM2_INTERR_DIS 0x00
250 #define RTC7_ALARM2_INTERR_EN 0x02
251 #define RTC7_TIMER_INTERR_DIS 0x00
252 #define RTC7_TIMER_INTERR_EN 0x04
253 #define RTC7_EXT_INTERR0_DIS 0x00
254 #define RTC7_EXT_INTERR0_EN 0x08
255 #define RTC7_EXT_INTERR1_DIS 0x00
256 #define RTC7_EXT_INTERR1_EN 0x10
257 #define RTC7_AN_INTERR_DIS 0x00
258 #define RTC7_AN_INTERR_EN 0x20
259  // End group macro
262 // --------------------------------------------------------------- PUBLIC TYPES
268 typedef struct
269 {
270  uint8_t seconds;
271  uint8_t minutes;
272  uint8_t hours;
273  uint8_t weekdays;
274  uint8_t monthday;
275  uint8_t month;
276  uint8_t year;
277 
278 }rtc7_time_t;
279 
283 typedef struct
284 {
285  // Input pins
286  digital_in_t itb;
287  digital_in_t rst;
288  digital_in_t ita;
289 
290  // Modules
291  i2c_master_t i2c;
292 
293  // ctx variable
294  uint8_t slave_address;
295 
297  uint8_t rtc7_century;
298 
299 } rtc7_t;
300 
304 typedef struct
305 {
306  // Communication gpio pins
307  pin_name_t scl;
308  pin_name_t sda;
309 
310  // Additional gpio pins
311  pin_name_t itb;
312  pin_name_t rst;
313  pin_name_t ita;
314 
315  // static variable
316  uint32_t i2c_speed;
317  uint8_t i2c_address;
318 
319 } rtc7_cfg_t;
320  // End types group
322 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
323 
329 #ifdef __cplusplus
330 extern "C"{
331 #endif
332 
342 
351 err_t rtc7_init ( rtc7_t *ctx, rtc7_cfg_t *cfg );
352 
363 err_t rtc7_generic_write ( rtc7_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
364 
375 err_t rtc7_generic_read ( rtc7_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
376 
386 err_t rtc7_write_reg ( rtc7_t *ctx, uint8_t reg, uint8_t data_in );
387 
398 err_t rtc7_read_reg ( rtc7_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
399 
411 err_t rtc7_init_time ( rtc7_t *ctx, int8_t time_zone );
412 
426 err_t rtc7_set_osc ( rtc7_t *ctx, uint8_t enable_osc, uint8_t clk_in, uint8_t clk_out );
427 
440 err_t rtc7_set_timer ( rtc7_t *ctx, uint8_t enable_timer, uint8_t timer_freq );
441 
452 err_t rtc7_reset ( rtc7_t *ctx );
453 
465 err_t rtc7_get_gmt_time ( rtc7_t *ctx, rtc7_time_t *gmt_time );
466 
478 err_t rtc7_get_local_time ( rtc7_t *ctx, rtc7_time_t *local_time );
479 
491 err_t rtc7_set_gmt_time( rtc7_t *ctx, rtc7_time_t *time );
492 
500 uint8_t rtc7_check_interrupt ( rtc7_t *ctx );
501 
509 uint8_t rtc7_check_psw ( rtc7_t *ctx );
510 
511 #ifdef __cplusplus
512 }
513 #endif
514 #endif // _RTC7_H_
515  // End public_function group
518 
519 // ------------------------------------------------------------------------- END
rtc7_reset
err_t rtc7_reset(rtc7_t *ctx)
Reset function.
rtc7_set_timer
err_t rtc7_set_timer(rtc7_t *ctx, uint8_t enable_timer, uint8_t timer_freq)
Timer Set function.
rtc7_time_t::year
uint8_t year
Definition: rtc7.h:276
rtc7_cfg_t::scl
pin_name_t scl
Definition: rtc7.h:307
rtc7_t::rst
digital_in_t rst
Definition: rtc7.h:287
rtc7_t::rtc7_century
uint8_t rtc7_century
Definition: rtc7.h:297
rtc7_time_t::monthday
uint8_t monthday
Definition: rtc7.h:274
rtc7_cfg_t::i2c_address
uint8_t i2c_address
Definition: rtc7.h:317
rtc7_time_t::hours
uint8_t hours
Definition: rtc7.h:272
rtc7_get_gmt_time
err_t rtc7_get_gmt_time(rtc7_t *ctx, rtc7_time_t *gmt_time)
Get Gmt Time function.
rtc7_cfg_t::itb
pin_name_t itb
Definition: rtc7.h:311
rtc7_generic_read
err_t rtc7_generic_read(rtc7_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
rtc7_time_t
Definition: rtc7.h:269
rtc7_cfg_t
Click configuration structure definition.
Definition: rtc7.h:305
rtc7_check_interrupt
uint8_t rtc7_check_interrupt(rtc7_t *ctx)
Interrupt Check function.
rtc7_cfg_t::ita
pin_name_t ita
Definition: rtc7.h:313
rtc7_t::itb
digital_in_t itb
Definition: rtc7.h:286
rtc7_time_t::seconds
uint8_t seconds
Definition: rtc7.h:270
rtc7_t
Click ctx object definition.
Definition: rtc7.h:284
rtc7_time_t::minutes
uint8_t minutes
Definition: rtc7.h:271
rtc7_t::current_time_zone
int8_t current_time_zone
Definition: rtc7.h:296
rtc7_write_reg
err_t rtc7_write_reg(rtc7_t *ctx, uint8_t reg, uint8_t data_in)
Write one byte function.
rtc7_cfg_t::sda
pin_name_t sda
Definition: rtc7.h:308
rtc7_time_t::month
uint8_t month
Definition: rtc7.h:275
rtc7_get_local_time
err_t rtc7_get_local_time(rtc7_t *ctx, rtc7_time_t *local_time)
Get Local Time function.
rtc7_t::slave_address
uint8_t slave_address
Definition: rtc7.h:294
rtc7_check_psw
uint8_t rtc7_check_psw(rtc7_t *ctx)
Power Switch Control function.
rtc7_t::i2c
i2c_master_t i2c
Definition: rtc7.h:291
rtc7_cfg_setup
void rtc7_cfg_setup(rtc7_cfg_t *cfg)
Config Object Initialization function.
rtc7_set_osc
err_t rtc7_set_osc(rtc7_t *ctx, uint8_t enable_osc, uint8_t clk_in, uint8_t clk_out)
Oscillator Set function.
rtc7_cfg_t::rst
pin_name_t rst
Definition: rtc7.h:312
rtc7_t::ita
digital_in_t ita
Definition: rtc7.h:288
rtc7_init
err_t rtc7_init(rtc7_t *ctx, rtc7_cfg_t *cfg)
Initialization function.
rtc7_read_reg
err_t rtc7_read_reg(rtc7_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
Read one byte function.
rtc7_time_t::weekdays
uint8_t weekdays
Definition: rtc7.h:273
rtc7_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: rtc7.h:316
rtc7_set_gmt_time
err_t rtc7_set_gmt_time(rtc7_t *ctx, rtc7_time_t *time)
Set Gmt Time function.
rtc7_generic_write
err_t rtc7_generic_write(rtc7_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
rtc7_init_time
err_t rtc7_init_time(rtc7_t *ctx, int8_t time_zone)
Initialization function.