diff --git a/stm32main.c b/stm32main.c new file mode 100644 index 0000000..ccf6ef7 --- /dev/null +++ b/stm32main.c @@ -0,0 +1,585 @@ +/* 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 +#include "stdio.h" +#include "string.h" +#define INT_STATUS_1_REG 0x00 +#define MAX30102_ADDR (0x57 << 1) +#define INT_STA1_REG 0x00 +#define INT_STA2_REG 0x01 +#define INT_ENA1_REG 0x02 +#define INT_ENA2_REG 0x03 +#define FIFO_WR_PTR 0x04 +#define FIFO_OVF_COUNT 0x05 +#define FIFO_RD_PTR 0x06 +#define FIFO_DATA_REG 0x07 +#define FIFO_CONFIG_REG 0x08 +#define MODE_CONFIG_REG 0x09 +#define SPO2_CONFIG_REG 0x0A +#define LED_PULSE_AMPL_1 0x0C +#define LED_PULSE_AMPL_2 0x0D +#define MULTI_LED_CONTR_REG_1 0x11 +#define MULTI_LED_CONTR_REG_2 0x12 + +// MAX30102 I2C Address +#define MAX30102_WR_ADDR 0xAE +#define MAX30102_RD_ADDR 0xAF +#define PORT_USED 0 +#define HR_Mode 0x02 // Red LED only +#define SpO2_Mode 0x03 // RED & IR LED +#define MulitLED_Mode 0x07 // RED & IR LED +#define THRESHOLD 1000 +#define SAMPLE_RATE 1000 +/* Private includes ----------------------------------------------------------*/ +/* USER CODE BEGIN Includes */ +int check=0; +I2C_HandleTypeDef hi2c1; +I2C_HandleTypeDef hi2c2; +I2C_HandleTypeDef hi2c3; + +TIM_HandleTypeDef htim3; + +UART_HandleTypeDef huart2; + +int max30102_write_reg(uint8_t , uint8_t,I2C_HandleTypeDef ); +int max30102_init(uint8_t,I2C_HandleTypeDef x); +void max30102_read_fifo_and_calculate_hr(I2C_HandleTypeDef ); +int max30102_read_register(uint8_t , uint8_t* , size_t ,I2C_HandleTypeDef ); +/* USER CODE BEGIN PV */ +void log_message(char *format, ...) { + char buffer[100]; // Adjust size as needed + va_list args; + va_start(args, format); + vsprintf(buffer, format, args); + va_end(args); + + HAL_UART_Transmit(&huart2, (uint8_t*)buffer, strlen(buffer), HAL_MAX_DELAY); +} +/* 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_I2C2_Init(void); +static void MX_I2C3_Init(void); +static void MX_USART2_UART_Init(void); +static void MX_TIM3_Init(void); + + +int max30102_write_reg(uint8_t reg , uint8_t value,I2C_HandleTypeDef x) +{ + uint8_t data[2]; + data[0] = reg; // Register address + data[1] = value; // Value to write + HAL_StatusTypeDef ret; + ret = HAL_I2C_Master_Transmit(&x, MAX30102_ADDR, data, sizeof(data), HAL_MAX_DELAY); + if ( ret != HAL_OK ){ + log_message("Not working write"); + return 0; + } + return 1; +} + +void max30102_read_fifo_and_calculate_hr(I2C_HandleTypeDef x) +{ + uint8_t wr_ptr=0; uint8_t rd_ptr=0; + max30102_read_register(FIFO_WR_PTR, &wr_ptr, 1,x); + max30102_read_register(FIFO_RD_PTR, &rd_ptr, 1,x); + + uint8_t num_samples = (wr_ptr - rd_ptr); + if(num_samples<1) + num_samples=num_samples+32; + + for (int i = 0; i < num_samples; i++) + { + uint8_t sample[6]; + HAL_Delay(10); + max30102_read_register(FIFO_DATA_REG, sample,6,x); + + //Extract only Red LED sample + uint32_t red_sample = ((uint32_t)(sample[0] << 16) | (uint32_t)(sample[1] << 8) | (uint32_t)(sample[2])) & 0x3FFFF; + + log_message("%d,",red_sample); + + } + +} + + int max30102_read_register(uint8_t reg, uint8_t* value, size_t length, I2C_HandleTypeDef x) + { + HAL_StatusTypeDef ret_code_t; + + ret_code_t = HAL_I2C_Master_Transmit(&x, MAX30102_ADDR, ®, 1, HAL_MAX_DELAY); + if (ret_code_t != HAL_OK ) { + log_message("Failed to set register address Error code"); + return 0; + } + + ret_code_t= HAL_I2C_Master_Receive(&x, MAX30102_ADDR, value, length,HAL_MAX_DELAY); + if (ret_code_t != HAL_OK ) { + log_message("Failed to read data from MAX30102 register"); + return 0; + } + return 1; + } + + + int max30102_init(uint8_t Mode, I2C_HandleTypeDef x) + { + if (!max30102_write_reg(INT_ENA1_REG, 0x00, x)) { // A_FULL=1, PPG_RDY_EN=0 ALC_OVF_EN=0 -> 0x80 + log_message("Failed to write INT_ENA1_REG"); + return 0; + } + if (!max30102_write_reg(INT_ENA2_REG, 0x00,x)) { // DIE_TEMP_RDY_EN = 0 + log_message("Failed to write INT_ENA2_REG"); + return 0; + } + if (!max30102_write_reg(FIFO_WR_PTR, 0x00,x)) { + log_message("Failed to write FIFO_WR_PTR"); + return 0; + } + if (!max30102_write_reg(FIFO_OVF_COUNT, 0x00,x)) { + log_message("Failed to write FIFO_OVF_COUNT"); + return 0; + } + if (!max30102_write_reg(FIFO_RD_PTR, 0x00,x)) { + log_message("Failed to write FIFO_RD_PTR"); + return 0; + } + if (!max30102_write_reg(FIFO_CONFIG_REG, 0xF0,x)) { // SMP_AVE=32(111), FIFO_ROLLOVER_EN=0, FIFO_A_FULL=32(0000) ->1110 0000=0xE0 + log_message("Failed to write FIFO_CONFIG_REG"); + return 0; + } + if (!max30102_write_reg(MODE_CONFIG_REG, Mode,x)) { + log_message("Failed to write MODE_CONFIG_REG"); + return 0; + } + if (!max30102_write_reg(SPO2_CONFIG_REG, 0x27,x)) { + log_message("Failed to write SPO2_CONFIG_REG"); + return 0; + } + if (!max30102_write_reg(LED_PULSE_AMPL_1, 0x32,x)) { // IR-LED current = 10mA + log_message("Failed to write LED_PULSE_AMPL_1"); + return 0; + } + if (!max30102_write_reg(LED_PULSE_AMPL_2, 0x32,x)) { // RED-LED current = 10mA + log_message("Failed to write LED_PULSE_AMPL_2"); + return 0; + } + + return 1; + } + +/* 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 PFP */ + +/* USER CODE END PFP */ + +/* Private user code ---------------------------------------------------------*/ +/* USER CODE BEGIN 0 */ + +/* 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_I2C2_Init(); + MX_I2C3_Init(); + MX_USART2_UART_Init(); + MX_TIM3_Init(); + log_message("Place_your_hand"); + HAL_Delay(2000); + max30102_init(SpO2_Mode,hi2c1); + max30102_init(SpO2_Mode,hi2c2); + max30102_init(SpO2_Mode,hi2c3); + + HAL_TIM_Base_Start_IT(&htim3); + /* USER CODE BEGIN 2 */ + + /* USER CODE END 2 */ +void kappdata(){ + max30102_read_fifo_and_calculate_hr(hi2c1); +} + +void vatta(){ + max30102_read_fifo_and_calculate_hr(hi2c2); +} + +void pitta(){ + max30102_read_fifo_and_calculate_hr(hi2c3); +} + /* Infinite loop */ + /* USER CODE BEGIN WHILE */ + while (1) + { + if(check==0) kappdata(); + if(check==1) vatta(); + if(check==2) pitta(); + + } + /* USER CODE END 3 */ +} + +/** + * @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_SCALE3); + + /** 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_ON; + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; + RCC_OscInitStruct.PLL.PLLM = 16; + RCC_OscInitStruct.PLL.PLLN = 336; + RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4; + RCC_OscInitStruct.PLL.PLLQ = 2; + RCC_OscInitStruct.PLL.PLLR = 2; + 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_2) != 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 I2C2 Initialization Function + * @param None + * @retval None + */ +static void MX_I2C2_Init(void) +{ + + /* USER CODE BEGIN I2C2_Init 0 */ + + /* USER CODE END I2C2_Init 0 */ + + /* USER CODE BEGIN I2C2_Init 1 */ + + /* USER CODE END I2C2_Init 1 */ + hi2c2.Instance = I2C2; + hi2c2.Init.ClockSpeed = 100000; + hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2; + hi2c2.Init.OwnAddress1 = 0; + hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; + hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; + hi2c2.Init.OwnAddress2 = 0; + hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; + hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; + if (HAL_I2C_Init(&hi2c2) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN I2C2_Init 2 */ + + /* USER CODE END I2C2_Init 2 */ + +} + +/** + * @brief I2C3 Initialization Function + * @param None + * @retval None + */ +static void MX_I2C3_Init(void) +{ + + /* USER CODE BEGIN I2C3_Init 0 */ + + /* USER CODE END I2C3_Init 0 */ + + /* USER CODE BEGIN I2C3_Init 1 */ + + /* USER CODE END I2C3_Init 1 */ + hi2c3.Instance = I2C3; + hi2c3.Init.ClockSpeed = 100000; + hi2c3.Init.DutyCycle = I2C_DUTYCYCLE_2; + hi2c3.Init.OwnAddress1 = 0; + hi2c3.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; + hi2c3.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; + hi2c3.Init.OwnAddress2 = 0; + hi2c3.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; + hi2c3.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; + if (HAL_I2C_Init(&hi2c3) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN I2C3_Init 2 */ + + /* USER CODE END I2C3_Init 2 */ + +} + +/** + * @brief TIM3 Initialization Function + * @param None + * @retval None + */ +static void MX_TIM3_Init(void) +{ + + /* USER CODE BEGIN TIM3_Init 0 */ + + /* USER CODE END TIM3_Init 0 */ + + TIM_ClockConfigTypeDef sClockSourceConfig = {0}; + TIM_MasterConfigTypeDef sMasterConfig = {0}; + + /* USER CODE BEGIN TIM3_Init 1 */ + + /* USER CODE END TIM3_Init 1 */ + htim3.Instance = TIM3; + htim3.Init.Prescaler = 8399; + htim3.Init.CounterMode = TIM_COUNTERMODE_UP; + htim3.Init.Period = 49999; + htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; + if (HAL_TIM_Base_Init(&htim3) != HAL_OK) + { + Error_Handler(); + } + sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; + if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK) + { + Error_Handler(); + } + sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; + sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; + if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN TIM3_Init 2 */ + + /* USER CODE END TIM3_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_GPIOC_CLK_ENABLE(); + __HAL_RCC_GPIOH_CLK_ENABLE(); + __HAL_RCC_GPIOA_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET); + + /*Configure GPIO pin : B1_Pin */ + GPIO_InitStruct.Pin = B1_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct); + + /*Configure GPIO pin : LD2_Pin */ + GPIO_InitStruct.Pin = LD2_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(LD2_GPIO_Port, &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 + */ +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 */