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?




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);}
Link to comment
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:



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 );




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


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:




Link to comment
Share on other sites


This topic is now archived and is closed to further replies.