用于DS18B20温度传感器的1线内核的VHDL封装器

时间:2017-07-02 14:04:28

标签: vhdl fpga 1wire

目前我正在尝试为this Opencore Verilog模块(1线主机)编写VHDL包装器,以便我可以从this温度传感器(DS18B20)发送/接收。

但是我很难理解其用法。即读/写使能与1线主模块的控制/状态寄存器中的循环位。

到目前为止,我所拥有的代码将cyc位设置为1,同时将读/写使能设置为1,但不会在每个位期间将它们循环。这是正确的还是我误解了?我是VHDL /阅读数据表的新手,所以我几天都在努力解决这个问题。任何帮助将不胜感激。

我发现我一直在使用this site作为参考,但它不涉及我正在使用的Verilog模块。

我也在寻找有关我的代码风格和一般VHDL技巧的提示。

我目前的代码:

LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL; --may need to remove if signed not used

ENTITY one_wire_temp_probe_control IS
    GENERIC (
        one_us_divider_g : integer range 0 to 50      := 50      -- clock divider for one micro second
    );    
    PORT (
        i_clk_50mhz      : IN STD_LOGIC;
        i_read_enable    : IN std_logic;
        io_temp_probe    : INOUT STD_LOGIC; --how do i register an inout
        o_temperature    : OUT signed(6 DOWNTO 0); 
        o_temp_ready     : OUT std_logic
     );
END one_wire_temp_probe_control;

ARCHITECTURE rtl of one_wire_temp_probe_control IS
    ----temp commands----
    CONSTANT skip_rom_c          : std_logic_vector(7 DOWNTO 0) := x"CC"; --command to skip ROM identity of temperature sensor
    CONSTANT convert_temp_c      : std_logic_vector(7 DOWNTO 0) := x"44"; --command to start temperature conversion
    CONSTANT read_scratchpad_c   : std_logic_vector(7 DOWNTO 0) := x"BE"; --command to read the scratchpad i.e. get temperature data
    CONSTANT command_bits_c      : integer RANGE 0 TO 8         := 8;     --number of bits in the above commands (note: range used to limit number of bits to minimum needed)
    CONSTANT data_bits_c         : integer RANGE 0 to 12        := 12;    --number of bits in received data
    ----1-wire commands----
    CONSTANT send_reset_pulse          : std_logic_vector(7 DOWNTO 0) := "00001010"; --command to send reset pulse
    CONSTANT write_command_structure_c : std_logic_vector(6 DOWNTO 0) := "0000000";  --structure of the command that must be passed to the 1-wire controller (----EDIT----)
    ----timing constants----
    CONSTANT delay_65us_c        : integer := one_us_divider_g * 65;      --65 micro-second delay
    CONSTANT delay_960us_c       : integer := one_us_divider_g * 960;     --960 micro-second delay 
    CONSTANT delay_750ms         : integer := one_us_divider_g * 1000 * 750; --760 milli-second delay

    ----state machine----
    TYPE state_type              IS (idle, presence_pulse, wait_presence_pulse, skip_rom, temp_conversion, wait_for_conversion,
                                     read_scratchpad, data_read, convert_data, wait_65us);
    SIGNAL state                 : state_type := idle;
    SIGNAL previous_state        : state_type := idle;
    ----1-wire----
    SIGNAL read_enable_s, write_enable_s, reset_s, owr_e_s : std_logic := '0';
    SIGNAL write_data_s, read_data_s                       : std_logic_vector(7 DOWNTO 0):= (OTHERS => '0'); --8 bit mode chosen in sockit_owm 
    SIGNAL address_s                                       : std_logic_vector(1 DOWNTO 0) := "00"; 
    SIGNAL timer_s                                         : integer := 0;
    ----commands---
    SIGNAL bit_counter_command_s : integer RANGE 0 TO command_bits_c := 0;  --counter for bits in commands (note: not -1 due to using 9th bit as state change)
    SIGNAL bit_counter_data_s    : integer RANGE 0 TO data_bits_c    := 0;  --counter for bits in data recieved
    ----temperature----
    SIGNAL temperature_raw_data  : std_logic_vector(11 DOWNTO 0) := (OTHERS => '0');

    ----one wire control----
    COMPONENT sockit_owm IS
    PORT (
        ----control interface----
        clk     : IN std_logic;
        rst     : IN std_logic;
        bus_ren : IN std_logic;
        bus_wen : IN std_logic;
        bus_adr : IN std_logic_vector(7 DOWNTO 0);
        bus_wdt : IN std_logic_vector(7 DOWNTO 0);
        bus_rdt : OUT std_logic_vector(7 DOWNTO 0);
        bus_irq : OUT std_logic;
        ----1-wire interface----
        owr_p   : OUT std_logic; --verilog code is a one bit wide vector
        owr_e   : OUT std_logic;
        owr_i   : IN std_logic
    );
    END COMPONENT;

