From 10892ce7623da3b342de95a6abb31056ed3dae6d Mon Sep 17 00:00:00 2001 From: "goprife@gmail.com" Date: Thu, 27 Dec 2012 08:03:18 +0000 Subject: [PATCH] clean code in bsp/simulator git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2533 bbd45198-f89e-11dd-88c7-29a3b14d5316 --- bsp/simulator/applications/application.c | 2 +- bsp/simulator/applications/platform.c | 4 +- bsp/simulator/applications/startup.c | 12 +- bsp/simulator/drivers/board.c | 41 ++-- bsp/simulator/drivers/board.h | 17 +- bsp/simulator/drivers/nand_sim.c | 234 ----------------------- bsp/simulator/drivers/sdl_fb.c | 3 +- bsp/simulator/drivers/serial.c | 5 +- bsp/simulator/rtconfig.h | 5 +- 9 files changed, 45 insertions(+), 278 deletions(-) delete mode 100644 bsp/simulator/drivers/nand_sim.c diff --git a/bsp/simulator/applications/application.c b/bsp/simulator/applications/application.c index 14683687a8..f2319aa6b0 100644 --- a/bsp/simulator/applications/application.c +++ b/bsp/simulator/applications/application.c @@ -21,7 +21,7 @@ void rt_init_thread_entry(void *parameter) { #ifdef RT_USING_LWIP - extern void pcap_netif_hw_init(void); + pcap_netif_hw_init(); #endif diff --git a/bsp/simulator/applications/platform.c b/bsp/simulator/applications/platform.c index 8d94fc501b..1f7f0e1a82 100644 --- a/bsp/simulator/applications/platform.c +++ b/bsp/simulator/applications/platform.c @@ -18,8 +18,8 @@ void rt_platform_init(void) #endif /* RT_USING_DFS */ #ifdef RT_USING_RTGUI - /* initilize sdl */ - sdl_start(); + /* start sdl thread to simulate an LCD */ + rt_hw_sdl_start(); #endif /* RT_USING_RTGUI */ #ifdef WIN32 diff --git a/bsp/simulator/applications/startup.c b/bsp/simulator/applications/startup.c index 8ce9257c08..94a39914a1 100644 --- a/bsp/simulator/applications/startup.c +++ b/bsp/simulator/applications/startup.c @@ -20,15 +20,7 @@ /** * @addtogroup win32 */ -#define HEAP_SIZE (1024*1024*10) -static rt_uint8_t * heap; -void vs_heap_init(void) -{ - heap = malloc(HEAP_SIZE); - if (heap == RT_NULL) - rt_kprintf("there is no memory in pc."); -} /*@{*/ extern int rt_application_init(void); @@ -37,6 +29,7 @@ extern void finsh_system_init(void); extern void finsh_set_device(const char* device); #endif +extern rt_uint8_t * heap; /** * This function will startup RT-Thread RTOS. */ @@ -59,9 +52,6 @@ void rtthread_startup(void) #ifdef RT_USING_HEAP /* init memory system */ -#if (MSVC) - vs_heap_init(); -#endif rt_system_heap_init((void*)heap, (void*)&heap[HEAP_SIZE-1]); #endif diff --git a/bsp/simulator/drivers/board.c b/bsp/simulator/drivers/board.c index 33b1300baa..703aa11444 100644 --- a/bsp/simulator/drivers/board.c +++ b/bsp/simulator/drivers/board.c @@ -14,28 +14,27 @@ #include #include - #include "board.h" +#include +#include /** * @addtogroup simulator on win32 */ +rt_uint8_t * heap; -/** - * This function will initial win32 - */ -void rt_hw_board_init() +rt_uint8_t * rt_hw_sram_init(void) { -#if defined(RT_USING_CONSOLE) - rt_hw_usart_init(); - rt_hw_serial_init(); - rt_console_set_device(RT_CONSOLE_DEVICE_NAME); -#endif + rt_uint8_t * heap; + heap = malloc(HEAP_SIZE); + if (heap == RT_NULL) + { + rt_kprintf("there is no memory in pc."); + _exit(1); + } + return heap; } -/* fix the compile errors for redefiniton of lwip_htonl in win socket */ -#ifdef WIN32 -#include void rt_hw_win32_low_cpu(void) { Sleep(1000); @@ -57,8 +56,20 @@ void rt_hw_exit(void) exit(0); } FINSH_FUNCTION_EXPORT_ALIAS(rt_hw_exit, exit, exit rt-thread); - #endif /* RT_USING_FINSH */ -#endif /* WIN32 */ +/** + * This function will initial win32 + */ +void rt_hw_board_init() +{ + /* init system memory */ + heap = rt_hw_sram_init(); + +#if defined(RT_USING_CONSOLE) + rt_hw_usart_init(); + rt_hw_serial_init(); + rt_console_set_device(RT_CONSOLE_DEVICE_NAME); +#endif +} /*@}*/ diff --git a/bsp/simulator/drivers/board.h b/bsp/simulator/drivers/board.h index 8c943ef818..8fb381347f 100644 --- a/bsp/simulator/drivers/board.h +++ b/bsp/simulator/drivers/board.h @@ -14,21 +14,20 @@ #ifndef __BOARD_H__ #define __BOARD_H__ - -void rt_hw_board_led_on(int n); -void rt_hw_board_led_off(int n); void rt_hw_board_init(void); - -void rt_hw_serial_init(void); +rt_uint8_t * rt_hw_sram_init(void); /* SD Card init function */ void rt_hw_sdcard_init(void); int rt_hw_mtd_nand_init(void); - -int sst25vfxx_mtd_init(const char * nor_name, unsigned int block_start, unsigned int block_end); - +int sst25vfxx_mtd_init(const char *, unsigned int , unsigned int); +void pcap_netif_hw_init(void); void rt_platform_init(void); - +void rt_hw_usart_init(void); +void rt_hw_serial_init(void); +void rt_hw_sdl_start(void); void rt_hw_win32_low_cpu(void); + +void rt_hw_exit(void); #endif diff --git a/bsp/simulator/drivers/nand_sim.c b/bsp/simulator/drivers/nand_sim.c deleted file mode 100644 index 69a328f3f4..0000000000 --- a/bsp/simulator/drivers/nand_sim.c +++ /dev/null @@ -1,234 +0,0 @@ -#include -#include -#include -#include -#include - -// #define NAND_TRACE rt_kprintf -#define NAND_TRACE(...) - -#define NAND_SIM "nand.bin" - -struct nand_device -{ - struct rt_mtd_nand_device parent; - FILE * file; -}; -static struct nand_device _nand; - -#define NAND_DEVICE(device) (( struct nand_device*)(device)) - -#define PAGE_DATA_SIZE 2048 /* page data size in bytes */ -#define PAGE_SPARE_SIZE 64 /* oob size in bytes */ -#define BLOCK_PAGES 64 /* the number of pages in a block */ -#define BLOCK_SIZE ((PAGE_DATA_SIZE + PAGE_SPARE_SIZE) * BLOCK_PAGES) - -#define BLOCK_COUNT 128 /* 128 blocks == 16M */ -#define BLOCK_MARK_SPARE_OFFSET 4 - -static rt_mutex_t lock; - -/* RT-Thread device interface */ - -static rt_err_t k9f1g08_mtd_check_block( - struct rt_mtd_nand_device* device, - rt_uint32_t block) -{ - rt_uint8_t block_status; - int result; - - struct nand_device * nand; - nand = NAND_DEVICE(device); - - fseek(nand->file, block * device->pages_per_block * - (device->page_size + device->oob_size) + - device->page_size + BLOCK_MARK_SPARE_OFFSET, - SEEK_SET); - result = fread(&block_status, 1, 1, nand->file); - if (result < 0) - { - NAND_TRACE("nand fread error\n"); - return -RT_ERROR; - } - - return block_status == 0xFF ? RT_EOK : -RT_ERROR; -} - -static rt_err_t k9f1g08_mtd_mark_bad_block( - struct rt_mtd_nand_device* device, - rt_uint32_t block) -{ - rt_uint8_t block_status; - int result; - - struct nand_device * nand; - nand = NAND_DEVICE(device); - - fseek(nand->file, block * device->pages_per_block * - (device->page_size + device->oob_size) + - device->page_size + BLOCK_MARK_SPARE_OFFSET, - SEEK_SET); - - block_status = 0x00; - result = fwrite(&block_status, 1, 1, nand->file); - if (result < 0) - { - NAND_TRACE("nand fwrite error\n"); - return -RT_ERROR; - } - - return RT_EOK; -} - -static char block_buffer[BLOCK_SIZE]; - -static rt_err_t k9f1g08_mtd_erase_block( - struct rt_mtd_nand_device* device, - rt_uint32_t block) -{ - int result; - - struct nand_device * nand; - nand = NAND_DEVICE(device); - - fseek(nand->file, block * device->pages_per_block * - (device->page_size + device->oob_size), - SEEK_SET); - - memset(block_buffer, 0xFF, sizeof(BLOCK_SIZE)); - result = fwrite(block_buffer, BLOCK_SIZE, 1, nand->file); - if (result < 0) - { - NAND_TRACE("nand fwrite error\n"); - return -RT_ERROR; - } - - return RT_EOK; -} - -/* return 0, ecc ok, 1, can be fixed , -1 can not be fixed */ -static rt_err_t k9f1g08_mtd_read( - struct rt_mtd_nand_device * device, - rt_off_t page, - rt_uint8_t * data, rt_uint32_t data_len, //may not always be 2048 - rt_uint8_t * spare, rt_uint32_t spare_len) -{ - int result; - int ecc_status = 0; - - struct nand_device * nand; - nand = NAND_DEVICE(device); - - if (data != RT_NULL && data_len != 0) - { - fseek(nand->file, page * (device->page_size + device->oob_size), - SEEK_SET); - result = fread(data, data_len, 1, nand->file); - if (result < 0) - ecc_status = -1; - } - - if (spare != RT_NULL && spare_len != 0) - { - fseek(nand->file, page * (device->page_size + device->oob_size) - +device->page_size, - SEEK_SET); - result = fread(spare, spare_len, 1, nand->file); - if (result < 0) - ecc_status = -1; - } - - return ecc_status; -} - -static rt_err_t k9f1g08_mtd_write ( - struct rt_mtd_nand_device * device, - rt_off_t page, - const rt_uint8_t * data, rt_uint32_t data_len,//will be 2048 always! - const rt_uint8_t * spare, rt_uint32_t spare_len) -{ - int result; - int ecc_status = 0; - - struct nand_device * nand; - nand = NAND_DEVICE(device); - - if (data != RT_NULL && data_len != 0) - { - fseek(nand->file, page * (device->page_size + device->oob_size), - SEEK_SET); - result = fwrite(data, data_len, 1, nand->file); - if (result < 0) - ecc_status = -1; - } - - if (spare != RT_NULL && spare_len != 0) - { - fseek(nand->file, page * (device->page_size + device->oob_size) - +device->page_size, - SEEK_SET); - result = fwrite(spare, spare_len, 1, nand->file); - if (result < 0) - ecc_status = -1; - } - - return ecc_status; -} - - -const static struct rt_mtd_nand_driver_ops k9f1g08_mtd_ops = -{ - RT_NULL, - k9f1g08_mtd_read, - k9f1g08_mtd_write, - k9f1g08_mtd_erase_block, - k9f1g08_mtd_check_block, - k9f1g08_mtd_mark_bad_block, -}; - -/* interface of nand and rt-thread device */ -static struct rt_mtd_nand_device nand_part[2]; - -int rt_hw_mtd_nand_init(void) -{ - int size; - rt_uint32_t id, total_block; - struct nand_device * nand; - struct rt_mtd_nand_device * nand_part; - - nand = &_nand; - nand_part = &(nand->parent); - - lock = rt_mutex_create("nand", RT_IPC_FLAG_FIFO); - - /* open sd card file, if not exist, then create it */ - nand->file = fopen(NAND_SIM, "rb+"); - if (nand->file == NULL) - { - int i; - /* create a file to simulate sd card */ - nand->file = fopen(NAND_SIM, "wb+"); - - memset(block_buffer, 0xFF, sizeof(block_buffer)); - for(i=0; ifile, i * BLOCK_SIZE, SEEK_SET); - fwrite(block_buffer, BLOCK_SIZE, 1, nand->file); - } - } - fseek(nand->file, 0, SEEK_SET); - - /* the first partition of nand */ - nand_part->page_size = PAGE_DATA_SIZE; - nand_part->pages_per_block = BLOCK_PAGES;//don't caculate oob size - nand_part->block_start = 0; - nand_part->block_end = BLOCK_COUNT -1; - nand_part->oob_size = PAGE_SPARE_SIZE; - nand_part->ops = &k9f1g08_mtd_ops; - rt_mtd_nand_register_device("nand0", nand_part); - return RT_EOK; -} - -#ifdef RT_USING_FINSH -#include -#endif diff --git a/bsp/simulator/drivers/sdl_fb.c b/bsp/simulator/drivers/sdl_fb.c index 56b54b2891..1c6436740b 100644 --- a/bsp/simulator/drivers/sdl_fb.c +++ b/bsp/simulator/drivers/sdl_fb.c @@ -281,7 +281,8 @@ static DWORD WINAPI sdl_loop(LPVOID lpParam) return 0; } -void sdl_start(void) +/* start sdl thread */ +void rt_hw_sdl_start(void) { HANDLE thread; DWORD thread_id; diff --git a/bsp/simulator/drivers/serial.c b/bsp/simulator/drivers/serial.c index 0777bff928..1555869a2c 100644 --- a/bsp/simulator/drivers/serial.c +++ b/bsp/simulator/drivers/serial.c @@ -155,7 +155,6 @@ static rt_err_t rt_hw_serial_register(rt_device_t device, const char* name, rt_u rt_err_t rt_hw_serial_init(void) { - return rt_hw_serial_register(&serial_device,"sci0", + return rt_hw_serial_register(&serial_device,RT_CONSOLE_DEVICE_NAME, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM); -} - +} \ No newline at end of file diff --git a/bsp/simulator/rtconfig.h b/bsp/simulator/rtconfig.h index 623114abbd..e84b5bc75a 100644 --- a/bsp/simulator/rtconfig.h +++ b/bsp/simulator/rtconfig.h @@ -8,6 +8,8 @@ #undef RT_USING_MINILIBC #define NORESOURCE //RT_VESRION in winuser.h #define _CRT_ERRNO_DEFINED //errno macro redefinition + +#define HEAP_SIZE (1024*1024*2) #endif /* SECTION: basic kernel options */ @@ -69,7 +71,6 @@ /* SECTION: Device System */ /* Using Device System */ #define RT_USING_DEVICE -/* #define RT_USING_SERIAL */ /* #define RT_USING_UART1 */ /* SECTION: Console options */ @@ -132,7 +133,7 @@ #define DFS_FD_MAX 4 /* SECTION: lwip, a lighwight TCP/IP protocol stack */ -#define RT_USING_LWIP +/* #define RT_USING_LWIP */ /* LwIP uses RT-Thread Memory Management */ #define RT_LWIP_USING_RT_MEM /* Enable ICMP protocol*/