当前位置: 首页> 科技> 名企 > 建筑模板尺寸及价格_武汉去施工网今日招工_北京网站优化公司_seo网络优化推广

建筑模板尺寸及价格_武汉去施工网今日招工_北京网站优化公司_seo网络优化推广

时间:2025/9/12 8:07:17来源:https://blog.csdn.net/weixin_45939263/article/details/147113210 浏览次数:0次
建筑模板尺寸及价格_武汉去施工网今日招工_北京网站优化公司_seo网络优化推广

使用 fprintf 函数

将输出目标从标准输出(stdout)更改为一个文件指针

1、首先,在头文件或全局变量中定义一个 FILE 类型的指针,用于指向输出文件。
2、在程序启动时,打开文件并将文件指针赋值给上面定义的全局指针。
3、在 read_info 函数中添加一个额外的参数来传递文件指针,或者可以使用全局变量直接在 fpga_reg_read 和 fpga_reg_write 函数中使用。
4、修改 fpga_reg_read 和 fpga_reg_write 函数中的 printf 为 fprintf,并传递文件指针。

#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>// 定义全局文件指针
FILE *log_file = NULL;#define FPGA_CMD_READ  0x80
#define FPGA_CMD_WRITE 0x00#define FPGA_SPI_DEVICE		"/dev/spidev0.0"static int fpga_spi_fd = -1;// 初始化日志文件
void init_log_file(const char *filename) {log_file = fopen(filename, "w");if (log_file == NULL) {perror("Failed to open log file");exit(EXIT_FAILURE);}
}// 关闭日志文件
void close_log_file() {if (log_file != NULL) {fclose(log_file);}
}int fpga_reg_io_init(void)
{int fd;
//	uint8_t mode = SPI_MODE_0;fd = open(FPGA_SPI_DEVICE, O_RDWR);if (fd < 0){fprintf(log_file, "FPGA: can't open device %s\n", FPGA_SPI_DEVICE);return -1;}#if 0if (ioctl(fd, SPI_IOC_WR_MODE, &mode) != 0){fprintf(log_file, "set spi mode failed\n");close(fd);return -1;}
#endiffpga_spi_fd = fd;return 0;
}int fpga_reg_read(unsigned int reg_no)
{uint8_t tx[8] = {0};uint8_t rx[8] = {0};struct spi_ioc_transfer tr;int ret;unsigned int value;if (reg_no & 0xc0000000){fprintf(log_file, "FPGA read: bad register number\n");return -1; // 返回错误码}tx[0] = (reg_no >> 23) | FPGA_CMD_READ;tx[1] = reg_no >> 15;tx[2] = reg_no >> 7;tx[3] = reg_no << 1;memset(&tr, 0, sizeof(tr));tr.tx_buf = (unsigned long)tx;tr.rx_buf = (unsigned long)rx;tr.len = 8;tr.delay_usecs = 0;tr.speed_hz = 1 * 1000 * 1000; // 1MHztr.bits_per_word = 8;ret = ioctl(fpga_spi_fd, SPI_IOC_MESSAGE(1), &tr);if (ret < 1){fprintf(log_file, "FPGA read: can't transfer message\n");return -1; // 返回错误码}value = (rx[4] << 24) | (rx[5] << 16) | (rx[6] << 8) | rx[7];fprintf(log_file, "spi_read :reg_no:02%x ,value:02%x\n", reg_no, value);return value; // 直接返回读取到的值
}int fpga_reg_write(unsigned int reg_no, unsigned int value)
{uint8_t tx[8] = {0};uint8_t rx[8] = {0};struct spi_ioc_transfer tr;int ret;if(reg_no & 0xc0000000){fprintf(log_file, "FPGA read: bad register number\n");return -1;}tx[0] = (reg_no >> 23) | FPGA_CMD_WRITE;tx[1] = reg_no >> 15;tx[2] = reg_no >> 7;tx[3] = reg_no << 1;tx[4] = value >> 24;tx[5] = value >> 16;tx[6] = value >> 8;tx[7] = value;memset(&tr, 0, sizeof(tr));tr.tx_buf = (unsigned long)tx;tr.rx_buf = (unsigned long)rx;tr.len = 8;tr.delay_usecs = 0;tr.speed_hz = 1 * 1000 * 1000; //1Mtr.bits_per_word = 8;ret = ioctl(fpga_spi_fd, SPI_IOC_MESSAGE(1), &tr);if (ret < 1){fprintf(log_file, "FPGA wirte: can't transfer message\n");return -1;}fprintf(log_file, "spi_write :reg_no:02%02x ,value:02%02x\n", reg_no, value);return 0;
}
关键字:建筑模板尺寸及价格_武汉去施工网今日招工_北京网站优化公司_seo网络优化推广

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: