volatile uint8_t motion_pending = 0; //Generic motion indicator field for low-power waker callback //... /* Infinite main loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE BEGIN 3 */ // If there is no pending motion and no incoming request, go to STOP mode if (!motion_pending) { // Try to catch a request quickly; if none, sleep. if (HAL_UART_Receive(&huart3, rx_buf, 1, 50) != HAL_OK) { enter_stop_mode(); continue; // after wake-up, loop again } } //Wait for the window request from the gateway and any motion if (HAL_UART_Receive(&huart3, rx_buf, 1, 500) == HAL_OK && rx_buf[0] == 0x01 && motion_pending) { // Clear the motion flag now motion_pending = 0; // Prepare response window, then send tx_buf[0] = 0x00; tx_buf[1] = 0x00; tx_buf[2] = 0x06; if (imu_send_window(&hi2c2, &huart3, tx_buf, LoRa_MAX_DATA_SIZE, 3, 96, 10) == 0) { HAL_GPIO_TogglePin(GPIOA, USER_LED_Pin); } } // After sending, go back to STOP quickly (next loop iteration will sleep) } //....