From 6e242e05298d024a4b612783489547e305f91c6c Mon Sep 17 00:00:00 2001 From: "goprife@gmail.com" Date: Sun, 4 Nov 2012 05:52:14 +0000 Subject: rebuild the directory organization of simulator git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2389 bbd45198-f89e-11dd-88c7-29a3b14d5316 --- bsp/simulator/drivers/SConscript | 16 ++ bsp/simulator/drivers/board.c | 45 ++++++ bsp/simulator/drivers/board.h | 32 ++++ bsp/simulator/drivers/nand_sim.c | 234 ++++++++++++++++++++++++++++++ bsp/simulator/drivers/sd_sim.c | 194 +++++++++++++++++++++++++ bsp/simulator/drivers/serial.c | 161 ++++++++++++++++++++ bsp/simulator/drivers/serial.h | 22 +++ bsp/simulator/drivers/sst25vfxx_mtd.h | 24 +++ bsp/simulator/drivers/sst25vfxx_mtd_sim.c | 228 +++++++++++++++++++++++++++++ bsp/simulator/drivers/usart_sim.c | 131 +++++++++++++++++ 10 files changed, 1087 insertions(+) create mode 100644 bsp/simulator/drivers/SConscript create mode 100644 bsp/simulator/drivers/board.c create mode 100644 bsp/simulator/drivers/board.h create mode 100644 bsp/simulator/drivers/nand_sim.c create mode 100644 bsp/simulator/drivers/sd_sim.c create mode 100644 bsp/simulator/drivers/serial.c create mode 100644 bsp/simulator/drivers/serial.h create mode 100644 bsp/simulator/drivers/sst25vfxx_mtd.h create mode 100644 bsp/simulator/drivers/sst25vfxx_mtd_sim.c create mode 100644 bsp/simulator/drivers/usart_sim.c (limited to 'bsp/simulator/drivers') diff --git a/bsp/simulator/drivers/SConscript b/bsp/simulator/drivers/SConscript new file mode 100644 index 0000000000..6a3cfe2b47 --- /dev/null +++ b/bsp/simulator/drivers/SConscript @@ -0,0 +1,16 @@ +from building import * + +cwd = GetCurrentDir() +src = Glob('*.c') + +# remove no need file. +if GetDepend('RT_USING_LWIP') == False: + SrcRemove(src, 'emac.c') +if GetDepend('RT_USING_DFS') == False: + SrcRemove(src, 'sd.c') + +CPPPATH = [cwd] + +group = DefineGroup('Drivers', src, depend = [''], CPPPATH = CPPPATH) + +Return('group') diff --git a/bsp/simulator/drivers/board.c b/bsp/simulator/drivers/board.c new file mode 100644 index 0000000000..5d9cfcfd68 --- /dev/null +++ b/bsp/simulator/drivers/board.c @@ -0,0 +1,45 @@ +/* + * File : board.c + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2009 RT-Thread Develop 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 + * + * Change Logs: + * Date Author Notes + * 2009-01-05 Bernard first implementation + */ + +#include +#include + +#include "board.h" + +/** + * @addtogroup simulator on win32 + */ + +/** + * This function will initial STM32 board. + */ +void rt_hw_board_init() +{ +#if 0 + /* NVIC Configuration */ + NVIC_Configuration(); + + /* Configure the SysTick */ + SysTick_Config( SystemCoreClock / RT_TICK_PER_SECOND ); +#endif + +#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 new file mode 100644 index 0000000000..cf7c375cc1 --- /dev/null +++ b/bsp/simulator/drivers/board.h @@ -0,0 +1,32 @@ +/* + * File : board.h + * 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 + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2009-09-22 Bernard add board.h to this bsp + */ + +#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); + +/* 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); + +void rt_platform_init(void); +#endif diff --git a/bsp/simulator/drivers/nand_sim.c b/bsp/simulator/drivers/nand_sim.c new file mode 100644 index 0000000000..69a328f3f4 --- /dev/null +++ b/bsp/simulator/drivers/nand_sim.c @@ -0,0 +1,234 @@ +#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/sd_sim.c b/bsp/simulator/drivers/sd_sim.c new file mode 100644 index 0000000000..93538620eb --- /dev/null +++ b/bsp/simulator/drivers/sd_sim.c @@ -0,0 +1,194 @@ +#include +#include +#include +#include +#include + +// #define SD_TRACE rt_kprintf +#define SD_TRACE(...) + +//#define SDCARD_SIM "F:\\Project\\tools\\SDCARD" +#define SDCARD_SIM "sd.bin" +#define SDCARD_SIZE (16*1024*1024) //16M + +struct sdcard_device +{ + struct rt_device parent; + FILE* file; +}; +static struct sdcard_device _sdcard; + +#define SDCARD_DEVICE(device) (( struct sdcard_device*)(device)) + +static rt_mutex_t lock; + +/* RT-Thread device interface */ + +static rt_err_t rt_sdcard_init(rt_device_t dev) +{ + return RT_EOK; +} + +static rt_err_t rt_sdcard_open(rt_device_t dev, rt_uint16_t oflag) +{ + return RT_EOK; +} + +static rt_err_t rt_sdcard_close(rt_device_t dev) +{ + return RT_EOK; +} + +/* position: block page address, not bytes address + * buffer: + * size : how many blocks + */ +static rt_size_t rt_sdcard_read(rt_device_t device, rt_off_t position, void* buffer, rt_size_t size) +{ + struct sdcard_device * sd; + int result = 0; + + SD_TRACE("sd read: pos %d, size %d\n", position, size); + + rt_mutex_take(lock, RT_WAITING_FOREVER); + sd = SDCARD_DEVICE(device); + fseek(sd->file, position * SECTOR_SIZE, SEEK_SET); + + result = fread(buffer, size * SECTOR_SIZE, 1, sd->file); + if (result < 0) + goto _err; + + rt_mutex_release(lock); + return size; + +_err: + SD_TRACE("sd read errors!\n"); + rt_mutex_release(lock); + return 0; +} + +/* position: block page address, not bytes address + * buffer: + * size : how many blocks + */ +static rt_size_t rt_sdcard_write(rt_device_t device, rt_off_t position, const void* buffer, rt_size_t size) +{ + struct sdcard_device * sd; + int result = 0; + + SD_TRACE("sst write: pos %d, size %d\n", position, size); + + rt_mutex_take(lock, RT_WAITING_FOREVER); + sd = SDCARD_DEVICE(device); + fseek(sd->file, position * SECTOR_SIZE, SEEK_SET); + + result = fwrite(buffer, size * SECTOR_SIZE, 1, sd->file); + if (result < 0) + goto _err; + + rt_mutex_release(lock); + return size; + +_err: + SD_TRACE("sd write errors!\n"); + rt_mutex_release(lock); + return 0; +} + +static rt_err_t rt_sdcard_control(rt_device_t dev, rt_uint8_t cmd, void *args) +{ + struct sdcard_device * sd; + unsigned int size; + + RT_ASSERT(dev != RT_NULL); + + sd = SDCARD_DEVICE(dev); + + if (cmd == 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->bytes_per_sector = SECTOR_SIZE; + geometry->block_size = SECTOR_SIZE; + + fseek(sd->file, 0, SEEK_END); + size = ftell(sd->file); + + geometry->sector_count = size/SECTOR_SIZE; + } + return RT_EOK; +} + + +rt_err_t rt_hw_sdcard_init(const char * spi_device_name) +{ + int size; + rt_uint32_t id, total_block; + struct sdcard_device * sd; + struct rt_device * device; + + sd = &_sdcard; + device = &(sd->parent); + + lock = rt_mutex_create("lock", RT_IPC_FLAG_FIFO); + + /* open sd card file, if not exist, then create it */ + sd->file = fopen(SDCARD_SIM, "rb+"); + if (sd->file == NULL) + { + /* create a file to simulate sd card */ + sd->file = fopen(SDCARD_SIM, "wb+"); + + fseek(sd->file, 0, SEEK_END); + size = ftell(sd->file); + + fseek(sd->file, 0, SEEK_SET ); + if (size < SDCARD_SIZE) + { + int i; + unsigned char* ptr; + + ptr = (unsigned char*) malloc (1024 * 1024); + if (ptr == NULL) + { + SD_TRACE("malloc error, no memory!\n"); + return RT_ERROR; + } + memset(ptr, 0x0, 1024 * 1024); + + fseek(sd->file, 0, SEEK_SET); + + for(i=0; i<(SDCARD_SIZE / (1024*1024)); i++) + fwrite(ptr, 1024 * 1024, 1, sd->file); + + free(ptr); + } + } + fseek(sd->file, 0, SEEK_SET); + + device->type = RT_Device_Class_Block; + device->init = rt_sdcard_init; + device->open = rt_sdcard_open; + device->close = rt_sdcard_close; + device->read = rt_sdcard_read; + device->write = rt_sdcard_write; + device->control = rt_sdcard_control; + device->user_data = NULL; + + rt_device_register(device, "sd0", + RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE); + + return RT_EOK; +} + +#ifdef RT_USING_FINSH +#include +void eraseall(void) +{ + printf("had not implemented yet!\n"); +} +FINSH_FUNCTION_EXPORT(eraseall, erase all block in SPI flash); +#endif diff --git a/bsp/simulator/drivers/serial.c b/bsp/simulator/drivers/serial.c new file mode 100644 index 0000000000..0777bff928 --- /dev/null +++ b/bsp/simulator/drivers/serial.c @@ -0,0 +1,161 @@ +/* +****************************************************************************** +* By : parai +* email:parai@foxmail.com +* 这并ä¸�是一个真的串å�£è®¾å¤‡ï¼Œå�ªæ˜¯ä¸ºäº†èƒ½å¤Ÿè®©å†…核打å�°ä¿¡æ�¯è€Œåˆ›å»º +****************************************************************************** +*/ + +#include "rtthread.h" + +#define _DEBUG_SERIAL 0 +#include "serial.h" +#include +struct rt_device serial_device; +extern struct serial_int_rx serial_rx; + +/*@{*/ + +/* RT-Thread Device Interface */ +/** + * This function initializes serial + */ +static rt_err_t rt_serial_init (rt_device_t dev) +{ + if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED)) + { + if (dev->flag & RT_DEVICE_FLAG_INT_RX) + { + rt_memset(serial_rx.rx_buffer, 0, + sizeof(serial_rx.rx_buffer)); + serial_rx.read_index = 0; + serial_rx.save_index = 0; + } + + dev->flag |= RT_DEVICE_FLAG_ACTIVATED; + } + return RT_EOK; +} + +static rt_err_t rt_serial_open(rt_device_t dev, rt_uint16_t oflag){ +#if _DEBUG_SERIAL==1 + printf("in rt_serial_open()\n"); +#endif + return RT_EOK; +} + +static rt_err_t rt_serial_close(rt_device_t dev) +{ +#if _DEBUG_SERIAL==1 + printf("in rt_serial_close()\n"); +#endif + return RT_EOK; +} +static rt_size_t rt_serial_read (rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size) +{ + rt_uint8_t* ptr; + rt_err_t err_code; + + ptr = buffer; + err_code = RT_EOK; + + if (dev->flag & RT_DEVICE_FLAG_INT_RX) + { + /* interrupt mode Rx */ + while (size) + { + rt_base_t level; + + /* disable interrupt */ + level = rt_hw_interrupt_disable(); + + if (serial_rx.read_index != serial_rx.save_index) + { + /* read a character */ + *ptr++ = serial_rx.rx_buffer[serial_rx.read_index]; + size--; + + /* move to next position */ + serial_rx.read_index ++; + if (serial_rx.read_index >= SERIAL_RX_BUFFER_SIZE) + serial_rx.read_index = 0; + } + else + { + /* set error code */ + err_code = -RT_EEMPTY; + + /* enable interrupt */ + rt_hw_interrupt_enable(level); + break; + } + + /* enable interrupt */ + rt_hw_interrupt_enable(level); + } + } + + + /* set error code */ + rt_set_errno(err_code); + return (rt_uint32_t)ptr - (rt_uint32_t)buffer; +} + +static rt_size_t rt_serial_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size) +{ +#if _DEBUG_SERIAL==1 + printf("in rt_serial_write()\n"); +#endif + printf("%s",(char*)buffer); + return size; +} + +static rt_err_t rt_serial_control (rt_device_t dev, rt_uint8_t cmd, void *args) +{ + RT_ASSERT(dev != RT_NULL); + + switch (cmd){ + case RT_DEVICE_CTRL_SUSPEND: + /* suspend device */ + dev->flag |= RT_DEVICE_FLAG_SUSPENDED; + break; + + case RT_DEVICE_CTRL_RESUME: + /* resume device */ + dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED; + break; + } + + return RT_EOK; +} + +/* + * serial register + */ +static rt_err_t rt_hw_serial_register(rt_device_t device, const char* name, rt_uint32_t flag) +{ + RT_ASSERT(device != RT_NULL); +#if _DEBUG_SERIAL==1 + printf("in rt_serial_register()\n"); +#endif + device->type = RT_Device_Class_Char; + device->rx_indicate = RT_NULL; + device->tx_complete = RT_NULL; + device->init = rt_serial_init; + device->open = rt_serial_open; + device->close = rt_serial_close; + device->read = rt_serial_read; + device->write = rt_serial_write; + device->control = rt_serial_control; + device->user_data = RT_NULL; + + /* register a character device */ + return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | flag); +} + +rt_err_t rt_hw_serial_init(void) +{ + return rt_hw_serial_register(&serial_device,"sci0", + RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM); +} + diff --git a/bsp/simulator/drivers/serial.h b/bsp/simulator/drivers/serial.h new file mode 100644 index 0000000000..fe82957b34 --- /dev/null +++ b/bsp/simulator/drivers/serial.h @@ -0,0 +1,22 @@ +/* +********************************************************************************************************* +* MC9S12DP256/DG128 Specific code +* BANKED MEMORY MODEL +* +* File : rthw.c +* By : parai +* email:parai@foxmail.com +*******************************************************************************************************/ + +#ifndef __RT_HW_SERIAL_H__ +#define __RT_HW_SERIAL_H__ + +#define SERIAL_RX_BUFFER_SIZE 80 +struct serial_int_rx +{ + rt_uint8_t rx_buffer[SERIAL_RX_BUFFER_SIZE]; + rt_uint32_t read_index, save_index; +}; + +rt_err_t rt_hw_serial_init(void); +#endif diff --git a/bsp/simulator/drivers/sst25vfxx_mtd.h b/bsp/simulator/drivers/sst25vfxx_mtd.h new file mode 100644 index 0000000000..6b08662474 --- /dev/null +++ b/bsp/simulator/drivers/sst25vfxx_mtd.h @@ -0,0 +1,24 @@ +/* + * File : sst25vfxx_mtd.h + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2011, 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 + * + * Change Logs: + * Date Author Notes + * 2011-12-16 aozima the first version + * 2012-02-01 mbbill MTD driver version + */ + +#ifndef SST25VFXX_MTD_H +#define SST25VFXX_MTD_H + +#include +#include + +rt_err_t sst25vfxx_mtd_init(const char *spi_device_name, rt_uint32_t block_start, rt_uint32_t block_end); + +#endif diff --git a/bsp/simulator/drivers/sst25vfxx_mtd_sim.c b/bsp/simulator/drivers/sst25vfxx_mtd_sim.c new file mode 100644 index 0000000000..d2c4117302 --- /dev/null +++ b/bsp/simulator/drivers/sst25vfxx_mtd_sim.c @@ -0,0 +1,228 @@ +/* + * File : rtdef.h + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2012, 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 + * + * Change Logs: + * Date Author Notes + * 2012-10-21 prife the first version + */ + +#include +#include +#include +#include +#include "sst25vfxx_mtd.h" + +#ifdef RT_USING_MTD_NOR +#define NOR_SIM "nor.bin" +/* JEDEC Manufacturer¡¯s ID */ +#define MF_ID (0xBF) +/* JEDEC Device ID : Memory Type */ +#define MT_ID (0x25) +/* JEDEC Device ID: Memory Capacity */ +#define MC_ID_SST25VF016 (0x41) +#define MC_ID_SST25VF032 (0x4A) +#define MC_ID_SST25VF064 (0x4B) + +#define BLOCK_SIZE (64*1024) + + +#define SST25_MTD(device) ((struct sst25_mtd*)(device)) +struct sst25_mtd +{ + struct rt_mtd_nor_device parent; + FILE * file; +}; +static struct sst25_mtd _sst25_mtd; + +static struct rt_mutex flash_lock; + +/* RT-Thread MTD device interface */ +static rt_uint32_t sst25vfxx_read_id(struct rt_mtd_nor_device* device) +{ + rt_uint8_t id_recv[3] = {MF_ID, MT_ID, MC_ID_SST25VF016}; + + return (id_recv[0] << 16) | (id_recv[1] << 8) | id_recv[2]; +} + +static int sst25vfxx_read(struct rt_mtd_nor_device* device, rt_off_t position, rt_uint8_t *data, rt_size_t size) +{ + struct sst25_mtd *sst25; + int result; + + sst25 = SST25_MTD(device); + RT_ASSERT(sst25 != RT_NULL); + + rt_mutex_take(&flash_lock, RT_WAITING_FOREVER); + + fseek(sst25->file, position, SEEK_SET); + result = fread(data, size, 1, sst25->file); + if (result < 0) + rt_kprintf("sst read error.\n"); + + rt_mutex_release(&flash_lock); + return size; +} + +static int sst25vfxx_write(struct rt_mtd_nor_device* device, rt_off_t position, + const rt_uint8_t *data, rt_size_t size) +{ + struct sst25_mtd *sst25; + int result; + + sst25 = SST25_MTD(device); + RT_ASSERT(sst25 != RT_NULL); + + rt_mutex_take(&flash_lock, RT_WAITING_FOREVER); + + fseek(sst25->file, position, SEEK_SET); + result = fwrite(data, size, 1, sst25->file); + if (result < 0) + rt_kprintf("sst write error.\n"); + + rt_mutex_release(&flash_lock); + return size; +} + +static char block_buffer[BLOCK_SIZE]; + +static rt_err_t sst25vfxx_erase_block(struct rt_mtd_nor_device* device, rt_uint32_t block) +{ + struct sst25_mtd *sst25; + int result; + + sst25 = SST25_MTD(device); + + RT_ASSERT(sst25 != RT_NULL); + + rt_mutex_take(&flash_lock, RT_WAITING_FOREVER); + + memset(block_buffer, 0xFF, BLOCK_SIZE); + fseek(sst25->file, block, SEEK_SET); + + result = fwrite(block_buffer, BLOCK_SIZE, 1, sst25->file); + if (result < 0) + rt_kprintf("sst write error.\n"); + + rt_mutex_release(&flash_lock); + return RT_EOK; +} + +const static struct rt_mtd_nor_driver_ops sst25vfxx_mtd_ops = +{ + sst25vfxx_read_id, + sst25vfxx_read, + sst25vfxx_write, + sst25vfxx_erase_block, +}; +static rt_err_t sst25vfxx_hw_init(struct sst25_mtd *mtd) +{ + mtd = mtd; + return RT_EOK; +} + +/** + * SST25vfxx API + */ +rt_err_t sst25vfxx_mtd_init(const char * nor_name, + rt_uint32_t block_start, + rt_uint32_t block_end) +{ + rt_uint32_t id, total_block; + struct sst25_mtd * sst25; + struct rt_mtd_nor_device *mtd; + + + sst25 = &_sst25_mtd; + mtd = &(sst25->parent); + + /* set page size and block size */ + mtd->block_size = 64 * 1024; /* 64kByte */ + mtd->ops = &sst25vfxx_mtd_ops; + + /* initialize mutex */ + if (rt_mutex_init(&flash_lock, nor_name, RT_IPC_FLAG_FIFO) != RT_EOK) + { + rt_kprintf("init sd lock mutex failed\n"); + } + + /* initialize flash */ + id = sst25vfxx_read_id(mtd); + switch (id & 0xff) + { + case MC_ID_SST25VF016: + total_block = (16 * 1024 * 1024 / 8) / mtd->block_size; + break; + case MC_ID_SST25VF032: + total_block = (32 * 1024 * 1024 / 8) / mtd->block_size; + break; + case MC_ID_SST25VF064: + total_block = (64 * 1024 * 1024 / 8) / mtd->block_size; + break; + default: + rt_kprintf("SST25 detection error, id: %x\n", id); + return -RT_ERROR; + } + + if ((block_end == RT_UINT32_MAX) || (block_end == 0)) + { + block_end = total_block; + } + else if (block_end > total_block) + { + rt_kprintf("SST25 total block: %d, out of block\n", total_block); + return -RT_ERROR; + } + + mtd->block_start = block_start; + mtd->block_end = block_end; + + /* open nor file, if not exist, then create it */ + sst25->file = fopen(NOR_SIM, "rb+"); + if (sst25->file == NULL) + { + int i; + /* create a file to simulate nor */ + sst25->file = fopen(NOR_SIM, "wb+"); + + memset(block_buffer, 0xFF, sizeof(block_buffer)); + for(i=0; ifile, i * BLOCK_SIZE, SEEK_SET); + fwrite(block_buffer, BLOCK_SIZE, 1, sst25->file); + } + } + + fseek(sst25->file, 0, SEEK_SET); + + /* initialize hardware */ + sst25vfxx_hw_init(&_sst25_mtd); + + /* register MTD device */ + rt_mtd_nor_register_device("nor", mtd); + + return RT_EOK; +} + +#ifdef RT_USING_FINSH +#include +void nor_erase(void) +{ + rt_uint32_t index; + struct rt_mtd_nor_device *mtd; + + mtd = SST25_MTD(&_sst25_mtd); + for (index = mtd->block_start; index < mtd->block_end; index ++) + { + sst25vfxx_erase_block(mtd, index * mtd->block_size); + } +} +FINSH_FUNCTION_EXPORT(nor_erase, erase all block in SPI flash); +#endif + +#endif diff --git a/bsp/simulator/drivers/usart_sim.c b/bsp/simulator/drivers/usart_sim.c new file mode 100644 index 0000000000..d77328bf32 --- /dev/null +++ b/bsp/simulator/drivers/usart_sim.c @@ -0,0 +1,131 @@ +#include +#include +#include +#include +#include "serial.h" + +struct serial_int_rx serial_rx; +extern struct rt_device serial_device; + +/* + * Handler for OSKey Thread + */ +static HANDLE OSKey_Thread; +static DWORD OSKey_ThreadID; + +static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam); +void rt_hw_usart_init(void) +{ + + /* + * create serial thread that revice key input from keyboard + */ + + OSKey_Thread = CreateThread(NULL, + 0, + (LPTHREAD_START_ROUTINE)ThreadforKeyGet, + 0, + CREATE_SUSPENDED, + &OSKey_ThreadID); + if(OSKey_Thread == NULL) + { + //Display Error Message + + return; + } + SetThreadPriority(OSKey_Thread, + THREAD_PRIORITY_NORMAL); + SetThreadPriorityBoost(OSKey_Thread, + TRUE); + SetThreadAffinityMask(OSKey_Thread, + 0x01); + /* + * Start OS get key Thread + */ + ResumeThread(OSKey_Thread); + +} + +/* + * ·½Ïò¼ü(¡û)£º 0xe04b + * ·½Ïò¼ü(¡ü)£º 0xe048 + * ·½Ïò¼ü(¡ú)£º 0xe04d + * ·½Ïò¼ü(¡ý)£º 0xe050 + */ +static int savekey(unsigned char key) +{ + /* save on rx buffer */ + { + rt_base_t level; + + /* disable interrupt */ + //ÔÝʱ¹Ø±ÕÖжϣ¬ÒòΪҪ²Ù×÷uartÊý¾Ý½á¹¹ + level = rt_hw_interrupt_disable(); + + /* save character */ + serial_rx.rx_buffer[serial_rx.save_index] = key; + serial_rx.save_index ++; + //ÏÂÃæµÄ´úÂë¼ì²ésave_indexÊÇ·ñÒѾ­µ½µ½»º³åÇøÎ²²¿£¬Èç¹ûÊÇÔò»Ø×ªµ½Í·²¿£¬³ÆÎªÒ»¸ö»·Ðλº³åÇø + if (serial_rx.save_index >= SERIAL_RX_BUFFER_SIZE) + serial_rx.save_index = 0; + + //ÕâÖÖÇé¿ö±íʾ·´×ªºóµÄsave_index×·ÉÏÁËread_index£¬ÔòÔö´óread_index£¬¶ªÆúÒ»¸ö¾ÉµÄÊý¾Ý + /* if the next position is read index, discard this 'read char' */ + if (serial_rx.save_index == serial_rx.read_index) + { + serial_rx.read_index ++; + if (serial_rx.read_index >= SERIAL_RX_BUFFER_SIZE) + serial_rx.read_index = 0; + } + + /* enable interrupt */ + //uartÊý¾Ý½á¹¹ÒѾ­²Ù×÷Íê³É£¬ÖØÐÂʹÄÜÖÐ¶Ï + rt_hw_interrupt_enable(level); + } + + /* invoke callback */ + if (serial_device.rx_indicate != RT_NULL) + { + rt_size_t rx_length; + + /* get rx length */ + rx_length = serial_rx.read_index > serial_rx.save_index ? + SERIAL_RX_BUFFER_SIZE - serial_rx.read_index + serial_rx.save_index : + serial_rx.save_index - serial_rx.read_index; + + serial_device.rx_indicate(&serial_device, rx_length); + } + return 0; +} +static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam) +{ + unsigned char key; + + (void)lpParam; //prevent compiler warnings + + for(;;) + { + key = _getch();//getchar(); + if (key == 0xE0) + { + key = _getch(); + + if (key == 0x48) //up key , 0x1b 0x5b 0x41 + { + savekey(0x1b); + savekey(0x5b); + savekey(0x41); + } + else if (key == 0x50)//0x1b 0x5b 0x42 + { + savekey(0x1b); + savekey(0x5b); + savekey(0x42); + } + + continue; + } + + savekey(key); + } +} /*** ThreadforKeyGet ***/ \ No newline at end of file -- cgit v1.3.1