Skip to content

Conversation

miri64
Copy link
Member

@miri64 miri64 commented Jul 4, 2017

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).

@miri64 miri64 added Area: drivers Area: Device drivers Type: new feature The issue requests / The PR implemements a new feature for RIOT Area: timers Area: timer subsystems State: waiting for other PR State: The PR requires another PR to be merged first labels Jul 4, 2017
@miri64 miri64 requested review from haukepetersen and aabadie July 4, 2017 11:34
@aabadie
Copy link
Contributor

aabadie commented Jul 4, 2017

I have one of these somewhere so I should be able to test.

@aabadie aabadie added this to the Release 2017.10 milestone Jul 4, 2017
@miri64
Copy link
Member Author

miri64 commented Jul 4, 2017

(I tested it on samr21-xpro FYI).

@miri64
Copy link
Member Author

miri64 commented Jul 4, 2017

Not sure if I should provide a separate test for the square wave generator.

Copy link
Contributor

@aabadie aabadie left a 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,
Copy link
Contributor

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

Copy link
Member Author

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.

Copy link
Contributor

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

static int _nvram_read(struct nvram *nvram, uint8_t *dst, uint32_t src,
size_t size)
{
ds1307_t *dev = nvram->extra;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const ds1307_t ?

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);
Copy link
Contributor

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

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

static ds1307_t dev;

static struct tm init = { /* Wed Sep 22 15:10:42 2010 is the author date of
* RIOT's initial commit ;-) */
Copy link
Contributor

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++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unsigned

Copy link
Member Author

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++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unsigned

Copy link
Member Author

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it is :-)

Copy link
Member Author

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 */
Copy link
Contributor

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

Copy link
Member Author

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.

Copy link
Contributor

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;

Copy link
Member Author

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.

Copy link
Contributor

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

Copy link
Member Author

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.

@miri64
Copy link
Member Author

miri64 commented Jul 4, 2017

Addressed first batch of comments

dev->addr = params->addr;

i2c_acquire(dev->i2c);
i2c_init_master(dev->i2c, params->clk);
Copy link
Contributor

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

@miri64
Copy link
Member Author

miri64 commented Jul 4, 2017

Missed some :-/.

Copy link
Contributor

@aabadie aabadie left a 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,
Copy link
Contributor

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, \
Copy link
Contributor

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

/**
* @brief Maximum size in byte of on-chip NVRAM
*/
#define DS1307_NVRAM_MAX_SIZE (56)
Copy link
Contributor

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 */
Copy link
Contributor

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;

* @param[in] params device configuration (i2c bus, address and bus clock)
*
* @return 0 on success
* @return -1 if unable to speak to the device
Copy link
Contributor

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...

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 */
Copy link
Member Author

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@miri64
Copy link
Member Author

miri64 commented Jul 6, 2017

Rebased to current master. No longer waiting on other PR.

@miri64 miri64 added CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR and removed State: waiting for other PR State: The PR requires another PR to be merged first labels Jul 6, 2017
Copy link
Contributor

@haukepetersen haukepetersen left a 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, \
Copy link
Contributor

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)
Copy link
Contributor

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.

Copy link
Member Author

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it is :-)

@haukepetersen
Copy link
Contributor

Tested on nucleo-l152 -> works as expected.

@miri64 miri64 force-pushed the ds1307/feat/initial branch from 1649867 to 162e0ae Compare July 31, 2017 12:35
@miri64 miri64 force-pushed the ds1307/feat/initial branch from 162e0ae to 94763ab Compare July 31, 2017 12:39
@miri64
Copy link
Member Author

miri64 commented Jul 31, 2017

Squashed.

@miri64
Copy link
Member Author

miri64 commented Jul 31, 2017

@aabadie you give your ACK as well?

@miri64
Copy link
Member Author

miri64 commented Aug 8, 2017

@aabadie ping?

Copy link
Contributor

@aabadie aabadie left a 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.

@aabadie
Copy link
Contributor

aabadie commented Aug 16, 2017

and go

@aabadie aabadie merged commit 5cb0be3 into RIOT-OS:master Aug 16, 2017
@miri64 miri64 deleted the ds1307/feat/initial branch August 16, 2017 09:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: drivers Area: Device drivers Area: timers Area: timer subsystems CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Type: new feature The issue requests / The PR implemements a new feature for RIOT
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants