Dawn Framework 1.0
Universal data acquisition framework for embedded systems
encoder.cxx
1// dawn/src/io/encoder.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/io/encoder.hxx"
7
8#include <cerrno>
9#include <cinttypes>
10#include <cstdio>
11
12using namespace dawn;
13
14int CIOEncoder::configureDesc(const CDescObject &desc)
15{
16 const SObjectCfg::SObjectCfgItem *item = nullptr;
17 size_t offset = 0;
18
19 for (size_t i = 0; i < desc.getSize(); i++)
20 {
21 item = desc.objectCfgItemAtOffset(offset);
22
23 switch (item->cfgid.s.cls)
24 {
26 {
27 offset += cfgCmnOffset(item);
28 break;
29 }
30
32 {
33 switch (item->cfgid.s.id)
34 {
35 case IO_ENCODER_CFG_POSMAX:
36 {
37 if (item->cfgid.s.size != 1)
38 {
39 DAWNERR("invalid encoder posmax cfg size %d\n", item->cfgid.s.size);
40 return -EINVAL;
41 }
42
43 const uint32_t *tmp = reinterpret_cast<const uint32_t *>(&item->data);
44 posmax = *tmp;
45 hasPosmax = true;
46 offset += 2;
47 break;
48 }
49
50 default:
51 {
52 DAWNERR("unsupported encoder cfg 0x%08" PRIx32 "\n", item->cfgid.v);
53 return -EINVAL;
54 }
55 }
56 break;
57 }
58
59 default:
60 {
61 DAWNERR("unsupported encoder cfg 0x%08" PRIx32 "\n", item->cfgid.v);
62 return -EINVAL;
63 }
64 }
65 }
66
67 return OK;
68}
69
70CIOEncoder::~CIOEncoder()
71{
72 deinit();
73}
74
76{
77 int ret;
78
79 ret = configureDesc(getDesc());
80 if (ret != OK)
81 {
82 DAWNERR("encoder configure failed (error %d)\n", ret);
83 return ret;
84 }
85
86 if (getCmnDevno() == -1)
87 {
88 DAWNERR("encoder device number not configured\n");
89 return -EINVAL;
90 }
91
92 snprintf(path, sizeof(path), ENCODER_PATH_FMT, getCmnDevno());
93
94 fd = encoder_open(path);
95 if (fd < 0)
96 {
97 DAWNERR("failed to open encoder %d\n", -errno);
98 return -errno;
99 }
100
101 if (hasPosmax)
102 {
103 ret = encoder_set_posmax(fd, posmax);
104 if (ret < 0)
105 {
106 DAWNERR("encoder set posmax failed %d\n", ret);
107 encoder_close(fd);
108 fd = -1;
109 return ret;
110 }
111 }
112
113 return OK;
114}
115
117{
119 {
120 DAWNERR("encoder requires DTYPE_INT32\n");
121 return -EINVAL;
122 }
123
124 return OK;
125}
126
128{
129 encoder_close(fd);
130 fd = -1;
131 return OK;
132}
133
134int CIOEncoder::getDataImpl(IODataCmn &data, size_t len)
135{
136 int32_t *tmp = static_cast<int32_t *>(data.getDataPtr());
137 int ret;
138
139 if (len != 1)
140 {
141 return -ENOTSUP;
142 }
143
144 ret = encoder_read_position(fd, tmp);
145 if (ret < 0)
146 {
147 DAWNERR("encoder_read_position failed %d\n", ret);
148 return ret;
149 }
150
151#ifdef CONFIG_DAWN_IO_TIMESTAMP
152 if (isTimestamp())
153 {
154 data.getTs() = getTimestamp();
155 }
156#endif
157
158 return OK;
159}
160
161int CIOEncoder::trigger(uint8_t cmd)
162{
163 if (cmd == CObject::CMD_RESET)
164 {
165 return encoder_reset(fd);
166 }
167
168 return -ENOTSUP;
169}
Descriptor wrapper for individual object configuration.
size_t getSize() const
Get number of configuration items for this object.
SObjectCfg::SObjectCfgItem * objectCfgItemAtOffset(size_t offset) const
Get configuration item at specified offset.
uint64_t getTimestamp()
Get current timestamp.
Definition common.cxx:194
bool isTimestamp() const
Check if I/O supports timestamp.
Definition common.cxx:189
int getCmnDevno() const
Get device number for this I/O.
Definition common.hxx:798
size_t cfgCmnOffset(const SObjectCfg::SObjectCfgItem *cfg)
Get offset of configuration item in descriptor.
Definition common.cxx:144
@ IO_CLASS_ANY
Any I/O class.
Definition common.hxx:86
@ IO_CLASS_ENCODER
Quadrature encoder (position)
Definition common.hxx:183
int getDataImpl(IODataCmn &data, size_t len)
Get data implementation (override in derived classes).
Definition encoder.cxx:134
int configure()
Configure object from descriptor data.
Definition encoder.cxx:75
int trigger(uint8_t cmd)
Execute a trigger command.
Definition encoder.cxx:161
int init()
One-time initialize object after bindings are resolved.
Definition encoder.cxx:116
int deinit()
De-initialize object.
Definition encoder.cxx:127
CDescObject & getDesc()
Get descriptor object for this object.
Definition object.cxx:190
uint8_t getDtype() const
Get data type field.
Definition object.cxx:175
@ CMD_RESET
Reset object internal state.
Definition object.hxx:42
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
Base interface for I/O data buffers (static and dynamic).
Definition idata.hxx:21
virtual void * getDataPtr(size_t batch=0)=0
Get pointer to data only (skips timestamp if present).
virtual uint64_t & getTs(size_t batch=0)=0
Get timestamp reference for batch.
Single configuration item within object.
ObjectCfgData_t data[]
Configuration data array (flexible, size from cfgid.s.size).
UObjectCfgId cfgid
Configuration ID header (type, class, id, size, rw, dtype).
@ DTYPE_INT32
Signed 32-bit integer (-2147483648 to 2147483647).
Definition objectid.hxx:92
ObjectCfgId v
Raw 32-bit ConfigID value (for storage, comparison).
Definition objectcfg.hxx:82
uint32_t cls
Object class (bits 21-29, max 511).
uint32_t id
Configuration identifier (bits 0-4, max 31).
Definition objectcfg.hxx:94
uint32_t size
Configuration data size in 32-bit words (bits 5-14, max 1023).
struct dawn::SObjectCfg::UObjectCfgId::@10 s
Bit-field structure for named member access.