arduino-emulator
Loading...
Searching...
No Matches
RemoteSPI.h
1
2/*
3 RemoteSPI.h
4 Copyright (c) 2025 Phil Schatzmann. All right reserved.
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19*/
20#pragma once
21
22#include "api/HardwareSPI.h"
23
24namespace arduino {
25
50class RemoteSPI : public HardwareSPI {
51 public:
52 RemoteSPI() = default;
53 RemoteSPI(Stream* stream) { service.setStream(stream); }
54 void setStream(Stream* stream) { service.setStream(stream); }
55
56 uint8_t transfer(uint8_t data) {
57 service.send(SpiTransfer8);
58 service.send(data);
59 service.flush();
60 return service.receive8();
61 }
62
63 uint16_t transfer16(uint16_t data) {
64 service.send(SpiTransfer16);
65 service.send(data);
66 service.flush();
67 return service.receive16();
68 }
69
70 void transfer(void* buf, size_t count) {
71 service.send(SpiTransfer);
72 service.send((uint32_t)count);
73 service.send(buf, count);
74 service.flush();
75 for (int j=0; j<count; j++) {
76 ((uint8_t*)buf)[j] = service.receive8();
77 }
78 }
79
80 void usingInterrupt(int interruptNumber) {
81 service.send(SpiUsingInterrupt);
82 service.send(interruptNumber);
83 service.flush();
84 }
85
86 void notUsingInterrupt(int interruptNumber) {
87 service.send(SpiNotUsingInterrupt);
88 service.send(interruptNumber);
89 service.flush();
90 }
91
92 void beginTransaction(SPISettings settings) {
93 service.send(SpiBeginTransaction);
94 // uint32_t clock, uint8_t bitOrder, uint8_t dataMode
95 service.send((uint32_t)settings.getClockFreq());
96 service.send((uint8_t)settings.getBitOrder());
97 service.send((uint8_t)settings.getDataMode());
98 service.flush();
99 }
100
101 void endTransaction(void) {
102 service.send(SpiEndTransaction);
103 service.flush();
104 }
105
106 void attachInterrupt() {
107 service.send(SpiAttachInterrupt);
108 service.flush();
109 }
110
111 void detachInterrupt() {
112 service.send(SpiDetachInterrupt);
113 service.flush();
114 }
115
116 void begin() {
117 service.send(SpiBegin);
118 service.flush();
119 }
120
121 void end() {
122 service.send(SpiEnd);
123 service.flush();
124 }
125
126 operator bool() { return service; }
127
128 protected:
129 HardwareService service;
130};
131
132} // namespace arduino
Definition DMAPool.h:103
Definition HardwareSPI.h:120
Provides a virtualized hardware communication service for SPI, I2C, I2S, and GPIO over a stream.
Definition HardwareService.h:115
Remote SPI implementation that operates over a communication stream.
Definition RemoteSPI.h:50
Definition HardwareSPI.h:46
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