Rev 36 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
18 | jlesech | 1 | /**************************************************************************//** |
2 | * \brief DS1307 RTC library |
||
3 | * \author Copyright (C) 2009 Julien Le Sech - www.idreammicro.com |
||
4 | * \version 1.0 |
||
5 | * \date 20090501 |
||
6 | * |
||
7 | * This file is part of the iDreamMicro library. |
||
8 | * |
||
9 | * This library is free software: you can redistribute it and/or modify it under |
||
10 | * the terms of the GNU Lesser General Public License as published by the Free |
||
11 | * Software Foundation, either version 3 of the License, or (at your option) any |
||
12 | * later version. |
||
13 | * |
||
14 | * This library is distributed in the hope that it will be useful, but WITHOUT |
||
15 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
||
16 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
||
17 | * details. |
||
18 | * |
||
19 | * You should have received a copy of the GNU Lesser General Public License |
||
20 | * along with this program. If not, see http://www.gnu.org/licenses/ |
||
21 | ******************************************************************************/ |
||
22 | |||
23 | /**************************************************************************//** |
||
24 | * \file ds1307.c |
||
25 | ******************************************************************************/ |
||
26 | |||
27 | /****************************************************************************** |
||
28 | * Header file inclusions. |
||
29 | ******************************************************************************/ |
||
30 | |||
31 | #include <ds1307/ds1307.h> |
||
32 | |||
33 | #include <twi/twi.h> |
||
34 | #include <useful/datetime.h> |
||
35 | |||
36 | #include <assert.h> |
||
37 | #include <stdlib.h> |
||
38 | |||
39 | /****************************************************************************** |
||
40 | * Private macros. |
||
41 | ******************************************************************************/ |
||
42 | |||
43 | #define DS1307__ADDRESS 0x68 |
||
44 | #define DS1307__SLAVE_ADDRESS (DS1307__ADDRESS << 1) |
||
45 | |||
46 | #define DS1307__REGISTER_ADDRESS__SECONDS 0x00 |
||
47 | #define DS1307__REGISTER_ADDRESS__MINUTES 0x01 |
||
48 | #define DS1307__REGISTER_ADDRESS__HOURS 0x02 |
||
49 | #define DS1307__REGISTER_ADDRESS__DAY 0x03 |
||
50 | #define DS1307__REGISTER_ADDRESS__DATE 0x04 |
||
51 | #define DS1307__REGISTER_ADDRESS__MONTH 0x05 |
||
52 | #define DS1307__REGISTER_ADDRESS__YEAR 0x06 |
||
53 | #define DS1307__REGISTER_ADDRESS__CONTROL 0x07 |
||
54 | |||
55 | #define DS1307__CLOCK_RATE 100000 |
||
56 | |||
57 | /****************************************************************************** |
||
58 | * Private function prototypes. |
||
59 | ******************************************************************************/ |
||
60 | |||
61 | /**************************************************************************//** |
||
62 | * \fn static void ds1307__set_start_address(void) |
||
63 | * |
||
64 | * \brief Set RTC address to 0. |
||
65 | ******************************************************************************/ |
||
66 | static |
||
67 | void |
||
68 | ds1307__set_start_address |
||
69 | ( |
||
70 | void |
||
71 | ); |
||
72 | |||
73 | /****************************************************************************** |
||
74 | * Public function definitions. |
||
75 | ******************************************************************************/ |
||
76 | |||
77 | /**************************************************************************//** |
||
78 | * \fn void ds1307__initialize(void) |
||
79 | * |
||
80 | * \brief Initialize RTC. |
||
81 | ******************************************************************************/ |
||
82 | void |
||
83 | ds1307__initialize |
||
84 | ( |
||
85 | void |
||
86 | ){ |
||
87 | // Initialize TWI. |
||
88 | twi__initialize(DS1307__CLOCK_RATE); |
||
89 | } |
||
90 | |||
91 | /**************************************************************************//** |
||
92 | * \fn void ds1307__get_time( |
||
93 | * date_time__time_t* p_time, |
||
94 | * ds1307__hour_mode_t* p_hour_mode) |
||
95 | * |
||
96 | * \brief Get RTC time. |
||
97 | * |
||
98 | * \param p_time a pointer to fill with RTC time |
||
99 | * \param p_hour_mode a pointer to fill with RTC hour mode |
||
100 | ******************************************************************************/ |
||
101 | void |
||
102 | ds1307__get_time |
||
103 | ( |
||
104 | datetime__time_t* p_time, |
||
105 | ds1307__hour_mode_t* p_hour_mode |
||
106 | ){ |
||
107 | // Check the preconditions. |
||
108 | assert(NULL != p_time); |
||
109 | assert(NULL != p_hour_mode); |
||
110 | |||
111 | twi__start(); |
||
112 | twi__send_slaw(DS1307__SLAVE_ADDRESS); |
||
113 | twi__send_data(DS1307__REGISTER_ADDRESS__SECONDS); |
||
114 | twi__stop(); |
||
115 | |||
116 | twi__start(); |
||
117 | twi__send_slar(DS1307__SLAVE_ADDRESS); |
||
118 | uint8_t seconds = 0; |
||
119 | twi__receive_data_ack(&seconds); |
||
120 | uint8_t minutes = 0; |
||
121 | twi__receive_data_ack(&minutes); |
||
122 | uint8_t hours = 0; |
||
123 | twi__receive_data_nack(&hours); |
||
124 | p_time->seconds = (((seconds & 0x70) >> 4) * 10) + (seconds & 0x0F); |
||
125 | p_time->minutes = (((minutes & 0x70) >> 4) * 10) + (minutes & 0x0F); |
||
126 | *p_hour_mode = (ds1307__hour_mode_t)((hours & 0x40) >> 6); |
||
127 | if (DS1307__HOUR_MODE__12_HOUR == *p_hour_mode) |
||
128 | { |
||
129 | p_time->meridiem = (datetime__meridiem_t)((hours & 0x20) >> 5); |
||
130 | p_time->hours = (((hours & 0x10) >> 4) * 10) + (hours & 0x0F); |
||
131 | } |
||
132 | else |
||
133 | { |
||
134 | p_time->hours = (((hours & 0x30) >> 4) * 10) + (hours & 0x0F); |
||
135 | } |
||
136 | twi__stop(); |
||
137 | } |
||
138 | |||
139 | /**************************************************************************//** |
||
140 | * \fn void ds1307__set_time( |
||
141 | * date_time__time_t* p_time, |
||
142 | * ds1307__hour_mode_t hour_mode) |
||
143 | * |
||
144 | * \brief Set RTC time. |
||
145 | * |
||
146 | * \param p_time time to set. p_time->meridiem isn't used in 12-hour mode. |
||
147 | * \param hour_mode hour mode |
||
148 | ******************************************************************************/ |
||
149 | void |
||
150 | ds1307__set_time |
||
151 | ( |
||
152 | datetime__time_t* p_time, |
||
153 | ds1307__hour_mode_t hour_mode |
||
154 | ){ |
||
155 | // Check the preconditions. |
||
156 | assert(NULL != p_time); |
||
157 | |||
158 | twi__start(); |
||
159 | twi__send_slaw(DS1307__SLAVE_ADDRESS); |
||
160 | twi__send_data(DS1307__REGISTER_ADDRESS__SECONDS); |
||
161 | |||
162 | uint8_t seconds = ((p_time->seconds / 10) << 4) + (p_time->seconds % 10); |
||
163 | uint8_t minutes = ((p_time->minutes / 10) << 4) + (p_time->minutes % 10); |
||
164 | uint8_t hours = 0; |
||
165 | if (DS1307__HOUR_MODE__12_HOUR == hour_mode) |
||
166 | { |
||
167 | hours = (hour_mode << 6) + (p_time->meridiem << 5) |
||
168 | + ((p_time->hours / 10) << 4) + (p_time->hours % 10); |
||
169 | } |
||
170 | else |
||
171 | { |
||
172 | hours = (hour_mode << 6) + ((p_time->hours / 10) << 4) |
||
173 | + (p_time->hours % 10); |
||
174 | } |
||
175 | |||
176 | twi__send_data(seconds); |
||
177 | twi__send_data(minutes); |
||
178 | twi__send_data(hours); |
||
179 | |||
180 | twi__stop(); |
||
181 | } |
||
182 | |||
183 | /**************************************************************************//** |
||
184 | * \fn void ds1307__get_date(date_time__date_t* p_date) |
||
185 | * |
||
186 | * \brief Get RTC date. |
||
187 | * |
||
188 | * \param p_date a pointer to fill with RTC date |
||
189 | ******************************************************************************/ |
||
190 | void |
||
191 | ds1307__get_date |
||
192 | ( |
||
193 | datetime__date_t* p_date |
||
194 | ){ |
||
195 | // Check the preconditions. |
||
196 | assert(NULL != p_date); |
||
197 | |||
198 | twi__start(); |
||
199 | twi__send_slaw(DS1307__SLAVE_ADDRESS); |
||
200 | twi__send_data(DS1307__REGISTER_ADDRESS__DAY); |
||
201 | twi__stop(); |
||
202 | |||
203 | twi__start(); |
||
204 | twi__send_slar(DS1307__SLAVE_ADDRESS); |
||
205 | |||
206 | uint8_t day = 0; |
||
207 | twi__receive_data_ack(&day); |
||
208 | uint8_t date = 0; |
||
209 | twi__receive_data_ack(&date); |
||
210 | uint8_t month = 0; |
||
211 | twi__receive_data_ack(&month); |
||
212 | uint8_t year = 0; |
||
213 | twi__receive_data_nack(&year); |
||
214 | |||
215 | p_date->day = day; |
||
216 | p_date->date = (((date & 0x30) >> 4) * 10) + (date & 0x0F); |
||
217 | p_date->month = (((month & 0x10) >> 4) * 10) + (month & 0x0F); |
||
218 | p_date->year = (((year & 0xF0) >> 4) * 10) + (year & 0x0F); |
||
219 | |||
220 | twi__stop(); |
||
221 | } |
||
222 | |||
223 | /**************************************************************************//** |
||
224 | * \fn void ds1307__set_date(date_time__date_t* p_date) |
||
225 | * |
||
226 | * \brief Set RTC date. |
||
227 | * |
||
228 | * \param p_date date to set |
||
229 | ******************************************************************************/ |
||
230 | void |
||
231 | ds1307__set_date |
||
232 | ( |
||
233 | datetime__date_t* p_date |
||
234 | ){ |
||
235 | // Check the preconditions. |
||
236 | assert(NULL != p_date); |
||
237 | |||
238 | twi__start(); |
||
239 | twi__send_slaw(DS1307__SLAVE_ADDRESS); |
||
240 | twi__send_data(DS1307__REGISTER_ADDRESS__DAY); |
||
241 | |||
242 | uint8_t date = ((p_date->date / 10) << 4) + (p_date->date % 10); |
||
243 | uint8_t month = ((p_date->month / 10) << 4) + (p_date->month % 10); |
||
244 | uint8_t year = ((p_date->year / 10) << 4) + (p_date->year % 10); |
||
245 | |||
246 | twi__send_data(p_date->day); |
||
247 | twi__send_data(date); |
||
248 | twi__send_data(month); |
||
249 | twi__send_data(year); |
||
250 | |||
251 | twi__stop(); |
||
252 | } |
||
253 | |||
254 | /****************************************************************************** |
||
255 | * Private function definitions. |
||
256 | ******************************************************************************/ |
||
257 | |||
258 | /**************************************************************************//** |
||
259 | * \fn static void ds1307__set_start_address(void) |
||
260 | * |
||
261 | * \brief Set RTC address to 0. |
||
262 | ******************************************************************************/ |
||
263 | static |
||
264 | void |
||
265 | ds1307__set_start_address |
||
266 | ( |
||
267 | void |
||
268 | ){ |
||
269 | twi__start(); |
||
270 | twi__send_slaw(DS1307__SLAVE_ADDRESS); |
||
271 | twi__send_data(0); |
||
272 | twi__stop(); |
||
273 | } |