diff options
Diffstat (limited to 'ds1302.h')
-rw-r--r-- | ds1302.h | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/ds1302.h b/ds1302.h new file mode 100644 index 0000000..d381265 --- /dev/null +++ b/ds1302.h @@ -0,0 +1,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); |