MDP -- Modular devices
The Modular Device Profile (MDP, ETG.5001) is the standard EtherCAT protocol for describing modular devices.
The master handles module detection and PDO configuration for MDP devices automatically. In the vast majority of scenarios, users do not need to work with MDP directly.
The SDK supports reconfiguration self-repair after an MDP module hot-plug.
In production environments, however, hot-plugging MDP modules is almost never a real scenario.
The module configuration of an EtherCAT slave is fixed once the device is powered on and does not change at runtime.
DENI is recommended for deploying MDP modular devices;
if automatic configuration is required, import the corresponding ESI file with esi_dll_load_directory() first, then let the master run its normal INIT → PreOp → OP state chain; the automatic orchestration completes on the first PreOp→SafeOp transition.
The object dictionary addresses of the modules in an MDP device are laid out at a fixed distance: the distance comes from the slave OD 0xF000:01 (ModuleIndexDistance), and the object dictionary address of each slot = distance × slot number.
MDP detection
BOOL GetSlaveHasMDP(uint16_t master, uint16_t slave);
Checks whether the slave is an MDP modular device. A return of TRUE means the slave declares MDP modules.
Parameters:
master(uint16_t) — master indexslave(uint16_t) — slave index
Returns:
BOOL— TRUE = the slave declares MDP modules; FALSE = not an MDP device or the query failed
Example:
if (!GetSlaveHasMDP(master, slave))
printf("该从站不是 MDP 设备\n"); /* This slave is not an MDP device */
Module structures
/* ethercat_advanced.h */
typedef struct {
uint16_t slot_index; /* slot number */
uint16_t module_profile; /* module profile (190/195/290/790/6900) */
uint16_t module_ident; /* module ident */
uint16_t pdo_input_offset; /* PDO input offset */
uint16_t pdo_input_size; /* PDO input size */
uint16_t pdo_output_offset; /* PDO output offset */
uint16_t pdo_output_size; /* PDO output size */
} mdp_module_t;
typedef struct {
int count;
mdp_module_t* modules;
} mdp_list_t;
Module discovery (ethercat_advanced.h)
/* ethercat_advanced.h */
mdp_list_t* mdp_discover(uint16_t master, uint16_t slave);
void mdp_free(mdp_list_t* list);
mdp_discover()reads the slave's detected module list over CoE (object dictionary0xF050, reflecting the modules physically inserted)- The returned value must be released with
mdp_free(); returnsNULLon failure
Parameters:
master(uint16_t) — master indexslave(uint16_t) — slave indexlist(mdp_list_t*) — the list returned bymdp_discover()(used bymdp_free()only)
Returns:
mdp_discover()→mdp_list_t*— detected module list (count+modulesarray); returnsNULLon failuremdp_free()→void— releases the list
mdp_discover() fills only slot_index / module_profile / module_ident and leaves the four pdo_* fields at 0 (a PDO offset can only be determined together with the PDO assignment table). Authoritative for sizes = GetPdoAssignmentSizes() / GetPdoMappingSize() (slave/startup.h); authoritative for offsets = GetPdoOffsets().
Example:
mdp_list_t* modules = mdp_discover(master, 1);
if (modules) {
for (int i = 0; i < modules->count; i++) {
mdp_module_t* m = &modules->modules[i];
printf("Slot %d: Profile=%d Ident=0x%04X\n",
m->slot_index, m->module_profile, m->module_ident);
}
mdp_free(modules);
}
Direct module ident list reads (slave/mdp.h)
slave/mdp.h provides lightweight static __inline wrappers that read the ETG.5001 standard module ident arrays (uint32_t module ident codes) directly, bypassing the SDO cache path. The runtime library implements them.
/* slave/mdp.h — call directly with the master/slave indices */
int mdp_get_config_module_list(uint16_t master, uint16_t slave,
uint32_t* out_idents, int max);
int mdp_get_detected_module_list(uint16_t master, uint16_t slave,
uint32_t* out_idents, int max);
int mdp_check_module_match(uint16_t master, uint16_t slave,
int* out_first_mismatch);
mdp_get_config_module_list()— reads0xF030ConfigModuleIdent[] (configured module idents). Returns the number written;<=0means failure.mdp_get_detected_module_list()— reads0xF050DetectedModuleIdent[] (detected module idents). Returns the number written;<=0means failure.mdp_check_module_match()— compares0xF030with0xF050. Returns1= exact match,0= mismatch (out_first_mismatchreceives the 0-based slot),<0= not supported / failure.
Parameters:
master(uint16_t) — master indexslave(uint16_t) — slave indexout_idents(uint32_t*) — output array, filled with the module ident codesmax(int) — capacity ofout_identsout_first_mismatch(int*) — output, first mismatching slot (0-based,mdp_check_module_match()only)
Returns:
mdp_get_config_module_list()/mdp_get_detected_module_list()→int— number written;<= 0means failuremdp_check_module_match()→int— 1 = exact match; 0 = mismatch;< 0= not supported / failure (it must not be used as "match" or "mismatch")
These wrappers are plain C functions called directly with the master/slave indices; there is no module handle parameter. When the SDK is not ready, the list functions return 0 and the check function returns <0.
MDP automatic orchestration (ETG.5001)
Automatic configuration of MDP modular devices = read 0xF050 (detected) → match the ESI <Modules> → generate the 0xF030 write sequence + the master PDO Assignment + module SdoInit. This orchestration runs automatically while the master advances its state chain (ESI startup parameters are applied before the first entry into PreOp; module orchestration runs on the first PreOp→SafeOp transition), so the SDK usually needs no orchestration code at all; slaves that already have an explicit / DENI plan are not reconfigured automatically.
Orchestration timing
As the master advances its state chain it reads the 0xF050 detected module list automatically and refreshes the module configuration; the application does not need to call an extra orchestration entry point. To query the result, use the public mdp_discover() / mdp_get_detected_module_list() / mdp_check_module_match() described above.
Deploying with a DENI file is recommended (one line loads the whole set of ESI/PDO/module configuration). For automatic configuration at runtime, hand the corresponding ESI to the SDK during the constructor/configuration phase with esi_dll_load_file() / esi_dll_load_directory(), then let the master run its normal INIT → PreOp → OP state chain: SetStateSequence() binds the ESI to the slaves while building the chain, and the automatic orchestration completes on the first PreOp→SafeOp transition.
PDO layout (slave/startup.h)
PDO sizes and offsets of modules do not come from mdp_discover(); use the PDO layout interfaces in slave/startup.h (direction: 0 = RxPDO, 1 = TxPDO):
int GetPdoAssignmentSizes(uint16_t master_index, uint16_t slave_index,
int direction, int32_t* out_byte_sizes, int max_count);
int GetPdoOffsets(uint16_t master_index, uint16_t slave_index, int direction,
const uint16_t* obj_indices, int32_t* out_offsets, int n_targets);
int GetPdoMappingSize(uint16_t master_index, uint16_t slave_index, uint16_t pdo_mapping_index);
Parameters:
master_index/slave_index(uint16_t) — master index / slave indexdirection(int) — 0 = RxPDO, 1 = TxPDOout_byte_sizes(int32_t*) — output array, filled with the byte size of each PDO in the assignment (a failed item is 0 = unknown)max_count(int) — capacity of the output arrayobj_indices(const uint16_t*) — array of object indices whose offsets are queriedout_offsets(int32_t*) — output array, filled with the byte offset of each object in the PDOn_targets(int) — number of objectspdo_mapping_index(uint16_t) — PDO mapping object index
Returns:
GetPdoAssignmentSizes()→int— number of entries written (>= 0); invalid arguments or an enumeration failure return -1GetPdoOffsets()→int— number of offsets produced (>= 0); entries that are not found or fail to read are -1; invalid arguments return -1GetPdoMappingSize()→int— total byte size of the mapping object; a failed read returns 0 (unknown)
Full example
#include "ethercat.h"
#include "ethercat_advanced.h"
#include "slave/mdp.h"
#include <stdio.h>
int main(void) {
uint16_t master = Initialize();
if (master == 0 || master == (uint16_t)-1) return 1;
uint16_t slaves_found = SetNetwork(master, "\\Device\\NPF_{GUID}", "");
if (slaves_found == 0 || slaves_found == (uint16_t)-1) return 1;
if (!SetStateSequence(master, EC_STATE_PRE_OP, 5000)) return 1;
uint16_t slave = 1;
if (!GetSlaveHasMDP(master, slave)) {
printf("该从站不是 MDP 设备\n"); /* This slave is not an MDP device */
Dispose(master);
return 1;
}
/* read the configured module ident list (0xF030) */
uint32_t configured[32];
int configured_count = mdp_get_config_module_list(master, slave, configured, 32);
if (configured_count <= 0)
printf("读取已配置模块失败\n"); /* Failed to read the configured modules */
else
printf("已配置模块 %d 个\n", configured_count); /* %d modules configured */
/* detected modules (slot/identity; get the PDO offsets and sizes separately with GetPdoOffsets/GetPdoAssignmentSizes) */
mdp_list_t* modules = mdp_discover(master, slave);
if (modules) {
for (int i = 0; i < modules->count; i++) {
mdp_module_t* m = &modules->modules[i];
printf("Slot %d: Profile=%d Ident=0x%04X\n",
m->slot_index, m->module_profile, m->module_ident);
}
mdp_free(modules);
} else {
printf("读取已检测模块失败\n"); /* Failed to read the detected modules */
}
/* module ident list check: configured (0xF030) vs detected (0xF050) */
int first_mismatch = -1;
int match = mdp_check_module_match(master, slave, &first_mismatch);
if (match == 1) {
printf("模块列表匹配\n"); /* Module list matches */
} else if (match == 0) {
printf("模块不匹配, 首个差异槽位: %d\n", first_mismatch); /* Module mismatch, first differing slot: %d */
} else {
printf("模块校验不可用或失败 (rc=%d)\n", match); /* must not be used as "match" or "mismatch" */
}
Dispose(master);
return 0;
}
Failures and handling
| Symptom | Cause | Handling |
|---|---|---|
| The detected module list is empty, or shorter than the modules actually inserted | Modules were not detected: not scanned, a module is absent, or the list read failed | Read the detected list again; if it is still empty, deploy through DENI, or import the corresponding ESI and run the startup sequence again |
| The configured and detected lists do not match | The modules actually present differ from the current configuration | Regenerate the configuration for the modules actually present (DENI, or import the ESI and run the startup sequence again); do not keep the old configuration |
| The module PDO layout cannot be obtained | The slave is not in OP, CoE is not accessible, or no module was detected | Confirm the slave is in OP and CoE is accessible, then retry; if it still fails, use explicit offsets |
| PDO mismatch: binding or exchange reports errors, offsets are misaligned | The module PDO assignment does not match the combination of modules actually present | Reconfigure and redeploy the modules, then read the PDO sizes and offsets once more to verify |