/*---------------------------------------------------------------------------------------------------------*/
/* */
/* Copyright(c) 2015 Nuvoton Technology Corp. All rights reserved. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
//***********************************************************************************************************
// Nuvoton Technology Corp.
// E-mail: MicroC-8bit@nuvoton.com
//***********************************************************************************************************
// Application: APROM program Data Flash Function
// ISP_AP.bin need to program to APROM
// Config0 bit0(DFEN) =0 (enable DFEN)
// Config1(CHBDA)=0x30
// Output : Test OK => P0 = 0
// Erase Data Verify err => P0 = ~0xA1
// Program Data Verify err => P0 = ~0xA2
//***********************************************************************************************************
//------------------------- <<< Use Configuration Wizard in Context Menu >>> --------------------------------
// <h> UART pin Select
// <o0.6> UART pin
// <0=> Select P1.0, P1.1 as UART pin(default)
// <1=> Select P2.6, P2.7 as UART pin(28 pin only)
// </h>
//-------------------------------- <<< end of configuration section >>> -------------------------------------
#define Uart_Port_Sel 0x00
#include "N79E715.h"
#include "Typedef.h"
#include "Define.h"
#include "Common.h"
#include "Delay.h"
#include "Version.h"
#include <intrins.h>
#include <stdio.h>
#include <string.h>
#define ERASE_ALL_DATA 0x22
#define FLASH_READ_DATA 0x00
#define BYTE_PROGRAM_DATA 0x21
#define ERASE_ALL_CONFIG 0xE2
#define FLASH_READ_CONFIG 0xC0
#define BYTE_PROGRAM_CONFIG 0xE1
bit EA_Save_bit;
//-----------------------------------------------------------------------------------------------------------
void Clear_BOF(void)
{
EA_Save_bit = EA;
EA = 0;
TA = 0xAA;
TA = 0x55;
PMCR &= CLR_BIT3;
EA = EA_Save_bit;
}
//-----------------------------------------------------------------------------------------------------------
void Enable_ISP_Mode(void)
{
/* Enable ISP */
EA_Save_bit = EA;
EA = 0;
TA = 0xAA;
TA = 0x55;
CHPCON |= 0x01;
EA = EA_Save_bit;
}
//-----------------------------------------------------------------------------------------------------------
void Disable_ISP_Mode(void)
{
/* Disable ISP */
EA_Save_bit = EA;
EA = 0;
TA = 0xAA;
TA = 0x55;
CHPCON &= 0xFE;
EA = EA_Save_bit;
}
//-----------------------------------------------------------------------------------------------------------
void Trigger_ISP(void)
{
EA_Save_bit = EA;
EA = 0;
EA = 0;
TA = 0xAA;
TA = 0x55;
ISPTRG |= 0x01;
EA = EA_Save_bit;
}
//-----------------------------------------------------------------------------------------------------------
void Erase_DATA(void)
{
UINT16 u16Count;
Enable_ISP_Mode();
P0 = ~0x01;
ISPCN = ERASE_ALL_DATA;
for(u16Count=96;u16Count<128;u16Count++)
{
ISPAL = LOBYTE(u16Count*128); // 96*128=12288=0x3000
ISPAH = HIBYTE(u16Count*128); // 96*128=12288=0x3000
Trigger_ISP();
}
Disable_ISP_Mode();
}
//-----------------------------------------------------------------------------------------------------------
void Erase_DATA_Verify(void)
{
UINT16 u16Count;
Enable_ISP_Mode();
P0 = ~0x02;
ISPAL = 0x00;
ISPAH = 0x30;
ISPCN = FLASH_READ_DATA;
for(u16Count=12288;u16Count<16384;u16Count++)
{
ISPFD = 0;
Trigger_ISP();
if(ISPFD != 0xFF)
{
printf ("\nEV err.");
P0 = ~0xA1;
while(1);
}
ISPAL++;
if(ISPAL == 0)
{
ISPAH++;
}
}
Disable_ISP_Mode();
}
//-----------------------------------------------------------------------------------------------------------
void Program_DATA(void)
{
UINT16 u16Count;
Enable_ISP_Mode();
P0 = ~0x03;
ISPAL = 0x00;
ISPAH = 0x30;
ISPFD = 0xFF;
ISPCN = BYTE_PROGRAM_DATA;
for(u16Count=12288;u16Count<16384;u16Count++)
{
ISPFD++;
Trigger_ISP();
ISPAL++;
if(ISPAL == 0)
{
ISPAH++;
}
}
Disable_ISP_Mode();
}
//-----------------------------------------------------------------------------------------------------------
void Program_DATA_Verify(void)
{
UINT16 u16Count;
UINT8 u8Read_Data;
Enable_ISP_Mode();
P0 = ~0x04;
ISPAL = 0x00;
ISPAH = 0x30;
u8Read_Data = 0x00;
ISPCN = FLASH_READ_DATA;
for(u16Count=12288;u16Count<16384;u16Count++)
{
Trigger_ISP();
if(ISPFD != u8Read_Data)
{
printf ("\nPV err.");
P0 = ~0xA2;
while(1);
}
ISPAL++;
if(ISPAL == 0)
{
ISPAH++;
}
u8Read_Data++;
}
Disable_ISP_Mode();
}
//-----------------------------------------------------------------------------------------------------------
void main (void)
{
Clear_BOF();
AUXR1 |= Uart_Port_Sel; // Select P10/P11 as UART pin(default)
InitialUART0_Timer1(9600); // 9600 Baud Rate [url=home.php?mod=space&uid=72445]@[/url] 11.0592MHz
Show_Version_Number_To_PC();
printf ("\n N79E84x ISP APROM Program Data Flash Sample Code.");
Erase_DATA();
Erase_DATA_Verify();
Program_DATA();
Program_DATA_Verify();
printf ("\n N79E715 ISP APROM Program Data Flash OK!\n");
P14 = 0;
P21 = 0;
while(1);
}
//-----------------------------------------------------------------------------------------------------------
|