Page 1 of 1

Moving SPI to alternate port

Posted: Wed Mar 02, 2011 4:02 am
by petesmart
Hi Ben,
to make it easier thought I would start up a specific topic around the question that I posted (and you kindly responded to) last night. Quote below:

Re: SPI Master
by Benj ยป Tue Mar 01, 2011 11:25 am

Hello Pete,

The Flowcode I2C component allows you to use it in hardware or software mode. The software mode does not require a MSSP peripheral so you can basically move the component anywhere you like on the chip.

SPI can also be bit banged quite easily but the SPI legacy component does not currently support this mode of operation.Regards
Ben Rowland
Matrix Multimedia
Benj
Matrix Staff

Posts: 3923
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix Multimedia Ltd
Has thanked: 14 times
Have thanks: 38 times
Website
I think the best approach would be to move the CAN module from Port C to an alternate port. Is this simply a matter of of editing the can module in the custom code facility and exchanging Port C for example to Port B, or is there a quick way to config.

As I have not had any experience with bit banging...can you point me to info pls...

thanks for your quick response.

All the best

Pete

Re: Moving SPI to alternate port

Posted: Wed Mar 02, 2011 11:03 am
by Benj
Hello Pete,

Ok to modify the CAN component should be fairly straightforward. There are two sections of code you will need to modify. The same should go for just about any SPI component so this technique could be used for other components too.

The first function we are going to edit is:
SPI_Init

All we need to do here is to initialise the port direction registers and the port registers, all other code in this function can be removed as we are no longer using hardware SPI.

Code: Select all

	//initialise spi
	set_bit(trisb, 0);   //SDI pin
	clear_bit(trisb,1);  //SDO Pin
	clear_bit(trisb,2);   //SCK Pin
	clear_bit(trisb,3);  //CS Pin
	set_bit(trisb,4);  //INT pin

	//disable the CAN chip select line
	set_bit(portb, 3);
The other function you will need to edit is the SPI_SendByte function. Here is a quick example of code for a bit banged SPI transaction.

Code: Select all

		char idx;
		for (idx = 0; idx < 8; idx++)
		{
			if (test_bit(byte, 7))
				set_bit(portb, 1);		// Set SDO bit
			else
				clear_bit(portb, 1);	// Clear SDO bit

			byte = byte << 1;

			set_bit(portb, 2);			// SCK Output 1

			delay_us(1);	//Added BR 06/10

			if(test_bit(portb, 0))
				byte = byte | 0x01;
			else
				byte = byte & 0xFE;

			clear_bit(portb, 2);		// SCK Output 0
		}
		return byte;

Re: Moving SPI to alternate port

Posted: Thu Mar 03, 2011 7:52 am
by petesmart
Thanks Ben much appreciated... will give it a go...

do i have to do something similar with SDI?

best

Pete

Re: Moving SPI to alternate port

Posted: Thu Mar 03, 2011 11:07 am
by Benj
Hi Pete,

SDI is covered with this section of code. Sorry missed out a comment.

Code: Select all

         if(test_bit(portb, 0))   //Test SDI Input
            byte = byte | 0x01;
         else
            byte = byte & 0xFE;

Re: Moving SPI to alternate port

Posted: Thu Mar 03, 2011 11:19 am
by petesmart
no problem... many thanks for the quick response

I should have read it more carefully for shouting for help... sorry to waste your time