diff --git a/src/Targets/TargetDescription/Pin.hpp b/src/Targets/TargetDescription/Pin.hpp new file mode 100644 index 00000000..913524f3 --- /dev/null +++ b/src/Targets/TargetDescription/Pin.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include + +namespace Targets::TargetDescription +{ + struct Pin + { + std::string position; + std::string pad; + + Pin( + const std::string& position, + const std::string& pad + ) + : position(position) + , pad(pad) + {} + }; +} diff --git a/src/Targets/TargetDescription/Pinout.hpp b/src/Targets/TargetDescription/Pinout.hpp index 3799b287..99d6c354 100644 --- a/src/Targets/TargetDescription/Pinout.hpp +++ b/src/Targets/TargetDescription/Pinout.hpp @@ -1,19 +1,35 @@ #pragma once -#include +#include +#include #include +#include "Pin.hpp" + +#include "src/Targets/TargetPinoutDescriptor.hpp" + namespace Targets::TargetDescription { - struct Pin - { - std::string pad; - int position; - }; - struct Pinout { + std::string key; std::string name; + ::Targets::PinoutType type; + std::optional function; std::vector pins; + + Pinout( + const std::string& key, + const std::string& name, + ::Targets::PinoutType type, + const std::optional& function, + const std::vector& pins + ) + : key(key) + , name(name) + , type(type) + , function(function) + , pins(pins) + {} }; } diff --git a/src/Targets/TargetDescription/TargetDescriptionFile.cpp b/src/Targets/TargetDescription/TargetDescriptionFile.cpp index edd5b321..79c352a4 100644 --- a/src/Targets/TargetDescription/TargetDescriptionFile.cpp +++ b/src/Targets/TargetDescription/TargetDescriptionFile.cpp @@ -7,11 +7,13 @@ #include "src/Services/StringService.hpp" #include "src/Targets/TargetMemory.hpp" +#include "src/Targets/TargetPinoutDescriptor.hpp" #include "src/Helpers/BiMap.hpp" #include "src/Logger/Logger.hpp" #include "src/Exceptions/Exception.hpp" #include "Exceptions/TargetDescriptionParsingFailureException.hpp" +#include "Exceptions/InvalidTargetDescriptionDataException.hpp" namespace Targets::TargetDescription { @@ -247,8 +249,16 @@ namespace Targets::TargetDescription ); } - this->loadVariants(document); - this->loadPinouts(document); + for ( + auto element = deviceElement.firstChildElement("pinouts").firstChildElement("pinout"); + !element.isNull(); + element = element.nextSiblingElement("pinout") + ) { + auto pinout = TargetDescriptionFile::pinoutFromXml(element); + this->pinoutsByKey.insert( + std::pair(pinout.key, std::move(pinout)) + ); + } } const std::string& TargetDescriptionFile::deviceAttribute(const std::string& attributeName) const { @@ -600,99 +610,50 @@ namespace Targets::TargetDescription ); } - void TargetDescriptionFile::loadVariants(const QDomDocument& document) { - const auto deviceElement = document.elementsByTagName("device").item(0).toElement(); + Pinout TargetDescriptionFile::pinoutFromXml(const QDomElement& xmlElement) { + static const auto typesByName = BiMap({ + {"soic", PinoutType::SOIC}, + {"ssop", PinoutType::SSOP}, + {"dip", PinoutType::DIP}, + {"qfn", PinoutType::QFN}, + {"mlf", PinoutType::MLF}, + {"drqfn", PinoutType::DUAL_ROW_QFN}, + {"qfp", PinoutType::QFP}, + {"bga", PinoutType::BGA}, + }); - auto variantNodes = document.elementsByTagName("variants").item(0).toElement() - .elementsByTagName("variant"); + const auto typeName = TargetDescriptionFile::getAttribute(xmlElement, "type"); - for (int variantIndex = 0; variantIndex < variantNodes.count(); variantIndex++) { - try { - auto variantXml = variantNodes.item(variantIndex).toElement(); - - if (!variantXml.hasAttribute("name")) { - throw Exception("Missing name attribute"); - } - - if (!variantXml.hasAttribute("package")) { - throw Exception("Missing package attribute"); - } - - if (!variantXml.hasAttribute("pinout")) { - throw Exception("Missing pinout attribute"); - } - - auto variant = Variant(); - variant.name = variantXml.attribute("name").toStdString(); - variant.pinoutName = variantXml.attribute("pinout").toLower().toStdString(); - variant.package = variantXml.attribute("package").toUpper().toStdString(); - - if (variantXml.hasAttribute("disabled")) { - variant.disabled = (variantXml.attribute("disabled") == "1"); - } - - this->variants.push_back(variant); - - } catch (const Exception& exception) { - Logger::debug( - "Failed to extract variant from target description element - " + exception.getMessage() - ); - } + const auto type = typesByName.valueAt(typeName); + if (!type.has_value()) { + throw InvalidTargetDescriptionDataException( + "Failed to extract pinout from TDF - invalid pinout type name \"" + typeName + "\"" + ); } + + auto output = Pinout( + TargetDescriptionFile::getAttribute(xmlElement, "key"), + TargetDescriptionFile::getAttribute(xmlElement, "name"), + *type, + TargetDescriptionFile::tryGetAttribute(xmlElement, "function"), + {} + ); + + for ( + auto element = xmlElement.firstChildElement("pin"); + !element.isNull(); + element = element.nextSiblingElement("pin") + ) { + output.pins.push_back(TargetDescriptionFile::pinFromXml(element)); + } + + return output; } - void TargetDescriptionFile::loadPinouts(const QDomDocument& document) { - const auto deviceElement = document.elementsByTagName("device").item(0).toElement(); - - auto pinoutNodes = document.elementsByTagName("pinouts").item(0).toElement() - .elementsByTagName("pinout"); - - for (int pinoutIndex = 0; pinoutIndex < pinoutNodes.count(); pinoutIndex++) { - try { - auto pinoutXml = pinoutNodes.item(pinoutIndex).toElement(); - - if (!pinoutXml.hasAttribute("name")) { - throw Exception("Missing name attribute"); - } - - auto pinout = Pinout(); - pinout.name = pinoutXml.attribute("name").toLower().toStdString(); - - auto pinNodes = pinoutXml.elementsByTagName("pin"); - - for (int pinIndex = 0; pinIndex < pinNodes.count(); pinIndex++) { - auto pinXml = pinNodes.item(pinIndex).toElement(); - - if (!pinXml.hasAttribute("position")) { - throw Exception( - "Missing position attribute on pin element " + std::to_string(pinIndex) - ); - } - - if (!pinXml.hasAttribute("pad")) { - throw Exception("Missing pad attribute on pin element " + std::to_string(pinIndex)); - } - - auto pin = Pin(); - bool positionConversionSucceeded = true; - pin.position = pinXml.attribute("position").toInt(&positionConversionSucceeded, 10); - pin.pad = pinXml.attribute("pad").toLower().toStdString(); - - if (!positionConversionSucceeded) { - throw Exception("Failed to convert position attribute value to integer on pin element " - + std::to_string(pinIndex)); - } - - pinout.pins.push_back(pin); - } - - this->pinoutsMappedByName.insert(std::pair(pinout.name, pinout)); - - } catch (const Exception& exception) { - Logger::debug( - "Failed to extract pinout from target description element - " + exception.getMessage() - ); - } - } + Pin TargetDescriptionFile::pinFromXml(const QDomElement& xmlElement) { + return Pin( + TargetDescriptionFile::getAttribute(xmlElement, "position"), + TargetDescriptionFile::getAttribute(xmlElement, "pad") + ); } } diff --git a/src/Targets/TargetDescription/TargetDescriptionFile.hpp b/src/Targets/TargetDescription/TargetDescriptionFile.hpp index 05e67733..cd8eda6e 100644 --- a/src/Targets/TargetDescription/TargetDescriptionFile.hpp +++ b/src/Targets/TargetDescription/TargetDescriptionFile.hpp @@ -22,6 +22,7 @@ #include "RegisterGroupInstance.hpp" #include "Signal.hpp" #include "Pinout.hpp" +#include "Pin.hpp" #include "Variant.hpp" #include "src/Targets/TargetFamily.hpp" @@ -109,9 +110,8 @@ namespace Targets::TargetDescription std::vector physicalInterfaces; std::map> modulesByKey; std::map> peripheralsByKey; + std::map> pinoutsByKey; std::map> peripheralRegisterGroupsMappedByModuleRegisterGroupName; - std::vector variants; - std::map pinoutsMappedByName; TargetDescriptionFile() = default; virtual ~TargetDescriptionFile() = default; @@ -144,15 +144,7 @@ namespace Targets::TargetDescription static Peripheral peripheralFromXml(const QDomElement& xmlElement); static RegisterGroupInstance registerGroupInstanceFromXml(const QDomElement& xmlElement); static Signal signalFromXml(const QDomElement& xmlElement); - - /** - * Extracts all variants and loads them into this->variants. - */ - void loadVariants(const QDomDocument& document); - - /** - * Extracts all pinouts and loads them into this->pinoutsMappedByName. - */ - void loadPinouts(const QDomDocument& document); + static Pinout pinoutFromXml(const QDomElement& xmlElement); + static Pin pinFromXml(const QDomElement& xmlElement); }; } diff --git a/src/Targets/TargetPinoutDescriptor.hpp b/src/Targets/TargetPinoutDescriptor.hpp new file mode 100644 index 00000000..fb4dd424 --- /dev/null +++ b/src/Targets/TargetPinoutDescriptor.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include + +namespace Targets +{ + enum class PinoutType: std::uint8_t + { + SOIC, + SSOP, + DIP, + QFN, + QFP, + DUAL_ROW_QFN, + MLF, + BGA, + }; +}