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 
38 #include "mikrosdk_version.h"
39 
40 #ifdef __GNUC__
41 #if mikroSDK_GET_VERSION < 20800ul
42 #include "rcu_delays.h"
43 #else
44 #include "delays.h"
45 #endif
46 #endif
47 
48 #include "drv_digital_out.h"
49 #include "drv_digital_in.h"
50 #include "drv_i2c_master.h"
51 
52 // -------------------------------------------------------------- PUBLIC MACROS
62 #define RTC9_MAP_MIKROBUS( cfg, mikrobus ) \
63  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
64  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
65  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST )
66 
72 #define RTC9_RETVAL uint8_t
73 
74 #define RTC9_OK 0x00
75 #define RTC9_INIT_ERROR 0xFF
76 
82 #define RTC9_SLAVE_ADDRESS 0x68
83 
89 #define RTC9_REG_PART_SECONDS 0x00
90 #define RTC9_REG_SECONDS 0x01
91 #define RTC9_REG_MINUTES 0x02
92 #define RTC9_REG_CENTURY_HOURS 0x03
93 #define RTC9_REG_DAY 0x04
94 #define RTC9_REG_DATE 0x05
95 #define RTC9_REG_MONTH 0x06
96 #define RTC9_REG_YEAR 0x07
97 #define RTC9_REG_DIGITAL_CALIBRATION 0x08
98 #define RTC9_REG_WATCHDOG 0x09
99 #define RTC9_REG_ALARM1_MONTH 0x0A
100 #define RTC9_REG_ALARM1_DATE 0x0B
101 #define RTC9_REG_ALARM1_HOUR 0x0C
102 #define RTC9_REG_ALARM1_MINUTES 0x0D
103 #define RTC9_REG_ALARM1_SECONDS 0x0E
104 #define RTC9_REG_FLAGS 0x0F
105 #define RTC9_REG_TIMER_VALUE 0x10
106 #define RTC9_REG_TIMER_CONTROL 0x11
107 #define RTC9_REG_ANALOG_CALIBRATION 0x12
108 #define RTC9_REG_SQW 0x13
109 #define RTC9_REG_ALARM2_MONTH 0x14
110 #define RTC9_REG_ALARM2_DATE 0x15
111 #define RTC9_REG_ALARM2_HOUR 0x16
112 #define RTC9_REG_ALARM2_MINUTES 0x17
113 #define RTC9_REG_ALARM2_SECONDS 0x18
114 
120 #define RTC9_FLAG_WATCHDOG_ENABLE 0x80
121 #define RTC9_FLAG_WATCHDOG_DISABLE 0x00
122 #define RTC9_FLAG_ALARM_1_ENABLE 0x40
123 #define RTC9_FLAG_ALARM_1_DISABLE 0x00
124 #define RTC9_FLAG_ALARM_2_ENABLE 0x20
125 #define RTC9_FLAG_ALARM_2_DISABLE 0x00
126 #define RTC9_FLAG_BATTERY_LOW_ENABLE 0x10
127 #define RTC9_FLAG_BATTERY_LOW_DISABLE 0x00
128 #define RTC9_FLAG_TIMER_ENABLE 0x08
129 #define RTC9_FLAG_TIMER_DISABLE 0x00
130 #define RTC9_FLAG_OSCILATOR_FAIL_ENABLE 0x04
131 #define RTC9_FLAG_OSCILATOR_FAIL_DISABLE 0x00
132 
138 #define RTC9_TIMER_CONTROL_ENABLE 0x80
139 #define RTC9_TIMER_CONTROL_DISABLE 0x00
140 #define RTC9_TIMER_CONTROL_NORMAL_INT 0x00
141 #define RTC9_TIMER_CONTROL_FREE_RUNNING_INT 0x40
142 #define RTC9_TIMER_CONTROL_INT_OUT_ASSERT 0x20
143 #define RTC9_TIMER_CONTROL_INT_OUT_DE_ASSERT 0x00
144 #define RTC9_TIMER_CONTROL_TIMER_SOURCE_FREQ_244p1_US 0x00
145 #define RTC9_TIMER_CONTROL_TIMER_SOURCE_FREQ_15p6_MS 0x01
146 #define RTC9_TIMER_CONTROL_TIMER_SOURCE_FREQ_1_S 0x02
147 #define RTC9_TIMER_CONTROL_TIMER_SOURCE_FREQ_60_S 0x03
148 
154 #define RTC9_ANALOG_CALIBRATION_OSCILATOR_FREQ_NORMAL 0x00
155 #define RTC9_ANALOG_CALIBRATION_OSCILATOR_FREQ_SLOW 0x27
156 #define RTC9_ANALOG_CALIBRATION_OSCILATOR_FREQ_FAST 0xC8
157 
163 #define RTC9_DIGITAL_CALIBRATION_POSITIVE 0x20
164 #define RTC9_DIGITAL_CALIBRATION_NEGATIVE 0x00
165 #define RTC9_DIGITAL_CALIBRATION_FREQ_TEST_ENABLED 0x40
166 #define RTC9_DIGITAL_CALIBRATION_FREQ_TEST_DISABLED 0x00
167 #define RTC9_DIGITAL_CALIBRATION_VALUE_0 0x00
168 #define RTC9_DIGITAL_CALIBRATION_VALUE_1 0x01
169 #define RTC9_DIGITAL_CALIBRATION_VALUE_2 0x02
170 #define RTC9_DIGITAL_CALIBRATION_VALUE_3 0x03
171 #define RTC9_DIGITAL_CALIBRATION_VALUE_4 0x04
172 #define RTC9_DIGITAL_CALIBRATION_VALUE_5 0x05
173 #define RTC9_DIGITAL_CALIBRATION_VALUE_6 0x06
174 #define RTC9_DIGITAL_CALIBRATION_VALUE_7 0x07
175 #define RTC9_DIGITAL_CALIBRATION_VALUE_8 0x08
176 #define RTC9_DIGITAL_CALIBRATION_VALUE_9 0x09
177 #define RTC9_DIGITAL_CALIBRATION_VALUE_10 0x0A
178 #define RTC9_DIGITAL_CALIBRATION_VALUE_11 0x0B
179 #define RTC9_DIGITAL_CALIBRATION_VALUE_12 0x0C
180 #define RTC9_DIGITAL_CALIBRATION_VALUE_13 0x0D
181 #define RTC9_DIGITAL_CALIBRATION_VALUE_14 0x0E
182 #define RTC9_DIGITAL_CALIBRATION_VALUE_15 0x0F
183 #define RTC9_DIGITAL_CALIBRATION_VALUE_16 0x10
184 #define RTC9_DIGITAL_CALIBRATION_VALUE_17 0x11
185 #define RTC9_DIGITAL_CALIBRATION_VALUE_18 0x12
186 #define RTC9_DIGITAL_CALIBRATION_VALUE_19 0x13
187 #define RTC9_DIGITAL_CALIBRATION_VALUE_20 0x14
188 #define RTC9_DIGITAL_CALIBRATION_VALUE_21 0x15
189 #define RTC9_DIGITAL_CALIBRATION_VALUE_22 0x16
190 #define RTC9_DIGITAL_CALIBRATION_VALUE_23 0x17
191 #define RTC9_DIGITAL_CALIBRATION_VALUE_24 0x18
192 #define RTC9_DIGITAL_CALIBRATION_VALUE_25 0x19
193 #define RTC9_DIGITAL_CALIBRATION_VALUE_26 0x1A
194 #define RTC9_DIGITAL_CALIBRATION_VALUE_27 0x1B
195 #define RTC9_DIGITAL_CALIBRATION_VALUE_28 0x1C
196 #define RTC9_DIGITAL_CALIBRATION_VALUE_29 0x1D
197 #define RTC9_DIGITAL_CALIBRATION_VALUE_30 0x1E
198 #define RTC9_DIGITAL_CALIBRATION_VALUE_31 0x1F
199 
205 #define RTC9_WATCHDOG_OSCILATOR_FAIL_ENABLED 0x80
206 #define RTC9_WATCHDOG_OSCILATOR_FAIL_DISABLED 0x00
207 #define RTC9_WATCHDOG_RESOLUTION_1_DIV_16_SEC 0x00
208 #define RTC9_WATCHDOG_RESOLUTION_1_DIV_4_SEC 0x01
209 #define RTC9_WATCHDOG_RESOLUTION_1_SEC 0x02
210 #define RTC9_WATCHDOG_RESOLUTION_4_SEC 0x03
211 
217 #define RTC9_MONTH_JANUARY 0x01
218 #define RTC9_MONTH_FEBRUARY 0x02
219 #define RTC9_MONTH_MARCH 0x03
220 #define RTC9_MONTH_APRIL 0x04
221 #define RTC9_MONTH_MAY 0x05
222 #define RTC9_MONTH_JUNE 0x06
223 #define RTC9_MONTH_JULY 0x07
224 #define RTC9_MONTH_AUGUST 0x08
225 #define RTC9_MONTH_SEPTEMBER 0x09
226 #define RTC9_MONTH_OCTOBER 0x0A
227 #define RTC9_MONTH_NOVEMBER 0x0B
228 #define RTC9_MONTH_DECEMBER 0x0C
229 
235 #define RTC9_DAY_SUNDAY 0x01
236 #define RTC9_DAY_MONDAY 0x02
237 #define RTC9_DAY_TUESDAY 0x03
238 #define RTC9_DAY_WEDNESDAY 0x04
239 #define RTC9_DAY_THURSDAY 0x05
240 #define RTC9_DAY_FRIDAY 0x06
241 #define RTC9_DAY_SATURDAY 0x07
242  // End group macro
245 // --------------------------------------------------------------- PUBLIC TYPES
251 typedef struct
252 {
253  uint8_t day;
254  uint8_t day_of_week;
255  uint8_t month;
256  uint8_t year;
257 
259 
260 typedef struct
261 {
262  uint8_t hour;
263  uint8_t min;
264  uint8_t sec;
265  uint8_t part_sec;
266 
268 
269 typedef struct
270 {
271  uint8_t day;
272  uint8_t day_of_week;
273  uint8_t month;
274  uint8_t year;
275 
277 
281 typedef struct
282 {
283  // Output pins
284 
285  digital_out_t rst;
286 
287  // Modules
288 
289  i2c_master_t i2c;
290 
291  // ctx variable
292 
293  uint8_t slave_address;
294 
298 
299 } rtc9_t;
300 
304 typedef struct
305 {
306  // Communication gpio pins
307 
308  pin_name_t scl;
309  pin_name_t sda;
310 
311  // Additional gpio pins
312 
313  pin_name_t rst;
314 
315  // static variable
316 
317  uint32_t i2c_speed;
318  uint8_t i2c_address;
319 
320 } rtc9_cfg_t;
321  // End types group
323 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
324 
330 #ifdef __cplusplus
331 extern "C"{
332 #endif
333 
343 
353 
364 void rtc9_generic_write ( rtc9_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
365 
376 void rtc9_generic_read ( rtc9_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
377 
385 void rtc9_wakeup ( rtc9_t *ctx );
386 
394 
402 
411 void rtc9_set_time( rtc9_t *ctx, uint8_t hour, uint8_t min, uint8_t sec );
412 
420 void rtc9_set_date ( rtc9_t *ctx, rtc9_set_data_t *set_data );
421 
429 void rtc9_get_time( rtc9_t *ctx, rtc9_get_time_t *get_time );
430 
438 void rtc9_get_date( rtc9_t *ctx, rtc9_get_date_t *get_data );
439 
447 char *rtc9_current_month( uint8_t month );
448 
456 char *rtc9_current_day_of_week( uint8_t day_of_week );
457 
466 void rtc9_set_alarm_time( rtc9_t *ctx, uint8_t hour, uint8_t min, uint8_t sec );
467 
476 void rtc9_set_alarm_date ( rtc9_t *ctx, uint8_t day, uint8_t month, uint8_t year );
477 
485 void rtc9_read_flag_status ( rtc9_t *ctx, uint8_t *read_data );
486 
487 #ifdef __cplusplus
488 }
489 #endif
490 #endif // _RTC9_H_
491  // End public_function group
494 
495 // ------------------------------------------------------------------------- END
rtc9_get_time_t
Definition: rtc9.h:261
rtc9_t::get_date
rtc9_get_date_t get_date
Definition: rtc9.h:297
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:263
rtc9_set_data_t::day_of_week
uint8_t day_of_week
Definition: rtc9.h:254
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:309
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:272
rtc9_set_data_t::month
uint8_t month
Definition: rtc9.h:255
rtc9_get_date_t
Definition: rtc9.h:270
rtc9_t::slave_address
uint8_t slave_address
Definition: rtc9.h:293
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:252
rtc9_set_data_t::year
uint8_t year
Definition: rtc9.h:256
rtc9_get_time_t::part_sec
uint8_t part_sec
Definition: rtc9.h:265
rtc9_get_date_t::month
uint8_t month
Definition: rtc9.h:273
rtc9_cfg_t::scl
pin_name_t scl
Definition: rtc9.h:308
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:271
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:313
rtc9_cfg_t
Click configuration structure definition.
Definition: rtc9.h:305
rtc9_t::i2c
i2c_master_t i2c
Definition: rtc9.h:289
rtc9_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: rtc9.h:317
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:318
rtc9_t::set_data
rtc9_set_data_t set_data
Definition: rtc9.h:295
rtc9_get_date_t::year
uint8_t year
Definition: rtc9.h:274
RTC9_RETVAL
#define RTC9_RETVAL
Definition: rtc9.h:72
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:262
rtc9_get_time_t::sec
uint8_t sec
Definition: rtc9.h:264
rtc9_t::get_time
rtc9_get_time_t get_time
Definition: rtc9.h:296
rtc9_set_data_t::day
uint8_t day
Definition: rtc9.h:253
rtc9_t
Click ctx object definition.
Definition: rtc9.h:282
rtc9_t::rst
digital_out_t rst
Definition: rtc9.h:285