Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Sauermann J.Realtime operating systems.Concepts and implementation of microkernels for embedded systems.1997.pdf
Скачиваний:
29
Добавлен:
23.08.2013
Размер:
1.32 Mб
Скачать

166

A.13 SerialIn.hh

 

 

A.13 SerialIn.hh

1 /* SerialIn.hh */

2

3#ifndef __SERIALIN_HH_DEFINED__

4#define __SERIALIN_HH_DEFINED__

6#include "Channels.hh"

8// forward declarations...

9class Semaphore;

10class SerialOut;

11template <class Type> class Queue_Gsem;

13class SerialIn

14{

15public:

16SerialIn(Channel);

17~SerialIn();

18

19 static unsigned int getOverflowCounter(Channel);

20

21int Getc();

22int Pollc();

23int Peekc();

24int Gethex(SerialOut &);

25int Getdec(SerialOut &);

27enum SerialError

28{

29OVERRUN_ERROR = 1,

30PARITY_ERROR = 2,

31

FRAME_ERROR = 3,

32BREAK_DETECT = 4

33};

34private:

35Channel channel;

36

37static Semaphore Channel_0;

38static Semaphore Channel_1;

40static Queue_Gsem<unsigned char> inbuf_0;

41static Queue_Gsem<unsigned char> inbuf_1;

42};

44#endif __SERIALIN_HH_DEFINED__

A. Appendices

167

 

 

A.14 SerialIn.cc

1 /* SerialIn.cc */

2

3#include "System.config"

4#include "SerialIn.hh"

5#include "SerialOut.hh"

6#include "Task.hh"

7#include "Queue.hh"

9Queue_Gsem<unsigned char> SerialIn::inbuf_0 (INBUF_0_SIZE);

10 Queue_Gsem<unsigned char> SerialIn::inbuf_1 (INBUF_1_SIZE); 11

12Semaphore SerialIn::Channel_0;

13Semaphore SerialIn::Channel_1;

15//=================================================================

16SerialIn::SerialIn(Channel ch) : channel(ch)

17{

18switch(channel)

19{

20

case

SERIAL_0:

Channel_0.P();

break;

21

case

SERIAL_1:

Channel_1.P();

break;

22}

23}

24//=================================================================

25SerialIn::~SerialIn()

26{

27switch(channel)

28{

29

case

SERIAL_0:

Channel_0.V();

break;

30

case

SERIAL_1:

Channel_1.V();

break;

31}

32}

33//=================================================================

34int SerialIn::Getc()

35{

36unsigned char cc;

37

38switch(channel)

39{

40

case SERIAL_0:

inbuf_0.Get(cc);

return

cc;

41

case SERIAL_1:

inbuf_1.Get(cc);

return

cc;

42

default:

return -1;

 

 

43}

44}

45//=================================================================

46int SerialIn::Pollc()

47{

48unsigned char cc;

49

50switch(channel)

51{

52case SERIAL_0: return inbuf_0.PolledGet(cc) ? -1 : cc;

53case SERIAL_1: return inbuf_1.PolledGet(cc) ? -1 : cc;

54default: return -1;

168

A.14 SerialIn.cc

 

 

55}

56}

57//=================================================================

58int SerialIn::Peekc()

59{

60unsigned char cc;

61

62switch(channel)

63{

64case SERIAL_0: return inbuf_0.Peek(cc) ? -1 : cc;

65case SERIAL_1: return inbuf_1.Peek(cc) ? -1 : cc;

66

default:

return -1;

67}

68}

69//=================================================================

70int SerialIn::Gethex(SerialOut &so)

71{

72int ret = 0;

73int cc;

74

75for (;;) switch(cc = Peekc())

76{

77case -1: // no char arrived yet

78

Task::Sleep(1);

79

continue;

80

 

81case '0': case '1': case '2': case '3': case '4':

82case '5': case '6': case '7': case '8': case '9':

83

ret <<= 4;

 

84

ret += cc-'0';

 

85

so.Print("%c", Pollc());

// echo char

86

continue;

 

87

 

 

88case 'A': case 'B': case 'C':

89case 'D': case 'E': case 'F':

90

ret <<= 4;

 

91

ret += cc+10-'A';

 

92

so.Print("%c", Pollc());

// echo char

93

continue;

 

94

 

 

95case 'a': case 'b': case 'c':

96case 'd': case 'e': case 'f':

97

ret <<= 4;

 

98

ret += cc+10-'a';

 

99

so.Print("%c", Pollc());

// echo char

100

continue;

 

101

 

 

102

default:

 

103

return ret;

 

104}

105}

106//=================================================================

107int SerialIn::Getdec(SerialOut &so)

108{

109int ret = 0;

110int cc;

A. Appendices

169

 

 

111

112for (;;) switch(cc = Peekc())

113{

114case -1: // no char arrived yet

115

Task::Sleep(1);

116

continue;

117

 

118case '0': case '1': case '2': case '3': case '4':

119case '5': case '6': case '7': case '8': case '9':

120

ret *= 10;

 

121

ret += cc-'0';

 

122

so.Print("%c", Pollc());

// echo char

123

continue;

 

124

 

 

125

default:

 

126

return ret;

 

127}

128}

129//=================================================================

130unsigned int SerialIn::getOverflowCounter(Channel channel)

131{

132switch(channel)

133{

134case SERIAL_0: return inbuf_0.getOverflowCount();

135case SERIAL_1: return inbuf_1.getOverflowCount();

136

default:

return 0;

137}

138}

139//=================================================================