Hot!PIC32 SPI

Author
electronicsArtist
Junior Member
  • Total Posts : 120
  • Reward points : 0
  • Joined: 2017/02/17 15:55:56
  • Location: 0
  • Status: offline
2017/11/27 14:01:23 (permalink)
0

PIC32 SPI

Hello folks trying to implement pic32 spi master mode with no success so far
can anyone make any suggestions 
much appreciated thanks 
code is below
using PIC32MX130F256B

 
/* ************************************************************************** */
/** Descriptive File Name
@Company
Company Name
@File Name
filename.c
@Summary
Brief description of the file.
/* PIC32MX130256B
SPI MASTER MODE*/
#include "xc.h"
#pragma config PMDL1WAY = OFF // Peripheral Module Disable Configuration (Allow multiple reconfigurations)
#pragma config IOL1WAY = OFF // Peripheral Pin Select Configuration (Allow multiple reconfigurations)
// DEVCFG2
#pragma config FPLLIDIV = DIV_1 // PLL Input Divider (1x Divider)
#pragma config FPLLMUL = MUL_24 // PLL Multiplier (24x Multiplier)
#pragma config FPLLODIV = DIV_1 // System PLL Output Clock Divider (PLL Divide by 1)
// DEVCFG1
#pragma config FNOSC = FRC // Oscillator Selection Bits (Fast RC Osc (FRC))
#pragma config FSOSCEN = OFF // Secondary Oscillator Enable (Disabled)
#pragma config IESO = OFF // Internal/External Switch Over (Enabled)
#pragma config POSCMOD = OFF // Primary Oscillator Configuration (Primary osc disabled)
#pragma config OSCIOFNC = ON // CLKO Output Signal Active on the OSCO Pin (Enabled)
#pragma config FPBDIV = DIV_2 // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/2)
#pragma config FCKSM = CSDCMD // Clock Switching and Monitor Selection (Clock Switch Disable, FSCM Disabled)
#pragma config WDTPS = PS1048576 // Watchdog Timer Postscaler (1:1048576)
#pragma config WINDIS = OFF // Watchdog Timer Window Enable (Watchdog Timer is in Non-Window Mode)
#pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls))
#pragma config FWDTWINSZ = WINSZ_25 // Watchdog Timer Window Size (Window Size is 25%)
// DEVCFG0
#pragma config JTAGEN = OFF // JTAG Enable (JTAG Port Enabled)
#pragma config ICESEL = ICS_PGx1 // ICE/ICD Comm Channel Select (Communicate on PGEC1/PGED1)
#pragma config PWP = OFF // Program Flash Write Protect (Disable)
#pragma config BWP = OFF // Boot Flash Write Protect bit (Protection Disabled)
#pragma config CP = OFF // Code Protect (Protection Disabled)
#define FCY 4000000
#include "xc.h"
#include <stdio.h>
#include <proc/p32mx130f256b.h>
#include <sys/attribs.h>
#include "config.h"
void config_spi (void);
int writeSPI1( int i);
char rData;
int main(){
config_spi ();
int i;

while(1){
rData= writeSPI1('a'); // transmit an A character
for(i=0;i<1000000;i++);

}
return 0;
}
void config_spi (void){
ANSELBCLR = 0xffff;
ANSELACLR = 1;
TRISBCLR = 0xffff;
TRISBSET = 0b01;


SDI1R = 0b0000; //use RPA0 for serial data in
RPB2R = 0b0011; //use RPB2 for serial data out
IEC0CLR=0x03800000; // disable all interrupts
SPI1CON = 0; // Stops and resets the SPI1.
rData=SPI1BUF; // clears the receive buffer
SPI1BRG=1; // use FPB/4 clock frequency
//SPI1STATCLR=0x40; // clear the Overflow
SPI1CON=0x08120; // SPI ON, 8 bits transfer, SMP=1, Master mode
// from now on, the device is ready to transmit and receive data
}
int writeSPI1( int i)
{
SPI1BUF = i; // write to buffer for TX
//while( !SPI2STATbits.SPIRBF); // wait for transfer complete
return SPI1BUF; // read the received value
}//writeSPI2

#1

