PebbleOS v4.47.0-1219-g7c542d7a9
Loading...
Searching...
No Matches
CANopen

CANopen device (CiA 301) on a CAN controller. More...

Modules

 CiA 402 drive
 Drive profile (CiA 402) on a CANopen node.
 

Data Structures

struct  pbl_canopen_od_entry
 An object dictionary entry: one sub-index of an object. More...
 
struct  pbl_canopen_od
 An object dictionary, generated by pbl_canopen_od(). More...
 
struct  pbl_canopen_callbacks
 Node callbacks, all called from the node's thread. More...
 
struct  pbl_canopen_config
 Node configuration. More...
 

Enumerations

enum  pbl_canopen_type {
  PBL_CANOPEN_TYPE_BOOLEAN = 0x01 , PBL_CANOPEN_TYPE_INTEGER8 = 0x02 , PBL_CANOPEN_TYPE_INTEGER16 = 0x03 , PBL_CANOPEN_TYPE_INTEGER32 = 0x04 ,
  PBL_CANOPEN_TYPE_UNSIGNED8 = 0x05 , PBL_CANOPEN_TYPE_UNSIGNED16 = 0x06 , PBL_CANOPEN_TYPE_UNSIGNED32 = 0x07 , PBL_CANOPEN_TYPE_REAL32 = 0x08 ,
  PBL_CANOPEN_TYPE_VISIBLE_STRING = 0x09 , PBL_CANOPEN_TYPE_OCTET_STRING = 0x0a , PBL_CANOPEN_TYPE_REAL64 = 0x11 , PBL_CANOPEN_TYPE_INTEGER64 = 0x15 ,
  PBL_CANOPEN_TYPE_UNSIGNED64 = 0x1b
}
 Data types of object dictionary entries. More...
 
enum  pbl_canopen_nmt_state { PBL_CANOPEN_NMT_INITIALIZING = 0x00 , PBL_CANOPEN_NMT_STOPPED = 0x04 , PBL_CANOPEN_NMT_OPERATIONAL = 0x05 , PBL_CANOPEN_NMT_PRE_OPERATIONAL = 0x7f }
 NMT states, with their heartbeat codes. More...
 
enum  pbl_canopen_reset { PBL_CANOPEN_RESET_COMM , PBL_CANOPEN_RESET_NODE }
 Resets, as requested by the NMT master. More...
 
enum  pbl_canopen_hb_event { PBL_CANOPEN_HB_BOOT , PBL_CANOPEN_HB_STATE , PBL_CANOPEN_HB_TIMEOUT }
 Heartbeat consumer events. More...
 

Functions

int pbl_canopen_init (struct pbl_canopen *co, const struct pbl_canopen_config *cfg)
 Initialize a node, stopped.
 
int pbl_canopen_start (struct pbl_canopen *co)
 Start the node: join the bus, restore every default and go pre-operational.
 
int pbl_canopen_stop (struct pbl_canopen *co)
 Stop the node and leave the bus.
 
enum pbl_canopen_nmt_state pbl_canopen_nmt_state (struct pbl_canopen *co)
 Get the NMT state.
 
int pbl_canopen_od_read (struct pbl_canopen *co, uint16_t index, uint8_t subindex, void *data, size_t len)
 Read an entry of the object dictionary.
 
int pbl_canopen_od_write (struct pbl_canopen *co, uint16_t index, uint8_t subindex, const void *data, size_t len)
 Write an entry of the object dictionary, whatever its access over the bus.
 
int pbl_canopen_tpdo_request (struct pbl_canopen *co, uint16_t num)
 Request the transmission of a TPDO.
 
int pbl_canopen_emcy_raise (struct pbl_canopen *co, uint16_t code, uint8_t reg, const uint8_t *msef)
 Raise an error: set its error register bits, add it to the error history and send an emergency message.
 
int pbl_canopen_emcy_clear (struct pbl_canopen *co, uint16_t code)
 Clear an error raised by pbl_canopen_emcy_raise(), and send an error reset message.
 

Entry attributes

#define PBL_CANOPEN_ATTR_READ   PBL_BIT(0)
 Readable over the bus.
 
#define PBL_CANOPEN_ATTR_WRITE   PBL_BIT(1)
 Writable over the bus.
 
#define PBL_CANOPEN_ATTR_TPDO   PBL_BIT(2)
 Mappable to a TPDO.
 
#define PBL_CANOPEN_ATTR_RPDO   PBL_BIT(3)
 Mappable to an RPDO.
 
#define PBL_CANOPEN_ATTR_NODEID   PBL_BIT(4)
 Default value plus the node-ID.
 

Detailed Description

CANopen device (CiA 301) on a CAN controller.

A node runs the NMT slave, heartbeat producer and consumers, SDO server, PDOs, SYNC consumer and EMCY producer of one device on the bus. Its object dictionary is generated from a YAML file by pbl_canopen_od() in the application's CMakeLists.txt, which declares the dictionary in a header of the same name:

