Skip to content

Commit 92e646e

Browse files
author
rt-labs bot
committed
chore: catchup to d78cccc
d78cccc Make sure can frame is zero initialized e43524e New documentation templating 239170d Limit dlc to 8 in can reception 7a94f54 Add heartbeat state callback to public API 944ee04 Add heartbeat consumer state change callback 1b07023 Switch to using ticks instead of time in osal 249c12c Use Sphinx Press HTML theme ea2b832 Fredrik's documentation updates 2022-12-23 Based-On-Commit: d78cccc Change-Id: Iea72cb5f2dd4649edd6887fe9802a098a07e8b13
1 parent 26faaf3 commit 92e646e

26 files changed

+203
-108
lines changed

Diff for: .gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ CMakeCache.txt
66
install
77
docs/_build
88

9+
# Python files
10+
*env*
11+
912
.cproject
1013
.project
1114
.dir-locals.el

Diff for: include/co_api.h

+7
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,13 @@ typedef struct co_cfg
319319
/** Notify callback */
320320
void (*cb_notify) (co_net_t * net, uint16_t index, uint8_t subindex);
321321

322+
/** Heartbeat node state change callback */
323+
void (*cb_heartbeat_state) (
324+
co_net_t * net,
325+
uint8_t node,
326+
uint8_t old_state,
327+
uint8_t new_state);
328+
322329
/** Function to open dictionary store */
323330
void * (*open) (co_store_t store, co_mode_t mode);
324331

Diff for: src/co_emcy.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
#define os_channel_send mock_os_channel_send
1818
#define os_channel_get_state mock_os_channel_get_state
1919
#define os_channel_bus_on mock_os_channel_bus_on
20-
#define os_get_current_time_us mock_os_get_current_time_us
20+
#define os_tick_current mock_os_tick_current
21+
#define os_tick_from_us mock_os_tick_from_us
2122
#endif
2223

2324
#include "co_emcy.h"
@@ -232,7 +233,7 @@ int co_emcy_tx (co_net_t * net, uint16_t code, uint16_t info, uint8_t msef[5])
232233
uint8_t msg[8] = {0};
233234
uint8_t * p = msg;
234235
uint8_t reg;
235-
uint32_t now;
236+
os_tick_t now;
236237
bool error_behavior = false;
237238

238239
if (net->number_of_errors < MAX_ERRORS)
@@ -263,7 +264,7 @@ int co_emcy_tx (co_net_t * net, uint16_t code, uint16_t info, uint8_t msef[5])
263264
}
264265

265266
/* Send EMCY if inhibit time has expired */
266-
now = os_get_current_time_us();
267+
now = os_tick_current();
267268
if (co_is_expired (now, net->emcy.timestamp, 100 * net->emcy.inhibit))
268269
{
269270
LOG_ERROR (CO_EMCY_LOG, "emcy %x\n", code);
@@ -325,7 +326,7 @@ int co_emcy_rx (co_net_t * net, uint32_t id, uint8_t * msg, size_t dlc)
325326
void co_emcy_handle_can_state (co_net_t * net)
326327
{
327328
int status;
328-
uint32_t now = os_get_current_time_us();;
329+
os_tick_t now = os_tick_current();
329330
os_channel_state_t previous = net->emcy.state;
330331

331332
/* Get current state */

Diff for: src/co_heartbeat.c

+38-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
#ifdef UNIT_TEST
1717
#define os_channel_send mock_os_channel_send
18-
#define os_get_current_time_us mock_os_get_current_time_us
18+
#define os_tick_current mock_os_tick_current
19+
#define os_tick_from_us mock_os_tick_from_us
1920
#define co_emcy_tx mock_co_emcy_tx
2021
#endif
2122

@@ -111,17 +112,40 @@ int co_heartbeat_rx (co_net_t * net, uint8_t node, void * msg, size_t dlc)
111112
if (net->heartbeat[ix].node == node)
112113
{
113114
co_heartbeat_t * heartbeat = &net->heartbeat[ix];
115+
uint8_t state;
116+
117+
heartbeat->timestamp = os_tick_current();
118+
state = co_fetch_uint8 (msg);
119+
LOG_DEBUG (
120+
CO_HEARTBEAT_LOG,
121+
"node %d got heartbeat state %02x\n",
122+
heartbeat->node,
123+
heartbeat->state);
114124

115-
heartbeat->timestamp = os_get_current_time_us();
116-
heartbeat->is_alive = true;
117-
LOG_DEBUG (CO_HEARTBEAT_LOG, "node %d got heartbeat\n", heartbeat->node);
125+
/* Check for node state change */
126+
if (state != heartbeat->state)
127+
{
128+
LOG_DEBUG (
129+
CO_HEARTBEAT_LOG,
130+
"node %d heartbeat state change\n",
131+
heartbeat->node);
132+
133+
/* Call user callback */
134+
if (net->cb_heartbeat_state)
135+
{
136+
net->cb_heartbeat_state (net, node, heartbeat->state, state);
137+
}
138+
139+
/* Update node state */
140+
heartbeat->state = state;
141+
}
118142
}
119143
}
120144

121145
return 0;
122146
}
123147

