Programming ChipKit Pic32 with MPLAB X

User avatar
majenko
Site Admin
Posts: 2164
Joined: Wed Nov 09, 2011 7:51 pm
Location: UK
Contact:

Re: Programming ChipKit Pic32 with MPLAB X

Post by majenko » Thu Jun 26, 2014 9:41 am

SPI2 should be the one to choose - that is presented on pins 11-13, (clock on 13).

Personally I have never really got on with plib and prefer to directly manipulate the registers.

You should have access to the SPI2CON register without problems. The p32xxxx.h file should automatically include the correct header for your selected chip which will provide access to all those registers.
Why not visit my shop? http://majenko.co.uk/catalog
Universal IDE: http://uecide.org
"I was trying to find out if it was possible to only eat one Jaffa Cake. I had to abandon the experiment because I ran out of Jaffa Cakes".

rascalito
Posts: 7
Joined: Sun Jun 22, 2014 8:32 am

Re: Programming ChipKit Pic32 with MPLAB X

Post by rascalito » Thu Jun 26, 2014 10:16 am

Hello!

Thanks for your useful hints. Great! I'm going to write my own brew of SPI
initialisation. It may take time since it's on my spare time, but it will definitely
help me to understand the internals even if my implementation is not as good
as plib.

Thanks again!

Pascal

PS: Indeed, I could access SPI2CON and SPI1CON as well. I don't know what I did
this morning, probably a typo of some kind.

User avatar
majenko
Site Admin
Posts: 2164
Joined: Wed Nov 09, 2011 7:51 pm
Location: UK
Contact:

Re: Programming ChipKit Pic32 with MPLAB X

Post by majenko » Thu Jun 26, 2014 11:30 am

SP1ICON? ;)

By the way, you also have access to the SPI2CONbits.* structures. Here's my initialization code for an I2C codec chip running on SPI1 of a PIC32MX470F512L chip:

Code: Select all

    // Configure the SPI port to take its clock from the REFCLK and
    // operate in I2S mode
    SPI1CONbits.ON = 0;
    SPI1CONbits.FRMEN = 0;
    SPI1CONbits.FRMSYNC = 0;
    SPI1CONbits.FRMPOL = 0;
    SPI1CONbits.MSSEN = 0;
    SPI1CONbits.FRMSYPW = 0;
    SPI1CONbits.FRMCNT = 0b000;
    SPI1CONbits.MCLKSEL = 1; // REFCLK
    SPI1CONbits.SPIFE = 0;
    SPI1CONbits.ENHBUF = 1;
    SPI1CONbits.SIDL = 0;
    SPI1CONbits.DISSDO = 0;
    SPI1CONbits.MODE32 = 0; // 16 bit per channel
    SPI1CONbits.MODE16 = 0; // 16 bit per channel
    SPI1CONbits.SMP = 0;
    SPI1CONbits.CKE = 0;
    SPI1CONbits.SSEN = 0;
    SPI1CONbits.CKP = 0;
    SPI1CONbits.MSTEN = 1; // Master mode
    SPI1CONbits.DISSDI = 1; // No SDI for now
    SPI1CONbits.STXISEL = 0b11;
    SPI1CONbits.SRXISEL = 0b00;

    SPI1CON2bits.SPISGNEXT = 0;
    SPI1CON2bits.FRMERREN = 0;
    SPI1CON2bits.SPIROVEN = 0;
    SPI1CON2bits.SPITUREN = 0;
    SPI1CON2bits.IGNROV = 0;
    SPI1CON2bits.IGNTUR = 1;
    SPI1CON2bits.AUDEN = 1; // Enable audio mode
    SPI1CON2bits.AUDMONO = 0; // Stereo audio
    SPI1CON2bits.AUDMOD = 0b00; // I2S mode

    SPI1BRG = 3; // 192000Hz SPI clock = 4 bytes per sample @ 48000sps

    SPI1CONbits.ON = 1; // Turn it on
Some bits, of course, will be different for your chip (no REFCLK for example) but you can see how much easier to read that is than setting bits with |= etc - but it is more long winded.

For your SPI you only need to really deal with the clock polarity and sample edge, the master mode, bit width, clock source, and turn it on. Most things as default are fine for basic SPI work.

Just remember to make turning it on your last operation ;)
Why not visit my shop? http://majenko.co.uk/catalog
Universal IDE: http://uecide.org
"I was trying to find out if it was possible to only eat one Jaffa Cake. I had to abandon the experiment because I ran out of Jaffa Cakes".

Max44
Posts: 24
Joined: Fri Apr 18, 2014 12:57 am
Location: North Dartmouth, Massachusetts

Re: Programming ChipKit Pic32 with MPLAB X

Post by Max44 » Sat Jun 28, 2014 6:18 pm

Pascal,

I may have missed it, but I didn't see where you're configuring your I/O pins. Although I assume if you have LEDs blinking it must have been done somewhere. Even for SPI, you need to define the associated TRIS port register for input (default) or output.

I'm working with a WF32 in MPLAB X. The WiFi module works over a SPI port. The pins are configured as (for example):

Code: Select all

//----------------------------
// MRF24WG0MA/B WiFi I/O pins
//----------------------------
//
// The WF32 MRF24G pins are
//
//      CS          RF12    MCU pin 40      ChipKit pin 54
//      SDI         RF5     MCU pin 50      ChipKit pin 56
//      SCK         RF13    MCU pin 39      ChipKit pin 53
//      SDO         RF4     MCU pin 49      ChipKit pin 55
//      RESET       RG0     MCU pon 90      ChipKit pin 59
//      INT         RA15    MCU pin 67      ChipKit pin 57
//      HIBERNATE   RG1     MCU Pin 89      ChipKit pin 58
//


#define WF_CS_TRIS			(TRISFbits.TRISF12)
#define WF_CS_IO			(LATFbits.LATF12)
#define WF_SDI_TRIS			(TRISFbits.TRISF5)
#define WF_SCK_TRIS			(TRISFbits.TRISF13)
#define WF_SDO_TRIS			(TRISFbits.TRISF4)
#define WF_RESET_TRIS                   (TRISGbits.TRISG0)
#define WF_RESET_IO			(LATGbits.LATG0)
Then, within your initialization routine, you would do something like:

Code: Select all

    // Set up the SPI module on the PIC for communications with the MRF24W
    WF_CS_TRIS     = 0;     // Drive SPI MRF24W chip select pin
    WF_CS_IO       = 1;

Maybe this will also help when you get around to those SPI initialization routines!

Max

svs_03
Posts: 2
Joined: Tue May 31, 2016 11:51 am

Re: Programming ChipKit Pic32 with MPLAB X

Post by svs_03 » Tue May 31, 2016 4:36 pm

I tried with one of the above solutions by adding avrdude.exe and .conf file . also I added linker files for max 32 board. The fallowing are the errors it is showing . how to proceed????

Post Reply