IgH EtherCAT Master  1.6.2
coe_emerg_ring.c
Go to the documentation of this file.
1 /*****************************************************************************
2  *
3  * Copyright (C) 2012 Florian Pose, Ingenieurgemeinschaft IgH
4  *
5  * This file is part of the IgH EtherCAT Master.
6  *
7  * The IgH EtherCAT Master is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License version 2, as
9  * published by the Free Software Foundation.
10  *
11  * The IgH EtherCAT Master is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14  * Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with the IgH EtherCAT Master; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  *
20  * vim: expandtab
21  *
22  ****************************************************************************/
23 
28 /****************************************************************************/
29 
30 #include <linux/slab.h>
31 
32 #include "coe_emerg_ring.h"
33 
34 /****************************************************************************/
35 
39  ec_coe_emerg_ring_t *ring,
41  )
42 {
43  ring->sc = sc;
44  ring->msgs = NULL;
45  ring->size = 0;
46  ring->read_index = 0;
47  ring->write_index = 0;
48  ring->overruns = 0;
49 }
50 
51 /****************************************************************************/
52 
56  ec_coe_emerg_ring_t *ring
57  )
58 {
59  if (ring->msgs) {
60  kfree(ring->msgs);
61  }
62 }
63 
64 /****************************************************************************/
65 
71  ec_coe_emerg_ring_t *ring,
72  size_t size
73  )
74 {
75  ring->size = 0;
76 
77  if (size < 0) {
78  size = 0;
79  }
80 
81  ring->read_index = ring->write_index = 0;
82 
83  if (ring->msgs) {
84  kfree(ring->msgs);
85  }
86  ring->msgs = NULL;
87 
88  if (size == 0) {
89  return 0;
90  }
91 
92  ring->msgs = kmalloc(sizeof(ec_coe_emerg_msg_t) * (size + 1), GFP_KERNEL);
93  if (!ring->msgs) {
94  return -ENOMEM;
95  }
96 
97  ring->size = size;
98  return 0;
99 }
100 
101 /****************************************************************************/
102 
106  ec_coe_emerg_ring_t *ring,
107  const u8 *msg
108  )
109 {
110  if (!ring->size ||
111  (ring->write_index + 1) % (ring->size + 1) == ring->read_index) {
112  ring->overruns++;
113  return;
114  }
115 
116  memcpy(ring->msgs[ring->write_index].data, msg,
118  ring->write_index = (ring->write_index + 1) % (ring->size + 1);
119 }
120 
121 /****************************************************************************/
122 
128  ec_coe_emerg_ring_t *ring,
129  u8 *msg
130  )
131 {
132  if (ring->read_index == ring->write_index) {
133  return -ENOENT;
134  }
135 
136  memcpy(msg, ring->msgs[ring->read_index].data, EC_COE_EMERGENCY_MSG_SIZE);
137  ring->read_index = (ring->read_index + 1) % (ring->size + 1);
138  return 0;
139 }
140 
141 /****************************************************************************/
142 
148  ec_coe_emerg_ring_t *ring
149  )
150 {
151  ring->read_index = ring->write_index;
152  ring->overruns = 0;
153  return 0;
154 }
155 
156 /****************************************************************************/
157 
163  const ec_coe_emerg_ring_t *ring
164  )
165 {
166  return ring->overruns;
167 }
168 
169 /****************************************************************************/
unsigned int write_index
Write index.
u8 data[EC_COE_EMERGENCY_MSG_SIZE]
Message data.
ec_coe_emerg_msg_t * msgs
Message ring.
size_t size
Ring size.
EtherCAT CoE emergency ring buffer structure.
EtherCAT CoE emergency ring buffer.
void ec_coe_emerg_ring_init(ec_coe_emerg_ring_t *ring, ec_slave_config_t *sc)
Emergency ring buffer constructor.
int ec_coe_emerg_ring_clear_ring(ec_coe_emerg_ring_t *ring)
Clear the ring.
EtherCAT CoE emergency message record.
void ec_coe_emerg_ring_push(ec_coe_emerg_ring_t *ring, const u8 *msg)
Add a new emergency message.
void ec_coe_emerg_ring_clear(ec_coe_emerg_ring_t *ring)
Emergency ring buffer destructor.
unsigned int overruns
Number of overruns since last reset.
int ec_coe_emerg_ring_overruns(const ec_coe_emerg_ring_t *ring)
Read the number of overruns.
unsigned int read_index
Read index.
ec_slave_config_t * sc
Slave configuration owning the ring.
EtherCAT slave configuration.
Definition: slave_config.h:111
#define EC_COE_EMERGENCY_MSG_SIZE
Size of a CoE emergency message in byte.
Definition: ecrt.h:293
int ec_coe_emerg_ring_size(ec_coe_emerg_ring_t *ring, size_t size)
Set the ring size.
int ec_coe_emerg_ring_pop(ec_coe_emerg_ring_t *ring, u8 *msg)
Remove an emergency message from the ring.