Why bit-bang SPI?

Go To Last Post
17 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Looking at the firmware for various AVR ISPs based on tiny85, tiny84, etc., I notice that they all seem to bit-bang the SPI communication with the target.

 

Is there a particular reason for this?  The MCUs have USI, so why not use that

 

Martin

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

From datasheet:

The USI three-wire mode is compliant to the Serial Peripheral Interface (SPI) mode 0 and 1, but does not have the
slave select (SS) pin functionality.

Mode 3 is quite common, but not supported.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0



various AVR ISPs based on tiny85, tiny84,

Why would you build a programmer using such a low end AVR?  

Here is an example of a fairly versatile AVR programmer

 

Even so, there is nothing on here expensive, so you can still have a low-cost programmer.

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Welcome to AVRFreaks!

 

Datasheet for '85 does show only ASM solution, for speed, but you may try C-solution if you like.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ah, I didn't know about the modes!

 

And ISP corresponds to mode 3?

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Mabartibin wrote:
And ISP corresponds to mode 3

 

You should stop call it ISP, for it is SPI.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ISP on AVR is mode #0

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

grohote wrote:
You should stop call it ISP, for it is SPI
don't agree. ISP is In System Programming and just happens to use an interface that looks a lot like SPI. 

 

As for why choose a low end Tiny. Surely it's cost. There can't be a lot of profit margin in a USBAsp but there'd be even less for each ¢ beyond the price of the Tiny they use if a larger/more expensive AVR were used. Just a shame the cheap "Tiny" s (until recently) didn't have real SPI to make implementing ISP easier. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Bit banging the SPI gives you speed and flexibility.  Even the USI is essentially bit-banged.   The USI data register is loaded with the 8-bit data value. It's waiting for 8 pulses: 8 up edges and 8 down edges alternated.   The USI control register is written a 1 on the clock-strobe bit 16 times by the main code.  Eight bits are shifted out of the data register and 8 are shifted in from the Slave.  When the 16 strobes are made by 16 consecutive assembler SBI instructions, then you have SPI running at system clock divided by 2.

 

  What makes the SPI peripheral different is that you can configure the SPI clock at a fraction of the system clock. Then in one instruction, write the data  and have the SPI process start running in the background.  When the eight bits are finished, the SPI triggers an interrupt.  The IRQ will either load the next byte to be transmitted, or shut down the SPI (and interrupt).  This doesn't make much difference when the SPI is running at SysClock/2 or /4.   But if your SPI transfer code is polling in a loop to see if the process is complete, then the CPU can be doing other things instead during this interval.   And with hardware SPI (but not the USI) you can easily do mode 3.  

 

But if your tiny=class CPU doesn't have a hardware SPI peripheral, then just bit-bang the sucka.  It doesn't take much time or flash space.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for all the comments!

 

OK, so to summarise:

  • According to David, ISP requires using mode #0 (confirmed by looking at non-tiny USBASP code)
  • According to N Winterbottom (and the datasheet), the tinies’ USI supports mode #0

 

So then it should be possible to use the USI for ISP?

 

Why do I bother? – all the usual excuses apply.

  • Have plenty of tiny84 lying around looking for a job.
  • Want to learn.
  • Dislike the fact that many cheap ready made ISPs have no level shifters.
  • No experience with SMD soldering
  • Not willing to design and order custom PCBs yet

 

Actually cost is not an issue since I won’t be making more than one.  I might use a mega328, that has proper SPI, but it seems like a bit of an overkill, no?

 

Martin

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As for why choose a low end Tiny. Surely it's cost.

That is very likely true.  But you can just go and buy one for $2-$5 ready to use, no need to make one. If you are designing a new programmer, with all the effort involved, I wouldn't start with a hobbled bottom-barrel device (otherwise you'd be competing with the $3 guys).  Then you can add your differentiating features (since there already a wide programmer variety available). Maybe this is more of a curiosity question, rather than the beginning of a new design.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Sat. Oct 9, 2021 - 04:30 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why make life difficult for yourself (unless you are making 5,000,000 units and every ¢ counts)? If a 328 makes the one off project easier then use it. Probably a lot to be learned by looking at how the ArduinoISP app does it too. (oh and Dean's ISP stuff in Buttload/LUFA too) 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Simonetta wrote:

Bit banging the SPI gives you speed and flexibility.  Even the USI is essentially bit-banged.   The USI data register is loaded with the 8-bit data value. It's waiting for 8 pulses: 8 up edges and 8 down edges alternated.   The USI control register is written a 1 on the clock-strobe bit 16 times by the main code.  Eight bits are shifted out of the data register and 8 are shifted in from the Slave.  When the 16 strobes are made by 16 consecutive assembler SBI instructions, then you have SPI running at system clock divided by 2.

 

The code I’m looking at (tiny ports of USBASP, usbtinyisp,…) does the shifting of the data and setting the ports ‘manually,’ which means max speeds waaaay less than 1/2 of the system clock.  That’s why I’m asking.

 

Simonetta wrote:

  What makes the SPI peripheral different is that you can configure the SPI clock at a fraction of the system clock. Then in one instruction, write the data  and have the SPI process start running in the background.  When the eight bits are finished, the SPI triggers an interrupt.  The IRQ will either load the next byte to be transmitted, or shut down the SPI (and interrupt).  This doesn't make much difference when the SPI is running at SysClock/2 or /4.   But if your SPI transfer code is polling in a loop to see if the process is complete, then the CPU can be doing other things instead during this interval.   And with hardware SPI (but not the USI) you can easily do mode 3.  

 

But if your tiny=class CPU doesn't have a hardware SPI peripheral, then just bit-bang the sucka.  It doesn't take much time or flash space.

 

According to app note AVR319, the USI can be clocked from Timer 0 Compare/Match.  I don’t think the timer is used for anything else in this code.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Go on.  Regular USBASP boards use a mega8.   Everything works fine.   e.g. hardware SPI for regular SCK speeds and software SPI for very slow SCK speeds.   As well as the software V-USB comms.

It also means that you can add extra functionality like TPI or 8051 support without worrying about limited Flash.   Or even use the USBASP pcb for different V-USB projects.

 

The USBtiny originally appeared with a tiny2313.   It was a very tight squeeze to get regular ISP functionality into 2kB.   In fact the original firmware "broke" with later avr-gcc releases.

 

Yes,  you can re-build USBASP firmware with different targets  or USBTINY with different targets.   A tiny84 or tiny85 has plenty of Flash for USBASP or USBTINY firmware.   You obviously need to know what you are doing when you modify the original source code for different targets etc.

 

Or simply buy a Chinese USBASP board that works straight out of the box.

 

David.

 

Last Edited: Sat. Oct 9, 2021 - 04:53 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:

As for why choose a low end Tiny. Surely it's cost.

 

… Maybe this is more of a curiosity question, rather than the beginning of a new design.

 

Yes, exactly, this is purely for fun :-)

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:

Go on.  Regular USBASP boards use a mega8.   Everything works fine.   e.g. hardware SPI for regular SCK speeds and software SPI for very slow SCK speeds.

 

Ooooh, now I see: the branch of the USBASP code for the tinies has the hardware SPI code #if-ed out, presumably since it doesn’t compile.  That’s why it only works in the slow modes.  Probably the author lost interest when that worked and never bothered to port the code for the regular speeds.

 

Quote:

Yes,  you can re-build USBASP firmware with different targets  or USBTINY with different targets.   A tiny84 or tiny85 has plenty of Flash for USBASP or USBTINY firmware.   You obviously need to know what you are doing when you modify the original source code for different targets etc.

…or learn on the way…

 

Quote:

Or simply buy a Chinese USBASP board that works straight out of the box.

Or use the Arduino ISP shield I have.  Not as much fun, and no educational effect.

 

Martin

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

USI looks pretty awful, actually.
Bit banging is much more generic, more flexible, doesn't look like it takes much more code (especially if you want to have controlled bitrates), and isn't enough slower to affect most applications (and especially ISP.)