2020-04-05 14:27:00 +00:00
|
|
|
|
/*
|
|
|
|
|
Copyright 2020 Blue Liang, liangkangnan@163.com
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2020-06-14 02:40:25 +00:00
|
|
|
|
// GPIO模块
|
2020-04-05 14:27:00 +00:00
|
|
|
|
module gpio(
|
|
|
|
|
|
|
|
|
|
input wire clk,
|
|
|
|
|
input wire rst,
|
|
|
|
|
|
|
|
|
|
input wire we_i,
|
|
|
|
|
input wire[31:0] addr_i,
|
|
|
|
|
input wire[31:0] data_i,
|
|
|
|
|
|
|
|
|
|
output reg[31:0] data_o,
|
2020-06-14 02:40:25 +00:00
|
|
|
|
|
|
|
|
|
input wire[1:0] io_pin_i,
|
|
|
|
|
output wire[31:0] reg_ctrl,
|
|
|
|
|
output wire[31:0] reg_data
|
2020-04-05 14:27:00 +00:00
|
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
2020-06-14 02:40:25 +00:00
|
|
|
|
// GPIO控制寄存器
|
|
|
|
|
localparam GPIO_CTRL = 4'h0;
|
|
|
|
|
// GPIO数据寄存器
|
2020-04-05 14:27:00 +00:00
|
|
|
|
localparam GPIO_DATA = 4'h4;
|
|
|
|
|
|
2020-06-14 02:40:25 +00:00
|
|
|
|
// 每2位控制1个IO的模式,最多支持16个IO
|
|
|
|
|
// 0: 高阻,1:输出,2:输入
|
|
|
|
|
reg[31:0] gpio_ctrl;
|
|
|
|
|
// 输入输出数据
|
2020-04-05 14:27:00 +00:00
|
|
|
|
reg[31:0] gpio_data;
|
|
|
|
|
|
|
|
|
|
|
2020-06-14 02:40:25 +00:00
|
|
|
|
assign reg_ctrl = gpio_ctrl;
|
|
|
|
|
assign reg_data = gpio_data;
|
2020-04-05 14:27:00 +00:00
|
|
|
|
|
|
|
|
|
|
2020-06-14 02:40:25 +00:00
|
|
|
|
// 写寄存器
|
2020-04-05 14:27:00 +00:00
|
|
|
|
always @ (posedge clk) begin
|
|
|
|
|
if (rst == 1'b0) begin
|
|
|
|
|
gpio_data <= 32'h0;
|
2020-06-14 02:40:25 +00:00
|
|
|
|
gpio_ctrl <= 32'h0;
|
2020-04-05 14:27:00 +00:00
|
|
|
|
end else begin
|
|
|
|
|
if (we_i == 1'b1) begin
|
|
|
|
|
case (addr_i[3:0])
|
2020-06-14 02:40:25 +00:00
|
|
|
|
GPIO_CTRL: begin
|
|
|
|
|
gpio_ctrl <= data_i;
|
|
|
|
|
end
|
2020-04-05 14:27:00 +00:00
|
|
|
|
GPIO_DATA: begin
|
|
|
|
|
gpio_data <= data_i;
|
|
|
|
|
end
|
|
|
|
|
endcase
|
2020-06-14 02:40:25 +00:00
|
|
|
|
end else begin
|
|
|
|
|
if (gpio_ctrl[1:0] == 2'b10) begin
|
|
|
|
|
gpio_data[0] <= io_pin_i[0];
|
|
|
|
|
end
|
|
|
|
|
if (gpio_ctrl[3:2] == 2'b10) begin
|
|
|
|
|
gpio_data[1] <= io_pin_i[1];
|
|
|
|
|
end
|
2020-04-05 14:27:00 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-06-14 02:40:25 +00:00
|
|
|
|
// 读寄存器
|
2020-04-05 14:27:00 +00:00
|
|
|
|
always @ (*) begin
|
|
|
|
|
if (rst == 1'b0) begin
|
2020-05-02 03:57:25 +00:00
|
|
|
|
data_o = 32'h0;
|
2020-04-05 14:27:00 +00:00
|
|
|
|
end else begin
|
|
|
|
|
case (addr_i[3:0])
|
2020-06-14 02:40:25 +00:00
|
|
|
|
GPIO_CTRL: begin
|
|
|
|
|
data_o = gpio_ctrl;
|
|
|
|
|
end
|
2020-04-05 14:27:00 +00:00
|
|
|
|
GPIO_DATA: begin
|
2020-05-02 03:57:25 +00:00
|
|
|
|
data_o = gpio_data;
|
|
|
|
|
end
|
|
|
|
|
default: begin
|
|
|
|
|
data_o = 32'h0;
|
2020-04-05 14:27:00 +00:00
|
|
|
|
end
|
|
|
|
|
endcase
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
endmodule
|