KiCad PCB EDA Suite
Loading...
Searching...
No Matches
tuning_profiles.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2020 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Jon Evans <[email protected]>
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include <lset.h>
23#include <netclass.h>
25#include <nlohmann/json.hpp>
26#include <settings/parameters.h>
27
28
30
31TUNING_PROFILES::TUNING_PROFILES( JSON_SETTINGS* aParent, const std::string& aPath ) :
32 NESTED_SETTINGS( "tuning_profiles", tuningParametersSchemaVersion, aParent, aPath, false )
33{
34 auto saveViaOverrideConfigurationLine =
35 []( nlohmann::json& json_array, const DELAY_PROFILE_VIA_OVERRIDE_ENTRY& item )
36 {
37 const nlohmann::json item_json = { { "signal_layer_from", LSET::Name( item.m_SignalLayerFrom ) },
38 { "signal_layer_to", LSET::Name( item.m_SignalLayerTo ) },
39 { "via_layer_from", LSET::Name( item.m_ViaLayerFrom ) },
40 { "via_layer_to", LSET::Name( item.m_ViaLayerTo ) },
41 { "delay", item.m_Delay } };
42
43 json_array.push_back( item_json );
44 };
45
46 auto readViaOverrideConfigurationLine = []( const nlohmann::json& entry )
47 {
48 wxString signalLayerFromName = entry["signal_layer_from"];
49 int signalLayerFromId = LSET::NameToLayer( signalLayerFromName );
50
51 wxString signalLayerToName = entry["signal_layer_to"];
52 int signalLayerToId = LSET::NameToLayer( signalLayerToName );
53
54 wxString viaLayerFromName = entry["via_layer_from"];
55 int viaLayerFromId = LSET::NameToLayer( viaLayerFromName );
56
57 wxString viaLayerToName = entry["via_layer_to"];
58 int viaLayerToId = LSET::NameToLayer( viaLayerToName );
59
60 int delay = entry["delay"];
61
62 DELAY_PROFILE_VIA_OVERRIDE_ENTRY item{ static_cast<PCB_LAYER_ID>( signalLayerFromId ),
63 static_cast<PCB_LAYER_ID>( signalLayerToId ),
64 static_cast<PCB_LAYER_ID>( viaLayerFromId ),
65 static_cast<PCB_LAYER_ID>( viaLayerToId ), delay };
66
67 return item;
68 };
69
70 auto saveUserDefinedProfileConfigurationLine =
71 [&saveViaOverrideConfigurationLine]( nlohmann::json& json_array, const TUNING_PROFILE& item )
72 {
73 nlohmann::json layer_entries = nlohmann::json::array();
74
75 for( const DELAY_PROFILE_TRACK_PROPAGATION_ENTRY& trackEntry : item.m_TrackPropagationEntries )
76 {
77 nlohmann::json layer_json;
78
79 layer_json["signal_layer"] = LSET::Name( trackEntry.m_signalLayer );
80 layer_json["top_reference_layer"] = LSET::Name( trackEntry.m_topReferenceLayer );
81 layer_json["bottom_reference_layer"] = LSET::Name( trackEntry.m_bottomReferenceLayer );
82 layer_json["width"] = trackEntry.m_width;
83 layer_json["diff_pair_gap"] = trackEntry.m_diffPairGap;
84 layer_json["delay"] = trackEntry.m_delay;
85
86 layer_entries.push_back( layer_json );
87 }
88
89 nlohmann::json via_overrides = nlohmann::json::array();
90
91 for( const DELAY_PROFILE_VIA_OVERRIDE_ENTRY& viaOverride : item.m_ViaOverrides )
92 {
93 saveViaOverrideConfigurationLine( via_overrides, viaOverride );
94 }
95
96 const nlohmann::json item_json = { { "profile_name", item.m_ProfileName.ToUTF8() },
97 { "type", static_cast<int>( item.m_Type ) },
98 { "target_impedance", item.m_TargetImpedance },
99 { "generate_drc_rules", item.m_GenerateNetClassDRCRules },
100 { "enable_time_domain_tuning", item.m_EnableTimeDomainTuning },
101 { "layer_entries", layer_entries },
102 { "via_prop_delay", item.m_ViaPropagationDelay },
103 { "via_overrides", via_overrides } };
104
105 json_array.push_back( item_json );
106 };
107
108 auto readUserDefinedProfileConfigurationLine = [&readViaOverrideConfigurationLine]( const nlohmann::json& entry )
109 {
110 const wxString profileName = entry["profile_name"];
111 const TUNING_PROFILE::PROFILE_TYPE profileType = static_cast<TUNING_PROFILE::PROFILE_TYPE>( entry["type"] );
112 const double targetImpedance = entry["target_impedance"];
113 const bool generateDrcRules = entry["generate_drc_rules"];
114 const bool enableTimeDomainTuning = entry["enable_time_domain_tuning"];
115 const int viaPropDelay = entry["via_prop_delay"];
116 std::vector<DELAY_PROFILE_TRACK_PROPAGATION_ENTRY> trackEntries;
117 std::map<PCB_LAYER_ID, DELAY_PROFILE_TRACK_PROPAGATION_ENTRY> trackEntriesMap;
118
119 for( const nlohmann::json& layerEntry : entry["layer_entries"] )
120 {
121 if( !layerEntry.is_object() )
122 continue;
123
124 wxString signalLayer = layerEntry["signal_layer"];
125 wxString topRefLayer = layerEntry["top_reference_layer"];
126 wxString bottomRefLayer = layerEntry["bottom_reference_layer"];
127 int signalLayerId = LSET::NameToLayer( signalLayer );
128 int topRefLayerId = LSET::NameToLayer( topRefLayer );
129 int bottomRefLayerId = LSET::NameToLayer( bottomRefLayer );
130
132 trackEntry.m_signalLayer = static_cast<PCB_LAYER_ID>( signalLayerId );
133 trackEntry.m_topReferenceLayer = static_cast<PCB_LAYER_ID>( topRefLayerId );
134 trackEntry.m_bottomReferenceLayer = static_cast<PCB_LAYER_ID>( bottomRefLayerId );
135 trackEntry.m_width = layerEntry["width"];
136 trackEntry.m_diffPairGap = layerEntry["diff_pair_gap"];
137 trackEntry.m_delay = layerEntry["delay"];
138 trackEntry.m_enableTimeDomainTuning = enableTimeDomainTuning;
139
140 trackEntries.push_back( trackEntry );
141 trackEntriesMap[static_cast<PCB_LAYER_ID>( signalLayerId )] = trackEntry;
142 }
143
144 std::vector<DELAY_PROFILE_VIA_OVERRIDE_ENTRY> viaOverrides;
145
146 for( const nlohmann::json& viaEntry : entry["via_overrides"] )
147 {
148 if( !viaEntry.is_object() || !viaEntry.contains( "signal_layer_from" ) )
149 continue;
150
151 viaOverrides.push_back( readViaOverrideConfigurationLine( viaEntry ) );
152 }
153
154 TUNING_PROFILE item{ profileName,
155 profileType,
156 targetImpedance,
157 generateDrcRules,
158 enableTimeDomainTuning,
159 std::move( trackEntries ),
160 viaPropDelay,
161 std::move( viaOverrides ),
162 std::move( trackEntriesMap ) };
163
164 return item;
165 };
166
167 m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>(
168 "tuning_profiles_impedance_geometric",
169 [&]() -> nlohmann::json
170 {
171 nlohmann::json ret = nlohmann::json::array();
172
173 for( const auto& entry : m_tuningProfiles )
174 saveUserDefinedProfileConfigurationLine( ret, entry );
175
176 return ret;
177 },
178 [&]( const nlohmann::json& aJson )
179 {
180 if( !aJson.is_array() )
181 return;
182
184
185 for( const nlohmann::json& entry : aJson )
186 {
187 if( !entry.is_object() || !entry.contains( "profile_name" ) )
188 continue;
189
190 m_tuningProfiles.emplace_back( readUserDefinedProfileConfigurationLine( entry ) );
191 }
192 },
193 {} ) );
194}
195
196
198{
199 // Release early before destroying members
200 if( m_parent )
201 {
202 m_parent->ReleaseNestedSettings( this );
203 m_parent = nullptr;
204 }
205}
206
207
209{
210 auto itr = std::find_if( m_tuningProfiles.begin(), m_tuningProfiles.end(),
211 [&aProfileName]( const TUNING_PROFILE& aProfile )
212 {
213 return aProfile.m_ProfileName == aProfileName;
214 } );
215
216 if( itr == m_tuningProfiles.end() )
217 return m_nullDelayProfile;
218
219 return *itr;
220}
221
222
224{
225 return m_tuningProfiles == aOther.m_tuningProfiles;
226}
Represents a single line in a time domain profile track propagation setup.
PCB_LAYER_ID m_bottomReferenceLayer
int m_delay
PCB_LAYER_ID m_topReferenceLayer
int m_diffPairGap
bool m_enableTimeDomainTuning
int m_width
PCB_LAYER_ID m_signalLayer
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
JSON_SETTINGS(const wxString &aFilename, SETTINGS_LOC aLocation, int aSchemaVersion)
static int NameToLayer(wxString &aName)
Return the layer number from a layer name.
Definition lset.cpp:117
static wxString Name(PCB_LAYER_ID aLayerId)
Return the fixed name association with aLayerId.
Definition lset.cpp:188
JSON_SETTINGS * m_parent
A pointer to the parent object to load and store from.
NESTED_SETTINGS(const std::string &aName, int aSchemaVersion, JSON_SETTINGS *aParent, const std::string &aPath, bool aLoadFromFile=true)
Like a normal param, but with custom getter and setter functions.
Definition parameters.h:296
TUNING_PROFILE m_nullDelayProfile
TUNING_PROFILE & GetTuningProfile(wxString aProfileName)
std::vector< TUNING_PROFILE > m_tuningProfiles
TUNING_PROFILES(JSON_SETTINGS *aParent, const std::string &aPath)
bool operator==(const TUNING_PROFILES &aOther) const
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:60
Represents a single line in the time domain configuration via overrides configuration grid.
Represents a single line in the tuning profile configuration grid.
constexpr int tuningParametersSchemaVersion