SPI1 使用中断发送数据 SPI2 使用dma接收数据
- 这个例子spi1使用中断发送数据
- spi2使用dma的方式接收数据
- 传输数位位宽是16bit,2个字节
#include "n32g430.h"
#include "bsp_spi.h"
#include "stdio.h"
#include "string.h"
#include "bsp_delay.h"
#define BufferSize 32
uint16_t RxIdx=0,TxIdx=0;
static uint16_t SPI_Master_Buffer_Tx[BufferSize] = {0x0102, 0x0304, 0x0506, 0x0708, 0x090A, 0x0B0C, 0x0D0E, 0x0F10,
0x1112, 0x1314, 0x1516, 0x1718, 0x191A, 0x1B1C, 0x1D1E, 0x1F20,
0x2122, 0x2324, 0x2526, 0x2728, 0x292A, 0x2B2C, 0x2D2E, 0x2F30,
0x3132, 0x3334, 0x3536, 0x3738, 0x393A, 0x3B3C, 0x3D3E, 0x3F40};
static uint16_t SPI_Slave_Buffer_Tx[BufferSize] = {0x5152, 0x5354, 0x5556, 0x5758, 0x595A, 0x5B5C, 0x5D5E, 0x5F60,
0x6162, 0x6364, 0x6566, 0x6768, 0x696A, 0x6B6C, 0x6D6E, 0x6F70,
0x7172, 0x7374, 0x7576, 0x7778, 0x797A, 0x7B7C, 0x7D7E, 0x7F80,
0x8182, 0x8384, 0x8586, 0x8788, 0x898A, 0x8B8C, 0x8D8E, 0x8F90};
static uint16_t SPI_Master_Buffer_Rx[BufferSize], SPI_Slave_Buffer_Rx[BufferSize];
static void bsp_spi_it_rcc_cinfig(void)
{
RCC_Pclk2_Config(RCC_HCLK_DIV2);
RCC_APB2_Peripheral_Clock_Enable(RCC_APB2_PERIPH_SPI1 | RCC_APB2_PERIPH_SPI2);
RCC_AHB_Peripheral_Clock_Enable(RCC_AHB_PERIPH_GPIOA | RCC_AHB_PERIPH_GPIOB);
RCC_AHB_Peripheral_Clock_Enable(RCC_AHB_PERIPH_DMA);
}
static void bsp_spi_it_gpio_config(void)
{
GPIO_InitType GPIO_InitStructure;
GPIO_Structure_Initialize(&GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_5;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.GPIO_Slew_Rate = GPIO_SLEW_RATE_FAST;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF1_SPI1;
GPIO_Peripheral_Initialize(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_3 | GPIO_PIN_4;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF2_SPI1;
GPIO_Peripheral_Initialize(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF1_SPI2;
GPIO_Peripheral_Initialize(GPIOB, &GPIO_InitStructure);
}
static void bsp_spi_it_nvic_config(void)
{
NVIC_InitType NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Initializes(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = SPI2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Initializes(&NVIC_InitStructure);
SPI_I2S_Interrupts_Enable(SPI1, SPI_I2S_INT_TE);
}
static void bsp_spi_it_config(void)
{
SPI_InitType SPI_InitStructure;
SPI_Initializes_Structure(&SPI_InitStructure);
SPI_InitStructure.DataDirection = SPI_DIR_SINGLELINE_TX;
SPI_InitStructure.SpiMode = SPI_MODE_MASTER;
SPI_InitStructure.DataLen = SPI_DATA_SIZE_16BITS;
SPI_InitStructure.CLKPOL = SPI_CLKPOL_LOW;
SPI_InitStructure.CLKPHA = SPI_CLKPHA_SECOND_EDGE;
SPI_InitStructure.NSS = SPI_NSS_HARD;
SPI_InitStructure.BaudRatePres = SPI_BR_PRESCALER_64;
SPI_InitStructure.FirstBit = SPI_FB_MSB;
SPI_InitStructure.CRCPoly = 7;
SPI_Initializes(SPI1, &SPI_InitStructure);
SPI_Set_Nss_Level(SPI1, SPI_NSS_HIGH);
SPI_SS_Output_Enable(SPI1);
SPI_InitStructure.SpiMode = SPI_MODE_SLAVE;
SPI_InitStructure.DataDirection = SPI_DIR_SINGLELINE_RX;
SPI_Initializes(SPI2, &SPI_InitStructure);
SPI_NSS_Config(SPI2, SPI_NSS_SOFT);
SPI_Set_Nss_Level(SPI1, SPI_NSS_LOW);
}
void SPI1_IRQHandler(void)
{
if (SPI_I2S_Interrupt_Flag_Status_Get(SPI1, SPI_I2S_INT_FLAG_TE) != RESET)
{
SPI_I2S_Data_Transmit(SPI1, SPI_Master_Buffer_Tx[TxIdx++]);
if (TxIdx == BufferSize)
{
SPI_I2S_Interrupts_Disable(SPI1, SPI_I2S_INT_TE);
}
}
}
static void bps_spi_dma_config(void)
{
DMA_InitType DMA_InitStructure;
DMA_Reset(DMA_CH1);
DMA_InitStructure.MemAddr = (uint32_t)&SPI_Slave_Buffer_Rx[0];
DMA_InitStructure.MemDataSize = DMA_MEM_DATA_WIDTH_HALFWORD;
DMA_InitStructure.MemoryInc = DMA_MEM_INC_MODE_ENABLE;
DMA_InitStructure.Direction = DMA_DIR_PERIPH_SRC;
DMA_InitStructure.PeriphAddr = (uint32_t)&SPI2->DAT;
DMA_InitStructure.PeriphDataSize = DMA_PERIPH_DATA_WIDTH_HALFWORD;
DMA_InitStructure.PeriphInc = DMA_PERIPH_INC_MODE_DISABLE;
DMA_InitStructure.BufSize = BufferSize;
DMA_InitStructure.CircularMode = DMA_CIRCULAR_MODE_DISABLE;
DMA_InitStructure.Mem2Mem = DMA_MEM2MEM_DISABLE;
DMA_InitStructure.Priority = DMA_CH_PRIORITY_MEDIUM;
DMA_Initializes(DMA_CH1, &DMA_InitStructure);
DMA_Channel_Request_Remap(DMA_CH1, DMA_REMAP_SPI2_RX);
DMA_Channel_Enable(DMA_CH1);
SPI_I2S_DMA_Transfer_Enable(SPI2, SPI_I2S_DMA_RX);
}
void bsp_spi_it_init(void)
{
bsp_spi_it_rcc_cinfig();
bsp_spi_it_gpio_config();
bsp_spi_it_nvic_config();
bsp_spi_it_config();
bps_spi_dma_config();
SPI_ON(SPI1);
SPI_ON(SPI2);
}
static int Buffercmp(uint16_t *pBuffer1, uint16_t *pBuffer2, uint16_t BufferLength)
{
while (BufferLength--)
{
if (*pBuffer1 != *pBuffer2)
{
return 0;
}
pBuffer1++;
pBuffer2++;
}
return 1;
}
void bsp_spi_it_test(void)
{
while(DMA_Flag_Status_Get(DMA, DMA_CH1_TXCF) == RESET);
for (int i = 0; i < BufferSize; i++)
{
printf("%02x ", SPI_Slave_Buffer_Rx[i]);
}
printf("\r\n");
int s = Buffercmp(SPI_Slave_Buffer_Rx, SPI_Master_Buffer_Tx, BufferSize);
if (s)
{
printf("Test PASS!\r\n");
}
else
{
printf("Test ERR!\r\n");
}
}