This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Writing a new programming algorithm

Hi to all.
I'm working on a LPC1788 project. I need to connect MCU to an external NOR flash (S29GL064) and allocate some const variables on it.
I wrote a new programming algorithm :

/***********************************************************************/
/*  This file is part of the ARM Toolchain package                     */
/*  Copyright KEIL ELEKTRONIK GmbH 2003 - 2011                         */
/***********************************************************************/
/*                                                                     */
/*  FlashDev.C:  Flash Programming Functions adapted                   */
/*               for Dual M29W640FBx(32-bit Bus)                       */
/*                                                                     */
/***********************************************************************/

#include "..\FlashOS.H"        // FlashOS Structures

#define M8(adr)  (*((volatile unsigned char  *) (adr)))
#define M16(adr) (*((volatile unsigned short *) (adr)))
#define M32(adr) (*((volatile unsigned long  *) (adr)))

#define STACK_SIZE   64        // Stack Size


union fsreg {                  // Flash Status Register
  struct b  {
    unsigned int q0:1;
    unsigned int q1:1;
    unsigned int q2:1;
    unsigned int q3:1;
    unsigned int q4:1;
    unsigned int q5:1;
    unsigned int q6:1;
    unsigned int q7:1;
  } b;
  unsigned int v;
} fsr;

unsigned long base_adr;

/*
 * Check if Program/Erase completed
 *    Parameter:      adr:  Block Start Address
 *    Return Value:   0 - OK,  1 - Failed
 */

int Polling (unsigned long adr) {
  unsigned int q6;

  fsr.v = M16(adr);
  q6 = fsr.b.q6;
  do {
    fsr.v = M16(adr);
    if (fsr.b.q6 == q6) return (0);  // Done
    q6 = fsr.b.q6;
  } while (fsr.b.q5 == 0);           // Check for Timeout
  fsr.v = M16(adr);
  q6 = fsr.b.q6;
  fsr.v = M16(adr);
  if (fsr.b.q6 == q6) return (0);    // Done
  M16(adr) = 0xF0;                   // Reset Device
  return (1);                        // Failed
}


/*
 *  Initialize Flash Programming Functions
 *    Parameter:      adr:  Device Base Address
 *                    clk:  Clock Frequency (Hz)
 *                    fnc:  Function Code (1 - Erase, 2 - Program, 3 - Verify)
 *    Return Value:   0 - OK,  1 - Failed
 */

int Init (unsigned long adr, unsigned long clk, unsigned long fnc) {
  base_adr = adr;

  return (0);
}


/*
 *  De-Initialize Flash Programming Functions
 *    Parameter:      fnc:  Function Code (1 - Erase, 2 - Program, 3 - Verify)
 *    Return Value:   0 - OK,  1 - Failed
 */

int UnInit (unsigned long fnc) {
  return (0);
}


/*
 *  Erase complete Flash Memory
 *    Return Value:   0 - OK,  1 - Failed
 */

int EraseChip (void) {

  // Start Chip Erase Command
  M16(base_adr + (0x0555 << 1)) = 0x00AA;
  M16(base_adr + (0x02AA << 1)) = 0x0055;
  M16(base_adr + (0x0555 << 1)) = 0x0080;
  M16(base_adr + (0x0555 << 1)) = 0x00AA;
  M16(base_adr + (0x02AA << 1)) = 0x0055;
        M16(base_adr + (0x0555 << 1)) = 0x0010;

  return (Polling(base_adr));  // Wait until Erase completed
}


/*
 *  Erase Sector in Flash Memory
 *    Parameter:      adr:  Sector Address
 *    Return Value:   0 - OK,  1 - Failed
 */

int EraseSector (unsigned long adr) {

  // Start Erase Sector Command
  M16(base_adr + (0x0555 << 1)) = 0x00AA;
  M16(base_adr + (0x02AA << 1)) = 0x0055;
  M16(base_adr + (0x0555 << 1)) = 0x0080;
  M16(base_adr + (0x0555 << 1)) = 0x00AA;
  M16(base_adr + (0x02AA << 1)) = 0x0055;
        M16(adr) = 0x30;

  do {
    fsr.v = M16(adr);
  } while (fsr.b.q3 == 0);     // Wait for Sector Erase Timeout

  return (Polling(adr));       // Wait until Erase completed
}


/*
 *  Program Page in Flash Memory
 *    Parameter:      adr:  Page Start Address
 *                    sz:   Page Size
 *                    buf:  Page Data
 *    Return Value:   0 - OK,  1 - Failed
 */

int ProgramPage (unsigned long adr, unsigned long sz, unsigned char *buf) {
  int  i;

  for (i = 0; i < ((sz+3)/4); i++)  {
    // Start Program Command
    M16(base_adr + (0x555 << 1)) = 0x00AA;
    M16(base_adr + (0x2AA << 1)) = 0x0055;
    M16(base_adr + (0x555 << 1)) = 0x00A0;
    M16(adr) = *((unsigned short *) buf);
    if (Polling(adr)) return (1);  // Wait until Programming completed
    buf += 2;
    adr += 2;
  }
  return (0);                      // Done
}

and :

/***********************************************************************/
/*  This file is part of the ARM Toolchain package                     */
/*  Copyright KEIL ELEKTRONIK GmbH 2003 - 2011                         */
/***********************************************************************/
/*                                                                     */
/*  FlashDev.C:  Device Description for Dual S29GL064N (32-bit Bus)    */
/*                                                                     */
/***********************************************************************/


#include "..\FlashOS.H" // FlashOS Structures
struct FlashDevice const FlashDevice = { FLASH_DRV_VERS, // Driver Version, do not modify! "S29GL064 Flash HY-LPC1788 (By hamid reza mehrabian)", // Device Name EXT16BIT, // Device Type 0x90000000, // Device Start Address 0x00800000, // Device Size in Bytes (8MB) 1024, // Programming Page Size 0, // Reserved, must be 0 0xFF, // Initial Content of Erased Memory 150, // Program Page Timeout 150 mSec 4000, // Erase Sector Timeout 4000 mSec // Specify Size and Address of Sectors 0x10000, 0x00000, // Uniform Sector Size 64kB SECTOR_END };

but at programming after erase and programming , verifying shows error.
I think that i need to Active EMC unit in the MCU to make a external memory connection.
Can i do it (Init EMC) in the "init function" of the programming algorithm code???
Or there is another way???

0