Skip to content

Commit dc67705

Browse files
p3pthinkyhead
authored andcommitted
✨ Simulator HAL and build targets (MarlinFirmware#22418)
1 parent e0fa6ed commit dc67705

37 files changed

+1847
-16
lines changed

.gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,11 @@ vc-fileutils.settings
143143
.vscode/launch.json
144144
.vscode/*.db
145145

146-
# cmake
146+
#Simulation
147+
imgui.ini
148+
eeprom.dat
149+
150+
#cmake
147151
CMakeLists.txt
148152
src/CMakeLists.txt
149153
CMakeListsPrivate.txt

Marlin/src/HAL/NATIVE_SIM/HAL.h

+217
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
/**
2+
* Marlin 3D Printer Firmware
3+
*
4+
* Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
5+
* Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
6+
* Copyright (c) 2015-2016 Nico Tonnhofer wurstnase.reprap@gmail.com
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
#pragma once
23+
24+
#define CPU_32_BIT
25+
#define HAL_IDLETASK
26+
void HAL_idletask();
27+
28+
#define F_CPU 100000000
29+
#define SystemCoreClock F_CPU
30+
#include <stdint.h>
31+
#include <stdarg.h>
32+
33+
#undef min
34+
#undef max
35+
36+
#include <algorithm>
37+
#include "pinmapping.h"
38+
39+
void _printf (const char *format, ...);
40+
void _putc(uint8_t c);
41+
uint8_t _getc();
42+
43+
//extern "C" volatile uint32_t _millis;
44+
45+
//arduino: Print.h
46+
#define DEC 10
47+
#define HEX 16
48+
#define OCT 8
49+
#define BIN 2
50+
//arduino: binary.h (weird defines)
51+
#define B01 1
52+
#define B10 2
53+
54+
#include "../shared/Marduino.h"
55+
#include "../shared/math_32bit.h"
56+
#include "../shared/HAL_SPI.h"
57+
#include "fastio.h"
58+
#include "watchdog.h"
59+
#include "serial.h"
60+
61+
#define SHARED_SERVOS HAS_SERVOS
62+
63+
extern MSerialT serial_stream_0;
64+
extern MSerialT serial_stream_1;
65+
extern MSerialT serial_stream_2;
66+
extern MSerialT serial_stream_3;
67+
68+
#define _MSERIAL(X) serial_stream_##X
69+
#define MSERIAL(X) _MSERIAL(X)
70+
71+
#if WITHIN(SERIAL_PORT, 0, 3)
72+
#define MYSERIAL1 MSERIAL(SERIAL_PORT)
73+
#else
74+
#error "SERIAL_PORT must be from 0 to 3. Please update your configuration."
75+
#endif
76+
77+
#ifdef SERIAL_PORT_2
78+
#if WITHIN(SERIAL_PORT_2, 0, 3)
79+
#define MYSERIAL2 MSERIAL(SERIAL_PORT_2)
80+
#else
81+
#error "SERIAL_PORT_2 must be from 0 to 3. Please update your configuration."
82+
#endif
83+
#endif
84+
85+
#ifdef MMU2_SERIAL_PORT
86+
#if WITHIN(MMU2_SERIAL_PORT, 0, 3)
87+
#define MMU2_SERIAL MSERIAL(MMU2_SERIAL_PORT)
88+
#else
89+
#error "MMU2_SERIAL_PORT must be from 0 to 3. Please update your configuration."
90+
#endif
91+
#endif
92+
93+
#ifdef LCD_SERIAL_PORT
94+
#if WITHIN(LCD_SERIAL_PORT, 0, 3)
95+
#define LCD_SERIAL MSERIAL(LCD_SERIAL_PORT)
96+
#else
97+
#error "LCD_SERIAL_PORT must be from 0 to 3. Please update your configuration."
98+
#endif
99+
#endif
100+
101+
102+
#define ST7920_DELAY_1 DELAY_NS(600)
103+
#define ST7920_DELAY_2 DELAY_NS(750)
104+
#define ST7920_DELAY_3 DELAY_NS(750)
105+
106+
//
107+
// Interrupts
108+
//
109+
#define CRITICAL_SECTION_START()
110+
#define CRITICAL_SECTION_END()
111+
#define ISRS_ENABLED()
112+
#define ENABLE_ISRS()
113+
#define DISABLE_ISRS()
114+
115+
inline void HAL_init() {}
116+
117+
// Utility functions
118+
#pragma GCC diagnostic push
119+
#pragma GCC diagnostic ignored "-Wunused-function"
120+
int freeMemory();
121+
#pragma GCC diagnostic pop
122+
123+
// ADC
124+
#define HAL_ADC_VREF 5.0
125+
#define HAL_ADC_RESOLUTION 10
126+
#define HAL_ANALOG_SELECT(ch) HAL_adc_enable_channel(ch)
127+
#define HAL_START_ADC(ch) HAL_adc_start_conversion(ch)
128+
#define HAL_READ_ADC() HAL_adc_get_result()
129+
#define HAL_ADC_READY() true
130+
131+
void HAL_adc_init();
132+
void HAL_adc_enable_channel(const uint8_t ch);
133+
void HAL_adc_start_conversion(const uint8_t ch);
134+
uint16_t HAL_adc_get_result();
135+
136+
// Reset source
137+
inline void HAL_clear_reset_source(void) {}
138+
inline uint8_t HAL_get_reset_source(void) { return RST_POWER_ON; }
139+
140+
/* ---------------- Delay in cycles */
141+
142+
#define DELAY_CYCLES(x) Kernel::delayCycles(x)
143+
#define SYSTEM_YIELD() Kernel::yield()
144+
145+
// Maple Compatibility
146+
typedef void (*systickCallback_t)(void);
147+
void systick_attach_callback(systickCallback_t cb);
148+
extern volatile uint32_t systick_uptime_millis;
149+
150+
// Marlin uses strstr in constexpr context, this is not supported, workaround by defining constexpr versions of the required functions.
151+
#define strstr(a, b) strstr_constexpr((a), (b))
152+
153+
constexpr inline std::size_t strlen_constexpr(const char* str) {
154+
// https://github.com/gcc-mirror/gcc/blob/5c7634a0e5f202935aa6c11b6ea953b8bf80a00a/libstdc%2B%2B-v3/include/bits/char_traits.h#L329
155+
if (str != nullptr) {
156+
std::size_t i = 0;
157+
while (str[i] != '\0') {
158+
++i;
159+
}
160+
161+
return i;
162+
}
163+
164+
return 0;
165+
}
166+
167+
constexpr inline int strncmp_constexpr(const char* lhs, const char* rhs, std::size_t count) {
168+
// https://github.com/gcc-mirror/gcc/blob/13b9cbfc32fe3ac4c81c4dd9c42d141c8fb95db4/libstdc%2B%2B-v3/include/bits/char_traits.h#L655
169+
if (lhs == nullptr || rhs == nullptr) {
170+
return rhs != nullptr ? -1 : 1;
171+
}
172+
173+
for (std::size_t i = 0; i < count; ++i) {
174+
if (lhs[i] != rhs[i]) {
175+
return lhs[i] < rhs[i] ? -1 : 1;
176+
} else if (lhs[i] == '\0') {
177+
return 0;
178+
}
179+
}
180+
181+
return 0;
182+
}
183+
184+
constexpr inline const char* strstr_constexpr(const char* str, const char* target) {
185+
// https://github.com/freebsd/freebsd/blob/master/sys/libkern/strstr.c
186+
if (char c = target != nullptr ? *target++ : '\0'; c != '\0' && str != nullptr) {
187+
std::size_t len = strlen_constexpr(target);
188+
do {
189+
char sc = {};
190+
do {
191+
if ((sc = *str++) == '\0') {
192+
return nullptr;
193+
}
194+
} while (sc != c);
195+
} while (strncmp_constexpr(str, target, len) != 0);
196+
--str;
197+
}
198+
199+
return str;
200+
}
201+
202+
constexpr inline char* strstr_constexpr(char* str, const char* target) {
203+
// https://github.com/freebsd/freebsd/blob/master/sys/libkern/strstr.c
204+
if (char c = target != nullptr ? *target++ : '\0'; c != '\0' && str != nullptr) {
205+
std::size_t len = strlen_constexpr(target);
206+
do {
207+
char sc = {};
208+
do {
209+
if ((sc = *str++) == '\0') {
210+
return nullptr;
211+
}
212+
} while (sc != c);
213+
} while (strncmp_constexpr(str, target, len) != 0);
214+
--str;
215+
}
216+
return str;
217+
}

Marlin/src/HAL/NATIVE_SIM/MarlinSPI.h

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Marlin 3D Printer Firmware
3+
*
4+
* Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
5+
* Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
6+
* Copyright (c) 2015-2016 Nico Tonnhofer wurstnase.reprap@gmail.com
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
#pragma once
23+
24+
#include <SPI.h>
25+
26+
using MarlinSPI = SPIClass;

Marlin/src/HAL/NATIVE_SIM/fastio.h

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
* Marlin 3D Printer Firmware
3+
* Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4+
*
5+
* Based on Sprinter and grbl.
6+
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
#pragma once
23+
24+
/**
25+
* Fast I/O Routines for X86_64
26+
*/
27+
28+
#include "../shared/Marduino.h"
29+
#include <pinmapping.h>
30+
31+
#define SET_DIR_INPUT(IO) Gpio::setDir(IO, 1)
32+
#define SET_DIR_OUTPUT(IO) Gpio::setDir(IO, 0)
33+
34+
#define SET_MODE(IO, mode) Gpio::setMode(IO, mode)
35+
36+
#define WRITE_PIN_SET(IO) Gpio::set(IO)
37+
#define WRITE_PIN_CLR(IO) Gpio::clear(IO)
38+
39+
#define READ_PIN(IO) Gpio::get(IO)
40+
#define WRITE_PIN(IO,V) Gpio::set(IO, V)
41+
42+
/**
43+
* Magic I/O routines
44+
*
45+
* Now you can simply SET_OUTPUT(STEP); WRITE(STEP, HIGH); WRITE(STEP, LOW);
46+
*
47+
* Why double up on these macros? see http://gcc.gnu.org/onlinedocs/cpp/Stringification.html
48+
*/
49+
50+
/// Read a pin
51+
#define _READ(IO) READ_PIN(IO)
52+
53+
/// Write to a pin
54+
#define _WRITE(IO,V) WRITE_PIN(IO,V)
55+
56+
/// toggle a pin
57+
#define _TOGGLE(IO) _WRITE(IO, !READ(IO))
58+
59+
/// set pin as input
60+
#define _SET_INPUT(IO) SET_DIR_INPUT(IO)
61+
62+
/// set pin as output
63+
#define _SET_OUTPUT(IO) SET_DIR_OUTPUT(IO)
64+
65+
/// set pin as input with pullup mode
66+
#define _PULLUP(IO,V) pinMode(IO, (V) ? INPUT_PULLUP : INPUT)
67+
68+
/// set pin as input with pulldown mode
69+
#define _PULLDOWN(IO,V) pinMode(IO, (V) ? INPUT_PULLDOWN : INPUT)
70+
71+
// hg42: all pins can be input or output (I hope)
72+
// hg42: undefined pins create compile error (IO, is no pin)
73+
// hg42: currently not used, but was used by pinsDebug
74+
75+
/// check if pin is an input
76+
#define _IS_INPUT(IO) (IO >= 0)
77+
78+
/// check if pin is an output
79+
#define _IS_OUTPUT(IO) (IO >= 0)
80+
81+
/// Read a pin wrapper
82+
#define READ(IO) _READ(IO)
83+
84+
/// Write to a pin wrapper
85+
#define WRITE(IO,V) _WRITE(IO,V)
86+
87+
/// toggle a pin wrapper
88+
#define TOGGLE(IO) _TOGGLE(IO)
89+
90+
/// set pin as input wrapper
91+
#define SET_INPUT(IO) _SET_INPUT(IO)
92+
/// set pin as input with pullup wrapper
93+
#define SET_INPUT_PULLUP(IO) do{ _SET_INPUT(IO); _PULLUP(IO, HIGH); }while(0)
94+
/// set pin as input with pulldown wrapper
95+
#define SET_INPUT_PULLDOWN(IO) do{ _SET_INPUT(IO); _PULLDOWN(IO, HIGH); }while(0)
96+
/// set pin as output wrapper - reads the pin and sets the output to that value
97+
#define SET_OUTPUT(IO) do{ _WRITE(IO, _READ(IO)); _SET_OUTPUT(IO); }while(0)
98+
// set pin as PWM
99+
#define SET_PWM(IO) SET_OUTPUT(IO)
100+
101+
/// check if pin is an input wrapper
102+
#define IS_INPUT(IO) _IS_INPUT(IO)
103+
/// check if pin is an output wrapper
104+
#define IS_OUTPUT(IO) _IS_OUTPUT(IO)
105+
106+
// Shorthand
107+
#define OUT_WRITE(IO,V) do{ SET_OUTPUT(IO); WRITE(IO,V); }while(0)
108+
109+
// digitalRead/Write wrappers
110+
#define extDigitalRead(IO) digitalRead(IO)
111+
#define extDigitalWrite(IO,V) digitalWrite(IO,V)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Marlin 3D Printer Firmware
3+
* Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4+
*
5+
* Based on Sprinter and grbl.
6+
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
#pragma once

0 commit comments

Comments
 (0)