⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flash.c

📁 C语言编写的监控中心终端程序。基于GPRS上传收发数据功能
💻 C
📖 第 1 页 / 共 3 页
字号:
#include "config.h"
#include "regmap.h"
#include "memmap.h"
#include "global.h"
#include "sio.h"
#include "gpio.h"
#include "func.h"
#include "util.h"
#include "osdfont.h"

#ifdef SUPPORT_FLASH

static inline void FLUSHWB(void)
{
    volatile UINT32	*p = (UINT32 *) 0x80000000;
    (void)p[0];
}

static inline void SET_WP_0(void)
{
    UINT32		x;

    x = regs0->gpio_out[0];
    x &= ~0x08;
    regs0->gpio_out[0] = x;
}

static inline void SET_WP_1(void)
{
    UINT32		x;

    x = regs0->gpio_out[0];
    x |= 0x08;
    regs0->gpio_out[0] = x;
}

int flash_read_ID(volatile BYTE * p)
{
    UINT8		id;
    int			i;

    *p = 0x90;
    for (i = 0; i < 2048; i++)
	asm volatile ("nop");
    id = *p;
    *p = 0xff;
    return id;
}

int flash_eraseblock(volatile BYTE * p)
{
    UINT8		status;
    int			i;

//  SET_WP_1();  FLUSHWB();
    *p = 0x20;		// FLUSHWB();
    *p = 0xd0;		// FLUSHWB();

//  wait finish
//  write 0x70
//  *p = 0x70;		// FLUSHWB();

    status = 0;
    while ((status & 0x80) == 0) {
	status = 0;
	status = *p;
    }

    for (i = 0; i < 2048; i++)
	asm volatile ("nop");

    *p = 0x50;		// clear status register
    *p = 0xFF;		// FLUSHWB();
    return 0;
}

int flash_prog(volatile BYTE * p, BYTE flash_write_data)
{
    UINT8		status;
    int			i;

//  WPB = 0
//  SET_WP_0(); FLUSHWB();

//  write 0x40
    *p = 0x40;		// FLUSHWB();
//  WPB = 1;
//  SET_WP_1(); FLUSHWB();

//  write data
    *p = flash_write_data;	// FLUSHWB();
//  wait finish
//  write 0x70
//  *p = 0x70;		// FLUSHWB();
    status = 0;
    while ((status & 0x80) == 0) {
	status = 0;
	status = *p;
    }

    for (i = 0; i < 2048; i++)
	asm volatile ("nop");

//  WPB = 0;
//  SET_WP_0();		FLUSHWB();
    *p = 0x50;		// clear status register
//  write 0xFF: read array
    *p = 0xFF;		// FLUSHWB();
//  regs0->stamp = *p;
    return 0;
}

