2022-03-23 17:59:20 +00:00
|
|
|
#include <furi_hal_speaker.h>
|
|
|
|
#include <furi_hal_gpio.h>
|
|
|
|
#include <furi_hal_resources.h>
|
2022-12-16 22:20:10 +00:00
|
|
|
#include <furi_hal_power.h>
|
2023-05-29 16:05:57 +00:00
|
|
|
#include <furi_hal_bus.h>
|
2022-03-23 17:59:20 +00:00
|
|
|
|
|
|
|
#include <stm32wbxx_ll_tim.h>
|
2022-12-16 22:20:10 +00:00
|
|
|
#include <furi_hal_cortex.h>
|
|
|
|
|
|
|
|
#define TAG "FuriHalSpeaker"
|
2022-03-23 17:59:20 +00:00
|
|
|
|
|
|
|
#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
|
|
|
|
|
2022-12-16 22:20:10 +00:00
|
|
|
static FuriMutex* furi_hal_speaker_mutex = NULL;
|
|
|
|
|
2022-03-23 17:59:20 +00:00
|
|
|
// #define FURI_HAL_SPEAKER_NEW_VOLUME
|
|
|
|
|
|
|
|
void furi_hal_speaker_init() {
|
2022-12-16 22:20:10 +00:00
|
|
|
furi_assert(furi_hal_speaker_mutex == NULL);
|
|
|
|
furi_hal_speaker_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
|
|
|
|
FURI_LOG_I(TAG, "Init OK");
|
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_speaker_deinit() {
|
|
|
|
furi_check(furi_hal_speaker_mutex != NULL);
|
|
|
|
furi_mutex_free(furi_hal_speaker_mutex);
|
|
|
|
furi_hal_speaker_mutex = NULL;
|
|
|
|
}
|
2022-03-25 10:33:01 +00:00
|
|
|
|
2022-12-16 22:20:10 +00:00
|
|
|
bool furi_hal_speaker_acquire(uint32_t timeout) {
|
|
|
|
furi_check(!FURI_IS_IRQ_MODE());
|
|
|
|
|
|
|
|
if(furi_mutex_acquire(furi_hal_speaker_mutex, timeout) == FuriStatusOk) {
|
|
|
|
furi_hal_power_insomnia_enter();
|
2023-05-29 16:05:57 +00:00
|
|
|
furi_hal_bus_enable(FuriHalBusTIM16);
|
2022-12-16 22:20:10 +00:00
|
|
|
furi_hal_gpio_init_ex(
|
|
|
|
&gpio_speaker, GpioModeAltFunctionPushPull, GpioPullNo, GpioSpeedLow, GpioAltFn14TIM16);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_speaker_release() {
|
|
|
|
furi_check(!FURI_IS_IRQ_MODE());
|
|
|
|
furi_check(furi_hal_speaker_is_mine());
|
|
|
|
|
|
|
|
furi_hal_speaker_stop();
|
2023-04-06 09:06:19 +00:00
|
|
|
furi_hal_gpio_init(&gpio_speaker, GpioModeAnalog, GpioPullDown, GpioSpeedLow);
|
2023-05-29 16:05:57 +00:00
|
|
|
|
|
|
|
furi_hal_bus_disable(FuriHalBusTIM16);
|
2022-12-16 22:20:10 +00:00
|
|
|
furi_hal_power_insomnia_exit();
|
|
|
|
|
|
|
|
furi_check(furi_mutex_release(furi_hal_speaker_mutex) == FuriStatusOk);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool furi_hal_speaker_is_mine() {
|
|
|
|
return (FURI_IS_IRQ_MODE()) ||
|
|
|
|
(furi_mutex_get_owner(furi_hal_speaker_mutex) == furi_thread_get_current_id());
|
2022-03-23 17:59:20 +00:00
|
|
|
}
|
|
|
|
|
2022-05-05 09:49:59 +00:00
|
|
|
static inline uint32_t furi_hal_speaker_calculate_autoreload(float frequency) {
|
2022-04-07 15:00:45 +00:00
|
|
|
uint32_t autoreload = (SystemCoreClock / FURI_HAL_SPEAKER_PRESCALER / frequency) - 1;
|
|
|
|
if(autoreload < 2) {
|
|
|
|
autoreload = 2;
|
|
|
|
} else if(autoreload > UINT16_MAX) {
|
|
|
|
autoreload = UINT16_MAX;
|
|
|
|
}
|
|
|
|
|
2022-05-05 09:49:59 +00:00
|
|
|
return autoreload;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint32_t furi_hal_speaker_calculate_compare(float volume) {
|
|
|
|
if(volume < 0) volume = 0;
|
|
|
|
if(volume > 1) volume = 1;
|
|
|
|
volume = volume * volume * volume;
|
2022-03-23 17:59:20 +00:00
|
|
|
|
|
|
|
#ifdef FURI_HAL_SPEAKER_NEW_VOLUME
|
2022-04-07 15:00:45 +00:00
|
|
|
uint32_t compare_value = volume * FURI_HAL_SPEAKER_MAX_VOLUME;
|
2022-05-05 09:49:59 +00:00
|
|
|
uint32_t clip_value = volume * LL_TIM_GetAutoReload(FURI_HAL_SPEAKER_TIMER) / 2;
|
2022-03-23 17:59:20 +00:00
|
|
|
if(compare_value > clip_value) {
|
|
|
|
compare_value = clip_value;
|
|
|
|
}
|
|
|
|
#else
|
2022-05-05 09:49:59 +00:00
|
|
|
uint32_t compare_value = volume * LL_TIM_GetAutoReload(FURI_HAL_SPEAKER_TIMER) / 2;
|
2022-03-23 17:59:20 +00:00
|
|
|
#endif
|
|
|
|
|
2022-04-07 15:00:45 +00:00
|
|
|
if(compare_value == 0) {
|
|
|
|
compare_value = 1;
|
|
|
|
}
|
|
|
|
|
2022-05-05 09:49:59 +00:00
|
|
|
return compare_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_speaker_start(float frequency, float volume) {
|
2022-12-16 22:20:10 +00:00
|
|
|
furi_check(furi_hal_speaker_is_mine());
|
|
|
|
|
2022-05-05 09:49:59 +00:00
|
|
|
if(volume <= 0) {
|
|
|
|
furi_hal_speaker_stop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LL_TIM_InitTypeDef TIM_InitStruct = {0};
|
|
|
|
TIM_InitStruct.Prescaler = FURI_HAL_SPEAKER_PRESCALER - 1;
|
|
|
|
TIM_InitStruct.Autoreload = furi_hal_speaker_calculate_autoreload(frequency);
|
|
|
|
LL_TIM_Init(FURI_HAL_SPEAKER_TIMER, &TIM_InitStruct);
|
|
|
|
|
2022-03-23 17:59:20 +00:00
|
|
|
LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0};
|
|
|
|
TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_PWM1;
|
|
|
|
TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_ENABLE;
|
2022-05-05 09:49:59 +00:00
|
|
|
TIM_OC_InitStruct.CompareValue = furi_hal_speaker_calculate_compare(volume);
|
2022-03-23 17:59:20 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-05-05 09:49:59 +00:00
|
|
|
void furi_hal_speaker_set_volume(float volume) {
|
2022-12-16 22:20:10 +00:00
|
|
|
furi_check(furi_hal_speaker_is_mine());
|
2022-05-05 09:49:59 +00:00
|
|
|
if(volume <= 0) {
|
|
|
|
furi_hal_speaker_stop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if FURI_HAL_SPEAKER_CHANNEL == LL_TIM_CHANNEL_CH1
|
|
|
|
LL_TIM_OC_SetCompareCH1(FURI_HAL_SPEAKER_TIMER, furi_hal_speaker_calculate_compare(volume));
|
|
|
|
#else
|
|
|
|
#error Invalid channel
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-03-23 17:59:20 +00:00
|
|
|
void furi_hal_speaker_stop() {
|
2022-12-16 22:20:10 +00:00
|
|
|
furi_check(furi_hal_speaker_is_mine());
|
2022-04-07 15:00:45 +00:00
|
|
|
LL_TIM_DisableAllOutputs(FURI_HAL_SPEAKER_TIMER);
|
2022-03-23 17:59:20 +00:00
|
|
|
LL_TIM_DisableCounter(FURI_HAL_SPEAKER_TIMER);
|
|
|
|
}
|