ds1307: initial import of a driver for the DS1307 RTC#7312
Conversation
|
I have one of these somewhere so I should be able to test. |
|
(I tested it on samr21-xpro FYI). |
|
Not sure if I should provide a separate test for the square wave generator. |
aabadie
left a comment
There was a problem hiding this comment.
Went briefly into this one, just codewise. See comments inline.
You could also add some auto_init support, since you wrote added default params.
| return (res < 0) ? res : (int)mode; | ||
| } | ||
|
|
||
| static int _nvram_read(struct nvram *nvram, uint8_t *dst, uint32_t src, |
There was a problem hiding this comment.
This PR is not an API change of the nvram.h API.
| static int _nvram_read(struct nvram *nvram, uint8_t *dst, uint32_t src, | ||
| size_t size) | ||
| { | ||
| ds1307_t *dev = nvram->extra; |
| regs[DS1307_REG_SEC] = (bcd_from_byte(time->tm_sec) & DS1307_REG_SEC_MASK); | ||
| regs[DS1307_REG_MIN] = (bcd_from_byte(time->tm_min) & DS1307_REG_MIN_MASK); | ||
| regs[DS1307_REG_HOUR] = (bcd_from_byte(time->tm_hour) & | ||
| DS1307_REG_HOUR_24H_MASK); |
There was a problem hiding this comment.
style: could be better aligned
| regs[DS1307_REG_HOUR] = (bcd_from_byte(time->tm_hour) & | ||
| DS1307_REG_HOUR_24H_MASK); | ||
| regs[DS1307_REG_DOW] = (bcd_from_byte(time->tm_wday + DS1307_DOW_OFFSET) & | ||
| DS1307_REG_DOW_MASK); |
| DS1307_REG_DOW_MASK); | ||
| regs[DS1307_REG_DOM] = (bcd_from_byte(time->tm_mday) & DS1307_REG_DOM_MASK); | ||
| regs[DS1307_REG_MON] = (bcd_from_byte(time->tm_mon + DS1307_MON_OFFSET) & | ||
| DS1307_REG_MON_MASK); |
| static ds1307_t dev; | ||
|
|
||
| static struct tm init = { /* Wed Sep 22 15:10:42 2010 is the author date of | ||
| * RIOT's initial commit ;-) */ |
|
|
||
| static void test_get_time(void) | ||
| { | ||
| for (int i = 0; i < 5; i++) { |
There was a problem hiding this comment.
Isn't used in the loop, so why?
| static void test_halt(void) | ||
| { | ||
| ds1307_halt(&dev); | ||
| for (int i = 0; i < 3; i++) { |
There was a problem hiding this comment.
Isn't used in the loop, so why?
|
|
||
| int main(void) | ||
| { | ||
| int res; |
| * @brief Device descriptor for DS1307 devices | ||
| */ | ||
| typedef struct { | ||
| i2c_t i2c; /**< I2C bus the device is connected to */ |
There was a problem hiding this comment.
These 2 attributes could be replaced by one ds1307_params_t
There was a problem hiding this comment.
But ds1307_t also contains clk, which I don't need later on.
There was a problem hiding this comment.
Ok, I see. Since it's only used during init, maybe using a macro would be enough. Then your device config just become:
typedef struct {
ds1307_params_t params;
nvram_t nvram;
} ds1307_t;
typedef struct {
i2c_t i2c;
uint8_t addr;
} ds1307_params_t;There was a problem hiding this comment.
Then a lot of other param structs for I2C are false too. I don't think that would be correct.
There was a problem hiding this comment.
I know, something that we can surely look into
There was a problem hiding this comment.
Yes, but not in this PR. I think #6576 does this differently anyway.
|
Addressed first batch of comments |
| dev->addr = params->addr; | ||
|
|
||
| i2c_acquire(dev->i2c); | ||
| i2c_init_master(dev->i2c, params->clk); |
There was a problem hiding this comment.
You could also check if I2C can be initialized
|
Missed some :-/. |
| return (res < 0) ? res : (int)mode; | ||
| } | ||
|
|
||
| static int _nvram_read(struct nvram *nvram, uint8_t *dst, uint32_t src, |
| #ifndef DS1307_PARAM_I2C_CLK | ||
| #define DS1307_PARAM_I2C_CLK (DS1307_I2C_MAX_CLK) | ||
|
|
||
| #define DS1307_PARAMS_DEFAULT { .i2c = DS1307_PARAM_I2C, \ |
| /** | ||
| * @brief Maximum size in byte of on-chip NVRAM | ||
| */ | ||
| #define DS1307_NVRAM_MAX_SIZE (56) |
| * @brief Device descriptor for DS1307 devices | ||
| */ | ||
| typedef struct { | ||
| i2c_t i2c; /**< I2C bus the device is connected to */ |
There was a problem hiding this comment.
Ok, I see. Since it's only used during init, maybe using a macro would be enough. Then your device config just become:
typedef struct {
ds1307_params_t params;
nvram_t nvram;
} ds1307_t;
typedef struct {
i2c_t i2c;
uint8_t addr;
} ds1307_params_t;| * @param[in] params device configuration (i2c bus, address and bus clock) | ||
| * | ||
| * @return 0 on success | ||
| * @return -1 if unable to speak to the device |
| DS1307_SQW_MODE_1000HZ = 0x00, /**< SQW: 1kHz */ | ||
| DS1307_SQW_MODE_4096HZ = 0x00, /**< SQW: 4.096 kHz */ | ||
| DS1307_SQW_MODE_8192HZ = 0x00, /**< SQW: 8.192 kHz */ | ||
| DS1307_SQW_MODE_32768HZ = 0x00, /**< SQW: 32.768 kHz */ |
There was a problem hiding this comment.
Oops, these are not supposed to be all 0. Will fix
56504d6 to
2d9b665
Compare
|
Rebased to current master. No longer waiting on other PR. |
haukepetersen
left a comment
There was a problem hiding this comment.
Only one minor thing about the bus address, the rest looks good to me
| #ifndef DS1307_PARAM_I2C_CLK | ||
| #define DS1307_PARAM_I2C_CLK (DS1307_I2C_MAX_CLK) | ||
|
|
||
| #define DS1307_PARAMS_DEFAULT { .i2c = DS1307_PARAM_I2C, \ |
There was a problem hiding this comment.
I would have done it without the tab alignment here, too, but IMHO there is no right and wrong, so this should be fine I guess.
| #define DS1307_PARAM_I2C (I2C_DEV(0)) | ||
| #endif | ||
| #ifndef DS1307_PARAM_ADDR | ||
| #define DS1307_PARAM_ADDR (DS1307_I2C_ADDRESS) |
There was a problem hiding this comment.
The slave address is fixed to 0x68, so no need to parameterize it -> use DS1307_I2C_ADDRESS directly in the code.
|
|
||
| int main(void) | ||
| { | ||
| int res; |
|
Tested on |
1649867 to
162e0ae
Compare
162e0ae to
94763ab
Compare
|
Squashed. |
|
@aabadie you give your ACK as well? |
|
@aabadie ping? |
aabadie
left a comment
There was a problem hiding this comment.
ACK, sorry for the late reply.
|
and go |
While searching my Arduino Leonardo to test #7306 I found this breakout board, and while the board itself is discontinued (there is an updated version) the chip itself is still a popular RTC in the arduino community, as far as I know. So I decided to build some skills and develop my first own driver :-).
Depends on
#7311(merged).