Add multitone support for loudspeaker

This commit is contained in:
Mario Hüttel 2020-05-24 23:35:17 +02:00
parent e97092042b
commit 355e81ba44
3 changed files with 66 additions and 11 deletions

View File

@ -27,6 +27,7 @@
static const uint8_t digio_pins[] = {DIGIO_PINS}; static const uint8_t digio_pins[] = {DIGIO_PINS};
static const uint8_t digio_default_io[] = {DIGIO_INOUT_DEFAULT}; static const uint8_t digio_default_io[] = {DIGIO_INOUT_DEFAULT};
static const uint8_t digio_default_altfunc[] = {DIGIO_ALTFUNC_DEFAULT}; static const uint8_t digio_default_altfunc[] = {DIGIO_ALTFUNC_DEFAULT};
static uint16_t loudspeaker_val;
static void digio_setup_pin_int(uint8_t bit_no, uint8_t in_out, uint8_t alt_func) static void digio_setup_pin_int(uint8_t bit_no, uint8_t in_out, uint8_t alt_func)
{ {
@ -118,6 +119,19 @@ int led_get(uint8_t num)
return ((LED_PORT->ODR & (1<<led_pins[num])) ? 1 : 0); return ((LED_PORT->ODR & (1<<led_pins[num])) ? 1 : 0);
} }
static void loudspeaker_freq_timer_init(void)
{
#if LOUDSPEAKER_MULTIFREQ
rcc_manager_enable_clock(&RCC->APB1ENR, BITMASK_TO_BITNO(RCC_APB1ENR_TIM7EN));
TIM7->CR1 = 0UL;
TIM7->CR2 = 0UL;
TIM7->PSC = 1000;
TIM7->DIER = TIM_DIER_UIE;
NVIC_EnableIRQ(TIM7_IRQn);
#endif
}
void loudspeaker_setup() void loudspeaker_setup()
{ {
rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(LOUDSPEAKER_RCC_MASK)); rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(LOUDSPEAKER_RCC_MASK));
@ -125,16 +139,56 @@ void loudspeaker_setup()
LOUDSPEAKER_PORT->MODER &= MODER_DELETE(LOUDSPEAKER_PIN); LOUDSPEAKER_PORT->MODER &= MODER_DELETE(LOUDSPEAKER_PIN);
LOUDSPEAKER_PORT->MODER |= OUTPUT(LOUDSPEAKER_PIN); LOUDSPEAKER_PORT->MODER |= OUTPUT(LOUDSPEAKER_PIN);
loudspeaker_set(0); loudspeaker_freq_timer_init();
loudspeaker_set(0U);
} }
void loudspeaker_set(int val)
static void loudspeaker_start_beep(uint16_t val)
{ {
if (val) #if LOUDSPEAKER_MULTIFREQ
TIM7->ARR = val;
TIM7->CNT = 0UL;
TIM7->CR1 |= TIM_CR1_CEN;
#else
(void)val;
LOUDSPEAKER_PORT->ODR |= (1<<LOUDSPEAKER_PIN); LOUDSPEAKER_PORT->ODR |= (1<<LOUDSPEAKER_PIN);
else #endif
LOUDSPEAKER_PORT->ODR &= ~(1<<LOUDSPEAKER_PIN);
} }
int loudspeaker_get()
static void loudspeaker_stop_beep(void)
{ {
return ((LOUDSPEAKER_PORT->ODR & (1<<LOUDSPEAKER_PIN)) ? 1 : 0); #if LOUDSPEAKER_MULTIFREQ
TIM7->CR1 &= ~TIM_CR1_CEN;
__DSB();
TIM7->SR = 0UL;
__DSB();
LOUDSPEAKER_PORT->ODR &= ~(1<<LOUDSPEAKER_PIN);
#else
LOUDSPEAKER_PORT->ODR &= ~(1<<LOUDSPEAKER_PIN);
#endif
} }
void loudspeaker_set(uint16_t val)
{
loudspeaker_val = val;
if (!val) {
loudspeaker_stop_beep();
} else {
loudspeaker_start_beep(val);
}
}
uint16_t loudspeaker_get()
{
return loudspeaker_val;
}
#if LOUDSPEAKER_MULTIFREQ
void TIM7_IRQHandler(void)
{
TIM7->SR = 0UL;
__DSB();
LOUDSPEAKER_PORT->ODR ^= (1<<LOUDSPEAKER_PIN);
}
#endif

View File

@ -58,10 +58,11 @@ int led_get(uint8_t num);
#define LOUDSPEAKER_PORT GPIOB #define LOUDSPEAKER_PORT GPIOB
#define LOUDSPEAKER_RCC_MASK RCC_AHB1ENR_GPIOBEN #define LOUDSPEAKER_RCC_MASK RCC_AHB1ENR_GPIOBEN
#define LOUDSPEAKER_PIN 1 #define LOUDSPEAKER_PIN 1
#define LOUDSPEAKER_MULTIFREQ 1
void loudspeaker_setup(); void loudspeaker_setup();
void loudspeaker_set(int val); void loudspeaker_set(uint16_t val);
int loudspeaker_get(); uint16_t loudspeaker_get();
#endif /* __DIGIO_H__ */ #endif /* __DIGIO_H__ */

View File

@ -259,7 +259,7 @@ int main()
oven_driver_set_power(0U); oven_driver_set_power(0U);
/* Activate loundspeaker permanently */ /* Activate loundspeaker permanently */
loudspeaker_set(1); loudspeaker_set(100);
} else if (pid_controller_active) { } else if (pid_controller_active) {
/* In case temperature measuremnt is okay and controlelr is working, write output power */ /* In case temperature measuremnt is okay and controlelr is working, write output power */
oven_driver_set_power(pid_controller_output < 0 ? 0U : (uint8_t)pid_controller_output); oven_driver_set_power(pid_controller_output < 0 ? 0U : (uint8_t)pid_controller_output);