-
Notifications
You must be signed in to change notification settings - Fork 2.1k
ds1307: initial import of a driver for the DS1307 RTC #7312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nvram could be made const
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR is not an API change of the nvram.h
API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, so that's fine then
drivers/ds1307/ds1307.c
Outdated
static int _nvram_read(struct nvram *nvram, uint8_t *dst, uint32_t src, | ||
size_t size) | ||
{ | ||
ds1307_t *dev = nvram->extra; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const ds1307_t ?
drivers/ds1307/ds1307.c
Outdated
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: could be better aligned
drivers/ds1307/ds1307.c
Outdated
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
drivers/ds1307/ds1307.c
Outdated
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and here
tests/driver_ds1307/main.c
Outdated
static ds1307_t dev; | ||
|
||
static struct tm init = { /* Wed Sep 22 15:10:42 2010 is the author date of | ||
* RIOT's initial commit ;-) */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe align comments
|
||
static void test_get_time(void) | ||
{ | ||
for (int i = 0; i < 5; i++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unsigned
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unsigned
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't used in the loop, so why?
|
||
int main(void) | ||
{ | ||
int res; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes it is :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see ll. 141-142.
* @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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These 2 attributes could be replaced by one ds1307_params_t
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But ds1307_t
also contains clk
, which I don't need later on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know, something that we can surely look into
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but not in this PR. I think #6576 does this differently anyway.
Addressed first batch of comments |
drivers/ds1307/ds1307.c
Outdated
dev->addr = params->addr; | ||
|
||
i2c_acquire(dev->i2c); | ||
i2c_init_master(dev->i2c, params->clk); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could also check if I2C can be initialized
Missed some :-/. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few more comments
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, so that's fine then
#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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't get what you mean
drivers/include/ds1307.h
Outdated
/** | ||
* @brief Maximum size in byte of on-chip NVRAM | ||
*/ | ||
#define DS1307_NVRAM_MAX_SIZE (56) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
56U ?
* @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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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;
drivers/include/ds1307.h
Outdated
* @param[in] params device configuration (i2c bus, address and bus clock) | ||
* | ||
* @return 0 on success | ||
* @return -1 if unable to speak to the device |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I insist on this one...
drivers/include/ds1307.h
Outdated
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, these are not supposed to be all 0. Will fix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
56504d6
to
2d9b665
Compare
Rebased to current master. No longer waiting on other PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The slave address is fixed to 0x68
, so no need to parameterize it -> use DS1307_I2C_ADDRESS
directly in the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
|
||
int main(void) | ||
{ | ||
int res; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes it is :-)
Tested on |
1649867
to
162e0ae
Compare
162e0ae
to
94763ab
Compare
Squashed. |
@aabadie you give your ACK as well? |
@aabadie ping? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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).