?? onewire_master.vhd
字號(hào):
when INIT => ---------------------------------------
-- Reset/Initialization state
---------------------------------------
-- The one-wire bus will be pulled up,
-- so that next state we can send a
-- Reset Pulse (active low) to the bus.
---------------------------------------
dout <= '1'; -- begin the operation by pulling up
-- one-wire bus to high
d_ctrl <= '0'; -- write to the one-wire bus
nextState <= TX_RST_PLS;
jc1_reset <= '1';
when TX_RST_PLS => ---------------------------------------
-- Transmit Reset Pulse state
---------------------------------------
-- In this state, the one-wire bus will
-- be pulled down (Tx "Reset Pulse") for
-- 480 us to reset the one-wire
-- device connected to the bus.
--
-- It enables FSM to move to next state
-- at 480 us. The transition of the state
-- will happend at 500 us.
--
-- Use JC1 and SR2 here to count for
-- longer time duration (0 ~ 480 us):
-----------------------------------------
-- Time JC1 SR2 SR2 SR2
-- elapse En Rst
-- (us) msb lsb
-----------------------------------------
-- 0 "00" '0' '0' "00000001"
-- 20 "01" '0' '0' "00000001"
-- 40 "11" '0' '0' "00000001"
-- 60 "10" '1' '0' "00000001"
-- 80 "00" '0' '0' "00000010"
-- 100 "01" '0' '0' "00000010"
-- 120 "11" '0' '0' "00000010"
-- 140 "10" '1' '0' "00000010"
-- 160 "00" '0' '0' "00000100"
-- 180 "01" '0' '0' "00000100"
-- ... ... ... ... ...
-- ... ... ... ... ...
-- 240 "00" '0' '0' "00001000"
-- ... ... ... ... ...
-- 320 "00" '0' '0' "00010000"
-- ... ... ... ... ...
-- 400 "00" '0' '0' "00100000"
-- ... ... ... ... ...
-- 480 "00" '0' '1' "01000000"
-- 500 "01" '0' '0' "00000001"
----------------------------------------
sr2_en <= ts_60_to_80us; -- enable sr2 to shift every 80 us,
-- transition will occur at next clock
-- cycle
if sr2_q(6) = '1' then -- count till 480 us has passed.
d_ctrl <= '1'; -- release one-wire bus by changing to
-- read mode
nextState <= RX_PRE_PLS; -- goes to next state
jc1_reset <= '1';
sr2_reset <= '1'; -- reset sr2 at 480us
else -- 0 ~ 480 us
d_ctrl <= '0'; -- write data to one-wire bus
dout <= '0'; -- Tx "Reset Pulse" for 480us
sr2_reset <= '0'; -- use sr2 to count for longer
-- time duration (0 ~ 480 us),
end if;
when RX_PRE_PLS =>
---------------------------------------
-- Detect Presence Pulse state
---------------------------------------
-- In this state, it sample the data
-- on the one wire bus when the
-- "Presence Pulse" will occur.
-- The data will be latched at 0~80 us.
-- Then it waits till total 500us has
-- has passed, and moves to next state
-- or goes back to INIT state according
-- to the presence of the "Presence
-- Pulse"
--
-- Use JC1 and SR2 here to count for
-- longer time duration (0 ~ 480 us):
--
-- Note:"Presence Pulse" indicates
-- a Serial Number Device is on the bus
-- and it's ready to operate.
----------------------------------------
sr2_reset <= '0' ; -- use sr2 to count for longer
-- time duration (0 ~ 480 us),
sr2_en <= ts_60_to_80us; -- enable sr2 to shift every 80 us
if sr2_q(6) = '1' then -- 480us passed
d_ctrl <= '1'; -- remain read status on the bus
if din_pp ='0' and din = '1' then -- detect presence pulse
-- and pull up after the
-- presence pulse
nextState <= TX_RD_CMD;
jc1_reset <= '1';
else
nextState <= INIT;
end if;
else -- 0 ~ 480 us
d_ctrl <= '1'; -- use read mode on the one-wire bus
end if;
when TX_RD_CMD => ---------------------------------------
-- Transmit ROM Function Command state
---------------------------------------
-- In this state, the onewire bus is
-- pulled down during first 10 us.
--
-- Then according to each bit of data
-- in the ROM Command (0x0F for Read ROM),
-- we write data to the Serial Number
-- Device:
-- (1) if we need to write '1' to the serial
-- number device, it will release
-- the one-wire bus to allow the
-- pull-up resistor to pull the wire to
-- '1';(2) if we need to write '0' to
-- the device, we output '0' directly
-- to the bus. This process happens from
-- 10 us to 60 us.
--
-- After 60us, it releases the bus allowing
-- the one-wire bus to be pulled back to
-- high, and enable SR1 to shift to
-- next bit.
--
-- After another 20us, the transition of SR1
-- will take place. The process will repeat
-- to transmit another bit in the ROM Command,
-- till all 8 bits in the ROM Command have
-- been sent out.
--
-- After 8 bits of data has been sent out,
-- it moves to next state
-----------------------------------------
sr1_reset <= '0'; -- start to use sr1 to count 8 bits in
-- one byte of data
if ts_60_to_80us = '1' then -- 60 us passed
d_ctrl <= '1'; -- set read mode to release the one-wire
-- bus
sr1_en <= '1'; -- one bit is sent, enable sr1 to
-- move to next bit after 80us
if sr1_q(7) = '1' then -- when all 8 bits has been sent
nextState <= RX_DATA; -- move to next state
jc1_reset <= '1';
end if;
elsif ts_0_to_10us = '1' then -- 0 ~ 10 us
dout <= '0'; -- output '0' to one-wire bus
d_ctrl <= '0'; -- set write mode on the bus
else -- 10 us ~ 60 us
dout <= tx_cmd_bit; -- write command bit to the bus
d_ctrl <= tx_cmd_bit;
-- if write '1' to the one-wire bus,
-- we disable output by set read mode on
-- the bus, and use the external pull-up
-- resistor to pull up the one wire bus
-- to high (which represents '1')
--
-- if write '0' to the one-wire bus,
-- we actually send a '0' to the one-wire
-- bus using write mode on the bus
end if;
when RX_DATA => ---------------------------------------
-- Receive Serial Number Data state
---------------------------------------
-- In this state, the onewire bus is
-- pulled down during first 1 us, thisthen
-- is the initialization of the Rx of one
-- bit . Then it release the bus by change
-- back to read mode.
--
-- From 13us to 15 us, it samples the
-- data on the one-wire bus, and assert
-- databit_valid signal.
--
-- After 15us, it release the bus allowing
-- the one-wire bus to be pulled back to
-- high.
--
-- At 60us, it enables SR1 to shift to
-- next bit. After 80us, it finished
-- reading ONE bit. Then it repeats
-- the process to receive other 7
-- bits in one byte.
--
-- After 8 bits for one byte of data have
-- been received, the SR2 is used to
-- count for total 8 bytes of serial
-- number data.
--
-- After 8 bytes of data has been received,
-- it moves to next state
-----------------------------------------
sr1_reset <= '0'; -- start to use sr1 to count 8 bits.
-- sr1 is configured to scroll over
-- when it reach the end. So it's
-- not necessary to reset it.
sr2_reset <= '0'; -- start to use sr2 to count for
-- 8 bytes coming from the one-wire device
if ts_60_to_80us = '1' then -- 60 us passed
sr1_en <= '1'; -- one bit is read, enable sr1 to move to
-- next bit
sr2_en <= sr1_q(7);-- when sr1 shift to last bit which
-- means all 8 bits in this byte of
-- data have already been read,
-- then we can move to next byte
databyte_valid <= sr1_q(7);
-- enable data output to external
-- world when all 8 bits are read
-- in.
if ( sr2_q(7) and databyte_valid) = '1' then
-- move to next state when all 8 bytes
-- has been received.
nextState <= IDLE;
jc1_reset <= '1';
end if;
elsif ts_0_to_1us = '1' then -- 0~1 us
dout <= '0'; -- pull down for only 1 us
d_ctrl <= '0'; -- output '0' to one-wire bus to start write
-- period
else -- 1~ 60 us
d_ctrl <= '1'; -- release one-wire bus by keeping the read
-- mode
databit_valid <= ts_14_to_15us;
-- assert databit_valid from 13 to 15us
-- in order to latch the data reading from
-- the one-wire device.
end if;
when IDLE =>
---------------------------------------
-- IDLE state
---------------------------------------
-- The onewire bus will be released to
-- read mode; the data bus will keep the
-- last byte (CRC value); crcok will
-- be valid as a latch signal.
--
-- Once enter IDLE state, it stays here
-- unless getting a system reset signal.
---------------------------------------
if crcok_i = '0' then
nextState <= INIT;
else
nextState <= IDLE;
end if;
when Others =>
end case;
end process statemux;
end rtl;
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -