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 : systick.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 /********************** macros and definitions *******************************/ 43 44 /********************** internal data declaration ****************************/ 45 46 /********************** internal functions declaration ***********************/ 47 48 /********************** internal data definition *****************************/ 49 50 /********************** external data declaration ****************************/ 51 52 /********************** external functions definition ************************/ 53 /* Provides a blocking delay in microseconds using the SysTick timer */ 54 void systick_delay_us(uint32_t delay_us) 55 { 56 uint32_t start, current, target, elapsed; 57 58 if (0 == delay_us) 59 return; 60 61 /* Get the start value of the SysTick counter */ 62 start = SysTick->VAL; 63 64 /* Calculate the total number of SysTick counts required for the delay */ 65 target = delay_us * (SystemCoreClock / 1000000UL); 66 67 /* Loop until the required counts have elapsed */ 68 while (1) 69 { 70 /* Get the current value of the SysTick counter */ 71 current = SysTick->VAL; 72 73 /* Handle the case where the SysTick counter wraps around, */ 74 /* counts down to 0 and reloads */ 75 if (current <= start) 76 { 77 elapsed = start - current; 78 } 79 else 80 { 81 /* Counter wrapped around, */ 82 /* add the reload value to account for the wrap */ 83 elapsed = SysTick->LOAD + start - current; 84 } 85 86 /* Exit the loop when the desired delay is reached */ 87 if (elapsed >= target) 88 { 89 break; 90 } 91 } 92 } 93 94 /********************** end of file ******************************************/
