cancel
Showing results for 
Search instead for 
Did you mean: 

Chip Select Pin in SPI with STM32F4Discovery

salahuddinash
Associate II
Posted on April 23, 2014 at 23:20

Hi everyone, 

I'm trying to implement SPI on stm32f4discovery kit 

here's the code I found on some blog

<b>

#include ''stm32f4_discovery.h''

#include <stm32f4xx.h>

#include <stm32f4xx_spi.h>

void delay(uint32_t cnt);

// this function initializes the SPI1 peripheral

void init_SPI1(void){

GPIO_InitTypeDef GPIO_InitStruct;

SPI_InitTypeDef SPI_InitStruct;

// enable clock for used IO pins

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

/* configure pins used by SPI1

* PA5 = SCK

* PA6 = MISO

* PA7 = MOSI

*/

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_6 | GPIO_Pin_5;

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;

GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_Init(GPIOA, &GPIO_InitStruct);

// connect SPI1 pins to SPI alternate function

GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);

// enable clock for used IO pins

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);

/* Configure the chip select pin

  in this case we will use PE7 */

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;

GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;

GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;

GPIO_Init(GPIOE, &GPIO_InitStruct);

GPIOE->BSRRL |= GPIO_Pin_7; // set PE7 high

// enable peripheral clock

RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

/* configure SPI1 in Mode 0 

* CPOL = 0 --> clock is low when idle

* CPHA = 0 --> data is sampled at the first edge

*/

SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // set to full duplex mode, seperate MOSI and MISO lines

SPI_InitStruct.SPI_Mode = SPI_Mode_Master;     // transmit in master mode, NSS pin has to be always high

SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; // one packet of data is 8 bits wide

SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;        // clock is low when idle

SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;      // data sampled at first edge

SPI_InitStruct.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set; // set the NSS management to internal and pull internal NSS high

SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; // SPI frequency is APB2 frequency / 4

SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first

SPI_Init(SPI1, &SPI_InitStruct); 

SPI_Cmd(SPI1, ENABLE); // enable SPI1

}

/* This funtion is used to transmit and receive data 

 * with SPI1

 * data --> data to be transmitted

 * returns received value

 */

uint8_t SPI1_send(uint8_t data){

SPI1->DR = data; // write data to be transmitted to the SPI data register

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

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

while( SPI1->SR & SPI_I2S_FLAG_BSY ); // wait until SPI is not busy anymore

return SPI1->DR; // return received data from SPI data register

}

int main(void){

uint8_t received_val = 0;

init_SPI1();

STM_EVAL_LEDInit(LED3);

STM_EVAL_LEDInit(LED4);

STM_EVAL_LEDInit(LED5);

STM_EVAL_LEDInit(LED6);

while(1){

GPIOE->BSRRH |= GPIO_Pin_7; // set PE7 (CS) low

SPI1_send(0xAA);  // transmit data

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

received_val += 0;

GPIOE->BSRRL |= GPIO_Pin_7; // set PE7 (CS) high

STM_EVAL_LEDOn(LED3);

STM_EVAL_LEDOn(LED4);

STM_EVAL_LEDOn(LED5);

STM_EVAL_LEDOn(LED6);

delay(3*1000000);

STM_EVAL_LEDOff(LED3);

STM_EVAL_LEDOff(LED4);

STM_EVAL_LEDOff(LED5);

STM_EVAL_LEDOff(LED6);

delay(3*1000000);

}

}

void delay(uint32_t cnt)

{

while(cnt--);

}

</b>

Now I don't understand the point related to PE7, it's said here that it's the Chip Select or Slave Select pin, but I checked out the user manual of stm32f4discovery kit and I didn't find that so .. instead I found PE3 intersected with (LIS302DL or LIS3DSH) there's (CS_I2C/SPI)

now what is LIS302DL or LIS3DSH and how the above code worked well and the LEDs blinked also it used PE7 not PE3

and a final question, how can I make the microcontroller communicate with RealTerm ? I mean how to adjust slave or master mode in the RealTerm also there's no tab specified for SPI but there's a tab for I2C?

Thanks for your time .. 
3 REPLIES 3
Posted on April 23, 2014 at 23:27

What does the unspecified blog say about PE7, perhaps they had some other arbitrary SPI device hung off the STM32F4-DISCO?

The bytes (commands) you send to the SPI device need to mean something too it, otherwise you're going to get odd responses.

The RealTerm SPI/I2C stuff relates to different modes of operation and hardware used by the guys that wrote RealTerm, they have no context related to the STM32, you are just sending characters for the terminal to display.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
salahuddinash
Associate II
Posted on April 24, 2014 at 00:24

Thanks sir for your quick answer

 

 

What does the unspecified blog say about PE7, perhaps they had some other arbitrary SPI device hung off the STM32F4-DISCO?

The blog doesn't explain anything, all the explanation is in the comments in the code.

The strange thing is that it looks like that this code works well on my stm32f4-discovery kit and the LEDs blink.. 

The bytes (commands) you send to the SPI device need to mean something too it, otherwise you're going to get odd responses.

I don't know what do you mean with that?

Posted on April 24, 2014 at 00:50

The strange thing is that it looks like that this code works well on my stm32f4-discovery kit and the LEDs blink.. 

But it does nothing in particular, it's like you winding the crank on a conveyer belt, whether there is anything on it, or not, the belt still advances.

I don't know what do you mean with that?

It means you need to look at the data sheet for the SPI device you are trying to talk to, what specific bytes, or sequence of bytes it expects to receive, and what it will send back to you.

For an SPI Flash device you might send a READ command, followed by several bytes describing the address within the device you wish to access.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..