[mb9bf506r]手动-自动格式整理

This commit is contained in:
Meco Man 2021-03-21 23:32:39 +08:00
parent 061182271f
commit 364ef38e54
10 changed files with 1073 additions and 1114 deletions

View File

@ -1,11 +1,7 @@
/*
* File : application.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2009 - 2012, RT-Thread Development Team
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
@ -33,35 +29,35 @@
void rt_init_thread_entry(void *parameter)
{
/* LED Initialization */
rt_hw_led_init();
/* LED Initialization */
rt_hw_led_init();
#ifdef RT_USING_COMPONENTS_INIT
/* initialization RT-Thread Components */
rt_components_init();
/* initialization RT-Thread Components */
rt_components_init();
#endif
/* Filesystem Initialization */
/* Filesystem Initialization */
#ifdef RT_USING_DFS
/* mount nand fat partition 1 as root directory */
if (dfs_mount("nand", "/", "elm", 0, 0) == 0)
rt_kprintf("File System initialized!\n");
else
rt_kprintf("File System init failed!\n");
/* mount nand fat partition 1 as root directory */
if (dfs_mount("nand", "/", "elm", 0, 0) == 0)
rt_kprintf("File System initialized!\n");
else
rt_kprintf("File System init failed!\n");
#endif
}
int rt_application_init(void)
{
rt_thread_t tid;
rt_thread_t tid;
tid = rt_thread_create("init",
rt_init_thread_entry, RT_NULL,
2048, RT_THREAD_PRIORITY_MAX/3, 20);
if (tid != RT_NULL)
rt_thread_startup(tid);
tid = rt_thread_create("init",
rt_init_thread_entry, RT_NULL,
2048, RT_THREAD_PRIORITY_MAX/3, 20);
if (tid != RT_NULL)
rt_thread_startup(tid);
return 0;
return 0;
}
/*@}*/

View File

@ -1,11 +1,7 @@
/*
* File : startup.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2009 - 2012, RT-Thread Development Team
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
@ -38,60 +34,60 @@ extern int __bss_end;
*/
void rtthread_startup(void)
{
/* initialize board */
rt_hw_board_init();
/* initialize board */
rt_hw_board_init();
/* show version */
rt_show_version();
/* show version */
rt_show_version();
#ifdef RT_USING_HEAP
#ifdef __CC_ARM
rt_system_heap_init((void*)&Image$$RW_IRAM1$$ZI$$Limit, (void*)FM3_SRAM_END);
#elif __ICCARM__
rt_system_heap_init(__segment_end("HEAP"), (void*)FM3_SRAM_END);
#else
/* init memory system */
rt_system_heap_init((void*)&__bss_end, (void*)FM3_SRAM_END);
#endif
#ifdef __CC_ARM
rt_system_heap_init((void*)&Image$$RW_IRAM1$$ZI$$Limit, (void*)FM3_SRAM_END);
#elif __ICCARM__
rt_system_heap_init(__segment_end("HEAP"), (void*)FM3_SRAM_END);
#else
/* init memory system */
rt_system_heap_init((void*)&__bss_end, (void*)FM3_SRAM_END);
#endif
#endif
/* init scheduler system */
rt_system_scheduler_init();
/* init scheduler system */
rt_system_scheduler_init();
#ifdef RT_USING_DEVICE
#if defined(RT_USING_DFS) && defined(RT_USING_DFS_UFFS)
rt_hw_nand_init();
rt_hw_nand_init();
#endif
#endif
/* initialize application */
rt_application_init();
/* initialize application */
rt_application_init();
/* initialize timer */
rt_system_timer_init();
/* initialize timer thread */
rt_system_timer_thread_init();
/* initialize timer thread */
rt_system_timer_thread_init();
/* initialize idle thread */
rt_thread_idle_init();
/* initialize idle thread */
rt_thread_idle_init();
/* start scheduler */
rt_system_scheduler_start();
/* start scheduler */
rt_system_scheduler_start();
/* never reach here */
return ;
/* never reach here */
return ;
}
int main(void)
{
/* disable interrupt first */
rt_hw_interrupt_disable();
/* disable interrupt first */
rt_hw_interrupt_disable();
/* startup RT-Thread RTOS */
rtthread_startup();
/* startup RT-Thread RTOS */
rtthread_startup();
return 0;
return 0;
}
/*@}*/

View File

@ -1,11 +1,7 @@
/*
* File : board.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2009 - 2012 RT-Thread Develop Team
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
@ -32,13 +28,13 @@
*/
void SysTick_Handler(void)
{
/* enter interrupt */
rt_interrupt_enter();
/* enter interrupt */
rt_interrupt_enter();
rt_tick_increase();
rt_tick_increase();
/* leave interrupt */
rt_interrupt_leave();
/* leave interrupt */
rt_interrupt_leave();
}
/**
@ -46,16 +42,16 @@ void SysTick_Handler(void)
*/
void rt_hw_board_init(void)
{
/* init systick */
SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
/* init systick */
SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
/* initialize UART device */
rt_hw_serial_init();
/* set console as UART device */
rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
/* initialize UART device */
rt_hw_serial_init();
/* set console as UART device */
rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
/* initialize nand flash device */
rt_hw_nand_init();
/* initialize nand flash device */
rt_hw_nand_init();
}
/*@}*/

View File

@ -1,11 +1,7 @@
/*
* File : board.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2009, RT-Thread Development Team
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes

File diff suppressed because it is too large Load Diff

View File

@ -1,11 +1,7 @@
/*
* File : fm3_uart.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006, RT-Thread Development Team
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
@ -55,7 +51,7 @@
#define ESCR_DATABITS_7 0x03U
#define ESCR_DATABITS_9 0x04U
#define FIFO_SIZE 16
#define FIFO_SIZE 16
/*
* Enable/DISABLE Interrupt Controller
@ -66,19 +62,19 @@
struct uart03_device
{
FM3_MFS03_UART_TypeDef *uart_regs;
/* irq number */
IRQn_Type rx_irq;
IRQn_Type tx_irq;
FM3_MFS03_UART_TypeDef *uart_regs;
/* irq number */
IRQn_Type rx_irq;
IRQn_Type tx_irq;
};
struct uart47_device
{
FM3_MFS47_UART_TypeDef *uart_regs;
/* irq number */
IRQn_Type rx_irq;
IRQn_Type tx_irq;
rt_uint8_t fifo_size;
FM3_MFS47_UART_TypeDef *uart_regs;
/* irq number */
IRQn_Type rx_irq;
IRQn_Type tx_irq;
rt_uint8_t fifo_size;
};
void rt_hw_serial_init(void);

View File

@ -1,11 +1,7 @@
/*
* File : led.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2011, RT-Thread Develop Team
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
@ -20,7 +16,7 @@
void rt_hw_led_on(rt_uint8_t num)
{
RT_ASSERT(num < LEDS_MAX_NUMBER);
RT_ASSERT(num < LEDS_MAX_NUMBER);
switch (num)
{
@ -40,7 +36,7 @@ void rt_hw_led_on(rt_uint8_t num)
void rt_hw_led_off(rt_uint8_t num)
{
RT_ASSERT(num < LEDS_MAX_NUMBER);
RT_ASSERT(num < LEDS_MAX_NUMBER);
switch (num)
{
@ -60,7 +56,7 @@ void rt_hw_led_off(rt_uint8_t num)
void rt_hw_led_toggle(rt_uint8_t num)
{
RT_ASSERT(num < LEDS_MAX_NUMBER);
RT_ASSERT(num < LEDS_MAX_NUMBER);
switch (num)
{

View File

@ -1,11 +1,7 @@
/*
* File : led.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2011, RT-Thread Develop Team
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
@ -17,22 +13,22 @@
#include "mb9bf506r.h"
#define LEDS_MAX_NUMBER 4
#define LEDS_MAX_NUMBER 4
/* LED */
#define USER_LED1 (1UL<<0x9)
#define USER_LED2 (1UL<<0xa)
#define USER_LED3 (1UL<<0xb)
#define USER_LED_MASK (USER_LED1 | USER_LED2 | USER_LED3)
#define USER_LED_PFR FM3_GPIO->PFR1
#define USER_LED_PCR FM3_GPIO->PCR1
#define USER_LED_PDOR FM3_GPIO->PDOR1
#define USER_LED_DDR FM3_GPIO->DDR1
#define USER_LED_MASK (USER_LED1 | USER_LED2 | USER_LED3)
#define USER_LED_PFR FM3_GPIO->PFR1
#define USER_LED_PCR FM3_GPIO->PCR1
#define USER_LED_PDOR FM3_GPIO->PDOR1
#define USER_LED_DDR FM3_GPIO->DDR1
#define RT_DEVICE_CTRL_LED_ON 0
#define RT_DEVICE_CTRL_LED_OFF 1
#define RT_DEVICE_CTRL_LED_TOGGLE 2
#define RT_DEVICE_CTRL_LED_ON 0
#define RT_DEVICE_CTRL_LED_OFF 1
#define RT_DEVICE_CTRL_LED_TOGGLE 2
void rt_hw_led_init(void);
void rt_hw_led_on(rt_uint8_t num);

View File

@ -1,11 +1,7 @@
/*
* File : nand.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006, RT-Thread Development Team
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
@ -19,14 +15,14 @@
* NandFlash driver for SamSung K9F5608
* 32M x 8bit
*/
#define PAGE_SIZE 512
#define PAGE_PER_BLOCK 32
#define BLOCK_NUM 2048
#define PAGE_SIZE 512
#define PAGE_PER_BLOCK 32
#define BLOCK_NUM 2048
/* device driver debug trace */
/* #define NAND_DEBUG */
#ifdef NAND_DEBUG
#define trace_log rt_kprintf
#define trace_log rt_kprintf
#else
#define trace_log(...)
#endif
@ -38,14 +34,14 @@
*/
struct rt_device_nand
{
struct rt_device parent; /* which is inherited from rt_device */
struct rt_device parent; /* which is inherited from rt_device */
rt_uint16_t block_num; /* total block number in device */
rt_uint16_t page_per_block; /* pages in one block */
rt_uint16_t page_size; /* page size */
rt_uint16_t block_num; /* total block number in device */
rt_uint16_t page_per_block; /* pages in one block */
rt_uint16_t page_size; /* page size */
/* this buffer which used as to save data before erase block */
rt_uint8_t block_buffer[PAGE_SIZE * PAGE_PER_BLOCK];
/* this buffer which used as to save data before erase block */
rt_uint8_t block_buffer[PAGE_SIZE * PAGE_PER_BLOCK];
};
static struct rt_device_nand _nand;
@ -60,8 +56,8 @@ static struct rt_device_nand _nand;
#define NF_OE_H() {IO_NF_PDOR |= NF_EN;}
#define NF_OE_L() {IO_NF_PDOR &= ~NF_EN;}
#define NF_DATA_OUT() {IO_NF_PDOR &= ~NF_DATA_DIR;}
#define NF_DATA_IN() {IO_NF_PDOR |= NF_DATA_DIR;}
#define NF_DATA_OUT() {IO_NF_PDOR &= ~NF_DATA_DIR;}
#define NF_DATA_IN() {IO_NF_PDOR |= NF_DATA_DIR;}
static unsigned char NF_ReadStatus(void);
static void Wait(unsigned int cnt);
@ -107,11 +103,11 @@ static unsigned char NF_ReadStatus(void)
*/
static void NF_Init(void)
{
FM3_GPIO->PFR5 |= (0x7ff); /* D0-D5, CS7, ALE, CLE, WEX, REX */
FM3_GPIO->PFR3 |= (0x3); /* D6-D7 */
FM3_GPIO->EPFR10 |= (1<<13 /* CS enable */
|1<<6 /* ALE, CLE, WEX, REX enable */
|1<<0); /* D0-D7 enable */
FM3_GPIO->PFR5 |= (0x7ff); /* D0-D5, CS7, ALE, CLE, WEX, REX */
FM3_GPIO->PFR3 |= (0x3); /* D6-D7 */
FM3_GPIO->EPFR10 |= (1<<13 /* CS enable */
|1<<6 /* ALE, CLE, WEX, REX enable */
|1<<0); /* D0-D7 enable */
FM3_EXBUS->AREA7 = 0x001f00e0; /* Select CS7 area, 32Mbyte size */
FM3_EXBUS->MODE7 |= (1<<4); /* Nand Flash mode turn on, set 8 bit width */
@ -125,9 +121,9 @@ static void NF_Init(void)
static void NF_UnInit(void)
{
FM3_GPIO->PFR5 &= ~(0x7ff); /* disable D0-D5, CS7, ALE, CLE, WEX, REX */
FM3_GPIO->PFR3 &= ~(0x3); /* disable D6-D7 */
FM3_GPIO->EPFR10 &= ~(1<<13 /* disable CS enable */
FM3_GPIO->PFR5 &= ~(0x7ff); /* disable D0-D5, CS7, ALE, CLE, WEX, REX */
FM3_GPIO->PFR3 &= ~(0x3); /* disable D6-D7 */
FM3_GPIO->EPFR10 &= ~(1<<13 /* disable CS enable */
|1<<6 /* disable ALE, CLE, WEX, REX enable */
|1<<0); /* disable D0-D7 enable */
FM3_EXBUS->MODE7 &= ~(1<<4);
@ -150,46 +146,46 @@ int NF_ReadPage(unsigned int block, unsigned int page, unsigned char *buffer,
unsigned int blockPage,i;
NF_Init();
blockPage=(block<<5)+page; /* 1 block=32 page */
blockPage=(block<<5)+page; /* 1 block=32 page */
NF_OE_L();
NF_DATA_OUT();
if (buffer != RT_NULL)
{
volatile unsigned char ch;
if (buffer != RT_NULL)
{
volatile unsigned char ch;
NF_CMD(NAND_CMD_READ0); /* send read data */
NF_CMD(NAND_CMD_READ0); /* send read data */
NF_ADDR(0);
NF_ADDR(blockPage & 0xff);
NF_ADDR((blockPage>>8) & 0xff); /* send 3 byte address */
NF_CLR_ALE();
NF_DATA_IN();
NF_ADDR(0);
NF_ADDR(blockPage & 0xff);
NF_ADDR((blockPage>>8) & 0xff); /* send 3 byte address */
NF_CLR_ALE();
NF_DATA_IN();
Wait(500);
Wait(500);
for(i=0;i<512;i++) /* read 512 bytes data */
buffer[i] = NF_RDDATA();
for(i=0;i<16;i++) /* read 16 bytes oob */
if (oob != RT_NULL)
oob[i] = NF_RDDATA();
else
ch = NF_RDDATA();
}
else
{
NF_CMD(NAND_CMD_READOOB); /* send read data */
for(i=0;i<512;i++) /* read 512 bytes data */
buffer[i] = NF_RDDATA();
for(i=0;i<16;i++) /* read 16 bytes oob */
if (oob != RT_NULL)
oob[i] = NF_RDDATA();
else
ch = NF_RDDATA();
}
else
{
NF_CMD(NAND_CMD_READOOB); /* send read data */
NF_ADDR(0);
NF_ADDR(blockPage & 0xff);
NF_ADDR((blockPage>>8) & 0xff); /* send 3 byte address */
NF_CLR_ALE();
NF_DATA_IN();
NF_ADDR(0);
NF_ADDR(blockPage & 0xff);
NF_ADDR((blockPage>>8) & 0xff); /* send 3 byte address */
NF_CLR_ALE();
NF_DATA_IN();
Wait(500);
Wait(500);
for (i=0; i<16; i++) /* read 16 bytes oob */
oob[i] = NF_RDDATA();
}
for (i=0; i<16; i++) /* read 16 bytes oob */
oob[i] = NF_RDDATA();
}
NF_OE_H();
NF_UnInit();
@ -206,7 +202,7 @@ int NF_EraseBlock(unsigned int block)
{
rt_uint32_t blockPage;
trace_log("Erase block %d: ", block);
trace_log("Erase block %d: ", block);
NF_Init();
blockPage = (block << 5);
@ -219,19 +215,19 @@ int NF_EraseBlock(unsigned int block)
if(NF_ReadStatus())
{
NF_Reset();
NF_OE_H();
NF_UnInit();
trace_log("Failed\n");
rt_kprintf("erase block failed\n");
NF_Reset();
NF_OE_H();
NF_UnInit();
trace_log("Failed\n");
rt_kprintf("erase block failed\n");
return FLASH_NG;
return FLASH_NG;
}
NF_OE_H();
NF_UnInit();
trace_log("OK\n");
trace_log("OK\n");
return FLASH_OK;
}
@ -261,24 +257,24 @@ int NF_WritePage(unsigned block, unsigned page, const rt_uint8_t *buffer)
NF_ADDR((blockPage>>8) & 0xff);
NF_CLR_ALE();
for(i=0;i<512;i++) NF_WRDATA(buffer[i]); /* write data */
for(i=0;i<16;i++) NF_WRDATA(se[i]); /* dummy write */
for(i=0;i<512;i++) NF_WRDATA(buffer[i]); /* write data */
for(i=0;i<16;i++) NF_WRDATA(se[i]); /* dummy write */
NF_CMD(NAND_CMD_PAGEPROG); /* start programming */
NF_CMD(NAND_CMD_PAGEPROG); /* start programming */
if(NF_ReadStatus())
{
NF_Reset();
NF_OE_H();
NF_UnInit();
NF_Reset();
NF_OE_H();
NF_UnInit();
trace_log("write failed\n");
return FLASH_NG;
trace_log("write failed\n");
return FLASH_NG;
}
/* verify the write data */
NF_DATA_OUT();
NF_CMD(NAND_CMD_READ0); /* send read command */
NF_CMD(NAND_CMD_READ0); /* send read command */
NF_ADDR(0);
NF_ADDR(blockPage & 0xff);
NF_ADDR((blockPage>>8) & 0xff);
@ -288,11 +284,11 @@ int NF_WritePage(unsigned block, unsigned page, const rt_uint8_t *buffer)
Wait(500);
for(i=0; i<512; i++)
{
data=NF_RDDATA(); /* verify 1-512 byte */
data=NF_RDDATA(); /* verify 1-512 byte */
if(data != buffer[i])
{
trace_log("block %d, page %d\n", block , page);
trace_log("write data failed[%d]: %02x %02x\n", i, data, buffer[i]);
trace_log("block %d, page %d\n", block , page);
trace_log("write data failed[%d]: %02x %02x\n", i, data, buffer[i]);
NF_Reset();
NF_OE_H();
@ -303,11 +299,11 @@ int NF_WritePage(unsigned block, unsigned page, const rt_uint8_t *buffer)
for(i=0; i<16; i++)
{
data=NF_RDDATA(); /* verify 16 byte dummy data */
data=NF_RDDATA(); /* verify 16 byte dummy data */
if(data != se[i])
{
trace_log("block %d, page %d\n", block , page);
trace_log("write oob failed[%d]: %02x %02x\n", i, data, se[i]);
trace_log("block %d, page %d\n", block , page);
trace_log("write oob failed[%d]: %02x %02x\n", i, data, se[i]);
NF_Reset();
NF_OE_H();
NF_UnInit();
@ -327,7 +323,7 @@ int NF_WritePage(unsigned block, unsigned page, const rt_uint8_t *buffer)
*/
void NF_ReadID(unsigned char *id)
{
unsigned char maker_code;
unsigned char maker_code;
NF_Init();
NF_OE_L();
NF_DATA_OUT();
@ -337,7 +333,7 @@ void NF_ReadID(unsigned char *id)
Wait(10);
NF_DATA_IN();
maker_code = NF_RDDATA();
maker_code = maker_code;
maker_code = maker_code;
*id = NF_RDDATA();
NF_OE_H();
NF_UnInit();
@ -345,58 +341,58 @@ void NF_ReadID(unsigned char *id)
static rt_err_t rt_nand_init (rt_device_t dev)
{
/* empty implementation */
return RT_EOK;
/* empty implementation */
return RT_EOK;
}
static rt_err_t rt_nand_open(rt_device_t dev, rt_uint16_t oflag)
{
/* empty implementation */
return RT_EOK;
/* empty implementation */
return RT_EOK;
}
static rt_err_t rt_nand_close(rt_device_t dev)
{
/* empty implementation */
return RT_EOK;
/* empty implementation */
return RT_EOK;
}
/* nand device read */
static rt_size_t rt_nand_read (rt_device_t dev, rt_off_t pos, void* buffer,
rt_size_t size)
{
rt_ubase_t block; /* block of position */
rt_ubase_t page, index; /* page in block of position */
rt_uint8_t *page_ptr, oob[16];
struct rt_device_nand *nand;
rt_ubase_t block; /* block of position */
rt_ubase_t page, index; /* page in block of position */
rt_uint8_t *page_ptr, oob[16];
struct rt_device_nand *nand;
/* get nand device */
nand = (struct rt_device_nand*) dev;
RT_ASSERT(nand != RT_NULL);
/* get nand device */
nand = (struct rt_device_nand*) dev;
RT_ASSERT(nand != RT_NULL);
/* get block and page */
block = pos / nand->page_per_block;
page = pos % nand->page_per_block;
/* get block and page */
block = pos / nand->page_per_block;
page = pos % nand->page_per_block;
trace_log("nand read: position %d, block %d, page %d, size %d\n",
pos, block, page, size);
trace_log("nand read: position %d, block %d, page %d, size %d\n",
pos, block, page, size);
/* set page buffer pointer */
page_ptr = (rt_uint8_t*) buffer;
for (index = 0; index < size; index ++)
{
NF_ReadPage(block, page + index, page_ptr, oob);
page_ptr += nand->page_size;
/* set page buffer pointer */
page_ptr = (rt_uint8_t*) buffer;
for (index = 0; index < size; index ++)
{
NF_ReadPage(block, page + index, page_ptr, oob);
page_ptr += nand->page_size;
if (page + index > nand->page_per_block)
{
block += 1;
page = 0;
}
}
if (page + index > nand->page_per_block)
{
block += 1;
page = 0;
}
}
/* return read size (count of block) */
return size;
/* return read size (count of block) */
return size;
}
/*
@ -409,188 +405,188 @@ static rt_size_t rt_nand_read (rt_device_t dev, rt_off_t pos, void* buffer,
*/
static int rt_nand_eraseblock_writepage(struct rt_device_nand* nand,
rt_ubase_t block, rt_ubase_t page,
const rt_uint8_t *buffer, rt_ubase_t pages)
const rt_uint8_t *buffer, rt_ubase_t pages)
{
rt_ubase_t index;
rt_uint32_t page_status;
rt_uint8_t *page_ptr, oob[16];
rt_ubase_t index;
rt_uint32_t page_status;
rt_uint8_t *page_ptr, oob[16];
/* set page status */
page_status = 0;
/* set page status */
page_status = 0;
/* read each page in block */
page_ptr = nand->block_buffer;
for (index = 0; index < nand->page_per_block; index ++)
{
NF_ReadPage(block, index, page_ptr, oob);
if (!oob[0])
page_status |= (1 << index);
page_ptr += nand->page_size;
}
/* read each page in block */
page_ptr = nand->block_buffer;
for (index = 0; index < nand->page_per_block; index ++)
{
NF_ReadPage(block, index, page_ptr, oob);
if (!oob[0])
page_status |= (1 << index);
page_ptr += nand->page_size;
}
/* erase block */
NF_EraseBlock(block);
/* erase block */
NF_EraseBlock(block);
page_ptr = &(nand->block_buffer[page * nand->page_size]);
/* merge buffer to page buffer */
for (index = 0; index < pages; index ++)
{
rt_memcpy(page_ptr, buffer, nand->page_size);
page_ptr = &(nand->block_buffer[page * nand->page_size]);
/* merge buffer to page buffer */
for (index = 0; index < pages; index ++)
{
rt_memcpy(page_ptr, buffer, nand->page_size);
/* set page status */
page_status |= (1 << (page + index));
/* set page status */
page_status |= (1 << (page + index));
/* move to next page */
page_ptr += nand->page_size;
buffer += nand->page_size;
}
/* move to next page */
page_ptr += nand->page_size;
buffer += nand->page_size;
}
/* write to flash */
page_ptr = nand->block_buffer;
for (index = 0; index < nand->page_per_block; index ++)
{
if (page_status & (1 << index))
NF_WritePage(block, index, page_ptr);
/* write to flash */
page_ptr = nand->block_buffer;
for (index = 0; index < nand->page_per_block; index ++)
{
if (page_status & (1 << index))
NF_WritePage(block, index, page_ptr);
/* move to next page */
page_ptr += nand->page_size;
}
/* move to next page */
page_ptr += nand->page_size;
}
return 0;
return 0;
}
/* nand device write */
static rt_size_t rt_nand_write (rt_device_t dev, rt_off_t pos,
const void* buffer, rt_size_t size)
{
rt_ubase_t block, page;
rt_uint8_t oob[16];
struct rt_device_nand *nand;
rt_ubase_t block, page;
rt_uint8_t oob[16];
struct rt_device_nand *nand;
nand = (struct rt_device_nand*) dev;
RT_ASSERT(nand != RT_NULL);
nand = (struct rt_device_nand*) dev;
RT_ASSERT(nand != RT_NULL);
/* get block and page */
block = pos / nand->page_per_block;
page = pos % nand->page_per_block;
/* get block and page */
block = pos / nand->page_per_block;
page = pos % nand->page_per_block;
trace_log("nand write: position %d, block %d, page %d, size %d\n",
pos, block, page, size);
trace_log("nand write: position %d, block %d, page %d, size %d\n",
pos, block, page, size);
if (size == 1)
{
/* write one page */
if (size == 1)
{
/* write one page */
/* read oob to get page status */
NF_ReadPage(block, page, RT_NULL, oob);
if (oob[0])
NF_WritePage(block, page, buffer);
else
/* erase block and then write page */
rt_nand_eraseblock_writepage(nand, block, page, buffer, 1);
}
else if (size > 1)
{
rt_ubase_t index;
rt_ubase_t need_erase_block;
const rt_uint8_t *page_ptr;
rt_ubase_t chunk_pages, pages;
/* read oob to get page status */
NF_ReadPage(block, page, RT_NULL, oob);
if (oob[0])
NF_WritePage(block, page, buffer);
else
/* erase block and then write page */
rt_nand_eraseblock_writepage(nand, block, page, buffer, 1);
}
else if (size > 1)
{
rt_ubase_t index;
rt_ubase_t need_erase_block;
const rt_uint8_t *page_ptr;
rt_ubase_t chunk_pages, pages;
pages = size;
page_ptr = (const rt_uint8_t*) buffer;
do
{
need_erase_block = 0;
/* calculate pages in current chunk */
if (pages > nand->page_per_block - page)
chunk_pages = nand->page_per_block - page;
else
chunk_pages = pages;
pages = size;
page_ptr = (const rt_uint8_t*) buffer;
do
{
need_erase_block = 0;
/* calculate pages in current chunk */
if (pages > nand->page_per_block - page)
chunk_pages = nand->page_per_block - page;
else
chunk_pages = pages;
/* get page status in current block */
for (index = page; index < page + chunk_pages; index ++)
{
NF_ReadPage(block, index, RT_NULL, oob);
if (!oob[0])
{
/* this page has data, need erase this block firstly */
need_erase_block = 1;
break;
}
}
/* get page status in current block */
for (index = page; index < page + chunk_pages; index ++)
{
NF_ReadPage(block, index, RT_NULL, oob);
if (!oob[0])
{
/* this page has data, need erase this block firstly */
need_erase_block = 1;
break;
}
}
if (need_erase_block)
{
/* erase block and then write it */
rt_nand_eraseblock_writepage(nand, block, page, page_ptr, chunk_pages);
page_ptr += chunk_pages * nand->page_size;
}
else
{
/* write pages directly */
for (index = page; index < page + chunk_pages; index ++)
{
NF_WritePage(block, index, page_ptr);
page_ptr += nand->page_size;
}
}
if (need_erase_block)
{
/* erase block and then write it */
rt_nand_eraseblock_writepage(nand, block, page, page_ptr, chunk_pages);
page_ptr += chunk_pages * nand->page_size;
}
else
{
/* write pages directly */
for (index = page; index < page + chunk_pages; index ++)
{
NF_WritePage(block, index, page_ptr);
page_ptr += nand->page_size;
}
}
pages -= chunk_pages;
page = 0; block ++; /* move to next block */
}
while (pages);
}
pages -= chunk_pages;
page = 0; block ++; /* move to next block */
}
while (pages);
}
return size;
return size;
}
static rt_err_t rt_nand_control (rt_device_t dev, int cmd, void *args)
{
struct rt_device_nand *nand;
struct rt_device_nand *nand;
nand = (struct rt_device_nand*) dev;
nand = (struct rt_device_nand*) dev;
RT_ASSERT(dev != RT_NULL);
switch (cmd)
{
case RT_DEVICE_CTRL_BLK_GETGEOME:
{
struct rt_device_blk_geometry *geometry;
{
case RT_DEVICE_CTRL_BLK_GETGEOME:
{
struct rt_device_blk_geometry *geometry;
geometry = (struct rt_device_blk_geometry *)args;
if (geometry == RT_NULL) return -RT_ERROR;
geometry = (struct rt_device_blk_geometry *)args;
if (geometry == RT_NULL) return -RT_ERROR;
geometry->bytes_per_sector = nand->page_size;
geometry->block_size = nand->page_size * nand->page_per_block;
geometry->sector_count = nand->block_num * nand->page_per_block;
}
break;
}
geometry->bytes_per_sector = nand->page_size;
geometry->block_size = nand->page_size * nand->page_per_block;
geometry->sector_count = nand->block_num * nand->page_per_block;
}
break;
}
return RT_EOK;
return RT_EOK;
}
void rt_hw_nand_init(void)
{
/* initialize nand flash structure */
_nand.block_num = BLOCK_NUM;
_nand.page_per_block = PAGE_PER_BLOCK;
_nand.page_size = PAGE_SIZE;
/* initialize nand flash structure */
_nand.block_num = BLOCK_NUM;
_nand.page_per_block = PAGE_PER_BLOCK;
_nand.page_size = PAGE_SIZE;
rt_memset(_nand.block_buffer, 0, sizeof(_nand.block_buffer));
rt_memset(_nand.block_buffer, 0, sizeof(_nand.block_buffer));
_nand.parent.type = RT_Device_Class_MTD;
_nand.parent.rx_indicate = RT_NULL;
_nand.parent.tx_complete = RT_NULL;
_nand.parent.init = rt_nand_init;
_nand.parent.open = rt_nand_open;
_nand.parent.close = rt_nand_close;
_nand.parent.read = rt_nand_read;
_nand.parent.write = rt_nand_write;
_nand.parent.control = rt_nand_control;
_nand.parent.type = RT_Device_Class_MTD;
_nand.parent.rx_indicate = RT_NULL;
_nand.parent.tx_complete = RT_NULL;
_nand.parent.init = rt_nand_init;
_nand.parent.open = rt_nand_open;
_nand.parent.close = rt_nand_close;
_nand.parent.read = rt_nand_read;
_nand.parent.write = rt_nand_write;
_nand.parent.control = rt_nand_control;
/* register a MTD device */
rt_device_register(&(_nand.parent), "nand", RT_DEVICE_FLAG_RDWR);
/* register a MTD device */
rt_device_register(&(_nand.parent), "nand", RT_DEVICE_FLAG_RDWR);
}
#ifdef NAND_DEBUG
@ -600,68 +596,68 @@ unsigned char nand_oob[16];
void dump_mem(unsigned char* buffer, int length)
{
int i;
int i;
if (length > 64) length = 64;
for (i = 0; i < length; i ++)
{
rt_kprintf("%02x ", *buffer++);
if (((i+1) % 16) == 0)
rt_kprintf("\n");
}
rt_kprintf("\n");
if (length > 64) length = 64;
for (i = 0; i < length; i ++)
{
rt_kprintf("%02x ", *buffer++);
if (((i+1) % 16) == 0)
rt_kprintf("\n");
}
rt_kprintf("\n");
}
void nand_read(int block, int page)
{
rt_kprintf("read block %d, page %d\n", block, page);
rt_kprintf("read block %d, page %d\n", block, page);
NF_ReadPage(block, page, nand_buffer, nand_oob);
rt_kprintf("page data:\n");
dump_mem(nand_buffer, 512);
rt_kprintf("oob data:\n");
dump_mem(nand_oob, 16);
NF_ReadPage(block, page, nand_buffer, nand_oob);
rt_kprintf("page data:\n");
dump_mem(nand_buffer, 512);
rt_kprintf("oob data:\n");
dump_mem(nand_oob, 16);
}
FINSH_FUNCTION_EXPORT_ALIAS(nand_read, read_page, read page[block/page]);
void nand_write(int block, int page)
{
int i;
for (i = 0; i < 512; i ++)
nand_buffer[i] = i;
int i;
for (i = 0; i < 512; i ++)
nand_buffer[i] = i;
NF_WritePage(block, page, nand_buffer);
NF_WritePage(block, page, nand_buffer);
}
FINSH_FUNCTION_EXPORT_ALIAS(nand_write, write_page, write page[block/page]);
void nand_erase(int block)
{
NF_EraseBlock(block);
NF_EraseBlock(block);
}
FINSH_FUNCTION_EXPORT_ALIAS(nand_erase, erase_block, erase block[block]);
void nand_readoob(int block, int page)
{
rt_kprintf("read oob on block %d, page %d\n", block, page);
rt_kprintf("read oob on block %d, page %d\n", block, page);
NF_ReadPage(block, page, RT_NULL, (unsigned char*)nand_oob);
rt_kprintf("oob data:\n");
dump_mem(nand_oob, 16);
NF_ReadPage(block, page, RT_NULL, (unsigned char*)nand_oob);
rt_kprintf("oob data:\n");
dump_mem(nand_oob, 16);
}
FINSH_FUNCTION_EXPORT_ALIAS(nand_readoob, readoob, read oob[block/page]);
void nand_erase_chip()
{
int i;
unsigned char id;
int i;
unsigned char id;
NF_ReadID(&id);
rt_kprintf("id: %02x\n", id);
NF_ReadID(&id);
rt_kprintf("id: %02x\n", id);
for (i = 0; i < 2048; i ++)
{
NF_EraseBlock(i);
}
for (i = 0; i < 2048; i ++)
{
NF_EraseBlock(i);
}
}
FINSH_FUNCTION_EXPORT_ALIAS(nand_erase_chip, erase_chip, erase whole chip);
#endif

View File

@ -1,11 +1,7 @@
/*
* File : nand.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006, RT-Thread Development Team
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes