cancel
Showing results for 
Search instead for 
Did you mean: 

Hello I am working with Blackpill and LIS3DH I got issue Sleep Mode.

Kai_Satone
Associate III

When I use sleep mode while debug it sleep mode Works. But when I try runing code without using debug method it dosnot go in Sleep mode. When I chack current.

WhatsApp Image 2024-03-08 at 15.59.59_4747fd50.jpg

When using debug method stape by stape.

WhatsApp Image 2024-03-08 at 15.59.59_c9f9cb4e.jpg

 Without using stape by stape debug method.

Hear is the Code :-

#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
#include "string.h"
#include "stdio.h"
#include "math.h"
 
void SystemClock_Config(void);
 
 
#define LIS3DHTR_ADDR (0x1D)
 
/* LIS3DHTR registers */
#define LIS3DHTR_CTRL_REG1 0x20
#define LIS3DHTR_CTRL_REG4 0x23
#define LIS3DHTR_OUT_X_L   0x28
#define LIS3DHTR_INT1_CFG  0x30
#define LIS3DHTR_INT1_THS  0x32
#define LIS3DHTR_INT1_DURATION 0x33
#define LED_PIN GPIO_PIN_10
#define LED_GPIO_PORT GPIOA
 
#define FALL_THRESHOLD 24 // 24 mg
 
