PIC32 SPI

Create new topics, add your comment, remark, bugs...
Post Reply
konsgn
Posts: 5
Joined: Mon Jan 03, 2011 3:57 pm

PIC32 SPI

Post by konsgn » Wed Jan 05, 2011 4:01 pm

Greetings,

I was wondering what the progress was on getting SPI functionality for the PIC32MX line?
What about using the C function call to call working C32 compiler code that would handle the SPI communication?
Are there any workarounds for getting SPI working?

Thank you,
Konsgn

resodad
Posts: 71
Joined: Tue Jul 07, 2009 1:06 pm
Location: West Point, New York

Re: PIC32 SPI

Post by resodad » Wed Jan 12, 2011 5:33 pm

Hi Lubin,
I am working with Konsgn to implement a SD card data logger on Cerebot 32MX4 (PIC32MX460F512L). I ran Microchip's example C program "MDD File System-SD Card" successfully. The setup uses SPI to read / write.

Now to port to Simulink. We would like to have a Simulink block to write data over SPI to the SD card.

I saw the QEI C-function block example on the forum.
Would it be possible for Konsgn and me to write a c-function block to do SPI?
Is there a better way?

Would you consider developing and publishing the block?

Cheers,
John

matgraci
Posts: 3
Joined: Fri Dec 04, 2009 6:27 pm

Re: PIC32 SPI

Post by matgraci » Tue Jan 18, 2011 10:15 am

Hi.

Does this mean that SPI functionality is not supported for PIC32 uCs with those blocksets?! I can't remember reading this anywhere when buying those blocksets for my PIC32MX360F512l uC! :?:
I noticed that when i insert the SPI Configuration Block that the Fsck can't be counted or something like that. Never figured that this blockset is not supported for that uC. I even noticed that I couldn't compile when using SPI2 even though SPI2 port is available on my uC.

matgraci
Posts: 3
Joined: Fri Dec 04, 2009 6:27 pm

Re: PIC32 SPI

Post by matgraci » Tue Jan 18, 2011 11:16 pm

I just wrote C function that is supposed to write data to SPI port. There is one more function that sets SPI configuration. I set my model so that config function is called only once.

Code: Select all

void configSPI() {
	static int first_pass = 1;
	int rData;
	if (first_pass) {
		first_pass = 0;
		IEC0CLR=0x03800000; // disable all interrupts
		SPI1CON = 0; // Stops and resets the SPI1.
		rData=SPI1BUF; // clears the receive buffer
		IFS0CLR=0x03800000; // clear any existing event
		IPC5CLR=0x1f000000; // clear the priority
		IPC5SET=0x0d000000; // Set IPL=3, Subpriority 1
		IEC0SET=0x03800000; // Enable RX, TX and Error interrupts
		SPI1BRG=0x1; // use FPB/4 clock frequency
		SPI1STATCLR=0x40; // clear the Overflow
		SPI1CON=0x8220; // SPI ON, 8 bits transfer, SMP=1, Master mode
		// from now on, the device is ready to transmit and receive data
	}
}
void sendSPI(char a) {
	IFS0CLR=0x03800000; // clear any existing event
	//rData=SPI1BUF; // clears the receive buffer
	SPI1BUF=a; // transmit an A character
}
void setport(){
	static int i=1;
        TRISD = 0;
	LATD = i;
	i = (i+1)%2;
}
This works fine (since I manage to send and receive some data to my other uC), but the problem is that function for writing to SPI port is called only once, even thought it should be called every 0.5s. I added other C function that lights one LED every 0.5s. I put that LED function and sendSPI function in one enabled subsystem and set all SampleTime-s to -1. So if everything is alright then LED should blink every 0.5s and some data should be sent over SPI port every 0.5s. I'v done that only for debugging. But it didn't happen. LED lighted only once and only one char was transferred over SPI port.

When I commented this line

Code: Select all

SPI1BUF=a
then LED started to blink. Why is that so?! Does anyone have any idea?
Attachments
dsPIC_32MX360F512L_Proba.mdl
(30.64 KiB) Downloaded 1425 times

resodad
Posts: 71
Joined: Tue Jul 07, 2009 1:06 pm
Location: West Point, New York

Re: PIC32 SPI

Post by resodad » Sun Jan 23, 2011 4:19 pm

Hi matgraci,

Thanks for posting, I am trying your model. I am new to this... where did you save your c code? what did you name it? how did you connect it to the model?
This is what I tried: Saved your code as SPIcode.c in the build directory, and entered the name SPIcode.c in the list of additional source files on the custom code page of the Real-Time Workshop page. The build produces a generic error.
matgraci wrote:Hi.

Does this mean that SPI functionality is not supported for PIC32 uCs with those blocksets?! I can't remember reading this anywhere when buying those blocksets for my PIC32MX360F512l uC! :?:
See the note: " * PIC 32F
SPI function not suported yet;" under the list of supported chips on page: http://www.kerhuel.eu/wiki/Simulink_-_E ... et_for_PIC.

matgraci
Posts: 3
Joined: Fri Dec 04, 2009 6:27 pm

Re: PIC32 SPI

Post by matgraci » Mon Jan 24, 2011 2:19 pm

Sorry, but i can't include the whole c file that I used because it contains some of the data and other things that I can't share with all of you. I think that you can't compile it because you did not include "pic32mxxx.h" in your .c file, or whatever is the the name of the file that contains names of all of the registers used for addressing ports and other peripherals.

Regarding my problem... When i changed from SPI1 to SPI2 some things started to work, just that I was not able to receive any data, only to send.

konsgn
Posts: 5
Joined: Mon Jan 03, 2011 3:57 pm

Re: PIC32 SPI

Post by konsgn » Wed Apr 27, 2011 9:01 pm

Hi Lubin,

I've been working a lot on getting SD card capability for logging data in Simulink. I have placed all the relevant headers and C files provided by Microchips Application Solution MDD file system in the working directory of the model. I have also removed any nested folders.

The problem I have noticed is that within the code provided by Microchip, they make use of delays that are often over 1ms long. Since we are running on a 1ms timestep on the PIC32MX460F512L, I would like to keep this timestep the same, but temporarily change the running of certain subfunctions to free-running mode. Would this be possible or should I be looking to modify the Delays?

I have posted a .zip with all the relevant files in case that helps.
Attachments
SD Datalogging.zip
Collection of files in Model Directory
(215.02 KiB) Downloaded 1545 times

resodad
Posts: 71
Joined: Tue Jul 07, 2009 1:06 pm
Location: West Point, New York

Re: PIC32 SPI

Post by resodad » Wed May 11, 2011 3:07 pm

Lubin and all,
konsgn wrote: I've been working a lot on getting SD card capability for logging data ...
I have posted a .zip with all the relevant files in case that helps.
please comment on the method konsgn uses to write to an SD card. Is there a better way?
John

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest