Main Site Documentation

Problems erasing sectors in GHI Flash Module


#1

Maybe I am missing something obvious here but I can only erase and rewrite to sectors 0 to 31. From sector 32 I can only write once after a complete chip erase.

Any idea what I am doing wrong?

I wrote a little test program to recreate the problem:


using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

namespace FlashTest
{
    public partial class Program
    {
        void ProgramStarted()
        {
            //flash.EraseChip();

            for (int i = 30; i < 34; i++)
            {
                TestSector(i);
            }
        }

        void TestSector(int sector)
        {
            bool resultOK = true;
            int sectorSize = 4 * 1024;
            byte[] b;

            // erase sector
            flash.EraseSector(sector, 1);

            // test whether all bytes in sector equal 255
            b = flash.ReadData(sector * sectorSize, sectorSize);
            for (int i = 0; i < sectorSize; i++)
            {
                if (b[i] != 255)
                {
                    Debug.Print("ERROR: not all bytes equal 255 in sector " + sector);
                    resultOK = false;
                    break;
                }
            }

            // write to sector
            // set all bytes in sector to 111
            b = new byte[sectorSize];
            for (int i = 0; i < sectorSize; i++)
            {
                b[i] = 111;
            }
            flash.WriteData(sector * sectorSize, b);

            // test whether all bytes in sector are 111
            b = flash.ReadData(sector * sectorSize, sectorSize);
            for (int i = 0; i < sectorSize; i++)
            {
                if (b[i] != 111)
                {
                    Debug.Print("ERROR: not all bytes equal 111 in sector " + sector);
                    resultOK = false;
                    break;
                }
            }

            if (resultOK)
            {
                Debug.Print("Sector " + sector + " OK");
            }
            else 
            {
                Debug.Print("Sector " + sector + " ERROR");
            }
        }
    }
}

Test results from first run after chip erase:

Sector 30 OK
Sector 31 OK
Sector 32 OK
Sector 33 OK

Test results from second run after chip erase:

Sector 30 OK
Sector 31 OK
ERROR: not all bytes equal 255 in sector 32
Sector 32 ERROR
ERROR: not all bytes equal 255 in sector 33
Sector 33 ERROR


#2

@ goldsmiths_irs - The flash module has two sector granularities. All sectors are 64K in size, however the first two sectors are subdivided into 4K sub-sectors.

Since erasing of flash memory is performed at the sector level, the Flash module can be erased in 4K blocks for the first two sectors which have 16 4K sub-sectors each ie. sectors 0…31 in your code. All subsequent sectors are 64K only and therefore have to be erased with full 64K at a time. So when you try to erase sector 32 and 33 using 4K it fails and does nothing.

The following code is modified to use 64K sectors and call the EraseBlock function which erases full 64K sectors.


using Microsoft.SPOT;

namespace FlashModuleTest
{
    public partial class Program
    {
        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            //flash.EraseChip();

            for (int i = 30; i < 34; i++)
            {
                TestSector(i);
            }
        }

        void TestSector(int sector)
        {
            bool resultOK = true;
            int sectorSize = 64 * 1024;
            byte[] b;

            // erase sector
            flash.EraseBlock(sector, 1);

            // test whether all bytes in sector equal 255
            b = flash.ReadData(sector * sectorSize, sectorSize);
            for (int i = 0; i < sectorSize; i++)
            {
                if (b[i] != 255)
                {
                    Debug.Print("ERROR: not all bytes equal 255 in sector " + sector);
                    resultOK = false;
                    break;
                }
            }

            // write to sector
            // set all bytes in sector to 111
            b = new byte[sectorSize];
            for (int i = 0; i < sectorSize; i++)
            {
                b[i] = 111;
            }
            flash.WriteData(sector * sectorSize, b);

            // test whether all bytes in sector are 111
            b = flash.ReadData(sector * sectorSize, sectorSize);
            for (int i = 0; i < sectorSize; i++)
            {
                if (b[i] != 111)
                {
                    Debug.Print("ERROR: not all bytes equal 111 in sector " + sector);
                    resultOK = false;
                    break;
                }
            }

            if (resultOK)
            {
                Debug.Print("Sector " + sector + " OK");
            }
            else
            {
                Debug.Print("Sector " + sector + " ERROR");
            }
        }

    }
}



#3

@ taylorza - Thanks for comprehensive answer.

I googled for some datasheets based on what I could read on the physical IC and found this:
http://www.spansion.com/Support/Datasheets/S25FL032P_00.pdf

Is this where you got your detailed information about the flash IC from?

The GHI guide (https://www.ghielectronics.com/docs/71/flash-module) only states this:

The second part of this sentence somehow doesn’t make sense to me.


#4

@ goldsmiths_irs - It is a pleasure. When I originally wrote the Flash File System for the module I used the data sheet as a reference. I just checked, the one you found is the same one I have downloaded.

It has been a while since I worked with this so I will need to recheck against the data sheet, but I do not think that the GHI docs are correct on this point.