使用stm32,spi接线就是一 一 对应接线而N32需要交叉接,为啥子哦、
参考:http://bbs.**/thread-1193300-1-1.html
#include "n32g430.h"
#include "bsp_spi.h"
#include "stdio.h"
#include "string.h"
#include "bsp_delay.h"
#define BufferSize 4
static uint16_t SPI_Master_Buffer_Tx[BufferSize] = {0X0102, 0X0304, 0X0506, 0X0708,};
static uint16_t SPI_Slave_Buffer_Rx[BufferSize];
static uint32_t TxIdx = 0, RxIdx = 0;
/*
#define SPI_MASTER_MOSI_PIN GPIO_PIN_7
#define SPI_MASTER_MISO_PIN GPIO_PIN_6
#define SPI_MASTER_CLK_PIN GPIO_PIN_5
#define SPI_MASTER_NSS_PIN GPIO_PIN_4
#define SPI_SLAVE_MOSI_PIN GPIO_PIN_15
#define SPI_SLAVE_MISO_PIN GPIO_PIN_14
#define SPI_SLAVE_CLK_PIN GPIO_PIN_13
#define SPI_SLAVE_NSS_PIN GPIO_PIN_12
*/
static void bsp_spi_it_rcc_cinfig(void)
{
/* 时钟分频PCLK2 = HCLK/2 */
RCC_Pclk2_Config(RCC_HCLK_DIV2);
/* Enable peripheral clocks --------------------------------------------------*/
/* spi 时钟 enable */
RCC_APB2_Peripheral_Clock_Enable(RCC_APB2_PERIPH_SPI1 | RCC_APB2_PERIPH_SPI2);
/* GPIO 外设 时钟 enable */
RCC_AHB_Peripheral_Clock_Enable(RCC_AHB_PERIPH_GPIOA | RCC_AHB_PERIPH_GPIOB);
}
static void bsp_spi_it_gpio_config(void)
{
GPIO_InitType GPIO_InitStructure;
GPIO_Structure_Initialize(&GPIO_InitStructure);
/* 配置主设备 master pins: NSS, SCK, MISO and MOSI */
GPIO_InitStructure.Pin = GPIO_PIN_7;
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(GPIOA, &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);
/* 配置从设备 slave pins: NSS, SCK, MISO and MOSI */
/* Confugure SPI pins as Input Floating */
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的中断
SPI_I2S_Interrupts_Enable(SPI1, SPI_I2S_INT_TE);
SPI_I2S_Interrupts_Enable(SPI2, SPI_I2S_INT_RNE);
}
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;//数据16位宽
SPI_InitStructure.CLKPOL = SPI_CLKPOL_LOW; //时钟默认电平是低
SPI_InitStructure.CLKPHA = SPI_CLKPHA_SECOND_EDGE;//第二个边沿采集数据
SPI_InitStructure.NSS = SPI_NSS_HARD;//硬件NSS
/* It is recommended that the SPI master mode of the C version chips should not exceed 18MHz */
SPI_InitStructure.BaudRatePres = SPI_BR_PRESCALER_64;//时钟分频
SPI_InitStructure.FirstBit = SPI_FB_MSB;//MSB
SPI_InitStructure.CRCPoly = 7;
SPI_Initializes(SPI1, &SPI_InitStructure);
SPI_NSS_Config(SPI1, SPI_NSS_SOFT);
SPI_Set_Nss_Level(SPI1, SPI_NSS_HIGH);
// SPI_SS_Output_Enable(SPI1);//NSS 使能
//SPI_CRC_Disable(SPI1);
/* SPI_SLAVE configuration ------------------------------------------------------*/
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_SS_Output_Enable(SPI2);//NSS 使能
//SPI_CRC_Disable(SPI2);
}
void SPI1_IRQHandler(void)
{
if (SPI_I2S_Interrupt_Flag_Status_Get(SPI1, SPI_I2S_INT_FLAG_TE) != RESET)
{
/* Send SPI_MASTER data */
SPI_I2S_Data_Transmit(SPI1, SPI_Master_Buffer_Tx[TxIdx++]);
if (TxIdx == BufferSize)
{
/* Disable SPI_MATER TXE interrupt */
SPI_I2S_Interrupts_Disable(SPI1, SPI_I2S_INT_TE);
}
}
}
/**
**/
void SPI2_IRQHandler(void)
{
if (SPI_I2S_Interrupt_Flag_Status_Get(SPI2, SPI_I2S_INT_FLAG_RNE) == SET)
{
/* Store SPI_SLAVE received data */
//printf("%02x ",SPI_I2S_Data_Get(SPI2));
SPI_Slave_Buffer_Rx[RxIdx++] = SPI_I2S_Data_Get(SPI2);
}
}
/*spi 中断模式接收发送数据测试*/
void bsp_spi_it_init(void)
{
//外设时钟使能
bsp_spi_it_rcc_cinfig();
//外设gpio设置
bsp_spi_it_gpio_config();
//中断配置
bsp_spi_it_nvic_config();
//spi外设配置
bsp_spi_it_config();
/* 使能spi外设 */
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(1)
// {
//
// while(RESET ==SPI_I2S_Flag_Status_Get(SPI1,SPI_I2S_FLAG_TE));
// printf("%02x ",SPI_Slave_Buffer_Rx[TxIdx%4]);
// SPI_I2S_Data_Transmit(SPI1, SPI_Master_Buffer_Tx[TxIdx++%4]);
// GPIO_Pin_Toggle(GPIOB, GPIO_PIN_2);
// //while(RESET ==SPI_I2S_Flag_Status_Get(SPI2,SPI_I2S_FLAG_RNE));
//
//
//}
while (RxIdx < 4)
{
SysTick_Delay_Ms(1);
}
for (int i = 0; i < BufferSize; i++)
{
printf("%02x ", SPI_Slave_Buffer_Rx[i]);
}
printf("\r\n");
/* Check the received data with the send ones */
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");
}
}
|