Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

SPI flash memory issue


  • Please log in to reply
2 replies to this topic

#1 todotani

todotani

    New Member

  • Members
  • Pip
  • 3 posts
  • LocationYokohama, JAPAN

Posted 27 September 2010 - 11:52 AM

Hi,

I'm trying to handle AT45DB161D SPI flash memory using the following code and could not read status/ID info correctly.
The test code read only status register and device ID (read/write data block is not implemented yet).

The read value from this test code resulted 0xFF, not expected value.
However, the same code worked as expected with FEZ Domino.
As I don't have oscilloscope, I could not narrow down the issue. Is there any advice for more investigation?

Test environments:
  • Firmware: 4.1.0.3
  • Host PC OS: Windows 7
What I did to fined cause of issue (but still resulted NG)
  • Change clock rate lower/higher than 1000Khz
  • Change SPI mode to 3 (Clock_IdleState = true, Clock_Edge = false)
  • Specify ChipSelect_Port in "SPI.Configuration". AS this resulted NG even with FEZ Domino, it seems AT45DB161D require CS active during command send and result acquisition period
  • Insert wait (1ms) between CS active and start transaction
  • Change jumper wire of SCK
  • Power supply from external power jack
As Arduino Ethernet shield worked with my Netduino, SPI data transmission between W5100 chip is working fine.

using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

/* -------------------------------------------
 * Pin assignment Netduino <--> AT45DB161D
 *  CS	D8  -- 4  Pull-up with 10k ohm R
 *  RESET D9  -- 3  Pull-down with 10k ohm R
 *  MOSI  D11 -- 1
 *  MISO  D12 -- 8
 *  SCK   D13 -- 2
--------------------------------------------*/

namespace NetSpiFlash
{
	public class Program
	{
    	public static void Main()
    	{
        	OutputPort csPin = new OutputPort(Pins.GPIO_PIN_D8, true);
        	OutputPort resetPin = new OutputPort(Pins.GPIO_PIN_D9, true);
        	SPI.Configuration config = new SPI.Configuration(
            	Cpu.Pin.GPIO_NONE,	// ChipSelect_Port. Not connected to the device
            	false,  // ChipSelect_ActiveState : active low
            	0,  	// ChipSelect_SetupTime
            	0,  	// ChipSelect_HoldTime
            	false,  // Clock_IdleState : the idle state of the clock is low
            	true,   // Data is sampled on the SPI clock riging edge (Mode 0)
            	1000,   // Clock_Rate in KHz
            	SPI.SPI_module.SPI1 // SPI_mod : D11 MOSI, D12 MISO, D13 SCK
        	);
        	SPI spi1 = new SPI(config);
        	AT45DB161D SpiFlash = new AT45DB161D(spi1, csPin, resetPin);

        	byte status;
        	while (true)
        	{
            	status = SpiFlash.ReadStatusRegister();
            	Debug.Print("Status Reg:" + status.ToString());
            	SpiFlash.ReadManufacturerAndDeviceID();
            	Debug.Print("Manufacturer ID:" + SpiFlash.Manufacturer.ToString());
            	Debug.Print("Device ID:" + SpiFlash.Device[0].ToString());
            	Thread.Sleep(1000);
        	}
    	}
	}

	public class AT45DB161D
	{
    	// AT45DB161D_Commands
    	private const byte STATUS_REGISTER_READ = 0xD7;
    	private const byte READ_MANUFACTURER_AND_DEVICE_ID = 0x9F;

    	// Chip Select value
    	private const bool CS_INACTIVE = true;
    	private const bool CS_ACTIVE = false;

    	// SPI read/write data
    	private byte[] writeBuffer = new byte[1];
    	private byte[] readBuffer = new byte[1];
    	private byte[] dummy = { 0 }; 

    	// Device ID feild
    	public byte Manufacturer;
    	public byte[] Device = new byte[2];
    	public byte ExtendedInfoLength;

    	private SPI spi;
    	private OutputPort cs;

    	/// <summary>
    	/// Constructor for Atmel AT45DB161D data flash
    	/// </summary>
    	public AT45DB161D(SPI spiMod, OutputPort csPin, OutputPort resetPin)
    	{
        	spi = spiMod;
        	cs = csPin;
        	cs.Write(CS_INACTIVE);

        	// Reset AT45DB161D chip
        	resetPin.Write(false);
        	Thread.Sleep(10);
        	resetPin.Write(true);
    	}

    	public byte ReadStatusRegister()
    	{
        	cs.Write(CS_ACTIVE);
        	//Thread.Sleep(1);
        	// Send status read command
        	SPIWriteByte(STATUS_REGISTER_READ);
        	byte status = SPIReadByte();
        	cs.Write(CS_INACTIVE);
        	return status;
    	}

    	public void ReadManufacturerAndDeviceID()
    	{
        	cs.Write(CS_ACTIVE);
        	//Thread.Sleep(1);
        	// Send status read command
        	SPIWriteByte(READ_MANUFACTURER_AND_DEVICE_ID);
        	// Read Manufacturer ID (1FH, 31)
        	Manufacturer = SPIReadByte();
        	// Read Device ID (Part 1) (26H, 38)
        	Device[0] = SPIReadByte();
        	// Read Device ID (Part 2) (00H)
        	Device[1] = SPIReadByte();
        	// Read Extended Device Information String Length (00H)
        	ExtendedInfoLength = SPIReadByte();
        	cs.Write(CS_INACTIVE);
    	}

    	private void SPIWriteByte(byte B)
    	{
        	writeBuffer[0] = b;
        	spi.Write(writeBuffer);
    	}

    	private byte SPIReadByte()
    	{
        	spi.WriteRead(dummy, readBuffer);
        	return readBuffer[0];
    	}
	}
}


#2 CoolStuff

CoolStuff

    Member

  • Members
  • PipPip
  • 11 posts

Posted 27 September 2010 - 02:15 PM

Did you try the driver provided on tinyclr.com?

http://www.tinyclr.c.../FEZ_Shields_Flash.cs

You can use it on FEZ Domino or Netduino, it is open source.

#3 todotani

todotani

    New Member

  • Members
  • Pip
  • 3 posts
  • LocationYokohama, JAPAN

Posted 27 September 2010 - 07:29 PM

Hi CoolStuff, Thank you for your response. GHI Flash Shield driver worked fine with Netduino! My code (actually port from Arduino driver) called two SPI function for register read like; 1) spi.Write(Status_Resgiter_Read_Command) 2) spi.WriteRead(dummy(0), readBuffer) I fund that GHI code packs 1) and 2) as a single function call like; 3)spi.WriteRead({Status_Resgiter_Read_Command, dummy(0)}, {Status_Resgiter_Read_Command, dummy(0)} ) Status register value is stored in second element (dummy) of command packet. That's say I haven't understand well about usage of NETMF's SPI object. Thank you.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.