• AVR Freaks

AnsweredHot!SPI2STATbits.SPIRBF does not work dsPIC33EP512GM710

Author
xmarcy
New Member
  • Total Posts : 4
  • Reward points : 0
  • Joined: 2020/10/16 00:45:36
  • Location: germany
  • Status: offline
2020/10/22 02:27:46 (permalink)
0

SPI2STATbits.SPIRBF does not work dsPIC33EP512GM710

Hi, Im using dsPIC33EP512GM710 the SPI 2 module in legacy mode.
 
if I send / receive bytes the rx buffer full flag isnt set by the PIC.
Thats only the case at the remappable SPI2 and SPI3 module. SPI1 works.
 
Im searching for a soluton for a long time and cant find my fault.
 
here is my code, maybe somebody can help me?
 
Thanks in advance!
 
Register defines:
#ifndef __SSR_H
#define __SSR_H
#include "pinIO.h"

// SSR Length
#define SSR_TX_LEN 6
#define SSR_RX_LEN 5


// SPI Port to use
#define SSR_SPI2 //1,2 oder 3

// PINS to latch input/output to/from shift registers
#define SSR_LATCH_IN pOUT_SSR_LATCH_IN //TODO
#define SSR_LATCH_OUT pOUT_SSRO_CS

//
#ifdef SSR_SPI1
    #define SSR_SPITBF SPI1STATbits.SPITBF
    #define SSR_SPIRBF SPI1STATbits.SPIRBF
    #define SSR_SPIBUF SPI1BUF
#endif

#ifdef SSR_SPI2
    #define SSR_SPITBF SPI2STATbits.SPITBF
    #define SSR_SPIRBF SPI2STATbits.SPIRBF
    #define SSR_SPIBUF SPI2BUF
#endif

#ifdef SSR_SPI3
    #define SSR_SPITBF SPI3STATbits.SPITBF
    #define SSR_SPIRBF SPI3STATbits.SPIRBF
    #define SSR_SPIBUF SPI3BUF
#endif


void ssrOutputUpdate(void);
void ssrOutputClear(void);
void ssrWrite(unsigned char * ,char);
void ssrRead(unsigned char * ,char);

#endif

 
Module Config
void SYSTEM_Init(void)
{
// Configure Oscillator to operate the device at 120Mhz = 60MIPS
// Fosc= Fin*M/(N1*N2), Fcy=Fosc/2
// Fosc= 10M*48/(2*2)=120Mhz for 10M input clock
PLLFBD = 46; // M=48
CLKDIVbits.PLLPRE = 0; // N1=2
CLKDIVbits.PLLPOST = 0; // N2=2

__builtin_write_OSCCONH(0x03); // Request Clockswitch
__builtin_write_OSCCONL(OSCCON | 0x01);

while(OSCCONbits.COSC!=0b011) {}; // Wait until crystal is active
while(OSCCONbits.LOCK!=1) {}; // wait until PLL locks

RCONbits.SWDTEN = 0; // Watchdog enable / disable //TODO

INTCON1bits.NSTDIS=1; // disable nesting interrupts


// init SPI Modules
//SPI1: Profinet IO
SPI1CON1bits.PPRE=0b01; // 16:1 ==> ???? kBit //TODO
SPI1CON1bits.MSTEN=1; // Master mode
SPI1CON1bits.CKP=0; // Clock polarity: rising edge
IFS0bits.SPI1IF=0; // clear IR flag
IEC0bits.SPI1IE=0; // Interrupts OFF
SPI1STATbits.SPIEN=1; // module enable
// SPI 1 no pin mapping available direct IO


//SPI2: RTDs, digital outputs, shift-registers
RPOR10bits.RP118R = 0b01000; //map out SDO2
RPOR11bits.RP120R = 0b01001; //map out SCK2
RPINR22bits.SDI2R= 119; //map in SDI2

SPI2CON1bits.PPRE=0b01; // 16:1 ==> ???? kBit //TODO
SPI2CON1bits.MSTEN=1; // Master mode
SPI2CON1bits.CKP=0; // Clock polarity: rising edge
IFS2bits.SPI2IF=0; // clear IR flag
IEC2bits.SPI2IE=0; // Interrupts OFF
SPI2STATbits.SPIEN=1; // module enable


// SPI3 ySD Card
// RPOR10bits.RP118R = 0b011111; //map out SDO3
// RPOR11bits.RP120R = 0b100000; //map out SCK3
// RPINR29bits.SDI3R= 119; //map in SDI3
//
//
// SPI3CON1bits.PPRE=0b01; // 16:1 ==> ???? kBit //TODO
// SPI3CON1bits.MSTEN=1; // Master mode
// SPI3CON1bits.CKP=0; // Clock polarity: rising edge
// IFS5bits.SPI3IF=0; // clear IR flag
// IEC5bits.SPI3IE=0; // Interrupts OFF
// SPI3STATbits.SPIEN=1; // module enable

}

