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.

1057 lines
37 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 <erc_sch_pin_context.h>
  31. #include <string_utils.h>
  32. #include <lib_pin.h>
  33. #include <sch_edit_frame.h>
  34. #include <sch_marker.h>
  35. #include <sch_reference_list.h>
  36. #include <sch_sheet.h>
  37. #include <sch_sheet_pin.h>
  38. #include <sch_textbox.h>
  39. #include <sch_line.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, VECTOR2I() );
  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<ERC_SCH_PIN_CONTEXT> 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 ), subgraph->m_sheet );
  503. pinToScreenMap[item] = subgraph->m_sheet.LastScreen();
  504. }
  505. }
  506. }
  507. std::set<std::pair<ERC_SCH_PIN_CONTEXT, ERC_SCH_PIN_CONTEXT>> tested;
  508. ERC_SCH_PIN_CONTEXT needsDriver;
  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( ERC_SCH_PIN_CONTEXT& refPin : pins )
  515. {
  516. if( refPin.Pin()->GetType() == ELECTRICAL_PINTYPE::PT_POWER_IN )
  517. {
  518. ispowerNet = true;
  519. break;
  520. }
  521. }
  522. for( ERC_SCH_PIN_CONTEXT& refPin : pins )
  523. {
  524. ELECTRICAL_PINTYPE refType = refPin.Pin()->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.Pin()
  531. || ( !needsDriver.Pin()->IsVisible() && refPin.Pin()->IsVisible() )
  532. || ( ispowerNet
  533. != ( needsDriver.Pin()->GetType()
  534. == ELECTRICAL_PINTYPE::PT_POWER_IN )
  535. && ispowerNet == ( refType == ELECTRICAL_PINTYPE::PT_POWER_IN ) ) )
  536. {
  537. needsDriver = refPin;
  538. }
  539. }
  540. if( ispowerNet )
  541. hasDriver |= ( DrivingPowerPinTypes.count( refType ) != 0 );
  542. else
  543. hasDriver |= ( DrivingPinTypes.count( refType ) != 0 );
  544. for( ERC_SCH_PIN_CONTEXT& testPin : pins )
  545. {
  546. if( testPin == refPin )
  547. continue;
  548. ERC_SCH_PIN_CONTEXT first_pin = refPin;
  549. ERC_SCH_PIN_CONTEXT second_pin = testPin;
  550. if( second_pin < first_pin )
  551. std::swap( first_pin, second_pin );
  552. std::pair<ERC_SCH_PIN_CONTEXT, ERC_SCH_PIN_CONTEXT> pair =
  553. std::make_pair( first_pin, second_pin );
  554. if( auto [ins_pin, inserted ] = tested.insert( pair ); !inserted )
  555. continue;
  556. // Multiple pins in the same symbol that share a type,
  557. // name and position are considered
  558. // "stacked" and shouldn't trigger ERC errors
  559. if( refPin.Pin()->GetParent() == testPin.Pin()->GetParent()
  560. && refPin.Pin()->GetPosition() == testPin.Pin()->GetPosition()
  561. && refPin.Pin()->GetName() == testPin.Pin()->GetName()
  562. && refPin.Pin()->GetType() == testPin.Pin()->GetType()
  563. && refPin.Sheet() == testPin.Sheet() )
  564. continue;
  565. ELECTRICAL_PINTYPE testType = testPin.Pin()->GetType();
  566. if( ispowerNet )
  567. hasDriver |= ( DrivingPowerPinTypes.count( testType ) != 0 );
  568. else
  569. hasDriver |= ( DrivingPinTypes.count( testType ) != 0 );
  570. PIN_ERROR erc = settings.GetPinMapValue( refType, testType );
  571. if( erc != PIN_ERROR::OK && settings.IsTestEnabled( ERCE_PIN_TO_PIN_WARNING ) )
  572. {
  573. std::shared_ptr<ERC_ITEM> ercItem =
  574. ERC_ITEM::Create( erc == PIN_ERROR::WARNING ? ERCE_PIN_TO_PIN_WARNING :
  575. ERCE_PIN_TO_PIN_ERROR );
  576. ercItem->SetItems( refPin.Pin(), testPin.Pin() );
  577. ercItem->SetSheetSpecificPath( refPin.Sheet() );
  578. ercItem->SetItemsSheetPaths( refPin.Sheet(), testPin.Sheet() );
  579. ercItem->SetErrorMessage(
  580. wxString::Format( _( "Pins of type %s and %s are connected" ),
  581. ElectricalPinTypeGetText( refType ),
  582. ElectricalPinTypeGetText( testType ) ) );
  583. SCH_MARKER* marker =
  584. new SCH_MARKER( ercItem, refPin.Pin()->GetTransformedPosition() );
  585. pinToScreenMap[refPin.Pin()]->Append( marker );
  586. errors++;
  587. }
  588. }
  589. }
  590. if( needsDriver.Pin() && !hasDriver && !has_noconnect )
  591. {
  592. int err_code = ispowerNet ? ERCE_POWERPIN_NOT_DRIVEN : ERCE_PIN_NOT_DRIVEN;
  593. if( settings.IsTestEnabled( err_code ) )
  594. {
  595. std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( err_code );
  596. ercItem->SetItems( needsDriver.Pin() );
  597. SCH_MARKER* marker =
  598. new SCH_MARKER( ercItem, needsDriver.Pin()->GetTransformedPosition() );
  599. pinToScreenMap[needsDriver.Pin()]->Append( marker );
  600. errors++;
  601. }
  602. }
  603. }
  604. return errors;
  605. }
  606. int ERC_TESTER::TestMultUnitPinConflicts()
  607. {
  608. const NET_MAP& nets = m_schematic->ConnectionGraph()->GetNetMap();
  609. int errors = 0;
  610. std::unordered_map<wxString, std::pair<wxString, SCH_PIN*>> pinToNetMap;
  611. for( const std::pair<NET_NAME_CODE_CACHE_KEY, std::vector<CONNECTION_SUBGRAPH*>> net : nets )
  612. {
  613. const wxString& netName = net.first.Name;
  614. for( CONNECTION_SUBGRAPH* subgraph : net.second )
  615. {
  616. for( EDA_ITEM* item : subgraph->m_items )
  617. {
  618. if( item->Type() == SCH_PIN_T )
  619. {
  620. SCH_PIN* pin = static_cast<SCH_PIN*>( item );
  621. if( !pin->GetLibPin()->GetParent()->IsMulti() )
  622. continue;
  623. wxString name = pin->GetParentSymbol()->GetRef( &subgraph->m_sheet ) +
  624. + ":" + pin->GetShownNumber();
  625. if( !pinToNetMap.count( name ) )
  626. {
  627. pinToNetMap[name] = std::make_pair( netName, pin );
  628. }
  629. else if( pinToNetMap[name].first != netName )
  630. {
  631. std::shared_ptr<ERC_ITEM> ercItem =
  632. ERC_ITEM::Create( ERCE_DIFFERENT_UNIT_NET );
  633. ercItem->SetErrorMessage( wxString::Format(
  634. _( "Pin %s is connected to both %s and %s" ),
  635. pin->GetShownNumber(),
  636. netName,
  637. pinToNetMap[name].first ) );
  638. ercItem->SetItems( pin, pinToNetMap[name].second );
  639. ercItem->SetSheetSpecificPath( subgraph->m_sheet );
  640. ercItem->SetItemsSheetPaths( subgraph->m_sheet, subgraph->m_sheet );
  641. SCH_MARKER* marker = new SCH_MARKER( ercItem,
  642. pin->GetTransformedPosition() );
  643. subgraph->m_sheet.LastScreen()->Append( marker );
  644. errors += 1;
  645. }
  646. }
  647. }
  648. }
  649. }
  650. return errors;
  651. }
  652. int ERC_TESTER::TestSimilarLabels()
  653. {
  654. const NET_MAP& nets = m_schematic->ConnectionGraph()->GetNetMap();
  655. int errors = 0;
  656. std::unordered_map<wxString, SCH_LABEL_BASE*> labelMap;
  657. for( const std::pair<NET_NAME_CODE_CACHE_KEY, std::vector<CONNECTION_SUBGRAPH*>> net : nets )
  658. {
  659. for( CONNECTION_SUBGRAPH* subgraph : net.second )
  660. {
  661. for( EDA_ITEM* item : subgraph->m_items )
  662. {
  663. switch( item->Type() )
  664. {
  665. case SCH_LABEL_T:
  666. case SCH_HIER_LABEL_T:
  667. case SCH_GLOBAL_LABEL_T:
  668. {
  669. SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( item );
  670. wxString normalized = label->GetShownText().Lower();
  671. if( !labelMap.count( normalized ) )
  672. {
  673. labelMap[normalized] = label;
  674. }
  675. else if( labelMap.at( normalized )->GetShownText() != label->GetShownText() )
  676. {
  677. std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_SIMILAR_LABELS );
  678. ercItem->SetItems( label, labelMap.at( normalized ) );
  679. SCH_MARKER* marker = new SCH_MARKER( ercItem, label->GetPosition() );
  680. subgraph->m_sheet.LastScreen()->Append( marker );
  681. errors += 1;
  682. }
  683. break;
  684. }
  685. default:
  686. break;
  687. }
  688. }
  689. }
  690. }
  691. return errors;
  692. }
  693. int ERC_TESTER::TestLibSymbolIssues()
  694. {
  695. wxCHECK( m_schematic, 0 );
  696. SYMBOL_LIB_TABLE* libTable = m_schematic->Prj().SchSymbolLibTable();
  697. wxString msg;
  698. int err_count = 0;
  699. SCH_SCREENS screens( m_schematic->Root() );
  700. for( SCH_SCREEN* screen = screens.GetFirst(); screen != nullptr; screen = screens.GetNext() )
  701. {
  702. std::vector<SCH_MARKER*> markers;
  703. for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
  704. {
  705. SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
  706. LIB_SYMBOL* libSymbolInSchematic = symbol->GetLibSymbolRef().get();
  707. wxCHECK2( libSymbolInSchematic, continue );
  708. wxString libName = symbol->GetLibId().GetLibNickname();
  709. LIB_TABLE_ROW* libTableRow = libTable->FindRow( libName, true );
  710. if( !libTableRow )
  711. {
  712. std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_LIB_SYMBOL_ISSUES );
  713. ercItem->SetItems( symbol );
  714. msg.Printf( _( "The current configuration does not include the library '%s'" ),
  715. UnescapeString( libName ) );
  716. ercItem->SetErrorMessage( msg );
  717. markers.emplace_back( new SCH_MARKER( ercItem, symbol->GetPosition() ) );
  718. continue;
  719. }
  720. else if( !libTable->HasLibrary( libName, true ) )
  721. {
  722. std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_LIB_SYMBOL_ISSUES );
  723. ercItem->SetItems( symbol );
  724. msg.Printf( _( "The library '%s' is not enabled in the current configuration" ),
  725. UnescapeString( libName ) );
  726. ercItem->SetErrorMessage( msg );
  727. markers.emplace_back( new SCH_MARKER( ercItem, symbol->GetPosition() ) );
  728. continue;
  729. }
  730. wxString symbolName = symbol->GetLibId().GetLibItemName();
  731. LIB_SYMBOL* libSymbol = SchGetLibSymbol( symbol->GetLibId(), libTable );
  732. if( libSymbol == nullptr )
  733. {
  734. std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_LIB_SYMBOL_ISSUES );
  735. ercItem->SetItems( symbol );
  736. msg.Printf( _( "Symbol '%s' not found in symbol library '%s'" ),
  737. UnescapeString( symbolName ),
  738. UnescapeString( libName ) );
  739. ercItem->SetErrorMessage( msg );
  740. markers.emplace_back( new SCH_MARKER( ercItem, symbol->GetPosition() ) );
  741. continue;
  742. }
  743. std::unique_ptr<LIB_SYMBOL> flattenedSymbol = libSymbol->Flatten();
  744. constexpr int flags = LIB_ITEM::COMPARE_FLAGS::EQUALITY | LIB_ITEM::COMPARE_FLAGS::ERC;
  745. if( flattenedSymbol->Compare( *libSymbolInSchematic, flags ) != 0 )
  746. {
  747. std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_LIB_SYMBOL_ISSUES );
  748. ercItem->SetItems( symbol );
  749. msg.Printf( _( "Symbol '%s' has been modified in library '%s'" ),
  750. UnescapeString( symbolName ),
  751. UnescapeString( libName ) );
  752. ercItem->SetErrorMessage( msg );
  753. markers.emplace_back( new SCH_MARKER( ercItem, symbol->GetPosition() ) );
  754. }
  755. }
  756. for( SCH_MARKER* marker : markers )
  757. {
  758. screen->Append( marker );
  759. err_count += 1;
  760. }
  761. }
  762. return err_count;
  763. }
  764. int ERC_TESTER::TestOffGridEndpoints( int aGridSize )
  765. {
  766. // The minimal grid size allowed to place a pin is 25 mils
  767. // the best grid size is 50 mils, but 25 mils is still usable
  768. // this is because all symbols are using a 50 mils grid to place pins, and therefore
  769. // the wires must be on the 50 mils grid
  770. // So raise an error if a pin is not on a 25 mil (or bigger: 50 mil or 100 mil) grid
  771. const int min_grid_size = schIUScale.MilsToIU( 25 );
  772. const int clamped_grid_size = ( aGridSize < min_grid_size ) ? min_grid_size : aGridSize;
  773. SCH_SCREENS screens( m_schematic->Root() );
  774. int err_count = 0;
  775. for( SCH_SCREEN* screen = screens.GetFirst(); screen != nullptr; screen = screens.GetNext() )
  776. {
  777. std::vector<SCH_MARKER*> markers;
  778. for( SCH_ITEM* item : screen->Items() )
  779. {
  780. if( item->Type() == SCH_LINE_T && item->IsConnectable() )
  781. {
  782. SCH_LINE* line = static_cast<SCH_LINE*>( item );
  783. if( ( line->GetStartPoint().x % clamped_grid_size ) != 0
  784. || ( line->GetStartPoint().y % clamped_grid_size) != 0 )
  785. {
  786. std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_ENDPOINT_OFF_GRID );
  787. ercItem->SetItems( line );
  788. markers.emplace_back( new SCH_MARKER( ercItem, line->GetStartPoint() ) );
  789. }
  790. else if( ( line->GetEndPoint().x % clamped_grid_size ) != 0
  791. || ( line->GetEndPoint().y % clamped_grid_size) != 0 )
  792. {
  793. std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_ENDPOINT_OFF_GRID );
  794. ercItem->SetItems( line );
  795. markers.emplace_back( new SCH_MARKER( ercItem, line->GetEndPoint() ) );
  796. }
  797. }
  798. else if( item->Type() == SCH_SYMBOL_T )
  799. {
  800. SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
  801. for( SCH_PIN* pin : symbol->GetPins( nullptr ) )
  802. {
  803. VECTOR2I pinPos = pin->GetTransformedPosition();
  804. if( ( pinPos.x % clamped_grid_size ) != 0
  805. || ( pinPos.y % clamped_grid_size) != 0 )
  806. {
  807. std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_ENDPOINT_OFF_GRID );
  808. ercItem->SetItems( pin );
  809. markers.emplace_back( new SCH_MARKER( ercItem, pinPos ) );
  810. break;
  811. }
  812. }
  813. }
  814. }
  815. for( SCH_MARKER* marker : markers )
  816. {
  817. screen->Append( marker );
  818. err_count += 1;
  819. }
  820. }
  821. return err_count;
  822. }
  823. int ERC_TESTER::TestSimModelIssues()
  824. {
  825. wxString msg;
  826. WX_STRING_REPORTER reporter( &msg );
  827. SCH_SHEET_LIST sheets = m_schematic->GetSheets();
  828. int err_count = 0;
  829. SIM_LIB_MGR libMgr( &m_schematic->Prj(), &reporter );
  830. for( SCH_SHEET_PATH& sheet : sheets )
  831. {
  832. std::vector<SCH_MARKER*> markers;
  833. for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
  834. {
  835. // Reset for each symbol
  836. msg.Clear();
  837. SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
  838. SIM_LIBRARY::MODEL model = libMgr.CreateModel( &sheet, *symbol );
  839. if( !msg.IsEmpty() )
  840. {
  841. std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_SIMULATION_MODEL );
  842. ercItem->SetErrorMessage( msg );
  843. ercItem->SetItems( symbol );
  844. markers.emplace_back( new SCH_MARKER( ercItem, symbol->GetPosition() ) );
  845. }
  846. }
  847. for( SCH_MARKER* marker : markers )
  848. {
  849. sheet.LastScreen()->Append( marker );
  850. err_count += 1;
  851. }
  852. }
  853. return err_count;
  854. }