You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1051 lines
36 KiB

3 years ago
3 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2011 Wayne Stambaugh <stambaughw@gmail.com>
  6. * Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. #include <algorithm>
  26. #include <numeric>
  27. #include "connection_graph.h"
  28. #include <common.h> // for ExpandEnvVarSubstitutions
  29. #include <erc.h>
  30. #include <string_utils.h>
  31. #include <lib_pin.h>
  32. #include <sch_edit_frame.h>
  33. #include <sch_marker.h>
  34. #include <sch_reference_list.h>
  35. #include <sch_sheet.h>
  36. #include <sch_sheet_pin.h>
  37. #include <sch_textbox.h>
  38. #include <sch_line.h>
  39. #include <sch_pin.h>
  40. #include <schematic.h>
  41. #include <drawing_sheet/ds_draw_item.h>
  42. #include <drawing_sheet/ds_proxy_view_item.h>
  43. #include <wx/ffile.h>
  44. #include <sim/sim_lib_mgr.h>
  45. /* ERC tests :
  46. * 1 - conflicts between connected pins ( example: 2 connected outputs )
  47. * 2 - minimal connections requirements ( 1 input *must* be connected to an
  48. * output, or a passive pin )
  49. */
  50. /*
  51. * Minimal ERC requirements:
  52. * All pins *must* be connected (except ELECTRICAL_PINTYPE::PT_NC).
  53. * When a pin is not connected in schematic, the user must place a "non
  54. * connected" symbol to this pin.
  55. * This ensures a forgotten connection will be detected.
  56. */
  57. // Messages for matrix rows:
  58. const wxString CommentERC_H[] =
  59. {
  60. _( "Input Pin" ),
  61. _( "Output Pin" ),
  62. _( "Bidirectional Pin" ),
  63. _( "Tri-State Pin" ),
  64. _( "Passive Pin" ),
  65. _( "Free Pin" ),
  66. _( "Unspecified Pin" ),
  67. _( "Power Input Pin" ),
  68. _( "Power Output Pin" ),
  69. _( "Open Collector" ),
  70. _( "Open Emitter" ),
  71. _( "No Connection" )
  72. };
  73. // Messages for matrix columns
  74. const wxString CommentERC_V[] =
  75. {
  76. _( "Input Pin" ),
  77. _( "Output Pin" ),
  78. _( "Bidirectional Pin" ),
  79. _( "Tri-State Pin" ),
  80. _( "Passive Pin" ),
  81. _( "Free Pin" ),
  82. _( "Unspecified Pin" ),
  83. _( "Power Input Pin" ),
  84. _( "Power Output Pin" ),
  85. _( "Open Collector" ),
  86. _( "Open Emitter" ),
  87. _( "No Connection" )
  88. };
  89. // List of pin types that are considered drivers for usual input pins
  90. // i.e. pin type = ELECTRICAL_PINTYPE::PT_INPUT, but not PT_POWER_IN
  91. // that need only a PT_POWER_OUT pin type to be driven
  92. const std::set<ELECTRICAL_PINTYPE> DrivingPinTypes =
  93. {
  94. ELECTRICAL_PINTYPE::PT_OUTPUT,
  95. ELECTRICAL_PINTYPE::PT_POWER_OUT,
  96. ELECTRICAL_PINTYPE::PT_PASSIVE,
  97. ELECTRICAL_PINTYPE::PT_TRISTATE,
  98. ELECTRICAL_PINTYPE::PT_BIDI
  99. };
  100. // List of pin types that are considered drivers for power pins
  101. // In fact only a ELECTRICAL_PINTYPE::PT_POWER_OUT pin type can drive
  102. // power input pins
  103. const std::set<ELECTRICAL_PINTYPE> DrivingPowerPinTypes =
  104. {
  105. ELECTRICAL_PINTYPE::PT_POWER_OUT
  106. };
  107. // List of pin types that require a driver elsewhere on the net
  108. const std::set<ELECTRICAL_PINTYPE> DrivenPinTypes =
  109. {
  110. ELECTRICAL_PINTYPE::PT_INPUT,
  111. ELECTRICAL_PINTYPE::PT_POWER_IN
  112. };
  113. int ERC_TESTER::TestDuplicateSheetNames( bool aCreateMarker )
  114. {
  115. SCH_SCREEN* screen;
  116. int err_count = 0;
  117. SCH_SCREENS screenList( m_schematic->Root() );
  118. for( screen = screenList.GetFirst(); screen != nullptr; screen = screenList.GetNext() )
  119. {
  120. std::vector<SCH_SHEET*> list;
  121. for( SCH_ITEM* item : screen->Items().OfType( SCH_SHEET_T ) )
  122. list.push_back( static_cast<SCH_SHEET*>( item ) );
  123. for( size_t i = 0; i < list.size(); i++ )
  124. {
  125. SCH_SHEET* sheet = list[i];
  126. for( size_t j = i + 1; j < list.size(); j++ )
  127. {
  128. SCH_SHEET* test_item = list[j];
  129. // We have found a second sheet: compare names
  130. // we are using case insensitive comparison to avoid mistakes between
  131. // similar names like Mysheet and mysheet
  132. if( sheet->GetName().CmpNoCase( test_item->GetName() ) == 0 )
  133. {
  134. if( aCreateMarker )
  135. {
  136. std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_DUPLICATE_SHEET_NAME );
  137. ercItem->SetItems( sheet, test_item );
  138. SCH_MARKER* marker = new SCH_MARKER( ercItem, sheet->GetPosition() );
  139. screen->Append( marker );
  140. }
  141. err_count++;
  142. }
  143. }
  144. }
  145. }
  146. return err_count;
  147. }
  148. void ERC_TESTER::TestTextVars( DS_PROXY_VIEW_ITEM* aDrawingSheet )
  149. {
  150. DS_DRAW_ITEM_LIST wsItems;
  151. auto unresolved = [this]( wxString str )
  152. {
  153. str = ExpandEnvVarSubstitutions( str, &m_schematic->Prj() );
  154. return str.Matches( wxT( "*${*}*" ) );
  155. };
  156. if( aDrawingSheet )
  157. {
  158. wsItems.SetMilsToIUfactor( schIUScale.IU_PER_MILS );
  159. wsItems.SetPageNumber( wxS( "1" ) );
  160. wsItems.SetSheetCount( 1 );
  161. wsItems.SetFileName( wxS( "dummyFilename" ) );
  162. wsItems.SetSheetName( wxS( "dummySheet" ) );
  163. wsItems.SetSheetLayer( wxS( "dummyLayer" ) );
  164. wsItems.SetProject( &m_schematic->Prj() );
  165. wsItems.BuildDrawItemsList( aDrawingSheet->GetPageInfo(), aDrawingSheet->GetTitleBlock());
  166. }
  167. SCH_SHEET_PATH savedCurrentSheet = m_schematic->CurrentSheet();
  168. SCH_SHEET_LIST sheets = m_schematic->GetSheets();
  169. for( SCH_SHEET_PATH& sheet : sheets )
  170. {
  171. m_schematic->SetCurrentSheet( sheet );
  172. SCH_SCREEN* screen = sheet.LastScreen();
  173. for( SCH_ITEM* item : screen->Items().OfType( SCH_LOCATE_ANY_T ) )
  174. {
  175. if( item->Type() == SCH_SYMBOL_T )
  176. {
  177. SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
  178. for( SCH_FIELD& field : symbol->GetFields() )
  179. {
  180. if( unresolved( field.GetShownText() ) )
  181. {
  182. VECTOR2I pos = field.GetPosition() - symbol->GetPosition();
  183. pos = symbol->GetTransform().TransformCoordinate( pos );
  184. pos += symbol->GetPosition();
  185. std::shared_ptr<ERC_ITEM> ercItem =
  186. ERC_ITEM::Create( ERCE_UNRESOLVED_VARIABLE );
  187. ercItem->SetItems( &field );
  188. SCH_MARKER* marker = new SCH_MARKER( ercItem, pos );
  189. screen->Append( marker );
  190. }
  191. }
  192. }
  193. else if( item->Type() == SCH_SHEET_T )
  194. {
  195. SCH_SHEET* subSheet = static_cast<SCH_SHEET*>( item );
  196. for( SCH_FIELD& field : subSheet->GetFields() )
  197. {
  198. if( unresolved( field.GetShownText() ) )
  199. {
  200. std::shared_ptr<ERC_ITEM> ercItem =
  201. ERC_ITEM::Create( ERCE_UNRESOLVED_VARIABLE );
  202. ercItem->SetItems( &field );
  203. SCH_MARKER* marker = new SCH_MARKER( ercItem, field.GetPosition() );
  204. screen->Append( marker );
  205. }
  206. }
  207. for( SCH_SHEET_PIN* pin : static_cast<SCH_SHEET*>( item )->GetPins() )
  208. {
  209. if( pin->GetShownText().Matches( wxT( "*${*}*" ) ) )
  210. {
  211. std::shared_ptr<ERC_ITEM> ercItem =
  212. ERC_ITEM::Create( ERCE_UNRESOLVED_VARIABLE );
  213. ercItem->SetItems( pin );
  214. SCH_MARKER* marker = new SCH_MARKER( ercItem, pin->GetPosition() );
  215. screen->Append( marker );
  216. }
  217. }
  218. }
  219. else if( SCH_TEXT* text = dynamic_cast<SCH_TEXT*>( item ) )
  220. {
  221. if( text->GetShownText().Matches( wxT( "*${*}*" ) ) )
  222. {
  223. std::shared_ptr<ERC_ITEM> ercItem =
  224. ERC_ITEM::Create( ERCE_UNRESOLVED_VARIABLE );
  225. ercItem->SetItems( text );
  226. SCH_MARKER* marker = new SCH_MARKER( ercItem, text->GetPosition() );
  227. screen->Append( marker );
  228. }
  229. }
  230. else if( SCH_TEXTBOX* textBox = dynamic_cast<SCH_TEXTBOX*>( item ) )
  231. {
  232. if( textBox->GetShownText().Matches( wxT( "*${*}*" ) ) )
  233. {
  234. std::shared_ptr<ERC_ITEM> ercItem =
  235. ERC_ITEM::Create( ERCE_UNRESOLVED_VARIABLE );
  236. ercItem->SetItems( textBox );
  237. SCH_MARKER* marker = new SCH_MARKER( ercItem, textBox->GetPosition() );
  238. screen->Append( marker );
  239. }
  240. }
  241. }
  242. for( DS_DRAW_ITEM_BASE* item = wsItems.GetFirst(); item; item = wsItems.GetNext() )
  243. {
  244. if( DS_DRAW_ITEM_TEXT* text = dynamic_cast<DS_DRAW_ITEM_TEXT*>( item ) )
  245. {
  246. if( text->GetShownText().Matches( wxT( "*${*}*" ) ) )
  247. {
  248. std::shared_ptr<ERC_ITEM> erc = ERC_ITEM::Create( ERCE_UNRESOLVED_VARIABLE );
  249. erc->SetErrorMessage( _( "Unresolved text variable in drawing sheet" ) );
  250. SCH_MARKER* marker = new SCH_MARKER( erc, text->GetPosition() );
  251. screen->Append( marker );
  252. }
  253. }
  254. }
  255. }
  256. m_schematic->SetCurrentSheet( savedCurrentSheet );
  257. }
  258. int ERC_TESTER::TestConflictingBusAliases()
  259. {
  260. wxString msg;
  261. int err_count = 0;
  262. SCH_SCREENS screens( m_schematic->Root() );
  263. std::vector< std::shared_ptr<BUS_ALIAS> > aliases;
  264. for( SCH_SCREEN* screen = screens.GetFirst(); screen != nullptr; screen = screens.GetNext() )
  265. {
  266. const std::set< std::shared_ptr<BUS_ALIAS> > screen_aliases = screen->GetBusAliases();
  267. for( const std::shared_ptr<BUS_ALIAS>& alias : screen_aliases )
  268. {
  269. std::vector<wxString> aliasMembers = alias->Members();
  270. std::sort( aliasMembers.begin(), aliasMembers.end() );
  271. for( const std::shared_ptr<BUS_ALIAS>& test : aliases )
  272. {
  273. std::vector<wxString> testMembers = test->Members();
  274. std::sort( testMembers.begin(), testMembers.end() );
  275. if( alias->GetName() == test->GetName() && aliasMembers != testMembers )
  276. {
  277. msg.Printf( _( "Bus alias %s has conflicting definitions on %s and %s" ),
  278. alias->GetName(),
  279. alias->GetParent()->GetFileName(),
  280. test->GetParent()->GetFileName() );
  281. std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_BUS_ALIAS_CONFLICT );
  282. ercItem->SetErrorMessage( msg );
  283. SCH_MARKER* marker = new SCH_MARKER( ercItem, wxPoint() );
  284. test->GetParent()->Append( marker );
  285. ++err_count;
  286. }
  287. }
  288. }
  289. aliases.insert( aliases.end(), screen_aliases.begin(), screen_aliases.end() );
  290. }
  291. return err_count;
  292. }
  293. int ERC_TESTER::TestMultiunitFootprints()
  294. {
  295. SCH_SHEET_LIST sheets = m_schematic->GetSheets();
  296. int errors = 0;
  297. SCH_MULTI_UNIT_REFERENCE_MAP refMap;
  298. sheets.GetMultiUnitSymbols( refMap, true );
  299. for( std::pair<const wxString, SCH_REFERENCE_LIST>& symbol : refMap )
  300. {
  301. SCH_REFERENCE_LIST& refList = symbol.second;
  302. if( refList.GetCount() == 0 )
  303. {
  304. wxFAIL; // it should not happen
  305. continue;
  306. }
  307. // Reference footprint
  308. SCH_SYMBOL* unit = nullptr;
  309. wxString unitName;
  310. wxString unitFP;
  311. for( unsigned i = 0; i < refList.GetCount(); ++i )
  312. {
  313. SCH_SHEET_PATH sheetPath = refList.GetItem( i ).GetSheetPath();
  314. unitFP = refList.GetItem( i ).GetFootprint();
  315. if( !unitFP.IsEmpty() )
  316. {
  317. unit = refList.GetItem( i ).GetSymbol();
  318. unitName = unit->GetRef( &sheetPath, true );
  319. break;
  320. }
  321. }
  322. for( unsigned i = 0; i < refList.GetCount(); ++i )
  323. {
  324. SCH_REFERENCE& secondRef = refList.GetItem( i );
  325. SCH_SYMBOL* secondUnit = secondRef.GetSymbol();
  326. wxString secondName = secondUnit->GetRef( &secondRef.GetSheetPath(), true );
  327. const wxString secondFp = secondRef.GetFootprint();
  328. wxString msg;
  329. if( unit && !secondFp.IsEmpty() && unitFP != secondFp )
  330. {
  331. msg.Printf( _( "Different footprints assigned to %s and %s" ),
  332. unitName, secondName );
  333. std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_DIFFERENT_UNIT_FP );
  334. ercItem->SetErrorMessage( msg );
  335. ercItem->SetItems( unit, secondUnit );
  336. SCH_MARKER* marker = new SCH_MARKER( ercItem, secondUnit->GetPosition() );
  337. secondRef.GetSheetPath().LastScreen()->Append( marker );
  338. ++errors;
  339. }
  340. }
  341. }
  342. return errors;
  343. }
  344. int ERC_TESTER::TestMissingUnits()
  345. {
  346. ERC_SETTINGS& settings = m_schematic->ErcSettings();
  347. SCH_SHEET_LIST sheets = m_schematic->GetSheets();
  348. int errors = 0;
  349. SCH_MULTI_UNIT_REFERENCE_MAP refMap;
  350. sheets.GetMultiUnitSymbols( refMap, true );
  351. for( std::pair<const wxString, SCH_REFERENCE_LIST>& symbol : refMap )
  352. {
  353. SCH_REFERENCE_LIST& refList = symbol.second;
  354. wxCHECK2( refList.GetCount(), continue );
  355. // Reference unit
  356. SCH_REFERENCE& base_ref = refList.GetItem( 0 );
  357. SCH_SYMBOL* unit = base_ref.GetSymbol();
  358. LIB_SYMBOL* libSymbol = base_ref.GetLibPart();
  359. if( static_cast<ssize_t>( refList.GetCount() ) == libSymbol->GetUnitCount() )
  360. continue;
  361. std::set<int> lib_units;
  362. std::set<int> instance_units;
  363. std::set<int> missing_units;
  364. auto report_missing = [&]( std::set<int>& aMissingUnits, wxString aErrorMsg, int aErrorCode )
  365. {
  366. wxString msg;
  367. wxString missing_pin_units = wxT( "[ " );
  368. int ii = 0;
  369. for( int missing_unit : aMissingUnits )
  370. {
  371. if( ii++ == 3 )
  372. {
  373. missing_pin_units += wxT( "....." );
  374. break;
  375. }
  376. missing_pin_units += libSymbol->GetUnitDisplayName( missing_unit ) + ", " ;
  377. }
  378. missing_pin_units.Truncate( missing_pin_units.length() - 2 );
  379. missing_pin_units += wxT( " ]" );
  380. msg.Printf( aErrorMsg, symbol.first, missing_pin_units );
  381. std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( aErrorCode );
  382. ercItem->SetErrorMessage( msg );
  383. ercItem->SetItems( unit );
  384. SCH_MARKER* marker = new SCH_MARKER( ercItem, unit->GetPosition() );
  385. base_ref.GetSheetPath().LastScreen()->Append( marker );
  386. ++errors;
  387. };
  388. for( int ii = 1; ii <= libSymbol->GetUnitCount(); ++ii )
  389. lib_units.insert( lib_units.end(), ii );
  390. for( size_t ii = 0; ii < refList.GetCount(); ++ii )
  391. instance_units.insert( instance_units.end(), refList.GetItem( ii ).GetUnit() );
  392. std::set_difference( lib_units.begin(), lib_units.end(),
  393. instance_units.begin(), instance_units.end(),
  394. std::inserter( missing_units, missing_units.begin() ) );
  395. if( !missing_units.empty() && settings.IsTestEnabled( ERCE_MISSING_UNIT ) )
  396. {
  397. report_missing( missing_units, _( "Symbol %s has unplaced units %s" ),
  398. ERCE_MISSING_UNIT );
  399. }
  400. std::set<int> missing_power;
  401. std::set<int> missing_input;
  402. std::set<int> missing_bidi;
  403. for( int missing_unit : missing_units )
  404. {
  405. LIB_PINS pins;
  406. int convert = 0;
  407. for( size_t ii = 0; ii < refList.GetCount(); ++ii )
  408. {
  409. if( refList.GetItem( ii ).GetUnit() == missing_unit )
  410. {
  411. convert = refList.GetItem( ii ).GetSymbol()->GetConvert();
  412. break;
  413. }
  414. }
  415. libSymbol->GetPins( pins, missing_unit, convert );
  416. for( auto pin : pins )
  417. {
  418. switch( pin->GetType() )
  419. {
  420. case ELECTRICAL_PINTYPE::PT_POWER_IN:
  421. missing_power.insert( missing_unit );
  422. break;
  423. case ELECTRICAL_PINTYPE::PT_BIDI:
  424. missing_bidi.insert( missing_unit );
  425. break;
  426. case ELECTRICAL_PINTYPE::PT_INPUT:
  427. missing_input.insert( missing_unit );
  428. break;
  429. default:
  430. break;
  431. }
  432. }
  433. }
  434. if( !missing_power.empty() && settings.IsTestEnabled( ERCE_MISSING_POWER_INPUT_PIN ) )
  435. {
  436. report_missing( missing_power, _( "Symbol %s has input power pins in units %s that are not placed." ),
  437. ERCE_MISSING_POWER_INPUT_PIN );
  438. }
  439. if( !missing_input.empty() && settings.IsTestEnabled( ERCE_MISSING_INPUT_PIN ) )
  440. {
  441. report_missing( missing_input, _( "Symbol %s has input pins in units %s that are not placed." ),
  442. ERCE_MISSING_INPUT_PIN );
  443. }
  444. if( !missing_bidi.empty() && settings.IsTestEnabled( ERCE_MISSING_BIDI_PIN ) )
  445. {
  446. report_missing( missing_bidi, _( "Symbol %s has bidirectional pins in units %s that are not placed." ),
  447. ERCE_MISSING_BIDI_PIN );
  448. }
  449. }
  450. return errors;
  451. }
  452. int ERC_TESTER::TestNoConnectPins()
  453. {
  454. int err_count = 0;
  455. for( const SCH_SHEET_PATH& sheet : m_schematic->GetSheets() )
  456. {
  457. std::map<VECTOR2I, std::vector<SCH_PIN*>> pinMap;
  458. for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
  459. {
  460. SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
  461. for( SCH_PIN* pin : symbol->GetPins( &sheet ) )
  462. {
  463. if( pin->GetLibPin()->GetType() == ELECTRICAL_PINTYPE::PT_NC )
  464. pinMap[pin->GetPosition()].emplace_back( pin );
  465. }
  466. }
  467. for( const std::pair<const VECTOR2I, std::vector<SCH_PIN*>>& pair : pinMap )
  468. {
  469. if( pair.second.size() > 1 )
  470. {
  471. err_count++;
  472. std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_NOCONNECT_CONNECTED );
  473. ercItem->SetItems( pair.second[0], pair.second[1],
  474. pair.second.size() > 2 ? pair.second[2] : nullptr,
  475. pair.second.size() > 3 ? pair.second[3] : nullptr );
  476. ercItem->SetErrorMessage( _( "Pins with 'no connection' type are connected" ) );
  477. SCH_MARKER* marker = new SCH_MARKER( ercItem, pair.first );
  478. sheet.LastScreen()->Append( marker );
  479. }
  480. }
  481. }
  482. return err_count;
  483. }
  484. int ERC_TESTER::TestPinToPin()
  485. {
  486. ERC_SETTINGS& settings = m_schematic->ErcSettings();
  487. const NET_MAP& nets = m_schematic->ConnectionGraph()->GetNetMap();
  488. int errors = 0;
  489. for( const std::pair<NET_NAME_CODE_CACHE_KEY, std::vector<CONNECTION_SUBGRAPH*>> net : nets )
  490. {
  491. std::vector<SCH_PIN*> pins;
  492. std::unordered_map<EDA_ITEM*, SCH_SCREEN*> pinToScreenMap;
  493. bool has_noconnect = false;
  494. for( CONNECTION_SUBGRAPH* subgraph: net.second )
  495. {
  496. if( subgraph->m_no_connect )
  497. has_noconnect = true;
  498. for( EDA_ITEM* item : subgraph->m_items )
  499. {
  500. if( item->Type() == SCH_PIN_T )
  501. {
  502. pins.emplace_back( static_cast<SCH_PIN*>( item ) );
  503. pinToScreenMap[item] = subgraph->m_sheet.LastScreen();
  504. }
  505. }
  506. }
  507. std::set<std::pair<SCH_PIN*, SCH_PIN*>> tested;
  508. SCH_PIN* needsDriver = nullptr;
  509. bool hasDriver = false;
  510. // We need different drivers for power nets and normal nets.
  511. // A power net has at least one pin having the ELECTRICAL_PINTYPE::PT_POWER_IN
  512. // and power nets can be driven only by ELECTRICAL_PINTYPE::PT_POWER_OUT pins
  513. bool ispowerNet = false;
  514. for( SCH_PIN* refPin : pins )
  515. {
  516. if( refPin->GetType() == ELECTRICAL_PINTYPE::PT_POWER_IN )
  517. {
  518. ispowerNet = true;
  519. break;
  520. }
  521. }
  522. for( SCH_PIN* refPin : pins )
  523. {
  524. ELECTRICAL_PINTYPE refType = refPin->GetType();
  525. if( DrivenPinTypes.count( refType ) )
  526. {
  527. // needsDriver will be the pin shown in the error report eventually, so try to
  528. // upgrade to a "better" pin if possible: something visible and only a power symbol
  529. // if this net needs a power driver
  530. if( !needsDriver ||
  531. ( !needsDriver->IsVisible() && refPin->IsVisible() ) ||
  532. ( ispowerNet != ( needsDriver->GetType() == ELECTRICAL_PINTYPE::PT_POWER_IN ) &&
  533. ispowerNet == ( refType == ELECTRICAL_PINTYPE::PT_POWER_IN ) ) )
  534. {
  535. needsDriver = refPin;
  536. }
  537. }
  538. if( ispowerNet )
  539. hasDriver |= ( DrivingPowerPinTypes.count( refType ) != 0 );
  540. else
  541. hasDriver |= ( DrivingPinTypes.count( refType ) != 0 );
  542. for( SCH_PIN* testPin : pins )
  543. {
  544. if( testPin == refPin )
  545. continue;
  546. SCH_PIN* first_pin = refPin;
  547. SCH_PIN* second_pin = testPin;
  548. if( first_pin > second_pin )
  549. std::swap( first_pin, second_pin );
  550. std::pair<SCH_PIN*, SCH_PIN*> pair = std::make_pair( first_pin, second_pin );
  551. if( auto [ins_pin, inserted ] = tested.insert( pair ); !inserted )
  552. continue;
  553. // Multiple pins in the same symbol that share a type,
  554. // name and position are considered
  555. // "stacked" and shouldn't trigger ERC errors
  556. if( refPin->GetParent() == testPin->GetParent() &&
  557. refPin->GetPosition() == testPin->GetPosition() &&
  558. refPin->GetName() == testPin->GetName() &&
  559. refPin->GetType() == testPin->GetType() )
  560. continue;
  561. ELECTRICAL_PINTYPE testType = testPin->GetType();
  562. if( ispowerNet )
  563. hasDriver |= ( DrivingPowerPinTypes.count( testType ) != 0 );
  564. else
  565. hasDriver |= ( DrivingPinTypes.count( testType ) != 0 );
  566. PIN_ERROR erc = settings.GetPinMapValue( refType, testType );
  567. if( erc != PIN_ERROR::OK && settings.IsTestEnabled( ERCE_PIN_TO_PIN_WARNING ) )
  568. {
  569. std::shared_ptr<ERC_ITEM> ercItem =
  570. ERC_ITEM::Create( erc == PIN_ERROR::WARNING ? ERCE_PIN_TO_PIN_WARNING :
  571. ERCE_PIN_TO_PIN_ERROR );
  572. ercItem->SetItems( refPin, testPin );
  573. ercItem->SetIsSheetSpecific();
  574. ercItem->SetErrorMessage(
  575. wxString::Format( _( "Pins of type %s and %s are connected" ),
  576. ElectricalPinTypeGetText( refType ),
  577. ElectricalPinTypeGetText( testType ) ) );
  578. SCH_MARKER* marker = new SCH_MARKER( ercItem,
  579. refPin->GetTransformedPosition() );
  580. pinToScreenMap[refPin]->Append( marker );
  581. errors++;
  582. }
  583. }
  584. }
  585. if( needsDriver && !hasDriver && !has_noconnect )
  586. {
  587. int err_code = ispowerNet ? ERCE_POWERPIN_NOT_DRIVEN : ERCE_PIN_NOT_DRIVEN;
  588. if( settings.IsTestEnabled( err_code ) )
  589. {
  590. std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( err_code );
  591. ercItem->SetItems( needsDriver );
  592. SCH_MARKER* marker = new SCH_MARKER( ercItem,
  593. needsDriver->GetTransformedPosition() );
  594. pinToScreenMap[needsDriver]->Append( marker );
  595. errors++;
  596. }
  597. }
  598. }
  599. return errors;
  600. }
  601. int ERC_TESTER::TestMultUnitPinConflicts()
  602. {
  603. const NET_MAP& nets = m_schematic->ConnectionGraph()->GetNetMap();
  604. int errors = 0;
  605. std::unordered_map<wxString, std::pair<wxString, SCH_PIN*>> pinToNetMap;
  606. for( const std::pair<NET_NAME_CODE_CACHE_KEY, std::vector<CONNECTION_SUBGRAPH*>> net : nets )
  607. {
  608. const wxString& netName = net.first.Name;
  609. for( CONNECTION_SUBGRAPH* subgraph : net.second )
  610. {
  611. for( EDA_ITEM* item : subgraph->m_items )
  612. {
  613. if( item->Type() == SCH_PIN_T )
  614. {
  615. SCH_PIN* pin = static_cast<SCH_PIN*>( item );
  616. if( !pin->GetLibPin()->GetParent()->IsMulti() )
  617. continue;
  618. wxString name = pin->GetParentSymbol()->GetRef( &subgraph->m_sheet ) +
  619. + ":" + pin->GetShownNumber();
  620. if( !pinToNetMap.count( name ) )
  621. {
  622. pinToNetMap[name] = std::make_pair( netName, pin );
  623. }
  624. else if( pinToNetMap[name].first != netName )
  625. {
  626. std::shared_ptr<ERC_ITEM> ercItem =
  627. ERC_ITEM::Create( ERCE_DIFFERENT_UNIT_NET );
  628. ercItem->SetErrorMessage( wxString::Format(
  629. _( "Pin %s is connected to both %s and %s" ),
  630. pin->GetShownNumber(),
  631. netName,
  632. pinToNetMap[name].first ) );
  633. ercItem->SetItems( pin, pinToNetMap[name].second );
  634. ercItem->SetIsSheetSpecific();
  635. SCH_MARKER* marker = new SCH_MARKER( ercItem,
  636. pin->GetTransformedPosition() );
  637. subgraph->m_sheet.LastScreen()->Append( marker );
  638. errors += 1;
  639. }
  640. }
  641. }
  642. }
  643. }
  644. return errors;
  645. }
  646. int ERC_TESTER::TestSimilarLabels()
  647. {
  648. const NET_MAP& nets = m_schematic->ConnectionGraph()->GetNetMap();
  649. int errors = 0;
  650. std::unordered_map<wxString, SCH_LABEL_BASE*> labelMap;
  651. for( const std::pair<NET_NAME_CODE_CACHE_KEY, std::vector<CONNECTION_SUBGRAPH*>> net : nets )
  652. {
  653. for( CONNECTION_SUBGRAPH* subgraph : net.second )
  654. {
  655. for( EDA_ITEM* item : subgraph->m_items )
  656. {
  657. switch( item->Type() )
  658. {
  659. case SCH_LABEL_T:
  660. case SCH_HIER_LABEL_T:
  661. case SCH_GLOBAL_LABEL_T:
  662. {
  663. SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( item );
  664. wxString normalized = label->GetShownText().Lower();
  665. if( !labelMap.count( normalized ) )
  666. {
  667. labelMap[normalized] = label;
  668. }
  669. else if( labelMap.at( normalized )->GetShownText() != label->GetShownText() )
  670. {
  671. std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_SIMILAR_LABELS );
  672. ercItem->SetItems( label, labelMap.at( normalized ) );
  673. SCH_MARKER* marker = new SCH_MARKER( ercItem, label->GetPosition() );
  674. subgraph->m_sheet.LastScreen()->Append( marker );
  675. errors += 1;
  676. }
  677. break;
  678. }
  679. default:
  680. break;
  681. }
  682. }
  683. }
  684. }
  685. return errors;
  686. }
  687. int ERC_TESTER::TestLibSymbolIssues()
  688. {
  689. wxCHECK( m_schematic, 0 );
  690. SYMBOL_LIB_TABLE* libTable = m_schematic->Prj().SchSymbolLibTable();
  691. wxString msg;
  692. int err_count = 0;
  693. SCH_SCREENS screens( m_schematic->Root() );
  694. for( SCH_SCREEN* screen = screens.GetFirst(); screen != nullptr; screen = screens.GetNext() )
  695. {
  696. std::vector<SCH_MARKER*> markers;
  697. for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
  698. {
  699. SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
  700. LIB_SYMBOL* libSymbolInSchematic = symbol->GetLibSymbolRef().get();
  701. wxCHECK2( libSymbolInSchematic, continue );
  702. wxString libName = symbol->GetLibId().GetLibNickname();
  703. LIB_TABLE_ROW* libTableRow = libTable->FindRow( libName, true );
  704. if( !libTableRow )
  705. {
  706. std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_LIB_SYMBOL_ISSUES );
  707. ercItem->SetItems( symbol );
  708. msg.Printf( _( "The current configuration does not include the library '%s'" ),
  709. UnescapeString( libName ) );
  710. ercItem->SetErrorMessage( msg );
  711. markers.emplace_back( new SCH_MARKER( ercItem, symbol->GetPosition() ) );
  712. continue;
  713. }
  714. else if( !libTable->HasLibrary( libName, true ) )
  715. {
  716. std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_LIB_SYMBOL_ISSUES );
  717. ercItem->SetItems( symbol );
  718. msg.Printf( _( "The library '%s' is not enabled in the current configuration" ),
  719. UnescapeString( libName ) );
  720. ercItem->SetErrorMessage( msg );
  721. markers.emplace_back( new SCH_MARKER( ercItem, symbol->GetPosition() ) );
  722. continue;
  723. }
  724. wxString symbolName = symbol->GetLibId().GetLibItemName();
  725. LIB_SYMBOL* libSymbol = SchGetLibSymbol( symbol->GetLibId(), libTable );
  726. if( libSymbol == nullptr )
  727. {
  728. std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_LIB_SYMBOL_ISSUES );
  729. ercItem->SetItems( symbol );
  730. msg.Printf( _( "Symbol '%s' not found in symbol library '%s'" ),
  731. UnescapeString( symbolName ),
  732. UnescapeString( libName ) );
  733. ercItem->SetErrorMessage( msg );
  734. markers.emplace_back( new SCH_MARKER( ercItem, symbol->GetPosition() ) );
  735. continue;
  736. }
  737. std::unique_ptr<LIB_SYMBOL> flattenedSymbol = libSymbol->Flatten();
  738. constexpr int flags = LIB_ITEM::COMPARE_FLAGS::EQUALITY | LIB_ITEM::COMPARE_FLAGS::ERC;
  739. if( flattenedSymbol->Compare( *libSymbolInSchematic, flags ) != 0 )
  740. {
  741. std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_LIB_SYMBOL_ISSUES );
  742. ercItem->SetItems( symbol );
  743. msg.Printf( _( "Symbol '%s' has been modified in library '%s'" ),
  744. UnescapeString( symbolName ),
  745. UnescapeString( libName ) );
  746. ercItem->SetErrorMessage( msg );
  747. markers.emplace_back( new SCH_MARKER( ercItem, symbol->GetPosition() ) );
  748. }
  749. }
  750. for( SCH_MARKER* marker : markers )
  751. {
  752. screen->Append( marker );
  753. err_count += 1;
  754. }
  755. }
  756. return err_count;
  757. }
  758. int ERC_TESTER::TestOffGridEndpoints( int aGridSize )
  759. {
  760. // The minimal grid size allowed to place a pin is 25 mils
  761. // the best grid size is 50 mils, but 25 mils is still usable
  762. // this is because all symbols are using a 50 mils grid to place pins, and therefore
  763. // the wires must be on the 50 mils grid
  764. // So raise an error if a pin is not on a 25 mil (or bigger: 50 mil or 100 mil) grid
  765. const int min_grid_size = schIUScale.MilsToIU( 25 );
  766. const int clamped_grid_size = ( aGridSize < min_grid_size ) ? min_grid_size : aGridSize;
  767. SCH_SCREENS screens( m_schematic->Root() );
  768. int err_count = 0;
  769. for( SCH_SCREEN* screen = screens.GetFirst(); screen != nullptr; screen = screens.GetNext() )
  770. {
  771. std::vector<SCH_MARKER*> markers;
  772. for( SCH_ITEM* item : screen->Items() )
  773. {
  774. if( item->Type() == SCH_LINE_T && item->IsConnectable() )
  775. {
  776. SCH_LINE* line = static_cast<SCH_LINE*>( item );
  777. if( ( line->GetStartPoint().x % clamped_grid_size ) != 0
  778. || ( line->GetStartPoint().y % clamped_grid_size) != 0 )
  779. {
  780. std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_ENDPOINT_OFF_GRID );
  781. ercItem->SetItems( line );
  782. markers.emplace_back( new SCH_MARKER( ercItem, line->GetStartPoint() ) );
  783. }
  784. else if( ( line->GetEndPoint().x % clamped_grid_size ) != 0
  785. || ( line->GetEndPoint().y % clamped_grid_size) != 0 )
  786. {
  787. std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_ENDPOINT_OFF_GRID );
  788. ercItem->SetItems( line );
  789. markers.emplace_back( new SCH_MARKER( ercItem, line->GetEndPoint() ) );
  790. }
  791. }
  792. else if( item->Type() == SCH_SYMBOL_T )
  793. {
  794. SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
  795. for( SCH_PIN* pin : symbol->GetPins( nullptr ) )
  796. {
  797. VECTOR2I pinPos = pin->GetTransformedPosition();
  798. if( ( pinPos.x % clamped_grid_size ) != 0
  799. || ( pinPos.y % clamped_grid_size) != 0 )
  800. {
  801. std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_ENDPOINT_OFF_GRID );
  802. ercItem->SetItems( pin );
  803. markers.emplace_back( new SCH_MARKER( ercItem, pinPos ) );
  804. break;
  805. }
  806. }
  807. }
  808. }
  809. for( SCH_MARKER* marker : markers )
  810. {
  811. screen->Append( marker );
  812. err_count += 1;
  813. }
  814. }
  815. return err_count;
  816. }
  817. int ERC_TESTER::TestSimModelIssues()
  818. {
  819. wxString msg;
  820. WX_STRING_REPORTER reporter( &msg );
  821. SCH_SHEET_LIST sheets = m_schematic->GetSheets();
  822. int err_count = 0;
  823. SIM_LIB_MGR libMgr( &m_schematic->Prj(), &reporter );
  824. for( SCH_SHEET_PATH& sheet : sheets )
  825. {
  826. std::vector<SCH_MARKER*> markers;
  827. for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
  828. {
  829. // Reset for each symbol
  830. msg.Clear();
  831. SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
  832. SIM_LIBRARY::MODEL model = libMgr.CreateModel( &sheet, *symbol );
  833. if( !msg.IsEmpty() )
  834. {
  835. std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_SIMULATION_MODEL );
  836. ercItem->SetErrorMessage( msg );
  837. ercItem->SetItems( symbol );
  838. markers.emplace_back( new SCH_MARKER( ercItem, symbol->GetPosition() ) );
  839. }
  840. }
  841. for( SCH_MARKER* marker : markers )
  842. {
  843. sheet.LastScreen()->Append( marker );
  844. err_count += 1;
  845. }
  846. }
  847. return err_count;
  848. }