German English
Peripherals

MX25L8005 - Access the MX25L8005 SPI Flash memory

The microcontroller on the radio module Preon32 has several IO interfaces which allows accessing nearly each kind of sensor and peripheral.

This examples shows how to use the serial flash memory chip MX25L8005 which has a SPI (Serial Protocol Interface) interface. The chip can operate at a SPI bus frequency of 25 MHz which is used to communicate with the chip.

Firstly the chip is erased.
Then a 'Hello World' string is written to a fixed address in chip.
At last IO streams are used to read and write data.
/*
 * Copyright (c) 2011., Virtenio GmbH
 * All rights reserved.
 */

package com.virtenio.webdemo;

import com.virtenio.drivers.flash.Flash;
import com.virtenio.drivers.flash.FlashSectorEraseStrategy;
import com.virtenio.drivers.flash.spi.MX25L8005;
import com.virtenio.drivers.gpio.GPIO;
import com.virtenio.drivers.gpio.NativeGPIO;
import com.virtenio.drivers.spi.NativeSPI;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FlashSPI {

    public static void main() throws IOException {

        NativeSPI spi = NativeSPI.getInstance(1);
        spi.open(0, 0, 25000000);
        GPIO chipSelect = NativeGPIO.getInstance(42);
        Flash flash = new MX25L8005(spi, chipSelect);

        // chip erase
        flash.eraseChip();
        flash.waitWhileBusy();

        // rw function
        flash.eraseSector(0);
        byte[] b1 = "Hello world!".getBytes();
        flash.write(0, b1);
        b1 = b1.clone();
        flash.read(0, b1);

        // streaming function
        OutputStream out = flash.getOutputStream(0, new FlashSectorEraseStrategy());
        InputStream in = flash.getInputStream(0);
        byte[] buffer = new byte[200];
        for (int i = 0; i < 25; i++) {
            System.out.println("Buf: " + i);
            out.write(buffer);
            byte[] check = new byte[buffer.length];
            in.read(check);
        }

    }

}