特别注意NSS引脚的配置,见代码。
1.GPIO的配置与103系列的不同;
2.SPI功能更强大,比如支持CRC校验,配置与103系列不同;
其他特性请参考datasheet.
- /*******************************************************************************
- //STM32F207VGT6 FOR ENC28J60
- //SPI2初始化/IO初始化等
- //SPI configuration
- @@ JUST FOR STM32F2XX
- *******************************************************************************/
- void ENC28J60_SPI2_Init(void)
- {
- SPI_InitTypeDef SPI_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- /*!< Enable the SPI2 clock */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
-
- /*!< Enable GPIO clocks */
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
- /*!< SPI2 pins configuration */
-
- /* Configure SPI2 pins as alternate function (No need to
- configure PB12 since NSS will be managed by software) */
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_SPI2); //nss
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_SPI2); //sck
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_SPI2); //miso
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_SPI2); //mosi
-
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // also 100Mhz
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // GPIO_PuPd_DOWN
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
- GPIO_Init(GPIOB, &GPIO_InitStructure);// PB13/14/15-SCK,MISO,MOSI
-
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_12; // RESRT and CS for enc28j60
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- /*!< Configure enc28j60 interrupt pin in PullUp mode ********************/
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- /*!< SPI2 pin configuration */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //INT
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;//SPI设置为双线双向全双工
- SPI_InitStructure.SPI_Mode = SPI_Mode_Master;//设置为主SPI
- SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;//设置SPI的数据大小:SPI发送接收8位帧结构
- SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;//选择了串行时钟的稳态:时钟悬空高
- SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;//数据捕获于第二个时钟沿
- SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;//(SPI_NSS_Soft)此时NSS引脚可以配置成普通GPIO去控制从设备
- SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;//Fclk/2
- SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; /* Initialize the SPI_FirstBit member */
- SPI_InitStructure.SPI_CRCPolynomial=7;
- SPI_Init(SPI2, &SPI_InitStructure);
- SPI_Cmd(SPI2, ENABLE);
-
- }
- /*******************************************************************************
- * Function Name : SPI_ReadWriteByte
- * Description : SPI读写一个字节(发送完成后返回本次通讯读取的数据)
- * Input : unsigned char TxData
- * Output : None
- * Return : unsigned char RxData
- *******************************************************************************/
- unsigned char SPI2_ReadWriteByte(unsigned char TxData)
- {
- unsigned char RxData = 0;
- /* Wait till Transmit buffer is empty */
- while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
- /* Send A data */
- SPI_I2S_SendData(SPI2, TxData);
- // while(SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) == RESET);
- //等待数据接收
- while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
- //取数据
- RxData = SPI_I2S_ReceiveData(SPI2);
- return (unsigned char)RxData;
- }
复制代码
|