1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-12-24 21:21:24 +01:00
CommandStation-EX/DCCRMT.cpp

236 lines
7.9 KiB
C++
Raw Normal View History

2021-11-14 13:10:16 +01:00
/*
2022-08-01 22:56:56 +02:00
* © 2021-2022, Harald Barth.
2021-11-14 13:10:16 +01:00
*
* This file is part of DCC-EX
*
* 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/>.
*/
2021-11-22 03:24:15 +01:00
#if defined(ARDUINO_ARCH_ESP32)
2022-08-03 08:57:34 +02:00
#include "defines.h"
2021-11-14 13:10:16 +01:00
#include "DIAG.h"
#include "DCCRMT.h"
2022-10-20 00:52:19 +02:00
#include "DCCTimer.h"
2021-11-19 00:34:56 +01:00
#include "DCCWaveform.h" // for MAX_PACKET_SIZE
#include "soc/gpio_sig_map.h"
2021-11-19 00:34:56 +01:00
// Number of bits resulting out of X bytes of DCC payload data
// Each byte has one bit extra and at the end we have one EOF marker
#define DATA_LEN(X) ((X)*9+1)
2021-11-14 13:10:16 +01:00
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4,2,0)
#error wrong IDF version
#endif
void setDCCBit1(rmt_item32_t* item) {
item->level0 = 1;
item->duration0 = DCC_1_HALFPERIOD;
item->level1 = 0;
item->duration1 = DCC_1_HALFPERIOD;
}
void setDCCBit0(rmt_item32_t* item) {
item->level0 = 1;
item->duration0 = DCC_0_HALFPERIOD;
item->level1 = 0;
item->duration1 = DCC_0_HALFPERIOD;
}
2021-11-19 00:34:56 +01:00
// special long zero to trigger scope
void setDCCBit0Long(rmt_item32_t* item) {
item->level0 = 1;
item->duration0 = DCC_0_HALFPERIOD + DCC_0_HALFPERIOD/10;
item->level1 = 0;
2021-11-19 00:34:56 +01:00
item->duration1 = DCC_0_HALFPERIOD + DCC_0_HALFPERIOD/10;
}
2021-11-14 14:48:32 +01:00
void setEOT(rmt_item32_t* item) {
item->val = 0;
}
// This is an array that contains the this pointers
// to all uses channel objects. This is used to determine
// which of the channels was triggering the ISR as there
// is only ONE common ISR routine for all channels.
RMTChannel *channelHandle[8] = { 0 };
2021-11-14 13:10:16 +01:00
void IRAM_ATTR interrupt(rmt_channel_t channel, void *t) {
RMTChannel *tt = channelHandle[channel];
if (tt) tt->RMTinterrupt();
2022-10-20 00:52:19 +02:00
if (channel == 0)
DCCTimer::updateMinimumFreeMemoryISR(0);
2021-11-14 13:10:16 +01:00
}
RMTChannel::RMTChannel(pinpair pins, bool isMain) {
2022-07-30 21:11:51 +02:00
byte ch;
byte plen;
if (isMain) {
ch = 0;
plen = PREAMBLE_BITS_MAIN;
} else {
ch = 2;
plen = PREAMBLE_BITS_PROG;
}
2021-11-14 13:10:16 +01:00
// preamble
2021-11-14 14:48:32 +01:00
preambleLen = plen+2; // plen 1 bits, one 0 bit and one EOF marker
2021-11-14 13:10:16 +01:00
preamble = (rmt_item32_t*)malloc(preambleLen*sizeof(rmt_item32_t));
for (byte n=0; n<plen; n++)
setDCCBit1(preamble + n); // preamble bits
2021-11-19 00:34:56 +01:00
#ifdef SCOPE
setDCCBit0Long(preamble + plen); // start of packet 0 bit long version
#else
setDCCBit0(preamble + plen); // start of packet 0 bit normal version
#endif
setEOT(preamble + plen + 1); // EOT marker
2021-11-14 13:10:16 +01:00
// idle
2021-11-19 00:34:56 +01:00
idleLen = 28;
2021-11-14 13:10:16 +01:00
idle = (rmt_item32_t*)malloc(idleLen*sizeof(rmt_item32_t));
2021-12-04 20:07:34 +01:00
if (isMain) {
for (byte n=0; n<8; n++) // 0 to 7
setDCCBit1(idle + n);
for (byte n=8; n<18; n++) // 8, 9 to 16, 17
setDCCBit0(idle + n);
for (byte n=18; n<26; n++) // 18 to 25
setDCCBit1(idle + n);
} else {
for (byte n=0; n<26; n++) // all zero
setDCCBit0(idle + n);
}
2021-11-14 14:48:32 +01:00
setDCCBit1(idle + 26); // end bit
2021-11-19 00:34:56 +01:00
setEOT(idle + 27); // EOT marker
2021-11-14 13:10:16 +01:00
2021-11-15 22:28:30 +01:00
// data: max packet size today is 5 + checksum
maxDataLen = DATA_LEN(MAX_PACKET_SIZE+1); // plus checksum
2021-11-15 23:10:23 +01:00
data = (rmt_item32_t*)malloc(maxDataLen*sizeof(rmt_item32_t));
2021-11-15 22:28:30 +01:00
2021-11-14 13:10:16 +01:00
rmt_config_t config;
// Configure the RMT channel for TX
bzero(&config, sizeof(rmt_config_t));
config.rmt_mode = RMT_MODE_TX;
config.channel = channel = (rmt_channel_t)ch;
2021-11-19 00:03:21 +01:00
config.clk_div = RMT_CLOCK_DIVIDER;
config.gpio_num = (gpio_num_t)pins.pin;
2021-11-15 22:28:30 +01:00
config.mem_block_num = 2; // With longest DCC packet 11 inc checksum (future expansion)
// number of bits needed is 22preamble + start +
// 11*9 + extrazero + EOT = 124
// 2 mem block of 64 RMT items should be enough
2021-11-14 13:10:16 +01:00
ESP_ERROR_CHECK(rmt_config(&config));
addPin(pins.invpin, true);
/*
// test: config another gpio pin
gpio_num_t gpioNum = (gpio_num_t)(pin-1);
PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[gpioNum], PIN_FUNC_GPIO);
gpio_set_direction(gpioNum, GPIO_MODE_OUTPUT);
gpio_matrix_out(gpioNum, RMT_SIG_OUT0_IDX, 0, 0);
*/
2021-11-14 13:10:16 +01:00
// NOTE: ESP_INTR_FLAG_IRAM is *NOT* included in this bitmask
ESP_ERROR_CHECK(rmt_driver_install(config.channel, 0, ESP_INTR_FLAG_LOWMED|ESP_INTR_FLAG_SHARED));
2022-08-13 18:38:51 +02:00
// DIAG(F("Register interrupt on core %d"), xPortGetCoreID());
2021-11-14 13:10:16 +01:00
2021-11-18 23:57:53 +01:00
ESP_ERROR_CHECK(rmt_set_tx_loop_mode(channel, true));
channelHandle[channel] = this; // used by interrupt
rmt_register_tx_end_callback(interrupt, 0);
2021-11-14 13:10:16 +01:00
rmt_set_tx_intr_en(channel, true);
2022-08-13 18:38:51 +02:00
DIAG(F("Channel %d DCC signal for %s start"), config.channel, isMain ? "MAIN" : "PROG");
2021-11-14 13:10:16 +01:00
// send one bit to kickstart the signal, remaining data will come from the
// packet queue. We intentionally do not wait for the RMT TX complete here.
2021-11-15 22:28:30 +01:00
//rmt_write_items(channel, preamble, preambleLen, false);
RMTprefill();
dataReady = false;
}
2021-11-22 03:55:00 +01:00
void RMTChannel::RMTprefill() {
2021-11-15 22:28:30 +01:00
rmt_fill_tx_items(channel, preamble, preambleLen, 0);
rmt_fill_tx_items(channel, idle, idleLen, preambleLen-1);
2021-11-14 13:10:16 +01:00
}
2021-11-15 22:28:30 +01:00
const byte transmitMask[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
int RMTChannel::RMTfillData(const byte buffer[], byte byteCount, byte repeatCount=0) {
//int RMTChannel::RMTfillData(dccPacket packet) {
// dataReady: Signals to then interrupt routine. It is set when
// we have data in the channel buffer which can be copied out
// to the HW. dataRepeat on the other hand signals back to
// the caller of this function if the data has been sent enough
// times (0 to 3 means 1 to 4 times in total).
2022-07-30 21:11:51 +02:00
if (dataRepeat > 0) // we have still old work to do
return dataRepeat;
if (dataReady == true) // the packet is not copied out yet
return 1000;
if (DATA_LEN(byteCount) > maxDataLen) { // this would overun our allocated memory for data
DIAG(F("Can not convert DCC bytes # %d to DCC bits %d, buffer too small"), byteCount, maxDataLen);
2022-07-30 21:11:51 +02:00
return -1; // something very broken, can not convert packet
}
2021-11-15 23:10:23 +01:00
// convert bytes to RMT stream of "bits"
2021-11-15 22:28:30 +01:00
byte bitcounter = 0;
for(byte n=0; n<byteCount; n++) {
2021-11-15 22:28:30 +01:00
for(byte bit=0; bit<8; bit++) {
if (buffer[n] & transmitMask[bit])
setDCCBit1(data + bitcounter++);
else
setDCCBit0(data + bitcounter++);
2021-11-14 13:10:16 +01:00
}
2021-11-15 22:28:30 +01:00
setDCCBit0(data + bitcounter++); // zero at end of each byte
2021-11-14 13:10:16 +01:00
}
2021-11-15 22:28:30 +01:00
setDCCBit1(data + bitcounter-1); // overwrite previous zero bit with one bit
setEOT(data + bitcounter++); // EOT marker
dataLen = bitcounter;
dataReady = true;
dataRepeat = repeatCount+1; // repeatCount of 0 means send once
2022-07-30 21:11:51 +02:00
return 0;
2021-11-15 22:28:30 +01:00
}
2021-11-22 03:55:00 +01:00
void IRAM_ATTR RMTChannel::RMTinterrupt() {
2021-11-18 23:57:53 +01:00
//no rmt_tx_start(channel,true) as we run in loop mode
//preamble is always loaded at beginning of buffer
2022-08-02 12:42:20 +02:00
packetCounter++;
if (!dataReady && dataRepeat == 0) { // we did run empty
rmt_fill_tx_items(channel, idle, idleLen, preambleLen-1);
return; // nothing to do about that
}
// take care of incoming data
2021-11-15 23:10:23 +01:00
if (dataReady) { // if we have new data, fill while preamble is running
2021-11-15 22:28:30 +01:00
rmt_fill_tx_items(channel, data, dataLen, preambleLen-1);
dataReady = false;
}
2021-11-15 23:10:23 +01:00
if (dataRepeat > 0) // if a repeat count was specified, work on that
2021-11-15 22:28:30 +01:00
dataRepeat--;
2021-11-14 13:10:16 +01:00
}
2022-08-04 01:19:11 +02:00
bool RMTChannel::addPin(byte pin, bool inverted) {
if (pin == UNUSED_PIN)
return true;
2022-08-04 01:19:11 +02:00
gpio_num_t gpioNum = (gpio_num_t)(pin);
esp_err_t err;
PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[gpioNum], PIN_FUNC_GPIO);
err = gpio_set_direction(gpioNum, GPIO_MODE_OUTPUT);
if (err != ESP_OK) return false;
gpio_matrix_out(gpioNum, RMT_SIG_OUT0_IDX+channel, inverted, 0);
2022-08-04 01:19:11 +02:00
if (err != ESP_OK) return false;
return true;
}
bool RMTChannel::addPin(pinpair pins) {
return addPin(pins.pin) && addPin(pins.invpin, true);
}
2021-11-22 03:24:15 +01:00
#endif //ESP32