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:
parent
2e1de690f2
commit
1b274d996f
|
@ -9,11 +9,11 @@ void rt_platform_init(void)
|
||||||
|
|
||||||
#if defined(RT_USING_MTD_NAND)
|
#if defined(RT_USING_MTD_NAND)
|
||||||
rt_hw_mtd_nand_init();
|
rt_hw_mtd_nand_init();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(RT_USING_MTD_NOR)
|
#if defined(RT_USING_MTD_NOR)
|
||||||
sst25vfxx_mtd_init("nor", 0, RT_UINT32_MAX);
|
sst25vfxx_mtd_init("nor", 0, RT_UINT32_MAX);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif /* RT_USING_DFS */
|
#endif /* RT_USING_DFS */
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ void rt_platform_init(void)
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
|
|
||||||
rt_thread_idle_sethook(rt_hw_win32_low_cpu);
|
rt_thread_idle_sethook(rt_hw_win32_low_cpu);
|
||||||
#endif
|
#endif
|
||||||
rt_thread_delay(50);
|
rt_thread_delay(50);
|
||||||
rt_device_init_all();
|
rt_device_init_all();
|
||||||
|
|
|
@ -26,67 +26,67 @@
|
||||||
extern int rt_application_init(void);
|
extern int rt_application_init(void);
|
||||||
#ifdef RT_USING_FINSH
|
#ifdef RT_USING_FINSH
|
||||||
extern void finsh_system_init(void);
|
extern void finsh_system_init(void);
|
||||||
extern void finsh_set_device(const char* device);
|
extern void finsh_set_device(const char *device);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
extern rt_uint8_t * heap;
|
extern rt_uint8_t *heap;
|
||||||
/**
|
/**
|
||||||
* This function will startup RT-Thread RTOS.
|
* This function will startup RT-Thread RTOS.
|
||||||
*/
|
*/
|
||||||
void rtthread_startup(void)
|
void rtthread_startup(void)
|
||||||
{
|
{
|
||||||
/* init board */
|
/* init board */
|
||||||
rt_hw_board_init();
|
rt_hw_board_init();
|
||||||
|
|
||||||
/* show version */
|
/* show version */
|
||||||
rt_show_version();
|
rt_show_version();
|
||||||
|
|
||||||
/* init tick */
|
/* init tick */
|
||||||
rt_system_tick_init();
|
rt_system_tick_init();
|
||||||
|
|
||||||
/* init kernel object */
|
/* init kernel object */
|
||||||
rt_system_object_init();
|
rt_system_object_init();
|
||||||
|
|
||||||
/* init timer system */
|
/* init timer system */
|
||||||
rt_system_timer_init();
|
rt_system_timer_init();
|
||||||
|
|
||||||
#ifdef RT_USING_HEAP
|
#ifdef RT_USING_HEAP
|
||||||
/* init memory system */
|
/* init memory system */
|
||||||
rt_system_heap_init((void*)heap, (void*)&heap[HEAP_SIZE-1]);
|
rt_system_heap_init((void *)heap, (void *)&heap[HEAP_SIZE - 1]);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* init scheduler system */
|
/* init scheduler system */
|
||||||
rt_system_scheduler_init();
|
rt_system_scheduler_init();
|
||||||
|
|
||||||
/* init all device */
|
/* init all device */
|
||||||
#ifdef RT_USING_DEVICE
|
#ifdef RT_USING_DEVICE
|
||||||
rt_device_init_all();
|
rt_device_init_all();
|
||||||
#endif
|
#endif
|
||||||
/* init application */
|
/* init application */
|
||||||
rt_application_init();
|
rt_application_init();
|
||||||
|
|
||||||
/* init timer thread */
|
/* init timer thread */
|
||||||
rt_system_timer_thread_init();
|
rt_system_timer_thread_init();
|
||||||
|
|
||||||
/* init idle thread */
|
/* init idle thread */
|
||||||
rt_thread_idle_init();
|
rt_thread_idle_init();
|
||||||
|
|
||||||
/* start scheduler */
|
/* start scheduler */
|
||||||
rt_system_scheduler_start();
|
rt_system_scheduler_start();
|
||||||
|
|
||||||
/* never reach here */
|
/* never reach here */
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
/* disable interrupt first */
|
/* disable interrupt first */
|
||||||
rt_hw_interrupt_disable();
|
rt_hw_interrupt_disable();
|
||||||
|
|
||||||
/* startup RT-Thread RTOS */
|
/* startup RT-Thread RTOS */
|
||||||
rtthread_startup();
|
rtthread_startup();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*@}*/
|
/*@}*/
|
||||||
|
|
|
@ -21,23 +21,23 @@
|
||||||
/**
|
/**
|
||||||
* @addtogroup simulator on win32
|
* @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;
|
rt_uint8_t *heap;
|
||||||
heap = malloc(HEAP_SIZE);
|
heap = malloc(HEAP_SIZE);
|
||||||
if (heap == RT_NULL)
|
if (heap == RT_NULL)
|
||||||
{
|
{
|
||||||
rt_kprintf("there is no memory in pc.");
|
rt_kprintf("there is no memory in pc.");
|
||||||
_exit(1);
|
_exit(1);
|
||||||
}
|
}
|
||||||
return heap;
|
return heap;
|
||||||
}
|
}
|
||||||
|
|
||||||
void rt_hw_win32_low_cpu(void)
|
void rt_hw_win32_low_cpu(void)
|
||||||
{
|
{
|
||||||
Sleep(1000);
|
Sleep(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(RT_USING_FINSH)
|
#if defined(RT_USING_FINSH)
|
||||||
|
@ -52,24 +52,24 @@ _CRTIMP void __cdecl abort(void);
|
||||||
#include <finsh.h>
|
#include <finsh.h>
|
||||||
void rt_hw_exit(void)
|
void rt_hw_exit(void)
|
||||||
{
|
{
|
||||||
rt_kprintf("RT-Thread, bye\n");
|
rt_kprintf("RT-Thread, bye\n");
|
||||||
exit(0);
|
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 */
|
#endif /* RT_USING_FINSH */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function will initial win32
|
* This function will initial win32
|
||||||
*/
|
*/
|
||||||
void rt_hw_board_init()
|
void rt_hw_board_init()
|
||||||
{
|
{
|
||||||
/* init system memory */
|
/* init system memory */
|
||||||
heap = rt_hw_sram_init();
|
heap = rt_hw_sram_init();
|
||||||
|
|
||||||
#if defined(RT_USING_CONSOLE)
|
#if defined(RT_USING_CONSOLE)
|
||||||
rt_hw_usart_init();
|
rt_hw_usart_init();
|
||||||
rt_hw_serial_init();
|
rt_hw_serial_init();
|
||||||
rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
|
rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
/*@}*/
|
/*@}*/
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
#ifndef __BOARD_H__
|
#ifndef __BOARD_H__
|
||||||
#define __BOARD_H__
|
#define __BOARD_H__
|
||||||
void rt_hw_board_init(void);
|
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 */
|
/* SD Card init function */
|
||||||
void rt_hw_sdcard_init(void);
|
void rt_hw_sdcard_init(void);
|
||||||
|
|
|
@ -5,38 +5,38 @@
|
||||||
|
|
||||||
#define NAND_SIM "nand.bin"
|
#define NAND_SIM "nand.bin"
|
||||||
#if 1
|
#if 1
|
||||||
#define OOB_SIZE 64
|
#define OOB_SIZE 64
|
||||||
#define PAGE_SIZE (2048 + 64)
|
#define PAGE_SIZE (2048 + 64)
|
||||||
#define PAGE_PER_BLOCK 64
|
#define PAGE_PER_BLOCK 64
|
||||||
#define BLOCK_SIZE (PAGE_SIZE * PAGE_PER_BLOCK)
|
#define BLOCK_SIZE (PAGE_SIZE * PAGE_PER_BLOCK)
|
||||||
#define BLOCK_NUM 512
|
#define BLOCK_NUM 512
|
||||||
// #define BLOCK_NUM 2048
|
// #define BLOCK_NUM 2048
|
||||||
#else
|
#else
|
||||||
#define OOB_SIZE 16
|
#define OOB_SIZE 16
|
||||||
#define PAGE_SIZE (512 + OOB_SIZE)
|
#define PAGE_SIZE (512 + OOB_SIZE)
|
||||||
#define PAGE_PER_BLOCK 32
|
#define PAGE_PER_BLOCK 32
|
||||||
#define BLOCK_SIZE (PAGE_SIZE * PAGE_PER_BLOCK)
|
#define BLOCK_SIZE (PAGE_SIZE * PAGE_PER_BLOCK)
|
||||||
#define BLOCK_NUM 512
|
#define BLOCK_NUM 512
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static unsigned char block_data[BLOCK_SIZE];
|
static unsigned char block_data[BLOCK_SIZE];
|
||||||
static struct rt_mtd_nand_device _nanddrv_file_device;
|
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)
|
static rt_uint8_t CountBitsInByte(rt_uint8_t byte)
|
||||||
{
|
{
|
||||||
rt_uint8_t count = 0;
|
rt_uint8_t count = 0;
|
||||||
|
|
||||||
while (byte > 0)
|
while (byte > 0)
|
||||||
{
|
{
|
||||||
if (byte & 1)
|
if (byte & 1)
|
||||||
{
|
{
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
byte >>= 1;
|
byte >>= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void Compute256(const rt_uint8_t *data, rt_uint8_t *code)
|
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;
|
// Xor all bytes together to get the column sum;
|
||||||
// At the same time, calculate the even and odd line codes
|
// 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];
|
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
|
// At this point, we have the line parities, and the column sum. First, We
|
||||||
// must caculate the parity group values on the column sum.
|
// 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)
|
if (columnSum & 1)
|
||||||
{
|
{
|
||||||
|
@ -113,7 +113,7 @@ static void Compute256(const rt_uint8_t *data, rt_uint8_t *code)
|
||||||
code[1] = 0;
|
code[1] = 0;
|
||||||
code[2] = 0;
|
code[2] = 0;
|
||||||
|
|
||||||
for (i=0; i < 4; i++)
|
for (i = 0; i < 4; i++)
|
||||||
{
|
{
|
||||||
code[0] <<= 2;
|
code[0] <<= 2;
|
||||||
code[1] <<= 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]);
|
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;
|
pucData += 256;
|
||||||
puCode += 3;
|
puCode += 3;
|
||||||
|
@ -177,222 +177,222 @@ void ecc_hamming_compute256x(const rt_uint8_t *pucData, rt_uint32_t dwSize, rt_u
|
||||||
}
|
}
|
||||||
|
|
||||||
/* read chip id */
|
/* 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 */
|
/* read/write/move page */
|
||||||
static rt_err_t nanddrv_file_read_page(struct rt_mtd_nand_device* device,
|
static rt_err_t nanddrv_file_read_page(struct rt_mtd_nand_device *device,
|
||||||
rt_off_t page,
|
rt_off_t page,
|
||||||
rt_uint8_t *data, rt_uint32_t data_len,
|
rt_uint8_t *data, rt_uint32_t data_len,
|
||||||
rt_uint8_t *spare, rt_uint32_t spare_len)
|
rt_uint8_t *spare, rt_uint32_t spare_len)
|
||||||
{
|
{
|
||||||
rt_uint32_t offset;
|
rt_uint32_t offset;
|
||||||
rt_uint8_t oob_buffer[OOB_SIZE];
|
rt_uint8_t oob_buffer[OOB_SIZE];
|
||||||
rt_uint8_t oob_ecc [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)
|
if (page / device->pages_per_block > device->block_end)
|
||||||
{
|
{
|
||||||
return -RT_EIO;
|
return -RT_EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* write page */
|
/* write page */
|
||||||
offset = page * PAGE_SIZE;
|
offset = page * PAGE_SIZE;
|
||||||
if (data != NULL)
|
if (data != NULL)
|
||||||
{
|
{
|
||||||
fseek(file, offset, SEEK_SET);
|
fseek(file, offset, SEEK_SET);
|
||||||
fread(data, data_len, 1, file);
|
fread(data, data_len, 1, file);
|
||||||
}
|
}
|
||||||
|
|
||||||
offset = page * PAGE_SIZE + (PAGE_SIZE - OOB_SIZE);
|
offset = page * PAGE_SIZE + (PAGE_SIZE - OOB_SIZE);
|
||||||
fseek(file, offset, SEEK_SET);
|
fseek(file, offset, SEEK_SET);
|
||||||
fread(oob_buffer, OOB_SIZE, 1, file);
|
fread(oob_buffer, OOB_SIZE, 1, file);
|
||||||
if (spare != NULL)
|
if (spare != NULL)
|
||||||
{
|
{
|
||||||
memcpy(spare, oob_buffer, spare_len);
|
memcpy(spare, oob_buffer, spare_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* verify ECC */
|
/* verify ECC */
|
||||||
if (data != RT_NULL)
|
if (data != RT_NULL)
|
||||||
{
|
{
|
||||||
ecc_hamming_compute256x(data, PAGE_SIZE - OOB_SIZE, &oob_ecc[0]);
|
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;
|
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,
|
static rt_err_t nanddrv_file_write_page(struct rt_mtd_nand_device *device,
|
||||||
rt_off_t page,
|
rt_off_t page,
|
||||||
const rt_uint8_t* data, rt_uint32_t data_len,
|
const rt_uint8_t *data, rt_uint32_t data_len,
|
||||||
const rt_uint8_t* oob, rt_uint32_t spare_len)
|
const rt_uint8_t *oob, rt_uint32_t spare_len)
|
||||||
{
|
{
|
||||||
rt_uint32_t offset;
|
rt_uint32_t offset;
|
||||||
rt_uint8_t oob_buffer[OOB_SIZE];
|
rt_uint8_t oob_buffer[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)
|
if (page / device->pages_per_block > device->block_end)
|
||||||
{
|
{
|
||||||
return -RT_EIO;
|
return -RT_EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* write page */
|
/* write page */
|
||||||
offset = page * PAGE_SIZE;
|
offset = page * PAGE_SIZE;
|
||||||
if (data != NULL)
|
if (data != NULL)
|
||||||
{
|
{
|
||||||
fseek(file, offset, SEEK_SET);
|
fseek(file, offset, SEEK_SET);
|
||||||
fwrite(data, PAGE_SIZE - OOB_SIZE, 1, file);
|
fwrite(data, PAGE_SIZE - OOB_SIZE, 1, file);
|
||||||
}
|
}
|
||||||
|
|
||||||
offset = page * PAGE_SIZE + (PAGE_SIZE - OOB_SIZE);
|
offset = page * PAGE_SIZE + (PAGE_SIZE - OOB_SIZE);
|
||||||
fseek(file, offset, SEEK_SET);
|
fseek(file, offset, SEEK_SET);
|
||||||
|
|
||||||
memset(oob_buffer, 0xff, sizeof(oob_buffer));
|
memset(oob_buffer, 0xff, sizeof(oob_buffer));
|
||||||
ecc_hamming_compute256x(data, PAGE_SIZE - OOB_SIZE, &oob_buffer[0]);
|
ecc_hamming_compute256x(data, PAGE_SIZE - OOB_SIZE, &oob_buffer[0]);
|
||||||
if (oob != RT_NULL)
|
if (oob != RT_NULL)
|
||||||
{
|
{
|
||||||
memcpy(&oob_buffer[OOB_SIZE - device->oob_free],
|
memcpy(&oob_buffer[OOB_SIZE - device->oob_free],
|
||||||
&oob[OOB_SIZE - device->oob_free],
|
&oob[OOB_SIZE - device->oob_free],
|
||||||
device->oob_free);
|
device->oob_free);
|
||||||
}
|
}
|
||||||
fwrite(oob_buffer, OOB_SIZE, 1, file);
|
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_uint32_t offset;
|
||||||
rt_uint8_t page_buffer[PAGE_SIZE - OOB_SIZE];
|
rt_uint8_t page_buffer[PAGE_SIZE - OOB_SIZE];
|
||||||
rt_uint8_t oob_buffer[OOB_SIZE];
|
rt_uint8_t oob_buffer[OOB_SIZE];
|
||||||
|
|
||||||
from = from + 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;
|
to = to + device->block_start * device->pages_per_block;
|
||||||
|
|
||||||
if (from/device->pages_per_block > device->block_end ||
|
if (from / device->pages_per_block > device->block_end ||
|
||||||
to/device->pages_per_block > device->block_end)
|
to / device->pages_per_block > device->block_end)
|
||||||
{
|
{
|
||||||
return -RT_EIO;
|
return -RT_EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (device->plane_num > 1)
|
if (device->plane_num > 1)
|
||||||
{
|
{
|
||||||
rt_uint32_t mask;
|
rt_uint32_t mask;
|
||||||
rt_uint16_t from_block, to_block;
|
rt_uint16_t from_block, to_block;
|
||||||
|
|
||||||
from_block = (rt_uint16_t)(from / PAGE_PER_BLOCK);
|
from_block = (rt_uint16_t)(from / PAGE_PER_BLOCK);
|
||||||
to_block = (rt_uint16_t)(to / PAGE_PER_BLOCK);
|
to_block = (rt_uint16_t)(to / PAGE_PER_BLOCK);
|
||||||
mask = device->plane_num - 1;
|
mask = device->plane_num - 1;
|
||||||
|
|
||||||
if ((from_block & mask) != (to_block & mask))
|
if ((from_block & mask) != (to_block & mask))
|
||||||
{
|
{
|
||||||
rt_kprintf("invalid page copy on the block. from [%d] --> to[%d]\n", from_block, to_block);
|
rt_kprintf("invalid page copy on the block. from [%d] --> to[%d]\n", from_block, to_block);
|
||||||
return -RT_EIO;
|
return -RT_EIO;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* read page */
|
/* read page */
|
||||||
offset = from * PAGE_SIZE;
|
offset = from * PAGE_SIZE;
|
||||||
fseek(file, offset, SEEK_SET);
|
fseek(file, offset, SEEK_SET);
|
||||||
fread(page_buffer, sizeof(page_buffer), 1, file);
|
fread(page_buffer, sizeof(page_buffer), 1, file);
|
||||||
fread(oob_buffer, sizeof(oob_buffer), 1, file);
|
fread(oob_buffer, sizeof(oob_buffer), 1, file);
|
||||||
|
|
||||||
/* write page */
|
/* write page */
|
||||||
offset = to * PAGE_SIZE;
|
offset = to * PAGE_SIZE;
|
||||||
fseek(file, offset, SEEK_SET);
|
fseek(file, offset, SEEK_SET);
|
||||||
fwrite(page_buffer, sizeof(page_buffer), 1, file);
|
fwrite(page_buffer, sizeof(page_buffer), 1, file);
|
||||||
fwrite(oob_buffer, sizeof(oob_buffer), 1, file);
|
fwrite(oob_buffer, sizeof(oob_buffer), 1, file);
|
||||||
|
|
||||||
return RT_EOK;
|
return RT_EOK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* erase block */
|
/* 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 */
|
/* add the start blocks */
|
||||||
block = block + device->block_start * device->pages_per_block;
|
block = block + device->block_start * device->pages_per_block;
|
||||||
|
|
||||||
fseek(file, block * BLOCK_SIZE, SEEK_SET );
|
fseek(file, block * BLOCK_SIZE, SEEK_SET);
|
||||||
fwrite(block_data, sizeof(block_data), 1, file);
|
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_id,
|
||||||
nanddrv_file_read_page,
|
nanddrv_file_read_page,
|
||||||
nanddrv_file_write_page,
|
nanddrv_file_write_page,
|
||||||
nanddrv_file_move_page,
|
nanddrv_file_move_page,
|
||||||
nanddrv_file_erase_block
|
nanddrv_file_erase_block
|
||||||
};
|
};
|
||||||
|
|
||||||
void nand_eraseall(void);
|
void nand_eraseall(void);
|
||||||
|
|
||||||
void rt_hw_mtd_nand_init(void)
|
void rt_hw_mtd_nand_init(void)
|
||||||
{
|
{
|
||||||
rt_uint16_t ecc_size;
|
rt_uint16_t ecc_size;
|
||||||
rt_uint32_t size;
|
rt_uint32_t size;
|
||||||
|
|
||||||
memset(block_data, 0xff, sizeof(block_data));
|
memset(block_data, 0xff, sizeof(block_data));
|
||||||
/* open file */
|
/* open file */
|
||||||
file = fopen(NAND_SIM, "rb+");
|
file = fopen(NAND_SIM, "rb+");
|
||||||
if (file == NULL)
|
if (file == NULL)
|
||||||
{
|
{
|
||||||
file = fopen(NAND_SIM, "wb+");
|
file = fopen(NAND_SIM, "wb+");
|
||||||
}
|
}
|
||||||
fseek(file, 0, SEEK_END);
|
fseek(file, 0, SEEK_END);
|
||||||
size = ftell(file);
|
size = ftell(file);
|
||||||
|
|
||||||
fseek(file, 0, SEEK_SET );
|
fseek(file, 0, SEEK_SET);
|
||||||
if (size < BLOCK_NUM * BLOCK_SIZE)
|
if (size < BLOCK_NUM * BLOCK_SIZE)
|
||||||
{
|
{
|
||||||
rt_uint32_t index;
|
rt_uint32_t index;
|
||||||
fseek(file, 0, SEEK_SET );
|
fseek(file, 0, SEEK_SET);
|
||||||
for (index = 0; index < BLOCK_NUM; index ++)
|
for (index = 0; index < BLOCK_NUM; index ++)
|
||||||
{
|
{
|
||||||
fwrite(block_data, sizeof(block_data), 1, file);
|
fwrite(block_data, sizeof(block_data), 1, file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fseek(file, 0, SEEK_SET );
|
fseek(file, 0, SEEK_SET);
|
||||||
|
|
||||||
ecc_size = (PAGE_SIZE - OOB_SIZE) * 3/256;
|
ecc_size = (PAGE_SIZE - OOB_SIZE) * 3 / 256;
|
||||||
_nanddrv_file_device.plane_num = 2;
|
_nanddrv_file_device.plane_num = 2;
|
||||||
_nanddrv_file_device.oob_size = OOB_SIZE;
|
_nanddrv_file_device.oob_size = OOB_SIZE;
|
||||||
_nanddrv_file_device.oob_free = OOB_SIZE - ecc_size;
|
_nanddrv_file_device.oob_free = OOB_SIZE - ecc_size;
|
||||||
_nanddrv_file_device.page_size = PAGE_SIZE - OOB_SIZE;
|
_nanddrv_file_device.page_size = PAGE_SIZE - OOB_SIZE;
|
||||||
_nanddrv_file_device.pages_per_block = PAGE_PER_BLOCK;
|
_nanddrv_file_device.pages_per_block = PAGE_PER_BLOCK;
|
||||||
_nanddrv_file_device.block_start = 0;
|
_nanddrv_file_device.block_start = 0;
|
||||||
_nanddrv_file_device.block_end = BLOCK_NUM/2;
|
_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.block_total = _nanddrv_file_device.block_end - _nanddrv_file_device.block_start;
|
||||||
_nanddrv_file_device.ops = &_ops;
|
_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)
|
#if defined(RT_USING_FINSH)
|
||||||
#include <finsh.h>
|
#include <finsh.h>
|
||||||
void nand_eraseall()
|
void nand_eraseall()
|
||||||
{
|
{
|
||||||
int index;
|
int index;
|
||||||
for (index = 0; index < _nanddrv_file_device.block_total; index ++)
|
for (index = 0; index < _nanddrv_file_device.block_total; index ++)
|
||||||
{
|
{
|
||||||
nanddrv_file_erase_block(&_nanddrv_file_device, index);
|
nanddrv_file_erase_block(&_nanddrv_file_device, index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
FINSH_FUNCTION_EXPORT(nand_eraseall, erase all of block in the nand flash);
|
FINSH_FUNCTION_EXPORT(nand_eraseall, erase all of block in the nand flash);
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
void nand_log(int level)
|
void nand_log(int level)
|
||||||
{
|
{
|
||||||
nftl_set_trace_level(level);
|
nftl_set_trace_level(level);
|
||||||
}
|
}
|
||||||
FINSH_FUNCTION_EXPORT(nand_log, set NFTL trace level);
|
FINSH_FUNCTION_EXPORT(nand_log, set NFTL trace level);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif //RT_USING_FINSH
|
#endif //RT_USING_FINSH
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <dfs_def.h>
|
#include <dfs_def.h>
|
||||||
|
|
||||||
// #define SD_TRACE rt_kprintf
|
// #define SD_TRACE rt_kprintf
|
||||||
#define SD_TRACE(...)
|
#define SD_TRACE(...)
|
||||||
|
|
||||||
//#define SDCARD_SIM "F:\\Project\\tools\\SDCARD"
|
//#define SDCARD_SIM "F:\\Project\\tools\\SDCARD"
|
||||||
|
@ -13,12 +13,12 @@
|
||||||
|
|
||||||
struct sdcard_device
|
struct sdcard_device
|
||||||
{
|
{
|
||||||
struct rt_device parent;
|
struct rt_device parent;
|
||||||
FILE* file;
|
FILE *file;
|
||||||
};
|
};
|
||||||
static struct sdcard_device _sdcard;
|
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;
|
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)
|
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)
|
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)
|
static rt_err_t rt_sdcard_close(rt_device_t dev)
|
||||||
{
|
{
|
||||||
return RT_EOK;
|
return RT_EOK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* position: block page address, not bytes address
|
/* position: block page address, not bytes address
|
||||||
* buffer:
|
* buffer:
|
||||||
* size : how many blocks
|
* 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;
|
struct sdcard_device *sd;
|
||||||
int result = 0;
|
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);
|
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);
|
result = fread(buffer, size * SECTOR_SIZE, 1, sd->file);
|
||||||
if (result < 0)
|
if (result < 0)
|
||||||
goto _err;
|
goto _err;
|
||||||
|
|
||||||
rt_mutex_release(lock);
|
rt_mutex_release(lock);
|
||||||
return size;
|
return size;
|
||||||
|
|
||||||
_err:
|
_err:
|
||||||
SD_TRACE("sd read errors!\n");
|
SD_TRACE("sd read errors!\n");
|
||||||
rt_mutex_release(lock);
|
rt_mutex_release(lock);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* position: block page address, not bytes address
|
/* position: block page address, not bytes address
|
||||||
* buffer:
|
* buffer:
|
||||||
* size : how many blocks
|
* 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;
|
struct sdcard_device *sd;
|
||||||
int result = 0;
|
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);
|
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);
|
result = fwrite(buffer, size * SECTOR_SIZE, 1, sd->file);
|
||||||
if (result < 0)
|
if (result < 0)
|
||||||
goto _err;
|
goto _err;
|
||||||
|
|
||||||
rt_mutex_release(lock);
|
rt_mutex_release(lock);
|
||||||
return size;
|
return size;
|
||||||
|
|
||||||
_err:
|
_err:
|
||||||
SD_TRACE("sd write errors!\n");
|
SD_TRACE("sd write errors!\n");
|
||||||
rt_mutex_release(lock);
|
rt_mutex_release(lock);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static rt_err_t rt_sdcard_control(rt_device_t dev, rt_uint8_t cmd, void *args)
|
static rt_err_t rt_sdcard_control(rt_device_t dev, rt_uint8_t cmd, void *args)
|
||||||
{
|
{
|
||||||
struct sdcard_device * sd;
|
struct sdcard_device *sd;
|
||||||
unsigned int size;
|
unsigned int size;
|
||||||
|
|
||||||
RT_ASSERT(dev != RT_NULL);
|
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->bytes_per_sector = SECTOR_SIZE;
|
||||||
geometry->block_size = SECTOR_SIZE;
|
geometry->block_size = SECTOR_SIZE;
|
||||||
|
|
||||||
fseek(sd->file, 0, SEEK_END);
|
fseek(sd->file, 0, SEEK_END);
|
||||||
size = ftell(sd->file);
|
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;
|
rt_uint32_t id, total_block;
|
||||||
struct sdcard_device * sd;
|
struct sdcard_device *sd;
|
||||||
struct rt_device * device;
|
struct rt_device *device;
|
||||||
|
|
||||||
sd = &_sdcard;
|
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 */
|
/* open sd card file, if not exist, then create it */
|
||||||
sd->file = fopen(SDCARD_SIM, "rb+");
|
sd->file = fopen(SDCARD_SIM, "rb+");
|
||||||
if (sd->file == NULL)
|
if (sd->file == NULL)
|
||||||
{
|
{
|
||||||
/* create a file to simulate sd card */
|
/* create a file to simulate sd card */
|
||||||
sd->file = fopen(SDCARD_SIM, "wb+");
|
sd->file = fopen(SDCARD_SIM, "wb+");
|
||||||
|
|
||||||
fseek(sd->file, 0, SEEK_END);
|
fseek(sd->file, 0, SEEK_END);
|
||||||
size = ftell(sd->file);
|
size = ftell(sd->file);
|
||||||
|
|
||||||
fseek(sd->file, 0, SEEK_SET );
|
fseek(sd->file, 0, SEEK_SET);
|
||||||
if (size < SDCARD_SIZE)
|
if (size < SDCARD_SIZE)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
unsigned char* ptr;
|
unsigned char *ptr;
|
||||||
|
|
||||||
ptr = (unsigned char*) malloc (1024 * 1024);
|
ptr = (unsigned char *) malloc(1024 * 1024);
|
||||||
if (ptr == NULL)
|
if (ptr == NULL)
|
||||||
{
|
{
|
||||||
SD_TRACE("malloc error, no memory!\n");
|
SD_TRACE("malloc error, no memory!\n");
|
||||||
return RT_ERROR;
|
return RT_ERROR;
|
||||||
}
|
}
|
||||||
memset(ptr, 0x0, 1024 * 1024);
|
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++)
|
for (i = 0; i < (SDCARD_SIZE / (1024 * 1024)); i++)
|
||||||
fwrite(ptr, 1024 * 1024, 1, sd->file);
|
fwrite(ptr, 1024 * 1024, 1, sd->file);
|
||||||
|
|
||||||
free(ptr);
|
free(ptr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fseek(sd->file, 0, SEEK_SET);
|
fseek(sd->file, 0, SEEK_SET);
|
||||||
|
|
||||||
device->type = RT_Device_Class_Block;
|
device->type = RT_Device_Class_Block;
|
||||||
device->init = rt_sdcard_init;
|
device->init = rt_sdcard_init;
|
||||||
device->open = rt_sdcard_open;
|
device->open = rt_sdcard_open;
|
||||||
device->close = rt_sdcard_close;
|
device->close = rt_sdcard_close;
|
||||||
device->read = rt_sdcard_read;
|
device->read = rt_sdcard_read;
|
||||||
device->write = rt_sdcard_write;
|
device->write = rt_sdcard_write;
|
||||||
device->control = rt_sdcard_control;
|
device->control = rt_sdcard_control;
|
||||||
device->user_data = NULL;
|
device->user_data = NULL;
|
||||||
|
|
||||||
rt_device_register(device, "sd0",
|
rt_device_register(device, "sd0",
|
||||||
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE);
|
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE);
|
||||||
|
|
||||||
return RT_EOK;
|
return RT_EOK;
|
||||||
}
|
}
|
||||||
|
@ -188,7 +188,7 @@ rt_err_t rt_hw_sdcard_init(const char * spi_device_name)
|
||||||
#include <finsh.h>
|
#include <finsh.h>
|
||||||
void eraseall(void)
|
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);
|
FINSH_FUNCTION_EXPORT(eraseall, erase all block in SPI flash);
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -4,132 +4,132 @@
|
||||||
#include <rtdevice.h>
|
#include <rtdevice.h>
|
||||||
#include <rtgui/driver.h>
|
#include <rtgui/driver.h>
|
||||||
|
|
||||||
#define SDL_SCREEN_WIDTH 800
|
#define SDL_SCREEN_WIDTH 800
|
||||||
#define SDL_SCREEN_HEIGHT 480
|
#define SDL_SCREEN_HEIGHT 480
|
||||||
|
|
||||||
struct sdlfb_device
|
struct sdlfb_device
|
||||||
{
|
{
|
||||||
struct rt_device parent;
|
struct rt_device parent;
|
||||||
|
|
||||||
SDL_Surface *screen;
|
SDL_Surface *screen;
|
||||||
rt_uint16_t width;
|
rt_uint16_t width;
|
||||||
rt_uint16_t height;
|
rt_uint16_t height;
|
||||||
};
|
};
|
||||||
struct sdlfb_device _device;
|
struct sdlfb_device _device;
|
||||||
|
|
||||||
/* common device interface */
|
/* common device interface */
|
||||||
static rt_err_t sdlfb_init(rt_device_t dev)
|
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)
|
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)
|
static rt_err_t sdlfb_close(rt_device_t dev)
|
||||||
{
|
{
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
return RT_EOK;
|
return RT_EOK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static rt_mutex_t sdllock;
|
static rt_mutex_t sdllock;
|
||||||
static rt_err_t sdlfb_control(rt_device_t dev, rt_uint8_t cmd, void *args)
|
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);
|
rt_mutex_take(sdllock, RT_WAITING_FOREVER);
|
||||||
device = (struct sdlfb_device*)dev;
|
device = (struct sdlfb_device *)dev;
|
||||||
RT_ASSERT(device != RT_NULL);
|
RT_ASSERT(device != RT_NULL);
|
||||||
RT_ASSERT(device->screen != RT_NULL);
|
RT_ASSERT(device->screen != RT_NULL);
|
||||||
|
|
||||||
switch (cmd)
|
switch (cmd)
|
||||||
{
|
{
|
||||||
case RTGRAPHIC_CTRL_GET_INFO:
|
case RTGRAPHIC_CTRL_GET_INFO:
|
||||||
{
|
{
|
||||||
struct rt_device_graphic_info *info;
|
struct rt_device_graphic_info *info;
|
||||||
|
|
||||||
info = (struct rt_device_graphic_info*) args;
|
info = (struct rt_device_graphic_info *) args;
|
||||||
info->bits_per_pixel = 16;
|
info->bits_per_pixel = 16;
|
||||||
info->pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565P;
|
info->pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565P;
|
||||||
info->framebuffer = device->screen->pixels;
|
info->framebuffer = device->screen->pixels;
|
||||||
info->width = device->screen->w;
|
info->width = device->screen->w;
|
||||||
info->height = device->screen->h;
|
info->height = device->screen->h;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case RTGRAPHIC_CTRL_RECT_UPDATE:
|
case RTGRAPHIC_CTRL_RECT_UPDATE:
|
||||||
{
|
{
|
||||||
struct rt_device_rect_info *rect;
|
struct rt_device_rect_info *rect;
|
||||||
rect = (struct rt_device_rect_info*)args;
|
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, rect->x, rect->y, rect->x + rect->w, rect->y + rect->h); */
|
||||||
SDL_UpdateRect(_device.screen, 0, 0, device->width, device->height);
|
SDL_UpdateRect(_device.screen, 0, 0, device->width, device->height);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case RTGRAPHIC_CTRL_SET_MODE:
|
case RTGRAPHIC_CTRL_SET_MODE:
|
||||||
{
|
{
|
||||||
#if 0
|
#if 0
|
||||||
struct rt_device_rect_info* rect;
|
struct rt_device_rect_info *rect;
|
||||||
|
|
||||||
rect = (struct rt_device_rect_info*)args;
|
rect = (struct rt_device_rect_info *)args;
|
||||||
if ((_device.width == rect->width) && (_device.height == rect->height)) return -RT_ERROR;
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
#endif
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
rt_mutex_release(sdllock);
|
rt_mutex_release(sdllock);
|
||||||
return RT_EOK;
|
return RT_EOK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void sdlfb_hw_init(void)
|
static void sdlfb_hw_init(void)
|
||||||
{
|
{
|
||||||
/* set video driver for VC++ debug */
|
/* set video driver for VC++ debug */
|
||||||
//_putenv("SDL_VIDEODRIVER=windib");
|
//_putenv("SDL_VIDEODRIVER=windib");
|
||||||
|
|
||||||
//if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO) < 0)
|
//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_EVERYTHING) < 0)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
_device.parent.init = sdlfb_init;
|
_device.parent.init = sdlfb_init;
|
||||||
_device.parent.open = sdlfb_open;
|
_device.parent.open = sdlfb_open;
|
||||||
_device.parent.close = sdlfb_close;
|
_device.parent.close = sdlfb_close;
|
||||||
_device.parent.read = RT_NULL;
|
_device.parent.read = RT_NULL;
|
||||||
_device.parent.write = RT_NULL;
|
_device.parent.write = RT_NULL;
|
||||||
_device.parent.control = sdlfb_control;
|
_device.parent.control = sdlfb_control;
|
||||||
|
|
||||||
_device.width = SDL_SCREEN_WIDTH;
|
_device.width = SDL_SCREEN_WIDTH;
|
||||||
_device.height = SDL_SCREEN_HEIGHT;
|
_device.height = SDL_SCREEN_HEIGHT;
|
||||||
_device.screen = SDL_SetVideoMode(_device.width, _device.height, 16, SDL_SWSURFACE | SDL_DOUBLEBUF);
|
_device.screen = SDL_SetVideoMode(_device.width, _device.height, 16, SDL_SWSURFACE | SDL_DOUBLEBUF);
|
||||||
if (_device.screen == NULL)
|
if (_device.screen == NULL)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
|
fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_WM_SetCaption ("RT-Thread/GUI Simulator", NULL);
|
SDL_WM_SetCaption("RT-Thread/GUI Simulator", NULL);
|
||||||
rt_device_register(RT_DEVICE(&_device), "sdl", RT_DEVICE_FLAG_RDWR);
|
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>
|
#include <windows.h>
|
||||||
|
@ -143,158 +143,158 @@ static void sdlfb_hw_init(void)
|
||||||
|
|
||||||
static DWORD WINAPI sdl_loop(LPVOID lpParam)
|
static DWORD WINAPI sdl_loop(LPVOID lpParam)
|
||||||
{
|
{
|
||||||
int quit = 0;
|
int quit = 0;
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
int button_state = 0;
|
int button_state = 0;
|
||||||
|
|
||||||
rt_device_t device;
|
rt_device_t device;
|
||||||
sdlfb_hw_init();
|
sdlfb_hw_init();
|
||||||
|
|
||||||
device = rt_device_find("sdl");
|
device = rt_device_find("sdl");
|
||||||
rtgui_graphic_set_device(device);
|
rtgui_graphic_set_device(device);
|
||||||
|
|
||||||
/* handle SDL event */
|
/* handle SDL event */
|
||||||
while(!quit)
|
while (!quit)
|
||||||
{
|
{
|
||||||
SDL_WaitEvent(&event);
|
SDL_WaitEvent(&event);
|
||||||
|
|
||||||
switch (event.type)
|
switch (event.type)
|
||||||
{
|
{
|
||||||
case SDL_MOUSEMOTION:
|
case SDL_MOUSEMOTION:
|
||||||
#if 0
|
#if 0
|
||||||
{
|
{
|
||||||
struct rtgui_event_mouse emouse;
|
struct rtgui_event_mouse emouse;
|
||||||
emouse.parent.type = RTGUI_EVENT_MOUSE_MOTION;
|
emouse.parent.type = RTGUI_EVENT_MOUSE_MOTION;
|
||||||
emouse.parent.sender = RT_NULL;
|
emouse.parent.sender = RT_NULL;
|
||||||
emouse.wid = RT_NULL;
|
emouse.wid = RT_NULL;
|
||||||
|
|
||||||
emouse.x = ((SDL_MouseMotionEvent*)&event)->x;
|
emouse.x = ((SDL_MouseMotionEvent *)&event)->x;
|
||||||
emouse.y = ((SDL_MouseMotionEvent*)&event)->y;
|
emouse.y = ((SDL_MouseMotionEvent *)&event)->y;
|
||||||
|
|
||||||
/* init mouse button */
|
/* init mouse button */
|
||||||
emouse.button = button_state;
|
emouse.button = button_state;
|
||||||
|
|
||||||
/* send event to server */
|
/* send event to server */
|
||||||
rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
|
rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SDL_MOUSEBUTTONDOWN:
|
case SDL_MOUSEBUTTONDOWN:
|
||||||
case SDL_MOUSEBUTTONUP:
|
case SDL_MOUSEBUTTONUP:
|
||||||
{
|
{
|
||||||
struct rtgui_event_mouse emouse;
|
struct rtgui_event_mouse emouse;
|
||||||
SDL_MouseButtonEvent* mb;
|
SDL_MouseButtonEvent *mb;
|
||||||
|
|
||||||
emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
|
emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
|
||||||
emouse.parent.sender = RT_NULL;
|
emouse.parent.sender = RT_NULL;
|
||||||
emouse.wid = RT_NULL;
|
emouse.wid = RT_NULL;
|
||||||
|
|
||||||
mb = (SDL_MouseButtonEvent*)&event;
|
mb = (SDL_MouseButtonEvent *)&event;
|
||||||
|
|
||||||
emouse.x = mb->x;
|
emouse.x = mb->x;
|
||||||
emouse.y = mb->y;
|
emouse.y = mb->y;
|
||||||
|
|
||||||
/* init mouse button */
|
/* init mouse button */
|
||||||
emouse.button = 0;
|
emouse.button = 0;
|
||||||
|
|
||||||
/* set emouse button */
|
/* set emouse button */
|
||||||
if (mb->button & (1 << (SDL_BUTTON_LEFT - 1)) )
|
if (mb->button & (1 << (SDL_BUTTON_LEFT - 1)))
|
||||||
{
|
{
|
||||||
emouse.button |= RTGUI_MOUSE_BUTTON_LEFT;
|
emouse.button |= RTGUI_MOUSE_BUTTON_LEFT;
|
||||||
}
|
}
|
||||||
else if (mb->button & (1 << (SDL_BUTTON_RIGHT - 1)))
|
else if (mb->button & (1 << (SDL_BUTTON_RIGHT - 1)))
|
||||||
{
|
{
|
||||||
emouse.button |= RTGUI_MOUSE_BUTTON_RIGHT;
|
emouse.button |= RTGUI_MOUSE_BUTTON_RIGHT;
|
||||||
}
|
}
|
||||||
else if (mb->button & (1 << (SDL_BUTTON_MIDDLE - 1)))
|
else if (mb->button & (1 << (SDL_BUTTON_MIDDLE - 1)))
|
||||||
{
|
{
|
||||||
emouse.button |= RTGUI_MOUSE_BUTTON_MIDDLE;
|
emouse.button |= RTGUI_MOUSE_BUTTON_MIDDLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mb->type == SDL_MOUSEBUTTONDOWN)
|
if (mb->type == SDL_MOUSEBUTTONDOWN)
|
||||||
{
|
{
|
||||||
emouse.button |= RTGUI_MOUSE_BUTTON_DOWN;
|
emouse.button |= RTGUI_MOUSE_BUTTON_DOWN;
|
||||||
button_state = emouse.button;
|
button_state = emouse.button;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
emouse.button |= RTGUI_MOUSE_BUTTON_UP;
|
emouse.button |= RTGUI_MOUSE_BUTTON_UP;
|
||||||
button_state = 0;
|
button_state = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* send event to server */
|
/* send event to server */
|
||||||
rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
|
rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SDL_KEYUP:
|
case SDL_KEYUP:
|
||||||
{
|
{
|
||||||
struct rtgui_event_kbd ekbd;
|
struct rtgui_event_kbd ekbd;
|
||||||
ekbd.parent.type = RTGUI_EVENT_KBD;
|
ekbd.parent.type = RTGUI_EVENT_KBD;
|
||||||
ekbd.parent.sender = RT_NULL;
|
ekbd.parent.sender = RT_NULL;
|
||||||
ekbd.type = RTGUI_KEYUP;
|
ekbd.type = RTGUI_KEYUP;
|
||||||
ekbd.wid = RT_NULL;
|
ekbd.wid = RT_NULL;
|
||||||
ekbd.mod = event.key.keysym.mod;
|
ekbd.mod = event.key.keysym.mod;
|
||||||
ekbd.key = event.key.keysym.sym;
|
ekbd.key = event.key.keysym.sym;
|
||||||
|
|
||||||
/* FIXME: unicode */
|
/* FIXME: unicode */
|
||||||
ekbd.unicode = 0;
|
ekbd.unicode = 0;
|
||||||
|
|
||||||
/* send event to server */
|
/* send event to server */
|
||||||
rtgui_server_post_event(&ekbd.parent, sizeof(struct rtgui_event_kbd));
|
rtgui_server_post_event(&ekbd.parent, sizeof(struct rtgui_event_kbd));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SDL_KEYDOWN:
|
case SDL_KEYDOWN:
|
||||||
{
|
{
|
||||||
struct rtgui_event_kbd ekbd;
|
struct rtgui_event_kbd ekbd;
|
||||||
ekbd.parent.type = RTGUI_EVENT_KBD;
|
ekbd.parent.type = RTGUI_EVENT_KBD;
|
||||||
ekbd.parent.sender = RT_NULL;
|
ekbd.parent.sender = RT_NULL;
|
||||||
ekbd.type = RTGUI_KEYDOWN;
|
ekbd.type = RTGUI_KEYDOWN;
|
||||||
ekbd.wid = RT_NULL;
|
ekbd.wid = RT_NULL;
|
||||||
ekbd.mod = event.key.keysym.mod;
|
ekbd.mod = event.key.keysym.mod;
|
||||||
ekbd.key = event.key.keysym.sym;
|
ekbd.key = event.key.keysym.sym;
|
||||||
|
|
||||||
/* FIXME: unicode */
|
/* FIXME: unicode */
|
||||||
ekbd.unicode = 0;
|
ekbd.unicode = 0;
|
||||||
|
|
||||||
/* send event to server */
|
/* send event to server */
|
||||||
rtgui_server_post_event(&ekbd.parent, sizeof(struct rtgui_event_kbd));
|
rtgui_server_post_event(&ekbd.parent, sizeof(struct rtgui_event_kbd));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SDL_QUIT:
|
case SDL_QUIT:
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
quit = 1;
|
quit = 1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (quit)
|
if (quit)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
//exit(0);
|
//exit(0);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* start sdl thread */
|
/* start sdl thread */
|
||||||
void rt_hw_sdl_start(void)
|
void rt_hw_sdl_start(void)
|
||||||
{
|
{
|
||||||
HANDLE thread;
|
HANDLE thread;
|
||||||
DWORD thread_id;
|
DWORD thread_id;
|
||||||
|
|
||||||
/* create thread that loop sdl event */
|
/* create thread that loop sdl event */
|
||||||
thread = CreateThread(NULL,
|
thread = CreateThread(NULL,
|
||||||
0,
|
0,
|
||||||
(LPTHREAD_START_ROUTINE)sdl_loop,
|
(LPTHREAD_START_ROUTINE)sdl_loop,
|
||||||
0,
|
0,
|
||||||
CREATE_SUSPENDED,
|
CREATE_SUSPENDED,
|
||||||
&thread_id);
|
&thread_id);
|
||||||
if(thread == NULL)
|
if (thread == NULL)
|
||||||
{
|
{
|
||||||
//Display Error Message
|
//Display Error Message
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
******************************************************************************
|
******************************************************************************
|
||||||
* By : parai
|
* By : parai
|
||||||
* email:parai@foxmail.com
|
* email:parai@foxmail.com
|
||||||
* 这并不是一个真的串口设备,只是为了能够让内核打印信息而创建
|
* 这并不是一个真的串口设备,只是为了能够让内核打印信息而创建
|
||||||
******************************************************************************
|
******************************************************************************
|
||||||
*/
|
*/
|
||||||
|
@ -20,141 +20,143 @@ extern struct serial_int_rx serial_rx;
|
||||||
/**
|
/**
|
||||||
* This function initializes serial
|
* 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_ACTIVATED))
|
||||||
{
|
{
|
||||||
if (dev->flag & RT_DEVICE_FLAG_INT_RX)
|
if (dev->flag & RT_DEVICE_FLAG_INT_RX)
|
||||||
{
|
{
|
||||||
rt_memset(serial_rx.rx_buffer, 0,
|
rt_memset(serial_rx.rx_buffer, 0,
|
||||||
sizeof(serial_rx.rx_buffer));
|
sizeof(serial_rx.rx_buffer));
|
||||||
serial_rx.read_index = 0;
|
serial_rx.read_index = 0;
|
||||||
serial_rx.save_index = 0;
|
serial_rx.save_index = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
|
dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
|
||||||
}
|
}
|
||||||
return RT_EOK;
|
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
|
#if _DEBUG_SERIAL==1
|
||||||
printf("in rt_serial_open()\n");
|
printf("in rt_serial_open()\n");
|
||||||
#endif
|
#endif
|
||||||
return RT_EOK;
|
return RT_EOK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static rt_err_t rt_serial_close(rt_device_t dev)
|
static rt_err_t rt_serial_close(rt_device_t dev)
|
||||||
{
|
{
|
||||||
#if _DEBUG_SERIAL==1
|
#if _DEBUG_SERIAL==1
|
||||||
printf("in rt_serial_close()\n");
|
printf("in rt_serial_close()\n");
|
||||||
#endif
|
#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_uint8_t *ptr;
|
||||||
rt_err_t err_code;
|
rt_err_t err_code;
|
||||||
|
|
||||||
ptr = buffer;
|
ptr = buffer;
|
||||||
err_code = RT_EOK;
|
err_code = RT_EOK;
|
||||||
|
|
||||||
if (dev->flag & RT_DEVICE_FLAG_INT_RX)
|
if (dev->flag & RT_DEVICE_FLAG_INT_RX)
|
||||||
{
|
{
|
||||||
/* interrupt mode Rx */
|
/* interrupt mode Rx */
|
||||||
while (size)
|
while (size)
|
||||||
{
|
{
|
||||||
rt_base_t level;
|
rt_base_t level;
|
||||||
|
|
||||||
/* disable interrupt */
|
/* disable interrupt */
|
||||||
level = rt_hw_interrupt_disable();
|
level = rt_hw_interrupt_disable();
|
||||||
|
|
||||||
if (serial_rx.read_index != serial_rx.save_index)
|
if (serial_rx.read_index != serial_rx.save_index)
|
||||||
{
|
{
|
||||||
/* read a character */
|
/* read a character */
|
||||||
*ptr++ = serial_rx.rx_buffer[serial_rx.read_index];
|
*ptr++ = serial_rx.rx_buffer[serial_rx.read_index];
|
||||||
size--;
|
size--;
|
||||||
|
|
||||||
/* move to next position */
|
/* move to next position */
|
||||||
serial_rx.read_index ++;
|
serial_rx.read_index ++;
|
||||||
if (serial_rx.read_index >= SERIAL_RX_BUFFER_SIZE)
|
if (serial_rx.read_index >= SERIAL_RX_BUFFER_SIZE)
|
||||||
serial_rx.read_index = 0;
|
serial_rx.read_index = 0;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* set error code */
|
/* set error code */
|
||||||
err_code = -RT_EEMPTY;
|
err_code = -RT_EEMPTY;
|
||||||
|
|
||||||
/* enable interrupt */
|
/* enable interrupt */
|
||||||
rt_hw_interrupt_enable(level);
|
rt_hw_interrupt_enable(level);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* enable interrupt */
|
/* enable interrupt */
|
||||||
rt_hw_interrupt_enable(level);
|
rt_hw_interrupt_enable(level);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* set error code */
|
/* set error code */
|
||||||
rt_set_errno(err_code);
|
rt_set_errno(err_code);
|
||||||
return (rt_uint32_t)ptr - (rt_uint32_t)buffer;
|
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
|
#if _DEBUG_SERIAL==1
|
||||||
printf("in rt_serial_write()\n");
|
printf("in rt_serial_write()\n");
|
||||||
#endif
|
#endif
|
||||||
printf("%s",(char*)buffer);
|
printf("%s", (char *)buffer);
|
||||||
return size;
|
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){
|
switch (cmd)
|
||||||
case RT_DEVICE_CTRL_SUSPEND:
|
{
|
||||||
/* suspend device */
|
case RT_DEVICE_CTRL_SUSPEND:
|
||||||
dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
|
/* suspend device */
|
||||||
break;
|
dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
|
||||||
|
break;
|
||||||
|
|
||||||
case RT_DEVICE_CTRL_RESUME:
|
case RT_DEVICE_CTRL_RESUME:
|
||||||
/* resume device */
|
/* resume device */
|
||||||
dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
|
dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return RT_EOK;
|
return RT_EOK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* serial register
|
* 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
|
#if _DEBUG_SERIAL==1
|
||||||
printf("in rt_serial_register()\n");
|
printf("in rt_serial_register()\n");
|
||||||
#endif
|
#endif
|
||||||
device->type = RT_Device_Class_Char;
|
device->type = RT_Device_Class_Char;
|
||||||
device->rx_indicate = RT_NULL;
|
device->rx_indicate = RT_NULL;
|
||||||
device->tx_complete = RT_NULL;
|
device->tx_complete = RT_NULL;
|
||||||
device->init = rt_serial_init;
|
device->init = rt_serial_init;
|
||||||
device->open = rt_serial_open;
|
device->open = rt_serial_open;
|
||||||
device->close = rt_serial_close;
|
device->close = rt_serial_close;
|
||||||
device->read = rt_serial_read;
|
device->read = rt_serial_read;
|
||||||
device->write = rt_serial_write;
|
device->write = rt_serial_write;
|
||||||
device->control = rt_serial_control;
|
device->control = rt_serial_control;
|
||||||
device->user_data = RT_NULL;
|
device->user_data = RT_NULL;
|
||||||
|
|
||||||
/* register a character device */
|
/* register a character device */
|
||||||
return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | flag);
|
return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | flag);
|
||||||
}
|
}
|
||||||
|
|
||||||
rt_err_t rt_hw_serial_init(void)
|
rt_err_t rt_hw_serial_init(void)
|
||||||
{
|
{
|
||||||
return rt_hw_serial_register(&serial_device,RT_CONSOLE_DEVICE_NAME,
|
return rt_hw_serial_register(&serial_device, RT_CONSOLE_DEVICE_NAME,
|
||||||
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM);
|
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM);
|
||||||
}
|
}
|
|
@ -4,8 +4,8 @@
|
||||||
* BANKED MEMORY MODEL
|
* BANKED MEMORY MODEL
|
||||||
*
|
*
|
||||||
* File : rthw.c
|
* File : rthw.c
|
||||||
* By : parai
|
* By : parai
|
||||||
* email:parai@foxmail.com
|
* email:parai@foxmail.com
|
||||||
*******************************************************************************************************/
|
*******************************************************************************************************/
|
||||||
|
|
||||||
#ifndef __RT_HW_SERIAL_H__
|
#ifndef __RT_HW_SERIAL_H__
|
||||||
|
@ -14,8 +14,8 @@
|
||||||
#define SERIAL_RX_BUFFER_SIZE 80
|
#define SERIAL_RX_BUFFER_SIZE 80
|
||||||
struct serial_int_rx
|
struct serial_int_rx
|
||||||
{
|
{
|
||||||
rt_uint8_t rx_buffer[SERIAL_RX_BUFFER_SIZE];
|
rt_uint8_t rx_buffer[SERIAL_RX_BUFFER_SIZE];
|
||||||
rt_uint32_t read_index, save_index;
|
rt_uint32_t read_index, save_index;
|
||||||
};
|
};
|
||||||
|
|
||||||
rt_err_t rt_hw_serial_init(void);
|
rt_err_t rt_hw_serial_init(void);
|
||||||
|
|
|
@ -1,18 +1,18 @@
|
||||||
/*
|
/*
|
||||||
* File : sst25vfxx_mtd.h
|
* File : sst25vfxx_mtd.h
|
||||||
* This file is part of RT-Thread RTOS
|
* This file is part of RT-Thread RTOS
|
||||||
* COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team
|
* COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team
|
||||||
*
|
*
|
||||||
* The license and distribution terms for this file may be
|
* The license and distribution terms for this file may be
|
||||||
* found in the file LICENSE in this distribution or at
|
* found in the file LICENSE in this distribution or at
|
||||||
* http://www.rt-thread.org/license/LICENSE
|
* http://www.rt-thread.org/license/LICENSE
|
||||||
*
|
*
|
||||||
* Change Logs:
|
* Change Logs:
|
||||||
* Date Author Notes
|
* Date Author Notes
|
||||||
* 2011-12-16 aozima the first version
|
* 2011-12-16 aozima the first version
|
||||||
* 2012-02-01 mbbill MTD driver version
|
* 2012-02-01 mbbill MTD driver version
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef SST25VFXX_MTD_H
|
#ifndef SST25VFXX_MTD_H
|
||||||
#define 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);
|
rt_err_t sst25vfxx_mtd_init(const char *spi_device_name, rt_uint32_t block_start, rt_uint32_t block_end);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -32,179 +32,179 @@
|
||||||
#define BLOCK_SIZE (64*1024)
|
#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 sst25_mtd
|
||||||
{
|
{
|
||||||
struct rt_mtd_nor_device parent;
|
struct rt_mtd_nor_device parent;
|
||||||
FILE * file;
|
FILE *file;
|
||||||
};
|
};
|
||||||
static struct sst25_mtd _sst25_mtd;
|
static struct sst25_mtd _sst25_mtd;
|
||||||
|
|
||||||
static struct rt_mutex flash_lock;
|
static struct rt_mutex flash_lock;
|
||||||
|
|
||||||
/* RT-Thread MTD device interface */
|
/* 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;
|
struct sst25_mtd *sst25;
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
sst25 = SST25_MTD(device);
|
sst25 = SST25_MTD(device);
|
||||||
RT_ASSERT(sst25 != RT_NULL);
|
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);
|
fseek(sst25->file, position, SEEK_SET);
|
||||||
result = fread(data, size, 1, sst25->file);
|
result = fread(data, size, 1, sst25->file);
|
||||||
if (result < 0)
|
if (result < 0)
|
||||||
rt_kprintf("sst read error.\n");
|
rt_kprintf("sst read error.\n");
|
||||||
|
|
||||||
rt_mutex_release(&flash_lock);
|
rt_mutex_release(&flash_lock);
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int sst25vfxx_write(struct rt_mtd_nor_device* device, rt_off_t position,
|
static int sst25vfxx_write(struct rt_mtd_nor_device *device, rt_off_t position,
|
||||||
const rt_uint8_t *data, rt_size_t size)
|
const rt_uint8_t *data, rt_size_t size)
|
||||||
{
|
{
|
||||||
struct sst25_mtd *sst25;
|
struct sst25_mtd *sst25;
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
sst25 = SST25_MTD(device);
|
sst25 = SST25_MTD(device);
|
||||||
RT_ASSERT(sst25 != RT_NULL);
|
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);
|
fseek(sst25->file, position, SEEK_SET);
|
||||||
result = fwrite(data, size, 1, sst25->file);
|
result = fwrite(data, size, 1, sst25->file);
|
||||||
if (result < 0)
|
if (result < 0)
|
||||||
rt_kprintf("sst write error.\n");
|
rt_kprintf("sst write error.\n");
|
||||||
|
|
||||||
rt_mutex_release(&flash_lock);
|
rt_mutex_release(&flash_lock);
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char block_buffer[BLOCK_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;
|
struct sst25_mtd *sst25;
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
sst25 = SST25_MTD(device);
|
sst25 = SST25_MTD(device);
|
||||||
|
|
||||||
RT_ASSERT(sst25 != RT_NULL);
|
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);
|
memset(block_buffer, 0xFF, BLOCK_SIZE);
|
||||||
fseek(sst25->file, block, SEEK_SET);
|
fseek(sst25->file, block, SEEK_SET);
|
||||||
|
|
||||||
result = fwrite(block_buffer, BLOCK_SIZE, 1, sst25->file);
|
result = fwrite(block_buffer, BLOCK_SIZE, 1, sst25->file);
|
||||||
if (result < 0)
|
if (result < 0)
|
||||||
rt_kprintf("sst write error.\n");
|
rt_kprintf("sst write error.\n");
|
||||||
|
|
||||||
rt_mutex_release(&flash_lock);
|
rt_mutex_release(&flash_lock);
|
||||||
return RT_EOK;
|
return RT_EOK;
|
||||||
}
|
}
|
||||||
|
|
||||||
const static struct rt_mtd_nor_driver_ops sst25vfxx_mtd_ops =
|
const static struct rt_mtd_nor_driver_ops sst25vfxx_mtd_ops =
|
||||||
{
|
{
|
||||||
sst25vfxx_read_id,
|
sst25vfxx_read_id,
|
||||||
sst25vfxx_read,
|
sst25vfxx_read,
|
||||||
sst25vfxx_write,
|
sst25vfxx_write,
|
||||||
sst25vfxx_erase_block,
|
sst25vfxx_erase_block,
|
||||||
};
|
};
|
||||||
static rt_err_t sst25vfxx_hw_init(struct sst25_mtd *mtd)
|
static rt_err_t sst25vfxx_hw_init(struct sst25_mtd *mtd)
|
||||||
{
|
{
|
||||||
mtd = mtd;
|
mtd = mtd;
|
||||||
return RT_EOK;
|
return RT_EOK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SST25vfxx API
|
* SST25vfxx API
|
||||||
*/
|
*/
|
||||||
rt_err_t sst25vfxx_mtd_init(const char * nor_name,
|
rt_err_t sst25vfxx_mtd_init(const char *nor_name,
|
||||||
rt_uint32_t block_start,
|
rt_uint32_t block_start,
|
||||||
rt_uint32_t block_end)
|
rt_uint32_t block_end)
|
||||||
{
|
{
|
||||||
rt_uint32_t id, total_block;
|
rt_uint32_t id, total_block;
|
||||||
struct sst25_mtd * sst25;
|
struct sst25_mtd *sst25;
|
||||||
struct rt_mtd_nor_device *mtd;
|
struct rt_mtd_nor_device *mtd;
|
||||||
|
|
||||||
|
|
||||||
sst25 = &_sst25_mtd;
|
sst25 = &_sst25_mtd;
|
||||||
mtd = &(sst25->parent);
|
mtd = &(sst25->parent);
|
||||||
|
|
||||||
/* set page size and block size */
|
/* set page size and block size */
|
||||||
mtd->block_size = 64 * 1024; /* 64kByte */
|
mtd->block_size = 64 * 1024; /* 64kByte */
|
||||||
mtd->ops = &sst25vfxx_mtd_ops;
|
mtd->ops = &sst25vfxx_mtd_ops;
|
||||||
|
|
||||||
/* initialize mutex */
|
/* initialize mutex */
|
||||||
if (rt_mutex_init(&flash_lock, nor_name, RT_IPC_FLAG_FIFO) != RT_EOK)
|
if (rt_mutex_init(&flash_lock, nor_name, RT_IPC_FLAG_FIFO) != RT_EOK)
|
||||||
{
|
{
|
||||||
rt_kprintf("init sd lock mutex failed\n");
|
rt_kprintf("init sd lock mutex failed\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* initialize flash */
|
/* initialize flash */
|
||||||
id = sst25vfxx_read_id(mtd);
|
id = sst25vfxx_read_id(mtd);
|
||||||
switch (id & 0xff)
|
switch (id & 0xff)
|
||||||
{
|
{
|
||||||
case MC_ID_SST25VF016:
|
case MC_ID_SST25VF016:
|
||||||
total_block = (16 * 1024 * 1024 / 8) / mtd->block_size;
|
total_block = (16 * 1024 * 1024 / 8) / mtd->block_size;
|
||||||
break;
|
break;
|
||||||
case MC_ID_SST25VF032:
|
case MC_ID_SST25VF032:
|
||||||
total_block = (32 * 1024 * 1024 / 8) / mtd->block_size;
|
total_block = (32 * 1024 * 1024 / 8) / mtd->block_size;
|
||||||
break;
|
break;
|
||||||
case MC_ID_SST25VF064:
|
case MC_ID_SST25VF064:
|
||||||
total_block = (64 * 1024 * 1024 / 8) / mtd->block_size;
|
total_block = (64 * 1024 * 1024 / 8) / mtd->block_size;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
rt_kprintf("SST25 detection error, id: %x\n", id);
|
rt_kprintf("SST25 detection error, id: %x\n", id);
|
||||||
return -RT_ERROR;
|
return -RT_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((block_end == RT_UINT32_MAX) || (block_end == 0))
|
if ((block_end == RT_UINT32_MAX) || (block_end == 0))
|
||||||
{
|
{
|
||||||
block_end = total_block;
|
block_end = total_block;
|
||||||
}
|
}
|
||||||
else if (block_end > total_block)
|
else if (block_end > total_block)
|
||||||
{
|
{
|
||||||
rt_kprintf("SST25 total block: %d, out of block\n", total_block);
|
rt_kprintf("SST25 total block: %d, out of block\n", total_block);
|
||||||
return -RT_ERROR;
|
return -RT_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
mtd->block_start = block_start;
|
mtd->block_start = block_start;
|
||||||
mtd->block_end = block_end;
|
mtd->block_end = block_end;
|
||||||
|
|
||||||
/* open nor file, if not exist, then create it */
|
/* open nor file, if not exist, then create it */
|
||||||
sst25->file = fopen(NOR_SIM, "rb+");
|
sst25->file = fopen(NOR_SIM, "rb+");
|
||||||
if (sst25->file == NULL)
|
if (sst25->file == NULL)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
/* create a file to simulate nor */
|
/* create a file to simulate nor */
|
||||||
sst25->file = fopen(NOR_SIM, "wb+");
|
sst25->file = fopen(NOR_SIM, "wb+");
|
||||||
|
|
||||||
memset(block_buffer, 0xFF, sizeof(block_buffer));
|
memset(block_buffer, 0xFF, sizeof(block_buffer));
|
||||||
for(i=0; i<total_block; i++)
|
for (i = 0; i < total_block; i++)
|
||||||
{
|
{
|
||||||
fseek(sst25->file, i * BLOCK_SIZE, SEEK_SET);
|
fseek(sst25->file, i * BLOCK_SIZE, SEEK_SET);
|
||||||
fwrite(block_buffer, BLOCK_SIZE, 1, sst25->file);
|
fwrite(block_buffer, BLOCK_SIZE, 1, sst25->file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fseek(sst25->file, 0, SEEK_SET);
|
fseek(sst25->file, 0, SEEK_SET);
|
||||||
|
|
||||||
/* initialize hardware */
|
/* initialize hardware */
|
||||||
sst25vfxx_hw_init(&_sst25_mtd);
|
sst25vfxx_hw_init(&_sst25_mtd);
|
||||||
|
|
||||||
/* register MTD device */
|
/* register MTD device */
|
||||||
rt_mtd_nor_register_device("nor", mtd);
|
rt_mtd_nor_register_device("nor", mtd);
|
||||||
|
|
||||||
return RT_EOK;
|
return RT_EOK;
|
||||||
}
|
}
|
||||||
|
@ -213,14 +213,14 @@ rt_err_t sst25vfxx_mtd_init(const char * nor_name,
|
||||||
#include <finsh.h>
|
#include <finsh.h>
|
||||||
void nor_erase(void)
|
void nor_erase(void)
|
||||||
{
|
{
|
||||||
rt_uint32_t index;
|
rt_uint32_t index;
|
||||||
struct rt_mtd_nor_device *mtd;
|
struct rt_mtd_nor_device *mtd;
|
||||||
|
|
||||||
mtd = SST25_MTD(&_sst25_mtd);
|
mtd = SST25_MTD(&_sst25_mtd);
|
||||||
for (index = mtd->block_start; index < mtd->block_end; index ++)
|
for (index = mtd->block_start; index < mtd->block_end; index ++)
|
||||||
{
|
{
|
||||||
sst25vfxx_erase_block(mtd, index * mtd->block_size);
|
sst25vfxx_erase_block(mtd, index * mtd->block_size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
FINSH_FUNCTION_EXPORT(nor_erase, erase all block in SPI flash);
|
FINSH_FUNCTION_EXPORT(nor_erase, erase all block in SPI flash);
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -17,17 +17,17 @@ static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam);
|
||||||
void rt_hw_usart_init(void)
|
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,
|
OSKey_Thread = CreateThread(NULL,
|
||||||
0,
|
0,
|
||||||
(LPTHREAD_START_ROUTINE)ThreadforKeyGet,
|
(LPTHREAD_START_ROUTINE)ThreadforKeyGet,
|
||||||
0,
|
0,
|
||||||
CREATE_SUSPENDED,
|
CREATE_SUSPENDED,
|
||||||
&OSKey_ThreadID);
|
&OSKey_ThreadID);
|
||||||
if(OSKey_Thread == NULL)
|
if (OSKey_Thread == NULL)
|
||||||
{
|
{
|
||||||
//Display Error Message
|
//Display Error Message
|
||||||
|
|
||||||
|
@ -47,85 +47,85 @@ void rt_hw_usart_init(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 方向键(←): 0xe04b
|
* 方向键(←): 0xe04b
|
||||||
* 方向键(↑): 0xe048
|
* 方向键(↑): 0xe048
|
||||||
* 方向键(→): 0xe04d
|
* 方向键(→): 0xe04d
|
||||||
* 方向键(↓): 0xe050
|
* 方向键(↓): 0xe050
|
||||||
*/
|
*/
|
||||||
static int savekey(unsigned char key)
|
static int savekey(unsigned char key)
|
||||||
{
|
{
|
||||||
/* save on rx buffer */
|
/* save on rx buffer */
|
||||||
{
|
{
|
||||||
rt_base_t level;
|
rt_base_t level;
|
||||||
|
|
||||||
/* disable interrupt */
|
/* disable interrupt */
|
||||||
//暂时关闭中断,因为要操作uart数据结构
|
//暂时关闭中断,因为要操作uart数据结构
|
||||||
level = rt_hw_interrupt_disable();
|
level = rt_hw_interrupt_disable();
|
||||||
|
|
||||||
/* save character */
|
/* save character */
|
||||||
serial_rx.rx_buffer[serial_rx.save_index] = key;
|
serial_rx.rx_buffer[serial_rx.save_index] = key;
|
||||||
serial_rx.save_index ++;
|
serial_rx.save_index ++;
|
||||||
//下面的代码检查save_index是否已经到到缓冲区尾部,如果是则回转到头部,称为一个环形缓冲区
|
//下面的代码检查save_index是否已经到到缓冲区尾部,如果是则回转到头部,称为一个环形缓冲区
|
||||||
if (serial_rx.save_index >= SERIAL_RX_BUFFER_SIZE)
|
if (serial_rx.save_index >= SERIAL_RX_BUFFER_SIZE)
|
||||||
serial_rx.save_index = 0;
|
serial_rx.save_index = 0;
|
||||||
|
|
||||||
//这种情况表示反转后的save_index追上了read_index,则增大read_index,丢弃一个旧的数据
|
//这种情况表示反转后的save_index追上了read_index,则增大read_index,丢弃一个旧的数据
|
||||||
/* if the next position is read index, discard this 'read char' */
|
/* if the next position is read index, discard this 'read char' */
|
||||||
if (serial_rx.save_index == serial_rx.read_index)
|
if (serial_rx.save_index == serial_rx.read_index)
|
||||||
{
|
{
|
||||||
serial_rx.read_index ++;
|
serial_rx.read_index ++;
|
||||||
if (serial_rx.read_index >= SERIAL_RX_BUFFER_SIZE)
|
if (serial_rx.read_index >= SERIAL_RX_BUFFER_SIZE)
|
||||||
serial_rx.read_index = 0;
|
serial_rx.read_index = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* enable interrupt */
|
/* enable interrupt */
|
||||||
//uart数据结构已经操作完成,重新使能中断
|
//uart数据结构已经操作完成,重新使能中断
|
||||||
rt_hw_interrupt_enable(level);
|
rt_hw_interrupt_enable(level);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* invoke callback */
|
/* invoke callback */
|
||||||
if (serial_device.rx_indicate != RT_NULL)
|
if (serial_device.rx_indicate != RT_NULL)
|
||||||
{
|
{
|
||||||
rt_size_t rx_length;
|
rt_size_t rx_length;
|
||||||
|
|
||||||
/* get rx length */
|
/* get rx length */
|
||||||
rx_length = serial_rx.read_index > serial_rx.save_index ?
|
rx_length = serial_rx.read_index > serial_rx.save_index ?
|
||||||
SERIAL_RX_BUFFER_SIZE - 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_rx.save_index - serial_rx.read_index;
|
||||||
|
|
||||||
serial_device.rx_indicate(&serial_device, rx_length);
|
serial_device.rx_indicate(&serial_device, rx_length);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam)
|
static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam)
|
||||||
{
|
{
|
||||||
unsigned char key;
|
unsigned char key;
|
||||||
|
|
||||||
(void)lpParam; //prevent compiler warnings
|
(void)lpParam; //prevent compiler warnings
|
||||||
|
|
||||||
for(;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
key = _getch();//getchar();
|
key = _getch();//getchar();
|
||||||
if (key == 0xE0)
|
if (key == 0xE0)
|
||||||
{
|
{
|
||||||
key = _getch();
|
key = _getch();
|
||||||
|
|
||||||
if (key == 0x48) //up key , 0x1b 0x5b 0x41
|
if (key == 0x48) //up key , 0x1b 0x5b 0x41
|
||||||
{
|
{
|
||||||
savekey(0x1b);
|
savekey(0x1b);
|
||||||
savekey(0x5b);
|
savekey(0x5b);
|
||||||
savekey(0x41);
|
savekey(0x41);
|
||||||
}
|
}
|
||||||
else if (key == 0x50)//0x1b 0x5b 0x42
|
else if (key == 0x50)//0x1b 0x5b 0x42
|
||||||
{
|
{
|
||||||
savekey(0x1b);
|
savekey(0x1b);
|
||||||
savekey(0x5b);
|
savekey(0x5b);
|
||||||
savekey(0x42);
|
savekey(0x42);
|
||||||
}
|
}
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
savekey(key);
|
savekey(key);
|
||||||
}
|
}
|
||||||
} /*** ThreadforKeyGet ***/
|
} /*** ThreadforKeyGet ***/
|
|
@ -1,271 +1,271 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <rtthread.h>
|
#include <rtthread.h>
|
||||||
#include "snake.h"
|
#include "snake.h"
|
||||||
|
|
||||||
#define ASSERT_RET(x, ret) \
|
#define ASSERT_RET(x, ret) \
|
||||||
do{ \
|
do{ \
|
||||||
if (x) \
|
if (x) \
|
||||||
return ret; \
|
return ret; \
|
||||||
}while(0)
|
}while(0)
|
||||||
|
|
||||||
rt_list_t snake_head;
|
rt_list_t snake_head;
|
||||||
SNAKE_DIR prevdir, newdir;
|
SNAKE_DIR prevdir, newdir;
|
||||||
|
|
||||||
static SNAKE_DIR dir_adjust(SNAKE_DIR dir)
|
static SNAKE_DIR dir_adjust(SNAKE_DIR dir)
|
||||||
{
|
{
|
||||||
if ( (SNAKE_DIR_UP == prevdir && SNAKE_DIR_DOWN != dir)
|
if ((SNAKE_DIR_UP == prevdir && SNAKE_DIR_DOWN != dir)
|
||||||
|| (SNAKE_DIR_DOWN == prevdir && SNAKE_DIR_UP != dir)
|
|| (SNAKE_DIR_DOWN == prevdir && SNAKE_DIR_UP != dir)
|
||||||
|| (SNAKE_DIR_LEFT == prevdir && SNAKE_DIR_RIGHT != dir)
|
|| (SNAKE_DIR_LEFT == prevdir && SNAKE_DIR_RIGHT != dir)
|
||||||
|| (SNAKE_DIR_RIGHT == prevdir && SNAKE_DIR_LEFT != dir)
|
|| (SNAKE_DIR_RIGHT == prevdir && SNAKE_DIR_LEFT != dir)
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
newdir = dir;
|
newdir = dir;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
rt_kprintf("dirction change error\n\r");
|
rt_kprintf("dirction change error\n\r");
|
||||||
}
|
}
|
||||||
|
|
||||||
return newdir;
|
return newdir;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void across_XY(point_t *node, const map_t *map)
|
static void across_XY(point_t *node, const map_t *map)
|
||||||
{
|
{
|
||||||
RT_ASSERT(node != RT_NULL && map != RT_NULL);
|
RT_ASSERT(node != RT_NULL && map != RT_NULL);
|
||||||
|
|
||||||
// 如果长度超出当前边框则可以穿越墙到对面
|
// 如果长度超出当前边框则可以穿越墙到对面
|
||||||
node->x = (node->x + map->width) % map->width;
|
node->x = (node->x + map->width) % map->width;
|
||||||
node->y = (node->y + map->height) % map->height;
|
node->y = (node->y + map->height) % map->height;
|
||||||
}
|
}
|
||||||
|
|
||||||
static SYS_STE node_update(snake_t *tail, const point_t *node, map_t *map)
|
static SYS_STE node_update(snake_t *tail, const point_t *node, map_t *map)
|
||||||
{
|
{
|
||||||
SYS_STE ret;
|
SYS_STE ret;
|
||||||
point_t *pos;
|
point_t *pos;
|
||||||
|
|
||||||
RT_ASSERT(tail != RT_NULL && node != RT_NULL && map != RT_NULL);
|
RT_ASSERT(tail != RT_NULL && node != RT_NULL && map != RT_NULL);
|
||||||
|
|
||||||
pos = map->snake_flush;
|
pos = map->snake_flush;
|
||||||
pos[0].x = pos[0].y = -1;
|
pos[0].x = pos[0].y = -1;
|
||||||
pos[1].x = pos[1].y = -1;
|
pos[1].x = pos[1].y = -1;
|
||||||
|
|
||||||
ret = (SYS_STE)map->range[node->y * map->width + node->x];
|
ret = (SYS_STE)map->range[node->y * map->width + node->x];
|
||||||
|
|
||||||
if (FOOD == 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));
|
snake_t *new = (snake_t *)rt_malloc(sizeof(snake_t));
|
||||||
if (!new)
|
if (!new)
|
||||||
return NORMAL;
|
return NORMAL;
|
||||||
|
|
||||||
pos[0] = *node;
|
pos[0] = *node;
|
||||||
new->body = *node;
|
new->body = *node;
|
||||||
rt_list_insert_after(&snake_head, &new->list);
|
rt_list_insert_after(&snake_head, &new->list);
|
||||||
}
|
}
|
||||||
else if (NORMAL == map->range[node->y * map->width + node->x])
|
else if (NORMAL == map->range[node->y * map->width + node->x])
|
||||||
{
|
{
|
||||||
// 将尾巴修改后拿到头部,其他不变
|
// 将尾巴修改后拿到头部,其他不变
|
||||||
rt_list_remove(&tail->list);
|
rt_list_remove(&tail->list);
|
||||||
map->range[tail->body.y * map->width + tail->body.x] = NORMAL;
|
map->range[tail->body.y * map->width + tail->body.x] = NORMAL;
|
||||||
|
|
||||||
pos[0] = *node;
|
pos[0] = *node;
|
||||||
pos[1] = tail->body;
|
pos[1] = tail->body;
|
||||||
|
|
||||||
tail->body = *node;
|
tail->body = *node;
|
||||||
rt_list_insert_after(&snake_head, &tail->list);
|
rt_list_insert_after(&snake_head, &tail->list);
|
||||||
}
|
}
|
||||||
|
|
||||||
map->range[node->y * map->width + node->x] = OVER;
|
map->range[node->y * map->width + node->x] = OVER;
|
||||||
|
|
||||||
if (ret != OVER)
|
if (ret != OVER)
|
||||||
prevdir = newdir;
|
prevdir = newdir;
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
map_t* map_init(rt_uint32_t width, rt_uint32_t heigth)
|
map_t *map_init(rt_uint32_t width, rt_uint32_t heigth)
|
||||||
{
|
{
|
||||||
map_t *map = rt_malloc(sizeof(map_t));
|
map_t *map = rt_malloc(sizeof(map_t));
|
||||||
|
|
||||||
if (map != RT_NULL)
|
if (map != RT_NULL)
|
||||||
{
|
{
|
||||||
map->range = rt_malloc(heigth * width);
|
map->range = rt_malloc(heigth * width);
|
||||||
|
|
||||||
if (!map->range)
|
if (!map->range)
|
||||||
{
|
{
|
||||||
rt_free(map);
|
rt_free(map);
|
||||||
map = RT_NULL;
|
map = RT_NULL;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
map->width = width;
|
map->width = width;
|
||||||
map->height = heigth;
|
map->height = heigth;
|
||||||
memset(map->range, NORMAL, heigth * width);
|
memset(map->range, NORMAL, heigth * width);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 构造一条指定长度的蛇在指定点
|
// 构造一条指定长度的蛇在指定点
|
||||||
rt_bool_t snake_init(const point_t *start, const int length, const SNAKE_DIR dir, map_t *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 i;
|
||||||
rt_int32_t inc_x, inc_y;
|
rt_int32_t inc_x, inc_y;
|
||||||
point_t old = *start;
|
point_t old = *start;
|
||||||
|
|
||||||
ASSERT_RET(!map || !start, RT_FALSE);
|
ASSERT_RET(!map || !start, RT_FALSE);
|
||||||
|
|
||||||
rt_list_init(&snake_head);
|
rt_list_init(&snake_head);
|
||||||
|
|
||||||
if (dir == SNAKE_DIR_UP || dir == SNAKE_DIR_DOWN)
|
if (dir == SNAKE_DIR_UP || dir == SNAKE_DIR_DOWN)
|
||||||
{
|
{
|
||||||
if (map->height <= length)
|
if (map->height <= length)
|
||||||
return RT_FALSE;
|
return RT_FALSE;
|
||||||
|
|
||||||
inc_x = 0;
|
inc_x = 0;
|
||||||
inc_y = dir == SNAKE_DIR_DOWN ? 1 : -1; // 反向延长身子,头部在指定位置
|
inc_y = dir == SNAKE_DIR_DOWN ? 1 : -1; // 反向延长身子,头部在指定位置
|
||||||
old.y -= inc_y;
|
old.y -= inc_y;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (map->width <= length)
|
if (map->width <= length)
|
||||||
return RT_FALSE;
|
return RT_FALSE;
|
||||||
|
|
||||||
inc_y = 0;
|
inc_y = 0;
|
||||||
inc_x = dir == SNAKE_DIR_RIGHT ? -1 : 1;
|
inc_x = dir == SNAKE_DIR_RIGHT ? -1 : 1;
|
||||||
old.x -= inc_x;
|
old.x -= inc_x;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i=0; i<length; i++)
|
for (i = 0; i < length; i++)
|
||||||
{
|
{
|
||||||
snake_t *new = (snake_t*)rt_malloc(sizeof(snake_t));
|
snake_t *new = (snake_t *)rt_malloc(sizeof(snake_t));
|
||||||
if (!new)
|
if (!new)
|
||||||
return RT_FALSE;
|
return RT_FALSE;
|
||||||
|
|
||||||
new->body.y = inc_y + old.y;
|
new->body.y = inc_y + old.y;
|
||||||
new->body.x = inc_x + old.x;
|
new->body.x = inc_x + old.x;
|
||||||
|
|
||||||
// 如果长度超出当前边框则可以穿越墙到对面
|
// 如果长度超出当前边框则可以穿越墙到对面
|
||||||
across_XY(&new->body, map);
|
across_XY(&new->body, map);
|
||||||
|
|
||||||
map->range[new->body.y * map->width + new->body.x] = OVER;
|
map->range[new->body.y * map->width + new->body.x] = OVER;
|
||||||
|
|
||||||
old = new->body;
|
old = new->body;
|
||||||
rt_list_insert_before(&snake_head, &new->list);
|
rt_list_insert_before(&snake_head, &new->list);
|
||||||
}
|
}
|
||||||
|
|
||||||
prevdir = dir;
|
prevdir = dir;
|
||||||
|
|
||||||
return RT_TRUE;
|
return RT_TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 构造出食物
|
// 构造出食物
|
||||||
rt_bool_t food_init(map_t *map, rt_uint32_t max_num)
|
rt_bool_t food_init(map_t *map, rt_uint32_t max_num)
|
||||||
{
|
{
|
||||||
point_t food;
|
point_t food;
|
||||||
|
|
||||||
#ifndef FOOD_TIMEOUT
|
#ifndef FOOD_TIMEOUT
|
||||||
#define FOOD_TIMEOUT 10
|
#define FOOD_TIMEOUT 10
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
rt_uint32_t timeout, num;
|
rt_uint32_t timeout, num;
|
||||||
|
|
||||||
ASSERT_RET(!map, RT_FALSE);
|
ASSERT_RET(!map, RT_FALSE);
|
||||||
|
|
||||||
num = 0;
|
num = 0;
|
||||||
timeout = rt_tick_get();
|
timeout = rt_tick_get();
|
||||||
srand(rand());
|
srand(rand());
|
||||||
|
|
||||||
map->food_flush[0].x = map->food_flush[0].y = -1;
|
map->food_flush[0].x = map->food_flush[0].y = -1;
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
food.x = rand() % map->width;
|
food.x = rand() % map->width;
|
||||||
food.y = rand() % map->height;
|
food.y = rand() % map->height;
|
||||||
|
|
||||||
if (map->range[food.y * map->width + food.x] == NORMAL)
|
if (map->range[food.y * map->width + food.x] == NORMAL)
|
||||||
{
|
{
|
||||||
map->food_flush[0] = food;
|
map->food_flush[0] = food;
|
||||||
map->range[food.y * map->width + food.x] = FOOD;
|
map->range[food.y * map->width + food.x] = FOOD;
|
||||||
num++;
|
num++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
while (num < max_num && rt_tick_get() - timeout < FOOD_TIMEOUT);
|
while (num < max_num && rt_tick_get() - timeout < FOOD_TIMEOUT);
|
||||||
|
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
|
||||||
void map_deinit(map_t *map)
|
void map_deinit(map_t *map)
|
||||||
{
|
{
|
||||||
if (map)
|
if (map)
|
||||||
{
|
{
|
||||||
if (map->range)
|
if (map->range)
|
||||||
{
|
{
|
||||||
rt_free(map->range);
|
rt_free(map->range);
|
||||||
map->range = RT_NULL;
|
map->range = RT_NULL;
|
||||||
}
|
}
|
||||||
rt_free(map);
|
rt_free(map);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void snake_deinit(void)
|
void snake_deinit(void)
|
||||||
{
|
{
|
||||||
snake_t *node;
|
snake_t *node;
|
||||||
|
|
||||||
while (!rt_list_isempty(&snake_head))
|
while (!rt_list_isempty(&snake_head))
|
||||||
{
|
{
|
||||||
node = rt_list_entry(snake_head.prev, snake_t, list);
|
node = rt_list_entry(snake_head.prev, snake_t, list);
|
||||||
rt_list_remove(&node->list);
|
rt_list_remove(&node->list);
|
||||||
rt_free(node);
|
rt_free(node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void food_deinit(void)
|
void food_deinit(void)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SYS_STE snake_step(SNAKE_DIR dir, map_t *map)
|
SYS_STE snake_step(SNAKE_DIR dir, map_t *map)
|
||||||
{
|
{
|
||||||
snake_t *tail, *head;
|
snake_t *tail, *head;
|
||||||
point_t node;
|
point_t node;
|
||||||
|
|
||||||
ASSERT_RET(!map, RT_FALSE);
|
ASSERT_RET(!map, RT_FALSE);
|
||||||
|
|
||||||
dir = dir_adjust(dir);
|
dir = dir_adjust(dir);
|
||||||
|
|
||||||
// 取出头尾两个节点,其他节点不需要改变
|
// 取出头尾两个节点,其他节点不需要改变
|
||||||
tail = rt_list_entry(snake_head.prev, snake_t, list);
|
tail = rt_list_entry(snake_head.prev, snake_t, list);
|
||||||
head = rt_list_entry(snake_head.next, snake_t, list);
|
head = rt_list_entry(snake_head.next, snake_t, list);
|
||||||
|
|
||||||
node = head->body;
|
node = head->body;
|
||||||
|
|
||||||
// 构造一个新的蛇头坐标
|
// 构造一个新的蛇头坐标
|
||||||
switch (dir)
|
switch (dir)
|
||||||
{
|
{
|
||||||
case SNAKE_DIR_UP:
|
case SNAKE_DIR_UP:
|
||||||
case SNAKE_DIR_DOWN:
|
case SNAKE_DIR_DOWN:
|
||||||
node.y = head->body.y + (dir == SNAKE_DIR_DOWN ? -1 : 1);
|
node.y = head->body.y + (dir == SNAKE_DIR_DOWN ? -1 : 1);
|
||||||
break;
|
break;
|
||||||
case SNAKE_DIR_LEFT:
|
case SNAKE_DIR_LEFT:
|
||||||
case SNAKE_DIR_RIGHT:
|
case SNAKE_DIR_RIGHT:
|
||||||
node.x = head->body.x + (dir == SNAKE_DIR_RIGHT ? 1 : -1);
|
node.x = head->body.x + (dir == SNAKE_DIR_RIGHT ? 1 : -1);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
across_XY(&node, map);
|
across_XY(&node, map);
|
||||||
|
|
||||||
return node_update(tail, &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)
|
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);
|
ASSERT_RET(!map || !start, RT_FALSE);
|
||||||
|
|
||||||
snake_deinit();
|
snake_deinit();
|
||||||
memset(map->range, NORMAL, map->width * map->height);
|
memset(map->range, NORMAL, map->width * map->height);
|
||||||
|
|
||||||
return snake_init(start, length, dir, map);
|
return snake_init(start, length, dir, map);
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
rt_int32_t x, y;
|
rt_int32_t x, y;
|
||||||
}point_t;
|
} point_t;
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
|
@ -23,7 +23,7 @@ typedef struct
|
||||||
rt_uint8_t *range; // map, map->range[y * map->width + x]
|
rt_uint8_t *range; // map, map->range[y * map->width + x]
|
||||||
point_t snake_flush[2];
|
point_t snake_flush[2];
|
||||||
point_t food_flush[1];
|
point_t food_flush[1];
|
||||||
}map_t;
|
} map_t;
|
||||||
|
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
|
@ -31,23 +31,23 @@ typedef enum
|
||||||
SNAKE_DIR_DOWN,
|
SNAKE_DIR_DOWN,
|
||||||
SNAKE_DIR_LEFT,
|
SNAKE_DIR_LEFT,
|
||||||
SNAKE_DIR_RIGHT
|
SNAKE_DIR_RIGHT
|
||||||
}SNAKE_DIR;
|
} SNAKE_DIR;
|
||||||
|
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
FOOD, // 吃到水果
|
FOOD, // 吃到水果
|
||||||
OVER, // 咬到自身
|
OVER, // 咬到自身
|
||||||
NORMAL // 正常行动
|
NORMAL // 正常行动
|
||||||
}SYS_STE;
|
} SYS_STE;
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
point_t body;
|
point_t body;
|
||||||
rt_list_t list;
|
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);
|
rt_bool_t snake_init(const point_t *start, const int length, const SNAKE_DIR dir, map_t *map);
|
||||||
|
|
|
@ -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 rt_size_t lattice_size_x, lattice_size_y;
|
||||||
static struct rtgui_rect room_rect, lattice_rect;
|
static struct rtgui_rect room_rect, lattice_rect;
|
||||||
|
|
||||||
map_t* map;
|
map_t *map;
|
||||||
SNAKE_DIR run_state;
|
SNAKE_DIR run_state;
|
||||||
rt_int32_t snake_len;
|
rt_int32_t snake_len;
|
||||||
rt_int32_t food_num;
|
rt_int32_t food_num;
|
||||||
|
@ -67,7 +67,7 @@ static void snake_draw(struct rtgui_widget *widget)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* get room size, run once frist. */
|
/* 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;
|
rt_size_t tmp;
|
||||||
|
|
||||||
|
@ -133,7 +133,7 @@ static void snake_draw(struct rtgui_widget *widget)
|
||||||
RTGUI_DC_FC(dc) = WALL_COLOR;
|
RTGUI_DC_FC(dc) = WALL_COLOR;
|
||||||
rtgui_dc_draw_rect(dc, &rect);
|
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));
|
memcpy(&rect, &lattice_rect, sizeof(struct rtgui_rect));
|
||||||
rect.x1 += 1;
|
rect.x1 += 1;
|
||||||
|
@ -142,7 +142,7 @@ static void snake_draw(struct rtgui_widget *widget)
|
||||||
rect.y1 + (LATTICE_SIZE * i));
|
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));
|
memcpy(&rect, &lattice_rect, sizeof(struct rtgui_rect));
|
||||||
rect.y1 += 1;
|
rect.y1 += 1;
|
||||||
|
@ -156,9 +156,9 @@ static void snake_draw(struct rtgui_widget *widget)
|
||||||
rt_uint32_t x, y;
|
rt_uint32_t x, y;
|
||||||
rt_bool_t first_node = RT_TRUE;
|
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])
|
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);
|
snake_fill_lattice(dc, second_node.x, second_node.y, SNAKE_COLOR);
|
||||||
second_node = map->snake_flush[0];
|
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;
|
x = map->snake_flush[i].x;
|
||||||
y = map->snake_flush[i].y;
|
y = map->snake_flush[i].y;
|
||||||
|
@ -220,7 +220,7 @@ static void snake_update(struct rtgui_widget *widget)
|
||||||
y = map->food_flush[0].y;
|
y = map->food_flush[0].y;
|
||||||
}
|
}
|
||||||
|
|
||||||
if((x >= 0) && (y >= 0))
|
if ((x >= 0) && (y >= 0))
|
||||||
{
|
{
|
||||||
switch (map->range[(map->width * y) + x])
|
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)
|
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)
|
if (ekbd->type == RTGUI_KEYDOWN)
|
||||||
{
|
{
|
||||||
switch (ekbd->key)
|
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)
|
if (event->type == RTGUI_EVENT_PAINT)
|
||||||
{
|
{
|
||||||
rt_kprintf("RTGUI_EVENT_PAINT\r\n");
|
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);
|
snake_draw(widget);
|
||||||
rtgui_timer_start(timer);
|
rtgui_timer_start(timer);
|
||||||
}
|
}
|
||||||
else if (event->type == RTGUI_EVENT_SHOW)
|
else if (event->type == RTGUI_EVENT_SHOW)
|
||||||
{
|
{
|
||||||
rt_kprintf("RTGUI_EVENT_SHOW\r\n");
|
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);
|
snake_draw(widget);
|
||||||
rtgui_timer_start(timer);
|
rtgui_timer_start(timer);
|
||||||
}
|
}
|
||||||
else if (event->type == RTGUI_EVENT_HIDE)
|
else if (event->type == RTGUI_EVENT_HIDE)
|
||||||
{
|
{
|
||||||
rt_kprintf("RTGUI_EVENT_HIDE\r\n");
|
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);
|
rtgui_timer_stop(timer);
|
||||||
}
|
}
|
||||||
else if (event->type == RTGUI_EVENT_WIN_DEACTIVATE)
|
else if (event->type == RTGUI_EVENT_WIN_DEACTIVATE)
|
||||||
{
|
{
|
||||||
rt_kprintf("RTGUI_EVENT_WIN_DEACTIVATE\r\n");
|
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);
|
rtgui_timer_stop(timer);
|
||||||
}
|
}
|
||||||
else if (event->type == RTGUI_EVENT_KBD)
|
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);
|
snake_handler(widget, event);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
rt_kprintf("event->type:%d\r\n", event->type);
|
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;
|
return RT_FALSE;
|
||||||
|
@ -325,7 +325,7 @@ static void timeout(struct rtgui_timer *timer, void *parameter)
|
||||||
{
|
{
|
||||||
struct rtgui_widget *widget;
|
struct rtgui_widget *widget;
|
||||||
SYS_STE ret;
|
SYS_STE ret;
|
||||||
|
|
||||||
if (!map)
|
if (!map)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -355,26 +355,26 @@ static void timeout(struct rtgui_timer *timer, void *parameter)
|
||||||
|
|
||||||
food_init(map, 1);
|
food_init(map, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
widget = RTGUI_WIDGET(parameter);
|
widget = RTGUI_WIDGET(parameter);
|
||||||
snake_update(widget);
|
snake_update(widget);
|
||||||
}
|
}
|
||||||
|
|
||||||
void snake_main(void)
|
void snake_main(void)
|
||||||
{
|
{
|
||||||
struct rtgui_app* application;
|
struct rtgui_app *application;
|
||||||
struct rtgui_win* win;
|
struct rtgui_win *win;
|
||||||
rtgui_rect_t rect;
|
rtgui_rect_t rect;
|
||||||
|
|
||||||
application = rtgui_app_create(rt_thread_self(), "sanke_app");
|
application = rtgui_app_create(rt_thread_self(), "sanke_app");
|
||||||
if (application != RT_NULL)
|
if (application != RT_NULL)
|
||||||
{
|
{
|
||||||
rtgui_get_screen_rect(&rect);
|
rtgui_get_screen_rect(&rect);
|
||||||
rtgui_set_mainwin_rect(&rect);
|
rtgui_set_mainwin_rect(&rect);
|
||||||
win = rtgui_mainwin_create(RT_NULL,
|
win = rtgui_mainwin_create(RT_NULL,
|
||||||
"sanke_win",
|
"sanke_win",
|
||||||
RTGUI_WIN_STYLE_MAINWIN | RTGUI_WIN_STYLE_DESTROY_ON_CLOSE);
|
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");
|
rt_kprintf("sanke_win create fail!\r\n");
|
||||||
return;
|
return;
|
||||||
|
|
Loading…
Reference in New Issue