int test_flash_access(void)
{
    UINT32		u;

//
//  force select following pins:
//  GPIOA[2]	MEMWE_B		GPIO_SEL_AUX[2]==0
//  GPIOA[3]	MEMWP_B		always
//  GPIOA[12]	MEMOE_B		GPIO_SEL_AUX[3]==0
//  GPIOA[13]	MEMCS1_B	GPIO_SEL_AUX[4]==0
//  GPIOA[14]	MEMCS2_B	GPIO_SEL_AUX[5]==0
//  GPIOA[15]	MEMCS3_B	GPIO_SEL_AUX[6]==0
//

/*
    u = regs0->gpio_sel_aux;
    u &= ~(0x007c);
    regs0->gpio_sel_aux = u;
*/

//
//  turn on output of MEMWP_B (GPIOA[3])
//
//  SET_WP_0();		// default 0
//  SET_WP_1();		// default 0

/*
    u = regs0->gpio_oe[0];
    u |= 0x0008;
    regs0->gpio_oe[0] = u;

    u = regs0->gpio_master[0];
    u |= 0x0008;
    regs0->gpio_master[0] = u;
*/

//
// also setup flash_1, flash_2 address
//

#define	FLASH1_BASE_64		(0x10)		// 0x80100000
#define	FLASH2_BASE_64		(0x20)		// 0x80200000
#define	ROM0_BASE_U		(0xa0000000)
#define	FLASH1_BASE_C		(0x80000000 | (FLASH1_BASE_64<<16))
#define	FLASH2_BASE_C		(0x80000000 | (FLASH2_BASE_64<<16))
#define	FLASH1_BASE_U		(0xbfc00000 | (FLASH1_BASE_64<<16))
#define	FLASH2_BASE_U		(0xbfc00000 | (FLASH2_BASE_64<<16))

    regs0->rom1_base = FLASH1_BASE_64;
    regs0->rom2_base = FLASH2_BASE_64;		// FLASH2_BASE_64;

//
//  now ready to issue flash command/data.
//

#define TLEN    4096
    {
	int		i;
	volatile BYTE	*p;
	UINT8		flash_id, read_byte1, read_byte2, read_byte3, read_byte4;

//	read id
	p = (volatile BYTE *)FLASH1_BASE_U;
	flash_id = flash_read_ID(p);
	*p = 0x50;		// clear status register

//	read it
/*
	p = (volatile BYTE *)FLASH1_BASE_U;
	for (i = 0; i < 32;) {
	    psprintf(LineBuffer, "%02x ", p[i]); i++;
	    epp_write_wait(LineBuffer);
	    if ((i & 0x0f) == 0) epp_write_wait("\n");
	}

	p = (volatile BYTE *)FLASH1_BASE_U;
	epp_write_wait("flash content\n");
	for (i = 0; i < 32;) {
	    psprintf(LineBuffer, "%02x ", p[i]); i++;
	    epp_write_wait(LineBuffer);
	    if ((i & 0x0f) == 0) epp_write_wait("\n");
	}
*/

	p = (volatile BYTE *)(FLASH1_BASE_U);
	flash_eraseblock(p);

	flash_prog(p, 0xaa);
	flash_prog(p + 1, 0xbb);
	flash_prog(p + 2, 0xcc);
	flash_prog(p + 3, 0xdd);

//	write 0xFF: read array
//	*p = 0xFF;		// FLUSHWB();
//	*p = 0xFF;		// FLUSHWB();

	flash_eraseblock(p);
	flash_prog(p + 0, 0xee);
//	monitor();
//	return 0;

	flash_eraseblock(p);
	for (i = 0; i < TLEN; i++)
	    flash_prog(p + i, 0xff);
	for (i = 0; i < TLEN; i++) {
	    if (p[i] != 0xff) {
	    }
	}
//	epp_write_wait("TEST 1 OK..\n");

	flash_eraseblock(p);
	for (i = 0; i < TLEN; i++)
	    flash_prog(p + i, 0x00);
	for (i = 0; i < TLEN; i++) {
	    if (p[i] != 0x00) {
	    }
	}
//	epp_write_wait("TEST 2 OK..\n");

	flash_eraseblock(p);
	for (i = 0; i < TLEN; i++)
	    flash_prog(p + i, i);
	for (i = 0; i < TLEN; i++) {
	    if (p[i] != (i & 0x00ff)) {
	    }
	}
//	epp_write_wait("TEST 3 OK..\n");
//	epp_write_wait("flash prg ended and OK..\n");
//	monitor();
    }
    return 0;
}

#endif SUPPORT_FLASH

#ifdef SUPPORT_FLASH_SERIAL

#ifdef SUPPORT_GPIO_BUS_FUNCTION
#define	FLASH_CLE		(1 << 3)
#define	FLASH_ALE		(1 << 7)
#define	FLASH_WP		(1 << 5)
#define	FLASH_CLE_LOW		(Bus1SetBit(FLASH_CLE, 0))
#define	FLASH_CLE_HIGH		(Bus1SetBit(FLASH_CLE, 1))
#define	FLASH_ALE_LOW		(Bus1SetBit(FLASH_ALE, 0))
#define	FLASH_ALE_HIGH		(Bus1SetBit(FLASH_ALE, 1))
#define	FLASH_WP_LOW		(Bus1SetBit(FLASH_WP, 0))
#define	FLASH_WP_HIGH		(Bus1SetBit(FLASH_WP, 1))

#define	FLASH_CE		14
#define	FLASH_WE		13
#define	FLASH_RE		12
#define	FLASH_RB		20
#define	FLASH_CE_LOW		(GPIO_O_SET(FLASH_CE, 0))
#define	FLASH_CE_HIGH		(GPIO_O_SET(FLASH_CE, 1))
#define	FLASH_WE_LOW		(GPIO_O_SET(FLASH_WE, 0))
#define	FLASH_WE_HIGH		(GPIO_O_SET(FLASH_WE, 1))
#define	FLASH_RE_LOW		(GPIO_O_SET(FLASH_RE, 0))
#define	FLASH_RE_HIGH		(GPIO_O_SET(FLASH_RE, 1))
#define	FLASH_RB_READ		(GPIO_I_GET(FLASH_RB))

#define	CMD_RESET		0xFF
#define	CMD_ERASE_1st		0x60
#define	CMD_ERASE_2nd		0xD0
#define	CMD_PROGRAM_1st		0x80
#define	CMD_PROGRAM_2nd		0x10
#define	CMD_READ_1_0		0x00
#define	CMD_READ_1_1		0x01
#define	CMD_READ_2		0x50
#define	CMD_READ_3		0x30
#define	CMD_READ_ID		0x90
#define	CMD_READ_STATUS		0x70

#define	FLASH_ID_SAMSUNG	0xEC
#define	FLASH_ID_TOSHIBA	0x98

#define	FLASH_TYPE_32MB		0x01
#define	FLASH_TYPE_64MB		0x02
#define	FLASH_TYPE_128MB	0x03
#define	FLASH_TYPE_256MB	0x04

#define	FLASH_TABLE_ID1		0xCF
#define	FLASH_TABLE_ID2		0x70
#define	FLASH_TABLE_ID3		0x12

#else	// Flash controlled by MCU

#define	FLASH_CS		41
#define	FLASH_SCK		42
#define	FLASH_SI		12
#define	FLASH_SO		13

#define	FLASH_CS_LOW		(GPIO_O_SET(FLASH_CS, 0))
#define	FLASH_CS_HIGH		(GPIO_O_SET(FLASH_CS, 1))
#define	FLASH_SCK_LOW		(GPIO_O_SET(FLASH_SCK, 0))
#define	FLASH_SCK_HIGH		(GPIO_O_SET(FLASH_SCK, 1))
#define	FLASH_SO_LOW		(GPIO_O_SET(FLASH_SO, 0))
#define	FLASH_SO_HIGH		(GPIO_O_SET(FLASH_SO, 1))
#define	FLASH_SI_READ		(GPIO_I_GET(FLASH_SI))

#define	CMD_MEMORY_PAGE_BUF1	0xa1
#define	CMD_MEMORY_ARRAY_READ	0xb2
#define	CMD_MEMORY_ERASE_BUF	0xc3
#define	CMD_STATUS_REG_READ	0x28

#define	STATUS_NOT_BUSY		0x82
#endif

void FlashDelay(UINT32 Count)
{
    UINT32		i;

    for (i = 0; i < Count; i++)
	asm volatile ("nop");
}

void FlashInitGPIO()
{
#ifdef SUPPORT_GPIO_BUS_FUNCTION
//  Input mode
    GPIO_G_SET(1, 0, (1 << (FLASH_RB - 16)));

//  Output mode
    GPIO_G_SET(0, 1, (1 << (FLASH_CE)) | (1 << (FLASH_WE)) | (1 << (FLASH_RE)));

    FLASH_CE_HIGH;
    FLASH_WE_HIGH;
    FLASH_RE_HIGH;
    FLASH_CLE_LOW;
    FLASH_ALE_LOW;
    FLASH_WP_LOW;
#else
//  Input mode
    GPIO_G_SET(0, 0, (1 << (FLASH_SI)));

//  Output mode
    GPIO_G_SET(2, 1, (1 << (FLASH_CS - 32)) | (1 << (FLASH_SCK - 32)));
    GPIO_G_SET(0, 1, (1 << (FLASH_SO)));

    FLASH_CS_HIGH;
    FLASH_SCK_HIGH;
#endif
}

#ifdef SUPPORT_GPIO_BUS_FUNCTION

#define	MAX_BUSY_DELAY		0x1000

void FlashWaitBusy()
{
    UINT16		i;

    for (i = 0; i < MAX_BUSY_DELAY; i++) {
	FlashDelay(20);
	if (FLASH_RB_READ)
	    break;
    }

    if (i >= MAX_BUSY_DELAY) {
	FLASH_CE_HIGH;
//	psprintf(LineBuffer, "ERROR : FLASH Wait too long.(%04x)", i);
//	MenuPrintMessage(4);
	psprintf(LineBuffer, "无效卡或者卡接触不良");
	MenuPrintMessage(3);
	psprintf(LineBuffer, "请关机,检查卡");
	MenuPrintMessage(4);
	while (1) {
	    polling();
	}
    }
}

void FlashWriteData(BYTE Data)
{
    BusSetData(Data);
    FLASH_WE_LOW;
    FLASH_WE_HIGH;
}

BYTE FlashReadData()
{
    BYTE		Result;

    FLASH_RE_LOW;
    Result = BusGetData();
    FLASH_RE_HIGH;

    return Result;
}

void FlashReset()
{
    FLASH_CE_LOW;
    BusSetOutput();			// Default is Output
    FLASH_RE_HIGH;
    FLASH_WE_HIGH;
    FLASH_CLE_HIGH;
    FlashWriteData(CMD_RESET);
    FLASH_CLE_LOW;

    FlashWaitBusy();
    FLASH_CE_HIGH;
}

void FlashReadDeviceInfo()
{
    FLASH_CE_LOW;
    BusSetOutput();			// Default is Output
    FLASH_RE_HIGH;
    FLASH_WE_HIGH;
    FLASH_CLE_HIGH;
    FlashWriteData(CMD_READ_ID);
    FLASH_CLE_LOW;
    FLASH_ALE_HIGH;
    FlashWriteData(0x00);
    FLASH_ALE_LOW;

    BusSetInput();
    FlashMaker = FlashReadData();
    if (FlashMaker == 0xEC ||		// Samsung
	FlashMaker == 0x98) {		// Toshiba
	FlashSizer = FlashReadData();
    }
    else {
	FlashMaker = 0xFF;
    }

    FLASH_CE_HIGH;
    BusSetOutput();			// Default is Output
}

BYTE FlashReadStatus()
{
    BYTE		Result;

    FLASH_CE_LOW;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -