You are on page 1of 4

SA

EM MICROELECTRONIC - MARIN

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. For To Begin, lets speak about 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

For introduce, wWe 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

SA

EM MICROELECTRONIC - MARIN

RAISONANCE OPEN4
TUTORIAL SPI

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

2-A bit of 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. For To usinge the SPI2, we will use the GPIOs as followsing: 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-Managinge 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 SPI_I SPI_I nitTyp nitTyp Lets begin by as an example by, a simple eDef configuration of your SPI2 peripheral. eDef SPI2_ SPI2_ Init; Init; 4-SPI initialization for communication GPIO GPIO _InitT _InitT I will bring to you my method for to configure the SPI2 peripheral. ypeD ypeD Lets create the variable type structure SPI and GPIO ef ef GPIO GPIO _SPI_ _SPI_ Init; 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

SA

EM MICROELECTRONIC

typedefstruct struct typedef {{ uint16_tSPI_Direction; SPI_Direction; uint16_t /*!< Specifiesthe the /*!< Specifies - MARIN SPIunidirectional unidirectionalor or SPI bidirectionaldata datamode. mode. bidirectional Thisparameter parametercan canbe be This any combination of @ref any combination of @ref SPI_data_direction*/ */ SPI_data_direction

RAISONANCE OPEN4
TUTORIAL SPI

uint16_tSPI_Mode; SPI_Mode; uint16_t /*!<Specifies Specifiesthe the /*!< SPIoperating operatingmode. mode. SPI These structure will contains few element of configuration Thisparameter parametercan canbe be This anycombination combinationof of@ref @ref any SPI_mode*/ */ SPI_mode . . }SPI_InitTypeDef; }SPI_InitTypeDef;
//ACTIVATIONDU DUSPI2 SPI2 //ACTIVATION GPIO_StructInit(&GPIO_SPI_Init); GPIO_StructInit(&GPIO_SPI_Init); GPIO_SPI_Init.GPIO_Pin==GPIO_Pin_8; GPIO_Pin_8; GPIO_SPI_Init.GPIO_Pin Pinselection selection Pin GPIO_SPI_Init.GPIO_Mode== GPIO_SPI_Init.GPIO_Mode GPIO_Mode_OUT; Mode Modeselection selectionOUTPUT OUTPUT GPIO_Mode_OUT; GPIO_SPI_Init.GPIO_OType== GPIO_SPI_Init.GPIO_OType GPIO_OType_PP; GPIO_OType_PP; GPIO_SPI_Init.GPIO_Speed== GPIO_SPI_Init.GPIO_Speed GPIO_Speed_40MHz; toGPIO_Speed_40MHz; the stm32lxx_spi.c and stm32lxx_spi.h. GPIO_Init(GPIOB,&GPIO_SPI_Init); GPIO_Init(GPIOB,&GPIO_SPI_Init); Functionof ofinitialization initialization Function GPIO_SPI_Init.GPIO_Pin==GPIO_Pin_15 GPIO_Pin_15| | GPIO_SPI_Init.GPIO_Pin GPIO_Pin_13; GPIO_Pin_13; GPIO_SPI_Init.GPIO_Mode== GPIO_SPI_Init.GPIO_Mode GPIO_Mode_AF; Mode ModeAlternate AlternateFunction Function GPIO_Mode_AF; GPIO_SPI_Init.GPIO_OType== GPIO_SPI_Init.GPIO_OType GPIO_OType_PP; PUSH PUSHPULL PULL GPIO_OType_PP; GPIO_SPI_Init.GPIO_PuPd== GPIO_SPI_Init.GPIO_PuPd GPIO_PuPd_NOPULL; GPIO_PuPd_NOPULL; GPIO_SPI_Init.GPIO_Speed== GPIO_SPI_Init.GPIO_Speed GPIO_Speed_40MHz; Maximum MaximumSpeed Speed GPIO_Speed_40MHz; GPIO_Init(GPIOB,&GPIO_SPI_Init); GPIO_Init(GPIOB,&GPIO_SPI_Init); GPIO_SPI_Init.GPIO_Pin==GPIO_Pin_14; GPIO_Pin_14; GPIO_SPI_Init.GPIO_Pin //SPICHOICE CHOICE //SPI GPIO_SPI_Init.GPIO_Mode GPIO_SPI_Init.GPIO_Mode == IS_SPI_ALL_PERIPH(SPI2); IS_SPI_ALL_PERIPH(SPI2); GPIO_Mode_AF; GPIO_Mode_AF; SPI_SSOutputCmd(SPI2, SPI_SSOutputCmd(SPI2, GPIO_SPI_Init.GPIO_OType = GPIO_SPI_Init.GPIO_OType ENABLE); Output Outputactivation activation = ENABLE); GPIO_OType_OD; OPEN OPENDRAIN DRAIN GPIO_OType_OD; //STRUCTURE INITIALIZATION //STRUCTURE INITIALIZATION GPIO_SPI_Init.GPIO_PuPd GPIO_SPI_Init.GPIO_PuPd == SPI_StructInit(&SPI2_Init); SPI_StructInit(&SPI2_Init); GPIO_PuPd_NOPULL; GPIO_PuPd_NOPULL; Initialization Structure Initialization Structure GPIO_SPI_Init.GPIO_Speed GPIO_SPI_Init.GPIO_Speed == GPIO_Speed_40MHz; GPIO_Speed_40MHz; //CONFIGURATIONSPI SPI: : //CONFIGURATION GPIO_Init(GPIOB,&GPIO_SPI_Init); GPIO_Init(GPIOB,&GPIO_SPI_Init); SPI2_Init.SPI_Direction== SPI2_Init.SPI_Direction SPI_Direction_2Lines_FullDuplex; Mode Mode SPI_Direction_2Lines_FullDuplex; Fullduplex duplex Full SPI2_Init.SPI_Mode== SPI2_Init.SPI_Mode SPI_Mode_Master; SPI_Mode_Master; SPI2_Init.SPI_DataSize== SPI2_Init.SPI_DataSize The NSS of SPI2 is not available; by the way, we are using an extra pin: PB8. It will compute SPI_DataSize_16b; depends depends onyour your SPI_DataSize_16b; on In a Second time, we will configure the SPI2 Peripheral: device spec device spec SPI2_Init.SPI_CPOL== SPI2_Init.SPI_CPOL SPI_CPOL_High; depends dependson onyour your SPI_CPOL_High; devicespec spec device SPI2_Init.SPI_CPHA== SPI2_Init.SPI_CPHA SPI_CPHA_2Edge; depends dependson onyour your SPI_CPHA_2Edge; devicespec spec device SPI2_Init.SPI_NSS== SPI2_Init.SPI_NSS SPI_NSS_Soft; Soft SoftMode Modefor formanaging managing SPI_NSS_Soft; theNSS NSSstate state(using (usingthe thefunction function the SPI_NSSInternalSoftwareConfig(SPI2,SPI_ SPI_NSSInternalSoftwareConfig(SPI2,SPI_ NSSInternalSoft_Reset);) NSSInternalSoft_Reset);) SPI2_Init.SPI_BaudRatePrescaler SPI2_Init.SPI_BaudRatePrescaler SPI_BaudRatePrescaler_4; Depends Dependson on ==SPI_BaudRatePrescaler_4; yourdevice deviceSPI SPIClock Clock(for (foraadevice devicewith with10 10 your MHzclock, clock,32MHz 32MHz/ /44==88MhZ) MhZ) MHz SPI2_Init.SPI_FirstBit== SPI2_Init.SPI_FirstBit SPI_FirstBit_MSB; depends dependson onyour your SPI_FirstBit_MSB; devicespec spec device SPI2_Init.SPI_CRCPolynomial== SPI2_Init.SPI_CRCPolynomial 0; 0;

