本帖最后由 zhongjp85 于 2018-10-26 10:20 编辑
STM32L031退出STOP模式后 串口无法使用 (不进入stop模式串口正常)
哪位能帮忙看下吗? 谢谢!
主要代码如下 init()是初始化IO口的:
- void test1(void)
- {
- /* 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 2 MHz */
- // SystemClock_Config();
- // init();
- //
- // printf("system is init ok!
");
- // printf("wait 5s NB Reset!
");
- /* Configure the RTC */
- RTC_Config();
-
- while(1)
- {
- SystemClock_Config();
- init();
- printf("------- wake up --------
");
- LedOn(1);
- HAL_Delay(5000);
- LedOff(1);
- HAL_Delay(5000);
-
- printf("
------- sleep --------
");
- rtc_wakeup_cfg();
- SystemPower_Config();
- HW_WakeupPinInit();
- enter_stop_mode();
- }
-
- }
- void tasks(void)
- {
- bool isGoSleep = false;
-
- while(!isGoSleep)
- {
- LedTask();
- SensorTask();
- isGoSleep = NB_Task();
- }
- }
- void rtc_wakeup_cfg(void)
- {
- /* Disable Wakeup Counter */
- HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);
- HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, RTC_TIME, RTC_WAKEUPCLOCK_CK_SPRE_16BITS);
- }
- void enter_stop_mode(void)
- {
- /* Enable Power Control clock */
- __HAL_RCC_PWR_CLK_ENABLE();
- /* Enter Stop Mode */
- HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
- }
- void HW_WakeupPinInit(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- /* Configure PB.1 as input with External interrupt */
- GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
-
- /* Enable GPIOA clock */
- __HAL_RCC_GPIOA_CLK_ENABLE();
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
-
- GPIO_InitStruct.Pin = GPIO_PIN_13 | GPIO_PIN_14;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
-
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
-
- /* Enable and set PA.12 (Arduino D2) EXTI Interrupt to the lowest priority */
- NVIC_SetPriority((IRQn_Type)(EXTI4_15_IRQn), 0x03);
- HAL_NVIC_EnableIRQ((IRQn_Type)(EXTI4_15_IRQn));
- }
- /** System Clock Configuration
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- RCC_PeriphCLKInitTypeDef PeriphClkInit;
- /**Configure the main internal regulator output voltage
- */
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.HSICalibrationValue = 16;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_4;
- RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**Initializes the CPU, AHB and APB busses clocks
- */
- 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_DIV1;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2|RCC_PERIPHCLK_LPUART1;
- PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
- PeriphClkInit.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK1;
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**Configure the Systick interrupt time
- */
- HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
- /**Configure the Systick
- */
- HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
- /* SysTick_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
- }
- /**
- * @brief System Power Configuration
- * The system Power is configured as follow :
- * + Regulator in LP mode
- * + VREFINT OFF, with fast wakeup enabled
- * + MSI as SysClk after Wake Up
- * + No IWDG
- * + Wakeup using EXTI Line (User push-button PC.13)
- * @param None
- * @retval None
- */
- static void SystemPower_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /* Enable Power Control clock */
- __HAL_RCC_PWR_CLK_ENABLE();
- /* Enable Ultra low power mode */
- HAL_PWREx_EnableUltraLowPower();
- /* Enable the fast wake up from Ultra low power mode */
- HAL_PWREx_EnableFastWakeUp();
- /* Select MSI as system clock source after Wake Up from Stop mode */
- __HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_STOP_WAKEUPCLOCK_MSI);
- /* Enable GPIOs clock */
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- __HAL_RCC_GPIOC_CLK_ENABLE();
- __HAL_RCC_GPIOH_CLK_ENABLE();
- /* Configure all GPIO port pins in Analog Input mode (floating input trigger OFF) */
- /* Note: Debug using ST-Link is not possible during the execution of this */
- /* example because communication between ST-link and the device */
- /* under test is done through UART. All GPIO pins are disabled (set */
- /* to analog input mode) including UART I/O pins. */
- GPIO_InitStructure.Pin = GPIO_PIN_All;
- GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
- GPIO_InitStructure.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
- HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
- HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
- HAL_GPIO_Init(GPIOH, &GPIO_InitStructure);
- /* Disable GPIOs clock */
- __HAL_RCC_GPIOA_CLK_DISABLE();
- __HAL_RCC_GPIOB_CLK_DISABLE();
- __HAL_RCC_GPIOC_CLK_DISABLE();
- __HAL_RCC_GPIOH_CLK_DISABLE();
- }
- /**
- * @brief RTC Configuration
- * RTC Clocked by LSI (see HAL_RTC_MspInit)
- * @param None
- * @retval None
- */
- static void RTC_Config(void)
- {
- /* Configure RTC */
- RTCHandle.Instance = RTC;
- /* Configure RTC prescaler and RTC data registers as follow:
- - Hour Format = Format 24
- - Asynch Prediv = Value according to source clock
- - Synch Prediv = Value according to source clock
- - OutPut = Output Disable
- - OutPutPolarity = High Polarity
- - OutPutType = Open Drain */
- RTCHandle.Init.HourFormat = RTC_HOURFORMAT_24;
- RTCHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
- RTCHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
- RTCHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
- RTCHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
- RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
- if(HAL_RTC_Init(&RTCHandle) != HAL_OK)
- {
- /* Initialization Error */
- Error_Handler();
- }
- }
- void MX_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- __HAL_RCC_GPIOC_CLK_ENABLE();
- __HAL_RCC_GPIOH_CLK_ENABLE();
- GPIOA->MODER = 0xFFFFFFFF;
- GPIOB->MODER = 0xFFFFFFFF;
- GPIOC->MODER = 0xFFFFFFFF;
- GPIOH->MODER = 0xFFFFFFFF;
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4|GPIO_PIN_5, GPIO_PIN_SET);
- /*Configure GPIO pins : PA1 PA4 PA5 */
- GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- /*Configure GPIO pin : PA7 PA6*/
- GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_6;
- GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
- GPIO_InitStruct.Pull = GPIO_PULLDOWN;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- __HAL_RCC_GPIOB_CLK_DISABLE();
- __HAL_RCC_GPIOC_CLK_DISABLE();
- __HAL_RCC_GPIOH_CLK_DISABLE();
- HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);
- }
- void MX_DMA_Init(void)
- {
- /* DMA controller clock enable */
- __HAL_RCC_DMA1_CLK_ENABLE();
- /* DMA interrupt init */
- /* DMA1_Channel2_3_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(DMA1_Channel2_3_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(DMA1_Channel2_3_IRQn);
- }
- /* LPUART1 init function */
- void MX_LPUART1_UART_Init(void)
- {
- hlpuart1.Instance = LPUART1;
- hlpuart1.Init.BaudRate = 115200;
- hlpuart1.Init.WordLength = UART_WORDLENGTH_8B;
- hlpuart1.Init.StopBits = UART_STOPBITS_1;
- hlpuart1.Init.Parity = UART_PARITY_NONE;
- hlpuart1.Init.Mode = UART_MODE_TX_RX;
- hlpuart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- hlpuart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
- hlpuart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
- if (HAL_UART_Init(&hlpuart1) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- }
- /* USART2 init function */
- void MX_USART2_UART_Init(void)
- {
- #if DEBUG
- huart2.Instance = USART2;
- huart2.Init.BaudRate = 115200;
- huart2.Init.WordLength = UART_WORDLENGTH_8B;
- huart2.Init.StopBits = UART_STOPBITS_1;
- huart2.Init.Parity = UART_PARITY_NONE;
- huart2.Init.Mode = UART_MODE_TX_RX;
- huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- huart2.Init.OverSampling = UART_OVERSAMPLING_16;
- huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
- huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
- if (HAL_UART_Init(&huart2) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- #endif
- }
- void HAL_UART_MspInit(UART_HandleTypeDef* huart)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- if(huart->Instance==LPUART1)
- {
- /* USER CODE BEGIN LPUART1_MspInit 0 */
- /* USER CODE END LPUART1_MspInit 0 */
- /* Peripheral clock enable */
- __HAL_RCC_LPUART1_CLK_ENABLE();
-
- /**LPUART1 GPIO Configuration
- PA2 ------> LPUART1_TX
- PA3 ------> LPUART1_RX
- */
- GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- GPIO_InitStruct.Alternate = GPIO_AF6_LPUART1;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- /* LPUART1 DMA Init */
- /* LPUART1_RX Init */
- hdma_lpuart1_rx.Instance = DMA1_Channel3;
- hdma_lpuart1_rx.Init.Request = DMA_REQUEST_5;
- hdma_lpuart1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
- hdma_lpuart1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
- hdma_lpuart1_rx.Init.MemInc = DMA_MINC_ENABLE;
- hdma_lpuart1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
- hdma_lpuart1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
- hdma_lpuart1_rx.Init.Mode = DMA_NORMAL;
- hdma_lpuart1_rx.Init.Priority = DMA_PRIORITY_LOW;
- if (HAL_DMA_Init(&hdma_lpuart1_rx) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- __HAL_LINKDMA(huart,hdmarx,hdma_lpuart1_rx);
- /* LPUART1 interrupt Init */
- HAL_NVIC_SetPriority(AES_RNG_LPUART1_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(AES_RNG_LPUART1_IRQn);
- /* USER CODE BEGIN LPUART1_MspInit 1 */
- /* USER CODE END LPUART1_MspInit 1 */
- }
- else if(huart->Instance==USART2)
- {
- /* USER CODE BEGIN USART2_MspInit 0 */
- /* USER CODE END USART2_MspInit 0 */
- /* Peripheral clock enable */
- __HAL_RCC_USART2_CLK_ENABLE();
-
- /**USART2 GPIO Configuration
- PA9 ------> USART2_TX
- PA10 ------> USART2_RX
- */
- GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- GPIO_InitStruct.Alternate = GPIO_AF4_USART2;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- /* USART2 interrupt Init */
- HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(USART2_IRQn);
- /* USER CODE BEGIN USART2_MspInit 1 */
- /* USER CODE END USART2_MspInit 1 */
- }
- }
- void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
- {
- if(huart->Instance==LPUART1)
- {
- /* USER CODE BEGIN LPUART1_MspDeInit 0 */
- /* USER CODE END LPUART1_MspDeInit 0 */
- /* Peripheral clock disable */
- __HAL_RCC_LPUART1_CLK_DISABLE();
-
- /**LPUART1 GPIO Configuration
- PA2 ------> LPUART1_TX
- PA3 ------> LPUART1_RX
- */
- HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3);
- /* LPUART1 DMA DeInit */
- HAL_DMA_DeInit(huart->hdmarx);
- /* LPUART1 interrupt DeInit */
- HAL_NVIC_DisableIRQ(AES_RNG_LPUART1_IRQn);
- /* USER CODE BEGIN LPUART1_MspDeInit 1 */
- /* USER CODE END LPUART1_MspDeInit 1 */
- }
- else if(huart->Instance==USART2)
- {
- /* USER CODE BEGIN USART2_MspDeInit 0 */
- /* USER CODE END USART2_MspDeInit 0 */
- /* Peripheral clock disable */
- __HAL_RCC_USART2_CLK_DISABLE();
-
- /**USART2 GPIO Configuration
- PA9 ------> USART2_TX
- PA10 ------> USART2_RX
- */
- HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);
- /* USART2 interrupt DeInit */
- HAL_NVIC_DisableIRQ(USART2_IRQn);
- /* USER CODE BEGIN USART2_MspDeInit 1 */
- /* USER CODE END USART2_MspDeInit 1 */
- }
- }
- /* USER CODE BEGIN 1 */
- void EnableUartIT(void)//′??ú3?ê??ˉ
- {
- //′??ú1
- __HAL_UART_ENABLE_IT(&hlpuart1, UART_IT_IDLE);
- __HAL_UART_CLEAR_IDLEFLAG(&hlpuart1);
- HAL_UART_Receive_DMA(&hlpuart1,LPUart1type.LPUart1DMARecBuff,LPUART1_DMA_REC_SIZE);
- #if DEBUG
- //′??ú2
- __HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE);
- HAL_UART_Receive_IT(&huart2, &Usart2Rx, 1);
- #endif
- }
复制代码
一周热门 更多>