FlexIO library and Teensy 4.1

Lude

Member
Hi,

we're moving from Teensy 3.5 to Teensy 4.1 on a custom pcb.
On 3.5 we use Serial2 (pin 9 and 10) to talk to modem but is on different pins on 4.1. (pin 7 and 8)
To prevent us to modify pcb we need to use FlexIo library but I can't make it work, modem do not respond.

The sketch I use is very simple:

Code:
#include <FlexIO_t4.h>
#include <FlexSerial.h>

#define MODEM_RX_PIN	9
#define MODEM_TX_PIN	10
#define MODEM_POWER_PIN	12
#define MODEM_RESET_PIN	11
#define MODEM_BAUD		115200

FlexSerial modem(MODEM_RX_PIN, MODEM_TX_PIN);

void setup()
{
	pinMode(MODEM_POWER_PIN, OUTPUT);
	pinMode(MODEM_RESET_PIN, OUTPUT);

	digitalWrite(MODEM_POWER_PIN, LOW);
	digitalWrite(MODEM_RESET_PIN, LOW);
	
	// waits for Arduino Serial Monitor
	while ( ! Serial ) { }
	
	Serial.print("turn on modem...please wait...");
	digitalWrite(MODEM_POWER_PIN, HIGH);
	delay(5000);	
	digitalWrite(MODEM_POWER_PIN, LOW);
	delay(7000);
	Serial.println("ok!");
	
	modem.begin(MODEM_BAUD);
}

void loop()
{
	int ch;
	
  	if ( Serial.available() )
	{
		while ( (ch = Serial.read()) != -1 )
			modem.write(ch);
	}

	if ( modem.available() )
	{
		while ( (ch = modem.read()) != -1 )
			Serial.write(ch);
	}
}

Any hint ?

Thank you,
Lude
 
Lude:

I can't help you with any suggestions on using the FlexIO on the T4, but how about just configuring pins 9 & 10 as inputs, then add a blue wire between the old pins & the new pins (7-to-9 & 8-to-10)...is that a possible fix ??

Mark J Culross
KD5RXT
 
@lude - I see at least one bug in the code, which I will fix. Better yet if someone tests it.

Not sure if it is what you are running into or not:

