Docs

Micro-controllers, wireless transmission and database

Head

MAX98357A I2S Sound with Nucleo STM32F446RE using STM32CubeIDE

Prerequisites

This project assumes you have already installed STM32CubeIDE. You need to have previously done a basic blink sketch with Nucleo STM32F446RE using STM32CubeIDE. I have made a complete video from installing STM32CubeIDE to LED blink program. You can watch it by clicking this link. https://www.youtube.com/watch?v=oAwZ0cjlmN8

Wiring Diagram

Diagram

STM32CubeIDE Settings

Click Multimedia → Click I2S2 →

Mode set to Half-Duplex Master

Configuration → Parameter Settings →

Transmission Mode set to Mode Master Transmit

Data and Frame Format set to 16 Bits data on 16 bits frame

Selected Audia Frequency 16KHz

Click Timer → Click TIM2 →

Clock Source set to Internal Clock

Configuration → Parameter Settings →

Prescaler set to 84-1 (APB1Timer-1)

Counter Period 31

Configuration → NVIC Settings → TIM2 global interrupt (Tick)

Additional code on top of STM32CubeIDE generated code

helloworld.h
/* USER CODE BEGIN PV */
// Put the contents of https://www.micropeta.com/helloworld.h below

/* USER CODE END PV */
/* USER CODE BEGIN 0 */
uint16_t txData;
int txIndex;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
  if(htim->Instance == TIM2)
  {
    txData = ((uint16_t)helloworld[txIndex+1] << 8) | helloworld[txIndex];
    txIndex = txIndex + 2;
    if(txIndex>53456) txIndex = 0;
    HAL_I2S_Transmit(&hi2s2, &txData, 1, 10);
  }
}
/* USER CODE END 0 */

  /* USER CODE BEGIN 2 */
  txIndex = 0;
  HAL_TIM_Base_Start_IT(&htim2);
  /* USER CODE END 2 */