Disabling SPI in software (temporarily)

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

Hi guys

 

I've been trying to find a way of temporarily disabling SPI in at ATMEGA 328P-PU.

What I want to do is have the AVR boot, disable SPI, do a level check on the SS (PB2) pin (usually used by SPI), then re-enables SPI.

I don't want to disable SPI permanently (so cannot use the fuse).  Looking through the data sheet I cannot seem to find a way of doing this. Does anyone know if this is possible?

 

This topic has a solution.
Last Edited: Sun. Oct 4, 2015 - 02:19 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Disabling spi is just clearing the spi enable bit. But are you wanting to disable ISP? That's what the fuse does.

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

Just temporarily - enough time to use one of the SPI pins for a level check and then return SPI back to normal. I'm using SPI to program the AVR which is why it cannot be disabled permanently.

So... If I clear the SPI enable bit, that doesn't actually set the fuse though (I assume not)?

I'm having to do this because I've run out of usable pins and I only need one more :).

Last Edited: Sun. Oct 4, 2015 - 12:19 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Rubbish. The SPI is only enabled when you set the SPE bit in SPCR. Until you do this, the SS pin is regular GPIO.

You are confusing external ISP programming with the regular runtime SPI peripheral.
The serial ISP can only be disabled by HVPP. I.e. by clearing the fuse.

David.

Last Edited: Sun. Oct 4, 2015 - 12:34 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Tried the following, but still detecting LOW on PB2 even though I've pulled that pin up with a 10K resistor

 

	#define bit_set(p,m) ((p) |= (m))
        #define bit_clear(p,m) ((p) &= ~(m))
	
	
	bit_clear(SPCR, BIT(6));
	bit_clear(DDRB, BIT(2));
	if (bit_get(PORTB, BIT(2))==1) displaytext("high");
	else displaytext("low");	
	bit_set(SPCR, BIT(6));

[edit]

 

We posted at the same time, David.

That's fine, but I cannot find any other real reason why I cannot use the that pin. It always detects as low no matter what I connect it to. I've tried multiple '328s and if I tried the same code on another pin (PB0 for instance) it works fine.

I'm happy to be corrected if I have gotten something wrong.

 

[edit 2]

 

I might see if I can change SPI to MASTER mode and see if it lets me use it then... I'll let you guys know how it goes.

 

[edit 3]

 

Nope. *sigh*

Last Edited: Sun. Oct 4, 2015 - 01:45 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Doh. Helps if I use PINB, not PORTB.

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

Most every 'freak here has uttered that "Doh!" (or another fitting audible facepalm). For the exact same reason you did. Welcome to a big club! :-)

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

What confuses me is how it worked for other pins, with PB2 being the only exception.  PORTB is PORTB, regardless of which pin is masked off.

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.