学前班
最后登录1970-1-1
在线时间 小时
注册时间2017-6-9
|
本帖最后由 zeng5200 于 2017-6-9 14:59 编辑
根据SPI 对Flash的读写代码修改的外部SPI接口初始化,基本上就是原装的,改了引脚的宏定义!这样初始化能成功吗?
我按照这样初始化,读取外设模块的ID就是读不出来~!求大神帮忙看看!
引脚宏定义:
#define macSPIx SPI1
#define macSPI_APBxClock_FUN RCC_APB2PeriphClockCmd
#define macSPI_CLK RCC_APB2Periph_SPI1
#define macSPI_CS_APBxClock_FUN RCC_APB2PeriphClockCmd
#define macSPI_CS_CLK RCC_APB2Periph_GPIOA
#define macSPI_CS_PORT GPIOA
#define macSPI_CS_PIN GPIO_Pin_7
#define macSPI_SCK_APBxClock_FUN RCC_APB2PeriphClockCmd
#define macSPI_SCK_CLK RCC_APB2Periph_GPIOA
#define macSPI_SCK_PORT GPIOC
#define macSPI_SCK_PIN GPIO_Pin_4
#define macSPI_MISO_APBxClock_FUN RCC_APB2PeriphClockCmd
#define macSPI_MISO_CLK RCC_APB2Periph_GPIOA
#define macSPI_MISO_PORT GPIOA
#define macSPI_MISO_PIN GPIO_Pin_5
#define macSPI_MOSI_APBxClock_FUN RCC_APB2PeriphClockCmd
#define macSPI_MOSI_CLK RCC_APB2Periph_GPIOA
#define macSPI_MOSI_PORT GPIOG
#define macSPI_MOSI_PIN GPIO_Pin_15
/************************** SPI Flash º¯Êýºê¶¨Òå********************************/
#define macSPI_FLASH_CS_ENABLE() GPIO_ResetBits( macSPI_CS_PORT, macSPI_CS_PIN )
#define macSPI_FLASH_CS_DISABLE() GPIO_SetBits( macSPI_CS_PORT, macSPI_CS_PIN )
SPI初始化:
void SPI_FLASH_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable SPI1 and GPIO clocks */
/*!< SPI_FLASH_SPI_CS_GPIO, SPI_FLASH_SPI_MOSI_GPIO,
SPI_FLASH_SPI_MISO_GPIO, SPI_FLASH_SPI_DETECT_GPIO
and SPI_FLASH_SPI_SCK_GPIO Periph clock enable */
/*!< SPI_FLASH_SPI Periph clock enable */
macSPI_APBxClock_FUN ( macSPI_CLK, ENABLE );
/*!< Configure SPI_FLASH_SPI_CS_PIN pin: SPI_FLASH Card CS pin */
macSPI_CS_APBxClock_FUN ( macSPI_CS_CLK, ENABLE );
GPIO_InitStructure.GPIO_Pin = macSPI_CS_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(macSPI_CS_PORT, &GPIO_InitStructure);
/*!< Configure SPI_FLASH_SPI pins: SCK */
macSPI_SCK_APBxClock_FUN ( macSPI_SCK_CLK, ENABLE );
GPIO_InitStructure.GPIO_Pin = macSPI_SCK_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(macSPI_SCK_PORT, &GPIO_InitStructure);
/*!< Configure SPI_FLASH_SPI pins: MISO */
macSPI_MISO_APBxClock_FUN ( macSPI_MISO_CLK, ENABLE );
GPIO_InitStructure.GPIO_Pin = macSPI_MISO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(macSPI_MISO_PORT, &GPIO_InitStructure);
/*!< Configure SPI_FLASH_SPI pins: MOSI */
macSPI_MOSI_APBxClock_FUN ( macSPI_MOSI_CLK, ENABLE );
GPIO_InitStructure.GPIO_Pin = macSPI_MOSI_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(macSPI_MOSI_PORT, &GPIO_InitStructure);
/* Deselect the FLASH: Chip Select high */
macSPI_FLASH_CS_DISABLE();
/* SPI1 configuration */
// W25X16: data input on the DIO pin is sampled on the rising edge of the CLK.
// Data on the DO and DIO pins are clocked out on the falling edge of CLK.
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 0;
SPI_Init(macSPIx , &SPI_InitStructure);
/* Enable SPI1 */
SPI_Cmd(macSPIx , ENABLE);
}
|
-
SPI接口引脚
|