kk_omnisys

Take over/release SPI using PPS

Recommended Posts

Hello All

 

I'm trying to share the USPI block between different hardware so I've written two functions to take control of the SPI and release it again. But it seems that the release does not work properly. Do I have to add/change something to make the SPI release properly from my assigned pins?

 

Regards 

Kalle

void takeSPI(){	//Relinquish control of PPS pins first	pinModePPS(SDIPIN, LOW);	pinModePPS(SCKPIN, LOW);	pinModePPS(SDOPIN, LOW);		//Assign our spi mode and pins	USPICTL = BIT(SPICP2) | BIT(SPIEN) | BIT(SPIBLOCK) ;		outputPinForFunction(ENC_SDI, IOPIN_USPI_MOSI);	pinModePPS(ENC_SDI, HIGH);	pinMode(ENC_SDI, OUTPUT);	outputPinForFunction(ENC_CLK, IOPIN_USPI_SCK);	pinModePPS(ENC_CLK, HIGH);	pinMode(SCKPIN, OUTPUT);}void releaseSPI(){	//Relinquish control of PPS pins first	pinModePPS(ENC_SDI, LOW);	pinModePPS(ENC_CLK, LOW);	USPICTL=BIT(SPICP0)|BIT(SPICPOL)|BIT(SPISRE)|BIT(SPIEN)|BIT(SPIBLOCK)|0x0E;		outputPinForFunction( SDIPIN, IOPIN_USPI_MOSI );	pinModePPS(SDIPIN,HIGH);	pinMode(SDIPIN,OUTPUT);	outputPinForFunction( SCKPIN, IOPIN_USPI_SCK);	pinModePPS(SCKPIN,HIGH);	pinMode(SCKPIN,OUTPUT);	pinModePPS(CSPIN,LOW);	pinMode(CSPIN,OUTPUT);		inputPinForFunction( SDOPIN, IOPIN_USPI_MISO );	pinMode(SDOPIN,INPUT);}

Share this post


Link to post
Share on other sites

Hmmm, this is a good question. I know that the SPI pins are connected to a multiplexer and when you call the functions above you are moving the pins on the multiplexer. And calling the functions again should move the pins to another location on the multiplexer. So how do you move them back to their initial state? What is the initial state?

 

I'm looking at the GPIO code:

https://github.com/alvieboy/ZPUino-HDL/blob/master/zpu/hdl/zpuino/zpuino_gpio.vhd

 

It looks like PPS comes in as spp_data (line 62) and is controlled by input_mapper_q (line 111). So I think if we set input_mapper_q to its initial value it should release the pins. I'm not sure about this but I think the initial value is 0. So Alvie is the real authority on this but I think the following would release the PPS pin by moving it to a non connected location:

 

outputPinForFunction( SDIPIN, 0 );

 

 

UPDATE:

Nope, just realized that according to a top level VHDL file such as this one there is something connected to 0:

https://github.com/alvieboy/ZPUino-HDL/blob/master/zpu/hdl/zpuino/boards/papilio_one/s3e500/variants/vanilla/papilio_one_top.vhd

Look at line 627 and 634.

 

So maybe doing this will be better:

outputPinForFunction( SDIPIN, 127 );

 

This is of course just guesswork on my part, Alvie will know the right way to do this. :)

 

There is more info here:

http://www.papilio.cc/index.php?n=Papilio.ZPUinoUserGuide#PPS

 

Jack.

Share this post


Link to post
Share on other sites

 

I'm trying to share the USPI block between different hardware

 

Hey,

 

what you mean exactly by "between different hardware" ? You are using PPS to remap the HW SPI into another HW device ?

Share this post


Link to post
Share on other sites

Hey,

 

what you mean exactly by "between different hardware" ? You are using PPS to remap the HW SPI into another HW device ?

 

Yes, I'd like to move the pins for the USPI to some other pins temporarily and then release them back to the original pins.

Share this post


Link to post
Share on other sites

Ok.

 

For outputs, setting pinModePPS to "LOW" should suffice.

 

For inputs, you will need to map them to any pin.

 

So, setting the old values again must work. Do you know if the problem is with outputs or inputs ?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now