driver/buzzer/buzzer.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /******************************************************************************* | ||
| 2 | * | ||
| 3 | * FILE: | ||
| 4 | * buzzer.c | ||
| 5 | * | ||
| 6 | * DESCRIPTION: | ||
| 7 | * Contains API functions for the flight computer buzzer | ||
| 8 | * | ||
| 9 | * COPYRIGHT: | ||
| 10 | * Copyright (c) 2025 Sun Devil Rocketry. | ||
| 11 | * All rights reserved. | ||
| 12 | * | ||
| 13 | * This software is licensed under terms that can be found in the LICENSE | ||
| 14 | * file in the root directory of this software component. | ||
| 15 | * If no LICENSE file comes with this software, it is covered under the | ||
| 16 | * BSD-3-Clause. | ||
| 17 | * | ||
| 18 | * https://opensource.org/license/bsd-3-clause | ||
| 19 | * | ||
| 20 | *******************************************************************************/ | ||
| 21 | |||
| 22 | |||
| 23 | /*------------------------------------------------------------------------------ | ||
| 24 | Standard Includes | ||
| 25 | ------------------------------------------------------------------------------*/ | ||
| 26 | #include <stdbool.h> | ||
| 27 | #include <string.h> | ||
| 28 | #include <math.h> | ||
| 29 | |||
| 30 | |||
| 31 | /*------------------------------------------------------------------------------ | ||
| 32 | Project Includes | ||
| 33 | ------------------------------------------------------------------------------*/ | ||
| 34 | #include "main.h" | ||
| 35 | #if defined( A0002_REV2 ) | ||
| 36 | #include "sdr_pin_defines_A0002.h" | ||
| 37 | #elif defined( FLIGHT_COMPUTER_LITE ) | ||
| 38 | #include "sdr_pin_defines_A0007.h" | ||
| 39 | #elif defined ( A0010 ) | ||
| 40 | #include "sdr_pin_defines_A0010.h" | ||
| 41 | #else | ||
| 42 | #error No buzzer compatible device specified | ||
| 43 | #endif | ||
| 44 | |||
| 45 | #ifdef EMULATOR | ||
| 46 | #include "emulator.h" | ||
| 47 | #endif | ||
| 48 | |||
| 49 | #include "buzzer.h" | ||
| 50 | |||
| 51 | |||
| 52 | /*------------------------------------------------------------------------------ | ||
| 53 | Global Variables | ||
| 54 | ------------------------------------------------------------------------------*/ | ||
| 55 | |||
| 56 | |||
| 57 | /*------------------------------------------------------------------------------ | ||
| 58 | Internal function prototypes | ||
| 59 | ------------------------------------------------------------------------------*/ | ||
| 60 | |||
| 61 | |||
| 62 | /*------------------------------------------------------------------------------ | ||
| 63 | API Functions | ||
| 64 | ------------------------------------------------------------------------------*/ | ||
| 65 | |||
| 66 | |||
| 67 | /******************************************************************************* | ||
| 68 | * * | ||
| 69 | * PROCEDURE: * | ||
| 70 | * buzzer_beep * | ||
| 71 | * * | ||
| 72 | * DESCRIPTION: * | ||
| 73 | * Beep the flight computer buzzer * | ||
| 74 | * * | ||
| 75 | *******************************************************************************/ | ||
| 76 | 14 | BUZZ_STATUS buzzer_beep | |
| 77 | ( | ||
| 78 | uint32_t duration /* Length of beep in milliseconds */ | ||
| 79 | ) | ||
| 80 | { | ||
| 81 | /*------------------------------------------------------------------------------ | ||
| 82 | Local variables | ||
| 83 | ------------------------------------------------------------------------------*/ | ||
| 84 | 14 | HAL_StatusTypeDef hal_status; /* Return codes from HAL API */ | |
| 85 | |||
| 86 | /*------------------------------------------------------------------------------ | ||
| 87 | Initializations | ||
| 88 | ------------------------------------------------------------------------------*/ | ||
| 89 | 14 | hal_status = HAL_OK; | |
| 90 | |||
| 91 | |||
| 92 | /*------------------------------------------------------------------------------ | ||
| 93 | API Function Implementation | ||
| 94 | ------------------------------------------------------------------------------*/ | ||
| 95 | #ifndef EMULATOR | ||
| 96 | /* Start generating PWM pulses */ | ||
| 97 | hal_status = HAL_TIM_PWM_Start( &(BUZZ_TIM), BUZZ_TIM_CHANNEL ); | ||
| 98 | if ( hal_status != HAL_OK ) | ||
| 99 | { | ||
| 100 | return BUZZ_HAL_ERROR; | ||
| 101 | } | ||
| 102 | |||
| 103 | /* Wait for beep duration */ | ||
| 104 | HAL_Delay( duration ); | ||
| 105 | |||
| 106 | /* Stop the PWM pulses */ | ||
| 107 | hal_status = HAL_TIM_PWM_Stop( &( BUZZ_TIM ), BUZZ_TIM_CHANNEL ); | ||
| 108 | if ( hal_status != HAL_OK ) | ||
| 109 | { | ||
| 110 | return BUZZ_HAL_ERROR; | ||
| 111 | } | ||
| 112 | else | ||
| 113 | { | ||
| 114 | return BUZZ_OK; | ||
| 115 | } | ||
| 116 | #else | ||
| 117 | 14 | emulator_buzzer_beep_request(duration); | |
| 118 | 14 | return hal_status; | |
| 119 | #endif | ||
| 120 | |||
| 121 | } /* buzzer_beep */ | ||
| 122 | |||
| 123 | |||
| 124 | /******************************************************************************* | ||
| 125 | * * | ||
| 126 | * PROCEDURE: * | ||
| 127 | * buzzer_multi_beeps * | ||
| 128 | * * | ||
| 129 | * DESCRIPTION: * | ||
| 130 | * Beep the flight computer buzzer a specified number of times (blocking) * | ||
| 131 | * * | ||
| 132 | *******************************************************************************/ | ||
| 133 | 5 | BUZZ_STATUS buzzer_multi_beeps | |
| 134 | ( | ||
| 135 | uint32_t beep_duration, /* Length of beep in milliseconds */ | ||
| 136 | uint32_t time_between_beeps, /* How long to wait between beeps in ms */ | ||
| 137 | uint8_t num_beeps /* How many times to repeat */ | ||
| 138 | ) | ||
| 139 | { | ||
| 140 | 5 | BUZZ_STATUS status = BUZZ_OK; | |
| 141 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 5 times.
|
18 | for ( uint8_t i = 0; i < num_beeps; i++ ) |
| 142 | { | ||
| 143 | 13 | status = buzzer_beep(beep_duration); | |
| 144 | |||
| 145 |
1/2✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
|
13 | if ( status != BUZZ_OK ) |
| 146 | { | ||
| 147 | return status; | ||
| 148 | } | ||
| 149 | |||
| 150 | 13 | HAL_Delay(time_between_beeps); | |
| 151 | } | ||
| 152 | |||
| 153 | return status; | ||
| 154 | |||
| 155 | } | ||
| 156 | |||
| 157 | |||
| 158 | /******************************************************************************* | ||
| 159 | * * | ||
| 160 | * PROCEDURE: * | ||
| 161 | * buzzer_num_beeps * | ||
| 162 | * * | ||
| 163 | * DESCRIPTION: * | ||
| 164 | * Beep the flight computer buzzer specified number of times * | ||
| 165 | * * | ||
| 166 | *******************************************************************************/ | ||
| 167 | ✗ | BUZZ_STATUS buzzer_num_beeps | |
| 168 | ( | ||
| 169 | uint8_t num_beeps /* Number of beeps */ | ||
| 170 | ) | ||
| 171 | { | ||
| 172 | /* This function is implemented as a non-blocking function that uses the | ||
| 173 | sysClock to detemine when to take an action. The function is meant to be | ||
| 174 | used to loops with processing times less than the beep and intermittent | ||
| 175 | durations. The static variable num_calls is used to maintain memory of | ||
| 176 | how many times the function has been called. The static variables | ||
| 177 | init_beep_time and stop_beep_time are used to keep track of the time of | ||
| 178 | starting and stopping a beep between subsequenct function calls. The static | ||
| 179 | variable last_end_time is used to add a delay between subsequent sequences | ||
| 180 | of beeps so the beep count can be discerned */ | ||
| 181 | ✗ | static uint8_t num_calls = 0; | |
| 182 | ✗ | static uint32_t init_beep_time = 0; | |
| 183 | ✗ | static uint32_t stop_beep_time = 0; | |
| 184 | ✗ | static uint32_t last_end_time = 0; | |
| 185 | |||
| 186 | /*------------------------------------------------------------------------------ | ||
| 187 | Local variables | ||
| 188 | ------------------------------------------------------------------------------*/ | ||
| 189 | ✗ | HAL_StatusTypeDef hal_status; /* Return codes from HAL API */ | |
| 190 | ✗ | uint8_t total_num_calls; /* Number of times the function must be | |
| 191 | called to beep num_beeps times */ | ||
| 192 | ✗ | uint8_t is_odd; /* 1 when num_calls is odd */ | |
| 193 | ✗ | uint32_t current_tick; /* Current Sys tick */ | |
| 194 | |||
| 195 | |||
| 196 | /*------------------------------------------------------------------------------ | ||
| 197 | Initializations | ||
| 198 | ------------------------------------------------------------------------------*/ | ||
| 199 | ✗ | hal_status = HAL_OK; | |
| 200 | ✗ | total_num_calls = 2*num_beeps; | |
| 201 | ✗ | is_odd = num_calls%2; | |
| 202 | ✗ | current_tick = HAL_GetTick(); | |
| 203 | |||
| 204 | |||
| 205 | /*------------------------------------------------------------------------------ | ||
| 206 | API Function Implementation | ||
| 207 | ------------------------------------------------------------------------------*/ | ||
| 208 | |||
| 209 | /* Check that enough time has elapsed since last beep sequence */ | ||
| 210 | ✗ | if ( ( current_tick - last_end_time ) <= BUZZ_SEQUENCE_DELAY ) | |
| 211 | { | ||
| 212 | return BUZZ_OK; | ||
| 213 | } | ||
| 214 | |||
| 215 | /* Check for 0 beeps */ | ||
| 216 | ✗ | if ( num_beeps == 0 ) | |
| 217 | { | ||
| 218 | return BUZZ_OK; | ||
| 219 | } | ||
| 220 | |||
| 221 | /* Remaining beeps */ | ||
| 222 | ✗ | if ( is_odd ) | |
| 223 | { | ||
| 224 | /* Stop Beep */ | ||
| 225 | ✗ | if ( ( ( current_tick - init_beep_time ) >= BUZZ_BEEP_DURATION ) ) | |
| 226 | { | ||
| 227 | ✗ | hal_status = HAL_TIM_PWM_Stop( &( BUZZ_TIM ), BUZZ_TIM_CHANNEL ); | |
| 228 | ✗ | if ( hal_status != HAL_OK ) | |
| 229 | { | ||
| 230 | return BUZZ_HAL_ERROR; | ||
| 231 | } | ||
| 232 | else | ||
| 233 | { | ||
| 234 | ✗ | stop_beep_time = current_tick; | |
| 235 | ✗ | num_calls++; | |
| 236 | /* Termination condition */ | ||
| 237 | ✗ | if ( num_calls == total_num_calls ) | |
| 238 | { | ||
| 239 | ✗ | num_calls = 0; | |
| 240 | ✗ | last_end_time = current_tick; | |
| 241 | } | ||
| 242 | ✗ | return BUZZ_OK; | |
| 243 | } | ||
| 244 | } | ||
| 245 | else | ||
| 246 | { | ||
| 247 | return BUZZ_OK; | ||
| 248 | } | ||
| 249 | |||
| 250 | } | ||
| 251 | else | ||
| 252 | { | ||
| 253 | /* Start beep */ | ||
| 254 | ✗ | if ( ( ( current_tick - stop_beep_time ) >= BUZZ_STOP_DURATION ) ) | |
| 255 | { | ||
| 256 | ✗ | hal_status = HAL_TIM_PWM_Start( &(BUZZ_TIM), BUZZ_TIM_CHANNEL ); | |
| 257 | ✗ | if ( hal_status != HAL_OK ) | |
| 258 | { | ||
| 259 | return BUZZ_HAL_ERROR; | ||
| 260 | } | ||
| 261 | else | ||
| 262 | { | ||
| 263 | ✗ | init_beep_time = current_tick; | |
| 264 | ✗ | num_calls++; | |
| 265 | ✗ | return BUZZ_OK; | |
| 266 | } | ||
| 267 | } | ||
| 268 | else | ||
| 269 | { | ||
| 270 | return BUZZ_OK; | ||
| 271 | } | ||
| 272 | } | ||
| 273 | } /* buzzer_num_beeps */ | ||
| 274 | |||
| 275 | |||
| 276 | /*------------------------------------------------------------------------------ | ||
| 277 | Internal procedures | ||
| 278 | ------------------------------------------------------------------------------*/ | ||
| 279 | |||
| 280 | |||
| 281 | /******************************************************************************* | ||
| 282 | * END OF FILE * | ||
| 283 | *******************************************************************************/ | ||
| 284 |