当前位置:网站首页>SoC 自定义 IP 核--呼吸灯
SoC 自定义 IP 核--呼吸灯
2022-07-22 03:35:00 【Baker_Streets】
一、新建IP核
1. 编写代码
pwm_logic
:
module pwm_logic (
input clk ,
input rst_n ,
input cn_en ,
input [31:0] counter_arr , // 最大值
input [31:0] counter_crr , // 比较值
output reg o_pwm
);
// 计数器--比较值
reg [31:0] cnt_compare;
wire add_com;
wire end_com;
// r
reg [31:0] counter_crr_r;
// 计数器--比较值
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
cnt_compare <= 32'b0;
end
else if (add_com) begin
if (end_com) begin
cnt_compare <= 32'b0;
end
else begin
cnt_compare <= cnt_compare + 1'b1;
end
end
else if (cn_en) begin
cnt_compare <= 0;
end
end
assign add_com = (~cn_end);
assign end_com = (add_com && (cnt_compare == counter_arr - 1));
// counter_crr_r
always @(posedge clk) begin
if (!cnt) begin
counter_crr_r <= counter_crr;
end
else begin
counter_crr_r <= counter_crr_r;
end
end
// o_pwm
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
o_pwm <= 0;
end
else if (cnt > counter_crr_r) begin
o_pwm <= 1;
end
else begin
o_pwm <= 0;
end
end
endmodule //pwm_logic
pwm_avalon.v
:
module pwm_avalon (
input clk ,
input rst_n ,
// avalon
input chipselect ,
input [1:0] address ,
input write ,
input [31:0] writedata ,
output [31:0] readdata ,
// o_pwm
output reg pwm_out
);
// 中间信号定义
reg [31:0] counter_arr; // 最大值
reg [31:0] counter_crr; // 比较值
reg control;
// r
reg [31:0] readdata_r;
// counter_arr
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
counter_arr <= 0;
end
else if (chipselect && write && (address == 0)) begin
counter_arr <= writedata;
end
else begin
counter_arr <= counter_arr;
end
end
// counter_crr
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
counter_crr <= 0;
end
else if (chipselect && write && (address == 1)) begin
counter_crr <= writedata;
end
else begin
counter_crr <= counter_crr;
end
end
// control
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
control <= 0;
end
else if (chipselect && write && (address == 2)) begin
control <= writedata;
end
else begin
control <= control;
end
end
// 读control寄存器
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
readdata_r <= 0;
end
// 延时一个时钟周期再读
else if (chipselect) begin
case (address)
0: readdata_r <= counter_arr;
1: readdata_r <= counter_crr;
2: readdata_r <= control;
default: readdata_r <= 0;
endcase
end
else begin
readdata_r <= readdata_r;
end
end
assign readdata = readdata_r;
pwm_logic u_pwm_logic(
/*input */ .clk (clk),
/*input */ .rst_n (rst_n),
/*input */ .cn_en (control),
/*input [31:0]*/ .counter_arr (counter_arr), // 最大值
/*input [31:0]*/ .counter_crr (counter_crr), // 比较值
/*output reg */ .o_pwm (pwm_out)
);
endmodule //pwm_avalon
2. 新建IP核
在Platfor Designer
中新建:
填写IP名称:
添加文件:
Signals & Interface
设置:
3. 创建IP
二、编写代码
1. 修改top
编译完毕后,参考SoC之HelloWorld
完成 生成rbf文件 、 生成新的hps_0头文件、生成dtb文件,并替换SD卡文件。
2. C代码编写
参考SoC之HelloWorld创建工程和文件,编写代码如下:
/* * breath_led.c * * Created on: 2022年7月20日 * Author: 16438 */
//gcc标准头文件
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
//HPS厂家提供的底层定义头文件
#define soc_cv_av //开发平台Cyclone V 系列
#include "hwlib.h"
#include "socal/socal.h"
#include "socal/hps.h"
//与用户具体的HPS 应用系统相关的硬件描述头文件
#include "hps_0.h"
#define HW_REGS_BASE (ALT_STM_OFST) //HPS外设地址段基地址
#define HW_REGS_SPAN (0x04000000) //HPS外设地址段地址空间 64MB大小
#define HW_REGS_MASK (HW_REGS_SPAN - 1) //HPS外设地址段地址掩码
static unsigned long * pwm_pio_virtual_base = NULL;
static void * virtual_base;
int init(){
// open函数打开MMU,获取总线虚拟地址
int fd = open("/dev/mem", (O_ASYNC | O_RDWR));
if (fd == -1){
printf("Open MMU 失败!\n");
}
virtual_base = mmap(NULL, HW_REGS_SPAN, (PROT_READ | PROT_WRITE), MAP_SHARED, fd, HW_REGS_BASE);
if ((unsigned long)virtual_base == MAP_SHARED){
printf("地址映射失败");
return -1;
}
pwm_pio_virtual_base = virtual_base +
((unsigned long)(ALT_LWFPGASLVS_OFST + PWM_0_BASE) & (unsigned long)(HW_REGS_MASK));
return fd;
}
int main(){
int fd = init();
if (fd == -1){
printf("初始化失败!\n");
return 1;
}
int max_value = 100000; // 最大值
int compare_value = 0; // 比较值,比比较值大的时候为1
int dealt_value = 100; // 每次变化的值
int is_increasing = 1; // 是否在增加
(*(pwm_pio_virtual_base + 0)) = max_value; // 设置最大值
(*(pwm_pio_virtual_base + 1)) = compare_value; // 设置比较值,初始0
(*(pwm_pio_virtual_base + 2)) = 0; // 设置control,低有效
while (1){
// 一个时钟周期0.02微秒
usleep((int)(max_value * 0.02));
if (is_increasing){
// 增
compare_value += dealt_value;
if (compare_value >= max_value){
compare_value = max_value;
is_increasing = 0;
}
} else{
// 减
compare_value -= dealt_value;
if (compare_value <= 0){
compare_value = 0;
is_increasing = 1;
}
}
(*(pwm_pio_virtual_base + 1)) = compare_value; // 设置比较值
}
if (munmap(virtual_base, HW_REGS_SPAN) == -1){
printf("取消映射失败!\n");
close(fd);
return -1;
}
close(fd);
return 0;
}
继续参考博客运行即可。
边栏推荐
- QT notes - customized qlistwidget
- 你了解Lumen和Nanite吗?在ue5场景制作中如何使用呢?
- [js]: splice(), charCodeAt()
- SSTI简单总结和CISCN 2019华东南]Double Secret
- Metauniverse enabling scenario Finance: a new track for the competitive development of commercial banks
- OPENCN学习DAY3
- Beijing, Shanghai, Guangzhou, Shenzhen and Hangzhou 30K test question: how to allocate JVM memory model?
- DOM style operation
- MySQL advanced addition, deletion, modification and query operations, including detailed explanation and operation of various queries and table design explanation
- StrMultiplyStr
猜你喜欢
载胶原蛋白酶的白蛋白复合纳米颗粒/牛血清白蛋白包裹二氧化铈纳米人工酶
单片机外围器件学习攻略,小bai必看
Leetcode high frequency question: zigzag (zigzag, zigzag) sequence traversal of binary tree
NFTScan 与 Port3 在 NFT 数据领域达成战略合作
Don't look for it, it's all sorted out for you -- complete collection of SQL statements
Jd-h5 development
常见的probe set和gallery set究竟是什么
多米诺骨牌上演:三箭资本崩盘始末
Preparation of tiniposide multilayer coated albumin nanoparticles / human serum albumin polycaprolactone nanoparticles
m利用SIMILINK仿真模块实现多径信道的动态仿真模拟
随机推荐
After 3 years of on-the-job testing experience, can't the interview and testing post even get 20K? Is there such a hole?
How does the 2022 social e-commerce model split marketing? -- Share purchase
Flask对数据库的查询以及关联
yarn 的使用
Is it true that microservices do not choose databases? How to choose?
socket通信中select函数用法
单片机是如何工作的?
2022社交电商模式怎么裂变营销?—分享购
机器学习基础篇(5)图像轮廓
PPy HSA conductive polymer polypyrrole PPy material BSA white rice nanoparticles / albumin coated nano lipid carrier BSA NLC
Ros2 self study notes: TF coordinate management
常见的probe set和gallery set究竟是什么
牛血清白蛋白-铂复合纳米材料/HSA-Pc NPs人血清白蛋白(HSA)包裹酞菁分子纳米粒
SSTI簡單總結和CISCN 2019華東南]Double Secret
ENVI_IDL: 批量制作专题地图
Command line program test automation
JMeter interface font size setting method
How to deal with tough interview questions
【js】作用域和作用域链
微服务真的不挑数据库吗?如何选择?