rtc8  2.0.0.0
rtc8.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 RTC8_H
36 #define RTC8_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 // -------------------------------------------------------------- PUBLIC MACROS
66 #define RTC8_MAP_MIKROBUS( cfg, mikrobus ) \
67  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
68  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
69  cfg.evi = MIKROBUS( mikrobus, MIKROBUS_CS ); \
70  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
71 
77 #define RTC8_OK 0
78 #define RTC8_ERROR -1
79 
85 #define RTC8_REG_SECONDS 0x00
86 #define RTC8_REG_MINUTES 0x01
87 #define RTC8_REG_HOURS 0x02
88 #define RTC8_REG_WEEKDAY 0x03
89 #define RTC8_REG_DATE 0x04
90 #define RTC8_REG_MONTH 0x05
91 #define RTC8_REG_YEAR 0x06
92 #define RTC8_REG_MINUTES_ALARM 0x07
93 #define RTC8_REG_HOURS_ALARM 0x08
94 #define RTC8_REG_WEEKDAY_ALARM 0x09
95 #define RTC8_REG_TIMER_VALUE_0 0x0A
96 #define RTC8_REG_TIMER_VALUE_1 0x0B
97 #define RTC8_REG_TIMER_STATUS_0 0x0C
98 #define RTC8_REG_TIMER_STATUS_1 0x0D
99 #define RTC8_REG_STATUS 0x0E
100 #define RTC8_REG_CONTROL1 0x0F
101 #define RTC8_REG_CONTROL2 0x10
102 #define RTC8_REG_GP_BITS 0x11
103 #define RTC8_REG_CLOCK_INT_MASK 0x12
104 #define RTC8_REG_EVENT_CONTROL 0x13
105 #define RTC8_REG_COUNT_TS 0x14
106 #define RTC8_REG_SECONDS_TS 0x15
107 #define RTC8_REG_MINUTES_TS 0x16
108 #define RTC8_REG_HOURS_TS 0x17
109 #define RTC8_REG_DATE_TS 0x18
110 #define RTC8_REG_MONTH_TS 0x19
111 #define RTC8_REG_YEAR_TS 0x1A
112 #define RTC8_REG_UNIX0 0x1B
113 #define RTC8_REG_UNIX1 0x1C
114 #define RTC8_REG_UNIX2 0x1D
115 #define RTC8_REG_UNIX3 0x1E
116 #define RTC8_REG_USER_RAM_1 0x1F
117 #define RTC8_REG_USER_RAM_2 0x20
118 #define RTC8_REG_PASSWORD_0 0x21
119 #define RTC8_REG_PASSWORD_1 0x22
120 #define RTC8_REG_PASSWORD_2 0x23
121 #define RTC8_REG_PASSWORD_3 0x24
122 #define RTC8_REG_EEPROM_ADDRESS 0x25
123 #define RTC8_REG_EEPROM_DATA 0x26
124 #define RTC8_REG_EEPROM_COM 0x27
125 #define RTC8_REG_ID 0x28
126 #define RTC8_REG_EEPROM_BACKUP 0x37
127 
133 #define RTC8_STATUS_EEPROM_BUSY 0x80
134 #define RTC8_STATUS_CLOCK_INT_FLAG 0x40
135 #define RTC8_STATUS_BACKUP_SWITCH_FLAG 0x20
136 #define RTC8_STATUS_UPDATE_FLAG 0x10
137 #define RTC8_STATUS_TIMER_FLAG 0x08
138 #define RTC8_STATUS_ALARM_FLAG 0x04
139 #define RTC8_STATUS_EVENT_FLAG 0x02
140 #define RTC8_STATUS_POR_FLAG 0x01
141 #define RTC8_STATUS_REG_CLEAR 0x00
142 
148 #define RTC8_CTRL1_TRPT_SINGLE_MODE 0x00
149 #define RTC8_CTRL1_TRPT_REPEAT_MODE 0x80
150 #define RTC8_CTRL1_WADA_WEEKDAY_ALARM 0x00
151 #define RTC8_CTRL1_WADA_DATE_ALARM 0x20
152 #define RTC8_CTRL1_USEL_SECOND_UPDATE 0x00
153 #define RTC8_CTRL1_USEL_MINUTE_UPDATE 0x10
154 #define RTC8_CTRL1_EERD_REFRESH_ACTIVE 0x00
155 #define RTC8_CTRL1_EERD_REFRESH_NO_ACTIVE 0x08
156 #define RTC8_CTRL1_TE_TIMER_STOPS 0x00
157 #define RTC8_CTRL1_TE_TIMER_STARTS 0x04
158 #define RTC8_CTRL1_TD_TIMER_CLK_4096Hz 0x00
159 #define RTC8_CTRL1_TD_TIMER_CLK_64Hz 0x01
160 #define RTC8_CTRL1_TD_TIMER_CLK_1Hz 0x02
161 #define RTC8_CTRL1_TD_TIMER_CLK_1_60Hz 0x03
162 
168 #define RTC8_CTRL2_TIME_STAMP_DISABLE 0x00
169 #define RTC8_CTRL2_TIME_STAMP_ENABLE 0x80
170 #define RTC8_CTRL2_INT_CLOCK_DISABLE 0x00
171 #define RTC8_CTRL2_INT_CLOCK_ENABLE 0x40
172 #define RTC8_CTRL2_TIME_UPDATE_INTERRUPT 0x20
173 #define RTC8_CTRL2_TIME_UPDATE_NO_INTERRUPT 0x00
174 #define RTC8_CTRL2_TIMER_INTERRUPT 0x10
175 #define RTC8_CTRL2_TIMER_NO_INTERRUPT 0x00
176 #define RTC8_CTRL2_ALARM_INTERRUPT 0x08
177 #define RTC8_CTRL2_ALARM_NO_INTERRUPT 0x00
178 #define RTC8_CTRL2_EVENT_INTERRUPT 0x04
179 #define RTC8_CTRL2_EVENT_NO_INTERRUPT 0x00
180 #define RTC8_CTRL2_HOUR_MODE_12 0x02
181 #define RTC8_CTRL2_HOUR_MODE_24 0x00
182 #define RTC8_CTRL2_RESET_ACTIVE 0x01
183 #define RTC8_CTRL2_NO_RESET 0x00
184 
190 #define RTC8_CLKIM_EVENT_INT_ENABLE 0x08
191 #define RTC8_CLKIM_EVENT_INT_DISABLE 0x00
192 #define RTC8_CLKIM_ALARM_INT_ENABLE 0x04
193 #define RTC8_CLKIM_ALARM_INT_DISABLE 0x00
194 #define RTC8_CLKIM_TIMER_INT_ENABLE 0x02
195 #define RTC8_CLKIM_TIMER_INT_DISABLE 0x00
196 #define RTC8_CLKIM_TIME_UPDATE_INT_ENABLE 0x01
197 #define RTC8_CLKIM_TIME_UPDATE_INT_DISABLE 0x00
198 
204 #define RTC8_EC_EVENT_LOW_LEVEL 0x00
205 #define RTC8_EC_EVENT_HIGH_LEVEL 0x40
206 #define RTC8_EC_EVENT_PERIOD_256Hz 0x10
207 #define RTC8_EC_EVENT_PERIOD_64Hz 0x20
208 #define RTC8_EC_EVENT_PERIOD_8Hz 0x30
209 #define RTC8_EC_EVENT_NO_FILTERING 0x00
210 #define RTC8_EC_TIME_STAMP_RESET_ENABLE 0x04
211 #define RTC8_EC_TIME_STAMP_RESET_DISABLE 0x00
212 #define RTC8_EC_TSOW_EVF_CLEARED 0x00
213 #define RTC8_EC_TSOW_EVF_NO_CLEARED 0x02
214 #define RTC8_EC_TSS_TSE_ON_EVI 0x00
215 #define RTC8_EC_TSS_TSE_ON_VBACKUP 0x01
216 
222 #define RTC8_EECMD_WRITE_ALL_EEPROM_REG 0x11
223 #define RTC8_EECMD_READ_ALL_EEPROM_REG 0x12
224 #define RTC8_EECMD_WRITE_ONE_EEPROM_REG 0x21
225 #define RTC8_EECMD_READ_ONE_EEPROM_REG 0x22
226 
232 #define RTC8_ALARM_IS_ACTIVE 0x01
233 #define RTC8_ALARM_NO_ACTIVE 0x00
234  // End group macro
237 // --------------------------------------------------------------- PUBLIC TYPES
246 typedef struct
247 {
248  // Output pins
249  digital_out_t evi;
250 
251  // Input pins
252  digital_in_t int_pin;
253 
254  // Modules
255  i2c_master_t i2c;
256 
257  // ctx variable
258  uint8_t slave_address;
259 
260 } rtc8_t;
261 
265 typedef struct
266 {
267  // Communication gpio pins
268  pin_name_t scl;
269  pin_name_t sda;
270 
271  // Additional gpio pins
272  pin_name_t evi;
273  pin_name_t int_pin;
274 
275  // static variable
276  uint32_t i2c_speed;
277  uint8_t i2c_address;
278 
279 } rtc8_cfg_t;
280 
284 typedef struct
285 {
286  uint8_t hours;
287  uint8_t minutes;
288  uint8_t seconds;
289 
290 } rtc8_time_t;
291 
295 typedef struct
296 {
297  uint8_t day;
298  uint8_t month;
299  uint8_t year;
300  uint8_t weekdays;
301 
302 } rtc8_date_t;
303 
307 typedef struct
308 {
309  uint8_t weekdays;
310  uint8_t hours;
311  uint8_t minutes;
312 
313 } rtc8_alarm_t;
314  // End types group
316 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
317 
323 #ifdef __cplusplus
324 extern "C"{
325 #endif
326 
336 
348 err_t rtc8_init ( rtc8_t *ctx, rtc8_cfg_t *cfg );
349 
360 err_t rtc8_default_cfg ( rtc8_t *ctx );
361 
375 err_t rtc8_generic_write ( rtc8_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
376 
390 err_t rtc8_generic_read ( rtc8_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
391 
404 err_t rtc8_write_data ( rtc8_t *ctx, uint8_t reg, uint8_t data_in );
405 
418 err_t rtc8_read_data ( rtc8_t *ctx, uint8_t reg, uint8_t *data_out );
419 
430 err_t rtc8_get_uinx_time ( rtc8_t *ctx, uint32_t *unix_time );
431 
442 err_t rtc8_set_unix_time ( rtc8_t *ctx, uint32_t unix_time );
443 
455 err_t rtc8_set_time ( rtc8_t *ctx, rtc8_time_t *time_s );
456 
469 err_t rtc8_set_date ( rtc8_t *ctx, rtc8_date_t *date_s );
470 
482 err_t rtc8_set_alarm ( rtc8_t *ctx, rtc8_alarm_t *alarm_s );
483 
491 uint8_t rtc8_get_int_pin ( rtc8_t *ctx );
492 
499 void rtc8_set_evi_pin ( rtc8_t *ctx, uint8_t state );
500 
513 err_t rtx8_get_time_and_date ( rtc8_t *ctx, rtc8_time_t *time_s, rtc8_date_t *date_s );
514 
524 
535 err_t rtc8_get_status ( rtc8_t *ctx, uint8_t *status );
536 
547 err_t rtc8_get_alarm_flag ( rtc8_t *ctx, uint8_t *alarm_flag );
548 
549 #ifdef __cplusplus
550 }
551 #endif
552 #endif // _RTC8_H_
553  // End public_function group
556 
557 // ------------------------------------------------------------------------- END
rtc8_set_unix_time
err_t rtc8_set_unix_time(rtc8_t *ctx, uint32_t unix_time)
Set UNIX time.
rtc8_cfg_t::evi
pin_name_t evi
Definition: rtc8.h:272
rtc8_time_t::hours
uint8_t hours
Definition: rtc8.h:286
rtc8_date_t::weekdays
uint8_t weekdays
Definition: rtc8.h:300
rtc8_cfg_t::i2c_address
uint8_t i2c_address
Definition: rtc8.h:277
rtc8_date_t::year
uint8_t year
Definition: rtc8.h:299
rtc8_alarm_t::hours
uint8_t hours
Definition: rtc8.h:310
rtc8_set_alarm
err_t rtc8_set_alarm(rtc8_t *ctx, rtc8_alarm_t *alarm_s)
Set Alarm.
rtc8_generic_read
err_t rtc8_generic_read(rtc8_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
rtc8_init
err_t rtc8_init(rtc8_t *ctx, rtc8_cfg_t *cfg)
Initialization function.
rtc8_set_time
err_t rtc8_set_time(rtc8_t *ctx, rtc8_time_t *time_s)
Set new time - 24 hour format.
rtc8_get_int_pin
uint8_t rtc8_get_int_pin(rtc8_t *ctx)
Get interrupt state.
rtc8_time_t
Time structure definition.
Definition: rtc8.h:285
rtc8_default_cfg
err_t rtc8_default_cfg(rtc8_t *ctx)
Click Default Configuration function.
rtx8_get_time_and_date
err_t rtx8_get_time_and_date(rtc8_t *ctx, rtc8_time_t *time_s, rtc8_date_t *date_s)
Get RTC data ( Time and Data )
rtc8_generic_write
err_t rtc8_generic_write(rtc8_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
rtc8_write_data
err_t rtc8_write_data(rtc8_t *ctx, uint8_t reg, uint8_t data_in)
Write data ( Convert and send data in BCD format )
rtc8_cfg_t
Click configuration structure definition.
Definition: rtc8.h:266
rtc8_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: rtc8.h:276
rtc8_reset_alarm_flag
err_t rtc8_reset_alarm_flag(rtc8_t *ctx)
Reset alarm.
rtc8_time_t::minutes
uint8_t minutes
Definition: rtc8.h:287
rtc8_set_evi_pin
void rtc8_set_evi_pin(rtc8_t *ctx, uint8_t state)
Set EVI(External Event Input) pin.
rtc8_alarm_t::minutes
uint8_t minutes
Definition: rtc8.h:311
rtc8_set_date
err_t rtc8_set_date(rtc8_t *ctx, rtc8_date_t *date_s)
Set new date.
rtc8_alarm_t::weekdays
uint8_t weekdays
Definition: rtc8.h:309
rtc8_cfg_t::scl
pin_name_t scl
Definition: rtc8.h:268
rtc8_time_t::seconds
uint8_t seconds
Definition: rtc8.h:288
rtc8_date_t
Date structure definition.
Definition: rtc8.h:296
rtc8_get_uinx_time
err_t rtc8_get_uinx_time(rtc8_t *ctx, uint32_t *unix_time)
Get current UNIX time.
rtc8_alarm_t
Alarm structure definition.
Definition: rtc8.h:308
rtc8_t::slave_address
uint8_t slave_address
Definition: rtc8.h:258
rtc8_cfg_t::sda
pin_name_t sda
Definition: rtc8.h:269
rtc8_cfg_t::int_pin
pin_name_t int_pin
Definition: rtc8.h:273
rtc8_date_t::day
uint8_t day
Definition: rtc8.h:297
rtc8_date_t::month
uint8_t month
Definition: rtc8.h:298
rtc8_t::int_pin
digital_in_t int_pin
Definition: rtc8.h:252
rtc8_read_data
err_t rtc8_read_data(rtc8_t *ctx, uint8_t reg, uint8_t *data_out)
Read one byte data from register in DEC format.
rtc8_t::evi
digital_out_t evi
Definition: rtc8.h:249
rtc8_get_status
err_t rtc8_get_status(rtc8_t *ctx, uint8_t *status)
Get status.
rtc8_get_alarm_flag
err_t rtc8_get_alarm_flag(rtc8_t *ctx, uint8_t *alarm_flag)
Get Alarm flag.
rtc8_cfg_setup
void rtc8_cfg_setup(rtc8_cfg_t *cfg)
Config Object Initialization function.
rtc8_t
Click ctx object definition.
Definition: rtc8.h:247
rtc8_t::i2c
i2c_master_t i2c
Definition: rtc8.h:255