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_pwm.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 <stdlib.h> 41 #include "main.h" 42 43 /* Demo includes. */ 44 #include "logger.h" 45 #include "dwt.h" 46 47 /* Application & Tasks includes. */ 48 #include "board.h" 49 #include "app.h" 50 #include <math.h> 51 52 /********************** macros and definitions *******************************/ 53 54 #define STEP (32) 55 #define PERIOD (65535) 56 57 /********************** internal data declaration ****************************/ 58 59 /********************** internal functions declaration ***********************/ 60 void setPWM(TIM_HandleTypeDef timer, 61 uint32_t channel, 62 uint16_t period, 63 uint16_t pulse); 64 65 /********************** internal data definition *****************************/ 66 const char *p_task_pwm = "Task PWM"; 67 68 /********************** external data declaration *****************************/ 69 extern TIM_HandleTypeDef htim3; 70 71 /********************** external functions definition ************************/ 72 void task_pwm_init(void *parameters) 73 { 74 shared_data_type *shared_data = (shared_data_type *) parameters; 75 76 shared_data->pwm_active = 0; 77 LOGGER_LOG(" %s is running - %s\r\n", GET_NAME(task_pwm_init), p_task_pwm); 78 } 79 80 void task_pwm_update(void *parameters) 81 { 82 float x_norm; 83 shared_data_type *data = (shared_data_type *) parameters; 84 85 if (data->adc_end_of_conversion) 86 { 87 data->adc_end_of_conversion = false; 88 89 // se descarta el ultimo digito del valor leido del adc 90 uint32_t x = (data->adc_value / 10) * 10; 91 92 // se mapea el valor leido de adc al intervalo [0, 1] 93 x_norm = ((float)x/4096); 94 95 // se mapea el valor normalizado elevado a la sexta 96 // al intervalo en y [0, PERIODO] esto porque el LED 97 // no tiene un comportamiento lineal 98 data->pwm_active = PERIOD*powf(x_norm, 6); 99 100 setPWM(htim3, TIM_CHANNEL_1, PERIOD, data->pwm_active); 101 } 102 } 103 104 void setPWM(TIM_HandleTypeDef timer, uint32_t channel, 105 uint16_t period, uint16_t pulse) 106 { 107 HAL_TIM_PWM_Stop(&timer, channel); 108 TIM_OC_InitTypeDef sConfigOC; 109 timer.Init.Period = period; 110 HAL_TIM_PWM_Init(&timer); 111 112 sConfigOC.OCMode = TIM_OCMODE_PWM1; 113 sConfigOC.Pulse = pulse; 114 sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; 115 sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; 116 HAL_TIM_PWM_ConfigChannel(&timer, &sConfigOC, channel); 117 HAL_TIM_PWM_Start(&timer, channel); 118 } 119 120 /********************** end of file ******************************************/
