Teensy 4.1 freezes with below code after 10 minutes or so.... Can you please help to find where the issue could be or where should I start looking
In below code, my EthernetServer waits for a client to be connected (client used is Windows TCP Client).
Once client is connected we send so dummy data every 20 milliseconds.
I have a low priority timer interrupt running every 50 milliseconds which trys to grab the async buffer using m_bufferMutex.try_lock() and if succedds then it puts dummy WRITE_SIZE much data to async buffer.
In below code, my EthernetServer waits for a client to be connected (client used is Windows TCP Client).
Once client is connected we send so dummy data every 20 milliseconds.
I have a low priority timer interrupt running every 50 milliseconds which trys to grab the async buffer using m_bufferMutex.try_lock() and if succedds then it puts dummy WRITE_SIZE much data to async buffer.
Code:
EthernetServer server(tcp_port); //global
EthernetClient m_client; //global
void TcpServerComms::init() // called in setup
{
server.begin();
}
void TcpServerComms::task() //Called in loop()
{
//Check if we have a client connected
if (!m_client)
{
m_client = server.accept();
if (!m_client) { return; }
m_client.println("Hello");
Serial7.printf("TCP Client connected \n");
}
//We check for data every 20 ms
auto currentMs = millis();
if ((currentMs - m_timeScheduledMs) < 20) { return; }
m_timeScheduledMs = currentMs;
if (m_client)
{
//CHECK IF WE HAVE ANY ASYNC DATA TO SEND
auto dataPresent = m_asyncDataBuff.size();
auto dataToRead = dataPresent > WRITE_SIZE ? WRITE_SIZE : dataPresent;
uint32_t clientSndBufSize = m_client.availableForWrite();
//Check how much data we can write
dataToRead = dataToRead > clientSndBufSize ? clientSndBufSize : dataToRead;
if (dataToRead > 0)
{
if (m_bufferMutex.try_lock() != 0)
{
uint32_t snappy = micros();
auto readData = m_asyncDataBuff.readBytes(m_tempBuffer, dataToRead);
m_bufferMutex.unlock();
m_client.write(m_tempBuffer, readData);
m_client.flush();
snappy = micros() - snappy;
Serial7.printf("TCP Serial data sent time = %d us\n", snappy);
}
}
}
}