BEGIN

    address_s <= "00"; --for the temp probe control we're not interested in other address spaces

    PROCESS(i_clk_50mhz) BEGIN --state change
        IF rising_edge(i_clk_50mhz) THEN
            CASE state is
                WHEN idle =>
                    o_temp_ready <= '0';
                    IF (i_read_enable = '1') THEN
                        state <= presence_pulse;
                    ELSE
                        state <= idle;
                    END IF; 

                WHEN presence_pulse =>
                ----send reset/presence pulse----
                    write_enable_s  <= '1';
                    write_data_s    <= send_reset_pulse;
                    timer_s         <= delay_960us_c;
                    state           <= wait_presence_pulse;

                WHEN wait_presence_pulse =>
                ----wait for 960 micro seconds----
                    read_enable_s <= '1';
                    IF (timer_s = 0) THEN
                        IF (read_data_s(0) = '0') THEN
                            state <= skip_rom;
                        ELSIF (read_data_s(0) = '1') THEN
                            --precence not detected
                        ELSE
                            state <= wait_presence_pulse;
                        END IF;
                    ELSE
                        timer_s <= timer_s - 1;
                        state   <= wait_presence_pulse;
                    END IF;

                WHEN skip_rom =>
                ----send skip rom command----
                    previous_state <= skip_rom;
                    write_enable_s <= '1';
                        IF (bit_counter_command_s = command_bits_c) THEN
                            bit_counter_command_s <= 0;
                            state                 <= temp_conversion;
                        ELSE
                            write_data_s <= write_command_structure_c & skip_rom_c(bit_counter_command_s); ---command structure concatonated with 1 bit from command
                            bit_counter_command_s <= bit_counter_command_s + 1;
                            timer_s               <= delay_65us_c;
                            state                 <= wait_65us;
                        END IF;   

                WHEN temp_conversion =>
                ----send temp conversion command to probe----
                    previous_state <= temp_conversion;
                    IF (bit_counter_command_s = bit_counter_command_s) THEN
                        bit_counter_command_s   <= 0;
                        timer_s                 <= delay_750ms;
                        state                   <= wait_for_conversion;
                    ELSE
                        write_data_s <= write_command_structure_c & convert_temp_c(bit_counter_command_s); ---command structure concatonated with 1 bit from command
                        bit_counter_command_s   <= bit_counter_command_s + 1;
                        timer_s                 <= delay_65us_c;
                        state                   <= wait_65us;
                    END IF;

                WHEN wait_for_conversion =>
                ----wait for temperature conversion to finish----
                    IF (timer_s = 0) then
                        state <= read_scratchpad;
                    ELSE
                        timer_s <= timer_s - 1;
                    END IF;

                WHEN read_scratchpad =>
                ----send read scratchpad command----
                    previous_state <= read_scratchpad;
                    IF (bit_counter_command_s = command_bits_c) THEN
                        state                 <= data_read;
                        bit_counter_command_s <= 0;
                    ELSE
                        write_data_s <= write_command_structure_c & read_scratchpad_c(bit_counter_command_s); ---command structure concatonated with 1 bit from command
                        bit_counter_command_s <= bit_counter_command_s + 1;
                        timer_s               <= delay_65us_c;
                        state                 <= wait_65us;
                    END IF;

                WHEN data_read =>
                ----read incoming data----
                    previous_state <= data_read;
                    read_enable_s <= '1';
                    IF (bit_counter_data_s = data_bits_c) THEN
                        bit_counter_data_s <= 0; --may need to invert this
                        state              <= convert_data;
                    ELSE
                        temperature_raw_data(bit_counter_data_s) <= read_data_s(0);
                        bit_counter_data_s                       <= bit_counter_data_s + 1;
                        timer_s                                  <= delay_65us_c;
                        state                                    <= wait_65us;
                    END IF;

                WHEN convert_data =>
                ----convert raw data into temperature----
                    o_temp_ready <= '1';

                WHEN wait_65us =>
                ----wait for read/write cycle to finish----
                    IF (timer_s = 0) THEN
                        state <= previous_state;
                    ELSE 
                        timer_s <= timer_s - 1;   
                        state   <= wait_65us;
                    END IF;
            END CASE;
        END IF;
    END PROCESS;

    ----one wire component instantiation----
    one_wire_control : sockit_owm
    PORT MAP(
        ----control interface----
        clk     => i_clk_50mhz,
        rst     => reset_s,
        bus_ren => read_enable_s,
        bus_wen => write_enable_s,
        bus_adr => address_s,
        bus_wdt => write_data_s,
        bus_rdt => read_data_s,
        bus_irq => OPEN,
        ----1-wire interface----
        owr_p   => OPEN,
        owr_e   => owr_e_s,
        owr_i   => io_temp_probe
    );
    io_temp_probe <= owr_e_s ? '0' : 'Z'; --I also need help converting this line to VHDL

