From 807db81bdd99812bbdbbf05aefd42901628d4cf3 Mon Sep 17 00:00:00 2001 From: Harsh Sharma Date: Wed, 16 Mar 2022 12:35:57 -0500 Subject: Fixed fpga loader --- src/AccessoryCards/LoraCard15.cpp | 6 +-- src/AccessoryCards/Mtac15Fpga.cpp | 81 +++++++++++++++++++++++++++++++-------- 2 files changed, 69 insertions(+), 18 deletions(-) (limited to 'src/AccessoryCards') diff --git a/src/AccessoryCards/LoraCard15.cpp b/src/AccessoryCards/LoraCard15.cpp index 3ed0f31..f71bff3 100644 --- a/src/AccessoryCards/LoraCard15.cpp +++ b/src/AccessoryCards/LoraCard15.cpp @@ -27,11 +27,11 @@ void Device::Lora15Card::AddToDeviceInfo(const std::string Port, LoraCard::AddToDeviceInfo(Port, ProductID); if (ProductID.rfind("MTCAP", 0) == 0) { - spiPath = "/dev/spidev0.0"; + spiPath = LORA_1_5_MTCAP_SPI; } else if (Port.back() == '2') { - spiPath = "/dev/spidev1.2"; + spiPath = LORA_1_5_MTCDT_SPI_AP_2; } else { - spiPath = "/dev/spidev0.2"; + spiPath = LORA_1_5_MTCDT_SPI_AP_1; } Mtac15Fpga mtac15Fpga(spiPath); diff --git a/src/AccessoryCards/Mtac15Fpga.cpp b/src/AccessoryCards/Mtac15Fpga.cpp index 151ff77..fda3737 100644 --- a/src/AccessoryCards/Mtac15Fpga.cpp +++ b/src/AccessoryCards/Mtac15Fpga.cpp @@ -316,7 +316,7 @@ int Mtac15Fpga::cresetWrite(char num) { std::string cresetPath = SYSFS_PLATFORM + port + CRESET; int fd = open(cresetPath.c_str(), O_WRONLY); if (fd < 0) { - printError("Unable to lock file, are you root?"); + printError("Unable to lock creset file: %d %s", fd, cresetPath.c_str()); return -1; } write(fd, &num, 1); @@ -669,6 +669,10 @@ int Mtac15Fpga::busContention() { Mtac15Fpga::Mtac15Fpga(std::string inputFile, std::string forcedPath) { FILE *fp = fopen(DEVICE_INFO_FILE, "r"); char buf[0XFFFF]; + if (fp== NULL) { + perror("Unable to open device info file"); + exitHandler(errno); + } rapidjson::FileReadStream input(fp, buf, sizeof(buf)); deviceInfo.ParseStream(input); fclose(fp); @@ -676,17 +680,20 @@ Mtac15Fpga::Mtac15Fpga(std::string inputFile, std::string forcedPath) { if (!deviceInfo.HasMember("hardwareVersion")) { printError("%s does not have hardware version info, exiting", DEVICE_INFO_FILE); - return; + exitHandler(99); } std::string hwVersion = deviceInfo["hardwareVersion"].GetString(); if (deviceInfo.HasMember("accessoryCards") && deviceInfo["accessoryCards"].IsArray() && deviceInfo["accessoryCards"].Size() > 0) { + rapidjson::SizeType acCardCount = deviceInfo["accessoryCards"].Size(); if (hwVersion.find("MTCDT3") != std::string::npos) { hardwareType = HARDWARE_MTCDT3; } else if (hwVersion.find("MTCDT") != std::string::npos) { hardwareType = HARDWARE_MTCDT; + } else if (hwVersion.find("MTCAP") != std::string::npos) { + hardwareType = HARDWARE_MTCAP; } else { return; } @@ -695,14 +702,53 @@ Mtac15Fpga::Mtac15Fpga(std::string inputFile, std::string forcedPath) { } else { input_file = inputFile; } - port = deviceInfo["accessoryCards"][0]["port"].GetString(); - if (port.back() == '2') { - spiPath = "/dev/spidev1.2"; + if(forcedPath.empty()) { + port = deviceInfo["accessoryCards"][0]["port"].GetString(); + if (port.back() == '2') { + spiPath = LORA_1_5_MTCDT_SPI_AP_2; + } else { + spiPath = LORA_1_5_MTCDT_SPI_AP_1; + } } else { - spiPath = "/dev/spidev0.2"; + if (forcedPath.compare("1") == 0) { + spiPath = LORA_1_5_MTCDT_SPI_AP_1; + port = "ap1"; + } else if (forcedPath.compare("2") == 0) { + port = "ap2"; + spiPath = LORA_1_5_MTCDT_SPI_AP_2; + } else { + spiPath = forcedPath; + rapidjson::SizeType i; + bool found = false; + for (i = 0; i < acCardCount; i++) { + if (spiPath.compare(deviceInfo["accessoryCards"][i]["spiPath"].GetString()) == 0) { + port = deviceInfo["accessoryCards"][i]["port"].GetString(); + found = true; + break; + } + } + if (!found) { + printError("Invalid spi path: %s", spiPath.c_str()); + exitHandler(99); + } + } + } + /* Sanity check config options with device info.json */ + bool valid_config = false; + rapidjson::SizeType j; + for (j = 0; j < acCardCount; j++) { + if (spiPath.compare(deviceInfo["accessoryCards"][j]["spiPath"].GetString()) == 0 + && port.compare(deviceInfo["accessoryCards"][j]["port"].GetString()) == 0) { + valid_config = true; + break; + } + } + if (!valid_config) { + printError("Path %s with port %s does not in exist in %s. Please set a valid config", spiPath.c_str(), port.c_str(), DEVICE_INFO_FILE); + exitHandler(99); } + getFpgaVersion(); - printInfo("Current FPGA version: %d", fpgaVersion); } else if (hwVersion.find("MTCAP") != std::string::npos) { hardwareType = HARDWARE_MTCAP; if (inputFile.empty()) { @@ -712,7 +758,19 @@ Mtac15Fpga::Mtac15Fpga(std::string inputFile, std::string forcedPath) { } spiPath = "/dev/spidev0.0"; getFpgaVersion(); - printInfo("Current FPGA version: %d", fpgaVersion); + } else { + printError("No accessory cards installed/invalid hardware"); + exitHandler(99); + } +} + +void Mtac15Fpga::printFpgaVersion() { + if(fpgaVersion == 255 || fpgaVersion == 0) { + printError("Found invalid FPGA Version %d on spi path %s", fpgaVersion, spiPath.c_str()); + exitHandler(errno); + } else { + printInfo("Found FPGA Version %d on spi path %s", fpgaVersion, spiPath.c_str()); + exit(0); } } @@ -740,11 +798,6 @@ int Mtac15Fpga::getFpgaVersion() { /* setup and upgrade the mtac card with the file specified */ int Mtac15Fpga::upgradeFpga() { - if (hardwareType == HARDWARE_INVALID) { - printError("Invalid hardware"); - return -1; - } - int ret; if (input_file.empty()) { @@ -756,8 +809,6 @@ int Mtac15Fpga::upgradeFpga() { if (ret != 0) { return ret; } - /* check MTAC Hardware Compatibility */ - printInfo("Checking hardware compatibility"); /* check input file checksum */ ret = sha256_file(input_file.c_str()); -- cgit v1.2.3