You are on page 1of 4

EM MICROELECTRONIC - MARIN SA

RAISONANCE OPEN4
TUTORIAL SPI

RAISONANCE OPEN4 TUTORIAL


SPI: COMMUNICATION THROUGH THE WORLD

1-Objectives:
The main goal of this tutorial is to experiment the user to the SPI utilization from the OPEN4 Board using the ST Library and Circle OS. To Begin, lets describe the equipment used for this tutorial: OPEN4 BASE Board Primer and his Target Board STM32L152VBT6 :

The ST LIBRARY See the tutorial for ST Library Circle OS version 4.32 Ride7 Environment 7.42.12.0305 Standard Version Rkit-ARM for Ride7 1.48.13.0029 We will speak first about the Circle OS involvement in the STM32L Plateform. Using this Illustration of STM32L Peripherals, we can see the resources used by Circle OS :

Let us focus on SPIx use; we can see that SPI1 and SPI2 is by default used by Circle OS for MEMS and SD CARD Management By the way, in the case you dont use a SD CARD at all, you can use the SPI2 which is free.

This document is the property of EM MICROELECTRONIC-MARIN SA and is furnished in confidence and upon the condition that all rights originating in the information, whether patented or not, will be respected.

Page 1 of 4

EM MICROELECTRONIC - MARIN SA

RAISONANCE OPEN4
TUTORIAL SPI

In this tutorial, you will learn how to create an operational SPI communication.

2- Hardware description
This tutorial is focused on the use of the Extension board with an extra device, like a sensor Extension Board

Target Board

Connector 10x10 Pin In this application, youre able to interface a sensor on the extension board via the communication standard available thanks to the connector 10x10 pin which make the link to the STM32L152 You can find the list which describes well the pin available on the connector in the STM32L-EvoPrimer_Manual.pdf and make the link with the STM32L5xx_datasheet.pdf where you can find the Pin description and pin localization. To use the SPI2, we will use the GPIOs as follows: STM32L Pin description PB8 (if you need NSS (Chip Select) pin for Hardware NSS) PB13 (SPI2 _ SCK) PB14 (SPI2 _ MISO) PB15 (SPI2 _ MOSI) Extension Board correspondence Pin 8 : WS Pin 7 : SCK Pin 5 : MISO Pin 6 : SD

3-Managing your Library


In your library named: stm32lxx_spi.c or stm32nameofyourtargetboard xx_spi.c, you can access the timer initialization function and operation. By example: How to use this driver Topic which explains the function group and how to configure your SPI, but it is not exhaustive and this tutorial will complete this. void SPI_StructInit() which initialize the SPIx init structure parameters values. etc just read the function description You will need: Stm32lxx_gpio library Stm32lxx_rcc library Stm32lxx_gpio library Lets begin as an example by a simple configuration of your SPI2 peripheral.

4-SPI initialization for communication


I will bring to you my method to configure the SPI2 peripheral. Lets create the variable type structure SPI and GPIO

SPI_InitTypeDef SPI2_Init; GPIO_InitTypeDef GPIO_SPI_Init;

This document is the property of EM MICROELECTRONIC-MARIN SA and is furnished in confidence and upon the condition that all rights originating in the information, whether patented or not, will be respected.

Page 2 of 4

EM MICROELECTRONIC - MARIN SA

RAISONANCE OPEN4
TUTORIAL SPI

These structure will contains few element of configuration

typedef struct { uint16_t SPI_Direction;

/*!< Specifies the SPI unidirectional or bidirectional data mode. This parameter can be any combination of @ref SPI_data_direction */ /*!< Specifies the SPI operating mode. This parameter can be any combination of @ref SPI_mode */

uint16_t SPI_Mode; . }SPI_InitTypeDef;

For other structure or function details, refer to the stm32lxx_spi.c and stm32lxx_spi.h. First of all, you need to configure your GPIO :

//ACTIVATION DU SPI2 GPIO_StructInit(&GPIO_SPI_Init); GPIO_SPI_Init.GPIO_Pin = GPIO_Pin_8; Pin selection GPIO_SPI_Init.GPIO_Mode = GPIO_Mode_OUT; Mode selection OUTPUT GPIO_SPI_Init.GPIO_OType = GPIO_OType_PP; GPIO_SPI_Init.GPIO_Speed = GPIO_Speed_40MHz; GPIO_Init(GPIOB,&GPIO_SPI_Init); Function of initialization GPIO_SPI_Init.GPIO_Pin = GPIO_Pin_15 | GPIO_Pin_13; GPIO_SPI_Init.GPIO_Mode = GPIO_Mode_AF; Mode Alternate Function GPIO_SPI_Init.GPIO_OType = GPIO_OType_PP; PUSH PULL GPIO_SPI_Init.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_SPI_Init.GPIO_Speed = GPIO_Speed_40MHz; Maximum Speed GPIO_Init(GPIOB,&GPIO_SPI_Init); GPIO_SPI_Init.GPIO_Pin = GPIO_Pin_14; GPIO_SPI_Init.GPIO_Mode = GPIO_Mode_AF; GPIO_SPI_Init.GPIO_OType = GPIO_OType_OD; OPEN DRAIN GPIO_SPI_Init.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_SPI_Init.GPIO_Speed = GPIO_Speed_40MHz; GPIO_Init(GPIOB,&GPIO_SPI_Init);

