Skip to content

Add RTC driver #177

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 2 commits into from
Closed

Add RTC driver #177

wants to merge 2 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Dec 13, 2017

Driver to use the internal RTC:

  • Enable/disable RTC
  • Set/Get time & calendar
  • Enable/disable alarm
  • Attach/detach alarm callback

This driver is linked with the STM32RTC library.

Copy link
Member

@LMESTM LMESTM left a comment

Choose a reason for hiding this comment

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

One comment / proposal about the callback type but overall looks good to me

@@ -175,7 +175,7 @@

/* Section 2: PHY configuration section */

/* DP83848 PHY Address*/
/* DP83848 PHY Address*/
Copy link
Member

Choose a reason for hiding this comment

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

You're mixing up 2 things in the commit - clean-up of various white spaces with the actual changes for RTC.
In such cases, please try to make 2 separate commits instead ...

Copy link
Author

Choose a reason for hiding this comment

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

Done!

* @param func: pointer to the callback
* @retval None
*/
void attachAlarmCallback(void (*func)(void))
Copy link
Member

Choose a reason for hiding this comment

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

rather than
void (*func)(void)
it could be useful from application to offer a callback of type
void (func)(void data)

Copy link
Member

Choose a reason for hiding this comment

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

you would mean: void (*func)(void*)?

Copy link
Author

Choose a reason for hiding this comment

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

What information would you like to pass in parameter? rtc handler? other?

Copy link
Member

Choose a reason for hiding this comment

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

This is up to application . It could the time when alarm was set, or a pointer to the led to be turned on. The application provides a pointer and you simply pass it back to application. This is useful for callback mecanisms. It would be particularly useful if setting several alarms.

Copy link
Author

Choose a reason for hiding this comment

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

It is not the application that choice this parameter. It is the driver which can return a parameter.
If you don't say me the parameter to return, I think return a void* is useless.
Or I must change the prototype of attachAlarmCallback to take account another parameter with the data pointer (also useless).

Copy link
Member

Choose a reason for hiding this comment

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

sorry if unclear, the user data is passed from application to driver, stored in driver and then passed back from driver to application. The driver doesn't know which informaition it contains.

This can be useful for coding generic callback where you don't need to store application data as globals or in context.
https://arduino.stackexchange.com/questions/23063/user-callback-functions
https://stackoverflow.com/questions/14270846/passing-a-parameter-in-a-callback-in-c

In the case of RTC alarm, this can be useful if application can set 2 different alarms in future, then application could pass the programmed time, or a different LED or whatever it needs

Copy link
Author

Choose a reason for hiding this comment

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

It is clearer thank you. And done.

Copy link
Member

@fpistm fpistm left a comment

Choose a reason for hiding this comment

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

This driver must handle all possible RTC clock config.

/* RTC prediv are set to the default values to match with the LSE clock source.
If the clock source is modified, those prescalers must be changed */
/* Synchonuous prediv */
#define PREDIV_S 0x00FF
Copy link
Member

Choose a reason for hiding this comment

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

Main restriction of the driver is the RTC clock configuration.
The driver have to be able to dela with all possible source (HSE, LSE,LSI).
Refer to AN4759 Overview of the STM32 MCU advanced RTC
§ 1.1.4 RTC clock configuration

* the backup registers) and RCC_CSR register are set to their reset values.
* @retval None
*/
void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc)
Copy link
Member

Choose a reason for hiding this comment

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

ditto

* @param format: enable the RTC in 12 or 24 hours mode
* @retval None
*/
void RTC_init(hourFormat_t format)
Copy link
Member

Choose a reason for hiding this comment

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

Have to mange RTC clock config

@ghost
Copy link
Author

ghost commented Jan 24, 2018

RTC driver updated with clock source selection.
The RTC clock source can be set: LSE, LSI or HSE.
User can set the prescaler values for his own configuration. The user clock configuration is not overwritten.
Default prescaler values are provided for the most common hardware clock configuration.

Compilation OK.
Tests OK for LSE and LSI. HSE not tested.
Tested only on boards NUCLEO L0, L4, F0, F1, F3, F4.
L1, F2 & F7 missing.

@ghost ghost requested a review from fpistm January 24, 2018 13:19
@fpistm fpistm self-assigned this Jan 29, 2018
@fpistm fpistm added this to the Next release milestone Feb 1, 2018
Copy link
Member

@fpistm fpistm left a comment

Choose a reason for hiding this comment

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

