Dawn Framework 1.0
Universal data acquisition framework for embedded systems
pot.cxx
1// dawn/src/io/pot.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/io/pot.hxx"
7
8#include <cstdio>
9
10using namespace dawn;
11
12int CIOPot::configureDesc(const CDescObject &desc)
13{
14 const SObjectCfg::SObjectCfgItem *item = nullptr;
15 size_t offset = 0;
16
17 for (size_t i = 0; i < desc.getSize(); i++)
18 {
19 item = desc.objectCfgItemAtOffset(offset);
20
21 if (item->cfgid.s.cls != CIOCommon::IO_CLASS_ANY &&
23 {
24 DAWNERR("Unsupported POT config 0x%08" PRIx32 "\n", item->cfgid.v);
25 return -EINVAL;
26 }
27
29 {
30 offset += cfgCmnOffset(item);
31 continue;
32 }
33
34 switch (item->cfgid.s.id)
35 {
36 case IO_POT_CFG_WIPER:
37 if (item->cfgid.s.size != 1)
38 {
39 DAWNERR("invalid POT wiper config size %d\n", item->cfgid.s.size);
40 return -EINVAL;
41 }
42
43 wiper = static_cast<uint8_t>(*reinterpret_cast<const uint32_t *>(&item->data[0]));
44 offset += 1 + item->cfgid.s.size;
45 break;
46
47 default:
48 DAWNERR("unsupported POT cfg id %u\n", item->cfgid.s.id);
49 return -EINVAL;
50 }
51 }
52
53 return OK;
54}
55
56CIOPot::~CIOPot()
57{
58 deinit();
59}
60
62{
63 uint8_t nwipers;
64 int ret;
65
67 {
68 DAWNERR("POT requires DTYPE_INT32\n");
69 return -EINVAL;
70 }
71
72 // Configure object
73
74 ret = configureDesc(getDesc());
75 if (ret != OK)
76 {
77 DAWNERR("POT configure failed (error %d)\n", ret);
78 return ret;
79 }
80
81 // Get path to POT
82
83 if (getCmnDevno() == -1)
84 {
85 DAWNERR("POT device number not configured\n");
86 return -EINVAL;
87 }
88
89 snprintf(path, sizeof(path), POT_PATH_FMT, getCmnDevno());
90
91 // Open file
92
93 fd = pot_open(path);
94 if (fd < 0)
95 {
96 DAWNERR("failed to open file %d\n", -errno);
97 return -errno;
98 }
99
100 // Wiper index must exist on the device
101
102 ret = pot_get_nwipers(fd, &nwipers);
103 if (ret < 0)
104 {
105 DAWNERR("pot_get_nwipers failed %d\n", ret);
106 deinit();
107 return ret;
108 }
109
110 if (wiper >= nwipers)
111 {
112 DAWNERR("POT wiper %u out of range (%u wipers)\n", wiper, nwipers);
113 deinit();
114 return -EINVAL;
115 }
116
117 return OK;
118}
119
121{
122 pot_close(fd);
123 fd = -1;
124 return OK;
125}
126
128{
129 int32_t *tmp = static_cast<int32_t *>(data.getDataPtr());
131 int ret;
132
133 // Only a single wiper value supported
134
135 if (data.getItems() > 1)
136 {
137 return -ENOMEM;
138 }
139
140 // Get data to write
141
142 info.wiper = wiper;
143 info.val = static_cast<uint32_t>(*tmp);
144
145 // Write wiper value
146
147 ret = pot_set_wiper(fd, &info);
148 if (ret < 0)
149 {
150 DAWNERR("pot_set_wiper failed %d\n", ret);
151 return ret;
152 }
153
154 return OK;
155}
156
157int CIOPot::getDataImpl(IODataCmn &data, size_t len)
158{
159 int32_t *tmp = static_cast<int32_t *>(data.getDataPtr());
161 int ret;
162
163 // No batch supported
164
165 if (len != 1)
166 {
167 return -ENOTSUP;
168 }
169
170 // Read wiper value
171
172 info.wiper = wiper;
173 info.val = 0;
174
175 ret = pot_get_wiper(fd, &info);
176 if (ret < 0)
177 {
178 DAWNERR("pot_get_wiper failed %d\n", ret);
179 return ret;
180 }
181
182 *tmp = static_cast<int32_t>(info.val);
183
184#ifdef CONFIG_DAWN_IO_TIMESTAMP
185 if (isTimestamp())
186 {
187 data.getTs() = getTimestamp();
188 }
189#endif
190
191 return OK;
192}
193
195{
196 return sizeof(int32_t);
197}
198
199size_t CIOPot::getDataDim() const
200{
201 return 1;
202}
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:831
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_POT
Digital potentiometer wiper.
Definition common.hxx:200
size_t getDataDim() const
Get data vector dimension.
Definition pot.cxx:199
int getDataImpl(IODataCmn &data, size_t len)
Get data implementation (override in derived classes).
Definition pot.cxx:157
int setDataImpl(IODataCmn &data)
Set data implementation (override in derived classes).
Definition pot.cxx:127
int deinit()
De-initialize object.
Definition pot.cxx:120
size_t getDataSize() const
Get data size in bytes.
Definition pot.cxx:194
int configure()
Configure object from descriptor data.
Definition pot.cxx:61
CDescObject & getDesc()
Get descriptor object for this object.
Definition object.cxx:190
uint8_t getDtype() const
Get data type field.
Definition object.cxx:175
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 size_t getItems()=0
Get number of items per batch.
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
POT wiper access data.
Definition pot.hxx:22
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.