tdse-tp4_04-led_ldr

FIUBA - Electrónica - Taller de Sistemas Embebidos - Trabajo Práctico N°: 4 - LED & LDR
Index Commits Files Refs README
app/inc/dwt.h (4803B)
   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   : dwt.h
  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 #ifndef DWT_INC_DWT_H_
  39 #define DWT_INC_DWT_H_
  40 
  41 /********************** CPP guard ********************************************/
  42 #ifdef __cplusplus
  43 extern "C" {
  44 #endif
  45 
  46 /********************** inclusions *******************************************/
  47 
  48 /********************** macros ***********************************************/
  49 
  50 /* init cycle counter */
  51 /* DWT (Data Watchpoint and Trace) registers, only exists on ARM Cortex with a DWT unit */
  52 /*!< DEMCR: Debug Exception and Monitor Control Register */
  53 /*!< TRCENA: Enable trace and debug block DEMCR (Debug Exception and Monitor Control Register) */
  54 /*!< DWT Cycle Counter register */
  55 /*!< CYCCNTENA bit in DWT_CONTROL register */
  56 #define cycle_counter_init() ({\
  57     CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;    /* enable DWT hardware */\
  58     DWT->CYCCNT = 0;                                /* reset cycle counter */\
  59     DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;            /* start counting */\
  60      })
  61 
  62 /* reset cycle counter */
  63 /*!< DWT Cycle Counter register */
  64 #define cycle_counter_reset() (DWT->CYCCNT = 0)
  65 
  66 /* start counting */
  67 /*!< CYCCNTENA bit in DWT_CONTROL register */
  68 #define cycle_counter_enable() (DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk)
  69 
  70 /* disable counting if not used any more */
  71 /*!< CYCCNTENA bit in DWT_CONTROL register */
  72 #define cycle_counter_disable() (~DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk)
  73 
  74 /* read cycle counter */
  75 /*!< DWT Cycle Counter register */
  76 #define cycle_counter_get() (DWT->CYCCNT)
  77 #define cycles_per_us (SystemCoreClock / 1000000)
  78 #define cycle_counter_time_us() (DWT->CYCCNT / cycles_per_us)
  79 
  80 /*  uint32_t cycle_counter = 0;
  81  *  uint32_t cycle_counter_time_us = 0;
  82  *                                                            // PC8 (GPIO)
  83  *  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, GPIO_PIN_RESET);    // => ______
  84  *  cycle_counter_init();
  85  *
  86  *  ...
  87  *  ...                                                        // => ______
  88  *                                                            //         ___
  89  *  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, GPIO_PIN_SET);     // => __/
  90  *  // or => HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_8);
  91  *  cycle_counter_reset();
  92  *                                                            //      ______
  93  *  ...                                                        // =>
  94  *
  95  *  cycle_counter = cycle_counter_get();
  96  *  cycle_counter_time_us = cycle_counter_time_us();        //      __
  97  *  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, GPIO_PIN_RESET);    // =>   \___
  98  *  // or => HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_8);
  99  *
 100  *                                                          // => ______
 101  *
 102  *  LOGGER_LOG("Cycles: %lu - Time %lu uS\r\n", cycle_counter, cycle_counter_time_us);
 103  */
 104 
 105 /********************** macros ***********************************************/
 106 
 107 /********************** typedef **********************************************/
 108 
 109 /********************** external data declaration ****************************/
 110 
 111 /********************** external functions declaration ***********************/
 112 
 113 /********************** End of CPP guard *************************************/
 114 #ifdef __cplusplus
 115 }
 116 #endif
 117 
 118 #endif /* DWT_INC_DWT_H_ */
 119 
 120 /********************** end of file ******************************************/