实验现象:
1、先烧写ARM程序,然后烧写FPGA程序。
2、打开串口精灵,通过串口精灵给ARM发送数据从而给FPGA发送数据 ,会接收到字符GINGKO。
3、通过串口精灵发送命令可以控制ARM·LED和FPGA·LED。
命令格式
LEDR\CR\LF ARM·LED、FPGA·LED亮
LEDG\CR\LF ARM·LED、FPGA·LED亮
LEDB\CR\LF ARM·LED、FPGA·LED亮
核心代码:
int main(void)
{ /* USER CODE BEGIN 1 */
int i;
char buffer[];
char i2c_buffer[];
/* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */
SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART6_UART_Init(); /* USER CODE BEGIN 2 */
i2c.initialize();
usart6.initialize();
usart6.printf("Hello,I am iCore4!\r\n");
LED_GREEN_ON; /* USER CODE END 2 */ /* Infinite loop */
/* USER CODE BEGIN WHILE */
while ()
{
/* USER CODE END WHILE */ /* USER CODE BEGIN 3 */
if(usart6.receive_ok_flag == ){
usart6.receive_ok_flag = ;
for(i = ;i < ;i ++){
buffer[i] = tolower(usart6.receive_buffer[i]);
}
if(memcmp(buffer,"ledr",strlen("ledr")) == ){ //±È½Ï½ÓÊÕÊý¾Ý
LED_RED_ON;
LED_GREEN_OFF;
LED_BLUE_OFF;
i2c.write_nbyte(0x03,0X8F,buffer,strlen(buffer)); //i2c×ÜÏß·¢ËÍÊý¾Ý
}
if(memcmp(buffer,"ledg",strlen("ledg")) == ){
LED_RED_OFF;
LED_GREEN_ON;
LED_BLUE_OFF;
i2c.write_nbyte(0x03,0X8F,buffer,strlen(buffer));
}
if(memcmp(buffer,"ledb",strlen("ledb")) == ){
LED_RED_OFF;
LED_GREEN_OFF;
LED_BLUE_ON;
i2c.write_nbyte(0x03,0X8F,buffer,strlen(buffer));
}
for(i = ; i < ; i ++);
i2c.read_nbyte(0x02,0x0F,i2c_buffer,0x06); //i2c×ÜÏß½ÓÊÕÊý¾Ý
usart6.printf(i2c_buffer);
}
}
/* USER CODE END 3 */ }
module i2c_ctrl(
input clk_25m,
input rst_n,
input scl,
inout sda,
output led_red,
output led_green,
output led_blue
); //---------------------------parameter--------------------------//
parameter ledr = {'d108,8'd101,'d100,8'd114,'d13},
ledg = {'d108,8'd101,'d100,8'd103,'d13},
ledb = {'d108,8'd101,'d100,8'd98,'d13},
GINGKO = {'d71,8'd73,'d78,8'd71,'d75,8'd79},
wide = 'd40; //-----------------------------address--------------------------//
reg[:]m;
reg[:]device_address,word_address; //接收地址先传输高位 always@(posedge scl or negedge rst_n)
if(!rst_n)
begin
m <= 'd0;
device_address <= 'd0;
word_address <= 'd0;
tx_en <= 'd0;
tx_en <= 'd0;
end
else case(m)
'd0,5'd1,'d2,5'd3,'d4,5'd5,'d6,5'd7: //接收设备地址
begin
if(!rx_en || !tx_en)
begin
m <= m + 'd1;
device_address <= {device_address[:],sda};
end
else m <= 'd0;
end
'd8:
begin
if(device_address == 'd3 || device_address == 8'd2)
begin
m <= 'd9;
device_address <= 'd0;
end
else if(rx_ack || tx_ack)
begin
m <= 'd18;
device_address <= 'd0;
end
else
begin
m <= 'd0;
end
end
'd9,5'd10,'d11,5'd12,'d13,5'd14,'d15,5'd16: //接收寄存器地址,以寄存器地址区分读写操作
begin
m <= m + 'd1;
word_address <= {word_address[:],sda};
end
'd17:
begin
if(word_address == 'h8f) //使能接收数据
begin
rx_en <= 'd1;
word_address <= 'd0;
end
else if(word_address == 'h0f) //使能发送数据
begin
tx_en <= 'd1;
word_address <= 'd0;
end
else if(rx_ack)
begin
rx_en <= 'd0;
m <= 'd0;
end
else if(tx_ack)
begin
tx_en <= 'd0;
m <= 'd18;
end
else m <= 'd17;
end
'd18:begin
m <= 'd0;
end
endcase //-------------------------------rx---------------------------//
/*接收数据*/
reg[:]data_in;
reg[:]data;
reg[:]i;
reg tx_en,rx_en;
reg rx_ack; always@(posedge scl or negedge rst_n)
if(!rst_n)
begin
i <= 'd0;
data <= 'd0;
data_in <= 'd0;
rx_ack <= 'd0;
end
else if(rx_en)
begin
case(i)
'd0,5'd1,'d2,5'd3,'d4,5'd5,'d6,5'd7: //移位完成数据接收
begin
i <= i + 'd1;
rx_ack <= 'd0;
data_in <= {data_in[:],sda};
end
'd8:
begin
if(data_in[:] == 'h0d)
begin
i <= 'd9;
rx_ack <= 'd1;
data <= data_in;
end
else
begin
i <= 'd0;
end
end
'd9:begin
rx_ack <= 'd0;
i <= 'd0;
end
default:i <= 'd0;
endcase
end //-----------------------------data-------------------------//
/*比较接收数据*/
reg [:]led;
always@(posedge clk_25m or negedge rst_n)
if(!rst_n)
begin
led <= 'b101;
end
else if (data[wide-:] == ledr)
led <= 'b011;
else if (data[wide-:] == ledg)
led <= 'b101;
else if (data[wide-:] == ledb)
led <= 'b110; assign {led_red,led_green,led_blue} = led; //----------------------------tx--------------------------// //发送数据 先传输高位
reg[:]data_out;
reg[:]j;
reg send_data;
reg tx_ack;
reg[:]tx_cnt;
always@(negedge scl or negedge rst_n)
if(!rst_n)
begin
j <= 'd0;
send_data <= 'd1;
tx_ack <= 'd0;
tx_cnt <= 'd0;
data_out <= GINGKO;
end
else case(j)
'd0,4'd1,'d2,4'd3,'d4,4'd5,'d6,4'd7: //移位输出数据
begin
if(tx_en)
begin
j <= j + 'd1;
{send_data,data_out[:]} <= data_out;
tx_ack <= 'd0;
end
end
'd8:begin
if(tx_cnt == 'd5) //判断最后一个字节,停止发送
begin
j <= j + 'd1;
tx_ack <= 'd1;
tx_cnt <= 'd0;
end
else
begin
j <= 'd0;
tx_ack <= 'd0;
tx_cnt <= tx_cnt + 'd1;
end
end
'd9:begin
j <= 'd0;
tx_ack <= 'd0;
data_out <= GINGKO;
end
'd10:begin
j <= 'd0;
tx_ack <= 'd0;
end
endcase assign sda = (j >= 'd1 && j <= 4'd8) ? send_data : 'dz; endmodule
源代码下载链接:
链接:http://pan.baidu.com/s/1geWgggF 密码:an3s
iCore4链接: