PROFIBUS Slave Stack

Profibus Slave stack Overview

This document describes the PROFIBUS DP Slave stack implementation provided by the RAPIDSEA protocol library. The stack supports PROFIBUS DPV0 cyclic exchange, DPV1 acyclic services, extended diagnostics, alarms, and FDL frame handling over an RS-485 serial transport.

The implementation is organized into an application-facing stack interface, a PROFIBUS DP layer, and a Fieldbus Data Link (FDL) layer. It is designed for embedded systems that require a compact protocol implementation with deterministic processing and low RAM usage.

API Interface

API Functions

Function

Description

rs_pbs_slave_open

Initialize and open a PROFIBUS slave instance using the supplied configuration. Returns a handle for subsequent calls.

rs_pbs_slave_process

Execute the periodic PROFIBUS slave stack task. Processes FDL frames, DP state transitions, cyclic and acyclic services, alarms, and timeouts.

rs_pbs_slave_get_version_info

Get the major, minor, and build versions of the PROFIBUS slave stack.

rs_pbs_slave_close

Close the PROFIBUS slave instance and release the serial port and associated resources.

rs_pbs_slave_identifier_based_diag_send

Queue an identifier-based diagnostic update for the specified module.

rs_pbs_slave_channel_based_diag_send

Queue a channel-based diagnostic update for a specific module and channel.

rs_pbs_slave_device_related_diag_send

Queue a device-related diagnostic update using the provided diagnostic payload.

rs_pbs_slave_alarm_send

Send a PROFIBUS DPV1 alarm to the master with specified type, slot, state, sequence, and payload.

rs_pbs_slave_status_msg_send

Send a PROFIBUS DPV1 status message to the master with specified type, slot, specifier, and payload.

Callback Functions

The application must implement callback handlers for asynchronous events and data exchange.

Callback Functions

Callback

Invocation

rcb_pbs_slave_cyclic_set_output

Called when cyclic output data from the master must be applied to device outputs.

rcb_pbs_slave_cyclic_get_input

Called when the stack requires the current input data to send back to the master.

rcb_pbs_slave_alarm_ack_received

Called when a DPV1 alarm acknowledgement is received from the master.

rcb_pbs_slave_acyclic_write

Called for DPV1 write requests from the master to update slave data.

rcb_pbs_slave_acyclic_read

Called for DPV1 read requests from the master to supply slave data.

rcb_pbs_slave_ms2_initiate_received

Called when a DPV1 MS2 initiate request is received.

rcb_pbs_slave_ms2_abort_received

Called when a DPV1 MS2 abort request is received.

rcb_pbs_slave_address_changed

Called after the slave address has been updated by the master.

rcb_pbs_slave_check_config_received

Called when a check-configuration request is received.

rcb_pbs_slave_get_config_received

Called when a get-configuration request is received.

rcb_pbs_slave_set_param_received

Called when a set-parameter request is received.

rcb_pbs_slave_get_diag_received

Called when a get-diagnostic request is received.

rcb_pbs_slave_sync_command_received

Called when a sync global command is received.

rcb_pbs_slave_unsync_command_received

Called when an unsync global command is received.

rcb_pbs_slave_freeze_command_received

Called when a freeze global command is received.

rcb_pbs_slave_unfreeze_command_received

Called when an unfreeze global command is received.

rcb_pbs_slave_clear_command_received

Called when a clear global command is received.

Implementation Guide

  1. Populate an rs_pbs_slave_config_t instance with serial port, slave address, baud rate, TSDR, I/O sizes, and module identifiers.

  2. Call rs_pbs_slave_open() with a pre-allocated rs_pbs_slave_instance_t and the configuration pointer.

  3. Implement required callback functions for cyclic I/O, acyclic read/write, diagnostics, and control commands.

  4. Call rs_pbs_slave_process() periodically from the main loop.

  5. Monitor the stack for DPV1 alarm and status events, using rs_pbs_slave_alarm_send() and rs_pbs_slave_status_msg_send() when needed.

  6. On shutdown or restart, call rs_pbs_slave_close() to release FDL serial resources and handle state.

Protocol/Stack Layers

The stack is structured into the following layers:

  • Application Layer - External configuration and callback integration.

  • PROFIBUS DP Layer - MS0 (Cyclic Service): Handles standard DPV0 cyclic data exchange between the Class 1 master and slave. It manages parameterization, configuration verification, diagnostics, and continuous input/output data transfer. - MS1 (Acyclic Service - Class 1): Provides acyclic data exchange between the Class 1 master and slave over the active MS0 connection. It supports acyclic read/write requests and alarm/status event reporting. - MS2 (Acyclic Service - Class 2): Provides acyclic communication channels for engineering or diagnostic tools (Class 2 masters). It handles connection establishment, termination, and independent acyclic read/write access to slave parameters and data.

  • FDL Layer - PROFIBUS frame reception, synchronization, timing, frame parsing, and serial transmit/receive.

  • Serial / RS-485 Transport - Underlying serial interface managed through rs_serial_* APIs in the FDL initialization.

Data Flow / Workflow

The stack processes PROFIBUS messages in the following sequence:

  1. rs_pbs_slave_process() calls pbs_fdl_service() to progress the FDL state machine.

  2. pbs_fdl_service() receives raw serial frames and validates frame length, FCS, addressing, and SAP/SSAP.

  3. Valid frames are classified into FDL service types, then mapped to DP service types.

  4. DP services handle cyclic exchange, configuration, diagnostics, or DPV1 acyclic requests.

  5. Application callbacks provide or consume current input/output data and accept control or configuration events.

  6. Responses are serialized and transmitted via the FDL send path.

  7. Watchdog and DPV1 connection timeouts are checked each stack cycle.

