歡迎您光臨本站 登入註冊首頁

基於FPGA 的SPI 自動發送模塊設計

admin @ 2014-03-26 , reply:0

概述

一、摘要:   SPI介面應用十分廣泛,在很多情況下,人們會用軟體模擬的方法來產生SPI時序或是採用帶SPI功能模塊的MCU。但隨著可編程邏輯技術的發展,人們往往需要自己……

一、摘要
    SPI 介面應用十分廣泛,在很多情況下,人們會用軟體模擬的方法來產生SPI 時序或是採用帶SPI 功能模塊的MCU。但隨著可編程邏輯技術的發展,人們往往需要自己設計簡單的SPI 發送模塊。本文介紹一種基於FPGA 的將并行數據以SPI 串列方式自動發送出去的方法。

二、關鍵字
    VHDL、FPGA、SPI、串列數據輸出選擇模塊、移位脈衝產生模塊、SPI 時鐘採集信號和無相移的SPI 基準時鐘產生模塊、SPI 時鐘輸出選擇模塊、8bit SPI 時鐘採集生成模塊、16bit SPI 時鐘採集生成模塊、24bit SPI 時鐘採集生成模塊、8bit 數據移位模塊、16bit 數據移位模塊、24bit 數據移位模塊。

三、功能框圖
SPI_MODES 為輸入模式選擇埠:
--"01"is 8bit 傳輸模式
--"10"is 16bit 傳輸模式
--"11"is 24bit 傳輸模式
CLKS 為整個模塊的基準時鐘
DBINOUTS 為并行數據輸入埠:
--8bit 模式為DBINOUTS(7 downto 0)
--16bit 模式為DBINOUTS(15 downto 0)
--24bit 模式為DBINOUTS(23 downto 0)
SPI_WR 為啟動SPI 傳輸的信號
 
整個功能模塊可工作在 8bit、16bit、24bit SPI 猝發傳輸狀態。對其進行軟體操作的步驟相當簡單:
--此模塊軟體操作流程如下
--1、SPI_MODES="xx" 設定串口操作模式
--2、DBINOUTS="xxxxxxxxxxxxxxxxxxxxxxxx" 輸入要發射的數據
--3、SPI_WR='0'
--4、SPI_WR='1'
--5、SPI_WR='0'
--8bit 模式延時2*8*4*CLKS
--16bit 模式延時2*16*4*CLKS
--24bit 模式延時2*24*4*CLKS
--6、DBINOUTS="xxxxxxxxxxxxxxxxxxxxxxxx" 輸入下一個要發射的數據

