---
title: "CANopen"
canonical_url: https://developer.repebble.com/pebbleos/docs/subsystems/canopen/
last_updated: 2026-12-01
sdk_version: 4.44
---
# CANopen

Source: https://developer.repebble.com/pebbleos/docs/subsystems/canopen/

`subsys/canopen/` runs a CANopen device, as CiA 301 specifies it, on a [CAN controller](https://developer.repebble.com/pebbleos/docs/drivers/can.md), with the CiA 402 drive profile on top for motor control. The object dictionary is generated from a YAML file, along with the EDS file the tools on the other end of the bus read.

| 

 | 

 |
| --- | --- |
| 

Directory

 | 

`subsys/canopen`

 |
| 

Headers

 | 

`pbl/canopen/canopen.h`, `pbl/canopen/cia402.h`

 |
| 

Kconfig

 | 

`CONFIG_CANOPEN`, enabled by the application; needs `CONFIG_CAN`

 |
| 

API

 | 

[API reference](https://developer.repebble.com/pebbleos/apidoc/dd/d9b/group __subsys__ canopen.html)

 |
| 

Samples

 | 

[Device](https://developer.repebble.com/pebbleos/docs/samples/subsys/canopen/device.md), [Drive](https://developer.repebble.com/pebbleos/docs/samples/subsys/canopen/drive.md)

 |

## Overview

A node is one device on the bus, with a node-ID from 1 to 127. The controller’s receive interrupt only queues frames; the services run on one thread, read and write the object dictionary, and call the application back. The application does its work in the callbacks, a thread of its own or an interrupt, as its timing needs.

```
flowchart TB
  master["NMT master<br/>SDO client, SYNC producer"]
  can["CAN controller<br/>pbl_can"]
  services["CANopen thread<br/>NMT · heartbeat · SDO · PDO · SYNC · EMCY"]
  od[("Object dictionary<br/>generated from YAML")]
  app["Application<br/>callbacks"]
  loop["Application work<br/>thread or interrupt"]
  master <-->|"CAN bus"| can
  can <-->|"RX ISR queues, TX queue drains"| services
  services <--> od
  services -->|"callbacks"| app
  app <-.->|"pbl_canopen_od_*()"| od
  app <-.->|"as it needs"| loop

  classDef app fill:#fa4a36,stroke:#fa4a36,color:#ffffff
  classDef system fill:#fd8a7c,stroke:#fd8a7c,color:#171717
  classDef base fill:#525252,stroke:#525252,color:#ffffff
  class app,loop app
  class services,od system
  class master,can base
```

It runs these services:

| 

Service

 | 

Objects

 | 

Notes

 |
| --- | --- | --- |
| 

NMT slave

 | 

 | 

Boot-up message, start, stop, pre-operational, reset node and reset communication

 |
| 

Heartbeat producer

 | 

0x1017

 | 

 |
| 

Heartbeat consumer

 | 

0x1016

 | 

Monitoring starts with the first heartbeat of each node

 |
| 

SDO server

 | 

0x1200

 | 

Expedited and segmented transfers

 |
| 

RPDOs

 | 

0x1400, 0x1600

 | 

Event-driven and synchronous, deadline monitoring

 |
| 

TPDOs

 | 

0x1800, 0x1a00

 | 

Event-driven with inhibit time and event timer, synchronous cyclic and acyclic, SYNC start value

 |
| 

SYNC consumer

 | 

0x1005, 0x1006, 0x1007

 | 

With or without a counter, loss detection, synchronous window

 |
| 

Error behavior

 | 

0x1029

 | 

NMT state on a communication error

 |
| 

EMCY producer

 | 

0x1001, 0x1003, 0x1014, 0x1015

 | 

Error register, error history, inhibit time

 |
| 

Parameter storage

 | 

0x1010, 0x1011

 | 

With `CONFIG_CANOPEN_STORAGE`, see [Parameter storage](#canopen-storage)

 |
| 

CiA 402 drive

 | 

0x6007, 0x603f-0x6502

 | 

With `CONFIG_CANOPEN_CIA402`, see [CiA 402 drive](#canopen-cia402)

 |

The node does not run SDO block transfers, an SDO client, an NMT master, a SYNC producer, LSS, node guarding, RTR-triggered PDOs or multiplexed PDOs.

## Quick start

A device with a counter it sends and an output the master sets. The YAML file describes the device, `pbl_canopen_od()` generates the dictionary from it, and the application starts a node on it:

 od.yaml

```
device:name: Example deviceproduct\_code: 1heartbeat:producer: 1000# mstpdos:- transmission: 254# on changeinhibit: 100# 10 msmapping: [counter]rpdos:- mapping: [output]objects:- index: 0x2000name: countertype: u32access: ropdo: true- index: 0x2001name: outputtype: u8pdo: true
```

 CMakeLists.txt

```
cmake\_minimum\_required(VERSION 3.20)find\_package(PebbleOS REQUIRED HINTS $ENV{PEBBLEOS\_BASE})project(example)target\_sources(app PRIVATE src/main.c)pbl\_canopen\_od(od.yaml)
```

 prj.conf

```
CONFIG\_CAN=yCONFIG\_CANOPEN=y
```

 src/main.c

```
#include "od.h"#include \<errno.h\>#include \<pbl/canopen/canopen.h\>#include \<pbl/drivers/can.h\>#include \<pbl/kernel/thread.h\>#include \<pbl/kernel/time.h\>static struct pbl\_canopen s\_co;/\* Every write from the bus goes through here first. \*/static int prv\_write(struct pbl\_canopen \*co, uint16\_t index, uint8\_t subindex, const void \*data,size\_t len, void \*arg) {return index == OD\_OUTPUT && \*(const uint8\_t \*)data \> 100U ? -ERANGE : 0;}static const struct pbl\_canopen\_callbacks s\_cb = {.write = prv\_write};int main(void) {const struct pbl\_canopen\_config cfg = {.can = CAN\_BUS,.od = &od,.node\_id = 5,.bitrate = 500000,.cb = &s\_cb,};uint32\_t counter = 0U;int ret;ret = pbl\_canopen\_init(&s\_co, &cfg);if (ret != 0) {return ret;}ret = pbl\_canopen\_start(&s\_co);if (ret != 0) {return ret;}for (;;) {pbl\_thread\_sleep(PBL\_MSEC(500));counter++;/\* A new value sends the TPDO that maps it. \*/(void)pbl\_canopen\_od\_write(&s\_co, OD\_COUNTER, 0U, &counter, sizeof(counter));}}
```

`pbl_canopen_od()` names the dictionary after the YAML file, `od` for `od.yaml`, unless `NAME` gives another symbol, and adds it to the `app` target unless `TARGET` names another one. The generated header defines a macro for the index of every application object, and for the sub-index of every record member: `OD_COUNTER`, `OD_CONFIG_GAIN`.

The node configures and starts the controller, restores every default, sends its boot-up message and goes pre-operational, where the master configures it over SDO before starting it. `pbl_canopen_od_read()` returns the size it read, and `pbl_canopen_od_write()` writes any entry, whatever its access over the bus; a value that changes triggers the event-driven and acyclic TPDOs that map it. `pbl_canopen_emcy_raise()` and `pbl_canopen_emcy_clear()` report the application’s errors: each one raised sets its error register bits, goes into the error history and sends an emergency message, and each one cleared sends an error reset message with the bits of the errors left.

From a host with a CAN adapter, the [canopen](https://canopen.readthedocs.io) Python package drives the node with the EDS file the build writes, `build/canopen/od.eds`:

```
import canopennetwork=canopen.Network()network.connect(interface="socketcan",channel="can0",bitrate=500000)node=network.add\_node(5,"build/canopen/od.eds")node.nmt.state="OPERATIONAL"node.sdo["output"].raw=42print(node.sdo["counter"].raw)
```

## NMT states

The master moves the node between states with NMT commands, and the node reports its state in its heartbeat. Each state allows some services only:

```
flowchart TB
  any(["Any state"])
  init["Initializing"]
  preop["Pre-operational<br/>SDO, SYNC, EMCY"]
  op["Operational<br/>SDO, SYNC, EMCY, PDOs"]
  stopped["Stopped<br/>NMT and heartbeat only"]
  any -.->|"reset node,<br/>reset communication"| init
  init -->|"boot-up message"| preop
  preop -->|"start"| op
  op -->|"enter pre-operational,<br/>communication error"| preop
  preop -->|"stop"| stopped
  stopped -->|"enter pre-operational"| preop
  op -->|"stop"| stopped
  stopped -->|"start"| op

  classDef app fill:#fa4a36,stroke:#fa4a36,color:#ffffff
  classDef system fill:#fd8a7c,stroke:#fd8a7c,color:#171717
  classDef base fill:#525252,stroke:#525252,color:#ffffff
  class op app
  class preop system
  class init,stopped,any base
```

Reset node and reset communication bring the node back to initializing from any state. In detail:

| 

Service

 | 

Pre-operational

 | 

Operational

 | 

Stopped

 |
| --- | --- | --- | --- |
| 

NMT, heartbeat

 | 

✓

 | 

✓

 | 

✓

 |
| 

SDO

 | 

✓

 | 

✓

 | 

 |
| 

EMCY

 | 

✓

 | 

✓

 | 

 |
| 

SYNC

 | 

Monitored

 | 

Monitored, callback

 | 

 |
| 

PDOs

 | 

 | 

✓

 | 

 |

A reset from the master restores the defaults of the communication objects, and with reset node those of the application objects too, then calls the reset callback before the boot-up message, to set the values that differ from them, e.g. a serial number in 0x1018. The NMT callback reports every change of state.

## Threading

Nodes run on one thread, `CANopen`, with `CONFIG_CANOPEN_THREAD_PRIO` and `CONFIG_CANOPEN_THREAD_STACK_SIZE`. The controller’s receive interrupt only queues frames, up to `CONFIG_CANOPEN_RX_QUEUE_SIZE` per node, and every timer is delayed work on the same thread, so the services never run concurrently. Frames go through a queue of `CONFIG_CANOPEN_TX_QUEUE_SIZE` that the controller drains as it sends.

The callbacks run on that thread, with the node locked; they may call the API, whose lock is recursive. They hold up every service of every node while they run. The write callback is called for every application entry an SDO or an RPDO writes, before the value is stored, and refuses it with a negative errno: an SDO is then aborted, `-EINVAL` and `-ERANGE` as an invalid value, `-EBUSY` as not possible in the present state.

On a SYNC, the synchronous RPDOs are applied, the SYNC callback runs, and the synchronous TPDOs go once it returns: the callback is where the application takes its new values and reports its own.

```
sequenceDiagram
  participant M as Master
  participant I as RX interrupt
  participant T as CANopen thread
  participant A as Application
  M->>I: RPDO (synchronous)
  I->>T: queue
  Note over T: buffered until SYNC
  M->>I: SYNC
  I->>T: queue, with pbl_cycle_get_32()
  T->>T: apply synchronous RPDOs
  T->>A: sync callback
  A->>T: pbl_canopen_od_read(), pbl_canopen_od_write()
  T->>M: synchronous TPDOs
```

The SYNC callback gets the value of `pbl_cycle_get_32()` when the controller handed the SYNC over, so it can tell how late it runs; past the synchronous window, the TPDOs due are not sent.

## Where the work runs

What the application does with the values it gets depends on how long it takes and how much timing matters. A few strategies:

| 

Strategy

 | 

Fits

 | 

Data exchange

 | 

Timing

 |
| --- | --- | --- | --- |
| 

In the callbacks

 | 

Short actions: an LED, a relay, a slow process value

 | 

The API, directly

 | 

After the services ahead of it on the node’s thread

 |
| 

An application thread

 | 

Longer work, or work that blocks: a bus transfer, flash, a slow control loop

 | 

A semaphore or a queue to wake it, the API, which it may call

 | 

Its own priority

 |
| 

An interrupt

 | 

Fast loops with strict timing, paced by a timer or a peripheral

 | 

Without locks

 | 

Independent of the bus traffic

 |

Some notes:

- Work in a callback delays the heartbeat, SDO replies and the other nodes’ services for as long as it runs; a short one costs nothing else.

- A thread may call the API, which locks the node, so it waits for a callback or a service in progress; a priority above the node’s thread does not change that.

- An interrupt never calls the API, and a zero-latency one, with `CONFIG_KERNEL_ZERO_LATENCY_IRQS`, no kernel function at all. A callback or a thread hands it the values, and takes its results, without a lock it could wait on: atomics for single values; for a set that must change at once, a copy with that interrupt masked, which delays at most one of its runs, or a double buffer whose index the writer swaps atomically. Masking every interrupt, zero-latency ones included, works too, but delays all of them.

- A CiA 402 drive is no different: its callbacks are those of the node.

 Callback

```
static void prv\_sync(struct pbl\_canopen \*co, uint8\_t counter, uint32\_t cycles, void \*arg) {uint8\_t on;if (pbl\_canopen\_od\_read(co, OD\_LED, 0U, &on, sizeof(on)) == (int)sizeof(on)) {led\_set(on != 0U);}}
```

 Thread

```
static PBL\_SEM\_DEFINE(s\_sync, 0, 1);static void prv\_sync(struct pbl\_canopen \*co, uint8\_t counter, uint32\_t cycles, void \*arg) {pbl\_sem\_give(&s\_sync);}static void prv\_worker(void \*arg) {int32\_t target;int32\_t actual;for (;;) {(void)pbl\_sem\_take(&s\_sync, PBL\_FOREVER);(void)pbl\_canopen\_od\_read(&s\_co, OD\_TARGET, 0U, &target, sizeof(target));actual = process\_step(target);(void)pbl\_canopen\_od\_write(&s\_co, OD\_ACTUAL, 0U, &actual, sizeof(actual));}}
```

 Interrupt, atomics

```
static atomic\_int\_least32\_t s\_target;static atomic\_int\_least32\_t s\_actual;/\* Loop interrupt \*/static void prv\_loop(void) {int32\_t target = atomic\_load(&s\_target);...atomic\_store(&s\_actual, actual);}static void prv\_sync(struct pbl\_canopen \*co, uint8\_t counter, uint32\_t cycles, void \*arg) {int32\_t target;int32\_t actual = atomic\_load(&s\_actual);if (pbl\_canopen\_od\_read(co, OD\_TARGET, 0U, &target, sizeof(target)) == (int)sizeof(target)) {atomic\_store(&s\_target, target);}(void)pbl\_canopen\_od\_write(co, OD\_ACTUAL, 0U, &actual, sizeof(actual));}
```

 Interrupt, masked copy

```
struct setpoint {int32\_t target;int32\_t limit;int16\_t gain;};static struct setpoint s\_setpoint;static void prv\_sync(struct pbl\_canopen \*co, uint8\_t counter, uint32\_t cycles, void \*arg) {struct setpoint sp;(void)pbl\_canopen\_od\_read(co, OD\_TARGET, 0U, &sp.target, sizeof(sp.target));(void)pbl\_canopen\_od\_read(co, OD\_LIMIT, 0U, &sp.limit, sizeof(sp.limit));(void)pbl\_canopen\_od\_read(co, OD\_GAIN, 0U, &sp.gain, sizeof(sp.gain));/\* The loop sees all of it from its next run on. \*/pbl\_irq\_disable(LOOP\_IRQ);s\_setpoint = sp;pbl\_irq\_enable(LOOP\_IRQ);}
```

## Object dictionary

The YAML file describes the device and its application objects. Every key is optional but `objects`’ `index`, `name` and `type`:

```
device:type: 0x00000000# 0x1000name: Example device# 0x1008hardware\_version: "1.0"# 0x1009software\_version: "1.0"# 0x100avendor\_id: 0x00000000# 0x1018product\_code: 0x00000001revision: 0x00000001serial: 0x00000000errors: 8# depth of the error history 0x1003, 0 for nonestorage: false# 0x1010 and 0x1011, with CONFIG\_CANOPEN\_STORAGEemcy:cob\_id: 0x85# 0x1014, 0x80 + node-ID by defaultinhibit: 0# 0x1015, in 100 µsheartbeat:producer: 1000# 0x1017, in msconsumers:# 0x1016: a count of empty entries, or a list- {node: 1, time: 1500}sync:cob\_id: 0x80# 0x1005period: 1000# 0x1006, in µs, 0 to not monitor the SYNCwindow: 500# 0x1007, in µs, 0 for noneerror\_behavior: pre-operational# 0x1029: pre-operational, none or stoppedtpdos:- transmission: 254# 0-240 synchronous, 254 or 255 event-driveninhibit: 100# in 100 µsevent\_timer: 1000# in mssync\_start: 0mapping: [counter, config.gain]rpdos:- cob\_id: 0x205# 0x200 + 0x100 per PDO + node-ID for the first fourenabled: truetransmission: 255event\_timer: 0# deadline, in msmapping: [output]objects:- index: 0x2000name: countertype: u32access: ropdo: true- index: 0x2001name: labeltype: stringsize: 32default: Example- index: 0x2002name: samplestype: arrayelement: i16length: 4access: ropdo: tx- index: 0x2003name: configtype: recordmembers:- {name: gain, type: u16, default: 10, pdo: true}- {name: offset, type: i32}
```

| 

Key

 | 

Values

 |
| --- | --- |
| 

`type`

 | 

`bool`, `i8`, `i16`, `i32`, `i64`, `u8`, `u16`, `u32`, `u64`, `f32`, `f64`, `string`, `octets`, `array`, `record`

 |
| 

`access`

 | 

`ro`, `wo`, `rw` (the default) or `const`

 |
| 

`pdo`

 | 

`true` for the PDOs its access allows, `tx` or `rx` for one kind only

 |
| 

`size`

 | 

Strings and octets, the default’s length otherwise; they cannot be mapped

 |

Arrays name their entries `name[1]` to `name[length]` and records `name.member`, in mappings. Application objects go at 0x2000 and above; the generator adds the communication objects from the sections above, with mapping objects of 8 entries for dynamic mapping. It also writes an EDS file, `od.eds` next to the generated sources.

## PDOs

A PDO carries up to 8 bytes of mapped entries in one frame, without the protocol overhead of an SDO. Its transmission type says when it goes:

| 

Type

 | 

TPDO

 | 

RPDO

 |
| --- | --- | --- |
| 

0

 | 

On the SYNC after a mapped value changed

 | 

Applied on the next SYNC

 |
| 

1-240

 | 

Every n-th SYNC, from the SYNC start value

 | 

Applied on the next SYNC

 |
| 

254, 255

 | 

When a mapped value changes, at most once per inhibit time, and with the event timer

 | 

Applied at once, deadline from the event timer

 |

PDO mappings can be changed over SDO with the procedure CiA 301 gives: invalidate the PDO, clear sub-index 0 of the mapping, write the entries, set sub-index 0, validate the PDO. Mapped entries are whole bytes, up to 8 per PDO and 8 bytes in all; RPDOs can also map the dummy entries 0x0001-0x0007.

## Communication errors

Bus off, a monitored heartbeat lost and the SYNC lost are communication errors. Besides its emergency message, each one moves the node to the NMT state that sub-index 1 of 0x1029 gives: pre-operational from operational (0, the default), no change (1) or stopped (2). The NMT callback reports the change; a device that drives something, such as an actuator, brings it to a safe state there whenever the node leaves operational, as its PDOs stop. A CiA 402 drive does it by itself, see [CiA 402 drive](#canopen-cia402).

With a communication cycle period in 0x1006, in µs, the SYNC is monitored from its first reception: once it misses one and a half periods, the node reacts as above, and clears the emergency when the SYNC comes back. With a synchronous window length in 0x1007, in µs, the synchronous TPDOs due on a SYNC are not sent once the window from its reception has passed, e.g. because the SYNC callback ran late, and the synchronous RPDOs received past it are dropped.

The node raises emergencies of its own, in the communication bit of the error register, and clears each one once its cause is gone:

| 

Code

 | 

Cause

 |
| --- | --- |
| 

0x8100

 | 

SYNC lost

 |
| 

0x8110

 | 

Received frames dropped

 |
| 

0x8120

 | 

Controller error passive

 |
| 

0x8130

 | 

Heartbeat lost

 |
| 

0x8140

 | 

Bus off

 |
| 

0x8210

 | 

RPDO shorter than its mapping

 |
| 

0x8220

 | 

RPDO longer than its mapping

 |
| 

0x8250

 | 

RPDO deadline missed

 |

## Parameter storage

With `CONFIG_CANOPEN_STORAGE` and `storage: true` in its dictionary, a node keeps its parameters, the entries the bus can write but the error history, in a settings file of `subsys/settings`, `/canopen<node-ID>`:

| 

Object

 | 

Sub-index

 | 

Signature

 | 

Effect

 |
| --- | --- | --- | --- |
| 

0x1010

 | 

1, 2, 3

 | 

`save` (0x65766173)

 | 

Saves all parameters, the communication ones or the application ones

 |
| 

0x1011

 | 

1, 2, 3

 | 

`load` (0x64616f6c)

 | 

Deletes them, for the defaults to come back on the next reset

 |

The node loads them on each reset that restores their defaults, at start too. Both objects read 1: the node saves on command only. Parameters are stored by index, sub-index and size, so a new dictionary loads those it still has, and the others keep their defaults. Saving runs on the node’s thread and writes flash, so it holds up the node for as long: the master saves while the node is pre-operational. A dictionary with storage is refused without `CONFIG_CANOPEN_STORAGE`.

## CiA 402 drive

With `CONFIG_CANOPEN_CIA402`, a node whose dictionary has a `cia402` section runs a drive, as CiA 402 specifies it: `include/pbl/canopen/cia402.h`. The master writes the controlword (0x6040) to move the drive from state to state, selects a mode of operation (0x6060) and sets its targets; the drive reports its state in the statusword (0x6041) and the motor’s actual values.

### Dictionary

The section lists the modes of operation the drive supports, and optionally its option codes:

```
cia402:modes: [pp, pv, pt, csp, csv, cst]options:abort\_connection: 1# 0x6007quick\_stop: 2# 0x605ashutdown: 0# 0x605bdisable\_operation: 1# 0x605cfault\_reaction: 2# 0x605e
```

| 

Mode

 | 

Value

 | 

The master sets

 | 

Typical use

 |
| --- | --- | --- | --- |
| 

Profile position, `pp`

 | 

1

 | 

A target position, the drive plans the move

 | 

Point-to-point moves

 |
| 

Profile velocity, `pv`

 | 

3

 | 

A target velocity, the drive ramps to it

 | 

Spindles, conveyors

 |
| 

Profile torque, `pt`

 | 

4

 | 

A target torque, with a slope

 | 

Tensioning

 |
| 

Cyclic synchronous position, `csp`

 | 

8

 | 

A position every SYNC

 | 

Multi-axis motion

 |
| 

Cyclic synchronous velocity, `csv`

 | 

9

 | 

A velocity every SYNC

 | 

Velocity loops closed by the master

 |
| 

Cyclic synchronous torque, `cst`

 | 

10

 | 

A torque every SYNC

 | 

Position and velocity loops in the master

 |

The generator adds the drive’s objects, named for PDO mappings:

| 

Objects

 | 

Names

 | 

Modes

 |
| --- | --- | --- |
| 

0x603f, 0x6040, 0x6041

 | 

`error_code`, `controlword`, `statusword`

 | 

All

 |
| 

0x6060, 0x6061, 0x6502

 | 

`modes_of_operation`, `modes_of_operation_display`, `supported_drive_modes`

 | 

All

 |
| 

0x6007, 0x605a-0x605e

 | 

`*_option_code`

 | 

All

 |
| 

0x607a, 0x6064

 | 

`target_position`, `position_actual_value`

 | 

pp, csp

 |
| 

0x60ff, 0x606c

 | 

`target_velocity`, `velocity_actual_value`

 | 

pv, csv

 |
| 

0x6071, 0x6077

 | 

`target_torque`, `torque_actual_value`

 | 

pt, cst

 |
| 

0x6081, 0x6083, 0x6084

 | 

`profile_velocity`, `profile_acceleration`, `profile_deceleration`

 | 

pp, pv

 |
| 

0x6085

 | 

`quick_stop_deceleration`

 | 

pp, pv, csp, csv

 |
| 

0x6087

 | 

`torque_slope`

 | 

pt

 |
| 

0x60c2

 | 

`interpolation_time_period`

 | 

csp, csv, cst

 |

A cyclic synchronous drive maps its controlword and target to synchronous RPDOs, and its statusword and actual value to synchronous TPDOs, as the [Drive sample](https://developer.repebble.com/pebbleos/docs/samples/subsys/canopen/drive.md) does:

```
cia402:modes: [pv, csv]rpdos:- mapping: [controlword, modes\_of\_operation]- transmission: 1# applied on SYNCmapping: [target\_velocity]tpdos:- transmission: 254# statusword changesinhibit: 10mapping: [statusword, modes\_of\_operation\_display]- transmission: 1# sampled on SYNCmapping: [statusword, velocity\_actual\_value]
```

### State machine

`pbl_cia402_init()` attaches a drive to an initialized node, in switch on disabled. The controlword commands move it from state to state, and each change of state calls the application to bring the power stage and the motor in line:

```
flowchart TB
  start(( )) --> sod
  sod["Switch on disabled"]
  ready["Ready to switch on"]
  son["Switched on"]
  op["Operation enabled"]
  qsa["Quick stop active"]
  anyf(["Any state"])
  fra["Fault reaction active"]
  fault["Fault"]
  sod -->|"Shutdown"| ready
  ready -->|"Switch on"| son
  son -->|"Enable operation"| op
  op -->|"Disable operation"| son
  son -->|"Shutdown"| ready
  op -->|"Quick stop"| qsa
  qsa -->|"quick stop done"| sod
  anyf -.->|"pbl_cia402_fault()"| fra
  fra -->|"fault reaction done"| fault
  fault -->|"Fault reset"| sod

  classDef on fill:#fa4a36,stroke:#fa4a36,color:#ffffff
  classDef powered fill:#fd8a7c,stroke:#fd8a7c,color:#171717
  classDef off fill:#525252,stroke:#525252,color:#ffffff
  class op,qsa,fra on
  class son powered
  class sod,ready,fault,anyf,start off
```

The motor is driven in the red states and the power stage is on in the pink one too. The diagram leaves out the shortcuts; every transition, with the callbacks it calls:

| 

From

 | 

Command

 | 

To

 | 

Callbacks

 |
| --- | --- | --- | --- |
| 

Switch on disabled

 | 

Shutdown

 | 

Ready to switch on

 | 

 |
| 

Ready to switch on

 | 

Switch on

 | 

Switched on

 | 

`switch_on`

 |
| 

Ready to switch on

 | 

Enable operation

 | 

Operation enabled

 | 

`switch_on`, `enable`

 |
| 

Switched on

 | 

Enable operation

 | 

Operation enabled

 | 

`enable`

 |
| 

Operation enabled

 | 

Disable operation

 | 

Switched on

 | 

`disable`

 |
| 

Switched on

 | 

Shutdown

 | 

Ready to switch on

 | 

`switch_off`

 |
| 

Operation enabled

 | 

Shutdown

 | 

Ready to switch on

 | 

`disable`, `switch_off`

 |
| 

Ready to switch on, switched on

 | 

Disable voltage, quick stop

 | 

Switch on disabled

 | 

`switch_off`

 |
| 

Operation enabled

 | 

Disable voltage

 | 

Switch on disabled

 | 

`disable`, `switch_off`

 |
| 

Operation enabled

 | 

Quick stop

 | 

Quick stop active

 | 

`quick_stop`

 |
| 

Quick stop active

 | 

`pbl_cia402_quick_stop_done()` with option codes 0-4, disable voltage

 | 

Switch on disabled

 | 

`disable`, `switch_off`

 |
| 

Quick stop active

 | 

Enable operation, with quick stop option codes 5-8

 | 

Operation enabled

 | 

 |
| 

Any but the fault states

 | 

`pbl_cia402_fault()`

 | 

Fault reaction active

 | 

`fault`

 |
| 

Fault reaction active

 | 

`pbl_cia402_fault_reaction_done()`

 | 

Fault

 | 

`disable`, `switch_off`

 |
| 

Fault

 | 

Fault reset

 | 

Switch on disabled

 | 

 |

The commands are these controlword values, the fault reset acting on the rising edge of bit 7:

| 

Command

 | 

Controlword

 | 

Bits 7, 3-0

 |
| --- | --- | --- |
| 

Shutdown

 | 

0x0006

 | 

`0 x110`

 |
| 

Switch on

 | 

0x0007

 | 

`0 0111`

 |
| 

Enable operation

 | 

0x000f

 | 

`0 1111`

 |
| 

Disable operation

 | 

0x0007

 | 

`0 0111`

 |
| 

Disable voltage

 | 

0x0000

 | 

`0 xx0x`

 |
| 

Quick stop

 | 

0x0002

 | 

`0 x01x`

 |
| 

Fault reset

 | 

0x0080

 | 

`↑ xxxx`

 |

The statusword reports each state, with the remote bit (9) set, the voltage enabled bit (4) set where the power stage is on, and the application’s bits on top:

| 

State

 | 

Statusword

 |
| --- | --- |
| 

Switch on disabled

 | 

0x0240

 |
| 

Ready to switch on

 | 

0x0221

 |
| 

Switched on

 | 

0x0233

 |
| 

Operation enabled

 | 

0x0237

 |
| 

Quick stop active

 | 

0x0217

 |
| 

Fault reaction active

 | 

0x021f

 |
| 

Fault

 | 

0x0208

 |

A master enables a drive with shutdown, then switch on and enable operation, waiting for each state in the statusword:

```
node.sdo["modes\_of\_operation"].raw=9# csvnode.sdo["controlword"].raw=0x06# ready to switch onnode.sdo["controlword"].raw=0x0f# operation enabledassertnode.sdo["statusword"].raw&0x6f==0x27
```

### Application

The application drives the motor from the callbacks of `struct pbl_cia402_ops`, each optional and called with the node locked, as [Where the work runs](#canopen-work) describes:

| 

Callback

 | 

Called

 | 

The application

 |
| --- | --- | --- |
| 

`switch_on`

 | 

Entering switched on

 | 

Turns the power stage on, without driving the motor; may refuse

 |
| 

`enable`

 | 

Entering operation enabled

 | 

Makes the motor follow its targets; may refuse

 |
| 

`disable`

 | 

Leaving operation enabled

 | 

Stops the motor as 0x605c says

 |
| 

`switch_off`

 | 

Leaving the powered states

 | 

Turns the power stage off

 |
| 

`quick_stop`

 | 

Entering quick stop active

 | 

Starts the quick stop ramp of 0x605a

 |
| 

`fault`

 | 

Entering fault reaction active

 | 

Starts the reaction of 0x605e

 |
| 

`set_mode`

 | 

Writing 0x6060

 | 

Switches the mode of operation; may refuse it

 |

A quick stop and a fault reaction may take time, e.g. a deceleration ramp: the application reports them done once the motor has stopped, with `pbl_cia402_quick_stop_done()` and `pbl_cia402_fault_reaction_done()`; a drive without the callback is done at once. `pbl_cia402_fault()` reports a fault, with its error code in 0x603f and an emergency, until the master resets it with the controlword. `pbl_cia402_set_status()` sets the statusword bits the application owns, such as target reached. These functions lock the node, so they are called from a thread or a callback, never an interrupt. In this example, the motor’s loop runs in an interrupt, as in the [Drive sample](https://developer.repebble.com/pebbleos/docs/samples/subsys/canopen/drive.md):

```
#include \<pbl/canopen/cia402.h\>static struct pbl\_canopen s\_co;static struct pbl\_cia402 s\_drive;enum command {CMD\_COAST,CMD\_RUN,CMD\_QUICK\_STOP,};static atomic\_uint\_least8\_t s\_command;static atomic\_int\_least32\_t s\_velocity;static int prv\_switch\_on(struct pbl\_cia402 \*drive, void \*arg) {return power\_stage\_on();}static void prv\_switch\_off(struct pbl\_cia402 \*drive, void \*arg) {power\_stage\_off();}static int prv\_enable(struct pbl\_cia402 \*drive, void \*arg) {atomic\_store(&s\_command, CMD\_RUN);return 0;}static void prv\_disable(struct pbl\_cia402 \*drive, void \*arg) {atomic\_store(&s\_command, CMD\_COAST);}static void prv\_quick\_stop(struct pbl\_cia402 \*drive, void \*arg) {atomic\_store(&s\_command, CMD\_QUICK\_STOP);}static const struct pbl\_cia402\_ops s\_ops = {.switch\_on = prv\_switch\_on,.switch\_off = prv\_switch\_off,.enable = prv\_enable,.disable = prv\_disable,.quick\_stop = prv\_quick\_stop,};int main(void) {...ret = pbl\_canopen\_init(&s\_co, &cfg);if (ret != 0) {return ret;}ret = pbl\_cia402\_init(&s\_drive, &s\_co, &s\_ops, nullptr);if (ret != 0) {return ret;}ret = pbl\_canopen\_start(&s\_co);if (ret != 0) {return ret;}for (;;) {pbl\_thread\_sleep(PBL\_MSEC(10));/\* The loop ramps down; report the quick stop done once it stands still. \*/uint8\_t command = CMD\_QUICK\_STOP;if (atomic\_load(&s\_velocity) == 0 &&atomic\_compare\_exchange\_strong(&s\_command, &command, CMD\_COAST)) {pbl\_cia402\_quick\_stop\_done(&s\_drive);}}}
```

### Losing the master

When the node leaves operational while the drive is operation enabled, by an NMT command or a communication error, its PDOs stop and the abort connection option code (0x6007) decides what the drive does:

| 

0x6007

 | 

Reaction

 |
| --- | --- |
| 

0

 | 

Nothing

 |
| 

1

 | 

A fault, with error code 0x8100 (the default)

 |
| 

2

 | 

Disable voltage: the motor coasts and the power stage goes off

 |
| 

3

 | 

A quick stop

 |

With the master’s heartbeat consumed, or a SYNC period set, a master that stops, or a bus that breaks, stops the motor once the heartbeat consumer time, or one and a half SYNC periods, have passed. A reset of the node brings the drive back to switch on disabled, unless it is in fault.

## Configuration

| 

Option

 | 

Default

 | 

 |
| --- | --- | --- |
| 

`CONFIG_CANOPEN_THREAD_PRIO`

 | 

4

 | 

Priority of the nodes’ thread, the highest

 |
| 

`CONFIG_CANOPEN_THREAD_STACK_SIZE`

 | 

1536

 | 

 |
| 

`CONFIG_CANOPEN_RX_QUEUE_SIZE`

 | 

16

 | 

Received frames queued per node

 |
| 

`CONFIG_CANOPEN_TX_QUEUE_SIZE`

 | 

16

 | 

Frames queued for transmission per node

 |
| 

`CONFIG_CANOPEN_SDO_TIMEOUT_MS`

 | 

1000

 | 

Segmented transfer timeout

 |
| 

`CONFIG_CANOPEN_EMCY_ACTIVE`

 | 

8

 | 

Errors raised at once per node

 |
| 

`CONFIG_CANOPEN_EMCY_QUEUE`

 | 

4

 | 

Emergency messages held back by the inhibit time

 |
| 

`CONFIG_CANOPEN_STORAGE`

 | 

n

 | 

Parameter storage, needs `CONFIG_SETTINGS`

 |
| 

`CONFIG_CANOPEN_STORAGE_SIZE`

 | 

4096

 | 

Space for the parameters of a node, in bytes

 |
| 

`CONFIG_CANOPEN_CIA402`

 | 

n

 | 

CiA 402 drive profile

 |

Generated from PebbleOS [7c542d7a9](https://github.com/coredevices/pebbleos/commit/7c542d7a9d6fddb551ac1d242c8bf3c8a6cc6ce6). This page is maintained in the pebbleos repository: [docs/subsystems/canopen.md](https://github.com/coredevices/pebbleos/blob/7c542d7a9d6fddb551ac1d242c8bf3c8a6cc6ce6/docs/subsystems/canopen.md).
