本帖最后由 LOVEEVER 于 2024-11-14 17:23 编辑
芯源cw32f003的spi参考初始化程序
void SPI_FLASH_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/************************GPIO Configuration***********************/
RCC_AHBPeriphClk_Enable(FLASH_SPI_SCK_GPIO_CLK | FLASH_SPI_MISO_GPIO_CLK | FLASH_SPI_MOSI_GPIO_CLK | FLASH_SPI_CS_GPIO_CLK, ENABLE);
RCC_APBPeriphClk_Enable2(RCC_APB2_PERIPH_SPI, ENABLE);
//SPI SCK MOSI MISO 复用
FLASH_SPI_AF_SCK;
FLASH_SPI_AF_MISO;
FLASH_SPI_AF_MOSI;
//CS
GPIO_InitStructure.Pins = FLASH_SPI_CS_GPIO_PIN;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_Init(FLASH_SPI_CS_GPIO_PORT, &GPIO_InitStructure);
//SCK
GPIO_InitStructure.Pins = FLASH_SPI_SCK_GPIO_PIN;
GPIO_Init(FLASH_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);
//MOSI
GPIO_InitStructure.Pins = FLASH_SPI_MOSI_GPIO_PIN;
GPIO_Init(FLASH_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);
//MISO
GPIO_InitStructure.Pins = FLASH_SPI_MISO_GPIO_PIN;
GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
GPIO_Init(FLASH_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);
//拉高CS
FLASH_SPI_CS_HIGH();
/************************SPI Configuration***********************/
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // 双线全双工
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; // 主机模式
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; // 帧数据长度为8bit
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; // 时钟空闲电平为高
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; // 第二个边沿采样
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; // 片选信号由SSI寄存器控制
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; // 波特率为PCLK的8分频
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; // 最高有效位 MSB 收发在前
SPI_InitStructure.SPI_Speed = SPI_Speed_Low; // 低速SPI
SPI_Init(&SPI_InitStructure);
SPI_Cmd(ENABLE);
}
|