1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006 |
- /* 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 "menu.h"
- #include "ssd1306_tests.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 htim3;
- TIM_HandleTypeDef htim4;
- TIM_HandleTypeDef htim6;
- DMA_HandleTypeDef hdma_tim3_ch4_up;
- 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;
- uint32_t Wave_LUT[] = {
- 0x80,0x83,0x86,0x89,0x8c,0x8f,0x92,0x95,
- 0x98,0x9b,0x9e,0xa2,0xa5,0xa7,0xaa,0xad,
- 0xb0,0xb3,0xb6,0xb9,0xbc,0xbe,0xc1,0xc4,
- 0xc6,0xc9,0xcb,0xce,0xd0,0xd3,0xd5,0xd7,
- 0xda,0xdc,0xde,0xe0,0xe2,0xe4,0xe6,0xe8,
- 0xea,0xeb,0xed,0xee,0xf0,0xf1,0xf3,0xf4,
- 0xf5,0xf6,0xf8,0xf9,0xfa,0xfa,0xfb,0xfc,
- 0xfd,0xfd,0xfe,0xfe,0xfe,0xff,0xff,0xff,
- 0xff,0xff,0xff,0xff,0xfe,0xfe,0xfe,0xfd,
- 0xfd,0xfc,0xfb,0xfa,0xfa,0xf9,0xf8,0xf6,
- 0xf5,0xf4,0xf3,0xf1,0xf0,0xee,0xed,0xeb,
- 0xea,0xe8,0xe6,0xe4,0xe2,0xe0,0xde,0xdc,
- 0xda,0xd7,0xd5,0xd3,0xd0,0xce,0xcb,0xc9,
- 0xc6,0xc4,0xc1,0xbe,0xbc,0xb9,0xb6,0xb3,
- 0xb0,0xad,0xaa,0xa7,0xa5,0xa2,0x9e,0x9b,
- 0x98,0x95,0x92,0x8f,0x8c,0x89,0x86,0x83,
- 0x80,0x7c,0x79,0x76,0x73,0x70,0x6d,0x6a,
- 0x67,0x64,0x61,0x5d,0x5a,0x58,0x55,0x52,
- 0x4f,0x4c,0x49,0x46,0x43,0x41,0x3e,0x3b,
- 0x39,0x36,0x34,0x31,0x2f,0x2c,0x2a,0x28,
- 0x25,0x23,0x21,0x1f,0x1d,0x1b,0x19,0x17,
- 0x15,0x14,0x12,0x11,0xf,0xe,0xc,0xb,
- 0xa,0x9,0x7,0x6,0x5,0x5,0x4,0x3,
- 0x2,0x2,0x1,0x1,0x1,0x0,0x0,0x0,
- 0x0,0x0,0x0,0x0,0x1,0x1,0x1,0x2,
- 0x2,0x3,0x4,0x5,0x5,0x6,0x7,0x9,
- 0xa,0xb,0xc,0xe,0xf,0x11,0x12,0x14,
- 0x15,0x17,0x19,0x1b,0x1d,0x1f,0x21,0x23,
- 0x25,0x28,0x2a,0x2c,0x2f,0x31,0x34,0x36,
- 0x39,0x3b,0x3e,0x41,0x43,0x46,0x49,0x4c,
- 0x4f,0x52,0x55,0x58,0x5a,0x5d,0x61,0x64,
- 0x67,0x6a,0x6d,0x70,0x73,0x76,0x79,0x7c,
- };
- /* 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_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_PWM_Start_DMA(&htim3, TIM_CHANNEL_4, Wave_LUT, sizeof(Wave_LUT) / 4);
- //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_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];
- uint8_t imgbuffer[172];
- 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;
- //ssd1306_SetContrast(60);
- //ssd1306_TestAll();
- Beep();
- while (1)
- {
- if(window == 1)
- {
- if(COMM_Receive(imgbuffer, COMM_USB, 200) == COMM_OK)
- {
- ssd1306_Fill(Black);
- for(int y = 0; y < 32; y++)
- {
- for(int x = 0; x < 43; x++)
- {
- uint16_t pos = (y * 43) + x;
- if(imgbuffer[pos / 8] & (1 << (pos % 8)))
- ssd1306_DrawPixel(x + 43, y, White);
- }
- }
- ssd1306_UpdateScreen();
- }
- }
- if(Flags.Exti)
- {
- uint16_t btn = Flags.Exti;
- Flags.Exti = 0;
- if(btn == GPIO_PIN_15)
- {
- window ^= 1;
- ssd1306_Fill(Black);
- ssd1306_UpdateScreen();
- }
- }
- if(Flags.Tim6)
- {
- int rcvlen = CDC_ReadData(imgbuffer, sizeof(imgbuffer));
- if(rcvlen > 0)
- UARTIO_Transmit(imgbuffer, rcvlen);
- rcvlen = UARTIO_Receive(imgbuffer, sizeof(imgbuffer));
- if(rcvlen > 0)
- CDC_WriteData(imgbuffer, rcvlen);
- 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;
- }
- if(window == 0)
- {
- ssd1306_DrawRectangle(0, 0, SSD1306_WIDTH - 1, SSD1306_HEIGHT - 1, White);
- ssd1306_SetCursor(1, 7);
- sprintf(tempstr, "%5.2f uSv/h", (float)pps_avg / 100.0);
- ssd1306_WriteString(tempstr, Font_11x18, White);
- ssd1306_UpdateScreen();
- }
- else
- {
- //ssd1306_SetCursor(2, 2);
- //ssd1306_WriteString(tempstr, Font_7x10, White);
- }
- 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);
- //if(window == 0)
- // 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 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};
- TIM_OC_InitTypeDef sConfigOC = {0};
- /* USER CODE BEGIN TIM3_Init 1 */
- /* USER CODE END TIM3_Init 1 */
- htim3.Instance = TIM3;
- htim3.Init.Prescaler = 0;
- htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
- htim3.Init.Period = 255;
- 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();
- }
- 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_Channel3_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(DMA1_Channel3_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(DMA1_Channel3_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();
- ssd1306_Fill(Black);
- ssd1306_SetCursor(1, 1);
- ssd1306_WriteString("HAL Fault", Font_11x18, White);
- ssd1306_UpdateScreen();
- 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 */
|