This example shows how to use Simulink® Coder Support Package for STMicroelectronics Nucleo board to enable and write to/ read from an SPI based EEPROM.
Simulink Support Package for STMicroelectronics Nucleo Boards enables you to use the SPI interface to communicate with SPI based devices.
In this example, you will learn how to communicate with an EEPROM interfaced to the STMicroelectronics Nucleo board via SPI. The example uses the 256kB "EEPROM CSI25C256K". This device uses a standard SPI protocol that is common to many other EEPROMs provided by different vendors. Make sure yours is compatible to the one used in this example.
This example shows how to program the STMicroelectronics Nucleo board to write to and read from the EEPROM.
Available models of this example:
STM Nucleo board:
The provided model is pre-configured for STMicroelectronics Nucleo F401RE board. You can configure this model for other supported Nucleo boards by browsing to Configuration Parameters > Hardware Implementation > Hardware board and selecting the required board.
We recommend completing the following examples:
To run this example, you will need the following hardware:
Supported STMicroelectronics Nucleo board
CSI25C256K 256kB SPI EEPROM Memory
Small breadboard (recommended)
The following figure shows the example model:
In this task, you will connect the EEPROM to the STMicroelectronics Nucleo board.
1. Attach the EEPROM to the STMicroelectronics Nucleo board using the following table:
SPI EEPROM pin | STMicroelectronics Nucleo F401RE board pin _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ /CS (pin 1) | 10 SO (pin 2) | 12 /WP (pin 3) | 5 V VSS (pin 4) | GND SI (pin 5) | 11 SCK (pin 6) | 13 /HOLD (pin 7) | 5 V VCC (pin 8) | 5 V _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
Most of the Nucleo board uses MBED abstraction and thus implements Arduino uno header which means same pins are used for designated functionality. However in case of Nucleo F031K6, Arduino nano headers are used by MBED abstraction.
SPI EEPROM pin | STMicroelectronics Nucleo F031K6 board pin _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ /CS (pin 1) | 6 SO (pin 2) | 12 /WP (pin 3) | 5 V VSS (pin 4) | GND SI (pin 5) | 11 SCK (pin 6) | 13 /HOLD (pin 7) | 5 V VCC (pin 8) | 5 V _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
In this task, you will configure the model for the supported STMicroelectronics Nucleo board.
1. Open the STMicroelectronics Nucleo SPI model.
2. In your Simulink model, open the Modeling tab and press CTRL+E to open Configuration Parameters dialog box.
3. Select the Hardware Implementation pane and select your required STMicroelectronics Nucleo board from the Hardware Implementation > Hardware board parameter list.
4. Click Apply.
In this task, you will configure the SPI properties of the model according to the SPI settings supported by the EEPROM.
1. Click Target Hardware Resources > SPI.
2. Set bus speed and SPI pins required for your hardware board. The EEPROM latches the input data on the rising edge of the SCK clock and shifts out data on the falling edge of the SCK clock.
3. Click OK.
In this task, you will configure the model to read data from the EEPROM using the SPI Master Transfer block. You will perform Monitor & Tune action to view the read data.
1. Notice the two subsystems in the model STMicroelectronics Nucleo SPI:
The One_time_initialization subsystem enables the EEPROM and writes data to it.
The Execution_loop subsystem reads data from the EEPROM.
The One_time_initialization subsystem executes only once at the first step of the execution of the model. For the rest of the execution time the Execution_loop subsystem runs. This selective execution of subsystems is handled using the Unit Delay and the Enabled Subsystem blocks from the Simulink library.
The EEPROM uses the following opcodes for enable, write, and read operations.
Command | Opcode | Operation _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ WREN | 6 | Enable Write Operations WRITE | 2 | Write Data to Memory READ | 3 | Read Data from Memory _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
2. Open the One_time_initialization subsystem and note the following points:
The Slave select pin parameter of the SPI Master Transfer block is set to 10 according to the connection in the circuit diagram.
Before writing to the EEPROM memory, you need to enable the write operations on the device. The Write Enable constant block set to 6 is connected to the SPI Master Transfer block. This sends the WREN command to the EEPROM over the SPI bus.
The Write constant block with the value of [101:105] sends the 5 bytes from 101 to 105 to write to the EEPROM.
The delay subsystems provide the delay required before writing data bytes to the SPI EEPROM after the write enable.
The priority of the delay subsystem is set to 10. The priority of the SPI Register Write block is set to 20. This ensures that the EEPROM is write enabled before sending the data bytes to be written. To set the Priority of a block, right click on the block > Properties > General > Priority. To know more about block priorities and their impact on block execution order, refer to Set Block Properties.
3. Open the Execution_loop subsystem and observe the following:
The SPI Register Read block reads the values from memory location on the EEPROM.
In this task you will simulate the model in Normal mode with Simulink IO to validate the design before generating code and deploying the model on the Nucleo board. For more information see Communicate with Hardware in Normal Mode Simulation Using Simulink IO.
To simulate the model in Normal Mode with Simulink IO:
1. Enable Simulink IO as described in How to Enable Simulink IO.
2. Click the Run button on the toolbar of the model to run it in Normal mode.
3. In the Execution_loop subsystem, monitor the Display block to observe the data read from the EEPROM.
4. Click the Stop button in the Simulink model to end the Normal mode execution.
In this task, you will perform Monitor and Tune to monitor the data read from the EEPROM.
1. Open the Hardware tab and click Monitor & Tune.
2. In the Execution_loop subsystem, monitor the Display block to observe the data read from the EEPROM.
3. Click the Stop button in the Hardware tab of Simulink model to end the Monitor and Tune execution.
Change the SPI clock frequency to a different value supported by the EEPROM.
Follow the steps in this example to communicate to other SPI devices.