Mega2560 - How do I drive TX1 HIGH/LOW?

Hi all,

I am using a Mega2560 to interface with an automotive K-Line (ISO14230).
I have a working interface with a sketch using the SoftwareSerial class, but new shield/pin conflicts now have me using the hardware serial ports and HardwareSerial class, and pinning out from TX1/RX1 on pins 18/19.

Before sending data out, I need to perform a "Fast Initialization" of the bus line.
The TX line is set HIGH for 300 mSec, LOW for 25, HIGH for 25, then a packet addressed to the vehicle module is sent at 10400 baud.

My analyzer on TX1 isn't showing these digitalWrite() calls at all...

The scope starts LO, then goes
HI 0.4ms,
LO 356ms, (which looks like the period between .end() and .begin() calls)
HI 4.7 ms,
then data begins at 10400 baud.

I hope I'm missing something obvious to the more learned among you?
How should I be manipulating the TX1 line state on HardwareSerial?

Thanks,

-dave

  HardwareSerial* k_line = &Serial1;
  uint8_t tx_pin = 18, len = 3, data[] = {1,2,3};


  k_line->end(); // empty i/o buffers; re-enable general pin i/o
  pinMode(tx_pin, OUTPUT);

  digitalWrite(tx_pin, HIGH); // K-line fast init T_idle = 300 ms
  delay(300);

  digitalWrite(tx_pin, LOW); // K-line T_init_L = 25 ms
  delay(25);

  digitalWrite(tx_pin, HIGH); // K-line T_init_H = 25 ms
  delay(25);
  
  k_line->begin(10400); // re-establish buffered i/o over digital pins
  while(!k_line) ; // common practice but seems pointless?

  for(uint8_t i=0; i<len; i++) k_line->write(data[n]); // works fine - data on scope

So... I ran some test scripts to explore this behavior, and found that if I duplicate my pinMode(TX2, OUTPUT) call at the head of my setup() function everything works as expected, but if I wait and call pinMode() only in the later communications function (as above) then the digitalWrite() calls have no effect and my TX2 pin behaves as described in my previous post.

My hypothesis is that I need to set the pin mode in advance of the first call to HardwareSerial.begin(), otherwise the port's tx pin mode becomes locked to the pinMode() function, even if HardwareSerial.end() has been called. I have limited data...

Has anyone else encountered this issue?

-dave

Once you call Serial1.begin() the hardware UART module will take control of the pins and override any digitalWrite() calls made to the pins.