/* 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 "adc.h" #include "dma.h" #include "i2c.h" #include "tim.h" #include "usart.h" #include "usb_device.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include #include "usbd_cdc_if.h" #include "version.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ typedef struct EMA_Filter { float filtered_sample; uint32_t last_sample; float coeff; } EMA_Filter_t; /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ #define NUM_SLIDERS (6u) #define SLIDER_COEFF 0.30f #define MAX_PWM_VALUE (10000u) #define PWM_LIMIT (0.5f) #define BTN_POLL_TIME (100u) #define UART_LOOP_TIME (1000u) /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ uint16_t m_adc1[4]; uint16_t m_adc2[2]; bool m_adc1_filtered_ready = false; bool m_adc2_filtered_ready = false; EMA_Filter_t m_slider_filters[NUM_SLIDERS]; float m_sliders[NUM_SLIDERS]; uint8_t m_buttons = 0; char buf[512] = {0}; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); /* USER CODE BEGIN PFP */ void filter_adc1(void); void filter_adc2(void); void set_pwm_outputs(void); float map(float x, float in_min, float in_max, float out_min, float out_max); float map_clamp(float x, float in_min, float in_max, float out_min, float out_max); #ifdef __GNUC__ /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf set to 'Yes') calls __io_putchar() */ #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) #else #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) #endif /* __GNUC__ */ /* 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 */ uint32_t prev_uart_tick = UART_LOOP_TIME; uint32_t prev_btn_time = BTN_POLL_TIME; /* 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_DMA_Init(); MX_I2C1_Init(); MX_TIM1_Init(); MX_TIM2_Init(); MX_TIM3_Init(); MX_ADC1_Init(); MX_ADC2_Init(); MX_USART1_UART_Init(); MX_USB_Device_Init(); /* USER CODE BEGIN 2 */ // int len = snprintf(buf, 128, "Version: %s\r\n", VERSION_STR); // CDC_Transmit_FS((uint8_t *)buf, len); // Initialize EMA filters for (uint32_t i = 0; i < NUM_SLIDERS; i++) { m_slider_filters[i].last_sample = 0; m_slider_filters[i].coeff = SLIDER_COEFF; } // ADC setup // Calibrate HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED); HAL_ADCEx_Calibration_Start(&hadc2, ADC_SINGLE_ENDED); HAL_ADC_Start_DMA(&hadc1, (uint32_t *)m_adc1, 4); HAL_ADC_Start_DMA(&hadc2, (uint32_t *)m_adc2, 2); // PWM Setup htim1.Instance->CCR1 = 0; htim1.Instance->CCR2 = 0; htim1.Instance->CCR3 = 0; htim2.Instance->CCR1 = 0; htim2.Instance->CCR2 = 0; htim2.Instance->CCR3 = 0; HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1); HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2); HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3); HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1); HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2); HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3); HAL_TIM_Base_Start(&htim3); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { if ((m_adc1_filtered_ready) == true && (m_adc2_filtered_ready == true)) { // Map ADC value to PWM value for (uint32_t i = 0; i < NUM_SLIDERS; i++) { m_sliders[i] = map_clamp(m_slider_filters[i].filtered_sample, 100.0f, 3900.0f, 0.0f, 100.0f); } // Channel 1 is master, Channels 2-6 are slaves for (uint32_t j = 1; j < NUM_SLIDERS; j++) { m_sliders[j] = map_clamp(m_sliders[j], 0.0f, 100.0f, 0.0f, m_sliders[0]); } set_pwm_outputs(); m_adc1_filtered_ready = false; m_adc2_filtered_ready = false; } // if ((HAL_GetTick() - prev_uart_tick) >= UART_LOOP_TIME) // { // prev_uart_tick = HAL_GetTick(); // int len = snprintf(buf, 512, "Master\tCh1\tCh2\tCh3\tCh4\t\r\n"); // len += snprintf((buf + len), (512 - len), "%03.1f\t%03.1f\t%03.1f\t%03.1f\t%03.1f\t\r\n", // m_sliders[0], m_sliders[1], m_sliders[2], m_sliders[3], m_sliders[4]); // len += snprintf((buf + len), (512 - len), "Version: %s\r\n", VERSION_STR); // CDC_Transmit_FS((uint8_t *)buf, len); // } if ((HAL_GetTick() - prev_btn_time) >= BTN_POLL_TIME) { prev_btn_time = HAL_GetTick(); m_buttons |= (m_buttons & ~0x01) | HAL_GPIO_ReadPin(Btn_0_GPIO_Port, Btn_0_Pin); m_buttons |= (m_buttons & ~0x02) | HAL_GPIO_ReadPin(Btn_1_GPIO_Port, Btn_1_Pin) << 1; m_buttons |= (m_buttons & ~0x04) | HAL_GPIO_ReadPin(Btn_2_GPIO_Port, Btn_2_Pin) << 2; m_buttons |= (m_buttons & ~0x08) | HAL_GPIO_ReadPin(Btn_3_GPIO_Port, Btn_3_Pin) << 3; m_buttons |= (m_buttons & ~0x10) | HAL_GPIO_ReadPin(Btn_4_GPIO_Port, Btn_4_Pin) << 4; m_buttons |= (m_buttons & ~0x20) | HAL_GPIO_ReadPin(Btn_5_GPIO_Port, Btn_5_Pin) << 5; m_buttons |= (m_buttons & ~0x40) | HAL_GPIO_ReadPin(Btn_6_GPIO_Port, Btn_6_Pin) << 6; m_buttons |= (m_buttons & ~0x80) | HAL_GPIO_ReadPin(Btn_7_GPIO_Port, Btn_7_Pin) << 7; } /* 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_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST); /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48 | RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSI48State = RCC_HSI48_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV4; RCC_OscInitStruct.PLL.PLLN = 85; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV4; RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2; 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_4) != HAL_OK) { Error_Handler(); } } /* USER CODE BEGIN 4 */ void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc) { if (hadc->Instance == ADC1) { filter_adc1(); } else if (hadc->Instance == ADC2) { filter_adc2(); } } /* * */ void filter_adc1(void) { for (uint32_t i = 0; i < 4; i++) { // EMA m_slider_filters[i].filtered_sample = (m_adc1[i] * m_slider_filters[i].coeff) + ((1 - m_slider_filters[i].coeff) * m_slider_filters[i].last_sample); // Store current sample for next time m_slider_filters[i].last_sample = m_adc1[i]; m_adc1_filtered_ready = true; } } /* * */ void filter_adc2(void) { for (uint32_t i = 4; i < NUM_SLIDERS; i++) { // EMA m_slider_filters[i].filtered_sample = (m_adc2[i - 4] * m_slider_filters[i].coeff) + ((1 - m_slider_filters[i].coeff) * m_slider_filters[i].last_sample); // Store current sample for next time m_slider_filters[i].last_sample = m_adc2[i - 4]; m_adc2_filtered_ready = true; } } /* * */ void set_pwm_outputs(void) { float ch1 = map_clamp(m_sliders[1], 0.0f, 100.0f, 0, (MAX_PWM_VALUE * PWM_LIMIT)); float ch2 = map_clamp(m_sliders[2], 0.0f, 100.0f, 0, (MAX_PWM_VALUE * PWM_LIMIT)); float ch3 = map_clamp(m_sliders[3], 0.0f, 100.0f, 0, (MAX_PWM_VALUE * PWM_LIMIT)); float ch4 = map_clamp(m_sliders[4], 0.0f, 100.0f, 0, (MAX_PWM_VALUE * PWM_LIMIT)); htim1.Instance->CCR1 = (uint32_t)(ch1); htim1.Instance->CCR2 = (uint32_t)(ch2); htim1.Instance->CCR3 = (uint32_t)(ch3); htim2.Instance->CCR1 = (uint32_t)(ch4); // htim2.Instance->CCR2 = (uint32_t)(MAX_PWM_VALUE * m_sliders[5]); // htim2.Instance->CCR3 = (uint32_t)(MAX_PWM_VALUE * m_sliders[5]); } /* * */ float map(float x, float in_min, float in_max, float out_min, float out_max) { return ((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min); } /* * */ float map_clamp(float x, float in_min, float in_max, float out_min, float out_max) { float y = (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; if (out_min > y) y = out_min; else if (out_max < y) y = out_max; return (y); } /** * @brief Retargets the C library printf function to the USART. * @param None * @retval None */ PUTCHAR_PROTOTYPE { /* Place your implementation of fputc here */ /* e.g. write a character to the USART1 and Loop until the end of transmission */ HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF); return ch; } /* 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 */