2022-06-02 03:01 AM
I'm using a stm32L4R9 disco.
I want to record PCM data from a mems microphone and store it on a sd card.
I have managed to make a project that gets data using DFSDM.
I have managed to make another project that simply create a .txt file on a sd card, using SDMMC and Fatfs.
But when I try to use DFSDM and Fatfs in the same project, the HAL_DELAY gets stuck in a loop, and I cant understand why. I have tried to change the preemption priorities, but It didn't change anything.
I'm putting the code here, if you have any idea of where this could comes from, I'd greatly appreciate it !
#include "main.h"
#include "fatfs.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <string.h>
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
DFSDM_Filter_HandleTypeDef hdfsdm1_filter0;
DFSDM_Channel_HandleTypeDef hdfsdm1_channel0;
DMA_HandleTypeDef hdma_dfsdm1_flt0;
SD_HandleTypeDef hsd1;
/* USER CODE BEGIN PV */
#define AUDIO_REC 64
int32_t RecBuf0[AUDIO_REC]={0};
int32_t PlayBuf0[AUDIO_REC]={0};
uint16_t SDWritingDone = 0 ;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_DFSDM1_Init(void);
static void MX_SDMMC1_SD_Init(void);
/* USER CODE BEGIN PFP */
void FirstMicFirstHalf(void);
void FirstMicSecondHalf(void);
void SecondMicFirstHalf(void);
void SecondMicSecondHalf(void);
void WriteSD(void);
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
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_DFSDM1_Init();
MX_SDMMC1_SD_Init();
MX_FATFS_Init();
/* USER CODE BEGIN 2 */
HAL_DFSDM_FilterRegularStart_DMA(&hdfsdm1_filter0, RecBuf0, AUDIO_REC);
HAL_Delay(200);
//HAL_DFSDM_FilterRegularStop_DMA(&hdfsdm1_filter0);
WriteSD();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
// [...]
/* USER CODE BEGIN 4 */
void HAL_DFSDM_FilterRegConvHalfCpltCallback(DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
{
if(hdfsdm_filter->Instance == hdfsdm1_filter0.Instance) FirstMicFirstHalf();
}
void HAL_DFSDM_FilterRegConvCpltCallback(DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
{
if(hdfsdm_filter->Instance == hdfsdm1_filter0.Instance) FirstMicSecondHalf();
}
void FirstMicFirstHalf()
{
for(int i=0;i<AUDIO_REC/2;i++)
{
PlayBuf0[i]=RecBuf0[i]>>8;
}
}
void FirstMicSecondHalf()
{
for(int i=AUDIO_REC/2;i<AUDIO_REC;i++)
{
PlayBuf0[i]=RecBuf0[i]>>8;
}
}
void WriteSD(void)
{
FRESULT res; /* FatFs function common result code */
uint32_t byteswritten; /* File write/read counts */
uint8_t wtext[] = "\r "; /* File write buffer */
int i = 1000;
uint8_t rtext[_MAX_SS];/* File read buffer */
if(f_mount(&SDFatFS, (TCHAR const*)SDPath, 0) != FR_OK)
{
Error_Handler();
}
else
{
if(f_mkfs((TCHAR const*)SDPath, FM_ANY, 0, rtext, sizeof(rtext)) != FR_OK)
{
Error_Handler();
}
else
{
//Open file for writing (Create)
if(f_open(&SDFile, "WED.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
{
Error_Handler();
}
else
{
//Write to the text file
for(int h = 0 ; h < 64 ; h++)
{
char buffer[12];
int len = itoa((int) PlayBuf0[i], buffer, 10);
res = f_write(&SDFile, buffer, strlen(buffer), (void *)&byteswritten);
res = f_write(&SDFile, wtext, strlen((char *)wtext), (void *)&byteswritten);
}
f_close(&SDFile);
}
}
}
f_mount(&SDFatFS, (TCHAR const*)NULL, 0);
SDWritingDone = 1 ;
}