/**********************************************************************/
/* Project Name:  main.c                                              */
/* Last modified: Mar/10/2011                                         */
/* By:            r62780                                              */
/**********************************************************************/
/* Description: MC9SC128 flash - demo                                 */
/* This software is classified as Engineering Sample Software.        */
/**********************************************************************
- This sample software was tested on C128 project board
- The board includes OSCCLK=16 MHz external crystal
- MCU bus frequency is OSCCLK/2=8MHz 
  (No PLL)
- Project was opened for C128 and tested on mask set 2L09S
- the sample software demonstrates FLASH programming as a part of 
  application
- the sample application writes a few words into FLASH locations


Flash is used in following way (see PRM file):
Data pages: 38,39,3D and 3E ~ (4000~7FFF);
Code pages: 3A,3B,3C and 3F ~ (C000~FFFF)
***********************************************************************/ 
#include <hidef.h>      /* common defines and macros */
#include "MC9S12C128.h"     /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12c128"


#include "map.h"        /* derivative information */
#include "flash.h"
//==============================================================================
// common definitions
//==============================================================================
#define     UBYTE       unsigned char
#define     SBYTE                char
#define     UWORD       unsigned int
#define     ULONG       unsigned long int
//==============================================================================
#pragma CODE_SEG DEFAULT
//==============================================================================
void delay(void);
//==============================================================================
unsigned int  *far pui;         // pointer to external memory
unsigned char *far puc;         // pointer to external memory
unsigned int  uix,uiy;
unsigned char m;
char     ucx;
unsigned long i,j,k;
unsigned long addr;
volatile unsigned long check;

unsigned int mytext[]=          // !!!! near address to ram 
{
 0x0123,0x1234,0x2345,0x3456,0x4567,0x5678,0x6789
};
unsigned int mytext1[]=          // !!!! near address to ram 
{
 0x1111,0x2222,0x3333,0x4444,0x5555,0x6666,0x7777
};

const unsigned int xvar[] @0x3E8000= {  0x11AA,0x22BB,0x33CC,0x44DD }; // the same as address $4000
//==============================================================================
// main
//==============================================================================
void main(void) 
{
  DDRB = 0xFF;                            // an output port
  //----------------------------------------
  // initialization of COP
  //----------------------------------------
  COPCTL = 0x00;     // disable the COP
  //----------------------------------------
  // initialization of FLASH module
  //----------------------------------------
  
  // you can use this function
  // ucx = Flash_Init(16000,125);         // Oscillator clock is 16000kHz, bus clock is 8MHz => tbus=125ns
  // ucx = Flash_Init(16000,125);         // Oscillator clock is 16000 kHz, bus clock is 8MHz => tbus=125ns
  // ucx = Flash_Init(8000,250);          // Oscillator clock is 8000 kHz, bus clock is 4MHz => tbus=250ns
  // ucx = Flash_Init(5000,125);          // Oscillator clock is 5000 kHz, bus clock is 8MHz => tbus=125ns
  // ucx = Flash_Init(4000,500);          // Oscillator clock is 4000 kHz, bus clock is 2MHz => tbus=500ns
  // or this function
  FCLKDIV = 74;
  
  (void)Flash_CatchError(ucx,1);         // function used only for debugging purposes

  //----------------------------------------
  // erase one sector of the FLASH example
  //----------------------------------------
  ucx = Flash_Erase_Sector(PG_38_SEC_00);
  (void)Flash_CatchError(ucx,2);
  ucx = Flash_Erase_Sector(PG_3E_SEC_00);        // the same as (unsigned int *far)0x4000
  (void)Flash_CatchError(ucx,3);
  ucx = Flash_Erase_Sector(0x4400);              // the same as PG_3E_SEC_01 or 0x3E8400
  (void)Flash_CatchError(ucx,4);
  ucx = Flash_Erase_Sector(pg_sec(0x3E,2));      // the same as PG_3E_SEC_02 or 0x3E8800
  (void)Flash_CatchError(ucx,5);
  //----------------------------------------
  // write/read one word into/from the FLASH example
  //----------------------------------------
  ucx = Flash_Write_Word(0x388000,0x0123); 
  (void)Flash_CatchError(ucx,6);
  pui = ((unsigned int *far)(0x388000)); uix=*pui;

  ucx = Flash_Write_Word(0x388002,0x2345);
  (void)Flash_CatchError(ucx,7);
  pui = ((unsigned int *far)(0x388002)); uix=*pui;

  //........................................

  ucx = Flash_Write_Word(0x4000,0x1122);
  (void)Flash_CatchError(ucx,8);
  pui = ((unsigned int *far)(0x4000)); uix=*pui;

  ucx = Flash_Write_Word(0x4002,0x3344);
  (void)Flash_CatchError(ucx,9);
  pui = ((unsigned int *far)(0x4002)); uix=*pui;

  ucx = Flash_Erase_Sector(0x4000);              // the same as PG_3E_SEC_00 or 0x3E8000
  (void)Flash_CatchError(ucx,10);

  ucx = Flash_Write_Word(0x4000,0x5566);
  (void)Flash_CatchError(ucx,11);
  pui = ((unsigned int *far)(0x4000)); uix=*pui;

  ucx = Flash_Write_Word(0x4002,0x7788);
  (void)Flash_CatchError(ucx,12);
  pui = ((unsigned int *far)(0x4002)); uix=*pui;
  //........................................
  ucx = Flash_Erase_Sector(0x3E8000);
  (void)Flash_CatchError(ucx,13);
  //........................................
  ucx = Flash_Write_Block((ULONG) (&mytext), 0x3E8000UL, 5);
  (void)Flash_CatchError(ucx,14);
  //........................................
  ucx = Flash_Erase_Sector(0x3E8000);
  (void)Flash_CatchError(ucx,15);
  //........................................
  ucx = Flash_Write_Block((ULONG) (&mytext1), 0x3E8000UL, 5);
  (void)Flash_CatchError(ucx,16);
  //----------------------------------------
	//erase flash pages 38, 39, 3D, 3E(paged access) example
  //----------------------------------------
  
  for(addr=0x388000L;addr<0x38BFFFL;addr+=0x400) // erase entire page
   { ucx = Flash_Erase_Sector(addr); 
     (void)Flash_CatchError(ucx,17);
   }
  for(addr=0x398000L;addr<0x39BFFFL;addr+=0x400) // erase entire page
   { ucx = Flash_Erase_Sector(addr); 
     (void)Flash_CatchError(ucx,18);
   }
  for(addr=0x3D8000L;addr<0x3DBFFFL;addr+=0x400) // erase entire page
   { ucx = Flash_Erase_Sector(addr); 
     (void)Flash_CatchError(ucx,19);
   }
  for(addr=0x3E8000L;addr<0x3EBFFFL;addr+=0x400) // erase entire page
   { ucx = Flash_Erase_Sector(addr); 
     (void)Flash_CatchError(ucx,20);
   }
  //----------------------------------------
	//write flash flash pages 38, 39, 3D, 3E(paged access) example
  //----------------------------------------
  uix=0x1111;
  for(addr=0x388000L;addr<0x38BFFFL;addr+=0x02)        
   {	ucx = Flash_Write_Word(addr,uix++); 
     (void)Flash_CatchError(ucx,21);
   }
  uix=0x2222;
  for(addr=0x398000L;addr<0x39BFFFL;addr+=0x02)        
   {	ucx = Flash_Write_Word(addr,uix++); 
     (void)Flash_CatchError(ucx,22);
   }
  uix=0x3333;
  for(addr=0x3D8000L;addr<0x3DBFFFL;addr+=0x02)        
   {	ucx = Flash_Write_Word(addr,uix++); 
     (void)Flash_CatchError(ucx,23);
   }
  uix=0x4444;
  for(addr=0x3E8000L;addr<0x3EBFFFL;addr+=0x02)        
   {	ucx = Flash_Write_Word(addr,uix++); 
     (void)Flash_CatchError(ucx,24);
   }
  //----------------------------------------
	// page 3E can be accessed also as nonpaged space 4000~7FFF
  //----------------------------------------
  for(addr=0x4000;addr<0x7FFF;addr+=0x400)       // erase entire page
   { ucx = Flash_Erase_Sector(addr); 
     (void)Flash_CatchError(ucx,25);
   }
  uix=0x5555;
  for(addr=0x4000;addr<0x7FFF;addr+=0x02)        // fill entire page
    {	ucx = Flash_Write_Word(addr,uix++); 
     (void)Flash_CatchError(ucx,26);
    }
  //----------------------------------------
	//read flash  from 4000 to 7FFF example - check written data
  //----------------------------------------

  uiy = 0x5555;
  for(addr=0x4000;addr<0x7FFF;addr+=0x02)        // go through page 3E
    {
      uix = *((unsigned int *far)(addr));        // read data from memory
      if(uix != uiy)                             // check writen data compatibility
       {
        // data is wrong - do something....
        PORTB = ~uiy;
        delay();               
       }
      else
			 {
        // data is correCt - do something....
			 }
      uiy ++;
    }

  //----------------------------------------
	//erase flash pages 38, 39, 3D, 3E(paged access) example
  //----------------------------------------
  
  for(addr=0x388000L;addr<0x38BFFFL;addr+=0x400) // erase entire page
   { ucx = Flash_Erase_Sector(addr); 
     (void)Flash_CatchError(ucx,27);
   }
  for(addr=0x398000L;addr<0x39BFFFL;addr+=0x400) // erase entire page
   { ucx = Flash_Erase_Sector(addr); 
     (void)Flash_CatchError(ucx,28);
   }
  for(addr=0x3D8000L;addr<0x3DBFFFL;addr+=0x400) // erase entire page
   { ucx = Flash_Erase_Sector(addr); 
     (void)Flash_CatchError(ucx,29);
   }
  for(addr=0x3E8000L;addr<0x3EBFFFL;addr+=0x400) // erase entire page
   { ucx = Flash_Erase_Sector(addr); 
     (void)Flash_CatchError(ucx,30);
   }

  //----------------------------------------

  for(;;) {} /* wait forever */
  /* please make sure that you never leave this function */
}
//==============================================================================
// void delay(void)
//==============================================================================
void delay(void)
{
  static unsigned int  a,b;
  for(a=0;a<0xFFFF;a++)
    {
      for(b=0;b<1;b++) asm nop;
    }
}
//==============================================================================
//******************************************************************************
//******************************************************************************
/* Services performed by FREESCALE in this matter are performed AS IS */
/* and without any warranty. CUSTOMER retains the final decision      */
/* relative to the total design and functionality of the end product. */ 
/* FREESCALE neither guarantees nor will be held liable by CUSTOMER   */
/* for the success of this project. FREESCALE DISCLAIMS ALL           */
/* WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY INCLUDING, BUT NOT     */
/* LIMITED TO, IMPLIED WARRANTY OF MERCHANTABILITY OR  FITNESS FOR A  */
/* PARTICULAR PURPOSE ON ANY HARDWARE, SOFTWARE ORE ADVISE SUPPLIED   */
/* TO THE PROJECT BY FREESCALE, AND OR NAY PRODUCT RESULTING FROM     */
/* FREESCALE SERVICES . IN NO EVENT SHALL FREESCALE BE LIABLE FOR     */
/* INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT. */
/*                                                                    */
/* CUSTOMER agrees to hold FREESCALE harmless against any and all     */
/* claims demands or actions by anyone on account of any damage, or   */
/* injury, whether commercial, contractual, or tortuous, rising       */
/* directly or indirectly as a result of the advise or assistance     */
/* supplied CUSTOMER in connection with product, services or goods    */
/* supplied under this Agreement.                                     */
/**********************************************************************/
