How to find out which Serial is used

Status
Not open for further replies.

Markus_L811

Well-known member
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
 
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;
 
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.
 
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?
 
Nope, you did not miss something :D yes on T3.5 all of them are the same and T3.6 the 6th is the LPUART
 
Nope, you did not miss something :D 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!
 
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-Teensy-4-0-First-Beta-Test. It has 7 or 8 Serial ports and the registers are different. IMXRT
 
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-Teensy-4-0-First-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
 
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:
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.
 
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.
 
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.
 
Status
Not open for further replies.
Back
Top