-
Notifications
You must be signed in to change notification settings - Fork 1
/
ds3231.h
55 lines (44 loc) · 1.37 KB
/
ds3231.h
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
#include <stdio.h>
#include <string.h>
#include "pico/stdlib.h"
#include "hardware/i2c.h"
#include <time.h>
//#include "pico/util/datetime.h"
// DS3231 I2C settings
#define I2C_PORT i2c0
#define I2C_SDA_PIN 4
#define I2C_SCL_PIN 5
#define I2C_ADDR 0x68
#define DS3231_I2C_TIMEOUT 5000
// TODO do something different with buffer size
#define I2C_BUFFER_SIZE 0x12
#define DS3231_TIME_REGISTER_COUNT 0x12
#define DS3231_REGISTER_DAY 0x03
enum ds3231_error_codes {
DS3231_NO_ERROR = 0,
DS3231_GENERIC_ERROR,
DS3231_TIMEOUT_ERROR,
DS3231_UNKNOWN_ERROR
};
static const char * ds3231_errors[] = {
[0] = "No error",
[DS3231_GENERIC_ERROR] = "Generic error.",
[DS3231_TIMEOUT_ERROR] = "Timeout error.",
[DS3231_UNKNOWN_ERROR] = "Unknown error."
};
// initialize DS3231 rtc
int initDS3231 ();
// read and write DS3231 rtc registers
int readDS3231Time (struct tm * datetime);
int setDS3231Time (struct tm * datetime);
int adjustDS3231Day ();
int setDS3231DayTM (struct tm * datetime);
int setDS3231Day (uint8_t day);
int readDS3231 (uint8_t * buffer, int bufferLength);
int writeDS3231 (uint8_t * buffer, int bufferLength);
// DS3231 error handling
int statusToDS3231ErrorCode (int status);
char * ds3231ErrorString (int status);
// buffer timestamp conversion methods
int timeToBuffer (struct tm * datetime, uint8_t * buffer);
void bufferToTime (uint8_t * buffer, struct tm * datetime);