END rtl;

提前谢谢你。 最好 汤姆

1 个答案:

答案 0 :(得分:1)

  

我也在寻找有关我的代码风格和一般VHDL技巧的提示。

行。

第一件事:不要让线条这么长。所以不要在一行末尾添加注释。在他们之前加上一行。

use IEEE.NUMERIC_STD.ALL; --may need to remove if signed not used

然后删除,因为我没有看到任何signed

one_us_divider_g : integer range 0 to 50      := 50      -- clock divider for one micro second

那么...... one_us_divider_g被设置为0会发生什么?似乎是非法的价值。用它来模拟?

io_temp_probe    : INOUT STD_LOGIC; --how do i register an inout

一种选择是使用三态IOBUFFER。这是一个特殊的FPGA边缘元件,它将输入和输出分成单独的信号。您可以通过设置控制端口使输出处于三态。

或者你也可以像在代码中一样进行(例如,Xilinx综合用户指南中也对此进行了解释)。这引出了我的代码中的另一个问题。

  

io_temp_probe&lt; = owr_e_s? '0':'Z'; - 我还需要帮助将此行转换为VHDL

io_temp_probe <= '0' when owr_e_s = '1' else 'Z';
CONSTANT command_bits_c      : integer RANGE 0 TO 8         := 8;

如果它是常数,则不需要整数范围。

CONSTANT send_reset_pulse : ...
CONSTANT delay_750ms : ...

缺少你所有常量后面的“_c”。但无论如何我不会添加这个“ s ”,“_ c”或“_g”。很多工作收获甚微。

COMPONENT sockit_owm IS
PORT (
    [...]
);
END COMPONENT;

现在已经有一段时间了,不再需要组件声明了。您可以删除它并更改实例化:

one_wire_control : entity work.sockit_owm
PORT MAP(
    [...]
WHEN idle =>
    [...]
    ELSE
        state <= idle;
    END IF; 

不是必需的。如果您不更改state,则会保留idle

WHEN wait_presence_pulse =>
    IF (timer_s = 0) THEN
        IF (read_data_s(0) = '0') THEN
            [...]
        ELSIF (read_data_s(0) = '1') THEN
            [...]
        ELSE
            state <= wait_presence_pulse;
        END IF;

read_data_s(0)包含'0'和'1'。你还期待其他任何价值吗?这只能在模拟中发生,而不是在实施中发生。因此,最后一个else语句中的代码无法访问。

[...]
    timer_s               <= delay_65us_c;
    state                 <= wait_65us;
[...]
WHEN wait_65us =>
    IF (timer_s = 0) THEN
        [...]
    ELSE 
        timer_s <= timer_s - 1;   
    END IF;

假设延迟为65 us持续10个时钟周期。将分隔符设置为1,delay_65us_c = 10。因此,在t = 0时,timer_s设置为10.在t = 1时,statewait_65us现在 - timer_s设置为9.依此类推:at t = 10,timer_s设置为0 ...但state 仍然 wait_65us。因此,在t = 11时,timer_s被检测到0,state被更改为前一个。它将在t = 12时进入。 因此,您将获得12个时钟周期延迟,而不是10个时钟周期延迟。

这是一个问题吗?如果是,您应该重新考虑您的代码。

SIGNAL read_enable_s, write_enable_s, reset_s, owr_e_s : std_logic := '0';
[... not used anywhere else... ]
one_wire_control : sockit_owm
PORT MAP(
    [...]
    rst     => reset_s,

你确定这是对的吗?许多组件在正常运行之前需要正确重置。