/******************************************************************************/
/* Project Name:  main.c                                                      */
/* Last modified: Nov/14/2008                                                 */
/* By:            r62780                                                      */
/******************************************************************************/
/* Description: MC9S12C32 - demo                                              */
/* This software is classified as Engineering Sample Software.                */
/******************************************************************************
- this sample software was tested on MC68EVB9S12C32 evaluation board
- the board includes 4 MHz oscillator - MCU bus frequency used is 2 MHz (No PLL)
  (No PLL)
- 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 memory: 
            - 4000~4FFF            (page 3E ~ (8000~8FFF);)
            - C000~CFFF            (page 3F ~ (8000~8FFF);)
                          
Code pages: 
            - 5000~7FFF            (page 3E ~ (9000~BFFF);)
            - D000~FFFF            (page 3F ~ (9000~BFFF);)
*******************************************************************************/ 
#include <hidef.h>      /* common defines and macros */
#pragma LINK_INFO DERIVATIVE "mc9s12c32"

#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 pfui;         // pointer to paged memory
unsigned int      *pui;          // pointer to unpaged 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[] @0x3E8800= {  0x11AA,0x22BB,0x33CC,0x44DD }; // the same as address $4800
//==============================================================================
// main
//==============================================================================
void main(void) 
{



  DDRB = 0xFF;
  //----------------------------------------
  // initialization of COP
  //----------------------------------------
  COPCTL = 0x00;     // disable the COP. Be sure the COP is refreshed before flash EW
                     // if the COP is enabled
  //----------------------------------------
  // initialization of FLASH module
  //----------------------------------------
//  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
  (void)Flash_CatchError(ucx,1);
// or you can use direct number FCLKDIV = 74;
// or you can use direct number FCLKDIV = 41;
// or you can use direct number FCLKDIV = 26;
// or you can use direct number FCLKDIV = 21;
  //----------------------------------------
  // erase one sector of the FLASH example
  //----------------------------------------
  ucx = Flash_Erase_Sector(PG_3E_SEC_00);        // the same as (unsigned int *far)0x4000
  (void)Flash_CatchError(ucx,2);
  ucx = Flash_Erase_Sector(PG_3F_SEC_00);        // the same as (unsigned int *far)0xC000
  (void)Flash_CatchError(ucx,3);
  ucx = Flash_Erase_Sector(0x4200);              // the same as PG_3E_SEC_01 or 0x3E8200
  (void)Flash_CatchError(ucx,4);
  ucx = Flash_Erase_Sector(0xC200);              // the same as PG_3F_SEC_01 or 0x3F8200
  (void)Flash_CatchError(ucx,5);
  //----------------------------------------
  // write/read one word into/from the FLASH example
  //----------------------------------------
  ucx = Flash_Write_Word(0x3E8000,0x0123);       // the same address as 0x4000
  (void)Flash_CatchError(ucx,6);
  pfui = ((unsigned int *far)(0x3E8000)); uix=*pfui;

  ucx = Flash_Write_Word(0x3E8002,0x2345);       // the same address as 0x4002   
  (void)Flash_CatchError(ucx,7);
  pfui = ((unsigned int *far)(0x3E8002)); uix=*pfui;

  ucx = Flash_Write_Word(0x4004,0x6789);         // the same address as 0x3E8004
  (void)Flash_CatchError(ucx,8);
  pui = (unsigned int*)(0x4004); uix=*pui;

  ucx = Flash_Write_Word(0x4006,0xABCD);         // the same address as 0x3E8006 
  (void)Flash_CatchError(ucx,9);
  pui = (unsigned int*)(0x4006); uix=*pui;
  //........................................
  ucx = Flash_Erase_Sector(0x3E8000);
  (void)Flash_CatchError(ucx,10);
  //........................................
  ucx = Flash_Write_Block((ULONG) (&mytext), 0x3E8000UL, 5);
  (void)Flash_CatchError(ucx,11);
  //........................................
  ucx = Flash_Erase_Sector(0x3E8000);
  (void)Flash_CatchError(ucx,12);
  //........................................
  ucx = Flash_Write_Block((ULONG) (&mytext1), 0x3E8000UL, 5);
  (void)Flash_CatchError(ucx,13);
  //----------------------------------------
	//erase data flash space - paged access example
  // the same as for(addr=0x4000;addr<0x5000;addr+=0x200)
  //----------------------------------------
  for(addr=0x3E8000L;addr<0x3E9000L;addr+=0x200) // erase entire page
   { ucx = Flash_Erase_Sector(addr); 
     (void)Flash_CatchError(ucx,14);
   }
  //----------------------------------------
	//erase data flash space - unpaged access example
	// the same as for(addr=0x3F8000L;addr<0x3F9000L;addr+=0x200)
  //----------------------------------------
  for(addr=0xC000;addr<0xD000;addr+=0x200)       // erase entire page
   { ucx = Flash_Erase_Sector(addr); 
     (void)Flash_CatchError(ucx,15);
   }
  //----------------------------------------

  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.                                     */
/**********************************************************************/
