9ceb218f80
Co-authored-by: Cursor <cursoragent@cursor.com>
165 lines
7.3 KiB
C
165 lines
7.3 KiB
C
/*!
|
|
\file exmc_nandflash_fixed.h
|
|
\brief header file of improved NAND Flash driver
|
|
|
|
\version 2026-03-09, V1.4.0, firmware for GD32H7xx
|
|
*/
|
|
|
|
/*
|
|
Copyright (c) 2025, GigaDevice Semiconductor Inc.
|
|
|
|
Redistribution and use in source and binary forms, with or without modification,
|
|
are permitted provided that the following conditions are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright notice, this
|
|
list of conditions and the following disclaimer.
|
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
this list of conditions and the following disclaimer in the documentation
|
|
and/or other materials provided with the distribution.
|
|
3. Neither the name of the copyright holder nor the names of its contributors
|
|
may be used to endorse or promote products derived from this software without
|
|
specific prior written permission.
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
|
OF SUCH DAMAGE.
|
|
*/
|
|
|
|
#ifndef EXMC_NANDFLASH_FIXED_H
|
|
#define EXMC_NANDFLASH_FIXED_H
|
|
|
|
#include "gd32h7xx.h"
|
|
|
|
/* EXMC NAND Flash区域定义 */
|
|
#define EXMC_CMD_AREA (uint32_t)(1<<16) /* A16 = CLE high command area */
|
|
#define EXMC_ADDR_AREA (uint32_t)(1<<17) /* A17 = ALE high address area */
|
|
#define EXMC_DATA_AREA ((uint32_t)0x00000000) /* data area */
|
|
|
|
/* NAND Flash命令定义 */
|
|
#define NAND_CMD_READ1_1ST ((uint8_t)0x00)
|
|
#define NAND_CMD_READ1_2ND ((uint8_t)0x30)
|
|
#define NAND_CMD_WRITE_1ST ((uint8_t)0x80)
|
|
#define NAND_CMD_WRITE_2ND ((uint8_t)0x10)
|
|
#define NAND_CMD_ERASE_1ST ((uint8_t)0x60)
|
|
#define NAND_CMD_ERASE_2ND ((uint8_t)0xD0)
|
|
#define NAND_CMD_READID ((uint8_t)0x90)
|
|
#define NAND_CMD_STATUS ((uint8_t)0x70)
|
|
#define NAND_CMD_LOCK_STATUS ((uint8_t)0x7A)
|
|
#define NAND_CMD_RESET ((uint8_t)0xFF)
|
|
|
|
/* NAND Flash状态定义 */
|
|
#define NAND_BUSY ((uint8_t)0x00)
|
|
#define NAND_ERROR ((uint8_t)0x01)
|
|
#define NAND_READY ((uint8_t)0x40)
|
|
#define NAND_TIMEOUT_ERROR ((uint8_t)0x80)
|
|
|
|
/* NAND Flash参数定义 */
|
|
#define NAND_ZONE_COUNT ((uint16_t)0x0001) /* 区域数量 */
|
|
#define NAND_ZONE_SIZE ((uint16_t)0x0400) /* 每区块数 (1024) */
|
|
#define NAND_BLOCK_SIZE ((uint16_t)0x0040) /* 每块页数 (64) */
|
|
#define NAND_PAGE_SIZE ((uint16_t)0x0800) /* 页大小 (2048字节) */
|
|
#define NAND_SPARE_AREA_SIZE ((uint16_t)0x0080) /* 备用区大小 (128字节) */
|
|
#define NAND_PAGE_TOTAL_SIZE (NAND_PAGE_SIZE + NAND_SPARE_AREA_SIZE) /* 总页大小 */
|
|
#define NAND_MAX_ADDRESS (((NAND_ZONE_COUNT*NAND_ZONE_SIZE)*NAND_BLOCK_SIZE)*NAND_PAGE_SIZE) /* 最大地址 */
|
|
#define NAND_BLOCK_COUNT 1024 /* 块总数 */
|
|
|
|
/* NAND Flash地址计算宏 */
|
|
#define ADDR_1ST_CYCLE(ADDR) (uint8_t)((ADDR)& 0xFF)
|
|
#define ADDR_2ND_CYCLE(ADDR) (uint8_t)(((ADDR)& 0xFF00) >> 8)
|
|
#define ADDR_3RD_CYCLE(ADDR) (uint8_t)(((ADDR)& 0xFF0000) >> 16)
|
|
#define ADDR_4TH_CYCLE(ADDR) (uint8_t)(((ADDR)& 0xFF000000) >> 24)
|
|
|
|
/* 返回值定义 */
|
|
#define NAND_OK 0
|
|
#define NAND_FAIL 1
|
|
|
|
/* EXMC NAND Flash基地址 */
|
|
#define BANK_NAND_ADDR ((uint32_t)0x80000000)
|
|
|
|
/* 硬件引脚定义 */
|
|
#define NAND_EXMC_D0_PIN GPIO_PIN_14 /* PD14 */
|
|
#define NAND_EXMC_D1_PIN GPIO_PIN_15 /* PD15 */
|
|
#define NAND_EXMC_D2_PIN GPIO_PIN_0 /* PD0 */
|
|
#define NAND_EXMC_D3_PIN GPIO_PIN_1 /* PD1 */
|
|
#define NAND_EXMC_D4_PIN GPIO_PIN_7 /* PE7 */
|
|
#define NAND_EXMC_D5_PIN GPIO_PIN_8 /* PE8 */
|
|
#define NAND_EXMC_D6_PIN GPIO_PIN_9 /* PE9 */
|
|
#define NAND_EXMC_D7_PIN GPIO_PIN_10 /* PE10 */
|
|
#define NAND_EXMC_NCE_PIN GPIO_PIN_7 /* PD7 */
|
|
#define NAND_EXMC_CLE_PIN GPIO_PIN_11 /* PD11 */
|
|
#define NAND_EXMC_ALE_PIN GPIO_PIN_12 /* PD12 */
|
|
#define NAND_EXMC_NWE_PIN GPIO_PIN_5 /* PD5 */
|
|
#define NAND_EXMC_NOE_PIN GPIO_PIN_4 /* PD4 */
|
|
#define NAND_EXMC_NWAIT_PIN GPIO_PIN_6 /* PD6 */
|
|
|
|
/* 硬件端口定义 */
|
|
#define NAND_EXMC_D0_PORT GPIOD
|
|
#define NAND_EXMC_D1_PORT GPIOD
|
|
#define NAND_EXMC_D2_PORT GPIOD
|
|
#define NAND_EXMC_D3_PORT GPIOD
|
|
#define NAND_EXMC_D4_PORT GPIOE
|
|
#define NAND_EXMC_D5_PORT GPIOE
|
|
#define NAND_EXMC_D6_PORT GPIOE
|
|
#define NAND_EXMC_D7_PORT GPIOE
|
|
#define NAND_EXMC_NCE_PORT GPIOD
|
|
#define NAND_EXMC_CLE_PORT GPIOD
|
|
#define NAND_EXMC_ALE_PORT GPIOD
|
|
#define NAND_EXMC_NWE_PORT GPIOD
|
|
#define NAND_EXMC_NOE_PORT GPIOD
|
|
#define NAND_EXMC_NWAIT_PORT GPIOD
|
|
|
|
/* 复用功能定义 */
|
|
#define NAND_EXMC_AF GPIO_AF_12
|
|
|
|
/* NAND Flash ID结构体 */
|
|
typedef struct {
|
|
uint8_t maker_id; /* 制造商ID */
|
|
uint8_t device_id; /* 设备ID */
|
|
uint8_t third_id; /* 第三字节ID */
|
|
uint8_t fourth_id; /* 第四字节ID */
|
|
} nand_id_struct;
|
|
|
|
/* NAND Flash地址结构体 */
|
|
typedef struct {
|
|
uint16_t zone; /* 区域号 */
|
|
uint16_t block; /* 块号 */
|
|
uint16_t page; /* 页号 */
|
|
uint16_t page_in_offset; /* 页内偏移 */
|
|
} nand_address_struct;
|
|
|
|
/* 新的API函数声明 */
|
|
/* 初始化和配置 */
|
|
int exmc_nandflash_init(void); /* 完整初始化 */
|
|
void exmc_nandflash_deinit(void); /* 完整去初始化 */
|
|
uint8_t exmc_nandflash_is_initialized(void); /* 检查初始化状态 */
|
|
|
|
/* 基本操作 */
|
|
int nand_read_id(nand_id_struct *nand_id); /* 读取NAND Flash ID */
|
|
int nand_reset(void); /* 重置NAND Flash */
|
|
int nand_format(void); /* 格式化NAND Flash */
|
|
|
|
/* 数据读写 */
|
|
int nand_write(uint32_t memaddr, uint8_t *pwritebuf, uint16_t bytecount); /* 写入数据 */
|
|
int nand_read(uint32_t memaddr, uint8_t *preadbuf, uint16_t bytecount); /* 读取数据 */
|
|
|
|
/* 备用区操作 */
|
|
int exmc_nand_writespare(uint8_t *pbuffer, nand_address_struct address, uint16_t bytecount); /* 写入备用区 */
|
|
int exmc_nand_readspare(uint8_t *pbuffer, nand_address_struct address, uint16_t bytecount); /* 读取备用区 */
|
|
|
|
/* 工具函数 */
|
|
void fill_buffer_nand(uint8_t *pbuffer, uint16_t buffer_length, uint32_t value); /* 填充缓冲区 */
|
|
int nand_self_test(void); /* 自检测试 */
|
|
void nand_print_info(void); /* 显示NAND Flash信息 */
|
|
|
|
/* 兼容原有接口已统一到新API中,无需重复声明 */
|
|
|
|
#endif /* EXMC_NANDFLASH_FIXED_H */
|
|
|