-
Notifications
You must be signed in to change notification settings - Fork 1
/
HuUart.c
259 lines (217 loc) · 5.5 KB
/
HuUart.c
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* HuUart.c
*
* Copyright (c) 2018 Yulay Rakhmangulov.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <avr/io.h>
#include <avr/iotn85.h>
#include <avr/interrupt.h>
#include "PortConfig.h"
#include "HuUart.h"
#include "Timeout.h"
volatile struct Uart hu;
void Hu_RxReset();
//void Hu_RxStart();
void Hu_TxStart();
void Hu_TxStop();
void Hu_SetTxPort(uint8_t b);
void Hu_SetRTS() //request to send
{
PORTB |= _BV(HuReqPORT);
}
void Hu_ClrRTS()
{
PORTB &= ~_BV(HuReqPORT);
}
void Hu_Init()
{
hu.rxState = Idle;
hu.rxFifo.head = 0;
hu.rxFifo.tail = 0;
hu.txState = Idle;
hu.txFifo.head = 0;
hu.txFifo.tail = 0;
Hu_RxReset();
Hu_SetTxPort(1); //SPACE
Hu_ClrRTS(); //REQ
}
void Hu_RxStart()
{
//TCNT0 = 0; //clear timer0
//GTCCR &= ~_BV(TSM); //start timer0 and clear prescaler
#if TIMER0_RELOAD > 168
#error This math does not work for timer reload value > 168
#endif
OCR0B = (TCNT0 + TIMER0_RELOAD/2) % TIMER0_RELOAD; //capture exact middle of the bit
TIMSK |= _BV(OCIE0B); //enable CompareMatch0B interrupt
hu.rxState = StartFrame;
PCMSK &= ~_BV(HuRxPORT); //disable pc interrupt for HU
}
void Hu_RxReset()
{
//GTCCR |= _BV(TSM) | _BV(PSR0); //stop timer0
TIMSK &= ~_BV(OCIE0B); //disable CompareMatch0B interrupt
PCMSK |= _BV(HuRxPORT); //enable pc interrupt for HU
hu.rxState = Idle;
}
bool Hu_RxReady()
{
return hu.rxFifo.tail != hu.rxFifo.head;
}
uint8_t Hu_GetByte()
{
uint8_t b = hu.rxFifo.buf[hu.rxFifo.tail];
hu.rxFifo.tail = (hu.rxFifo.tail + 1) % MAX_FIFO_BUF_SIZE;
return b;
}
uint8_t calcParityBit(uint8_t b)
{
b ^= (b >> 4);
b ^= (b >> 2);
b ^= (b >> 1);
return b & 1;
}
void Hu_PutByte(uint8_t b)
{
uint8_t head = (hu.txFifo.head + 1) % MAX_FIFO_BUF_SIZE;
while(head == hu.txFifo.tail) ; //busy wait for free space
hu.txFifo.buf[hu.txFifo.head] = b;
hu.txFifo.head = head;
Hu_TxStart();
}
inline void Hu_TxStart()
{
TIMSK |= _BV(OCIE0A); //enable interrupt for CompareMatch0A
}
inline void Hu_TxStop()
{
TIMSK &= ~_BV(OCIE0A); //disable interrupt for CompareMatch0A
}
void Hu_SetTxPort(uint8_t b)
{
if(b) //logic polarity control
{
PORTB &= ~_BV(HuTxPORT);
}
else
{
PORTB |= _BV(HuTxPORT);
}
}
/* Timer0 compare match A interrupt handler */
//HU Serial Transmit driver
ISR( TIMER0_COMPA_vect )
{
if(hu.txState == Idle)
{
if(hu.txFifo.tail != hu.txFifo.head)
{
hu.txBuf = hu.txFifo.buf[hu.txFifo.tail];
hu.txState = StartFrame;
hu.txFifo.tail = (hu.txFifo.tail + 1) % MAX_FIFO_BUF_SIZE;
hu.txParity = calcParityBit(hu.txBuf);
resetTimeout(); //reset timeout before each Tx
}
}
else
{
++(hu.txState); //next state
switch(hu.txState)
{
case StartBit:
Hu_SetTxPort(0);
break;
case Bit0:
case Bit1:
case Bit2:
case Bit3:
case Bit4:
case Bit5:
case Bit6:
case Bit7:
Hu_SetTxPort(hu.txBuf & 1);
hu.txBuf >>= 1;
break;
case ParityBit:
Hu_SetTxPort(hu.txParity);
break;
case StopBit2:
{
Hu_SetTxPort(1);
volatile struct Fifo* fifo = &hu.txFifo;
if(fifo->tail == fifo->head) //no more data to send
{
Hu_TxStop();
}
hu.txState = Idle;
break;
}
default:
break;
}
}
}
/* Timer0 compare match B interrupt handler */
//HU Serial Receive driver
ISR( TIMER0_COMPB_vect )
{
if(hu.rxState != Idle)
{
++(hu.rxState); //next state
switch(hu.rxState)
{
case StartBit:
if(PINB & _BV(HuRxPORT)) //false start?
{
Hu_RxReset();
}
break;
case Bit0:
case Bit1:
case Bit2:
case Bit3:
case Bit4:
case Bit5:
case Bit6:
case Bit7:
hu.rxBuf >>= 1;
if(PINB & _BV(HuRxPORT))
{
hu.rxBuf |= 0x80;
}
break;
case ParityBit:
if(calcParityBit(hu.rxBuf) != ((PINB & _BV(HuRxPORT)) >> HuRxPORT)) //parity error
{
Hu_RxReset();
}
break;
case StopBit2:
if(PINB & _BV(HuRxPORT))
{
volatile struct Fifo* fifo = &hu.rxFifo;
//NOTE:
// No check for available space!
// Main loop must read out faster then new data come
fifo->buf[fifo->head] = hu.rxBuf;
fifo->head = (fifo->head + 1) % MAX_FIFO_BUF_SIZE;
}
Hu_RxReset();
break;
default:
break;
}
}
}