STM32L031 stop模式恢复后串口无法使用

2019-12-16 22:37发布

本帖最后由 zhongjp85 于 2018-10-26 10:20 编辑

STM32L031退出STOP模式后 串口无法使用  (不进入stop模式串口正常)

哪位能帮忙看下吗? 谢谢!

主要代码如下 init()是初始化IO口的:

  1. void test1(void)
  2. {
  3.   /* STM32L0xx HAL library initialization:
  4.        - Configure the Flash prefetch, Flash preread and Buffer caches
  5.        - Systick timer is configured by default as source of time base, but user
  6.              can eventually implement his proper time base source (a general purpose
  7.              timer for example or other time source), keeping in mind that Time base
  8.              duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
  9.              handled in milliseconds basis.
  10.        - Low Level Initialization
  11.      */
  12.     HAL_Init();   
  13.    
  14.     /* Configure the system clock to 2 MHz */
  15. //    SystemClock_Config();
  16. //    init();
  17. //   
  18. //    printf("system is init ok! ");
  19. //        printf("wait 5s NB Reset! ");

  20.     /* Configure the RTC */
  21.     RTC_Config();   
  22.    
  23.     while(1)
  24.     {
  25.         SystemClock_Config();
  26.         init();
  27.         printf("------- wake up -------- ");
  28.         LedOn(1);
  29.         HAL_Delay(5000);
  30.         LedOff(1);
  31.         HAL_Delay(5000);
  32.         
  33.         printf(" ------- sleep -------- ");
  34.         rtc_wakeup_cfg();      
  35.         SystemPower_Config();
  36.         HW_WakeupPinInit();
  37.         enter_stop_mode();
  38.     }
  39.    
  40. }

  41. void tasks(void)
  42. {  
  43.     bool isGoSleep = false;
  44.    
  45.     while(!isGoSleep)
  46.     {   
  47.         LedTask();
  48.         SensorTask();
  49.         isGoSleep = NB_Task();
  50.     }
  51. }

  52. void rtc_wakeup_cfg(void)
  53. {
  54.     /* Disable Wakeup Counter */
  55.     HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);

  56.     HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, RTC_TIME, RTC_WAKEUPCLOCK_CK_SPRE_16BITS);
  57. }

  58. void enter_stop_mode(void)
  59. {
  60.     /* Enable Power Control clock */
  61.     __HAL_RCC_PWR_CLK_ENABLE();
  62.     /* Enter Stop Mode */
  63.     HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
  64. }

  65. void HW_WakeupPinInit(void)
  66. {   
  67.     GPIO_InitTypeDef GPIO_InitStruct;
  68.     /* Configure PB.1 as input with External interrupt */
  69.     GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7;
  70.     GPIO_InitStruct.Pull = GPIO_NOPULL;
  71.     GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  72.    
  73.     /* Enable GPIOA clock */
  74.     __HAL_RCC_GPIOA_CLK_ENABLE();

  75.     HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  76.    
  77.     GPIO_InitStruct.Pin = GPIO_PIN_13 | GPIO_PIN_14;
  78.     GPIO_InitStruct.Pull = GPIO_NOPULL;
  79.     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  80.    
  81.     HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);   
  82.    
  83.     /* Enable and set PA.12 (Arduino D2) EXTI Interrupt to the lowest priority */
  84.     NVIC_SetPriority((IRQn_Type)(EXTI4_15_IRQn), 0x03);
  85.     HAL_NVIC_EnableIRQ((IRQn_Type)(EXTI4_15_IRQn));
  86. }

  87. /** System Clock Configuration
  88. */
  89. void SystemClock_Config(void)
  90. {
  91.     RCC_OscInitTypeDef RCC_OscInitStruct;
  92.     RCC_ClkInitTypeDef RCC_ClkInitStruct;
  93.     RCC_PeriphCLKInitTypeDef PeriphClkInit;

  94.     /**Configure the main internal regulator output voltage
  95.     */
  96.     __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  97.     /**Initializes the CPU, AHB and APB busses clocks
  98.     */
  99.     RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  100.     RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  101.     RCC_OscInitStruct.HSICalibrationValue = 16;
  102.     RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  103.     RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  104.     RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_4;
  105.     RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
  106.     if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  107.     {
  108.         _Error_Handler(__FILE__, __LINE__);
  109.     }

  110.     /**Initializes the CPU, AHB and APB busses clocks
  111.     */
  112.     RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  113.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  114.     RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  115.     RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  116.     RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  117.     RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  118.     if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  119.     {
  120.         _Error_Handler(__FILE__, __LINE__);
  121.     }

  122.     PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2|RCC_PERIPHCLK_LPUART1;
  123.     PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
  124.     PeriphClkInit.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK1;
  125.     if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  126.     {
  127.         _Error_Handler(__FILE__, __LINE__);
  128.     }

  129.     /**Configure the Systick interrupt time
  130.     */
  131.     HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  132.     /**Configure the Systick
  133.     */
  134.     HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  135.     /* SysTick_IRQn interrupt configuration */
  136.     HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);

  137. }

  138. /**
  139.   * @brief  System Power Configuration
  140.   *         The system Power is configured as follow :
  141.   *            + Regulator in LP mode
  142.   *            + VREFINT OFF, with fast wakeup enabled
  143.   *            + MSI as SysClk after Wake Up
  144.   *            + No IWDG
  145.   *            + Wakeup using EXTI Line (User push-button PC.13)
  146.   * @param  None
  147.   * @retval None
  148.   */
  149. static void SystemPower_Config(void)
  150. {
  151.     GPIO_InitTypeDef GPIO_InitStructure;

  152.     /* Enable Power Control clock */
  153.     __HAL_RCC_PWR_CLK_ENABLE();

  154.     /* Enable Ultra low power mode */
  155.     HAL_PWREx_EnableUltraLowPower();

  156.     /* Enable the fast wake up from Ultra low power mode */
  157.     HAL_PWREx_EnableFastWakeUp();

  158.     /* Select MSI as system clock source after Wake Up from Stop mode */
  159.     __HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_STOP_WAKEUPCLOCK_MSI);

  160.     /* Enable GPIOs clock */
  161.     __HAL_RCC_GPIOA_CLK_ENABLE();
  162.     __HAL_RCC_GPIOB_CLK_ENABLE();
  163.     __HAL_RCC_GPIOC_CLK_ENABLE();
  164.     __HAL_RCC_GPIOH_CLK_ENABLE();

  165.     /* Configure all GPIO port pins in Analog Input mode (floating input trigger OFF) */
  166.     /* Note: Debug using ST-Link is not possible during the execution of this   */
  167.     /*       example because communication between ST-link and the device       */
  168.     /*       under test is done through UART. All GPIO pins are disabled (set   */
  169.     /*       to analog input mode) including  UART I/O pins.           */
  170.     GPIO_InitStructure.Pin = GPIO_PIN_All;
  171.     GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
  172.     GPIO_InitStructure.Pull = GPIO_NOPULL;

  173.     HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
  174.     HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
  175.     HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
  176.     HAL_GPIO_Init(GPIOH, &GPIO_InitStructure);

  177.     /* Disable GPIOs clock */
  178.     __HAL_RCC_GPIOA_CLK_DISABLE();
  179.     __HAL_RCC_GPIOB_CLK_DISABLE();
  180.     __HAL_RCC_GPIOC_CLK_DISABLE();
  181.     __HAL_RCC_GPIOH_CLK_DISABLE();
  182. }

  183. /**
  184.   * @brief  RTC Configuration
  185.   *         RTC Clocked by LSI (see HAL_RTC_MspInit)
  186.   * @param  None
  187.   * @retval None
  188.   */
  189. static void RTC_Config(void)
  190. {
  191.   /* Configure RTC */
  192.   RTCHandle.Instance = RTC;
  193.   /* Configure RTC prescaler and RTC data registers as follow:
  194.     - Hour Format = Format 24
  195.     - Asynch Prediv = Value according to source clock
  196.     - Synch Prediv = Value according to source clock
  197.     - OutPut = Output Disable
  198.     - OutPutPolarity = High Polarity
  199.     - OutPutType = Open Drain */
  200.   RTCHandle.Init.HourFormat = RTC_HOURFORMAT_24;
  201.   RTCHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
  202.   RTCHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
  203.   RTCHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
  204.   RTCHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  205.   RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  206.   if(HAL_RTC_Init(&RTCHandle) != HAL_OK)
  207.   {
  208.     /* Initialization Error */
  209.     Error_Handler();
  210.   }
  211. }


  212. void MX_GPIO_Init(void)
  213. {

  214.     GPIO_InitTypeDef GPIO_InitStruct;

  215.     /* GPIO Ports Clock Enable */
  216.     __HAL_RCC_GPIOA_CLK_ENABLE();
  217.     __HAL_RCC_GPIOB_CLK_ENABLE();
  218.     __HAL_RCC_GPIOC_CLK_ENABLE();
  219.     __HAL_RCC_GPIOH_CLK_ENABLE();

  220.     GPIOA->MODER = 0xFFFFFFFF;
  221.     GPIOB->MODER = 0xFFFFFFFF;
  222.     GPIOC->MODER = 0xFFFFFFFF;
  223.     GPIOH->MODER = 0xFFFFFFFF;

  224.     /*Configure GPIO pin Output Level */
  225.     HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);

  226.     /*Configure GPIO pin Output Level */
  227.     HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4|GPIO_PIN_5, GPIO_PIN_SET);

  228.     /*Configure GPIO pins : PA1 PA4 PA5 */
  229.     GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5;
  230.     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  231.     GPIO_InitStruct.Pull = GPIO_NOPULL;
  232.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  233.     HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  234.     /*Configure GPIO pin : PA7 PA6*/
  235.     GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_6;
  236.     GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  237.     GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  238.     HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  239.     __HAL_RCC_GPIOB_CLK_DISABLE();
  240.     __HAL_RCC_GPIOC_CLK_DISABLE();
  241.     __HAL_RCC_GPIOH_CLK_DISABLE();

  242.     HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0);
  243.     HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);

  244. }

  245. void MX_DMA_Init(void)
  246. {
  247.   /* DMA controller clock enable */
  248.   __HAL_RCC_DMA1_CLK_ENABLE();

  249.   /* DMA interrupt init */
  250.   /* DMA1_Channel2_3_IRQn interrupt configuration */
  251.   HAL_NVIC_SetPriority(DMA1_Channel2_3_IRQn, 0, 0);
  252.   HAL_NVIC_EnableIRQ(DMA1_Channel2_3_IRQn);

  253. }



  254. /* LPUART1 init function */
  255. void MX_LPUART1_UART_Init(void)
  256. {

  257.     hlpuart1.Instance = LPUART1;
  258.     hlpuart1.Init.BaudRate = 115200;
  259.     hlpuart1.Init.WordLength = UART_WORDLENGTH_8B;
  260.     hlpuart1.Init.StopBits = UART_STOPBITS_1;
  261.     hlpuart1.Init.Parity = UART_PARITY_NONE;
  262.     hlpuart1.Init.Mode = UART_MODE_TX_RX;
  263.     hlpuart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  264.     hlpuart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  265.     hlpuart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  266.     if (HAL_UART_Init(&hlpuart1) != HAL_OK)
  267.     {
  268.         _Error_Handler(__FILE__, __LINE__);
  269.     }

  270. }

  271. /* USART2 init function */
  272. void MX_USART2_UART_Init(void)
  273. {
  274. #if DEBUG
  275.     huart2.Instance = USART2;
  276.     huart2.Init.BaudRate = 115200;
  277.     huart2.Init.WordLength = UART_WORDLENGTH_8B;
  278.     huart2.Init.StopBits = UART_STOPBITS_1;
  279.     huart2.Init.Parity = UART_PARITY_NONE;
  280.     huart2.Init.Mode = UART_MODE_TX_RX;
  281.     huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  282.     huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  283.     huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  284.     huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  285.     if (HAL_UART_Init(&huart2) != HAL_OK)
  286.     {
  287.         _Error_Handler(__FILE__, __LINE__);
  288.     }
  289. #endif

  290. }
  291. void HAL_UART_MspInit(UART_HandleTypeDef* huart)
  292. {

  293.   GPIO_InitTypeDef GPIO_InitStruct;
  294.   if(huart->Instance==LPUART1)
  295.   {
  296.   /* USER CODE BEGIN LPUART1_MspInit 0 */

  297.   /* USER CODE END LPUART1_MspInit 0 */
  298.     /* Peripheral clock enable */
  299.     __HAL_RCC_LPUART1_CLK_ENABLE();
  300.   
  301.     /**LPUART1 GPIO Configuration   
  302.     PA2     ------> LPUART1_TX
  303.     PA3     ------> LPUART1_RX
  304.     */
  305.     GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;
  306.     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  307.     GPIO_InitStruct.Pull = GPIO_PULLUP;
  308.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  309.     GPIO_InitStruct.Alternate = GPIO_AF6_LPUART1;
  310.     HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  311.     /* LPUART1 DMA Init */
  312.     /* LPUART1_RX Init */
  313.     hdma_lpuart1_rx.Instance = DMA1_Channel3;
  314.     hdma_lpuart1_rx.Init.Request = DMA_REQUEST_5;
  315.     hdma_lpuart1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  316.     hdma_lpuart1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  317.     hdma_lpuart1_rx.Init.MemInc = DMA_MINC_ENABLE;
  318.     hdma_lpuart1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  319.     hdma_lpuart1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  320.     hdma_lpuart1_rx.Init.Mode = DMA_NORMAL;
  321.     hdma_lpuart1_rx.Init.Priority = DMA_PRIORITY_LOW;
  322.     if (HAL_DMA_Init(&hdma_lpuart1_rx) != HAL_OK)
  323.     {
  324.       _Error_Handler(__FILE__, __LINE__);
  325.     }

  326.     __HAL_LINKDMA(huart,hdmarx,hdma_lpuart1_rx);

  327.     /* LPUART1 interrupt Init */
  328.     HAL_NVIC_SetPriority(AES_RNG_LPUART1_IRQn, 0, 0);
  329.     HAL_NVIC_EnableIRQ(AES_RNG_LPUART1_IRQn);
  330.   /* USER CODE BEGIN LPUART1_MspInit 1 */

  331.   /* USER CODE END LPUART1_MspInit 1 */
  332.   }
  333.   else if(huart->Instance==USART2)
  334.   {
  335.   /* USER CODE BEGIN USART2_MspInit 0 */

  336.   /* USER CODE END USART2_MspInit 0 */
  337.     /* Peripheral clock enable */
  338.     __HAL_RCC_USART2_CLK_ENABLE();
  339.   
  340.     /**USART2 GPIO Configuration   
  341.     PA9     ------> USART2_TX
  342.     PA10     ------> USART2_RX
  343.     */
  344.     GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
  345.     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  346.     GPIO_InitStruct.Pull = GPIO_PULLUP;
  347.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  348.     GPIO_InitStruct.Alternate = GPIO_AF4_USART2;
  349.     HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  350.     /* USART2 interrupt Init */
  351.     HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
  352.     HAL_NVIC_EnableIRQ(USART2_IRQn);
  353.   /* USER CODE BEGIN USART2_MspInit 1 */

  354.   /* USER CODE END USART2_MspInit 1 */
  355.   }

  356. }

  357. void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
  358. {

  359.   if(huart->Instance==LPUART1)
  360.   {
  361.   /* USER CODE BEGIN LPUART1_MspDeInit 0 */

  362.   /* USER CODE END LPUART1_MspDeInit 0 */
  363.     /* Peripheral clock disable */
  364.     __HAL_RCC_LPUART1_CLK_DISABLE();
  365.   
  366.     /**LPUART1 GPIO Configuration   
  367.     PA2     ------> LPUART1_TX
  368.     PA3     ------> LPUART1_RX
  369.     */
  370.     HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3);

  371.     /* LPUART1 DMA DeInit */
  372.     HAL_DMA_DeInit(huart->hdmarx);

  373.     /* LPUART1 interrupt DeInit */
  374.     HAL_NVIC_DisableIRQ(AES_RNG_LPUART1_IRQn);
  375.   /* USER CODE BEGIN LPUART1_MspDeInit 1 */

  376.   /* USER CODE END LPUART1_MspDeInit 1 */
  377.   }
  378.   else if(huart->Instance==USART2)
  379.   {
  380.   /* USER CODE BEGIN USART2_MspDeInit 0 */

  381.   /* USER CODE END USART2_MspDeInit 0 */
  382.     /* Peripheral clock disable */
  383.     __HAL_RCC_USART2_CLK_DISABLE();
  384.   
  385.     /**USART2 GPIO Configuration   
  386.     PA9     ------> USART2_TX
  387.     PA10     ------> USART2_RX
  388.     */
  389.     HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);

  390.     /* USART2 interrupt DeInit */
  391.     HAL_NVIC_DisableIRQ(USART2_IRQn);
  392.   /* USER CODE BEGIN USART2_MspDeInit 1 */

  393.   /* USER CODE END USART2_MspDeInit 1 */
  394.   }

  395. }

  396. /* USER CODE BEGIN 1 */

  397. void EnableUartIT(void)//′??ú3?ê??ˉ
  398. {
  399.         //′??ú1
  400.     __HAL_UART_ENABLE_IT(&hlpuart1, UART_IT_IDLE);

  401.     __HAL_UART_CLEAR_IDLEFLAG(&hlpuart1);
  402.     HAL_UART_Receive_DMA(&hlpuart1,LPUart1type.LPUart1DMARecBuff,LPUART1_DMA_REC_SIZE);
  403. #if DEBUG
  404.     //′??ú2
  405.     __HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE);

  406.     HAL_UART_Receive_IT(&huart2, &Usart2Rx, 1);
  407. #endif
  408. }
复制代码
0条回答

一周热门 更多>