format code style with astyle in bsp/simulator

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2535 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
goprife@gmail.com 2012-12-27 08:26:35 +00:00
parent 2e1de690f2
commit 1b274d996f
15 changed files with 1074 additions and 1072 deletions

View File

@ -9,11 +9,11 @@ void rt_platform_init(void)
#if defined(RT_USING_MTD_NAND)
rt_hw_mtd_nand_init();
#endif
#endif
#if defined(RT_USING_MTD_NOR)
sst25vfxx_mtd_init("nor", 0, RT_UINT32_MAX);
#endif
#endif
#endif /* RT_USING_DFS */
@ -24,7 +24,7 @@ void rt_platform_init(void)
#ifdef WIN32
rt_thread_idle_sethook(rt_hw_win32_low_cpu);
rt_thread_idle_sethook(rt_hw_win32_low_cpu);
#endif
rt_thread_delay(50);
rt_device_init_all();

View File

@ -26,67 +26,67 @@
extern int rt_application_init(void);
#ifdef RT_USING_FINSH
extern void finsh_system_init(void);
extern void finsh_set_device(const char* device);
extern void finsh_set_device(const char *device);
#endif
extern rt_uint8_t * heap;
extern rt_uint8_t *heap;
/**
* This function will startup RT-Thread RTOS.
*/
void rtthread_startup(void)
{
/* init board */
rt_hw_board_init();
/* init board */
rt_hw_board_init();
/* show version */
rt_show_version();
/* show version */
rt_show_version();
/* init tick */
rt_system_tick_init();
/* init tick */
rt_system_tick_init();
/* init kernel object */
rt_system_object_init();
/* init kernel object */
rt_system_object_init();
/* init timer system */
rt_system_timer_init();
/* init timer system */
rt_system_timer_init();
#ifdef RT_USING_HEAP
/* init memory system */
rt_system_heap_init((void*)heap, (void*)&heap[HEAP_SIZE-1]);
/* init memory system */
rt_system_heap_init((void *)heap, (void *)&heap[HEAP_SIZE - 1]);
#endif
/* init scheduler system */
rt_system_scheduler_init();
/* init scheduler system */
rt_system_scheduler_init();
/* init all device */
/* init all device */
#ifdef RT_USING_DEVICE
rt_device_init_all();
rt_device_init_all();
#endif
/* init application */
rt_application_init();
/* init application */
rt_application_init();
/* init timer thread */
rt_system_timer_thread_init();
/* init idle thread */
rt_thread_idle_init();
/* init 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

@ -21,23 +21,23 @@
/**
* @addtogroup simulator on win32
*/
rt_uint8_t * heap;
rt_uint8_t *heap;
rt_uint8_t * rt_hw_sram_init(void)
rt_uint8_t *rt_hw_sram_init(void)
{
rt_uint8_t * heap;
heap = malloc(HEAP_SIZE);
if (heap == RT_NULL)
{
rt_kprintf("there is no memory in pc.");
_exit(1);
}
return heap;
rt_uint8_t *heap;
heap = malloc(HEAP_SIZE);
if (heap == RT_NULL)
{
rt_kprintf("there is no memory in pc.");
_exit(1);
}
return heap;
}
void rt_hw_win32_low_cpu(void)
{
Sleep(1000);
Sleep(1000);
}
#if defined(RT_USING_FINSH)
@ -52,24 +52,24 @@ _CRTIMP void __cdecl abort(void);
#include <finsh.h>
void rt_hw_exit(void)
{
rt_kprintf("RT-Thread, bye\n");
exit(0);
rt_kprintf("RT-Thread, bye\n");
exit(0);
}
FINSH_FUNCTION_EXPORT_ALIAS(rt_hw_exit, exit, exit rt-thread);
FINSH_FUNCTION_EXPORT_ALIAS(rt_hw_exit, exit, exit rt - thread);
#endif /* RT_USING_FINSH */
/**
* This function will initial win32
* This function will initial win32
*/
void rt_hw_board_init()
{
/* init system memory */
heap = rt_hw_sram_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);
rt_hw_usart_init();
rt_hw_serial_init();
rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
#endif
}
/*@}*/

View File

@ -15,7 +15,7 @@
#ifndef __BOARD_H__
#define __BOARD_H__
void rt_hw_board_init(void);
rt_uint8_t * rt_hw_sram_init(void);
rt_uint8_t *rt_hw_sram_init(void);
/* SD Card init function */
void rt_hw_sdcard_init(void);

View File

