Lely core libraries 2.3.4
emcy.c File Reference

This file is part of the CANopen library; it contains the implementation of the emergency (EMCY) object functions. More...

#include "co.h"
#include <lely/can/buf.h>
#include <lely/co/dev.h>
#include <lely/co/emcy.h>
#include <lely/co/obj.h>
#include <lely/co/sdo.h>
#include <lely/co/val.h>
#include <lely/util/diag.h>
#include <lely/util/endian.h>
#include <lely/util/time.h>
#include <assert.h>
#include <stdlib.h>
Include dependency graph for emcy.c:

Go to the source code of this file.

Data Structures

struct  co_emcy_msg
 An EMCY message. More...
 
struct  co_emcy_node
 A remote CANopen EMCY producer node. More...
 
struct  __co_emcy
 A CANopen EMCY producer/consumer service. More...
 
struct  co_1003
 The pre-defined error field. More...
 

Functions

static int co_emcy_node_recv (const struct can_msg *msg, void *data)
 The CAN receive callback function for a remote CANopen EMCY producer node.
 
static int co_emcy_set_1003 (co_emcy_t *emcy)
 Sets the value of CANopen object 1003 (Pre-defined error field).
 
static co_unsigned32_t co_1003_dn_ind (co_sub_t *sub, struct co_sdo_req *req, void *data)
 The download indication function for (all sub-objects of) CANopen object 1003 (Pre-defined error field).
 
static co_unsigned32_t co_1014_dn_ind (co_sub_t *sub, struct co_sdo_req *req, void *data)
 The download indication function for (all sub-objects of) CANopen object 1014 (COB-ID emergency message).
 
static void co_emcy_set_1028 (co_emcy_t *emcy, co_unsigned8_t id, co_unsigned32_t cobid)
 Sets the value of CANopen object 1028 (Emergency consumer object).
 
static co_unsigned32_t co_1028_dn_ind (co_sub_t *sub, struct co_sdo_req *req, void *data)
 The download indication function for (all sub-objects of) CANopen object 1028 (Emergency consumer object).
 
static int co_emcy_timer (const struct timespec *tp, void *data)
 The CAN timer callback function for an EMCY service.
 
static int co_emcy_send (co_emcy_t *emcy, co_unsigned16_t eec, co_unsigned8_t er, const co_unsigned8_t msef[5])
 Adds an EMCY message to the CAN frame buffer and sends it if possible.
 
static void co_emcy_flush (co_emcy_t *emcy)
 Sends any messages in the CAN frame buffer unless the inhibit time has not yet elapsed, in which case it sets the CAN timer.
 
co_emcy_tco_emcy_create (can_net_t *net, co_dev_t *dev)
 Creates a new CANopen EMCY producer/consumer service.
 
void co_emcy_destroy (co_emcy_t *emcy)
 Destroys a CANopen EMCY producer/consumer service.
 
int co_emcy_start (co_emcy_t *emcy)
 Starts an EMCY service.
 
void co_emcy_stop (co_emcy_t *emcy)
 Stops an EMCY service.
 
int co_emcy_is_stopped (const co_emcy_t *emcy)
 Retuns 1 if the specified EMCY service is stopped, and 0 if not.
 
can_net_tco_emcy_get_net (const co_emcy_t *emcy)
 Returns a pointer to the CAN network of an EMCY producer/consumer service.
 
co_dev_tco_emcy_get_dev (const co_emcy_t *emcy)
 Returns a pointer to the CANopen device of an EMCY producer/consumer service.
 
int co_emcy_push (co_emcy_t *emcy, co_unsigned16_t eec, co_unsigned8_t er, const co_unsigned8_t msef[5])
 Pushes a CANopen EMCY message to the stack and broadcasts it if the EMCY producer service is active.
 
int co_emcy_pop (co_emcy_t *emcy, co_unsigned16_t *peec, co_unsigned8_t *per)
 Pops the most recent CANopen EMCY message from the stack and broadcasts an 'error reset' message if the EMCY producer service is active.
 
void co_emcy_peek (const co_emcy_t *emcy, co_unsigned16_t *peec, co_unsigned8_t *per)
 Retrieves, but does not pop, the most recent CANopen EMCY message from the stack.
 
int co_emcy_remove (co_emcy_t *emcy, size_t n)
 Pops a CANopen EMCY message from the stack, even if it is not the most recent message, and broadcasts an 'error reset' message if the EMCY producer service is active.
 
ssize_t co_emcy_find (const co_emcy_t *emcy, co_unsigned16_t eec)
 Finds a CANopen EMCY message in the stack.
 
