1. Configure channel 2 of the DFSDM peripheral with system clock as fDFSDM clock source, DFSDM clock divider D =57, oversampling ratio R = 64, continuous conversion mode. Write a program to record the samples from digital microphone over 2 seconds and implement the playback of the recorded data using timer TIM7 and microcontroller internal DAC. Connect DAC output pin to the external amplifier board with a speaker. Turn on LD5 green LED to indicate recording phase, turn it off while the recorded samples are played back.

2. Determine the amount of memory buffer (in bytes) required for the specified duration of audio recording using DFSDM configuration data from question 1, if the audio samples are stored with 16-bit resolution. Show all your work.

1. To write a program that records samples from a digital microphone and plays back the recorded data, you can follow this procedure:

First, include necessary headers:
```c
#include "stm32f4xx_hal.h"
#include "stm32f4xx_hal_dfsdm.h"
#include "stm32f4xx_hal_tim.h"
#include "stm32f4xx_hal_dac.h"
#include "stm32f4xx_hal_gpio.h"
```

Next, define global variables and constants:
```c
#define SAMPLES_DURATION 2 // 2 seconds
#define DFSDM_DIVIDER 57
#define DFSDM_OVERSAMPLING 64

uint32_t Audio_Samples[SAMPLES_DURATION * 1000]; // Buffer to store samples
uint32_t Recorded_Samples = 0; // Counter for samples recorded
uint32_t Playback_Samples = 0; // Counter for samples played back
```

Next, configure the system clock, DFSDM peripheral, DAC, TIM7, and GPIOs:
```c
void SystemClock_Config(void) {
// Implement the system clock configuration here
}

void DFSDM_Config(void) {
// Configure the DFSDM peripheral here
}

void DAC_Config(void) {
// Configure the DAC peripheral here
}

void TIM7_Config(void) {
// Implement the TIM7 configuration here
}

void GPIO_Config(void) {
// Implement the GPIO configuration here
}
```

In `main()`, implement the program logic:
```c
int main(void) {

HAL_Init();
SystemClock_Config();
GPIO_Config();
DFSDM_Config();
DAC_Config();
TIM7_Config();

// Turn on the green LED.
HAL_GPIO_WritePin(GPIO_PORT_LD5, GPIO_PIN_LD5, GPIO_PIN_SET);

// Configure and enable continuous conversion in DFSDM.
HAL_DFSDM_Start(&hDFSDM_Channel2);

// Wait for 2 seconds and record audio samples.
do {
if (HAL_DFSDM_GetFlagStatus(&hDFSDM_Channel2, DFSDM_FLAG_RDY)) {
Audio_Samples[Recorded_Samples++] = HAL_DFSDM_GetValue(&hDFSDM_Channel2) & 0xFFFF;
}
} while (Recorded_Samples < (SAMPLES_DURATION * 1000));

// Stop DFSDM conversion.
HAL_DFSDM_Stop(&hDFSDM_Channel2);

// Turn off the green LED.
HAL_GPIO_WritePin(GPIO_PORT_LD5, GPIO_PIN_LD5, GPIO_PIN_RESET);

// Configure timer TIM7.
__HAL_TIM_SET_PRESCALER(&hTIM7, (uint32_t)(((SystemCoreClock / 2) / 1000000) - 1)); // 1MHz
__HAL_TIM_SET_COUNTER(&hTIM7, 0);

// Start timer TIM7.
HAL_TIM_Base_Start(&hTIM7);

// Wait for corresponding time steps and play back the recorded samples.
while (Playback_Samples < Recorded_Samples) {
if (__HAL_TIM_GET_FLAG(&hTIM7, TIM_FLAG_UPDATE)) {
__HAL_TIM_CLEAR_FLAG(&hTIM7, TIM_FLAG_UPDATE);
HAL_DAC_SetValue(&hDAC, DAC_CHANNEL_1, DAC_ALIGN_12B_R, Audio_Samples[Playback_Samples++]);
}
}

// Stop Timer TIM7.
HAL_TIM_Base_Stop(&hTIM7);

}
```

2. To determine the amount of memory buffer (in bytes) required for the specified duration of audio recording, follow these steps:

Calculate the audio sampling frequency:
```c
Sampling Frequency = System Clock Frequency / (DFSDM_DIVIDER * DFSDM_OVERSAMPLING)
```

Find the memory required for the specified duration of audio recording:
```c
Memory = Audio Samples * Sample Size
```

Substitute given values from question 1 and find the memory required for audio recording:
```c
Sampling Frequency = (fSystem Clock / (DFSDM_DIVIDER * DFSDM_OVERSAMPLING)) = (168,000,000 / (57 * 64)) = 48,000 Hz

Audio Samples = Duration (in sec) * Sampling Frequency = 2 * 48,000 = 96,000 audio samples

Sample Size = 16-bits = 2 bytes

Memory = Audio Samples * Sample Size = 96,000 * 2 = 192,000 bytes
```

So, 192,000 bytes of memory buffer is required for the specified duration of audio recording.