|
| 1 | +/* |
| 2 | + * Copyright (C) 2017 Freie Universität Berlin |
| 3 | + * |
| 4 | + * This file is subject to the terms and conditions of the GNU Lesser |
| 5 | + * General Public License v2.1. See the file LICENSE in the top level |
| 6 | + * directory for more details. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * @{ |
| 11 | + * |
| 12 | + * @file |
| 13 | + * @author Martine Lenders <m.lenders@fu-berlin.de> |
| 14 | + */ |
| 15 | + |
| 16 | +#include <string.h> |
| 17 | + |
| 18 | +#include "bcd.h" |
| 19 | + |
| 20 | +#include "ds1307.h" |
| 21 | +#include "ds1307_internal.h" |
| 22 | + |
| 23 | +#define ENABLE_DEBUG (0) |
| 24 | +#include "debug.h" |
| 25 | + |
| 26 | +static int _nvram_read(struct nvram *dev, uint8_t *dst, uint32_t src, |
| 27 | + size_t size); |
| 28 | +static int _nvram_write(struct nvram *dev, const uint8_t *src, uint32_t dst, |
| 29 | + size_t size); |
| 30 | + |
| 31 | +static uint8_t _convert_12_to_24(uint8_t hour) |
| 32 | +{ |
| 33 | + if (hour & DS1307_REG_HOUR_12H) { |
| 34 | + uint8_t tmp = bcd_to_byte(hour & DS1307_REG_HOUR_12H_MASK); |
| 35 | + if (hour & DS1307_REG_HOUR_PM) { |
| 36 | + if (tmp < 12) { |
| 37 | + tmp += 12; |
| 38 | + } |
| 39 | + } |
| 40 | + else { |
| 41 | + if (tmp == 12) { |
| 42 | + tmp = 0; |
| 43 | + } |
| 44 | + } |
| 45 | + hour = (bcd_from_byte(tmp) & DS1307_REG_HOUR_24H_MASK); |
| 46 | + } |
| 47 | + return hour; |
| 48 | +} |
| 49 | + |
| 50 | +int ds1307_init(ds1307_t *dev, const ds1307_params_t *params) |
| 51 | +{ |
| 52 | + int res; |
| 53 | + uint8_t hour; |
| 54 | + |
| 55 | + dev->i2c = params->i2c; |
| 56 | + |
| 57 | + i2c_acquire(dev->i2c); |
| 58 | + res = i2c_init_master(dev->i2c, params->clk); |
| 59 | + if (res < 0) { |
| 60 | + i2c_release(dev->i2c); |
| 61 | + DEBUG("ds1307: Error initializing I2C: %i\n", res); |
| 62 | + return -1; |
| 63 | + } |
| 64 | + /* normalize hour format */ |
| 65 | + res = i2c_read_reg(dev->i2c, DS1307_I2C_ADDRESS, DS1307_REG_HOUR, &hour); |
| 66 | + if (res < 0) { |
| 67 | + i2c_release(dev->i2c); |
| 68 | + DEBUG("ds1307: Error reading HOUR register on init: %i\n", res); |
| 69 | + return -1; |
| 70 | + } |
| 71 | + res = i2c_write_reg(dev->i2c, DS1307_I2C_ADDRESS, DS1307_REG_HOUR, |
| 72 | + _convert_12_to_24(hour)); |
| 73 | + i2c_release(dev->i2c); |
| 74 | + |
| 75 | + if (res < 0) { |
| 76 | + DEBUG("ds1307: Error writing HOUR register on init: %i\n", res); |
| 77 | + return -1; |
| 78 | + } |
| 79 | + dev->nvram.read = _nvram_read; |
| 80 | + dev->nvram.write = _nvram_write; |
| 81 | + dev->nvram.size = (DS1307_REG_RAM_LAST - DS1307_REG_RAM_FIRST) + 1; |
| 82 | + dev->nvram.extra = dev; |
| 83 | + return 0; |
| 84 | +} |
| 85 | + |
| 86 | +int ds1307_set_time(const ds1307_t *dev, const struct tm *time) |
| 87 | +{ |
| 88 | + uint8_t regs[DS1307_REG_YEAR - DS1307_REG_SEC + 1]; |
| 89 | + int res; |
| 90 | + |
| 91 | + regs[DS1307_REG_SEC] = (bcd_from_byte(time->tm_sec) & DS1307_REG_SEC_MASK); |
| 92 | + regs[DS1307_REG_MIN] = (bcd_from_byte(time->tm_min) & DS1307_REG_MIN_MASK); |
| 93 | + regs[DS1307_REG_HOUR] = (bcd_from_byte(time->tm_hour) & |
| 94 | + DS1307_REG_HOUR_24H_MASK); |
| 95 | + regs[DS1307_REG_DOW] = (bcd_from_byte(time->tm_wday + DS1307_DOW_OFFSET) & |
| 96 | + DS1307_REG_DOW_MASK); |
| 97 | + regs[DS1307_REG_DOM] = (bcd_from_byte(time->tm_mday) & DS1307_REG_DOM_MASK); |
| 98 | + regs[DS1307_REG_MON] = (bcd_from_byte(time->tm_mon + DS1307_MON_OFFSET) & |
| 99 | + DS1307_REG_MON_MASK); |
| 100 | + regs[DS1307_REG_YEAR] = bcd_from_byte(time->tm_year + DS1307_YEAR_OFFSET); |
| 101 | + i2c_acquire(dev->i2c); |
| 102 | + res = i2c_write_regs(dev->i2c, DS1307_I2C_ADDRESS, DS1307_REG_SEC, regs, |
| 103 | + sizeof(regs)); |
| 104 | + DEBUG("ds1307: wrote bytes %02x %02x %02x %02x %02x %02x %02x to device (result: %i)\n", |
| 105 | + regs[DS1307_REG_SEC], regs[DS1307_REG_MIN], regs[DS1307_REG_HOUR], |
| 106 | + regs[DS1307_REG_DOW], regs[DS1307_REG_DOM], regs[DS1307_REG_MON], |
| 107 | + regs[DS1307_REG_YEAR], res); |
| 108 | + i2c_release(dev->i2c); |
| 109 | + return (res < 0) ? -1 : 0; |
| 110 | +} |
| 111 | + |
| 112 | +int ds1307_get_time(const ds1307_t *dev, struct tm *time) |
| 113 | +{ |
| 114 | + uint8_t regs[DS1307_REG_YEAR - DS1307_REG_SEC + 1]; |
| 115 | + int res; |
| 116 | + |
| 117 | + i2c_acquire(dev->i2c); |
| 118 | + res = i2c_read_regs(dev->i2c, DS1307_I2C_ADDRESS, DS1307_REG_SEC, regs, |
| 119 | + sizeof(regs)); |
| 120 | + DEBUG("ds1307: read bytes %02x %02x %02x %02x %02x %02x %02x from device (result: %i)\n", |
| 121 | + regs[DS1307_REG_SEC], regs[DS1307_REG_MIN], regs[DS1307_REG_HOUR], |
| 122 | + regs[DS1307_REG_DOW], regs[DS1307_REG_DOM], regs[DS1307_REG_MON], |
| 123 | + regs[DS1307_REG_YEAR], res); |
| 124 | + i2c_release(dev->i2c); |
| 125 | + if (res < 0) { |
| 126 | + return -1; |
| 127 | + } |
| 128 | + time->tm_sec = bcd_to_byte(regs[DS1307_REG_SEC] & DS1307_REG_SEC_MASK); |
| 129 | + time->tm_min = bcd_to_byte(regs[DS1307_REG_MIN] & DS1307_REG_MIN_MASK); |
| 130 | + time->tm_hour = bcd_to_byte(regs[DS1307_REG_HOUR] & |
| 131 | + DS1307_REG_HOUR_24H_MASK); |
| 132 | + time->tm_wday = (bcd_to_byte(regs[DS1307_REG_DOW] & DS1307_REG_DOW_MASK) - |
| 133 | + DS1307_DOW_OFFSET); |
| 134 | + time->tm_mday = bcd_to_byte(regs[DS1307_REG_DOM] & DS1307_REG_DOM_MASK); |
| 135 | + time->tm_mon = bcd_to_byte(regs[DS1307_REG_MON] & DS1307_REG_MON_MASK) - |
| 136 | + DS1307_MON_OFFSET; |
| 137 | + time->tm_year = (bcd_to_byte(regs[DS1307_REG_YEAR]) - DS1307_YEAR_OFFSET); |
| 138 | + return 0; |
| 139 | +} |
| 140 | + |
| 141 | +int ds1307_halt(const ds1307_t *dev) |
| 142 | +{ |
| 143 | + int res; |
| 144 | + uint8_t sec; |
| 145 | + |
| 146 | + i2c_acquire(dev->i2c); |
| 147 | + res = i2c_read_reg(dev->i2c, DS1307_I2C_ADDRESS, DS1307_REG_SEC, &sec); |
| 148 | + if (res < 0) { |
| 149 | + i2c_release(dev->i2c); |
| 150 | + DEBUG("ds1307: Error reading SEC register on halt: %i\n", res); |
| 151 | + return -1; |
| 152 | + } |
| 153 | + sec |= DS1307_REG_SEC_CH; |
| 154 | + res = i2c_write_reg(dev->i2c, DS1307_I2C_ADDRESS, DS1307_REG_SEC, sec); |
| 155 | + i2c_release(dev->i2c); |
| 156 | + return (res < 0) ? -1 : 0; |
| 157 | +} |
| 158 | + |
| 159 | +int ds1307_set_sqw_mode(const ds1307_t *dev, ds1307_sqw_mode_t mode) |
| 160 | +{ |
| 161 | + int res; |
| 162 | + |
| 163 | + i2c_acquire(dev->i2c); |
| 164 | + res = i2c_write_reg(dev->i2c, DS1307_I2C_ADDRESS, DS1307_REG_SQW_CTL, |
| 165 | + (uint8_t)mode); |
| 166 | + i2c_release(dev->i2c); |
| 167 | + return res; |
| 168 | +} |
| 169 | + |
| 170 | +int ds1307_get_sqw_mode(const ds1307_t *dev) |
| 171 | +{ |
| 172 | + uint8_t mode; |
| 173 | + int res; |
| 174 | + |
| 175 | + i2c_acquire(dev->i2c); |
| 176 | + res = i2c_read_reg(dev->i2c, DS1307_I2C_ADDRESS, DS1307_REG_SQW_CTL, &mode); |
| 177 | + i2c_release(dev->i2c); |
| 178 | + return (res < 0) ? res : (int)mode; |
| 179 | +} |
| 180 | + |
| 181 | +static int _nvram_read(struct nvram *nvram, uint8_t *dst, uint32_t src, |
| 182 | + size_t size) |
| 183 | +{ |
| 184 | + const ds1307_t *dev = nvram->extra; |
| 185 | + int res; |
| 186 | + |
| 187 | + if ((src + size) > nvram->size) { |
| 188 | + return -3; |
| 189 | + } |
| 190 | + i2c_acquire(dev->i2c); |
| 191 | + res = i2c_read_regs(dev->i2c, DS1307_I2C_ADDRESS, |
| 192 | + DS1307_REG_RAM_FIRST + src, dst, size); |
| 193 | + i2c_release(dev->i2c); |
| 194 | + return res; |
| 195 | +} |
| 196 | + |
| 197 | +static int _nvram_write(struct nvram *nvram, const uint8_t *src, uint32_t dst, |
| 198 | + size_t size) |
| 199 | +{ |
| 200 | + const ds1307_t *dev = nvram->extra; |
| 201 | + int res; |
| 202 | + |
| 203 | + if ((dst + size) > nvram->size) { |
| 204 | + return -3; |
| 205 | + } |
| 206 | + i2c_acquire(dev->i2c); |
| 207 | + res = i2c_write_regs(dev->i2c, DS1307_I2C_ADDRESS, |
| 208 | + DS1307_REG_RAM_FIRST + dst, src, size); |
| 209 | + i2c_release(dev->i2c); |
| 210 | + return res; |
| 211 | +} |
| 212 | + |
| 213 | +/** @} */ |
0 commit comments