Hello Richard,

thank you for your quick repsonse and your advice.

We modified our program to fit your remarks, but the
ecrt_slave_config_complete_sdo(...) function fails with the error
"Failed to configure SDO: Invalid argument" and a return code smaller
than zero.
Also some errors appear in dmesq log:
[  +0,359596] EtherCAT ERROR 0-4: SDO upload 0x1600:00 aborted.
[  +0,000010] EtherCAT ERROR 0-4: SDO abort message 0x06020000: "This
object does not exist in the object directory".
[  +0,000005] EtherCAT ERROR 0-4: Failed to read number of mapped PDO
entries.
[  +0,000005] EtherCAT WARNING 0-4: Failed to read PDO entries for PDO
0x1600.

After starting the program, the "Run" led of the EL6631 stays green, the
"Pn Run/Err" keeps blinking green and the "Pn Err" is off.

What am I missing?
Attached you find the whole C program source code, dmesg log, output of
the program and the XML definitions exported from TwinCAT.

Best regards,
Carlos Herkt

Am 10.09.2014 09:16, schrieb Richard Hacker:
> I don't see you using pn_config_master_0xf800 et al. anywhere in your
> proggie! Besides you're missing CoE 0x8002.
>
> Mit freundlichem Gruß
>
> Richard Hacker
>

/* Dmesg output after starting program */
[ +10,975693] EtherCAT: Requesting master 0...
[  +0,000008] EtherCAT: Successfully requested master 0.
[  +3,000440] EtherCAT 0: Domain0: Logical address 0x00000000, 98 byte, 
expected working counter 3.
[  +0,000006] EtherCAT 0:   Datagram domain0-0-main: Logical offset 0x00000000, 
98 byte, type LRW.
[  +0,000129] EtherCAT 0: Master thread exited.
[  +0,000006] EtherCAT 0: Starting EtherCAT-OP thread.
[  +0,001573] EtherCAT WARNING 0: No app_time received up to now, but master 
already active.
[  +0,359596] EtherCAT ERROR 0-4: SDO upload 0x1600:00 aborted.
[  +0,000010] EtherCAT ERROR 0-4: SDO abort message 0x06020000: "This object 
does not exist in the object directory".
[  +0,000005] EtherCAT ERROR 0-4: Failed to read number of mapped PDO entries.
[  +0,000005] EtherCAT WARNING 0-4: Failed to read PDO entries for PDO 0x1600.
[  +0,579982] EtherCAT ERROR 0-4: SDO upload 0x1A00:00 aborted.
[  +0,000012] EtherCAT ERROR 0-4: SDO abort message 0x06020000: "This object 
does not exist in the object directory".
[  +0,000005] EtherCAT ERROR 0-4: Failed to read number of mapped PDO entries.
[  +0,000004] EtherCAT WARNING 0-4: Failed to read PDO entries for PDO 0x1A00.
[  +0,679983] EtherCAT 0: Domain 0: Working counter changed to 3/3.
[  +0,050383] EtherCAT 0: Slave states on main device: PREOP, OP.
[  +3,776881] EtherCAT 0: Releasing master...
[  +0,000063] EtherCAT 0: Master thread exited.
[  +0,000021] EtherCAT 0: Starting EtherCAT-IDLE thread.
[  +0,000702] EtherCAT 0: Released.
[  +0,138335] EtherCAT 0: Slave states on main device: PREOP.
ÿþ<?xml version="1.0" encoding="UTF-16"?>
<TreeItem>
  <ItemName>Term 5 (EL6631)</ItemName>
  <PathName>TIID^Device 2 (EtherCAT)^Term 1 (EK1100)^Term 5 (EL6631)</PathName>
  <ItemType>5</ItemType>
  <ItemId>5</ItemId>
  <ObjectId>#x03020005</ObjectId>
  <ItemSubType>9106</ItemSubType>
  <ItemSubTypeName>EL6631 PROFINET IO Controller</ItemSubTypeName>
  <ChildCount>0</ChildCount>
  <Disabled>false</Disabled>
  <TreeImageId>124</TreeImageId>
  <BoxDef>
    <FieldbusAddress>1005</FieldbusAddress>
    <AmsAddress>
      <AmsPort>0</AmsPort>
      <AmsPortTimeout>5</AmsPortTimeout>
    </AmsAddress>
  </BoxDef>
  <EtherCAT>
    <Slave>
      <Info>
        <Name><![CDATA[Term 5 (EL6631)]]></Name>
        <PhysAddr>1005</PhysAddr>
        <AutoIncAddr>65532</AutoIncAddr>
        <Physics>KK</Physics>
        <VendorId>2</VendorId>
        <ProductCode>434581586</ProductCode>
        <RevisionNo>1179648</RevisionNo>
        <SerialNo>0</SerialNo>
        <ProductRevision>EL6631-0000-0018</ProductRevision>
        <Type>EL6631</Type>
      </Info>
      <ProcessData>
        <Send>
          <BitStart>936</BitStart>
          <BitLength>368</BitLength>
        </Send>
        <Recv>
          <BitStart>936</BitStart>
          <BitLength>416</BitLength>
        </Recv>
        <Sm2>
          <Type>Outputs</Type>
          <StartAddress>6144</StartAddress>
          <ControlByte>100</ControlByte>
          <Enable>1</Enable>
          <Pdo>5632</Pdo>
          <Pdo>6142</Pdo>
          <Pdo>6143</Pdo>
        </Sm2>
        <Sm3>
          <Type>Inputs</Type>
          <StartAddress>9216</StartAddress>
          <ControlByte>32</ControlByte>
          <Enable>1</Enable>
          <Pdo>6656</Pdo>
          <Pdo>7166</Pdo>
          <Pdo>7167</Pdo>
        </Sm3>
        <TxPdo Mandatory="true" Sm="3">
          <Index>#x1a00</Index>
          <Name>cifxrepns_In</Name>
          <Entry>
            <Index>#x6000</Index>
            <SubIndex>1</SubIndex>
            <BitLen>16</BitLen>
            <Name>cifxrepns_BoxState</Name>
            <DataType>UINT</DataType>
            <AdsInfo>
              <AmsAddress>
                <AmsPort>11</AmsPort>
              </AmsAddress>
              <IndexGroup>50593808</IndexGroup>
              <IndexOffset>-2147483531</IndexOffset>
              <Length>2</Length>
            </AdsInfo>
          </Entry>
          <Entry>
            <Index>#x6000</Index>
            <SubIndex>2</SubIndex>
            <BitLen>16</BitLen>
            <Name>cifxrepns_BoxDiag</Name>
            <DataType>UINT</DataType>
            <AdsInfo>
              <AmsAddress>
                <AmsPort>11</AmsPort>
              </AmsAddress>
              <IndexGroup>50593808</IndexGroup>
              <IndexOffset>-2147483529</IndexOffset>
              <Length>2</Length>
            </AdsInfo>
          </Entry>
          <Entry>
            <Index>#x6001</Index>
            <SubIndex>1</SubIndex>
            <BitLen>160</BitLen>
            <Name>Input Data Submodule 1</Name>
            <DataType>ARRAY [0..19] OF BYTE</DataType>
            <AdsInfo>
              <AmsAddress>
                <AmsPort>11</AmsPort>
              </AmsAddress>
              <IndexGroup>50593808</IndexGroup>
              <IndexOffset>-2147483527</IndexOffset>
              <Length>20</Length>
            </AdsInfo>
          </Entry>
          <Entry>
            <Index>#x6001</Index>
            <SubIndex>2</SubIndex>
            <BitLen>160</BitLen>
            <Name>Input Data Submodule 2</Name>
            <DataType>ARRAY [0..19] OF BYTE</DataType>
            <AdsInfo>
              <AmsAddress>
                <AmsPort>11</AmsPort>
              </AmsAddress>
              <IndexGroup>50593808</IndexGroup>
              <IndexOffset>-2147483507</IndexOffset>
              <Length>20</Length>
            </AdsInfo>
          </Entry>
        </TxPdo>
        <RxPdo Mandatory="true" Sm="2">
          <Index>#x1600</Index>
          <Name>cifxrepns_Out</Name>
          <Entry>
            <Index>#x7000</Index>
            <SubIndex>1</SubIndex>
            <BitLen>16</BitLen>
            <Name>cifxrepns_BoxCtrl</Name>
            <DataType>UINT</DataType>
            <AdsInfo>
              <AmsAddress>
                <AmsPort>11</AmsPort>
              </AmsAddress>
              <IndexGroup>50593808</IndexGroup>
              <IndexOffset>-2130706315</IndexOffset>
              <Length>2</Length>
            </AdsInfo>
          </Entry>
          <Entry>
            <Index>#x7001</Index>
            <SubIndex>1</SubIndex>
            <BitLen>160</BitLen>
            <Name>Output Data Submodule 1</Name>
            <DataType>ARRAY [0..19] OF BYTE</DataType>
            <AdsInfo>
              <AmsAddress>
                <AmsPort>11</AmsPort>
              </AmsAddress>
              <IndexGroup>50593808</IndexGroup>
              <IndexOffset>-2130706313</IndexOffset>
              <Length>20</Length>
            </AdsInfo>
          </Entry>
          <Entry>
            <Index>#x7001</Index>
            <SubIndex>2</SubIndex>
            <BitLen>160</BitLen>
            <Name>Output Data Submodule 2</Name>
            <DataType>ARRAY [0..19] OF BYTE</DataType>
            <AdsInfo>
              <AmsAddress>
                <AmsPort>11</AmsPort>
              </AmsAddress>
              <IndexGroup>50593808</IndexGroup>
              <IndexOffset>-2130706293</IndexOffset>
              <Length>20</Length>
            </AdsInfo>
          </Entry>
        </RxPdo>
        <TxPdo Mandatory="true" Sm="3">
          <Index>#x1bfe</Index>
          <Name>PnIoProtocolState</Name>
          <Entry>
            <Index>#xf100</Index>
            <SubIndex>1</SubIndex>
            <BitLen>16</BitLen>
            <Name>DevState</Name>
            <DataType>UINT</DataType>
            <AdsInfo>
              <AmsAddress>
                <AmsPort>11</AmsPort>
              </AmsAddress>
              <IndexGroup>50593808</IndexGroup>
              <IndexOffset>-2147483487</IndexOffset>
              <Length>2</Length>
            </AdsInfo>
          </Entry>
          <Entry>
            <Index>#xf100</Index>
            <SubIndex>2</SubIndex>
            <BitLen>16</BitLen>
            <Name>PnIoError</Name>
            <DataType>UINT</DataType>
            <AdsInfo>
              <AmsAddress>
                <AmsPort>11</AmsPort>
              </AmsAddress>
              <IndexGroup>50593808</IndexGroup>
              <IndexOffset>-2147483485</IndexOffset>
              <Length>2</Length>
            </AdsInfo>
          </Entry>
          <Entry>
            <Index>#xf100</Index>
            <SubIndex>3</SubIndex>
            <BitLen>16</BitLen>
            <Name>PnIoDiag</Name>
            <DataType>UINT</DataType>
            <AdsInfo>
              <AmsAddress>
                <AmsPort>11</AmsPort>
              </AmsAddress>
              <IndexGroup>50593808</IndexGroup>
              <IndexOffset>-2147483483</IndexOffset>
              <Length>2</Length>
            </AdsInfo>
          </Entry>
        </TxPdo>
        <TxPdo Mandatory="true" Sm="3">
          <Index>#x1bff</Index>
          <Name>ECatState</Name>
          <Entry>
            <Index>#xf101</Index>
            <SubIndex>1</SubIndex>
            <BitLen>16</BitLen>
            <Name>ECatState</Name>
            <DataType>UINT</DataType>
            <AdsInfo>
              <AmsAddress>
                <AmsPort>11</AmsPort>
              </AmsAddress>
              <IndexGroup>50593808</IndexGroup>
              <IndexOffset>-2147483481</IndexOffset>
              <Length>2</Length>
            </AdsInfo>
          </Entry>
        </TxPdo>
        <RxPdo Mandatory="true" Sm="2">
          <Index>#x17fe</Index>
          <Name>PnIoProtocolCtrl</Name>
          <Entry>
            <Index>#xf200</Index>
            <SubIndex>1</SubIndex>
            <BitLen>16</BitLen>
            <Name>DevCtrl</Name>
            <DataType>UINT</DataType>
            <AdsInfo>
              <AmsAddress>
                <AmsPort>11</AmsPort>
              </AmsAddress>
              <IndexGroup>50593808</IndexGroup>
              <IndexOffset>-2130706273</IndexOffset>
              <Length>2</Length>
            </AdsInfo>
          </Entry>
        </RxPdo>
        <RxPdo Mandatory="true" Sm="2">
          <Index>#x17ff</Index>
          <Name>ECatCtrl</Name>
          <Entry>
            <Index>#xf201</Index>
            <SubIndex>1</SubIndex>
            <BitLen>16</BitLen>
            <Name>ECatCtrl</Name>
            <DataType>UINT</DataType>
            <AdsInfo>
              <AmsAddress>
                <AmsPort>11</AmsPort>
              </AmsAddress>
              <IndexGroup>50593808</IndexGroup>
              <IndexOffset>-2130706271</IndexOffset>
              <Length>2</Length>
            </AdsInfo>
          </Entry>
        </RxPdo>
      </ProcessData>
      <Mailbox DataLinkLayer="true">
        <Send>
          <Start>4096</Start>
          <Length>1024</Length>
        </Send>
        <Recv>
          <Start>5120</Start>
          <Length>1024</Length>
          <StatusBitAddr>1</StatusBitAddr>
        </Recv>
        <Timeout>
          <ReturningRequest>100</ReturningRequest>
          <Response>1000</Response>
        </Timeout>
        <BootStrap>
          <Send>
            <Start>4096</Start>
            <Length>1024</Length>
          </Send>
          <Recv>
            <Start>5120</Start>
            <Length>1024</Length>
          </Recv>
        </BootStrap>
        <Protocol>AoE</Protocol>
        <Protocol>CoE</Protocol>
        <Protocol>FoE</Protocol>
        <CoE>
          <InitCmds>
            <InitCmd Fixed="true" CompleteAccess="true">
              <Transition>PS</Transition>
              <Comment><![CDATA[EL6631 CoE Init Cmd 0 (F800:00)]]></Comment>
              <Timeout>0</Timeout>
              <Ccs>1</Ccs>
              <Index>63488</Index>
              <SubIndex>0</SubIndex>
              <Data>0f000100656c363633312d706e636f6e74726f6c6c657200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002500200160ea48eec0a80101ffffff00c0a80101c0a8b2fd0501ffff000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000</Data>
            </InitCmd>
            <InitCmd Fixed="true" CompleteAccess="true">
              <Transition>PS</Transition>
              <Comment><![CDATA[EL6631 CoE Init Cmd 1 (F820:00)]]></Comment>
              <Timeout>0</Timeout>
              <Ccs>1</Ccs>
              <Index>63520</Index>
              <SubIndex>0</SubIndex>
              <Data>0200c0a8b2fd01012203</Data>
            </InitCmd>
            <InitCmd Fixed="true" CompleteAccess="true">
              <Transition>PS</Transition>
              <Comment><![CDATA[EL6631 CoE Init Cmd 2 (8000:00)]]></Comment>
              <Timeout>0</Timeout>
              <Ccs>1</Ccs>
              <Index>32768</Index>
              <SubIndex>0</SubIndex>
              <Data>3a00636966787265706e7300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003014c0200000810c0a8b2fd050120000100c0a80109ffffff00c0a8010100c001000000010001000100050008000200000000000000000000000000000000000000200004000100030000000000020000000000000000000000000000000000000000000000000000000000000000000000</Data>
            </InitCmd>
            <InitCmd Fixed="true" CompleteAccess="true">
              <Transition>PS</Transition>
              <Comment><![CDATA[EL6631 CoE Init Cmd 3 (8002:00)]]></Comment>
              <Timeout>0</Timeout>
              <Ccs>1</Ccs>
              <Index>32770</Index>
              <SubIndex>0</SubIndex>
              <Data>7800000000000000008001110000011100000000000000000000000002000503240980003200e803000000000000000000000000000000000000000000000000000000000180011100000211000000000000000000000100040054015c000000000000000000010001000000000000000000000000000000000000000280011100000311000000000000000000000200040054015c000000000000000000000000000000000000000000000000000000000000000100011100000011000000000000000000000300080000000000000000000000000000000000000000000000000000000000000000000000010001001100000010000000000000001400000004000800000000000000000000000000000000000000000000000000000000000000000000000200010011000000100000000000000014000000050008000000000000000000000000000000000000000000000000000000000000000000000005000100100000000f0000001400000000000000060008000000000000000000000000000000000000000000000000000000000000000000000006000100100000000f000000140000000000000007000800000000000000000000000000000000000000000000000000000000000000</Data>
            </InitCmd>
            <InitCmd Fixed="true" CompleteAccess="true">
              <Transition>PS</Transition>
              <Comment><![CDATA[download pdo 0x1A00 entries]]></Comment>
              <Timeout>0</Timeout>
              <Ccs>1</Ccs>
              <Index>6656</Index>
              <SubIndex>0</SubIndex>
              <Data>04001001006010020060a0010160a0020160</Data>
            </InitCmd>
            <InitCmd Fixed="true" CompleteAccess="true">
              <Transition>PS</Transition>
              <Comment><![CDATA[download pdo 0x1600 entries]]></Comment>
              <Timeout>0</Timeout>
              <Ccs>1</Ccs>
              <Index>5632</Index>
              <SubIndex>0</SubIndex>
              <Data>030010010070a0010170a0020170</Data>
            </InitCmd>
            <InitCmd Fixed="true" CompleteAccess="true">
              <Transition>PS</Transition>
              <Comment><![CDATA[download pdo 0x1BFE entries]]></Comment>
              <Timeout>0</Timeout>
              <Ccs>1</Ccs>
              <Index>7166</Index>
              <SubIndex>0</SubIndex>
              <Data>0300100100f1100200f1100300f1</Data>
            </InitCmd>
            <InitCmd Fixed="true" CompleteAccess="true">
              <Transition>PS</Transition>
              <Comment><![CDATA[download pdo 0x1BFF entries]]></Comment>
              <Timeout>0</Timeout>
              <Ccs>1</Ccs>
              <Index>7167</Index>
              <SubIndex>0</SubIndex>
              <Data>0100100101f1</Data>
            </InitCmd>
            <InitCmd Fixed="true" CompleteAccess="true">
              <Transition>PS</Transition>
              <Comment><![CDATA[download pdo 0x17FE entries]]></Comment>
              <Timeout>0</Timeout>
              <Ccs>1</Ccs>
              <Index>6142</Index>
              <SubIndex>0</SubIndex>
              <Data>0100100100f2</Data>
            </InitCmd>
            <InitCmd Fixed="true" CompleteAccess="true">
              <Transition>PS</Transition>
              <Comment><![CDATA[download pdo 0x17FF entries]]></Comment>
              <Timeout>0</Timeout>
              <Ccs>1</Ccs>
              <Index>6143</Index>
              <SubIndex>0</SubIndex>
              <Data>0100100101f2</Data>
            </InitCmd>
            <InitCmd Fixed="true" CompleteAccess="true">
              <Transition>PS</Transition>
              <Comment><![CDATA[download pdo 0x1C12 index]]></Comment>
              <Timeout>0</Timeout>
              <Ccs>1</Ccs>
              <Index>7186</Index>
              <SubIndex>0</SubIndex>
              <Data>03000016fe17ff17</Data>
            </InitCmd>
            <InitCmd Fixed="true" CompleteAccess="true">
              <Transition>PS</Transition>
              <Comment><![CDATA[download pdo 0x1C13 index]]></Comment>
              <Timeout>0</Timeout>
              <Ccs>1</Ccs>
              <Index>7187</Index>
              <SubIndex>0</SubIndex>
              <Data>0300001afe1bff1b</Data>
            </InitCmd>
          </InitCmds>
          <Profile>
            <ChannelInfo>
              <ProfileNo>5001</ProfileNo>
              <AddInfo>0</AddInfo>
            </ChannelInfo>
          </Profile>
        </CoE>
        <AoE>
          <InitCmds>
            <InitCmd Fixed="true">
              <Transition>IP</Transition>
              <Transition>PS</Transition>
              <Comment><![CDATA[AoE Init Cmd (download NetId)]]></Comment>
              <Timeout>0</Timeout>
              <Data>c0a8b2fd0301ed03c0a8b2fd0301ed0303000400120000000000000001000000010000000300000006000000c0a8b2fd0501</Data>
            </InitCmd>
          </InitCmds>
          <NetId>192.168.178.253.5.1</NetId>
        </AoE>
      </Mailbox>
      <InitCmds>
        <InitCmd>
          <Transition>PI</Transition>
          <Transition>BI</Transition>
          <Transition>SI</Transition>
          <Transition>OI</Transition>
          <Comment><![CDATA[set device state to INIT]]></Comment>
          <Requires>cycle</Requires>
          <Cmd>2</Cmd>
          <Adp>65532</Adp>
          <Ado>288</Ado>
          <Data>1100</Data>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>SI</Transition>
          <Transition>OI</Transition>
          <Comment><![CDATA[clear DC activation]]></Comment>
          <Requires>cycle</Requires>
          <Cmd>2</Cmd>
          <Adp>65532</Adp>
          <Ado>2432</Ado>
          <Data>0000</Data>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>PI</Transition>
          <Transition>SI</Transition>
          <Transition>OI</Transition>
          <Comment><![CDATA[check device state for INIT]]></Comment>
          <Requires>cycle</Requires>
          <Cmd>1</Cmd>
          <Adp>65532</Adp>
          <Ado>304</Ado>
          <Data>0000</Data>
          <Retries>3</Retries>
          <Validate>
            <Data>0100</Data>
            <DataMask>0f00</DataMask>
            <Timeout>20000</Timeout>
          </Validate>
        </InitCmd>
        <InitCmd>
          <Transition>BI</Transition>
          <Comment><![CDATA[check device state for INIT]]></Comment>
          <Requires>cycle</Requires>
          <Cmd>1</Cmd>
          <Adp>65532</Adp>
          <Ado>304</Ado>
          <Data>0000</Data>
          <Retries>3</Retries>
          <Validate>
            <Data>0100</Data>
            <DataMask>0f00</DataMask>
            <Timeout>40000</Timeout>
          </Validate>
        </InitCmd>
        <InitCmd>
          <Transition>IP</Transition>
          <Transition>IB</Transition>
          <Comment><![CDATA[set device state to INIT]]></Comment>
          <Requires>cycle</Requires>
          <Cmd>2</Cmd>
          <Adp>65532</Adp>
          <Ado>288</Ado>
          <Data>1100</Data>
          <Cnt>1</Cnt>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>IP</Transition>
          <Transition>IB</Transition>
          <Comment><![CDATA[check device state for INIT]]></Comment>
          <Requires>cycle</Requires>
          <Cmd>1</Cmd>
          <Adp>65532</Adp>
          <Ado>304</Ado>
          <Data>0000</Data>
          <Cnt>1</Cnt>
          <Retries>3</Retries>
          <Validate>
            <Data>0100</Data>
            <DataMask>0f00</DataMask>
            <Timeout>10000</Timeout>
          </Validate>
        </InitCmd>
        <InitCmd>
          <Transition>IP</Transition>
          <Comment><![CDATA[assign EEPROM to ECAT]]></Comment>
          <Requires>cycle</Requires>
          <Cmd>2</Cmd>
          <Adp>65532</Adp>
          <Ado>1280</Ado>
          <Data>00</Data>
          <Cnt>1</Cnt>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>IP</Transition>
          <Comment><![CDATA[check vendor id]]></Comment>
          <Cmd>2</Cmd>
          <Adp>65532</Adp>
          <Ado>1282</Ado>
          <Data>000108000000</Data>
          <Cnt>1</Cnt>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>IP</Transition>
          <Comment><![CDATA[check vendor id]]></Comment>
          <Requires>cycle</Requires>
          <Cmd>1</Cmd>
          <Adp>65532</Adp>
          <Ado>1288</Ado>
          <Data>00000000</Data>
          <Cnt>1</Cnt>
          <Retries>3</Retries>
          <Validate>
            <Data>02000000</Data>
            <Timeout>100</Timeout>
          </Validate>
        </InitCmd>
        <InitCmd>
          <Transition>IP</Transition>
          <Comment><![CDATA[check product code]]></Comment>
          <Cmd>2</Cmd>
          <Adp>65532</Adp>
          <Ado>1282</Ado>
          <Data>00010a000000</Data>
          <Cnt>1</Cnt>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>IP</Transition>
          <Comment><![CDATA[check product code]]></Comment>
          <Requires>cycle</Requires>
          <Cmd>1</Cmd>
          <Adp>65532</Adp>
          <Ado>1288</Ado>
          <Data>00000000</Data>
          <Cnt>1</Cnt>
          <Retries>3</Retries>
          <Validate>
            <Data>5230e719</Data>
            <Timeout>100</Timeout>
          </Validate>
        </InitCmd>
        <InitCmd>
          <Transition>IP</Transition>
          <Transition>IB</Transition>
          <Comment><![CDATA[set physical address]]></Comment>
          <Requires>cycle</Requires>
          <Cmd>2</Cmd>
          <Adp>65532</Adp>
          <Ado>16</Ado>
          <Data>ed03</Data>
          <Cnt>1</Cnt>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>IP</Transition>
          <Transition>IB</Transition>
          <Transition>PI</Transition>
          <Transition>SI</Transition>
          <Transition>OI</Transition>
          <Comment><![CDATA[clear sm 0/1 (mailbox out/in)]]></Comment>
          <Cmd>5</Cmd>
          <Adp>1005</Adp>
          <Ado>2048</Ado>
          <Data>00000000000000000000000000000000</Data>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>BI</Transition>
          <Comment><![CDATA[clear sm 0/1 (mailbox out/in)]]></Comment>
          <Cmd>2</Cmd>
          <Adp>65532</Adp>
          <Ado>2048</Ado>
          <Data>00000000000000000000000000000000</Data>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>IP</Transition>
          <Comment><![CDATA[set sm 0 (mailbox out)]]></Comment>
          <Cmd>5</Cmd>
          <Adp>1005</Adp>
          <Ado>2048</Ado>
          <Data>0010000426000100</Data>
          <Cnt>1</Cnt>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>IP</Transition>
          <Comment><![CDATA[set sm 1 (mailbox in)]]></Comment>
          <Cmd>5</Cmd>
          <Adp>1005</Adp>
          <Ado>2056</Ado>
          <Data>0014000422000100</Data>
          <Cnt>1</Cnt>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>IB</Transition>
          <Comment><![CDATA[set sm 0 (bootstrap out)]]></Comment>
          <Cmd>5</Cmd>
          <Adp>1005</Adp>
          <Ado>2048</Ado>
          <Data>0010000426000100</Data>
          <Cnt>1</Cnt>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>IB</Transition>
          <Comment><![CDATA[set sm 1 (bootstrap in)]]></Comment>
          <Cmd>5</Cmd>
          <Adp>1005</Adp>
          <Ado>2056</Ado>
          <Data>0014000422000100</Data>
          <Cnt>1</Cnt>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>PS</Transition>
          <Comment><![CDATA[set DC start time]]></Comment>
          <Requires>cycle</Requires>
          <Cmd>5</Cmd>
          <Adp>1005</Adp>
          <Ado>2448</Ado>
          <Data>0000000000000000</Data>
          <Cnt>1</Cnt>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>PS</Transition>
          <Comment><![CDATA[set DC activation]]></Comment>
          <Requires>cycle</Requires>
          <Cmd>5</Cmd>
          <Adp>1005</Adp>
          <Ado>2432</Ado>
          <Data>0001</Data>
          <Cnt>1</Cnt>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>PS</Transition>
          <Comment><![CDATA[set DC latch cfg]]></Comment>
          <Requires>cycle</Requires>
          <Cmd>5</Cmd>
          <Adp>1005</Adp>
          <Ado>2472</Ado>
          <Data>0000</Data>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>IP</Transition>
          <Transition>PP</Transition>
          <Comment><![CDATA[clear DC activation]]></Comment>
          <Requires>cycle</Requires>
          <Cmd>5</Cmd>
          <Adp>1005</Adp>
          <Ado>2432</Ado>
          <Data>0000</Data>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>SP</Transition>
          <Transition>OP</Transition>
          <Comment><![CDATA[set device state to PREOP]]></Comment>
          <Requires>cycle</Requires>
          <Cmd>5</Cmd>
          <Adp>1005</Adp>
          <Ado>288</Ado>
          <Data>1200</Data>
          <Retries>300</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>IP</Transition>
          <Transition>SP</Transition>
          <Transition>SI</Transition>
          <Transition>OP</Transition>
          <Transition>OI</Transition>
          <Comment><![CDATA[clear sms]]></Comment>
          <Cmd>5</Cmd>
          <Adp>1005</Adp>
          <Ado>2064</Ado>
          <Data>00000000000000000000000000000000</Data>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>PS</Transition>
          <Comment><![CDATA[set sm 2 (outputs)]]></Comment>
          <Cmd>5</Cmd>
          <Adp>1005</Adp>
          <Ado>2064</Ado>
          <Data>00182e0064000100</Data>
          <Cnt>1</Cnt>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>PS</Transition>
          <Comment><![CDATA[set sm 3 (inputs)]]></Comment>
          <Cmd>5</Cmd>
          <Adp>1005</Adp>
          <Ado>2072</Ado>
          <Data>0024340020000100</Data>
          <Cnt>1</Cnt>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>PS</Transition>
          <Comment><![CDATA[set fmmu 0 (outputs)]]></Comment>
          <Cmd>5</Cmd>
          <Adp>1005</Adp>
          <Ado>1536</Ado>
          <Data>001800012e0000070018000201000000</Data>
          <Cnt>1</Cnt>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>PS</Transition>
          <Comment><![CDATA[set fmmu 1 (inputs)]]></Comment>
          <Cmd>5</Cmd>
          <Adp>1005</Adp>
          <Ado>1552</Ado>
          <Data>00180001340000070024000101000000</Data>
          <Cnt>1</Cnt>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>IP</Transition>
          <Transition>IB</Transition>
          <Comment><![CDATA[set fmmu 2 (mailbox state)]]></Comment>
          <Cmd>5</Cmd>
          <Adp>1005</Adp>
          <Ado>1568</Ado>
          <Data>00000009010001010d08000101000000</Data>
          <Cnt>1</Cnt>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>OS</Transition>
          <Comment><![CDATA[set device state to SAFEOP]]></Comment>
          <Requires>cycle</Requires>
          <Cmd>5</Cmd>
          <Adp>1005</Adp>
          <Ado>288</Ado>
          <Data>0400</Data>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>SP</Transition>
          <Transition>SI</Transition>
          <Transition>OP</Transition>
          <Transition>OI</Transition>
          <Comment><![CDATA[clear fmmu 0]]></Comment>
          <Cmd>5</Cmd>
          <Adp>1005</Adp>
          <Ado>1536</Ado>
          <Data>00000000000000000000000000000000</Data>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>SP</Transition>
          <Transition>SI</Transition>
          <Transition>OP</Transition>
          <Transition>OI</Transition>
          <Comment><![CDATA[clear fmmu 1]]></Comment>
          <Cmd>5</Cmd>
          <Adp>1005</Adp>
          <Ado>1552</Ado>
          <Data>00000000000000000000000000000000</Data>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>PI</Transition>
          <Transition>BI</Transition>
          <Transition>SI</Transition>
          <Transition>OI</Transition>
          <Comment><![CDATA[clear fmmu 2]]></Comment>
          <Cmd>5</Cmd>
          <Adp>1005</Adp>
          <Ado>1568</Ado>
          <Data>00000000000000000000000000000000</Data>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>SP</Transition>
          <Transition>OP</Transition>
          <Comment><![CDATA[clear DC activation]]></Comment>
          <Requires>cycle</Requires>
          <Cmd>5</Cmd>
          <Adp>1005</Adp>
          <Ado>2432</Ado>
          <Data>0000</Data>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>SP</Transition>
          <Transition>OP</Transition>
          <Comment><![CDATA[check device state for PREOP]]></Comment>
          <Requires>cycle</Requires>
          <Cmd>4</Cmd>
          <Adp>1005</Adp>
          <Ado>304</Ado>
          <Data>000000000000</Data>
          <Retries>3</Retries>
          <Validate>
            <Data>020000000000</Data>
            <DataMask>0f0000000000</DataMask>
            <Timeout>20000</Timeout>
          </Validate>
        </InitCmd>
        <InitCmd>
          <Transition>IP</Transition>
          <Transition>IB</Transition>
          <Comment><![CDATA[assign EEPROM to PDI]]></Comment>
          <Requires>cycle</Requires>
          <Cmd>5</Cmd>
          <Adp>1005</Adp>
          <Ado>1280</Ado>
          <Data>01</Data>
          <Cnt>1</Cnt>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>II</Transition>
          <Comment><![CDATA[assign EEPROM back to ECAT]]></Comment>
          <Requires>cycle</Requires>
          <Cmd>2</Cmd>
          <Adp>65532</Adp>
          <Ado>1280</Ado>
          <Data>00</Data>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>IP</Transition>
          <Comment><![CDATA[set device state to PREOP]]></Comment>
          <Requires>cycle</Requires>
          <Cmd>5</Cmd>
          <Adp>1005</Adp>
          <Ado>288</Ado>
          <Data>1200</Data>
          <Cnt>1</Cnt>
          <Retries>300</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>IP</Transition>
          <Comment><![CDATA[check device state for PREOP]]></Comment>
          <Requires>cycle</Requires>
          <Cmd>4</Cmd>
          <Adp>1005</Adp>
          <Ado>304</Ado>
          <Data>000000000000</Data>
          <Cnt>1</Cnt>
          <Retries>3</Retries>
          <Validate>
            <Data>020000000000</Data>
            <DataMask>1f0000000000</DataMask>
            <Timeout>10000</Timeout>
          </Validate>
        </InitCmd>
        <InitCmd>
          <Transition>IP</Transition>
          <Transition>BI</Transition>
          <Comment><![CDATA[assign EEPROM back to ECAT]]></Comment>
          <Requires>cycle</Requires>
          <Cmd>2</Cmd>
          <Adp>65532</Adp>
          <Ado>1280</Ado>
          <Data>00</Data>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>IB</Transition>
          <Comment><![CDATA[set device state to BOOT]]></Comment>
          <Requires>cycle</Requires>
          <Cmd>5</Cmd>
          <Adp>1005</Adp>
          <Ado>288</Ado>
          <Data>1300</Data>
          <Cnt>1</Cnt>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>IB</Transition>
          <Comment><![CDATA[check device state for BOOT]]></Comment>
          <Requires>cycle</Requires>
          <Cmd>4</Cmd>
          <Adp>1005</Adp>
          <Ado>304</Ado>
          <Data>000000000000</Data>
          <Cnt>1</Cnt>
          <Retries>3</Retries>
          <Validate>
            <Data>030000000000</Data>
            <DataMask>1f0000000000</DataMask>
            <Timeout>10000</Timeout>
          </Validate>
        </InitCmd>
        <InitCmd>
          <Transition>PS</Transition>
          <Comment><![CDATA[set device state to SAFEOP]]></Comment>
          <Requires>cycle</Requires>
          <Cmd>5</Cmd>
          <Adp>1005</Adp>
          <Ado>288</Ado>
          <Data>0400</Data>
          <Cnt>1</Cnt>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>PS</Transition>
          <Comment><![CDATA[check device state for SAFEOP]]></Comment>
          <Requires>cycle</Requires>
          <Cmd>4</Cmd>
          <Adp>1005</Adp>
          <Ado>304</Ado>
          <Data>000000000000</Data>
          <Cnt>1</Cnt>
          <Retries>3</Retries>
          <Validate>
            <Data>040000000000</Data>
            <DataMask>1f0000000000</DataMask>
            <Timeout>10000</Timeout>
          </Validate>
        </InitCmd>
        <InitCmd>
          <Transition>OS</Transition>
          <Comment><![CDATA[check device state for SAFEOP]]></Comment>
          <Requires>cycle</Requires>
          <Cmd>4</Cmd>
          <Adp>1005</Adp>
          <Ado>304</Ado>
          <Data>000000000000</Data>
          <Retries>3</Retries>
          <Validate>
            <Data>040000000000</Data>
            <DataMask>0f0000000000</DataMask>
            <Timeout>200</Timeout>
          </Validate>
        </InitCmd>
        <InitCmd>
          <Transition>SO</Transition>
          <Comment><![CDATA[set device state to OP]]></Comment>
          <Requires>cycle</Requires>
          <Cmd>5</Cmd>
          <Adp>1005</Adp>
          <Ado>288</Ado>
          <Data>0800</Data>
          <Cnt>1</Cnt>
          <Retries>3</Retries>
        </InitCmd>
        <InitCmd>
          <Transition>SO</Transition>
          <Comment><![CDATA[check device state for OP]]></Comment>
          <Requires>cycle</Requires>
          <Cmd>4</Cmd>
          <Adp>1005</Adp>
          <Ado>304</Ado>
          <Data>000000000000</Data>
          <Cnt>1</Cnt>
          <Retries>3</Retries>
          <Validate>
            <Data>080000000000</Data>
            <DataMask>1f0000000000</DataMask>
            <Timeout>10000</Timeout>
          </Validate>
        </InitCmd>
      </InitCmds>
      <PreviousPort Selected="true">
        <Port>B</Port>
        <PhysAddr>1004</PhysAddr>
      </PreviousPort>
      <SyncUnits>
        <SyncUnit>Profinet</SyncUnit>
      </SyncUnits>
      <DC>
        <PotentialReferenceClock>false</PotentialReferenceClock>
        <ReferenceClock>true</ReferenceClock>
        <CycleTime0>4000000</CycleTime0>
        <CycleTime1>0</CycleTime1>
        <ShiftTime>0</ShiftTime>
      </DC>
      <Settings>
        <StateMachine>
          <AutoRestoreStates>true</AutoRestoreStates>
          <WaitForWcStateOkay>true</WaitForWcStateOkay>
          <ReInitAfterCommError>true</ReInitAfterCommError>
          <LogCommChanges>true</LogCommChanges>
          <FinalState>OP</FinalState>
          <Timeout>
            <I2P>10000</I2P>
            <P2S2O>10000</P2S2O>
            <Back2PI>20000</Back2PI>
            <O2S>200</O2S>
          </Timeout>
          <CheckVendorId>EQ</CheckVendorId>
          <CheckProductCode>EQ</CheckProductCode>
          <CheckRevisionNo/>
          <CheckSerialNo/>
        </StateMachine>
        <Watchdog>
          <Divider0400 Enabled="false">2498</Divider0400>
          <PdiTime0410 Enabled="false">1000</PdiTime0410>
          <SmTime0420 Enabled="false">1000</SmTime0420>
        </Watchdog>
        <PdoSettings>
          <PdoAssign>1</PdoAssign>
          <PdoConfig>1</PdoConfig>
        </PdoSettings>
      </Settings>
      <AssociatedDevice>Device 4 (EL6631)</AssociatedDevice>
    </Slave>
    <AmsAddress>
      <AmsNetId>192.168.178.253.3.1</AmsNetId>
      <AmsPort>1005</AmsPort>
    </AmsAddress>
  </EtherCAT>
</TreeItem>
/*****************************************************************************
 *
 *  $Id$
 *
 *  Copyright (C) 2007-2009  Florian Pose, Ingenieurgemeinschaft IgH
 *
 *  This file is part of the IgH EtherCAT Master.
 *
 *  The IgH EtherCAT Master is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License version 2, as
 *  published by the Free Software Foundation.
 *
 *  The IgH EtherCAT Master is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
 *  Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with the IgH EtherCAT Master; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  ---
 *
 *  The license mentioned above concerns the source code only. Using the
 *  EtherCAT technology and brand is only permitted in compliance with the
 *  industrial property and similar rights of Beckhoff Automation GmbH.
 *
 ****************************************************************************/

#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

/****************************************************************************/

#include "ecrt.h"

/****************************************************************************/

// Application parameters
#define FREQUENCY 100
#define PRIORITY 1

// Optional features
#define CONFIGURE_PDOS  1
#define SDO_ACCESS      0

/****************************************************************************/

// EtherCAT
static ec_master_t *master = NULL;
static ec_master_state_t master_state = {};

static ec_domain_t *domain1 = NULL;
static ec_domain_state_t domain1_state = {};

static ec_slave_config_t *sc = NULL;
static ec_slave_config_state_t sc_state = {};

// Timer
static unsigned int sig_alarms = 0;
static unsigned int user_alarms = 0;

/****************************************************************************/

// process data
static uint8_t *domain1_pd = NULL;

#define BusCouplerPos  0, 0
#define ProfiNetPos    0, 4

#define Beckhoff_EK1100 0x00000002, 0x044c2c52
#define ProfiNetTerm 0x00000002, 0x19e73052

// Configuration from XML file

const char *pn_config_master_0xf800 = 
"0f000100656c363633312d706e636f6e74726f6c6c657200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002500200160ea48eec0a80101ffffff00c0a80101c0a8b2fd0501ffff000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
const char *pn_config_master_0xf820 = "0200c0a8b2fd01012203";
const char *pn_config_master_0x8000 = 
"3a00636966787265706e7300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003014c0200000810c0a8b2fd050120000100c0a80109ffffff00c0a8010100c001000000010001000100050008000200000000000000000000000000000000000000200004000100030000000000020000000000000000000000000000000000000000000000000000000000000000000000";
const char *pn_config_slave_0x8002 = 
"7800000000000000008001110000011100000000000000000000000002000503240980003200e803000000000000000000000000000000000000000000000000000000000180011100000211000000000000000000000100040054015c000000000000000000010001000000000000000000000000000000000000000280011100000311000000000000000000000200040054015c000000000000000000000000000000000000000000000000000000000000000100011100000011000000000000000000000300080000000000000000000000000000000000000000000000000000000000000000000000010001001100000010000000000000001400000004000800000000000000000000000000000000000000000000000000000000000000000000000200010011000000100000000000000014000000050008000000000000000000000000000000000000000000000000000000000000000000000005000100100000000f0000001400000000000000060008000000000000000000000000000000000000000000000000000000000000000000000006000100100000000f000000140000000000000007000800000000000000000000000000000000000000000000000000000000000000";

// offsets for PDO entries

static unsigned int off_in0;
static unsigned int off_out0;

const static ec_pdo_entry_reg_t domain1_regs[] = {
        {ProfiNetPos, ProfiNetTerm, 0x6001, 1, &off_in0},
        {ProfiNetPos, ProfiNetTerm, 0x7001, 1, &off_out0},
        {}
};

static unsigned int counter = 0;
static unsigned int blink = 0;

/*****************************************************************************/

#if CONFIGURE_PDOS

/* Master 0, Slave 4, "EL6631"
 * Vendor ID:       0x00000002
 * Product code:    0x19e73052
 * Revision number: 0x00120000
 */
ec_pdo_entry_info_t profinet_pdo_entries[] = {
        // TxPDO: cifxrepns_In
        {0x6000, 0x01, 16},             // cifxrepns_BoxState
        {0x6000, 0x02, 16},             // cifxrepns_BoxDiag
        {0x6001, 0x01, 160},    // Input Data Submodule 1
        {0x6001, 0x02, 160},    // Input Data Submodule 2

        // RxPDO: cifxrepns_Out
        {0x7000, 0x01, 16},             // cifxrepns_BoxCtrl
        {0x7001, 0x01, 160},    // Output Data Submodule 1
        {0x7001, 0x02, 160},    // Output Data Submodule 2

        // TxPDO: PnIoProtocolState
        {0xf100, 0x01, 16},             // DevState
        {0xf100, 0x02, 16},             // PnIoError
        {0xf100, 0x03, 16},             // PnIoDiag

        // TxPDO: ECatState
        {0xf101, 0x01, 16},             // ECatState

        // RxPdo: PnIoProtocolCtrl
        {0xf200, 0x01, 16},             // DevCtrl

        // RxPdo: ECatCtrl
        {0xf201, 0x01, 16},             // ECatCtrl
        {}
};