124-
int co_heartbeat_timer (co_net_t * net, uint32_t now)
148+
int co_heartbeat_timer (co_net_t * net, os_tick_t now)
125149
{
126150
unsigned int ix;
127151
bool heartbeat_error = false;
@@ -171,21 +195,28 @@ int co_heartbeat_timer (co_net_t * net, uint32_t now)
171195
continue;
172196

173197
/* Check that heartbeat has not already expired */
174-
if (!heartbeat->is_alive)
198+
if (heartbeat->state == 0)
175199
continue;
176200

177201
/* Check heartbeat has not expired */
178202
if (co_is_expired (now, heartbeat->timestamp, 1000 * heartbeat->time))
179203
{
180204
/* Expired */
181-
heartbeat->is_alive = false;
182205
co_bitmap_clear (net->nodes, heartbeat->node);
183206
LOG_ERROR (
184207
CO_HEARTBEAT_LOG,
185208
"node %d heartbeat expired\n",
186209
heartbeat->node);
187210

211+
/* Call user callback */
212+
if (net->cb_heartbeat_state)
213+
{
214+
net->cb_heartbeat_state (net, heartbeat->node, heartbeat->state, 0);
215+
}
216+
217+
heartbeat->state = 0;
188218
heartbeat_error = true;
219+
189220
co_emcy_error_register_set (net, CO_ERR_COMMUNICATION);
190221
co_emcy_tx (net, 0x8130, 0, NULL);
191222
}

Diff for: src/co_heartbeat.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ int co_heartbeat_rx (co_net_t * net, uint8_t node, void * msg, size_t dlc);
5656
*
5757
* @return 0 on success, -1 on failure
5858
*/
59-
int co_heartbeat_timer (co_net_t * net, uint32_t now);
59+
int co_heartbeat_timer (co_net_t * net, os_tick_t now);
6060

6161
#ifdef __cplusplus
6262
}

Diff for: src/co_main.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ void co_handle_rx (co_net_t * net)
9898

9999
void co_handle_periodic (co_net_t * net)
100100
{
101-
uint32_t now = os_get_current_time_us();
101+
os_tick_t now = os_tick_current();
102102

103103
co_sdo_server_timer (net, now);
104104
co_sdo_client_timer (net, now);
@@ -278,7 +278,7 @@ int co_sdo_read (
278278
job->sdo.data = data;
279279
job->sdo.remain = size;
280280
job->callback = co_job_callback;
281-
job->timestamp = os_get_current_time_us();
281+
job->timestamp = os_tick_current();
282282
job->type = CO_JOB_SDO_READ;
283283

284284
os_mbox_post (net->mbox, job, OS_WAIT_FOREVER);
@@ -307,7 +307,7 @@ int co_sdo_write (
307307
job->sdo.data = (uint8_t *)data;
308308
job->sdo.remain = size;
309309
job->callback = co_job_callback;
310-
job->timestamp = os_get_current_time_us();
310+
job->timestamp = os_tick_current();
311311
job->type = CO_JOB_SDO_WRITE;
312312

313313
os_mbox_post (net->mbox, job, OS_WAIT_FOREVER);
@@ -421,6 +421,7 @@ co_net_t * co_init (const char * canif, const co_cfg_t * cfg)
421421
net->cb_sync = cfg->cb_sync;
422422
net->cb_emcy = cfg->cb_emcy;
423423
net->cb_notify = cfg->cb_notify;
424+
net->cb_heartbeat_state = cfg->cb_heartbeat_state;
424425

425426
net->restart_ms = cfg->restart_ms;
426427

Diff for: src/co_main.h

+16-9
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ typedef struct co_pdo
7474
uint8_t sync_counter;
7575
uint16_t inhibit_time;
7676
uint16_t event_timer;
77-
uint32_t timestamp;
77+
os_tick_t timestamp;
7878
uint64_t frame;
7979
size_t bitlength;
8080
uint8_t number_of_mappings;
@@ -149,7 +149,7 @@ typedef struct co_job
149149
co_emcy_job_t emcy;
150150
co_pdo_job_t pdo;
151151
};
152-
uint32_t timestamp;
152+
os_tick_t timestamp;
153153
struct co_client * client;
154154
void (*callback) (struct co_job * job);
155155
int result;
@@ -167,9 +167,9 @@ struct co_client
167167
typedef struct co_heartbeat
168168
{
169169
uint8_t node;
170-
bool is_alive;
170+
uint8_t state;
171171
uint16_t time;
172-
uint32_t timestamp;
172+
os_tick_t timestamp;
173173
} co_heartbeat_t;
174174

175175
/** Node guarding state */
@@ -179,7 +179,7 @@ typedef struct co_node_guard
179179
uint16_t guard_time;
180180
uint8_t life_time_factor;
181181
uint8_t toggle;
182-
uint32_t timestamp;
182+
os_tick_t timestamp;
183183
} co_node_guard_t;
184184

185185
/** LSS states */
@@ -206,14 +206,14 @@ typedef struct co_sync
206206
uint8_t counter;
207207
uint8_t overflow;
208208
uint32_t period;
209-
uint32_t timestamp;
209+
os_tick_t timestamp;
210210
} co_sync_t;
211211