The below diagram captures the high level sequence of operations associated with the PROFIBUS slave.

Communication Flow

PROFIBUS Communication Flow

PROFIBUS Communication Flow

Error Handling

The stack uses conservative error detection and recovery:

  • rs_pbs_slave_open() returns a handle on success and a negative value on failure.

  • rs_pbs_slave_close() returns status after closing the FDL serial interface.

  • rs_pbs_slave_process() ignores invalid handles and does not proceed if the instance is unavailable.

  • The FDL layer validates frame length, SD type, LEr/LE consistency, ED byte, and checksum.

  • Timeouts trigger FDL error recovery and bus resynchronization.

  • Invalid or unsupported service frames are rejected and the stack returns to wait state.

  • Application callbacks should return status codes or lengths consistent with expected data flow based on DPV1 read/write semantics.

Memory / Buffer Details

The PROFIBUS slave stack uses a 4KB memory map and explicit buffer sizing for protocol exchanges.

Memory Map

Region

Size

Description

RESERVED1

64 bytes

System/header area

IM0..IM3

4 x 64 bytes

Identification and maintenance data

CONFIG_DATA

128 bytes

Slave configuration data

SET_PARAM_DATA

244 bytes

DPV0 set parameter block

CHK_CONFIG_DATA

244 bytes

DPV0 check configuration block

GET_DIAG_DATA

6 bytes

Standard diagnostic request data

OUTPUT_DATA

244 bytes

Master-to-slave cyclic output data

INPUT_DATA

244 bytes

Slave-to-master cyclic input data

EXT_DIAG_DATA

238 bytes

Extended diagnostics area

ALARMS

255 bytes

DPV1 alarm queue area

STATUS_MESSAGE

64 bytes

DPV1 status message area

DPV1_DATA

2048 bytes

DPV1 acyclic data payload

RESERVED2

61 bytes

Future use

State Machine / States

The stack defines both DP and FDL state machines.

DP Layer States

DP Layer States

State

Description

RS_PBSS_DP_STATE_POWER_ON_RESET

Initial state after power-on or reset. The slave prepares its internal context and waits to transition to parameterization.

RS_PBSS_DP_STATE_WAIT_PARAM

The slave awaits a valid Set_Parameter (Set_Prm) telegram from the master to configure operational parameters.

RS_PBSS_DP_STATE_WAIT_CONFIG

The slave awaits a Check_Configuration (Chk_Cfg) telegram to verify that its I/O module configuration matches the master’s.

RS_PBSS_DP_STATE_DIAGNOSTIC

The slave sends diagnostic data (Slave_Diag) to the master, clearing pending startup diagnostics before data exchange.

RS_PBSS_DP_STATE_DATA_EXCHANGE

Normal operational state. The slave cyclically exchanges I/O data and processes acyclic requests and alarms.

High level state machine of the PROFIBUS DP slave is depicted below.

DP Layer State Machine

DP Layer State Machine

FDL Layer States

FDL Layer States

State

Description

PBSS_FDL_STATE_WAIT_SYNC

The FDL layer searches for a synchronization period (idle bus time) to align with the bus timing.

PBSS_FDL_STATE_WAIT_DATA

The layer waits for the Start Delimiter (SD) of a new incoming frame.

PBSS_FDL_STATE_GET_DATA

Actively receiving the remaining bytes of the frame based on the parsed length and format.

PBSS_FDL_STATE_PROCESS_DATA

Validates the received frame (checksum, addressing) and hands it off to the DP layer.

PBSS_FDL_STATE_SEND_DATA

Transmitting a response telegram back onto the serial bus.

PBSS_FDL_STATE_ERROR_RECOVERY

Handles serial errors, parity faults, or timeouts, discarding corrupted data and initiating resynchronization.

Best Practices

  • Initialize the configuration structure with valid baud rate, TSDR, and module identifiers before calling rs_pbs_slave_open().

  • Call rs_pbs_slave_process() from a deterministic periodic task; a millisecond tick is sufficient.

  • Keep serial port access on a single context unless external synchronization is provided.

  • Provide callbacks for cyclic I/O and diagnostics that execute quickly to preserve real-time behavior.

  • Do not assume thread safety for the stack handle manager; use external locks if accessing the stack from multiple tasks.

  • Validate all DPV1 request lengths and return values in callback implementations.

  • Use the configured input_length and output_length values to size application data buffers.

Dependency

This stack depends on the RAPIDSEA core library and serial interface:

  • rs_lib.h for base types and return values.

  • Serial interface APIs: rs_serial_open(), rs_serial_read(), rs_serial_write(), rs_serial_close(), rs_serial_set_config(), rs_serial_set_inter_char_timeout().

  • Handle manager APIs used by rs_handle_mgr_get_handle() and rs_handle_mgr_get_info_for_handle().

  • PROFIBUS frame and telegram utilities from pbs_frame.h and pbs_fdl_telegram.h.

Example Demo

An example PROFIBUS Slave implementation is available and is described in PROFIBUS Slave Demo. The demo source is pb_slave_app.c in the apps/profibus_slave_demo_app folder; it demonstrates NVM I&M loading from memory_map.bin, configuration of module IDs (8DI, 8DO, 2AI, 2AO), opening the slave via rs_pbs_slave_open(), and continuous processing with rs_pbs_slave_process(). The demo uses DIP-switch address selection and provides stub callback implementations for DPV1 and diagnostic handling.

PROFIBUS SLAVE Header Details

Documentation from the relevant header as follows:

Warning

doxygenfile: Cannot find file “rs_pbs_slave.h