Da Vinci Firmware 1
Firmware for the DaVinci-M rocket avionics board.
Loading...
Searching...
No Matches
buzzer.c
Go to the documentation of this file.
1
8#include "buzzer.h"
9#include "main.h" // Needed for HAL_Delay and timer register macros.
10
11// --- Private Constants ---
12// Define these here to avoid cluttering the header. Adjust if your system clock changes.
13#define TIMER_CLOCK_FREQ 240000000UL
14#define TIMER_PRESCALER 2400
15
16// --- Global Variable Definition ---
18
23void buzzer_init(buzzer_t *bz, TIM_HandleTypeDef *htim, uint32_t channel)
24{
25 // Assert that the provided pointers are valid.
26 if (bz == NULL || htim == NULL) {
28 }
29
30 bz->htim = htim;
31 bz->tim_channel = channel;
32}
33
38void musica_maestro(buzzer_t *bz, const note_t *partition, uint16_t melody_length)
39{
40 if (bz == NULL || bz->htim == NULL || partition == NULL) {
41 return; // Safety check
42 }
43
44 // Start the PWM signal. The duty cycle is set to 50% by configuring ARR/2.
45 HAL_TIM_PWM_Start(bz->htim, bz->tim_channel);
46
47 for (uint16_t i = 0; i < melody_length; i++) {
48 uint16_t freq = partition[i].frequency;
49 uint16_t duration = partition[i].duration_ms;
50
51 if (freq == REST || duration == 0) {
52 // Silence for a rest note
53 __HAL_TIM_SET_COMPARE(bz->htim, bz->tim_channel, 0); // Set duty cycle to 0% for silence
54 if (duration > 0) {
55 HAL_Delay(duration);
56 }
57 } else {
58 // Play a tone
59 // Calculate the Auto-Reload Register (ARR) value for the desired frequency
60 uint32_t arr_value = (TIMER_CLOCK_FREQ / (TIMER_PRESCALER * freq)) - 1;
61
62 // Clamp the ARR value to the 16-bit timer limit
63 if (arr_value > 0xFFFF) {
64 arr_value = 0xFFFF;
65 }
66
67 // Set the timer period (determines frequency)
68 __HAL_TIM_SET_AUTORELOAD(bz->htim, arr_value);
69 // Set the duty cycle to 50% for a standard tone
70 __HAL_TIM_SET_COMPARE(bz->htim, bz->tim_channel, arr_value / 2);
71
72 // Hold the note for its duration
73 HAL_Delay(duration);
74 }
75 }
76
77 // Stop the PWM signal after the melody is finished
78 HAL_TIM_PWM_Stop(bz->htim, bz->tim_channel);
79}
#define NULL
Definition bmp3_defs.h:88
#define TIMER_CLOCK_FREQ
The input clock frequency to the timer peripheral.
Definition buzzer.c:13
#define TIMER_PRESCALER
A fixed prescaler to bring the timer frequency into a usable range.
Definition buzzer.c:14
Provides a driver for playing musical notes on a PWM-driven buzzer.
#define REST
A rest (silence) in a melody.
Definition buzzer.h:54
buzzer_t buzzer
Global instance of the buzzer object.
Definition buzzer.c:17
void musica_maestro(buzzer_t *bz, const note_t *partition, uint16_t melody_length)
Plays a melody by iterating through an array of notes.
Definition buzzer.c:38
void buzzer_init(buzzer_t *bz, TIM_HandleTypeDef *htim, uint32_t channel)
Initializes the buzzer object and its associated timer hardware.
Definition buzzer.c:23
void Error_Handler(void)
This function is executed in case of error occurrence.
Definition main.c:2042
buzzer_t * bz
Definition main.c:121
: Header for main.c file. This file contains the common defines of the application.
Represents the buzzer device handle.
Definition buzzer.h:39
uint32_t tim_channel
The specific timer channel connected to the buzzer.
Definition buzzer.h:41
TIM_HandleTypeDef * htim
Pointer to the HAL timer handle controlling the buzzer.
Definition buzzer.h:40
Represents a single musical note with a frequency and duration.
Definition buzzer.h:31
uint16_t duration_ms
The duration to play the note in milliseconds (ms).
Definition buzzer.h:33
uint16_t frequency
The frequency of the note in Hertz (Hz). Use REST for silence.
Definition buzzer.h:32