Challenge

Project for Driver Development and Resource Sharing Implementation

 
 
Name- Ashutosh Singh
Batch - B
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.cpp
  * @brief          : This is the project for experiencing the RTOS Task
  *                   Synchronization for reading the Analog channel of ADC and
  *                   Printing the ADV Value on LCD through I2C Serial protocol.
  * @author         : ashutosh.singh
  * @created on     : sep1, 2023.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 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
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "main.h"
#include "LiquidCrystal_I2C.h"
/* USER CODE END Includes */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
#define I2C_ADDR    0x27
#define LCD_COLUMNS 20
#define LCD_LINES   4
/* USER CODE END PM */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
I2C_HandleTypeDef hi2c1;
UART_HandleTypeDef huart2;
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES); /* Object lcd is created */
ADC_HandleTypeDef hadc1;  /* Create Adc handle */
/* USER CODE END PD */
/*
 *  Declare a mutex Semaphore Handle which we will use to manage the ADC Module and LCD Module.
 *  It will be used to ensure only only one Task is accessing this resource at any time.
 */
SemaphoreHandle_t xAdcReadSemaphore;
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
uint32_t g_Adc_Ch0_Value = 0;
uint32_t g_Adc_Ch1_Value = 0;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_ADC1_Init(void);
/*Task Declaration */
void TaskAnalogRead_ch0( void *pvParameters );
void TaskAnalogRead_ch1( void *pvParameters );
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint32_t HAL_ADC_ReadChannel( long unsigned int channel)
{
    uint32_t value = 0;
    ADC_ChannelConfTypeDef sConfig = {0};
    /*
    * Configure Regular Channel
    */
    sConfig.Channel = channel;
    sConfig.Rank = ADC_REGULAR_RANK_1;
    sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;
    if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
    {
      Error_Handler();
    }
    HAL_ADC_Start(&hadc1);
    HAL_ADC_PollForConversion(&hadc1, 100);
    value=HAL_ADC_GetValue(&hadc1);
    HAL_ADC_Stop(&hadc1);
    return value;
}
void HAL_LcdPrintData( void )
{
    lcd.clear();
    lcd.setCursor(1, 0);
    lcd.print("skill lync project");
    lcd.setCursor(0, 2);
    lcd.print("CH0_AdcVal:");
    lcd.print(g_Adc_Ch0_Value);
    lcd.setCursor(0, 3);
    lcd.print("CH1_AdcVal:");
    lcd.print(g_Adc_Ch1_Value);
}
/* USER CODE END 0 */




/*
 * Task to read channel0 of ADC and convert analog value into discrete digital value
 */
void TaskAnalogRead_ch0( void *pvParameters __attribute__((unused)) )
{
  for (;;)  /* A Task shall never return or exit. */
  {
    /*
    * See if we can obtain or "Take" the xAdcReadSemaphore Semaphore.
    * If the semaphore is not available, wait 5 ticks of the Scheduler to see if it becomes free.
    */
    if ( xSemaphoreTake( xAdcReadSemaphore, ( TickType_t ) 5 ) == pdTRUE )
    {
      /*
       * We were able to obtain or "Take" the semaphore and can now access the shared resource.
       * We want to have the xAdcReadSemaphore Module for us alone,
       * so we don't want it getting stolen during the middle of a conversion.
       * print out the state of the button:
       */
      g_Adc_Ch0_Value = HAL_ADC_ReadChannel( ADC_CHANNEL_0 );
     
      HAL_LcdPrintData();
      if ( ( xAdcReadSemaphore ) != NULL )
        xSemaphoreGive( xAdcReadSemaphore ); /* Now free or "Give" the Serial Port for others. */
    }
    vTaskDelay(1000);  // one tick delay (15ms) in between reads for stability
  }
}
/*
 * Task to read channel1 of ADC and convert analog value into discrete digital value
 */
void TaskAnalogRead_ch1( void *pvParameters __attribute__((unused)) )
{
  for (;;)  /* A Task shall never return or exit. */
  {
    /*
    * See if we can obtain or "Take" the xAdcReadSemaphore Semaphore.
    * If the semaphore is not available, wait 5 ticks of the Scheduler to see if it becomes free.
    */
    if ( xSemaphoreTake( xAdcReadSemaphore, ( TickType_t ) 5 ) == pdTRUE )
    {
      /*
       * We were able to obtain or "Take" the semaphore and can now access the shared resource.
       * We want to have the xAdcReadSemaphore Module for us alone,
       * so we don't want it getting stolen during the middle of a conversion.
       * print out the state of the button:
       */
      g_Adc_Ch1_Value = HAL_ADC_ReadChannel( ADC_CHANNEL_1 );
     
      HAL_LcdPrintData();
     
      if ( ( xAdcReadSemaphore ) != NULL )
        xSemaphoreGive( xAdcReadSemaphore ); // Now free or "Give" the Serial Port for others.
    }
    vTaskDelay(1000);  // one tick delay (15ms) in between reads for stability
  }
}
/*  
 *  The setup function runs once when you press reset or power the board
 */
