[20] | 1 | /* USER CODE BEGIN Header */ |
---|
| 2 | /** |
---|
| 3 | ****************************************************************************** |
---|
| 4 | * @file : main.c |
---|
| 5 | * @brief : Main program body |
---|
| 6 | ****************************************************************************** |
---|
| 7 | * @attention |
---|
| 8 | * |
---|
| 9 | * Copyright (c) 2025 STMicroelectronics. |
---|
| 10 | * All rights reserved. |
---|
| 11 | * |
---|
| 12 | * This software is licensed under terms that can be found in the LICENSE file |
---|
| 13 | * in the root directory of this software component. |
---|
| 14 | * If no LICENSE file comes with this software, it is provided AS-IS. |
---|
| 15 | * |
---|
| 16 | ****************************************************************************** |
---|
| 17 | */ |
---|
| 18 | /* USER CODE END Header */ |
---|
| 19 | /* Includes ------------------------------------------------------------------*/ |
---|
| 20 | #include "main.h" |
---|
| 21 | #include "adc.h" |
---|
| 22 | #include "dma.h" |
---|
| 23 | #include "fdcan.h" |
---|
| 24 | #include "i2c.h" |
---|
| 25 | #include "spi.h" |
---|
| 26 | #include "usart.h" |
---|
| 27 | #include "usb.h" |
---|
| 28 | #include "gpio.h" |
---|
| 29 | |
---|
| 30 | /* Private includes ----------------------------------------------------------*/ |
---|
| 31 | /* USER CODE BEGIN Includes */ |
---|
| 32 | #include <stdio.h> |
---|
| 33 | #include "sysdata.h" |
---|
| 34 | #include "wh_counter.h" |
---|
| 35 | #include "ah_counter.h" |
---|
| 36 | #include "eeprom.h" |
---|
| 37 | #include "modbus.h" |
---|
| 38 | #include "chip_temperature.h" |
---|
| 39 | /* USER CODE END Includes */ |
---|
| 40 | |
---|
| 41 | /* Private typedef -----------------------------------------------------------*/ |
---|
| 42 | /* USER CODE BEGIN PTD */ |
---|
| 43 | |
---|
| 44 | /* USER CODE END PTD */ |
---|
| 45 | |
---|
| 46 | /* Private define ------------------------------------------------------------*/ |
---|
| 47 | /* USER CODE BEGIN PD */ |
---|
| 48 | |
---|
| 49 | /* USER CODE END PD */ |
---|
| 50 | |
---|
| 51 | /* Private macro -------------------------------------------------------------*/ |
---|
| 52 | /* USER CODE BEGIN PM */ |
---|
| 53 | |
---|
| 54 | /* USER CODE END PM */ |
---|
| 55 | |
---|
| 56 | /* Private variables ---------------------------------------------------------*/ |
---|
| 57 | |
---|
| 58 | /* USER CODE BEGIN PV */ |
---|
| 59 | modbus_t modbusData; |
---|
| 60 | #define ADCCONVERTEDVALUES_BUFFER_SIZE 2 |
---|
| 61 | __IO uint32_t adc12Data[ADCCONVERTEDVALUES_BUFFER_SIZE] __attribute__((section(".RAM1"))); |
---|
| 62 | __IO uint32_t adc1Data[1] __attribute__((section(".RAM1"))); |
---|
| 63 | __IO uint32_t adc2Data[1] __attribute__((section(".RAM1"))); |
---|
| 64 | __IO uint32_t adc3Data[3] __attribute__((section(".RAM1"))); |
---|
| 65 | __IO uint32_t adc4Data[1] __attribute__((section(".RAM1"))); |
---|
| 66 | __IO uint32_t adc5Data[4] __attribute__((section(".RAM1"))); |
---|
| 67 | int silentmode =0; |
---|
| 68 | /* USER CODE END PV */ |
---|
| 69 | |
---|
| 70 | /* Private function prototypes -----------------------------------------------*/ |
---|
| 71 | void SystemClock_Config(void); |
---|
| 72 | /* USER CODE BEGIN PFP */ |
---|
| 73 | |
---|
| 74 | /* USER CODE END PFP */ |
---|
| 75 | |
---|
| 76 | /* Private user code ---------------------------------------------------------*/ |
---|
| 77 | /* USER CODE BEGIN 0 */ |
---|
| 78 | |
---|
| 79 | /* USER CODE END 0 */ |
---|
| 80 | |
---|
| 81 | /** |
---|
| 82 | * @brief The application entry point. |
---|
| 83 | * @retval int |
---|
| 84 | */ |
---|
| 85 | int main(void) |
---|
| 86 | { |
---|
| 87 | |
---|
| 88 | /* USER CODE BEGIN 1 */ |
---|
| 89 | uint8_t firstStartCatcher; |
---|
| 90 | /* USER CODE END 1 */ |
---|
| 91 | |
---|
| 92 | /* MCU Configuration--------------------------------------------------------*/ |
---|
| 93 | |
---|
| 94 | /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ |
---|
| 95 | HAL_Init(); |
---|
| 96 | |
---|
| 97 | /* USER CODE BEGIN Init */ |
---|
| 98 | |
---|
| 99 | /* USER CODE END Init */ |
---|
| 100 | |
---|
| 101 | /* Configure the system clock */ |
---|
| 102 | SystemClock_Config(); |
---|
| 103 | |
---|
| 104 | /* USER CODE BEGIN SysInit */ |
---|
| 105 | |
---|
| 106 | /* USER CODE END SysInit */ |
---|
| 107 | |
---|
| 108 | /* Initialize all configured peripherals */ |
---|
| 109 | MX_GPIO_Init(); |
---|
| 110 | MX_DMA_Init(); |
---|
| 111 | MX_ADC1_Init(); |
---|
| 112 | MX_ADC2_Init(); |
---|
| 113 | MX_ADC3_Init(); |
---|
| 114 | MX_ADC4_Init(); |
---|
| 115 | MX_ADC5_Init(); |
---|
| 116 | MX_FDCAN2_Init(); |
---|
| 117 | MX_FDCAN3_Init(); |
---|
| 118 | MX_I2C3_Init(); |
---|
| 119 | MX_I2C4_Init(); |
---|
| 120 | MX_SPI3_Init(); |
---|
| 121 | MX_USART1_UART_Init(); |
---|
| 122 | MX_USART2_UART_Init(); |
---|
| 123 | MX_USB_PCD_Init(); |
---|
| 124 | /* USER CODE BEGIN 2 */ |
---|
| 125 | printf("Test debug io\r\n"); |
---|
| 126 | SYS_DATA_Init(); |
---|
| 127 | WH_COUNTER_Init(); |
---|
| 128 | AH_COUNTER_Init(); |
---|
| 129 | |
---|
| 130 | |
---|
| 131 | startType_t startType = EEPROM_isFirstStart(); |
---|
| 132 | |
---|
| 133 | switch(startType) |
---|
| 134 | { |
---|
| 135 | case FIRST_START_AFTER_ERASE: EEPROM_fullRestore(&sys_data); break; |
---|
| 136 | case FIRST_START_AFTER_COMPARTIBLE_UPDATE: EEPROM_readConfig(&sys_data); break; |
---|
| 137 | case FIRST_START_AFTER_INCOMPARTIBLE_UPDATE: EEPROM_factoryRestore(&sys_data, 0); break; // Preserving calibration and settings |
---|
| 138 | case FIRST_START_ERROR: EEPROM_fullRestore(&sys_data); break; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | if(HAL_GPIO_ReadPin(GPIO_INPUT_BTN_MODE_GPIO_Port, GPIO_INPUT_BTN_MODE_Pin) == GPIO_PIN_RESET) |
---|
| 142 | { |
---|
| 143 | HAL_Delay(50); |
---|
| 144 | if(HAL_GPIO_ReadPin(GPIO_INPUT_BTN_MODE_GPIO_Port, GPIO_INPUT_BTN_MODE_Pin) == GPIO_PIN_RESET) |
---|
| 145 | { |
---|
| 146 | printf("factory restore...\n"); |
---|
| 147 | EEPROM_factoryRestore(&sys_data, 1); |
---|
| 148 | } |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | // Modbus Initialisierung |
---|
| 152 | mbInit(&modbusData, sys_data.s.parameter.baudrate, sys_data.s.parameter.parityMode, sys_data.s.parameter.stopBit, &huart2); |
---|
| 153 | |
---|
| 154 | // STM32G0 Chiptemperatur Kalibrierung |
---|
| 155 | CHIP_TEMPERATURE_Calibration(/*&sys_data*/); |
---|
| 156 | |
---|
| 157 | HAL_ADCEx_Calibration_Start(&hadc1, ADC_DIFFERENTIAL_ENDED); |
---|
| 158 | HAL_ADCEx_Calibration_Start(&hadc2, ADC_DIFFERENTIAL_ENDED); |
---|
| 159 | HAL_ADCEx_Calibration_Start(&hadc3, ADC_SINGLE_ENDED); |
---|
| 160 | HAL_ADCEx_Calibration_Start(&hadc4, ADC_DIFFERENTIAL_ENDED); |
---|
| 161 | HAL_ADCEx_Calibration_Start(&hadc5, ADC_SINGLE_ENDED); |
---|
| 162 | |
---|
| 163 | |
---|
| 164 | |
---|
| 165 | //SET_BIT(hadc2.Instance->CFGR, ADC_CFGR_DMAEN); //Enable DMA transfer for ADC slave (ADC12_CCR.MDMA = 0b00 -> MDMA mode disabled) |
---|
| 166 | //HAL_DMA_Start(hadc2.DMA_Handle,(uint32_t)&hadc2.Instance->DR, (uint32_t)adc2Data,1); //Start ADC slave DMA |
---|
| 167 | //SET_BIT(hadc1.Instance->CFGR, ADC_CFGR_DMAEN); //Enable DMA transfer for ADC master (ADC12_CCR.MDMA = 0b00 -> MDMA mode disabled) |
---|
| 168 | |
---|
| 169 | //HAL_ADC_Start_DMA(&hadc2, (uint32_t*)adc2Data, 1); |
---|
| 170 | if (HAL_ADCEx_MultiModeStart_DMA(&hadc1,(uint32_t *)adc12Data,ADCCONVERTEDVALUES_BUFFER_SIZE)) //Start ADC interleaved mode |
---|
| 171 | { |
---|
| 172 | /* Start Error */ |
---|
| 173 | Error_Handler(); |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | if (HAL_ADC_Start_DMA(&hadc5, (uint32_t *) adc5Data , 4)) |
---|
| 177 | { |
---|
| 178 | /* Start Error */ |
---|
| 179 | Error_Handler(); |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | /* USER CODE END 2 */ |
---|
| 183 | |
---|
| 184 | /* Infinite loop */ |
---|
| 185 | /* USER CODE BEGIN WHILE */ |
---|
| 186 | while (1) |
---|
| 187 | { |
---|
| 188 | /* USER CODE END WHILE */ |
---|
| 189 | |
---|
| 190 | /* USER CODE BEGIN 3 */ |
---|
| 191 | // Modbus Kommunikation |
---|
| 192 | HAL_Delay(1000); |
---|
| 193 | printf("data12d1=%d,data12d2=%d,data5=%d\r\n", adc12Data[0], adc12Data[1], adc5Data[2]); |
---|
| 194 | if (mbGetFrameComplete(&modbusData) == true) |
---|
| 195 | { |
---|
| 196 | if (mbSlaveCheckModbusRtuQuery(&modbusData) == RESPOND_TO_QUERY) |
---|
| 197 | { |
---|
| 198 | if (silentmode == 0) |
---|
| 199 | { |
---|
| 200 | mbSlaveProcessRtuQuery(&modbusData); |
---|
| 201 | } |
---|
| 202 | } |
---|
| 203 | else |
---|
| 204 | { |
---|
| 205 | huart1.RxState = HAL_UART_STATE_BUSY_RX; |
---|
| 206 | } |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | } |
---|
| 210 | /* USER CODE END 3 */ |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | /** |
---|
| 214 | * @brief System Clock Configuration |
---|
| 215 | * @retval None |
---|
| 216 | */ |
---|
| 217 | void SystemClock_Config(void) |
---|
| 218 | { |
---|
| 219 | RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
---|
| 220 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
---|
| 221 | |
---|
| 222 | /** Configure the main internal regulator output voltage |
---|
| 223 | */ |
---|
| 224 | HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1); |
---|
| 225 | |
---|
| 226 | /** Initializes the RCC Oscillators according to the specified parameters |
---|
| 227 | * in the RCC_OscInitTypeDef structure. |
---|
| 228 | */ |
---|
| 229 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_HSE; |
---|
| 230 | RCC_OscInitStruct.HSEState = RCC_HSE_ON; |
---|
| 231 | RCC_OscInitStruct.HSI48State = RCC_HSI48_ON; |
---|
| 232 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
---|
| 233 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; |
---|
| 234 | RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV2; |
---|
| 235 | RCC_OscInitStruct.PLL.PLLN = 16; |
---|
| 236 | RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; |
---|
| 237 | RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2; |
---|
| 238 | RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2; |
---|
| 239 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) |
---|
| 240 | { |
---|
| 241 | Error_Handler(); |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | /** Initializes the CPU, AHB and APB buses clocks |
---|
| 245 | */ |
---|
| 246 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |
---|
| 247 | |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; |
---|
| 248 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
---|
| 249 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
---|
| 250 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; |
---|
| 251 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; |
---|
| 252 | |
---|
| 253 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK) |
---|
| 254 | { |
---|
| 255 | Error_Handler(); |
---|
| 256 | } |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | /* USER CODE BEGIN 4 */ |
---|
| 260 | |
---|
| 261 | |
---|
| 262 | /* USER CODE END 4 */ |
---|
| 263 | |
---|
| 264 | /** |
---|
| 265 | * @brief This function is executed in case of error occurrence. |
---|
| 266 | * @retval None |
---|
| 267 | */ |
---|
| 268 | void Error_Handler(void) |
---|
| 269 | { |
---|
| 270 | /* USER CODE BEGIN Error_Handler_Debug */ |
---|
| 271 | /* User can add his own implementation to report the HAL error return state */ |
---|
| 272 | __disable_irq(); |
---|
| 273 | printf("error handler!\r\n"); |
---|
| 274 | while (1) |
---|
| 275 | { |
---|
| 276 | } |
---|
| 277 | /* USER CODE END Error_Handler_Debug */ |
---|
| 278 | } |
---|
| 279 | #ifdef USE_FULL_ASSERT |
---|
| 280 | /** |
---|
| 281 | * @brief Reports the name of the source file and the source line number |
---|
| 282 | * where the assert_param error has occurred. |
---|
| 283 | * @param file: pointer to the source file name |
---|
| 284 | * @param line: assert_param error line source number |
---|
| 285 | * @retval None |
---|
| 286 | */ |
---|
| 287 | void assert_failed(uint8_t *file, uint32_t line) |
---|
| 288 | { |
---|
| 289 | /* USER CODE BEGIN 6 */ |
---|
| 290 | /* User can add his own implementation to report the file name and line number, |
---|
| 291 | ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ |
---|
| 292 | printf("Wrong parameters value: file %s on line %d\r\n", file, line); |
---|
| 293 | /* USER CODE END 6 */ |
---|
| 294 | } |
---|
| 295 | #endif /* USE_FULL_ASSERT */ |
---|