@@ -31,8 +31,8 @@ entity UART is
31
31
Generic (
32
32
BAUD_RATE : integer := 115200 ; -- baud rate value
33
33
DATA_BITS : integer := 8 ; -- legal values: 5,6,7,8
34
- -- PARITY_BIT : string := "none"; -- TODO, now must be none parity bit, legal values: "none", "odd ", "even ", "mark", "space"
35
- -- STOP_BITS : integer; -- TODO, now must be 1 stop bit
34
+ PARITY_BIT : string := " none" ; -- legal values: "none", "even ", "odd ", "mark", "space"
35
+ -- STOP_BITS : integer; -- TODO, now must be 1 stop bit
36
36
CLK_FREQ : integer := 50e6 ; -- set system clock frequency in Hz
37
37
INPUT_FIFO : boolean := False ; -- enable input data FIFO
38
38
FIFO_DEPTH : integer := 256 -- set depth of input data FIFO
@@ -67,6 +67,7 @@ architecture FULL of UART is
67
67
signal tx_busy : std_logic ;
68
68
signal tx_data_in : std_logic_vector (DATA_BITS- 1 downto 0 );
69
69
signal tx_data_send : std_logic ;
70
+ signal tx_parity_bit : std_logic ;
70
71
71
72
signal uart_ticks : integer range 0 to divider_value- 1 ;
72
73
signal uart_clk_en : std_logic ;
@@ -82,8 +83,11 @@ architecture FULL of UART is
82
83
signal rx_bit_count_en : std_logic ;
83
84
signal rx_bit_count_rst : std_logic ;
84
85
signal rx_data_shreg_en : std_logic ;
86
+ signal rx_parity_bit : std_logic ;
87
+ signal rx_parity_error : std_logic := '0' ;
88
+ signal rx_parity_check_en : std_logic ;
85
89
86
- type state is (idle, txsync, startbit, databits, stopbit);
90
+ type state is (idle, txsync, startbit, databits, paritybit, stopbit);
87
91
signal tx_pstate : state;
88
92
signal tx_nstate : state;
89
93
signal rx_pstate : state;
@@ -207,6 +211,24 @@ begin
207
211
end if ;
208
212
end process ;
209
213
214
+ -- -------------------------------------------------------------------------
215
+ -- UART TRANSMITTER PARITY GENERATOR
216
+ -- -------------------------------------------------------------------------
217
+
218
+ tx_parity_g : if (PARITY_BIT /= " none" ) generate
219
+
220
+ tx_parity_gen_i : entity work .UART_PARITY
221
+ generic map (
222
+ DATA_WIDTH => DATA_BITS,
223
+ PARITY_TYPE => PARITY_BIT
224
+ )
225
+ port map (
226
+ DATA_IN => tx_data,
227
+ PARITY_OUT => tx_parity_bit
228
+ );
229
+
230
+ end generate ;
231
+
210
232
-- -------------------------------------------------------------------------
211
233
-- UART TRANSMITTER FSM
212
234
-- -------------------------------------------------------------------------
@@ -224,7 +246,7 @@ begin
224
246
end process ;
225
247
226
248
-- NEXT STATE AND OUTPUTS LOGIC
227
- process (tx_pstate, tx_data_send, tx_clk_en, tx_data, tx_bit_count)
249
+ process (tx_pstate, tx_data_send, tx_clk_en, tx_data, tx_bit_count, tx_parity_bit )
228
250
begin
229
251
230
252
case tx_pstate is
@@ -272,11 +294,27 @@ begin
272
294
tx_bit_count_en <= '1' ;
273
295
274
296
if ((tx_clk_en = '1' ) AND (tx_bit_count = DATA_BITS- 1 )) then
275
- tx_nstate <= idle;
297
+ if (PARITY_BIT = " none" ) then
298
+ tx_nstate <= idle;
299
+ else
300
+ tx_nstate <= paritybit;
301
+ end if ;
276
302
else
277
303
tx_nstate <= databits;
278
304
end if ;
279
305
306
+ when paritybit =>
307
+ tx_busy <= '1' ;
308
+ TX_UART <= tx_parity_bit;
309
+ tx_bit_count_rst <= '1' ;
310
+ tx_bit_count_en <= '0' ;
311
+
312
+ if (tx_clk_en = '1' ) then
313
+ tx_nstate <= idle;
314
+ else
315
+ tx_nstate <= paritybit;
316
+ end if ;
317
+
280
318
when others =>
281
319
tx_busy <= '1' ;
282
320
TX_UART <= '1' ;
@@ -353,6 +391,35 @@ begin
353
391
354
392
DATA_OUT <= rx_data;
355
393
394
+ -- -------------------------------------------------------------------------
395
+ -- UART RECEIVER PARITY GENERATOR AND CHECK
396
+ -- -------------------------------------------------------------------------
397
+
398
+ rx_parity_g : if (PARITY_BIT /= " none" ) generate
399
+
400
+ rx_parity_gen_i : entity work .UART_PARITY
401
+ generic map (
402
+ DATA_WIDTH => DATA_BITS,
403
+ PARITY_TYPE => PARITY_BIT
404
+ )
405
+ port map (
406
+ DATA_IN => rx_data,
407
+ PARITY_OUT => rx_parity_bit
408
+ );
409
+
410
+ rx_parity_check_reg : process (CLK)
411
+ begin
412
+ if (rising_edge (CLK)) then
413
+ if (RST = '1' ) then
414
+ rx_parity_error <= '0' ;
415
+ elsif (rx_parity_check_en = '1' ) then
416
+ rx_parity_error <= rx_parity_bit XOR RX_UART;
417
+ end if ;
418
+ end if ;
419
+ end process ;
420
+
421
+ end generate ;
422
+
356
423
-- -------------------------------------------------------------------------
357
424
-- UART RECEIVER FSM
358
425
-- -------------------------------------------------------------------------
@@ -370,7 +437,7 @@ begin
370
437
end process ;
371
438
372
439
-- NEXT STATE AND OUTPUTS LOGIC
373
- process (rx_pstate, RX_UART, rx_clk_en, rx_bit_count)
440
+ process (rx_pstate, RX_UART, rx_clk_en, rx_bit_count, rx_parity_error )
374
441
begin
375
442
case rx_pstate is
376
443
@@ -381,6 +448,7 @@ begin
381
448
rx_bit_count_en <= '0' ;
382
449
rx_data_shreg_en <= '0' ;
383
450
rx_clk_divider_en <= '0' ;
451
+ rx_parity_check_en <= '0' ;
384
452
385
453
if (RX_UART = '0' ) then
386
454
rx_nstate <= startbit;
@@ -395,6 +463,7 @@ begin
395
463
rx_bit_count_en <= '0' ;
396
464
rx_data_shreg_en <= '0' ;
397
465
rx_clk_divider_en <= '1' ;
466
+ rx_parity_check_en <= '0' ;
398
467
399
468
if (rx_clk_en = '1' ) then
400
469
rx_nstate <= databits;
@@ -409,22 +478,44 @@ begin
409
478
rx_bit_count_en <= '1' ;
410
479
rx_data_shreg_en <= '1' ;
411
480
rx_clk_divider_en <= '1' ;
481
+ rx_parity_check_en <= '0' ;
412
482
413
483
if ((rx_clk_en = '1' ) AND (rx_bit_count = DATA_BITS- 1 )) then
414
- rx_nstate <= stopbit;
484
+ if (PARITY_BIT = " none" ) then
485
+ rx_nstate <= stopbit;
486
+ else
487
+ rx_nstate <= paritybit;
488
+ end if ;
415
489
else
416
490
rx_nstate <= databits;
417
491
end if ;
418
492
493
+ when paritybit =>
494
+ DATA_VLD <= '0' ;
495
+ FRAME_ERROR <= '0' ;
496
+ rx_bit_count_rst <= '1' ;
497
+ rx_bit_count_en <= '0' ;
498
+ rx_data_shreg_en <= '0' ;
499
+ rx_clk_divider_en <= '1' ;
500
+
501
+ if (rx_clk_en = '1' ) then
502
+ rx_nstate <= stopbit;
503
+ rx_parity_check_en <= '1' ;
504
+ else
505
+ rx_nstate <= paritybit;
506
+ rx_parity_check_en <= '0' ;
507
+ end if ;
508
+
419
509
when stopbit =>
420
510
rx_bit_count_rst <= '1' ;
421
511
rx_bit_count_en <= '0' ;
422
512
rx_data_shreg_en <= '0' ;
423
513
rx_clk_divider_en <= '1' ;
514
+ rx_parity_check_en <= '0' ;
424
515
425
516
if (rx_clk_en = '1' ) then
426
517
rx_nstate <= idle;
427
- DATA_VLD <= '1' ;
518
+ DATA_VLD <= NOT rx_parity_error ;
428
519
FRAME_ERROR <= NOT RX_UART;
429
520
else
430
521
rx_nstate <= stopbit;
@@ -439,6 +530,7 @@ begin
439
530
rx_bit_count_en <= '0' ;
440
531
rx_data_shreg_en <= '0' ;
441
532
rx_clk_divider_en <= '0' ;
533
+ rx_parity_check_en <= '0' ;
442
534
rx_nstate <= idle;
443
535
444
536
end case ;
0 commit comments