rtc9  2.0.0.0
rtc9.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 RTC9_H
36 #define RTC9_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 RTC9_MAP_MIKROBUS( cfg, mikrobus ) \
67  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
68  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
69  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST )
70 
76 #define RTC9_RETVAL uint8_t
77 
78 #define RTC9_OK 0x00
79 #define RTC9_INIT_ERROR 0xFF
80 
86 #define RTC9_SLAVE_ADDRESS 0x68
87 
93 #define RTC9_REG_PART_SECONDS 0x00
94 #define RTC9_REG_SECONDS 0x01
95 #define RTC9_REG_MINUTES 0x02
96 #define RTC9_REG_CENTURY_HOURS 0x03
97 #define RTC9_REG_DAY 0x04
98 #define RTC9_REG_DATE 0x05
99 #define RTC9_REG_MONTH 0x06
100 #define RTC9_REG_YEAR 0x07
101 #define RTC9_REG_DIGITAL_CALIBRATION 0x08
102 #define RTC9_REG_WATCHDOG 0x09
103 #define RTC9_REG_ALARM1_MONTH 0x0A
104 #define RTC9_REG_ALARM1_DATE 0x0B
105 #define RTC9_REG_ALARM1_HOUR 0x0C
106 #define RTC9_REG_ALARM1_MINUTES 0x0D
107 #define RTC9_REG_ALARM1_SECONDS 0x0E
108 #define RTC9_REG_FLAGS 0x0F
109 #define RTC9_REG_TIMER_VALUE 0x10
110 #define RTC9_REG_TIMER_CONTROL 0x11
111 #define RTC9_REG_ANALOG_CALIBRATION 0x12
112 #define RTC9_REG_SQW 0x13
113 #define RTC9_REG_ALARM2_MONTH 0x14
114 #define RTC9_REG_ALARM2_DATE 0x15
115 #define RTC9_REG_ALARM2_HOUR 0x16
116 #define RTC9_REG_ALARM2_MINUTES 0x17
117 #define RTC9_REG_ALARM2_SECONDS 0x18
118 
124 #define RTC9_FLAG_WATCHDOG_ENABLE 0x80
125 #define RTC9_FLAG_WATCHDOG_DISABLE 0x00
126 #define RTC9_FLAG_ALARM_1_ENABLE 0x40
127 #define RTC9_FLAG_ALARM_1_DISABLE 0x00
128 #define RTC9_FLAG_ALARM_2_ENABLE 0x20
129 #define RTC9_FLAG_ALARM_2_DISABLE 0x00
130 #define RTC9_FLAG_BATTERY_LOW_ENABLE 0x10
131 #define RTC9_FLAG_BATTERY_LOW_DISABLE 0x00
132 #define RTC9_FLAG_TIMER_ENABLE 0x08
133 #define RTC9_FLAG_TIMER_DISABLE 0x00
134 #define RTC9_FLAG_OSCILATOR_FAIL_ENABLE 0x04
135 #define RTC9_FLAG_OSCILATOR_FAIL_DISABLE 0x00
136 
142 #define RTC9_TIMER_CONTROL_ENABLE 0x80
143 #define RTC9_TIMER_CONTROL_DISABLE 0x00
144 #define RTC9_TIMER_CONTROL_NORMAL_INT 0x00
145 #define RTC9_TIMER_CONTROL_FREE_RUNNING_INT 0x40
146 #define RTC9_TIMER_CONTROL_INT_OUT_ASSERT 0x20
147 #define RTC9_TIMER_CONTROL_INT_OUT_DE_ASSERT 0x00
148 #define RTC9_TIMER_CONTROL_TIMER_SOURCE_FREQ_244p1_US 0x00
149 #define RTC9_TIMER_CONTROL_TIMER_SOURCE_FREQ_15p6_MS 0x01
150 #define RTC9_TIMER_CONTROL_TIMER_SOURCE_FREQ_1_S 0x02
151 #define RTC9_TIMER_CONTROL_TIMER_SOURCE_FREQ_60_S 0x03
152 
158 #define RTC9_ANALOG_CALIBRATION_OSCILATOR_FREQ_NORMAL 0x00
159 #define RTC9_ANALOG_CALIBRATION_OSCILATOR_FREQ_SLOW 0x27
160 #define RTC9_ANALOG_CALIBRATION_OSCILATOR_FREQ_FAST 0xC8
161 
167 #define RTC9_DIGITAL_CALIBRATION_POSITIVE 0x20
168 #define RTC9_DIGITAL_CALIBRATION_NEGATIVE 0x00
169 #define RTC9_DIGITAL_CALIBRATION_FREQ_TEST_ENABLED 0x40
170 #define RTC9_DIGITAL_CALIBRATION_FREQ_TEST_DISABLED 0x00
171 #define RTC9_DIGITAL_CALIBRATION_VALUE_0 0x00
172 #define RTC9_DIGITAL_CALIBRATION_VALUE_1 0x01
173 #define RTC9_DIGITAL_CALIBRATION_VALUE_2 0x02
174 #define RTC9_DIGITAL_CALIBRATION_VALUE_3 0x03
175 #define RTC9_DIGITAL_CALIBRATION_VALUE_4 0x04
176 #define RTC9_DIGITAL_CALIBRATION_VALUE_5 0x05
177 #define RTC9_DIGITAL_CALIBRATION_VALUE_6 0x06
178 #define RTC9_DIGITAL_CALIBRATION_VALUE_7 0x07
179 #define RTC9_DIGITAL_CALIBRATION_VALUE_8 0x08
180 #define RTC9_DIGITAL_CALIBRATION_VALUE_9 0x09
181 #define RTC9_DIGITAL_CALIBRATION_VALUE_10 0x0A
182 #define RTC9_DIGITAL_CALIBRATION_VALUE_11 0x0B
183 #define RTC9_DIGITAL_CALIBRATION_VALUE_12 0x0C
184 #define RTC9_DIGITAL_CALIBRATION_VALUE_13 0x0D
185 #define RTC9_DIGITAL_CALIBRATION_VALUE_14 0x0E
186 #define RTC9_DIGITAL_CALIBRATION_VALUE_15 0x0F
187 #define RTC9_DIGITAL_CALIBRATION_VALUE_16 0x10
188 #define RTC9_DIGITAL_CALIBRATION_VALUE_17 0x11
189 #define RTC9_DIGITAL_CALIBRATION_VALUE_18 0x12
190 #define RTC9_DIGITAL_CALIBRATION_VALUE_19 0x13
191 #define RTC9_DIGITAL_CALIBRATION_VALUE_20 0x14
192 #define RTC9_DIGITAL_CALIBRATION_VALUE_21 0x15
193 #define RTC9_DIGITAL_CALIBRATION_VALUE_22 0x16
194 #define RTC9_DIGITAL_CALIBRATION_VALUE_23 0x17
195 #define RTC9_DIGITAL_CALIBRATION_VALUE_24 0x18
196 #define RTC9_DIGITAL_CALIBRATION_VALUE_25 0x19
197 #define RTC9_DIGITAL_CALIBRATION_VALUE_26 0x1A
198 #define RTC9_DIGITAL_CALIBRATION_VALUE_27 0x1B
199 #define RTC9_DIGITAL_CALIBRATION_VALUE_28 0x1C
200 #define RTC9_DIGITAL_CALIBRATION_VALUE_29 0x1D
201 #define RTC9_DIGITAL_CALIBRATION_VALUE_30 0x1E
202 #define RTC9_DIGITAL_CALIBRATION_VALUE_31 0x1F
203 
209 #define RTC9_WATCHDOG_OSCILATOR_FAIL_ENABLED 0x80
210 #define RTC9_WATCHDOG_OSCILATOR_FAIL_DISABLED 0x00
211 #define RTC9_WATCHDOG_RESOLUTION_1_DIV_16_SEC 0x00
212 #define RTC9_WATCHDOG_RESOLUTION_1_DIV_4_SEC 0x01
213 #define RTC9_WATCHDOG_RESOLUTION_1_SEC 0x02
214 #define RTC9_WATCHDOG_RESOLUTION_4_SEC 0x03
215 
221 #define RTC9_MONTH_JANUARY 0x01
222 #define RTC9_MONTH_FEBRUARY 0x02
223 #define RTC9_MONTH_MARCH 0x03
224 #define RTC9_MONTH_APRIL 0x04
225 #define RTC9_MONTH_MAY 0x05
226 #define RTC9_MONTH_JUNE 0x06
227 #define RTC9_MONTH_JULY 0x07
228 #define RTC9_MONTH_AUGUST 0x08
229 #define RTC9_MONTH_SEPTEMBER 0x09
230 #define RTC9_MONTH_OCTOBER 0x0A
231 #define RTC9_MONTH_NOVEMBER 0x0B
232 #define RTC9_MONTH_DECEMBER 0x0C
233 
239 #define RTC9_DAY_SUNDAY 0x01
240 #define RTC9_DAY_MONDAY 0x02
241 #define RTC9_DAY_TUESDAY 0x03
242 #define RTC9_DAY_WEDNESDAY 0x04
243 #define RTC9_DAY_THURSDAY 0x05
244 #define RTC9_DAY_FRIDAY 0x06
245 #define RTC9_DAY_SATURDAY 0x07
246  // End group macro
249 // --------------------------------------------------------------- PUBLIC TYPES
255 typedef struct
256 {
257  uint8_t day;
258  uint8_t day_of_week;
259  uint8_t month;
260  uint8_t year;
261 
263 
264 typedef struct
265 {
266  uint8_t hour;
267  uint8_t min;
268  uint8_t sec;
269  uint8_t part_sec;
270 
272 
273 typedef struct
274 {
275  uint8_t day;
276  uint8_t day_of_week;
277  uint8_t month;
278  uint8_t year;
279 
281 
285 typedef struct
286 {
287  // Output pins
288 
289  digital_out_t rst;
290 
291  // Modules
292 
293  i2c_master_t i2c;
294 
295  // ctx variable
296 
297  uint8_t slave_address;
298 
302 
303 } rtc9_t;
304 
308 typedef struct
309 {
310  // Communication gpio pins
311 
312  pin_name_t scl;
313  pin_name_t sda;
314 
315  // Additional gpio pins
316 
317  pin_name_t rst;
318 
319  // static variable
320 
321  uint32_t i2c_speed;
322  uint8_t i2c_address;
323 
324 } rtc9_cfg_t;
325  // End types group
327 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
328 
334 #ifdef __cplusplus
335 extern "C"{
336 #endif
337 
347 
357 
368 void rtc9_generic_write ( rtc9_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
369 
380 void rtc9_generic_read ( rtc9_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
381 
389 void rtc9_wakeup ( rtc9_t *ctx );
390 
398 
406 
415 void rtc9_set_time( rtc9_t *ctx, uint8_t hour, uint8_t min, uint8_t sec );
416 
424 void rtc9_set_date ( rtc9_t *ctx, rtc9_set_data_t *set_data );
425 
433 void rtc9_get_time( rtc9_t *ctx, rtc9_get_time_t *get_time );
434 
442 void rtc9_get_date( rtc9_t *ctx, rtc9_get_date_t *get_data );
443 
451 char *rtc9_current_month( uint8_t month );
452 
460 char *rtc9_current_day_of_week( uint8_t day_of_week );
461 
470 void rtc9_set_alarm_time( rtc9_t *ctx, uint8_t hour, uint8_t min, uint8_t sec );
471 
480 void rtc9_set_alarm_date ( rtc9_t *ctx, uint8_t day, uint8_t month, uint8_t year );
481 
489 void rtc9_read_flag_status ( rtc9_t *ctx, uint8_t *read_data );
490 
491 #ifdef __cplusplus
492 }
493 #endif
494 #endif // _RTC9_H_
495  // End public_function group
498 
499 // ------------------------------------------------------------------------- END
rtc9_get_time_t
Definition: rtc9.h:265
rtc9_t::get_date
rtc9_get_date_t get_date
Definition: rtc9.h:301
rtc9_set_date
void rtc9_set_date(rtc9_t *ctx, rtc9_set_data_t *set_data)
Set new date.
rtc9_get_time_t::min
uint8_t min
Definition: rtc9.h:267
rtc9_set_data_t::day_of_week
uint8_t day_of_week
Definition: rtc9.h:258
rtc9_set_alarm_date
void rtc9_set_alarm_date(rtc9_t *ctx, uint8_t day, uint8_t month, uint8_t year)
Set new ALARM 1 date.
rtc9_wakeup
void rtc9_wakeup(rtc9_t *ctx)
Wake-up process.
rtc9_current_month
char * rtc9_current_month(uint8_t month)
Month in the String format.
rtc9_cfg_t::sda
pin_name_t sda
Definition: rtc9.h:313
rtc9_init
RTC9_RETVAL rtc9_init(rtc9_t *ctx, rtc9_cfg_t *cfg)
Initialization function.
rtc9_get_date_t::day_of_week
uint8_t day_of_week
Definition: rtc9.h:276
rtc9_set_data_t::month
uint8_t month
Definition: rtc9.h:259
rtc9_get_date_t
Definition: rtc9.h:274
rtc9_t::slave_address
uint8_t slave_address
Definition: rtc9.h:297
rtc9_generic_read
void rtc9_generic_read(rtc9_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
rtc9_set_data_t
Definition: rtc9.h:256
rtc9_set_data_t::year
uint8_t year
Definition: rtc9.h:260
rtc9_get_time_t::part_sec
uint8_t part_sec
Definition: rtc9.h:269
rtc9_get_date_t::month
uint8_t month
Definition: rtc9.h:277
rtc9_cfg_t::scl
pin_name_t scl
Definition: rtc9.h:312
rtc9_cfg_setup
void rtc9_cfg_setup(rtc9_cfg_t *cfg)
Config Object Initialization function.
rtc9_set_alarm_time
void rtc9_set_alarm_time(rtc9_t *ctx, uint8_t hour, uint8_t min, uint8_t sec)
Set new ALARM 1 time - 24 hour format.
rtc9_hardware_reset
void rtc9_hardware_reset(rtc9_t *ctx)
Hardware reset.
rtc9_software_reset
void rtc9_software_reset(rtc9_t *ctx)
Software reset.
rtc9_get_date_t::day
uint8_t day
Definition: rtc9.h:275
rtc9_generic_write
void rtc9_generic_write(rtc9_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
rtc9_current_day_of_week
char * rtc9_current_day_of_week(uint8_t day_of_week)
Day of Week in the String format.
rtc9_cfg_t::rst
pin_name_t rst
Definition: rtc9.h:317
rtc9_cfg_t
Click configuration structure definition.
Definition: rtc9.h:309
rtc9_t::i2c
i2c_master_t i2c
Definition: rtc9.h:293
rtc9_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: rtc9.h:321
rtc9_read_flag_status
void rtc9_read_flag_status(rtc9_t *ctx, uint8_t *read_data)
Reads FLAGS regiter.
rtc9_get_time
void rtc9_get_time(rtc9_t *ctx, rtc9_get_time_t *get_time)
Get new time - 24 hour format.
rtc9_get_date
void rtc9_get_date(rtc9_t *ctx, rtc9_get_date_t *get_data)
Get new date.
rtc9_cfg_t::i2c_address
uint8_t i2c_address
Definition: rtc9.h:322
rtc9_t::set_data
rtc9_set_data_t set_data
Definition: rtc9.h:299
rtc9_get_date_t::year
uint8_t year
Definition: rtc9.h:278
RTC9_RETVAL
#define RTC9_RETVAL
Definition: rtc9.h:76
rtc9_set_time
void rtc9_set_time(rtc9_t *ctx, uint8_t hour, uint8_t min, uint8_t sec)
Set new time - 24 hour format.
rtc9_get_time_t::hour
uint8_t hour
Definition: rtc9.h:266
rtc9_get_time_t::sec
uint8_t sec
Definition: rtc9.h:268
rtc9_t::get_time
rtc9_get_time_t get_time
Definition: rtc9.h:300
rtc9_set_data_t::day
uint8_t day
Definition: rtc9.h:257
rtc9_t
Click ctx object definition.
Definition: rtc9.h:286
rtc9_t::rst
digital_out_t rst
Definition: rtc9.h:289