首次使用N32G430,SPI初始化踩坑,同系列芯片很快就移植成功,遇到这款芯片,照着历程折腾了N久,折腾了时钟,对照了IO口,核对了重映射,都没有实现,甚是不解,到最后才发现少了行同系列芯片都没有的代码SPI_Set_Nss_Level(SPI1, SPI_NSS_HIGH)。
void spi1Init(void) //SPI1 gpio and mode initial
{
SPI_I2S_Reset(SPI1);
RCC_AHB_Peripheral_Clock_Enable(RCC_AHB_PERIPH_GPIOA|RCC_AHB_PERIPH_GPIOB); //GPIO Periph clock enable
RCC_APB2_Peripheral_Clock_Enable(RCC_APB2_PERIPH_SPI1); //spi clock enable
RCC_APB2_Peripheral_Clock_Enable(RCC_APB2_PERIPH_AFIO);
GPIO_InitType GPIO_InitStructure; //SPI1 GPIO initial
GPIO_Structure_Initialize(&GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_5; //MOSI
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_4; //MISO
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.GPIO_Slew_Rate = GPIO_SLEW_RATE_FAST;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF2_SPI1;
GPIO_Peripheral_Initialize(GPIOB, &GPIO_InitStructure);
/* If CLKPOL is HIGH, set SPI_SCK GPIO to GPIO_PULL_UP, otherwise set to GPIO_PULL_DOWN */
GPIO_InitStructure.Pin = GPIO_PIN_3; //SCK
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.GPIO_Slew_Rate = GPIO_SLEW_RATE_FAST;
GPIO_InitStructure.GPIO_Pull = GPIO_PULL_UP;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF2_SPI1;
GPIO_Peripheral_Initialize(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_15; //CS
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT_PP;
GPIO_InitStructure.GPIO_Pull = GPIO_PULL_UP;
GPIO_InitStructure.GPIO_Current = GPIO_DS_4MA;
GPIO_InitStructure.GPIO_Slew_Rate = GPIO_SLEW_RATE_FAST;
GPIO_Peripheral_Initialize(GPIOA, &GPIO_InitStructure);
SPI_InitType SPI_InitStructure;
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_8;
SPI_InitStructure.FirstBit = SPI_FB_MSB; //FirstBit :MSB most significant bit
SPI_InitStructure.CRCPoly = 7; //CRC
SPI_Initializes(SPI1, &SPI_InitStructure);
SPI1_CS(1); //sFLASH_CS_HIGH();
SPI_Set_Nss_Level(SPI1, SPI_NSS_HIGH); //
SPI_ON(SPI1);
}
uint8_t spi1ReadWriteByte(uint8_t byte)
{
SPI_I2S_Data_Transmit(SPI1,byte);
while(SPI_I2S_Flag_Status_Get(SPI1, SPI_I2S_FLAG_TE) == RESET);
while(SPI_I2S_Flag_Status_Get(SPI1, SPI_I2S_FLAG_RNE) == RESET) ;
return SPI1->DAT;
}