void setup() {
    /* USER CODE BEGIN 2 */
    lcd.init();
    lcd.backlight();
    HAL_Delay(1000);
  /*
   *  AdcReadSemaphore are useful to stop a Task proceeding, where it should be paused to wait,
   *  because it is sharing a resource, such as ADC Module.
   *  Semaphores should only be used whilst the scheduler is running, but we can set it up here.
   */
  if ( xAdcReadSemaphore == NULL )  /* Check to confirm that the xAdcReadSemaphore has not already been created. */
  {
    xAdcReadSemaphore = xSemaphoreCreateMutex();  /* Create a mutex semaphore we will use to manage the Adc Module */
    if ( ( xAdcReadSemaphore ) != NULL )
      xSemaphoreGive( ( xAdcReadSemaphore ) );  /* Make the Adc Module available for use, by "Giving" the Semaphore. */
  }
  /* Now set up two Tasks to run independently. */
  xTaskCreate(
    TaskAnalogRead_ch0
    ,  (const portCHAR *)"TaskAnalogRead_ch0"  /* A name just for humans  */
    ,  128  /* This stack size can be checked & adjusted by reading the Stack Highwater */
    ,  NULL
    ,  1  /* Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest. */
    ,  NULL );
  xTaskCreate(
    TaskAnalogRead_ch1
    ,  (const portCHAR *) "TaskAnalogRead_ch1"
    ,  128  /* Stack size */
    ,  NULL
    ,  1  /* Priority */
    ,  NULL );
}
/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
 
  /* USER CODE BEGIN 1 */
  /* MCU Configuration--------------------------------------------------------*/
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  /* USER CODE BEGIN Init */
  HAL_Init();
  /* USER CODE END Init */
  /* Configure the system clock */
  /* USER CODE BEGIN SysInit */
  SystemClock_Config();
  /* USER CODE END SysInit */
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_I2C1_Init();
  MX_USART2_UART_Init();
  MX_ADC1_Init();  
 
  setup();
  vTaskStartScheduler();
  while(1);
}
void loop()
{
  // Empty. Things are done in Tasks.
}
/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {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.HSIDiv = RCC_HSI_DIV1;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  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_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  {
    Error_Handler();
  }
}
/**
  * @brief I2C1 Initialization Function
  * @param None
  * @retval None
  */
static void MX_I2C1_Init(void)
{
  /* USER CODE BEGIN I2C1_Init 0 */
  hi2c1.Instance = I2C1;
  hi2c1.Init.Timing = 0x20303E5D;
  hi2c1.Init.OwnAddress1 = 0;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c1.Init.OwnAddress2 = 0;
  hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure Analogue filter
  */
  if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure Digital filter
  */
  if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE END I2C1_Init 0 */
}
/**
  * @brief USART2 Initialization Function
  * @param None
  * @retval None
  */
static void MX_USART2_UART_Init(void)
{
  /* USER CODE BEGIN USART2_Init 0 */
  huart2.Instance = USART2;
  huart2.Init.BaudRate = 115200;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE END USART2_Init 0 */
}
/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOF_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(Led_GPIO_Port, Led_Pin, GPIO_PIN_SET);
  /*Configure GPIO pin : User_Button_Pin */
  GPIO_InitStruct.Pin = User_Button_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(User_Button_GPIO_Port, &GPIO_InitStruct);
  /*Configure GPIO pin : Led_Pin */
  GPIO_InitStruct.Pin = Led_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  HAL_GPIO_Init(Led_GPIO_Port, &GPIO_InitStruct);
  /* EXTI interrupt init*/
  HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);
}
/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
#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 */
/**
  * @brief ADC1 Initialization Function
  * @param None
  * @retval None
  */
static void MX_ADC1_Init(void)
{
  /* USER CODE BEGIN ADC1_Init 0 */
  /* USER CODE END ADC1_Init 0 */
  ADC_ChannelConfTypeDef sConfig = {0};
  /* USER CODE BEGIN ADC1_Init 1 */
  /* USER CODE END ADC1_Init 1 */
  /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  */
  hadc1.Instance = ADC1;
  hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
  hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  hadc1.Init.LowPowerAutoWait = DISABLE;
  hadc1.Init.LowPowerAutoPowerOff = DISABLE;
  hadc1.Init.ContinuousConvMode = DISABLE;
  hadc1.Init.NbrOfConversion = 1;
  hadc1.Init.DiscontinuousConvMode = DISABLE;
  hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc1.Init.DMAContinuousRequests = DISABLE;
  hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  hadc1.Init.SamplingTimeCommon1 = ADC_SAMPLETIME_1CYCLE_5;
  hadc1.Init.SamplingTimeCommon2 = ADC_SAMPLETIME_1CYCLE_5;
  hadc1.Init.OversamplingMode = DISABLE;
  hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;
  if (HAL_ADC_Init(&hadc1) != HAL_OK)
  {
    Error_Handler();
  }
  /*
  * Configure Regular Channel
  */
  /*
  sConfig.Channel = ADC_CHANNEL_0;
  sConfig.Rank = ADC_REGULAR_RANK_1;
  sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  */
}
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.