tdse-tp4_03-adc_pwm

FIUBA - Electrónica - Taller de Sistemas Embebidos - Trabajo Práctico No 4 - Proyecto No 03
Index Commits Files Refs README
app/src/task_adc.c (3893B)
   1 /*
   2  * Copyright (c) 2023 Juan Manuel Cruz <jcruz@fi.uba.ar> <jcruz@frba.utn.edu.ar>.
   3  * All rights reserved.
   4  *
   5  * Redistribution and use in source and binary forms, with or without
   6  * modification, are permitted provided that the following conditions are met:
   7  *
   8  * 1. Redistributions of source code must retain the above copyright
   9  *    notice, this list of conditions and the following disclaimer.
  10  *
  11  * 2. Redistributions in binary form must reproduce the above copyright
  12  *    notice, this list of conditions and the following disclaimer in the
  13  *    documentation and/or other materials provided with the distribution.
  14  *
  15  * 3. Neither the name of the copyright holder nor the names of its
  16  *    contributors may be used to endorse or promote products derived from
  17  *    this software without specific prior written permission.
  18  *
  19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  29  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30  * POSSIBILITY OF SUCH DAMAGE.
  31  *
  32  * @file   : task_adc.c
  33  * @date   : Set 26, 2023
  34  * @author : Juan Manuel Cruz <jcruz@fi.uba.ar> <jcruz@frba.utn.edu.ar>
  35  * @version    v1.0.0
  36  */
  37 
  38 /********************** inclusions *******************************************/
  39 /* Project includes. */
  40 #include "main.h"
  41 
  42 /* Demo includes. */
  43 #include "logger.h"
  44 #include "dwt.h"
  45 
  46 /* Application & Tasks includes. */
  47 #include "board.h"
  48 #include "app.h"
  49 
  50 /********************** macros and definitions *******************************/
  51 
  52 #define ADC_AVG_SAMPLES 10
  53 
  54 
  55 /********************** internal data declaration ****************************/
  56 
  57 
  58 /********************** internal functions declaration ***********************/
  59 HAL_StatusTypeDef ADC_Poll_Read(uint16_t *value);
  60 
  61 /********************** internal data definition *****************************/
  62 const char *p_task_adc = "Task ADC";
  63 
  64 /********************** external data declaration *****************************/
  65 
  66 extern ADC_HandleTypeDef hadc1;
  67 
  68 /********************** external functions definition ************************/
  69 void task_adc_init(void *parameters)
  70 {
  71     shared_data_type *shared_data = (shared_data_type *) parameters;
  72 
  73     /* Print out: Task Initialized */
  74     LOGGER_LOG("  %s is running - %s\r\n", GET_NAME(task_adc_init), p_task_adc);
  75 
  76     shared_data->adc_end_of_conversion = false;
  77 }
  78 
  79 void task_adc_update(void *parameters)
  80 {
  81     shared_data_type *shared_data = (shared_data_type *) parameters;
  82 
  83     if (HAL_OK == ADC_Poll_Read(&shared_data->adc_value))
  84     {
  85         shared_data->adc_end_of_conversion = true;
  86     }
  87     else
  88     {
  89         LOGGER_LOG("error\n");
  90     }
  91 }
  92 
  93 // Requests start of conversion, waits until conversion done
  94 HAL_StatusTypeDef ADC_Poll_Read(uint16_t *value) {
  95     HAL_StatusTypeDef res;
  96     uint32_t sum = 0;
  97 
  98     for (int i = 0; i < ADC_AVG_SAMPLES; i++)
  99     {
 100         if ((res = HAL_ADC_Start(&hadc1)) != HAL_OK)
 101         {
 102             return res;
 103         }
 104 
 105         if ((res = HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY)) != HAL_OK)
 106         {
 107             return res;
 108         }
 109 
 110         sum += HAL_ADC_GetValue(&hadc1);
 111         HAL_ADC_Stop(&hadc1);
 112     }
 113 
 114     *value = (sum / ADC_AVG_SAMPLES);
 115     return HAL_OK;
 116 }
 117 
 118 
 119 
 120 /********************** end of file ******************************************/