提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
提示:这里可以添加本文要记录的大概内容: 例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。
提示:以下是本篇文章正文内容,下面案例可供参考
一、实验目的与意义
1.了解spi通信原理 2.掌握集成开发环境QuartusII集成开发环境使用方法
二、实验设备及平台
1.iCore 双核心板 2.Blaster 仿真器 3.Micro USB线缆 4.QuartusII 开发平台 5.计算机
三、实验原理
通过FPGA建立的spi模块, 对外提供接口, 与STM32的SPI相连接, Commix 串口精灵与STM32通过串口连接, 实现三者之间的通讯.
FPGA代码如下:
`timescale 1 ns / 1 ps
module SPI(
input CLK_25M,
input spi_clk,
input spi_mosi,
input spi_cs,
output reg spi_miso,
output FPGA_LEDR,
output FPGA_LEDG,
output FPGA_LEDB
);
reg [5:0]rst_cnt = 6'd0;
reg rst_n = 1'd0;
always @(posedge CLK_25M)
if(rst_cnt == 6'd50)
begin
rst_n <= 1'd1;
rst_cnt <= rst_cnt;
end
else rst_cnt <= rst_cnt + 1'd1;
parameter ledr = {8'd108,8'd101,8'd100,8'd114},
ledg = {8'd108,8'd101,8'd100,8'd103},
ledb = {8'd108,8'd101,8'd100,8'd98},
hello = {8'd104,8'd101,8'd108,8'd108,8'd111};
reg [3:0]i;
reg [7:0]data_in;
reg [39:0]temp_data,data;
always@(posedge spi_clk or negedge rst_n)
if(!rst_n)
begin
i <= 4'd0;
temp_data <= 40'd0;
data <= 40'd0;
data_in <= 8'd0;
end
else case(i)
4'd0:
begin
i <= i + 1'd1;
data_in <= {data_in[6:0],spi_mosi};
temp_data <= {temp_data[31:0],data_in};
if(data_in == 8'd13)
begin
data <= temp_data;
end
else
begin
data <= data;
end
end
4'd1,4'd2,4'd3,4'd4,4'd5,4'd6:
begin
i <= i + 1'd1;
data_in <= {data_in[6:0],spi_mosi};
end
4'd7:begin
i <= 4'd0;
data_in <= {data_in[6:0],spi_mosi};
end
default: i <= 4'd0;
endcase
reg [2:0]led;
always@(posedge CLK_25M or negedge rst_n)
if(!rst_n)
begin
led <= 3'b111;
end
else if (data == ledr)
led <= 3'b011;
else if (data == ledg)
led <= 3'b101;
else if (data == ledb)
led <= 3'b110;
assign {FPGA_LEDR,FPGA_LEDG,FPGA_LEDB} = led;
reg spi_clk_r;
always@(posedge CLK_25M or negedge rst_n)
if(!rst_n)
begin
spi_clk_r <= 1'd1;
end
else
spi_clk_r <= spi_clk;
reg [39:0]data_out;
reg [5:0]j;
always@(negedge spi_clk_r or negedge rst_n)
if(!rst_n)
begin
data_out <= hello;
j <= 6'd0;
end
else case(j)
6'd0:
begin
{spi_miso,data_out[39:1]} <= data_out;
j <= j + 1'd1;
end
6'd39:
begin
{spi_miso,data_out[39:1]} <= data_out;
data_out <= hello;
j <= 6'd0;
end
default:
begin
{spi_miso,data_out[39:1]} <= data_out;
j <= j + 1'd1;
end
endcase
endmodule
ARM主程序代码如下:
int main(void)
{
led.initialize();
spi.initialize();
usart4.initialize(9600);
int i,n,k;
char buffer[20];
char spibuffer[20];
usart4.printf("hello! I am iCore3!\r\n");
while(1){
if(usart4.receive_ok_flag){
usart4.receive_ok_flag = 0;
for(i = 0;i < 20;i++){
buffer[i] = tolower(usart4.receive_buffer[i]);
}
n = strlen(buffer);
if(memcmp(buffer,"ledr",strlen("ledr")) == 0){
LED_RED_ON;
LED_GREEN_OFF;
LED_BLUE_OFF;
}
if(memcmp(buffer,"ledg",strlen("ledg")) == 0){
LED_GREEN_ON;
LED_RED_OFF;
LED_BLUE_OFF;
}
if(memcmp(buffer,"ledb",strlen("ledb")) == 0){
LED_BLUE_ON;
LED_GREEN_OFF;
LED_RED_OFF;
}
spi.write_nbyte(n,buffer);
for (n = 0;n < 100; n++);
SPI_CS_OFF;
for(k = 0; k < 5; k++){
spibuffer[k] = spi.write_byte(0x00);
}
SPI_CS_ON;
usart4.printf(spibuffer);
}
}
}
四、实验步骤
- 把仿真器与iCore3的SWD调试口连接
- 把 USB-Blaster 与 iCore3 的JTAG 调试口相连
- 将跳线帽插在USB UART
- 为iCore3 供电
- 打开串口精灵, 找到对应的对口打开
- 下载FPGA程序到板子
- 下载ARM程序到板子
- 输入串口命令, 观察实验现象
五、实验现象
在Commix 上发送命令后, 对应的ARM 和 FPGA 的LED 灯亮, 同时接收显示: hello
串口发送命令格式:
LEDR\CR\LF ARM_红 FPGA_红
LEDG\CR\LF ARM_绿 FPGA_绿
LEDB\CR\LF ARM_蓝 FPGA_蓝
六、总结
1.串口精灵如何与开发板相连接? 未完待续
|