cancel
Showing results for 
Search instead for 
Did you mean: 

SPI on STM32F207 IG

smkuls
Associate II
Posted on March 01, 2014 at 18:26

The registers aren't getting affected when I run the following code on the board. The clock is getting set however the SPI->CR1 register is not getting affected at all, upon debugging the code it shows only 0's.

&sharpinclude <stm32f2xx.h>

&sharpinclude <GPIO_STM32F2xx.h>

//&sharpinclude ''SPI_STM32F2xx.c''

void init123()

{

RCC->AHB1ENR |= (0x01);   //IO port A clock enable for GPIO Pins for MOSI,MISO and SCK

GPIO_PinConfigure(GPIOA,5,GPIO_MODE_AF,GPIO_OUTPUT_PUSH_PULL,GPIO_OUTPUT_SPEED_50MHz,GPIO_NO_PULL_UP_DOWN); //For PA5

GPIO_PinConfigure(GPIOA,6,GPIO_MODE_AF,GPIO_OUTPUT_PUSH_PULL,GPIO_OUTPUT_SPEED_50MHz,GPIO_NO_PULL_UP_DOWN); //For PA6

GPIO_PinConfigure(GPIOA,7,GPIO_MODE_AF,GPIO_OUTPUT_PUSH_PULL,GPIO_OUTPUT_SPEED_50MHz,GPIO_NO_PULL_UP_DOWN); //For PA7

//For configuring the above pins in SPI

GPIO_PinAF (GPIOA,5,GPIO_AF_SPI1);

GPIO_PinAF (GPIOA,6,GPIO_AF_SPI1);

GPIO_PinAF (GPIOA,7,GPIO_AF_SPI1);

RCC->AHB1ENR |= (0x01<<3);  //IO port E clock enable fr GPIO Pins for Chip Select

GPIO_PinConfigure(GPIOE,7,GPIO_MODE_OUTPUT,GPIO_OUTPUT_PUSH_PULL,GPIO_OUTPUT_SPEED_50MHz,GPIO_PULL_UP); //For Chip Select - PE7

GPIOE->BSRRL |= (0x01<<6);  //For setting PE7 High

SPI1->CR1 &= ~(0x01<<6);   //SPI Enable

RCC->APB2ENR |= (0x01<<11);  //SPI clock enable

SPI1->CR1 &= ~(0x01<<15);   //Full Duplex mode

SPI1->CR1 |= (0x01<<2);   //SPI in Master Mode

SPI1->CR1 &= ~(0x01<<11);  //8 bit data format

SPI1->CR1 &= ~ (0x01<<1);   // CPOL - 0

SPI1->CR1 |= 0x01;    // CPHA - 1

SPI1->CR1 &= ~(0x111<<3);  //Baud Rate 000 config

SPI1->CR1 &= ~ (0x01<<6);  // MSB First

SPI1->CR1 |= (0x01<<6);   //SPI Enable

}

uint8_t send123(uint8_t data)

{

SPI1->DR=data;

while (!(SPI1->SR & 0x10));  // wait until transmit complete

while (!(SPI1->SR & 0x01));  // wait until receive complete

while (!(SPI1->SR & 0x01<<6));  // wait until SPI is not busy anymore

return SPI1->DR;

}

int main(void)

{

uint8_t received_val = 0;

init123();

while(1)

{

GPIOE->BSRRL |= (0x01<<6);  //For setting PE7 Low

send123(0xAA);  // transmit data

received_val = send123(0x00); // transmit dummy byte and receive data

GPIOE->BSRRL |= (0x01<<6);  //For setting PE7 High

}

}

#spi #stm32f2xx #stm32f2xx #spi
2 REPLIES 2
Posted on March 01, 2014 at 22:34

RCC->APB2ENR |= (0x01<<11);  //SPI clock enable

Isn't 0x1000 (1 << 12) ?, plus you really should do it several instructions prior to programming the peripheral.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
smkuls
Associate II
Posted on March 02, 2014 at 08:21

Thank you so much, that worked! I moved the SPI clock enable right after the configuration of the GPIO pins for SPI.

I am working on SPI for the first time and also I have no prior experience with this board.

I am trying to test the SPI code by sending and receiving data to/from MSP430F54388. When I run the code I am unable to send the data. The MODF bit in the status register gets set. The code gets stuck on the while loop (at the part where it is waiting for the transmit buffer to become empty)

Any suggestions?

I just want to test SPI, can you suggest another way to do it? Like a loopback or something? And if at all there is a way to do loopback then how do I proceed? By connecting the MISO and MOSI together? What about the Clock and the Chip Select?