software SPI interfacing

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

Hi guys

I am new to SPI interfacing, so I dig around the internet and find out about it, so here are my question:

if i don't want to use the hardware spi in AVR, can I just simply do it in software like this:

void spi()
{
   CS_PIN=low;                //indicate start

   DATA_OUT_PIN=high/low;     //put data on
   SCK_PIN=high; SCK_PIN=low; //tick the clcok
   data_received=DATA_IN_PIN; //read data
   //repeat until all data is sent/received

   CS_PIN=high;               //indicate stop
}

provided the interrupt is disable during the this process.

to me, it looks like the frequency of the clock is not critical important, and I can have different frequency during this process.

I am just curious.

PS: sorry if my code is too hard to read, I haven't really adapt to a good format yet

Zhuhua Wu - Electronic Engineering Student

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

Sure. Thats the way we had to do it before it was common peripheral hardware.

I would be careful about WHEN you change the clock state. There are several options about when to change the clock and when to read the data or put new data on the MOSI line.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

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

sweet, thanks, so I can connect the SPI device to any pins, awesome.

Zhuhua Wu - Electronic Engineering Student

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

If you use Codevision it provides the bit banged SPI code for you.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

I timed writing bytes using the hw spi and the sw spi, and the sw spi is only about Half Fast.

Imagecraft compiler user

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

If using GCC I have found this routine from E L Chan (as part of PetitFs) works very well:

;---------------------------------------------------------------------------;
; Transmit a byte
;
; void xmit_spi (BYTE);

.global xmit_spi
.func xmit_spi
xmit_spi:
	ldi	r25, 8
1:	sbrc	r24, 7		; DI = Bit to sent
	sbi	PORT_DI		; 
	sbrs	r24, 7		; 
	cbi	PORT_DI		; /
	lsl	r24		; Get DO from MMC
	sbic	PIN_DO		; 
	inc	r24		; /
	sbi	PORT_CK		; A positive pulse to SCLK
	cbi	PORT_CK		; /
	dec	r25		; Repeat 8 times
	brne	1b		; /
	ret
.endfunc

There's some clever stuff going on there - he uses R24 as both the output and the input byte at the same time as the GCC ABI says function input/output parameters should be in R24

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

I can't remember off-hand. There are more efficent ways of doing soft SPI in assembler. Cliff's example is about 15 cycles per bit. The hardware SPI peripheral can manage 2 cycles per bit.

Our Scandinavian colleagues are good at these algorithms. I think that they can get soft SPI down to about 8 cycles per bit.

In practice, you only choose software when the hardware is too fast. e.g. in ISP programmers.
Or you wired your pcb wrong !

David.

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

Quote:

If you use Codevision it provides the bit banged SPI code for you.

??? Where do I find that, js?

Bit-banged I2C? Check.
Bit-banged DS1302? Check.
1wire? Check.
SPI? ???

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

I can see "Bit banged peripherals" and there is a driver for the DS1302, maybe not exactly full SPI.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly