tx-gxx-linux/device/gxx-linux/fpgaupdate/fpgacontrol.cpp

122 lines
3.3 KiB
C++
Raw Normal View History

2023-04-08 00:56:20 +00:00
#include "fpgacontrol.h"
#include "libmtd.h"
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdint.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <thread>
#define MTD_NODE "/dev/mtd0"
#define FPGAFILEPATH "/etc/fpgaupdatefile.bin"
fpgacontrol::fpgacontrol() :
m_fpgafile_fd(0),
m_updateEnable(false)
{
//printf("fpga version : %d \n", fpgaversion);
}
fpgacontrol::~fpgacontrol()
{
}
bool fpgacontrol::ifneedupdatefpga()
{
if ((access(FPGAFILEPATH, F_OK)) != -1)
{
m_updateEnable = true;
printf("found %s file ,will update fpga later!\n", FPGAFILEPATH);
}
else
{
m_updateEnable = false;
printf("%s file do not exist!\n", FPGAFILEPATH);
}
return m_updateEnable;
}
bool fpgacontrol::updatefpga()
{
if (!m_updateEnable)
{
printf("checked don't need to update fpga\n");
return false;
}
return updatefpgafirmware(FPGAFILEPATH);
}
bool fpgacontrol::updatefpgafirmware(std::string fpgafirmpath)
{
system("echo 68 > /sys/class/gpio/export");
system("echo out > /sys/class/gpio/gpio68/direction");
system("echo 0 > /sys/class/gpio/gpio68/value");
if ((m_fpgafile_fd = open(MTD_NODE, O_RDWR)) <= 0)
{
printf("update fpga error,on open mtd node!\n");
return false;
}
libmtd_t desc;
desc = libmtd_open();
if (NULL == desc)
{
printf("libmtd_open error...\n");
return false;
}
mtd_dev_info dev_info;
mtd_get_dev_info(desc, MTD_NODE, &dev_info);
printf("mtd_num = %x\n", dev_info.mtd_num);
printf("major = %x\n", dev_info.major);
printf("minor = %x\n", dev_info.minor);
printf("type_str = %s\n", dev_info.type_str);
printf("name = %s\n", dev_info.name);
printf("size = %d\n", dev_info.size);
printf("eb_cnt = %d\n", dev_info.eb_cnt);
printf("eb_size = %d\n", dev_info.eb_size);
printf("min_io_size = %d\n", dev_info.min_io_size);
printf("subpage_size = %d\n", dev_info.subpage_size);
printf("oob_size = %d\n", dev_info.oob_size);
printf("region_cnt = %d\n", dev_info.region_cnt);
printf("writable = %d\n", dev_info.writable);
printf("bb_allowed = %d\n", dev_info.bb_allowed);
for (int i = 0; i < dev_info.eb_cnt; i++)
{
if (mtd_erase(desc, &dev_info, m_fpgafile_fd, i) != 0)
{
printf("mtd_erase error...........\n");
if (desc)
{
libmtd_close(desc);
desc = NULL;
}
if (m_fpgafile_fd)
{
close(m_fpgafile_fd);
m_fpgafile_fd = NULL;
}
//system("echo 1 > /sys/class/gpio/gpio221/value");
std::this_thread::sleep_for(std::chrono::milliseconds(100));
return false;
}
}
int ret = mtd_write_img(&dev_info, m_fpgafile_fd, 0, 0, fpgafirmpath.c_str());
libmtd_close(desc);
if (ret < 0)
{
printf("mtd_write_img failed...........\n");
return false;
}
else
{
printf("mtd_write_img success...........\n");
std::string rmcmd = "rm -f /etc/fpgaupdatefile.bin";
system(rmcmd.c_str());
return true;
}
}