Example description
SPI_FLASH SPI FLASH example
Example Description
This example provides a basic example of how to use the SPI firmware library
and an associated driver "spi_flash.c" to communicate with an M25P64 SPI FLASH.
The first step consist in reading the SPI Flash ID. A comparison between the ID
read from SPI flash and the expected one is done and LED1 is turned on in case
of success otherwise LED2 is turned on.
Using this driver the program performs an erase of the sector to be accessed, a
write of a Tx_Buffer, defined in the main.c file, to the memory followed by a read
of the written data. Then data read from the memory stored in the Rx_Buffer are
compared with the expected values of the Tx_Buffer. The result of this comparison
is stored in the "TransferStatus1" variable.
A second erase of the same sector is done at the end, and a test is done to be
sure that all the data written there are erased further to the sector erase. All
the data location are read and checked with 0xFF value. The result of this test
is stored in "TransferStatus2" variable which is FAILED in case of error.
The FLASH_WRITE_ADDRESS and the FLASH_READ_ADDRESS where the program start the
write and the read operations are defined in the main.c file.
Directory contents
- SPI/SPI_FLASH/spi_flash.c SPI M25Pxxx driver
- SPI/SPI_FLASH/spi_flash.h Header for spi_flash.c
- SPI/SPI_FLASH/stm32f2xx_conf.h Library Configuration file
- SPI/SPI_FLASH/stm32f2xx_it.c Interrupt handlers
- SPI/SPI_FLASH/stm32f2xx_it.h Header for stm32f2xx_it.c
- SPI/SPI_FLASH/main.c Main program
- SPI/SPI_FLASH/system_stm32f2xx.c STM32F2xx system source file
The "system_stm32f2xx.c" is generated by an automatic clock configuration
tool and can be easily customized to your own configuration.
To select different clock setup, use the "STM32F2xx_Clock_Configuration_V1.0.0.xls" tool.
Hardware and Software environment
- This example runs on STM32F2xx.
- This example can't be tested with STM322xG-EVAL evaluation board (no SPI
FLASH available).
- HW platform Set-up
- Connect LED1 and LED2 respectively to PG.06 and PG.08 pins
- Connect both SPI2 and SPI FLASH pins as following:
- Connect SPI2_NSS (PI0) pin to SPI Flash chip select (pin1)
- Connect SPI2_SCK (PI1) pin to SPI Flash serial clock (pin6)
- Connect SPI2_MISO(PI2) pin to SPI Flash serial data output (pin2)
- Connect SPI2_MOSI(PI3) pin to SPI Flash serial data input (pin5)
- Connect SPI Flash Write Protect (pin3) to Vdd
- Connect SPI Flash Hold (pin7) to Vdd
- Connect SPI Flash Vcc (pin8) to Vdd
- Connect SPI Flash Vss (pin4) to Vss
===============================================================================
SPI_DataExchangeInterrupt SPI Communication Boards Data Exchange using interrupt example
Example Description
This example provides a small application in which joystick buttons are used
to trigger SPI communication using interrupts and though using SPI firmware
library.
- Hardware Description
To use this example, you need to load it on two STM32 boards (let's call them
BoardA and BoardB) then connect these two boards through SPI lines and GND.
In the firmware example uncomment the dedicated line in the main.h file to use
the SPI peripheral as STM32 Master device or as STM32 Slave .
*------------------------------------------------------------------------------*
| BoardA BoardB |
| ____________________ ____________________ |
| | | | | |
| | | | | |
| | __________ | | __________ | |
| | |SPI Master|____|_______SCK_______|____|SPI Slave | | |
| | | Device |____|_______MOSI______|____| Device | | |
| | |__________| | | |__________| | |
| | | | | |
| | O LD1 | | O LD1 | |
| | O LD2 Joystick | | O LD2 Joystick | |
| | O LD3 _ | | O LD3 _ | |
| | O LD4 |_| | | O LD4 |_| | |
| | | | | |
| | GND O--|-----------------|--O GND | |
| |____________________| |____________________| |
| |
| |
*------------------------------------------------------------------------------*
The connection between the pins should use a short wires and a common Ground.
- Software Description
In master board, SPI peripheral is configured as Master full duplex with Interrupt,
whereas in Slave board, SPI peripheral is configured as Slave Full duplex with Interrupt.
At each joystick buttons press:
- The Master sends a specific command to the Slave, the command contains the
transaction code (CMD_RIGHT, CMD_LEFT, CMD_UP, CMD_DOWN or CMD_SEL), then
the defined NumberOfByte from TxBuffer.
- The Slave after checking the received command, compares the NumberOfByte received data
with the defined ones into TxBuffer. Received data correctness is signaled by LED
lightening and though as follow:
- Received command CMD_RIGHT and data correctly received --> LD2 and LD3 ON
- Received command CMD_LEFT and data correctly received --> LD4 ON
- Received command CMD_UP and data correctly received --> LD2 ON
- Received command CMD_DOWN and data correctly received --> LD3 ON
- Received command CMD_SEL and data correctly received --> LD2, LD3 and LD4 ON
In both boards, the data transfers is managed using SPIx_IRQHandler in stm32f2xx_it.c
file.
The SysTick is configured to generate interrupt each 10ms. A dedicated counter
inside the SysTick ISR is used to toggle LD1 each 100ms indicating that the
firmware is running.
These operations can be repeated infinitely.
A defined communication timeout is insuring that the application will not remain
stuck if the SPI communication is corrupted.
You can adjust this timeout through the USER_TIMEOUT define inside main.h file
depending on CPU frequency and application conditions (interrupts routines,
number of data to transfer, baudrate, CPU frequency...).
When a given operation is not finished within the defined timeout, a callback is
made to the function "TimeOut_UserCallback()", this function is used to manage
timeout communication failure. In this example, this callback implements a simple
infinite loop, but user can add his own implementation to manage timeout failure.
Directory contents
- SPI/SPI_TwoBoards/DataExchangeInterrupt/stm32f2xx_conf.h Library Configuration file
- SPI/SPI_TwoBoards/DataExchangeInterrupt/stm32f2xx_it.c Interrupt handlers
- SPI/SPI_TwoBoards/DataExchangeInterrupt/stm32f2xx_it.h Interrupt handlers header file
- SPI/SPI_TwoBoards/DataExchangeInterrupt/main.c Main program
- SPI/SPI_TwoBoards/DataExchangeInterrupt/main.h Main program header file
- SPI/SPI_TwoBoards/DataExchangeInterrupt/system_stm32f2xx.c STM32F2xx system source file
The "system_stm32f2xx.c" is generated by an automatic clock configuration
tool and can be easily customized to your own configuration.
To select different clock setup, use the "STM32F2xx_Clock_Configuration_V1.0.0.xls" tool.
Hardware and Software environment
- This example runs on STM32F2xx Devices.
- This example has been tested with STM322xG-EVAL RevB (with no additional
hardware), and can be easily tailored to any other development board.
- STM322xG-EVAL Set-up
- Use LED1, LED2, LED3 and LED4 connected respectively to PG.06, PG.08, PI.09
and PC.07 pins
- Use the Joystick Push Button
- Connect BoardA SPI2 SCK pin (PI1) to BoardB SPI2 SCK pin (PI1)
- Connect BoardA SPI2 MOSI pin (PI3) to BoardB SPI2 MOSI pin (PI3)