Skip to content

Commit 10281cf

Browse files
committed
Merge branch 'ftdi'
2 parents c6afc0f + 900aa16 commit 10281cf

22 files changed

+2542
-56
lines changed

Arduino.cmake

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,14 @@ function(arduino_sketch name ino_file)
119119
target_compile_definitions(${name} PUBLIC ${ARG_DEFINITIONS})
120120
endif()
121121

122+
# Handle FTDI support if specified
123+
if (USE_FTDI)
124+
# Find and link libftdi1
125+
find_package(PkgConfig REQUIRED)
126+
pkg_check_modules(FTDI REQUIRED libftdi1)
127+
target_link_libraries(${name} ${FTDI_LIBRARIES})
128+
target_include_directories(${name} PUBLIC ${FTDI_INCLUDE_DIRS})
129+
target_compile_options(${name} PUBLIC ${FTDI_CFLAGS_OTHER})
130+
endif(USE_FTDI)
122131
endfunction()
123132

ArduinoCore-Linux/cores/arduino/Arduino.cpp

Lines changed: 111 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Arduino.cpp
2+
Arduino.cpp
33
Copyright (c) 2025 Phil Schatzmann. All right reserved.
44
55
This library is free software; you can redistribute it and/or
@@ -17,7 +17,9 @@
1717
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1818
*/
1919
#include "Arduino.h"
20+
2021
#include <stdio.h>
22+
2123
#include <chrono>
2224
#include <cstring>
2325
#include <ctime>
@@ -57,23 +59,39 @@ PluggableUSB_::PluggableUSB_() {}
5759

5860
} // namespace arduino
5961

60-
// sleep ms milliseconds
62+
/**
63+
* @brief Pause the program for the amount of time (in milliseconds) specified as parameter
64+
* @param ms The number of milliseconds to pause (unsigned long)
65+
*/
6166
void delay(unsigned long ms) {
6267
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
6368
}
6469

65-
// sleep us milliseconds
70+
/**
71+
* @brief Pause the program for the amount of time (in microseconds) specified as parameter
72+
* @param us The number of microseconds to pause (unsigned int)
73+
*/
6674
void delayMicroseconds(unsigned int us) {
6775
std::this_thread::sleep_for(std::chrono::microseconds(us));
6876
}
6977

70-
// double to string conversion -> we can use sprintf which is complete in linux
78+
/**
79+
* @brief Convert a floating point number to string
80+
* @param val The double value to convert
81+
* @param width The minimum field width (signed char)
82+
* @param prec The precision (number of digits after decimal point)
83+
* @param sout The output string buffer
84+
* @return Pointer to the output string
85+
*/
7186
char* dtostrf(double val, signed char width, unsigned char prec, char* sout) {
7287
sprintf(sout, "%*.*lf", width, prec, val);
7388
return sout;
7489
}
7590