#include "od.h"
static struct pbl_canopen s_co;
const struct pbl_canopen_config cfg = {
.od = &od,
.node_id = 5,
.bitrate = 500000,
};
ret = pbl_canopen_init(&s_co, &cfg);
ret = pbl_canopen_start(&s_co);
ret = pbl_canopen_od_write(&s_co, OD_STATUS, 0, &status, sizeof(status));
const struct pbl_can *const CAN_BUS
The controller of the board's CAN bus connector.
const struct pbl_canopen_od * od
Object dictionary, used by this node only.
Definition canopen.h:275
const struct pbl_can * can
CAN controller, configured and started by the node.
Definition canopen.h:273
Node configuration.
Definition canopen.h:271
int pbl_canopen_init(struct pbl_canopen *co, const struct pbl_canopen_config *cfg)
Initialize a node, stopped.
int pbl_canopen_od_write(struct pbl_canopen *co, uint16_t index, uint8_t subindex, const void *data, size_t len)
Write an entry of the object dictionary, whatever its access over the bus.
int pbl_canopen_start(struct pbl_canopen *co)
Start the node: join the bus, restore every default and go pre-operational.

The node runs on a thread of its own, which also calls the callbacks. Calls return 0 or a negative errno.


Data Structure Documentation

◆ pbl_canopen_od_entry

struct pbl_canopen_od_entry

An object dictionary entry: one sub-index of an object.

Data Fields
uint8_t attr Attributes, PBL_CANOPEN_ATTR_* flags.
void * data Value, in the CPU's byte order.
uint16_t index Index.
uint16_t size Size of the value in bytes, the largest one for strings.
uint8_t subindex Sub-index.
uint8_t type Data type, pbl_canopen_type.

◆ pbl_canopen_od

struct pbl_canopen_od

An object dictionary, generated by pbl_canopen_od().

Its values live in two areas, communication (0x1000-0x1fff) and application (0x2000-0xffff), each restored from its defaults on the matching reset.

Data Fields
void * app Application area values.
const void * app_defaults Application area defaults.
size_t app_size Application area size in bytes.
uint8_t * buf Buffer for SDO transfers, as large as the largest entry.
size_t buf_size Size of buf in bytes.
void * comm Communication area values.
const void * comm_defaults Communication area defaults.
size_t comm_size Communication area size in bytes.
const struct pbl_canopen_od_entry * entries Entries, sorted by index and sub-index.
struct pbl_canopen__hb_consumer * hb_consumers Heartbeat consumers.
uint16_t num_entries Number of entries.
uint8_t num_errors Depth of the error history, object 0x1003, 0 without it.
uint8_t num_hb_consumers Number of heartbeat consumers, entries of object 0x1016.
uint16_t num_rpdos Number of RPDOs.
uint16_t num_tpdos Number of TPDOs.
struct pbl_canopen__rpdo * rpdos RPDOs.
struct pbl_canopen__tpdo * tpdos TPDOs.

◆ pbl_canopen_config

struct pbl_canopen_config

Node configuration.

Data Fields
void * arg Callback argument.
uint32_t bitrate Bit rate, in bits per second.
const struct pbl_can * can CAN controller, configured and started by the node.
const struct pbl_canopen_callbacks * cb Callbacks, or NULL.
uint8_t node_id Node-ID, 1 to 127.
const struct pbl_canopen_od * od Object dictionary, used by this node only.

Macro Definition Documentation

◆ PBL_CANOPEN_ATTR_NODEID

#define PBL_CANOPEN_ATTR_NODEID   PBL_BIT(4)

Default value plus the node-ID.

◆ PBL_CANOPEN_ATTR_READ

#define PBL_CANOPEN_ATTR_READ   PBL_BIT(0)

Readable over the bus.

◆ PBL_CANOPEN_ATTR_RPDO

#define PBL_CANOPEN_ATTR_RPDO   PBL_BIT(3)

Mappable to an RPDO.

◆ PBL_CANOPEN_ATTR_TPDO

#define PBL_CANOPEN_ATTR_TPDO   PBL_BIT(2)

Mappable to a TPDO.

◆ PBL_CANOPEN_ATTR_WRITE

#define PBL_CANOPEN_ATTR_WRITE   PBL_BIT(1)

Writable over the bus.

Enumeration Type Documentation

◆ pbl_canopen_hb_event

Heartbeat consumer events.

Enumerator
PBL_CANOPEN_HB_BOOT 

A monitored node sent its boot-up message.

PBL_CANOPEN_HB_STATE 

A monitored node changed state.

PBL_CANOPEN_HB_TIMEOUT 

A monitored node missed its heartbeat.

◆ pbl_canopen_nmt_state

NMT states, with their heartbeat codes.

