summaryrefslogtreecommitdiffstats
path: root/ds1307.c
blob: 6f1df2cf5cf40dd8de9802f3dad1cc4b330481ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "i2cmaster.h"
#include "ds1307.h"

static uint8_t ds1307_init_rw(unsigned char reg);
static uint8_t ds1307_init_write(unsigned char reg);
static uint8_t ds1307_init_read(unsigned char reg);


#define BCD2DEC(x) ((0x0F & (x)) + 10 * ((x) >> 4))
#define DEC2BCD(x) ((((x) / 10) << 4) | ((x) % 10))

uint8_t ds1307_start(void)
{
	//set 24 hour mode
	ds1307_init_read(0x02);
	unsigned char hour = i2c_readNak();
	hour &= ~(1 << DS1307_BIT_AMPM);
	ds1307_init_write(0x02);
	i2c_write(hour);
	i2c_stop();

	//unhalt clock
	ds1307_halt(0);

	return 0;
}

static uint8_t ds1307_init_rw(unsigned char reg)
{
	i2c_start_wait(DS1307_ADDR + I2C_WRITE);
	i2c_write(reg);
	return 0;

}

static uint8_t ds1307_init_write(unsigned char reg)
{
	return ds1307_init_rw(reg);
}

static uint8_t ds1307_init_read(unsigned char reg)
{
	ds1307_init_rw(reg);
	i2c_rep_start(DS1307_ADDR + I2C_READ);
	return 0;
}

uint8_t ds1307_halt(uint8_t halted)
{
	ds1307_init_read(0x00);
	unsigned char sec = i2c_readAck();

	if (halted)
		sec |= (1 << DS1307_BIT_CH);
	else
		sec &= ~(1 << DS1307_BIT_CH);

	ds1307_init_write(0x00);
	i2c_write(sec);
	i2c_stop();

	return 0;
}


uint8_t ds1307_getDateTime(struct datetime* dt)
{
	unsigned char c;
	ds1307_init_read(0x00);
	//omit CH bit
	dt->second = BCD2DEC(i2c_readAck() & ~(1 << DS1307_BIT_CH));
	dt->minute = BCD2DEC(i2c_readAck());
	//omit AMPM bit (which should be 0 because we only have 24 hour mode)
	dt->hour = BCD2DEC(i2c_readAck() & ~(1 << DS1307_BIT_AMPM));
	dt->dow = BCD2DEC(i2c_readAck());
	dt->day = BCD2DEC(i2c_readAck());
	dt->month = BCD2DEC(i2c_readAck());
	dt->year = 2000 + BCD2DEC(i2c_readNak());

	return 0;
}

uint8_t ds1307_setDateTime(struct datetime* dt)
{
	unsigned char ch = 0;
	unsigned char ampm = 0;

	ds1307_init_read(0x00);
	ch = i2c_readAck() & (1 << DS1307_BIT_CH);
	i2c_readAck();
	ampm = i2c_readNak() & (1 << DS1307_BIT_AMPM);


	ds1307_init_write(0x00);
	i2c_write(DEC2BCD(dt->second) | (ch << DS1307_BIT_CH));
	i2c_write(DEC2BCD(dt->minute));
	i2c_write(DEC2BCD(dt->hour) | (ampm << DS1307_BIT_AMPM));
	i2c_write(DEC2BCD(dt->dow));
	i2c_write(DEC2BCD(dt->day));
	i2c_write(DEC2BCD(dt->month));
	i2c_write(DEC2BCD(dt->year - 2000));
	i2c_stop();

	return  0;
}

uint8_t ds1307_SQW(uint8_t enabled)
{
	unsigned char sqw;
	ds1307_init_read(0x07);
	sqw = i2c_readNak();

	if (enabled)
		sqw |= (1 << DS1307_BIT_SQWE);
	else
		sqw &= ~(1 << DS1307_BIT_SQWE);

	ds1307_init_write(0x07);
	i2c_write(sqw);
	i2c_stop();

	return 0;
}

uint8_t ds1307_SQWRate(enum SQWRate rate)
{
	unsigned char sqw;
	ds1307_init_read(0x07);
	sqw = i2c_readNak();

	sqw = (sqw & ~(0x3)) | (rate & 0x3);

	ds1307_init_write(0x07);
	i2c_write(sqw);
	i2c_stop();

	return 0;
}