Line 1... |
Line 1... |
--! uart control unit
|
--! uart control unit
|
library IEEE;
|
library IEEE;
|
use IEEE.STD_LOGIC_1164.ALL;
|
use IEEE.STD_LOGIC_1164.ALL;
|
|
use ieee.std_logic_unsigned.all;
|
|
use ieee.std_logic_arith.all;
|
|
|
--! Use CPU Definitions package
|
--! Use CPU Definitions package
|
use work.pkgDefinitions.all;
|
use work.pkgDefinitions.all;
|
|
|
entity uart_control is
|
entity uart_control is
|
Line 24... |
Line 26... |
end uart_control;
|
end uart_control;
|
|
|
architecture Behavioral of uart_control is
|
architecture Behavioral of uart_control is
|
signal config_clk : std_logic_vector((nBitsLarge-1) downto 0);
|
signal config_clk : std_logic_vector((nBitsLarge-1) downto 0);
|
signal config_baud : std_logic_vector((nBitsLarge-1) downto 0);
|
signal config_baud : std_logic_vector((nBitsLarge-1) downto 0);
|
--signal byte_to_receive : std_logic_vector((nBits-1) downto 0);
|
signal received_byte : std_logic_vector((nBits-1) downto 0);
|
signal byte_to_transmitt : std_logic_vector((nBits-1) downto 0);
|
signal byte_to_transmit : std_logic_vector((nBits-1) downto 0);
|
signal controlStates : uartControl;
|
|
|
|
signal sigDivRst : std_logic;
|
signal sigDivRst : std_logic;
|
signal sigDivDone : std_logic;
|
signal sigDivDone : std_logic;
|
signal sigDivQuotient : std_logic_vector((nBitsLarge-1) downto 0);
|
signal sigDivQuotient : std_logic_vector((nBitsLarge-1) downto 0);
|
--signal sigDivReminder : std_logic_vector((nBitsLarge-1) downto 0);
|
|
signal sigDivNumerator : std_logic_vector((nBitsLarge-1) downto 0);
|
signal sigDivNumerator : std_logic_vector((nBitsLarge-1) downto 0);
|
signal sigDivDividend : std_logic_vector((nBitsLarge-1) downto 0);
|
signal sigDivDividend : std_logic_vector((nBitsLarge-1) downto 0);
|
|
|
|
-- Signals used to control the configuration
|
|
signal startConfigBaud : std_logic;
|
|
signal startConfigClk : std_logic;
|
|
signal startDataSend : std_logic;
|
|
signal commBlocksInitiated : std_logic;
|
|
signal finishedDataSend : std_logic;
|
|
signal doneWriteReg : std_logic;
|
|
signal startReadReg : std_logic;
|
|
|
-- Divisor component
|
-- Divisor component
|
component divisor is
|
component divisor is
|
Port ( rst : in STD_LOGIC;
|
Port ( rst : in STD_LOGIC;
|
clk : in STD_LOGIC;
|
clk : in STD_LOGIC;
|
quotient : out STD_LOGIC_VECTOR ((nBitsLarge-1) downto 0);
|
quotient : out STD_LOGIC_VECTOR ((nBitsLarge-1) downto 0);
|
Line 58... |
Line 67... |
numerator => sigDivNumerator,
|
numerator => sigDivNumerator,
|
divident => sigDivDividend,
|
divident => sigDivDividend,
|
done => sigDivDone
|
done => sigDivDone
|
);
|
);
|
|
|
-- Process that read uart control registers
|
-- Process to handle the of writting the registers
|
process (rst, reg_addr, WE, start, byte_to_transmitt, data_byte_rx, rx_data_ready, config_clk, config_baud)
|
process (clk)
|
begin
|
begin
|
|
-- On the wishbone specification we should handle the reset synchronously
|
|
if rising_edge(clk) then
|
if rst = '1' then
|
if rst = '1' then
|
DAT_O <= (others => 'Z');
|
config_clk <= (others => '0');
|
else
|
config_baud <= (others => '0');
|
if (WE = '0') and (start = '1') then
|
byte_to_transmit <= (others => '0');
|
|
startConfigBaud <= '0';
|
|
startConfigClk <= '0';
|
|
startDataSend <= '0';
|
|
doneWriteReg <= '0';
|
|
elsif (WE and start) = '1' then
|
case reg_addr is
|
case reg_addr is
|
when "00" =>
|
when "00" =>
|
DAT_O <= config_clk;
|
config_clk <= DAT_I;
|
|
startConfigClk <= '1';
|
|
startDataSend <= '0';
|
|
startConfigBaud <= '0';
|
when "01" =>
|
when "01" =>
|
DAT_O <= config_baud;
|
config_baud <= DAT_I;
|
|
startConfigBaud <= '1';
|
|
startDataSend <= '0';
|
|
startConfigClk <= '0';
|
when "10" =>
|
when "10" =>
|
-- Byte that will be transmitted
|
-- If we have an overrun, discard the byte
|
DAT_O <= "000000000000000000000000" & byte_to_transmitt;
|
if finishedDataSend = '1' then
|
when "11" =>
|
byte_to_transmit <= DAT_I((nBits-1) downto 0);
|
-- Byte that will be received
|
|
if rx_data_ready = '1' then
|
|
DAT_O <= "000000000000000000000000" & data_byte_rx;
|
|
--DAT_O <= "000000000000000000000000" & byte_to_receive;
|
|
else
|
else
|
DAT_O <= (others => 'Z');
|
byte_to_transmit <= byte_to_transmit;
|
end if;
|
end if;
|
|
startConfigBaud <= '0';
|
|
startConfigClk <= '0';
|
|
startDataSend <= '1';
|
when others =>
|
when others =>
|
DAT_O <= (others => 'Z');
|
startConfigBaud <= '0';
|
|
startConfigClk <= '0';
|
|
startDataSend <= '0';
|
end case;
|
end case;
|
else
|
|
DAT_O <= (others => 'Z');
|
|
end if;
|
end if;
|
end if;
|
end if;
|
end process;
|
end process;
|
|
|
-- Process that populate the uart control registers
|
-- Process to handle the reading of registers
|
process (rst, clk, reg_addr,WE,start)
|
process (clk)
|
begin
|
begin
|
|
-- On the wishbone specification we should handle the reset synchronously
|
|
if rising_edge(clk) then
|
if rst = '1' then
|
if rst = '1' then
|
config_clk <= (others => '0');
|
DAT_O <= (others => 'Z');
|
config_baud <= (others => '0');
|
startReadReg <= '0';
|
byte_to_transmitt <= (others => '0');
|
elsif ((WE = '0') and (start = '1')) then
|
elsif rising_edge(clk) then
|
startReadReg <= '1';
|
if (WE = '1') and (start = '1') then
|
|
case reg_addr is
|
case reg_addr is
|
when "00" =>
|
when "00" =>
|
config_clk <= DAT_I;
|
DAT_O <= config_clk;
|
when "01" =>
|
when "01" =>
|
config_baud <= DAT_I;
|
DAT_O <= config_baud;
|
when "10" =>
|
when "10" =>
|
-- Byte that will be transmitted
|
DAT_O <= conv_std_logic_vector(0, (nBitsLarge-nBits)) & byte_to_transmit;
|
byte_to_transmitt <= DAT_I((nBits-1) downto 0);
|
when "11" =>
|
|
DAT_O <= conv_std_logic_vector(0, (nBitsLarge-nBits)) & received_byte;
|
when others =>
|
when others =>
|
null;
|
null;
|
end case;
|
end case;
|
end if;
|
end if;
|
end if;
|
end if;
|
end process;
|
end process;
|
|
|
-- Process to handle the next state logic
|
-- Process that stores the data that comes from the serial receiver block
|
process (rst, clk, reg_addr, WE, start)
|
process (rx_data_ready)
|
variable baud_configured : std_logic;
|
|
variable clk_configured : std_logic;
|
|
variable div_result_baud_wait : std_logic_vector ((nBitsLarge-1) downto 0);
|
|
begin
|
begin
|
if rst = '1' then
|
if rising_edge(rx_data_ready) then
|
controlStates <= idle;
|
received_byte <= data_byte_rx;
|
baud_configured := '0';
|
|
clk_configured := '0';
|
|
div_result_baud_wait := (others => '0');
|
|
done <= '0';
|
|
sigDivRst <= '1';
|
|
rst_comm_blocks <= '1';
|
|
tx_start <= '0';
|
|
--byte_to_receive <= (others => 'Z');
|
|
elsif rising_edge(clk) then
|
|
case controlStates is
|
|
when idle =>
|
|
done <= '0';
|
|
-- Go to config state
|
|
if (reg_addr = "00") and (WE = '1') then
|
|
controlStates <= config_state_clk;
|
|
clk_configured := '1';
|
|
elsif (reg_addr = "01") and (WE = '1') then
|
|
controlStates <= config_state_baud;
|
|
baud_configured := '1';
|
|
end if;
|
|
|
|
when config_state_clk =>
|
|
sigDivRst <= '1';
|
|
sigDivNumerator <= config_clk;
|
|
if baud_configured = '0' then
|
|
-- Baud not configured yet so wait for it...
|
|
controlStates <= idle;
|
|
done <= '1';
|
|
else
|
else
|
-- If already configured wait for division completion...
|
received_byte <= received_byte;
|
controlStates <= start_division;
|
|
end if;
|
end if;
|
|
end process;
|
|
|
when config_state_baud =>
|
-- Process to send data over the serial transmitter
|
sigDivRst <= '1';
|
process (startDataSend, commBlocksInitiated, clk)
|
sigDivDividend <= config_baud;
|
variable cont_steps : integer range 0 to 3;
|
if clk_configured = '0' then
|
begin
|
-- Clock not configured yet so wait for it...
|
if (startDataSend = '0' and commBlocksInitiated = '0') then
|
controlStates <= idle;
|
data_byte_tx <= (others => '0');
|
done <= '1';
|
tx_start <= '0';
|
|
finishedDataSend <= '1';
|
|
elsif rising_edge(clk) then
|
|
if cont_steps < 3 then
|
|
cont_steps := cont_steps + 1;
|
else
|
else
|
-- If already configured wait for division completion...
|
cont_steps := 3;
|
controlStates <= start_division;
|
|
end if;
|
end if;
|
|
|
when start_division =>
|
case cont_steps is
|
sigDivRst <= '0';
|
when 1 =>
|
controlStates <= wait_division;
|
data_byte_tx <= byte_to_transmit;
|
|
tx_start <= '0';
|
|
when 2 =>
|
|
tx_start <= '1';
|
|
when others =>
|
|
null;
|
|
end case;
|
|
|
when wait_division =>
|
if tx_data_sent = '1' then
|
if sigDivDone = '0' then
|
finishedDataSend <= '1';
|
controlStates <= wait_division;
|
|
else
|
else
|
-- Division done, get the result to put on the wait_cycles signal of the baud generator
|
finishedDataSend <= '0';
|
div_result_baud_wait := sigDivQuotient;
|
|
controlStates <= config_state_baud_generator;
|
|
end if;
|
end if;
|
|
|
when config_state_baud_generator =>
|
|
-- Configure the wait_cycle for the desired baud rate...
|
|
baud_wait <= div_result_baud_wait;
|
|
controlStates <= rx_tx_state;
|
|
done <= '1';
|
|
|
|
-- Control the serial_receiver or serial_transmitter block
|
|
when rx_tx_state =>
|
|
rst_comm_blocks <= '0';
|
|
tx_start <= '0';
|
|
controlStates <= rx_tx_state;
|
|
if (WE = '1') and (start = '1') then
|
|
if reg_addr = "10" then
|
|
controlStates <= tx_state_wait;
|
|
done <= '0';
|
|
end if;
|
|
end if;
|
end if;
|
|
end process;
|
|
|
if (WE = '0') and (start = '1') then
|
-- Process to send the ACK signal, remember that optimally this ACK should be as fast as possible
|
case reg_addr is
|
-- to avoid locking the bus, on this case if you send a more bytes then you can transmit the ideal
|
when "11" =>
|
-- is to create an error flag to indicate overrun.
|
controlStates <= rx_state_wait;
|
-- On this case on any attempt of reading or writting on registers we will be lock on 1 cycle
|
|
process (clk, rst, startConfigBaud, startConfigClk, startDataSend, startReadReg )
|
when "10" =>
|
variable joinSignal : std_logic_vector(3 downto 0);
|
|
variable cont_steps : integer range 0 to 3;
|
|
begin
|
|
if rising_edge(clk) then
|
|
if rst = '1' then
|
done <= '1';
|
done <= '1';
|
controlStates <= rx_tx_state;
|
cont_steps := 0;
|
|
else
|
when others =>
|
joinSignal := startConfigBaud & startConfigClk & startDataSend & startReadReg;
|
null;
|
if (joinSignal = "0000") then
|
end case;
|
done <= '1';
|
end if;
|
else
|
|
case cont_steps is
|
if (start = '0') then
|
when 0 =>
|
|
if start = '1' then
|
done <= '0';
|
done <= '0';
|
end if;
|
end if;
|
|
when others =>
|
|
done <= '1';
|
|
end case;
|
|
|
|
if cont_steps < 2 then
|
-- Send data and wait to transmit
|
cont_steps := cont_steps + 1;
|
when tx_state_wait =>
|
|
tx_start <= '1';
|
|
data_byte_tx <= byte_to_transmitt;
|
|
if tx_data_sent = '0' then
|
|
controlStates <= tx_state_wait;
|
|
else
|
else
|
controlStates <= rx_tx_state;
|
cont_steps := 0;
|
done <= '1';
|
end if;
|
|
end if;
|
end if;
|
end if;
|
|
end if;
|
|
end process;
|
|
|
-- Receive data and wait to receive
|
-- Process to calculate the amount of cycles to wait (clock_speed / desired_baud), and initiate the board
|
when rx_state_wait =>
|
process (startConfigBaud,startConfigClk, clk)
|
if rx_data_ready = '1' then
|
variable cont_steps : integer range 0 to 3;
|
-- Put an ack on the next cycle
|
begin
|
controlStates <= rx_state_ack;
|
if (startConfigBaud and startConfigClk) = '0' then
|
|
sigDivRst <= '1';
|
|
cont_steps := 0;
|
|
baud_wait <= (others => '0');
|
|
commBlocksInitiated <= '0';
|
|
elsif rising_edge(clk) then
|
|
if cont_steps < 3 then
|
|
cont_steps := cont_steps + 1;
|
else
|
else
|
controlStates <= rx_state_wait;
|
cont_steps := 3;
|
done <= '0';
|
|
end if;
|
end if;
|
|
|
-- Ack that we got a value
|
case cont_steps is
|
when rx_state_ack =>
|
when 1 =>
|
done <= '1';
|
sigDivNumerator <= config_clk;
|
controlStates <= rx_tx_state;
|
sigDivDividend <= config_baud;
|
|
sigDivRst <= '1';
|
|
when 2 =>
|
|
sigDivRst <= '0';
|
|
when others =>
|
|
null;
|
end case;
|
end case;
|
|
|
|
-- Enable the communication block when the baud is calculated
|
|
if sigDivDone = '1' then
|
|
rst_comm_blocks <= '0';
|
|
baud_wait <= sigDivQuotient;
|
|
commBlocksInitiated <= '1';
|
|
else
|
|
baud_wait <= (others => '0');
|
|
rst_comm_blocks <= '1';
|
|
commBlocksInitiated <= '0';
|
|
end if;
|
end if;
|
end if;
|
end process;
|
end process;
|
|
|
end Behavioral;
|
end Behavioral;
|
|
|