打印
[N32WBxxx]

N32WB031 SPI+DMA发送demo

[复制链接]
375|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
Afanx|  楼主 | 2023-2-6 11:20 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 Afanx 于 2023-2-6 11:20 编辑

例程附件:
N32WB03x_SPI_DMA_demo.zip (211.58 KB)

GPIO配置:
void GPIO_Configuration(void)
{
    GPIO_InitType GPIO_InitStructure;

    RCC_EnableAPB2PeriphClk(SPI_MASTER_GPIO_CLK | RCC_APB2_PERIPH_AFIO, ENABLE);

    GPIO_InitStruct(&GPIO_InitStructure);
    /* Configure SPI_MASTER pins: SCK ,MISO and MOSI */
    GPIO_InitStructure.Pin            = SPI_MASTER_PIN_SCK | SPI_MASTER_PIN_MOSI;
    GPIO_InitStructure.GPIO_Speed     = GPIO_SPEED_HIGH;
    GPIO_InitStructure.GPIO_Mode      = GPIO_MODE_AF_PP;
    GPIO_InitStructure.GPIO_Pull      = GPIO_NO_PULL;
    GPIO_InitStructure.GPIO_Alternate = SPI_MASTER_GPIO_AF;
    GPIO_InitPeripheral(SPI_MASTER_GPIO, &GPIO_InitStructure);
}
DMA配置:
void DMA_SPI_Configuration(void)
{
    DMA_InitType DMA_InitStructure;

    RCC_EnableAHBPeriphClk(SPI_MASTER_DMA_CLK, ENABLE);
    /* SPI_MASTER_TX_DMA_CHANNEL configuration */
    DMA_DeInit(SPI_MASTER_TX_DMA_CHANNEL);
    DMA_InitStructure.PeriphAddr     = (uint32_t)&SPI_MASTER->DAT;
    DMA_InitStructure.MemAddr        = (uint32_t)SPI_Master_Tx_Buffer;
    DMA_InitStructure.Direction      = DMA_DIR_PERIPH_DST;
    DMA_InitStructure.BufSize        = BUFFER_SIZE;
    DMA_InitStructure.PeriphInc      = DMA_PERIPH_INC_DISABLE;
    DMA_InitStructure.DMA_MemoryInc  = DMA_MEM_INC_ENABLE;
    DMA_InitStructure.PeriphDataSize = DMA_PERIPH_DATA_SIZE_BYTE;
    DMA_InitStructure.MemDataSize    = DMA_MemoryDataSize_Byte;
    DMA_InitStructure.CircularMode   = DMA_MODE_NORMAL;
    DMA_InitStructure.Priority       = DMA_PRIORITY_HIGH;
    DMA_InitStructure.Mem2Mem        = DMA_M2M_DISABLE;
    DMA_Init(SPI_MASTER_TX_DMA_CHANNEL, &DMA_InitStructure);
    DMA_RequestRemap(SPI_MASTER_TX_DMA_REMAP, DMA, SPI_MASTER_TX_DMA_CHANNEL, ENABLE);
}
SPI配置:
void SPI_Configuration(void)
{
    SPI_InitType SPI_InitStructure;

    RCC_EnableAPB2PeriphClk(SPI_MASTER_CLK, ENABLE);

    SPI_I2S_DeInit(SPI_MASTER);
    /* SPI_MASTER configuration
     * ------------------------------------------------*/
    SPI_InitStructure.DataDirection = SPI_DIR_DOUBLELINE_FULLDUPLEX;
    SPI_InitStructure.SpiMode       = SPI_MODE_MASTER;
    SPI_InitStructure.DataLen       = SPI_DATA_SIZE_8BITS;
    SPI_InitStructure.CLKPOL        = SPI_CLKPOL_HIGH;
    SPI_InitStructure.CLKPHA        = SPI_CLKPHA_SECOND_EDGE;
    SPI_InitStructure.NSS           = SPI_NSS_SOFT;
    SPI_InitStructure.BaudRatePres  = SPI_BR_PRESCALER_32;
    SPI_InitStructure.FirstBit      = SPI_FB_MSB;
    SPI_InitStructure.CRCPoly       = CRC_POLYNOMIAL;
    SPI_Init(SPI_MASTER, &SPI_InitStructure);

    /* Enable SPI_MASTER */
    SPI_Enable(SPI_MASTER, ENABLE);
}
封装SPI_DMA写函数:
void SPI_Write_DMA(SPI_Module* SPIx, uint8_t* writePtr, uint16_t len)
{
    uint32_t timeOut = 0x1000;
    while ((SPI_I2S_GetStatus(SPIx, SPI_I2S_BUSY_FLAG) != RESET) && (timeOut--)) __NOP();  // Wait BUSY

    // SPI_I2S_ReceiveData(SPIx);  // CLear RNE

    SPI_I2S_EnableDma(SPIx, SPI_I2S_DMA_TX, DISABLE);  // Disable DMA request

    DMA_EnableChannel(SPI_MASTER_TX_DMA_CHANNEL, DISABLE); // Disable DMA
    SPI_MASTER_TX_DMA_CHANNEL->MADDR = (uint32_t)writePtr;
    SPI_MASTER_TX_DMA_CHANNEL->TXNUM = len;
    DMA_EnableChannel(SPI_MASTER_TX_DMA_CHANNEL, ENABLE);  // Enable DMA

    SPI_I2S_EnableDma(SPIx, SPI_I2S_DMA_TX, ENABLE);  // DMA Start Work
}
主函数测试:
int main(void)
{
    log_init();

    log_info("\n this is a SPI master DMA Demo.\n");

    GPIO_Configuration();
    DMA_SPI_Configuration();
    SPI_Configuration();
        
    log_info("\n DMA Transfer start.\n");
        
    for (int i = 0; i < 3; i++) {
        SPI_Write_DMA(SPI_MASTER, SPI_Master_Tx_Buffer, BUFFER_SIZE);
               
                while (!DMA_GetFlagStatus(SPI_MASTER_TX_DMA_FLAG, SPI_MASTER_DMA)) __NOP();
    }
        
    log_info("\n DMA Transfer end.\n");

    while (1) {
    }
}
测试结果:




使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

13

主题

51

帖子

1

粉丝