212212
/** EMCY state */
213213
typedef struct co_emcy
214214
{
215215
uint32_t cobid; /**< EMCY COB ID */
216-
uint32_t timestamp; /**< Timestamp of last EMCY */
216+
os_tick_t timestamp; /**< Timestamp of last EMCY */
217217
uint32_t bus_off_timestamp; /**< Timestamp of bus-off event */
218218
uint16_t inhibit; /**< Inhibit time [100 us] */
219219
uint8_t error; /**< Error register */
@@ -238,9 +238,9 @@ struct co_net
238238
co_emcy_t emcy; /**< EMCY state */
239239
co_sync_t sync; /**< SYNC state */
240240
co_state_t state; /**< NMT state */
241-
uint32_t hb_timestamp; /**< Heartbeat producer timestamp */
241+
os_tick_t hb_timestamp; /**< Heartbeat producer timestamp */
242242
uint32_t hb_time; /**< Heartbeat producer time */
243-
uint32_t sync_timestamp; /**< Timestamp of last SYNC */
243+
os_tick_t sync_timestamp; /**< Timestamp of last SYNC */
244244
uint32_t sync_window; /**< Synchronous window length */
245245
uint32_t restart_ms; /**< Delay before attempting to recover from bus-off */
246246
co_pdo_t pdo_tx[MAX_TX_PDO]; /**< TPDOs */
@@ -279,6 +279,13 @@ struct co_net
279279
/** Notify callback */
280280
void (*cb_notify) (co_net_t * net, uint16_t index, uint8_t subindex);
281281

282+
/** Heartbeat node state change callback */
283+
void (*cb_heartbeat_state) (
284+
co_net_t * net,
285+
uint8_t node,
286+
uint8_t old_state,
287+
uint8_t new_state);
288+
282289
/** Function to open dictionary store */
283290
void * (*open) (co_store_t store, co_mode_t mode);
284291

Diff for: src/co_node_guard.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
#ifdef UNIT_TEST
1717
#define os_channel_send mock_os_channel_send
1818
#define os_channel_receive mock_os_channel_receive
19-
#define os_get_current_time_us mock_os_get_current_time_us
19+
#define os_tick_current mock_os_tick_current
20+
#define os_tick_from_us mock_os_tick_from_us
2021
#endif
2122

2223
#include "co_node_guard.h"
@@ -84,7 +85,7 @@ int co_node_guard_rx (co_net_t * net, uint32_t id, void * msg, size_t dlc)
8485
return -1;
8586

8687
net->node_guard.is_alive = true;
87-
net->node_guard.timestamp = os_get_current_time_us();
88+
net->node_guard.timestamp = os_tick_current();
8889

8990
/* Heartbeat producer (heartbeat is prioritised over node guarding)*/
9091
if (net->hb_time == 0)
@@ -113,7 +114,7 @@ int co_node_guard_rx (co_net_t * net, uint32_t id, void * msg, size_t dlc)
113114
return 0;
114115
}
115116

