feat: H7 support (#5738)
Co-authored-by: Richard Li <richard.li@ces.hk> Co-authored-by: philmoz <phil.a.mitchell@gmail.com> Co-authored-by: raphaelcoeffic <1050031+raphaelcoeffic@users.noreply.github.com>
This commit is contained in:
co-authored by
Richard Li
philmoz
raphaelcoeffic
parent
5a15ffb017
commit
3d25c2a0f8
@@ -19,16 +19,18 @@
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#include "vs1053b.h"
|
||||
|
||||
#include "stm32_hal_ll.h"
|
||||
#include "stm32_gpio_driver.h"
|
||||
#include "stm32_gpio.h"
|
||||
#include "stm32_spi.h"
|
||||
|
||||
#include "hal/gpio.h"
|
||||
#include "hal/audio_driver.h"
|
||||
|
||||
#include "board.h"
|
||||
#include "edgetx.h"
|
||||
#include "timers_driver.h"
|
||||
|
||||
#define VS_WRITE_COMMAND 0x02
|
||||
#define VS_READ_COMMAND 0x03
|
||||
@@ -66,101 +68,80 @@
|
||||
#define SM_LINE1 0x4000
|
||||
#define SM_CLK_RANGE 0x8000
|
||||
|
||||
#define SPI_SPEED_2 0
|
||||
#define SPI_SPEED_4 1
|
||||
#define SPI_SPEED_8 2
|
||||
#define SPI_SPEED_16 3
|
||||
#define SPI_SPEED_32 4
|
||||
#define SPI_SPEED_64 5
|
||||
#define SPI_SPEED_128 6
|
||||
#define SPI_SPEED_256 7
|
||||
#define SPI_LOW_SPEED 1755000
|
||||
#define SPI_HIGH_SPEED 9216000
|
||||
|
||||
#define MP3_BUFFER_SIZE 32
|
||||
#define VS1053_BUFFER_SIZE 32
|
||||
|
||||
#define CS_HIGH() stm32_spi_unselect(&_audio_spi)
|
||||
#define CS_LOW() stm32_spi_select(&_audio_spi)
|
||||
#define XDCS_HIGH() do { AUDIO_XDCS_GPIO->BSRR = AUDIO_XDCS_GPIO_PIN; } while (0)
|
||||
#define XDCS_LOW() do { AUDIO_XDCS_GPIO->BSRR = AUDIO_XDCS_GPIO_PIN << 16; } while (0)
|
||||
#define RST_HIGH() do { AUDIO_RST_GPIO->BSRR = AUDIO_RST_GPIO_PIN; } while (0)
|
||||
#define RST_LOW() do { AUDIO_RST_GPIO->BSRR = AUDIO_RST_GPIO_PIN << 16; } while (0)
|
||||
#define XDCS_HIGH() _xdcs_high()
|
||||
#define XDCS_LOW() _xdcs_low()
|
||||
#define READ_DREQ() _read_dreq()
|
||||
|
||||
#define READ_DREQ() (LL_GPIO_IsInputPinSet(AUDIO_DREQ_GPIO, AUDIO_DREQ_GPIO_PIN))
|
||||
static const vs1053b_t* _instance = nullptr;
|
||||
|
||||
static const stm32_spi_t _audio_spi = {
|
||||
.SPIx = AUDIO_SPI,
|
||||
.SPI_GPIOx = AUDIO_SPI_SCK_GPIO,
|
||||
.SPI_Pins = AUDIO_SPI_SCK_GPIO_PIN | AUDIO_SPI_MISO_GPIO_PIN | AUDIO_SPI_MOSI_GPIO_PIN,
|
||||
.CS_GPIOx = AUDIO_CS_GPIO,
|
||||
.CS_Pin = AUDIO_CS_GPIO_PIN,
|
||||
};
|
||||
// mute support
|
||||
static bool _is_muted = false;
|
||||
static uint32_t _last_play_ts = 0;
|
||||
|
||||
static void audioSpiInit(void)
|
||||
static uint8_t* _audio_buffer = nullptr;
|
||||
static uint32_t _audio_buffer_len = 0;
|
||||
|
||||
// volume is set asynchronously:
|
||||
// - first saved in _async_volume
|
||||
// - then vs1053b_update_volume() is called periodically
|
||||
// to actually send the new setting to the device.
|
||||
static int16_t _async_volume = -1;
|
||||
|
||||
static inline void _xdcs_high() { gpio_set(_instance->XDCS); }
|
||||
static inline void _xdcs_low() { gpio_clear(_instance->XDCS); }
|
||||
static inline uint32_t _read_dreq() { return gpio_read(_instance->DREQ); }
|
||||
|
||||
static inline void _reset_high()
|
||||
{
|
||||
stm32_gpio_enable_clock(AUDIO_XDCS_GPIO);
|
||||
stm32_gpio_enable_clock(AUDIO_RST_GPIO);
|
||||
stm32_gpio_enable_clock(AUDIO_DREQ_GPIO);
|
||||
|
||||
LL_GPIO_InitTypeDef pinInit;
|
||||
LL_GPIO_StructInit(&pinInit);
|
||||
|
||||
pinInit.Mode = LL_GPIO_MODE_OUTPUT;
|
||||
pinInit.Pin = AUDIO_XDCS_GPIO_PIN;
|
||||
LL_GPIO_Init(AUDIO_XDCS_GPIO, &pinInit);
|
||||
|
||||
pinInit.Pin = AUDIO_RST_GPIO_PIN;
|
||||
LL_GPIO_Init(AUDIO_RST_GPIO, &pinInit);
|
||||
|
||||
pinInit.Pin = AUDIO_DREQ_GPIO_PIN;
|
||||
pinInit.Mode = LL_GPIO_MODE_INPUT;
|
||||
LL_GPIO_Init(AUDIO_DREQ_GPIO, &pinInit);
|
||||
|
||||
stm32_spi_init(&_audio_spi, LL_SPI_DATAWIDTH_8BIT);
|
||||
}
|
||||
|
||||
// static void audioWaitReady()
|
||||
// {
|
||||
// // The audio amp needs ~2s to start
|
||||
// RTOS_WAIT_MS(2000); // 2s
|
||||
// }
|
||||
|
||||
static void audioSpiSetSpeed(uint8_t speed)
|
||||
{
|
||||
AUDIO_SPI->CR1 &= 0xFFC7; // Fsck=Fcpu/256
|
||||
switch (speed) {
|
||||
case SPI_SPEED_2:
|
||||
AUDIO_SPI->CR1 |= 0x00 << 3; // Fsck=Fpclk/2=36Mhz
|
||||
break;
|
||||
case SPI_SPEED_4:
|
||||
AUDIO_SPI->CR1 |= 0x01 << 3; // Fsck=Fpclk/4=18Mhz
|
||||
break;
|
||||
case SPI_SPEED_8:
|
||||
AUDIO_SPI->CR1 |= 0x02 << 3; // Fsck=Fpclk/8=9Mhz
|
||||
break;
|
||||
case SPI_SPEED_16:
|
||||
AUDIO_SPI->CR1 |= 0x03 << 3; // Fsck=Fpclk/16=4.5Mhz
|
||||
break;
|
||||
case SPI_SPEED_32:
|
||||
AUDIO_SPI->CR1 |= 0x04 << 3; // Fsck=Fpclk/32=2.25Mhz
|
||||
break;
|
||||
case SPI_SPEED_64:
|
||||
AUDIO_SPI->CR1 |= 0x05 << 3; // Fsck=Fpclk/16=1.125Mhz
|
||||
break;
|
||||
case SPI_SPEED_128:
|
||||
AUDIO_SPI->CR1 |= 0x06 << 3; // Fsck=Fpclk/16=562.5Khz
|
||||
break;
|
||||
case SPI_SPEED_256:
|
||||
AUDIO_SPI->CR1 |= 0x07 << 3; // Fsck=Fpclk/16=281.25Khz
|
||||
break;
|
||||
if (_instance->RST != GPIO_UNDEF) {
|
||||
gpio_set(_instance->RST);
|
||||
}
|
||||
AUDIO_SPI->CR1 |= 0x01 << 6;
|
||||
}
|
||||
|
||||
static uint8_t audioSpiReadWriteByte(uint8_t value)
|
||||
static inline void _reset_low()
|
||||
{
|
||||
return stm32_spi_transfer_byte(&_audio_spi, value);
|
||||
if (_instance->RST != GPIO_UNDEF) {
|
||||
gpio_clear(_instance->RST);
|
||||
}
|
||||
}
|
||||
|
||||
static uint8_t audioWaitDreq(int32_t delay_us)
|
||||
static inline void _set_mute_pin(bool muted)
|
||||
{
|
||||
if (_instance->MUTE != GPIO_UNDEF) {
|
||||
_is_muted = muted;
|
||||
bool inv = _instance->flags & VS1053B_MUTE_INVERTED;
|
||||
gpio_write(_instance->MUTE, inv ? !muted : muted);
|
||||
}
|
||||
}
|
||||
|
||||
static void vs1053b_gpio_init(void)
|
||||
{
|
||||
gpio_init(_instance->XDCS, GPIO_OUT, GPIO_PIN_SPEED_HIGH);
|
||||
gpio_init(_instance->DREQ, GPIO_IN, GPIO_PIN_SPEED_HIGH);
|
||||
|
||||
if (_instance->RST != GPIO_UNDEF) {
|
||||
gpio_init(_instance->RST, GPIO_OUT, GPIO_PIN_SPEED_HIGH);
|
||||
}
|
||||
|
||||
if (_instance->MUTE != GPIO_UNDEF) {
|
||||
gpio_init(_instance->MUTE, GPIO_OUT, GPIO_PIN_SPEED_HIGH);
|
||||
_set_mute_pin(true);
|
||||
}
|
||||
|
||||
stm32_spi_init(_instance->spi, LL_SPI_DATAWIDTH_8BIT);
|
||||
}
|
||||
|
||||
static uint8_t vs1053b_spi_rw_byte(uint8_t value)
|
||||
{
|
||||
return stm32_spi_transfer_byte(_instance->spi, value);
|
||||
}
|
||||
|
||||
static uint8_t vs1053b_wait_dreq(int32_t delay_us)
|
||||
{
|
||||
while (READ_DREQ() == 0) {
|
||||
if (delay_us-- == 0) return 0;
|
||||
@@ -169,115 +150,120 @@ static uint8_t audioWaitDreq(int32_t delay_us)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static uint16_t audioSpiReadReg(uint8_t address)
|
||||
static uint16_t vs1053b_read_reg(uint8_t address)
|
||||
{
|
||||
if (!audioWaitDreq(100))
|
||||
if (!vs1053b_wait_dreq(100))
|
||||
return 0;
|
||||
|
||||
audioSpiSetSpeed(SPI_SPEED_64);
|
||||
stm32_spi_set_max_baudrate(_instance->spi, SPI_LOW_SPEED);
|
||||
XDCS_HIGH();
|
||||
CS_LOW();
|
||||
audioSpiReadWriteByte(VS_READ_COMMAND);
|
||||
audioSpiReadWriteByte(address);
|
||||
uint16_t result = audioSpiReadWriteByte(0xff) << 8;
|
||||
result += audioSpiReadWriteByte(0xff);
|
||||
stm32_spi_select(_instance->spi);
|
||||
vs1053b_spi_rw_byte(VS_READ_COMMAND);
|
||||
vs1053b_spi_rw_byte(address);
|
||||
volatile uint16_t result = vs1053b_spi_rw_byte(0xff) << 8;
|
||||
result += vs1053b_spi_rw_byte(0xff);
|
||||
delay_01us(100); // 10us
|
||||
CS_HIGH();
|
||||
audioSpiSetSpeed(SPI_SPEED_8);
|
||||
stm32_spi_unselect(_instance->spi);
|
||||
|
||||
stm32_spi_set_max_baudrate(_instance->spi, SPI_HIGH_SPEED);
|
||||
return result;
|
||||
}
|
||||
|
||||
static uint8_t audioSpiWriteCmd(uint8_t address, uint16_t data)
|
||||
static uint8_t vs1053b_write_cmd(uint8_t address, uint16_t data)
|
||||
{
|
||||
if (!audioWaitDreq(100))
|
||||
if (!vs1053b_wait_dreq(100))
|
||||
return 0;
|
||||
|
||||
audioSpiSetSpeed(SPI_SPEED_64);
|
||||
stm32_spi_set_max_baudrate(_instance->spi, SPI_LOW_SPEED);
|
||||
XDCS_HIGH();
|
||||
CS_LOW();
|
||||
audioSpiReadWriteByte(VS_WRITE_COMMAND);
|
||||
audioSpiReadWriteByte(address);
|
||||
audioSpiReadWriteByte(data >> 8);
|
||||
audioSpiReadWriteByte(data);
|
||||
stm32_spi_select(_instance->spi);
|
||||
vs1053b_spi_rw_byte(VS_WRITE_COMMAND);
|
||||
vs1053b_spi_rw_byte(address);
|
||||
vs1053b_spi_rw_byte(data >> 8);
|
||||
vs1053b_spi_rw_byte(data);
|
||||
delay_01us(50); // 5us
|
||||
CS_HIGH();
|
||||
audioSpiSetSpeed(SPI_SPEED_8);
|
||||
stm32_spi_unselect(_instance->spi);
|
||||
stm32_spi_set_max_baudrate(_instance->spi, SPI_HIGH_SPEED);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void audioResetDecodeTime(void)
|
||||
static void vs1053b_reset_decode_time()
|
||||
{
|
||||
audioSpiWriteCmd(SPI_DECODE_TIME, 0x0000);
|
||||
audioSpiWriteCmd(SPI_DECODE_TIME, 0x0000);
|
||||
vs1053b_write_cmd(SPI_DECODE_TIME, 0x0000);
|
||||
vs1053b_write_cmd(SPI_DECODE_TIME, 0x0000);
|
||||
}
|
||||
|
||||
static uint8_t audioHardReset(void)
|
||||
static uint8_t vs1053b_hard_reset()
|
||||
{
|
||||
XDCS_HIGH();
|
||||
CS_HIGH();
|
||||
RST_LOW();
|
||||
stm32_spi_unselect(_instance->spi);
|
||||
_reset_low();
|
||||
delay_ms(100); // 100ms
|
||||
RST_HIGH();
|
||||
_reset_high();
|
||||
|
||||
if (!audioWaitDreq(5000))
|
||||
if (!vs1053b_wait_dreq(5000))
|
||||
return 0;
|
||||
|
||||
delay_ms(20); // 20ms
|
||||
return 1;
|
||||
}
|
||||
|
||||
static uint8_t audioSoftReset(void)
|
||||
static uint8_t vs1053b_soft_reset()
|
||||
{
|
||||
audioSpiSetSpeed(SPI_SPEED_64);
|
||||
if (!audioWaitDreq(100))
|
||||
stm32_spi_set_max_baudrate(_instance->spi, SPI_LOW_SPEED);
|
||||
if (!vs1053b_wait_dreq(100))
|
||||
return 0;
|
||||
|
||||
audioSpiReadWriteByte(0x00); // start the transfer
|
||||
vs1053b_spi_rw_byte(0x00); // start the transfer
|
||||
|
||||
uint8_t retry = 0;
|
||||
uint16_t mode = SM_SDINEW;
|
||||
while (audioSpiReadReg(SPI_MODE) != mode && retry < 100) {
|
||||
while (vs1053b_read_reg(SPI_MODE) != mode && retry < 100) {
|
||||
retry++;
|
||||
audioSpiWriteCmd(SPI_MODE, mode | SM_RESET);
|
||||
vs1053b_write_cmd(SPI_MODE, mode | SM_RESET);
|
||||
delay_ms(2);
|
||||
}
|
||||
|
||||
// wait for set up successful
|
||||
retry = 0;
|
||||
while (audioSpiReadReg(SPI_CLOCKF) != 0x9800 && retry < 100) {
|
||||
while (vs1053b_read_reg(SPI_CLOCKF) != 0x9800 && retry < 100) {
|
||||
retry++;
|
||||
audioSpiWriteCmd(SPI_CLOCKF, 0x9800);
|
||||
vs1053b_write_cmd(SPI_CLOCKF, 0x9800);
|
||||
}
|
||||
|
||||
audioResetDecodeTime(); // reset the decoding time
|
||||
audioSpiSetSpeed(SPI_SPEED_8);
|
||||
vs1053b_reset_decode_time(); // reset the decoding time
|
||||
stm32_spi_set_max_baudrate(_instance->spi, SPI_HIGH_SPEED);
|
||||
|
||||
XDCS_LOW();
|
||||
audioSpiReadWriteByte(0X0);
|
||||
audioSpiReadWriteByte(0X0);
|
||||
audioSpiReadWriteByte(0X0);
|
||||
audioSpiReadWriteByte(0X0);
|
||||
vs1053b_spi_rw_byte(0X0);
|
||||
vs1053b_spi_rw_byte(0X0);
|
||||
vs1053b_spi_rw_byte(0X0);
|
||||
vs1053b_spi_rw_byte(0X0);
|
||||
delay_01us(100); // 10us
|
||||
XDCS_HIGH();
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static uint32_t audioSpiWriteData(const uint8_t * buffer, uint32_t size)
|
||||
static uint32_t vs1053b_send_data(const uint8_t * buffer, uint32_t size)
|
||||
{
|
||||
XDCS_LOW();
|
||||
|
||||
uint32_t index = 0;
|
||||
while (index < size && READ_DREQ() != 0) {
|
||||
for (int i=0; i<MP3_BUFFER_SIZE && index<size; i++) {
|
||||
audioSpiReadWriteByte(buffer[index++]);
|
||||
for (int i = 0; i < VS1053_BUFFER_SIZE && index < size; i++) {
|
||||
vs1053b_spi_rw_byte(buffer[index++]);
|
||||
}
|
||||
}
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
static void audioSpiWriteBuffer(const uint8_t * buffer, uint32_t size)
|
||||
static void vs1053b_send_buffer(const uint8_t * buffer, uint32_t size)
|
||||
{
|
||||
const uint8_t * p = buffer;
|
||||
while (size > 0) {
|
||||
uint32_t written = audioSpiWriteData(p, size);
|
||||
uint32_t written = vs1053b_send_data(p, size);
|
||||
p += written;
|
||||
size -= written;
|
||||
}
|
||||
@@ -304,150 +290,117 @@ const uint8_t RiffHeader[] = {
|
||||
0x00, 0x00, 0x00, 0x00, // data
|
||||
};
|
||||
|
||||
static void audioSendRiffHeader()
|
||||
static void vs1053b_send_riff_header()
|
||||
{
|
||||
audioSpiWriteBuffer(RiffHeader, sizeof(RiffHeader));
|
||||
vs1053b_send_buffer(RiffHeader, sizeof(RiffHeader));
|
||||
}
|
||||
|
||||
#if defined(AUDIO_MUTE_GPIO)
|
||||
static inline void setMutePin(bool enabled)
|
||||
static void vs1053b_update_volume()
|
||||
{
|
||||
#if defined(INVERTED_MUTE_PIN)
|
||||
enabled = !enabled;
|
||||
#endif
|
||||
gpio_write(AUDIO_MUTE_GPIO, enabled);
|
||||
}
|
||||
|
||||
static inline bool getMutePin(void)
|
||||
{
|
||||
bool muted = gpio_read(AUDIO_MUTE_GPIO) ? 1 : 0;
|
||||
#if defined(INVERTED_MUTE_PIN)
|
||||
muted = !muted;
|
||||
#endif
|
||||
return muted;
|
||||
}
|
||||
|
||||
static void audioMute()
|
||||
{
|
||||
#if defined(AUDIO_UNMUTE_DELAY)
|
||||
tmr10ms_t now = get_tmr10ms();
|
||||
if (!audioQueue.lastAudioPlayTime) {
|
||||
// we start the mute delay now
|
||||
audioQueue.lastAudioPlayTime = now;
|
||||
} else if (now - audioQueue.lastAudioPlayTime > AUDIO_MUTE_DELAY / 10) {
|
||||
// delay expired, we may mute
|
||||
setMutePin(true);
|
||||
if (_async_volume >= 0) {
|
||||
uint8_t value = _async_volume;
|
||||
vs1053b_write_cmd(SPI_VOL, (value << 8) + value);
|
||||
_async_volume = -1;
|
||||
}
|
||||
#else
|
||||
// mute
|
||||
setMutePin(true);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void audioUnmute()
|
||||
static void vs1053b_mute()
|
||||
{
|
||||
if (_is_muted) return;
|
||||
|
||||
if (_instance->mute_delay_ms) {
|
||||
uint32_t now = timersGetMsTick();
|
||||
if (!_last_play_ts) {
|
||||
// we start the mute delay now
|
||||
_last_play_ts = now;
|
||||
} else if (now - _last_play_ts < _instance->mute_delay_ms) {
|
||||
// delay not expired, we may not mute yet
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
_set_mute_pin(true);
|
||||
}
|
||||
|
||||
static void vs1053b_unmute()
|
||||
{
|
||||
if(isFunctionActive(FUNCTION_DISABLE_AUDIO_AMP)) {
|
||||
setMutePin(true);
|
||||
_set_mute_pin(true);
|
||||
return;
|
||||
}
|
||||
|
||||
#if defined(AUDIO_UNMUTE_DELAY)
|
||||
// if muted
|
||||
if (getMutePin()) {
|
||||
// ..un-mute
|
||||
setMutePin(false);
|
||||
RTOS_WAIT_MS(AUDIO_UNMUTE_DELAY);
|
||||
if (_instance->unmute_delay_ms) {
|
||||
// if muted
|
||||
if (_is_muted) {
|
||||
// ..un-mute
|
||||
_set_mute_pin(false);
|
||||
RTOS_WAIT_MS(_instance->unmute_delay_ms);
|
||||
}
|
||||
// reset the mute delay
|
||||
_last_play_ts = 0;
|
||||
} else {
|
||||
_set_mute_pin(false);
|
||||
}
|
||||
// reset the mute delay
|
||||
audioQueue.lastAudioPlayTime = 0;
|
||||
#else
|
||||
setMutePin(false);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(PCBX12S)
|
||||
static void audioShutdownInit()
|
||||
void vs1053b_init(const vs1053b_t* dev)
|
||||
{
|
||||
gpio_init(AUDIO_SHUTDOWN_GPIO, GPIO_OUT, GPIO_PIN_SPEED_LOW);
|
||||
_instance = dev;
|
||||
|
||||
// we never RESET it, there is a 2s delay on STARTUP
|
||||
gpio_set(AUDIO_SHUTDOWN_GPIO);
|
||||
}
|
||||
#endif
|
||||
vs1053b_gpio_init();
|
||||
vs1053b_hard_reset();
|
||||
vs1053b_soft_reset();
|
||||
|
||||
void audioInit()
|
||||
{
|
||||
#if defined(AUDIO_MUTE_GPIO)
|
||||
// Mute before init anything
|
||||
gpio_init(AUDIO_MUTE_GPIO, GPIO_OUT, GPIO_PIN_SPEED_LOW);
|
||||
setMutePin(true);
|
||||
#endif
|
||||
|
||||
#if defined(PCBX12S)
|
||||
audioShutdownInit();
|
||||
#endif
|
||||
|
||||
audioSpiInit();
|
||||
audioHardReset();
|
||||
audioSoftReset();
|
||||
audioSpiSetSpeed(SPI_SPEED_8);
|
||||
stm32_spi_set_max_baudrate(_instance->spi, SPI_HIGH_SPEED);
|
||||
delay_ms(1); // 1ms
|
||||
audioSendRiffHeader();
|
||||
}
|
||||
|
||||
static uint8_t * currentBuffer = nullptr;
|
||||
static uint32_t currentSize = 0;
|
||||
static int16_t newVolume = -1;
|
||||
vs1053b_send_riff_header();
|
||||
}
|
||||
|
||||
static void set_volume(uint8_t volume)
|
||||
{
|
||||
newVolume = volume;
|
||||
_async_volume = volume;
|
||||
}
|
||||
|
||||
static void audioSetCurrentBuffer(const AudioBuffer * buffer)
|
||||
{
|
||||
if (buffer) {
|
||||
currentBuffer = (uint8_t *)buffer->data;
|
||||
currentSize = buffer->size * 2;
|
||||
_audio_buffer = (uint8_t *)buffer->data;
|
||||
_audio_buffer_len = buffer->size * 2;
|
||||
}
|
||||
else {
|
||||
currentBuffer = nullptr;
|
||||
currentSize = 0;
|
||||
_audio_buffer = nullptr;
|
||||
_audio_buffer_len = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static const uint8_t nullBytes[32] = {0};
|
||||
|
||||
void audioConsumeCurrentBuffer()
|
||||
{
|
||||
if (newVolume >= 0) {
|
||||
uint8_t value = newVolume;
|
||||
audioSpiWriteCmd(SPI_VOL, (value << 8) + value);
|
||||
// audioSendRiffHeader();
|
||||
newVolume = -1;
|
||||
}
|
||||
vs1053b_update_volume();
|
||||
|
||||
if (!currentBuffer) {
|
||||
if (!_audio_buffer) {
|
||||
audioSetCurrentBuffer(audioQueue.buffersFifo.getNextFilledBuffer());
|
||||
}
|
||||
|
||||
if (currentBuffer) {
|
||||
#if defined(AUDIO_MUTE_GPIO)
|
||||
audioUnmute();
|
||||
#endif
|
||||
uint32_t written = audioSpiWriteData(currentBuffer, currentSize);
|
||||
currentBuffer += written;
|
||||
currentSize -= written;
|
||||
if (currentSize == 0) {
|
||||
if (_audio_buffer) {
|
||||
vs1053b_unmute();
|
||||
|
||||
uint32_t written = vs1053b_send_data(_audio_buffer, _audio_buffer_len);
|
||||
_audio_buffer += written;
|
||||
_audio_buffer_len -= written;
|
||||
if (_audio_buffer_len == 0) {
|
||||
audioQueue.buffersFifo.freeNextFilledBuffer();
|
||||
currentBuffer = nullptr;
|
||||
currentSize = 0;
|
||||
_audio_buffer = nullptr;
|
||||
_audio_buffer_len = 0;
|
||||
}
|
||||
} else {
|
||||
if(READ_DREQ()) {
|
||||
vs1053b_send_data(nullBytes, sizeof(nullBytes));
|
||||
}
|
||||
vs1053b_mute();
|
||||
}
|
||||
#if defined(AUDIO_MUTE_GPIO)
|
||||
else {
|
||||
audioMute();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// adjust this value for a volume level just above the silence
|
||||
@@ -462,10 +415,10 @@ void audioSetVolume(uint8_t volume)
|
||||
}
|
||||
// maximum volume is 0x00 and total silence is 0xFE
|
||||
if (volume == 0) {
|
||||
set_volume(0xFE); // silence
|
||||
}
|
||||
else {
|
||||
uint32_t vol = (VOLUME_MIN_DB * 2) - ((uint32_t)volume * (VOLUME_MIN_DB * 2)) / VOLUME_LEVEL_MAX;
|
||||
set_volume(0xFE); // silence
|
||||
} else {
|
||||
uint32_t vol = (VOLUME_MIN_DB * 2) -
|
||||
((uint32_t)volume * (VOLUME_MIN_DB * 2)) / VOLUME_LEVEL_MAX;
|
||||
set_volume(vol);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user