The data transmitting code
void ssrWrite(unsigned char * d,char len)
//*****************************************************************************
// write serial shift register
// ------------------------------------------------------------------------
// Inputs: - data-pointer, len to write
// Outputs: -
//*****************************************************************************
{
    unsigned char temp;
    unsigned char i;
    temp=SSR_SPIBUF;
    for (i=0; i<len;i++)
    {
        
            while(SSR_SPITBF);
            SSR_SPIBUF=d[i];
         
            while(!SSR_SPIRBF); Hier bleibts hängen, wenn SPI2 oder 3 verwendet wird!!
            temp=SSR_SPIBUF;
    }

    SSR_LATCH_OUT=1;
    delayUs(2);
    SSR_LATCH_OUT=0;
}


[/code]
#1
du00000001
Just Some Member
  • Total Posts : 4011
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: SPI2STATbits.SPIRBF does not work dsPIC33EP512GM710 2020/10/22 09:33:09 (permalink) ☄ Helpfulby xmarcy 2020/10/22 14:08:44
0
Do you receive anything at all (on SPI2/3) ?
 
This might be one of the dsPICs that require you to PPS the SPICLK for the RX direction to the same pin that's assigned to the TX SPICLK. A real pitfall for new users.

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#2
xmarcy
New Member
  • Total Posts : 4
  • Reward points : 0
  • Joined: 2020/10/16 00:45:36
  • Location: germany
  • Status: offline
Re: SPI2STATbits.SPIRBF does not work dsPIC33EP512GM710 2020/10/22 12:26:34 (permalink)
0
Hi du00000001!

Thanks for your reply:)

I have only one SPICLK pin to PPS... What do you mean?
#3
ric
Super Member
  • Total Posts : 28943
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: SPI2STATbits.SPIRBF does not work dsPIC33EP512GM710 2020/10/22 13:28:57 (permalink) ☼ Best Answerby xmarcy 2020/10/22 13:59:22
5 (1)
He means exactly what he said.
On many devices with PPS, you have to set both the input function and the output function of the CLK pin.
This enables output of the clock from the baud rate generator, and input of the clock back to the shift register.

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#4
xmarcy
New Member
  • Total Posts : 4
  • Reward points : 0
  • Joined: 2020/10/16 00:45:36
  • Location: germany
  • Status: offline
Re: SPI2STATbits.SPIRBF does not work dsPIC33EP512GM710 2020/10/22 14:08:09 (permalink)
0
Hey ric!
 
Thanks for your reply. That was so easy! Ive searced the fault for about two days...
To PPS the clock imput was complete new for me... (I had a PIC break of about 5 years)
 
THANK YOU!
 
best regards
marc
#5
ric
Super Member
  • Total Posts : 28943
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: SPI2STATbits.SPIRBF does not work dsPIC33EP512GM710 2020/10/22 14:49:19 (permalink)
5 (1)
Glad it helped.
Usually when the double assignment is required, this is mentioned in the datasheet, but yours doesn't.
The block diagram in "FIGURE 18-1:SPIx MODULE BLOCK DIAGRAM" if anything implies that is it not the case on your chip.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#6
du00000001
Just Some Member
  • Total Posts : 4011
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: SPI2STATbits.SPIRBF does not work dsPIC33EP512GM710 2020/10/23 00:09:47 (permalink)
0
Shabadabadooo :)
 
@ric
Fig 18-1 doesn't reflect the need to "tap" the shift clock. But - as most times - it is a somewhat simplified schematic of the "real ting": while most if not all schematics show a single shift register (shared by RX and TX), I suspect that most implementations have separate shift registers for RX and TX to avoid timing issues.

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