ec_pdo_info_t profinet_pdos[] = {
        {0x1a00, 4, profinet_pdo_entries + 0},  // TxPDO: cifxrepns_In
        {0x1bfe, 3, profinet_pdo_entries + 7},  // TxPDO: PnIoProtocolState
        {0x1bff, 1, profinet_pdo_entries + 10}, // TxPDO: ECatState
        {0x1600, 3, profinet_pdo_entries + 4},  // RxPDO: cifxrepns_Out
        {0x17fe, 1, profinet_pdo_entries + 11}, // RxPDO: PnIoProtocolCtrl
        {0x17ff, 1, profinet_pdo_entries + 12}, // RxPDO: ECatCtrl
};

ec_sync_info_t profinet_syncs[] = {
        {0, EC_DIR_OUTPUT, 0, NULL, EC_WD_DISABLE},
        {1, EC_DIR_INPUT, 0, NULL, EC_WD_DISABLE},
        {2, EC_DIR_OUTPUT, 3, profinet_pdos + 3, EC_WD_ENABLE},
        {3, EC_DIR_INPUT, 3, profinet_pdos + 0, EC_WD_DISABLE},
        {0xff}
};

#endif

/*****************************************************************************/

#if SDO_ACCESS
static ec_sdo_request_t *sdo;
#endif

/*****************************************************************************/

/* Helper funktion, buffer has be allocated and big enough */
unsigned int strToCharArray(const char *str,unsigned char *buffer)
{
        unsigned int i;
        unsigned char byteVal;
        //        const char *str = pb_config;
        //        unsigned char cf_data[256];
        size_t size = strlen(str);

        for (i = 0; i<  size; i++) {
                sscanf(str, "%2X",&byteVal);
                str += 2;
                buffer[i] = (uint8_t) byteVal;
        }
        return (size / 2);
}

void check_domain1_state(void)
{
        ec_domain_state_t ds;

        ecrt_domain_state(domain1, &ds);

        if (ds.working_counter != domain1_state.working_counter)
                printf("Domain1: WC %u.\n", ds.working_counter);
        if (ds.wc_state != domain1_state.wc_state)
                printf("Domain1: State %u.\n", ds.wc_state);

        domain1_state = ds;
}

/*****************************************************************************/

void check_master_state(void)
{
        ec_master_state_t ms;

        ecrt_master_state(master, &ms);

        if (ms.slaves_responding != master_state.slaves_responding)
                printf("%u slave(s).\n", ms.slaves_responding);
        if (ms.al_states != master_state.al_states)
                printf("AL states: 0x%02X.\n", ms.al_states);
        if (ms.link_up != master_state.link_up)
                printf("Link is %s.\n", ms.link_up ? "up" : "down");

        master_state = ms;
}