Code:
bool FlexSerial::call_back (FlexIOHandler *pflex) {
	DEBUG_digitalWriteFast(DEBUG_PIN_CALLBACK, HIGH);
	// check for RX
	IMXRT_FLEXIO_t *p = &pflex->port();
	if (pflex == _rx_pflex) {
		if (_rx_pflex->port().SHIFTSTAT & _rx_shifter_mask) {
			DEBUG_digitalWriteFast(DEBUG_PIN_CALLBACK_READ, HIGH);
			uint8_t c = _rx_pflex->port().SHIFTBUFBYS[_rx_shifter] & 0xff;
			uint32_t head;
			head = _rx_buffer_head;
			if (++head >= RX_BUFFER_SIZE) head = 0;
			// don't save char if buffer is full...
			if [COLOR="#FF0000"](_tx_buffer_tail [/COLOR]!= head) {
				_rx_buffer[_rx_buffer_head] = c;
				_rx_buffer_head = head;
			}
			DEBUG_digitalWriteFast(DEBUG_PIN_CALLBACK_READ, LOW);
		}
	}
(line 395 in FlexSerial.cpp at least in my case). That _tx_buffer_tail should be _rx_buffer_tail

Which was a copy/paste/edit (and missed on edit) bug. I will check in a few more places.

But you might see if that helps
 
I just pushed up changes to my github project: https://github.com/KurtE/FlexIO_t4

Again would be good to know it helps or not.

Hopefully will be pulled into next Teensyduino beta/release

I tried with simple test of Echo Serial to Serial1 to FlexSerial on those pins, back to serial1 back to Serial
With jumpers from pins 1->9 and 10->0
Code:
#include <FlexIO_t4.h>
#include <FlexSerial.h>

#define MODEM_RX_PIN 9
#define MODEM_TX_PIN 10
#define MODEM_BAUD 115200

FlexSerial modem(MODEM_RX_PIN, MODEM_TX_PIN);

void setup() {

  // waits for Arduino Serial Monitor
  while (!Serial) {}

  Serial1.begin(MODEM_BAUD);
  modem.begin(MODEM_BAUD);
}

void loop() {
  int ch;

  if (Serial.available()) {
    while ((ch = Serial.read()) != -1)
      Serial1.write(ch);
  }

  if (Serial1.available()) {
    while ((ch = Serial1.read()) != -1)
      Serial.write(ch);
  }

  if (modem.available()) {
    for (;;) {
      int ch_peek = modem.peek();
      ch = modem.read();
      if ((ch != ch_peek) && (ch_peek != -1)) {
        Serial.printf("FlexIO Read(%x) != peek(%x)\n", ch, ch_peek);
      }
      if (ch == -1) break;
      modem.write(ch);
    }
  }
}

I also tested that peek value equaled read value. note special case where peek == -1 and read does not as that can happen where the read comes through after the peek...

Hopefully give these updates a try and see if fixes the issue.
 
Lude:

I can't help you with any suggestions on using the FlexIO on the T4, but how about just configuring pins 9 & 10 as inputs, then add a blue wire between the old pins & the new pins (7-to-9 & 8-to-10)...is that a possible fix ??

Mark J Culross
KD5RXT

it is certainly the most reasonable solution!
in this way we'll use an hardware serial instead of a software one!

Thank you.
 
@lude - I see at least one bug in the code, which I will fix. Better yet if someone tests it.

Not sure if it is what you are running into or not:

Code:
bool FlexSerial::call_back (FlexIOHandler *pflex) {
	DEBUG_digitalWriteFast(DEBUG_PIN_CALLBACK, HIGH);
	// check for RX
	IMXRT_FLEXIO_t *p = &pflex->port();
	if (pflex == _rx_pflex) {
		if (_rx_pflex->port().SHIFTSTAT & _rx_shifter_mask) {
			DEBUG_digitalWriteFast(DEBUG_PIN_CALLBACK_READ, HIGH);
			uint8_t c = _rx_pflex->port().SHIFTBUFBYS[_rx_shifter] & 0xff;
			uint32_t head;
			head = _rx_buffer_head;
			if (++head >= RX_BUFFER_SIZE) head = 0;
			// don't save char if buffer is full...
			if [COLOR="#FF0000"](_tx_buffer_tail [/COLOR]!= head) {
				_rx_buffer[_rx_buffer_head] = c;
				_rx_buffer_head = head;
			}
			DEBUG_digitalWriteFast(DEBUG_PIN_CALLBACK_READ, LOW);
		}
	}
(line 395 in FlexSerial.cpp at least in my case). That _tx_buffer_tail should be _rx_buffer_tail

Which was a copy/paste/edit (and missed on edit) bug. I will check in a few more places.

But you might see if that helps

It works!
Thank you!
 
I just pushed up changes to my github project: https://github.com/KurtE/FlexIO_t4

Again would be good to know it helps or not.

Hopefully will be pulled into next Teensyduino beta/release

I tried with simple test of Echo Serial to Serial1 to FlexSerial on those pins, back to serial1 back to Serial
With jumpers from pins 1->9 and 10->0
Code:
#include <FlexIO_t4.h>
#include <FlexSerial.h>

#define MODEM_RX_PIN 9
#define MODEM_TX_PIN 10
#define MODEM_BAUD 115200

FlexSerial modem(MODEM_RX_PIN, MODEM_TX_PIN);

void setup() {

  // waits for Arduino Serial Monitor
  while (!Serial) {}

  Serial1.begin(MODEM_BAUD);
  modem.begin(MODEM_BAUD);
}

void loop() {
  int ch;

  if (Serial.available()) {
    while ((ch = Serial.read()) != -1)
      Serial1.write(ch);
  }

  if (Serial1.available()) {
    while ((ch = Serial1.read()) != -1)
      Serial.write(ch);
  }

  if (modem.available()) {
    for (;;) {
      int ch_peek = modem.peek();
      ch = modem.read();
      if ((ch != ch_peek) && (ch_peek != -1)) {
        Serial.printf("FlexIO Read(%x) != peek(%x)\n", ch, ch_peek);
      }
      if (ch == -1) break;
      modem.write(ch);
    }
  }
}

I also tested that peek value equaled read value. note special case where peek == -1 and read does not as that can happen where the read comes through after the peek...

Hopefully give these updates a try and see if fixes the issue.

Thanky you!
 
Hi,

I've another question regarding FlexIO library.

I was using following pseudo code to read from hw serial:

Code:
while ( modem->available() > 0 )
	buf[index++] = modem->read();

Now, using same code I get some -1 first then the data expected.
The example actually does this check but I cannot understand why.
It is the way the library works ? And if I expect binary data from serial and 0xFF (-1) is part of it ?
How can I handle this ?

thanks.
 
Back
Top