int co_emcy_clear (co_emcy_t *emcy)
 Clears the CANopen EMCY message stack and broadcasts the 'error reset/no error' message if the EMCY producer service is active.
 
void co_emcy_get_ind (const co_emcy_t *emcy, co_emcy_ind_t **pind, void **pdata)
 Retrieves the indication function invoked when a CANopen EMCY message is received.
 
void co_emcy_set_ind (co_emcy_t *emcy, co_emcy_ind_t *ind, void *data)
 Sets the indication function invoked when a CANopen EMCY message is received.
 

Detailed Description

This file is part of the CANopen library; it contains the implementation of the emergency (EMCY) object functions.

See also
lely/co/emcy.h
Author
J. S. Seldenthuis jseld.nosp@m.enth.nosp@m.uis@l.nosp@m.ely..nosp@m.com

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Definition in file emcy.c.

Function Documentation

◆ co_emcy_node_recv()

static int co_emcy_node_recv ( const struct can_msg msg,
void *  data 
)
static

The CAN receive callback function for a remote CANopen EMCY producer node.

See also
can_recv_func_t

Definition at line 634 of file emcy.c.

◆ co_emcy_set_1003()

static int co_emcy_set_1003 ( co_emcy_t emcy)
static

Sets the value of CANopen object 1003 (Pre-defined error field).

This function copies the list of active EMCY messages to the array at object 1003.

Returns
0 on success, or -1 on error.

Definition at line 672 of file emcy.c.

◆ co_1003_dn_ind()

static co_unsigned32_t co_1003_dn_ind ( co_sub_t sub,
struct co_sdo_req req,
void *  data 
)
static

The download indication function for (all sub-objects of) CANopen object 1003 (Pre-defined error field).

See also
co_sub_dn_ind_t

Definition at line 693 of file emcy.c.

◆ co_1014_dn_ind()

static co_unsigned32_t co_1014_dn_ind ( co_sub_t sub,
struct co_sdo_req req,
void *  data 
)
static

The download indication function for (all sub-objects of) CANopen object 1014 (COB-ID emergency message).

See also
co_sub_dn_ind_t

Definition at line 726 of file emcy.c.

◆ co_emcy_set_1028()

static void co_emcy_set_1028 ( co_emcy_t emcy,
co_unsigned8_t  id,
co_unsigned32_t  cobid 
)
static

Sets the value of CANopen object 1028 (Emergency consumer object).

Parameters
emcya pointer to an EMCY service.
idthe node-ID.
cobidthe COB-ID of the EMCY object.

Definition at line 769 of file emcy.c.

◆ co_1028_dn_ind()

static co_unsigned32_t co_1028_dn_ind ( co_sub_t sub,
struct co_sdo_req req,
void *  data 
)
static

The download indication function for (all sub-objects of) CANopen object 1028 (Emergency consumer object).

See also
co_sub_dn_ind_t

Definition at line 793 of file emcy.c.

◆ co_emcy_timer()

static int co_emcy_timer ( const struct timespec tp,
void *  data 
)
static

The CAN timer callback function for an EMCY service.

See also
can_timer_func_t

Definition at line 844 of file emcy.c.

◆ co_emcy_send()

static int co_emcy_send ( co_emcy_t emcy,
co_unsigned16_t  eec,
co_unsigned8_t  er,
const co_unsigned8_t  msef[5] 
)
static

Adds an EMCY message to the CAN frame buffer and sends it if possible.

Parameters
emcya pointer to an EMCY service.
eecthe emergency error code.
erthe error register.
msefthe manufacturer-specific error code (can be NULL).
Returns
0 on success, or -1 on error.

Definition at line 856 of file emcy.c.

◆ co_emcy_create()

co_emcy_t * co_emcy_create ( can_net_t net,
co_dev_t dev 
)

Creates a new CANopen EMCY producer/consumer service.

The service is started as if by co_emcy_start().

Parameters
neta pointer to a CAN network.
deva pointer to a CANopen device.
Returns
a pointer to a new EMCY service, or NULL on error. In the latter case, the error number can be obtained with get_errc().
See also
co_emcy_destroy()

Definition at line 333 of file emcy.c.

◆ co_emcy_destroy()

void co_emcy_destroy ( co_emcy_t emcy)

Destroys a CANopen EMCY producer/consumer service.

See also
co_emcy_create()

Definition at line 360 of file emcy.c.

◆ co_emcy_start()

int co_emcy_start ( co_emcy_t emcy)

Starts an EMCY service.

