arduino-emulator
Loading...
Searching...
No Matches
HardwareSPI_FTDI.h
1#pragma once
2/*
3 HardwareSPI_FTDI.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#ifdef USE_FTDI
21#include <inttypes.h>
22#include <ftdi.h>
23// Undefine DEPRECATED macro from libftdi1 to avoid conflict with Arduino API
24#ifdef DEPRECATED
25#undef DEPRECATED
26#endif
27
28#include "api/Common.h"
29#include "api/HardwareSPI.h"
30#include "api/Stream.h"
31
32namespace arduino {
33
55 public:
61
65 ~HardwareSPI_FTDI() override;
66
67 void begin() override {
68 begin(0x0403, 0x6010);
69 }
70
79 bool begin(int vendor_id, int product_id,
80 const char* description = nullptr, const char* serial = nullptr);
81
85 void end() override;
86
92 uint8_t transfer(uint8_t data) override;
93
99 uint16_t transfer16(uint16_t data) override;
100
106 void transfer(void* buf, size_t count) override;
107
108 // Transaction Functions
113 void usingInterrupt(int interruptNumber) override;
114
119 void notUsingInterrupt(int interruptNumber) override;
120
126
130 void endTransaction(void) override;
131
132 // SPI Configuration methods
136 void attachInterrupt() override;
137
141 void detachInterrupt() override;
142
147 operator bool() { return is_open && ftdi_context != nullptr; }
148
149 protected:
150 struct ftdi_context* ftdi_context = nullptr;
151 int ftdi_channel = 0; // 0 for Channel A, 1 for Channel B
152 bool is_open = false;
153
154 // Current SPI settings
155 uint32_t current_clock = 1000000; // Default 1MHz
156 SPIMode current_mode = SPI_MODE0; // Default mode 0
157 BitOrder current_bit_order = MSBFIRST; // Default MSB first
158
159 // MPSSE command definitions for SPI (using different names to avoid macro conflicts)
160 static const uint8_t CMD_WRITE_NEG = 0x01; // Write TDI/DO on negative clock edge
161 static const uint8_t CMD_BITMODE = 0x02; // Write in bit mode
162 static const uint8_t CMD_READ_NEG = 0x04; // Read TDO/DI on negative clock edge
163 static const uint8_t CMD_LSB = 0x08; // LSB first
164 static const uint8_t CMD_DO_WRITE = 0x10; // Write TDI/DO
165 static const uint8_t CMD_DO_READ = 0x20; // Read TDO/DI
166 static const uint8_t CMD_WRITE_TMS = 0x40; // Write TMS/CS
167
172 bool configureMPSSE();
173
179 bool setClockFrequency(uint32_t frequency);
180
188
195 int sendData(const uint8_t* data, size_t length);
196
203 int receiveData(uint8_t* data, size_t length);
204};
205
206} // namespace arduino
207
208#endif // USE_FTDI
Definition DMAPool.h:103
Implementation of SPI communication for FTDI FT2232HL using MPSSE mode.
Definition HardwareSPI_FTDI.h:54
uint8_t getMPSSECommand(bool read_enable, bool write_enable)
Get MPSSE command byte based on current SPI settings.
Definition HardwareSPI_FTDI.cpp:272
void endTransaction(void) override
End the current SPI transaction.
Definition HardwareSPI_FTDI.cpp:193
uint16_t transfer16(uint16_t data) override
Transfer a 16-bit value over SPI.
Definition HardwareSPI_FTDI.cpp:120
bool configureMPSSE()
Configure FTDI device for MPSSE mode.
Definition HardwareSPI_FTDI.cpp:205
void detachInterrupt() override
Detach interrupt (not implemented for FTDI).
Definition HardwareSPI_FTDI.cpp:201
int sendData(const uint8_t *data, size_t length)
Send raw data to FTDI device.
Definition HardwareSPI_FTDI.cpp:300
void beginTransaction(SPISettings settings) override
Begin an SPI transaction with specific settings.
Definition HardwareSPI_FTDI.cpp:179
void notUsingInterrupt(int interruptNumber) override
Unregister interrupt usage (not implemented for FTDI).
Definition HardwareSPI_FTDI.cpp:175
uint8_t transfer(uint8_t data) override
Transfer a single byte over SPI.
Definition HardwareSPI_FTDI.cpp:97
bool setClockFrequency(uint32_t frequency)
Set the SPI clock frequency.
Definition HardwareSPI_FTDI.cpp:250
void usingInterrupt(int interruptNumber) override
Register interrupt usage (not implemented for FTDI).
Definition HardwareSPI_FTDI.cpp:171
int receiveData(uint8_t *data, size_t length)
Receive raw data from FTDI device.
Definition HardwareSPI_FTDI.cpp:307
~HardwareSPI_FTDI() override
Destructor - closes FTDI connection.
Definition HardwareSPI_FTDI.cpp:28
void end() override
Close the SPI interface and cleanup resources.
Definition HardwareSPI_FTDI.cpp:88
void attachInterrupt() override
Attach interrupt (not implemented for FTDI).
Definition HardwareSPI_FTDI.cpp:197
Definition HardwareSPI.h:120
Definition HardwareSPI.h:46
We provide the WiFi class to simulate the Arduino WIFI. In in Linux we can expect that networking is ...
Definition CanMsg.cpp:31