2020-07-03 18:35:02 +02:00
|
|
|
/*
|
|
|
|
* © 2020, Chris Harlow. All rights reserved.
|
|
|
|
*
|
|
|
|
* This file is part of Asbelos DCC API
|
|
|
|
*
|
|
|
|
* This is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* It is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-05-26 13:44:02 +02:00
|
|
|
#include <Arduino.h>
|
2020-07-24 13:00:07 +02:00
|
|
|
//#include <TimerOne.h> // use IDE menu Tools..Manage Libraries to locate and install TimerOne
|
|
|
|
#include <ArduinoTimers.h> // use IDE menu Tools..Manage Libraries to locate and install TimerOne
|
2020-07-25 12:00:04 +02:00
|
|
|
#include "AnalogReadFast.h"
|
2020-05-26 13:44:02 +02:00
|
|
|
#include "Hardware.h"
|
|
|
|
#include "Config.h"
|
2020-05-29 16:44:57 +02:00
|
|
|
#include "DIAG.h"
|
2020-05-26 13:44:02 +02:00
|
|
|
|
2020-06-23 21:01:43 +02:00
|
|
|
|
2020-06-04 21:30:11 +02:00
|
|
|
#if defined(ARDUINO_ARCH_AVR)
|
2020-06-22 11:47:25 +02:00
|
|
|
#include <DIO2.h> // use IDE menu Tools..Manage Libraries to locate and install DIO2
|
2020-06-04 21:30:11 +02:00
|
|
|
#define WritePin digitalWrite2
|
2020-07-12 20:12:25 +02:00
|
|
|
#define ReadPin digitalRead2
|
2020-06-04 21:30:11 +02:00
|
|
|
#else
|
|
|
|
#define WritePin digitalWrite
|
2020-07-12 20:12:25 +02:00
|
|
|
#define ReadPin digitalRead
|
2020-06-04 21:30:11 +02:00
|
|
|
#endif
|
|
|
|
|
2020-05-26 13:44:02 +02:00
|
|
|
void Hardware::init() {
|
|
|
|
pinMode(MAIN_POWER_PIN, OUTPUT);
|
2020-07-10 01:03:47 +02:00
|
|
|
pinMode(MAIN_BRAKE_PIN, OUTPUT);
|
2020-05-26 13:44:02 +02:00
|
|
|
pinMode(MAIN_SIGNAL_PIN, OUTPUT);
|
2020-07-12 01:36:56 +02:00
|
|
|
if (MAIN_SIGNAL_PIN_ALT != UNUSED_PIN) pinMode(MAIN_SIGNAL_PIN_ALT, OUTPUT);
|
2020-05-26 13:44:02 +02:00
|
|
|
pinMode(MAIN_SENSE_PIN, INPUT);
|
2020-07-12 01:36:56 +02:00
|
|
|
if (MAIN_FAULT_PIN != UNUSED_PIN) pinMode(MAIN_FAULT_PIN, INPUT);
|
2020-05-26 13:44:02 +02:00
|
|
|
|
|
|
|
pinMode(PROG_POWER_PIN, OUTPUT);
|
2020-07-10 01:03:47 +02:00
|
|
|
pinMode(PROG_BRAKE_PIN, OUTPUT);
|
2020-05-26 13:44:02 +02:00
|
|
|
pinMode(PROG_SIGNAL_PIN, OUTPUT);
|
2020-07-12 01:36:56 +02:00
|
|
|
if (PROG_SIGNAL_PIN_ALT != UNUSED_PIN) pinMode(PROG_SIGNAL_PIN_ALT, OUTPUT);
|
2020-05-26 13:44:02 +02:00
|
|
|
pinMode(PROG_SENSE_PIN, INPUT);
|
2020-07-12 01:36:56 +02:00
|
|
|
if (PROG_FAULT_PIN != UNUSED_PIN) pinMode(PROG_FAULT_PIN, INPUT);
|
2020-05-26 13:44:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Hardware::setPower(bool isMainTrack, bool on) {
|
2020-06-04 21:30:11 +02:00
|
|
|
WritePin(isMainTrack ? MAIN_POWER_PIN : PROG_POWER_PIN, on ? HIGH : LOW);
|
2020-05-26 13:44:02 +02:00
|
|
|
}
|
2020-05-26 19:34:54 +02:00
|
|
|
void Hardware::setBrake(bool isMainTrack, bool on) {
|
2020-07-25 18:49:37 +02:00
|
|
|
WritePin(isMainTrack ? MAIN_BRAKE_PIN : PROG_BRAKE_PIN, on ? HIGH : LOW);
|
2020-05-26 19:34:54 +02:00
|
|
|
}
|
2020-05-26 13:44:02 +02:00
|
|
|
|
|
|
|
void Hardware::setSignal(bool isMainTrack, bool high) {
|
|
|
|
byte pin = isMainTrack ? MAIN_SIGNAL_PIN : PROG_SIGNAL_PIN;
|
|
|
|
byte pin2 = isMainTrack ? MAIN_SIGNAL_PIN_ALT : PROG_SIGNAL_PIN_ALT;
|
2020-06-04 21:30:11 +02:00
|
|
|
WritePin(pin, high ? HIGH : LOW);
|
2020-07-12 01:36:56 +02:00
|
|
|
if (pin2 != UNUSED_PIN) WritePin(pin2, high ? LOW : HIGH);
|
2020-05-26 13:44:02 +02:00
|
|
|
}
|
|
|
|
|
2020-08-14 10:41:14 +02:00
|
|
|
void Hardware::setSyncSignal(bool high) {
|
|
|
|
// This sets the same signal down both tracks at the same time.
|
|
|
|
// Speed notes....
|
|
|
|
// Objective is to get the two track signals to change as close as possible
|
|
|
|
// the high ? HIGH:LOW will only be evaluated once
|
|
|
|
// The UNUSED_PIN check will be done at compile time.
|
|
|
|
// If even more speed is required, its possible (not SAMD) to pre-prepare the
|
|
|
|
// DIO pinnumber->pincode translation so the WritePin (digitalWrite2) does not
|
|
|
|
// have to calculate the register and bit numbers every time.
|
|
|
|
|
|
|
|
WritePin(MAIN_SIGNAL_PIN, high ? HIGH : LOW);
|
|
|
|
WritePin(PROG_SIGNAL_PIN, high ? HIGH : LOW);
|
|
|
|
if (MAIN_SIGNAL_PIN_ALT != UNUSED_PIN) WritePin(MAIN_SIGNAL_PIN_ALT, high ? LOW : HIGH);
|
|
|
|
if (PROG_SIGNAL_PIN_ALT != UNUSED_PIN) WritePin(PROG_SIGNAL_PIN_ALT, high ? LOW : HIGH);
|
|
|
|
}
|
|
|
|
|
2020-07-02 18:54:09 +02:00
|
|
|
int Hardware::getCurrentRaw(bool isMainTrack) {
|
2020-07-12 01:36:56 +02:00
|
|
|
// tooo much crap for a interrupt routine. Will see how that goes.
|
|
|
|
byte faultpin = isMainTrack ? MAIN_FAULT_PIN : PROG_FAULT_PIN;
|
2020-07-15 23:51:50 +02:00
|
|
|
byte powerpin = isMainTrack ? MAIN_POWER_PIN : PROG_POWER_PIN;
|
2020-07-03 00:19:59 +02:00
|
|
|
|
2020-07-15 23:51:50 +02:00
|
|
|
if (faultpin != UNUSED_PIN && ReadPin(faultpin) == LOW && ReadPin(powerpin) == HIGH)
|
|
|
|
return (int) (32000 / (isMainTrack ? MAIN_SENSE_FACTOR : PROG_SENSE_FACTOR)); // 32A should be enough
|
2020-07-02 13:49:35 +02:00
|
|
|
// IMPORTANT: This function can be called in Interrupt() time within the 56uS timer
|
|
|
|
// The default analogRead takes ~100uS which is catastrphic
|
|
|
|
// so analogReadFast is used here. (-2uS)
|
2020-07-02 18:54:09 +02:00
|
|
|
return analogReadFast(isMainTrack ? MAIN_SENSE_PIN : PROG_SENSE_PIN);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-04 21:44:36 +02:00
|
|
|
unsigned int Hardware::getCurrentMilliamps(bool isMainTrack, int raw) {
|
2020-07-15 23:51:50 +02:00
|
|
|
return (unsigned int)(raw * (isMainTrack ? MAIN_SENSE_FACTOR : PROG_SENSE_FACTOR));
|
2020-07-04 21:44:36 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 13:44:02 +02:00
|
|
|
void Hardware::setCallback(int duration, void (*isr)()) {
|
2020-07-24 13:00:07 +02:00
|
|
|
TimerA.initialize();
|
|
|
|
TimerA.setPeriod(duration);
|
|
|
|
TimerA.attachInterrupt(isr);
|
|
|
|
TimerA.start();
|
2020-05-26 18:06:15 +02:00
|
|
|
}
|
2020-06-04 21:30:11 +02:00
|
|
|
|
2020-07-15 23:51:50 +02:00
|
|
|
// shortcut to cpu dependent high speed write
|
|
|
|
void Hardware::pinWrite(int pin, bool high) {
|
|
|
|
WritePin(pin,high);
|
|
|
|
}
|
2020-06-23 18:43:50 +02:00
|
|
|
|
2020-06-22 11:47:25 +02:00
|
|
|
// Railcom support functions, not yet implemented
|
2020-06-30 10:09:39 +02:00
|
|
|
//void Hardware::setSingleCallback(int duration, void (*isr)()) {
|
2020-06-22 11:47:25 +02:00
|
|
|
// Timer2.initialize(duration);
|
|
|
|
// Timer2.disablePwm(TIMER1_A_PIN);
|
|
|
|
// Timer2.disablePwm(TIMER1_B_PIN);
|
|
|
|
// Timer2.attachInterrupt(isr);
|
2020-06-30 10:09:39 +02:00
|
|
|
//}
|
2020-06-22 11:47:25 +02:00
|
|
|
|
2020-06-30 10:09:39 +02:00
|
|
|
//void Hardware::resetSingleCallback(int duration) {
|
2020-06-22 11:47:25 +02:00
|
|
|
// if (duration==0) Timer2.stop();
|
|
|
|
// else Timer2.initialize(duration);
|
2020-06-30 10:09:39 +02:00
|
|
|
//}
|