四、VHDL 描述解讀
--以下描述的是一個SPI 自動發射模塊
--在很多情況下,人們會用軟體模擬的方法來產生SPI 時序
--這裡採用硬體的方法,即使軟體操作更為簡單,有提高了傳輸的速度
--------------------------------------------------------------
--此模塊軟體操作流程如下
--1、SPI_MODES="xx" 設定串口操作模式
--2、DBINOUTS="xxxxxxxxxxxxxxxxxxxxxxxx" 輸入要發射的數據
--3、SPI_WR='0'
--4、SPI_WR='1'
--5、SPI_WR='0'
--8bit 模式延時2*8*4*CLKS
--16bit 模式延時2*16*4*CLKS
--24bit 模式延時2*24*4*CLKS
--6、DBINOUTS="xxxxxxxxxxxxxxxxxxxxxxxx" 輸入下一個要發射的數據
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity SPI_interface is
port(CLKS :in std_logic; --基準時鐘
LCD_SCLS :out std_logic;--SPI 發射時鐘,上升沿有效
LCD_SDIS :out std_logic;--SPI 數據串列輸出口
SPI_MODES :in std_logic_vector(1 downto 0);
--串口操作模式選擇
--"01"is 8bit trans mode
--"10"is 16bit trans mode
--"11"is 24bit trans mode
SPI_WR :in std_logic; --啟動串口發送信號
DBINOUTS :in std_logic_vector(23 downto 0));
--背發送數據的并行輸入口
--8bit mode use DBINOUTS(7 downto 0)
--16bit mode use DBINOUTS(15 downto 0)
--24bit mode use DBINOUTS(23 downto 0)
end;
architecture SPI_interface_behav of SPI_interface is
signal DB8BIT_reg :std_logic_vector(7 downto 0); --8bit 數據移位寄存器
signal DB16BIT_reg :std_logic_vector(15 downto 0);--16bit 數據移位寄存器
signal DB24BIT_reg :std_logic_vector(23 downto 0);--24bit 數據移位寄存器
signal counter4 :std_logic_vector(3 downto 0); --移位脈衝產生計數器
signal counter4s :std_logic_vector(1 downto 0); --SPI 時鐘生成計數器
signal counter8 :std_logic_vector(4 downto 0); --8bit SPI 時鐘控制計數器
signal counter16 :std_logic_vector(5 downto 0); --16bit SPI 時鐘控制計數器
signal counter24 :std_logic_vector(5 downto 0); --24bit SPI 時鐘控制計數器
signal shift :std_logic;--移位時鐘脈衝
signal LCD_SCLSS :std_logic;--SPI 時鐘採集信號
signal LCD_SCLSSS :std_logic;--無相移的SPI 基準時鐘
signal LCD_SCLSS8 :std_logic;--8bit SPI 時鐘信號
signal LCD_SCLSS16 :std_logic;--16bit SPI 時鐘信號
signal LCD_SCLSS24 :std_logic;--24bit SPI 時鐘信號
signal LCD_SDIS_8BIT :std_logic;--8bit SPI 數據信號
signal LCD_SDIS_16BIT :std_logic;--16bit SPI 數據信號
signal LCD_SDIS_24BIT :std_logic;--24bit SPI 數據信號
begin
--串列數據輸出選擇模塊
u1:process(LCD_SDIS_8BIT,LCD_SDIS_16BIT,LCD_SDIS_24BIT,SPI_MODES)
begin
if SPI_MODES="01" then --選擇8bit 串列數據輸出
LCD_SDIS<=LCD_SDIS_8BIT;
elsif SPI_MODES="10" then --選擇16bit 串列數據輸出
LCD_SDIS<=LCD_SDIS_16BIT;
elsif SPI_MODES="11" then --選擇24bit 串列數據輸出
LCD_SDIS<=LCD_SDIS_24BIT;
else LCD_SDIS<='1';
end if;
end process;
--移位脈衝產生模塊
u2:process(CLKS)
begin
if CLKS='1' and CLKS'event then
if counter4="0011" then
counter4<="0000";
shift <='1';
else counter4<=counter4+1;
shift <='0';
end if;
end if;
end process;
--SPI 時鐘採集信號和無相移的SPI 基準時鐘產生模塊
u3:process(CLKS)
begin
if CLKS='1' and CLKS'event then
if counter4s<"11" then
counter4s<=counter4s+1;
else counter4s<="00";
end if;
end if;
LCD_SCLSS<=counter4s(0); --SPI 時鐘採集信號
LCD_SCLSSS<=counter4s(1); --無相移的SPI 基準時鐘
end process;
--SPI 時鐘輸出選擇模塊
u4:process(LCD_SCLSS8,LCD_SCLSS16,LCD_SCLSS24,SPI_MODES)
begin
if SPI_MODES="01" then
LCD_SCLS<=LCD_SCLSS8; --選擇8bit SPI 時鐘模式
elsif SPI_MODES="10" then
LCD_SCLS<=LCD_SCLSS16; --選擇16bit SPI 時鐘模式
elsif SPI_MODES="11" then
LCD_SCLS<=LCD_SCLSS24; --選擇24bit SPI 時鐘模式
else LCD_SCLS<='1';
end if;
end process;
--8bit SPI 時鐘採集生成模塊
counter8_u:process(LCD_SCLSS)
begin
if SPI_WR='1' then
counter8<="10001";
elsif LCD_SCLSS='1' and LCD_SCLSS'event then
if counter8>0 then
counter8<=counter8-1;
LCD_SCLSS8<=LCD_SCLSSS;
end if;
end if;
end process;
--16bit SPI 時鐘採集生成模塊
counter16_u:process(LCD_SCLSS)
begin
if SPI_WR='1' then
counter16<="100001";
elsif LCD_SCLSS='1' and LCD_SCLSS'event then
if counter16>0 then
counter16<=counter16-1;
LCD_SCLSS16<=LCD_SCLSSS;
end if;
end if;
end process;
--24bit SPI 時鐘採集生成模塊
counter24_u:process(LCD_SCLSS)
begin
if SPI_WR='1' then
counter24<="110011";
elsif LCD_SCLSS='1' and LCD_SCLSS'event then
if counter24>0 then
counter24<=counter24-1;
if (counter24="000000")or(counter24="000001")or
(counter24="110011")or(counter24="000010")then
LCD_SCLSS24<='0';
else
LCD_SCLSS24<=LCD_SCLSSS;
end if;
end if;
end if;
end process;
--8bit 數據移位模塊
DB8BIT_U:process(shift,SPI_WR,DBINOUTS)
begin
if SPI_WR='1' then
DB8BIT_reg<=DBINOUTS(7 downto 0);
else
if shift='1' and shift'event then
LCD_SDIS_8BIT<=DB8BIT_reg(0);
DB8BIT_reg(6 downto 0)<=DB8BIT_reg(7 downto 1);
end if;
end if;
end process;
--16bit 數據移位模塊
DB16BIT_U:process(shift,SPI_WR,DBINOUTS)
begin
if SPI_WR='1' then
DB16BIT_reg(15 downto 0)<=DBINOUTS(15 downto 0);
else
if shift='1' and shift'event then
LCD_SDIS_16BIT<=DB16BIT_reg(0);
DB16BIT_reg(14 downto 0)<=DB16BIT_reg(15 downto 1);
end if;
end if;
end process;
--24bit 數據移位模塊
DB24BIT_U:process(shift,SPI_WR,DBINOUTS)
begin
if SPI_WR='1' then
DB24BIT_reg(23 downto 0)<=DBINOUTS(23 downto 0);
else
if shift='1' and shift'event then
LCD_SDIS_24BIT<=DB24BIT_reg(0);
DB24BIT_reg(22 downto 0)<=DB24BIT_reg(23 downto 1);
end if;
end if;
end process;
end;

五、模擬波形圖
 
 
 
 

六、編譯后資源佔用情況
 

七、結束語
    本文旨在給學習可編程技術的人們提供一個參考,起到拋磚引玉的作用。望閱讀過此文的讀者提供更好的方法,與所有的學習者共享,共勉!

八、參考資料
《用VHDL 語言在CPLD 上實現串列通信》


[admin via 研發互助社區 ] 基於FPGA 的SPI 自動發送模塊設計已經有8054次圍觀

http://cocdig.com/docs/show-post-42964.html