LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware > Linux - Embedded & Single-board computer
User Name
Password
Linux - Embedded & Single-board computer This forum is for the discussion of Linux on both embedded devices and single-board computers (such as the Raspberry Pi, BeagleBoard and PandaBoard). Discussions involving Arduino, plug computers and other micro-controller like devices are also welcome.

Notices


Reply
  Search this Thread
Old 01-30-2021, 04:00 PM   #1
scotty7
LQ Newbie
 
Registered: Jan 2021
Posts: 4

Rep: Reputation: Disabled
ioctl() only works after iterating over data with printf function


Hi everyone,

Noob here. I've been playing around with reading & writing i2c from userspace and I've come up against a weird issue.

I've created a variadic function to send a number of bytes to an i2c device. It works great if I include the 'debug printf for loop'. It iterates through the data I'm about to send, casts it as an integer (because I couldn't find a printf formatter for a short) and prints it out. For some reason if I comment out that loop, then the ioctl function will return an error.

I didn't think casting inside a printf statement would do anything to the original data, so the fact that it's messing with the code after it is odd to me. If anyone can shine some light on why this is happening I would appreciate it.

Code:
bool i2c_write(short addr, short reg, short num, ...){ 

	va_list data;
	va_start(data, num);
	
	size_t size = num+1;
	unsigned char *send_buffer = malloc(size);

	if(send_buffer == NULL){
		printf("i2c_write: Could not allocate memory");
		return false;
	}
	
	send_buffer[0] = reg;
	for(int i = 0; i<num; i++){
		send_buffer[i+1] = (unsigned char)va_arg(data, int);
	}

	//debug to see sent data
	// for(int i = 0; i<size; i++){
	// 	printf("send buffer [%d]: %d", i, (int)send_buffer[i]);
	// }

	va_end(data);

	struct i2c_rdwr_ioctl_data burst;
	struct i2c_msg i2c_message;
	
	burst.nmsgs 		= 1;
	burst.msgs 		= &i2c_message;

	i2c_msg.addr 	= addr;
	i2c_msg.buf 		= send_buffer;
	i2c_msg.len 		= num+1;

	int result = ioctl(i2c_fd, I2C_RDWR, &burst); //actually send the data

	free(send_buffer);

	if(result < num){ 	//check for the correct number of send bytes, doesn't seem to work atm
		printf("i2c_write: data was not sent properly, %d bytes sent. %s",result,strerror(errno));
		return false;
	}else{
		return true;
	}
}
 
Old 02-03-2021, 11:14 PM   #2
scotty7
LQ Newbie
 
Registered: Jan 2021
Posts: 4

Original Poster
Rep: Reputation: Disabled
Fixed it! Well actually someone else suggested I try it, and it ended up being the culprit.

Not setting the flag on some ioctl calls was causing the issue. On every call to ioctl it seems to set the flags to <some value>. Then on subsequent calls, if the flags were not set, the ioctl function would read these bad flags, and fail.

Lesson learned. Set the flags, even if you don't think they need setting.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] For loop iterating over a list of IP addresses from a txt file hebruiser Programming 3 03-03-2018 10:33 PM
[SOLVED] printf $"Hello $var\n" vs. printf "Hello $var\n" -- not a typo. What is it? GrapefruiTgirl Programming 2 10-21-2010 08:21 AM
How to pass IOCTL arguments from usespace ioctl call devkpict Linux - Kernel 1 12-07-2007 06:45 PM
Iterating over a map(c++)? Guest1234 Programming 2 05-12-2005 03:33 PM
How is 'man 3 printf' different from 'man printf' ?? purpleburple Linux - General 3 09-23-2002 12:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware > Linux - Embedded & Single-board computer

All times are GMT -5. The time now is 12:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration