Communicating with an EEPROM

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

Introduction

Simulink Coder Support Package for Beaglebone Blue 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 Beaglebone Blue 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:

  • Beaglebone Blue board

  • CAT25256 256kB SPI EEPROM Memory

  • USB cable

  • Breadboard wires

  • A breadboard (recommended)

Model

The following figure shows the example model:

SPI Interface on the Beaglebone Blue Hardware

The SPI interface is enabled by default in Beaglebone Blue.

Task 1 - Connect the EEPROM to the Beaglebone Blue Hardware

In this task, you connect the EEPROM to the Beaglebone Blue board.

1. Refer to the following pin-out diagram of Beaglebone Blue for connections.

Attach the EEPROM to the Beaglebone Blue board using the following connections:

   SPI EEPROM pin   | Beaglebone Blue pin
 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
      /CS   (pin 1) |  SPI1_SS1
      SO    (pin 2) |  SPI1_MISO
      /WP   (pin 3) |  3.3 V
      VSS   (pin 4) |  GND
      SI    (pin 5) |  SPI1_MOSI
      SCK   (pin 6) |  SPI1_SCK
      /HOLD (pin 7) |  3.3 V
      VCC   (pin 8) |  3.3 V
 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

Task 2 - Configure the Model for Supported Beaglebone Blue Hardware

In this task, you configure the model for Beaglebone Blue hardware.

1. Open the Communicating with an SPI based EEPROM using Beaglebone Blue Hardware model.

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

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

4. Click Apply.

Task 3 - Configure the Model to Write and Read Data using the 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 Communicating with an SPI based EEPROM using Beaglebone Blue Hardware 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 SPI select parameter of the SPI WriteRead block is set to S1.1.

  • 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 on the block > Properties > General > Priority. To know more about block priorities and their impact on block execution order, refer to Set 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 Beaglebone Blue. Notice the Mode (Clock polarity and phase) parameter on SPI WriteRead, SPI Register Write, and SPI Register Read blocks are set to 0.

Task 4 - 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

  • Follow the steps in this example to communicate to other SPI devices.

Summary

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

  • Communicate with an SPI device.

  • Initialize an EEPROM for a write operation.

  • Program the Beaglebone Blue board to write and read multiple bytes of data from the SPI EEPROM.

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