Line 33... |
Line 33... |
output is_receiving, // Low when receive line is idle.
|
output is_receiving, // Low when receive line is idle.
|
output is_transmitting, // Low when transmit line is idle.
|
output is_transmitting, // Low when transmit line is idle.
|
output recv_error // Indicates error in receiving packet.
|
output recv_error // Indicates error in receiving packet.
|
);
|
);
|
|
|
parameter CLOCK_DIVIDE = 325; // clock rate (50Mhz) / baud rate (9600) / 16
|
parameter CLOCK_DIVIDE = 1302; // clock rate (50Mhz) / (baud rate (9600) * 4)
|
|
|
// States for the receiving state machine.
|
// States for the receiving state machine.
|
// These are just constants, not parameters to override.
|
// These are just constants, not parameters to override.
|
parameter RX_IDLE = 0;
|
parameter RX_IDLE = 0;
|
parameter RX_CHECK_START = 1;
|
parameter RX_CHECK_START = 1;
|
Line 104... |
Line 104... |
// start of data.
|
// start of data.
|
if (!rx) begin
|
if (!rx) begin
|
// Wait half the period - should resume in the
|
// Wait half the period - should resume in the
|
// middle of this first pulse.
|
// middle of this first pulse.
|
rx_clk_divider = CLOCK_DIVIDE;
|
rx_clk_divider = CLOCK_DIVIDE;
|
rx_countdown = 8;
|
rx_countdown = 2;
|
recv_state = RX_CHECK_START;
|
recv_state = RX_CHECK_START;
|
end
|
end
|
end
|
end
|
RX_CHECK_START: begin
|
RX_CHECK_START: begin
|
if (!rx_countdown) begin
|
if (!rx_countdown) begin
|
// Check the pulse is still there
|
// Check the pulse is still there
|
if (!rx) begin
|
if (!rx) begin
|
// Pulse still there - good
|
// Pulse still there - good
|
// Wait the bit period to resume half-way
|
// Wait the bit period to resume half-way
|
// through the first bit.
|
// through the first bit.
|
rx_countdown = 16;
|
rx_countdown = 4;
|
rx_bits_remaining = 8;
|
rx_bits_remaining = 8;
|
recv_state = RX_READ_BITS;
|
recv_state = RX_READ_BITS;
|
end else begin
|
end else begin
|
// Pulse lasted less than half the period -
|
// Pulse lasted less than half the period -
|
// not a valid transmission.
|
// not a valid transmission.
|
Line 131... |
Line 131... |
if (!rx_countdown) begin
|
if (!rx_countdown) begin
|
// Should be half-way through a bit pulse here.
|
// Should be half-way through a bit pulse here.
|
// Read this bit in, wait for the next if we
|
// Read this bit in, wait for the next if we
|
// have more to get.
|
// have more to get.
|
rx_data = {rx, rx_data[7:1]};
|
rx_data = {rx, rx_data[7:1]};
|
rx_countdown = 16;
|
rx_countdown = 4;
|
rx_bits_remaining = rx_bits_remaining - 1;
|
rx_bits_remaining = rx_bits_remaining - 1;
|
recv_state = rx_bits_remaining ? RX_READ_BITS : RX_CHECK_STOP;
|
recv_state = rx_bits_remaining ? RX_READ_BITS : RX_CHECK_STOP;
|
end
|
end
|
end
|
end
|
RX_CHECK_STOP: begin
|
RX_CHECK_STOP: begin
|
Line 155... |
Line 155... |
// There was an error receiving.
|
// There was an error receiving.
|
// Raises the recv_error flag for one clock
|
// Raises the recv_error flag for one clock
|
// cycle while in this state and then waits
|
// cycle while in this state and then waits
|
// 2 bit periods before accepting another
|
// 2 bit periods before accepting another
|
// transmission.
|
// transmission.
|
rx_countdown = 32;
|
rx_countdown = 8;
|
recv_state = RX_DELAY_RESTART;
|
recv_state = RX_DELAY_RESTART;
|
end
|
end
|
RX_RECEIVED: begin
|
RX_RECEIVED: begin
|
// Successfully received a byte.
|
// Successfully received a byte.
|
// Raises the received flag for one clock
|
// Raises the received flag for one clock
|
// cycle while in this state and then waits
|
// cycle while in this state.
|
// 1/4 bit period before starting back in the
|
recv_state = RX_IDLE;
|
// idle state. This only actually waits for
|
|
// 3/4 of the stop bit before resuming, but
|
|
// this should not do any harm as the stop bit
|
|
// is received the same as the line idle state.
|
|
rx_countdown = 4;
|
|
recv_state = RX_DELAY_RESTART;
|
|
end
|
end
|
endcase
|
endcase
|
|
|
// Transmit state machine
|
// Transmit state machine
|
case (tx_state)
|
case (tx_state)
|
Line 183... |
Line 177... |
// of the tx_byte input.
|
// of the tx_byte input.
|
tx_data = tx_byte;
|
tx_data = tx_byte;
|
// Send the initial, low pulse of 1 bit period
|
// Send the initial, low pulse of 1 bit period
|
// to signal the start, followed by the data
|
// to signal the start, followed by the data
|
tx_clk_divider = CLOCK_DIVIDE;
|
tx_clk_divider = CLOCK_DIVIDE;
|
tx_countdown = 16;
|
tx_countdown = 4;
|
tx_out = 0;
|
tx_out = 0;
|
tx_bits_remaining = 8;
|
tx_bits_remaining = 8;
|
tx_state = TX_SENDING;
|
tx_state = TX_SENDING;
|
end
|
end
|
end
|
end
|
Line 195... |
Line 189... |
if (!tx_countdown) begin
|
if (!tx_countdown) begin
|
if (tx_bits_remaining) begin
|
if (tx_bits_remaining) begin
|
tx_bits_remaining = tx_bits_remaining - 1;
|
tx_bits_remaining = tx_bits_remaining - 1;
|
tx_out = tx_data[0];
|
tx_out = tx_data[0];
|
tx_data = {1'b0, tx_data[7:1]};
|
tx_data = {1'b0, tx_data[7:1]};
|
tx_countdown = 16;
|
tx_countdown = 4;
|
tx_state = TX_SENDING;
|
tx_state = TX_SENDING;
|
end else begin
|
end else begin
|
// Set delay to send out 2 stop bits.
|
// Set delay to send out 2 stop bits.
|
tx_out = 1;
|
tx_out = 1;
|
tx_countdown = 32;
|
tx_countdown = 8;
|
tx_state = TX_DELAY_RESTART;
|
tx_state = TX_DELAY_RESTART;
|
end
|
end
|
end
|
end
|
end
|
end
|
TX_DELAY_RESTART: begin
|
TX_DELAY_RESTART: begin
|