Spi.read() function

I was updating the code above to use the new SPI class function readblob() but I’m not sure how to do bitwise operations on the object that readblob() returns.

Here’s what I tried:
`imp.configure(“spi1”, [], []);
hardware.pin1.configure(DIGITAL_OUT); //chip select
hardware.spi257.configure(MSB_FIRST | CLOCK_IDLE_LOW , 1000);
t <- hardware.spi257.readblob(4);
//Shift bits to combine into a 16 bit unsigned integer containing the temp and the fault bit
local tc = t[0]<<8 | t[1];
//Shift bits into a 16 bit unsigned integer (cold junction/faults)
local cjc = t[2]<<8 | t[3];

//Define functions
function readCelsius(){
if (tc & 0x1){
return 2000 //if there is a fault return 2000 degrees
};
tc = tc & 0xfffc; //mask the last two bits
tc = (tc<<16)>>16; //convert to a signed integer
return tc / 16.0; //
}`

So that looks ok - are you having issues with it?

just in case it may help someone…

here is some code based on the above with the error codes flushed out. I tested an open circuit, short to VCC and short to ground.

`
function readCelcius(){

hardware.pin7.write(0); //pull CS low to start the transmission of temp data
	
	//0[31..24],1[23..16],2[15..8],3[7..0]
    local temp32=hardware.spi257.readblob(4);//SPI read is totally completed here

hardware.pin7.write(1); // pull CS high

local tc = 0;

if ((temp32[1] & 1) ==1){
	
    //Error bit is set
	
	local errorcode = (temp32[3] & 7);// 7 is B00000111
	
	if (errorcode>0){
		
		//One or more of the three error bits is set
		//B00000001 open circuit
		//B00000010 short to ground
		//B00000100 short to VCC
		
		switch (errorcode){
        
        case 1:
		    
            server.log("TC open circuit");
		    break;
		
		case 2:
        
            server.log("TC short to ground");
		    break;
        
        case 3:
        
            server.log("TC open circuit and short to ground")
            break;
		
		case 4:
        
            server.log("TC short to VCC");
		    break;
		
		default:
        
            //Bad coding error if you get here
		    break;
		}
		
		TCErrCount+=1;
		//if there is a fault return this number, or another number of your choice
		 tc= 67108864; 
	}
    else
    {
         server.log("error in SPI read");
    }
    
} 
else //No Error code raised
{
	local highbyte =(temp32[0]<<6); //move 8 bits to the left 6 places

//move to the right two places, lowing two bits that were not related
local lowbyte = (temp32[1]>>2);
tc = highbyte | lowbyte; //now have right-justifed 14 bits but the 14th digit is the sign

	//Shifting the bits to make sure negative numbers are handled
    
    //get the sign indicator into position 31 of the signed 32-bit integer
	
    //Then, scale the number back down, the right-shift operator of squirrel/impOS
    //seems to handle the sign bit
    
    tc = ((tc<<18)>>18); 
    
	server.log((1.0* tc/4.0));//This is the temperature in C
    
}

return tc; //will have to divide it by 4 in the recieving code

}
`

PS

I get occasional ground-short errors that I cannot explain even with the thermocouple in open air. I am just going to train my code to ignore those results. I looked at the spi signals on a scope and they look clean. I am using the board from Adafruit

Working on attaching (2) T-Couples to an Imp using the code from above. Thanks to everyone who posted, it is really helping me understand SPI. I’m sending the data to Xively.com as well…I’ll update when I have two working. Current code:
`
// Read data from MAX31855 chip on Adafruit breakout boards

// pins:
// imp 2 DO
// imp 5 CLK
// imp 7 CS

//Configure Pins

hardware.spi257.configure(MSB_FIRST | CLOCK_IDLE_LOW , 1000);
hardware.pin7.configure(DIGITAL_OUT); //chip select

// Output structure for sending temperature to server
local tempOut = OutputPort(“Temperature (F)”, “number”);
local tempOutStr = OutputPort(“Temperature (F)”, “string”);

//Define functions
function readCelcius(){

hardware.pin7.write(0); //pull CS low to start the transmission of temp data
	
	//0[31..24],1[23..16],2[15..8],3[7..0]
    local temp32=hardware.spi257.readblob(4);//SPI read is totally completed here

hardware.pin7.write(1); // pull CS high
local tc = 0;
if ((temp32[1] & 1) ==1){
	
    //Error bit is set
	
	local errorcode = (temp32[3] & 7);// 7 is B00000111
	
	if (errorcode>0){
		
		//One or more of the three error bits is set
		//B00000001 open circuit
		//B00000010 short to ground
		//B00000100 short to VCC
		
		switch (errorcode){
        
        case 1:
		    
            server.log("TC open circuit");
		    break;
		
		case 2:
        
            server.log("TC short to ground");
		    break;
        
        case 3:
        
            server.log("TC open circuit and short to ground")
            break;
		
		case 4:
        
            server.log("TC short to VCC");
		    break;
		
		default:
        
            //Bad coding error if you get here
		    break;
		}
		
		TCErrCount+=1;
		//if there is a fault return this number, or another number of your choice
		 tc= 67108864; 
	}
    else
    {
         server.log("error in SPI read");
    }
    
} 
else //No Error code raised
{
	local highbyte =(temp32[0]<<6); //move 8 bits to the left 6 places
	
    //move to the right two places, lowing two bits that were not related
	local lowbyte = (temp32[1]>>2);		
	tc = highbyte | lowbyte; //now have right-justifed 14 bits but the 14th digit is the sign
     
	//Shifting the bits to make sure negative numbers are handled
    
    //get the sign indicator into position 31 of the signed 32-bit integer
	
    //Then, scale the number back down, the right-shift operator of squirrel/impOS
    //seems to handle the sign bit
    
    tc = ((tc<<18)>>18); 
    server.log("Celcius");
	local celcius = (1.0* tc/4.0);
    server.log(celcius);//This is the temperature in C
    local farenheit = (((celcius*9)/5)+32);
    server.log("Farenheit");
    server.log(farenheit);//This is the temperature in F
        // emit values to our output ports
    tempOut.set(farenheit);
    tempOutStr.set(format("%.01f", farenheit));
}

//return tc; //will have to divide it by 4 in the recieving code
imp.wakeup(5, readCelcius);

}

imp.configure(“spi1”, [], [tempOut, tempOutStr]);

readCelcius();
`