Forum Rule: Always post complete source code & details to reproduce any issue!
Page 1 of 2 1 2 LastLast
Results 1 to 25 of 28

Thread: Serial2 Alternate pins 26 and 31

  1. #1
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,685

    Serial2 Alternate pins 26 and 31

    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::

    Quote Originally Posted by PaulStoffregen View Post
    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 | PORT_PCR_MUX(3);
      CORE_PIN31_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);

  2. #2
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    5,679
    Looks good

  3. #3
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,685
    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)?

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,554
    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.

  5. #5
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,685
    Quote Originally Posted by PaulStoffregen View Post
    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 . . .

  6. #6
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,685
    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 by defragster; 03-24-2016 at 11:18 PM. Reason: #define ESP8266_SERIAL Serial2

  7. #7
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,685
    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 by defragster; 03-24-2016 at 11:16 PM. Reason: updated with link to the connector board

  8. #8
    Junior Member
    Join Date
    Jul 2015
    Posts
    18
    Hi!!!! One Question!!!


    I can use both Serial2 ??

    Pin 9 and Pin 10 and also
    Pin 26 and Pin 31

  9. #9
    Senior Member
    Join Date
    Jul 2014
    Posts
    2,330
    Quote Originally Posted by fersical View Post
    Hi!!!! One Question!!!


    I can use both Serial2 ??

    Pin 9 and Pin 10 and also
    Pin 26 and Pin 31
    one or the other

  10. #10
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,685
    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);

  11. #11
    Senior Member
    Join Date
    Nov 2012
    Location
    Salt Lake City, UT, USA
    Posts
    276
    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 by bboyes; 07-06-2016 at 02:53 AM.

  12. #12
    Senior Member
    Join Date
    Nov 2012
    Location
    Salt Lake City, UT, USA
    Posts
    276

    Where can I get Serial[12].setRX, etc?

    Quote Originally Posted by defragster View Post
    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...

  13. #13
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,416
    You might try doing the sets before the begin and see if that works,

  14. #14
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,685
    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 by defragster; 07-11-2016 at 04:58 AM.

  15. #15
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,685
    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.

  16. #16
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,685
    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.

  17. #17
    Senior Member
    Join Date
    Nov 2012
    Location
    Salt Lake City, UT, USA
    Posts
    276

    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());

  18. #18
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,416
    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

  19. #19
    Senior Member
    Join Date
    Nov 2012
    Location
    Salt Lake City, UT, USA
    Posts
    276

    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 by bboyes; 07-12-2016 at 08:27 PM.

  20. #20
    Senior Member
    Join Date
    Nov 2012
    Location
    Salt Lake City, UT, USA
    Posts
    276
    Quote Originally Posted by defragster View Post
    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()

  21. #21
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,416
    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.

  22. #22
    Senior Member
    Join Date
    Nov 2012
    Location
    Salt Lake City, UT, USA
    Posts
    276

    Serial2.setRX, TX work with KurtE patch in TD 1.29

    Quote Originally Posted by KurtE View Post
    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?

    Bruce

  23. #23
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,416
    Yep, I issues a Pull request and Paul pulled it in within a few minutes, so yes will be in next release.

  24. #24
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,685
    Bummer - I never tested that - I set my ALT Serial2 ESP8266 aside the other week . . . when it was working with the manual setting

  25. #25
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •