How to use ATtiny2313 USI

I want to use the Universal Serial Interface (USI) in an ATtiny2313, in three-wire mode, specifically SPI mode 0. It'll be operating as master, and only concerned with data output. There are three options for clocking the interface: external, software, or Timer0 overflow. The examples given in the datasheet use software clocking. I can probably make that work, by inserting NOPs to achieve the clock rate I want. But the Timer0 option seems more elegant. Wondering if anyone has used that or can point me to a resource.

I have a couple questions that I can't find answers to in the datasheet or elsewhere.

  1. The datasheet says SPI modes 0 and 1 are supported, but I'm not clear whether that applies to all of the clocking options. There is only one setting for the software (USICLK) strobe option, and one for the Timer0 overflow option. Should I assume these are SPI mode 0?

  2. When using the Timer0 clock option, what stops the clock? I assume the timer keeps running. Seems like some mechanism would be needed for the USI module to stop clocking the USCK pin once a byte is sent. I could see stopping the clock when the USIOIF bit in the USISR register gets set, but the datasheet doesn't mention anything like this. So I'm wondering whether I have to do something to stop the clock?

I'm using WinAVR and am kind of new to this level, but have had some success with Arduino. Thanks in advance!

I can't extract any information from the datasheet regarding what might stop the timer. The diagrams don't tell for sure. Personally I'm not too happy with the example code as well. I only managed to get one of the examples working in C, which can be found in the arduino playground wiki.

uint8_t spi_transfer(uint8_t data) {
  USIDR = data;
  USISR = _BV(USIOIF); // clear flag

  while ( (USISR & _BV(USIOIF)) == 0 ) {
   USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USICLK)|(1<<USITC);
  }
  return USIDR;
}

madworm:
I can't extract any information from the datasheet regarding what might stop the timer. The diagrams don't tell for sure. Personally I'm not too happy with the example code as well. I only managed to get one of the examples working in C, which can be found in the arduino playground wiki.

Thanks for your reply, glad to know it's not just me XD I am also working in C, and came up with something very similar that does indeed work, however this is the equivalent of the example in the datasheet, i.e. software clocking. Difficult to determine the clock frequency. Of course it could be estimated by looking at the generated assembly language, etc. The SPI module in the ATmega328P is much nicer. But I may play with the ATtiny2313 a bit more.