It should be possible to define [HSE|LSE|LSI]_A?SYNC_PREDIVas for each variant clock values are defined.
Ex:

#define LSE_VALUE ((uint32_t)32768U) /*!< Value of the External oscillator in Hz*/

((WEEKDAY) == RTC_WEEKDAY_SATURDAY) || \
((WEEKDAY) == RTC_WEEKDAY_SUNDAY))

#define IS_RTC_HOUR12(HOUR) IS_RTC_HOUR24(HOUR)
Copy link
Member

Choose a reason for hiding this comment

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

Seems F1 do not define it. Probably because it do not handle 12h format.
A comment could be added to give the reason.

}
RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
if(source == LSE_CLOCK) {
RtcHandle.Init.AsynchPrediv = LSE_ASYNCH_PREDIV;
Copy link
Member

Choose a reason for hiding this comment

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

[HSE|LSE|LSI]_A?SYNC_PREDIV should not be hard-coded. User should have a way to dynamically change them without add a dedicated define. At least an API to change the default value.
Anyway, as mentioned in rtc.h, it should be possible to define the best PREDIV

- LSE = 32.768 kHz
- HSE = 8 MHz

Prescalers should be set by user in case the clock configuration is different.
Copy link
Member

Choose a reason for hiding this comment

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

It should be possible to define [HSE|LSE|LSI]_A?SYNC_PREDIV as for each variant clock values are defined.
Ex:

#define LSE_VALUE ((uint32_t)32768U) /*!< Value of the External oscillator in Hz*/

@lacklustrlabs
Copy link
Contributor

Have you considered adding an attachAlarmCallback(std::function) function?
I guess that will introduce an additional, potentially costly, C/C++ interfacing layer.
What is your opinion?

@fpistm
Copy link
Member

fpistm commented Feb 9, 2018

@lacklustrlabs
I do not considered this but it could. This has been done for the interrupt.
I'm not very aware about that and it's use/utility but why not.

@lacklustrlabs
Copy link
Contributor

@fpistm
As you know std::function is a generic way of handling callable objects in C++ (functions, lambda expressions, bind expressions).

I guess std::function callbacks can be added at any time later on, the only thing that should be considered now is if std::function callbacks are good (fast and memory efficient) enough to replace C-style callbacks. If so, the switch-over should be made now, before the API is released.

If you want plain C-style callbacks in the API, the std::function callbacks can be added at any time later. The downside of this option is a, possible, duplicated callback handling.

biagiom added a commit to biagiom/Arduino_Core_STM32 that referenced this pull request Feb 12, 2018
… quakesense branch

- Add RTC driver for STM32 boards
- Fix some conflicts in cores/arduino/board.h
@fpistm
Copy link
Member

fpistm commented Mar 6, 2018

Hi @fprwi6labs
any news on this PR?

@fpistm
Copy link
Member

fpistm commented Mar 6, 2018

@lacklustrlabs
It could be considered.
Maybe @fprwi6labs have an opinion on this

@fpistm fpistm added the waiting feedback Further information is required label Mar 7, 2018
fpr added 2 commits March 15, 2018 14:16
@ghost
Copy link
Author

ghost commented Mar 15, 2018

Hi @fpistm
Prescalers are now dynamically calculated.
Compilation OK for all available boards but not tested with HSE clock source.

@lacklustrlabs
Is it possible to use std::function<void(void *)>? I tried but without success and I didn't find example.
So I will not implement this feature for now.

@ghost ghost requested review from LMESTM and fpistm March 15, 2018 14:37
@fpistm fpistm modified the milestones: Next release, 1.2.1 Mar 23, 2018
@biagiom biagiom mentioned this pull request Apr 3, 2018
@fpistm fpistm removed the waiting feedback Further information is required label Apr 5, 2018
Copy link
Member

@fpistm fpistm left a comment

Choose a reason for hiding this comment

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

Some issues and restrictions found during the review.
Those have been fixed in #235

@fpistm fpistm added abandoned No more work on this and removed review on going labels Apr 24, 2018
@fpistm
Copy link
Member

fpistm commented Apr 24, 2018

#235 include this PR rebased on top of the master.
This on is abandoned.

@fpistm fpistm closed this Apr 24, 2018
@fpistm fpistm removed this from the 1.2.1/1.3.0 milestone May 28, 2018
@fpistm fpistm added enhancement New feature or request and removed New feature labels Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
abandoned No more work on this enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants