forked from xcore/tool_axe
-
Notifications
You must be signed in to change notification settings - Fork 1
/
UartRx.cpp
185 lines (173 loc) · 4.14 KB
/
UartRx.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright (c) 2011-2012, Richard Osborne, All rights reserved
// This software is freely distributable under a derivative of the
// University of Illinois/NCSA Open Source License posted in
// LICENSE.txt and at <http://github.xcore.com/>
#include "UartRx.h"
#include "Signal.h"
#include "RunnableQueue.h"
#include "PortInterface.h"
#include "Runnable.h"
#include "PeripheralDescriptor.h"
#include "Peripheral.h"
#include "Property.h"
#include "SystemState.h"
#include "Node.h"
#include "Core.h"
#include <iostream>
#include <cstdlib>
const unsigned DEFAULT_BIT_RATE = 115200;
class UartRx : public PortInterface, public Runnable, public Peripheral {
private:
RunnableQueue &scheduler;
unsigned currentPinValue;
enum State {
WAIT_FOR_HIGH,
WAIT_FOR_START,
CHECK_START,
RECEIVE,
CHECK_PARITY,
CHECK_STOP
};
ticks_t bitTime;
unsigned numDataBits;
unsigned numStopBits;
enum Parity {
ODD,
EVEN,
NONE
};
Parity parity;
State state;
bool byteValid;
unsigned bitNumber;
unsigned byte;
bool computeParity();
void receiveByte(unsigned byte);
void scheduleUpdate(ticks_t time);
public:
UartRx(RunnableQueue &s, ticks_t bitTime);
virtual void seePinsChange(const Signal &value, ticks_t time);
virtual void run(ticks_t time);
};
UartRx::UartRx(RunnableQueue &s, ticks_t bt) :
scheduler(s),
currentPinValue(0),
bitTime(bt),
numDataBits(8),
numStopBits(1),
parity(NONE),
state(WAIT_FOR_HIGH)
{
}
void UartRx::scheduleUpdate(ticks_t time)
{
scheduler.push(*this, time);
}
void UartRx::seePinsChange(const Signal &value, ticks_t time)
{
currentPinValue = value.getValue(time);
switch (state) {
default:
break;
case WAIT_FOR_HIGH:
if (currentPinValue == 1)
state = WAIT_FOR_START;
break;
case WAIT_FOR_START:
if (currentPinValue == 0) {
state = CHECK_START;
scheduleUpdate(time + bitTime / 2);
}
break;
}
}
bool UartRx::computeParity()
{
bool value = 0;
unsigned tmp = byte;
if (parity == ODD)
value = !value;
while (tmp) {
value = !value;
tmp = tmp & (tmp - 1);
}
return value;
}
void UartRx::receiveByte(unsigned byte)
{
std::cout << (char)byte;
std::cout.flush();
}
void UartRx::run(ticks_t time)
{
switch (state) {
default: assert(0 && "Unexpected state");
case CHECK_START:
if (currentPinValue == 0) {
state = RECEIVE;
bitNumber = 0;
byte = 0;
byteValid = true;
scheduleUpdate(time + bitTime);
} else {
state = WAIT_FOR_START;
}
break;
case RECEIVE:
byte |= currentPinValue << bitNumber;
bitNumber++;
if (bitNumber == numDataBits) {
state = (parity == NONE) ? CHECK_STOP : CHECK_PARITY;
bitNumber = 0;
}
scheduleUpdate(time + bitTime);
break;
case CHECK_PARITY:
byteValid = currentPinValue == computeParity();
state = CHECK_STOP;
scheduleUpdate(time + bitTime);
break;
case CHECK_STOP:
if (currentPinValue == 1) {
bitNumber++;
if (bitNumber == numStopBits) {
if (byteValid)
state = WAIT_FOR_START;
receiveByte(byte);
} else {
scheduleUpdate(time + bitTime);
}
} else {
state = WAIT_FOR_HIGH;
}
break;
}
}
static Peripheral *
createUartRx(SystemState &system, const Properties &properties)
{
int32_t bitrate = DEFAULT_BIT_RATE;
if (const Property *p = properties.get("bitrate"))
bitrate = p->getAsInteger();
ticks_t bitTime = (CYCLES_PER_TICK*100000000)/bitrate;
UartRx *p = new UartRx(system.getScheduler(), bitTime) ;
PortArg portArg = properties.get("port")->getAsPort();
Port *port = portArg.lookup(system);
if (!port) {
// TODO check this in generic code.
std::cerr << "Error: Invalid port ";
portArg.dump(std::cerr);
std::cerr << '\n';
std::exit(1);
}
port->setLoopback(p);
return p;
}
std::auto_ptr<PeripheralDescriptor> getPeripheralDescriptorUartRx()
{
std::auto_ptr<PeripheralDescriptor> p(
new PeripheralDescriptor("uart-rx", &createUartRx));
p->addProperty(PropertyDescriptor::portProperty("port")).setRequired(true);
p->addProperty(PropertyDescriptor::integerProperty("bitrate"));
return p;
}