Uploaded sketch is not working on teensy 3.6 after restarting

Anayat

New member
Dear All,

I am using the nvidia jetson nx boards( ubuntu 18.04) USB 3.1 interface to power ON the Teensy 3.6 Board.

I have uploaded the FLEXCAN library example sketch to the teensy 3.6 board. The code is given below:

#include <FlexCAN.h>

#ifndef __MK66FX1M0__
#error "Teensy 3.6 with dual CAN bus is required to run this example"
#endif

static CAN_message_t msg;
static uint8_t hex[17] = "0123456789abcdef";

// -------------------------------------------------------------
static void hexDump(uint8_t dumpLen, uint8_t *bytePtr)
{
uint8_t working;
while( dumpLen-- ) {
working = *bytePtr++;
Serial.write( hex[ working>>4 ] );
Serial.write( hex[ working&15 ] );
}
Serial.write('\r');
Serial.write('\n');
}


// -------------------------------------------------------------
void setup(void)
{
delay(1000);
Serial.println(F("Hello Teensy 3.6 dual CAN Test."));

Can0.begin();
Can1.begin();

//if using enable pins on a transceiver they need to be set on
pinMode(2, OUTPUT);
pinMode(35, OUTPUT);

digitalWrite(2, HIGH);
digitalWrite(35, HIGH);

msg.ext = 0;
msg.id = 0x100;
msg.len = 8;
msg.buf[0] = 10;
msg.buf[1] = 20;
msg.buf[2] = 0;
msg.buf[3] = 100;
msg.buf[4] = 128;
msg.buf[5] = 64;
msg.buf[6] = 32;
msg.buf[7] = 16;
}


// -------------------------------------------------------------
void loop(void)
{
CAN_message_t inMsg;
while (Can0.available())
{
Can0.read(inMsg);
Serial.print("CAN bus 0: "); hexDump(8, inMsg.buf);
}
msg.buf[0]++;
Can1.write(msg);
msg.buf[0]++;
Can1.write(msg);
msg.buf[0]++;
Can1.write(msg);
msg.buf[0]++;
Can1.write(msg);
msg.buf[0]++;
Can1.write(msg);
delay(20);
}


The sketch works perfectly for me. I am able to send and receive CAN messages. But the problem is when i turn off the NVIDIA NX board and again power ON then the uploaded sketch is not working.
Interestingly if i unplug the Teensy 3.6 and again plug in then the uploaded sketch started working.

I have checked the LED blinking example also. The LED blinking example is working perfectly and i am not facing any issue.

Any suggestion how to solve this problem will be highly appreciated.


Regards

Anayat
 
Perhaps you should comment out these 2 lines and try again:
Serial.println(F("Hello Teensy 3.6 dual CAN Test."));
Serial.print("CAN bus 0: "); hexDump(8, inMsg.buf);


Paul
 
Hi Mr. Paul,

Thanks for your reply. If I comment out the serial print then it's working.
But I need the serial print data to use the CAN messages in another application.
Below code, I have used to read the teensy 3.6 CAN messages.

// C library headers
#include <stdio.h>
#include <string.h>

// Linux headers
#include <fcntl.h> // Contains file controls like O_RDWR
#include <errno.h> // Error integer and strerror() function
#include <termios.h> // Contains POSIX terminal control definitions
#include <unistd.h> // write(), read(), close()
/******************Main*******************************/
int main() {
int serial_port = open("/dev/ttyACM0", O_RDONLY);
// Create new termios struc, we call it 'tty' for convention
struct termios tty;
// Set in/out baud rate to be 9600
//cfsetispeed(&tty, B9600);
//cfsetospeed(&tty, B9600);
char read_buf [256];
memset(&read_buf, '\0', sizeof(read_buf));
int num_bytes;
while(1)
{
num_bytes = read(serial_port, &read_buf, sizeof(read_buf));

if (num_bytes < 0)
{
printf("Error reading: %s", strerror(errno));
return 1;
}
printf("Read %i bytes. Received message: %s", num_bytes, read_buf);
}
close(serial_port);
return 0; // success
}

I am receiving some values when I upload the sketch.

But again if I power off the NVIDIA NX Board and turn on after some time then I am not getting CAN Message from Teensy 3.6 to my NVIDIA NX serial port.

