-
Notifications
You must be signed in to change notification settings - Fork 0
/
ds1302pico_ex01.c
98 lines (85 loc) · 2.65 KB
/
ds1302pico_ex01.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
/**
* ds1302 example
*
* @version 1.0.0
* @author Ken Webb <[email protected]>
* @license MIT
* @see https://github.com/kenwebb/
*
* Raspberry Pi Pico - ds1302 C code
* 27 Oct 2022
* adapted from Arduino libraries by Ken Webb
* This is an example app with main() function.
* It uses the adapted Arduino-based libraries.
*
* To compile, upload, observe (from Raspberry Pi 4):
* cd TO_BUILD_FOLDER
* cmake ..
* make
* openocd -f interface/raspberrypi-swd.cfg -f target/rp2040.cfg -c "program ds1302pico_ex01.elf verify reset exit"
* minicom -b 115200 -o -D /dev/serial0
* minicom -b 115200 -o -D /dev/ttyACM0
*/
#include <stdio.h>
#include <stdlib.h>
#include "pico/stdlib.h"
#include "ds1302.h"
#define PIN_GPIO_CLK 18
#define PIN_GPIO_DAT 17
#define PIN_GPIO_RST 16
int main() {
stdio_init_all();
sleep_ms(2000); // the initial printf() only work if I have this initial 2000 ms delay
//printf("starting ds1302pico ...\n"); // does not work
printf("\nPico - ds1302 - Date Time DOW\n");
//gpio_init(25);
//gpio_set_dir(25, GPIO_OUT);
ds1302init(PIN_GPIO_RST, PIN_GPIO_CLK, PIN_GPIO_DAT);
writeProtect(0); // disable write protection
DateTime now;
// initialize time on ds1302; ONLY DO THIS ONCE
/*now.year = 22;
now.month = MONTH_OCT;
now.day = 29;
now.hour = 9;
now.minute = 59;
now.second = 0;
now.dow = DOW_SAT;*/
//ds1302setDateTime(&now);
// test bulk RAM ops - write (from msparks)
static const char kHelloWorld[] = "abcdefghijklmnopqrstuvwxyzABCDEF"; // 31 chars; anything after E gets chopped off
// test RAM ops
ds1302testRam();
// test RAM ops - write hex (from msparks)
uint8_t testaddrGET = 0xC1;
uint8_t testdataGET;
for (int i = 0; i < kRamSize*2; i += 2) {
ds1302getRamByte(testaddrGET + i, &testdataGET);
printf("%02X ", testdataGET);
}
printf("\n");
// test bulk RAM ops - write ASCII (from msparks)
uint8_t ram[kRamSize];
readRamBulk(ram, kRamSize);
printf("%s\n", (const char*)ram);
while (true) {
sleep_ms(1000); // one second
ds1302getDateTime(&now);
//printf("DateTime second %d\n", now.second); // OK 0-59
char* dow = "Noneday";
switch (now.dow) {
case DOW_MON: dow = "Monday"; break;
case DOW_TUE: dow = "Tuesday"; break;
case DOW_WED: dow = "Wednesday"; break;
case DOW_THU: dow = "Thursday"; break;
case DOW_FRI: dow = "Friday"; break;
case DOW_SAT: dow = "Saturday"; break;
case DOW_SUN: dow = "Sunday"; break;
default: break;
}
printf("%02d/%02d/20%d %02d:%02d:%02d %s\n", now.month, now.day, now.year,
now.hour, now.minute, now.second,
dow);
}
return 0;
}