volatile uint8_t fallDetected = 0;
 
 
char *str={0};
uint8_t Rx_data;
 
 
 
 
int main(void)
{
  HAL_Init();
 SystemClock_Config();
 
  MX_GPIO_Init();
  MX_I2C1_Init();
  MX_USART2_UART_Init();
 
  HAL_UART_Receive_IT(&huart2, &Rx_data, 1);
 
      uint8_t ctrl_reg1_data = 0x27; // X, Y, Z axis enabled, power on mode, data rate selection = 10Hz
      HAL_I2C_Mem_Write(&hi2c1, LIS3DHTR_ADDR << 1, LIS3DHTR_CTRL_REG1, I2C_MEMADD_SIZE_8BIT, &ctrl_reg1_data, 1, HAL_MAX_DELAY);
 
      uint8_t ctrl_reg4_data = 0x00; // Continuous update, +/- 2g, Self-test disabled
      HAL_I2C_Mem_Write(&hi2c1, LIS3DHTR_ADDR << 1, LIS3DHTR_CTRL_REG4, I2C_MEMADD_SIZE_8BIT, &ctrl_reg4_data, 1, HAL_MAX_DELAY);
 
      uint8_t int1_cfg_data = 0x2A; // Enable interrupt on X, Y, Z axes' data overrun and data-ready events
      HAL_I2C_Mem_Write(&hi2c1, LIS3DHTR_ADDR << 1, LIS3DHTR_INT1_CFG, I2C_MEMADD_SIZE_8BIT, &int1_cfg_data, 1, HAL_MAX_DELAY);
 
      uint8_t int1_ths_data = FALL_THRESHOLD; // Set threshold for fall detection
      HAL_I2C_Mem_Write(&hi2c1, LIS3DHTR_ADDR << 1, LIS3DHTR_INT1_THS, I2C_MEMADD_SIZE_8BIT, &int1_ths_data, 1, HAL_MAX_DELAY);
 
      uint8_t int1_duration_data = 0x00; // Set duration of interrupt pulse to minimum value
      HAL_I2C_Mem_Write(&hi2c1, LIS3DHTR_ADDR << 1, LIS3DHTR_INT1_DURATION, I2C_MEMADD_SIZE_8BIT, &int1_duration_data, 1, HAL_MAX_DELAY);
 
  while (1)
  {
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 0);
  HAL_Delay(5000);
  char str[100]; // Declare str at the beginning
  strcpy(str, "Going into SLEEP MODE in 5 seconds\r\n");
      HAL_UART_Transmit(&huart2, (uint8_t *)str, strlen (str), 100);
 
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 1);
        HAL_Delay(10000);
      HAL_SuspendTick();
 
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 0);  // Just to indicate that the sleep mode is activated
 
      HAL_PWR_EnableSleepOnExit ();
 
              char message="shreyash";
              uint8_t data[6]={0};
                      HAL_I2C_Mem_Read(&hi2c1, LIS3DHTR_ADDR << 1, LIS3DHTR_OUT_X_L | 0x80, I2C_MEMADD_SIZE_8BIT, data, 6, HAL_MAX_DELAY);
 
                      int16_t xAccl = ((int16_t)((data[1] << 8| data[0]));
                      int16_t yAccl = ((int16_t)((data[3] << 8| data[2]));
                      int16_t zAccl = ((int16_t)((data[5] << 8| data[4]));
 
 
      sprintf(str, "\r\nx: %d ,\r\ny: %d ,r\nz: %d\r\n", xAccl,yAccl,zAccl);
      HAL_UART_Transmit(&huart2, (uint8_t *)str, sizeof(str), 100);
 
      //char magnitude = sqrt(xAccl * xAccl + yAccl * yAccl + zAccl * zAccl);
      char magnitude = sqrt(pow(xAccl, 2) + pow(yAccl, 2) + pow(zAccl, 2));
      sprintf(str, " trupti  %d\r \n",(int)( magnitude/10));
      HAL_UART_Transmit(&huart2,(uint8_t *)str, sizeof(str), 100);
 
      if((magnitude/10)>=20)
        {
 
        }
          else{
 
 
          HAL_UART_Transmit(&huart2, "below\r\n", strlen ("below\r\n"), HAL_MAX_DELAY);
            }
 
 
        strcpy(str, "Going into SLEEP(below) MODE in 5 seconds\r\n");
            HAL_UART_Transmit(&huart2, (uint8_t *)str, strlen (str), 100);
 
            HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 1);
              HAL_Delay(100);
 
            HAL_SuspendTick();
 
            HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 0);  // Just to indicate that the sleep mode is activated
 
            HAL_PWR_EnableSleepOnExit ();
  }
}
 
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
    if (GPIO_Pin == GPIO_PIN_0)
    { // Change according to your setup
        HAL_PWR_DisableSleepOnExit();
 
        char *wake_msg = "WakeUP from SLEEP\r\n";
        HAL_UART_Transmit(&huart2, (uint8_t *)wake_msg, strlen(wake_msg), 1000);
 
        for (int i=0; i<5; i++)
    {
      HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_4);
 
       HAL_Delay(5000);
    }
 
 
        char str[100]; // Declare str at the beginning
          strcpy(str, "Going into SLEEP(above) MODE in 5 seconds\r\n");
              HAL_UART_Transmit(&huart2, (uint8_t *)str, strlen (str), 100);
 
              HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 1);
                HAL_Delay(10000);
              HAL_SuspendTick();
 
              HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 0);  // Just to indicate that the sleep mode is activated
 
              HAL_PWR_EnableSleepOnExit ();    }
}

 

 

Thank you

2 REPLIES 2
Andrew Neil
Evangelist III

Please use this button to properly post source code:

AndrewNeil_0-1709894959505.png

 

 

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
#include "string.h"
#include "stdio.h"
#include "math.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */


#define LIS3DHTR_ADDR (0x1D)

/* LIS3DHTR registers */
#define LIS3DHTR_CTRL_REG1 0x20
#define LIS3DHTR_CTRL_REG4 0x23
#define LIS3DHTR_OUT_X_L   0x28
#define LIS3DHTR_INT1_CFG  0x30
#define LIS3DHTR_INT1_THS  0x32
#define LIS3DHTR_INT1_DURATION 0x33
#define LED_PIN GPIO_PIN_10
#define LED_GPIO_PORT GPIOA

/* Threshold for fall detection */
#define FALL_THRESHOLD 24 // 24 mg

volatile uint8_t fallDetected = 0;


char *str={0};
uint8_t Rx_data;




