/*---------------------------------------------------------------------------------------------------------*/
/* */
/* Copyright(c) 2015 Nuvoton Technology Corp. All rights reserved. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
//***********************************************************************************************************
// Nuvoton Technology Corp.
// E-mail: MicroC-8bit@nuvoton.com
//***********************************************************************************************************
// Application: LDROM program Data Flash Function
// ISP_LD.bin need to program to LD
// Config0 bit7(CBS) need to select LDROM
//
// Output : Test OK => P0 = 0
// Erase Data Verify err => P0 = ~0xA1
// Program Data Verify err => P0 = ~0xA2
//
// Because ICE bridge can't update LDROM, Please using ICP/HW writer to update LDROM.
//***********************************************************************************************************
//------------------------- <<< 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;
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 Erase_CONFIG(void)
{
BIT EA_TMP;
EA_TMP = EA;
EA = 0;
Enable_ISP_Mode();
P0 = ~0x05;
ISPCN = ERASE_ALL_CONFIG;
ISPAL = 0x00;
ISPAH = 0x00;
Trigger_ISP();
Disable_ISP_Mode();
EA = EA_TMP;
}
//-----------------------------------------------------------------------------------------------------------
void Program_CONFIG(UINT8 u8CONFIG_NUMBER,UINT8 u8Value)
{
BIT EA_TMP;
EA_TMP = EA;
EA = 0;
Enable_ISP_Mode();
P0 = ~0x06;
ISPAL = u8CONFIG_NUMBER;
ISPAH = 0x00;
ISPFD = u8Value;
ISPCN = BYTE_PROGRAM_CONFIG;
Trigger_ISP();
Disable_ISP_Mode();
EA = EA_TMP;
}
//-----------------------------------------------------------------------------------------------------------
UINT8 Read_CONFIG(UINT8 u8CONFIG_NUMBER)
{
UINT8 u8Read_Data;
Enable_ISP_Mode();
P0 = ~0x07;
ISPAL = u8CONFIG_NUMBER;
ISPAH = 0x00;
u8Read_Data = 0x00;
ISPCN = FLASH_READ_CONFIG;
Trigger_ISP();
u8Read_Data = ISPFD;
Disable_ISP_Mode();
return u8Read_Data;
}
//-----------------------------------------------------------------------------------------------------------
void main (void)
{
Clear_BOF();
InitialUART0_Timer1(9600); // 9600 Baud Rate [url=home.php?mod=space&uid=72445]@[/url] 11.0592MHz
printf ("\n N79E715 ISP LDROM Program Data Flash Sample Code.");
Erase_DATA();
Erase_DATA_Verify();
Program_DATA();
Program_DATA_Verify();
printf ("\nN79E715 ISP LDROM Program Data Flash OK!\n");
P14 = 0;
P21 = 0;
while(1);
}
//-----------------------------------------------------------------------------------------------------------
|