2025-04-28 2:48 PM
My sensor does not turn on and does not send any data. Here is my code:
#include "main.h"
#include "max30100_for_stm32_hal.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include <string.h>
#include "max30100_for_stm32_hal.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define BPM_THRESHOLD 30000 // Seuil de détection de pic IR (à ajuster selon les données du capteur)
#define SAMPLE_RATE 1000 // Intervalle de collecte de données en ms
#define MAX30100_REG_STATUS 0x00
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c1;
UART_HandleTypeDef huart2;
/* USER CODE BEGIN PV */
uint32_t ir_data = 0;
uint32_t red_data = 0;
int bpm = 0;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
static void MX_USART2_UART_Init(void);
int calculate_bpm(uint32_t ir_value);
/* USER CODE BEGIN PFP */
int __io_putchar(int ch);
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int calculate_bpm(uint32_t ir_value)
{
static uint32_t last_ir_value = 0;
static uint32_t last_peak_time = 0;
static int pulse_count = 0;
uint32_t current_time = HAL_GetTick();
// Détection d'un pic IR basé sur un seuil (d'après la valeur IR)
if (ir_value > BPM_THRESHOLD && last_ir_value <= BPM_THRESHOLD) // Si on détecte un pic IR
{
if (last_peak_time > 0)
{
// Calcul du BPM basé sur l'intervalle entre les pics IR
int interval = current_time - last_peak_time;
pulse_count++;
if (pulse_count >= 5) // Après avoir détecté 5 pics
{
bpm = 60000 / interval; // Calcul du BPM en utilisant l'intervalle entre les pics
pulse_count = 0; // Réinitialisation du compteur de pics
}
}
last_peak_time = current_time; // Mise à jour du temps du dernier pic
}
last_ir_value = ir_value; // Mise à jour de la dernière valeur IR
return bpm;
}
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 */
// Initialisation du capteur MAX30100 via la bibliothèque HAL
// Appeler l'initialisation du capteur MAX30100
MAX30100_Init(&hi2c1, &huart2);
// Vérifier l'état du capteur sans tester une valeur précise
uint8_t data;
data = MAX30100_ReadReg(MAX30100_REG_STATUS);
if (data != 0xFF) { // 0xFF signifie généralement "pas de réponse" sur I2C
printf("Capteur MAX30100 prêt. Surveillance de la fréquence cardiaque en cours...\r\n");
} else {
printf("Erreur de communication I2C avec le capteur MAX30100\r\n");
Error_Handler();
}
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
// Lire les données du FIFO du capteur
MAX30100_ReadFIFO(); // Lecture du FIFO sans paramètres
// Récupérer ensuite les dernières données IR et RED
ir_data = MAX30100_getIR(); // fonction que tu dois avoir dans ta lib
bpm = calculate_bpm(ir_data); // Calcul BPM à partir de ir_data
printf("IR: %lu, Fréquence cardiaque: %d bpm\r\n", ir_data, bpm);
HAL_Delay(SAMPLE_RATE); // Délai entre chaque mesure
}
int __io_putchar(int ch)
{
HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
return ch;
}
int fputc(int ch, FILE *f)
{
return __io_putchar(ch);
}
/* USER CODE END 3 */
}
// Rediriger printf vers UART
// Redirection printf vers UART
// Gestion des erreurs
void Error_Handler(void)
{
while (1)
{
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); // Clignote la LED pour signaler une erreur
HAL_Delay(100);
}
}
/**
* @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_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
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_HSI;
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_0) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief I2C1 Initialization Function
* @param None
* @retval None
*/
static void MX_I2C1_Init(void)
{
/* USER CODE BEGIN I2C1_Init 0 */
/* USER CODE END I2C1_Init 0 */
/* USER CODE BEGIN I2C1_Init 1 */
/* USER CODE END I2C1_Init 1 */
hi2c1.Instance = I2C1;
hi2c1.Init.ClockSpeed = 100000;
hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN I2C1_Init 2 */
/* USER CODE END I2C1_Init 2 */
}
/**
* @brief USART2 Initialization Function
* @param None
* @retval None
*/
static void MX_USART2_UART_Init(void)
{
/* USER CODE BEGIN USART2_Init 0 */
/* USER CODE END USART2_Init 0 */
/* USER CODE BEGIN USART2_Init 1 */
/* USER CODE END USART2_Init 1 */
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;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART2_Init 2 */
/* USER CODE END USART2_Init 2 */
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
/*Configure GPIO pin : PA5 */
GPIO_InitStruct.Pin = 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);
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
/* 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 */
2025-04-29 12:34 AM
Could you please update your post and use "Insert code sample" for your code? You're more likely to get an answer if it is formatted in a way that makes it easier to read.
2025-04-29 1:11 AM
Welcome to the forum.
Please see How to write your question to maximize your chances to find a solution; in particular How to insert source code.
@1233 wrote:My sensor does not turn on and does not send any data.
As described in the articles linked above, you need to give more details.
In particular, what debugging have you done to find what's wrong?
Have you looked at the I2C lines with an oscilloscope to verify basic signal integrity?
Have you looked at the I2C lines with an analyser to verify the comms?
Note that the MAX30100 is an Analog Devices product - nothing to do with ST.
For support with the MAX30100, see:
https://www.analog.com/en/products/max30100.html
Their "Engineer Zone" forums: https://ez.analog.com/
2025-04-29 1:24 AM
When is the funeral?