The NSS of SPI2 is not available; by the way, we are using an extra pin: PB8. It will compute the NSS role. In a Second time, we will configure the SPI2 Peripheral:

//SPI CHOICE IS_SPI_ALL_PERIPH(SPI2); SPI_SSOutputCmd(SPI2, ENABLE); Output activation //STRUCTURE INITIALIZATION SPI_StructInit(&SPI2_Init); Initialization Structure //CONFIGURATION SPI : SPI2_Init.SPI_Direction = SPI_Direction_2Lines_FullDuplex; Mode Full duplex SPI2_Init.SPI_Mode = SPI_Mode_Master; SPI2_Init.SPI_DataSize = SPI_DataSize_16b; depends on your device spec SPI2_Init.SPI_CPOL = SPI_CPOL_High; depends on your device spec SPI2_Init.SPI_CPHA = SPI_CPHA_2Edge; depends on your device spec SPI2_Init.SPI_NSS = SPI_NSS_Soft; Soft Mode for managing the NSS state (using the function SPI_NSSInternalSoftwareConfig(SPI2,SPI_NSSInternalSoft_Reset);) SPI2_Init.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; Depends on your device SPI Clock (for a device with 10 MHz clock, 32MHz / 4 = 8 MhZ) SPI2_Init.SPI_FirstBit = SPI_FirstBit_MSB; depends on your device spec SPI2_Init.SPI_CRCPolynomial = 0;

This document is the property of EM MICROELECTRONIC-MARIN SA and is furnished in confidence and upon the condition that all rights originating in the information, whether patented or not, will be respected.

Page 3 of 4

EM MICROELECTRONIC - MARIN SA

RAISONANCE OPEN4
TUTORIAL SPI

For finish, you just have to enable your SPI by using the function:

SPI_Cmd(SPI2,ENABLE);

In theory, this is sufficient to configure your SPI2 but we will see that we must coordinate operation for synchronize the communication.

5- Sending and receiving data


To send or receive a data, the process of communication depends on your device. For the case study, my device just needs to wait the NSS pin goes low and after, the data transmission comes by itself.

u16 COM_SPI(u8 Address, u8 Data) { u16 Trame = 0; u16 out = 0; Trame = Address << 8; Trame = Trame | (u16)Data; GPIO_ResetBits(GPIOB, GPIO_Pin_8); NSS Hard goes low SPI_NSSInternalSoftwareConfig(SPI2,SPI_NSSInternalSoft_Reset); NSS Master Soft Goes low SPI_I2S_SendData(SPI2,Trame); //SEND DATA while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE) == 0); Wait for empty buffer of transmission SPI_I2S_ClearFlag(SPI2,SPI_I2S_FLAG_TXE); Clear Flag and interrupt SPI_I2S_ClearFlag(SPI2,SPI_I2S_FLAG_RXNE); SPI_I2S_ClearITPendingBit(SPI2, SPI_I2S_IT_TXE); //RECEIVE DATA while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_RXNE )== 0); Wait for filled buffer of reception SPI_I2S_ClearFlag(SPI2,SPI_I2S_FLAG_RXNE); GPIO_SetBits(GPIOB, GPIO_Pin_8); NSS Hard Goes high SPI_NSSInternalSoftwareConfig(SPI2,SPI_NSSInternalSoft_Set); NSS Master Soft Goes high out = SPI_I2S_ReceiveData(SPI2); return out; } We can note that the reception of data is always done during a transmission; the device simply answers to my request and I must read the buffer to evacuate the data. Otherwise, the reception buffer will not be cleared and store the previous values. The master does not have a Hard NSS Pin connected to your device; this is why we use it in Software mode.

6-In a nutshell
You must understand the NSS pin specificity and how to configure GPIO, SPI Activate Peripheral Clock : RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); Configure GPIO Structure Configure SPI2 Structure Create a function computing the SPI Communication Launch SPI2

This document is the property of EM MICROELECTRONIC-MARIN SA and is furnished in confidence and upon the condition that all rights originating in the information, whether patented or not, will be respected.

Page 4 of 4

You might also like