/*****************************************************************************/

void check_slave_config_states(void)
{
        ec_slave_config_state_t s;

        ecrt_slave_config_state(sc, &s);

        if (s.al_state != sc_state.al_state)
                printf("DigIn: State 0x%02X.\n", s.al_state);
        if (s.online != sc_state.online)
                printf("DigIn: %s.\n", s.online ? "online" : "offline");
        if (s.operational != sc_state.operational)
                printf("DigIn: %soperational.\n",
                                s.operational ? "" : "Not ");

        sc_state = s;
}

/*****************************************************************************/

#if SDO_ACCESS
void read_sdo(void)
{
        switch (ecrt_sdo_request_state(sdo)) {
                case EC_REQUEST_UNUSED: // request was not used yet
                        ecrt_sdo_request_read(sdo); // trigger first read
                        break;
                case EC_REQUEST_BUSY:
                        fprintf(stderr, "Still busy...\n");
                        break;
                case EC_REQUEST_SUCCESS:
                        fprintf(stderr, "SDO value: 0x%04X\n",
                                        
EC_READ_U16(ecrt_sdo_request_data(sdo)));
                        ecrt_sdo_request_read(sdo); // trigger next read
                        break;
                case EC_REQUEST_ERROR:
                        fprintf(stderr, "Failed to read SDO!\n");
                        ecrt_sdo_request_read(sdo); // retry reading
                        break;
        }
}
#endif

/****************************************************************************/

void cyclic_task()
{
        // receive process data
        ecrt_master_receive(master);
        ecrt_domain_process(domain1);

        // check process data state (optional)
        check_domain1_state();

        if (counter) {
                counter--;
        } else { // do this at 1 Hz
                counter = FREQUENCY;

                // check for master state (optional)
                check_master_state();

                // check for islave configuration state(s) (optional)
                check_slave_config_states();

#if SDO_ACCESS
                // read process data SDO
                read_sdo();
#endif

        }

        printf("0:%x\n1:%x\n", EC_READ_U8(domain1_pd + off_in0), 
EC_READ_U8(domain1_pd + off_in0 + 1));
        EC_WRITE_U8(domain1_pd + off_out0, 0x06);


        // send process data
        ecrt_domain_queue(domain1);
        ecrt_master_send(master);
}

/****************************************************************************/

void signal_handler(int signum) {
        switch (signum) {
                case SIGALRM:
                        sig_alarms++;
                        break;
        }
}

/****************************************************************************/

int main(int argc, char **argv) {

        int cnt; 
        unsigned char pb_config_buf[1024];
        struct sigaction sa;
        struct itimerval tv;

        master = ecrt_request_master(0);
        if (!master)
                return -1;

        domain1 = ecrt_master_create_domain(master);
        if (!domain1)
                return -1;


        /* ELK1100 BusCoupler */
        sc = ecrt_master_slave_config(master, BusCouplerPos, Beckhoff_EK1100);
        if (!sc) {
                fprintf(stderr, "Failed to get EK1100 configuration of 
coupler.\n");
                return -1;
        }


        /* EL6631 Master */
        if (!(sc = ecrt_master_slave_config(master, ProfiNetPos, 
ProfiNetTerm))) {
                fprintf(stderr, "Failed to get slave configuration.\n");
                return -1;
        }

        cnt = strToCharArray(pn_config_master_0xf800, pb_config_buf);
        ecrt_slave_config_complete_sdo(sc, 0xf800, pb_config_buf, cnt);

        cnt = strToCharArray(pn_config_master_0xf820, pb_config_buf);
        ecrt_slave_config_complete_sdo(sc, 0xf820, pb_config_buf, cnt);

        cnt = strToCharArray(pn_config_master_0x8000 ,pb_config_buf);
        ecrt_slave_config_complete_sdo(sc, 0x8000, pb_config_buf, cnt); 

        cnt = strToCharArray(pn_config_slave_0x8002, pb_config_buf);
        ecrt_slave_config_complete_sdo(sc, 0x8002, pb_config_buf, cnt);

        sleep(3); // Workaround from mailinglist: 
permalink.gmane.org/gmane.network.etherlab.devel/64


        printf("Configuring PDOs...\n");
        if (ecrt_slave_config_pdos(sc, EC_END, profinet_syncs)) {
                fprintf(stderr, "Failed to configure PDOs.\n");
                return -1;
        }

        if (ecrt_domain_reg_pdo_entry_list(domain1, domain1_regs)) {
                fprintf(stderr, "PDO entry registration failed!\n");
                return -1;
        }


        /* Active Master */
        printf("Activating master... ");
        if (ecrt_master_activate(master)){
                fprintf(stderr, "activation failed.\n");
                return -1;
        }
        printf("ok!\n");

        if (!(domain1_pd = ecrt_domain_data(domain1))) {
                fprintf(stderr, "Domain data initialization failed.\n");
                return -1;
        }
        printf("Domain data registered ok.\n");

#if PRIORITY
        pid_t pid = getpid();
        if (setpriority(PRIO_PROCESS, pid, -19))
                fprintf(stderr, "Warning: Failed to set priority: %s\n",
                                strerror(errno));
#endif

        sa.sa_handler = signal_handler;
        sigemptyset(&sa.sa_mask);
        sa.sa_flags = 0;
        if (sigaction(SIGALRM, &sa, 0)) {
                fprintf(stderr, "Failed to install signal handler!\n");
                return -1;
        }

        printf("Starting timer...\n");
        tv.it_interval.tv_sec = 0;
        tv.it_interval.tv_usec = 1000000 / FREQUENCY;
        tv.it_value.tv_sec = 0;
        tv.it_value.tv_usec = 1000;
        if (setitimer(ITIMER_REAL, &tv, NULL)) {
                fprintf(stderr, "Failed to start timer: %s\n", strerror(errno));
                return 1;
        }

        printf("Started.\n");
        while (1) {
                pause();

#if 0
                struct timeval t;
                gettimeofday(&t, NULL);
                printf("%u.%06u\n", t.tv_sec, t.tv_usec);
#endif

                while (sig_alarms != user_alarms) {
                        cyclic_task();
                        user_alarms++;
                }
        }

        return 0;
}

/****************************************************************************/
/* Output of program */
Failed to configure SDO: Invalid argument
Failed to configure SDO: Invalid argument
Failed to configure SDO: Invalid argument
Failed to configure SDO: Invalid argument
Configuring PDOs...
Activating master... ok!
Domain data registered ok.
Starting timer...
Started.
6 slave(s).
AL states: 0x02.
Link is up.
DigIn: State 0x02.
DigIn: online.
_______________________________________________
etherlab-users mailing list
etherlab-users@etherlab.org
http://lists.etherlab.org/mailman/listinfo/etherlab-users

Reply via email to