Serial2 Alternate pins 26 and 31

Status
Not open for further replies.

defragster

Senior Member+
Teensy 3.2 card back shows Pins 26 as RX2 and 31 as TX2 for (gray) alternate pins.

I don't see a begin() or other functional option to specify these alternate pins at runtime to change from pin 9 as RX2 and pin 10 as TX2.

Did I miss a function to do this - or a compile time flag?


A BING search found this post, is this the right answer ( as edited for Serial2 ) ?

Except I'm unsure about PORT_PCR_MUX(3) covering those pins?

do Serial2.begin(9600): then::

After Serial1.begin(), pins 0 and 1 will be in use.


After Serial2.begin(), pins 9 and 10 will be in use.

This should disable those pins:

Code:
  CORE_PIN9_CONFIG = 0;
  CORE_PIN10_CONFIG = 0;

Then to activate the alternate pins, try this:

Code:
  CORE_PIN26_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | [B]PORT_PCR_MUX(3)[/B];
  CORE_PIN31_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | [B]PORT_PCR_MUX(3)[/B];
 
Thanks!

I understand that pins are arranged in PORT groups - Is the MUX(3) associated with it's function as a serial port or the pin? I've never read anything but passing comments about how the MUX allows pin function to change. Is this a rtFm issue (Freescale manual)?
 
Every pin has a 8-way mux. 0 is always analog or disable, 1 is always the digital GPIO, and 2 to 7 are various peripherals. There's a huge table in chapter 10 that shows what's connected to all the mux inputs on every pin. To make sense of stuff like PORT_PCR_MUX(3), you need to look at that table. Looking at the schematic helps too, so you can figure out which Freescale port name (eg, "PTC7") corresponds to the digital pin number we use in Arduino.
 
Every pin has a 8-way mux. 0 is always analog or disable, 1 is always the digital GPIO, and 2 to 7 are various peripherals. There's a huge table in chapter 10 that shows what's connected to all the mux inputs on every pin. To make sense of stuff like PORT_PCR_MUX(3), you need to look at that table. Looking at the schematic helps too, so you can figure out which Freescale port name (eg, "PTC7") corresponds to the digital pin number we use in Arduino.

Thanks Paul - Most helpful - great summary explains well - and starting pointer ... PDF search on "MUX" started me on DMA at chapter 20 . . .
 
Indeed - with this run time code - I see ALTERNATE SERIAL2 pins working - from FrankB's Connector Board on T_3.2 -wired from pin mounted onehorse ESP8266 unit!

Code:
#define ESP8266_SERIAL Serial2

    ESP8266_SERIAL.begin( 115200 );
    CORE_PIN9_CONFIG = 0;
    CORE_PIN10_CONFIG = 0;
    CORE_PIN26_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3);
    CORE_PIN31_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
    Serial.println("ALTERNATE Serial2 @ 26/31");


void serialEvent2() {
  while (ESP8266_SERIAL.available()) {
    Serial.write( ESP8266_SERIAL.read() );
  }
}

void serialEvent() {
  while (Serial.available()) {
    ESP8266_SERIAL.write(Serial.read());
  }

With a hardwired Teensy for each - I'm going to put a FLAG in EPROM() for runtime initialization of the correct 'serial#' port in setup() and also 'follow' the pin mapping from Teensy to ESP as I'll have 4 unique units to start. Three of them onehorse units that sit GPIO on different pins and will use Serial1 and #2 and #2_Alt - then a single Teensy Sketch can act accordingly.
 
Last edited:
BTW - this has been working reliably. Haven't pushed the baud rate - but no issues using the Serial2 alternate pins pulled from the FrankB ConnectorBoard.

I can confirm resetting the baud with .begin loses the connection - I didn't do the code, but assume I just need to redo the four lines above to re-move the CORE_PIN### functionality.

<edit>: updated with link to the connector board to order from OSH: https://oshpark.com/shared_projects/0T6ZdhhG
 
Last edited:
Support functions for moving to alternate pins was added:
virtual void setRX(uint8_t pin)
virtual void setTX(uint8_t pin, bool opendrain=false)

Where the proper Alternate pins for Serial# will be set like:
Code:
Serial1.begin (57600, SERIAL_8N1);
Serial1.setRX (3);
Serial1.setTX (4);

Serial2.begin (57600, SERIAL_8N1);
Serial2.setRX (26);
Serial2.setTX (31);
 
Where/when has this been added?
Serial2.setRX (26);
Serial2.setTX (31);
doesn't throw a compiler error but it also doesn't work. The CORE_PIN config does: thanks for posting that. I'm using Arduino 1.6.8 and TD 1.28

Is there a place where these alternate functions, and details like serial FIFO or ring buffer size, behaviour, exception handling, etc are documented?

Thanks
Bruce
 
Last edited:
Where can I get Serial[12].setRX, etc?

Support functions for moving to alternate pins was added:
Added where? To what? Do you mean a pull request in Github? If you can point to a repo where I could grab that, it would be great. Thanks...
 
I just put on the release TeensyDuino 1.29 and the code is there setRX() maps to [ teensy3/Serial1.c :: void serial_set_rx(uint8_t pin) ]

This has to be done after begin() - begin will force/restore the default pins - then they can be changed. The pins used have to be those accepted as valid ALT pins for the device in use, and there.

I have not tried this - if you can post a simple sketch that doesn't work I'll see if I can try it.

<edit> I made a sample for K66 testing and it works to one ALT Serial1 but not the other on that board . . . more when I can post code
 
Last edited:
Posted sample for K66 on that thread - You can go there and see my code. It works - after Serial1.begin() it rotates the Serial1 pins through the three sets available on that device on each completed incoming USB string and echoes it out Serial1 that is looped back and echoed on USB Serial. I didn't test on the T_3.1/2 but I can if it doesn't help fix your issue.
 
WORKS :: I just confirmed on my T_3.1 that changing that sketch ~line 33 "if ( ++UASer > 2 ) UASer = 0;" to " if ( ++UASer > 1 ) UASer = 0;" will jump between the Primary and Alternate Serial 1 pins on each USB message.

Jumper 0&1 and 21&5 - it works - pull a jumper and it picks up when the jumper is restored to that pair.
 
Serial2.set doesn't appear to work in TD 1.28

On Teensy 3.2 with TD 1.28, this code does not switch Serial2 to alternate pins. No compiler (1.6.8) errors.
Code:
	Serial2.begin(9600);			// UI Side 0 = LCD and keypad
	while((!Serial2) && (millis()<10000));		// wait until serial port is open or timeout
	Serial2.setRX (26);
	Serial2.setTX (31);	
	Serial.println("ALTERNATE Serial2 @ 26/31");
	Serial.print("Serial2 ready at ");
	Serial.println(millis());

This works, even if I put it *before* the Serial2.begin():
Code:
	Serial2.begin(9600);			// UI Side 0 = LCD and keypad
	while((!Serial2) && (millis()<10000));		// wait until serial port is open or timeout
	// Serial2.setRX (26);
	// Serial2.setTX (31);	
	// Serial.println("ALTERNATE Serial2 @ 26/31");
	// Code to enable alternate serial pins on Serial 2
	CORE_PIN9_CONFIG = 0;
	CORE_PIN10_CONFIG = 0;
	CORE_PIN26_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3);
	CORE_PIN31_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
	Serial.println("ALTERNATE Serial2 @ 26/31");
	Serial.print("Serial2 ready at ");
	Serial.println(millis());
 
