Skip to content

Commit

Permalink
Use DMA for RawI2S
Browse files Browse the repository at this point in the history
  • Loading branch information
sreckamp committed Feb 1, 2024
1 parent d251c16 commit 14fe364
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 46 deletions.
98 changes: 52 additions & 46 deletions benchmark/interface/Application/Audio/RawI2SWaveSink.cpp
Original file line number Diff line number Diff line change
@@ -1,49 +1,50 @@
#include "RawI2SWaveSink.hpp"

//#include "stm32h5xx_hal_gpio.h"
#include "stm32h573i_discovery_audio.h"
#include "gpdma.h"
#include "linked_list.h"
#include "sai.h"
#include "stm32h5xx_hal_sai.h"
#include "tx_semaphore.h"

extern DMA_QListTypeDef RawSAIQueue;

static INT active_buffer = -1;
static TX_SEMAPHORE buffer_semaphore;
//static DMA_HandleTypeDef handle_GPDMA1_Channel7;

//static SAI_HandleTypeDef hsai_BlockA2;

/**
* @brief Tx Transfer completed callbacks.
* @param hsai : pointer to a SAI_HandleTypeDef structure that contains
* the configuration information for SAI module.
* @retval None
*/
//void BSP_AUDIO_OUT_TransferComplete_CallBack(uint32_t instance)
//{
// if(instance == 0)
// {
// active_buffer = 1;
// tx_semaphore_ceiling_put(&buffer_semaphore, 1);
// }
//}
//
///**
// * @brief Tx Transfer Half completed callbacks
// * @param hsai : pointer to a SAI_HandleTypeDef structure that contains
// * the configuration information for SAI module.
// * @retval None
// */
//void BSP_AUDIO_OUT_HalfTransfer_CallBack(uint32_t instance)
//{
// if(instance == 0)
// {
// active_buffer = 0;
// tx_semaphore_ceiling_put(&buffer_semaphore, 1);
// }
//}
void HAL_SAI_TxCpltCallback(SAI_HandleTypeDef *hsai)
{
if(hsai == &hsai_BlockB1)
{
active_buffer = 1;
tx_semaphore_ceiling_put(&buffer_semaphore, 1);
}
}

/**
* @brief Tx Transfer Half completed callbacks
* @param hsai : pointer to a SAI_HandleTypeDef structure that contains
* the configuration information for SAI module.
* @retval None
*/
void HAL_SAI_TxHalfCpltCallback(SAI_HandleTypeDef *hsai)
{
if(hsai == &hsai_BlockB1)
{
active_buffer = 0;
tx_semaphore_ceiling_put(&buffer_semaphore, 1);
}
}

namespace Audio
{
RawI2SWaveSink::RawI2SWaveSink(Tasks::TaskRunner &runner, TX_BYTE_POOL &byte_pool)
: WaveSink(runner, byte_pool)
: WaveSink(runner, byte_pool), state(RESET)
{
if(buffer_semaphore.tx_semaphore_id != TX_SEMAPHORE_ID)
{
Expand All @@ -54,41 +55,46 @@ namespace Audio

PlayerState RawI2SWaveSink::GetState()
{
ULONG state;
BSP_AUDIO_OUT_GetState(0, &state);
return state == AUDIO_OUT_STATE_RESET
? RESET
: (state == AUDIO_OUT_STATE_STOP ? STOPPED : UNKNOWN);
return state;
}

PlayerState RawI2SWaveSink::Initialize()
{
BSP_AUDIO_Init_t init;
init.BitsPerSample = AUDIO_RESOLUTION_16B;
init.ChannelsNbr = 2;
init.Device = AUDIO_OUT_DEVICE_HEADPHONE;
init.SampleRate = AUDIO_FREQUENCY_44K;
init.Volume = 80;
__HAL_LINKDMA(&hsai_BlockB1, hdmatx, handle_GPDMA1_Channel7);
MX_RawSAIQueue_Config();
HAL_DMAEx_List_SetCircularMode(&RawSAIQueue);

BSP_AUDIO_OUT_Init(0, &init);
state = STOPPED;
return GetState();
}

void RawI2SWaveSink::Configure(const WaveSource &source)
{
BSP_AUDIO_OUT_SetBitsPerSample(0, source.GetSampleSize());
BSP_AUDIO_OUT_SetChannelsNbr(0, source.GetChannelCount());
BSP_AUDIO_OUT_SetSampleRate(0, source.GetFrequency());
// BSP_AUDIO_OUT_SetBitsPerSample(0, source.GetSampleSize());
// BSP_AUDIO_OUT_SetChannelsNbr(0, source.GetChannelCount());
// BSP_AUDIO_OUT_SetSampleRate(0, source.GetFrequency());
}

PlayerResult RawI2SWaveSink::Play(UCHAR *buffer, ULONG size)
{
return BSP_AUDIO_OUT_Play(0, buffer, size) == BSP_ERROR_NONE ? SUCCESS : ERROR;
if(state == STOPPED)
{
HAL_SAI_Transmit_DMA(&hsai_BlockB1, buffer, size);
state = PLAYING;
return SUCCESS;
}
return ERROR;
}

PlayerResult RawI2SWaveSink::Stop()
{
return BSP_AUDIO_OUT_Stop(0) == BSP_ERROR_NONE ? SUCCESS : ERROR;
if(state == PLAYING)
{
HAL_SAI_DMAStop(&hsai_BlockB1);
state = STOPPED;
return SUCCESS;
}
return ERROR;
}

INT RawI2SWaveSink::WaitForActiveBuffer()
Expand Down
2 changes: 2 additions & 0 deletions benchmark/interface/Application/Audio/RawI2SWaveSink.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace Audio
PlayerResult Stop();
INT WaitForActiveBuffer();
void SetActiveBuffer(INT buffer_id);
private:
PlayerState state;
};
}

Expand Down

0 comments on commit 14fe364

Please sign in to comment.