Da Vinci Firmware 1
Firmware for the DaVinci-M rocket avionics board.
Loading...
Searching...
No Matches
Buzzer Driver

Low-level driver for the PWM buzzer. More...

Collaboration diagram for Buzzer Driver:

Modules

 Musical Note and Tempo Definitions
 

Files

file  buzzer.h
 Provides a driver for playing musical notes on a PWM-driven buzzer.
 

Data Structures

struct  note_t
 Represents a single musical note with a frequency and duration. More...
 
struct  buzzer_t
 Represents the buzzer device handle. More...
 

Functions

void buzzer_init (buzzer_t *bz, TIM_HandleTypeDef *htim, uint32_t channel)
 Initializes the buzzer object and its associated timer hardware.
 
void musica_maestro (buzzer_t *bz, const note_t *partition, uint16_t melody_length)
 Plays a melody by iterating through an array of notes.
 

Variables

buzzer_t buzzer
 Global instance of the buzzer object.
 

Detailed Description

Low-level driver for the PWM buzzer.

Function Documentation

◆ buzzer_init()

void buzzer_init ( buzzer_t bz,
TIM_HandleTypeDef *  htim,
uint32_t  channel 
)

Initializes the buzzer object and its associated timer hardware.

Initializes the buzzer object.

This function should be called once at startup to associate the buzzer object with its hardware timer.

Parameters
[in,out]bzPointer to the global buzzer_t object.
[in]htimPointer to the initialized TIM_HandleTypeDef for the buzzer.
[in]channelThe TIM_CHANNEL_x that the buzzer is connected to.
Returns
None

Definition at line 23 of file buzzer.c.

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}
#define NULL
Definition bmp3_defs.h:88
void Error_Handler(void)
This function is executed in case of error occurrence.
Definition main.c:2042
buzzer_t * bz
Definition main.c:121
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

References bz, Error_Handler(), buzzer_t::htim, NULL, and buzzer_t::tim_channel.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ musica_maestro()

void musica_maestro ( buzzer_t bz,
const note_t partition,
uint16_t  melody_length 
)

Plays a melody by iterating through an array of notes.

Plays a melody from an array of notes.

Iterates through the provided array of note_t structures, playing each note for its specified duration.

Parameters
[in]bzPointer to the initialized buzzer_t object.
[in]partitionAn array of note_t objects representing the melody.
[in]melody_lengthThe number of notes in the partition array.
Returns
None

Definition at line 38 of file buzzer.c.

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 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
#define REST
A rest (silence) in a melody.
Definition buzzer.h:54
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

References bz, note_t::duration_ms, note_t::frequency, buzzer_t::htim, NULL, REST, buzzer_t::tim_channel, TIMER_CLOCK_FREQ, and TIMER_PRESCALER.

Referenced by main().

Here is the caller graph for this function:

Variable Documentation

◆ buzzer

buzzer_t buzzer
extern

Global instance of the buzzer object.

Definition at line 17 of file buzzer.c.