-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDS1302.c
374 lines (305 loc) · 9.47 KB
/
DS1302.c
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include "DS1302.h"
// Write Register Address
#define DS1302_SEC 0x80
#define DS1302_MIN 0x82
#define DS1302_HOUR 0x84
#define DS1302_DATE 0x86
#define DS1302_MONTH 0x88
#define DS1302_DAY 0x8a
#define DS1302_YEAR 0x8c
#define DS1302_CONTROL 0x8e
#define DS1302_CHARGER 0x90
#define DS1302_CLKBURST 0xbe
#define DS1302_RAMBURST 0xfe
#define RAMSIZE 0x31 // Ram Size in bytes
#define DS1302_RAMSTART 0xc0 // First Address
#define HEX2BCD(v) ((v) % 10 + (v) / 10 * 16)
#define BCD2HEX(v) ((v) % 16 + (v) / 16 * 10)
// GPIO Pins
#define DS1302_GPIO GPIOI
#define DS1302_SCLK GPIO_PIN_0
#define DS1302_SDA GPIO_PIN_3
#define DS1302_RST GPIO_PIN_2
/* Clock signal need to be at least 1 micro second wide, those delays are generated with DWT */
/* More info: https://www.carminenoviello.com/2015/09/04/precisely-measure-microseconds-stm32/ */
#pragma push
#pragma O3
static void delayUS_DWT(uint32_t us) {
volatile uint32_t cycles = (SystemCoreClock/1000000L)*us;
volatile uint32_t start = DWT->CYCCNT;
do {
} while(DWT->CYCCNT - start < cycles);
}
#pragma pop
// SDA Write(output) Mode
static void writeSDA(void) {
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.Pin = DS1302_SDA;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
HAL_GPIO_Init(DS1302_GPIO, &GPIO_InitStructure);
}
// SDA Read(input) Mode
static void readSDA(void) {
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.Pin = DS1302_SDA;
GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
GPIO_InitStructure.Pull = GPIO_PULLDOWN;
GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
HAL_GPIO_Init(DS1302_GPIO, &GPIO_InitStructure);
}
/* Sends an address or command */
static void DS1302_SendCmd(uint8_t cmd) {
uint8_t i;
for (i = 0; i < 8; i ++)
{
// DS1302_SDA = (bit)(addr & 1);
HAL_GPIO_WritePin(DS1302_GPIO, DS1302_SDA, (cmd & 1) ? GPIO_PIN_SET : GPIO_PIN_RESET);
// DS1302_SCK = 1;
HAL_GPIO_WritePin(DS1302_GPIO, DS1302_SCLK, GPIO_PIN_SET);
delayUS_DWT(1);
// DS1302_SCK = 0;
HAL_GPIO_WritePin(DS1302_GPIO, DS1302_SCLK, GPIO_PIN_RESET);
delayUS_DWT(1);
cmd >>= 1;
}
}
/* Writes a byte to 'addr' */
static void DS1302_WriteByte(uint8_t addr, uint8_t d)
{
uint8_t i;
// DS1302_RST = 1;
HAL_GPIO_WritePin(DS1302_GPIO, DS1302_RST, GPIO_PIN_SET);
//addr = addr & 0xFE;
DS1302_SendCmd(addr); // Sends address
for (i = 0; i < 8; i ++)
{
// DS1302_SDA = (bit)(d & 1);
HAL_GPIO_WritePin(DS1302_GPIO, DS1302_SDA, (d & 1) ? GPIO_PIN_SET : GPIO_PIN_RESET);
// DS1302_SCK = 1;
HAL_GPIO_WritePin(DS1302_GPIO, DS1302_SCLK, GPIO_PIN_SET);
delayUS_DWT(1);
// DS1302_SCK = 0;
HAL_GPIO_WritePin(DS1302_GPIO, DS1302_SCLK, GPIO_PIN_RESET);
delayUS_DWT(1);
d >>= 1;
}
// DS1302_RST = 0;
HAL_GPIO_WritePin(DS1302_GPIO, DS1302_RST, GPIO_PIN_RESET);
// DS1302_SDA = 0;
HAL_GPIO_WritePin(DS1302_GPIO, DS1302_SDA, GPIO_PIN_RESET);
}
/* Sends 'cmd' command and writes in burst mode 'len' bytes from 'temp' */
static void DS1302_WriteBurst(uint8_t cmd, uint8_t len, uint8_t * temp)
{
uint8_t i, j;
DS1302_WriteByte(DS1302_CONTROL,0x00); // Disable write protection
// DS1302_RST = 1;
HAL_GPIO_WritePin(DS1302_GPIO, DS1302_RST, GPIO_PIN_SET);
DS1302_SendCmd(cmd); // Sends burst write command
for(j = 0; j < len; j++) {
for (i = 0; i < 8; i ++)
{
// DS1302_SDA = (bit)(d & 1);
HAL_GPIO_WritePin(DS1302_GPIO, DS1302_SDA, (temp[j] & 1) ? GPIO_PIN_SET : GPIO_PIN_RESET);
// DS1302_SCK = 1;
HAL_GPIO_WritePin(DS1302_GPIO, DS1302_SCLK, GPIO_PIN_SET);
delayUS_DWT(1);
// DS1302_SCK = 0;
HAL_GPIO_WritePin(DS1302_GPIO, DS1302_SCLK, GPIO_PIN_RESET);
delayUS_DWT(1);
temp[j] >>= 1;
}
}
// DS1302_RST = 0;
HAL_GPIO_WritePin(DS1302_GPIO, DS1302_RST, GPIO_PIN_RESET);
// DS1302_SDA = 0;
HAL_GPIO_WritePin(DS1302_GPIO, DS1302_SDA, GPIO_PIN_RESET);
DS1302_WriteByte(DS1302_CONTROL,0x80); // Enable write protection
}
/* Reads a byte from addr */
static uint8_t DS1302_ReadByte(uint8_t addr)
{
uint8_t i;
uint8_t temp = 0;
// DS1302_RST = 1;
HAL_GPIO_WritePin(DS1302_GPIO, DS1302_RST, GPIO_PIN_SET);
addr = addr | 0x01; // Generate Read Address
DS1302_SendCmd(addr); // Sends address
readSDA();
for (i = 0; i < 8; i ++)
{
temp >>= 1;
// if(DS1302_SDA)
if(HAL_GPIO_ReadPin(DS1302_GPIO, DS1302_SDA))
temp |= 0x80;
// DS1302_SCK = 1;
HAL_GPIO_WritePin(DS1302_GPIO, DS1302_SCLK, GPIO_PIN_SET);
delayUS_DWT(1);
// DS1302_SCK = 0;
HAL_GPIO_WritePin(DS1302_GPIO, DS1302_SCLK, GPIO_PIN_RESET);
delayUS_DWT(1);
}
writeSDA();
// DS1302_RST = 0;
HAL_GPIO_WritePin(DS1302_GPIO, DS1302_RST, GPIO_PIN_RESET);
// DS1302_SDA = 0;
HAL_GPIO_WritePin(DS1302_GPIO, DS1302_SDA, GPIO_PIN_RESET);
return temp;
}
/* Sends 'cmd' command and reads in burst mode 'len' bytes into 'temp' */
static void DS1302_ReadBurst(uint8_t cmd, uint8_t len, uint8_t * temp)
{
uint8_t i, j;
// DS1302_RST = 1;
HAL_GPIO_WritePin(DS1302_GPIO, DS1302_RST, GPIO_PIN_SET);
cmd = cmd | 0x01; // Generate read command
DS1302_SendCmd(cmd); // Sends burst read command
readSDA();
for (j = 0; j < len; j ++) {
temp[j] = 0;
for (i = 0; i < 8; i ++)
{
temp[j] >>= 1;
// if(DS1302_SDA)
if(HAL_GPIO_ReadPin(DS1302_GPIO, DS1302_SDA))
temp[j] |= 0x80;
// DS1302_SCK = 1;
HAL_GPIO_WritePin(DS1302_GPIO, DS1302_SCLK, GPIO_PIN_SET);
delayUS_DWT(1);
// DS1302_SCK = 0;
HAL_GPIO_WritePin(DS1302_GPIO, DS1302_SCLK, GPIO_PIN_RESET);
delayUS_DWT(1);
}
}
writeSDA();
// DS1302_RST = 0;
HAL_GPIO_WritePin(DS1302_GPIO, DS1302_RST, GPIO_PIN_RESET);
HAL_GPIO_WritePin(DS1302_GPIO, DS1302_SDA, GPIO_PIN_RESET);
}
/* Writes time byte by byte from 'buf' */
void DS1302_WriteTime(uint8_t *buf)
{
DS1302_WriteByte(DS1302_CONTROL,0x00); // Disable write protection
delayUS_DWT(1);
DS1302_WriteByte(DS1302_SEC,0x80);
DS1302_WriteByte(DS1302_YEAR,HEX2BCD(buf[1]));
DS1302_WriteByte(DS1302_MONTH,HEX2BCD(buf[2]));
DS1302_WriteByte(DS1302_DATE,HEX2BCD(buf[3]));
DS1302_WriteByte(DS1302_HOUR,HEX2BCD(buf[4]));
DS1302_WriteByte(DS1302_MIN,HEX2BCD(buf[5]));
DS1302_WriteByte(DS1302_SEC,HEX2BCD(buf[6]));
DS1302_WriteByte(DS1302_DAY,HEX2BCD(buf[7]));
DS1302_WriteByte(DS1302_CONTROL,0x80); // Enable write protection
delayUS_DWT(1);
}
/* Reads time byte by byte to 'buf' */
void DS1302_ReadTime(uint8_t *buf)
{
uint8_t tmp;
tmp = DS1302_ReadByte(DS1302_YEAR);
buf[1] = BCD2HEX(tmp);
tmp = DS1302_ReadByte(DS1302_MONTH);
buf[2] = BCD2HEX(tmp);
tmp = DS1302_ReadByte(DS1302_DATE);
buf[3] = BCD2HEX(tmp);
tmp = DS1302_ReadByte(DS1302_HOUR);
buf[4] = BCD2HEX(tmp);
tmp = DS1302_ReadByte(DS1302_MIN);
buf[5] = BCD2HEX(tmp);
tmp = DS1302_ReadByte((DS1302_SEC))&0x7F;
buf[6] = BCD2HEX(tmp);
tmp = DS1302_ReadByte(DS1302_DAY);
buf[7] = BCD2HEX(tmp);
}
/* Initialization */
void DS1302_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.Pin = DS1302_SCLK | DS1302_SDA | DS1302_RST;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
HAL_GPIO_Init(DS1302_GPIO, &GPIO_InitStructure);
DS1302_WriteByte(DS1302_CHARGER,0x00); // Disable Trickle Charger
// DS1302_RST = 0;
HAL_GPIO_WritePin(DS1302_GPIO, DS1302_RST, GPIO_PIN_RESET);
// DS1302_SCK = 0;
HAL_GPIO_WritePin(DS1302_GPIO, DS1302_SCLK, GPIO_PIN_RESET);
DWT->CTRL |= 1 ; // enable the counter for microsecond delay, see "void delayUS_DWT(uint32_t us)"
}
/* Writes 'val' to ram address 'addr' */
/* Ram addresses range from 0 to 30 */
void DS1302_WriteRam(uint8_t addr, uint8_t val) {
DS1302_WriteByte(DS1302_CONTROL,0x00); // Disable write protection
delayUS_DWT(1);
if (addr >= RAMSIZE) {
return;
}
DS1302_WriteByte(DS1302_RAMSTART + (2 * addr), val);
DS1302_WriteByte(DS1302_CONTROL,0x80); // Enable write protection
delayUS_DWT(1);
}
/* Reads ram address 'addr' */
uint8_t DS1302_ReadRam(uint8_t addr) {
if (addr >= RAMSIZE) {
return 0;
}
return DS1302_ReadByte(DS1302_RAMSTART + (2 * addr));
}
/* Clears the entire ram writing 0 */
void DS1302_ClearRam(void) {
uint8_t i;
for(i=0; i< RAMSIZE; i++){
DS1302_WriteRam(i,0x00);
}
}
/* Reads time in burst mode, includes control byte */
void DS1302_ReadTimeBurst(uint8_t * buf) {
uint8_t temp[8] = {0, 0, 0, 0, 0, 0, 0, 0};
DS1302_ReadBurst(DS1302_CLKBURST, 8, temp);
buf[1] = BCD2HEX(temp[6]); // Year
buf[2] = BCD2HEX(temp[4]); // Month
buf[3] = BCD2HEX(temp[3]); // Date
buf[4] = BCD2HEX(temp[2]); // Hour
buf[5] = BCD2HEX(temp[1]); // Min
buf[6] = BCD2HEX(temp[0]); // Sec
buf[7] = BCD2HEX(temp[5]); // Day
buf[0] = temp[7]; // Control
}
/* Writes time in burst mode, includes control byte */
void DS1302_WriteTimeBurst(uint8_t * buf) {
uint8_t temp[8];
temp[0]=HEX2BCD(buf[6]); // Sec
temp[1]=HEX2BCD(buf[5]); // Min
temp[2]=HEX2BCD(buf[4]); // Hour
temp[3]=HEX2BCD(buf[3]); // Date
temp[4]=HEX2BCD(buf[2]); // Month
temp[5]=HEX2BCD(buf[7]); // Day
temp[6]=HEX2BCD(buf[1]); // Year
temp[7]=buf[0]; // Control
DS1302_WriteBurst(DS1302_CLKBURST, 8, temp);
}
/* Reads ram in burst mode 'len' bytes into 'buf' */
void DS1302_ReadRamBurst(uint8_t len, uint8_t * buf) {
uint8_t i;
if(len <= 0) {
return;
}
if (len > RAMSIZE) {
len = RAMSIZE;
}
for(i = 0; i < len; i++) {
buf[i] = 0;
}
DS1302_ReadBurst(DS1302_RAMBURST, len, buf);
}
/* Writes ram in burst mode 'len' bytes from 'buf' */
void DS1302_WriteRamBurst(uint8_t len, uint8_t * buf) {
if(len <= 0) {
return;
}
if (len > RAMSIZE) {
len = RAMSIZE;
}
DS1302_WriteBurst(DS1302_RAMBURST, len, buf);
}