diff --git a/CHANGELOG.md b/CHANGELOG.md index df16746b..fae042e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added changelog merge automation. - Added StreamFit - Added developer tools html. +- Added automatic board revision detection. ### Changed - Fixed a few compile issues for case sensitive operating systems. diff --git a/include/Main.h b/include/Main.h index ee2ad928..10b01d06 100644 --- a/include/Main.h +++ b/include/Main.h @@ -12,6 +12,7 @@ #include "SmartSpin_parameters.h" #include "BLE_Common.h" #include "LittleFS_Upgrade.h" +#include "boards.h" #define MAIN_LOG_TAG "Main" diff --git a/include/boards.h b/include/boards.h new file mode 100644 index 00000000..51b196bf --- /dev/null +++ b/include/boards.h @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2020 Anthony Doud & Joel Baranick + * All rights reserved + * + * SPDX-License-Identifier: GPL-2.0-only + */ + +#pragma once + +#include "settings.h" +#ifndef UNIT_TEST +#include +#else +#include +#endif + +class Board { + public: + String name; + int versionVoltage; + int shiftUpPin; + int shiftDownPin; + int enablePin; + int stepPin; + int dirPin; + int stepperSerialTxPin; + int stepperSerialRxPin; + int auxSerialTxPin; + int auxSerialRxPin; +}; + +class Boards { + public: + Board rev1; + Board rev2; + + Boards() { + // Rev 1 + rev1.name = r1_NAME; + rev1.versionVoltage = r1_VERSION_VOLTAGE; + rev1.shiftUpPin = r1_SHIFT_UP_PIN; + rev1.shiftDownPin = r1_SHIFT_DOWN_PIN; + rev1.enablePin = r1_ENABLE_PIN; + rev1.stepPin = r1_STEP_PIN; + rev1.dirPin = r1_DIR_PIN; + rev1.stepperSerialTxPin = r1_STEPPERSERIAL_TX; + rev1.stepperSerialRxPin = r1_STEPPERSERIAL_RX; + rev1.auxSerialTxPin = 0; + rev1.auxSerialRxPin = 0; + // Rev 2 + rev2.name = r2_NAME; + rev2.versionVoltage = r2_VERSION_VOLTAGE; + rev2.shiftUpPin = r2_SHIFT_UP_PIN; + rev2.shiftDownPin = r2_SHIFT_DOWN_PIN; + rev2.enablePin = r2_ENABLE_PIN; + rev2.stepPin = r2_STEP_PIN; + rev2.dirPin = r2_DIR_PIN; + rev2.stepperSerialTxPin = r2_STEPPERSERIAL_TX; + rev2.stepperSerialRxPin = r2_STEPPERSERIAL_RX; + rev2.auxSerialTxPin = r2_AUX_SERIAL_TX; + rev2.auxSerialRxPin = r2_AUX_SERIAL_RX; + } +}; \ No newline at end of file diff --git a/include/settings.h b/include/settings.h index 85b89daa..0c5915c6 100644 --- a/include/settings.h +++ b/include/settings.h @@ -91,29 +91,73 @@ // Default debounce delay for shifters. Increase if you have false shifts. Decrease if shifting takes too long. #define DEBOUNCE_DELAY 400 -// Hardware pin for rocker Switch -#define RADIO_PIN 27 +// Hardware Revision check pin +#define REV_PIN 34 + +//////////// Defines for hardware Revision 1 //////////// + +//Board Name +#define r1_NAME "Revision One" + +// ID Voltage on pin 34. Values are 0-4095 (0-3.3v) +#define r1_VERSION_VOLTAGE 0 // Hardware pin for Shift Up -#define SHIFT_UP_PIN 19 +#define r1_SHIFT_UP_PIN 19 // Hardware pin for Shift Down -#define SHIFT_DOWN_PIN 18 +#define r1_SHIFT_DOWN_PIN 18 // Hardware pin for stepper Enable -#define ENABLE_PIN 13 +#define r1_ENABLE_PIN 13 // Hardware pin for stepper step -#define STEP_PIN 25 +#define r1_STEP_PIN 25 // Hardware pin for stepper dir -#define DIR_PIN 33 +#define r1_DIR_PIN 33 // TMC2208/TMC2224 SoftwareSerial receive pin -#define STEPPERSERIAL_RX 14 +#define r1_STEPPERSERIAL_RX 14 // TMC2208/TMC2224 SoftwareSerial transmit pin -#define STEPPERSERIAL_TX 12 +#define r1_STEPPERSERIAL_TX 12 +//////////////////////////////////////////////////////// +//////////// Defines for hardware Revision 2 //////////// + +//Board Name +#define r2_NAME "Revision Two" + +// ID Voltage on pin 34. Values are 0-4095 (0-3.3v) +#define r2_VERSION_VOLTAGE 4095 + +// Hardware pin for Shift Up +#define r2_SHIFT_UP_PIN 26 + +// Hardware pin for Shift Down +#define r2_SHIFT_DOWN_PIN 32 + +// Hardware pin for stepper Enable +#define r2_ENABLE_PIN 27 + +// Hardware pin for stepper step +#define r2_STEP_PIN 25 + +// Hardware pin for stepper dir +#define r2_DIR_PIN 33 + +// TMC2209 SoftwareSerial receive pin +#define r2_STEPPERSERIAL_RX 18 + +// TMC2209 SoftwareSerial transmit pin +#define r2_STEPPERSERIAL_TX 19 + +// TMC2209 SoftwareSerial receive pin +#define r2_AUX_SERIAL_RX 21 + +// TMC2209 SoftwareSerial transmit pin +#define r2_AUX_SERIAL_TX 22 +//////////////////////////////////////////////////////// // TMC2208/TMC2224 HardwareSerial port #define SERIAL_PORT stepperSerial diff --git a/src/Main.cpp b/src/Main.cpp index 6b81d2b9..b3422b33 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -18,6 +18,7 @@ HardwareSerial stepperSerial(2); TMC2208Stepper driver(&SERIAL_PORT, R_SENSE); // Hardware Serial + FastAccelStepperEngine engine = FastAccelStepperEngine(); FastAccelStepper *stepper = NULL; // Setup a task so the stepper will run on a different core than the main code @@ -25,6 +26,9 @@ FastAccelStepper *stepper = NULL; TaskHandle_t moveStepperTask; TaskHandle_t maintenanceLoopTask; +Boards boards; +Board currentBoard; + ///////////// Initialize the Config ///////////// SS2K ss2k; userParameters userConfig; @@ -61,8 +65,17 @@ void SS2K::stopTasks() { void setup() { // Serial port for debugging purposes Serial.begin(512000); - stepperSerial.begin(57600, SERIAL_8N2, STEPPERSERIAL_RX, STEPPERSERIAL_TX); SS2K_LOG(MAIN_LOG_TAG, "Compiled %s%s", __DATE__, __TIME__); + pinMode(REV_PIN, INPUT); + int actualVoltage = analogRead(REV_PIN); + if (actualVoltage - boards.rev1.versionVoltage >= boards.rev2.versionVoltage - actualVoltage) { + currentBoard = boards.rev2; + } else { + currentBoard = boards.rev1; + } + SS2K_LOG(MAIN_LOG_TAG, "Current Board Revision is: %s", currentBoard.name); + + stepperSerial.begin(57600, SERIAL_8N2, currentBoard.stepperSerialRxPin, currentBoard.stepperSerialTxPin); // Initialize LittleFS SS2K_LOG(MAIN_LOG_TAG, "Mounting Filesystem"); @@ -84,17 +97,16 @@ void setup() { userPWC.printFile(); userPWC.saveToLittleFS(); - pinMode(RADIO_PIN, INPUT_PULLUP); - pinMode(SHIFT_UP_PIN, INPUT_PULLUP); // Push-Button with input Pullup - pinMode(SHIFT_DOWN_PIN, INPUT_PULLUP); // Push-Button with input Pullup + pinMode(currentBoard.shiftUpPin, INPUT_PULLUP); // Push-Button with input Pullup + pinMode(currentBoard.shiftDownPin, INPUT_PULLUP); // Push-Button with input Pullup pinMode(LED_PIN, OUTPUT); - pinMode(ENABLE_PIN, OUTPUT); - pinMode(DIR_PIN, OUTPUT); // Stepper Direction Pin - pinMode(STEP_PIN, OUTPUT); // Stepper Step Pin - digitalWrite(ENABLE_PIN, + pinMode(currentBoard.enablePin, OUTPUT); + pinMode(currentBoard.dirPin, OUTPUT); // Stepper Direction Pin + pinMode(currentBoard.stepPin, OUTPUT); // Stepper Step Pin + digitalWrite(currentBoard.enablePin, HIGH); // Should be called a disable Pin - High Disables FETs - digitalWrite(DIR_PIN, LOW); - digitalWrite(STEP_PIN, LOW); + digitalWrite(currentBoard.dirPin, LOW); + digitalWrite(currentBoard.stepPin, LOW); digitalWrite(LED_PIN, LOW); ss2k.setupTMCStepperDriver(); @@ -132,8 +144,8 @@ void setup() { ss2k.resetIfShiftersHeld(); SS2K_LOG(MAIN_LOG_TAG, "Creating Shifter Interrupts"); // Setup Interrups so shifters work anytime - attachInterrupt(digitalPinToInterrupt(SHIFT_UP_PIN), ss2k.shiftUp, CHANGE); - attachInterrupt(digitalPinToInterrupt(SHIFT_DOWN_PIN), ss2k.shiftDown, CHANGE); + attachInterrupt(digitalPinToInterrupt(currentBoard.shiftUpPin), ss2k.shiftUp, CHANGE); + attachInterrupt(digitalPinToInterrupt(currentBoard.shiftDownPin), ss2k.shiftDown, CHANGE); digitalWrite(LED_PIN, HIGH); xTaskCreatePinnedToCore(SS2K::maintenanceLoop, /* Task function. */ @@ -218,9 +230,9 @@ void SS2K::restartWifi() { void SS2K::moveStepper(void *pvParameters) { engine.init(); bool _stepperDir = userConfig.getStepperDir(); - stepper = engine.stepperConnectToPin(STEP_PIN); - stepper->setDirectionPin(DIR_PIN, _stepperDir); - stepper->setEnablePin(ENABLE_PIN); + stepper = engine.stepperConnectToPin(currentBoard.stepPin); + stepper->setDirectionPin(currentBoard.dirPin, _stepperDir); + stepper->setEnablePin(currentBoard.enablePin); stepper->setAutoEnable(true); stepper->setSpeedInHz(STEPPER_SPEED); stepper->setAcceleration(STEPPER_ACCELERATION); @@ -272,7 +284,7 @@ void SS2K::moveStepper(void *pvParameters) { while (stepper->isMotorRunning()) { vTaskDelay(100 / portTICK_PERIOD_MS); } - stepper->setDirectionPin(DIR_PIN, _stepperDir); + stepper->setDirectionPin(currentBoard.dirPin, _stepperDir); } } } @@ -291,7 +303,7 @@ bool IRAM_ATTR SS2K::deBounce() { ///////////// Interrupt Functions ///////////// void IRAM_ATTR SS2K::shiftUp() { // Handle the shift up interrupt IRAM_ATTR is to keep the interrput code in ram always if (ss2k.deBounce() && !rtConfig.getERGMode()) { - if (!digitalRead(SHIFT_UP_PIN)) { // double checking to make sure the interrupt wasn't triggered by emf + if (!digitalRead(currentBoard.shiftUpPin)) { // double checking to make sure the interrupt wasn't triggered by emf rtConfig.setShifterPosition(rtConfig.getShifterPosition() - 1 + userConfig.getShifterDir() * 2); } else { ss2k.lastDebounceTime = 0; @@ -301,7 +313,7 @@ void IRAM_ATTR SS2K::shiftUp() { // Handle the shift up interrupt IRAM_ATTR is void IRAM_ATTR SS2K::shiftDown() { // Handle the shift down interrupt if (ss2k.deBounce() && !rtConfig.getERGMode()) { - if (!digitalRead(SHIFT_DOWN_PIN)) { // double checking to make sure the interrupt wasn't triggered by emf + if (!digitalRead(currentBoard.shiftDownPin)) { // double checking to make sure the interrupt wasn't triggered by emf rtConfig.setShifterPosition(rtConfig.getShifterPosition() + 1 - userConfig.getShifterDir() * 2); } else { ss2k.lastDebounceTime = 0; @@ -310,7 +322,7 @@ void IRAM_ATTR SS2K::shiftDown() { // Handle the shift down interrupt } void SS2K::resetIfShiftersHeld() { - if ((digitalRead(SHIFT_UP_PIN) == LOW) && (digitalRead(SHIFT_DOWN_PIN) == LOW)) { + if ((digitalRead(currentBoard.shiftUpPin) == LOW) && (digitalRead(currentBoard.shiftDownPin) == LOW)) { SS2K_LOG(MAIN_LOG_TAG, "Resetting to defaults via shifter buttons."); for (int x = 0; x < 10; x++) { // blink fast to acknowledge digitalWrite(LED_PIN, HIGH); @@ -328,7 +340,7 @@ void SS2K::resetIfShiftersHeld() { } void SS2K::scanIfShiftersHeld() { - if ((digitalRead(SHIFT_UP_PIN) == LOW) && (digitalRead(SHIFT_DOWN_PIN) == LOW)) { // are both shifters held? + if ((digitalRead(currentBoard.shiftUpPin) == LOW) && (digitalRead(currentBoard.shiftDownPin) == LOW)) { // are both shifters held? SS2K_LOG(MAIN_LOG_TAG, "Shifters Held %d", shiftersHoldForScan); if (shiftersHoldForScan < 1) { // have they been held for enough loops? SS2K_LOG(MAIN_LOG_TAG, "Shifters Held < 1 %d", shiftersHoldForScan);