📄 flashphytecpcm023.c
字号:
/*
File: FlashPhytecPCM023.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <iolpc2294.h>
#ifdef ERASE_ONLY
#define ERASE_CHIP
#endif
#define FLASH_BASE 0x80000000
#ifndef DATAWIDTH
#define DATAWIDTH 16
#endif
#ifndef AM29LV160DT
#ifndef AM29LV160DB
#ifndef AM29DL800BT
#ifndef AM29DL800BB
#define AM29DL800BT
#endif
#endif
#endif
#endif
#if(DATAWIDTH == 8)
#define BCFG0_value 0x00003CE3
#define BCFG1_value 0x00003CE3
#define BCFG2_value 0x00003CE3
#define BCFG3_value 0x00003CE3
#define PINSEL2_value 0x0F814904 // PINSEL2: CS0, CS1, CS2, CS3, OE, WE, BLS0, D0..7, A0..23, JTAG
#else
#if(DATAWIDTH == 16)
#define BCFG0_value 0x10003CE3
#define BCFG1_value 0x10003CE3
#define BCFG2_value 0x10003CE3
#define BCFG3_value 0x10003CE3
#define PINSEL2_value 0x0F814914 // PINSEL2: CS0, CS1, CS2, CS3, OE, WE, BLS0..1, D0..15, A0..23, JTAG
#else
#define BCFG0_value 0x20003CE3
#define BCFG1_value 0x20003CE3
#define BCFG2_value 0x20003CE3
#define BCFG3_value 0x20003CE3
#define PINSEL2_value 0x0E6149E4 // PINSEL2: CS0, CS1, CS2, OE, WE, BLS0..3, D0..31, A2..23, JTAG
#endif
#endif
#ifdef AM29LV160DT
#include "AM29LV160DT.h"
#endif
#ifdef AM29LV160DB
#include "AM29LV160DB.h"
#endif
#ifdef AM29DL800BT
#include "AM29DL800BT.h"
#endif
#ifdef AM29DL800BB
#include "AM29DL800BB.h"
#endif
#include "Interface.h" // The flash loader framework API declarations.
#if (DATAWIDTH == 8)
#define FLASHWORD unsigned char
#else
#if (DATAWIDTH == 16)
#define FLASHWORD unsigned short
#else
#define FLASHWORD unsigned long
#endif
#endif
#define __FLASHWORD__ (volatile FLASHWORD *)
#if(DATAWIDTH == 8)
#define FLASH_CMD_ADDR1 (0xaaa) /* command address 1 */
#define FLASH_CMD_ADDR2 (0x555) /* command address 2 */
#else
#define FLASH_CMD_ADDR1 (0x555 << (DATAWIDTH/16)) /* command address 1 */
#define FLASH_CMD_ADDR2 (0x2aa << (DATAWIDTH/16)) /* command address 2 */
#endif
#define FLASH_CMD_1 ((FLASHWORD)0x00aa00aa) /* command sequence 1 */
#define FLASH_CMD_2 ((FLASHWORD)0x00550055) /* command sequence 2 */
#define FLASH_ERASE_SECTOR ((FLASHWORD)0x00300030) /* erase a single sector */
#define FLASH_ERASE_CHIP ((FLASHWORD)0x00100010) /* erase chip */
#define FLASH_PREFIX ((FLASHWORD)0x00800080) /* erase prefix */
#define FLASH_PGM_SINGLE ((FLASHWORD)0x00a000a0) /* single pulse mode */
static void WaitForComplete(void)
{
unsigned int i;
do{
i = (FLASHWORD)0x00400040 & *(__FLASHWORD__ FLASH_BASE);
} while(i != ((FLASHWORD)0x00400040 & *(__FLASHWORD__ FLASH_BASE)));
do{
i = (FLASHWORD)0x00400040 & *(__FLASHWORD__ FLASH_BASE);
} while(i != ((FLASHWORD)0x00400040 & *(__FLASHWORD__ FLASH_BASE)));
}
#ifndef ERASE_CHIP
static void EraseSector( unsigned int addr )
{
*(__FLASHWORD__(addr + FLASH_CMD_ADDR1)) = FLASH_CMD_1;
*(__FLASHWORD__(addr + FLASH_CMD_ADDR2)) = FLASH_CMD_2;
*(__FLASHWORD__(addr + FLASH_CMD_ADDR1)) = FLASH_PREFIX;
*(__FLASHWORD__(addr + FLASH_CMD_ADDR1)) = FLASH_CMD_1;
*(__FLASHWORD__(addr + FLASH_CMD_ADDR2)) = FLASH_CMD_2;
*(__FLASHWORD__ addr) = FLASH_ERASE_SECTOR;
WaitForComplete();
}
#else
static void EraseChip( unsigned int addr )
{
*(__FLASHWORD__(addr + FLASH_CMD_ADDR1)) = FLASH_CMD_1;
*(__FLASHWORD__(addr + FLASH_CMD_ADDR2)) = FLASH_CMD_2;
*(__FLASHWORD__(addr + FLASH_CMD_ADDR1)) = FLASH_PREFIX;
*(__FLASHWORD__(addr + FLASH_CMD_ADDR1)) = FLASH_CMD_1;
*(__FLASHWORD__(addr + FLASH_CMD_ADDR2)) = FLASH_CMD_2;
*(__FLASHWORD__(addr + FLASH_CMD_ADDR1)) = FLASH_ERASE_CHIP;
WaitForComplete();
}
#endif
static void writeBuffer(unsigned long addr, FLASHWORD buffer)
{
int i;
#ifndef ERASE_CHIP
int j, k, m;
#endif
unsigned long temp;
int cumulative_sector_index;
unsigned long current_sector_type_base, current_sector_base;
int current_sector_type_index;
for(i = cumulative_sector_index = 0, current_sector_type_base = FLASH_BASE;
i < flash_sector_descriptor.type_num; i++)
{
if((addr < (temp = current_sector_type_base + flash_sector_descriptor.type[i].size * flash_sector_descriptor.type[i].num)) &&
(current_sector_type_base <= addr))
{
current_sector_type_index = (addr - current_sector_type_base) / flash_sector_descriptor.type[i].size;
current_sector_base = current_sector_type_base + current_sector_type_index * flash_sector_descriptor.type[i].size;
#ifndef ERASE_CHIP
/* If the sector to be written has not been erased, it'll be erased first */
j = cumulative_sector_index + current_sector_type_index;
if((flash_sector_descriptor.erasebits[k = j / (4 * sizeof(int))] & (m = 1 << (j % (4 * sizeof(int))))) == 0)
{
#if(DATAWIDTH > 16)
/* Both sectors at odd and even sector base addresses will be erased */
m = (m << 1) | m;
#endif
flash_sector_descriptor.erasebits[k] |= m;
EraseSector(current_sector_base);
}
#endif
*(__FLASHWORD__(current_sector_base + FLASH_CMD_ADDR1)) = FLASH_CMD_1;
*(__FLASHWORD__(current_sector_base + FLASH_CMD_ADDR2)) = FLASH_CMD_2;
*(__FLASHWORD__(current_sector_base + FLASH_CMD_ADDR1)) = FLASH_PGM_SINGLE;
*(__FLASHWORD__ addr) = buffer;
WaitForComplete();
return;
}
cumulative_sector_index += flash_sector_descriptor.type[i].num;
current_sector_type_base = temp;
}
}
void FlashWriteByte(unsigned long newaddr, int byte)
{
static FLASHWORD buffer = (FLASHWORD)0xFFFFFFFF;
static unsigned long oldaddr = 0;
unsigned long baseaddr;
int bitshift;
baseaddr = oldaddr - (oldaddr % (DATAWIDTH / 8));
if(byte != -1)
{
bitshift = 8 * (newaddr % (DATAWIDTH / 8));
if((baseaddr <= newaddr) && (newaddr < (baseaddr + (DATAWIDTH / 8))))
{
buffer = ((FLASHWORD)byte << bitshift) | (buffer & ~((FLASHWORD)0xFF << bitshift));
}else
{
writeBuffer(baseaddr, buffer);
baseaddr = newaddr - (newaddr % (DATAWIDTH / 8));
buffer = ((FLASHWORD)byte << bitshift) | (*(__FLASHWORD__ baseaddr) & ~((FLASHWORD)0xFF << bitshift));
}
oldaddr = newaddr;
}else /* flash buffer */
{
writeBuffer(baseaddr, buffer);
}
}
void FlashDriverInitialize(int argc, char const* argv[])
{
// Register the flash write function.
FlRegisterWriteFunction(FlashWriteByte);
BCFG0 = BCFG0_value;
BCFG1 = BCFG1_value;
BCFG2 = BCFG2_value;
BCFG3 = BCFG3_value;
PINSEL2 = PINSEL2_value;
#ifdef ERASE_CHIP
EraseChip(FLASH_BASE);
#endif
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -