1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-24 16:46:13 +01:00

Compare commits

..

7 Commits

Author SHA1 Message Date
Travis Farmer
011f9396ec
Merge 98f00300ec into d46a6f092a 2023-11-10 20:10:21 +00:00
travis-farmer
98f00300ec
removed I2C port change option as it is breaking 2023-11-10 15:09:56 -05:00
travis-farmer
9a6a305fc5
so far, 8 good signals, one (tested) is DC 2023-11-10 14:24:14 -05:00
travis-farmer
904fd5a780
possible fix for 4+ tracks 2023-11-10 13:36:40 -05:00
travis-farmer
40c0c20df8
Post tests - PASSED 2023-11-10 10:46:14 -05:00
travis-farmer
36db724466
fixed Typo 2023-11-10 09:17:39 -05:00
travis-farmer
5c0ab9a311
pre-test storing of signal updates 2023-11-10 08:56:21 -05:00
3 changed files with 68 additions and 93 deletions

View File

@ -43,14 +43,19 @@
INTERRUPT_CALLBACK interruptHandler=0; INTERRUPT_CALLBACK interruptHandler=0;
//HardwareTimer* timer = NULL; #ifndef DCC_EX_TIMER
//HardwareTimer* timerAux = NULL; #if defined(TIM6)
HardwareTimer timer(TIM3); #define DCC_EX_TIMER TIM6
HardwareTimer timerAux(TIM2); #elif defined(TIM7)
#define DCC_EX_TIMER TIM7
static bool tim2ModeHA = false; #elif defined(TIM12)
static bool tim3ModeHA = false; #define DCC_EX_TIMER TIM12
#else
#warning This Giga variant does not have Timers 1,8 or 11!!
#endif
#endif // ifndef DCC_EX_TIMER
HardwareTimer dcctimer(TIM8);
void DCCTimer_Handler() __attribute__((interrupt)); void DCCTimer_Handler() __attribute__((interrupt));
void DCCTimer_Handler() { void DCCTimer_Handler() {
@ -59,66 +64,38 @@ void DCCTimer_Handler() {
void DCCTimer::begin(INTERRUPT_CALLBACK callback) { void DCCTimer::begin(INTERRUPT_CALLBACK callback) {
interruptHandler=callback; interruptHandler=callback;
noInterrupts(); noInterrupts();
// adc_set_sample_rate(ADC_SAMPLETIME_480CYCLES); dcctimer.pause();
timer.pause(); dcctimer.setPrescaleFactor(1);
timerAux.pause(); // timer.setOverflow(CLOCK_CYCLES * 2);
timer.setPrescaleFactor(1); dcctimer.setOverflow(DCC_SIGNAL_TIME, MICROSEC_FORMAT);
timer.setOverflow(DCC_SIGNAL_TIME, MICROSEC_FORMAT); // dcctimer.attachInterrupt(Timer11_Handler);
timer.attachInterrupt(DCCTimer_Handler); dcctimer.attachInterrupt(DCCTimer_Handler);
timer.refresh(); dcctimer.setInterruptPriority(0, 0); // Set highest preemptive priority!
timerAux.setPrescaleFactor(1); dcctimer.refresh();
timerAux.setOverflow(DCC_SIGNAL_TIME, MICROSEC_FORMAT); dcctimer.resume();
timerAux.refresh();
timer.resume(); interrupts();
timerAux.resume();
interrupts();
} }
bool DCCTimer::isPWMPin(byte pin) { bool DCCTimer::isPWMPin(byte pin) {
switch (pin) { //TODO: STM32 whilst this call to digitalPinHasPWM will reveal which pins can do PWM,
case 12: // there's no support yet for High Accuracy, so for now return false
return true; // return digitalPinHasPWM(pin);
case 13: (void) pin;
return true; return false;
default:
return false;
}
} }
void DCCTimer::setPWM(byte pin, bool high) { void DCCTimer::setPWM(byte pin, bool high) {
switch (pin) { // TODO: High Accuracy mode is not supported as yet, and may never need to be
case 12: (void) pin;
if (!tim3ModeHA) { (void) high;
timerAux.setMode(1, TIMER_OUTPUT_COMPARE_INACTIVE, 12); return;
tim3ModeHA = true;
}
if (high)
TIM3->CCMR1 = (TIM3->CCMR1 & ~TIM_CCMR1_OC1M_Msk) | TIM_CCMR1_OC1M_0;
else
TIM3->CCMR1 = (TIM3->CCMR1 & ~TIM_CCMR1_OC1M_Msk) | TIM_CCMR1_OC1M_1;
break;
case 13:
if (!tim2ModeHA) {
timer.setMode(1, TIMER_OUTPUT_COMPARE_INACTIVE, 13);
tim2ModeHA = true;
}
if (high)
TIM2->CCMR1 = (TIM2->CCMR1 & ~TIM_CCMR1_OC1M_Msk) | TIM_CCMR1_OC1M_0;
else
TIM2->CCMR1 = (TIM2->CCMR1 & ~TIM_CCMR1_OC1M_Msk) | TIM_CCMR1_OC1M_1;
break;
}
} }
void DCCTimer::clearPWM() { void DCCTimer::clearPWM() {
timer.setMode(1, TIMER_OUTPUT_COMPARE_INACTIVE, NC); return;
tim2ModeHA = false;
timerAux.setMode(1, TIMER_OUTPUT_COMPARE_INACTIVE, NC);
tim3ModeHA = false;
} }
void DCCTimer::getSimulatedMacAddress(byte mac[6]) { void DCCTimer::getSimulatedMacAddress(byte mac[6]) {
@ -161,6 +138,7 @@ void DCCTimer::reset() {
//Watchdog::start(500); //Watchdog::start(500);
//while(true) {}; //while(true) {};
return;
} }
int * ADCee::analogvals = NULL; int * ADCee::analogvals = NULL;
@ -170,9 +148,15 @@ int16_t ADCee::ADCmax()
return 1023; return 1023;
} }
AdvancedADC adc(A0, A1); 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;
const int samples_per_round = 512;
int ADCee::init(uint8_t pin) { int ADCee::init(uint8_t pin) {
adc.begin(AN_RESOLUTION_10, 16000, 1, 512); adc.stop();
if (pin >= A0 && pin <= A3) adc.begin(AN_RESOLUTION_10, 16000, 1, samples_per_round, num_active_pins, active_pins);
else if (pin >= A4 && pin <= A7) adc.begin(AN_RESOLUTION_10, 16000, 1, samples_per_round, num_active_pins, active_pinsB);
return 123; return 123;
} }
@ -180,13 +164,16 @@ int ADCee::init(uint8_t pin) {
* Read function ADCee::read(pin) to get value instead of analogRead(pin) * Read function ADCee::read(pin) to get value instead of analogRead(pin)
*/ */
int ADCee::read(uint8_t pin, bool fromISR) { int ADCee::read(uint8_t pin, bool fromISR) {
int tmpPin = 0;
if (pin >= A0 && pin <= A3) tmpPin = (pin - A0);
else if (pin >= A4 && pin <= A7) tmpPin = ((pin - A0) - 4);
static SampleBuffer buf = adc.read(); static SampleBuffer buf = adc.read();
int retVal = -123; int retVal = -123;
if (adc.available()) { if (adc.available()) {
buf.release(); buf.release();
buf = adc.read(); buf = adc.read();
} }
return (buf[pin - A0]); return (buf[tmpPin]);
} }
/* /*

View File

@ -35,11 +35,7 @@
#define WIRE_HAS_TIMEOUT #define WIRE_HAS_TIMEOUT
#endif #endif
#if defined(GIGA_I2C_1)
#define DCCEX_WIRE Wire1
#else
#define DCCEX_WIRE Wire
#endif
@ -47,9 +43,9 @@
* Initialise I2C interface software * Initialise I2C interface software
***************************************************************************/ ***************************************************************************/
void I2CManagerClass::_initialise() { void I2CManagerClass::_initialise() {
DCCEX_WIRE.begin(); Wire.begin();
#if defined(WIRE_HAS_TIMEOUT) #if defined(WIRE_HAS_TIMEOUT)
DCCEX_WIRE.setWireTimeout(_timeout, true); Wire.setWireTimeout(_timeout, true);
#endif #endif
} }
@ -58,7 +54,7 @@ void I2CManagerClass::_initialise() {
* on Arduino. Mega4809 supports 1000000 (Fast+) too. * on Arduino. Mega4809 supports 1000000 (Fast+) too.
***************************************************************************/ ***************************************************************************/
void I2CManagerClass::_setClock(unsigned long i2cClockSpeed) { void I2CManagerClass::_setClock(unsigned long i2cClockSpeed) {
DCCEX_WIRE.setClock(i2cClockSpeed); Wire.setClock(i2cClockSpeed);
} }
/*************************************************************************** /***************************************************************************
@ -69,7 +65,7 @@ void I2CManagerClass::_setClock(unsigned long i2cClockSpeed) {
void I2CManagerClass::setTimeout(unsigned long value) { void I2CManagerClass::setTimeout(unsigned long value) {
_timeout = value; _timeout = value;
#if defined(WIRE_HAS_TIMEOUT) #if defined(WIRE_HAS_TIMEOUT)
DCCEX_WIRE.setWireTimeout(value, true); Wire.setWireTimeout(value, true);
#endif #endif
} }
@ -82,7 +78,7 @@ static uint8_t muxSelect(I2CAddress address) {
I2CMux muxNo = address.muxNumber(); I2CMux muxNo = address.muxNumber();
I2CSubBus subBus = address.subBus(); I2CSubBus subBus = address.subBus();
if (muxNo != I2CMux_None) { if (muxNo != I2CMux_None) {
DCCEX_WIRE.beginTransmission(I2C_MUX_BASE_ADDRESS+muxNo); Wire.beginTransmission(I2C_MUX_BASE_ADDRESS+muxNo);
uint8_t data = (subBus == SubBus_All) ? 0xff : uint8_t data = (subBus == SubBus_All) ? 0xff :
(subBus == SubBus_None) ? 0x00 : (subBus == SubBus_None) ? 0x00 :
#if defined(I2CMUX_PCA9547) #if defined(I2CMUX_PCA9547)
@ -94,8 +90,8 @@ static uint8_t muxSelect(I2CAddress address) {
// with a bit set for the subBus to be enabled // with a bit set for the subBus to be enabled
1 << subBus; 1 << subBus;
#endif #endif
DCCEX_WIRE.write(&data, 1); Wire.write(&data, 1);
return DCCEX_WIRE.endTransmission(true); // have to release I2C bus for it to work return Wire.endTransmission(true); // have to release I2C bus for it to work
} }
return I2C_STATUS_OK; return I2C_STATUS_OK;
} }
@ -118,9 +114,9 @@ uint8_t I2CManagerClass::write(I2CAddress address, const uint8_t buffer[], uint8
#endif #endif
// Only send new transaction if address is non-zero. // Only send new transaction if address is non-zero.
if (muxStatus == I2C_STATUS_OK && address != 0) { if (muxStatus == I2C_STATUS_OK && address != 0) {
DCCEX_WIRE.beginTransmission(address); Wire.beginTransmission(address);
if (size > 0) DCCEX_WIRE.write(buffer, size); if (size > 0) Wire.write(buffer, size);
status = DCCEX_WIRE.endTransmission(); status = Wire.endTransmission();
} }
#ifdef I2C_EXTENDED_ADDRESS #ifdef I2C_EXTENDED_ADDRESS
// Deselect MUX if there's more than one MUX present, to avoid having multiple ones selected // Deselect MUX if there's more than one MUX present, to avoid having multiple ones selected
@ -169,25 +165,25 @@ uint8_t I2CManagerClass::read(I2CAddress address, uint8_t readBuffer[], uint8_t
// Only start new transaction if address is non-zero. // Only start new transaction if address is non-zero.
if (muxStatus == I2C_STATUS_OK && address != 0) { if (muxStatus == I2C_STATUS_OK && address != 0) {
if (writeSize > 0) { if (writeSize > 0) {
DCCEX_WIRE.beginTransmission(address); Wire.beginTransmission(address);
DCCEX_WIRE.write(writeBuffer, writeSize); Wire.write(writeBuffer, writeSize);
status = DCCEX_WIRE.endTransmission(false); // Don't free bus yet status = Wire.endTransmission(false); // Don't free bus yet
} }
if (status == I2C_STATUS_OK) { if (status == I2C_STATUS_OK) {
#ifdef WIRE_HAS_TIMEOUT #ifdef WIRE_HAS_TIMEOUT
DCCEX_WIRE.clearWireTimeoutFlag(); Wire.clearWireTimeoutFlag();
DCCEX_WIRE.requestFrom(address, (size_t)readSize); Wire.requestFrom(address, (size_t)readSize);
if (!DCCEX_WIRE.getWireTimeoutFlag()) { if (!Wire.getWireTimeoutFlag()) {
while (DCCEX_WIRE.available() && nBytes < readSize) while (Wire.available() && nBytes < readSize)
readBuffer[nBytes++] = DCCEX_WIRE.read(); readBuffer[nBytes++] = Wire.read();
if (nBytes < readSize) status = I2C_STATUS_TRUNCATED; if (nBytes < readSize) status = I2C_STATUS_TRUNCATED;
} else { } else {
status = I2C_STATUS_TIMEOUT; status = I2C_STATUS_TIMEOUT;
} }
#else #else
DCCEX_WIRE.requestFrom(address, (size_t)readSize); Wire.requestFrom(address, (size_t)readSize);
while (DCCEX_WIRE.available() && nBytes < readSize) while (Wire.available() && nBytes < readSize)
readBuffer[nBytes++] = DCCEX_WIRE.read(); readBuffer[nBytes++] = Wire.read();
if (nBytes < readSize) status = I2C_STATUS_TRUNCATED; if (nBytes < readSize) status = I2C_STATUS_TRUNCATED;
#endif #endif
} }

View File

@ -161,14 +161,6 @@
//#endif //#endif
#define SDA I2C_SDA #define SDA I2C_SDA
#define SCL I2C_SCL #define SCL I2C_SCL
#define DCC_EX_TIMER
// these don't work...
//extern const uint16_t PROGMEM port_to_input_PGM[];
//extern const uint16_t PROGMEM port_to_output_PGM[];
//extern const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[];
//#define digitalPinToBitMask(P) ( pgm_read_byte( digital_pin_to_bit_mask_PGM + (P) ) )
//#define portOutputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_output_PGM + (P))) )
//#define portInputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_input_PGM + (P))) )
/* TODO when ready /* TODO when ready
#elif defined(ARDUINO_ARCH_RP2040) #elif defined(ARDUINO_ARCH_RP2040)