MATLAB Examples

Communicating with an SPI based EEPROM using STMicroelectronics Nucleo board

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.

Supported Hardware:

Available versions of this example:

STM Nucleo board: stmnucleo_SPI_EEPROM

The provided model is pre-configured for STMicroelectronics Nucleo F401RE board. You can configure this model for other supported Nucleo boards by selecting them as the "Hardware board" on the Hardware Implementation pane of the Model Configuration Parameters as described in Task 2 of this example.

Contents

Introduction

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.

Prerequisites

We recommend completing the Getting Started with STMicroelectronics Nucleo board example and completing the Communicating with STMicroelectronics Nucleo board example to learn about External mode.

Required Hardware

To run this example, you will need the following hardware:

  • Supported STMicroelectronics Nucleo board
  • CSI25C256K 256kB SPI EEPROM Memory
  • USB cable
  • Breadboard wires
  • Small breadboard (recommended)

Model

The following figure shows the example model:

Task 1 - Connect the EEPROM to the STMicroelectronics Nucleo board

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
 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

Task 2 - Configure the Model for Supported STMicroelectronics Nucleo board

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, click Simulation > Model Configuration Parameters to open Configuration Parameters dialog.

3. Select the Hardware Implementation pane and select your required STMicroelectronics Nucleo board from the Hardware Implementation > Hardware board parameter list.

4. Click Apply.

Task 3 - Configure the SPI properties of the Model

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.

Task 4 - Configure the Model to Write and Read Data using the SPI Master Transfer

In this task, you will configure the model to read data from the EEPROM using the SPI Master Transfer block. You will run the model in External mode 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.

Task 5 - Run the Model in External Mode

In this task, you will run the model in External mode to monitor the data read from the EEPROM.

1. Change the Simulation mode on the toolbar of the model to External.

2. Click the Run button on the toolbar of the model to run it in External 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 External mode execution.

Other Things to Try

  • 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.

Summary

This example showed how to program your STMicroelectronics Nucleo board to write to and read from an SPI based EEPROM. In this example you learned how to:

  • Communicate with an SPI device.
  • Initialize the EEPROM to enable write operations.
  • Program the STMicroelectronics Nucleo board to write and read multiple bytes of data from the SPI based EEPROM.

You can use this example as a reference to access other SPI devices.