From 54f86b83b60192f8b33dad90b33b47246165ac51 Mon Sep 17 00:00:00 2001 From: Harsh Sharma Date: Thu, 3 Mar 2022 14:44:44 -0600 Subject: LoRa updates with MTAC-003 --- include/Device/Device.h | 254 +++++++++++++++++++++++++++------------------- include/Fpga/Fpga.h | 74 ++++++++++++++ include/General.h | 87 ++++++++-------- include/Utility/Utility.h | 43 ++++---- 4 files changed, 290 insertions(+), 168 deletions(-) create mode 100644 include/Fpga/Fpga.h (limited to 'include') diff --git a/include/Device/Device.h b/include/Device/Device.h index 198cc67..5f2bf12 100644 --- a/include/Device/Device.h +++ b/include/Device/Device.h @@ -1,113 +1,163 @@ #ifndef DEVICE_H_ #define DEVICE_H_ +#include "Fpga.h" #include "General.h" #include "Utility.h" #include "Version.h" +#include /* ceil */ + +class Mtac15Fpga; class Device { - private: - bool verbose ; - bool isRoot; - rapidjson::Document capabilities; - rapidjson::Document ethSwitch; - rapidjson::Document deviceInfo; - static const std::vector apIdentifiers; - rapidjson::Document accessoryCards; - rapidjson::Value accessoryCard; - rapidjson::Document::AllocatorType& alloc = deviceInfo.GetAllocator(); - rapidjson::Document::AllocatorType& accessoryCardsAlloc = accessoryCards.GetAllocator(); - static std::map capabilityList; - static std::map ethSwitchList; - static std::map deviceInfoList; - - static const std::regex apFilters; - static const std::regex lora15Filters; - static const std::regex loraG16Filters; - static const std::regex loraG64Filters; - static const std::regex gpiobFilters; - static const std::regex mfserFilters; - static const std::regex serialModeFilter; - static const std::regex storeFilters; - static const std::regex showFilters; - - class AccessoryCard { - protected: - Device& device; - const std::string productId; - const std::string port; - public: - AccessoryCard(Device& d, const std::string ProductId, const std::string Port); - }; - - class LoraCard : public AccessoryCard { - protected: - std::string spiPath; - uint8_t fpgaVersion = 255; - public: - LoraCard(Device& d, const std::string ProductId, const std::string Port); - void setCapabilities(); - }; - - class Lora15Card : public LoraCard { - private: - void *spi_target_ptr = NULL; - public: - Lora15Card(Device& d, const std::string ProductId, const std::string Port); - int spiOpen(const char *spidev); - int spiRead(uint8_t address, uint8_t *data); - int spiClose(); - }; - - class Lora21Card : public LoraCard { - private: - std::string cmdFpgaVersion; - public: - Lora21Card(Device& d, const std::string ProductId, const std::string Port); - }; - - class Lora21ExtCard : public Lora21Card { - private: - std::string cmdFpgaVersion2; - public: - Lora21ExtCard(Device& d, const std::string ProductId, const std::string Port); - }; - - class Gpiob : public AccessoryCard { - public: - Gpiob(Device& d, const std::string ProductId, const std::string Port); - }; - - class Mfser : public AccessoryCard { - public: - Mfser(Device& d, const std::string ProductId, const std::string Port); - }; - - public: - Device(); - void getSystemTreeJson(const char * dir_name); - void init(); - bool isAccessoryCard(const char * d_name, const char * dir_name); - bool isValidDirectory(const struct dirent * entry, std::string fullPath, const char * d_name); - void load(); - void logInfo(std::string info); - void logError(std::string info); - void json(); - void mapFileToCapability(); - void mapFirmware(); - void mapMacAddress2(); - void printDir(const std::string dir_name, std::vector &results); - void printJson(); - void printVersion (std::string name); - void printUsage(std::string program); - void show(std::string program); - void showTrigger(std::string name); - void sortAccessoryCards(); - void store(std::string name, std::string value); - void storeTrigger(std::string name, std::string value); - void Verbose(bool val); - bool Verbose(); - void writeJson(); + private: + bool isRoot; + rapidjson::Document capabilities; + rapidjson::Document ethSwitch; + rapidjson::Document deviceInfo; + static const std::vector apIdentifiers; + rapidjson::Document accessoryCards; + rapidjson::Value accessoryCard; + rapidjson::Document::AllocatorType &alloc = deviceInfo.GetAllocator(); + rapidjson::Document::AllocatorType &acAlloc = accessoryCards.GetAllocator(); + static std::map capabilityList; + static std::map ethSwitchList; + static std::map deviceInfoList; + + static const std::regex apFilters; + static const std::regex serialModeFilter; + static const std::regex storeFilters; + static const std::regex showFilters; + static const std::regex mtcdt3Filters; + static const std::regex mtrFilters; + + const unsigned int accessoryCardsListSize = 7; + + class AccessoryCard { + protected: + Device &device; + std::regex name; + + public: + AccessoryCard(Device &d, const std::regex Name); + virtual void AddToDeviceInfo(const std::string Port, + const std::string ProductID); + const std::regex GetName(); + }; + + class LoraCard : public AccessoryCard { + protected: + std::string spiPath; + uint8_t fpgaVersion; + + public: + LoraCard(Device &d, const std::regex Name); + void AddToDeviceInfo(const std::string Port, + const std::string ProductID); + }; + + class Lora03Card : public LoraCard { + private: + std::string spiPath1261; + uint8_t tmp102; + + public: + Lora03Card(Device &d); + void AddToDeviceInfo(const std::string Port, + const std::string ProductID); + }; + + class Lora10Card : public LoraCard { + public: + Lora10Card(Device &d); + void AddToDeviceInfo(const std::string Port, + const std::string ProductID); + }; + + class Lora15Card : public LoraCard { + private: + Lora15Card(); + void *spi_target_ptr = NULL; + const char pin_high = '1'; + const char pin_low = '0'; + + public: + Lora15Card(Device &d); + void AddToDeviceInfo(const std::string Port, + const std::string ProductID); + }; + + class Lora21Card : public LoraCard { + private: + std::string cmdFpgaVersion; + + public: + Lora21Card(Device &d); + Lora21Card(Device &d, const std::regex Name); + void AddToDeviceInfo(const std::string Port, + const std::string ProductID); + }; + + class Lora21ExtCard : public Lora21Card { + private: + std::string cmdFpgaVersion2; + + public: + Lora21ExtCard(Device &d); + void AddToDeviceInfo(const std::string Port, + const std::string ProductID); + }; + + class Lora2G4Card : public LoraCard { + public: + Lora2G4Card(Device &d); + void AddToDeviceInfo(const std::string Port, + const std::string ProductID); + }; + + class Gpiob : public AccessoryCard { + public: + Gpiob(Device &d); + void AddToDeviceInfo(const std::string Port, + const std::string ProductID); + }; + + class Mfser : public AccessoryCard { + public: + Mfser(Device &d); + void AddToDeviceInfo(const std::string Port, + const std::string ProductID); + }; + + std::vector> accessoryCardsList; + + public: + Device(); + void getSystemTreeJson(const char *dir_name); + void init(); + bool isAccessoryCard(const char *d_name, const char *dir_name); + bool isValidDirectory(const struct dirent *entry, std::string fullPath, + const char *d_name); + void load(); + void logInfo(std::string info); + void logError(std::string info); + void json(); + void mapFileToCapability(); + void mapFirmware(); + void mapMacAddress2(); + void printDir(const std::string dir_name, + std::vector &results); + void printJson(); + void printVersion(std::string name); + void printUsage(std::string program); + void show(std::string program); + void showTrigger(std::string name); + void sortAccessoryCards(); + void store(std::string name, std::string value); + void storeTrigger(std::string name, std::string value); + void Verbose(bool val); + bool Verbose(); + void writeJson(); }; #endif /* DEVICE_H_ */ diff --git a/include/Fpga/Fpga.h b/include/Fpga/Fpga.h new file mode 100644 index 0000000..986a269 --- /dev/null +++ b/include/Fpga/Fpga.h @@ -0,0 +1,74 @@ +#ifndef FPGA_H_ +#define FPGA_H_ + +#include "General.h" +#include "Utility.h" +#include "Version.h" +#include /* ceil */ +#include /* generate hash */ + +#define MTAC_SX1301 0x0 +#define MTAC_FPGA 0x1 + +#define MTAC_FPGA_ADDRESS 0x1 +#define READ_ACCESS 0x00 +#define WRITE_ACCESS 0x80 +#define SPI_SPEED 8000000 + +#define MTAC_PAGE_REG 0 +#define MTAC_SOFT_RESET 1 +#define MTAC_VERSION 2 + +#define MTCDT_DEFAULT_FILE "/usr/lib/mts-flash-binaries/mtcdt-fpga-v31.hex" +#define MTCAP_DEFAULT_FILE "/usr/lib/mts-flash-binaries/mtcap-fpga-v31.hex" + +struct mtac_reg_s { + int8_t page; /*!< page containing the register (-1 for all pages) */ + uint8_t addr; /*!< base address of the register (7 bit) */ + uint8_t offs; /*!< position of the register LSB (between 0 to 7) */ + bool sign; /*!< 1 indicates the register is signed (2 complem.) */ + uint8_t leng; /*!< number of bits in the register */ + bool rdon; /*!< 1 indicates a read-only register */ + int32_t dflt; /*!< register default value */ +}; + +class Mtac15Fpga { + private: + HardwareType hardwareType = HARDWARE_INVALID; + void *spi_target_ptr = NULL; + const char pin_high = '1'; + const char pin_low = '0'; + std::string port; + std::string spiPath; + std::string input_file; + uint8_t fpgaVersion; + rapidjson::Document deviceInfo; + rapidjson::Document::AllocatorType &alloc = deviceInfo.GetAllocator(); + + void sha256_hash_string(unsigned char hash[SHA256_DIGEST_LENGTH], + char outputBuffer[65]); + void sha256(char *string, char outputBuffer[65]); + int sha256_file(const char *path); + int spiOpen(); + int spiRead(uint8_t spi_mux_target, uint8_t address, uint8_t *data); + int spiWrite(uint8_t spi_mux_target, uint8_t address, uint8_t data); + int spiClose(); + int busContention(); + int cresetWrite(char num); + int releaseDevice(); + int writeEnable(); + int chipEraseVerify(); + int chipErase(); + int mtacErase(); + int pageProgram(uint8_t adr_lower, uint8_t adr_higher, uint32_t data[256]); + int mtacProgram(const char input_file[]); + + public: + Mtac15Fpga(std::string inputFile, std::string forcedPath); + Mtac15Fpga(std::string path); + int getFpgaVersion(); + int FpgaVersion(); + int upgradeFpga(); +}; + +#endif /* FPGA_H_ */ diff --git a/include/General.h b/include/General.h index c5be2ff..e45f06a 100644 --- a/include/General.h +++ b/include/General.h @@ -2,7 +2,7 @@ #define GENERAL_HPP #ifdef _MSC_VER -#pragma warning ( disable : 4514 4512 4710 4355) +#pragma warning(disable : 4514 4512 4710 4355) #endif typedef signed char sint8; @@ -13,71 +13,68 @@ typedef signed int sint32; typedef unsigned int uint32; typedef signed long long sint64; typedef unsigned long long uint64; -typedef signed int sint; //32 bit - even on 64 bit machines -typedef unsigned int uint; //32 bit - even on 64 bit machines +typedef signed int sint; // 32 bit - even on 64 bit machines +typedef unsigned int uint; // 32 bit - even on 64 bit machines -#define memzero(ptr) memset(ptr,0,sizeof(*(ptr))) -#define ArrayZero(ptr,elements) memset(ptr,0,sizeof(*(ptr)) * (elements)) +#define memzero(ptr) memset(ptr, 0, sizeof(*(ptr))) +#define ArrayZero(ptr, elements) memset(ptr, 0, sizeof(*(ptr)) * (elements)) +#include #include #include +#include +#include /* open */ #include #include -#include -#include /* printf fprintf */ -#include /* open */ #include -#include -#include /* lseek, close */ -#include #include +#include /* printf fprintf */ +#include +#include #include - +#include /* lseek, close */ +#include /* command line parser */ #include "rapidjson/document.h" -#include "rapidjson/writer.h" -#include "rapidjson/stringbuffer.h" +#include "rapidjson/filereadstream.h" #include "rapidjson/istreamwrapper.h" +#include "rapidjson/stringbuffer.h" +#include "rapidjson/writer.h" -#define MAX_ACC_CARDS 4 - -#define VERBOSE false -#define MTS_IO_CONTROLS_STATUS_LED false -#define MTS_IO_CONTROLS_LS_LED false - -#define FIRMWARE_VERSION "Version" -#define FIRMWARE_DATE "Date" - -#define SYSFS_PLATFORM "/sys/devices/platform/mts-io/" -#define HW_VERSION_PATH "/sys/devices/platform/mts-io/hw-version" -#define LEDS_GPIO_DIR "/sys/devices/platform/leds-gpio/leds/" -#define FIRMWARE_FILE "/etc/issue" - -#define LORA_1_5_AP1_FPGA_VERSION "mts-fpga-loader -p 1 -c | grep version | awk '{printf $4}'" -#define LORA_1_5_AP2_FPGA_VERSION "mts-fpga-loader -p 2 -c | grep version | awk '{printf $4}'" -#define LORA_2_1_FPGA_VERSION "mts-fpga-loader -g | grep version | awk '{printf $4}'" -#define LORA_2_1_EXT_FPGA_VERSION "mts-fpga-loader -b 1 -g | grep version | awk '{printf $4}'" +#define MAX_ACC_CARDS 4 +#define VERBOSE false +#define MTS_IO_CONTROLS_STATUS_LED false +#define MTS_IO_CONTROLS_LS_LED false -#define MTAC_SX1301 0x0 -#define MTAC_FPGA 0x1 +#define FIRMWARE_VERSION "Version" +#define FIRMWARE_DATE "Date" +#define FIRMWARE_RELEASE "Release" -#define MTAC_FPGA_ADDRESS 0x1 +#define SYSFS_PLATFORM "/sys/devices/platform/mts-io/" +#define HW_VERSION_PATH "/sys/devices/platform/mts-io/hw-version" +#define LEDS_GPIO_DIR "/sys/devices/platform/leds-gpio/leds/" +#define FIRMWARE_FILE "/etc/issue" +#define CRESET "/creset" -#define READ_ACCESS 0x00 -#define SPI_SPEED 8000000 +#define LORA_2_1_FPGA_VERSION \ + "mts-fpga-loader -g | grep version | awk '{printf $4}'" +#define LORA_2_1_EXT_FPGA_VERSION \ + "mts-fpga-loader -b 1 -g | grep version | awk '{printf $4}'" -#define DEVICE_INFO_FILE "/var/run/config/device_info.json" -#define RESET_SHORT_CMD "reset_short_handler" -#define RESET_LONG_CMD "reset_long_handler" -#define KILL_SIGNAL "kill -l " +#define DEVICE_INFO_FILE "/var/run/config/device_info.json" +#define RESET_SHORT_CMD "reset_short_handler" +#define RESET_LONG_CMD "reset_long_handler" +#define KILL_SIGNAL "kill -l " +enum HardwareType { + HARDWARE_MTCDT, + HARDWARE_MTCDT3, + HARDWARE_MTCAP, + HARDWARE_INVALID +}; #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) - #endif - - - diff --git a/include/Utility/Utility.h b/include/Utility/Utility.h index 8d68baa..caf5661 100644 --- a/include/Utility/Utility.h +++ b/include/Utility/Utility.h @@ -6,34 +6,35 @@ #include /********************************************************************** -* COPYRIGHT 2020 MULTI-TECH SYSTEMS, INC. -* -* ALL RIGHTS RESERVED BY AND FOR THE EXCLUSIVE BENEFIT OF -* MULTI-TECH SYSTEMS, INC. -* -* MULTI-TECH SYSTEMS, INC. - CONFIDENTIAL AND PROPRIETARY -* INFORMATION AND/OR TRADE SECRET. -* -* NOTICE: ALL CODE, PROGRAM, INFORMATION, SCRIPT, INSTRUCTION, -* DATA, AND COMMENT HEREIN IS AND SHALL REMAIN THE CONFIDENTIAL -* INFORMATION AND PROPERTY OF MULTI-TECH SYSTEMS, INC. -* USE AND DISCLOSURE THEREOF, EXCEPT AS STRICTLY AUTHORIZED IN A -* WRITTEN AGREEMENT SIGNED BY MULTI-TECH SYSTEMS, INC. IS PROHIBITED. -* -***********************************************************************/ + * COPYRIGHT 2020 MULTI-TECH SYSTEMS, INC. + * + * ALL RIGHTS RESERVED BY AND FOR THE EXCLUSIVE BENEFIT OF + * MULTI-TECH SYSTEMS, INC. + * + * MULTI-TECH SYSTEMS, INC. - CONFIDENTIAL AND PROPRIETARY + * INFORMATION AND/OR TRADE SECRET. + * + * NOTICE: ALL CODE, PROGRAM, INFORMATION, SCRIPT, INSTRUCTION, + * DATA, AND COMMENT HEREIN IS AND SHALL REMAIN THE CONFIDENTIAL + * INFORMATION AND PROPERTY OF MULTI-TECH SYSTEMS, INC. + * USE AND DISCLOSURE THEREOF, EXCEPT AS STRICTLY AUTHORIZED IN A + * WRITTEN AGREEMENT SIGNED BY MULTI-TECH SYSTEMS, INC. IS PROHIBITED. + * + ***********************************************************************/ inline bool fileExists(std::string file) { struct stat buffer = {}; - return (stat (file.c_str(), &buffer) == 0) ? true : false; + return (stat(file.c_str(), &buffer) == 0) ? true : false; } -inline std::string toCamelCase(const char * d_name) { +inline std::string toCamelCase(const char *d_name) { std::string camelString = strdup(d_name); std::string tempString = ""; - for (size_t x = 0; x < camelString.length(); x++){ - if (camelString[x] == '-' || camelString[x] == '_'){ + for (size_t x = 0; x < camelString.length(); x++) { + if (camelString[x] == '-' || camelString[x] == '_') { tempString = camelString.substr(x + 1, 1); - transform(tempString.begin(), tempString.end(), tempString.begin(), toupper); + transform(tempString.begin(), tempString.end(), tempString.begin(), + toupper); camelString.erase(x, 2); camelString.insert(x, tempString); } @@ -50,7 +51,7 @@ inline void exitHandler(int code) { inline mode_t fileType(std::string file) { struct stat buf = {}; - if (stat (file.c_str(), &buf) == 0) { + if (stat(file.c_str(), &buf) == 0) { return buf.st_mode & S_IFMT; } else { return -1; -- cgit v1.2.3