• AVR Freaks

Hot!SPI Issues

Author
AKJ
Starting Member
  • Total Posts : 49
  • Reward points : 0
  • Joined: 2019/05/27 06:55:52
  • Location: 0
  • Status: offline
2019/08/07 07:44:30 (permalink)
0

SPI Issues

Hello,
 
i am currently trying to create an SPI communication from a dsPIC33EP512GM706, operating as master.
First, the SPI pins needed to be remapped, then the SPI initialisation and then the send function needed to be implemented. 
After i did all these, the 'send' function gets stuck, because the while(!SPI2STATbits.SPIRBF) doesn't end.
I am quite confused as to where the error could arise as there are 3 steps in total. The followings are my implementation:
 
Pin remap

// Remap RP pins to input
RPINR22bits.SDI2R = 0b1110111; // SDI2 uses RPI119,
RPINR23bits.SS2R = 0b1111001; // SS2 uses RPI121
// Remap RP pins to ouput
RPOR11bits.RP120R = 0b001000; // SDO2 uses RP120
RPOR10bits.RP118R = 0b001001; // SCK2 uses RP118

// Only set SDI to input
TRISGbits.TRISG7 = 1;
TRISGbits.TRISG8 = 0;
TRISGbits.TRISG6 = 0;
TRISGbits.TRISG9 = 0;
// Change pins to analog.
ANSELG = 0;

 
SPI2 init

IFS2bits.SPI2IF = 0; // clear the interrupt flag
IEC2bits.SPI2IE = 0; // Disable the interrupt

// SP2CON! Register Settings
SPI2CON1bits.DISSCK = 0; // Internal Serial Clock is enable
SPI2CON1bits.DISSDO = 0; // SDOx pin is controlled by the module
SPI2CON1bits.MODE16 = 0; // Communication is byte-wide (8 bits)
SPI2CON1bits.MSTEN = 1; // Master mode enabled
SPI2CON1bits.SMP = 0; // Input data is ampled at the middle of data output time
SPI2CON1bits.CKE = 0; // Serial output data changes on transition from
// idle clock state to active clock state
SPI2CON1bits.CKP = 0; // Idle state for clock is a low level; actice state is
// a low level
SPI2STATbits.SPIEN = 1; // Enable SPI module

// Interrupt controller Settings
IFS2bits.SPI2IF = 0; // Clear the interrupt flag
IEC2bits.SPI2IE = 1; // Enable the interrupt
PORTGbits.RG9 = 1;

 
SPI2 send

uint8_t SPI2_send(uint8_t message)
{
// uint16_t temp;
PORTGbits.RG9 = 0;
// temp = SPI2BUF;
SPI2BUF = message;
while(!SPI2STATbits.SPIRBF)
{
int a = 1;
}
PORTGbits.RG9 = 1;
return SPI2BUF;
}

 
Any help solving the infinite loop would be very much appreciated.
 
Thanks.
#1

4 Replies Related Threads

    du00000001
    Just Some Member
    • Total Posts : 3159
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: online
    Re: SPI Issues 2019/08/07 08:25:18 (permalink)
    0
    Is the chip transmitting at all ? (Check with a scope!)
     
    And why do you enable the interrupt ("IEC2bits.SPI2IE = 1;") if you intend to poll the port ?
    Are you really shure you're stuck in the polling and not in an infinite loop in the default interrupt handler? Breaking (assuming you use some debugger) might help to identify this.

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #2
    vjasinski
    Super Member
    • Total Posts : 159
    • Reward points : 0
    • Joined: 2013/04/30 11:48:06
    • Location: Michigan, USA
    • Status: offline
    Re: SPI Issues 2019/08/07 08:46:26 (permalink)
    0
    You may need to configure an SPI clock input on the same pin you configured as the clock output. Some PIC's require this.
    Also, the SS input is only used on the slave device, you may have to use a chip select output to enable the slave device.
    #3
    AKJ
    Starting Member
    • Total Posts : 49
    • Reward points : 0
    • Joined: 2019/05/27 06:55:52
    • Location: 0
    • Status: offline
    Re: SPI Issues 2019/08/07 09:06:03 (permalink)
    0
    du0000000001,
     
    It seemed, i misconfigured the mappable pins (just tried the Maplab Code Configurator).
    The right values are: 

    RPOR10bits.RP118R = 0x9;
    RPOR11bits.RP120R = 0x8;
    RPINR22bits.SDI2R = 0x77;
    RPINR22bits.SCK2R = 0x76;

    The SCK2 value was wrong. It should have been 01110110 instead of 01111001. The documentation might be wrong. There could be an error in the datasheet, as happened here too https://www.microchip.com/forums/m968246.aspx.
    Thanks
    #4
    du00000001
    Just Some Member
    • Total Posts : 3159
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: online
    Re: SPI Issues 2019/08/07 09:30:36 (permalink)
    0
    I doubt there's an error in the PPS input section of the datasheet:
    0111 0110 translates to Port G, Bit 6 (aka RP118 for this device)
    0111 1001 translates to Port G, Bit 9 (RPI121)
     
    (While the RP(I) assignments may vary, the translation to Port #/Bit # is given within the coding of the value and doesn't vary over the families more than whether Port A is assigned to 0x0 or to 0x1 and whether there are 8, 16 or maybe 32 bits per port.)

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #5
    Jump to:
    © 2019 APG vNext Commercial Version 4.5