rt-thread-official/bsp/simulator/drivers/usart_sim.c

131 lines
3.1 KiB
C
Raw Normal View History

#include <rtthread.h>
#include <windows.h>
#include <mmsystem.h>
#include <stdio.h>
#include "serial.h"
struct serial_int_rx serial_rx;
extern struct rt_device serial_device;
/*
* Handler for OSKey Thread
*/
static HANDLE OSKey_Thread;
static DWORD OSKey_ThreadID;
static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam);
void rt_hw_usart_init(void)
{
/*
* create serial thread that revice key input from keyboard
*/
OSKey_Thread = CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE)ThreadforKeyGet,
0,
CREATE_SUSPENDED,
&OSKey_ThreadID);
if(OSKey_Thread == NULL)
{
//Display Error Message
return;
}
SetThreadPriority(OSKey_Thread,
THREAD_PRIORITY_NORMAL);
SetThreadPriorityBoost(OSKey_Thread,
TRUE);
SetThreadAffinityMask(OSKey_Thread,
0x01);
/*
* Start OS get key Thread
*/
ResumeThread(OSKey_Thread);
}
/*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<EFBFBD><EFBFBD>)<EFBFBD><EFBFBD> 0xe04b
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<EFBFBD><EFBFBD>)<EFBFBD><EFBFBD> 0xe048
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<EFBFBD><EFBFBD>)<EFBFBD><EFBFBD> 0xe04d
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<EFBFBD><EFBFBD>)<EFBFBD><EFBFBD> 0xe050
*/
static int savekey(unsigned char key)
{
/* save on rx buffer */
{
rt_base_t level;
/* disable interrupt */
//<2F><>ʱ<EFBFBD>ر<EFBFBD><D8B1>жϣ<D0B6><CFA3><EFBFBD>ΪҪ<CEAA><D2AA><EFBFBD><EFBFBD>uart<72><74><EFBFBD>ݽṹ
level = rt_hw_interrupt_disable();
/* save character */
serial_rx.rx_buffer[serial_rx.save_index] = key;
serial_rx.save_index ++;
//<2F><><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>save_index<65>Ƿ<EFBFBD><C7B7>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊһ<CEAA><D2BB><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD>
if (serial_rx.save_index >= SERIAL_RX_BUFFER_SIZE)
serial_rx.save_index = 0;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>save_index׷<78><D7B7><EFBFBD><EFBFBD>read_index<65><78><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>read_index<65><78><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ɵ<EFBFBD><C9B5><EFBFBD><EFBFBD><EFBFBD>
/* if the next position is read index, discard this 'read char' */
if (serial_rx.save_index == serial_rx.read_index)
{
serial_rx.read_index ++;
if (serial_rx.read_index >= SERIAL_RX_BUFFER_SIZE)
serial_rx.read_index = 0;
}
/* enable interrupt */
//uart<72><74><EFBFBD>ݽṹ<DDBD>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɣ<EFBFBD><C9A3><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD>ж<EFBFBD>
rt_hw_interrupt_enable(level);
}
/* invoke callback */
if (serial_device.rx_indicate != RT_NULL)
{
rt_size_t rx_length;
/* get rx length */
rx_length = serial_rx.read_index > serial_rx.save_index ?
SERIAL_RX_BUFFER_SIZE - serial_rx.read_index + serial_rx.save_index :
serial_rx.save_index - serial_rx.read_index;
serial_device.rx_indicate(&serial_device, rx_length);
}
return 0;
}
static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam)
{
unsigned char key;
(void)lpParam; //prevent compiler warnings
for(;;)
{
key = _getch();//getchar();
if (key == 0xE0)
{
key = _getch();
if (key == 0x48) //up key , 0x1b 0x5b 0x41
{
savekey(0x1b);
savekey(0x5b);
savekey(0x41);
}
else if (key == 0x50)//0x1b 0x5b 0x42
{
savekey(0x1b);
savekey(0x5b);
savekey(0x42);
}
continue;
}
savekey(key);
}
} /*** ThreadforKeyGet ***/