Rev 36 | Rev 58 | Go to most recent revision | Details | Compare with Previous | 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 | /****************************************************************************** |
||
56 | * Public function definitions. |
||
57 | ******************************************************************************/ |
||
58 | |||
59 | /**************************************************************************//** |
||
60 | * \fn void ds1307__initialize(void) |
||
61 | * |
||
62 | * \brief Initialize RTC. |
||
63 | ******************************************************************************/ |
||
64 | void |
||
65 | ds1307__initialize |
||
66 | ( |
||
67 | void |
||
68 | ){ |
||
36 | jlesech | 69 | // We don't initialize TWI here: we may have several devices on the bus. |
18 | jlesech | 70 | // Initialize TWI. |
36 | jlesech | 71 | //twi__initialize(DS1307__CLOCK_RATE); |
18 | jlesech | 72 | } |
73 | |||
74 | /**************************************************************************//** |
||
75 | * \fn void ds1307__get_time( |
||
76 | * date_time__time_t* p_time, |
||
77 | * ds1307__hour_mode_t* p_hour_mode) |
||
78 | * |
||
79 | * \brief Get RTC time. |
||
80 | * |
||
81 | * \param p_time a pointer to fill with RTC time |
||
82 | * \param p_hour_mode a pointer to fill with RTC hour mode |
||
83 | ******************************************************************************/ |
||
84 | void |
||
85 | ds1307__get_time |
||
86 | ( |
||
87 | datetime__time_t* p_time, |
||
88 | ds1307__hour_mode_t* p_hour_mode |
||
89 | ){ |
||
90 | // Check the preconditions. |
||
91 | assert(NULL != p_time); |
||
92 | assert(NULL != p_hour_mode); |
||
93 | |||
94 | twi__start(); |
||
95 | twi__send_slaw(DS1307__SLAVE_ADDRESS); |
||
96 | twi__send_data(DS1307__REGISTER_ADDRESS__SECONDS); |
||
97 | twi__stop(); |
||
98 | |||
99 | twi__start(); |
||
100 | twi__send_slar(DS1307__SLAVE_ADDRESS); |
||
101 | uint8_t seconds = 0; |
||
102 | twi__receive_data_ack(&seconds); |
||
103 | uint8_t minutes = 0; |
||
104 | twi__receive_data_ack(&minutes); |
||
105 | uint8_t hours = 0; |
||
106 | twi__receive_data_nack(&hours); |
||
107 | p_time->seconds = (((seconds & 0x70) >> 4) * 10) + (seconds & 0x0F); |
||
108 | p_time->minutes = (((minutes & 0x70) >> 4) * 10) + (minutes & 0x0F); |
||
109 | *p_hour_mode = (ds1307__hour_mode_t)((hours & 0x40) >> 6); |
||
110 | if (DS1307__HOUR_MODE__12_HOUR == *p_hour_mode) |
||
111 | { |
||
112 | p_time->meridiem = (datetime__meridiem_t)((hours & 0x20) >> 5); |
||
113 | p_time->hours = (((hours & 0x10) >> 4) * 10) + (hours & 0x0F); |
||
114 | } |
||
115 | else |
||
116 | { |
||
117 | p_time->hours = (((hours & 0x30) >> 4) * 10) + (hours & 0x0F); |
||
118 | } |
||
119 | twi__stop(); |
||
120 | } |
||
121 | |||
122 | /**************************************************************************//** |
||
123 | * \fn void ds1307__set_time( |
||
124 | * date_time__time_t* p_time, |
||
125 | * ds1307__hour_mode_t hour_mode) |
||
126 | * |
||
127 | * \brief Set RTC time. |
||
128 | * |
||
129 | * \param p_time time to set. p_time->meridiem isn't used in 12-hour mode. |
||
130 | * \param hour_mode hour mode |
||
131 | ******************************************************************************/ |
||
132 | void |
||
133 | ds1307__set_time |
||
134 | ( |
||
135 | datetime__time_t* p_time, |
||
136 | ds1307__hour_mode_t hour_mode |
||
137 | ){ |
||
138 | // Check the preconditions. |
||
139 | assert(NULL != p_time); |
||
140 | |||
141 | twi__start(); |
||
142 | twi__send_slaw(DS1307__SLAVE_ADDRESS); |
||
143 | twi__send_data(DS1307__REGISTER_ADDRESS__SECONDS); |
||
144 | |||
145 | uint8_t seconds = ((p_time->seconds / 10) << 4) + (p_time->seconds % 10); |
||
146 | uint8_t minutes = ((p_time->minutes / 10) << 4) + (p_time->minutes % 10); |
||
147 | uint8_t hours = 0; |
||
148 | if (DS1307__HOUR_MODE__12_HOUR == hour_mode) |
||
149 | { |
||
150 | hours = (hour_mode << 6) + (p_time->meridiem << 5) |
||
151 | + ((p_time->hours / 10) << 4) + (p_time->hours % 10); |
||
152 | } |
||
153 | else |
||
154 | { |
||
155 | hours = (hour_mode << 6) + ((p_time->hours / 10) << 4) |
||
156 | + (p_time->hours % 10); |
||
157 | } |
||
158 | |||
159 | twi__send_data(seconds); |
||
160 | twi__send_data(minutes); |
||
161 | twi__send_data(hours); |
||
162 | |||
163 | twi__stop(); |
||
164 | } |
||
165 | |||
166 | /**************************************************************************//** |
||
167 | * \fn void ds1307__get_date(date_time__date_t* p_date) |
||
168 | * |
||
169 | * \brief Get RTC date. |
||
170 | * |
||
171 | * \param p_date a pointer to fill with RTC date |
||
172 | ******************************************************************************/ |
||
173 | void |
||
174 | ds1307__get_date |
||
175 | ( |
||
176 | datetime__date_t* p_date |
||
177 | ){ |
||
178 | // Check the preconditions. |
||
179 | assert(NULL != p_date); |
||
180 | |||
181 | twi__start(); |
||
182 | twi__send_slaw(DS1307__SLAVE_ADDRESS); |
||
183 | twi__send_data(DS1307__REGISTER_ADDRESS__DAY); |
||
184 | twi__stop(); |
||
185 | |||
186 | twi__start(); |
||
187 | twi__send_slar(DS1307__SLAVE_ADDRESS); |
||
188 | |||
189 | uint8_t day = 0; |
||
190 | twi__receive_data_ack(&day); |
||
191 | uint8_t date = 0; |
||
192 | twi__receive_data_ack(&date); |
||
193 | uint8_t month = 0; |
||
194 | twi__receive_data_ack(&month); |
||
195 | uint8_t year = 0; |
||
196 | twi__receive_data_nack(&year); |
||
197 | |||
198 | p_date->day = day; |
||
199 | p_date->date = (((date & 0x30) >> 4) * 10) + (date & 0x0F); |
||
200 | p_date->month = (((month & 0x10) >> 4) * 10) + (month & 0x0F); |
||
201 | p_date->year = (((year & 0xF0) >> 4) * 10) + (year & 0x0F); |
||
202 | |||
203 | twi__stop(); |
||
204 | } |
||
205 | |||
206 | /**************************************************************************//** |
||
207 | * \fn void ds1307__set_date(date_time__date_t* p_date) |
||
208 | * |
||
209 | * \brief Set RTC date. |
||
210 | * |
||
211 | * \param p_date date to set |
||
212 | ******************************************************************************/ |
||
213 | void |
||
214 | ds1307__set_date |
||
215 | ( |
||
216 | datetime__date_t* p_date |
||
217 | ){ |
||
218 | // Check the preconditions. |
||
219 | assert(NULL != p_date); |
||
220 | |||
221 | twi__start(); |
||
222 | twi__send_slaw(DS1307__SLAVE_ADDRESS); |
||
223 | twi__send_data(DS1307__REGISTER_ADDRESS__DAY); |
||
224 | |||
225 | uint8_t date = ((p_date->date / 10) << 4) + (p_date->date % 10); |
||
226 | uint8_t month = ((p_date->month / 10) << 4) + (p_date->month % 10); |
||
227 | uint8_t year = ((p_date->year / 10) << 4) + (p_date->year % 10); |
||
228 | |||
229 | twi__send_data(p_date->day); |
||
230 | twi__send_data(date); |
||
231 | twi__send_data(month); |
||
232 | twi__send_data(year); |
||
233 | |||
234 | twi__stop(); |
||
235 | } |