Advertisement
Guest User

Untitled

a guest
May 6th, 2020
624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.22 KB | None | 0 0
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  10.   * All rights reserved.</center></h2>
  11.   *
  12.   * This software component is licensed by ST under BSD 3-Clause license,
  13.   * the "License"; You may not use this file except in compliance with the
  14.   * License. You may obtain a copy of the License at:
  15.   *                        opensource.org/licenses/BSD-3-Clause
  16.   *
  17.   ******************************************************************************
  18.   */
  19. /* USER CODE END Header */
  20.  
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "main.h"
  23.  
  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26.  
  27. /* USER CODE END Includes */
  28.  
  29. /* Private typedef -----------------------------------------------------------*/
  30. /* USER CODE BEGIN PTD */
  31.  
  32. /* USER CODE END PTD */
  33.  
  34. /* Private define ------------------------------------------------------------*/
  35. /* USER CODE BEGIN PD */
  36.  
  37. #define HSEM_ID_0 (0U) /* HW semaphore 0*/
  38. /* USER CODE END PD */
  39.  
  40. /* Private macro -------------------------------------------------------------*/
  41. /* USER CODE BEGIN PM */
  42.  
  43. /* USER CODE END PM */
  44.  
  45. /* Private variables ---------------------------------------------------------*/
  46. ADC_HandleTypeDef hadc2;
  47.  
  48. /* USER CODE BEGIN PV */
  49.  
  50. /* USER CODE END PV */
  51.  
  52. /* Private function prototypes -----------------------------------------------*/
  53. void SystemClock_Config(void);
  54. static void MX_GPIO_Init(void);
  55. static void MX_ADC2_Init(void);
  56. /* USER CODE BEGIN PFP */
  57.  
  58. /* USER CODE END PFP */
  59.  
  60. /* Private user code ---------------------------------------------------------*/
  61. /* USER CODE BEGIN 0 */
  62. uint32_t adp, zastava;
  63. /* USER CODE END 0 */
  64.  
  65. /**
  66.   * @brief  The application entry point.
  67.   * @retval int
  68.   */
  69. int main(void)
  70. {
  71.   /* USER CODE BEGIN 1 */
  72.  
  73.   /* USER CODE END 1 */
  74.   /* USER CODE BEGIN Boot_Mode_Sequence_0 */
  75.     int32_t timeout;
  76.   /* USER CODE END Boot_Mode_Sequence_0 */
  77.  
  78. /* USER CODE BEGIN Boot_Mode_Sequence_1 */
  79.   /* Wait until CPU2 boots and enters in stop mode or timeout*/
  80.   timeout = 0xFFFF;
  81.   while((__HAL_RCC_GET_FLAG(RCC_FLAG_D2CKRDY) != RESET) && (timeout-- > 0));
  82.   if ( timeout < 0 )
  83.   {
  84.   Error_Handler();
  85.   }
  86. /* USER CODE END Boot_Mode_Sequence_1 */
  87.   /* MCU Configuration--------------------------------------------------------*/
  88.  
  89.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  90.   HAL_Init();
  91.  
  92.   /* USER CODE BEGIN Init */
  93.  
  94.   /* USER CODE END Init */
  95.  
  96.   /* Configure the system clock */
  97.   SystemClock_Config();
  98. /* USER CODE BEGIN Boot_Mode_Sequence_2 */
  99. /* When system initialization is finished, Cortex-M7 will release Cortex-M4 by means of
  100. HSEM notification */
  101. /*HW semaphore Clock enable*/
  102. __HAL_RCC_HSEM_CLK_ENABLE();
  103. /*Take HSEM */
  104. HAL_HSEM_FastTake(HSEM_ID_0);
  105. /*Release HSEM in order to notify the CPU2(CM4)*/
  106. HAL_HSEM_Release(HSEM_ID_0,0);
  107. /* wait until CPU2 wakes up from stop mode */
  108. timeout = 0xFFFF;
  109. while((__HAL_RCC_GET_FLAG(RCC_FLAG_D2CKRDY) == RESET) && (timeout-- > 0));
  110. if ( timeout < 0 )
  111. {
  112. Error_Handler();
  113. }
  114. /* USER CODE END Boot_Mode_Sequence_2 */
  115.  
  116.   /* USER CODE BEGIN SysInit */
  117.  
  118.   /* USER CODE END SysInit */
  119.  
  120.   /* Initialize all configured peripherals */
  121.   MX_GPIO_Init();
  122.   MX_ADC2_Init();
  123.   /* USER CODE BEGIN 2 */
  124.     HAL_ADC_Stop(&hadc2);
  125.     if(HAL_ADCEx_Calibration_Start(&hadc2, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED) == HAL_OK)
  126.         zastava=5;
  127.     else
  128.         zastava=9;
  129.   /* USER CODE END 2 */
  130.  
  131.   /* Infinite loop */
  132.   /* USER CODE BEGIN WHILE */
  133.   while (1)
  134.   {
  135.     /* USER CODE END WHILE */
  136.         HAL_ADC_Start(&hadc2);
  137.         if(HAL_ADC_PollForConversion(&hadc2, 5) == HAL_OK){
  138.             adp = HAL_ADC_GetValue(&hadc2);
  139.         }
  140.         HAL_ADC_Stop(&hadc2);
  141.         HAL_Delay(50);
  142.     /* USER CODE BEGIN 3 */
  143.   }
  144.   /* USER CODE END 3 */
  145. }
  146.  
  147. /**
  148.   * @brief System Clock Configuration
  149.   * @retval None
  150.   */
  151. void SystemClock_Config(void)
  152. {
  153.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  154.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  155.   RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  156.  
  157.   /** Supply configuration update enable
  158.   */
  159.   HAL_PWREx_ConfigSupply(PWR_DIRECT_SMPS_SUPPLY);
  160.   /** Configure the main internal regulator output voltage
  161.   */
  162.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
  163.  
  164.   while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
  165.   /** Macro to configure the PLL clock source
  166.   */
  167.   __HAL_RCC_PLL_PLLSOURCE_CONFIG(RCC_PLLSOURCE_HSI);
  168.   /** Initializes the CPU, AHB and APB busses clocks
  169.   */
  170.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  171.   RCC_OscInitStruct.HSIState = RCC_HSI_DIV1;
  172.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  173.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  174.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  175.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  176.   {
  177.     Error_Handler();
  178.   }
  179.   /** Initializes the CPU, AHB and APB busses clocks
  180.   */
  181.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  182.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
  183.                               |RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
  184.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  185.   RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
  186.   RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
  187.   RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV1;
  188.   RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
  189.   RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV1;
  190.   RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV1;
  191.  
  192.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  193.   {
  194.     Error_Handler();
  195.   }
  196.   PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  197.   PeriphClkInitStruct.PLL2.PLL2M = 32;
  198.   PeriphClkInitStruct.PLL2.PLL2N = 129;
  199.   PeriphClkInitStruct.PLL2.PLL2P = 2;
  200.   PeriphClkInitStruct.PLL2.PLL2Q = 2;
  201.   PeriphClkInitStruct.PLL2.PLL2R = 2;
  202.   PeriphClkInitStruct.PLL2.PLL2RGE = RCC_PLL2VCIRANGE_1;
  203.   PeriphClkInitStruct.PLL2.PLL2VCOSEL = RCC_PLL2VCOWIDE;
  204.   PeriphClkInitStruct.PLL2.PLL2FRACN = 0;
  205.   PeriphClkInitStruct.AdcClockSelection = RCC_ADCCLKSOURCE_PLL2;
  206.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  207.   {
  208.     Error_Handler();
  209.   }
  210. }
  211.  
  212. /**
  213.   * @brief ADC2 Initialization Function
  214.   * @param None
  215.   * @retval None
  216.   */
  217. static void MX_ADC2_Init(void)
  218. {
  219.  
  220.   /* USER CODE BEGIN ADC2_Init 0 */
  221.  
  222.   /* USER CODE END ADC2_Init 0 */
  223.  
  224.   ADC_ChannelConfTypeDef sConfig = {0};
  225.  
  226.   /* USER CODE BEGIN ADC2_Init 1 */
  227.  
  228.   /* USER CODE END ADC2_Init 1 */
  229.   /** Common config
  230.   */
  231.   hadc2.Instance = ADC2;
  232.   hadc2.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  233.   hadc2.Init.Resolution = ADC_RESOLUTION_10B;
  234.   hadc2.Init.ScanConvMode = ADC_SCAN_DISABLE;
  235.   hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  236.   hadc2.Init.LowPowerAutoWait = DISABLE;
  237.   hadc2.Init.ContinuousConvMode = DISABLE;
  238.   hadc2.Init.NbrOfConversion = 1;
  239.   hadc2.Init.DiscontinuousConvMode = DISABLE;
  240.   hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  241.   hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  242.   hadc2.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR;
  243.   hadc2.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  244.   hadc2.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE;
  245.   hadc2.Init.OversamplingMode = DISABLE;
  246.   if (HAL_ADC_Init(&hadc2) != HAL_OK)
  247.   {
  248.     Error_Handler();
  249.   }
  250.   /** Configure Regular Channel
  251.   */
  252.   sConfig.Channel = ADC_CHANNEL_11;
  253.   sConfig.Rank = ADC_REGULAR_RANK_1;
  254.   sConfig.SamplingTime = ADC_SAMPLETIME_8CYCLES_5;
  255.   sConfig.SingleDiff = ADC_SINGLE_ENDED;
  256.   sConfig.OffsetNumber = ADC_OFFSET_NONE;
  257.   sConfig.Offset = 0;
  258.   if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
  259.   {
  260.     Error_Handler();
  261.   }
  262.   /* USER CODE BEGIN ADC2_Init 2 */
  263.  
  264.   /* USER CODE END ADC2_Init 2 */
  265.  
  266. }
  267.  
  268. /**
  269.   * @brief GPIO Initialization Function
  270.   * @param None
  271.   * @retval None
  272.   */
  273. static void MX_GPIO_Init(void)
  274. {
  275.  
  276.   /* GPIO Ports Clock Enable */
  277.   __HAL_RCC_GPIOC_CLK_ENABLE();
  278.  
  279. }
  280.  
  281. /* USER CODE BEGIN 4 */
  282.  
  283. /* USER CODE END 4 */
  284.  
  285. /**
  286.   * @brief  This function is executed in case of error occurrence.
  287.   * @retval None
  288.   */
  289. void Error_Handler(void)
  290. {
  291.   /* USER CODE BEGIN Error_Handler_Debug */
  292.   /* User can add his own implementation to report the HAL error return state */
  293.  
  294.   /* USER CODE END Error_Handler_Debug */
  295. }
  296.  
  297. #ifdef  USE_FULL_ASSERT
  298. /**
  299.   * @brief  Reports the name of the source file and the source line number
  300.   *         where the assert_param error has occurred.
  301.   * @param  file: pointer to the source file name
  302.   * @param  line: assert_param error line source number
  303.   * @retval None
  304.   */
  305. void assert_failed(uint8_t *file, uint32_t line)
  306. {
  307.   /* USER CODE BEGIN 6 */
  308.   /* User can add his own implementation to report the file name and line number,
  309.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  310.   /* USER CODE END 6 */
  311. }
  312. #endif /* USE_FULL_ASSERT */
  313.  
  314. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement