Skip to content

Commit

Permalink
XpressNet: next node normal inquiry is now sent after 120us timeout
Browse files Browse the repository at this point in the history
device must respond within 110us according to standard, command station must wait at least 120us
  • Loading branch information
reinder committed Aug 20, 2024
1 parent bf99721 commit d848593
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ int main()
{
TraintasticCS::process();
XpressNet::process();
sleep_ms(5);
sleep_us(100); // FIXME: if lower, xpressnet rx fifo contains garbage
}
}

32 changes: 32 additions & 0 deletions src/utils/time.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* This file is part of the Traintastic CS RP2040 firmware,
* see <https://github.com/traintastic/traintastic-cs-rp2040>.
*
* Copyright (C) 2024 Reinder Feenstra
*
* This program 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 2
* of the License, or (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#ifndef UTILS_TIME_HPP
#define UTILS_TIME_HPP

#include <pico/types.h>

inline bool operator>=(const absolute_time_t lhs, const absolute_time_t rhs)
{
return to_us_since_boot(lhs) >= to_us_since_boot(rhs);
}

#endif
20 changes: 14 additions & 6 deletions src/xpressnet/xpressnet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
#include "../traintasticcs/traintasticcs.hpp"
#include "../utils/bit.hpp"
#include "../utils/endian.hpp"
#include "../utils/time.hpp"

namespace XpressNet {

static bool g_enabled = false;
static uint8_t g_address;
static uint8_t g_rxBuffer[32];
static uint8_t g_rxBufferCount;
static absolute_time_t g_nextNormalInquiry;

static void received();

Expand All @@ -59,6 +61,7 @@ void enable()
{
g_address = 0;
g_rxBufferCount = 0;
g_nextNormalInquiry = make_timeout_time_ms(1000);

pio_sm_set_enabled(XPRESSNET_PIO, XPRESSNET_SM_RX, false);
pio_sm_set_enabled(XPRESSNET_PIO, XPRESSNET_SM_TX, false);
Expand Down Expand Up @@ -158,6 +161,8 @@ void process()

while(!pio_sm_is_rx_fifo_empty(XPRESSNET_PIO, XPRESSNET_SM_RX))
{
g_nextNormalInquiry = at_the_end_of_time;

uint16_t value = pio_sm_get(XPRESSNET_PIO, XPRESSNET_SM_RX) >> (32 - 9);
if((value & 0x100) == 0) // data byte
{
Expand All @@ -180,22 +185,25 @@ void process()
if(checksum == g_rxBuffer[length - 1])
{
received();
g_rxBufferCount -= length;
}
else
{
g_rxBufferCount = 0; // reset buffer, checksum invalid
}
g_rxBufferCount = 0;
}

if(g_rxBufferCount == 0)
{
g_nextNormalInquiry = make_timeout_time_us(25);
}
}

if(pio_sm_is_tx_fifo_empty(XPRESSNET_PIO, XPRESSNET_SM_TX))
if(pio_sm_is_tx_fifo_empty(XPRESSNET_PIO, XPRESSNET_SM_TX) &&
get_absolute_time() >= g_nextNormalInquiry)
{
if(++g_address > 31)
{
g_address = 1;
}
sendNormalInquiry(g_address);
g_nextNormalInquiry = make_timeout_time_us(120);
}
}

Expand Down

0 comments on commit d848593

Please sign in to comment.