arduino-emulator
Loading...
Searching...
No Matches
HardwareService.h
1/*
2 HardwareService.h
3 Copyright (c) 2025 Phil Schatzmann. All right reserved.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19
20#pragma once
21
22#include "api/Stream.h"
23
24namespace arduino {
25
32enum HWCalls {
33 I2cBegin0,
34 I2cBegin1,
35 I2cEnd,
36 I2cSetClock,
37 I2cBeginTransmission,
38 I2cEndTransmission1,
39 I2cEndTransmission,
40 I2cRequestFrom3,
41 I2cRequestFrom2,
42 I2cOnReceive,
43 I2cOnRequest,
44 I2cWrite,
45 I2cAvailable,
46 I2cRead,
47 I2cPeek,
48 SpiTransfer,
49 SpiTransfer8,
50 SpiTransfer16,
51 SpiUsingInterrupt,
52 SpiNotUsingInterrupt,
53 SpiBeginTransaction,
54 SpiEndTransaction,
55 SpiAttachInterrupt,
56 SpiDetachInterrupt,
57 SpiBegin,
58 SpiEnd,
59 GpioPinMode,
60 GpioDigitalWrite,
61 GpioDigitalRead,
62 GpioAnalogRead,
63 GpioAnalogReference,
64 GpioAnalogWrite,
65 GpioTone,
66 GpioNoTone,
67 GpioPulseIn,
68 GpioPulseInLong,
69 GpioAnalogWriteFrequency,
70 GpioAnalogWriteResolution,
71 SerialBegin,
72 SerialEnd,
73 SerialWrite,
74 SerialRead,
75 SerialAvailable,
76 SerialPeek,
77 SerialFlush,
78 I2sSetup,
79 I2sBegin3,
80 I2sBegin2,
81 I2sEnd,
82 I2sAvailable,
83 I2sRead,
84 I2sPeek,
85 I2sFlush,
86 I2sWrite,
87 I2sAvailableForWrite,
88 I2sSetBufferSize
89};
90
116 public:
117 HardwareService() {}
118
119 void setStream(Stream* str) { io = str; }
120
121 void send(HWCalls call) {
123 io->write((uint8_t*)&val, sizeof(uint16_t));
124 }
125
126 void send(uint8_t data) { io->write((uint8_t*)&data, sizeof(data)); }
127
128 void send(uint16_t dataIn) {
130 io->write((uint8_t*)&data, sizeof(data));
131 }
132
133 void send(uint32_t dataIn) {
135 io->write((uint8_t*)&data, sizeof(data));
136 }
137
138 void send(uint64_t dataIn) {
139 uint32_t data = swap_uint64(dataIn);
140 io->write((uint8_t*)&data, sizeof(data));
141 }
142
143 void send(int32_t dataIn) {
144 int32_t data = swap_int32(dataIn);
145 io->write((uint8_t*)&data, sizeof(data));
146 }
147 void send(int64_t dataIn) {
148 int32_t data = swap_int64(dataIn);
149 io->write((uint8_t*)&data, sizeof(data));
150 }
151
152 void send(bool data) { io->write((uint8_t*)&data, sizeof(data)); }
153
154 void send(void* data, size_t len) { io->write((uint8_t*)data, len); }
155
156 void flush() { io->flush(); }
157
158 uint16_t receive16() {
160 blockingRead((char*)&result, sizeof(uint16_t));
161 return swap_uint16(result);
162 }
163
164 uint32_t receive32() {
166 blockingRead((char*)&result, sizeof(uint32_t));
167 return swap_uint32(result);
168 }
169
170 uint64_t receive64() {
172 blockingRead((char*)&result, sizeof(uint64_t));
173 return swap_uint64(result);
174 }
175
176 uint8_t receive8() {
178 blockingRead((char*)&result, sizeof(uint8_t));
179 return result;
180 }
181
182 uint16_t receive(void* data, int len) {
183 return blockingRead((char*)data, len);
184 }
185
186 operator bool() { return io != nullptr; }
187
188 protected:
189 Stream* io = nullptr;
190 bool isLittleEndian = !is_big_endian();
191 int timeout_ms = 1000;
192
193 uint16_t blockingRead(void* data, int len, int timeout = 1000) {
194 int offset = 0;
195 long start = millis();
196 while (offset < len && (millis() - start) < timeout) {
197 int n = io->readBytes((char*)data + offset, len - offset);
198 offset += n;
199 }
200 return offset;
201 }
202
203 // check if the system is big endian
204 bool is_big_endian(void) {
205 union {
206 uint32_t i;
207 char c[4];
208 } bint = {0x01020304};
209
210 return bint.c[0] == 1;
211 }
212
215 if (isLittleEndian) return val;
216 return (val << 8) | (val >> 8);
217 }
218
221 if (isLittleEndian) return val;
222 return (val << 8) | ((val >> 8) & 0xFF);
223 }
224
227 if (isLittleEndian) return val;
228 val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF);
229 return (val << 16) | (val >> 16);
230 }
231
234 if (isLittleEndian) return val;
235 val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF);
236 return (val << 16) | ((val >> 16) & 0xFFFF);
237 }
238
239 int64_t swap_int64(int64_t val) {
240 if (isLittleEndian) return val;
241 val = ((val << 8) & 0xFF00FF00FF00FF00ULL) |
242 ((val >> 8) & 0x00FF00FF00FF00FFULL);
243 val = ((val << 16) & 0xFFFF0000FFFF0000ULL) |
244 ((val >> 16) & 0x0000FFFF0000FFFFULL);
245 return (val << 32) | ((val >> 32) & 0xFFFFFFFFULL);
246 }
247
248 uint64_t swap_uint64(uint64_t val) {
249 if (isLittleEndian) return val;
250 val = ((val << 8) & 0xFF00FF00FF00FF00ULL) |
251 ((val >> 8) & 0x00FF00FF00FF00FFULL);
252 val = ((val << 16) & 0xFFFF0000FFFF0000ULL) |
253 ((val >> 16) & 0x0000FFFF0000FFFFULL);
254 return (val << 32) | (val >> 32);
255 }
256};
257
258} // namespace arduino
Definition DMAPool.h:103
Provides a virtualized hardware communication service for SPI, I2C, I2S, and GPIO over a stream.
Definition HardwareService.h:115
int32_t swap_int32(int32_t val)
Byte swap int.
Definition HardwareService.h:233
uint16_t swap_uint16(uint16_t val)
Byte swap unsigned short.
Definition HardwareService.h:214
int16_t swap_int16(int16_t val)
Byte swap short.
Definition HardwareService.h:220
uint32_t swap_uint32(uint32_t val)
Byte swap unsigned int.
Definition HardwareService.h:226
Definition Stream.h:51
We provide the WiFi class to simulate the Arduino WIFI. In in Linux we can expect that networking is ...
Definition CanMsg.cpp:31
HWCalls
We virtualize the hardware and send the requests and replys over a stream.
Definition HardwareService.h:32