/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * *

© Copyright (c) 2024 STMicroelectronics. * All rights reserved.

* * This software component is licensed by ST under BSD 3-Clause license, * the "License"; You may not use this file except in compliance with the * License. You may obtain a copy of the License at: * opensource.org/licenses/BSD-3-Clause * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "states.h" #include "debug.h" /* 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 ---------------------------------------------------------*/ I2C_HandleTypeDef hi2c1; UART_HandleTypeDef huart5; /* USER CODE BEGIN PV */ UART_WakeUpTypeDef WakeUpSelection; /* 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_UART5_Init(void); /* 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_UART5_Init(); /* USER CODE BEGIN 2 */ // HAL_PWREx_EnableLowPowerRunMode(); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ STATE_Start(); __HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_STOP_WAKEUPCLOCK_MSI); /* make sure that no UART transfer is on-going */ while(__HAL_UART_GET_FLAG(&huart5, USART_ISR_BUSY) == SET); /* make sure that UART is ready to receive * (test carried out again later in HAL_UARTEx_StopModeWakeUpSourceConfig) */ while(__HAL_UART_GET_FLAG(&huart5, USART_ISR_REACK) == RESET); WakeUpSelection.WakeUpEvent = UART_WAKEUP_ON_READDATA_NONEMPTY; if (HAL_UARTEx_StopModeWakeUpSourceConfig(&huart5, WakeUpSelection)!= HAL_OK) { Error_Handler(); } /* Enable the UART Wake UP from STOP1 mode Interrupt */ __HAL_UART_ENABLE_IT(&huart5, UART_IT_WUF); /* enable MCU wake-up by UART */ HAL_UARTEx_EnableStopMode(&huart5); /* enter STOP1 mode */ DEBUG_Transmit((uint8_t *)"Entering stop mode\r\n", 20); HAL_SuspendTick(); HAL_PWREx_EnterSTOP1Mode(PWR_SLEEPENTRY_WFI); HAL_ResumeTick(); DEBUG_Transmit((uint8_t *)"success\r\n", 9); /* at that point, MCU has been awoken: the LED has been turned back on */ SystemClock_Config(); /* Wake Up based on RXNE flag successful */ HAL_UARTEx_DisableStopMode(&huart5); while (1) { // STATE_Listen(); /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* 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 */ if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK) { Error_Handler(); } /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI; RCC_OscInitStruct.MSIState = RCC_MSI_ON; RCC_OscInitStruct.MSICalibrationValue = 0; RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5; 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_MSI; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV8; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV8; 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.Timing = 0x00000509; hi2c1.Init.OwnAddress1 = 0; hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; hi2c1.Init.OwnAddress2 = 0; hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK; hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_ENABLE; hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_ENABLE; if (HAL_I2C_Init(&hi2c1) != HAL_OK) { Error_Handler(); } /** Configure Analogue filter */ if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK) { Error_Handler(); } /** Configure Digital filter */ if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN I2C1_Init 2 */ /* USER CODE END I2C1_Init 2 */ } /** * @brief UART5 Initialization Function * @param None * @retval None */ static void MX_UART5_Init(void) { /* USER CODE BEGIN UART5_Init 0 */ /* USER CODE END UART5_Init 0 */ /* USER CODE BEGIN UART5_Init 1 */ /* USER CODE END UART5_Init 1 */ huart5.Instance = UART5; huart5.Init.BaudRate = 9600; huart5.Init.WordLength = UART_WORDLENGTH_8B; huart5.Init.StopBits = UART_STOPBITS_1; huart5.Init.Parity = UART_PARITY_NONE; huart5.Init.Mode = UART_MODE_TX_RX; huart5.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart5.Init.OverSampling = UART_OVERSAMPLING_16; huart5.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; huart5.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; if (HAL_UART_Init(&huart5) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN UART5_Init 2 */ /* USER CODE END UART5_Init 2 */ } /** * @brief GPIO Initialization Function * @param None * @retval None */ static void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOI_CLK_ENABLE(); __HAL_RCC_GPIOH_CLK_ENABLE(); __HAL_RCC_GPIOE_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOG_CLK_ENABLE(); HAL_PWREx_EnableVddIO2(); __HAL_RCC_GPIOD_CLK_ENABLE(); __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOF_CLK_ENABLE(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(STM_PE0_BM1_GE_GPIO_Port, STM_PE0_BM1_GE_Pin, GPIO_PIN_RESET); /*Configure GPIO pins : PI10 PI0 PI9 PI7 PI6 PI2 PI1 PI11 PI3 PI8 PI4 PI5 */ GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_0|GPIO_PIN_9|GPIO_PIN_7 |GPIO_PIN_6|GPIO_PIN_2|GPIO_PIN_1|GPIO_PIN_11 |GPIO_PIN_3|GPIO_PIN_8|GPIO_PIN_4|GPIO_PIN_5; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOI, &GPIO_InitStruct); /*Configure GPIO pins : PH2 PH14 PH15 PH12 PH13 PH9 PH7 PH3 PH6 PH0 PH1 PH4 PH5 PH10 PH11 PH8 */ GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_12 |GPIO_PIN_13|GPIO_PIN_9|GPIO_PIN_7|GPIO_PIN_3 |GPIO_PIN_6|GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4 |GPIO_PIN_5|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_8; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOH, &GPIO_InitStruct); /*Configure GPIO pin : STM_PE0_BM1_GE_Pin */ GPIO_InitStruct.Pin = STM_PE0_BM1_GE_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(STM_PE0_BM1_GE_GPIO_Port, &GPIO_InitStruct); /*Configure GPIO pins : PB4 PB5 PB6 PB7 PB11 PB0 PB1 PB15 PB14 PB2 PB10 PB12 PB13 */ GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7 |GPIO_PIN_11|GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_15 |GPIO_PIN_14|GPIO_PIN_2|GPIO_PIN_10|GPIO_PIN_12 |GPIO_PIN_13; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /*Configure GPIO pins : PA15 PA12 PA11 PA10 PA9 PA8 PA0 PA5 PA4 PA7 PA3 PA6 PA2 PA1 */ GPIO_InitStruct.Pin = GPIO_PIN_15|GPIO_PIN_12|GPIO_PIN_11|GPIO_PIN_10 |GPIO_PIN_9|GPIO_PIN_8|GPIO_PIN_0|GPIO_PIN_5 |GPIO_PIN_4|GPIO_PIN_7|GPIO_PIN_3|GPIO_PIN_6 |GPIO_PIN_2|GPIO_PIN_1; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /*Configure GPIO pin : STM_PE1_BM1_ALERT_Pin */ GPIO_InitStruct.Pin = STM_PE1_BM1_ALERT_Pin; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(STM_PE1_BM1_ALERT_GPIO_Port, &GPIO_InitStruct); /*Configure GPIO pins : PG9 PG15 PG10 PG11 PG12 PG14 PG13 PG6 PG1 PG8 PG7 PG0 PG5 PG4 PG3 PG2 */ GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_15|GPIO_PIN_10|GPIO_PIN_11 |GPIO_PIN_12|GPIO_PIN_14|GPIO_PIN_13|GPIO_PIN_6 |GPIO_PIN_1|GPIO_PIN_8|GPIO_PIN_7|GPIO_PIN_0 |GPIO_PIN_5|GPIO_PIN_4|GPIO_PIN_3|GPIO_PIN_2; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOG, &GPIO_InitStruct); /*Configure GPIO pins : PD0 PD4 PD1 PD5 PD6 PD3 PD7 PD15 PD10 PD14 PD12 PD11 PD13 PD9 PD8 */ GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_4|GPIO_PIN_1|GPIO_PIN_5 |GPIO_PIN_6|GPIO_PIN_3|GPIO_PIN_7|GPIO_PIN_15 |GPIO_PIN_10|GPIO_PIN_14|GPIO_PIN_12|GPIO_PIN_11 |GPIO_PIN_13|GPIO_PIN_9|GPIO_PIN_8; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); /*Configure GPIO pins : PE4 PE3 PE2 PE6 PE5 PE10 PE9 PE15 PE8 PE14 PE7 PE13 PE12 PE11 */ GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_3|GPIO_PIN_2|GPIO_PIN_6 |GPIO_PIN_5|GPIO_PIN_10|GPIO_PIN_9|GPIO_PIN_15 |GPIO_PIN_8|GPIO_PIN_14|GPIO_PIN_7|GPIO_PIN_13 |GPIO_PIN_12|GPIO_PIN_11; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOE, &GPIO_InitStruct); /*Configure GPIO pins : PC10 PC13 PC11 PC6 PC9 PC8 PC7 PC4 PC0 PC1 PC2 PC5 PC3 */ GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_13|GPIO_PIN_11|GPIO_PIN_6 |GPIO_PIN_9|GPIO_PIN_8|GPIO_PIN_7|GPIO_PIN_4 |GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_5 |GPIO_PIN_3; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); /*Configure GPIO pins : PF2 PF1 PF0 PF3 PF4 PF5 PF10 PF15 PF14 PF11 PF13 PF12 */ GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_1|GPIO_PIN_0|GPIO_PIN_3 |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_10|GPIO_PIN_15 |GPIO_PIN_14|GPIO_PIN_11|GPIO_PIN_13|GPIO_PIN_12; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); } /* 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 */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/