/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_I2C1_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
  HAL_UART_Receive_IT(&huart2, &Rx_data, 1);
  /* LIS3DHTR initialization */
      uint8_t ctrl_reg1_data = 0x27; // X, Y, Z axis enabled, power on mode, data rate selection = 10Hz
      HAL_I2C_Mem_Write(&hi2c1, LIS3DHTR_ADDR << 1, LIS3DHTR_CTRL_REG1, I2C_MEMADD_SIZE_8BIT, &ctrl_reg1_data, 1, HAL_MAX_DELAY);

      uint8_t ctrl_reg4_data = 0x00; // Continuous update, +/- 2g, Self-test disabled
      HAL_I2C_Mem_Write(&hi2c1, LIS3DHTR_ADDR << 1, LIS3DHTR_CTRL_REG4, I2C_MEMADD_SIZE_8BIT, &ctrl_reg4_data, 1, HAL_MAX_DELAY);

      uint8_t int1_cfg_data = 0x2A; // Enable interrupt on X, Y, Z axes' data overrun and data-ready events
      HAL_I2C_Mem_Write(&hi2c1, LIS3DHTR_ADDR << 1, LIS3DHTR_INT1_CFG, I2C_MEMADD_SIZE_8BIT, &int1_cfg_data, 1, HAL_MAX_DELAY);

      uint8_t int1_ths_data = FALL_THRESHOLD; // Set threshold for fall detection
      HAL_I2C_Mem_Write(&hi2c1, LIS3DHTR_ADDR << 1, LIS3DHTR_INT1_THS, I2C_MEMADD_SIZE_8BIT, &int1_ths_data, 1, HAL_MAX_DELAY);

      uint8_t int1_duration_data = 0x00; // Set duration of interrupt pulse to minimum value
      HAL_I2C_Mem_Write(&hi2c1, LIS3DHTR_ADDR << 1, LIS3DHTR_INT1_DURATION, I2C_MEMADD_SIZE_8BIT, &int1_duration_data, 1, HAL_MAX_DELAY);

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
	  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 0);
	  HAL_Delay(5000);
	  char str[100]; // Declare str at the beginning
		  strcpy(str, "Going into SLEEP MODE in 5 seconds\r\n");
		    	  HAL_UART_Transmit(&huart2, (uint8_t *)str, strlen (str), 100);

		    	  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 1);
		      	  HAL_Delay(10000);