@ -5,38 +5,38 @@
#define NAND_SIM "nand.bin"
#if 1
#define OOB_SIZE 64
#define PAGE_SIZE (2048 + 64)
#define PAGE_PER_BLOCK 64
#define BLOCK_SIZE (PAGE_SIZE * PAGE_PER_BLOCK)
#define BLOCK_NUM 512
// #define BLOCK_NUM 2048
#define OOB_SIZE 64
#define PAGE_SIZE (2048 + 64)
#define PAGE_PER_BLOCK 64
#define BLOCK_SIZE (PAGE_SIZE * PAGE_PER_BLOCK)
#define BLOCK_NUM 512
// #define BLOCK_NUM 2048
#else
#define OOB_SIZE 16
#define PAGE_SIZE (512 + OOB_SIZE)
#define PAGE_PER_BLOCK 32
#define BLOCK_SIZE (PAGE_SIZE * PAGE_PER_BLOCK)
#define BLOCK_NUM 512
#define OOB_SIZE 16
#define PAGE_SIZE (512 + OOB_SIZE)
#define PAGE_PER_BLOCK 32
#define BLOCK_SIZE (PAGE_SIZE * PAGE_PER_BLOCK)
#define BLOCK_NUM 512
#endif
static unsigned char block_data[BLOCK_SIZE];
static struct rt_mtd_nand_device _nanddrv_file_device;
static FILE* file = NULL;
static FILE *file = NULL;
static rt_uint8_t CountBitsInByte(rt_uint8_t byte)
{
rt_uint8_t count = 0;
rt_uint8_t count = 0;
while (byte > 0)
{
if (byte & 1)
{
count++;
}
byte >>= 1;
}
while (byte > 0)
{
if (byte & 1)
{
count++;
}
byte >>= 1;
}
return count;
return count;
}
static void Compute256(const rt_uint8_t *data, rt_uint8_t *code)
@ -50,7 +50,7 @@ static void Compute256(const rt_uint8_t *data, rt_uint8_t *code)
// Xor all bytes together to get the column sum;
// At the same time, calculate the even and odd line codes
for (i=0; i < 256; i++)
for (i = 0; i < 256; i++)
{
columnSum ^= data[i];
@ -93,7 +93,7 @@ static void Compute256(const rt_uint8_t *data, rt_uint8_t *code)
// At this point, we have the line parities, and the column sum. First, We
// must caculate the parity group values on the column sum.
for (i=0; i < 8; i++)
for (i = 0; i < 8; i++)
{
if (columnSum & 1)
{
@ -113,7 +113,7 @@ static void Compute256(const rt_uint8_t *data, rt_uint8_t *code)
code[1] = 0;
code[2] = 0;
for (i=0; i < 4; i++)
for (i = 0; i < 4; i++)
{
code[0] <<= 2;
code[1] <<= 2;
@ -164,11 +164,11 @@ static void Compute256(const rt_uint8_t *data, rt_uint8_t *code)
code[2] = (~(rt_uint32_t)code[2]);
}
void ecc_hamming_compute256x(const rt_uint8_t *pucData, rt_uint32_t dwSize, rt_uint8_t* puCode )
void ecc_hamming_compute256x(const rt_uint8_t *pucData, rt_uint32_t dwSize, rt_uint8_t *puCode)
{
while ( dwSize > 0 )
while (dwSize > 0)
{
Compute256( pucData, puCode ) ;
Compute256(pucData, puCode) ;
pucData += 256;
puCode += 3;
@ -177,222 +177,222 @@ void ecc_hamming_compute256x(const rt_uint8_t *pucData, rt_uint32_t dwSize, rt_u
}
/* read chip id */
static rt_uint32_t nanddrv_file_read_id(struct rt_mtd_nand_device* device)
static rt_uint32_t nanddrv_file_read_id(struct rt_mtd_nand_device *device)
{
return 0x00;
return 0x00;
}
/* read/write/move page */
static rt_err_t nanddrv_file_read_page(struct rt_mtd_nand_device* device,
rt_off_t page,
rt_uint8_t *data, rt_uint32_t data_len,
rt_uint8_t *spare, rt_uint32_t spare_len)
static rt_err_t nanddrv_file_read_page(struct rt_mtd_nand_device *device,
rt_off_t page,
rt_uint8_t *data, rt_uint32_t data_len,
rt_uint8_t *spare, rt_uint32_t spare_len)
{
rt_uint32_t offset;
rt_uint8_t oob_buffer[OOB_SIZE];
rt_uint8_t oob_ecc [OOB_SIZE];
rt_uint32_t offset;
rt_uint8_t oob_buffer[OOB_SIZE];
rt_uint8_t oob_ecc [OOB_SIZE];
page = page + device->block_start * device->pages_per_block;
page = page + device->block_start * device->pages_per_block;
if (page/device->pages_per_block > device->block_end)
{
return -RT_EIO;
}
if (page / device->pages_per_block > device->block_end)
{
return -RT_EIO;
}
/* write page */
offset = page * PAGE_SIZE;
if (data != NULL)
{
fseek(file, offset, SEEK_SET);
fread(data, data_len, 1, file);
}
/* write page */
offset = page * PAGE_SIZE;
if (data != NULL)
{
fseek(file, offset, SEEK_SET);
fread(data, data_len, 1, file);
}
offset = page * PAGE_SIZE + (PAGE_SIZE - OOB_SIZE);
fseek(file, offset, SEEK_SET);
fread(oob_buffer, OOB_SIZE, 1, file);
if (spare != NULL)
{
memcpy(spare, oob_buffer, spare_len);
}
offset = page * PAGE_SIZE + (PAGE_SIZE - OOB_SIZE);
fseek(file, offset, SEEK_SET);
fread(oob_buffer, OOB_SIZE, 1, file);
if (spare != NULL)
{
memcpy(spare, oob_buffer, spare_len);
}
/* verify ECC */
if (data != RT_NULL)
{
ecc_hamming_compute256x(data, PAGE_SIZE - OOB_SIZE, &oob_ecc[0]);
if (memcmp(&oob_ecc[0], &oob_buffer[0], OOB_SIZE - device->oob_free) != 0) return -RT_MTD_EECC;
}
/* verify ECC */
if (data != RT_NULL)
{
ecc_hamming_compute256x(data, PAGE_SIZE - OOB_SIZE, &oob_ecc[0]);
if (memcmp(&oob_ecc[0], &oob_buffer[0], OOB_SIZE - device->oob_free) != 0) return -RT_MTD_EECC;
}
return RT_EOK;
return RT_EOK;
}
static rt_err_t nanddrv_file_write_page(struct rt_mtd_nand_device* device,
rt_off_t page,
const rt_uint8_t* data, rt_uint32_t data_len,
const rt_uint8_t* oob, rt_uint32_t spare_len)
static rt_err_t nanddrv_file_write_page(struct rt_mtd_nand_device *device,
rt_off_t page,
const rt_uint8_t *data, rt_uint32_t data_len,
const rt_uint8_t *oob, rt_uint32_t spare_len)
{
rt_uint32_t offset;
rt_uint8_t oob_buffer[OOB_SIZE];
rt_uint32_t offset;
rt_uint8_t oob_buffer[OOB_SIZE];
page = page + device->block_start * device->pages_per_block;
if (page/device->pages_per_block > device->block_end)
{
return -RT_EIO;
}
page = page + device->block_start * device->pages_per_block;
if (page / device->pages_per_block > device->block_end)
{
return -RT_EIO;
}
/* write page */
offset = page * PAGE_SIZE;
if (data != NULL)
{
fseek(file, offset, SEEK_SET);
fwrite(data, PAGE_SIZE - OOB_SIZE, 1, file);
}
/* write page */
offset = page * PAGE_SIZE;
if (data != NULL)
{
fseek(file, offset, SEEK_SET);
fwrite(data, PAGE_SIZE - OOB_SIZE, 1, file);
}
offset = page * PAGE_SIZE + (PAGE_SIZE - OOB_SIZE);
fseek(file, offset, SEEK_SET);
offset = page * PAGE_SIZE + (PAGE_SIZE - OOB_SIZE);
fseek(file, offset, SEEK_SET);
memset(oob_buffer, 0xff, sizeof(oob_buffer));
ecc_hamming_compute256x(data, PAGE_SIZE - OOB_SIZE, &oob_buffer[0]);
if (oob != RT_NULL)
{
memcpy(&oob_buffer[OOB_SIZE - device->oob_free],
&oob[OOB_SIZE - device->oob_free],
device->oob_free);
}
fwrite(oob_buffer, OOB_SIZE, 1, file);
memset(oob_buffer, 0xff, sizeof(oob_buffer));
ecc_hamming_compute256x(data, PAGE_SIZE - OOB_SIZE, &oob_buffer[0]);
if (oob != RT_NULL)
{
memcpy(&oob_buffer[OOB_SIZE - device->oob_free],
&oob[OOB_SIZE - device->oob_free],
device->oob_free);
}
fwrite(oob_buffer, OOB_SIZE, 1, file);
return RT_EOK;
return RT_EOK;
}
static rt_err_t nanddrv_file_move_page(struct rt_mtd_nand_device* device, rt_off_t from, rt_off_t to)
static rt_err_t nanddrv_file_move_page(struct rt_mtd_nand_device *device, rt_off_t from, rt_off_t to)
{
rt_uint32_t offset;
rt_uint8_t page_buffer[PAGE_SIZE - OOB_SIZE];
rt_uint8_t oob_buffer[OOB_SIZE];
rt_uint32_t offset;
rt_uint8_t page_buffer[PAGE_SIZE - OOB_SIZE];
rt_uint8_t oob_buffer[OOB_SIZE];
from = from + device->block_start * device->pages_per_block;
to = to + device->block_start * device->pages_per_block;
from = from + device->block_start * device->pages_per_block;
to = to + device->block_start * device->pages_per_block;
if (from/device->pages_per_block > device->block_end ||
to/device->pages_per_block > device->block_end)
{
return -RT_EIO;
}
if (from / device->pages_per_block > device->block_end ||
to / device->pages_per_block > device->block_end)
{
return -RT_EIO;
}
if (device->plane_num > 1)
{
rt_uint32_t mask;
rt_uint16_t from_block, to_block;
if (device->plane_num > 1)
{
rt_uint32_t mask;
rt_uint16_t from_block, to_block;
from_block = (rt_uint16_t)(from / PAGE_PER_BLOCK);
to_block = (rt_uint16_t)(to / PAGE_PER_BLOCK);
mask = device->plane_num - 1;
from_block = (rt_uint16_t)(from / PAGE_PER_BLOCK);
to_block = (rt_uint16_t)(to / PAGE_PER_BLOCK);
mask = device->plane_num - 1;
if ((from_block & mask) != (to_block & mask))
{
rt_kprintf("invalid page copy on the block. from [%d] --> to[%d]\n", from_block, to_block);
return -RT_EIO;
}
}
if ((from_block & mask) != (to_block & mask))
{
rt_kprintf("invalid page copy on the block. from [%d] --> to[%d]\n", from_block, to_block);
return -RT_EIO;
}
}
/* read page */
offset = from * PAGE_SIZE;
fseek(file, offset, SEEK_SET);
fread(page_buffer, sizeof(page_buffer), 1, file);
fread(oob_buffer, sizeof(oob_buffer), 1, file);
/* read page */
offset = from * PAGE_SIZE;
fseek(file, offset, SEEK_SET);
fread(page_buffer, sizeof(page_buffer), 1, file);
fread(oob_buffer, sizeof(oob_buffer), 1, file);
/* write page */
offset = to * PAGE_SIZE;
fseek(file, offset, SEEK_SET);
fwrite(page_buffer, sizeof(page_buffer), 1, file);
fwrite(oob_buffer, sizeof(oob_buffer), 1, file);
/* write page */
offset = to * PAGE_SIZE;
fseek(file, offset, SEEK_SET);
fwrite(page_buffer, sizeof(page_buffer), 1, file);
fwrite(oob_buffer, sizeof(oob_buffer), 1, file);
return RT_EOK;
return RT_EOK;
}
/* erase block */
static rt_err_t nanddrv_file_erase_block(struct rt_mtd_nand_device* device, rt_uint32_t block)
static rt_err_t nanddrv_file_erase_block(struct rt_mtd_nand_device *device, rt_uint32_t block)
{
if (block > BLOCK_NUM) return -RT_EIO;
if (block > BLOCK_NUM) return -RT_EIO;
/* add the start blocks */
block = block + device->block_start * device->pages_per_block;
/* add the start blocks */
block = block + device->block_start * device->pages_per_block;
fseek(file, block * BLOCK_SIZE, SEEK_SET );
fwrite(block_data, sizeof(block_data), 1, file);
fseek(file, block * BLOCK_SIZE, SEEK_SET);
fwrite(block_data, sizeof(block_data), 1, file);
return RT_EOK;
return RT_EOK;
}
const static struct rt_mtd_nand_driver_ops _ops =
const static struct rt_mtd_nand_driver_ops _ops =
{
nanddrv_file_read_id,
nanddrv_file_read_page,
nanddrv_file_write_page,
nanddrv_file_move_page,
nanddrv_file_erase_block
nanddrv_file_read_id,
nanddrv_file_read_page,
nanddrv_file_write_page,
nanddrv_file_move_page,
nanddrv_file_erase_block
};
void nand_eraseall(void);
void rt_hw_mtd_nand_init(void)
{
rt_uint16_t ecc_size;
rt_uint32_t size;
rt_uint16_t ecc_size;
rt_uint32_t size;
memset(block_data, 0xff, sizeof(block_data));
/* open file */
file = fopen(NAND_SIM, "rb+");
if (file == NULL)
{
file = fopen(NAND_SIM, "wb+");
}
fseek(file, 0, SEEK_END);
size = ftell(file);
memset(block_data, 0xff, sizeof(block_data));
/* open file */
file = fopen(NAND_SIM, "rb+");
if (file == NULL)
{
file = fopen(NAND_SIM, "wb+");
}
fseek(file, 0, SEEK_END);
size = ftell(file);
fseek(file, 0, SEEK_SET );
if (size < BLOCK_NUM * BLOCK_SIZE)
{
rt_uint32_t index;
fseek(file, 0, SEEK_SET );
for (index = 0; index < BLOCK_NUM; index ++)
{
fwrite(block_data, sizeof(block_data), 1, file);
}
}
fseek(file, 0, SEEK_SET );
fseek(file, 0, SEEK_SET);
if (size < BLOCK_NUM * BLOCK_SIZE)
{
rt_uint32_t index;
fseek(file, 0, SEEK_SET);
for (index = 0; index < BLOCK_NUM; index ++)
{
fwrite(block_data, sizeof(block_data), 1, file);
}
}
fseek(file, 0, SEEK_SET);
ecc_size = (PAGE_SIZE - OOB_SIZE) * 3/256;
_nanddrv_file_device.plane_num = 2;
_nanddrv_file_device.oob_size = OOB_SIZE;
_nanddrv_file_device.oob_free = OOB_SIZE - ecc_size;
_nanddrv_file_device.page_size = PAGE_SIZE - OOB_SIZE;
_nanddrv_file_device.pages_per_block = PAGE_PER_BLOCK;
_nanddrv_file_device.block_start = 0;
_nanddrv_file_device.block_end = BLOCK_NUM/2;
_nanddrv_file_device.block_total = _nanddrv_file_device.block_end - _nanddrv_file_device.block_start;
_nanddrv_file_device.ops = &_ops;
ecc_size = (PAGE_SIZE - OOB_SIZE) * 3 / 256;
_nanddrv_file_device.plane_num = 2;
_nanddrv_file_device.oob_size = OOB_SIZE;
_nanddrv_file_device.oob_free = OOB_SIZE - ecc_size;
_nanddrv_file_device.page_size = PAGE_SIZE - OOB_SIZE;
_nanddrv_file_device.pages_per_block = PAGE_PER_BLOCK;
_nanddrv_file_device.block_start = 0;
_nanddrv_file_device.block_end = BLOCK_NUM / 2;
_nanddrv_file_device.block_total = _nanddrv_file_device.block_end - _nanddrv_file_device.block_start;
_nanddrv_file_device.ops = &_ops;
rt_mtd_nand_register_device("nand0", &_nanddrv_file_device);
rt_mtd_nand_register_device("nand0", &_nanddrv_file_device);
}
#if defined(RT_USING_FINSH)
#include <finsh.h>
void nand_eraseall()
{
int index;
for (index = 0; index < _nanddrv_file_device.block_total; index ++)
{
nanddrv_file_erase_block(&_nanddrv_file_device, index);
}
int index;
for (index = 0; index < _nanddrv_file_device.block_total; index ++)
{
nanddrv_file_erase_block(&_nanddrv_file_device, index);
}
}
FINSH_FUNCTION_EXPORT(nand_eraseall, erase all of block in the nand flash);
#if 0
void nand_log(int level)
{
nftl_set_trace_level(level);
nftl_set_trace_level(level);
}
FINSH_FUNCTION_EXPORT(nand_log, set NFTL trace level);
#endif
#endif
#endif //RT_USING_FINSH

View File

@ -4,7 +4,7 @@
#include <string.h>
#include <dfs_def.h>
// #define SD_TRACE rt_kprintf
// #define SD_TRACE rt_kprintf
#define SD_TRACE(...)
//#define SDCARD_SIM "F:\\Project\\tools\\SDCARD"
@ -13,12 +13,12 @@
struct sdcard_device
{
struct rt_device parent;
FILE* file;
struct rt_device parent;
FILE *file;
};
static struct sdcard_device _sdcard;
#define SDCARD_DEVICE(device) (( struct sdcard_device*)(device))
#define SDCARD_DEVICE(device) (( struct sdcard_device*)(device))
static rt_mutex_t lock;
@ -26,79 +26,79 @@ static rt_mutex_t lock;
static rt_err_t rt_sdcard_init(rt_device_t dev)
{
return RT_EOK;
return RT_EOK;
}
static rt_err_t rt_sdcard_open(rt_device_t dev, rt_uint16_t oflag)
{
return RT_EOK;
return RT_EOK;
}
static rt_err_t rt_sdcard_close(rt_device_t dev)
{
return RT_EOK;
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)
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;
struct sdcard_device *sd;
int result = 0;
SD_TRACE("sd read: pos %d, size %d\n", position, size);
SD_TRACE("sd read: pos %d, size %d\n", position, size);
rt_mutex_take(lock, RT_WAITING_FOREVER);
rt_mutex_take(lock, RT_WAITING_FOREVER);
sd = SDCARD_DEVICE(device);
fseek(sd->file, position * SECTOR_SIZE, SEEK_SET);
fseek(sd->file, position * SECTOR_SIZE, SEEK_SET);
result = fread(buffer, size * SECTOR_SIZE, 1, sd->file);
if (result < 0)
goto _err;
result = fread(buffer, size * SECTOR_SIZE, 1, sd->file);
if (result < 0)
goto _err;
rt_mutex_release(lock);
rt_mutex_release(lock);
return size;
_err:
SD_TRACE("sd read errors!\n");
rt_mutex_release(lock);
return 0;
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)
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;
struct sdcard_device *sd;
int result = 0;
SD_TRACE("sst write: pos %d, size %d\n", position, size);
SD_TRACE("sst write: pos %d, size %d\n", position, size);
rt_mutex_take(lock, RT_WAITING_FOREVER);
rt_mutex_take(lock, RT_WAITING_FOREVER);
sd = SDCARD_DEVICE(device);
fseek(sd->file, position * SECTOR_SIZE, SEEK_SET);
fseek(sd->file, position * SECTOR_SIZE, SEEK_SET);
result = fwrite(buffer, size * SECTOR_SIZE, 1, sd->file);
if (result < 0)
goto _err;
result = fwrite(buffer, size * SECTOR_SIZE, 1, sd->file);
if (result < 0)
goto _err;
rt_mutex_release(lock);
rt_mutex_release(lock);
return size;
_err:
SD_TRACE("sd write errors!\n");
rt_mutex_release(lock);
return 0;
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;
struct sdcard_device *sd;
unsigned int size;
RT_ASSERT(dev != RT_NULL);
@ -114,72 +114,72 @@ static rt_err_t rt_sdcard_control(rt_device_t dev, rt_uint8_t cmd, void *args)
geometry->bytes_per_sector = SECTOR_SIZE;
geometry->block_size = SECTOR_SIZE;
fseek(sd->file, 0, SEEK_END);
size = ftell(sd->file);
fseek(sd->file, 0, SEEK_END);
size = ftell(sd->file);
geometry->sector_count = size/SECTOR_SIZE;
geometry->sector_count = size / SECTOR_SIZE;
}
return RT_EOK;
return RT_EOK;
}
rt_err_t rt_hw_sdcard_init(const char * spi_device_name)
rt_err_t rt_hw_sdcard_init(const char *spi_device_name)
{
int size;
int size;
rt_uint32_t id, total_block;
struct sdcard_device * sd;
struct rt_device * device;
struct sdcard_device *sd;
struct rt_device *device;
sd = &_sdcard;
device = &(sd->parent);
device = &(sd->parent);
lock = rt_mutex_create("lock", RT_IPC_FLAG_FIFO);
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+");
/* 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_END);
size = ftell(sd->file);
fseek(sd->file, 0, SEEK_SET );
if (size < SDCARD_SIZE)
{
int i;
unsigned char* ptr;
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);
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);
fseek(sd->file, 0, SEEK_SET);
for(i=0; i<(SDCARD_SIZE / (1024*1024)); i++)
fwrite(ptr, 1024 * 1024, 1, sd->file);
for (i = 0; i < (SDCARD_SIZE / (1024 * 1024)); i++)
fwrite(ptr, 1024 * 1024, 1, sd->file);
free(ptr);
}
}
fseek(sd->file, 0, SEEK_SET);
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;
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);
rt_device_register(device, "sd0",
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE);
return RT_EOK;
}
@ -188,7 +188,7 @@ rt_err_t rt_hw_sdcard_init(const char * spi_device_name)
#include <finsh.h>
void eraseall(void)
{
printf("had not implemented yet!\n");
printf("had not implemented yet!\n");
}
FINSH_FUNCTION_EXPORT(eraseall, erase all block in SPI flash);
#endif

View File

@ -4,132 +4,132 @@
#include <rtdevice.h>
#include <rtgui/driver.h>
#define SDL_SCREEN_WIDTH 800
#define SDL_SCREEN_HEIGHT 480
#define SDL_SCREEN_WIDTH 800
#define SDL_SCREEN_HEIGHT 480
struct sdlfb_device
{
struct rt_device parent;
struct rt_device parent;
SDL_Surface *screen;
rt_uint16_t width;
rt_uint16_t height;
SDL_Surface *screen;
rt_uint16_t width;
rt_uint16_t height;
};
struct sdlfb_device _device;
/* common device interface */
static rt_err_t sdlfb_init(rt_device_t dev)
{
return RT_EOK;
return RT_EOK;
}
static rt_err_t sdlfb_open(rt_device_t dev, rt_uint16_t oflag)
{
return RT_EOK;
return RT_EOK;
}
static rt_err_t sdlfb_close(rt_device_t dev)
{
SDL_Quit();
return RT_EOK;
SDL_Quit();
return RT_EOK;
}
static rt_mutex_t sdllock;
static rt_err_t sdlfb_control(rt_device_t dev, rt_uint8_t cmd, void *args)
{
struct sdlfb_device *device;
struct sdlfb_device *device;
rt_mutex_take(sdllock, RT_WAITING_FOREVER);
device = (struct sdlfb_device*)dev;
RT_ASSERT(device != RT_NULL);
RT_ASSERT(device->screen != RT_NULL);
rt_mutex_take(sdllock, RT_WAITING_FOREVER);
device = (struct sdlfb_device *)dev;
RT_ASSERT(device != RT_NULL);
RT_ASSERT(device->screen != RT_NULL);
switch (cmd)
{
case RTGRAPHIC_CTRL_GET_INFO:
{
struct rt_device_graphic_info *info;
switch (cmd)
{
case RTGRAPHIC_CTRL_GET_INFO:
{
struct rt_device_graphic_info *info;
info = (struct rt_device_graphic_info*) args;
info->bits_per_pixel = 16;
info->pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565P;
info->framebuffer = device->screen->pixels;
info->width = device->screen->w;
info->height = device->screen->h;
}
break;
case RTGRAPHIC_CTRL_RECT_UPDATE:
{
struct rt_device_rect_info *rect;
rect = (struct rt_device_rect_info*)args;
info = (struct rt_device_graphic_info *) args;
info->bits_per_pixel = 16;
info->pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565P;
info->framebuffer = device->screen->pixels;
info->width = device->screen->w;
info->height = device->screen->h;
}
break;
case RTGRAPHIC_CTRL_RECT_UPDATE:
{
struct rt_device_rect_info *rect;
rect = (struct rt_device_rect_info *)args;
/* SDL_UpdateRect(_device.screen, rect->x, rect->y, rect->x + rect->w, rect->y + rect->h); */
SDL_UpdateRect(_device.screen, 0, 0, device->width, device->height);
}
break;
case RTGRAPHIC_CTRL_SET_MODE:
{
/* SDL_UpdateRect(_device.screen, rect->x, rect->y, rect->x + rect->w, rect->y + rect->h); */
SDL_UpdateRect(_device.screen, 0, 0, device->width, device->height);
}
break;
case RTGRAPHIC_CTRL_SET_MODE:
{
#if 0
struct rt_device_rect_info* rect;
struct rt_device_rect_info *rect;
rect = (struct rt_device_rect_info*)args;
if ((_device.width == rect->width) && (_device.height == rect->height)) return -RT_ERROR;
_device.width = rect->width;
_device.height = rect->height;
if (_device.screen != RT_NULL)
{
SDL_FreeSurface(_device.screen);
/* re-create screen surface */
_device.screen = SDL_SetVideoMode(_device.width, _device.height, 16, SDL_SWSURFACE | SDL_DOUBLEBUF);
if ( _device.screen == NULL )
{
fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
exit(1);
}
rect = (struct rt_device_rect_info *)args;
if ((_device.width == rect->width) && (_device.height == rect->height)) return -RT_ERROR;
SDL_WM_SetCaption ("RT-Thread/GUI Simulator", NULL);
}
_device.width = rect->width;
_device.height = rect->height;
if (_device.screen != RT_NULL)
{
SDL_FreeSurface(_device.screen);
/* re-create screen surface */
_device.screen = SDL_SetVideoMode(_device.width, _device.height, 16, SDL_SWSURFACE | SDL_DOUBLEBUF);
if (_device.screen == NULL)
{
fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
exit(1);
}
SDL_WM_SetCaption("RT-Thread/GUI Simulator", NULL);
}
#endif
}
break;
}
rt_mutex_release(sdllock);
return RT_EOK;
}
break;
}
rt_mutex_release(sdllock);
return RT_EOK;
}
static void sdlfb_hw_init(void)
{
/* set video driver for VC++ debug */
//_putenv("SDL_VIDEODRIVER=windib");
/* set video driver for VC++ debug */
//_putenv("SDL_VIDEODRIVER=windib");
//if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO) < 0)
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
//if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO) < 0)
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1);
}
_device.parent.init = sdlfb_init;
_device.parent.open = sdlfb_open;
_device.parent.close = sdlfb_close;
_device.parent.read = RT_NULL;
_device.parent.write = RT_NULL;
_device.parent.control = sdlfb_control;
_device.parent.init = sdlfb_init;
_device.parent.open = sdlfb_open;
_device.parent.close = sdlfb_close;
_device.parent.read = RT_NULL;
_device.parent.write = RT_NULL;
_device.parent.control = sdlfb_control;
_device.width = SDL_SCREEN_WIDTH;
_device.height = SDL_SCREEN_HEIGHT;
_device.screen = SDL_SetVideoMode(_device.width, _device.height, 16, SDL_SWSURFACE | SDL_DOUBLEBUF);
_device.width = SDL_SCREEN_WIDTH;
_device.height = SDL_SCREEN_HEIGHT;
_device.screen = SDL_SetVideoMode(_device.width, _device.height, 16, SDL_SWSURFACE | SDL_DOUBLEBUF);
if (_device.screen == NULL)
{
{
fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
exit(1);
}
SDL_WM_SetCaption ("RT-Thread/GUI Simulator", NULL);
rt_device_register(RT_DEVICE(&_device), "sdl", RT_DEVICE_FLAG_RDWR);
SDL_WM_SetCaption("RT-Thread/GUI Simulator", NULL);
rt_device_register(RT_DEVICE(&_device), "sdl", RT_DEVICE_FLAG_RDWR);
sdllock = rt_mutex_create("fb",RT_IPC_FLAG_FIFO);
sdllock = rt_mutex_create("fb", RT_IPC_FLAG_FIFO);
}
#include <windows.h>
@ -143,158 +143,158 @@ static void sdlfb_hw_init(void)
static DWORD WINAPI sdl_loop(LPVOID lpParam)
{
int quit = 0;
SDL_Event event;
int button_state = 0;
int quit = 0;
SDL_Event event;
int button_state = 0;
rt_device_t device;
sdlfb_hw_init();
rt_device_t device;
sdlfb_hw_init();
device = rt_device_find("sdl");
rtgui_graphic_set_device(device);
device = rt_device_find("sdl");
rtgui_graphic_set_device(device);
/* handle SDL event */
while(!quit)
{
SDL_WaitEvent(&event);
/* handle SDL event */
while (!quit)
{
SDL_WaitEvent(&event);
switch (event.type)
{
case SDL_MOUSEMOTION:
switch (event.type)
{
case SDL_MOUSEMOTION:
#if 0
{
struct rtgui_event_mouse emouse;
emouse.parent.type = RTGUI_EVENT_MOUSE_MOTION;
emouse.parent.sender = RT_NULL;
emouse.wid = RT_NULL;
{
struct rtgui_event_mouse emouse;
emouse.parent.type = RTGUI_EVENT_MOUSE_MOTION;
emouse.parent.sender = RT_NULL;
emouse.wid = RT_NULL;
emouse.x = ((SDL_MouseMotionEvent*)&event)->x;
emouse.y = ((SDL_MouseMotionEvent*)&event)->y;
emouse.x = ((SDL_MouseMotionEvent *)&event)->x;
emouse.y = ((SDL_MouseMotionEvent *)&event)->y;
/* init mouse button */
emouse.button = button_state;
/* init mouse button */
emouse.button = button_state;
/* send event to server */
rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
}
/* send event to server */
rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
}
#endif
break;
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
{
struct rtgui_event_mouse emouse;
SDL_MouseButtonEvent* mb;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
{
struct rtgui_event_mouse emouse;
SDL_MouseButtonEvent *mb;
emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
emouse.parent.sender = RT_NULL;
emouse.wid = RT_NULL;
emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
emouse.parent.sender = RT_NULL;
emouse.wid = RT_NULL;
mb = (SDL_MouseButtonEvent*)&event;
mb = (SDL_MouseButtonEvent *)&event;
emouse.x = mb->x;
emouse.y = mb->y;
emouse.x = mb->x;
emouse.y = mb->y;
/* init mouse button */
emouse.button = 0;
/* init mouse button */
emouse.button = 0;
/* set emouse button */
if (mb->button & (1 << (SDL_BUTTON_LEFT - 1)) )
{
emouse.button |= RTGUI_MOUSE_BUTTON_LEFT;
}
else if (mb->button & (1 << (SDL_BUTTON_RIGHT - 1)))
{
emouse.button |= RTGUI_MOUSE_BUTTON_RIGHT;
}
else if (mb->button & (1 << (SDL_BUTTON_MIDDLE - 1)))
{
emouse.button |= RTGUI_MOUSE_BUTTON_MIDDLE;
}
/* set emouse button */
if (mb->button & (1 << (SDL_BUTTON_LEFT - 1)))
{
emouse.button |= RTGUI_MOUSE_BUTTON_LEFT;
}
else if (mb->button & (1 << (SDL_BUTTON_RIGHT - 1)))
{
emouse.button |= RTGUI_MOUSE_BUTTON_RIGHT;
}
else if (mb->button & (1 << (SDL_BUTTON_MIDDLE - 1)))
{
emouse.button |= RTGUI_MOUSE_BUTTON_MIDDLE;
}
if (mb->type == SDL_MOUSEBUTTONDOWN)
{
emouse.button |= RTGUI_MOUSE_BUTTON_DOWN;
button_state = emouse.button;
}
else
{
emouse.button |= RTGUI_MOUSE_BUTTON_UP;
button_state = 0;
}
if (mb->type == SDL_MOUSEBUTTONDOWN)
{
emouse.button |= RTGUI_MOUSE_BUTTON_DOWN;
button_state = emouse.button;
}
else
{
emouse.button |= RTGUI_MOUSE_BUTTON_UP;
button_state = 0;
}
/* send event to server */
rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
}
break;
/* send event to server */
rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
}
break;
case SDL_KEYUP:
{
struct rtgui_event_kbd ekbd;
ekbd.parent.type = RTGUI_EVENT_KBD;
ekbd.parent.sender = RT_NULL;
ekbd.type = RTGUI_KEYUP;
ekbd.wid = RT_NULL;
ekbd.mod = event.key.keysym.mod;
ekbd.key = event.key.keysym.sym;
case SDL_KEYUP:
{
struct rtgui_event_kbd ekbd;
ekbd.parent.type = RTGUI_EVENT_KBD;
ekbd.parent.sender = RT_NULL;
ekbd.type = RTGUI_KEYUP;
ekbd.wid = RT_NULL;
ekbd.mod = event.key.keysym.mod;
ekbd.key = event.key.keysym.sym;
/* FIXME: unicode */
ekbd.unicode = 0;
/* FIXME: unicode */
ekbd.unicode = 0;
/* send event to server */
rtgui_server_post_event(&ekbd.parent, sizeof(struct rtgui_event_kbd));
}
break;
/* send event to server */
rtgui_server_post_event(&ekbd.parent, sizeof(struct rtgui_event_kbd));
}
break;
case SDL_KEYDOWN:
{
struct rtgui_event_kbd ekbd;
ekbd.parent.type = RTGUI_EVENT_KBD;
ekbd.parent.sender = RT_NULL;
ekbd.type = RTGUI_KEYDOWN;
ekbd.wid = RT_NULL;
ekbd.mod = event.key.keysym.mod;
ekbd.key = event.key.keysym.sym;
case SDL_KEYDOWN:
{
struct rtgui_event_kbd ekbd;
ekbd.parent.type = RTGUI_EVENT_KBD;
ekbd.parent.sender = RT_NULL;
ekbd.type = RTGUI_KEYDOWN;
ekbd.wid = RT_NULL;
ekbd.mod = event.key.keysym.mod;
ekbd.key = event.key.keysym.sym;
/* FIXME: unicode */
ekbd.unicode = 0;
/* FIXME: unicode */
ekbd.unicode = 0;
/* send event to server */
rtgui_server_post_event(&ekbd.parent, sizeof(struct rtgui_event_kbd));
}
break;
/* send event to server */
rtgui_server_post_event(&ekbd.parent, sizeof(struct rtgui_event_kbd));
}
break;
case SDL_QUIT:
SDL_Quit();
quit = 1;
break;
case SDL_QUIT:
SDL_Quit();
quit = 1;
break;
default:
break;
}
default:
break;
}
if (quit)
break;
}
//exit(0);
return 0;
if (quit)
break;
}
//exit(0);
return 0;
}
/* start sdl thread */
void rt_hw_sdl_start(void)
{
HANDLE thread;
DWORD thread_id;
HANDLE thread;
DWORD thread_id;
/* create thread that loop sdl event */
/* create thread that loop sdl event */
thread = CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE)sdl_loop,
(LPTHREAD_START_ROUTINE)sdl_loop,
0,
CREATE_SUSPENDED,
&thread_id);
if(thread == NULL)
if (thread == NULL)
{
//Display Error Message

View File

@ -1,7 +1,7 @@
/*
******************************************************************************
* By : parai
* email:parai@foxmail.com
* By : parai
* email:parai@foxmail.com
*
******************************************************************************
*/
@ -20,141 +20,143 @@ extern struct serial_int_rx serial_rx;
/**
* This function initializes serial
*/
static rt_err_t rt_serial_init (rt_device_t dev)
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;
}
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;
dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
}
return RT_EOK;
}
static rt_err_t rt_serial_open(rt_device_t dev, rt_uint16_t oflag){
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");
printf("in rt_serial_open()\n");
#endif
return RT_EOK;
return RT_EOK;
}
static rt_err_t rt_serial_close(rt_device_t dev)
{
#if _DEBUG_SERIAL==1
printf("in rt_serial_close()\n");
printf("in rt_serial_close()\n");
#endif
return RT_EOK;
return RT_EOK;
}
static rt_size_t rt_serial_read (rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
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;
rt_uint8_t *ptr;
rt_err_t err_code;
ptr = buffer;
err_code = RT_EOK;
ptr = buffer;
err_code = RT_EOK;
if (dev->flag & RT_DEVICE_FLAG_INT_RX)
{
/* interrupt mode Rx */
while (size)
{
rt_base_t level;
if (dev->flag & RT_DEVICE_FLAG_INT_RX)
{
/* interrupt mode Rx */
while (size)
{
rt_base_t level;
/* disable interrupt */
level = rt_hw_interrupt_disable();
/* 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--;
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;
/* 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);
break;
}
/* enable interrupt */
rt_hw_interrupt_enable(level);
}
}
/* enable interrupt */
rt_hw_interrupt_enable(level);
}
}
/* set error code */
rt_set_errno(err_code);
return (rt_uint32_t)ptr - (rt_uint32_t)buffer;
/* 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)
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");
printf("in rt_serial_write()\n");
#endif
printf("%s",(char*)buffer);
return size;
printf("%s", (char *)buffer);
return size;
}
static rt_err_t rt_serial_control (rt_device_t dev, rt_uint8_t cmd, void *args)
static rt_err_t rt_serial_control(rt_device_t dev, rt_uint8_t cmd, void *args)
{
RT_ASSERT(dev != RT_NULL);
RT_ASSERT(dev != RT_NULL);
switch (cmd){
case RT_DEVICE_CTRL_SUSPEND:
/* suspend device */
dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
break;
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;
}
case RT_DEVICE_CTRL_RESUME:
/* resume device */
dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
break;
}
return RT_EOK;
return RT_EOK;
}
/*
* serial register
*/
static rt_err_t rt_hw_serial_register(rt_device_t device, const char* name, rt_uint32_t flag)
static rt_err_t rt_hw_serial_register(rt_device_t device, const char *name, rt_uint32_t flag)
{
RT_ASSERT(device != RT_NULL);
RT_ASSERT(device != RT_NULL);
#if _DEBUG_SERIAL==1
printf("in rt_serial_register()\n");
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;
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);
/* 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,RT_CONSOLE_DEVICE_NAME,
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM);
return rt_hw_serial_register(&serial_device, RT_CONSOLE_DEVICE_NAME,
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM);
}

View File

@ -4,8 +4,8 @@
* BANKED MEMORY MODEL
*
* File : rthw.c
* By : parai
* email:parai@foxmail.com
* By : parai
* email:parai@foxmail.com
*******************************************************************************************************/
#ifndef __RT_HW_SERIAL_H__
@ -14,8 +14,8 @@
#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_uint8_t rx_buffer[SERIAL_RX_BUFFER_SIZE];
rt_uint32_t read_index, save_index;
};
rt_err_t rt_hw_serial_init(void);

View File

@ -1,18 +1,18 @@
/*
* 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
/*
* 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
@ -21,4 +21,4 @@
rt_err_t sst25vfxx_mtd_init(const char *spi_device_name, rt_uint32_t block_start, rt_uint32_t block_end);
#endif
#endif

View File

@ -32,179 +32,179 @@
#define BLOCK_SIZE (64*1024)
#define SST25_MTD(device) ((struct sst25_mtd*)(device))
#define SST25_MTD(device) ((struct sst25_mtd*)(device))
struct sst25_mtd
{
struct rt_mtd_nor_device parent;
FILE * file;
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)
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};
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];
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)
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;
int result;
sst25 = SST25_MTD(device);
RT_ASSERT(sst25 != RT_NULL);
rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
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");
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);
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)
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;
int result;
sst25 = SST25_MTD(device);
RT_ASSERT(sst25 != RT_NULL);
rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
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");
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);
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)
static rt_err_t sst25vfxx_erase_block(struct rt_mtd_nor_device *device, rt_uint32_t block)
{
struct sst25_mtd *sst25;
int result;
int result;
sst25 = SST25_MTD(device);
RT_ASSERT(sst25 != RT_NULL);
rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
memset(block_buffer, 0xFF, BLOCK_SIZE);
fseek(sst25->file, block, SEEK_SET);
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");
result = fwrite(block_buffer, BLOCK_SIZE, 1, sst25->file);
if (result < 0)
rt_kprintf("sst write error.\n");
rt_mutex_release(&flash_lock);
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,
sst25vfxx_read_id,
sst25vfxx_read,
sst25vfxx_write,
sst25vfxx_erase_block,
};
static rt_err_t sst25vfxx_hw_init(struct sst25_mtd *mtd)
{
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_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 sst25_mtd *sst25;
struct rt_mtd_nor_device *mtd;
sst25 = &_sst25_mtd;
mtd = &(sst25->parent);
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");
}
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;
total_block = (16 * 1024 * 1024 / 8) / mtd->block_size;
break;
case MC_ID_SST25VF032:
total_block = (32 * 1024 * 1024 / 8) / mtd->block_size;
break;
total_block = (32 * 1024 * 1024 / 8) / mtd->block_size;
break;
case MC_ID_SST25VF064:
total_block = (64 * 1024 * 1024 / 8) / mtd->block_size;
break;
total_block = (64 * 1024 * 1024 / 8) / mtd->block_size;
break;
default:
rt_kprintf("SST25 detection error, id: %x\n", id);
return -RT_ERROR;
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;
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;
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+");
/* 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; i<total_block; i++)
{
fseek(sst25->file, i * BLOCK_SIZE, SEEK_SET);
fwrite(block_buffer, BLOCK_SIZE, 1, sst25->file);
}
}
memset(block_buffer, 0xFF, sizeof(block_buffer));
for (i = 0; i < total_block; i++)
{
fseek(sst25->file, i * BLOCK_SIZE, SEEK_SET);
fwrite(block_buffer, BLOCK_SIZE, 1, sst25->file);
}
}
fseek(sst25->file, 0, SEEK_SET);
fseek(sst25->file, 0, SEEK_SET);
/* initialize hardware */
sst25vfxx_hw_init(&_sst25_mtd);
/* register MTD device */
rt_mtd_nor_register_device("nor", mtd);
rt_mtd_nor_register_device("nor", mtd);
return RT_EOK;
}
@ -213,14 +213,14 @@ rt_err_t sst25vfxx_mtd_init(const char * nor_name,
#include <finsh.h>
void nor_erase(void)
{
rt_uint32_t index;
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);
}
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