For other structure or function details, refer

First of all, you need to configure your GPIO :

the NSS role.

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

S S TUTORIAL SPI PI PI _ _ C C m m For finish, you just have to enable your SPI by using the function: d( d( S S PI PI 2, 2, E E N u16COM_SPI(u8 COM_SPI(u8Address, Address,u8 u8Data) Data) N u16 A { In theory, this is sufficient toA configure your SPI2 but we will see that we must coordinate operation for synchronize the { B u16Trame Trame= =0; 0; communication. B u16 L u16out out= =0; 0; L u16 E) Trame= =Address Address<< <<8; 8; E) Trame 5- Sending and receivinge data Trame= =Trame Trame| |(u16)Data; (u16)Data; ;; Trame GPIO_ResetBits(GPIOB,GPIO_Pin_8); GPIO_Pin_8); GPIO_ResetBits(GPIOB, For To sending data or receive a data, the process of communication depends on your device. NSSHard Hardgoes goeslow low NSS For my cFor the case study, my device just needs to wait the NSS pin goes low and after, the data transmission comes by itself. SPI_NSSInternalSoftwareConfig(SPI2,SPI_NSSInternal SPI_NSSInternalSoftwareConfig(SPI2,SPI_NSSInternal Soft_Reset); NSS NSSMaster MasterSoft SoftGoes Goeslow low Soft_Reset); SPI_I2S_SendData(SPI2,Trame); SPI_I2S_SendData(SPI2,Trame); //SENDDATA DATA //SEND while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TX while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TX E)== ==0); 0); Wait Waitfor forempty emptybuffer bufferof oftransmission transmission E) SPI_I2S_ClearFlag(SPI2,SPI_I2S_FLAG_TXE); SPI_I2S_ClearFlag(SPI2,SPI_I2S_FLAG_TXE); ClearFlag Flagand andinterrupt interrupt Clear SPI_I2S_ClearFlag(SPI2,SPI_I2S_FLAG_RXNE); SPI_I2S_ClearFlag(SPI2,SPI_I2S_FLAG_RXNE); SPI_I2S_ClearITPendingBit(SPI2, SPI_I2S_ClearITPendingBit(SPI2, SPI_I2S_IT_TXE); SPI_I2S_IT_TXE); //RECEIVEDATA DATA //RECEIVE while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_RX while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_RX NE)== )==0); 0); Wait Waitfor forfilled filledbuffer bufferof ofreception reception NE SPI_I2S_ClearFlag(SPI2,SPI_I2S_FLAG_RXNE); SPI_I2S_ClearFlag(SPI2,SPI_I2S_FLAG_RXNE); GPIO_SetBits(GPIOB,GPIO_Pin_8); GPIO_Pin_8); NSS NSS GPIO_SetBits(GPIOB, Hard Goes high Hard Goes high SPI_NSSInternalSoftwareConfig(SPI2,SPI_NSSInternal SPI_NSSInternalSoftwareConfig(SPI2,SPI_NSSInternal Soft_Set); NSS NSSMaster MasterSoft SoftGoes Goeshigh high Soft_Set); out= =SPI_I2S_ReceiveData(SPI2); SPI_I2S_ReceiveData(SPI2); out We can note that the reception of data is always return done during return out; a transmission; the device simply answers to my request and I must out; read the buffer for 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.

SA

EM MICROELECTRONIC - MARIN

RAISONANCE OPEN4

6-Lets recapIn 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