一、参考原理图
1、INMP441
2、STM32
注意INMP441的第4引脚,用来选择左声道还是右声道。
二、代码生成
代码使用cubemx生成
1、iis设置
2、DMA设置
3、生成代码
三、代码修改
1、首先定义一个数组
#define BUFFER_SIZE (4)
static uint32_t simpleBuf[BUFFER_SIZE];
/* USER CODE BEGIN PV */
uint32_t val24;
int val32;
/* USER CODE END PV */
2、定义接收完成中断函数
/* USER CODE BEGIN 0 */
unsigned cb_cnt=0;
//I2S接收完成回调函数
void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef *hi2s)
{
if(hi2s==&hi2s2){
cb_cnt++;//回调次数计数
//将两个32整型合并为一个
//dat32 example: 0000fffb 00004f00
val24=(dma[0]<<8)+(dma[1]>>8);
//将24位有符号整型扩展到32位
if(val24 & 0x800000){//negative
val32=0xff000000 | val24;
}else{//positive
val32=val24;
}
//以采样频率的十分之一,串口发送采样值
if(cb_cnt%10==0)
printf("%d\r\n",val32);
}
}
/* USER CODE END 0 */
3、启动接收
HAL_I2S_Receive_DMA(&hi2s2,(uint16_t*)simpleBuf,BUFFER_SIZE);
4、完整代码
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2022 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "dma.h"
#include "i2s.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
//#include "stm32f1xx_hal.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 ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
uint32_t dma[4];
uint32_t val24;
int val32;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
unsigned cb_cnt=0;
//I2S接收完成回调函数
void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef *hi2s)
{
if(hi2s==&hi2s2){
cb_cnt++;//回调次数计数
//将两个32整型合并为一个
//dat32 example: 0000fffb 00004f00
val24=(simpleBuf[0]<<8)+(simpleBuf[1]>>8);
//将24位有符号整型扩展到32位
if(val24 & 0x800000){//negative
val32=0xff000000 | val24;
}else{//positive
val32=val24;
}
//以采样频率的十分之一,串口发送采样值
if(cb_cnt%10==0)
printf("%d\r\n",val32);
}
}
/* 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_USART1_UART_Init();
MX_DMA_Init();
MX_I2S2_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
//开启DMA传输
HAL_I2S_Receive_DMA(&hi2s2,(uint16_t*)simpleBuf,BUFFER_SIZE);
while (1)
{
HAL_Delay(10);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
//后面都是Cube自动生成的代码,省略
5、代码解析
根据以上说明,最后一个参数size的单位是由数据帧的长度决定的。前面在Cube中设置的数据格式为24 Bits Data on 32 Bits Frame,因此DMA读取数据的总长度为size×4字节。前面定义的DMA缓冲区是一个长度为4的uint32_t型数组,缓冲区中的数据格式为:前2个元素表示左声道数据
,后2个表示右声道数据
。
单通道数据格式为:第一个字节:0x00000004
第一个字节:0x00004500
。通过val24=(dma[0]<<8)+(dma[1]>>8);
合成0x00fffb4f
,然后区分正负极:
if(val24 & 0x800000){
val32=0xff000000 | val24;
}else{
val32=val24;
}