View File

@ -17,17 +17,17 @@ static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam);
void rt_hw_usart_init(void)
{
/*
* create serial thread that revice key input from keyboard
*/
/*
* 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)
0,
(LPTHREAD_START_ROUTINE)ThreadforKeyGet,
0,
CREATE_SUSPENDED,
&OSKey_ThreadID);
if (OSKey_Thread == NULL)
{
//Display Error Message
@ -47,85 +47,85 @@ void rt_hw_usart_init(void)
}
/*
* () 0xe04b
* () 0xe048
* () 0xe04d
* () 0xe050
* () 0xe04b
* () 0xe048
* () 0xe04d
* () 0xe050
*/
static int savekey(unsigned char key)
{
/* save on rx buffer */
{
rt_base_t level;
/* save on rx buffer */
{
rt_base_t level;
/* disable interrupt */
/* disable interrupt */
//暂时关闭中断因为要操作uart数据结构
level = rt_hw_interrupt_disable();
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 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;
}
/* 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);
}
/* enable interrupt */
//uart数据结构已经操作完成重新使能中断
rt_hw_interrupt_enable(level);
}
/* invoke callback */
if (serial_device.rx_indicate != RT_NULL)
{
rt_size_t rx_length;
/* 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;
/* 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;
serial_device.rx_indicate(&serial_device, rx_length);
}
return 0;
}
static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam)
{
unsigned char key;
unsigned char key;
(void)lpParam; //prevent compiler warnings
for(;;)
for (;;)
{
key = _getch();//getchar();
if (key == 0xE0)
{
key = _getch();
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);
}
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;
}
continue;
}
savekey(key);
savekey(key);
}
} /*** ThreadforKeyGet ***/

View File

@ -1,271 +1,271 @@
#include <string.h>
#include <stdlib.h>
#include <rtthread.h>
#include "snake.h"
#define ASSERT_RET(x, ret) \
do{ \
if (x) \
return ret; \
}while(0)
rt_list_t snake_head;
SNAKE_DIR prevdir, newdir;
static SNAKE_DIR dir_adjust(SNAKE_DIR dir)
{
if ( (SNAKE_DIR_UP == prevdir && SNAKE_DIR_DOWN != dir)
|| (SNAKE_DIR_DOWN == prevdir && SNAKE_DIR_UP != dir)
|| (SNAKE_DIR_LEFT == prevdir && SNAKE_DIR_RIGHT != dir)
|| (SNAKE_DIR_RIGHT == prevdir && SNAKE_DIR_LEFT != dir)
)
{
newdir = dir;
}
else
{
rt_kprintf("dirction change error\n\r");
}
return newdir;
}
static void across_XY(point_t *node, const map_t *map)
{
RT_ASSERT(node != RT_NULL && map != RT_NULL);
// 如果长度超出当前边框则可以穿越墙到对面
node->x = (node->x + map->width) % map->width;
node->y = (node->y + map->height) % map->height;
}
static SYS_STE node_update(snake_t *tail, const point_t *node, map_t *map)
{
SYS_STE ret;
point_t *pos;
RT_ASSERT(tail != RT_NULL && node != RT_NULL && map != RT_NULL);
pos = map->snake_flush;
pos[0].x = pos[0].y = -1;
pos[1].x = pos[1].y = -1;
ret = (SYS_STE)map->range[node->y * map->width + node->x];
if (FOOD == map->range[node->y * map->width + node->x])
{
// 吃一个食物增加一个节点
snake_t *new = (snake_t*)rt_malloc(sizeof(snake_t));
if (!new)
return NORMAL;
pos[0] = *node;
new->body = *node;
rt_list_insert_after(&snake_head, &new->list);
}
else if (NORMAL == map->range[node->y * map->width + node->x])
{
// 将尾巴修改后拿到头部,其他不变
rt_list_remove(&tail->list);
map->range[tail->body.y * map->width + tail->body.x] = NORMAL;
pos[0] = *node;
pos[1] = tail->body;
tail->body = *node;
rt_list_insert_after(&snake_head, &tail->list);
}
map->range[node->y * map->width + node->x] = OVER;
if (ret != OVER)
prevdir = newdir;
return ret;
}
map_t* map_init(rt_uint32_t width, rt_uint32_t heigth)
{
map_t *map = rt_malloc(sizeof(map_t));
if (map != RT_NULL)
{
map->range = rt_malloc(heigth * width);
if (!map->range)
{
rt_free(map);
map = RT_NULL;
}
else
{
map->width = width;
map->height = heigth;
memset(map->range, NORMAL, heigth * width);
}
}
return map;
}
// 构造一条指定长度的蛇在指定点
rt_bool_t snake_init(const point_t *start, const int length, const SNAKE_DIR dir, map_t *map)
{
rt_int32_t i;
rt_int32_t inc_x, inc_y;
point_t old = *start;
ASSERT_RET(!map || !start, RT_FALSE);
rt_list_init(&snake_head);
if (dir == SNAKE_DIR_UP || dir == SNAKE_DIR_DOWN)
{
if (map->height <= length)
return RT_FALSE;
inc_x = 0;
inc_y = dir == SNAKE_DIR_DOWN ? 1 : -1; // 反向延长身子,头部在指定位置
old.y -= inc_y;
}
else
{
if (map->width <= length)
return RT_FALSE;
inc_y = 0;
inc_x = dir == SNAKE_DIR_RIGHT ? -1 : 1;
old.x -= inc_x;
}
for (i=0; i<length; i++)
{
snake_t *new = (snake_t*)rt_malloc(sizeof(snake_t));
if (!new)
return RT_FALSE;
new->body.y = inc_y + old.y;
new->body.x = inc_x + old.x;
// 如果长度超出当前边框则可以穿越墙到对面
across_XY(&new->body, map);
map->range[new->body.y * map->width + new->body.x] = OVER;
old = new->body;
rt_list_insert_before(&snake_head, &new->list);
}
prevdir = dir;
return RT_TRUE;
}
// 构造出食物
rt_bool_t food_init(map_t *map, rt_uint32_t max_num)
{
point_t food;
#ifndef FOOD_TIMEOUT
#define FOOD_TIMEOUT 10
#endif
rt_uint32_t timeout, num;
ASSERT_RET(!map, RT_FALSE);
num = 0;
timeout = rt_tick_get();
srand(rand());
map->food_flush[0].x = map->food_flush[0].y = -1;
do
{
food.x = rand() % map->width;
food.y = rand() % map->height;
if (map->range[food.y * map->width + food.x] == NORMAL)
{
map->food_flush[0] = food;
map->range[food.y * map->width + food.x] = FOOD;
num++;
}
}
while (num < max_num && rt_tick_get() - timeout < FOOD_TIMEOUT);
return num;
}
void map_deinit(map_t *map)
{
if (map)
{
if (map->range)
{
rt_free(map->range);
map->range = RT_NULL;
}
rt_free(map);
}
}
void snake_deinit(void)
{
snake_t *node;
while (!rt_list_isempty(&snake_head))
{
node = rt_list_entry(snake_head.prev, snake_t, list);
rt_list_remove(&node->list);
rt_free(node);
}
}
void food_deinit(void)
{
}
SYS_STE snake_step(SNAKE_DIR dir, map_t *map)
{
snake_t *tail, *head;
point_t node;
ASSERT_RET(!map, RT_FALSE);
dir = dir_adjust(dir);
// 取出头尾两个节点,其他节点不需要改变
tail = rt_list_entry(snake_head.prev, snake_t, list);
head = rt_list_entry(snake_head.next, snake_t, list);
node = head->body;
// 构造一个新的蛇头坐标
switch (dir)
{
case SNAKE_DIR_UP:
case SNAKE_DIR_DOWN:
node.y = head->body.y + (dir == SNAKE_DIR_DOWN ? -1 : 1);
break;
case SNAKE_DIR_LEFT:
case SNAKE_DIR_RIGHT:
node.x = head->body.x + (dir == SNAKE_DIR_RIGHT ? 1 : -1);
break;
}
across_XY(&node, map);
return node_update(tail, &node, map);
}
rt_bool_t snake_restart(const point_t *start, const int length, const SNAKE_DIR dir, map_t *map)
{
ASSERT_RET(!map || !start, RT_FALSE);
snake_deinit();
memset(map->range, NORMAL, map->width * map->height);
return snake_init(start, length, dir, map);
}
#include <string.h>
#include <stdlib.h>
#include <rtthread.h>
#include "snake.h"
#define ASSERT_RET(x, ret) \
do{ \
if (x) \
return ret; \
}while(0)
rt_list_t snake_head;
SNAKE_DIR prevdir, newdir;
static SNAKE_DIR dir_adjust(SNAKE_DIR dir)
{
if ((SNAKE_DIR_UP == prevdir && SNAKE_DIR_DOWN != dir)
|| (SNAKE_DIR_DOWN == prevdir && SNAKE_DIR_UP != dir)
|| (SNAKE_DIR_LEFT == prevdir && SNAKE_DIR_RIGHT != dir)
|| (SNAKE_DIR_RIGHT == prevdir && SNAKE_DIR_LEFT != dir)
)
{
newdir = dir;
}
else
{
rt_kprintf("dirction change error\n\r");
}
return newdir;
}
static void across_XY(point_t *node, const map_t *map)
{
RT_ASSERT(node != RT_NULL && map != RT_NULL);
// 如果长度超出当前边框则可以穿越墙到对面
node->x = (node->x + map->width) % map->width;
node->y = (node->y + map->height) % map->height;
}
static SYS_STE node_update(snake_t *tail, const point_t *node, map_t *map)
{
SYS_STE ret;
point_t *pos;
RT_ASSERT(tail != RT_NULL && node != RT_NULL && map != RT_NULL);
pos = map->snake_flush;
pos[0].x = pos[0].y = -1;
pos[1].x = pos[1].y = -1;
ret = (SYS_STE)map->range[node->y * map->width + node->x];
if (FOOD == map->range[node->y * map->width + node->x])
{
// 吃一个食物增加一个节点
snake_t *new = (snake_t *)rt_malloc(sizeof(snake_t));
if (!new)
return NORMAL;
pos[0] = *node;
new->body = *node;
rt_list_insert_after(&snake_head, &new->list);
}
else if (NORMAL == map->range[node->y * map->width + node->x])
{
// 将尾巴修改后拿到头部,其他不变
rt_list_remove(&tail->list);
map->range[tail->body.y * map->width + tail->body.x] = NORMAL;
pos[0] = *node;
pos[1] = tail->body;
tail->body = *node;
rt_list_insert_after(&snake_head, &tail->list);
}
map->range[node->y * map->width + node->x] = OVER;
if (ret != OVER)
prevdir = newdir;
return ret;
}
map_t *map_init(rt_uint32_t width, rt_uint32_t heigth)
{
map_t *map = rt_malloc(sizeof(map_t));
if (map != RT_NULL)
{
map->range = rt_malloc(heigth * width);
if (!map->range)
{
rt_free(map);
map = RT_NULL;
}
else
{
map->width = width;
map->height = heigth;
memset(map->range, NORMAL, heigth * width);
}
}
return map;
}
// 构造一条指定长度的蛇在指定点
rt_bool_t snake_init(const point_t *start, const int length, const SNAKE_DIR dir, map_t *map)
{
rt_int32_t i;
rt_int32_t inc_x, inc_y;
point_t old = *start;
ASSERT_RET(!map || !start, RT_FALSE);
rt_list_init(&snake_head);
if (dir == SNAKE_DIR_UP || dir == SNAKE_DIR_DOWN)
{
if (map->height <= length)
return RT_FALSE;
inc_x = 0;
inc_y = dir == SNAKE_DIR_DOWN ? 1 : -1; // 反向延长身子,头部在指定位置
old.y -= inc_y;
}
else
{
if (map->width <= length)
return RT_FALSE;
inc_y = 0;
inc_x = dir == SNAKE_DIR_RIGHT ? -1 : 1;
old.x -= inc_x;
}
for (i = 0; i < length; i++)
{
snake_t *new = (snake_t *)rt_malloc(sizeof(snake_t));
if (!new)
return RT_FALSE;
new->body.y = inc_y + old.y;
new->body.x = inc_x + old.x;
// 如果长度超出当前边框则可以穿越墙到对面
across_XY(&new->body, map);
map->range[new->body.y * map->width + new->body.x] = OVER;
old = new->body;
rt_list_insert_before(&snake_head, &new->list);
}
prevdir = dir;
return RT_TRUE;
}
// 构造出食物
rt_bool_t food_init(map_t *map, rt_uint32_t max_num)
{
point_t food;
#ifndef FOOD_TIMEOUT
#define FOOD_TIMEOUT 10
#endif
rt_uint32_t timeout, num;
ASSERT_RET(!map, RT_FALSE);
num = 0;
timeout = rt_tick_get();
srand(rand());
map->food_flush[0].x = map->food_flush[0].y = -1;
do
{
food.x = rand() % map->width;
food.y = rand() % map->height;
if (map->range[food.y * map->width + food.x] == NORMAL)
{
map->food_flush[0] = food;
map->range[food.y * map->width + food.x] = FOOD;
num++;
}
}
while (num < max_num && rt_tick_get() - timeout < FOOD_TIMEOUT);
return num;
}
void map_deinit(map_t *map)
{
if (map)
{
if (map->range)
{
rt_free(map->range);
map->range = RT_NULL;
}
rt_free(map);
}
}
void snake_deinit(void)
{
snake_t *node;
while (!rt_list_isempty(&snake_head))
{
node = rt_list_entry(snake_head.prev, snake_t, list);
rt_list_remove(&node->list);
rt_free(node);
}
}
void food_deinit(void)
{
}
SYS_STE snake_step(SNAKE_DIR dir, map_t *map)
{
snake_t *tail, *head;
point_t node;
ASSERT_RET(!map, RT_FALSE);
dir = dir_adjust(dir);
// 取出头尾两个节点,其他节点不需要改变
tail = rt_list_entry(snake_head.prev, snake_t, list);
head = rt_list_entry(snake_head.next, snake_t, list);
node = head->body;
// 构造一个新的蛇头坐标
switch (dir)
{
case SNAKE_DIR_UP:
case SNAKE_DIR_DOWN:
node.y = head->body.y + (dir == SNAKE_DIR_DOWN ? -1 : 1);
break;
case SNAKE_DIR_LEFT:
case SNAKE_DIR_RIGHT:
node.x = head->body.x + (dir == SNAKE_DIR_RIGHT ? 1 : -1);
break;
}
across_XY(&node, map);
return node_update(tail, &node, map);
}
rt_bool_t snake_restart(const point_t *start, const int length, const SNAKE_DIR dir, map_t *map)
{
ASSERT_RET(!map || !start, RT_FALSE);
snake_deinit();
memset(map->range, NORMAL, map->width * map->height);
return snake_init(start, length, dir, map);
}

View File

@ -14,7 +14,7 @@
typedef struct
{
rt_int32_t x, y;
}point_t;
} point_t;
typedef struct
{
@ -23,7 +23,7 @@ typedef struct
rt_uint8_t *range; // map, map->range[y * map->width + x]
point_t snake_flush[2];
point_t food_flush[1];
}map_t;
} map_t;
typedef enum
{
@ -31,23 +31,23 @@ typedef enum
SNAKE_DIR_DOWN,
SNAKE_DIR_LEFT,
SNAKE_DIR_RIGHT
}SNAKE_DIR;
} SNAKE_DIR;
typedef enum
{
FOOD, // 吃到水果
OVER, // 咬到自身
NORMAL // 正常行动
}SYS_STE;
} SYS_STE;
typedef struct
{
point_t body;
rt_list_t list;
}snake_t;
} snake_t;
// 构造一个地图
map_t* map_init(rt_uint32_t width, rt_uint32_t heigth);
map_t *map_init(rt_uint32_t width, rt_uint32_t heigth);
// 构造一条指定长度的蛇在指定点
rt_bool_t snake_init(const point_t *start, const int length, const SNAKE_DIR dir, map_t *map);

