cancel
Showing results for 
Search instead for 
Did you mean: 

SPI2 Not active

lsc7718
Associate
Posted on November 16, 2011 at 01:47

#define SPI2_GPIO_CLK     RCC_APB2Periph_GPIOB

#define SPI2_PORT      GPIOB

#define SPI2_CLK                     RCC_APB1Periph_SPI2

#define SPI2_SCK_PIN               GPIO_Pin_13               

#define SPI2_MISO_PIN                GPIO_Pin_14               

#define SPI2_MOSI_PIN                GPIO_Pin_15               

#define SPI2_NSS_PIN                    GPIO_Pin_12               

#endif /* USE_SPI2 */

#define DUMMY_DATA      0x11

#define F_RAM_SPI_ID     SPI2_ID

#define F_RAM_PORT      SPI2_PORT

#define F_RAM_SCK_PIN     SPI2_SCK_PIN

#define F_RAM_MISO_PIN     SPI2_MISO_PIN                             

#define F_RAM_MOSI_PIN     SPI2_MOSI_PIN                             

#define F_RAM_NSS_PIN     SPI2_NSS_PIN     

#define __SPI_HW__ 1

void SPI_Low_DeInit(void)

{

 SPI_Cmd(SPI2,DISABLE);

 SPI_I2S_DeInit(SPI2);

}

void RCC_Configuration_SPI(void)

{

  /*!< F_RAM_SPI_CS_GPIO, F_RAM_SPI_MOSI_GPIO, F_RAM_SPI_MISO_GPIO

       and F_RAM_SPI_SCK_GPIO Periph clock enable */

#ifdef USE_SPI2

 RCC_APB2PeriphClockCmd(SPI2_GPIO_CLK | RCC_APB2Periph_AFIO, ENABLE);

 RCC_APB1PeriphClockCmd(SPI2_CLK, ENABLE);

#endif

}

void GPIO_Configuration_SPI(void)

{

 GPIO_InitTypeDef GPIO_InitStructure;

 

 

#ifdef USE_SPI2

 /*!< Configure F_RAM_SPI pins: SCK */

#ifdef __SPI_HW__

 GPIO_InitStructure.GPIO_Pin = SPI2_SCK_PIN | SPI2_MOSI_PIN;

 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

 GPIO_Init(SPI2_PORT, &GPIO_InitStructure);

#elif __SPI_SW__

 GPIO_InitStructure.GPIO_Pin = SPI2_SCK_PIN | SPI2_MOSI_PIN;

 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

 GPIO_Init(SPI2_PORT, &GPIO_InitStructure);

#endif /* __SPI_HW__ */

 GPIO_InitStructure.GPIO_Pin = SPI2_MISO_PIN;

 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

 //GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

 GPIO_Init(SPI2_PORT, &GPIO_InitStructure);

 

 /*!< Configure F_RAM_CS_PIN pin: F_RAM Card CS pin */

 GPIO_InitStructure.GPIO_Pin = SPI2_NSS_PIN;

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

 GPIO_Init(SPI2_PORT, &GPIO_InitStructure);

#endif /* USE_SPI2 */

}

void SPI_Configuration(void)

{

#ifdef __SPI_HW__

 SPI_InitTypeDef  SPI_InitStructure;

 

 /*!< Deselect the F-RAM: Chip Select high */

 //F_RAM_HOLD_HIGH();

 

 F_RAM_CS(Bit_SET);

#ifdef USE_SPI2

 SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

 SPI_InitStructure.SPI_Mode = SPI_Mode_Master;

 SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;

#if 0

 SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;

 SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;

#else

 SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;

 SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;

#endif

 SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;

#if 0

 SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;

#else

 SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;

#endif

#if 0

 SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;

#else

 SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

#endif

 SPI_InitStructure.SPI_CRCPolynomial = 7;

 SPI_Init(SPI2, &SPI_InitStructure);

 /*!< Enable the sFLASH_SPI  */

 SPI_Cmd(SPI2, ENABLE);

#endif /* USE_SPI2 */

#endif /* __SPI_HW__ */

}

void main(void)

{

 SPI_Low_DeInit();

 GPIO_Configuration_SPI();

 RCC_Configuration_SPI();

 SPI_Configuration();

}

u8 F_RAM_SendByte(u8 byte)

{

 u8 ret;

   /*!< Loop while DR register in not emplty */

   while (SPI_I2S_GetFlagStatus(F_RAM_SPI, SPI_I2S_FLAG_TXE) == RESET);

   /*!< Send byte through the SPI1 peripheral */

   SPI_I2S_SendData(F_RAM_SPI,(u16)byte);

 

 /*!< Wait to receive a byte */

 while (SPI_I2S_GetFlagStatus(F_RAM_SPI, SPI_I2S_FLAG_RXNE) == RESET);

 /*!< Return the byte read from the SPI bus */

 ret = (u8)SPI_I2S_ReceiveData(F_RAM_SPI);

 return ret;

}

===============================================================

2 REPLIES 2
Posted on November 16, 2011 at 02:16

You'll need to be enabling the clocks on the peripherals if you want the settings to work.

ie RCC before GPIO

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
lsc7718
Associate
Posted on November 16, 2011 at 02:46

change RCC and GPIO.

but not active spi2.

schmetic is ...

0690X00000602j7QAA.jpg

0690X00000602jRQAQ.jpg