cancel
Showing results for 
Search instead for 
Did you mean: 

Bug in HAL RTC at IS_RTC_MONTH macro ?

I M
Associate
Posted on April 18, 2017 at 13:05

Hi,

I am working with the HAL library using the RTC module. I think I found a bug in the 

stm32l1xx_hal_rtc.h file.

The file stm32l1xx_hal_rtc.h has the following code

...

#define RTC_MONTH_JULY ((uint8_t)0x07)

#define RTC_MONTH_AUGUST ((uint8_t)0x08)

#define RTC_MONTH_SEPTEMBER ((uint8_t)0x09)

#define RTC_MONTH_OCTOBER ((uint8_t)0x10) // ------------> But not 0x0A

#define RTC_MONTH_NOVEMBER ((uint8_t)0x11)

//

------------> 

But not 0x0B

#define RTC_MONTH_DECEMBER ((uint8_t)0x12)

//

------------> 

But not 0x0C

#define IS_RTC_MONTH(MONTH) (((MONTH) >= (uint32_t)1) && ((MONTH) <= (uint32_t)12))   

...

IS_RTC_MONTH macro checks if MONTH is <= 12, but RTC_MONTH_OCTOBER has value 0x10 which is 16 > 12, 

RTC_MONTH_NOVEMBER has value 0x11 (17 > 12) and RTC_MONTH_DECEMBER has value 0x12 (18 > 12). Therefore, the checks with these 3 months will fail.

The 2 available options are:

  • Redefine 

    RTC_MONTH_OCTOBER,

    RTC_MONTH_NOVEMBER and 

    RTC_MONTH_DECEMBER

  • Check that MONTH is between 0x00 and 0x09 or 0x10 and 0x12.

Is it a bug?

1 ACCEPTED SOLUTION

Accepted Solutions
Posted on April 19, 2017 at 18:29

Hi,

The RTC of the STM32 microcontrollers can work on binary or BCD format, the format is selected, the most used mode is BCD, and for that reason the macros of October, November and December have those values. The HAL_RTC have two functions to work with the values:

uint8_t RTC_ByteToBcd2(uint8_t Value)

uint8_t RTC_Bcd2ToByte(uint8_t Value)

So I suggest to don't use the macro 

IS_RTC_MONTH(MONTH), instead implement a function like this:

uint8_t 

IS_RTC_MONTH_2(uint8_t MONTH){

   if((RTC_Bcd2ToByte(MONTH)) >= (uint32_t)1) && (RTC_Bcd2ToByte(MONTH)) <= (uint32_t)12){

     return 1;

   }

   else{

      return 0;

   }

}

View solution in original post

2 REPLIES 2
Posted on April 19, 2017 at 18:29

Hi,

The RTC of the STM32 microcontrollers can work on binary or BCD format, the format is selected, the most used mode is BCD, and for that reason the macros of October, November and December have those values. The HAL_RTC have two functions to work with the values:

uint8_t RTC_ByteToBcd2(uint8_t Value)

uint8_t RTC_Bcd2ToByte(uint8_t Value)

So I suggest to don't use the macro 

IS_RTC_MONTH(MONTH), instead implement a function like this:

uint8_t 

IS_RTC_MONTH_2(uint8_t MONTH){

   if((RTC_Bcd2ToByte(MONTH)) >= (uint32_t)1) && (RTC_Bcd2ToByte(MONTH)) <= (uint32_t)12){

     return 1;

   }

   else{

      return 0;

   }

}

Posted on April 20, 2017 at 09:01

 ,

 ,

OK, I did not see the comment before the RTC_MONTH_xxx defines:

/* Coded in BCD format */

 ,

♯ define RTC_MONTH_JANUARY ((uint8_t)0x01)

 ,

♯ define RTC_MONTH_FEBRUARY ((uint8_t)0x02)

...

So if I need to use IS_RTC_MONTH I need to use ,

RTC_Bcd2ToByte(MONTH) first.

Thank you for your reply.