tdse-tp4_04-led_ldr

FIUBA - Electrónica - Taller de Sistemas Embebidos - Trabajo Práctico N°: 4 - LED & LDR
Index Commits Files Refs README
Core/Src/main.c (9878B)
   1 /* USER CODE BEGIN Header */
   2 /**
   3   ******************************************************************************
   4   * @file           : main.c
   5   * @brief          : Main program body
   6   ******************************************************************************
   7   * @attention
   8   *
   9   * Copyright (c) 2024 STMicroelectronics.
  10   * All rights reserved.
  11   *
  12   * This software is licensed under terms that can be found in the LICENSE file
  13   * in the root directory of this software component.
  14   * If no LICENSE file comes with this software, it is provided AS-IS.
  15   *
  16   ******************************************************************************
  17   */
  18 /* USER CODE END Header */
  19 /* Includes ------------------------------------------------------------------*/
  20 #include "main.h"
  21 
  22 /* Private includes ----------------------------------------------------------*/
  23 /* USER CODE BEGIN Includes */
  24 #include "app.h"
  25 /* USER CODE END Includes */
  26 
  27 /* Private typedef -----------------------------------------------------------*/
  28 /* USER CODE BEGIN PTD */
  29 
  30 /* USER CODE END PTD */
  31 
  32 /* Private define ------------------------------------------------------------*/
  33 /* USER CODE BEGIN PD */
  34 
  35 /* USER CODE END PD */
  36 
  37 /* Private macro -------------------------------------------------------------*/
  38 /* USER CODE BEGIN PM */
  39 
  40 /* USER CODE END PM */
  41 
  42 /* Private variables ---------------------------------------------------------*/
  43 ADC_HandleTypeDef hadc1;
  44 
  45 TIM_HandleTypeDef htim3;
  46 
  47 UART_HandleTypeDef huart2;
  48 
  49 /* USER CODE BEGIN PV */
  50 
  51 /* USER CODE END PV */
  52 
  53 /* Private function prototypes -----------------------------------------------*/
  54 void SystemClock_Config(void);
  55 static void MX_GPIO_Init(void);
  56 static void MX_USART2_UART_Init(void);
  57 static void MX_ADC1_Init(void);
  58 static void MX_TIM3_Init(void);
  59 /* USER CODE BEGIN PFP */
  60 // extern void initialise_monitor_handles(void);
  61 /* USER CODE END PFP */
  62 
  63 /* Private user code ---------------------------------------------------------*/
  64 /* USER CODE BEGIN 0 */
  65 
  66 /* USER CODE END 0 */
  67 
  68 /**
  69   * @brief  The application entry point.
  70   * @retval int
  71   */
  72 int main(void)
  73 {
  74 
  75   /* USER CODE BEGIN 1 */
  76   // initialise_monitor_handles();
  77   /* USER CODE END 1 */
  78 
  79   /* MCU Configuration--------------------------------------------------------*/
  80 
  81   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  82   HAL_Init();
  83 
  84   /* USER CODE BEGIN Init */
  85 
  86   /* USER CODE END Init */
  87 
  88   /* Configure the system clock */
  89   SystemClock_Config();
  90 
  91   /* USER CODE BEGIN SysInit */
  92 
  93   /* USER CODE END SysInit */
  94 
  95   /* Initialize all configured peripherals */
  96   MX_GPIO_Init();
  97   MX_USART2_UART_Init();
  98   MX_ADC1_Init();
  99   MX_TIM3_Init();
 100   /* USER CODE BEGIN 2 */
 101   app_init();
 102   /* USER CODE END 2 */
 103 
 104   /* Infinite loop */
 105   /* USER CODE BEGIN WHILE */
 106   while (1)
 107   {
 108     /* USER CODE END WHILE */
 109 
 110     /* USER CODE BEGIN 3 */
 111     app_update();
 112   }
 113   /* USER CODE END 3 */
 114 }
 115 
 116 /**
 117   * @brief System Clock Configuration
 118   * @retval None
 119   */
 120 void SystemClock_Config(void)
 121 {
 122   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
 123   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
 124   RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
 125 
 126   /** Initializes the RCC Oscillators according to the specified parameters
 127   * in the RCC_OscInitTypeDef structure.
 128   */
 129   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
 130   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
 131   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
 132   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
 133   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
 134   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL2;
 135   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
 136   {
 137     Error_Handler();
 138   }
 139 
 140   /** Initializes the CPU, AHB and APB buses clocks
 141   */
 142   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
 143                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
 144   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
 145   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
 146   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
 147   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
 148 
 149   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
 150   {
 151     Error_Handler();
 152   }
 153   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
 154   PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV2;
 155   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
 156   {
 157     Error_Handler();
 158   }
 159 }
 160 
 161 /**
 162   * @brief ADC1 Initialization Function
 163   * @param None
 164   * @retval None
 165   */
 166 static void MX_ADC1_Init(void)
 167 {
 168 
 169   /* USER CODE BEGIN ADC1_Init 0 */
 170 
 171   /* USER CODE END ADC1_Init 0 */
 172 
 173   ADC_ChannelConfTypeDef sConfig = {0};
 174 
 175   /* USER CODE BEGIN ADC1_Init 1 */
 176 
 177   /* USER CODE END ADC1_Init 1 */
 178 
 179   /** Common config
 180   */
 181   hadc1.Instance = ADC1;
 182   hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
 183   hadc1.Init.ContinuousConvMode = DISABLE;
 184   hadc1.Init.DiscontinuousConvMode = DISABLE;
 185   hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
 186   hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
 187   hadc1.Init.NbrOfConversion = 1;
 188   if (HAL_ADC_Init(&hadc1) != HAL_OK)
 189   {
 190     Error_Handler();
 191   }
 192 
 193   /** Configure Regular Channel
 194   */
 195   sConfig.Channel = ADC_CHANNEL_0;
 196   sConfig.Rank = ADC_REGULAR_RANK_1;
 197   sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
 198   if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
 199   {
 200     Error_Handler();
 201   }
 202   /* USER CODE BEGIN ADC1_Init 2 */
 203 
 204   /* USER CODE END ADC1_Init 2 */
 205 
 206 }
 207 
 208 /**
 209   * @brief TIM3 Initialization Function
 210   * @param None
 211   * @retval None
 212   */
 213 static void MX_TIM3_Init(void)
 214 {
 215 
 216   /* USER CODE BEGIN TIM3_Init 0 */
 217 
 218   /* USER CODE END TIM3_Init 0 */
 219 
 220   TIM_MasterConfigTypeDef sMasterConfig = {0};
 221   TIM_OC_InitTypeDef sConfigOC = {0};
 222 
 223   /* USER CODE BEGIN TIM3_Init 1 */
 224 
 225   /* USER CODE END TIM3_Init 1 */
 226   htim3.Instance = TIM3;
 227   htim3.Init.Prescaler = 0;
 228   htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
 229   htim3.Init.Period = 65535;
 230   htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
 231   htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
 232   if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
 233   {
 234     Error_Handler();
 235   }
 236   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
 237   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
 238   if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
 239   {
 240     Error_Handler();
 241   }
 242   sConfigOC.OCMode = TIM_OCMODE_PWM1;
 243   sConfigOC.Pulse = 0;
 244   sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
 245   sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
 246   if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
 247   {
 248     Error_Handler();
 249   }
 250   /* USER CODE BEGIN TIM3_Init 2 */
 251 
 252   /* USER CODE END TIM3_Init 2 */
 253   HAL_TIM_MspPostInit(&htim3);
 254 
 255 }
 256 
 257 /**
 258   * @brief USART2 Initialization Function
 259   * @param None
 260   * @retval None
 261   */
 262 static void MX_USART2_UART_Init(void)
 263 {
 264 
 265   /* USER CODE BEGIN USART2_Init 0 */
 266 
 267   /* USER CODE END USART2_Init 0 */
 268 
 269   /* USER CODE BEGIN USART2_Init 1 */
 270 
 271   /* USER CODE END USART2_Init 1 */
 272   huart2.Instance = USART2;
 273   huart2.Init.BaudRate = 115200;
 274   huart2.Init.WordLength = UART_WORDLENGTH_8B;
 275   huart2.Init.StopBits = UART_STOPBITS_1;
 276   huart2.Init.Parity = UART_PARITY_NONE;
 277   huart2.Init.Mode = UART_MODE_TX_RX;
 278   huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
 279   huart2.Init.OverSampling = UART_OVERSAMPLING_16;
 280   if (HAL_UART_Init(&huart2) != HAL_OK)
 281   {
 282     Error_Handler();
 283   }
 284   /* USER CODE BEGIN USART2_Init 2 */
 285 
 286   /* USER CODE END USART2_Init 2 */
 287 
 288 }
 289 
 290 /**
 291   * @brief GPIO Initialization Function
 292   * @param None
 293   * @retval None
 294   */
 295 static void MX_GPIO_Init(void)
 296 {
 297   GPIO_InitTypeDef GPIO_InitStruct = {0};
 298 /* USER CODE BEGIN MX_GPIO_Init_1 */
 299 /* USER CODE END MX_GPIO_Init_1 */
 300 
 301   /* GPIO Ports Clock Enable */
 302   __HAL_RCC_GPIOC_CLK_ENABLE();
 303   __HAL_RCC_GPIOD_CLK_ENABLE();
 304   __HAL_RCC_GPIOA_CLK_ENABLE();
 305   __HAL_RCC_GPIOB_CLK_ENABLE();
 306 
 307   /*Configure GPIO pin Output Level */
 308   HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
 309 
 310   /*Configure GPIO pin : B1_Pin */
 311   GPIO_InitStruct.Pin = B1_Pin;
 312   GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
 313   GPIO_InitStruct.Pull = GPIO_NOPULL;
 314   HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
 315 
 316   /*Configure GPIO pin : LD2_Pin */
 317   GPIO_InitStruct.Pin = LD2_Pin;
 318   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
 319   GPIO_InitStruct.Pull = GPIO_NOPULL;
 320   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 321   HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
 322 
 323   /* EXTI interrupt init*/
 324   HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
 325   HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
 326 
 327 /* USER CODE BEGIN MX_GPIO_Init_2 */
 328 /* USER CODE END MX_GPIO_Init_2 */
 329 }
 330 
 331 /* USER CODE BEGIN 4 */
 332 
 333 /* USER CODE END 4 */
 334 
 335 /**
 336   * @brief  This function is executed in case of error occurrence.
 337   * @retval None
 338   */
 339 void Error_Handler(void)
 340 {
 341   /* USER CODE BEGIN Error_Handler_Debug */
 342   /* User can add his own implementation to report the HAL error return state */
 343   __disable_irq();
 344   while (1)
 345   {
 346   }
 347   /* USER CODE END Error_Handler_Debug */
 348 }
 349 
 350 #ifdef  USE_FULL_ASSERT
 351 /**
 352   * @brief  Reports the name of the source file and the source line number
 353   *         where the assert_param error has occurred.
 354   * @param  file: pointer to the source file name
 355   * @param  line: assert_param error line source number
 356   * @retval None
 357   */
 358 void assert_failed(uint8_t *file, uint32_t line)
 359 {
 360   /* USER CODE BEGIN 6 */
 361   /* User can add his own implementation to report the file name and line number,
 362      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
 363   /* USER CODE END 6 */
 364 }
 365 #endif /* USE_FULL_ASSERT */