12 Replies Related Threads

    Akshayy
    Super Member
    • Total Posts : 251
    • Reward points : 0
    • Joined: 2016/07/29 06:35:35
    • Location: 0
    • Status: offline
    Re: PIC32 SPI 2017/12/01 04:38:18 (permalink)
    5 (1)
    u have to wait till transfer completes.Uncomment it in writeSPI1.
    #2
    Serafino Convertini
    Starting Member
    • Total Posts : 77
    • Reward points : 0
    • Joined: 2015/04/15 03:30:48
    • Location: 0
    • Status: offline
    Re: PIC32 SPI 2017/12/01 07:53:27 (permalink)
    5 (1)
    Try this
    void SPI1_Exchange()
    {
        unsigned int spi_counter = 0;
        myWriteBuffer[0] = something_0;
        myWriteBuffer[1] = something_1;
        myWriteBuffer[2] = something_2;
        myWriteBuffer[3] = something_3;
        myWriteBuffer[4] = something_4;
        
        ClearChipSelect(); // Set to 0your CSn pin
        for(spi_counter = 0; spi_counter < SPI_BUFFER_SIZE; spi_counter++)
        {
            SPI1BUF = myWriteBuffer[spi_counter];
            while(SPI1STATbits.SPIRBE == true);
            myReadBuffer[spi_counter] = SPI1BUF;
        }
        SetChipSelect(User); // Set to 1 your CSn pin
    }

     
    You can customize this code as you prefer. Hope it could help you
    #3
    electronicsArtist
    Junior Member
    • Total Posts : 120
    • Reward points : 0
    • Joined: 2017/02/17 15:55:56
    • Location: 0
    • Status: offline
    Re: PIC32 SPI 2017/12/01 20:04:39 (permalink)
    0
    thanks for responding all
    Serafino isnt the SS driven by the master automatically 
    can i play with the chip select aka SS in software?
    #4
    qɥb
    Monolothic Member
    • Total Posts : 1231
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: online
    Re: PIC32 SPI 2017/12/01 22:32:40 (permalink)
    5 (1)
    semed
    Serafino isnt the SS driven by the master automatically

    No.
    Only in "framed" mode, wchich is genrally onlyt used in some audio applications.
     

    can i play with the chip select aka SS in software?

    Yes, but you do not HAVE to use the SS pin. That's for when you are acting as a slave, or in "framed" mode.
    It's common for a Master to have more than one CS pin, one per device it is talking to.
     

    PicForum "it just works"
    #5
    electronicsArtist
    Junior Member
    • Total Posts : 120
    • Reward points : 0
    • Joined: 2017/02/17 15:55:56
    • Location: 0
    • Status: offline
    Re: PIC32 SPI 2017/12/02 09:56:52 (permalink)
    0
    i am trying to send data back and forth btween two MCus  one acts as a slave and the other master
    so far
    i am able to send to the slave ok but to send from slave to master is not working 
    i am using an input pin to tell the master when slave is ready to send
    after signaling  the slave starts sending
    and the when the master gets the signal starts reading from the slave but its not working that well
    the master keeps reading while the slaves signal is high
    sometimes i dont get any data from the slave and when i do its just gibberish
    #6
    NKurzman
    A Guy on the Net
    • Total Posts : 15509
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: PIC32 SPI 2017/12/02 10:12:18 (permalink)
    5 (1)
    That is not how SPI works. The slave data must be in the SPI buffer when the master reads. 8bits (or 16bits) is exchanged at a time. The master sends and receives at the same time. The slave get ready when it’s SS pin is asserted. (Not for 8 bit PICs this does not work well, connecting SS to an interrupt helps) the master sends and receives data with the slave. The transaction ends when the SS PIN is de asserted
    #7
    Jim Nickerson
    User 452 _
    • Total Posts : 4441
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: online
    Re: PIC32 SPI 2017/12/02 10:34:12 (permalink)
    5 (1)
    Have you tried using MCC ?
    #8
    electronicsArtist
    Junior Member
    • Total Posts : 120
    • Reward points : 0
    • Joined: 2017/02/17 15:55:56
    • Location: 0
    • Status: offline
    Re: PIC32 SPI 2017/12/02 10:53:39 (permalink)
    0
    thanks for responding all i havent tried MCC
    I wrote a SPI receive ISR for the slave which works ok when receiving from the master 
    but for the master to get data from slave, does the slave have to write to the SPIxBUF register AND wait for transmit to complete before signaling to master to read the data?
     can both the master and slave drive the SS pin?
    #9
    arpananand
    Super Member
    • Total Posts : 372
    • Reward points : 0
    • Joined: 2009/11/18 04:35:42
    • Location: 0
    • Status: offline
    Re: PIC32 SPI 2017/12/06 06:59:57 (permalink)
    5 (1)
    as others are pointing out, in SPI, slave can not start a transmision. it always has to be started by master. so, if you want slave to send some data to master, keep that data is slave SPIBUF, send some dummy data from master to slave, and then the data which u wrote in slave SPIBUF, will be transferred to master.
     
    And slave can not drive SS pin, it will be driven by master only.
    #10
    electronicsArtist
    Junior Member
    • Total Posts : 120
    • Reward points : 0
    • Joined: 2017/02/17 15:55:56
    • Location: 0
    • Status: offline
    Re: PIC32 SPI 2017/12/07 14:19:49 (permalink)
    0
    thanks arpananand
    #11
    bg_blea
    Super Member
    • Total Posts : 127
    • Reward points : 0
    • Joined: 2014/07/02 06:02:02
    • Location: 0
    • Status: offline
    Re: PIC32 SPI 2017/12/07 21:05:12 (permalink)
    5 (1)
    qɥb
    semed
    Serafino isnt the SS driven by the master automatically

    No.
    Only in "framed" mode, wchich is genrally onlyt used in some audio applications.
     



    Based on "Section 23 - SPI" datasheet the master should be able to drive the SS pin in hardware. See SPIxCON bit MSSEN (not available on all devices).
    The footnote states that this bit is ignored in framed mode, and that makes sense since framed mode requires the sync. So it would be useless to provide this bit otherwise. It also uses FRMPOL bit that has the note that is used only in framed mode...
     
    I am not able to verify this until the weekend. I just got some MCP4822, 2 channel 12 bit DACs. Sequence is:
    - select MCP4822 (ss low)
    - write 16 bits
    - deselect MCP4822. AC output available on deselect
     
    So, if I don't have to toggle the SS in software every write, I can DMA write to the MCP4822. This is for a slave device that takes one write to output, compared to a tft controller that accepts an entire frame buffer before output in which case I can drive SS in software and start DMA after.
     
    :) all in the details, all in the datasheet with all its typos and errors
    #12
    electronicsArtist
    Junior Member
    • Total Posts : 120
    • Reward points : 0
    • Joined: 2017/02/17 15:55:56
    • Location: 0
    • Status: offline
    Re: PIC32 SPI 2018/01/25 10:01:44 (permalink)
    0
    thanks bg_blea i greatly appreciate your response i finally got it working now
    #13
    Jump to:
    © 2018 APG vNext Commercial Version 4.5