Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 13 of 13

Thread: How to find out which Serial is used

  1. #1
    Senior Member
    Join Date
    Apr 2013
    Posts
    127

    How to find out which Serial is used

    Hi,

    I'm just writing a library with use of stream to use several Serials for Teensy 3.x

    .cpp
    Code:
    XXX::XXX(HardwareSerial* stream, uint16_t baudrate) {
      (*stream).begin(baudrate);
      this->_stream = stream;
    .h
    Code:
    private:
      Stream* _stream;
    My problem is that I need to know which Serial is used to set specific UART register how do I find out the used Serial?

    Thanks
    Markus

  2. #2
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    4,896
    I have done sort of a Kludge in the past...
    Something like:
    Code:
        if (pserial == &Serial1) s_pkuart = &KINETISK_UART0;
        else if (pserial == &Serial2) s_pkuart = &KINETISK_UART1;
        else if (pserial == &Serial3) s_pkuart = &KINETISK_UART2;
    #if defined(__MK64FX512__) || defined(__MK66FX1M0__) 
        else if (pserial == &Serial4) s_pkuart = &KINETISK_UART3;
        else if (pserial == &Serial5) s_pkuart = &KINETISK_UART4;
    #endif
    Note: for T3.5/6 did not put in for Serial6... More complicated on Serial6 if T3.6 as complete different subsystem and registers...

    Edit: Should mention once I have s_pkuart set, I then do things like: s_pkuart->C1 |= UART_C1_LOOPS | UART_C1_RSRC;

  3. #3
    Senior Member
    Join Date
    Apr 2013
    Posts
    127
    Quote Originally Posted by KurtE View Post
    I have done sort of a Kludge in the past...
    Something like:
    Code:
        if (pserial == &Serial1) s_pkuart = &KINETISK_UART0;
        else if (pserial == &Serial2) s_pkuart = &KINETISK_UART1;
        else if (pserial == &Serial3) s_pkuart = &KINETISK_UART2;
    #if defined(__MK64FX512__) || defined(__MK66FX1M0__) 
        else if (pserial == &Serial4) s_pkuart = &KINETISK_UART3;
        else if (pserial == &Serial5) s_pkuart = &KINETISK_UART4;
    #endif
    Note: for T3.5/6 did not put in for Serial6... More complicated on Serial6 if T3.6 as complete different subsystem and registers...

    Edit: Should mention once I have s_pkuart set, I then do things like: s_pkuart->C1 |= UART_C1_LOOPS | UART_C1_RSRC;
    Thanks KurtE that does the trick.

  4. #4
    Senior Member
    Join Date
    Apr 2013
    Posts
    127
    Quote Originally Posted by KurtE View Post
    Note: for T3.5/6 did not put in for Serial6... More complicated on Serial6 if T3.6 as complete different subsystem and registers...
    Good to know I take a closer look at it.
    ->
    If I look right at the Data Sheets from the T 3.5 all 6 UARTs are the same on the other hand the T 3.6 the first 5 UARTs identical, UART 6 is LowPower, or did I miss something?

  5. #5
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    4,896
    Nope, you did not miss something yes on T3.5 all of them are the same and T3.6 the 6th is the LPUART

  6. #6
    Senior Member
    Join Date
    Apr 2013
    Posts
    127
    Quote Originally Posted by KurtE View Post
    Nope, you did not miss something yes on T3.5 all of them are the same and T3.6 the 6th is the LPUART
    What a messy thing, it Looks so nice just with Support for T3.0 - T3.2 even the T LC can be dropped in, seperated but it fits somehow, but adding the nearly identical T3.5 and T3.6 brings chaos, damn LPUART

    Thanks KurtE for your help!

  7. #7
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    4,896
    Again not sure what it is you are needing to do to set the registers.... In my earlier case it was for the above, it was to setup to use half duplex to support Robotis Dynamixel servos. At the time I punted on T3.5/6 to support Serial6 (although will probably for the heck of it add it for 3.5) as it just adds another set of #if defined(__MK64FX512__)

    For me in most of these cases I did not care, as on most Teensy boards, I wanted to use Serial1/2 as they have the larger fifo queues... I should probably take a look again at Serial6 on T6, as I don't remember how well the performance compared.

    But just a heads up: Soon the Teensy 4 board will be released. Lots more details up on the Beta thread: https://forum.pjrc.com/threads/54711...irst-Beta-Test. It has 7 or 8 Serial ports and the registers are different. IMXRT

  8. #8
    Senior Member
    Join Date
    Apr 2013
    Posts
    127
    Quote Originally Posted by KurtE View Post
    Again not sure what it is you are needing to do to set the registers.... In my earlier case it was for the above, it was to setup to use half duplex to support Robotis Dynamixel servos. At the time I punted on T3.5/6 to support Serial6 (although will probably for the heck of it add it for 3.5) as it just adds another set of #if defined(__MK64FX512__)

    For me in most of these cases I did not care, as on most Teensy boards, I wanted to use Serial1/2 as they have the larger fifo queues... I should probably take a look again at Serial6 on T6, as I don't remember how well the performance compared.

    But just a heads up: Soon the Teensy 4 board will be released. Lots more details up on the Beta thread: https://forum.pjrc.com/threads/54711...irst-Beta-Test. It has 7 or 8 Serial ports and the registers are different. IMXRT
    I was working on this:
    https://github.com/MarkusLange/Teensy_3.x_LIN_Master

  9. #9
    Senior Member
    Join Date
    Feb 2017
    Posts
    299
    Trouble is, your starting philosophy is wrong. The purpose of the Stream class is to handle the I/O without regard to the underlying hardware (could be USB, HardwareSerial, SoftwareSerial, etc). The code that instantiates objects of your 'XXX' class should take care of any required, hardware-specific initialization or settings since it KNOWS which Stream object is being used.
    Last edited by gfvalvo; 06-09-2019 at 12:19 PM.

  10. #10
    Senior Member
    Join Date
    Apr 2013
    Posts
    127
    Quote Originally Posted by gfvalvo View Post
    Trouble is, your starting philosophy is wrong. The purpose of the Stream class is to handle the I/O without regard to the underlying hardware (could be USB, HardwareSerial, SoftwareSerial, etc). The code that instantiates objects of your 'XXX' class should take care of any required, hardware-specific initialization or settings since it KNOWS which Stream object is being used.
    May I get you wrong, but Stream works as expected, and does all the stuff. But I need to set some Registers that Serial did not handle by default. The registers are for each Serial specific so I have to know the Serial to do my settings.

  11. #11
    Senior Member
    Join Date
    Feb 2017
    Posts
    299
    That's fine, but the code to do that register manipulation doesn't belong in your XXX class. Since the class deals with a Stream pointer, it should only do things that Stream knows about. The Serial-specific register manipulation should be done in the code that invokes the XXX constructor because that's the code that KNOWS which Serial port pointer it's supplying.

  12. #12
    Senior Member
    Join Date
    Apr 2013
    Posts
    127
    This Post https://forum.pjrc.com/threads/56451...l=1#post207632 contains the code, feel free to take a look.

  13. #13
    Senior Member
    Join Date
    Feb 2017
    Posts
    299
    OK, I just re-read your original post. I see you're actually supplying a pointer to a HardwareSerial object. It was a little confusing since you named it "stream". I misread that as the class Stream. Makes more sense now.

Posting Permissions

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