h7的HAL 1.8.0版本库的RTC时钟没起振 死循环 无法退出

h7的HAL 1.8.0版本库  和1.7.0的版本都存在RTC 退出去的情况 当外部晶振不起振会出现在RTC函数出不来的情况

H7的RTC有bug的地方 
没改动之前的代码
这个只有H7的1.8.0的版本会有问题  H7的1.7.0的版本没有问题  
当晶振不起振的时候 就会死这里不动

这段代码是 H7的1.8.0和1.7.0的版本  都是有问题  会出现意外退不出去的情况

HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef *hrtc)
{
  uint32_t tickstart;
  HAL_StatusTypeDef status = HAL_OK;
  /* Check if the Initialization mode is set */
#if defined(RTC_ICSR_INITF)
  if((hrtc->Instance->ICSR & RTC_ICSR_INITF) == 0U)
  {
    /* Set the Initialization mode */
    SET_BIT(hrtc->Instance->ICSR, RTC_ICSR_INIT);

    tickstart = HAL_GetTick();
    /* Wait till RTC is in INIT state and if Time out is reached exit */
    while ((hrtc->Instance->ICSR & RTC_ICSR_INITF) == 0U)
    {
      if((HAL_GetTick()  - tickstart) > RTC_TIMEOUT_VALUE)
      {
        status = HAL_TIMEOUT;
        hrtc->State = HAL_RTC_STATE_TIMEOUT;
      }
    }
  }
#endif /* RTC_ICSR_INITF */
#if defined(RTC_ISR_INITF)
  if((hrtc->Instance->ISR & RTC_ISR_INITF) == 0U)
  {
    /* Set the Initialization mode */
    hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;

    tickstart = HAL_GetTick();
    /* Wait till RTC is in INIT state and if Time out is reached exit */
    while ((hrtc->Instance->ISR & RTC_ISR_INITF) == 0U)
    {
      if((HAL_GetTick()  - tickstart) > RTC_TIMEOUT_VALUE)
      {
        status = HAL_TIMEOUT;
        hrtc->State = HAL_RTC_STATE_TIMEOUT;
      }
    }
  }
#endif /* RTC_ISR_INITF */

  return status;
}


以下这段代码是 H7的1.8.0和1.7.0的版本  都是有问题  会出现意外退不出去的情况

HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
{
  HAL_StatusTypeDef status = HAL_ERROR;
  uint32_t          tickstart;

  /* Check RTC handler */
  if(hrtc != NULL)
  {
    /* Check the parameters */
    assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));

    /* Set RTC state */
    hrtc->State = HAL_RTC_STATE_BUSY;

    /* Disable the write protection for RTC registers */
    __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
    /* Enter Initialization mode */
    status = RTC_EnterInitMode(hrtc);
    if (status == HAL_OK)
    {
      /* Reset TR, DR and CR registers */
      hrtc->Instance->TR = 0x00000000U;
      hrtc->Instance->DR = ((uint32_t)(RTC_DR_WDU_0 | RTC_DR_MU_0 | RTC_DR_DU_0));
      /* Reset All CR bits except CR[2:0] */
      hrtc->Instance->CR &= RTC_CR_WUCKSEL;

      tickstart = HAL_GetTick();

      /* Wait till WUTWF flag is set and if Time out is reached exit */
#if defined(RTC_ICSR_WUTWF)
      while (((hrtc->Instance->ICSR) & RTC_ICSR_WUTWF) == 0U)
#endif /* RTC_ICSR_WUTWF */
#if defined(RTC_ISR_WUTWF)
        while (((hrtc->Instance->ISR)  & RTC_ISR_WUTWF)  == 0U)
#endif /* RTC_ISR_WUTWF */
        {
          if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
          {
            /* Enable the write protection for RTC registers */
            __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);

            /* Set RTC state */
            hrtc->State = HAL_RTC_STATE_TIMEOUT;

            status = HAL_TIMEOUT;
          }
        }

      /* Exit initialization mode */
      status = RTC_ExitInitMode(hrtc);
      if (status == HAL_OK)
      {
        /* Reset all RTC CR register bits */
        hrtc->Instance->CR &= 0x00000000U;

        /* Reset other RTC registers */
        hrtc->Instance->WUTR = RTC_WUTR_WUT;
        hrtc->Instance->PRER = ((uint32_t)(RTC_PRER_PREDIV_A | 0x000000FFU));
        hrtc->Instance->ALRMAR = 0x00000000U;
        hrtc->Instance->ALRMBR = 0x00000000U;
        hrtc->Instance->SHIFTR = 0x00000000U;
        hrtc->Instance->CALR = 0x00000000U;
        hrtc->Instance->ALRMASSR = 0x00000000U;
        hrtc->Instance->ALRMBSSR = 0x00000000U;

#if defined(RTC_ISR_INIT)
        /* Reset Tamper configuration register */
        hrtc->Instance->TAMPCR = 0x00000000U;

        /* Reset Option register */
        hrtc->Instance->OR = 0x00000000U;
#endif /* RTC_ISR_INIT */
      }
    }

    if(status == HAL_OK)
    {
#if defined(TAMP_CR1_TAMP1E)
      /* Reset TAMP registers */
      ((TAMP_TypeDef *)((uint32_t)hrtc->Instance + TAMP_OFFSET))->CR1 = 0xFFFF0000U;
      ((TAMP_TypeDef *)((uint32_t)hrtc->Instance + TAMP_OFFSET))->CR2 = 0x00000000U;
#endif /* TAMP_CR1_TAMP1E */

      /* Enable the write protection for RTC registers */
      __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);

