Main Content

Communicate with EEPROM Using Raspberry Pi

This example shows how to use Simulink® Support Package for Raspberry Pi® Hardware to read from and write to an SPI EEPROM.

Introduction

Simulink Support Package for Raspberry Pi Hardware 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 connected to the Raspberry Pi board via SPI. The example uses the 256kB "ON Semiconductor EEPROM CAT25256". This device uses a standard SPI protocol that is common to many other EEPROMs provided by different vendors. For more details about the device, refer to the CAT25256 datasheet.

Prerequisites

Required Hardware

To run this example, you need the following hardware:

  • Raspberry Pi board

  • CAT25256 256kB SPI EEPROM Memory

  • USB cable

  • Breadboard wires

  • A breadboard (recommended)

Configure SPI Interface on Raspberry Pi Hardware

The SPI interface is disabled by default in Raspberry Pi. To enable the SPI interface use the raspi-config utility that comes with the Raspbian Linux® image. You can open an interactive shell to your Raspberry Pi board and execute the following command:

     $ sudo raspi-config

To enable or disable the SPI interface, use up and down arrow keys to select the Advanced Options > SPI menu item which brings up a Yes or No question.

Go back to the main menu using left and right arrow keys and select Finish. If the raspi-config utility asks you to reboot, accept it. After your board reboots, the SPI interface is enabled on channels CE0 and CE1.

Task 1 - Connect EEPROM to Raspberry Pi Hardware

In this task, you connect the EEPROM to the Raspberry Pi board.

1. Refer to the following pin-out diagram of Raspberry Pi 3 Model B for connections.

Attach the EEPROM to the Raspberry Pi board using the following connections:

   SPI EEPROM pin   | Raspberry Pi pin
 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
      /CS   (pin 1) |  GPIO 8 (CE0)
      SO    (pin 2) |  GPIO 9 (SPI0_MISO)
      /WP   (pin 3) |  5 V
      VSS   (pin 4) |  GND
      SI    (pin 5) |  GPIO 10 (SPI0_MOSI)
      SCK   (pin 6) |  GPIO 11 (SPI0_SCLK)
      /HOLD (pin 7) |  5 V
      VCC   (pin 8) |  5 V
 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

You can also connect /CS pin of EEPROM to GPIO 7 (CE1) of Raspberry Pi. This example shows Raspberry Pi 3 Model B connected to EEPROM. However, you can connect any Raspberry Pi board supported by Simulink Support Package for Raspberry Pi Hardware. Use showPins method of the raspi object to check the pin-out diagram of your Raspberry Pi board.

Task 2 - Configure Simulink Model for Supported Raspberry Pi Hardware

In this task, you configure the model for Raspberry Pi hardware.

1. Open the raspberrypi_SPI_EEPROM Simulink model.

2. In your Simulink model, click Simulation > Model Configuration Parameters to open Configuration Parameters dialog.

3. Select the Hardware Implementation pane and select Raspberry Pi from the Hardware board parameter list. Do not change any other settings.

4. Click Apply.

Task 3 - Configure SPI Properties of Simulink Model

In this task, you configure the SPI properties of the model according to the SPI settings supported by the EEPROM.

1. Click Target Hardware Resources > SPI.

2. Set the SPI0 CE0 BusSpeed(kHz) parameter in the SPI group to one of the values in dropdown, say, 8000. As per the CAT25256 datasheet, the EEPROM supports a maximum SPI clock frequency of 20 MHz.

3. Click OK.

Task 4 - Configure Simulink Model to Write and Read Data Using SPI Blocks

In this task, you have to configure the model to read data from the EEPROM using the SPI WriteRead, SPI Register Write, and SPI Register Read blocks. You will run the model in External mode to view the data read from the EEPROM.

1. Open the raspberrypi_SPI_EEPROM Simulink model.

2. Notice the following subsystems in the model:

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 runs at model initialization while the Execution_loop subsystem runs every sample hit. 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 as per the CAT25256 datasheet:

       Command  | Opcode | Operation
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
        WREN    | 6      | Enable Write Operations
        WRITE   | 2      | Write Data to Memory
        READ    | 3      | Read Data from Memory
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

3. Open the One_time_initialization subsystem and note the following:

  • The Chip select pin parameter of the SPI WriteRead block is set to SPI0_CE0.

  • Before writing to the EEPROM memory, you need to enable the write operations on the device. The Write Enable constant block outputs WREN command (numeric value 6) to the EEPROM.

  • After the WRITE command is sent, the 16-bit EEPROM memory location followed by the data bytes are sent to the EEPROM.

The Register address parameter in the SPI Register Write block contains [2 0 10] which sends the WRITE command followed by the 16-bit memory location 10 (0x000A) on the EEPROM. The Data to Write constant block with the value of [101 -19 35 -87 4] writes 5 int8 values to the EEPROM.

  • The delay subsystems provide the delay required for the successful completion of the Write Enable before sending data values.

  • The priority of the delay subsystem (corresponding to the WREN command) is set to 1. The priority of the SPI Register Write block (corresponding to the WRITE command) is set to any value higher than 1. 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 the block > Properties > General > Priority. To know more about block priorities and their impact on block execution order, refer to Specify Block Properties.

4. Open the Execution_loop subsystem and observe the following:

  • After the READ command, the 16-bit EEPROM memory location is sent.

The Register address parameter in the SPI Register Read block with the value of [3 0 10] sends the READ command followed by the 16-bit memory location 10 on the EEPROM. The Output data type parameter is set to int8 and Output data length parameter is set to 5 corresponding to the data type and number of values written to the EEPROM.

5. 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. This behavior corresponds to SPI mode 0 (Clock Polarity 0, Clock Phase 0) of the Raspberry Pi. Notice the Mode (Clock polarity and phase) parameter on SPI WriteRead, SPI Register Write, and SPI Register Read blocks are set to 0.

6. Mark the output of the Execution_loop subsystem for logging. By logging the signal, you can inspect and compare data from multiple simulations for validating model designs For information on signal logging, see Configure Signals for Logging.

Task 5 - Run Simulink Model

1. Open the Modeling tab and press Ctrl+E to open Configuration Parameters dialog box.

2. In the Configuration Parameters dialog box, select Hardware Implementation.

3. From the Groups list under Target hardware resources, select External Mode and verify that the Communication Interface parameter is set to XCP on TCP/IP.

4. On the Hardware tab of the Simulink model, in the Mode section, select Run on board and then click Monitor & Tune.

5. In the Execution_loop subsystem, monitor the Display block to observe the data read from the EEPROM.

6. When new simulation data becomes available in Simulation Data Inspector(SDI), the Simulation Data Inspector button appears highlighted. In the model toolbar, click the Simulation Data Inspector button to inspect and compare data from multiple simulations for validating model designs. For more information on SDI, see Analyze Simulation Results.

7. Click Stop to end the Monitor and Tune 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.