Two suggestions:
Try:
Code:
	Serial2.setRX (26);
	Serial2.setTX (31);	
	Serial2.begin(9600);			// UI Side 0 = LCD and keypad

Also try with TD 1.29
 
setRX, TX works but only before begin()

Weird I just tried Arduino 1.6.9 and TD 1.29. same code as above. No better. There must be something odd about my setup but I can't imagine what. The TX out (P31) is just always low when using Serial2.setTX(31).

@KurtE It works if begin() is moved to after the setRX setTX calls. This is opposite of @defragster instructions.

Works:
Code:
	Serial2.setRX (26);
	Serial2.setTX (31);	
	Serial2.begin(9600);			// UI Side 0 = LCD and keypad
	while((!Serial2) && (millis()<10000));		// wait until serial port is open or timeout
	Serial.println("ALTERNATE Serial2 @ 26/31");
 
Last edited:
This has to be done after begin() - begin will force/restore the default pins - then they can be changed. The pins used have to be those accepted as valid ALT pins for the device in use, and there.

See my post from today. It works on T3.2 (with TD1.29) but only if the setRX setTX are *before* begin()
 
Yep - There are two different code paths here and there is a bug in Serial2. Could you do a test for me to see if it works.

Can you edit the file ...\cores\teensy3\Serial2.c

Look for the function: serial2_set_tx (line 205)
Now look for the line(212): if ((SIM_SCGC4 & SIM_SCGC4_UART2)) {
Change UART2 to UART1.

Likewise in the set_rx function line 239.

Save the changes and try the code with the sets after the begin.
 
Serial2.setRX, TX work with KurtE patch in TD 1.29

Yep - There are two different code paths here and there is a bug in Serial2. Could you do a test for me to see if it works..
OK now those lines are
Code:
if ((SIM_SCGC4 & SIM_SCGC4_UART1)) {
and my code is
Code:
	Serial2.begin(9600);			// UI Side 0 = LCD and keypad
	Serial2.setRX (26);
	Serial2.setTX (31);

And that works... Hmm. Thanks for the patch. Presumably this will be in TD 1.30? So how was it "working" before if ahead of the begin() and not getting overwritten by begin()? How were others reporting it worked in 1.28 and 1.29 without this patch? :confused:

Bruce
 
Yep, I issues a Pull request and Paul pulled it in within a few minutes, so yes will be in next release.
 
Bummer - I never tested that - I set my ALT Serial2 ESP8266 aside the other week . . . when it was working with the manual setting
 
So I was able to reassign the Serial2 RX and TX to pins 26 & 31. Works great; thanks. However, I did this for the primary reason of restoring the SD card function of the Audio adapter (uses pins 9 and 10, I believe). I am also including the SD.h library.

I don't mean to ask for hand-holding, but I can't seem to reason out the places and sequences to do this and also preserve SD card function on the Audio adapter. I saw on another thread this comment from Paul:

You'll probably also need to add this after Serial2.begin(), to restore pin 9 to I2S usage:


CORE_PIN9_CONFIG = PORT_PCR_MUX(6); // pin 9, PTC3, I2S0_TX_BCLK

Can somebody please give me some guidance to get compatibility between these two function? Thanks.
 
Status
Not open for further replies.
Back
Top