#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
      if(hrtc->MspDeInitCallback == NULL)
      {
        hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
      }

      /* DeInit the low level hardware: CLOCK, NVIC.*/
      hrtc->MspDeInitCallback(hrtc);
#else
      /* De-Initialize RTC MSP */
      HAL_RTC_MspDeInit(hrtc);
#endif /* (USE_HAL_RTC_REGISTER_CALLBACKS) */

      hrtc->State = HAL_RTC_STATE_RESET;

      /* Release Lock */
      __HAL_UNLOCK(hrtc);
    }
  }

  /* return status */
  return status;
}

 



修复之后的代码  这个H7的1.8.0的版本库改动代码参考  H7的1.7.0的版本 自行修改

HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef *hrtc)
{
  uint32_t tickstart;
  HAL_StatusTypeDef status = HAL_OK;
  /* Check if the Initialization mode is set */
#if defined(RTC_ICSR_INITF)
  if((hrtc->Instance->ICSR & RTC_ICSR_INITF) == 0U)
  {
    /* Set the Initialization mode */
    SET_BIT(hrtc->Instance->ICSR, RTC_ICSR_INIT);

    tickstart = HAL_GetTick();
    /* Wait till RTC is in INIT state and if Time out is reached exit */
    while ((hrtc->Instance->ICSR & RTC_ICSR_INITF) == 0U)
    {
      if((HAL_GetTick()  - tickstart) > RTC_TIMEOUT_VALUE)//固件库这个位置 有bug 退不出来
      {
        status = HAL_TIMEOUT;
        hrtc->State = HAL_RTC_STATE_TIMEOUT;
        break;
      }
    }
  }
#endif /* RTC_ICSR_INITF */
#if defined(RTC_ISR_INITF)
  if((hrtc->Instance->ISR & RTC_ISR_INITF) == 0U)
  {
    /* Set the Initialization mode */
    hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;

    tickstart = HAL_GetTick();
    /* Wait till RTC is in INIT state and if Time out is reached exit */
    while ((hrtc->Instance->ISR & RTC_ISR_INITF) == 0U)//固件库这个位置 有bug 退不出来
    {
      if((HAL_GetTick()  - tickstart) > RTC_TIMEOUT_VALUE)
      {
        status = HAL_TIMEOUT;
        hrtc->State = HAL_RTC_STATE_TIMEOUT;
        break;
      }
    }
  }
#endif /* RTC_ISR_INITF */

  return status;
}
HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
{
  HAL_StatusTypeDef status = HAL_ERROR;
  uint32_t          tickstart;

  /* Check RTC handler */
  if(hrtc != NULL)
  {
    /* Check the parameters */
    assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));

    /* Set RTC state */
    hrtc->State = HAL_RTC_STATE_BUSY;

    /* Disable the write protection for RTC registers */
    __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
    /* Enter Initialization mode */
    status = RTC_EnterInitMode(hrtc);
    if (status == HAL_OK)
    {
      /* Reset TR, DR and CR registers */
      hrtc->Instance->TR = 0x00000000U;
      hrtc->Instance->DR = ((uint32_t)(RTC_DR_WDU_0 | RTC_DR_MU_0 | RTC_DR_DU_0));
      /* Reset All CR bits except CR[2:0] */
      hrtc->Instance->CR &= RTC_CR_WUCKSEL;

      tickstart = HAL_GetTick();

      /* Wait till WUTWF flag is set and if Time out is reached exit */
#if defined(RTC_ICSR_WUTWF)
      while (((hrtc->Instance->ICSR) & RTC_ICSR_WUTWF) == 0U)
#endif /* RTC_ICSR_WUTWF */
#if defined(RTC_ISR_WUTWF)
        while (((hrtc->Instance->ISR)  & RTC_ISR_WUTWF)  == 0U)
#endif /* RTC_ISR_WUTWF */
        {
          if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)//固件库这个位置 有bug 退不出来
          {
            /* Enable the write protection for RTC registers */
            __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);

            /* Set RTC state */
            hrtc->State = HAL_RTC_STATE_TIMEOUT;

            status = HAL_TIMEOUT;
                        //
                        break;
          }
        }

      /* Exit initialization mode */
      status = RTC_ExitInitMode(hrtc);
      if (status == HAL_OK)
      {
        /* Reset all RTC CR register bits */
        hrtc->Instance->CR &= 0x00000000U;

        /* Reset other RTC registers */
        hrtc->Instance->WUTR = RTC_WUTR_WUT;
        hrtc->Instance->PRER = ((uint32_t)(RTC_PRER_PREDIV_A | 0x000000FFU));
        hrtc->Instance->ALRMAR = 0x00000000U;
        hrtc->Instance->ALRMBR = 0x00000000U;
        hrtc->Instance->SHIFTR = 0x00000000U;
        hrtc->Instance->CALR = 0x00000000U;
        hrtc->Instance->ALRMASSR = 0x00000000U;
        hrtc->Instance->ALRMBSSR = 0x00000000U;

#if defined(RTC_ISR_INIT)
        /* Reset Tamper configuration register */
        hrtc->Instance->TAMPCR = 0x00000000U;

        /* Reset Option register */
        hrtc->Instance->OR = 0x00000000U;
#endif /* RTC_ISR_INIT */
      }
    }

    if(status == HAL_OK)
    {
#if defined(TAMP_CR1_TAMP1E)
      /* Reset TAMP registers */
      ((TAMP_TypeDef *)((uint32_t)hrtc->Instance + TAMP_OFFSET))->CR1 = 0xFFFF0000U;
      ((TAMP_TypeDef *)((uint32_t)hrtc->Instance + TAMP_OFFSET))->CR2 = 0x00000000U;
#endif /* TAMP_CR1_TAMP1E */

      /* Enable the write protection for RTC registers */
      __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);

#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
      if(hrtc->MspDeInitCallback == NULL)
      {
        hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
      }

      /* DeInit the low level hardware: CLOCK, NVIC.*/
      hrtc->MspDeInitCallback(hrtc);
#else
      /* De-Initialize RTC MSP */
      HAL_RTC_MspDeInit(hrtc);
#endif /* (USE_HAL_RTC_REGISTER_CALLBACKS) */

      hrtc->State = HAL_RTC_STATE_RESET;

      /* Release Lock */
      __HAL_UNLOCK(hrtc);
    }
  }

  /* return status */
  return status;
}

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值