8000 Sat Feb 11 12:33:57 CST 2017 · programmer131/esp32-snippets@cb81167 · GitHub
[go: up one dir, main page]

Skip to content

Commit cb81167

Browse files
author
kolban
committed
Sat Feb 11 12:33:57 CST 2017
1 parent d11d8a5 commit cb81167

File tree

3 files changed

+157
-1
lines changed

3 files changed

+157
-1
lines changed

filesystems/espfs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Primarily, we use flash memory mapping to access the data as opposed to individu
1010
and the primary intent, a new method was added with the signature:
1111

1212
```
13-
int espFsAccess(EspFsFile *fh, char *buf, size_t *len)
13+
int espFsAccess(EspFsFile *fh, void **buf, size_t *len)
1414
```
1515

1616
This function returns a pointer to the whole content of the file which is stored in buf. The

hardware/rtc/ds1307.c

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
#include <apps/sntp/sntp.h>
2+
#include <driver/i2c.h>
3+
#include <esp_log.h>
4+
#include <freertos/FreeRTOS.h>
5+
#include <freertos/task.h>
6+
#include <lwip/sockets.h>
7+
#include <stdio.h>
8+
#include <time.h>
9+
10+
#include "sdkconfig.h"
11+
12+
#define SDA_PIN 18
13+
#define SCL_PIN 19
14+
#define DS1307_ADDRESS 0x68
15+
16+
static char tag[] = "ds1307";
17+
18+
static uint8_t intToBCD(uint8_t num) {
19+
return ((num / 10) << 4) | (num%10);
20+
}
21+
22+
static uint8_t bcdToInt(uint8_t bcd) {
23+
// 0x10
24+
return ((bcd >> 4) * 10) + (bcd & 0x0f);;
25+
}
26+
27+
static void startSNTP() {
28+
ip_addr_t addr;
29+
sntp_setoperatingmode(SNTP_OPMODE_POLL);
30+
inet_pton(AF_INET, "129.6.15.28", &addr);
31+
sntp_setserver(0, &addr);
32+
sntp_init();
33+
}
34+
35+
36+
static void initI2C() {
37+
i2c_config_t conf;
38+
conf.mode = I2C_MODE_MASTER;
39+
conf.sda_io_num = SDA_PIN;
40+
conf.scl_io_num = SCL_PIN;
41+
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
42+
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
43+
conf.master.clk_speed = 100000;
44+
ESP_ERROR_CHECK(i2c_param_config(I2C_NUM_0, &conf));
45+
ESP_ERROR_CHECK(i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0));
46+
}
47+
48+
/*
49+
* The value read from the DS1307 is 7 bytes encoded in BCD:
50+
* 0 - Seconds - 00-59
51+
* 1 - Minutes - 00-59
52+
* 2 - Hours - 00-23
53+
* 3 - Day - 01-07
54+
* 4 - Date - 01-31
55+
* 5 - Month - 01-12
56+
* 6 - Year - 00-99
57+
*
58+
*/
59+
time_t readValue() {
60+
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
61+
ESP_ERROR_CHECK(i2c_master_start(cmd));
62+
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, (DS1307_ADDRESS << 1) | I2C_MASTER_WRITE, 1 /* expect ack */));
63+
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, 0x0, 1));
64+
ESP_ERROR_CHECK(i2c_master_start(cmd));
65+
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, (DS1307_ADDRESS << 1) | I2C_MASTER_READ, 1 /* expect ack */));
66+
uint8_t data[7];
67+
ESP_ERROR_CHECK(i2c_master_read(cmd, data, 7, 0));
68+
ESP_ERROR_CHECK(i2c_master_stop(cmd));
69+
ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000/portTICK_PERIOD_MS));
70+
i2c_cmd_link_delete(cmd);
71+
72+
int i;
73+
for (i=0; i<7; i++) {
74+
ESP_LOGD(tag, "%d: 0x%.2x", i, data[i]);
75+
}
76+
77+
struct tm tm;
78+
tm.tm_sec = bcdToInt(data[0]);
79+
tm.tm_min = bcdToInt(data[1]);
80+
tm.tm_hour = bcdToInt(data[2]);
81+
tm.tm_mday = bcdToInt(data[4]);
82+
tm.tm_mon = bcdToInt(data[5]) - 1; // 0-11 - Note: The month on the DS1307 is 1-12.
83+
tm.tm_year = bcdToInt(data[6]) + 100; // Years since 1900
84+
time_t readTime = mktime(&tm);
85+
return readTime;
86+
}
87+
88+
void writeValue(time_t newTime) {
89+
ESP_LOGD(tag, ">> writeValue: %ld", newTime);
90+
struct tm tm;
91+
gmtime_r(&newTime, &tm);
92+
char buf[30];
93+
ESP_LOGD(tag, " - %s", asctime_r(&tm, buf));
94+
95+
esp_err_t errRc;
96+
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
97+
ESP_ERROR_CHECK(i2c_master_start(cmd));
98+
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, (DS1307_ADDRESS << 1) | I2C_MASTER_WRITE, 1 /* expect ack */));
99+
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, 0x0, 1));
100+
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, intToBCD(tm.tm_sec), 1)); // seconds
101+
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, intToBCD(tm.tm_min), 1 )); // minutes
102+
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, intToBCD(tm.tm_hour), 1 )); // hours
103+
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, intToBCD(tm.tm_wday+1), 1 )); // week day
104+
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, intToBCD(tm.tm_mday), 1)); // date of month
105+
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, intToBCD(tm.tm_mon+1), 1)); // month
106+
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, intToBCD(tm.tm_year-100), 1)); // year
107+
ESP_ERROR_CHECK(i2c_master_stop(cmd));
108+
errRc = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000/portTICK_PERIOD_MS);
109+
if (errRc != 0) {
110+
ESP_LOGE(tag, "i2c_master_cmd_begin: %d", errRc);
111+
}
112+
i2c_cmd_link_delete(cmd);
113+
}
114+
115+
void task_ds1307(void *ignore) {
116+
int doWrite = 1;
117+
ESP_LOGD(tag, ">> ds1307");
118+
initI2C();
119+
if (doWrite) {
120+
startSNTP();
121+
time_t t;
122+
while(time(&t) < 1000) {
123+
ESP_LOGD(tag, "Waiting for SNTP ...");
124+
vTaskDelay(1000/portTICK_PERIOD_MS);
125+
}
126+
writeValue(t);
127+
}
128+
129+
while(1) {
130+
time_t t = time(NULL);
131+
ESP_LOGD(tag, "time: %ld", t);
132+
t = readValue();
133+
ESP_LOGD(tag, "Read from DS1307: %ld", t);
134+
vTaskDelay(1000/portTICK_PERIOD_MS);
135+
}
136+
}

sntp/fragments/sntp.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <esp_log.h>
2+
#include <freertos/FreeRTOS.h>
3+
#include <freertos/task.h>
4+
#include <time.h>
5+
#include <apps/sntp/sntp.h>
6+
#include <lwip/sockets.h>
7+
#include <sys/time.h>
8+
#include "sdkconfig.h"
9+
10+
/*
11+
* Connect to an Internet time server to get the current date and
12+
* time.
13+
*/
14+
void startSNTP() {
15+
ip_addr_t addr;
16+
sntp_setoperatingmode(SNTP_OPMODE_POLL);
17+
inet_pton(AF_INET, "129.6.15.28", &addr);
18+
sntp_setserver(0, &addr);
19+
sntp_init();
20+
}

0 commit comments

Comments
 (0)
0