arduinoserial-portteensy

How can I create a serial and read at a certain frequency on a Teensy


I think my question is more of a lack of comprehension, but here is an overview:

I'm trying to discuss with an old Mitsubishi which uses serial communications. The initialization works by toggling the K-Line at 5 bauds (this technique seems to be used elsewhere), then pass the K-Line as INPUT, read some code, and after that you can read and write directly on the K-Line. The details of said protocol (and the fact that we are only using one line) are detailled here.

This messes me up a bit, since I'm used to a TX pin and an RX pin, not having to switch a pin between TX and RX after each read and write, but let's assume this works.

How can I set up a serial communication (specify baudrate and RX/TX pins, which will be the same here) on a Teensy 3.2 ? (classic Arduino C++ stuff)

Or maybe I can have two pins, each connected to the K-Line. This is physically the same but allow me to define it in software as a standard serial.

Here is the written code so far:

void setup() {

    // HERE: SETUP SERIAL WITH ONE PIN
    // Or do the two-pins-on-same-wire-thing with HWSerial.begin(15625, SERIAL_8N1); 
    pinMode(DATA_PIN, OUTPUT);


    // MUT Init
    int init = MUT_INIT_CODE; 
    for (int i= 0; i < 9; i++){
        if (init & 1){ 
            digitalWrite(DATA_PIN, HIGH);
            digitalWrite(LED_PIN, HIGH);
        } else {
            digitalWrite(DATA_PIN, LOW);
            digitalWrite(LED_PIN, LOW);
        }
        init >>= 1;
        delay(MUT_INIT_DELAY);
    }

    byte answer[3];
    pinMode(DATA_PIN, INPUT);
    HWSerial.readBytes(answer, 3);

}

Thanks in advance!


Solution

  • Technically you can not tie RX and TX together, because a hardware serial module (like the one inside the Teensy) will drive TX high by default, so RX would be always high as well. You need to configure your pin sometimes as an input, sometimes as an output.

    You can not use a hardware serial module because the TX and RX pins are predefined and separate, and you should try using a software serial emulation library like https://www.pjrc.com/teensy/td_libs_AltSoftSerial.html

    Try changing the code to add a function to reconfigure your unique pin as an input or output, as needed, and declare the same pin for TX and RX.

    edit: a hardware approach would be to use an analog multiplexer like the MAX4619. This way you can use a hardware serial module on the Teensy, connect TX and RX to the multiplexer's X0/X1 pins, and the K-line on the X pin. You can toggle the multiplexer's A command pin with a Teensy GPIO. I didn't test this approach because I don't have this kind of car, but it might be worth giving it a try.

    edit2: to answer more specifically the op's question, the pins of the serial modules of the teensy are hardwired. On a Teensy 3.2, Serial1 is connected to pins 0/1 or 5/21, Serial2 to pins 9/10 or 26/31, etc. The list is available here: https://www.pjrc.com/teensy/td_uart.html The baud rate is configured by calling Serial1.begin(YOUR_BAUD_RATE);