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 
38 #include "drv_digital_out.h"
39 #include "drv_digital_in.h"
40 #include "drv_i2c_master.h"
41 
42 // -------------------------------------------------------------- PUBLIC MACROS
52 #define RTC8_MAP_MIKROBUS( cfg, mikrobus ) \
53  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
54  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
55  cfg.evi = MIKROBUS( mikrobus, MIKROBUS_CS ); \
56  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
57 
63 #define RTC8_RETVAL uint8_t
64 
65 #define RTC8_OK 0x00
66 #define RTC8_INIT_ERROR 0xFF
67 
73 #define RTC8_REG_SECONDS 0x00
74 #define RTC8_REG_MINUTES 0x01
75 #define RTC8_REG_HOURS 0x02
76 #define RTC8_REG_WEEKDAY 0x03
77 #define RTC8_REG_DATE 0x04
78 #define RTC8_REG_MONTH 0x05
79 #define RTC8_REG_YEAR 0x06
80 #define RTC8_REG_MINUTES_ALARM 0x07
81 #define RTC8_REG_HOURS_ALARM 0x08
82 #define RTC8_REG_WEEKDAY_ALARM 0x09
83 #define RTC8_REG_TIMER_VALUE_0 0x0A
84 #define RTC8_REG_TIMER_VALUE_1 0x0B
85 #define RTC8_REG_TIMER_STATUS_0 0x0C
86 #define RTC8_REG_TIMER_STATUS_1 0x0D
87 #define RTC8_REG_STATUS 0x0E
88 #define RTC8_REG_CONTROL1 0x0F
89 #define RTC8_REG_CONTROL2 0x10
90 #define RTC8_REG_GP_BITS 0x11
91 #define RTC8_REG_CLOCK_INT_MASK 0x12
92 #define RTC8_REG_EVENT_CONTROL 0x13
93 #define RTC8_REG_COUNT_TS 0x14
94 #define RTC8_REG_SECONDS_TS 0x15
95 #define RTC8_REG_MINUTES_TS 0x16
96 #define RTC8_REG_HOURS_TS 0x17
97 #define RTC8_REG_DATE_TS 0x18
98 #define RTC8_REG_MONTH_TS 0x19
99 #define RTC8_REG_YEAR_TS 0x1A
100 #define RTC8_REG_UNIX0 0x1B
101 #define RTC8_REG_UNIX1 0x1C
102 #define RTC8_REG_UNIX2 0x1D
103 #define RTC8_REG_UNIX3 0x1E
104 #define RTC8_REG_USER_RAM_1 0x1F
105 #define RTC8_REG_USER_RAM_2 0x20
106 #define RTC8_REG_PASSWORD_0 0x21
107 #define RTC8_REG_PASSWORD_1 0x22
108 #define RTC8_REG_PASSWORD_2 0x23
109 #define RTC8_REG_PASSWORD_3 0x24
110 #define RTC8_REG_EEPROM_ADDRESS 0x25
111 #define RTC8_REG_EEPROM_DATA 0x26
112 #define RTC8_REG_EEPROM_COM 0x27
113 #define RTC8_REG_ID 0x28
114 #define RTC8_REG_EEPROM_BACKUP 0x37
115 
121 #define RTC8_STATUS_EEPROM_BUSY 0x80
122 #define RTC8_STATUS_CLOCK_INT_FLAG 0x40
123 #define RTC8_STATUS_BACKUP_SWITCH_FLAG 0x20
124 #define RTC8_STATUS_UPDATE_FLAG 0x10
125 #define RTC8_STATUS_TIMER_FLAG 0x08
126 #define RTC8_STATUS_ALARM_FLAG 0x04
127 #define RTC8_STATUS_EVENT_FLAG 0x02
128 #define RTC8_STATUS_POR_FLAG 0x01
129 #define RTC8_STATUS_REG_CLEAR 0x00
130 
136 #define RTC8_CTRL1_TRPT_SINGLE_MODE 0x00
137 #define RTC8_CTRL1_TRPT_REPEAT_MODE 0x80
138 #define RTC8_CTRL1_WADA_WEEKDAY_ALARM 0x00
139 #define RTC8_CTRL1_WADA_DATE_ALARM 0x20
140 #define RTC8_CTRL1_USEL_SECOND_UPDATE 0x00
141 #define RTC8_CTRL1_USEL_MINUTE_UPDATE 0x10
142 #define RTC8_CTRL1_EERD_REFRESH_ACTIVE 0x00
143 #define RTC8_CTRL1_EERD_REFRESH_NO_ACTIVE 0x08
144 #define RTC8_CTRL1_TE_TIMER_STOPS 0x00
145 #define RTC8_CTRL1_TE_TIMER_STARTS 0x04
146 #define RTC8_CTRL1_TD_TIMER_CLK_4096Hz 0x00
147 #define RTC8_CTRL1_TD_TIMER_CLK_64Hz 0x01
148 #define RTC8_CTRL1_TD_TIMER_CLK_1Hz 0x02
149 #define RTC8_CTRL1_TD_TIMER_CLK_1_60Hz 0x03
150 
156 #define RTC8_CTRL2_TIME_STAMP_DISABLE 0x00
157 #define RTC8_CTRL2_TIME_STAMP_ENABLE 0x80
158 #define RTC8_CTRL2_INT_CLOCK_DISABLE 0x00
159 #define RTC8_CTRL2_INT_CLOCK_ENABLE 0x40
160 #define RTC8_CTRL2_TIME_UPDATE_INTERRUPT 0x20
161 #define RTC8_CTRL2_TIME_UPDATE_NO_INTERRUPT 0x00
162 #define RTC8_CTRL2_TIMER_INTERRUPT 0x10
163 #define RTC8_CTRL2_TIMER_NO_INTERRUPT 0x00
164 #define RTC8_CTRL2_ALARM_INTERRUPT 0x08
165 #define RTC8_CTRL2_ALARM_NO_INTERRUPT 0x00
166 #define RTC8_CTRL2_EVENT_INTERRUPT 0x04
167 #define RTC8_CTRL2_EVENT_NO_INTERRUPT 0x00
168 #define RTC8_CTRL2_HOUR_MODE_12 0x02
169 #define RTC8_CTRL2_HOUR_MODE_24 0x00
170 #define RTC8_CTRL2_RESET_ACTIVE 0x01
171 #define RTC8_CTRL2_NO_RESET 0x00
172 
178 #define RTC8_CLKIM_EVENT_INT_ENABLE 0x08
179 #define RTC8_CLKIM_EVENT_INT_DISABLE 0x00
180 #define RTC8_CLKIM_ALARM_INT_ENABLE 0x04
181 #define RTC8_CLKIM_ALARM_INT_DISABLE 0x00
182 #define RTC8_CLKIM_TIMER_INT_ENABLE 0x02
183 #define RTC8_CLKIM_TIMER_INT_DISABLE 0x00
184 #define RTC8_CLKIM_TIME_UPDATE_INT_ENABLE 0x01
185 #define RTC8_CLKIM_TIME_UPDATE_INT_DISABLE 0x00
186 
192 #define RTC8_EC_EVENT_LOW_LEVEL 0x00
193 #define RTC8_EC_EVENT_HIGH_LEVEL 0x40
194 #define RTC8_EC_EVENT_PERIOD_256Hz 0x10
195 #define RTC8_EC_EVENT_PERIOD_64Hz 0x20
196 #define RTC8_EC_EVENT_PERIOD_8Hz 0x30
197 #define RTC8_EC_EVENT_NO_FILTERING 0x00
198 #define RTC8_EC_TIME_STAMP_RESET_ENABLE 0x04
199 #define RTC8_EC_TIME_STAMP_RESET_DISABLE 0x00
200 #define RTC8_EC_TSOW_EVF_CLEARED 0x00
201 #define RTC8_EC_TSOW_EVF_NO_CLEARED 0x02
202 #define RTC8_EC_TSS_TSE_ON_EVI 0x00
203 #define RTC8_EC_TSS_TSE_ON_VBACKUP 0x01
204 
210 #define RTC8_EECMD_WRITE_ALL_EEPROM_REG 0x11
211 #define RTC8_EECMD_READ_ALL_EEPROM_REG 0x12
212 #define RTC8_EECMD_WRITE_ONE_EEPROM_REG 0x21
213 #define RTC8_EECMD_READ_ONE_EEPROM_REG 0x22
214 
220 #define RTC8_ALARM_IS_ACTIVE 0x01
221 #define RTC8_ALARM_NO_ACTIVE 0x00
222  // End group macro
225 // --------------------------------------------------------------- PUBLIC TYPES
234 typedef struct
235 {
236  // Output pins
237 
238  digital_out_t evi;
239 
240  // Input pins
241 
242  digital_in_t int_pin;
243 
244  // Modules
245 
246  i2c_master_t i2c;
247 
248  // ctx variable
249 
250  uint8_t slave_address;
251 
252 } rtc8_t;
253 
257 typedef struct
258 {
259  // Communication gpio pins
260 
261  pin_name_t scl;
262  pin_name_t sda;
263 
264  // Additional gpio pins
265 
266  pin_name_t evi;
267  pin_name_t int_pin;
268 
269  // static variable
270 
271  uint32_t i2c_speed;
272  uint8_t i2c_address;
273 
274 } rtc8_cfg_t;
275 
279 typedef struct
280 {
281  uint8_t hours;
282  uint8_t minutes;
283  uint8_t seconds;
284 
285 }rtc8_time_t;
286 
290 typedef struct
291 {
292  uint8_t day;
293  uint8_t month;
294  uint8_t year;
295  uint8_t weekdays;
296 
297 }rtc8_date_t;
298 
302 typedef struct
303 {
304  uint8_t weekdays;
305  uint8_t hours;
306  uint8_t minutes;
307 
308 }rtc8_alarm_t;
309  // End types group
311 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
312 
318 #ifdef __cplusplus
319 extern "C"{
320 #endif
321 
330 void rtc8_cfg_setup ( rtc8_cfg_t *cfg );
331 
340 RTC8_RETVAL rtc8_init ( rtc8_t *ctx, rtc8_cfg_t *cfg );
341 
349 void rtc8_default_cfg ( rtc8_t *ctx );
350 
361 void rtc8_generic_write ( rtc8_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
362 
373 void rtc8_generic_read ( rtc8_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
374 
388 uint8_t rtc8_write_data_bcd_format ( rtc8_t *ctx, uint8_t reg, uint8_t _data );
389 
400 uint8_t rtc8_read_data_bcd_format ( rtc8_t *ctx, uint8_t reg );
401 
409 uint32_t rtc8_get_uinx_time ( rtc8_t *ctx );
410 
418 void rtc8_set_unix_time ( rtc8_t *ctx, int32_t unix_time );
419 
428 uint8_t rtc8_set_time ( rtc8_t *ctx, rtc8_time_t *time_s );
429 
438 uint8_t rtc8_set_date ( rtc8_t *ctx, rtc8_date_t *date_s );
439 
448 uint8_t rtc8_set_alarm ( rtc8_t *ctx, rtc8_alarm_t *alarm_s );
449 
457 uint8_t rtc8_get_interrupt_state ( rtc8_t *ctx );
458 
465 void rtc8_set_external_event_input ( rtc8_t *ctx, uint8_t state );
466 
474 void rtx8_get_time_and_date ( rtc8_t *ctx, rtc8_time_t *time_s, rtc8_date_t *date_s );
475 
481 void rtc8_reset_alarm_flag ( rtc8_t *ctx );
482 
489 uint8_t rtc8_get_status( rtc8_t *ctx );
490 
497 uint8_t rtc8_get_alarm_flag( rtc8_t *ctx );
498 
499 #ifdef __cplusplus
500 }
501 #endif
502 #endif // _RTC8_H_
503  // End public_function group
506 
507 // ------------------------------------------------------------------------- END
void rtc8_generic_write(rtc8_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
void rtc8_set_external_event_input(rtc8_t *ctx, uint8_t state)
Set EVI(External Event Input) pin.
uint8_t weekdays
Definition: rtc8.h:295
uint8_t day
Definition: rtc8.h:292
RTC8_RETVAL rtc8_init(rtc8_t *ctx, rtc8_cfg_t *cfg)
Initialization function.
uint8_t slave_address
Definition: rtc8.h:250
uint8_t month
Definition: rtc8.h:293
Click configuration structure definition.
Definition: rtc8.h:257
uint8_t weekdays
Definition: rtc8.h:304
digital_out_t evi
Definition: rtc8.h:238
uint8_t minutes
Definition: rtc8.h:282
uint8_t seconds
Definition: rtc8.h:283
Time structure definition.
Definition: rtc8.h:279
digital_in_t int_pin
Definition: rtc8.h:242
uint8_t i2c_address
Definition: rtc8.h:272
uint8_t rtc8_get_status(rtc8_t *ctx)
Get status.
uint32_t i2c_speed
Definition: rtc8.h:271
uint8_t year
Definition: rtc8.h:294
uint8_t rtc8_set_time(rtc8_t *ctx, rtc8_time_t *time_s)
Set new time - 24 hour format.
void rtc8_generic_read(rtc8_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
uint8_t rtc8_read_data_bcd_format(rtc8_t *ctx, uint8_t reg)
Read one byte data from register in DEC format.
uint8_t rtc8_get_alarm_flag(rtc8_t *ctx)
Get Alarm flag.
#define RTC8_RETVAL
Definition: rtc8.h:63
uint32_t rtc8_get_uinx_time(rtc8_t *ctx)
Get current UNIX time.
pin_name_t int_pin
Definition: rtc8.h:267
void rtc8_set_unix_time(rtc8_t *ctx, int32_t unix_time)
Set UNIX time.
void rtx8_get_time_and_date(rtc8_t *ctx, rtc8_time_t *time_s, rtc8_date_t *date_s)
Get RTC data ( Time and Data )
Click ctx object definition.
Definition: rtc8.h:234
Alarm structure definition.
Definition: rtc8.h:302
i2c_master_t i2c
Definition: rtc8.h:246
pin_name_t evi
Definition: rtc8.h:266
pin_name_t sda
Definition: rtc8.h:262
void rtc8_cfg_setup(rtc8_cfg_t *cfg)
Config Object Initialization function.
uint8_t rtc8_set_alarm(rtc8_t *ctx, rtc8_alarm_t *alarm_s)
Set Alarm.
uint8_t hours
Definition: rtc8.h:305
void rtc8_default_cfg(rtc8_t *ctx)
Click Default Configuration function.
uint8_t rtc8_write_data_bcd_format(rtc8_t *ctx, uint8_t reg, uint8_t _data)
Write data ( Convert and send data in BCD format )
Date structure definition.
Definition: rtc8.h:290
uint8_t hours
Definition: rtc8.h:281
pin_name_t scl
Definition: rtc8.h:261
void rtc8_reset_alarm_flag(rtc8_t *ctx)
Reset alarm.
uint8_t minutes
Definition: rtc8.h:306
uint8_t rtc8_set_date(rtc8_t *ctx, rtc8_date_t *date_s)
Set new date.
uint8_t rtc8_get_interrupt_state(rtc8_t *ctx)
Get interrupt state.