123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983 |
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- * @attention
- *
- * <h2><center>© Copyright (c) 2021 STMicroelectronics.
- * All rights reserved.</center></h2>
- *
- * 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"
- #include "usb_device.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #include "usbd_cdc_if.h"
- #include "uartio.h"
- #include "commprt.h"
- #include <stdlib.h>
- #include <stdio.h>
- /* USER CODE END Includes */
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- /* USER CODE END PTD */
- /* Private define ------------------------------------------------------------*/
- /* USER CODE BEGIN PD */
- #define VREFINT_CAL_VALUE (*((uint16_t*)VREFINT_CAL_ADDR_CMSIS))
- #define VREFINT_CAL_VREF 3000UL
- /* USER CODE END PD */
- /* Private macro -------------------------------------------------------------*/
- /* USER CODE BEGIN PM */
- /* USER CODE END PM */
- /* Private variables ---------------------------------------------------------*/
- ADC_HandleTypeDef hadc;
- DMA_HandleTypeDef hdma_adc;
- COMP_HandleTypeDef hcomp2;
- CRC_HandleTypeDef hcrc;
- DAC_HandleTypeDef hdac;
- I2C_HandleTypeDef hi2c1;
- I2C_HandleTypeDef hi2c2;
- TIM_HandleTypeDef htim2;
- TIM_HandleTypeDef htim3;
- TIM_HandleTypeDef htim4;
- TIM_HandleTypeDef htim6;
- UART_HandleTypeDef huart1;
- DMA_HandleTypeDef hdma_usart1_rx;
- DMA_HandleTypeDef hdma_usart1_tx;
- /* USER CODE BEGIN PV */
- struct {
- uint32_t CH0;
- uint32_t TEMP;
- uint32_t VREF;
- } adc_meas;
- volatile struct {
- uint16_t Tim2;
- uint16_t Tim3;
- uint16_t Tim4;
- uint16_t Tim6;
- uint16_t Comp;
- uint16_t Exti;
- } Flags;
- volatile uint32_t pulses = 0;
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_DMA_Init(void);
- static void MX_GPIO_Init(void);
- static void MX_ADC_Init(void);
- static void MX_DAC_Init(void);
- static void MX_I2C2_Init(void);
- static void MX_COMP2_Init(void);
- static void MX_USART1_UART_Init(void);
- static void MX_TIM6_Init(void);
- static void MX_TIM4_Init(void);
- static void MX_TIM3_Init(void);
- static void MX_TIM2_Init(void);
- static void MX_I2C1_Init(void);
- static void MX_CRC_Init(void);
- /* USER CODE BEGIN PFP */
- /* USER CODE END PFP */
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
- {
- pulses++;
- }
- void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
- {
- Flags.Exti |= GPIO_Pin;
- }
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
- {
- if(htim == &htim4)
- {
- Flags.Tim4++;
- }
- if(htim == &htim6)
- {
- Flags.Tim6 = 1;
- }
- }
- uint32_t GetVcc(void)
- {
- return (VREFINT_CAL_VALUE * VREFINT_CAL_VREF / adc_meas.VREF);
- }
- void SetDACVoltage(uint32_t channel, uint32_t voltage)
- {
- HAL_DAC_SetValue(&hdac, channel, DAC_ALIGN_12B_R, (voltage * 4095UL) / GetVcc());
- }
- void Switch3V3Regulator(OnOff_t state)
- {
- if(state == ON)
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_SET);
- else
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET);
- }
- void SwitchPeriphSupply(OnOff_t state)
- {
- if(state == ON)
- HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
- else
- HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
- }
- void Beep()
- {
- HAL_TIM_Base_Stop(&htim2);
- HAL_TIM_Base_Start(&htim2);
- }
- /* 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_DMA_Init();
- MX_GPIO_Init();
- MX_ADC_Init();
- MX_DAC_Init();
- MX_COMP2_Init();
- MX_USART1_UART_Init();
- MX_TIM6_Init();
- MX_TIM4_Init();
- MX_TIM3_Init();
- MX_TIM2_Init();
- MX_I2C1_Init();
- MX_USB_DEVICE_Init();
- MX_CRC_Init();
- /* USER CODE BEGIN 2 */
- Switch3V3Regulator(ON);
- SwitchPeriphSupply(ON);
- HAL_Delay(100);
- MX_I2C2_Init();
- ssd1306_Init();
- ssd1306_Fill(Black);
- ssd1306_UpdateScreen();
- HAL_DAC_Start(&hdac, DAC_CHANNEL_1);
- HAL_DAC_Start(&hdac, DAC_CHANNEL_2);
- HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_R, 0xFFF);
- HAL_DAC_SetValue(&hdac, DAC_CHANNEL_2, DAC_ALIGN_12B_R, 0xFFF);
- HAL_Delay(10);
- HAL_ADC_Start_DMA(&hadc, (uint32_t*)&adc_meas, 3);
- HAL_Delay(10);
- HAL_ADC_Stop_DMA(&hadc);
- SetDACVoltage(DAC_CHANNEL_1, SIPM_BIAS_VOLTAGE_OFFSET);
- SetDACVoltage(DAC_CHANNEL_2, COMP_THRESHOLD_VOLTAGE);
- HAL_COMP_Start_IT(&hcomp2);
- HAL_TIM_Base_Start_IT(&htim6);
- HAL_TIM_Base_Start_IT(&htim4);
- HAL_TIM_OC_Start(&htim3, TIM_CHANNEL_4);
- UARTIO_Init(&huart1);
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- char tempstr[32];
- uint32_t pps = 0;
- uint32_t pps_avg = 0;
- uint32_t total_dose = 0;
- uint8_t pwdn_cnt = 0;
- uint8_t window = 0;
- Flags.Exti = 0;
- while (1)
- {
- if(Flags.Exti)
- {
- uint16_t btn = Flags.Exti;
- Flags.Exti = 0;
- if(btn == GPIO_PIN_15)
- {
- window ^= 1;
- }
- }
- if(Flags.Tim6)
- {
- Flags.Tim6 = 0;
- HAL_ADC_Stop_DMA(&hadc);
- if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_14) == GPIO_PIN_RESET)
- {
- if(++pwdn_cnt > 10)
- {
- ssd1306_Reset();
- SwitchPeriphSupply(OFF);
- HAL_I2C_DeInit(&hi2c1);
- HAL_I2C_DeInit(&hi2c2);
- HAL_UART_DeInit(&huart1);
- HAL_GPIO_DeInit(GPIOB, GPIO_PIN_All);
- HAL_GPIO_DeInit(GPIOA, GPIO_PIN_All);
- HAL_PWR_EnterSTOPMode(PWR_MAINREGULATOR_ON, PWR_STOPENTRY_WFI);
- }
- }
- else
- {
- pwdn_cnt = 0;
- }
- if((pps / pps_avg > 2) || (pps_avg / pps > 2))
- {
- pps_avg = pps;
- }
- else
- {
- pps_avg = (pps + pps_avg) / 2;
- }
- ssd1306_Fill(Black);
- ssd1306_DrawRectangle(0, 0, SSD1306_WIDTH - 1, SSD1306_HEIGHT - 1, White);
- if(window == 0)
- {
- ssd1306_SetCursor(1, 7);
- sprintf(tempstr, "%5.2f uSv/h", (float)pps_avg / 100.0);
- ssd1306_WriteString(tempstr, Font_11x18, White);
- }
- else
- {
- ssd1306_SetCursor(2, 2);
- ssd1306_WriteString(tempstr, Font_7x10, White);
- }
- ssd1306_UpdateScreen();
- SetDACVoltage(DAC_CHANNEL_1, SIPM_BIAS_VOLTAGE_OFFSET);
- SetDACVoltage(DAC_CHANNEL_2, COMP_THRESHOLD_VOLTAGE);
- HAL_ADC_Start_DMA(&hadc, (uint32_t*)&adc_meas, 3);
- sprintf(tempstr, "%5.2f uSv/h\r\n", (float)pps_avg / 100.0);
- COMM_Transmit((uint8_t*)&pps_avg, sizeof(pps_avg), COMM_USB);
- }
- if(pulses > 50 || Flags.Tim4 > 3)
- {
- uint32_t elapsed = htim4.Instance->CNT + (Flags.Tim4 * 50000);
- HAL_TIM_Base_Stop(&htim4);
- pps = (pulses * 100000) / elapsed;
- pulses = 0;
- Flags.Tim4 = 0;
- htim4.Instance->CNT = 0;
- HAL_TIM_Base_Start(&htim4);
- Beep();
- }
- /* 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
- */
- __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_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- 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_HSE;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
- RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV4;
- 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_DIV1;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /**
- * @brief ADC Initialization Function
- * @param None
- * @retval None
- */
- static void MX_ADC_Init(void)
- {
- /* USER CODE BEGIN ADC_Init 0 */
- /* USER CODE END ADC_Init 0 */
- ADC_ChannelConfTypeDef sConfig = {0};
- /* USER CODE BEGIN ADC_Init 1 */
- /* USER CODE END ADC_Init 1 */
- /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
- */
- hadc.Instance = ADC1;
- hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
- hadc.Init.Resolution = ADC_RESOLUTION_12B;
- hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
- hadc.Init.ScanConvMode = ADC_SCAN_ENABLE;
- hadc.Init.EOCSelection = ADC_EOC_SEQ_CONV;
- hadc.Init.LowPowerAutoWait = ADC_AUTOWAIT_DISABLE;
- hadc.Init.LowPowerAutoPowerOff = ADC_AUTOPOWEROFF_DISABLE;
- hadc.Init.ChannelsBank = ADC_CHANNELS_BANK_A;
- hadc.Init.ContinuousConvMode = DISABLE;
- hadc.Init.NbrOfConversion = 3;
- hadc.Init.DiscontinuousConvMode = DISABLE;
- hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
- hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
- hadc.Init.DMAContinuousRequests = DISABLE;
- if (HAL_ADC_Init(&hadc) != HAL_OK)
- {
- Error_Handler();
- }
- /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
- */
- sConfig.Channel = ADC_CHANNEL_0;
- sConfig.Rank = ADC_REGULAR_RANK_1;
- sConfig.SamplingTime = ADC_SAMPLETIME_48CYCLES;
- if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
- */
- sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
- sConfig.Rank = ADC_REGULAR_RANK_2;
- if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
- */
- sConfig.Channel = ADC_CHANNEL_VREFINT;
- sConfig.Rank = ADC_REGULAR_RANK_3;
- if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN ADC_Init 2 */
- /* USER CODE END ADC_Init 2 */
- }
- /**
- * @brief COMP2 Initialization Function
- * @param None
- * @retval None
- */
- static void MX_COMP2_Init(void)
- {
- /* USER CODE BEGIN COMP2_Init 0 */
- /* USER CODE END COMP2_Init 0 */
- /* USER CODE BEGIN COMP2_Init 1 */
- /* USER CODE END COMP2_Init 1 */
- hcomp2.Instance = COMP2;
- hcomp2.Init.InvertingInput = COMP_INVERTINGINPUT_DAC2;
- hcomp2.Init.NonInvertingInput = COMP_NONINVERTINGINPUT_PB4;
- hcomp2.Init.Output = COMP_OUTPUT_NONE;
- hcomp2.Init.Mode = COMP_MODE_HIGHSPEED;
- hcomp2.Init.WindowMode = COMP_WINDOWMODE_DISABLE;
- hcomp2.Init.TriggerMode = COMP_TRIGGERMODE_IT_RISING;
- if (HAL_COMP_Init(&hcomp2) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN COMP2_Init 2 */
- /* USER CODE END COMP2_Init 2 */
- }
- /**
- * @brief CRC Initialization Function
- * @param None
- * @retval None
- */
- static void MX_CRC_Init(void)
- {
- /* USER CODE BEGIN CRC_Init 0 */
- /* USER CODE END CRC_Init 0 */
- /* USER CODE BEGIN CRC_Init 1 */
- /* USER CODE END CRC_Init 1 */
- hcrc.Instance = CRC;
- if (HAL_CRC_Init(&hcrc) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN CRC_Init 2 */
- /* USER CODE END CRC_Init 2 */
- }
- /**
- * @brief DAC Initialization Function
- * @param None
- * @retval None
- */
- static void MX_DAC_Init(void)
- {
- /* USER CODE BEGIN DAC_Init 0 */
- /* USER CODE END DAC_Init 0 */
- DAC_ChannelConfTypeDef sConfig = {0};
- /* USER CODE BEGIN DAC_Init 1 */
- /* USER CODE END DAC_Init 1 */
- /** DAC Initialization
- */
- hdac.Instance = DAC;
- if (HAL_DAC_Init(&hdac) != HAL_OK)
- {
- Error_Handler();
- }
- /** DAC channel OUT1 config
- */
- sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
- sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
- if (HAL_DAC_ConfigChannel(&hdac, &sConfig, DAC_CHANNEL_1) != HAL_OK)
- {
- Error_Handler();
- }
- /** DAC channel OUT2 config
- */
- sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE;
- if (HAL_DAC_ConfigChannel(&hdac, &sConfig, DAC_CHANNEL_2) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN DAC_Init 2 */
- /* USER CODE END DAC_Init 2 */
- }
- /**
- * @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 = 400000;
- 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 TIM2 Initialization Function
- * @param None
- * @retval None
- */
- static void MX_TIM2_Init(void)
- {
- /* USER CODE BEGIN TIM2_Init 0 */
- /* USER CODE END TIM2_Init 0 */
- TIM_ClockConfigTypeDef sClockSourceConfig = {0};
- TIM_MasterConfigTypeDef sMasterConfig = {0};
- /* USER CODE BEGIN TIM2_Init 1 */
- /* USER CODE END TIM2_Init 1 */
- htim2.Instance = TIM2;
- htim2.Init.Prescaler = 600;
- htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
- htim2.Init.Period = 1000;
- htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
- htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
- if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
- {
- Error_Handler();
- }
- sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
- if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_TIM_OnePulse_Init(&htim2, TIM_OPMODE_SINGLE) != HAL_OK)
- {
- Error_Handler();
- }
- sMasterConfig.MasterOutputTrigger = TIM_TRGO_ENABLE;
- sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
- if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN TIM2_Init 2 */
- /* USER CODE END TIM2_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_SlaveConfigTypeDef sSlaveConfig = {0};
- TIM_MasterConfigTypeDef sMasterConfig = {0};
- TIM_OC_InitTypeDef sConfigOC = {0};
- /* USER CODE BEGIN TIM3_Init 1 */
- /* USER CODE END TIM3_Init 1 */
- htim3.Instance = TIM3;
- htim3.Init.Prescaler = 48;
- htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
- htim3.Init.Period = 1000;
- htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
- htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
- 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();
- }
- if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
- {
- Error_Handler();
- }
- sSlaveConfig.SlaveMode = TIM_SLAVEMODE_GATED;
- sSlaveConfig.InputTrigger = TIM_TS_ITR1;
- if (HAL_TIM_SlaveConfigSynchro(&htim3, &sSlaveConfig) != HAL_OK)
- {
- Error_Handler();
- }
- sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
- sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
- if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
- {
- Error_Handler();
- }
- sConfigOC.OCMode = TIM_OCMODE_PWM1;
- sConfigOC.Pulse = 10;
- sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
- sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
- if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN TIM3_Init 2 */
- /* USER CODE END TIM3_Init 2 */
- HAL_TIM_MspPostInit(&htim3);
- }
- /**
- * @brief TIM4 Initialization Function
- * @param None
- * @retval None
- */
- static void MX_TIM4_Init(void)
- {
- /* USER CODE BEGIN TIM4_Init 0 */
- /* USER CODE END TIM4_Init 0 */
- TIM_ClockConfigTypeDef sClockSourceConfig = {0};
- TIM_MasterConfigTypeDef sMasterConfig = {0};
- /* USER CODE BEGIN TIM4_Init 1 */
- /* USER CODE END TIM4_Init 1 */
- htim4.Instance = TIM4;
- htim4.Init.Prescaler = 240;
- htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
- htim4.Init.Period = 50000-1;
- htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
- htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
- if (HAL_TIM_Base_Init(&htim4) != HAL_OK)
- {
- Error_Handler();
- }
- sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
- if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK)
- {
- Error_Handler();
- }
- sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
- sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
- if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN TIM4_Init 2 */
- /* USER CODE END TIM4_Init 2 */
- }
- /**
- * @brief TIM6 Initialization Function
- * @param None
- * @retval None
- */
- static void MX_TIM6_Init(void)
- {
- /* USER CODE BEGIN TIM6_Init 0 */
- /* USER CODE END TIM6_Init 0 */
- TIM_MasterConfigTypeDef sMasterConfig = {0};
- /* USER CODE BEGIN TIM6_Init 1 */
- /* USER CODE END TIM6_Init 1 */
- htim6.Instance = TIM6;
- htim6.Init.Prescaler = 600;
- htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
- htim6.Init.Period = 20000;
- htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
- if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
- {
- Error_Handler();
- }
- sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
- sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
- if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN TIM6_Init 2 */
- /* USER CODE END TIM6_Init 2 */
- }
- /**
- * @brief USART1 Initialization Function
- * @param None
- * @retval None
- */
- static void MX_USART1_UART_Init(void)
- {
- /* USER CODE BEGIN USART1_Init 0 */
- /* USER CODE END USART1_Init 0 */
- /* USER CODE BEGIN USART1_Init 1 */
- /* USER CODE END USART1_Init 1 */
- huart1.Instance = USART1;
- huart1.Init.BaudRate = 115200;
- huart1.Init.WordLength = UART_WORDLENGTH_8B;
- huart1.Init.StopBits = UART_STOPBITS_1;
- huart1.Init.Parity = UART_PARITY_NONE;
- huart1.Init.Mode = UART_MODE_TX_RX;
- huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- huart1.Init.OverSampling = UART_OVERSAMPLING_16;
- if (HAL_UART_Init(&huart1) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN USART1_Init 2 */
- /* USER CODE END USART1_Init 2 */
- }
- /**
- * Enable DMA controller clock
- */
- static void MX_DMA_Init(void)
- {
- /* DMA controller clock enable */
- __HAL_RCC_DMA1_CLK_ENABLE();
- /* DMA interrupt init */
- /* DMA1_Channel1_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
- /* DMA1_Channel4_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(DMA1_Channel4_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(DMA1_Channel4_IRQn);
- /* DMA1_Channel5_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(DMA1_Channel5_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(DMA1_Channel5_IRQn);
- }
- /**
- * @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_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(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET);
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
- /*Configure GPIO pins : PC13 PC14 PC15 */
- GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
- GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
- /*Configure GPIO pins : PA1 PA2 PA3 PA6
- PA8 PA15 */
- GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_6
- |GPIO_PIN_8|GPIO_PIN_15;
- GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- /*Configure GPIO pin : PA7 */
- GPIO_InitStruct.Pin = GPIO_PIN_7;
- 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);
- /*Configure GPIO pin : PB0 */
- GPIO_InitStruct.Pin = GPIO_PIN_0;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- /*Configure GPIO pins : PB2 PB12 PB13 PB3
- PB5 PB8 PB9 */
- GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_3
- |GPIO_PIN_5|GPIO_PIN_8|GPIO_PIN_9;
- GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- /*Configure GPIO pins : PB14 PB15 */
- GPIO_InitStruct.Pin = GPIO_PIN_14|GPIO_PIN_15;
- GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- /* EXTI interrupt init*/
- HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
- }
- /* 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 */
|