Enumerator
PBL_CANOPEN_NMT_INITIALIZING 
PBL_CANOPEN_NMT_STOPPED 
PBL_CANOPEN_NMT_OPERATIONAL 
PBL_CANOPEN_NMT_PRE_OPERATIONAL 

◆ pbl_canopen_reset

Resets, as requested by the NMT master.

Enumerator
PBL_CANOPEN_RESET_COMM 

Communication parameters back to their defaults.

PBL_CANOPEN_RESET_NODE 

Communication and application parameters back to their defaults.

◆ pbl_canopen_type

Data types of object dictionary entries.

Enumerator
PBL_CANOPEN_TYPE_BOOLEAN 
PBL_CANOPEN_TYPE_INTEGER8 
PBL_CANOPEN_TYPE_INTEGER16 
PBL_CANOPEN_TYPE_INTEGER32 
PBL_CANOPEN_TYPE_UNSIGNED8 
PBL_CANOPEN_TYPE_UNSIGNED16 
PBL_CANOPEN_TYPE_UNSIGNED32 
PBL_CANOPEN_TYPE_REAL32 
PBL_CANOPEN_TYPE_VISIBLE_STRING 
PBL_CANOPEN_TYPE_OCTET_STRING 
PBL_CANOPEN_TYPE_REAL64 
PBL_CANOPEN_TYPE_INTEGER64 
PBL_CANOPEN_TYPE_UNSIGNED64 

Function Documentation

◆ pbl_canopen_emcy_clear()

int pbl_canopen_emcy_clear ( struct pbl_canopen *  co,
uint16_t  code 
)

Clear an error raised by pbl_canopen_emcy_raise(), and send an error reset message.

Parameters
coNode.
codeEmergency error code.
Returns
0, or -ENOENT if the error is not raised.

◆ pbl_canopen_emcy_raise()

int pbl_canopen_emcy_raise ( struct pbl_canopen *  co,
uint16_t  code,
uint8_t  reg,
const uint8_t *  msef 
)

Raise an error: set its error register bits, add it to the error history and send an emergency message.

Parameters
coNode.
codeEmergency error code.
regError register bits, besides the generic error bit.
msefManufacturer-specific error field, 5 bytes, or NULL.
Returns
0, -EALREADY if the error is raised, or -ENOSPC if too many errors are.

◆ pbl_canopen_init()

int pbl_canopen_init ( struct pbl_canopen *  co,
const struct pbl_canopen_config *  cfg 
)

Initialize a node, stopped.

Parameters
coNode.
cfgConfiguration, copied.
Returns
0, -EINVAL for an invalid configuration, -ENOTSUP for a dictionary with storage without CONFIG_CANOPEN_STORAGE, or another negative errno.

◆ pbl_canopen_nmt_state()

enum pbl_canopen_nmt_state pbl_canopen_nmt_state ( struct pbl_canopen *  co)

Get the NMT state.

Parameters
coNode.
Returns
NMT state, PBL_CANOPEN_NMT_INITIALIZING while stopped.

◆ pbl_canopen_od_read()

int pbl_canopen_od_read ( struct pbl_canopen *  co,
uint16_t  index,
uint8_t  subindex,
void *  data,
size_t  len 
)

Read an entry of the object dictionary.

Parameters
coNode.
indexIndex.
subindexSub-index.
[out]dataValue, in the CPU's byte order.
lenSize of data in bytes.
Returns
Size of the value in bytes, -ENOENT if there is no such entry, or -ENOSPC if data is too small for it.

◆ pbl_canopen_od_write()

int pbl_canopen_od_write ( struct pbl_canopen *  co,
uint16_t  index,
uint8_t  subindex,
const void *  data,
size_t  len 
)

Write an entry of the object dictionary, whatever its access over the bus.

A value that changes triggers the event-driven TPDOs that map the entry.

Parameters
coNode.
indexIndex.
subindexSub-index.
dataValue, in the CPU's byte order.
lenSize of the value in bytes: the entry's size, or up to it for strings.
Returns
0, -ENOENT if there is no such entry, or -EINVAL for a wrong size.

◆ pbl_canopen_start()

int pbl_canopen_start ( struct pbl_canopen *  co)

Start the node: join the bus, restore every default and go pre-operational.

Parameters
coNode.
Returns
0, -EALREADY if started, or a negative errno from the CAN controller.

◆ pbl_canopen_stop()

int pbl_canopen_stop ( struct pbl_canopen *  co)

Stop the node and leave the bus.

Parameters
coNode.
Returns
0 or a negative errno from the CAN controller.

◆ pbl_canopen_tpdo_request()

int pbl_canopen_tpdo_request ( struct pbl_canopen *  co,
uint16_t  num 
)

Request the transmission of a TPDO.

An event-driven TPDO goes as soon as its inhibit time allows, an acyclic synchronous one on the next SYNC.

Parameters
coNode.
numTPDO number, from 1.
Returns
0, -ENOENT if there is no such TPDO, or -ENOTSUP for a cyclic one.