stm32之hal库i2s驱动的封装记录

2024-05-07 07:44
文章标签 封装 驱动 记录 stm32 hal i2s

本文主要是介绍stm32之hal库i2s驱动的封装记录,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

  1. 本次使用的框架为threadx+filex+shell组件

程序

共用驱动文件(dma和nvic)

nvic文件

头文件
/** Copyright (c) 2024-2024,shchl** SPDX-License-Identifier: Apache-2.0** Change Logs:* Date           Author       Notes* 2024-5-3     shchl   first version*/#ifndef TX_STM32_F4_DRV_NVIC_OS_H
#define TX_STM32_F4_DRV_NVIC_OS_H
#include "drv_common.h"void stm32_nvic_common_enable(uint32_t instance,uint32_t preempt,uint32_t sub);
void stm32_nvic_common_disable(uint32_t instance);
uint8_t stm32_nvic_common_enabled_check(uint32_t instance);
#endif //TX_STM32_F4_DRV_NVIC_OS_H
源文件
/** Copyright (c) 2024-2024,shchl** SPDX-License-Identifier: Apache-2.0** Change Logs:* Date           Author       Notes* 2024-5-3     shchl   first version*/#include "drv_nvic_os.h"enum stm32_irq_op_enum {OPEN_IRQn,CLOSE_IRQn,READ_IRQn
};/*** @brief* @param instance* @param preempt* @param sub* @param open_flag*/
static uint32_t stm32_nvic_common(uint32_t instance, uint32_t preempt, uint32_t sub, enum stm32_irq_op_enum mode) {uint32_t irq;switch (instance) {
#define irq_set(IRQn) {irq=IRQn;}breakcase (uint32_t) SPI1: irq_set(SPI1_IRQn);case (uint32_t) SPI2: irq_set(SPI2_IRQn);case (uint32_t) SPI3: irq_set(SPI3_IRQn);case (uint32_t) USART1: irq_set(USART1_IRQn);case (uint32_t) USART2: irq_set(USART2_IRQn);case (uint32_t) USART3: irq_set(USART3_IRQn);case (uint32_t) UART4: irq_set(UART4_IRQn);case (uint32_t) UART5: irq_set(UART5_IRQn);case (uint32_t) SDIO: irq_set(SDIO_IRQn);case (uint32_t) DMA2_Stream0: irq_set(DMA2_Stream0_IRQn);case (uint32_t) DMA2_Stream1: irq_set(DMA2_Stream1_IRQn);case (uint32_t) DMA2_Stream2: irq_set(DMA2_Stream2_IRQn);case (uint32_t) DMA2_Stream3: irq_set(DMA2_Stream3_IRQn);case (uint32_t) DMA2_Stream4: irq_set(DMA2_Stream4_IRQn);case (uint32_t) DMA2_Stream5: irq_set(DMA2_Stream5_IRQn);case (uint32_t) DMA2_Stream6: irq_set(DMA2_Stream6_IRQn);case (uint32_t) DMA2_Stream7: irq_set(DMA2_Stream7_IRQn);case (uint32_t) DMA1_Stream0: irq_set(DMA1_Stream0_IRQn);case (uint32_t) DMA1_Stream1: irq_set(DMA1_Stream1_IRQn);case (uint32_t) DMA1_Stream2: irq_set(DMA1_Stream2_IRQn);case (uint32_t) DMA1_Stream3: irq_set(DMA1_Stream3_IRQn);case (uint32_t) DMA1_Stream4: irq_set(DMA1_Stream4_IRQn);case (uint32_t) DMA1_Stream5: irq_set(DMA1_Stream5_IRQn);case (uint32_t) DMA1_Stream6: irq_set(DMA1_Stream6_IRQn);case (uint32_t) DMA1_Stream7: irq_set(DMA1_Stream7_IRQn);default: {return UINT32_MAX;}}
#undef  irq_setswitch (mode) {case OPEN_IRQn: {HAL_NVIC_SetPriority(irq, preempt, sub);HAL_NVIC_EnableIRQ(irq);}break;case CLOSE_IRQn: {HAL_NVIC_DisableIRQ(irq);}break;default: {break;}}return irq;
}/*** @brief 中断使能* @param instance* @param preempt* @param sub*/
void stm32_nvic_common_enable(uint32_t instance, uint32_t preempt, uint32_t sub) {stm32_nvic_common(instance, preempt, sub, OPEN_IRQn);
}void stm32_nvic_common_disable(uint32_t instance) {stm32_nvic_common(instance, 0, 0, CLOSE_IRQn);
}
/*** @brief nvic 启用状态检测* @param instance* @return 0 未启用,1 启用*/
uint8_t stm32_nvic_common_enabled_check(uint32_t instance) {uint32_t irq = stm32_nvic_common(instance, 0, 0, READ_IRQn);return NVIC_GetEnableIRQ(irq);
}

DMA文件

头文件
/** Copyright (c) 2024-2024,shchl** SPDX-License-Identifier: Apache-2.0** Change Logs:* Date           Author       Notes* 2024-5-3     shchl   first version*/#ifndef TX_STM32_F4_DRV_DMA_OS_H
#define TX_STM32_F4_DRV_DMA_OS_H#include "drv_common.h"struct stm32_dma_info {uint32_t instance;DMA_HandleTypeDef *dma_tx;DMA_HandleTypeDef *dma_rx;
};struct stm32_dma_info *stm32_dma_info_get(uint32_t instance);void stm32_dma_clk_enable(const DMA_HandleTypeDef *handle);
void stm32_dma_clk_disable(const DMA_HandleTypeDef *handle);
#endif //TX_STM32_F4_DRV_DMA_OS_H
源文件
/** Copyright (c) 2024-2024,shchl** SPDX-License-Identifier: Apache-2.0** Change Logs:* Date           Author       Notes* 2024-5-3     shchl   first version*/#include "drv_dma_os.h"#if SPI1_DMA
static DMA_HandleTypeDef spi1_dma_tx = {.Instance=DMA2_Stream3, .Init.Channel=DMA_CHANNEL_3};
static DMA_HandleTypeDef spi1_dma_rx = {.Instance=DMA2_Stream0, .Init.Channel=DMA_CHANNEL_3};
#endifstatic DMA_HandleTypeDef sdio_dma_tx = {.Instance = DMA2_Stream6, .Init.Channel = DMA_CHANNEL_4};
static DMA_HandleTypeDef sdio_dma_rx = {.Instance = DMA2_Stream3, .Init.Channel = DMA_CHANNEL_4};
static DMA_HandleTypeDef i2s_spi2_dma_tx = {.Instance = DMA1_Stream4, .Init.Channel = DMA_CHANNEL_0};
static DMA_HandleTypeDef i2s_spi2_dma_rx = {.Instance = DMA1_Stream3, .Init.Channel = DMA_CHANNEL_3};static struct stm32_dma_info dma_info_map[] = {
#if SPI1_DMA{(uint32_t) SPI1, &spi1_dma_tx, &spi1_dma_rx},
#endif{(uint32_t) SDIO, &sdio_dma_tx, &sdio_dma_rx},{(uint32_t) SPI2, &i2s_spi2_dma_tx,&i2s_spi2_dma_rx}};
#define DMA_MAP_CNT ( sizeof(dma_info_map)/ sizeof(dma_info_map[0]))struct stm32_dma_info *stm32_dma_info_get(uint32_t instance) {for (int i = 0; i < DMA_MAP_CNT; ++i) {if (dma_info_map[i].instance == instance) {return dma_info_map + i;}}return NULL;
}void stm32_dma_clk_enable(const DMA_HandleTypeDef *handle) {switch ((uint32_t) handle->Instance) {case (uint32_t) DMA2_Stream0:case (uint32_t) DMA2_Stream1:case (uint32_t) DMA2_Stream2:case (uint32_t) DMA2_Stream3:case (uint32_t) DMA2_Stream4:case (uint32_t) DMA2_Stream5:case (uint32_t) DMA2_Stream6:case (uint32_t) DMA2_Stream7:__HAL_RCC_DMA2_CLK_ENABLE();break;case (uint32_t) DMA1_Stream0:case (uint32_t) DMA1_Stream1:case (uint32_t) DMA1_Stream2:case (uint32_t) DMA1_Stream3:case (uint32_t) DMA1_Stream4:case (uint32_t) DMA1_Stream5:case (uint32_t) DMA1_Stream6:case (uint32_t) DMA1_Stream7:__HAL_RCC_DMA1_CLK_ENABLE();break;default:return;}
}void stm32_dma_clk_disable(const DMA_HandleTypeDef *handle) {switch ((uint32_t) handle->Instance) {case (uint32_t) DMA2_Stream0:case (uint32_t) DMA2_Stream1:case (uint32_t) DMA2_Stream2:case (uint32_t) DMA2_Stream3:case (uint32_t) DMA2_Stream4:case (uint32_t) DMA2_Stream5:case (uint32_t) DMA2_Stream6:case (uint32_t) DMA2_Stream7:__HAL_RCC_DMA2_CLK_DISABLE();break;case (uint32_t) DMA1_Stream0:case (uint32_t) DMA1_Stream1:case (uint32_t) DMA1_Stream2:case (uint32_t) DMA1_Stream3:case (uint32_t) DMA1_Stream4:case (uint32_t) DMA1_Stream5:case (uint32_t) DMA1_Stream6:case (uint32_t) DMA1_Stream7:__HAL_RCC_DMA1_CLK_DISABLE();break;default:return;}
}#if SPI1_DMA
void DMA2_Stream0_IRQHandler(void) {TX_INTERRUPT_SAVE_AREATX_DISABLEHAL_DMA_IRQHandler(&spi1_dma_rx);TX_RESTORE
}void DMA2_Stream3_IRQHandler(void) {TX_INTERRUPT_SAVE_AREATX_DISABLEHAL_DMA_IRQHandler(&spi1_dma_tx);TX_RESTORE
}
#endifvoid DMA2_Stream3_IRQHandler(void) {TX_INTERRUPT_SAVE_AREATX_DISABLEHAL_DMA_IRQHandler(&sdio_dma_rx);TX_RESTORE
}void DMA2_Stream6_IRQHandler(void) {TX_INTERRUPT_SAVE_AREATX_DISABLEHAL_DMA_IRQHandler(&sdio_dma_tx);TX_RESTORE
}void DMA1_Stream4_IRQHandler(void) {HAL_DMA_IRQHandler(&i2s_spi2_dma_tx);
}
void DMA1_Stream3_IRQHandler(void) {HAL_DMA_IRQHandler(&i2s_spi2_dma_rx);
}

