AnsweredAssumed Answered

Stop mode: no entry

Question asked by d.urbano on Oct 17, 2014
Latest reply on Oct 17, 2014 by d.urbano
Hi at all,
I'm working on an stm32401ccu, but I can't send it in stop mode; my code is quite simple, in fact I use the UART1 to send a command that realize the entry in stop mode, and a button linked to PB0 pin (configured as EXTI0), to exit from stop mode. There are 2 external oscillator, 16MHz and 32.768KHz, and the first one is selected as primary source clock. This is my very simple code:
001./* Includes ------------------------------------------------------------------*/
002.#include "stm32f4xx_hal.h"
003.#include "usart.h"
004.#include "gpio.h"
005.#include "stdbool.h"
006./* Private variables ---------------------------------------------------------*/
007. 
008./* USER CODE BEGIN 0 */
009.bool f_EnterStopMode = false;
010.bool f_reset         = false;
011.bool swt             = false;
012. 
013.uint8_t RxBuffer1[3];
014./* USER CODE END 0 */
015. 
016./* Private function prototypes -----------------------------------------------*/
017.void SystemClock_Config(void);
018. 
019.int main(void)
020.{
021. 
022.  /* USER CODE BEGIN 1 */
023.    uint32_t temp;
024.  /* USER CODE END 1 */
025. 
026.  /* MCU Configuration----------------------------------------------------------*/
027. 
028.  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
029.  HAL_Init();
030. 
031.  /* Configure the system clock */
032.  SystemClock_Config();
033. 
034.  /* System interrupt init*/
035.  /* Sets the priority grouping field */
036.  HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_0);
037.  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
038. 
039.  /* Initialize all configured peripherals */
040.  MX_GPIO_Init();
041.  MX_USART1_UART_Init();
042. 
043.  /* USER CODE BEGIN 2 */
044.    HAL_UART_Transmit(&huart1, (uint8_t *) &"$", 1, 5);
045.    HAL_Delay(300);
046.    HAL_UART_Transmit(&huart1, (uint8_t *) &"Device ready  ", 15, 5);
047.    HAL_UART_Receive_IT(&huart1, (uint8_t*)RxBuffer1, 1);
048.     
049.    HAL_GPIO_WritePin(GPIOB, 5, GPIO_PIN_SET);
050.    HAL_GPIO_WritePin(GPIOB, 5, GPIO_PIN_RESET);
051.  /* USER CODE END 2 */
052. 
053.  /* USER CODE BEGIN 3 */
054.  /* Infinite loop */
055.  while (1)
056.  {
057.      HAL_Delay(1000);
058.      if(swt) HAL_UART_Transmit(&huart1, (uint8_t *) &".", 1, 15);
059.      else    HAL_UART_Transmit(&huart1, (uint8_t *) &"|", 1, 15);     
060.      swt = !swt;
061.       
062.      if(f_reset) HAL_NVIC_SystemReset();     
063.       
064.      if(f_EnterStopMode)
065.      {
066.          HAL_UART_Transmit(&huart1, (uint8_t *) &"\r\nDevice is going to enter in stop mode", 39, 15);
067.          f_EnterStopMode = false;
068.          temp = PWR->CR;
069.          temp |= (uint32_t) 0x00000200;//FPDS[bit 9] = 1
070.          temp &= (uint32_t) 0xFFFFFBFF;//LPLVDS[bit 10] = 0
071.          PWR->CR = temp;
072. 
073.          __HAL_RCC_HSI_ENABLE();//enable HSI RC oscillator
074.          while((RCC->CR & (uint32_t)0x00000002)==0);//wait until HSI is stable
075.          // Switch to HSI oscillator before enter stop mode
076.          temp = RCC->CFGR;
077.          temp &= (uint32_t) 0xFFFFFFFC;
078.          RCC->CFGR = temp;
079.           
080.          HAL_NVIC_ClearPendingIRQ(USART1_IRQn);
081.          HAL_NVIC_ClearPendingIRQ(EXTI0_IRQn);
082.          HAL_NVIC_ClearPendingIRQ(SysTick_IRQn);
083.          temp = EXTI->PR;
084.          temp|= (uint32_t) 0x0067FFFF;
085.          EXTI->PR = temp;
086.           
087.          HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);//<- qui il codice si ferma
088.          /* L'istruzione agisce sul power control register resettando il bit 1, PDDS,
089.           e settando il bit 0, LPDS, in base al valore di 'Regulator' */   
090.             
091.          SystemClock_Config();//<- qui il codice riprende la sua esecuzione 
092.          HAL_UART_Transmit(&huart1, (uint8_t *) &"\r\nDevice is now in run mode", 35, 15);
093.      }
094.  }
095.  /* USER CODE END 3 */
096. 
097.}
098. 
099./** System Clock Configuration
100.*/
101.void SystemClock_Config(void)
102.{
103. 
104.  RCC_ClkInitTypeDef RCC_ClkInitStruct;
105.  RCC_OscInitTypeDef RCC_OscInitStruct;
106. 
107.  __PWR_CLK_ENABLE();
108. 
109.  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
110. 
111.  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
112.  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
113.  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
114.  HAL_RCC_OscConfig(&RCC_OscInitStruct);
115. 
116.  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
117.  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
118.  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
119.  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
120.  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
121.  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
122.     
123.}
124. 
125./* USER CODE BEGIN 4 */
126. 
127./**
128.  * @brief EXTI line detection callbacks
129.  * @param GPIO_Pin: Specifies the pins connected EXTI line
130.  * @retval None
131.  */
132.void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
133.{
134.     
135.}
136./**
137.  * @brief  Rx Transfer completed callback
138.  * @param  UartHandle: UART handle
139.  * @note  
140.  * @retval None
141.  */
142.void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
143.{
144.    if(UartHandle == &huart1)
145.    {
146.        HAL_GPIO_WritePin(GPIOB, 5, GPIO_PIN_SET);
147.        if(RxBuffer1[0]=='P')       f_EnterStopMode = true
148.        else if(RxBuffer1[0]=='R')  f_reset         = true;     
149.  
150.        /* Re-enable UART1 (external) receiver interrupt and initialize RxBuffer1 as reception buffer */
151.        if(HAL_UART_Receive_IT(&huart1, (uint8_t*)RxBuffer1, 1) != HAL_OK)
152.        {
153.            //Error_Handler();
154.        }
155.        HAL_GPIO_WritePin(GPIOB, 5, GPIO_PIN_RESET);       
156.    }
157. 
158.}
159./* USER CODE END 4 */
160. 
161.#ifdef USE_FULL_ASSERT
162. 
163./**
164.   * @brief Reports the name of the source file and the source line number
165.   * where the assert_param error has occurred.
166.   * @param file: pointer to the source file name
167.   * @param line: assert_param error line source number
168.   * @retval None
169.   */
170.void assert_failed(uint8_t* file, uint32_t line)
171.{
172.  /* USER CODE BEGIN 6 */
173./* User can add his own implementation to report the file name and line number,
174.ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
175.  /* USER CODE END 6 */
176. 
177.}
178. 
179.#endif
It seems all is what the reference manual says about stop mode, but it does not work and I' like to know why; please, could anyone help me ?
Regards

Outcomes