Question
Data from ADC (DMA) over UART (DMA)
Hi,
I am trying to perform continuous ADC conversions from a microphone using DMA, save that data to a large buffer, and then transmit that data using Uart (DMA). I have successfully setup the ADC to perform the DMA conversions along with saving them to the large buffer.
How can I modify this code so that when half of the buffer is full, that half is transmitted over UART DMA while the second half of the buffer is filling. Then continuing on with a ping pong buffer uart transmit.
Thank you for any help.
#define ADC_BUF_LEN 4096
uint16_t adc_buf[ADC_BUF_LEN];
uint8_t First_Half_Filled = 0; // First half of buffer is filled with ADC values
uint8_t Second_Half_Filled = 0; // Second half of buffer is filled with ADC values
uint8_t First_Half_Sent = 0; // First half of buffer is send over UART DMA
uint8_t Second_Half_Sent = 0; // Second half of buffer is sent over DMA
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_ADC1_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
HAL_ADC_Start_DMA(&hadc1, (uint32_t*)adc_buf, ADC_BUF_LEN); HAL_UART_Transmit_DMA(&huart2, (uint8_t *)adc_buf, ADC_BUF_LEN/2);
while (1)
{
if(First_Half_Filled == 1)
{
First_Half_Filled = 0;
}
if(Second_Half_Filled == 1)
{
Second_Half_Filled = 0;
}
if(First_Half_Sent == 1)
{
First_Half_Sent = 0;
}
if(Second_Half_Sent == 1)
{
Second_Half_Sent = 0;
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
// Callbacks
// Called when the buffer is completely filled
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
Second_Half_Filled = 1;
}
void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef* huart2)
{
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
First_Half_Sent = 1;
}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef* huart2)
{
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
Second_Half_Sent = 1;
}