Click here to Skip to main content
14,269,494 members

Linux Programming

 
Questionioctl - from code to hardware Pin
Vaclav_3-Jan-19 4:41
memberVaclav_3-Jan-19 4:41 
SuggestionRe: ioctl - from code to hardware Pin
Richard MacCutchan3-Jan-19 6:31
protectorRichard MacCutchan3-Jan-19 6:31 
GeneralRe: ioctl - from code to hardware Pin
Vaclav_3-Jan-19 11:03
memberVaclav_3-Jan-19 11:03 
GeneralRe: ioctl - from code to hardware Pin
Richard MacCutchan3-Jan-19 22:11
protectorRichard MacCutchan3-Jan-19 22:11 
GeneralRe: ioctl - from code to hardware Pin
Vaclav_4-Jan-19 5:36
memberVaclav_4-Jan-19 5:36 
QuestionWhich is most current Linux "man" resource to consult with? Pin
Vaclav_2-Jan-19 5:36
memberVaclav_2-Jan-19 5:36 
AnswerRe: Which is most current Linux "man" resource to consult with? Pin
Richard MacCutchan2-Jan-19 6:29
protectorRichard MacCutchan2-Jan-19 6:29 
QuestionCoding C++ ioctl SPI_IOC_MESSAGE Pin
Vaclav_26-Dec-18 4:44
memberVaclav_26-Dec-18 4:44 
I would like somebody to walk me thru this code to help me understand some details I am having an issue with.

The purpose of the code is to send 8 bits data to hardware – LCD - using SPI communication protocol and Linux ioctl, in C++.

It in an essence works, but I “do not get” totally how.
Since I am not interested in psychoanalysis of my code or “doing it differently” I would prefer to hold off my questions until somebody with interest and knowledge of all of the above – passing data to function , operation of ioctl “command” - SPI_IOC_MESSAGE and SPI (bidirectional ) communication replies.

Basic implementation of the code is a copy of C code and I like to clean it up so it looks as real C++.



function call

vna.spi.SPI_Transfer_3_Line(1);// send command - software reset


function SPI transfer

Reformatted

int C_IOCTL_SPI::SPI_Transfer_3_Line(char data) {
#ifdef DEBUG

	cout << "*** TRACE file " << __FILE__ << endl;
	cout << " function " << __FUNCTION__ << endl;
	cout << " data    " << +data << endl;
	cout << " @ line   \033[0m " << dec << __LINE__ << endl;
	cout << "\033[0m  " << endl;
	//exit(1);
#endif

	struct spi_ioc_transfer xfer[2];
	unsigned char buf[32], *bp;
	unsigned char tx_buf[32], *tx_bp;
	unsigned char rx_buf[32], *rx_bp;
	int len, status;

// just for fun check file desriptor
	if (FileDescriptor) {
#ifdef DEBUG
		cout << "FileDescriptor OK " << dec << +FileDescriptor << endl;
#endif
	}

	memset(xfer, 0, sizeof xfer);
	memset(buf, 0, sizeof buf);

	memset(tx_buf, 0, sizeof tx_buf);
	memset(rx_buf, 0, sizeof rx_buf);

	len = sizeof buf;

      // pass data 
	buf[0] = data;
	// modify structure with real data
	//len = 16;      // number of expected characters in response ??
	xfer[0].tx_buf = (unsigned long) buf;
	xfer[0].rx_buf = (unsigned long) buf;
	xfer[0].len = 1; // command 0x0 plus data actual length
      //  works with len = 1
	// print TX buffer
	for (int index = 0; index != xfer[0].len; index++)
	cout << "TX buffer index " << dec << index << " 0x" << hex << +buf[index]<< endl;

	// here or in next structure ???
	// xfer[1].cs_change = 1; // switch to receive ?? no  respoinse
	for (int index = 0; index < 0x10; index++) {
		// print only valid commands
		if (buf[index])
			cout
					<< "   ********************                 commnad    tx buffer value "
					<< +buf[index] << endl;
	}

//	xfer[0].bits_per_word = 9;
//	xfer[0].speed_hz = 1; // temp slow

// NOTE  with cs_change = 1 getting response
// xfer[1].cs_change = 1; // switch to receive respose
// but it stops LCD from setting

	xfer[1].rx_buf = (unsigned long) buf;
	xfer[1].len = 16; // RX buffer expected length
//	xfer[1].speed_hz = 1;  // temp slow
// status total number of characters processed
status = ioctl(FileDescriptor, SPI_IOC_MESSAGE(2), xfer);
// read only ?? 
//	status = ioctl(FileDescriptor, SPI_IOR_MESSAGE(2), xfer);
	if (status < 0) {
		cout << "status " << status << endl;
		perror("Transfer error SPI_IOC_MESSAGE");
		return -1;
	}
	printf("Expected reply of %d characters \n", len);
	printf("Processed %d characters \n", status);

#ifdef DEBUG
	cout << " TX DATA  0x" << hex << +data << endl;
	cout << " TX DATA dec " << dec << +data << endl;
#endif

	printf("response(%d): ", status);

	for (int index = 0; index != xfer[1].len; index++) {
		// print only if > 0
		if (buf[index])
			cout
					<< " !!!!!!!!!!!!!!!!!!!!!!    DATA response    rx buffer value "


modified 27-Dec-18 12:43pm.

GeneralRe: Coding C++ ioctl SPI_IOC_MESSAGE Pin
Richard MacCutchan26-Dec-18 7:01
protectorRichard MacCutchan26-Dec-18 7:01 
GeneralRe: Coding C++ ioctl SPI_IOC_MESSAGE Pin
Vaclav_26-Dec-18 7:51
memberVaclav_26-Dec-18 7:51 
GeneralRe: Coding C++ ioctl SPI_IOC_MESSAGE Pin
Richard MacCutchan26-Dec-18 22:24
protectorRichard MacCutchan26-Dec-18 22:24 
QuestionAre there any Yocto masters here? Pin
Munchies_Matt6-Dec-18 3:32
memberMunchies_Matt6-Dec-18 3:32 
QuestionSo... Pin
Nathan Minier4-Dec-18 1:29
professionalNathan Minier4-Dec-18 1:29 
AnswerRe: So... Pin
Eddy Vluggen4-Dec-18 1:32
mveEddy Vluggen4-Dec-18 1:32 
GeneralRe: So... Pin
Nathan Minier4-Dec-18 1:35
professionalNathan Minier4-Dec-18 1:35 
AnswerRe: So... Pin
#realJSOP7-Jan-19 1:39
mve#realJSOP7-Jan-19 1:39 
QuestionFIRST ONE! Pin
#realJSOP1-Dec-18 4:45
mve#realJSOP1-Dec-18 4:45 
QuestionRe: FIRST ONE! Pin
Eddy Vluggen3-Dec-18 10:01
mveEddy Vluggen3-Dec-18 10:01 
AnswerRe: FIRST ONE! Pin
ZurdoDev4-Dec-18 2:32
professionalZurdoDev4-Dec-18 2:32 
GeneralRe: FIRST ONE! Pin
Eddy Vluggen4-Dec-18 2:51
mveEddy Vluggen4-Dec-18 2:51 
GeneralRe: FIRST ONE! Pin
ZurdoDev4-Dec-18 2:56
professionalZurdoDev4-Dec-18 2:56 
AnswerRe: FIRST ONE! Pin
#realJSOP7-Jan-19 1:38
mve#realJSOP7-Jan-19 1:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.