Main Content

Communicating with EEPROM Using BeagleBone Blue Hardware

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

Introduction

The 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:

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 beagleboneblue_SPI_EEPROM Simulink model.

2. In your Simulink model, click Simulation > Model Configuration Parameters to open the 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 beagleboneblue_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 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 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 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.