Skip to content

Commit 78ff13a

Browse files
committed
Add type Integer48
Let's add integer48 type by using uint64_t as base data type. Signed-off-by: Giulio Benetti <giulio.benetti@benettiengineering.com>
1 parent f4e4db8 commit 78ff13a

File tree

4 files changed

+191
-5
lines changed

4 files changed

+191
-5
lines changed

src/core/co_core.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ extern "C" {
3434
#include "co_integer8.h"
3535
#include "co_integer16.h"
3636
#include "co_integer32.h"
37+
#include "co_integer48.h"
3738

3839
/* cia301 types */
3940
#include "co_emcy_hist.h"

src/object/basic/co_integer48.c

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/******************************************************************************
2+
Copyright 2020 Embedded Office GmbH & Co. KG
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
******************************************************************************/
16+
17+
/******************************************************************************
18+
* INCLUDES
19+
******************************************************************************/
20+
21+
#include "co_core.h"
22+
23+
/******************************************************************************
24+
* PRIVATE DEFINES
25+
******************************************************************************/
26+
27+
#define COT_ENTRY_SIZE (uint32_t)6
28+
29+
/******************************************************************************
30+
* PRIVATE FUNCTIONS
31+
******************************************************************************/
32+
33+
static uint32_t COTInt48Size (struct CO_OBJ_T *obj, struct CO_NODE_T *node, uint32_t width);
34+
static CO_ERR COTInt48Read (struct CO_OBJ_T *obj, struct CO_NODE_T *node, void *buffer, uint32_t size);
35+
static CO_ERR COTInt48Write(struct CO_OBJ_T *obj, struct CO_NODE_T *node, void *buffer, uint32_t size);
36+
37+
/******************************************************************************
38+
* PUBLIC GLOBALS
39+
******************************************************************************/
40+
41+
const CO_OBJ_TYPE COTInt48 = { COTInt48Size, 0, COTInt48Read, COTInt48Write, 0 };
42+
43+
/******************************************************************************
44+
* FUNCTIONS
45+
******************************************************************************/
46+
47+
static uint32_t COTInt48Size(struct CO_OBJ_T *obj, struct CO_NODE_T *node, uint32_t width)
48+
{
49+
uint32_t result = (uint32_t)0;
50+
51+
CO_UNUSED(node);
52+
CO_UNUSED(width);
53+
54+
if (CO_IS_DIRECT(obj->Key) != 0) {
55+
result = (uint32_t)COT_ENTRY_SIZE;
56+
} else {
57+
/* check for valid reference */
58+
if ((obj->Data) != (CO_DATA)0) {
59+
result = (uint32_t)COT_ENTRY_SIZE;
60+
}
61+
}
62+
return (result);
63+
}
64+
65+
static CO_ERR COTInt48Read(struct CO_OBJ_T *obj, struct CO_NODE_T *node, void *buffer, uint32_t size)
66+
{
67+
CO_ERR result = CO_ERR_NONE;
68+
uint64_t value;
69+
70+
CO_UNUSED(node);
71+
ASSERT_PTR_ERR(obj, CO_ERR_BAD_ARG);
72+
ASSERT_PTR_ERR(buffer, CO_ERR_BAD_ARG);
73+
74+
if (CO_IS_DIRECT(obj->Key) != 0) {
75+
value = (uint64_t)(obj->Data);
76+
} else {
77+
value = *((uint64_t *)(obj->Data));
78+
}
79+
if (CO_IS_NODEID(obj->Key) != 0) {
80+
value += node->NodeId;
81+
}
82+
if (size == COT_ENTRY_SIZE) {
83+
*((uint64_t *)buffer) = value;
84+
} else {
85+
result = CO_ERR_BAD_ARG;
86+
}
87+
return (result);
88+
}
89+
90+
static CO_ERR COTInt48Write(struct CO_OBJ_T *obj, struct CO_NODE_T *node, void *buffer, uint32_t size)
91+
{
92+
CO_ERR result = CO_ERR_NONE;
93+
uint64_t value;
94+
uint64_t oldValue;
95+
96+
CO_UNUSED(node);
97+
ASSERT_PTR_ERR(obj, CO_ERR_BAD_ARG);
98+
ASSERT_PTR_ERR(buffer, CO_ERR_BAD_ARG);
99+
100+
value = *((uint64_t *)buffer);
101+
if (size == COT_ENTRY_SIZE) {
102+
if (CO_IS_NODEID(obj->Key) != 0) {
103+
value -= node->NodeId;
104+
}
105+
if (CO_IS_DIRECT(obj->Key) != 0) {
106+
oldValue = (uint64_t)obj->Data;
107+
obj->Data = (CO_DATA)(value);
108+
} else {
109+
oldValue = *((uint64_t *)(obj->Data));
110+
*((uint64_t *)(obj->Data)) = value;
111+
}
112+
if ((CO_IS_ASYNC(obj->Key) != 0 ) &&
113+
(CO_IS_PDOMAP(obj->Key) != 0 ) &&
114+
(oldValue != value)) {
115+
COTPdoTrigObj(node->TPdo, obj);
116+
}
117+
} else {
118+
result = CO_ERR_BAD_ARG;
119+
}
120+
return (result);
121+
}

src/object/basic/co_integer48.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/******************************************************************************
2+
Copyright 2020 Embedded Office GmbH & Co. KG
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
******************************************************************************/
16+
17+
#ifndef CO_INTEGER48_H_
18+
#define CO_INTEGER48_H_
19+
20+
#ifdef __cplusplus /* for compatibility with C++ environments */
21+
extern "C" {
22+
#endif
23+
24+
/******************************************************************************
25+
* INCLUDES
26+
******************************************************************************/
27+
28+
#include "co_types.h"
29+
#include "co_err.h"
30+
#include "co_obj.h"
31+
32+
/******************************************************************************
33+
* DEFINES
34+
******************************************************************************/
35+
36+
#define CO_TUNSIGNED48 ((const CO_OBJ_TYPE *)&COTInt48)
37+
#define CO_TSIGNED48 ((const CO_OBJ_TYPE *)&COTInt48)
38+
39+
/******************************************************************************
40+
* PUBLIC CONSTANTS
41+
******************************************************************************/
42+
43+
/*! \brief OBJECT TYPE SIGNED48 / UNSIGNED48
44+
*
45+
* This type is a basic type for 48bit values, regardless of signed or
46+
* unsigned usage.
47+
*/
48+
extern const CO_OBJ_TYPE COTInt48;
49+
50+
#ifdef __cplusplus /* for compatibility with C++ environments */
51+
}
52+
#endif
53+
54+
#endif /* #ifndef CO_INTEGER48_H_ */

src/service/cia301/co_pdo.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ void COTPdoTx(CO_TPDO *pdo)
269269
CO_IF_FRM frm;
270270
uint32_t sz;
271271
uint8_t pdosz;
272-
uint32_t data;
272+
uint64_t data;
273273
uint8_t num;
274274

275275
if ((pdo->Node->Nmt.Allowed & CO_PDO_ALLOWED) == 0) {
@@ -310,8 +310,8 @@ void COTPdoTx(CO_TPDO *pdo)
310310
frm.DLC = 0;
311311
for (num = 0; num < pdo->ObjNum; num++) {
312312
pdosz = pdo->Size[num];
313-
if (pdosz <= 4) {
314-
/* supported mapping: 1 to 4 bytes */
313+
if (pdosz <= 6) {
314+
/* supported mapping: 1 to 6 bytes */
315315
sz = COObjGetSize(pdo->Map[num], pdo->Node, 0L);
316316
if (sz <= (uint32_t)(8 - frm.DLC)) {
317317
if (pdosz == 3) {
@@ -331,6 +331,9 @@ void COTPdoTx(CO_TPDO *pdo)
331331
} else {
332332
CO_SET_LONG(&frm, data, frm.DLC);
333333
}
334+
} else if (sz == 6u) {
335+
CO_SET_WORD(&frm, data, frm.DLC);
336+
CO_SET_LONG(&frm, (data >> 16), (frm.DLC + 2));
334337
}
335338
frm.DLC += pdosz;
336339
}
@@ -572,6 +575,7 @@ CO_RPDO *CORPdoCheck(CO_RPDO *pdo, CO_IF_FRM *frm)
572575
void CORPdoWrite(CO_RPDO *pdo, CO_IF_FRM *frm)
573576
{
574577
CO_OBJ *obj;
578+
uint64_t val64;
575579
uint32_t val32;
576580
uint16_t val16;
577581
uint8_t val08;
@@ -584,8 +588,8 @@ void CORPdoWrite(CO_RPDO *pdo, CO_IF_FRM *frm)
584588
obj = pdo->Map[on];
585589
pdosz = pdo->Size[on];
586590
if (obj != 0) {
587-
if (pdosz <= 4) {
588-
/* supported mapping: 1 to 4 bytes */
591+
if (pdosz <= 6) {
592+
/* supported mapping: 1 to 6 bytes */
589593
sz = (uint8_t)COObjGetSize(obj, pdo->Node, 0L);
590594
if (sz == 1u) {
591595
val08 = CO_GET_BYTE(frm, dlc);
@@ -602,6 +606,12 @@ void CORPdoWrite(CO_RPDO *pdo, CO_IF_FRM *frm)
602606
}
603607
dlc += pdosz;
604608
COObjWrValue(obj, pdo->Node, (void *)&val32, sz);
609+
} else if (sz == 6u) {
610+
val64 = CO_GET_LONG(frm, dlc);
611+
val64 |= (uint64_t)(CO_GET_WORD(frm, dlc + 4)) << 32;
612+
val64 &= 0x0000FFFFFFFFFFFF;
613+
dlc += pdosz;
614+
COObjWrValue(obj, pdo->Node, (void *)&val64, sz);
605615
}
606616
} else {
607617
CORpdoWriteData(frm, dlc, pdosz, obj);

0 commit comments

Comments
 (0)