summaryrefslogtreecommitdiff
path: root/ds1302.h
blob: d3812659f2701a193be750e72261e3818a65c822 (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
#include <bcm2835.h>
#include <time.h>

/*Registers Addresses*/
#define DS1302_SECOND			0x80
#define DS1302_MINUTE       	0x82
#define DS1302_HOUR      		0x84
#define DS1302_DATE       		0x86
#define DS1302_MONTH        	0x88
#define DS1302_DAY        		0x8A
#define DS1302_YEAR        		0x8C
#define DS1302_WRITE_PROTECT    0x8E
#define DS1302_TRICKLE_CHARGER  0x90
#define DS1302_CLOCK_BURST    	0xBE
#define DS1302_RAM        		0xC0

#define DS1302_CLOCKDELAY 15

#define DS1302_START_YEAR 2000

/*! Clock refers to the CLK pin
    select refers to CE pin or RST pin on older devices
    io refers to the input/Push-Pull Output pin
*/
struct ds1302_spi_session {
    RPiGPIOPin clock;
    RPiGPIOPin select;
    RPiGPIOPin io;    
};

/*! Where a burst operation is reading from or writing to. */
typedef enum {
    DS1302_REGISTER,
    DS1302_MEMORY
} ds1302BurstType;

typedef enum {
    DS1302_SUNDAY = 1,
    DS1302_MONDAY,
    DS1302_TUESDAY,
    DS1302_WEDNESDAY,
    DS1302_THURSDAY,
    DS1302_FRIDAY,
    DS1302_SATURDAY
} ds1302WeekDay;



/*! Initialize the SPI connection over the GPIO pins specified in ds1302_spi_session.
    This will set the CLK and I/O lines low, and the CE pin will be set high
*/
void ds1302_spi_session_start(struct ds1302_spi_session * session);

/*! Terminate the SPI connection 
    This will set the CLK and CE pins low.
*/
void ds1302_spi_session_end(struct ds1302_spi_session  * session);

/*! Read a register on the ds1302. The result is returned*/
uint8_t ds1302_read_register(struct ds1302_spi_session * session, uint8_t address);
/*! Write to register on the ds1302.*/
void ds1302_write_register(struct ds1302_spi_session * session, uint8_t address, uint8_t data);

/*! Perform a burst read operation on either the clock registers or the memory registers.
    output will contain the results and size must be the allocated size of output.
    Returns the number of bytes read.
*/
uint8_t ds1302_burst_read(struct ds1302_spi_session * session, ds1302BurstType type, uint8_t * output, size_t size);

/*! Perform a burst write operation on either the clock registers or the memory registers.
    Returns the number of bytes written.
*/
uint8_t ds1302_burst_write(struct ds1302_spi_session * session, ds1302BurstType type, uint8_t * input, size_t size);

/*! Obtain the read address for a register */
uint8_t ds1302_readAddress(uint8_t address);


/*! Set the write protect register */
uint8_t ds1302_set_writeprotect(struct ds1302_spi_session * session, uint8_t protect);

/*! Convert the encoding on the ds1302 obtained from the ds1302 to regular base 10 encoding. */
uint8_t ds1302_convertToDecimal(uint8_t input);

/*! Inverse of ds1302_convertToDecimal. Converts base 10 encoding to the ds1302's representation. */
uint8_t ds1302_convertToDecimalHex(uint8_t input);

/*! Perform a burst read operation on the clock registers and return the date
    inside a struct tm.
*/
void ds1302_getCalendarTime(struct ds1302_spi_session * session, struct tm * date);

/*! Perform a burst write operation on the clock registers and set the date
    according to the provided struct tm.
*/
void ds1302_setCalendarTime(struct ds1302_spi_session * session, struct tm * date);