c4a0847c99
* FuriHal: new speaker HAL * FuriHal: drop PWM * FuriHal: move COMP1 to LL * FuriHal: move COMP1 to LL backport to F6 * FuriHal: remove missing gpio_rfid_carrier from F6 * FurHal: use LL for system controls in flash HAL * Drop F6 source tree * Drop F6 from GitHub workflow * Tie USE_FULL_ASSERT with APP_UNIT_TESTS * Speaker: return to old volume calculation * FreeRTOS: move TCB header to glue Co-authored-by: DrZlo13 <who.just.the.doctor@gmail.com>
55 lines
1.9 KiB
C
55 lines
1.9 KiB
C
#include <furi_hal_speaker.h>
|
|
#include <furi_hal_gpio.h>
|
|
#include <furi_hal_resources.h>
|
|
|
|
#include <stm32wbxx_ll_tim.h>
|
|
|
|
#define FURI_HAL_SPEAKER_TIMER TIM16
|
|
#define FURI_HAL_SPEAKER_CHANNEL LL_TIM_CHANNEL_CH1
|
|
#define FURI_HAL_SPEAKER_PRESCALER 500
|
|
#define FURI_HAL_SPEAKER_MAX_VOLUME 60
|
|
|
|
// #define FURI_HAL_SPEAKER_NEW_VOLUME
|
|
|
|
void furi_hal_speaker_init() {
|
|
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM16);
|
|
LL_TIM_DeInit(FURI_HAL_SPEAKER_TIMER);
|
|
hal_gpio_init_ex(
|
|
&gpio_speaker, GpioModeAltFunctionPushPull, GpioPullNo, GpioSpeedLow, GpioAltFn14TIM16);
|
|
}
|
|
|
|
void furi_hal_speaker_start(float frequency, float volume) {
|
|
if(volume < 0) volume = 0;
|
|
if(volume > 1) volume = 1;
|
|
volume = volume * volume * volume;
|
|
|
|
LL_TIM_InitTypeDef TIM_InitStruct = {0};
|
|
TIM_InitStruct.Prescaler = FURI_HAL_SPEAKER_PRESCALER - 1;
|
|
TIM_InitStruct.Autoreload = ((SystemCoreClock / FURI_HAL_SPEAKER_PRESCALER) / frequency) - 1;
|
|
LL_TIM_Init(FURI_HAL_SPEAKER_TIMER, &TIM_InitStruct);
|
|
|
|
#ifdef FURI_HAL_SPEAKER_NEW_VOLUME
|
|
uint16_t compare_value = volume * FURI_HAL_SPEAKER_MAX_VOLUME;
|
|
uint16_t clip_value = volume * TIM_InitStruct.Autoreload / 2;
|
|
if(compare_value > clip_value) {
|
|
compare_value = clip_value;
|
|
}
|
|
#else
|
|
uint16_t compare_value = volume * TIM_InitStruct.Autoreload / 2;
|
|
#endif
|
|
|
|
LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0};
|
|
TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_PWM1;
|
|
TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_ENABLE;
|
|
TIM_OC_InitStruct.CompareValue = compare_value;
|
|
LL_TIM_OC_Init(FURI_HAL_SPEAKER_TIMER, FURI_HAL_SPEAKER_CHANNEL, &TIM_OC_InitStruct);
|
|
|
|
LL_TIM_EnableAllOutputs(FURI_HAL_SPEAKER_TIMER);
|
|
LL_TIM_EnableCounter(FURI_HAL_SPEAKER_TIMER);
|
|
}
|
|
|
|
void furi_hal_speaker_stop() {
|
|
LL_TIM_CC_DisableChannel(FURI_HAL_SPEAKER_TIMER, FURI_HAL_SPEAKER_CHANNEL);
|
|
LL_TIM_DisableCounter(FURI_HAL_SPEAKER_TIMER);
|
|
}
|