Dawn Framework 1.0
Universal data acquisition framework for embedded systems
gpi.cxx
1// dawn/src/io/gpi.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/io/gpi.hxx"
7
8using namespace dawn;
9
10int CIOGpi::configureDesc(const CDescObject &desc)
11{
12 const SObjectCfg::SObjectCfgItem *item = nullptr;
13 size_t offset = 0;
14
15 for (size_t i = 0; i < desc.getSize(); i++)
16 {
17 item = desc.objectCfgItemAtOffset(offset);
18
20 {
21 DAWNERR("Unsupported GPI config 0x%08" PRIx32 "\n", item->cfgid.v);
22 return -EINVAL;
23 }
24
25 offset += cfgCmnOffset(item);
26 }
27
28 return OK;
29}
30
31CIOGpi::~CIOGpi()
32{
33 deinit();
34}
35
37{
38 int ret;
39
40 // Configure object
41
42 ret = configureDesc(getDesc());
43 if (ret != OK)
44 {
45 DAWNERR("GPI configure failed (error %d)\n", ret);
46 return ret;
47 }
48
49 // Get path to GPI
50
51 if (getCmnDevno() == -1)
52 {
53 DAWNERR("GPI device number not configured\n");
54 return -EINVAL;
55 }
56
57 snprintf(path, sizeof(path), GPI_PATH_FMT, getCmnDevno());
58
59 // Open file
60
61 fd = gpi_open(path);
62 if (fd < 0)
63 {
64 DAWNERR("gpi_open failed %d\n", -errno);
65 return -errno;
66 }
67
68 // Intialize gpi
69
70 gpi_init(fd);
71
72#ifdef CONFIG_DAWN_IO_NOTIFY
73 // Register interrupt pin is required if notifications supported
74
75 isNotifyIO = true;
76 if (gpi_notify(fd) == false)
77 {
78 // Notify not supported for this GPI
79
80 isNotifyIO = false;
81 }
82#endif
83
84 return OK;
85}
86
88{
90 {
91 DAWNERR("GPI requires DTYPE_UINT32\n");
92 return -EINVAL;
93 }
94
95 return OK;
96}
97
99{
100 // Close file
101
102 gpi_close(fd);
103 return OK;
104}
105
106int CIOGpi::getDataImpl(IODataCmn &data, size_t len)
107{
108 bool *tmp = static_cast<bool *>(data.getDataPtr());
109 bool invalue;
110 int ret;
111
112 // No batch supported
113
114 if (len != 1)
115 {
116 return -ENOTSUP;
117 }
118
119 // Read input
120
121 ret = gpi_read(fd, &invalue);
122 if (ret < 0)
123 {
124 DAWNERR("gpi_read failed %d\n", ret);
125 return ret;
126 }
127
128#ifdef CONFIG_DAWN_IO_TIMESTAMP
129 if (isTimestamp())
130 {
131 data.getTs() = getTimestamp();
132 }
133#endif
134
135 *tmp = static_cast<bool>(invalue);
136
137 return OK;
138}
139
140#ifdef CONFIG_DAWN_IO_NOTIFY
141int CIOGpi::getFd() const
142{
143 if (isNotifyIO)
144 {
145 return fd;
146 }
147 else
148 {
149 // Notify not supported
150
151 return -1;
152 }
153}
154#endif
155
157{
158 return sizeof(uint32_t);
159}
160
161size_t CIOGpi::getDataDim() const
162{
163 return 1;
164}
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.
virtual int getFd() const
Get file descriptor for notifications.
Definition common.hxx:425
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
int configure()
Configure object from descriptor data.
Definition gpi.cxx:36
size_t getDataSize() const
Get data size in bytes.
Definition gpi.cxx:156
int getDataImpl(IODataCmn &data, size_t len)
Get data implementation (override in derived classes).
Definition gpi.cxx:106
size_t getDataDim() const
Get data vector dimension.
Definition gpi.cxx:161
int deinit()
De-initialize object.
Definition gpi.cxx:98
int init()
One-time initialize object after bindings are resolved.
Definition gpi.cxx:87
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 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.
UObjectCfgId cfgid
Configuration ID header (type, class, id, size, rw, dtype).
@ DTYPE_UINT32
Unsigned 32-bit integer (0 to 4294967295).
Definition objectid.hxx:96
ObjectCfgId v
Raw 32-bit ConfigID value (for storage, comparison).
Definition objectcfg.hxx:82
uint32_t cls
Object class (bits 21-29, max 511).
struct dawn::SObjectCfg::UObjectCfgId::@10 s
Bit-field structure for named member access.