i2s底层驱动(使用的是双缓冲方式)

头文件

/** Copyright (c) 2024-2024,shchl** SPDX-License-Identifier: Apache-2.0** Change Logs:* Date           Author       Notes* 2024-5-6     shchl   first version*/#ifndef DRV_I2S_OS_H
#define DRV_I2S_OS_H
#include <drv_common.h>
#define I2S_AUDIO_INSTANCE SPI2
void bsp_I2sParSet(uint32_t sample_freq, uint16_t bps);
void bsp_I2sDmaEnable(uint8_t dma_tx_enable, uint8_t dma_rx_enable);
void bsp_I2sDmaParSet(DMA_InitTypeDef *tx_cfg, DMA_InitTypeDef *rx_cfg);void bsp_I2sInit(void);
void i2s_dma_tx_trans_set(const uint8_t *m0, const uint8_t *m1, uint16_t size);void i2s_play_start();
void i2s_play_stop();
void i2s_play_pause();
void i2s_play_resume();void i2s_dma_rx_trans_set(const uint8_t *m0, const uint8_t *m1, uint16_t size);
void i2s_record_start();
void i2s_record_stop();
void i2s_record_pause();
void i2s_record_resume();
#endif //DRV_I2S_OS_H

源文件

/** Copyright (c) 2024-2024,shchl** SPDX-License-Identifier: Apache-2.0** Change Logs:* Date           Author       Notes* 2024-5-6     shchl   first version*/#include "drv_i2s_os.h"
#include <drv_dma_os.h>
#include <drv_nvic_os.h>extern void i2s_dma_m0_cpltcallback(struct __DMA_HandleTypeDef *hdma) __attribute__((weak));extern void i2s_dma_m1_cpltcallback(struct __DMA_HandleTypeDef *hdma) __attribute__((weak));extern void i2s_dma_errcallback(struct __DMA_HandleTypeDef *hdma)__attribute__((weak));static I2S_HandleTypeDef handle = {.Instance = I2S_AUDIO_INSTANCE};
#define _i2s_handle (handle)/**** @param sample_freq 采样频率(hz)* @param bps 数据长度*/
void bsp_I2sParSet(const uint32_t sample_freq, uint16_t bps) {// 采样频率时钟设置RCC_PeriphCLKInitTypeDef RCCI2S2_ClkInitSture;uint32_t AudioFreq = I2S_AUDIOFREQ_44K;;if (_i2s_handle.Init.AudioFreq != sample_freq) {uint32_t PLLI2SN;uint32_t PLLI2SR;
#define SWITCH_SAMPLE_FREQ(sn,sr,freq)    {PLLI2SN = sn;PLLI2SR = sr;AudioFreq =freq;}break;// 参数配置switch (sample_freq) {case I2S_AUDIOFREQ_8K: SWITCH_SAMPLE_FREQ(256, 5, sample_freq)case I2S_AUDIOFREQ_11K: SWITCH_SAMPLE_FREQ(429, 4, sample_freq)case I2S_AUDIOFREQ_16K: SWITCH_SAMPLE_FREQ(213, 2, sample_freq)case I2S_AUDIOFREQ_22K: SWITCH_SAMPLE_FREQ(429, 4, sample_freq)case I2S_AUDIOFREQ_32K: SWITCH_SAMPLE_FREQ(213, 2, sample_freq)case I2S_AUDIOFREQ_44K: SWITCH_SAMPLE_FREQ(271, 2, sample_freq)case I2S_AUDIOFREQ_48K: SWITCH_SAMPLE_FREQ(258, 3, sample_freq)case I2S_AUDIOFREQ_96K: SWITCH_SAMPLE_FREQ(344, 2, sample_freq)case I2S_AUDIOFREQ_192K: SWITCH_SAMPLE_FREQ(393, 2, sample_freq)default: SWITCH_SAMPLE_FREQ(271, 2, 44100) // 使用44100}
#undef SWITCH_SAMPLE_FREQRCCI2S2_ClkInitSture.PeriphClockSelection = RCC_PERIPHCLK_I2S; //外设时钟源选择RCCI2S2_ClkInitSture.PLLI2S.PLLI2SN = PLLI2SN; //设置PLLI2SNRCCI2S2_ClkInitSture.PLLI2S.PLLI2SR = PLLI2SR; //设置PLLI2SRHAL_RCCEx_PeriphCLKConfig(&RCCI2S2_ClkInitSture); //设置时钟,并启用}// i2s 配置{_i2s_handle.Instance = I2S_AUDIO_INSTANCE;_i2s_handle.Init.AudioFreq = AudioFreq; //IIS频率设置_i2s_handle.Init.MCLKOutput = I2S_MCLKOUTPUT_ENABLE; //主时钟输出使能_i2s_handle.Init.ClockSource = I2S_CLOCK_PLL; //IIS时钟源为PLLswitch (bps) {//("飞利浦标准,24位数据长度");case 24:_i2s_handle.Init.Mode = I2S_MODE_MASTER_TX; //IIS模式_i2s_handle.Init.Standard = I2S_STANDARD_PHILIPS; //IIS标准_i2s_handle.Init.CPOL = I2S_CPOL_LOW; //空闲状态时钟电平_i2s_handle.Init.DataFormat = I2S_DATAFORMAT_24B; //IIS数据长度break;case 32://飞利浦标准,主机发送,时钟低电平有效,24位扩展帧长度_i2s_handle.Init.Mode = I2S_MODE_MASTER_TX;_i2s_handle.Init.Standard = I2S_STANDARD_PHILIPS;_i2s_handle.Init.CPOL = I2S_CPOL_LOW;_i2s_handle.Init.DataFormat = I2S_DATAFORMAT_32B;break;default:// 飞利浦标准,主机发送,时钟低电平有效,16位帧长度_i2s_handle.Init.Mode = I2S_MODE_MASTER_TX;_i2s_handle.Init.Standard = I2S_STANDARD_PHILIPS;_i2s_handle.Init.CPOL = I2S_CPOL_LOW;_i2s_handle.Init.DataFormat = I2S_DATAFORMAT_16B_EXTENDED;break;}}}void bsp_I2sDmaEnable(uint8_t dma_tx_enable, uint8_t dma_rx_enable) {struct stm32_dma_info *dma_info = stm32_dma_info_get((uint32_t) _i2s_handle.Instance);if (dma_tx_enable && dma_info->dma_tx) {__HAL_LINKDMA(&_i2s_handle, hdmatx, *(dma_info->dma_tx)); //将DMA与I2S联系起来}if (dma_rx_enable && dma_info->dma_rx) {__HAL_LINKDMA(&_i2s_handle, hdmarx, *(dma_info->dma_rx)); //将DMA与I2S联系起来}
}/*** @brief dma 配置设置   使用此函数之前,必须调用 @ref bsp_I2sDmaEnable ;* @param tx_cfg   tx dma 配置* @param rx_cfg   rx dma 配置*/
void bsp_I2sDmaParSet(DMA_InitTypeDef *tx_cfg, DMA_InitTypeDef *rx_cfg) {if (_i2s_handle.hdmatx) {if (tx_cfg) {// 统一在dma配置源文件中配置,不在此处进行配置_i2s_handle.hdmatx->Init.Direction = tx_cfg->Direction;_i2s_handle.hdmatx->Init.PeriphInc = tx_cfg->PeriphInc;_i2s_handle.hdmatx->Init.MemInc = tx_cfg->MemInc;_i2s_handle.hdmatx->Init.PeriphDataAlignment = tx_cfg->PeriphDataAlignment;_i2s_handle.hdmatx->Init.MemDataAlignment = tx_cfg->MemDataAlignment;_i2s_handle.hdmatx->Init.Mode = tx_cfg->Mode;_i2s_handle.hdmatx->Init.Priority = tx_cfg->Priority;_i2s_handle.hdmatx->Init.FIFOMode = tx_cfg->FIFOMode;_i2s_handle.hdmatx->Init.FIFOThreshold = tx_cfg->FIFOThreshold;_i2s_handle.hdmatx->Init.MemBurst = tx_cfg->MemBurst;_i2s_handle.hdmatx->Init.PeriphBurst = tx_cfg->PeriphBurst;} else {// 统一在dma配置源文件中配置,不在此处进行配置// hi2s_ptr->hdmatx->Instance = DMA1_Stream4; //DMA1数据流4// hi2s_ptr->hdmatx->Init.Channel = DMA_CHANNEL_0; //通道0_i2s_handle.hdmatx->Init.Direction = DMA_MEMORY_TO_PERIPH; //存储器到外设模式_i2s_handle.hdmatx->Init.PeriphInc = DMA_PINC_DISABLE; //外设非增量模式_i2s_handle.hdmatx->Init.MemInc = DMA_MINC_ENABLE; //存储器增量模式_i2s_handle.hdmatx->Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD; //外设数据长度:16位_i2s_handle.hdmatx->Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD; //存储器数据长度:16位_i2s_handle.hdmatx->Init.Mode = DMA_CIRCULAR; //使用循环模式_i2s_handle.hdmatx->Init.Priority = DMA_PRIORITY_MEDIUM; //高优先级_i2s_handle.hdmatx->Init.FIFOMode = DMA_FIFOMODE_ENABLE; //使用FIFO_i2s_handle.hdmatx->Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL; // 使用FIFO阈值完全配置_i2s_handle.hdmatx->Init.MemBurst = DMA_MBURST_INC8; //存储器单次突发传输_i2s_handle.hdmatx->Init.PeriphBurst = DMA_MBURST_INC8; //外设突发单次传输}}if (_i2s_handle.hdmarx) {if (rx_cfg) {_i2s_handle.hdmarx->Init.Direction = rx_cfg->Direction;_i2s_handle.hdmarx->Init.PeriphInc = rx_cfg->PeriphInc;_i2s_handle.hdmarx->Init.MemInc = rx_cfg->MemInc;_i2s_handle.hdmarx->Init.PeriphDataAlignment = rx_cfg->PeriphDataAlignment;_i2s_handle.hdmarx->Init.MemDataAlignment = rx_cfg->MemDataAlignment;_i2s_handle.hdmarx->Init.Mode = rx_cfg->Mode;_i2s_handle.hdmarx->Init.Priority = rx_cfg->Priority;_i2s_handle.hdmarx->Init.FIFOMode = rx_cfg->FIFOMode;_i2s_handle.hdmarx->Init.FIFOThreshold = rx_cfg->FIFOThreshold;_i2s_handle.hdmarx->Init.MemBurst = rx_cfg->MemBurst;_i2s_handle.hdmarx->Init.PeriphBurst = rx_cfg->PeriphBurst;} else {// todo 录音部分// _i2s_handle.hdmarx->Instance=DMA1_Stream3;                       		//DMA1数据流3// _i2s_handle.hdmarx->Init.Channel=DMA_CHANNEL_3;                  		//通道3_i2s_handle.hdmarx->Init.Direction = DMA_PERIPH_TO_MEMORY; //外设到存储器模式_i2s_handle.hdmarx->Init.PeriphInc = DMA_PINC_DISABLE; //外设非增量模式_i2s_handle.hdmarx->Init.MemInc = DMA_MINC_ENABLE; //存储器增量模式_i2s_handle.hdmarx->Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD; //外设数据长度:16位_i2s_handle.hdmarx->Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD; //存储器数据长度:16位_i2s_handle.hdmatx->Init.Mode = DMA_CIRCULAR; //使用循环模式_i2s_handle.hdmatx->Init.Priority = DMA_PRIORITY_MEDIUM; //高优先级_i2s_handle.hdmatx->Init.FIFOMode = DMA_FIFOMODE_ENABLE; //使用FIFO_i2s_handle.hdmatx->Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL; // 使用FIFO阈值完全配置_i2s_handle.hdmatx->Init.MemBurst = DMA_MBURST_INC8; //存储器单次突发传输_i2s_handle.hdmatx->Init.PeriphBurst = DMA_MBURST_INC8; //外设突发单次传输}}
}void bsp_I2sInit(void) {// 初始化{HAL_I2S_Init(&_i2s_handle);}}
void i2s_play_start() {// 判断是否初始化i2sDISABLE_INT();if (_i2s_handle.Instance) {__HAL_DMA_ENABLE(_i2s_handle.hdmatx); //打开dma/*使能相应标志位*/__HAL_I2S_ENABLE(&_i2s_handle); //使能I2S2/* Enable Tx DMA Request */__HAL_I2S_ENABLE_IT(&_i2s_handle, SPI_CR2_TXDMAEN); //SPI2 TX DMA请求使能.}ENABLE_INT();
}void i2s_play_stop() {// 判断是否初始化,并只初始化一次if (_i2s_handle.Instance) {// 复位i2s,内部已经对相应的DMA进行复位,所以这里就不需要__HAL_I2S_DISABLE_IT(&_i2s_handle, SPI_CR2_TXDMAEN); //SPI2 TX DMA请求使能.HAL_I2S_DeInit(&_i2s_handle);_i2s_handle.Instance = NULL;}
}void i2s_play_pause() {if ((_i2s_handle.hdmatx->Instance->CR & DMA_SxCR_EN)) {/* Disable the stream */__HAL_DMA_DISABLE(_i2s_handle.hdmatx);/* Check if the DMA Stream is effectively disabled */while ((_i2s_handle.hdmatx->Instance->CR & DMA_SxCR_EN) != RESET) {}}
}void i2s_play_resume() {if ((_i2s_handle.hdmatx->Instance->CR & DMA_SxCR_EN) == RESET) {__HAL_DMA_ENABLE(_i2s_handle.hdmatx);}
}/*** 用于播放* @param m0* @param m1* @param size*/
void i2s_dma_tx_trans_set(const uint8_t *m0, const uint8_t *m1, uint16_t size) {if (_i2s_handle.hdmatx == NULL) {Error_Handler();}/*配置回调函数*/_i2s_handle.hdmatx->XferCpltCallback = i2s_dma_m0_cpltcallback;_i2s_handle.hdmatx->XferM1CpltCallback = i2s_dma_m1_cpltcallback;_i2s_handle.hdmatx->XferErrorCallback = i2s_dma_errcallback;/*开启dma双缓冲模式*//* 开启双缓冲模式 */_i2s_handle.hdmatx->Instance->CR |= (uint32_t) DMA_SxCR_DBM;/* m1地址 */_i2s_handle.hdmatx->Instance->M1AR = (uint32_t) m1;/* m0地址 */_i2s_handle.hdmatx->Instance->M0AR = (uint32_t) m0;/* 传输数据长度 */_i2s_handle.hdmatx->Instance->NDTR = size;/* 目标地址 */_i2s_handle.hdmatx->Instance->PAR = (uint32_t) &(_i2s_handle.Instance->DR);/* 清除所有标志位 */__HAL_DMA_CLEAR_FLAG(_i2s_handle.hdmatx, __HAL_DMA_GET_TC_FLAG_INDEX( _i2s_handle.hdmatx));__HAL_DMA_CLEAR_FLAG(_i2s_handle.hdmatx, __HAL_DMA_GET_HT_FLAG_INDEX( _i2s_handle.hdmatx));__HAL_DMA_CLEAR_FLAG(_i2s_handle.hdmatx, __HAL_DMA_GET_TE_FLAG_INDEX( _i2s_handle.hdmatx));__HAL_DMA_CLEAR_FLAG(_i2s_handle.hdmatx, __HAL_DMA_GET_DME_FLAG_INDEX( _i2s_handle.hdmatx));__HAL_DMA_CLEAR_FLAG(_i2s_handle.hdmatx, __HAL_DMA_GET_FE_FLAG_INDEX( _i2s_handle.hdmatx));/* 使能中断标志位*/_i2s_handle.hdmatx->Instance->CR |= DMA_IT_TC | DMA_IT_TE | DMA_IT_DME;_i2s_handle.hdmatx->Instance->FCR |= DMA_IT_FE;
}void i2s_dma_rx_trans_set(const uint8_t *m0, const uint8_t *m1, uint16_t size) {if (_i2s_handle.hdmarx == NULL) {Error_Handler();}/*配置回调函数*/_i2s_handle.hdmarx->XferCpltCallback = i2s_dma_m0_cpltcallback;_i2s_handle.hdmarx->XferM1CpltCallback = i2s_dma_m1_cpltcallback;_i2s_handle.hdmarx->XferErrorCallback = i2s_dma_errcallback;/*开启dma双缓冲模式*//* 开启双缓冲模式 */_i2s_handle.hdmarx->Instance->CR |= (uint32_t) DMA_SxCR_DBM;/* m1地址 */_i2s_handle.hdmarx->Instance->M1AR = (uint32_t) m1;/* m0地址 */_i2s_handle.hdmarx->Instance->M0AR = (uint32_t) m0;/* 传输数据长度 */_i2s_handle.hdmarx->Instance->NDTR = size;/* 源地址 */_i2s_handle.hdmarx->Instance->PAR = (uint32_t) &(_i2s_handle.Instance->DR);/* 清除所有标志位 */__HAL_DMA_CLEAR_FLAG(_i2s_handle.hdmarx, __HAL_DMA_GET_TC_FLAG_INDEX( _i2s_handle.hdmarx));__HAL_DMA_CLEAR_FLAG(_i2s_handle.hdmarx, __HAL_DMA_GET_HT_FLAG_INDEX( _i2s_handle.hdmarx));__HAL_DMA_CLEAR_FLAG(_i2s_handle.hdmarx, __HAL_DMA_GET_TE_FLAG_INDEX( _i2s_handle.hdmarx));__HAL_DMA_CLEAR_FLAG(_i2s_handle.hdmarx, __HAL_DMA_GET_DME_FLAG_INDEX( _i2s_handle.hdmarx));__HAL_DMA_CLEAR_FLAG(_i2s_handle.hdmarx, __HAL_DMA_GET_FE_FLAG_INDEX( _i2s_handle.hdmarx));/* 使能中断标志位*/_i2s_handle.hdmarx->Instance->CR |= DMA_IT_TC | DMA_IT_TE | DMA_IT_DME;_i2s_handle.hdmarx->Instance->FCR |= DMA_IT_FE;
}void i2s_record_start() {// 判断是否初始化i2sDISABLE_INT();if (_i2s_handle.Instance) {__HAL_DMA_ENABLE(_i2s_handle.hdmarx); //打开dma/*使能相应标志位*/__HAL_I2S_ENABLE(&_i2s_handle); //使能I2S2/* Enable Tx DMA Request */__HAL_I2S_ENABLE_IT(&_i2s_handle, SPI_CR2_RXDMAEN); //SPI2 TX DMA请求使能.}ENABLE_INT();
}void i2s_record_stop() {// 判断是否初始化,并只初始化一次if (_i2s_handle.Instance) {// 复位i2s,内部已经对相应的DMA进行复位,所以这里就不需要__HAL_I2S_DISABLE_IT(&_i2s_handle, SPI_CR2_RXDMAEN); //SPI2 TX DMA请求使能.HAL_I2S_DeInit(&_i2s_handle);_i2s_handle.Instance = NULL;}
}void i2s_record_pause() {if ((_i2s_handle.hdmarx->Instance->CR & DMA_SxCR_EN)) {/* Disable the stream */__HAL_DMA_DISABLE(_i2s_handle.hdmarx);/* Check if the DMA Stream is effectively disabled */while ((_i2s_handle.hdmarx->Instance->CR & DMA_SxCR_EN) != RESET) {}}
}void i2s_record_resume() {if ((_i2s_handle.hdmarx->Instance->CR & DMA_SxCR_EN) == RESET) {__HAL_DMA_ENABLE(_i2s_handle.hdmarx);}
}

硬件io文件

/** Copyright (c) 2024-2024,shchl** SPDX-License-Identifier: Apache-2.0** Change Logs:* Date           Author       Notes* 2024-5-2     shchl   first version*/
#include "bsp.h"/*** @brief I2S MSP Init* @param  hi2s_ptr pointer to a I2S_HandleTypeDef structure that contains*         the configuration information for I2S module* @retval None*/
void HAL_I2S_MspInit(I2S_HandleTypeDef *hi2s_ptr) {if (hi2s_ptr->Instance == SPI2) {/*I2S GPIO 初始化*/{GPIO_InitTypeDef GPIO_InitStruct;/* Peripheral clock enable */__HAL_RCC_SPI2_CLK_ENABLE();__HAL_RCC_GPIOC_CLK_ENABLE();__HAL_RCC_GPIOB_CLK_ENABLE();/**I2S2 GPIO ConfigurationPC2     ------> I2S2_ext_SDPC3     ------> I2S2_SDPB12     ------> I2S2_WSPB13     ------> I2S2_CKPC6     ------> I2S2_MCK*/GPIO_InitStruct.Pin = GPIO_PIN_2;GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;GPIO_InitStruct.Alternate = GPIO_AF6_I2S2ext;HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);GPIO_InitStruct.Pin = GPIO_PIN_3 | GPIO_PIN_6;GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);GPIO_InitStruct.Pin = GPIO_PIN_12 | GPIO_PIN_13;GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);}/* I2S DMA发送配置 */if (hi2s_ptr->hdmatx) {stm32_dma_clk_enable(hi2s_ptr->hdmatx); //使能DMA时钟HAL_DMA_Init(hi2s_ptr->hdmatx); //初始化DMAstm32_nvic_common_enable((uint32_t) hi2s_ptr->hdmatx->Instance, 0, 0);};}
}void HAL_I2S_MspDeInit(I2S_HandleTypeDef *handle_ptr) {if (handle_ptr->Instance == SPI2) {/**//* Peripheral clock disable */__HAL_RCC_SPI2_CLK_DISABLE();/**I2S2 GPIO ConfigurationPC2     ------> I2S2_ext_SDPC3     ------> I2S2_SDPB12     ------> I2S2_WSPB13     ------> I2S2_CKPC6     ------> I2S2_MCK*/HAL_GPIO_DeInit(GPIOC, GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_6);HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10 | GPIO_PIN_12);if (handle_ptr->hdmatx) {/* I2S2 DMA DeInit */HAL_DMA_DeInit(handle_ptr->hdmatx);stm32_nvic_common_disable((uint32_t) handle_ptr->hdmatx->Instance);}}
}

这篇关于stm32之hal库i2s驱动的封装记录的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/966744

相关文章

Linux_kernel驱动开发11

一、改回nfs方式挂载根文件系统         在产品将要上线之前,需要制作不同类型格式的根文件系统         在产品研发阶段,我们还是需要使用nfs的方式挂载根文件系统         优点:可以直接在上位机中修改文件系统内容,延长EMMC的寿命         【1】重启上位机nfs服务         sudo service nfs-kernel-server resta

Node.js学习记录(二)

目录 一、express 1、初识express 2、安装express 3、创建并启动web服务器 4、监听 GET&POST 请求、响应内容给客户端 5、获取URL中携带的查询参数 6、获取URL中动态参数 7、静态资源托管 二、工具nodemon 三、express路由 1、express中路由 2、路由的匹配 3、路由模块化 4、路由模块添加前缀 四、中间件

【STM32】SPI通信-软件与硬件读写SPI

SPI通信-软件与硬件读写SPI 软件SPI一、SPI通信协议1、SPI通信2、硬件电路3、移位示意图4、SPI时序基本单元(1)开始通信和结束通信(2)模式0---用的最多(3)模式1(4)模式2(5)模式3 5、SPI时序(1)写使能(2)指定地址写(3)指定地址读 二、W25Q64模块介绍1、W25Q64简介2、硬件电路3、W25Q64框图4、Flash操作注意事项软件SPI读写W2

记录每次更新到仓库 —— Git 学习笔记 10

记录每次更新到仓库 文章目录 文件的状态三个区域检查当前文件状态跟踪新文件取消跟踪(un-tracking)文件重新跟踪(re-tracking)文件暂存已修改文件忽略某些文件查看已暂存和未暂存的修改提交更新跳过暂存区删除文件移动文件参考资料 咱们接着很多天以前的 取得Git仓库 这篇文章继续说。 文件的状态 不管是通过哪种方法,现在我们已经有了一个仓库,并从这个仓

JavaSE——封装、继承和多态

1. 封装 1.1 概念      面向对象程序三大特性:封装、继承、多态 。而类和对象阶段,主要研究的就是封装特性。何为封装呢?简单来说就是套壳屏蔽细节 。     比如:对于电脑这样一个复杂的设备,提供给用户的就只是:开关机、通过键盘输入,显示器, USB 插孔等,让用户来和计算机进行交互,完成日常事务。但实际上:电脑真正工作的却是CPU 、显卡、内存等一些硬件元件。

STM32(十一):ADC数模转换器实验

AD单通道: 1.RCC开启GPIO和ADC时钟。配置ADCCLK分频器。 2.配置GPIO,把GPIO配置成模拟输入的模式。 3.配置多路开关,把左面通道接入到右面规则组列表里。 4.配置ADC转换器, 包括AD转换器和AD数据寄存器。单次转换,连续转换;扫描、非扫描;有几个通道,触发源是什么,数据对齐是左对齐还是右对齐。 5.ADC_CMD 开启ADC。 void RCC_AD

STM32内部闪存FLASH(内部ROM)、IAP

1 FLASH简介  1 利用程序存储器的剩余空间来保存掉电不丢失的用户数据 2 通过在程序中编程(IAP)实现程序的自我更新 (OTA) 3在线编程(ICP把整个程序都更新掉) 1 系统的Bootloader写死了,只能用串口下载到指定的位置,启动方式也不方便需要配置BOOT引脚触发启动  4 IAP(自己写的Bootloader,实现程序升级) 1 比如蓝牙转串口,

FreeRTOS-基本介绍和移植STM32

FreeRTOS-基本介绍和STM32移植 一、裸机开发和操作系统开发介绍二、任务调度和任务状态介绍2.1 任务调度2.1.1 抢占式调度2.1.2 时间片调度 2.2 任务状态 三、FreeRTOS源码和移植STM323.1 FreeRTOS源码3.2 FreeRTOS移植STM323.2.1 代码移植3.2.2 时钟中断配置 一、裸机开发和操作系统开发介绍 裸机:前后台系

寻迹模块TCRT5000的应用原理和功能实现(基于STM32)

目录 概述 1 认识TCRT5000 1.1 模块介绍 1.2 电气特性 2 系统应用 2.1 系统架构 2.2 STM32Cube创建工程 3 功能实现 3.1 代码实现 3.2 源代码文件 4 功能测试 4.1 检测黑线状态 4.2 未检测黑线状态 概述 本文主要介绍TCRT5000模块的使用原理,包括该模块的硬件实现方式,电路实现原理,还使用STM32类

学习记录:js算法(二十八):删除排序链表中的重复元素、删除排序链表中的重复元素II

文章目录 删除排序链表中的重复元素我的思路解法一:循环解法二:递归 网上思路 删除排序链表中的重复元素 II我的思路网上思路 总结 删除排序链表中的重复元素 给定一个已排序的链表的头 head , 删除所有重复的元素,使每个元素只出现一次 。返回 已排序的链表 。 图一 图二 示例 1:(图一)输入:head = [1,1,2]输出:[1,2]示例 2:(图