1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-24 08:36:14 +01:00
CommandStation-EX/DCCTimerGiga.cpp

200 lines
5.2 KiB
C++
Raw Normal View History

2023-10-18 22:59:56 +02:00
/*
* @ 2023 Travis Farmer
* © 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"
#include "Portenta_H7_TimerInterrupt.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
//HardwareTimer* timer = NULL;
//HardwareTimer* timerAux = NULL;
HardwareTimer timer(TIM2);
HardwareTimer timerAux(TIM3);
static bool tim2ModeHA = false;
static bool tim3ModeHA = false;
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-10-21 21:57:50 +02:00
noInterrupts();
2023-10-21 19:40:44 +02:00
// adc_set_sample_rate(ADC_SAMPLETIME_480CYCLES);
2023-10-21 21:57:50 +02:00
timer.pause();
timerAux.pause();
timer.setPrescaleFactor(1);
timer.setOverflow(DCC_SIGNAL_TIME, MICROSEC_FORMAT);
timer.attachInterrupt(DCCTimer_Handler);
timer.refresh();
timerAux.setPrescaleFactor(1);
timerAux.setOverflow(DCC_SIGNAL_TIME, MICROSEC_FORMAT);
timerAux.refresh();
timer.resume();
timerAux.resume();
2023-10-21 19:40:44 +02:00
2023-10-21 21:57:50 +02:00
interrupts();
2023-10-21 10:10:50 +02:00
}
bool DCCTimer::isPWMPin(byte pin) {
2023-10-21 19:40:44 +02:00
switch (pin) {
case 12:
return true;
case 13:
return true;
default:
return false;
}
2023-10-21 10:10:50 +02:00
}
void DCCTimer::setPWM(byte pin, bool high) {
2023-10-21 19:40:44 +02:00
switch (pin) {
case 9:
2023-10-21 19:40:44 +02:00
if (!tim3ModeHA) {
timerAux.setMode(1, TIMER_OUTPUT_COMPARE_INACTIVE, 9);
2023-10-21 19:40:44 +02:00
tim3ModeHA = true;
}
if (high)
TIM17->CCMR1 = (TIM17->CCMR1 & ~TIM_CCMR1_OC1M_Msk) | TIM_CCMR1_OC1M_0;
2023-10-21 19:40:44 +02:00
else
TIM17->CCMR1 = (TIM17->CCMR1 & ~TIM_CCMR1_OC1M_Msk) | TIM_CCMR1_OC1M_1;
2023-10-21 19:40:44 +02:00
break;
case 8:
2023-10-21 19:40:44 +02:00
if (!tim2ModeHA) {
timer.setMode(1, TIMER_OUTPUT_COMPARE_INACTIVE, 8);
2023-10-21 19:40:44 +02:00
tim2ModeHA = true;
}
if (high)
TIM16->CCMR1 = (TIM16->CCMR1 & ~TIM_CCMR1_OC1M_Msk) | TIM_CCMR1_OC1M_0;
2023-10-21 19:40:44 +02:00
else
TIM16->CCMR1 = (TIM16->CCMR1 & ~TIM_CCMR1_OC1M_Msk) | TIM_CCMR1_OC1M_1;
2023-10-21 19:40:44 +02:00
break;
}
2023-10-21 10:10:50 +02:00
}
void DCCTimer::clearPWM() {
2023-10-21 19:40:44 +02:00
timer.setMode(1, TIMER_OUTPUT_COMPARE_INACTIVE, NC);
tim2ModeHA = false;
timerAux.setMode(1, TIMER_OUTPUT_COMPARE_INACTIVE, NC);
tim3ModeHA = false;
2023-10-21 10:10:50 +02:00
}
void DCCTimer::getSimulatedMacAddress(byte mac[6]) {
volatile uint32_t *serno1 = (volatile uint32_t *)0x1FFF7A10;
volatile uint32_t *serno2 = (volatile uint32_t *)0x1FFF7A14;
// volatile uint32_t *serno3 = (volatile uint32_t *)0x1FFF7A18;
volatile uint32_t m1 = *serno1;
volatile uint32_t m2 = *serno2;
mac[0] = m1 >> 8;
mac[1] = m1 >> 0;
mac[2] = m2 >> 24;
mac[3] = m2 >> 16;
mac[4] = m2 >> 8;
mac[5] = m2 >> 0;
}
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;
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-10-21 10:10:50 +02:00
}
2023-10-18 22:59:56 +02:00
int16_t ADCee::ADCmax()
{
2023-10-23 16:30:04 +02:00
return 4095;
2023-10-18 22:59:56 +02:00
}
int ADCee::init(uint8_t pin) {
2023-10-23 16:30:04 +02:00
analogReadResolution(12);
//DIAG(F("ADCee Init: current val:%d"),analogRead(pin));
2023-10-18 22:59:56 +02:00
return analogRead(pin);
}
/*
* Read function ADCee::read(pin) to get value instead of analogRead(pin)
*/
int ADCee::read(uint8_t pin, bool fromISR) {
int current;
2023-10-23 16:30:04 +02:00
//DIAG(F("ADCee Read:%d"),fromISR);
//if (!fromISR) noInterrupts();
2023-10-18 22:59:56 +02:00
current = analogRead(pin);
2023-10-23 16:30:04 +02:00
//if (!fromISR) interrupts();
2023-10-18 22:59:56 +02:00
return current;
}
/*
* 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