//	           EnterSleepMode();

		    /*    Suspend Tick increment to prevent wakeup by Systick interrupt.
		    	  Otherwise the Systick interrupt will wake up the device within 1ms (HAL time base)
		    */
		    	  HAL_SuspendTick();

		    	  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 0);  // Just to indicate that the sleep mode is activated

		    	  HAL_PWR_EnableSleepOnExit ();

		    	  	     	  char message="shreyash";
		    	  	 //    	   sprintf(str, "shreyash");
		    	  	 //    	   HAL_UART_Transmit(&huart2, str, sizeof(str), 100);

		    	  	     	  uint8_t data[6]={0};
		    	  	     	        	  HAL_I2C_Mem_Read(&hi2c1, LIS3DHTR_ADDR << 1, LIS3DHTR_OUT_X_L | 0x80, I2C_MEMADD_SIZE_8BIT, data, 6, HAL_MAX_DELAY);

		    	  	     	        	  int16_t xAccl = ((int16_t)((data[1] << 8) | data[0]));
		    	  	     	        	  int16_t yAccl = ((int16_t)((data[3] << 8) | data[2]));
		    	  	     	        	  int16_t zAccl = ((int16_t)((data[5] << 8) | data[4]));


		    	  	sprintf(str, "\r\nx: %d ,\r\ny: %d ,r\nz: %d\r\n", xAccl,yAccl,zAccl);
		    	  	HAL_UART_Transmit(&huart2, (uint8_t *)str, sizeof(str), 100);

		    	  	 //char magnitude = sqrt(xAccl * xAccl + yAccl * yAccl + zAccl * zAccl);
		    	  	 char magnitude = sqrt(pow(xAccl, 2) + pow(yAccl, 2) + pow(zAccl, 2));
		    	  	 sprintf(str, " trupti  %d\r \n",(int)( magnitude/10));
		    	  	 HAL_UART_Transmit(&huart2,(uint8_t *)str, sizeof(str), 100);

		    	  	if((magnitude/10)>=20)
		    	  	  {

		    	  		 HAL_GPIO_EXTI_Callback(GPIO_PIN_0);
//		    	  		 HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
//
//
//
//		    	  			    //	  Resume Tick interrupt if disabled prior to sleep mode entry
//		    	  			    	  HAL_ResumeTick();
//
//		    	  		//	    	  str = "WakeUP from SLEEP\r\n";
//		    	  			    	  HAL_UART_Transmit(&huart2, "WakeUP from SLEEP\r\n", strlen ("WakeUP from SLEEP\r\n"), HAL_MAX_DELAY);
//
//		    	  			    	  for (int i=0; i<5; i++)
//		    	  			    	  {
//		    	  			    		  HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_4);
//
//		    	  			    		  HAL_Delay(100);
//
//		    	  			    	  }
		    	  	  }
		    	  	    else{


		    	  	    	 HAL_UART_Transmit(&huart2, "below\r\n", strlen ("below\r\n"), HAL_MAX_DELAY);
		    	  	      }


		    	  			  strcpy(str, "Going into SLEEP(below) MODE in 5 seconds\r\n");
		    	  			    	  HAL_UART_Transmit(&huart2, (uint8_t *)str, strlen (str), 100);

		    	  			    	  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 1);
		    	  			      	  HAL_Delay(100);
		    	  	//	           EnterSleepMode();

		    	  			    /*    Suspend Tick increment to prevent wakeup by Systick interrupt.
		    	  			    	  Otherwise the Systick interrupt will wake up the device within 1ms (HAL time base)
		    	  			    */
		    	  			    	  HAL_SuspendTick();

		    	  			    	  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 0);  // Just to indicate that the sleep mode is activated

		    	  			    	  HAL_PWR_EnableSleepOnExit ();
    /* USER CODE BEGIN 3 */
  }
}

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
    if (GPIO_Pin == GPIO_PIN_0)
    { // Change according to your setup
        HAL_PWR_DisableSleepOnExit();

        char *wake_msg = "WakeUP from SLEEP\r\n";
        HAL_UART_Transmit(&huart2, (uint8_t *)wake_msg, strlen(wake_msg), 1000);

        for (int i=0; i<5; i++)
    {
      	HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_4);

       HAL_Delay(5000);
    }


        char str[100]; // Declare str at the beginning
        		  strcpy(str, "Going into SLEEP(above) MODE in 5 seconds\r\n");
        		    	  HAL_UART_Transmit(&huart2, (uint8_t *)str, strlen (str), 100);

        		    	  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 1);
        		      	  HAL_Delay(10000);
        //	           EnterSleepMode();

        		    /*    Suspend Tick increment to prevent wakeup by Systick interrupt.
        		    	  Otherwise the Systick interrupt will wake up the device within 1ms (HAL time base)
        		    */
        		    	  HAL_SuspendTick();

        		    	  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 0);  // Just to indicate that the sleep mode is activated

        		    	  HAL_PWR_EnableSleepOnExit ();
        // Additional logic to handle wakeup event
    }
}

//void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin_4)
//{
//    str = "WakeUP from SLEEP by EXTI\r\n";
//    HAL_UART_Transmit(&huart2, (uint8_t *)str, strlen (str), HAL_MAX_DELAY);
//    HAL_PWR_DisableSleepOnExit ();
//}
  /* USER CODE END 3 */
//  void EnterSleepMode(void) {
//      // Indicate going to sleep
//      char *sleep_msg = "Going into SLEEP MODE\r\n";
//      HAL_UART_Transmit(&huart2, (uint8_t *)sleep_msg, strlen(sleep_msg), 1000);
//      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 1);
//       HAL_Delay(10000);
//      HAL_SuspendTick();
//      HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
//      HAL_ResumeTick();
//  }



/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 12;
  RCC_OscInitStruct.PLL.PLLN = 96;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses 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_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#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 CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */