Go Down

Topic: Disabling UART on Due? (Read 3 times) previous topic - next topic

tuxduino

Thanks for your response. Looked up that code, very interesting :)

Bi0H4z4rD

#6
Jan 10, 2013, 12:12 am Last Edit: Jan 10, 2013, 12:14 am by Bi0H4z4rD Reason: 1
The "code" was so what i meant could be understood, not a sketch!

Here is the full code for the function:

Code: [Select]
void iso_init()
{
 long currentTime = millis();
 static long initTime;
 switch (ISO_InitStep)
 {
   case 0:
     // setup
     ECUconnection = false;
     serial_tx_off(); //disable UART so we can "bit-Bang" the slow init.
     serial_rx_off();
     initTime = currentTime + 3000;
     ISO_InitStep++;
     break;
   case 1:
     if (currentTime >= initTime)
     {
       // drive K line high for 300ms
       digitalWrite(K_OUT, HIGH);

       initTime = currentTime + 300;
       ISO_InitStep++;
     }
     break;
   case 2:
   case 7:
     if (currentTime >= initTime)
     {
       // start or stop bit
       digitalWrite(K_OUT, (ISO_InitStep == 2 ? LOW : HIGH));
       initTime = currentTime + (ISO_InitStep == 2 ? 200 : 260);
       ISO_InitStep++;
     }
     break;
   case 3:
   case 5:
     if (currentTime >= initTime)
     {
       // two bits HIGH
       digitalWrite(K_OUT, HIGH);

       initTime = currentTime + 400;
       ISO_InitStep++;
     }
     break;
   case 4:
   case 6:
     if (currentTime >= initTime)
     {
       // two bits LOW
       digitalWrite(K_OUT, LOW);

       initTime = currentTime + 400;
       ISO_InitStep++;
     }
     break;
   case 8:
     if (currentTime >= initTime)
     {


       // bit banging done, now verify connection at 10400 baud
       byte b = 0;
       // switch now to 10400 bauds
       Serial.begin(10400);

       // wait for 0x55 from the ECU (up to 300ms)
       //since our time out for reading is 125ms, we will try it up to three times
       byte i=0;
       while(i<3 && !iso_read_byte(&b))
       {
         i++;
       }

       if(b == 0x55)
       {
         ISO_InitStep++;
       }
       else
       {
         // oops unexpected data, try again
         ISO_InitStep = 0;
       }
     }
     break;
   case 9:
     if (currentTime >= initTime)
     {
       byte b;
       bool bread;
     
       bread = iso_read_byte(&b);  // read kw1

   
       bread = iso_read_byte(&b);  // read kw2


       // 25ms delay needed before reply (url with spec is on forum page 56)
       // it does not work without it on VW MK4
       delay(25);
     
       // send ~kw2 (invert of last keyword)
       iso_write_byte(~b);

   
       // ECU answer by 0xCC (~0x33)
       // read several times, ECU not always responds in time
       byte i=0;
       bread = iso_read_byte(&b);
       while (i<3 && !bread)
       {
         i++;
         bread = iso_read_byte(&b);
       }
     
       if (b == 0xCC)
       {
          ECUconnection = true;

       }
       ISO_InitStep = 0;
     }
     break;
 }

void serial_rx_off()
{
 UCSR0B &= ~(_BV(RXEN0));  //disable UART RX
}

void serial_tx_off()
{
  UCSR0B &= ~(_BV(TXEN0));  //disable UART TX
  delay(20);                 //allow time for buffers to flush
}


I need to replicate the last two functions ( serial_tx_off() and serial_rx_off() ) on Due in order for this to work.

BR

tuxduino

Ouch, I meant that *iso* code :P

Going to have a look at your code, though :D

Bi0H4z4rD

Ouch, my missunderstanding too i guess  :P

Anyway code is not mine, im just modding it to do some other stuff, but that's the part i need to get working.

If you want to see the full project for the code i have posted, you can find it here:

http://code.google.com/p/opengauge/wiki/OBDuino

BR

tuxduino

Section 29.2.2 of the SAM3X datasheet and block diagram in section 29.2.3 suggest peripheral clocks can be independently disconnected. So I guess one way to "turn off" an USART would be to disable its clock.

Disclaimer: just thinking out loud, I've just skimmed the datasheet :)

Go Up