View File

@ -24,7 +24,7 @@ static rt_size_t room_size_x, room_size_y;
static rt_size_t lattice_size_x, lattice_size_y;
static struct rtgui_rect room_rect, lattice_rect;
map_t* map;
map_t *map;
SNAKE_DIR run_state;
rt_int32_t snake_len;
rt_int32_t food_num;
@ -67,7 +67,7 @@ static void snake_draw(struct rtgui_widget *widget)
}
/* get room size, run once frist. */
if((room_size_x == 0) || (room_size_y == 0))
if ((room_size_x == 0) || (room_size_y == 0))
{
rt_size_t tmp;
@ -133,7 +133,7 @@ static void snake_draw(struct rtgui_widget *widget)
RTGUI_DC_FC(dc) = WALL_COLOR;
rtgui_dc_draw_rect(dc, &rect);
for(i=1; i<lattice_size_y; i++)
for (i = 1; i < lattice_size_y; i++)
{
memcpy(&rect, &lattice_rect, sizeof(struct rtgui_rect));
rect.x1 += 1;
@ -142,7 +142,7 @@ static void snake_draw(struct rtgui_widget *widget)
rect.y1 + (LATTICE_SIZE * i));
}
for(i=1; i<lattice_size_x; i++)
for (i = 1; i < lattice_size_x; i++)
{
memcpy(&rect, &lattice_rect, sizeof(struct rtgui_rect));
rect.y1 += 1;
@ -156,9 +156,9 @@ static void snake_draw(struct rtgui_widget *widget)
rt_uint32_t x, y;
rt_bool_t first_node = RT_TRUE;
for (y=0; y<map->height; y++)
for (y = 0; y < map->height; y++)
{
for (x=0; x<map->width; x++)
for (x = 0; x < map->width; x++)
{
switch (map->range[y * map->width + x])
{
@ -207,9 +207,9 @@ static void snake_update(struct rtgui_widget *widget)
snake_fill_lattice(dc, second_node.x, second_node.y, SNAKE_COLOR);
second_node = map->snake_flush[0];
for(i=0; i<3; i++)
for (i = 0; i < 3; i++)
{
if(i < 2)
if (i < 2)
{
x = map->snake_flush[i].x;
y = map->snake_flush[i].y;
@ -220,7 +220,7 @@ static void snake_update(struct rtgui_widget *widget)
y = map->food_flush[0].y;
}
if((x >= 0) && (y >= 0))
if ((x >= 0) && (y >= 0))
{
switch (map->range[(map->width * y) + x])
{
@ -246,9 +246,9 @@ static void snake_update(struct rtgui_widget *widget)
static void snake_handler(struct rtgui_widget *widget, rtgui_event_t *event)
{
struct rtgui_event_kbd* ekbd;
struct rtgui_event_kbd *ekbd;
ekbd = (struct rtgui_event_kbd*) event;
ekbd = (struct rtgui_event_kbd *) event;
if (ekbd->type == RTGUI_KEYDOWN)
{
switch (ekbd->key)
@ -284,38 +284,38 @@ static rt_bool_t event_handler(struct rtgui_object *object, rtgui_event_t *event
if (event->type == RTGUI_EVENT_PAINT)
{
rt_kprintf("RTGUI_EVENT_PAINT\r\n");
rtgui_win_event_handler((struct rtgui_object*)object, event);
rtgui_win_event_handler((struct rtgui_object *)object, event);
snake_draw(widget);
rtgui_timer_start(timer);
}
else if (event->type == RTGUI_EVENT_SHOW)
{
rt_kprintf("RTGUI_EVENT_SHOW\r\n");
rtgui_win_event_handler((struct rtgui_object*)object, event);
rtgui_win_event_handler((struct rtgui_object *)object, event);
snake_draw(widget);
rtgui_timer_start(timer);
}
else if (event->type == RTGUI_EVENT_HIDE)
{
rt_kprintf("RTGUI_EVENT_HIDE\r\n");
rtgui_win_event_handler((struct rtgui_object*)object, event);
rtgui_win_event_handler((struct rtgui_object *)object, event);
rtgui_timer_stop(timer);
}
else if (event->type == RTGUI_EVENT_WIN_DEACTIVATE)
{
rt_kprintf("RTGUI_EVENT_WIN_DEACTIVATE\r\n");
rtgui_win_event_handler((struct rtgui_object*)object, event);
rtgui_win_event_handler((struct rtgui_object *)object, event);
rtgui_timer_stop(timer);
}
else if (event->type == RTGUI_EVENT_KBD)
{
rtgui_win_event_handler((struct rtgui_object*)object, event);
rtgui_win_event_handler((struct rtgui_object *)object, event);
snake_handler(widget, event);
}
else
{
rt_kprintf("event->type:%d\r\n", event->type);
return rtgui_win_event_handler((struct rtgui_object*)object, event);
return rtgui_win_event_handler((struct rtgui_object *)object, event);
}
return RT_FALSE;
@ -325,7 +325,7 @@ static void timeout(struct rtgui_timer *timer, void *parameter)
{
struct rtgui_widget *widget;
SYS_STE ret;
if (!map)
return;
@ -355,26 +355,26 @@ static void timeout(struct rtgui_timer *timer, void *parameter)
food_init(map, 1);
}
widget = RTGUI_WIDGET(parameter);
snake_update(widget);
}
void snake_main(void)
{
struct rtgui_app* application;
struct rtgui_win* win;
rtgui_rect_t rect;
struct rtgui_app *application;
struct rtgui_win *win;
rtgui_rect_t rect;
application = rtgui_app_create(rt_thread_self(), "sanke_app");
if (application != RT_NULL)
{
rtgui_get_screen_rect(&rect);
rtgui_set_mainwin_rect(&rect);
rtgui_get_screen_rect(&rect);
rtgui_set_mainwin_rect(&rect);
win = rtgui_mainwin_create(RT_NULL,
"sanke_win",
RTGUI_WIN_STYLE_MAINWIN | RTGUI_WIN_STYLE_DESTROY_ON_CLOSE);
if(win == RT_NULL)
if (win == RT_NULL)
{
rt_kprintf("sanke_win create fail!\r\n");
return;