STM32H7 code for virtual analog synthesiser
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

352 lines
9.9 KiB

#include <cmath>
#include <cstdio>
#include <algorithm>
#include "wm8994.h"
#include "main.h"
#include "synth.h"
#define AUDIO_I2C_ADDRESS 0x34
#define SAMPLE_RATE 96000
#define DMA_BUFFER_SIZE 64
#define BUFFER_SIZE (DMA_BUFFER_SIZE / 2)
SAI_HandleTypeDef hSAI;
UART_HandleTypeDef huart3;
static AUDIO_Drv_t *AudioDrv = NULL;
void *AudioCompObj;
/* Buffer location should aligned to cache line size (32 bytes) */
int32_t outputBuffer[DMA_BUFFER_SIZE] __attribute__((aligned (32)));
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART3_UART_Init(void);
static void MX_SAI1_Init(void);
Synth synth;
uint32_t counter = 0;
int baseNote = 0;
void synthesise(int32_t* buffer, size_t bufferSize) {
float synthBuffer[bufferSize];
std::fill(synthBuffer, synthBuffer + bufferSize, 0.f);
synth.tick(synthBuffer, bufferSize);
for(size_t i = 0; i < bufferSize; ++i) {
buffer[i] = (int32_t) (synthBuffer[i] * 8388608.f);
}
if(counter % 3000 == 0) {
counter = 0;
synth.noteOn(0, 57 + baseNote, 100);
synth.noteOn(0, 61 + baseNote, 100);
synth.noteOn(0, 64 + baseNote, 100);
} else if(counter % 3000 == 2500) {
synth.noteOff(0, 57 + baseNote);
synth.noteOff(0, 61 + baseNote);
synth.noteOff(0, 64 + baseNote);
baseNote = (baseNote + 1) % 13;
}
++counter;
}
/**
* @brief The application entry point.
* @retval int
*/
int main(void) {
/* Enable I-Cache */
SCB_EnableICache();
/* Enable D-Cache */
SCB_EnableDCache();
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART3_UART_Init();
MX_SAI1_Init();
/* Start the playback */
if(AudioDrv->Play(AudioCompObj) != 0) {
Error_Handler();
}
if(HAL_SAI_Transmit_DMA(&hSAI, (uint8_t *) outputBuffer, DMA_BUFFER_SIZE) != HAL_OK) {
Error_Handler();
}
printf("Ready\r\n");
while(1) {
}
}
/**
* @brief Tx Transfer completed callbacks.
* @param hsai : pointer to a SAI_HandleTypeDef structure that contains
* the configuration information for SAI module.
* @retval None
*/
void HAL_SAI_TxCpltCallback(SAI_HandleTypeDef *hSAI) {
synthesise(&outputBuffer[BUFFER_SIZE], BUFFER_SIZE);
SCB_CleanDCache_by_Addr((uint32_t*) &outputBuffer[BUFFER_SIZE], sizeof(uint32_t) * BUFFER_SIZE);
}
/**
* @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) {
synthesise(outputBuffer, BUFFER_SIZE);
SCB_CleanDCache_by_Addr((uint32_t*) outputBuffer, sizeof(uint32_t) * BUFFER_SIZE);
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void) {
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Supply configuration update enable
*/
HAL_PWREx_ConfigSupply(PWR_DIRECT_SMPS_SUPPLY);
/** Configure the main internal regulator output voltage
*/
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);
while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 2;
RCC_OscInitStruct.PLL.PLLN = 44;
RCC_OscInitStruct.PLL.PLLP = 1;
RCC_OscInitStruct.PLL.PLLQ = 1;
RCC_OscInitStruct.PLL.PLLR = 2;
RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_3;
RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
RCC_OscInitStruct.PLL.PLLFRACN = 0;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType =
RCC_CLOCKTYPE_HCLK |
RCC_CLOCKTYPE_SYSCLK |
RCC_CLOCKTYPE_PCLK1 |
RCC_CLOCKTYPE_PCLK2 |
RCC_CLOCKTYPE_D3PCLK1 |
RCC_CLOCKTYPE_D1PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK) {
Error_Handler();
}
}
/**
* @brief Register Bus IOs if component ID is OK
* @retval error status
*/
static int32_t WM8994_Probe(void) {
int32_t ret = BSP_ERROR_NONE;
WM8994_IO_t IOCtx;
static WM8994_Object_t WM8994Obj;
uint32_t wm8994_id;
/* Configure the audio driver */
IOCtx.Address = AUDIO_I2C_ADDRESS;
IOCtx.Init = BSP_I2C4_Init;
IOCtx.DeInit = BSP_I2C4_DeInit;
IOCtx.ReadReg = BSP_I2C4_ReadReg16;
IOCtx.WriteReg = BSP_I2C4_WriteReg16;
IOCtx.GetTick = BSP_GetTick;
if(WM8994_RegisterBusIO(&WM8994Obj, &IOCtx) != WM8994_OK) {
ret = BSP_ERROR_BUS_FAILURE;
} else if(WM8994_Reset(&WM8994Obj) != WM8994_OK) {
ret = BSP_ERROR_COMPONENT_FAILURE;
} else if(WM8994_ReadID(&WM8994Obj, &wm8994_id) != WM8994_OK) {
ret = BSP_ERROR_COMPONENT_FAILURE;
} else if(wm8994_id != WM8994_ID) {
ret = BSP_ERROR_UNKNOWN_COMPONENT;
} else {
AudioDrv = (AUDIO_Drv_t *) &WM8994_Driver;
AudioCompObj = &WM8994Obj;
}
return ret;
}
/**
* @brief SAI1 Initialization Function
* @param None
* @retval None
*/
static void MX_SAI1_Init(void) {
/* Initialize SAI */
__HAL_SAI_RESET_HANDLE_STATE(&hSAI);
hSAI.Instance = SAI1_Block_B;
__HAL_SAI_DISABLE(&hSAI);
hSAI.Init.AudioMode = SAI_MODEMASTER_TX;
hSAI.Init.Synchro = SAI_ASYNCHRONOUS;
hSAI.Init.OutputDrive = SAI_OUTPUTDRIVE_ENABLE;
hSAI.Init.NoDivider = SAI_MASTERDIVIDER_ENABLE;
hSAI.Init.FIFOThreshold = SAI_FIFOTHRESHOLD_1QF;
hSAI.Init.AudioFrequency = SAI_AUDIO_FREQUENCY_96K;
hSAI.Init.Protocol = SAI_FREE_PROTOCOL;
hSAI.Init.DataSize = SAI_DATASIZE_24;
hSAI.Init.FirstBit = SAI_FIRSTBIT_MSB;
hSAI.Init.ClockStrobing = SAI_CLOCKSTROBING_FALLINGEDGE;
hSAI.Init.SynchroExt = SAI_SYNCEXT_DISABLE;
hSAI.Init.MonoStereoMode = SAI_STEREOMODE;
hSAI.Init.CompandingMode = SAI_NOCOMPANDING;
hSAI.Init.TriState = SAI_OUTPUT_NOTRELEASED;
hSAI.Init.MckOverSampling = SAI_MCK_OVERSAMPLING_DISABLE;
hSAI.Init.PdmInit.Activation = DISABLE;
hSAI.FrameInit.FrameLength = 64;
hSAI.FrameInit.ActiveFrameLength = 32;
hSAI.FrameInit.FSDefinition = SAI_FS_CHANNEL_IDENTIFICATION;
hSAI.FrameInit.FSPolarity = SAI_FS_ACTIVE_LOW;
hSAI.FrameInit.FSOffset = SAI_FS_BEFOREFIRSTBIT;
hSAI.SlotInit.FirstBitOffset = 0;
hSAI.SlotInit.SlotSize = SAI_SLOTSIZE_DATASIZE;
hSAI.SlotInit.SlotNumber = 2;
hSAI.SlotInit.SlotActive = (SAI_SLOTACTIVE_0 | SAI_SLOTACTIVE_1);
if (HAL_SAI_Init(&hSAI) != HAL_OK) {
Error_Handler();
}
/* Enable SAI to generate clock used by audio driver */
__HAL_SAI_ENABLE(&hSAI);
WM8994_Probe();
/* Fill codec_init structure */
WM8994_Init_t codec_init;
codec_init.InputDevice = WM8994_IN_NONE;
codec_init.OutputDevice = WM8994_OUT_HEADPHONE;
codec_init.Frequency = SAMPLE_RATE;
codec_init.Resolution = WM8994_RESOLUTION_24b; /* Not used */
codec_init.Volume = 0x39; // 0 dB
/* Initialize the codec internal registers */
if(AudioDrv->Init(AudioCompObj, &codec_init) < 0) {
Error_Handler();
}
}
/**
* @brief USART3 Initialization Function
* @param None
* @retval None
*/
static void MX_USART3_UART_Init(void) {
huart3.Instance = USART3;
huart3.Init.BaudRate = 115200;
huart3.Init.WordLength = UART_WORDLENGTH_8B;
huart3.Init.StopBits = UART_STOPBITS_1;
huart3.Init.Parity = UART_PARITY_NONE;
huart3.Init.Mode = UART_MODE_TX_RX;
huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart3.Init.OverSampling = UART_OVERSAMPLING_16;
huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart3.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if(HAL_UART_Init(&huart3) != HAL_OK) {
Error_Handler();
}
if(HAL_UARTEx_SetTxFifoThreshold(&huart3, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK) {
Error_Handler();
}
if(HAL_UARTEx_SetRxFifoThreshold(&huart3, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK) {
Error_Handler();
}
if (HAL_UARTEx_DisableFifoMode(&huart3) != HAL_OK) {
Error_Handler();
}
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void) {
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
}
extern "C" {
int __io_putchar(int ch) {
HAL_UART_Transmit(&huart3, (uint8_t *) &ch, 1, 0xFFFF);
return ch;
}
}
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void) {
/* User can add his own implementation to report the HAL error return state */
printf("Error\r\n");
__disable_irq();
while(1) {
}
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line) {
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
}
#endif /* USE_FULL_ASSERT */