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 "drv_digital_out.h"
39 #include "drv_digital_in.h"
40 #include "drv_i2c_master.h"
41 
42 // -------------------------------------------------------------- PUBLIC MACROS
52 #define RTC9_MAP_MIKROBUS( cfg, mikrobus ) \
53  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
54  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
55  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST )
56 
62 #define RTC9_RETVAL uint8_t
63 
64 #define RTC9_OK 0x00
65 #define RTC9_INIT_ERROR 0xFF
66 
72 #define RTC9_SLAVE_ADDRESS 0x68
73 
79 #define RTC9_REG_PART_SECONDS 0x00
80 #define RTC9_REG_SECONDS 0x01
81 #define RTC9_REG_MINUTES 0x02
82 #define RTC9_REG_CENTURY_HOURS 0x03
83 #define RTC9_REG_DAY 0x04
84 #define RTC9_REG_DATE 0x05
85 #define RTC9_REG_MONTH 0x06
86 #define RTC9_REG_YEAR 0x07
87 #define RTC9_REG_DIGITAL_CALIBRATION 0x08
88 #define RTC9_REG_WATCHDOG 0x09
89 #define RTC9_REG_ALARM1_MONTH 0x0A
90 #define RTC9_REG_ALARM1_DATE 0x0B
91 #define RTC9_REG_ALARM1_HOUR 0x0C
92 #define RTC9_REG_ALARM1_MINUTES 0x0D
93 #define RTC9_REG_ALARM1_SECONDS 0x0E
94 #define RTC9_REG_FLAGS 0x0F
95 #define RTC9_REG_TIMER_VALUE 0x10
96 #define RTC9_REG_TIMER_CONTROL 0x11
97 #define RTC9_REG_ANALOG_CALIBRATION 0x12
98 #define RTC9_REG_SQW 0x13
99 #define RTC9_REG_ALARM2_MONTH 0x14
100 #define RTC9_REG_ALARM2_DATE 0x15
101 #define RTC9_REG_ALARM2_HOUR 0x16
102 #define RTC9_REG_ALARM2_MINUTES 0x17
103 #define RTC9_REG_ALARM2_SECONDS 0x18
104 
110 #define RTC9_FLAG_WATCHDOG_ENABLE 0x80
111 #define RTC9_FLAG_WATCHDOG_DISABLE 0x00
112 #define RTC9_FLAG_ALARM_1_ENABLE 0x40
113 #define RTC9_FLAG_ALARM_1_DISABLE 0x00
114 #define RTC9_FLAG_ALARM_2_ENABLE 0x20
115 #define RTC9_FLAG_ALARM_2_DISABLE 0x00
116 #define RTC9_FLAG_BATTERY_LOW_ENABLE 0x10
117 #define RTC9_FLAG_BATTERY_LOW_DISABLE 0x00
118 #define RTC9_FLAG_TIMER_ENABLE 0x08
119 #define RTC9_FLAG_TIMER_DISABLE 0x00
120 #define RTC9_FLAG_OSCILATOR_FAIL_ENABLE 0x04
121 #define RTC9_FLAG_OSCILATOR_FAIL_DISABLE 0x00
122 
128 #define RTC9_TIMER_CONTROL_ENABLE 0x80
129 #define RTC9_TIMER_CONTROL_DISABLE 0x00
130 #define RTC9_TIMER_CONTROL_NORMAL_INT 0x00
131 #define RTC9_TIMER_CONTROL_FREE_RUNNING_INT 0x40
132 #define RTC9_TIMER_CONTROL_INT_OUT_ASSERT 0x20
133 #define RTC9_TIMER_CONTROL_INT_OUT_DE_ASSERT 0x00
134 #define RTC9_TIMER_CONTROL_TIMER_SOURCE_FREQ_244p1_US 0x00
135 #define RTC9_TIMER_CONTROL_TIMER_SOURCE_FREQ_15p6_MS 0x01
136 #define RTC9_TIMER_CONTROL_TIMER_SOURCE_FREQ_1_S 0x02
137 #define RTC9_TIMER_CONTROL_TIMER_SOURCE_FREQ_60_S 0x03
138 
144 #define RTC9_ANALOG_CALIBRATION_OSCILATOR_FREQ_NORMAL 0x00
145 #define RTC9_ANALOG_CALIBRATION_OSCILATOR_FREQ_SLOW 0x27
146 #define RTC9_ANALOG_CALIBRATION_OSCILATOR_FREQ_FAST 0xC8
147 
153 #define RTC9_DIGITAL_CALIBRATION_POSITIVE 0x20
154 #define RTC9_DIGITAL_CALIBRATION_NEGATIVE 0x00
155 #define RTC9_DIGITAL_CALIBRATION_FREQ_TEST_ENABLED 0x40
156 #define RTC9_DIGITAL_CALIBRATION_FREQ_TEST_DISABLED 0x00
157 #define RTC9_DIGITAL_CALIBRATION_VALUE_0 0x00
158 #define RTC9_DIGITAL_CALIBRATION_VALUE_1 0x01
159 #define RTC9_DIGITAL_CALIBRATION_VALUE_2 0x02
160 #define RTC9_DIGITAL_CALIBRATION_VALUE_3 0x03
161 #define RTC9_DIGITAL_CALIBRATION_VALUE_4 0x04
162 #define RTC9_DIGITAL_CALIBRATION_VALUE_5 0x05
163 #define RTC9_DIGITAL_CALIBRATION_VALUE_6 0x06
164 #define RTC9_DIGITAL_CALIBRATION_VALUE_7 0x07
165 #define RTC9_DIGITAL_CALIBRATION_VALUE_8 0x08
166 #define RTC9_DIGITAL_CALIBRATION_VALUE_9 0x09
167 #define RTC9_DIGITAL_CALIBRATION_VALUE_10 0x0A
168 #define RTC9_DIGITAL_CALIBRATION_VALUE_11 0x0B
169 #define RTC9_DIGITAL_CALIBRATION_VALUE_12 0x0C
170 #define RTC9_DIGITAL_CALIBRATION_VALUE_13 0x0D
171 #define RTC9_DIGITAL_CALIBRATION_VALUE_14 0x0E
172 #define RTC9_DIGITAL_CALIBRATION_VALUE_15 0x0F
173 #define RTC9_DIGITAL_CALIBRATION_VALUE_16 0x10
174 #define RTC9_DIGITAL_CALIBRATION_VALUE_17 0x11
175 #define RTC9_DIGITAL_CALIBRATION_VALUE_18 0x12
176 #define RTC9_DIGITAL_CALIBRATION_VALUE_19 0x13
177 #define RTC9_DIGITAL_CALIBRATION_VALUE_20 0x14
178 #define RTC9_DIGITAL_CALIBRATION_VALUE_21 0x15
179 #define RTC9_DIGITAL_CALIBRATION_VALUE_22 0x16
180 #define RTC9_DIGITAL_CALIBRATION_VALUE_23 0x17
181 #define RTC9_DIGITAL_CALIBRATION_VALUE_24 0x18
182 #define RTC9_DIGITAL_CALIBRATION_VALUE_25 0x19
183 #define RTC9_DIGITAL_CALIBRATION_VALUE_26 0x1A
184 #define RTC9_DIGITAL_CALIBRATION_VALUE_27 0x1B
185 #define RTC9_DIGITAL_CALIBRATION_VALUE_28 0x1C
186 #define RTC9_DIGITAL_CALIBRATION_VALUE_29 0x1D
187 #define RTC9_DIGITAL_CALIBRATION_VALUE_30 0x1E
188 #define RTC9_DIGITAL_CALIBRATION_VALUE_31 0x1F
189 
195 #define RTC9_WATCHDOG_OSCILATOR_FAIL_ENABLED 0x80
196 #define RTC9_WATCHDOG_OSCILATOR_FAIL_DISABLED 0x00
197 #define RTC9_WATCHDOG_RESOLUTION_1_DIV_16_SEC 0x00
198 #define RTC9_WATCHDOG_RESOLUTION_1_DIV_4_SEC 0x01
199 #define RTC9_WATCHDOG_RESOLUTION_1_SEC 0x02
200 #define RTC9_WATCHDOG_RESOLUTION_4_SEC 0x03
201 
207 #define RTC9_MONTH_JANUARY 0x01
208 #define RTC9_MONTH_FEBRUARY 0x02
209 #define RTC9_MONTH_MARCH 0x03
210 #define RTC9_MONTH_APRIL 0x04
211 #define RTC9_MONTH_MAY 0x05
212 #define RTC9_MONTH_JUNE 0x06
213 #define RTC9_MONTH_JULY 0x07
214 #define RTC9_MONTH_AUGUST 0x08
215 #define RTC9_MONTH_SEPTEMBER 0x09
216 #define RTC9_MONTH_OCTOBER 0x0A
217 #define RTC9_MONTH_NOVEMBER 0x0B
218 #define RTC9_MONTH_DECEMBER 0x0C
219 
225 #define RTC9_DAY_SUNDAY 0x01
226 #define RTC9_DAY_MONDAY 0x02
227 #define RTC9_DAY_TUESDAY 0x03
228 #define RTC9_DAY_WEDNESDAY 0x04
229 #define RTC9_DAY_THURSDAY 0x05
230 #define RTC9_DAY_FRIDAY 0x06
231 #define RTC9_DAY_SATURDAY 0x07
232  // End group macro
235 // --------------------------------------------------------------- PUBLIC TYPES
241 typedef struct
242 {
243  uint8_t day;
244  uint8_t day_of_week;
245  uint8_t month;
246  uint8_t year;
247 
249 
250 typedef struct
251 {
252  uint8_t hour;
253  uint8_t min;
254  uint8_t sec;
255  uint8_t part_sec;
256 
258 
259 typedef struct
260 {
261  uint8_t day;
262  uint8_t day_of_week;
263  uint8_t month;
264  uint8_t year;
265 
267 
271 typedef struct
272 {
273  // Output pins
274 
275  digital_out_t rst;
276 
277  // Modules
278 
279  i2c_master_t i2c;
280 
281  // ctx variable
282 
283  uint8_t slave_address;
284 
288 
289 } rtc9_t;
290 
294 typedef struct
295 {
296  // Communication gpio pins
297 
298  pin_name_t scl;
299  pin_name_t sda;
300 
301  // Additional gpio pins
302 
303  pin_name_t rst;
304 
305  // static variable
306 
307  uint32_t i2c_speed;
308  uint8_t i2c_address;
309 
310 } rtc9_cfg_t;
311  // End types group
313 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
314 
320 #ifdef __cplusplus
321 extern "C"{
322 #endif
323 
332 void rtc9_cfg_setup ( rtc9_cfg_t *cfg );
333 
342 RTC9_RETVAL rtc9_init ( rtc9_t *ctx, rtc9_cfg_t *cfg );
343 
354 void rtc9_generic_write ( rtc9_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
355 
366 void rtc9_generic_read ( rtc9_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
367 
375 void rtc9_wakeup ( rtc9_t *ctx );
376 
383 void rtc9_software_reset( rtc9_t *ctx ) ;
384 
391 void rtc9_hardware_reset ( rtc9_t *ctx );
392 
401 void rtc9_set_time( rtc9_t *ctx, uint8_t hour, uint8_t min, uint8_t sec );
402 
410 void rtc9_set_date ( rtc9_t *ctx, rtc9_set_data_t *set_data );
411 
419 void rtc9_get_time( rtc9_t *ctx, rtc9_get_time_t *get_time );
420 
428 void rtc9_get_date( rtc9_t *ctx, rtc9_get_date_t *get_data );
429 
437 char *rtc9_current_month( uint8_t month );
438 
446 char *rtc9_current_day_of_week( uint8_t day_of_week );
447 
456 void rtc9_set_alarm_time( rtc9_t *ctx, uint8_t hour, uint8_t min, uint8_t sec );
457 
466 void rtc9_set_alarm_date ( rtc9_t *ctx, uint8_t day, uint8_t month, uint8_t year );
467 
475 void rtc9_read_flag_status ( rtc9_t *ctx, uint8_t *read_data );
476 
477 #ifdef __cplusplus
478 }
479 #endif
480 #endif // _RTC9_H_
481  // End public_function group
484 
485 // ------------------------------------------------------------------------- END
rtc9_get_time_t
Definition: rtc9.h:250
rtc9_t::get_date
rtc9_get_date_t get_date
Definition: rtc9.h:287
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:253
rtc9_set_data_t::day_of_week
uint8_t day_of_week
Definition: rtc9.h:244
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:299
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:262
rtc9_set_data_t::month
uint8_t month
Definition: rtc9.h:245
rtc9_get_date_t
Definition: rtc9.h:259
rtc9_t::slave_address
uint8_t slave_address
Definition: rtc9.h:283
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:241
rtc9_set_data_t::year
uint8_t year
Definition: rtc9.h:246
rtc9_get_time_t::part_sec
uint8_t part_sec
Definition: rtc9.h:255
rtc9_get_date_t::month
uint8_t month
Definition: rtc9.h:263
rtc9_cfg_t::scl
pin_name_t scl
Definition: rtc9.h:298
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:261
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:303
rtc9_cfg_t
Click configuration structure definition.
Definition: rtc9.h:294
rtc9_t::i2c
i2c_master_t i2c
Definition: rtc9.h:279
rtc9_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: rtc9.h:307
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:308
rtc9_t::set_data
rtc9_set_data_t set_data
Definition: rtc9.h:285
rtc9_get_date_t::year
uint8_t year
Definition: rtc9.h:264
RTC9_RETVAL
#define RTC9_RETVAL
Definition: rtc9.h:62
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:252
rtc9_get_time_t::sec
uint8_t sec
Definition: rtc9.h:254
rtc9_t::get_time
rtc9_get_time_t get_time
Definition: rtc9.h:286
rtc9_set_data_t::day
uint8_t day
Definition: rtc9.h:243
rtc9_t
Click ctx object definition.
Definition: rtc9.h:271
rtc9_t::rst
digital_out_t rst
Definition: rtc9.h:275