76-
// Returns the number of milliseconds passed since epich
91+
/**
92+
* @brief Returns the number of milliseconds passed since the Arduino board began running the current program
93+
* @return Number of milliseconds passed since the program started (unsigned long)
94+
*/
7795
unsigned long millis() {
7896
static uint64_t start = 0;
7997
using namespace std::chrono;
@@ -89,7 +107,10 @@ unsigned long millis() {
89107
return result - start;
90108
}
91109

92-
// Returns the micros of milliseconds passed since epich
110+
/**
111+
* @brief Returns the number of microseconds since the Arduino board began running the current program
112+
* @return Number of microseconds passed since the program started (unsigned long)
113+
*/
93114
unsigned long micros(void) {
94115
using namespace std::chrono;
95116
// Get current time with precision of milliseconds
@@ -100,30 +121,114 @@ unsigned long micros(void) {
100121
return now.time_since_epoch().count();
101122
}
102123

124+
/**
125+
* @brief Configure the specified pin to behave either as an input or an output
126+
* @param pinNumber The pin whose mode you wish to set
127+
* @param pinMode INPUT, OUTPUT, or INPUT_PULLUP
128+
*/
103129
void pinMode(pin_size_t pinNumber, PinMode pinMode) {
104130
GPIO.pinMode(pinNumber, pinMode);
105131
}
132+
133+
/**
134+
* @brief Write a HIGH or a LOW value to a digital pin
135+
* @param pinNumber The pin number
136+
* @param status HIGH or LOW
137+
*/
106138
void digitalWrite(pin_size_t pinNumber, PinStatus status) {
107139
GPIO.digitalWrite(pinNumber, status);
108140
}
141+
142+
/**
143+
* @brief Reads the value from a specified digital pin, either HIGH or LOW
144+
* @param pinNumber The number of the digital pin you want to read
145+
* @return HIGH or LOW
146+
*/
109147
PinStatus digitalRead(pin_size_t pinNumber) {
110148
return GPIO.digitalRead(pinNumber);
111149
}
150+
151+
/**
152+
* @brief Reads the value from the specified analog pin
153+
* @param pinNumber The number of the analog input pin to read from (0 to 5 on most boards)
154+
* @return The analog reading on the pin (int 0 to 1023)
155+
*/
112156
int analogRead(pin_size_t pinNumber) { return GPIO.analogRead(pinNumber); }
157+
158+
/**
159+
* @brief Configures the reference voltage used for analog input
160+
* @param mode Which type of reference to use (DEFAULT, INTERNAL, EXTERNAL)
161+
*/
113162
void analogReference(uint8_t mode) { GPIO.analogReference(mode); }
163+
164+
/**
165+
* @brief Writes an analog value (PWM wave) to a pin
166+
* @param pinNumber The pin to write to
167+
* @param value The duty cycle: between 0 (always off) and 255 (always on)
168+
*/
114169
void analogWrite(pin_size_t pinNumber, int value) {
115170
GPIO.analogWrite(pinNumber, value);
116171
}
172+
173+
174+
/**
175+
* @brief Generates a square wave of the specified frequency (and 50% duty cycle) on a pin
176+
* @param pinNumber The pin on which to generate the tone
177+
* @param frequency The frequency of the tone in hertz - unsigned int
178+
* @param duration The duration of the tone in milliseconds (optional) - unsigned long
179+
*/
117180
void tone(uint8_t pinNumber, unsigned int frequency, unsigned long duration) {
118181
GPIO.tone(pinNumber, frequency, duration);
119182
}
183+
184+
/**
185+
* @brief Stops the generation of a square wave triggered by tone()
186+
* @param pinNumber The pin on which to stop generating the tone
187+
*/
120188
void noTone(uint8_t pinNumber) { GPIO.noTone(pinNumber); }
189+
190+
/**
191+
* @brief Reads a pulse (either HIGH or LOW) on a pin
192+
* @param pinNumber The number of the pin on which you want to read the pulse
193+
* @param state Type of pulse to read: either HIGH or LOW
194+
* @param timeout The number of microseconds to wait for the pulse to be completed (optional)
195+
* @return The length of the pulse (in microseconds) or 0 if no complete pulse was received within the timeout
196+
*/
121197
unsigned long pulseIn(uint8_t pinNumber, uint8_t state, unsigned long timeout) {
122198
return GPIO.pulseIn(pinNumber, state, timeout);
123199
}
200+
201+
/**
202+
* @brief Alternative to pulseIn() which is better at handling long pulses and interrupt affected systems
203+
* @param pinNumber The number of the pin on which you want to read the pulse
204+
* @param state Type of pulse to read: either HIGH or LOW
205+
* @param timeout The number of microseconds to wait for the pulse to be completed (optional)
206+
* @return The length of the pulse (in microseconds) or 0 if no complete pulse was received within the timeout
207+
*/
124208
unsigned long pulseInLong(uint8_t pinNumber, uint8_t state,
125209
unsigned long timeout) {
126210
return GPIO.pulseInLong(pinNumber, state, timeout);
127211
}
128212

213+
/**
214+
* @brief Set the PWM frequency for analogWrite() on the specified pin
215+
* @param pin The pin number to configure PWM frequency for
216+
* @param freq The desired PWM frequency in Hz
217+
*/
218+
void analogWriteFrequency(pin_size_t pin, uint32_t freq) {
219+
GPIO.analogWriteFrequency(pin, freq);
220+
}
221+
222+
/**
223+
* @brief Set the resolution of the analogWrite() values
224+
* @param bits The resolution in bits (e.g., 8 for 0-255, 10 for 0-1023)
225+
*/
226+
void analogWriteResolution(uint8_t bits) {
227+
GPIO.analogWriteResolution(bits);
228+
}
229+
230+
/**
231+
* @brief Passes control to other tasks when called
232+
* @note This is used to prevent watchdog timer resets in long-running loops
233+
*/
129234
void yield() {}

ArduinoCore-Linux/cores/arduino/Arduino.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,7 @@
4646
#include "HardwareSetup.h"
4747

4848
using namespace arduino;
49+
50+
// supported by some platforms
51+
void analogWriteFrequency(pin_size_t pin, uint32_t freq);
52+
void analogWriteResolution(uint8_t bits);

ArduinoCore-Linux/cores/arduino/GPIOWrapper.cpp

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
/*
2-
GPIOWrapper.cpp
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
2+
GPIOWrapper.cpp
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
1818
*/
1919

2020
#include "GPIOWrapper.h"
21+
22+
#include "ArduinoLogger.h"
2123
#include "HardwareGPIO.h"
2224
#include "HardwareService.h"
25+
#ifdef USE_FTDI
26+
#include "../ftdi/HardwareGPIO_FTDI.h"
27+
#endif
2328

2429
/**
2530
* We support different implementations for the GPIO
@@ -75,6 +80,7 @@ void GPIOWrapper::analogWrite(pin_size_t pinNumber, int value) {
7580
}
7681
}
7782

83+
7884
void GPIOWrapper::tone(uint8_t _pin, unsigned int frequency,
7985
unsigned long duration) {
8086
HardwareGPIO* gpio = getGPIO();
@@ -110,4 +116,18 @@ unsigned long GPIOWrapper::pulseInLong(uint8_t pin, uint8_t state,
110116
}
111117
}
112118

119+
void GPIOWrapper::analogWriteFrequency(pin_size_t pin, uint32_t freq) {
120+
HardwareGPIO* gpio = getGPIO();
121+
if (gpio != nullptr) {
122+
gpio->analogWriteFrequency(pin, freq);
123+
}
124+
}
125+
126+
void GPIOWrapper::analogWriteResolution(uint8_t bits) {
127+
HardwareGPIO* gpio = getGPIO();
128+
if (gpio != nullptr) {
129+
gpio->analogWriteResolution(bits);
130+
}
131+
}
132+
113133
} // namespace arduino

0 commit comments

Comments
 (0)