usart_sim.c 3.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * File      : serial.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2013 RT-Thread Develop Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
 * Date           Author       Notes
 * 2012-09-25     prife        first implementation
 * 2013-01-15     prife        support linux
 * 2013-02-6      prife        rewrite to fit the new serial.c
 */
M
Ming, Bai 已提交
16 17
#include  <rthw.h>
#include  <rtthread.h>
P
prife 已提交
18 19

#ifdef _WIN32
M
Ming, Bai 已提交
20 21 22
#include  <windows.h>
#include  <mmsystem.h>
#include  <conio.h>
P
prife 已提交
23
#endif
M
Ming, Bai 已提交
24

P
prife 已提交
25
#include  <stdio.h>
M
Ming, Bai 已提交
26 27
#include "serial.h"

28
struct serial_device serial1;
M
Ming, Bai 已提交
29

30
#define SAVEKEY(key)  seial_save_byte(key, &serial1)
P
prife 已提交
31
#ifdef _WIN32
M
Ming, Bai 已提交
32 33 34 35 36 37 38 39 40
/*
 * Handler for OSKey Thread
 */
static HANDLE       OSKey_Thread;
static DWORD        OSKey_ThreadID;

static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam);
void rt_hw_usart_init(void)
{
41
    rt_hw_serial_init(&serial1, RT_CONSOLE_DEVICE_NAME);
M
Ming, Bai 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    /*
     * create serial thread that receive 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);
}

P
prife 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83
#else /* POSIX version */

#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
#include <signal.h>
#include <termios.h> /* for tcxxxattr, ECHO, etc */
#include <unistd.h> /* for STDIN_FILENO */

static void * ThreadforKeyGet(void * lpParam);
static pthread_t OSKey_Thread;
void rt_hw_usart_init(void)
{
    int res;
84 85

    rt_hw_serial_init(&serial1, RT_CONSOLE_DEVICE_NAME);
P
prife 已提交
86 87 88 89 90 91 92 93
    res = pthread_create(&OSKey_Thread, NULL, &ThreadforKeyGet, NULL);
    if (res)
    {
        printf("pthread create faild, <%d>\n", res);
        exit(EXIT_FAILURE);
    }
}
#endif
M
Ming, Bai 已提交
94

P
prife 已提交
95
#ifdef _WIN32
M
Ming, Bai 已提交
96
static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam)
P
prife 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
#else

static struct termios oldt, newt;
/*simulate windows' getch(), it works!!*/
void set_stty(void)
{
	/* get terminal input's attribute */
    tcgetattr(STDIN_FILENO, &oldt);
    newt = oldt;

	/* set termios' local mode */
    newt.c_lflag &= ~(ECHO|ICANON);
    tcsetattr(STDIN_FILENO, TCSANOW, &newt);
}

void restore_stty(void)
{
   /* recover terminal's attribute */
   tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
}

#define getch  getchar

static void * ThreadforKeyGet(void * lpParam)
#endif /* not _WIN32*/
M
Ming, Bai 已提交
122
{
123 124 125 126 127 128
 /*
 * 方向键(←): 0xe04b
 * 方向键(↑): 0xe048
 * 方向键(→): 0xe04d
 * 方向键(↓): 0xe050
 */
M
Ming, Bai 已提交
129 130
    unsigned char key;

P
prife 已提交
131 132 133 134 135 136 137
#ifndef _WIN32
    sigset_t  sigmask, oldmask;
	/* set the getchar without buffer */
	sigfillset(&sigmask);
	pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
	set_stty();
#endif
M
Ming, Bai 已提交
138 139 140 141
    (void)lpParam;              //prevent compiler warnings
    for (;;)
    {
        key = getch();
P
prife 已提交
142
#ifdef _WIN32
M
Ming, Bai 已提交
143 144 145 146 147 148
        if (key == 0xE0)
        {
            key = getch();

            if (key == 0x48) //up key , 0x1b 0x5b 0x41
            {
149 150 151
                SAVEKEY(0x1b);
                SAVEKEY(0x5b);
                SAVEKEY(0x41);
M
Ming, Bai 已提交
152 153 154
            }
            else if (key == 0x50)//0x1b 0x5b 0x42
            {
155 156 157
                SAVEKEY(0x1b);
                SAVEKEY(0x5b);
                SAVEKEY(0x42);
M
Ming, Bai 已提交
158 159 160 161
            }

            continue;
        }
P
prife 已提交
162
#endif
163
        SAVEKEY(key);
M
Ming, Bai 已提交
164
    }
P
prife 已提交
165
} /*** ThreadforKeyGet ***/