/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * Copyright (c) 2022 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 "tim.h" #include "usart.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "SEGGER_RTT.h" #include "CBuffer/circular_buffer.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ #define CB_BUFFER_SIZE 32 #define CB_ITEM_SIZE sizeof(uint8_t) /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ CB_TypeDef hcb_rx; CB_TypeDef hcb_tx; uint8_t buffer_rx[CB_BUFFER_SIZE]; uint8_t buffer_tx[CB_BUFFER_SIZE]; uint8_t one_char; uint16_t adc_values[12]; HAL_StatusTypeDef ret; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_NVIC_Init(void); /* USER CODE BEGIN PFP */ void Start_pwm(TIM_HandleTypeDef *htim, uint8_t no_channels); /* 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_DMA_Init(); MX_ADC2_Init(); MX_USART2_UART_Init(); MX_TIM3_Init(); MX_TIM15_Init(); /* Initialize interrupts */ MX_NVIC_Init(); /* USER CODE BEGIN 2 */ // SEGGER_RTT_Init(); // SEGGER_RTT_ConfigUpBuffer(0, NULL, NULL, 0, SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL); SEGGER_RTT_WriteString(0, "Start calibration adc\r\n"); ret = HAL_ADCEx_Calibration_Start(&hadc2, ADC_SINGLE_ENDED); if (ret != HAL_OK) { Error_Handler(); } SEGGER_RTT_WriteString(0, "Start adc and dma\r\n"); ret = HAL_ADC_Start_DMA(&hadc2, (uint32_t *)&adc_values, 12); if (ret != HAL_OK) { Error_Handler(); } SEGGER_RTT_WriteString(0, "Start timer 3 pwm\r\n"); Start_pwm(&htim3, 4); SEGGER_RTT_WriteString(0, "Start timer 15 pwm\r\n"); Start_pwm(&htim15, 2); // HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1); // HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2); // HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3); // HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4); // HAL_TIM_PWM_Start(&htim15, TIM_CHANNEL_1); // HAL_TIM_PWM_Start(&htim15, TIM_CHANNEL_2); // // 0 - 199 htim3.Instance->CCR1 = 19; // PC6 htim3.Instance->CCR2 = 49; // PC7 htim3.Instance->CCR3 = 79; // PB0 htim3.Instance->CCR4 = 109; // PB1 htim15.Instance->CCR1 = 139; // PB14 htim15.Instance->CCR2 = 169; // PB15 cb_init(&hcb_rx, buffer_rx, CB_BUFFER_SIZE, CB_ITEM_SIZE); cb_init(&hcb_tx, buffer_tx, CB_BUFFER_SIZE, CB_ITEM_SIZE); HAL_UART_Receive_IT(&huart2, &one_char, 1); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { SEGGER_RTT_WriteString(0, "\r\n *** while loop ***\r\n"); HAL_Delay(3000); /* 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}; RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; 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_HSI; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1; 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_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2 | RCC_PERIPHCLK_TIM15 | RCC_PERIPHCLK_ADC12 | RCC_PERIPHCLK_TIM34; PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1; PeriphClkInit.Adc12ClockSelection = RCC_ADC12PLLCLK_DIV1; PeriphClkInit.Tim15ClockSelection = RCC_TIM15CLK_HCLK; PeriphClkInit.Tim34ClockSelection = RCC_TIM34CLK_HCLK; if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) { Error_Handler(); } } /** * @brief NVIC Configuration. * @retval None */ static void MX_NVIC_Init(void) { /* DMA2_Channel1_IRQn interrupt configuration */ HAL_NVIC_SetPriority(DMA2_Channel1_IRQn, 0, 0); HAL_NVIC_EnableIRQ(DMA2_Channel1_IRQn); } /* USER CODE BEGIN 4 */ void Start_pwm(TIM_HandleTypeDef *htim, uint8_t no_channels) { uint32_t all_channels[] = {TIM_CHANNEL_1, TIM_CHANNEL_2, TIM_CHANNEL_3, TIM_CHANNEL_4}; for (uint8_t i = 0; i < no_channels; i++) { ret = HAL_TIM_PWM_Start(htim, all_channels[i]); if (ret != HAL_OK) { Error_Handler(); } } } void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { if (huart->Instance == USART2) { switch (one_char) { case 0: break; // NULL (ignoruje) case 10: break; // LF (ignoruje) case 13: one_char = 0; default: cb_push_back(&hcb_rx, &one_char); } HAL_UART_Receive_IT(&huart2, &one_char, 1); } } /* 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 */