STM32
直播中

尤立虔

9年用户 1068经验值
擅长:控制/MCU
私信 关注
[问答]

STM32L053C8-Discovery开发板LPTIM例程,下载进开发板,程序不能正常进行的原因?

开发板目录:STM32Cube_FW_L0_V1.8.0ProjectsSTM32L053C8-DiscoveryExamplesLPtiM路径的这个例程:没有做任何修改,但是下载进开发板,程序不能正常进行, LPTIM唤醒定时器没有进入中断,本来进入中断LED灯会亮起来,然后唤醒STOP模式。但是开发板灯没有亮,中断没进去。难道开发板的代码也这么补严谨?


  • /**
  •   ******************************************************************************
  •   * @file    LPTIM/LPTIM_Timeout/Src/main.c
  •   * @author  MCD Application Team
  •   * @version V1.8.0
  •   * @date    25-November-2016
  •   * @brief   This example describes how to implement a low power timeout to
  •   *          wake-up the system using the LPTIMER, through the STM32L0xx HAL API.
  •   ******************************************************************************
  •   * @attention
  •   *
  •   *

    © COPYRIGHT(c) 2016 STMicroelectronics

  •   *
  •   * Redistribution and use in source and binary forms, with or without modification,
  •   * are permitted provided that the following conditions are met:
  •   *   1. Redistributions of source code must retain the above copyright notice,
  •   *      this list of conditions and the following disclaimer.
  •   *   2. Redistributions in binary form must reproduce the above copyright notice,
  •   *      this list of conditions and the following disclaimer in the documentation
  •   *      and/or other materials provided with the distribution.
  •   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  •   *      may be used to endorse or promote products derived from this software
  •   *      without specific prior written permission.
  •   *
  •   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  •   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  •   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  •   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  •   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  •   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  •   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  •   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  •   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  •   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  •   *
  •   ******************************************************************************
  •   */

  • /* Includes ------------------------------------------------------------------*/
  • #include "main.h"

  • /** @addtogroup STM32L0xx_HAL_Examples
  •   * @{
  •   */

  • /** @addtogroup LPTIM_Timeout
  •   * @{
  •   */

  • /* Private typedef -----------------------------------------------------------*/
  • /* Private define ------------------------------------------------------------*/
  • /* Set the Maximum value of the counter (Auto-Reload) that defines the Period */
  • #define Period               (uint32_t) 65535

  • /* Set the Timeout value */
  • #define Timeout              (uint32_t) (32768 - 1)

  • /* Private macro -------------------------------------------------------------*/
  • /* Private variables ---------------------------------------------------------*/
  • /* LPTIM handle declaration */
  • LPTIM_HandleTypeDef             LptimHandle;

  • /* Clocks structure declaration */
  • RCC_PeriphCLKInitTypeDef        RCC_PeriphCLKInitStruct;

  • /* Private function prototypes -----------------------------------------------*/
  • static void SystemClock_Config(void);
  • static void LSE_ClockEnable(void);
  • static void Error_Handler(void);

  • /* Private functions ---------------------------------------------------------*/

  • static void SystemClockConfig_STOP(void)
  • {
  •   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  •   RCC_OscInitTypeDef RCC_OscInitStruct;

  •   /* Enable Power Control clock */
  •   __HAL_RCC_PWR_CLK_ENABLE();

  •   /* The voltage scaling allows optimizing the power consumption when the device is
  •      clocked below the maximum system frequency, to update the voltage scaling value
  •      regarding system frequency refer to product datasheet.  */
  •   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  •   /* Get the Oscillators configuration according to the internal RCC registers */
  •   HAL_RCC_GetOscConfig( RCC_OscInitStruct);

  •   /* After wake-up from STOP reconfigure the system clock: Enable HSI and PLL */
  •   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  •   RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
  •   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  •   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  •   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  •   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL4;
  •   RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV2;
  •   RCC_OscInitStruct.HSICalibrationValue = 0x10;
  •   if(HAL_RCC_OscConfig( RCC_OscInitStruct) != HAL_OK)
  •   {
  •     Error_Handler();
  •   }

  •   /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  •      clocks dividers */
  •   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
  •   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  •   if(HAL_RCC_ClockConfig( RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  •   {
  •     Error_Handler();
  •   }
  • }
  • /**
  •   * @brief  Main program
  •   * @param  None
  •   * @retval None
  •   */
  • int main(void)
  • {
  • /* This sample code shows how to use STM32L0xx LPTIM HAL API to realize a
  •     timeout function to wakeup the system from Stop mode.
  •     To proceed, 4 steps are required:
  • */

  •   /* STM32L0xx HAL library initialization:
  •        - Configure the Flash prefetch, Flash preread and Buffer caches
  •        - Systick timer is configured by default as source of time base, but user
  •              can eventually implement his proper time base source (a general purpose
  •              timer for example or other time source), keeping in mind that Time base
  •              duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
  •              handled in milliseconds basis.
  •        - Low Level Initialization
  •      */
  •   HAL_Init();

  •   /* Configure the System clock to have a frequency of 2 MHz (Up to 32MHZ possible) */
  •   SystemClock_Config();

  •   /* Enable the LSE source */
  •   LSE_ClockEnable();

  •   /* Initialize LED2 */

  •     BSP_LED_Init(LED2);

  •   /* ### - 1 - Re-target the LSE to Clock the LPTIM Counter ################# */
  •   /* Select the LSE clock as LPTIM peripheral clock */
  •   RCC_PeriphCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPTIM1;
  •   RCC_PeriphCLKInitStruct.LptimClockSelection = RCC_LPTIM1CLKSOURCE_LSI;
  •   HAL_RCCEx_PeriphCLKConfig( RCC_PeriphCLKInitStruct);

  •   /* ### - 2 - Initialize LPTIM peripheral ################################## */
  •   /*
  •    *  Instance        = LPTIM1.
  •    *  Clock Source    = APB or LowPowerOSCillator (in this example LSE is
  •    *                    already selected from the RCC level).
  •    *  Counter source  = Internal event.
  •    *  Clock prescaler = 1 (No division).
  •    *  Counter Trigger = Trigger1: PC3 or PB6 (PC3 in this example).
  •    *  Active Edge     = Rising edge.
  •    */

  •   LptimHandle.Instance = LPTIM1;

  •   LptimHandle.Init.Clock.Source       = LPTIM_CLOCKSOURCE_APBCLOCK_LPOSC;
  •   LptimHandle.Init.Clock.Prescaler    = LPTIM_PRESCALER_DIV1;
  •   LptimHandle.Init.Trigger.Source     = LPTIM_TRIGSOURCE_0;
  •   LptimHandle.Init.Trigger.ActiveEdge = LPTIM_ACTIVEEDGE_RISING;
  •   LptimHandle.Init.CounterSource      = LPTIM_COUNTERSOURCE_INTERNAL;

  •   /* Initialize LPTIM peripheral according to the passed parameters */
  •   if (HAL_LPTIM_Init( LptimHandle) != HAL_OK)
  •   {
  •     Error_Handler();
  •   }

  •   /* ### - 3 - Start the Timeout function in interrupt mode ################# */
  •   /*
  •    *  Period = 65535
  •    *  Pulse  = 32767
  •    *  According to this configuration (LPTIMER clocked by LSE   compare = 32767,
  •    *  the Timeout period = (compare + 1)/LSE_Frequency = 1s
  •    */
  •   if (HAL_LPTIM_TimeOut_Start_IT( LptimHandle, Period, Timeout) != HAL_OK)
  •   {
  •     Error_Handler();
  •   }

  •   /* ### - 4 - Enter in Stop mode ########################################### */
  •   HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);

  •   /* Infinite Loop */
  • //  while (1)
  • //  {
  • //
  • //  }
  • }

  • /**
  •   * @brief  Compare match callback in non blocking mode
  •   * @param  hlptim : LPTIM handle
  •   * @retval None
  •   */
  • void HAL_LPTIM_CompareMatchCallback(LPTIM_HandleTypeDef *hlptim)
  • {
  •   /* Timeout was reached, turn on LED2 */


  •                    BSP_LED_On(LED2);

  • }


  • /**
  •   * @brief  System Clock Configuration
  •   *         The system Clock is configured as follow :
  •   *            System Clock source            = MSI
  •   *            SYSCLK(Hz)                     = 2000000
  •   *            HCLK(Hz)                       = 2000000
  •   *            AHB Prescaler                  = 1
  •   *            APB1 Prescaler                 = 1
  •   *            APB2 Prescaler                 = 1
  •   *            Flash Latency(WS)              = 0
  •   *            Main regulator output voltage  = Scale3 mode
  •   * @param  None
  •   * @retval None
  •   */
  • static void SystemClock_Config(void)
  • {
  •   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  •   RCC_OscInitTypeDef RCC_OscInitStruct;

  •   /* Enable Power Control clock */
  •   __HAL_RCC_PWR_CLK_ENABLE();

  •   /* The voltage scaling allows optimizing the power consumption when the device is
  •      clocked below the maximum system frequency, to update the voltage scaling value
  •      regarding system frequency refer to product datasheet.  */
  •   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);

  •   /* Enable MSI Oscillator */
  •   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  •   RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  •   RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5;
  •   RCC_OscInitStruct.MSICalibrationValue=0x00;
  •   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  •    if(HAL_RCC_OscConfig( RCC_OscInitStruct) != HAL_OK)
  •   {
  •     /* Initialization Error */
  •     Error_Handler();
  •   }


  •   /* Select MSI as system clock source and configure the HCLK, PCLK1 and PCLK2
  •      clocks dividers */
  •   RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  •   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
  •   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  •   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  •   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  •   if(HAL_RCC_ClockConfig( RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  •   {
  •     /* Initialization Error */
  •     Error_Handler();
  •   }
  • }

  • /**
  •   * @brief  Enable External Low Speed Clock (LSE)
  •   * @param  None
  •   * @retval None
  •   */
  • static void LSE_ClockEnable(void)
  • {
  •   RCC_OscInitTypeDef RCC_OscInitStruct;

  •   /* Enable LSE clock */
  •   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
  •   RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  •   HAL_RCC_OscConfig( RCC_OscInitStruct);
  • }

  • /**
  •   * @brief  This function is executed in case of error occurrence.
  •   * @param  None
  •   * @retval None
  •   */
  • static void Error_Handler(void)
  • {
  •   /* Infinite loop */
  •   while(1)
  •   {
  •   }
  • }
  • #ifdef  USE_FULL_ASSERT

  • /**
  •   * @brief  Reports the name of the source file and the source line number
  •   *         where the assert_param error has occurred.
  •   * @param  file: pointer to the source file name
  •   * @param  line: assert_param error line source number
  •   * @retval None
  •   */
  • void assert_failed(uint8_t* file, uint32_t line)
  • {
  •   /* User can add his own implementation to report the file name and line number,
  •      ex: printf("Wrong parameters value: file %s on line %drn", file, line) */

  •   /* Infinite loop */
  •   while (1)
  •   {
  •   }
  • }
  • #endif

  • /**
  •   * @}
  •   */

  • /**
  •   * @}
  •   */

  • /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/


回帖(1)

敷衍作笑谈

2024-5-7 18:18:06
STM32L053C8-Discovery开发板的LPTIM例程无法正常运行可能有以下几个原因:

1. **固件库版本不匹配**:确保你使用的STM32Cube固件库版本与开发板兼容。你提到使用的是STM32Cube_FW_L0_V1.8.0,确保这个版本适用于你的开发板。

2. **时钟配置问题**:检查LPTIM的时钟配置是否正确。确保LPTIM的时钟源和时钟频率设置正确,以便LPTIM能够正常工作。

3. **中断配置问题**:检查中断配置是否正确。确保LPTIM中断已启用,并且中断优先级设置正确。

4. **GPIO配置问题**:检查LED灯的GPIO配置是否正确。确保GPIO引脚已正确配置为输出模式,并且与LED灯连接正确。

5. **LPTIM初始化问题**:检查LPTIM的初始化设置是否正确。确保LPTIM的计数器模式、触发源、自动重载寄存器等设置正确。

6. **唤醒源配置问题**:检查唤醒源配置是否正确。确保LPTIM被配置为唤醒源,并且在进入STOP模式时能够唤醒MCU。

7. **硬件问题**:如果以上步骤都检查过了,但问题仍然存在,可能是开发板本身存在硬件问题。尝试使用其他例程或项目验证开发板是否正常工作。

8. **固件库代码问题**:虽然固件库通常经过严格测试,但仍然可能存在一些问题。你可以尝试查找固件库的更新版本,或者在STM32社区寻求帮助。

为了解决这个问题,你可以按照以下步骤进行排查:

1. 检查固件库版本是否与开发板兼容。
2. 检查LPTIM的时钟配置。
3. 检查中断配置。
4. 检查GPIO配置。
5. 检查LPTIM初始化设置。
6. 检查唤醒源配置。
7. 验证开发板硬件是否正常工作。
8. 查找固件库的更新版本或寻求社区帮助。

希望这些建议能帮助你解决问题。
举报

更多回帖

发帖
×
20
完善资料,
赚取积分