Dawn Framework 1.0
Universal data acquisition framework for embedded systems
adc_sync.cxx
1// dawn/src/io/adc_sync.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/io/adc_sync.hxx"
7
8#include <cinttypes>
9
10using namespace dawn;
11
12int CIOAdcSync::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 switch (item->cfgid.s.cls)
22 {
24 {
25 offset += cfgCmnOffset(item);
26 break;
27 }
28
30 {
31 switch (item->cfgid.s.id)
32 {
33 case IO_ADC_SYNC_CFG_TRIGGER_FREQ:
34 {
35 if (item->cfgid.s.size != 1)
36 {
37 DAWNERR("invalid adc_sync trigger freq cfg size %d\n",
38 item->cfgid.s.size);
39 return -EINVAL;
40 }
41
42 triggerFreqHz = *reinterpret_cast<const uint32_t *>(&item->data);
43 if (triggerFreqHz == 0)
44 {
45 DAWNERR("adc_sync trigger frequency must be > 0\n");
46 return -EINVAL;
47 }
48
49 hasTriggerFreq = true;
50 offset += 2;
51 break;
52 }
53
54 default:
55 {
56 DAWNERR("unsupported adc_sync cfg 0x%08" PRIx32 "\n", item->cfgid.v);
57 return -EINVAL;
58 }
59 }
60 break;
61 }
62
63 default:
64 {
65 DAWNERR("unsupported adc_sync cfg 0x%08" PRIx32 "\n", item->cfgid.v);
66 return -EINVAL;
67 }
68 }
69 }
70
71 return OK;
72}
73
75{
76 int ret;
77
78 ret = configureDesc(getDesc());
79 if (ret != OK)
80 {
81 DAWNERR("adc_sync configure failed (error %d)\n", ret);
82 return ret;
83 }
84
85 ret = configureDevice();
86 if (ret != OK)
87 {
88 return ret;
89 }
90
91 if (hasTriggerFreq)
92 {
93 ret = adc_set_timer_freq(getFdInternal(), triggerFreqHz);
94 if (ret < 0)
95 {
96 return ret;
97 }
98 }
99
100 return OK;
101}
102
103int CIOAdcSync::getDataImpl(IODataCmn &data, size_t len)
104{
105 if (len == 0)
106 {
107 return -EINVAL;
108 }
109
110 return readSamples(data, len);
111}
112
114{
115 return OK;
116}
117
119{
120 return adc_stop(getFdInternal());
121}
122
123int CIOAdcSync::trigger(uint8_t cmd)
124{
125 UNUSED(cmd);
126 return adc_start(getFdInternal());
127}
128
129int CIOAdcSync::onSetObjConfig(SObjectCfg::ObjectCfgId objcfg, uint32_t *data, size_t len)
130{
131 int ret;
132
133 if (SObjectCfg::objectCfgGetId(objcfg) != IO_ADC_SYNC_CFG_TRIGGER_FREQ)
134 {
135 return OK;
136 }
137
138 if (len != 1 || data[0] == 0)
139 {
140 return -EINVAL;
141 }
142
143 ret = adc_set_timer_freq(getFdInternal(), data[0]);
144 if (ret < 0)
145 {
146 return ret;
147 }
148
149 triggerFreqHz = data[0];
150 hasTriggerFreq = true;
151 return OK;
152}
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.
int getDataImpl(IODataCmn &data, size_t len)
Get data implementation (override in derived classes).
Definition adc_sync.cxx:103
int doStop()
Stop implementation hook.
Definition adc_sync.cxx:118
int trigger(uint8_t cmd)
Execute a trigger command.
Definition adc_sync.cxx:123
int configure()
Configure object from descriptor data.
Definition adc_sync.cxx:74
int doStart()
Start implementation hook.
Definition adc_sync.cxx:113
int onSetObjConfig(SObjectCfg::ObjectCfgId objcfg, uint32_t *data, size_t len)
Pre-update hook for runtime configuration writes.
Definition adc_sync.cxx:129
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_ADC_SYNC
ADC sync (HW-triggered control loop)
Definition common.hxx:178
CDescObject & getDesc()
Get descriptor object for this object.
Definition object.cxx:190
uint32_t ObjectCfgId
ConfigID type - single 32-bit value.
Definition objectcfg.hxx:60
static uint8_t objectCfgGetId(const ObjectCfgId objcfg)
Extract configuration identifier from ConfigID.
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
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).
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.