[STM32L0] STM32L053C8-Discovery开发板LPTIM例程有问题

[复制链接]
 楼主| nuc990 发表于 2017-6-3 19:38 | 显示全部楼层 |阅读模式
本帖最后由 nuc990 于 2017-6-3 19:39 编辑

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

  1. /**
  2.   ******************************************************************************
  3.   * [url=home.php?mod=space&uid=288409]@file[/url]    LPTIM/LPTIM_Timeout/Src/main.c
  4.   * [url=home.php?mod=space&uid=187600]@author[/url]  MCD Application Team
  5.   * [url=home.php?mod=space&uid=895143]@version[/url] V1.8.0
  6.   * [url=home.php?mod=space&uid=212281]@date[/url]    25-November-2016
  7.   * [url=home.php?mod=space&uid=247401]@brief[/url]   This example describes how to implement a low power timeout to
  8.   *          wake-up the system using the LPTIMER, through the STM32L0xx HAL API.
  9.   ******************************************************************************
  10.   * @attention
  11.   *
  12.   * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  13.   *
  14.   * Redistribution and use in source and binary forms, with or without modification,
  15.   * are permitted provided that the following conditions are met:
  16.   *   1. Redistributions of source code must retain the above copyright notice,
  17.   *      this list of conditions and the following disclaimer.
  18.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  19.   *      this list of conditions and the following disclaimer in the documentation
  20.   *      and/or other materials provided with the distribution.
  21.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  22.   *      may be used to endorse or promote products derived from this software
  23.   *      without specific prior written permission.
  24.   *
  25.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35.   *
  36.   ******************************************************************************
  37.   */

  38. /* Includes ------------------------------------------------------------------*/
  39. #include "main.h"

  40. /** @addtogroup STM32L0xx_HAL_Examples
  41.   * @{
  42.   */

  43. /** @addtogroup LPTIM_Timeout
  44.   * @{
  45.   */

  46. /* Private typedef -----------------------------------------------------------*/
  47. /* Private define ------------------------------------------------------------*/
  48. /* Set the Maximum value of the counter (Auto-Reload) that defines the Period */
  49. #define Period               (uint32_t) 65535

  50. /* Set the Timeout value */
  51. #define Timeout              (uint32_t) (32768 - 1)

  52. /* Private macro -------------------------------------------------------------*/
  53. /* Private variables ---------------------------------------------------------*/
  54. /* LPTIM handle declaration */
  55. LPTIM_HandleTypeDef             LptimHandle;

  56. /* Clocks structure declaration */
  57. RCC_PeriphCLKInitTypeDef        RCC_PeriphCLKInitStruct;

  58. /* Private function prototypes -----------------------------------------------*/
  59. static void SystemClock_Config(void);
  60. static void LSE_ClockEnable(void);
  61. static void Error_Handler(void);

  62. /* Private functions ---------------------------------------------------------*/

  63. static void SystemClockConfig_STOP(void)
  64. {
  65.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  66.   RCC_OscInitTypeDef RCC_OscInitStruct;

  67.   /* Enable Power Control clock */
  68.   __HAL_RCC_PWR_CLK_ENABLE();

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

  73.   /* Get the Oscillators configuration according to the internal RCC registers */
  74.   HAL_RCC_GetOscConfig(&RCC_OscInitStruct);

  75.   /* After wake-up from STOP reconfigure the system clock: Enable HSI and PLL */
  76.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  77.   RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
  78.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  79.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  80.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  81.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL4;
  82.   RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV2;
  83.   RCC_OscInitStruct.HSICalibrationValue = 0x10;
  84.   if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  85.   {
  86.     Error_Handler();
  87.   }

  88.   /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  89.      clocks dividers */
  90.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
  91.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  92.   if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  93.   {
  94.     Error_Handler();
  95.   }
  96. }
  97. /**
  98.   * [url=home.php?mod=space&uid=247401]@brief[/url]  Main program
  99.   * @param  None
  100.   * @retval None
  101.   */
  102. int main(void)
  103. {
  104. /* This sample code shows how to use STM32L0xx LPTIM HAL API to realize a
  105.     timeout function to wakeup the system from Stop mode.
  106.     To proceed, 4 steps are required:
  107. */

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

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

  120.   /* Enable the LSE source */
  121.   LSE_ClockEnable();

  122.   /* Initialize LED2 */

  123.     BSP_LED_Init(LED2);  

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

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

  139.   LptimHandle.Instance = LPTIM1;

  140.   LptimHandle.Init.Clock.Source       = LPTIM_CLOCKSOURCE_APBCLOCK_LPOSC;
  141.   LptimHandle.Init.Clock.Prescaler    = LPTIM_PRESCALER_DIV1;  
  142.   LptimHandle.Init.Trigger.Source     = LPTIM_TRIGSOURCE_0;
  143.   LptimHandle.Init.Trigger.ActiveEdge = LPTIM_ACTIVEEDGE_RISING;
  144.   LptimHandle.Init.CounterSource      = LPTIM_COUNTERSOURCE_INTERNAL;

  145.   /* Initialize LPTIM peripheral according to the passed parameters */
  146.   if (HAL_LPTIM_Init(&LptimHandle) != HAL_OK)
  147.   {
  148.     Error_Handler();
  149.   }

  150.   /* ### - 3 - Start the Timeout function in interrupt mode ################# */
  151.   /*
  152.    *  Period = 65535
  153.    *  Pulse  = 32767
  154.    *  According to this configuration (LPTIMER clocked by LSE & compare = 32767,
  155.    *  the Timeout period = (compare + 1)/LSE_Frequency = 1s
  156.    */
  157.   if (HAL_LPTIM_TimeOut_Start_IT(&LptimHandle, Period, Timeout) != HAL_OK)
  158.   {
  159.     Error_Handler();
  160.   }

  161.         
  162.   /* ### - 4 - Enter in Stop mode ########################################### */
  163.   HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);  
  164.   /* Infinite Loop */
  165. //  while (1)
  166. //  {
  167. //               
  168. //  }
  169. }

  170. /**
  171.   * @brief  Compare match callback in non blocking mode
  172.   * @param  hlptim : LPTIM handle
  173.   * @retval None
  174.   */
  175. void HAL_LPTIM_CompareMatchCallback(LPTIM_HandleTypeDef *hlptim)
  176. {
  177.   /* Timeout was reached, turn on LED2 */
  178.            


  179.                    BSP_LED_On(LED2);
  180.   
  181. }


  182. /**
  183.   * @brief  System Clock Configuration
  184.   *         The system Clock is configured as follow :
  185.   *            System Clock source            = MSI
  186.   *            SYSCLK(Hz)                     = 2000000
  187.   *            HCLK(Hz)                       = 2000000
  188.   *            AHB Prescaler                  = 1
  189.   *            APB1 Prescaler                 = 1
  190.   *            APB2 Prescaler                 = 1
  191.   *            Flash Latency(WS)              = 0
  192.   *            Main regulator output voltage  = Scale3 mode
  193.   * @param  None
  194.   * @retval None
  195.   */
  196. static void SystemClock_Config(void)
  197. {
  198.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  199.   RCC_OscInitTypeDef RCC_OscInitStruct;

  200.   /* Enable Power Control clock */
  201.   __HAL_RCC_PWR_CLK_ENABLE();

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

  206.   /* Enable MSI Oscillator */
  207.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  208.   RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  209.   RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5;
  210.   RCC_OscInitStruct.MSICalibrationValue=0x00;
  211.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  212.    if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  213.   {
  214.     /* Initialization Error */
  215.     Error_Handler();
  216.   }


  217.   /* Select MSI as system clock source and configure the HCLK, PCLK1 and PCLK2
  218.      clocks dividers */
  219.   RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  220.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
  221.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  222.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;  
  223.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;  
  224.   if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  225.   {
  226.     /* Initialization Error */
  227.     Error_Handler();
  228.   }
  229. }

  230. /**
  231.   * @brief  Enable External Low Speed Clock (LSE)
  232.   * @param  None
  233.   * @retval None
  234.   */
  235. static void LSE_ClockEnable(void)
  236. {
  237.   RCC_OscInitTypeDef RCC_OscInitStruct;

  238.   /* Enable LSE clock */
  239.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
  240.   RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  241.   HAL_RCC_OscConfig(&RCC_OscInitStruct);
  242. }

  243. /**
  244.   * @brief  This function is executed in case of error occurrence.
  245.   * @param  None
  246.   * @retval None
  247.   */
  248. static void Error_Handler(void)
  249. {
  250.   /* Infinite loop */
  251.   while(1)
  252.   {
  253.   }
  254. }
  255. #ifdef  USE_FULL_ASSERT

  256. /**
  257.   * @brief  Reports the name of the source file and the source line number
  258.   *         where the assert_param error has occurred.
  259.   * @param  file: pointer to the source file name
  260.   * @param  line: assert_param error line source number
  261.   * @retval None
  262.   */
  263. void assert_failed(uint8_t* file, uint32_t line)
  264. {
  265.   /* User can add his own implementation to report the file name and line number,
  266.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  267.   /* Infinite loop */
  268.   while (1)
  269.   {
  270.   }
  271. }
  272. #endif

  273. /**
  274.   * @}
  275.   */

  276. /**
  277.   * @}
  278.   */

  279. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/


651471519 发表于 2017-8-3 20:55 | 显示全部楼层
LPTIM我试过 没有问题的啊  也能唤醒MCU
蒙牛灬伊利 发表于 2019-5-29 17:09 | 显示全部楼层
我也发现了,请问楼主解决了吗
香水城 发表于 2019-6-3 14:33 | 显示全部楼层
你确定下到底是 DISCOVERY绿色开发板 还是 Nucleo 白色开发板?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

114

主题

225

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部

114

主题

225

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部