chipKIT® Development Platform

Inspired by Arduino™

SPI Wild While Usage

Created Mon, 08 Feb 2016 21:10:44 +0000 by FredCailloux


FredCailloux

Mon, 08 Feb 2016 21:10:44 +0000

I have this code snipet from an SPI driver for the LS7366 and it include a strange usage of the While keyword. while (pReg->stat.SPIRBF == 0); with no Loop or internal conditional execution, just the plain While() with one condition and that's it? :o

void read_7366(int reg, unsigned char *bytearray) {
	unsigned char ir = (0x1 << 6 ) | (reg << 3); 			//Instruction
	unsigned char ReadData;

	if ( (reg == MDR0) || (reg == MDR1) || (reg == STR) ) {
		volatile SpiRegMap*	pReg=SpiMapTbl[SPICHN-1];	//One byte to read
		SLAVE_SELECT = 0;
		delay();						//Setup time
		SpiChnPutC(SPICHN, ir);					//Write instruction after TX buffer empty
		while (pReg->stat.SPIRBF == 0);				//Wait for RX buffer full
		ReadData = SpiChnGetC(SPICHN);				//Read what was clocked in during last write (nothing)
		SpiChnPutC(SPICHN, 0);					//Clock out dummy byte after TX buffer empty
		while (pReg->stat.SPIRBF == 0);				//Wait for RX buffer full
		ReadData = SpiChnGetC(SPICHN);				//Read what was clocked in during last write (register)
		*bytearray = ReadData;
		SLAVE_SELECT = 1;					//End comm
		return; }

Cna anyone explain the details and the why ? Thanks


majenko

Mon, 08 Feb 2016 21:29:40 +0000

It is exactly what it says it is.

While SPIRBF is 0 do nothing.

That is, wait here until SPIRPF is anything other than 0. I.e., wait for something to arrive in the receive buffer before continuing.

In BASIC it would be something like:

10 IF SPIRBF = 0 THEN GOTO 10