Subversion Repositories idreammicro-avr

Rev

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