AnsweredAssumed Answered

STM32F407G-DISC1 - RTC - HAL

Question asked by Jibbajibba on Sep 22, 2016
Latest reply on Sep 23, 2016 by FTITI.Walid
hi guys
 
im currently working on the STM32407G-DISC1, the goal is RTC usage. HWlib is HAL.
Below i posted the RTC related code. I did check both the CubeMX autogenerated code as well as the F4 RTC example as well tested with LSI and HSE (I took the one from the NucleoF103).
But somehow the time is not incremented.. keeping 00:00:00. I wonder a bit that there is no IRQ routine except for the EXTI alarm line.
    
What could possibly be wrong here?

------------------------------------------------------------------

#define HAL_MODULE_ENABLED
#define HAL_RTC_MODULE_ENABLED
#define HAL_TIM_MODULE_ENABLED
#define HAL_UART_MODULE_ENABLED
#define HAL_GPIO_MODULE_ENABLED
#define HAL_DMA_MODULE_ENABLED
#define HAL_RCC_MODULE_ENABLED
#define HAL_FLASH_MODULE_ENABLED
#define HAL_PWR_MODULE_ENABLED
#define HAL_CORTEX_MODULE_ENABLED


extern void SystemClock_Configuration(void)
{
  SystemClock_HSE_LSI();
}

static void SystemClock_HSE_LSI(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;

  __HAL_RCC_PWR_CLK_ENABLE();

  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 336;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 7;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  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_5) != HAL_OK)
  {
    Error_Handler();
  }

  PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  /* SysTick_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
{
  if(hrtc->Instance==RTC)
  {
    /* Peripheral clock enable */
    __HAL_RCC_RTC_ENABLE();
    /* Peripheral interrupt init */
    HAL_NVIC_SetPriority(RTC_Alarm_IRQn, 15, 0);
    HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
  }
}

static e_retVal RTC_Configuration(void)
{
  RTC_TimeTypeDef sTime;
  RTC_DateTypeDef sDate;
  RTC_AlarmTypeDef sAlarm;

  // Initialize RTC and set the Time and Date
  rtcStruct.RtcHandle.Instance = RTC;
  rtcStruct.RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24;
  rtcStruct.RtcHandle.Init.AsynchPrediv = 127;
  rtcStruct.RtcHandle.Init.SynchPrediv = 255;
  rtcStruct.RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
  rtcStruct.RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  rtcStruct.RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  if (HAL_RTC_Init(&(rtcStruct.RtcHandle)) != HAL_OK)
  {
    return e_retVal_Fail;
  }

  sTime.Hours = 0x0;
  sTime.Minutes = 0x0;
  sTime.Seconds = 0x0;
  sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  if (HAL_RTC_SetTime(&(rtcStruct.RtcHandle), &sTime, RTC_FORMAT_BCD) != HAL_OK)
  {
    return e_retVal_Fail;
  }

  sDate.WeekDay = RTC_WEEKDAY_MONDAY;
  sDate.Month = RTC_MONTH_JANUARY;
  sDate.Date = 0x1;
  sDate.Year = 0x0;

  if (HAL_RTC_SetDate(&(rtcStruct.RtcHandle), &sDate, RTC_FORMAT_BCD) != HAL_OK)
  {
    return e_retVal_Fail;
  }

  // Enable the Alarm A
  sAlarm.AlarmTime.Hours = 0x23;
  sAlarm.AlarmTime.Minutes = 0x0;
  sAlarm.AlarmTime.Seconds = 0x0;
  sAlarm.AlarmTime.SubSeconds = 0x0;
  sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
  sAlarm.AlarmMask = RTC_ALARMMASK_NONE;
  sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
  sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
  sAlarm.AlarmDateWeekDay = 0x1;
  sAlarm.Alarm = RTC_ALARM_A;
  // FIXME do not set alarm, yet anyway..
  if (HAL_RTC_SetAlarm_IT(&(rtcStruct.RtcHandle), &sAlarm, RTC_FORMAT_BCD) != HAL_OK)
  {
    return e_retVal_Fail;
  }

  return e_retVal_Success;
}

void RTC_Alarm_IRQHandler(void)
{
  s_rtc_struct * rtcStruct = Rtc_GetStructPtr();

  HAL_RTC_AlarmIRQHandler(&(rtcStruct->RtcHandle));
}

Outcomes