-
Notifications
You must be signed in to change notification settings - Fork 5
/
mcu.v
320 lines (277 loc) · 7.98 KB
/
mcu.v
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//
// HPSDR - High Performance Software Defined Radio
//
// Hermes code.
//
// 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; either version 2 of the License, or
// (at your option) any later version.
//
// 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, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Copyright 2020 Davide Gerhard IV3CVE
module mcu
#(parameter [63:0] fw_version = 64'b0)
(
input clk,
// UART interface to MCU
input mcu_uart_rx,
output mcu_uart_tx,
// high when ptt is enabled
input ptt
);
//**** UART declaration ****//
// the clock used divided by the UART speed
// calc: main clock (clk) divided by UART speed
// in this case: 122880000/9600
// since we are using as parameters 9600-8N1 = 12800
// since we are using as parameters 19200-8N1 = 6400
localparam uart_clock_per_bit = 160;
// high when the byte is transmitted
wire uart_tx_done;
// high when the UART needs to send the byte
reg uart_tx_dv = 1'b0;
// byte to transmit through UART
reg [7:0] uart_tx_byte = 8'b0;
// high during tramission
wire uart_tx_active;
// instantiate the TX UART module
UART_TX #(.CLKS_PER_BIT(uart_clock_per_bit)) UART_TX_Inst
(
.i_Clock(clk),
.i_TX_DV(uart_tx_dv),
.i_TX_Byte(uart_tx_byte),
.o_TX_Active(uart_tx_active),
.o_TX_Serial(mcu_uart_tx),
.o_TX_Done(uart_tx_done),
.i_Rst_L(1'b1)
);
// waiting that trasmission has finished
reg send_waiting = 1'b0;
// UART TX status
localparam [2:0] TX_IDLE = 3'd0;
localparam [2:0] TX_VERSION = 3'd1;
localparam [2:0] TX_RADIO = 3'd2;
localparam [2:0] TX_PTT = 3'd3;
// state machine for the booting operation
reg [2:0] state_tx = TX_IDLE;
// variable to check if PTT has changed his status
reg ptt_old = 1'b0;
// just to send the radio stage at boot time
// we can got immediatelly to TX_RADIO since
// we need a cycle to initialize the UART module
reg first_start = 1'b1;
// counter used to track how many bytes we have sent within a state
reg [7:0] sent_bytes_counter = 8'b0;
// MCU TX FSM
always @(posedge clk)
begin
if (~send_waiting)
case (state_tx)
TX_IDLE:
begin
if (ptt != ptt_old)
begin
ptt_old <= ptt;
if (ptt)
state_tx <= TX_PTT;
else
state_tx <= TX_RADIO;
end
else if (first_start)
state_tx <= TX_VERSION;
end
TX_VERSION: // send version
begin
if (sent_bytes_counter == 0)
begin
// this is the radio version
uart_tx_byte <= 8'h30 | 8'h03;
uart_tx_dv <= 1'b1;
send_waiting <= 1'b1;
sent_bytes_counter <= sent_bytes_counter + 8'd1;
end
else if (sent_bytes_counter <= 8)
begin
uart_tx_byte <= fw_version[(71-(sent_bytes_counter*8))-:8];
uart_tx_dv <= 1'b1;
send_waiting <= 1'b1;
sent_bytes_counter <= sent_bytes_counter + 8'd1;
end
// at the end go to the next stage
else
begin
sent_bytes_counter <= 8'd0;
if (first_start)
state_tx <= TX_RADIO;
else
state_tx <= TX_IDLE;
end
end
// we are in the radio stage (only RX)
TX_RADIO:
begin
uart_tx_byte <= 8'h23;
uart_tx_dv <= 1'b1;
send_waiting <= 1'b1;
state_tx <= TX_IDLE;
if (first_start)
first_start <= 1'b0;
end
// we are in transmission
TX_PTT:
begin
uart_tx_byte <= 8'h24;
uart_tx_dv <= 1'b1;
send_waiting <= 1'b1;
state_tx <= TX_IDLE;
end
endcase
// send the next when we have sent the previous one
else if (send_waiting & uart_tx_done)
begin
uart_tx_dv <= 1'b0;
send_waiting <= 1'b0;
end
end // always @(posedge clk)
endmodule
//////////////////////////////////////////////////////////////////////
// Author: Russell Merrick
// Git: https://github.com/nandland/nandland
// License: not found
//////////////////////////////////////////////////////////////////////
// This file contains the UART Transmitter. This transmitter is able
// to transmit 8 bits of serial data, one start bit, one stop bit,
// and no parity bit. When transmit is complete o_Tx_done will be
// driven high for one clock cycle.
//
// Set Parameter CLKS_PER_BIT as follows:
// CLKS_PER_BIT = (Frequency of i_Clock)/(Frequency of UART)
// Example: 25 MHz Clock, 115200 baud UART
// (25000000)/(115200) = 217
module UART_TX
#(parameter CLKS_PER_BIT = 217)
(
input i_Rst_L,
input i_Clock,
input i_TX_DV,
input [7:0] i_TX_Byte,
output reg o_TX_Active,
output reg o_TX_Serial,
output reg o_TX_Done
);
localparam TX_IDLE = 3'b000;
localparam TX_START_BIT = 3'b001;
localparam TX_DATA_BITS = 3'b010;
localparam TX_STOP_BIT = 3'b011;
localparam TX_CLEANUP = 3'b100;
reg [2:0] t_SM_Main = TX_IDLE;
reg [$clog2(CLKS_PER_BIT):0] t_Clock_Count;
reg [2:0] t_Bit_Index;
reg [7:0] t_TX_Data;
// Purpose: Control TX state machine
always @(posedge i_Clock or negedge i_Rst_L)
begin
if (~i_Rst_L)
begin
t_SM_Main <= 3'b000;
o_TX_Done <= 1'b0;
end
else
begin
case (t_SM_Main)
TX_IDLE :
begin
o_TX_Serial <= 1'b1; // Drive Line High for Idle
o_TX_Done <= 1'b0;
t_Clock_Count <= 0;
t_Bit_Index <= 0;
o_TX_Active <= 1'b0;
if (i_TX_DV == 1'b1)
begin
o_TX_Active <= 1'b1;
t_TX_Data <= i_TX_Byte;
t_SM_Main <= TX_START_BIT;
end
else
t_SM_Main <= TX_IDLE;
end // case: IDLE
// Send out Start Bit. Start bit = 0
TX_START_BIT :
begin
o_TX_Serial <= 1'b0;
// Wait CLKS_PER_BIT-1 clock cycles for start bit to finish
if (t_Clock_Count < CLKS_PER_BIT-1)
begin
t_Clock_Count <= t_Clock_Count + 1;
t_SM_Main <= TX_START_BIT;
end
else
begin
t_Clock_Count <= 0;
t_SM_Main <= TX_DATA_BITS;
end
end // case: TX_START_BIT
// Wait CLKS_PER_BIT-1 clock cycles for data bits to finish
TX_DATA_BITS :
begin
o_TX_Serial <= t_TX_Data[t_Bit_Index];
if (t_Clock_Count < CLKS_PER_BIT-1)
begin
t_Clock_Count <= t_Clock_Count + 1;
t_SM_Main <= TX_DATA_BITS;
end
else
begin
t_Clock_Count <= 0;
// Check if we have sent out all bits
if (t_Bit_Index < 7)
begin
t_Bit_Index <= t_Bit_Index + 1;
t_SM_Main <= TX_DATA_BITS;
end
else
begin
t_Bit_Index <= 0;
t_SM_Main <= TX_STOP_BIT;
end
end
end // case: TX_DATA_BITS
// Send out Stop bit. Stop bit = 1
TX_STOP_BIT :
begin
o_TX_Serial <= 1'b1;
// Wait CLKS_PER_BIT-1 clock cycles for Stop bit to finish
if (t_Clock_Count < CLKS_PER_BIT-1)
begin
t_Clock_Count <= t_Clock_Count + 1;
t_SM_Main <= TX_STOP_BIT;
end
else
begin
o_TX_Done <= 1'b1;
t_Clock_Count <= 0;
t_SM_Main <= TX_CLEANUP;
o_TX_Active <= 1'b0;
end
end // case: TX_STOP_BIT
// Stay here 1 clock
TX_CLEANUP :
begin
o_TX_Done <= 1'b1;
t_SM_Main <= TX_IDLE;
end
default :
t_SM_Main <= TX_IDLE;
endcase
end // else: !if(~i_Rst_L)
end // always @ (posedge i_Clock or negedge i_Rst_L)
endmodule