116-
int co_node_guard_timer (co_net_t * net, uint32_t now)
117+
int co_node_guard_timer (co_net_t * net, os_tick_t now)
117118
{
118119
uint32_t guard_factor =
119120
(net->node_guard.guard_time * net->node_guard.life_time_factor);

Diff for: src/co_node_guard.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ int co_node_guard_rx (co_net_t * net, uint32_t id, void * msg, size_t dlc);
5555
*
5656
* @return 0 on success, -1 on failure
5757
*/
58-
int co_node_guard_timer (co_net_t * net, uint32_t now);
58+
int co_node_guard_timer (co_net_t * net, os_tick_t now);
5959

6060
#ifdef __cplusplus
6161
}

Diff for: src/co_pdo.c

+9-8
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
#ifdef UNIT_TEST
1717
#define os_channel_send mock_os_channel_send
18-
#define os_get_current_time_us mock_os_get_current_time_us
18+
#define os_tick_current mock_os_tick_current
19+
#define os_tick_from_us mock_os_tick_from_us
1920
#define co_obj_find mock_co_obj_find
2021
#define co_entry_find mock_co_entry_find
2122
#define co_emcy_tx mock_co_emcy_tx
@@ -532,7 +533,7 @@ uint32_t co_od1A00_fn (
532533
static void co_pdo_transmit (co_net_t * net, co_pdo_t * pdo)
533534
{
534535
size_t dlc;
535-
uint32_t now = os_get_current_time_us();
536+
os_tick_t now = os_tick_current();
536537

537538
if (IS_EVENT (pdo->transmission_type) && pdo->inhibit_time > 0)
538539
{
@@ -549,7 +550,7 @@ static void co_pdo_transmit (co_net_t * net, co_pdo_t * pdo)
549550
pdo->queued = false;
550551
}
551552

552-
int co_pdo_timer (co_net_t * net, uint32_t now)
553+
int co_pdo_timer (co_net_t * net, os_tick_t now)
553554
{
554555
unsigned int ix;
555556

@@ -659,7 +660,7 @@ int co_pdo_sync (co_net_t * net, uint8_t * msg, size_t dlc)
659660
if (net->state != STATE_OP)
660661
return -1;
661662

662-
net->sync_timestamp = os_get_current_time_us();
663+
net->sync_timestamp = os_tick_current();
663664

664665
/* Transmit TPDOs */
665666
for (ix = 0; ix < MAX_TX_PDO; ix++)
@@ -735,7 +736,7 @@ int co_pdo_sync (co_net_t * net, uint8_t * msg, size_t dlc)
735736
void co_pdo_rx (co_net_t * net, uint32_t id, void * msg, size_t dlc)
736737
{
737738
unsigned int ix;
738-
uint32_t now;
739+
os_tick_t now;
739740

740741
/* Check state */
741742
if (net->state != STATE_OP)
@@ -762,7 +763,7 @@ void co_pdo_rx (co_net_t * net, uint32_t id, void * msg, size_t dlc)
762763
/* Transmit value sampled at previous SYNC */
763764
dlc = CO_BYTELENGTH (pdo->bitlength);
764765
os_channel_send (net->channel, pdo->cobid, &pdo->frame, dlc);
765-
pdo->timestamp = os_get_current_time_us();
766+
pdo->timestamp = os_tick_current();
766767
pdo->queued = false;
767768
}
768769
}
@@ -787,14 +788,14 @@ void co_pdo_rx (co_net_t * net, uint32_t id, void * msg, size_t dlc)
787788
if (pdo->transmission_type <= CO_PDO_TT_CYCLIC_MAX && net->sync_window > 0)
788789
{
789790
/* Check that sync window has not expired */
790-
now = os_get_current_time_us();
791+
now = os_tick_current();
791792
if (co_is_expired (now, net->sync_timestamp, net->sync_window))
792793
continue;
793794
}
794795

795796
/* Buffer frame */
796797
memcpy (&pdo->frame, msg, dlc);
797-
pdo->timestamp = os_get_current_time_us();
798+
pdo->timestamp = os_tick_current();
798799

799800
if (IS_EVENT (pdo->transmission_type))
800801
{

Diff for: src/co_pdo.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ void co_pdo_rx (co_net_t * net, uint32_t id, void * msg, size_t dlc);
103103
*
104104
* @return 0 on success, -1 on failure
105105
*/
106-
int co_pdo_timer (co_net_t * net, uint32_t now);
106+
int co_pdo_timer (co_net_t * net, os_tick_t now);
107107

108108
/**
109109
* PDO trigger

0 commit comments

Comments
 (0)