2023-10-18 22:59:56 +02:00
|
|
|
/*
|
2023-10-27 16:59:09 +02:00
|
|
|
* © 2023 Travis Farmer
|
2023-10-18 22:59:56 +02:00
|
|
|
* © 2023 Neil McKechnie
|
|
|
|
* © 2022-2023 Paul M. Antoine
|
|
|
|
* © 2021 Mike S
|
|
|
|
* © 2021, 2023 Harald Barth
|
|
|
|
* © 2021 Fred Decker
|
|
|
|
* © 2021 Chris Harlow
|
|
|
|
* © 2021 David Cutting
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// ATTENTION: this file only compiles on a STM32 based boards
|
|
|
|
// Please refer to DCCTimer.h for general comments about how this class works
|
|
|
|
// This is to avoid repetition and duplication.
|
2023-10-21 10:10:50 +02:00
|
|
|
#if defined(ARDUINO_GIGA)
|
2023-10-18 22:59:56 +02:00
|
|
|
|
|
|
|
#include "DCCTimer.h"
|
|
|
|
#include "DIAG.h"
|
2023-10-28 21:02:55 +02:00
|
|
|
#include "GigaHardwareTimer.h"
|
2023-10-23 23:41:45 +02:00
|
|
|
#include <Arduino_AdvancedAnalog.h>
|
2023-10-26 23:36:35 +02:00
|
|
|
//#include "config.h"
|
2023-10-18 22:59:56 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Experimental code for High Accuracy (HA) DCC Signal mode
|
|
|
|
// Warning - use of TIM2 and TIM3 can affect the use of analogWrite() function on certain pins,
|
|
|
|
// which is used by the DC motor types.
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2023-10-21 10:10:50 +02:00
|
|
|
INTERRUPT_CALLBACK interruptHandler=0;
|
2023-10-21 19:40:44 +02:00
|
|
|
|
2023-11-10 14:56:21 +01:00
|
|
|
#ifndef DCC_EX_TIMER
|
|
|
|
#if defined(TIM6)
|
|
|
|
#define DCC_EX_TIMER TIM6
|
|
|
|
#elif defined(TIM7)
|
|
|
|
#define DCC_EX_TIMER TIM7
|
|
|
|
#elif defined(TIM12)
|
|
|
|
#define DCC_EX_TIMER TIM12
|
|
|
|
#else
|
|
|
|
#warning This Giga variant does not have Timers 1,8 or 11!!
|
|
|
|
#endif
|
|
|
|
#endif // ifndef DCC_EX_TIMER
|
2023-10-21 19:40:44 +02:00
|
|
|
|
2023-11-10 14:56:21 +01:00
|
|
|
HardwareTimer dcctimer(TIM8);
|
2023-10-21 17:55:21 +02:00
|
|
|
void DCCTimer_Handler() __attribute__((interrupt));
|
|
|
|
|
|
|
|
void DCCTimer_Handler() {
|
|
|
|
interruptHandler();
|
|
|
|
}
|
2023-10-21 10:10:50 +02:00
|
|
|
|
|
|
|
void DCCTimer::begin(INTERRUPT_CALLBACK callback) {
|
|
|
|
interruptHandler=callback;
|
2023-11-10 14:56:21 +01:00
|
|
|
noInterrupts();
|
|
|
|
|
|
|
|
dcctimer.pause();
|
|
|
|
dcctimer.setPrescaleFactor(1);
|
|
|
|
// timer.setOverflow(CLOCK_CYCLES * 2);
|
|
|
|
dcctimer.setOverflow(DCC_SIGNAL_TIME, MICROSEC_FORMAT);
|
|
|
|
// dcctimer.attachInterrupt(Timer11_Handler);
|
|
|
|
dcctimer.attachInterrupt(DCCTimer_Handler);
|
|
|
|
dcctimer.setInterruptPriority(0, 0); // Set highest preemptive priority!
|
|
|
|
dcctimer.refresh();
|
|
|
|
dcctimer.resume();
|
|
|
|
|
|
|
|
interrupts();
|
2023-10-21 10:10:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DCCTimer::isPWMPin(byte pin) {
|
2023-11-10 14:56:21 +01:00
|
|
|
//TODO: STM32 whilst this call to digitalPinHasPWM will reveal which pins can do PWM,
|
|
|
|
// there's no support yet for High Accuracy, so for now return false
|
|
|
|
// return digitalPinHasPWM(pin);
|
|
|
|
(void) pin;
|
|
|
|
return false;
|
2023-10-21 10:10:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DCCTimer::setPWM(byte pin, bool high) {
|
2023-11-10 14:56:21 +01:00
|
|
|
// TODO: High Accuracy mode is not supported as yet, and may never need to be
|
|
|
|
(void) pin;
|
|
|
|
(void) high;
|
|
|
|
return;
|
2023-10-21 10:10:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DCCTimer::clearPWM() {
|
2023-11-10 14:56:21 +01:00
|
|
|
return;
|
2023-10-21 10:10:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DCCTimer::getSimulatedMacAddress(byte mac[6]) {
|
2023-10-27 09:43:41 +02:00
|
|
|
volatile uint32_t *serno1 = (volatile uint32_t *)UID_BASE;
|
|
|
|
volatile uint32_t *serno2 = (volatile uint32_t *)UID_BASE+4;
|
2023-10-27 14:47:24 +02:00
|
|
|
volatile uint32_t *serno3 = (volatile uint32_t *)UID_BASE+8;
|
2023-10-27 09:43:41 +02:00
|
|
|
volatile uint32_t m1 = *serno1;
|
|
|
|
volatile uint32_t m2 = *serno2;
|
2023-10-27 14:47:24 +02:00
|
|
|
volatile uint32_t m3 = *serno3;
|
|
|
|
mac[0] = 0xBE;
|
|
|
|
mac[1] = 0xEF;
|
|
|
|
mac[2] = m1 ^ m3 >> 24;
|
|
|
|
mac[3] = m1 ^ m3 >> 16;
|
|
|
|
mac[4] = m1 ^ m3 >> 8;
|
|
|
|
mac[5] = m1 ^ m3 >> 0;
|
|
|
|
//DIAG(F("MAC: %P:%P:%P:%P:%P:%P"),mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
|
2023-10-27 09:43:41 +02:00
|
|
|
|
2023-10-21 10:10:50 +02:00
|
|
|
}
|
|
|
|
volatile int DCCTimer::minimum_free_memory=__INT_MAX__;
|
|
|
|
|
|
|
|
// Return low memory value...
|
|
|
|
int DCCTimer::getMinimumFreeMemory() {
|
|
|
|
noInterrupts(); // Disable interrupts to get volatile value
|
|
|
|
int retval = freeMemory();
|
|
|
|
interrupts();
|
|
|
|
return retval;
|
|
|
|
}
|
2023-10-21 21:57:50 +02:00
|
|
|
extern "C" char* sbrk(int incr);
|
2023-10-21 10:10:50 +02:00
|
|
|
|
|
|
|
int DCCTimer::freeMemory() {
|
2023-10-21 21:57:50 +02:00
|
|
|
|
2023-10-21 10:10:50 +02:00
|
|
|
char top;
|
2023-10-22 14:16:34 +02:00
|
|
|
unsigned int tmp = (unsigned int)(&top - reinterpret_cast<char*>(sbrk(0)));
|
|
|
|
return (int)(tmp / 1000);
|
2023-10-21 10:10:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DCCTimer::reset() {
|
2023-10-21 11:38:13 +02:00
|
|
|
//Watchdog &watchdog = Watchdog::get_instance();
|
|
|
|
//Watchdog::stop();
|
|
|
|
//Watchdog::start(500);
|
2023-10-21 12:16:46 +02:00
|
|
|
|
|
|
|
//while(true) {};
|
2023-11-10 14:56:21 +01:00
|
|
|
return;
|
2023-10-21 10:10:50 +02:00
|
|
|
}
|
2023-10-18 22:59:56 +02:00
|
|
|
|
2023-10-24 16:11:55 +02:00
|
|
|
int * ADCee::analogvals = NULL;
|
|
|
|
|
2023-10-18 22:59:56 +02:00
|
|
|
int16_t ADCee::ADCmax()
|
|
|
|
{
|
2023-11-12 10:44:59 +01:00
|
|
|
return 4095;
|
2023-10-18 22:59:56 +02:00
|
|
|
}
|
2023-10-24 16:11:55 +02:00
|
|
|
|
2023-11-10 19:36:40 +01:00
|
|
|
AdvancedADC adc;
|
|
|
|
pin_size_t active_pins[] = {A0, A1, A2, A3};
|
|
|
|
pin_size_t active_pinsB[] = {A4, A5, A6, A7};
|
|
|
|
int num_active_pins = 4;
|
2023-11-10 20:24:14 +01:00
|
|
|
const int samples_per_round = 512;
|
2023-10-18 22:59:56 +02:00
|
|
|
int ADCee::init(uint8_t pin) {
|
2023-11-10 19:36:40 +01:00
|
|
|
adc.stop();
|
2023-11-12 10:44:59 +01:00
|
|
|
if (pin >= A0 && pin <= A3) adc.begin(AN_RESOLUTION_12, 16000, 1, samples_per_round, num_active_pins, active_pins);
|
|
|
|
else if (pin >= A4 && pin <= A7) adc.begin(AN_RESOLUTION_12, 16000, 1, samples_per_round, num_active_pins, active_pinsB);
|
2023-10-23 23:47:40 +02:00
|
|
|
return 123;
|
2023-10-18 22:59:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read function ADCee::read(pin) to get value instead of analogRead(pin)
|
|
|
|
*/
|
|
|
|
int ADCee::read(uint8_t pin, bool fromISR) {
|
2023-11-10 19:36:40 +01:00
|
|
|
int tmpPin = 0;
|
|
|
|
if (pin >= A0 && pin <= A3) tmpPin = (pin - A0);
|
|
|
|
else if (pin >= A4 && pin <= A7) tmpPin = ((pin - A0) - 4);
|
2023-10-24 16:11:55 +02:00
|
|
|
static SampleBuffer buf = adc.read();
|
|
|
|
int retVal = -123;
|
2023-10-23 23:41:45 +02:00
|
|
|
if (adc.available()) {
|
|
|
|
buf.release();
|
2023-10-24 16:11:55 +02:00
|
|
|
buf = adc.read();
|
2023-10-23 20:38:29 +02:00
|
|
|
}
|
2023-11-10 19:36:40 +01:00
|
|
|
return (buf[tmpPin]);
|
2023-10-18 22:59:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Scan function that is called from interrupt
|
|
|
|
*/
|
2023-10-23 13:29:37 +02:00
|
|
|
#pragma GCC push_options
|
|
|
|
#pragma GCC optimize ("-O3")
|
2023-10-18 22:59:56 +02:00
|
|
|
void ADCee::scan() {
|
|
|
|
}
|
2023-10-23 13:29:37 +02:00
|
|
|
#pragma GCC pop_options
|
2023-10-18 22:59:56 +02:00
|
|
|
|
|
|
|
void ADCee::begin() {
|
|
|
|
noInterrupts();
|
2023-10-23 16:30:04 +02:00
|
|
|
|
2023-10-18 22:59:56 +02:00
|
|
|
interrupts();
|
|
|
|
}
|
|
|
|
#endif
|