Jetson TK1, unwanted SPI message delay


Problem in Short: I can’t send SPI messages from Jetson TK1 (ubuntu 14.04) to another MC in real time (or with in 1-2 ms limit). There is an unknown 8-16 ms unwanted delay.
Other than sending the message, the program works quite accurately within 2 ms tolerance. Catching all the objects captured via camera, and storing the images with appropriate time stamps.

Question: What may be the cause of this delay? And how can I solve it?

Details of the Problem: Using Jetson TK1 and a USB3.0 camera to capture small patch of images at every 8 ms (@125 fps), analyzing these frames with OpenCV and if an object appears then sends an 8 bit SPI messages to a arm based MC (TM4C). As soon as SPI signal reach to TM4C, blue light on that MC switch. TM4C is waiting for the input message all the time. Light switching is quite handy for visual testing purposes.

For each frame, capturing, analyzing images and sending 8 bit SPI signals finish within 2 ms (at most). Everything seem quite perfect according to timer log files I store in the txt file and images captured when the object detected (using sys/time.h as timer).

But in the actual test, where I use another independent high speed camera, there is an unwanted 8-16 ms delay on the light switching time after an object entered into the scene.

More Details:
*Using Grinch Kernel 19.3

*After each restart, typing these to console in root (in order to maximize cpu performance):
echo 0 > /sys/module/usbcore/paramters/usbfs_memory_mb
echo 0 | sudo tee /sys/module/usbcore/parameters/usbfs_memory_mb
echo 0 > /sys/devices/system/cpu/cpuquiet/tegra_cpuquiet/enable
echo 1 > /sys/devices/system/cpu/cpu0/online
echo 1 > /sys/devices/system/cpu/cpu1/online
echo 1 > /sys/devices/system/cpu/cpu2/online
echo 1 > /sys/devices/system/cpu/cpu3/online
echo performance > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor

*Running the output file of cpp as root.

*SPI transfer function I use:

int transfer(int fd, unsigned char send[], unsigned char receive[], int length)
	struct spi_ioc_transfer transfer; // the transfer structure  
	transfer.tx_buf = (unsigned long) send; // the buffer for sending data 
	transfer.rx_buf = (unsigned long) receive; // the buffer for receiving data 
	transfer.len = length; // the length of buffer 
	transfer.speed_hz = 4000000; // the speed in Hz 
	transfer.bits_per_word = 8; // bits per word 
	transfer.delay_usecs = 0; // delay in us // send the SPI message (all of the above fields, inc. buffers) 

	int status = ioctl(fd, SPI_IOC_MESSAGE(1), &transfer); 
        if (status < 0) 
		perror("SPI: SPI_IOC_MESSAGE Failed"); 
		return -1; 
	return status;

How do you make sure the delay is on TK1? You may need to profile the SPI transfer from user space to kernel SPI driver start transfer to break down the delay time.

Thanks for the response. Checked the problem with logic analyser, and yes problem is at Jetson side.

I have newly started learning Linux system calls and kernel, so quite newbie on this subject. Which tool do you suggest me for profiling?

Hi Soajan
You can print the timestamp from the API and to SPI driver start transfer message to break down it.

We just figured out that; the camera was buffering the images, hence sending the old stored images to the card.

So the delay problem had nothing to do with Jetson TK1 or Ubuntu. Problem is solved