spi fundamentals question

Hi everyone. This is my first post. I have been dissecting some hacked code for a digital thermocouple reader. The code is very messy and has numerous flaws i.e. the author seems to be unaware of how signed numbers are read among other things. So I am taking a conservative approach to trusting all aspects of the code. I have a general understanding of spi communication and I am aware that there is an spi library that can be accessed to make this code easier. However since going through the code I have become interested on how one would read spi data through programmed logic. Here is one of the functions in the code that compiles the data sent from the thermocouple reader into a 32 bit word.

uint32_t Adafruit_MAX31855::spiread32(void) { 
  int i;
  uint32_t d = 0;

  digitalWrite(sclk, LOW);
  //digitalWrite(sclk, HIGH);
  _delay_ms(1);
  digitalWrite(cs, LOW);
  _delay_ms(1);

  for (i=31; i>=0; i--)
  {
    digitalWrite(sclk, LOW);
    _delay_ms(1);
    d <<= 1;
    if (digitalRead(miso)) {
      d |= 1;
    }

    digitalWrite(sclk, HIGH);
    _delay_ms(1);
  }

  digitalWrite(cs, HIGH);

  return d;
}

I commented my own thoughts in the code. I am thinking the clock should be initialized high as the thermocouple reader has an actively low clock. If the clock is initialized low I am thinking the first instance of the for loop will fail and subsequently all bits will be shifted to the right one place with the last bit totally being ignored. I could very well be mistaken perhaps I am off about the first read requiring a clock change from high to low. I would appreciate any thoughts from anyone who agrees or who can teach me something new. Thanks

That code looks like it is bit-banging something that could be more easily handled with hardware. The 328P has a hardware SPI port, so to make a transfer all you have to do is load the byte into the SPDR register and wait for the SPIF flag to let you know it is done.

I understand there are much more efficient ways to handle this. My question is simply based on curiosity at this point and not on function. I appreciate your reply a lot that being said

Well the question about the clock line being high or low depends on the device you're talking about. Arduino can go both ways, but the data sheet on the thermocouple device might dictate otherwise. It will also tell you if that is edge triggered or level triggered which will tell you if it is a good idea to leave the clock sitting in the active state. But you're probably right that it should start high if it is an active low clock.

The only other thing about that code is that it is going to be super slow. You're sending 1 bit with 4 milliseconds of delay if I'm reading that right. That's only 250 bits per second. The hardware SPI runs at thousands of times that speed.

That may even be the reason this person was bit-banging it. If that device needs really slow SPI, and some do, then I don't know that you could get the hardware SPI to go that slow.