Is there any better way to read the Teensys values from another embedded device that has Linux Ubuntu?


Thanks again for your quick support.

Regards

Anayat
 
Code:
#include <FlexCAN.h>

#ifndef __MK66FX1M0__
#error "Teensy 3.6 with dual CAN bus is required to run this example"
#endif

static CAN_message_t msg;
static uint8_t hex[17] = "0123456789abcdef";

// -------------------------------------------------------------
static void hexDump(uint8_t dumpLen, uint8_t* bytePtr)
{
	uint8_t working;
	while (dumpLen--) {
		working = *bytePtr++;
		Serial.write(hex[working >> 4]);
		Serial.write(hex[working & 15]);
	}
	Serial.write('\r');
	Serial.write('\n');
}


// -------------------------------------------------------------
void setup(void)
{
	/********************************************************
	*  MODIFY YOUR CODE TO INCLUDE THE FOLLOWING TWO LINES  *
	*********************************************************/
	Serial.begin(9600);	            // You need this line. Baud rate can be anything
	while (!Serial && millis() < 5000); // wait for up to 5 seconds for Serial Usb to establish itself
	// delay(1000);  -- NOT NEEDED
	Serial.println(F("Hello Teensy 3.6 dual CAN Test."));

	Can0.begin();
	Can1.begin();

	//if using enable pins on a transceiver they need to be set on
	pinMode(2, OUTPUT);
	pinMode(35, OUTPUT);

	digitalWrite(2, HIGH);
	digitalWrite(35, HIGH);

	msg.ext = 0;
	msg.id = 0x100;
	msg.len = 8;
	msg.buf[0] = 10;
	msg.buf[1] = 20;
	msg.buf[2] = 0;
	msg.buf[3] = 100;
	msg.buf[4] = 128;
	msg.buf[5] = 64;
	msg.buf[6] = 32;
	msg.buf[7] = 16;
}


// -------------------------------------------------------------
void loop(void)
{
	CAN_message_t inMsg;
	while (Can0.available())
	{
		Can0.read(inMsg);
		Serial.print("CAN bus 0: "); hexDump(8, inMsg.buf);
	}
	msg.buf[0]++;
	Can1.write(msg);
	msg.buf[0]++;
	Can1.write(msg);
	msg.buf[0]++;
	Can1.write(msg);
	msg.buf[0]++;
	Can1.write(msg);
	msg.buf[0]++;
	Can1.write(msg);
	delay(20);
}
Can you try with the modifies code above.
Also in future could you include your code between code tags using the # button.
It makes it so much easier to read and understand as I am sure you will agree if
you compare my offering above with yours.
 
Yes, I agree with Bricomp's suggestion.
Teensy boots very fast, so one needs to insert some waiting time for the nvidia jetson to finish it's boot procedure.

Paul
 
Yes, I agree with Bricomp's suggestion.
Teensy boots very fast, so one needs to insert some waiting time for the nvidia jetson to finish it's boot procedure.

Paul

Is there any way to know or any Teensy-API to know if the USB enumeration is started and if it is completed?
 
You could use this:

extern "C" volatile uint8_t usb_configuration;

usb_configuration will be zero at startup and non-zero when the USB host configures the device. But most USB hosts read additional descriptors and load drivers after the set configuration command, so it's not really the end of enumeration.

For Serial.print(), normally you would check Serail as a boolean, which tells if the USB host has sent a command to configure the serial parameters. So like this:

while (!Serial) { /* wait */ }
 
Found a potential solution in this thread.
When I uploaded this code into a Teensy 3.2 and click "Restart Port" in USB Device Tree Viewer, I see the onboard LED go off and on.
Powering the Teensy by 5V externally [no USB cable connected], the LED stays off.
Code:
// https://forum.pjrc.com/threads/26039-Detect-USB-connected?p=53359&viewfull=1#post53359
void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  if (!bitRead(USB0_OTGSTAT, 5)) {
    digitalWrite(LED_BUILTIN, HIGH);
  }
  else {
    digitalWrite(LED_BUILTIN, LOW); 
  }
  delay(100);
}

Untitled.png

Paul
 
Back
Top