Skip to content

Modify description of set_time() function. #5100

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

Closed
wants to merge 1 commit into from

Conversation

mprse
Copy link
Contributor

@mprse mprse commented Sep 14, 2017

Description

Add note about setting RTC time value to 0.

This PR is a result of discussion from PR#5087:

set_time() function on some platforms does not set RTC time to 0. In such case specific driver function - rtc_write() sets time to 1 instead of 0. This is caused because on some platforms 0 is an invalid time. Unfortunately there is no information about this behaviour. Maybe a warning should be added to the set_time() function description?

Just a note in the doxygen should be enough.

Status

READY

Migrations

NO

Add note about setting RTC time value to 0.
@0xc0170
Copy link
Contributor

0xc0170 commented Sep 19, 2017

cc @c1728p9

@0xc0170
Copy link
Contributor

0xc0170 commented Sep 19, 2017

what platforms have this behaviour ?

@mprse
Copy link
Contributor Author

mprse commented Sep 20, 2017

For example K64F board have this issue. But it looks like it is valid for the following freescale platforms:
/m-bed/targets/TARGET_Freescale/TARGET_K20XX/rtc_api.c
/m-bed/targets/TARGET_Freescale/TARGET_KLXX/rtc_api.c
/m-bed/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/api/rtc_api.c

Code example:

void rtc_write(time_t t)
{
    if (t == 0) {    <---- issue
        t = 1;
    }
    RTC_StopTimer(RTC);
    RTC->TSR = t;
    RTC_StartTimer(RTC);
}

Note: When I remove code which modify 'time to set' to 1 in case when 0 is given, then RTC time can be successfully set to 0 and RTC counts time (checked on K64F board). Maybe drivers should be modified?

@0xc0170
Copy link
Contributor

0xc0170 commented Sep 20, 2017

That makes it more clear now. I had a look at KL27Z reference manual

Writing to TSR with zero is supported, but not
recommended because TSR will read as zero when SR[TIF] or SR[TOF] are set (indicating the time is
invalid).

This is target specific. I've checked other targets, they support writing 0. There are often anomalies in the API like this, not certain if we create notes for each target (we might end up with lot of notes in some cases).
We could change this to set to 0 in those Freescale targets, and make a note from the statement above in their targets, that setting 0 makes those 2 flags set, be aware.

One thing I have noticed, lot of our targets use time_base variable that is set in rtc_write()? An example: https://github.com/ARMmbed/mbed-os/blob/master/targets/TARGET_Silicon_Labs/TARGET_EFM32/rtc_api.c#L268

@mmahadevan108

@bulislaw @c1728p9 @deepikabhavnani Suggestions?

* @note On some platforms time can not be set to 0 since it is invalid value.
* In such case when 0 value is passed to this function, then RTC time
* value is set to 1.
*
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 consider this a bug with the vendor implementation. If 0 is unsupported by hardware then they should have a software translation.

I would argue code like this should be updated to have an offset instead:

#define RTC_OFFSET 1
time_t rtc_read(void)
{
    return (time_t)(RTC->TSR - RTC_OFFSET);
}

void rtc_write(time_t t)
{
    RTC_StopTimer(RTC);
    RTC->TSR = t + RTC_OFFSET;
    RTC_StartTimer(RTC);
}

@mmahadevan108
Copy link
Contributor

I have created a PR to allow writing 0 to the TSR register. There is no hardware limitation in writing this value.

It is suggested in the manual to avoid this value as 0 when read from the TSR also indicates that a overflow or invalid flag is set. However we do not use this way inside mbed and probe the status register directly for overflow and invalid time information.

@0xc0170
Copy link
Contributor

0xc0170 commented Sep 21, 2017

We can close this PR. @mprse Thanks for finding this bug

@0xc0170 0xc0170 closed this Sep 21, 2017
@mprse mprse deleted the set_time_description_update branch September 22, 2017 08:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants