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

View File

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

View File

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

View File

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

File diff suppressed because it is too large Load Diff

View File

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

View File

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

View File

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

View File

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

View File

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