Postcondition
on success, co_emcy_is_stopped() returns 0.
Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().
See also
co_emcy_stop()

Definition at line 370 of file emcy.c.

◆ co_emcy_stop()

void co_emcy_stop ( co_emcy_t emcy)

Stops an EMCY service.

Postcondition
co_emcy_is_stopped() returns 1.
See also
co_emcy_start()

Definition at line 410 of file emcy.c.

◆ co_emcy_is_stopped()

int co_emcy_is_stopped ( const co_emcy_t emcy)

Retuns 1 if the specified EMCY service is stopped, and 0 if not.

See also
co_emcy_start, co_emcy_stop()

Definition at line 446 of file emcy.c.

◆ co_emcy_push()

int co_emcy_push ( co_emcy_t emcy,
co_unsigned16_t  eec,
co_unsigned8_t  er,
const co_unsigned8_t  msef[5] 
)

Pushes a CANopen EMCY message to the stack and broadcasts it if the EMCY producer service is active.

Parameters
emcya pointer to an EMCY producer service.
eecthe emergency error code.
erthe error register.
msefthe manufacturer-specific error code (can be NULL).
Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().
See also
co_emcy_pop(), co_emcy_peek(), co_emcy_clear()

Definition at line 470 of file emcy.c.

◆ co_emcy_pop()

int co_emcy_pop ( co_emcy_t emcy,
co_unsigned16_t *  peec,
co_unsigned8_t *  per 
)

Pops the most recent CANopen EMCY message from the stack and broadcasts an 'error reset' message if the EMCY producer service is active.

Parameters
emcya pointer to an EMCY producer service.
peecthe address at which to store the emergency error code (can be NULL).
perthe address at which to store the error register (can be NULL).
Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().
See also
co_emcy_push(), co_emcy_peek(), co_emcy_clear()

Definition at line 526 of file emcy.c.

◆ co_emcy_peek()

void co_emcy_peek ( const co_emcy_t emcy,
co_unsigned16_t *  peec,
co_unsigned8_t *  per 
)

Retrieves, but does not pop, the most recent CANopen EMCY message from the stack.

Parameters
emcya pointer to an EMCY producer service.
peecthe address at which to store the emergency error code (can be NULL).
perthe address at which to store the error register (can be NULL).
See also
co_emcy_push(), co_emcy_pop(), co_emcy_clear()

Definition at line 536 of file emcy.c.

◆ co_emcy_remove()

int co_emcy_remove ( co_emcy_t emcy,
size_t  n 
)

Pops a CANopen EMCY message from the stack, even if it is not the most recent message, and broadcasts an 'error reset' message if the EMCY producer service is active.

Parameters
emcya pointer to an EMCY producer service.
nthe index of the message to be popped, where 0 is the most recent message.
Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().
See also
co_emcy_find()

Definition at line 551 of file emcy.c.

◆ co_emcy_find()

ssize_t co_emcy_find ( const co_emcy_t emcy,
co_unsigned16_t  eec 
)

Finds a CANopen EMCY message in the stack.

This function returns the index of the first matching message in LIFO order.

Parameters
emcya pointer to an EMCY consumer service.
eecthe emergency error code of the message to be found.
Returns
the index of the first matching message, or -1 if not found.
See also
co_emcy_remove()

Definition at line 582 of file emcy.c.

◆ co_emcy_clear()

int co_emcy_clear ( co_emcy_t emcy)

Clears the CANopen EMCY message stack and broadcasts the 'error reset/no error' message if the EMCY producer service is active.

Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().
See also
co_emcy_push(), co_emcy_pop(), co_emcy_peek()

Definition at line 595 of file emcy.c.

◆ co_emcy_get_ind()

void co_emcy_get_ind ( const co_emcy_t emcy,
co_emcy_ind_t **  pind,
void **  pdata 
)

Retrieves the indication function invoked when a CANopen EMCY message is received.

Parameters
emcya pointer to an EMCY consumer service.
pindthe address at which to store a pointer to the indication function (can be NULL).
pdatathe address at which to store a pointer to user-specified data (can be NULL).
See also
co_emcy_set_ind()

Definition at line 614 of file emcy.c.

◆ co_emcy_set_ind()

void co_emcy_set_ind ( co_emcy_t emcy,
co_emcy_ind_t ind,
void *  data 
)

Sets the indication function invoked when a CANopen EMCY message is received.

Parameters
emcya pointer to an EMCY consumer service.
inda pointer to the function to be invoked.
dataa pointer to user-specified data (can be NULL). data is passed as the last parameter to func.
See also
co_emcy_get_ind()

Definition at line 625 of file emcy.c.