cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F407G-DISC1 - RTC (LSI) with HAL

hackerli90
Associate II
Posted on November 07, 2016 at 12:50

hi

im trying to get the rtc clock working on my discovery board.

i configured the project with cubeMX, id like to use the LSI since i got no LSE, and im working with the latest HAL F4 driver.

the rtc init works fine, every call ends with HAL_OK.

also, if i start debugging and always the FIRST rtc_gettime call is successfully!

from this point on though, the rtc is state READY but it looks like it has been reset or something, since the 2nd getTime call returns the init time from this point on.

just the subseconds still change, but not the actual time.

it looks to me like i missed a IRQ handler or similar, but the HAL driver does only have an alarm irq handler (exti).

anyone an idea where this errors origin could be?
5 REPLIES 5
Walid FTITI_O
Senior II
Posted on November 07, 2016 at 14:15

Hi Jibbajibba, 

To let other users trying to help you, you should share your target code ( main function, RTC ocnfiguration , system clock .. -> all user files if needed) and the .ioc file .

-Hannibal-

hackerli90
Associate II
Posted on November 07, 2016 at 15:11

Hi hannibal

okay, i will do so tonight.

hackerli90
Associate II
Posted on November 07, 2016 at 18:49

okay here is the cube example code:

/* Private variables ---------------------------------------------------------*/

RTC_HandleTypeDef hrtc;

/* USER CODE BEGIN PV */

/* Private variables ---------------------------------------------------------*/

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

void Error_Handler(void);

static void MX_GPIO_Init(void);

static void MX_RTC_Init(void);

/* USER CODE BEGIN PFP */

/* Private function prototypes -----------------------------------------------*/

/* USER CODE END PFP */

/* USER CODE BEGIN 0 */

static RTC_TimeTypeDef buffer;

/* USER CODE END 0 */

int main(void)

{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

  HAL_Init();

  /* Configure the system clock */

  SystemClock_Config();

  /* Initialize all configured peripherals */

  MX_GPIO_Init();

  MX_RTC_Init();

  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */

  /* USER CODE BEGIN WHILE */

  while (1)

  {

  /* USER CODE END WHILE */

      if (HAL_RTC_GetTime(&hrtc, &buffer, RTC_FORMAT_BCD) != HAL_OK)

      {

        Error_Handler();

      }

  /* USER CODE BEGIN 3 */

  }

  /* USER CODE END 3 */

}

/** System Clock Configuration

*/

void SystemClock_Config(void)

{

  RCC_OscInitTypeDef RCC_OscInitStruct;

  RCC_ClkInitTypeDef RCC_ClkInitStruct;

  RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;

    /**Configure the main internal regulator output voltage

    */

  __HAL_RCC_PWR_CLK_ENABLE();

  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

    /**Initializes the CPU, AHB and APB busses clocks

    */

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;

  RCC_OscInitStruct.HSIState = RCC_HSI_ON;

  RCC_OscInitStruct.HSICalibrationValue = 16;

  RCC_OscInitStruct.LSIState = RCC_LSI_ON;

  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;

  RCC_OscInitStruct.PLL.PLLM = 8;

  RCC_OscInitStruct.PLL.PLLN = 50;

  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;

  RCC_OscInitStruct.PLL.PLLQ = 7;

  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

  {

    Error_Handler();

  }

    /**Initializes the CPU, AHB and APB busses clocks

    */

  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;

  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;

  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)

  {

    Error_Handler();

  }

  PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;

  PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;

  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)

  {

    Error_Handler();

  }

    /**Configure the Systick interrupt time

    */

  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

    /**Configure the Systick

    */

  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  /* SysTick_IRQn interrupt configuration */

  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);

}

/* RTC init function */

static void MX_RTC_Init(void)

{

  RTC_TimeTypeDef sTime;

  RTC_DateTypeDef sDate;

    /**Initialize RTC Only

    */

  hrtc.Instance = RTC;

  hrtc.Init.HourFormat = RTC_HOURFORMAT_12;

  hrtc.Init.AsynchPrediv = 127;

  hrtc.Init.SynchPrediv = 255;

  hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;

  hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;

  hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;

  if (HAL_RTC_Init(&hrtc) != HAL_OK)

  {

    Error_Handler();

  }

    /**Initialize RTC and set the Time and Date

    */

  sTime.Hours = 0x3;

  sTime.Minutes = 0x3;

  sTime.Seconds = 0x3;

  sTime.TimeFormat = RTC_HOURFORMAT12_AM;

  sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;

  sTime.StoreOperation = RTC_STOREOPERATION_RESET;

  if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)

  {

    Error_Handler();

  }

  sDate.WeekDay = RTC_WEEKDAY_MONDAY;

  sDate.Month = RTC_MONTH_JANUARY;

  sDate.Date = 0x1;

  sDate.Year = 0x16;

  if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)

  {

    Error_Handler();

  }

}

void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)

{

  if(hrtc->Instance==RTC)

  {

  /* USER CODE BEGIN RTC_MspInit 0 */

  /* USER CODE END RTC_MspInit 0 */

    /* Peripheral clock enable */

    __HAL_RCC_RTC_ENABLE();

  /* USER CODE BEGIN RTC_MspInit 1 */

  /* USER CODE END RTC_MspInit 1 */

  }

}

/**

* @brief This function handles System tick timer.

*/

void SysTick_Handler(void)

{

  /* USER CODE BEGIN SysTick_IRQn 0 */

  /* USER CODE END SysTick_IRQn 0 */

  HAL_IncTick();

  HAL_SYSTICK_IRQHandler();

  /* USER CODE BEGIN SysTick_IRQn 1 */

  /* USER CODE END SysTick_IRQn 1 */

}
Walid FTITI_O
Senior II
Posted on November 08, 2016 at 16:03

Hi Jibbajibba, 

You should call HAL_RTC_GetDate()  afer calling HAL_RTC_GetTime() as the following note indicate inside the User maual

http://www.ue.eti.pg.gda.pl/~bpa/kolo_chip/aeti2016/docs/HAL%20Manual.pdf

''Description of STM32F4xx HAL drivers '':

Notes You must call HAL_RTC_GetDate() afterHAL_RTC_GetTime() to unlock the values in the higher-ordercalendar shadow registers to ensure consistency between thetime and date values. Reading RTC current time locks thevalues in calendar shadow registers until Current date is read.

-Hannibal-

hackerli90
Associate II
Posted on November 08, 2016 at 20:49

SOLVED !

hannibal, that was the error. thank you a lot, was searching for a long time!

  while (1)

  {

    if(HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BCD) != HAL_OK)

    {

      break;

    }

    if(HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BCD) != HAL_OK)

    {

      break;

    }

  }