` `UART是常用模塊,無論是調試還是用于使用過程中的數據傳輸,都很重要。該模塊使用FPGA實現時候,一般接收數據比較容易實現,但是發送數據容易出錯,原因在于時鐘不精確,導致數據接收錯誤。
` `對于50MHz時鐘的晶振,一般使用9600的波特率,因為我們需要得到9600*16Hz的時鐘,使用PLL可以得到精確的這個值。
## UART接收模塊
```
`timescale 1ns/1ns
module uart_rd
(
input i_rst_n,
input i_clk,
output [7 : 0] o_rx_data, //UART數據接收接收到的數據
output o_uart_rx_busy, //UART數據接收模塊接收數據忙
output o_uart_rx_error, //UART數據接收幀出錯
input i_uart_rx //UART串行數據輸入
);
reg r_uart_rx_buf;
reg r_uart_rx_falling;
reg [3 : 0] r_sample_cnt; //采樣計數器
reg [1 : 0] cstate, nstate;
parameter [1 : 0] idle = 2'b00;
parameter [1 : 0] receive_data = 2'b01;
parameter [1 : 0] receive_done = 2'b10;
reg [3 : 0] r_shift_cnt;
reg [9 : 0] r_shift;
reg [7 : 0] r_rx_data;
reg r_uart_rx_error;
assign o_rx_data = r_rx_data;
assign o_uart_rx_busy = (cstate == idle)? 1'b0 : 1'b1;
assign o_uart_rx_error = r_uart_rx_error;
//*********************************PROCESS**************************************
// FUNCTION :捕獲UART數據的下降沿
//******************************************************************************
always @(posedge i_clk, negedge i_rst_n)
begin
if(1'b0 == i_rst_n)
begin
r_uart_rx_buf <= 1'b0;
r_uart_rx_falling <= 1'b0;
end
else
begin
r_uart_rx_buf <= i_uart_rx;
r_uart_rx_falling <= ~i_uart_rx & (r_uart_rx_buf);
end
end
//*********************************PROCESS**************************************
// FUNCTION :UART下個狀態到現狀態的轉化
//******************************************************************************
always @(posedge i_clk, negedge i_rst_n)
begin
if(1'b0 == i_rst_n)
cstate <= idle;
else
cstate <= nstate;
end
//*********************************PROCESS**************************************
// FUNCTION :UART下個狀態的轉化
//******************************************************************************
always @(*)
begin
case(cstate)
idle :
if(1'b1 == r_uart_rx_falling)
nstate = receive_data;
else
nstate = idle;
receive_data :
if(4'd10 == r_shift_cnt)
nstate = receive_done;
else
nstate = receive_data;
receive_done :
nstate = idle;
default :
nstate = idle;
endcase
end
//*********************************PROCESS**************************************
// FUNCTION :UART在現狀態的操作
//******************************************************************************
always @(posedge i_clk, negedge i_rst_n)
begin
if(1'b0 == i_rst_n)
begin
r_shift <= 10'b1111111111;
r_shift_cnt <= 4'd0;
r_sample_cnt <= 4'd0;
r_rx_data <= 8'd0;
r_uart_rx_error <= 1'b0;
end
else
begin
if(receive_data == cstate)
if(4'd5 == r_sample_cnt)
begin
r_shift_cnt <= r_shift_cnt + 4'd1;
r_shift <= {i_uart_rx, r_shift[9 : 1]};
r_sample_cnt <= r_sample_cnt + 4'd1;
end
else
r_sample_cnt <= r_sample_cnt + 4'd1;
else if(receive_done == cstate)
begin
r_shift_cnt <= 4'd0;
r_shift <= 10'b1111111111;
r_rx_data <= r_shift[8 : 1];
if(1'b0 == r_shift[9])
r_uart_rx_error <= 1'b1;
else
r_uart_rx_error <= 1'b0;
r_shift_cnt <= 4'd0;
r_sample_cnt <= 4'd0;
end
else
begin
r_shift <= 10'b1111111111;
r_shift_cnt <= 4'd0;
r_sample_cnt <= 4'd0;
end
end
end
endmodule
```
## UART發送模塊
```
`timescale 1ns/1ns
module uart_tx
(
input i_rst_n,
input i_clk,
input i_tx_order, //UART數據發送指令
input [7 : 0] i_tx_data, //UART發送的數據
output o_uart_tx_busy, //UART發送模塊忙
output o_uart_tx //UART數據輸出
);
reg r_tx_order_buf;
reg r_tx_order_rising;
// reg [4 : 0] r_div_cnt; //時鐘分頻計數器
// reg r_div_clk; //分頻時鐘信號
reg [1 : 0] cstate, nstate;
parameter [1 : 0] idle = 2'b00;
parameter [1 : 0] load_data = 2'b01;
parameter [1 : 0] shift_data = 2'b10;
reg [3 : 0] r_shift_cnt;
reg [9 : 0] r_shift;
reg [3 : 0] r_hold_cnt;
assign o_uart_tx = r_shift[0];
assign o_uart_tx_busy = (cstate == idle)? 1'b0 : 1'b1;
////*********************************PROCESS**************************************
//// FUNCTION :產生分頻時鐘
////******************************************************************************
//
// always @(posedge i_clk, negedge i_rst_n)
// begin
// if(1'b0 == i_rst_n)
// begin
// r_div_cnt <= 4'd0;
// r_div_clk <= 1'b0;
// end
// else
// begin
// r_div_cnt <= r_div_cnt + 4'd1;
// r_div_clk <= r_div_cnt[3];
// end
// end
//*********************************PROCESS**************************************
// FUNCTION :捕獲UART發送指令的上升沿
//******************************************************************************
always @(posedge i_clk, negedge i_rst_n)
begin
if(1'b0 == i_rst_n)
begin
r_tx_order_buf <= 1'b0;
r_tx_order_rising <= 1'b0;
end
else
begin
r_tx_order_buf <= i_tx_order;
r_tx_order_rising <= i_tx_order & (~r_tx_order_buf);
end
end
//*********************************PROCESS**************************************
// FUNCTION :UART下個狀態轉化到現狀態
//******************************************************************************
always @(posedge i_clk, negedge i_rst_n)
begin
if(1'b0 == i_rst_n)
cstate <= idle;
else
cstate <= nstate;
end
//*********************************PROCESS**************************************
// FUNCTION :UART下個狀態轉化進程
//******************************************************************************
always @(*)
begin
case(cstate)
idle :
if(1'b1 == r_tx_order_rising)
nstate = shift_data;
else
nstate = idle;
shift_data :
if(4'd9 == r_shift_cnt)
nstate = idle;
else
nstate = shift_data;
default :
nstate = idle;
endcase
end
//*********************************PROCESS**************************************
// FUNCTION :UART在各個現狀態下的操作
//******************************************************************************
always @(posedge i_clk, negedge i_rst_n)
begin
if(1'b0 == i_rst_n)
begin
r_shift <= 10'b1111111111;
r_shift_cnt <= 4'd0;
r_hold_cnt <= 4'd0;
end
else
begin
if(idle == cstate && 1'b1 == r_tx_order_rising)
begin
r_shift_cnt <= 4'd0;
r_shift <= {1'b1, i_tx_data, 1'b0};
r_hold_cnt <= r_hold_cnt + 4'd1;
end
else if(shift_data == cstate)
begin
r_hold_cnt <= r_hold_cnt + 4'd1;
if(4'd15 == r_hold_cnt)
begin
r_shift_cnt <= r_shift_cnt + 4'd1;
r_shift <= {1'b1, r_shift[9 : 1]};
end
end
else
begin
r_shift <= 10'b1111111111;
r_shift_cnt <= 4'd0;
end
end
end
endmodule
```
## 時鐘
` `時鐘使用IP核實現。
## 測試模塊
```
module top(
input wire clk50,
//rst,//
output reg led, //用于指示
input wire rxd,
output wire txd
);
wire rx_busy;//16倍波特率
//reset
//*********************************PROCESS**************************************
// 復位模塊
//******************************************************************************
reg rst_n ;
reg [9:0]delay_cnt;
always@(posedge clk50)
begin
if(delay_cnt>=10'd1000)begin
delay_cnt <= delay_cnt;
rst_n <= 1'b1;
end
else begin
rst_n <= 1'b0;
delay_cnt <= delay_cnt + 1'b1;
end
end
/**********************************************************
//串口接口
**********************************************************/
wire [7:0]data_temp;
//wire rx_busy;
uart_interface uart_inst_int
(
.clk50 (clk50) ,
.rst_n (rst_n) ,
.rxd (rxd) ,
.txd (txd) ,
.tx_data(data_temp) ,
.rx_data(data_temp) ,
.rx_busy(rx_busy) ,
.tx_start(!rx_busy),
.tx_busy()
);
//指示燈
//assign txd = led;
reg [31:0]cnt;
//always@(posedge clk50)
//begin
// if(cnt >= 32'd25000000 - 1)
// begin
// cnt <= 0;
// led <=~led;
// end
// else begin
// cnt <= cnt + 1'b1 ;
// end
//end
always@(posedge rx_busy)
led<=~led;
endmodule
```
- 序
- 第1章 Linux下開發FPGA
- 1.1 Linux下安裝diamond
- 1.2 使用輕量級linux仿真工具iverilog
- 1.3 使用linux shell來讀寫串口
- 1.4 嵌入式上的linux
- 設備數教程
- linux C 標準庫文檔
- linux 網絡編程
- 開機啟動流程
- 1.5 linux上實現與樹莓派,FPGA等通信的串口腳本
- 第2章 Intel FPGA的使用
- 2.1 特別注意
- 2.2 高級應用開發流程
- 2.2.1 生成二進制bit流rbf
- 2.2.2 制作Preloader Image
- 2.2.2.1 生成BSP文件
- 2.2.2.2 編譯preloader和uboot
- 2.2.2.3 更新SD的preloader和uboot
- 2.3 HPS使用
- 2.3.1 通過JTAG下載代碼
- 2.3.2 HPS軟件部分開發
- 2.3 quartus中IP核的使用
- 2.3.1 Intel中RS232串口IP的使用
- 2.4 一些問題的解決方法
- 2.4.1 關于引腳的復用的綜合出錯
- 第3章 關于C/C++的一些語法
- 3.1 C中數組作為形參不傳長度
- 3.2 匯編中JUMP和CALL的區別
- 3.3 c++中map的使用
- 3.4 鏈表的一些應用
- 3.5 vector的使用
- 3.6 使用C實現一個簡單的FIFO
- 3.6.1 循環隊列
- 3.7 C語言不定長參數
- 3.8 AD采樣計算同頻信號的相位差
- 3.9 使用C實現棧
- 3.10 增量式PID
- 第4章 Xilinx的FPGA使用
- 4.1 Alinx使用中的一些問題及解決方法
- 4.1.1 在Genarate Bitstream時提示沒有name.tcl
- 4.1.2 利用verilog求位寬
- 4.1.3 vivado中AXI寫DDR說明
- 4.1.4 zynq中AXI GPIO中斷問題
- 4.1.5 關于時序約束
- 4.1.6 zynq的PS端利用串口接收電腦的數據
- 4.1.7 SDK啟動出錯的解決方法
- 4.1.8 讓工具綜合是不優化某一模塊的方法
- 4.1.9 固化程序(雙核)
- 4.1.10 分配引腳時的問題
- 4.1.11 vivado仿真時相對文件路徑的問題
- 4.2 GCC使用Attribute分配空間給變量
- 4.3 關于Zynq的DDR寫入byte和word的方法
- 4.4 常用模塊
- 4.4.1 I2S接收串轉并
- 4.5 時鐘約束
- 4.5.1 時鐘約束
- 4.6 VIVADO使用
- 4.6.1 使用vivado進行仿真
- 4.7 關于PicoBlaze軟核的使用
- 4.8 vivado一些IP的使用
- 4.8.1 float-point浮點單元的使用
- 4.10 zynq的雙核中斷
- 第5章 FPGA的那些好用的工具
- 5.1 iverilog
- 5.2 Arduino串口繪圖器工具
- 5.3 LabVIEW
- 5.4 FPGA開發實用小工具
- 5.5 Linux下繪制時序圖軟件
- 5.6 verilog和VHDL相互轉換工具
- 5.7 linux下搭建輕量易用的verilog仿真環境
- 5.8 VCS仿真verilog并查看波形
- 5.9 Verilog開源的綜合工具-Yosys
- 5.10 sublim text3編輯器配置verilog編輯環境
- 5.11 在線工具
- 真值表 -> 邏輯表達式
- 5.12 Modelsim使用命令仿真
- 5.13 使用TCL實現的個人仿真腳本
- 5.14 在cygwin下使用命令行下載arduino代碼到開發板
- 5.15 STM32開發
- 5.15.1 安裝Atollic TrueSTUDIO for STM32
- 5.15.2 LED閃爍吧
- 5.15.3 模擬U盤
- 第6章 底層實現
- 6.1 硬件實現加法的流程
- 6.2 硬件實現乘法器
- 6.3 UART實現
- 6.3.1 通用串口發送模塊
- 6.4 二進制數轉BCD碼
- 6.5 基本開源資源
- 6.5.1 深度資源
- 6.5.2 FreeCore資源集合
- 第7章 常用模塊
- 7.1 溫濕度傳感器DHT11的verilog驅動
- 7.2 DAC7631驅動(verilog)
- 7.3 按鍵消抖
- 7.4 小腳丫數碼管顯示
- 7.5 verilog實現任意人數表決器
- 7.6 基本模塊head.v
- 7.7 四相八拍步進電機驅動
- 7.8 單片機部分
- 7.8.1 I2C OLED驅動
- 第8章 verilog 掃盲區
- 8.1 時序電路中數據的讀寫
- 8.2 從RTL角度來看verilog中=和<=的區別
- 8.3 case和casez的區別
- 8.4 關于參數的傳遞與讀取(paramter)
- 8.5 關于符號優先級
- 第9章 verilog中的一些語法使用
- 9.1 可綜合的repeat
- 第10章 system verilog
- 10.1 簡介
- 10.2 推薦demo學習網址
- 10.3 VCS在linux上環境的搭建
- 10.4 deepin15.11(linux)下搭建system verilog的vcs仿真環境
- 10.5 linux上使用vcs寫的腳本仿真管理
- 10.6 system verilog基本語法
- 10.6.1 數據類型
- 10.6.2 枚舉與字符串
- 第11章 tcl/tk的使用
- 11.1 使用Tcl/Tk
- 11.2 tcl基本語法教程
- 11.3 Tk的基本語法
- 11.3.1 建立按鈕
- 11.3.2 復選框
- 11.3.3 單選框
- 11.3.4 標簽
- 11.3.5 建立信息
- 11.3.6 建立輸入框
- 11.3.7 旋轉框
- 11.3.8 框架
- 11.3.9 標簽框架
- 11.3.10 將窗口小部件分配到框架/標簽框架
- 11.3.11 建立新的上層窗口
- 11.3.12 建立菜單
- 11.3.13 上層窗口建立菜單
- 11.3.14 建立滾動條
- 11.4 窗口管理器
- 11.5 一些學習的腳本
- 11.6 一些常用的操作語法實現
- 11.6.1 刪除同一后綴的文件
- 11.7 在Lattice的Diamond中使用tcl
- 第12章 FPGA的重要知識
- 12.1 面積與速度的平衡與互換
- 12.2 硬件原則
- 12.3 系統原則
- 12.4 同步設計原則
- 12.5 乒乓操作
- 12.6 串并轉換設計技巧
- 12.7 流水線操作設計思想
- 12.8 數據接口的同步方法
- 第13章 小項目
- 13.1 數字濾波器
- 13.2 FIFO
- 13.3 一個精簡的CPU( mini-mcu )
- 13.3.1 基本功能實現
- 13.3.2 中斷添加
- 13.3.3 使用中斷實現流水燈(實際硬件驗證)
- 13.3.4 綜合一點的應用示例
- 13.4.5 使用flex開發匯編編譯器
- 13.4.5 linux--Flex and Bison
- 13.4 有符號數轉單精度浮點數
- 13.5 串口調試FPGA模板