From 887a4eedd90f0e9bfa82a1503926b16291c497ac Mon Sep 17 00:00:00 2001 From: Phil Hord Date: Wed, 18 May 2016 16:21:03 -0400 Subject: [PATCH] Simulator: fix pin numbering consistency The simulator code is compiled with different definitions than the rest of the code even when compiling the simulator. This was done originally to satisfy the compiler, but it was the wrong way to go. The result is that the main Teacup code may decide to do things one way (X_INVERT_DIR, for example) but the simulator code will do things a different way (no X_INVERT_DIR). Fix this by including the board and printer definitions also in the simulator code, and use a simple enum trick to give consistent definitions to the needed PIN definitions, safely ignoring the ones the config does not use. This requires that we include simulator.h after 'config.h' in all cases. Manage that by moving simulator.h from its previous home in arduino.h into config_wrapper.h. After this change we will be able to reliably communicate the expected state of the endstop pins from the simulator. --- arduino.h | 14 ++++---- config_wrapper.h | 2 ++ sendf.h | 2 +- simulator.h | 76 ++++++++++++++++++++++++------------------- simulator/simulator.c | 9 ++++- 5 files changed, 60 insertions(+), 43 deletions(-) diff --git a/arduino.h b/arduino.h index 719d490..4df6f5d 100644 --- a/arduino.h +++ b/arduino.h @@ -73,16 +73,14 @@ #include "arduino_lpc1114.h" #endif -#elif defined SIMULATOR - - #include "simulator.h" - #endif /* __AVR__, __ARMEL__, SIMULATOR */ -#if ! defined DIO0_PIN && ! defined PIO0_1_PIN - #error Pins for this chip not defined in arduino.h! If you write an \ - appropriate pin definition and have this firmware work on your chip, \ - please tell us via Github or the forum thread. +#ifndef SIMULATOR + #if ! defined DIO0_PIN && ! defined PIO0_1_PIN + #error Pins for this chip not defined in arduino.h! If you write an \ + appropriate pin definition and have this firmware work on your chip, \ + please tell us via Github or the forum thread. + #endif #endif #ifndef BSS diff --git a/config_wrapper.h b/config_wrapper.h index 47869b6..78083b1 100644 --- a/config_wrapper.h +++ b/config_wrapper.h @@ -19,6 +19,8 @@ #include USER_CONFIG +#include "simulator.h" + /** Give users a hint in case they obviously forgot to read instructions. */ diff --git a/sendf.h b/sendf.h index 64c7249..6f735b2 100644 --- a/sendf.h +++ b/sendf.h @@ -2,7 +2,7 @@ #ifndef _SENDF_H #define _SENDF_H -#include "arduino.h" +#include "config_wrapper.h" // No __attribute__ ((format (printf, 1, 2)) here because %q isn't supported. diff --git a/simulator.h b/simulator.h index 6392706..1e2ed2c 100644 --- a/simulator.h +++ b/simulator.h @@ -1,30 +1,5 @@ #ifdef SIMULATOR -#undef X_STEP_PIN -#undef X_DIR_PIN -#undef X_MIN_PIN -#undef X_ENABLE_PIN -#undef Y_STEP_PIN -#undef Y_DIR_PIN -#undef Y_MIN_PIN -#undef Y_ENABLE_PIN -#undef Z_STEP_PIN -#undef Z_DIR_PIN -#undef Z_MIN_PIN -#undef Z_ENABLE_PIN -#undef E_STEP_PIN -#undef E_DIR_PIN -#undef E_ENABLE_PIN -#undef STEPPER_ENABLE_PIN - -#undef PS_MOSFET_PIN -#undef PS_ON_PIN -#undef RX_ENABLE_PIN -#undef TX_ENABLE_PIN -#undef X_MAX_PIN -#undef Y_MAX_PIN -#undef Z_MAX_PIN - // Compiler appeasement #undef disable_transmit #undef enable_transmit @@ -56,6 +31,42 @@ #define eeprom_write_dword(ptr32, i32) (*(ptr32)=i32) #define eeprom_write_word(ptr16, i16) (*(ptr16)=i16) + +/** + The following enum gives numeric values to each of the pin names which would + normally be defined in the chip-specific include file. All of the pins + Teacup uses are listed here even though not all of them may be defined in the + config. + + Let's consider the X_MIN_PIN as an example. When the following code says + + enum { ... X_MIN_PIN, ...} + + it means one of these two different things: + + 1. X_MIN_PIN is defined in the board config file. For example, + + #define X_MIN_PIN DIO7 + + In this case the C pre-processor will replace X_MIN_PIN below with DIO7, + and DIO7 will be enumerated (given a numeric value) as 2. + + 2. X_MIN_PIN is not defined in the board config file, which Teacup interprets + as the printer not having an X-min endstop. In this case X_MIN_PIN will + be enumerated (given a numeric value) as 2. + + Importantly the C preprocessor interpret "defined(X_MIN_PIN)" the same even + with this enum in place. So in the first case, defined(X_MIN_PIN) is true; + in the second case, it is not (even though we have enumerated it a value). + + The simulator uses this trick to provide stand-in values for all the pins the + config does or does not define, but the conditionally compiled code will + react consistently between the simulated or the real controller compilation + targets. + + Additionally, this trick ensures each pin has a known value when it is used. + For example, X_MIN_PIN will always be '2' in the simulator when it is used. +*/ typedef enum { // Define pins used X_STEP_PIN, @@ -76,6 +87,12 @@ typedef enum { STEPPER_ENABLE_PIN, + X_MAX_PIN, + Y_MAX_PIN, + Z_MAX_PIN, + PS_ON_PIN, + PS_MOSFET_PIN, + SCK, MOSI, MISO, @@ -83,14 +100,7 @@ typedef enum { RX_ENABLE_PIN, TX_ENABLE_PIN, -/* - * Not used in the simulator. Add them to this list to enable them if needed. - PS_MOSFET_PIN, - PS_ON_PIN, - X_MAX_PIN, - Y_MAX_PIN, - Z_MAX_PIN, -*/ + PIN_NB /* End of PINS marker; Put all new pins before this one */ } pin_t; diff --git a/simulator/simulator.c b/simulator/simulator.c index 8e7c470..5784329 100644 --- a/simulator/simulator.c +++ b/simulator/simulator.c @@ -6,7 +6,7 @@ // If no time scale specified, use 1/10th real-time for simulator #define DEFAULT_TIME_SCALE 10 - +#include "config_wrapper.h" #include "simulator.h" #include "data_recorder.h" @@ -157,6 +157,13 @@ void sim_start(int argc, char** argv) { NAME_PIN(E_ENABLE_PIN); NAME_PIN(STEPPER_ENABLE_PIN); + + // Rarely used; uncomment here if you want to see them in the datalog. + //NAME_PIN(X_MAX_PIN); + //NAME_PIN(Y_MAX_PIN); + //NAME_PIN(Z_MAX_PIN); + //NAME_PIN(PS_ON_PIN); + //NAME_PIN(PS_MOSFET_PIN); } /* -- debugging ------------------------------------------------------------ */