Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPI Settings #1

Open
yamiyukiharu opened this issue Jul 9, 2017 · 12 comments
Open

SPI Settings #1

yamiyukiharu opened this issue Jul 9, 2017 · 12 comments

Comments

@yamiyukiharu
Copy link

yamiyukiharu commented Jul 9, 2017

Hi guillaumesmo, thanks for the library as it saved me a lot of development time on the chip! I tested your library with a PIC18F45K80, and I realize that I need to use the following SPI settings in order to make it work:

SSPSTATbits.CKE = 1;         //Transmit occurs on transition from active to Idle clock state
SSPCON1bits.CKP = 1;       //Idle state for clock is a high level 

compared to your settings :

 SSPSTATbits.CKE=1; // clock phase: propagation on rising edge
 SSPCON1bits.CKP=0; // clock polarity: idle low

Is there a problem with your settings or am I missing something out?

Thanks!

@guillaumesmo
Copy link
Owner

Hi,

documentation of FT800 says "Only SPI mode 0 is supported", which means that it should work with idle low (CKP=0) and transmit on transition from Idle to active clock state (CKE=0). I don't know how I got it to work in my case.

mode 0 and 3 (CKE=1, CKP=1) have similar transmission, so they are interchangeable most of the time, which might explain why your settings work, but I would advise you to test again with CKE=0 and CKP=0

If that works for you I'll adapt the settings in the code

@virat92
Copy link

virat92 commented Mar 28, 2018

First Thank you ,i seen your work its fabulous
i am working on PIC32MZ2048EFH144
interface with FT800 , i think, i am doing some mistake it's not working, can you pls check where i did mistake,

i used yours and Jirka S's Code as Reference(from microchip forum)

here my initialization

void ini_vGLCD_DRV(void)
{
SDI1R= 0x04; //sdi1 MISO
RPB3R =0x05; //sdo1 MOSI

SYSTEM_PRINTF(("SPI INIT START\n\r"));

TRISDbits.TRISD0 = 0; //CLK as OUT

PIN_CS = 1; // #define PIN_CS LATBbits.LATB8
PIN_PD = 1; //#define PIN_PD LATDbits.LATD12

PIN_CS_DIR = 0; //#define PIN_CS_DIR TRISBbits.TRISB8
PIN_PD_DIR = 0; //#define PIN_PD_DIR TRISDbits.TRISD12

SPI1BRG= 24; // use FPB/4 clock frequency = 2 mhz //used PBCLK2 which 100mhz
SPI1STATCLR=0x40; // clear the Overflow
SPI1CON = 0x8120; // SPI ON, 8 bits transfer

SYSTEM_PRINTF(("SPI INIT COMPLETE\n\r"));

FT800_Init();

}

@yamiyukiharu
Copy link
Author

@virat92 Have you tried the tweak on the SPI clock polarity that I mentioned? It has been working for me all the while after I made that change.

@virat92
Copy link

virat92 commented Mar 29, 2018

yes sir, i did but still not working it got stuck at tr8

#define PIN_CS LATBbits.LATB8
#define PIN_PD LATDbits.LATD12

#define PIN_CS_DIR TRISBbits.TRISB8
#define PIN_PD_DIR TRISDbits.TRISD12

#define FT800_StartTransmission() PIN_CS_DIR = 0
#define FT800_StopTransmission() PIN_CS_DIR = 1

``void ini_vGLCD_DRV(void)
{
SYSTEM_PRINTF(("SPI INIT START\n\r"));
/SDO1 MOSI/
ANSELBbits.ANSB8=0;
PLIB_PORTS_RemapOutput(PORTS_ID_0, OUTPUT_FUNC_SDO1, OUTPUT_PIN_RPB3 );

/*SDI1 MISO*/
ANSELFCLR=0x02;
TRISFSET=0x02;  //as in pin
PLIB_PORTS_RemapInput(PORTS_ID_0, INPUT_FUNC_SDI1, INPUT_PIN_RPF1 );

/*CS */
ANSELBCLR=0x100; //Disable analog
TRISBCLR=0x100; // set as OUT

/*PD */
ANSELDCLR=0x1000;  //disable  analog
TRISDCLR=0x1000;  //set as out 

TRISDbits.TRISD1 = 0;   //CLK as OUT
TRISFbits.TRISF1 = 1;   //SDI1 as in pin 
TRISBbits.TRISB3 = 0;  //SDO as out pin
PIN_CS = 0;   // #define PIN_CS LATBbits.LATB8
PIN_CS_DIR=1;

SPI1CON=0;
SPI1STATCLR=0x40;   // clear the Overflow
SPI1CONbits.MCLKSEL=0;      //  PBCLK2 100Mhz
int rData;
rData = SPI1BUF;
SPI1BRG= 24;                 //   2Mhz
SPI1CONbits.ON = 1;
SPI1CONbits.CKP=0;   // clock polarity: idle low   
SPI1CONbits.CKE=1; // clock phase: propagation on rising edge  

PIN_PD = 0;      //#define PIN_PD LATDbits.LATD12 
PIN_PD_DIR=1;

SYSTEM_PRINTF(("SPI INIT COMPLETE\n\r"));
FT800_Init();

}

unsigned char tr8(unsigned char value)
{
int dummy;
SYSTEM_PRINTF(("tr8 ......start \n\r"));
SPI1BUF = value;
while(!SPI1STATbits.SPITBE); //<------here i got stuck i tried with SPI1STATbits.SPIRBF also
SYSTEM_PRINTF(("tr......stop \n\r"));
dummy = SPI1BUF;
}

@virat92
Copy link

virat92 commented Mar 29, 2018

@yamiyukiharu
Sir is there any method to check whether my spi interface is proper or not.
Like sending read command verify Slave ID or somthing.

@yamiyukiharu
Copy link
Author

yamiyukiharu commented Mar 29, 2018

I have slightly modified the tr8 function to suit the SPI library generated from MCC Code Configurator. Below are the changes that I made (note that I am using PIC18 platform, it will be a bit different from PIC32):
Original:
#define tr8(value) SSPBUF = value;while(!SSPSTATbits.BF)
Modified:
#define tr8(value) SPI_Exchange8bit(value)

uint8_t SPI_Exchange8bit(uint8_t data)
{
    // Clear the Write Collision flag, to allow writing
    SSPCON1bits.WCOL = 0;

    SSPBUF = data;

    while(SSPSTATbits.BF == SPI_RX_IN_PROGRESS)
    {
    }

    return (SSPBUF);
}

So I guess we are probably missing out on clearing the write collision flag

@virat92
Copy link

virat92 commented Mar 29, 2018

Thank you for your reply :)
i'll try and let ypu know

@virat92
Copy link

virat92 commented Mar 30, 2018

problem remain same ,sir
i checked my CS , PD manually,
i read ROMCHIP id but it gives FFFFF , i think problem in SPI interface but i cant troubleshoot ,i checked everything ,offcourse i m missing something.
pls give suggestion what to do.

@yamiyukiharu
Copy link
Author

Well in that case, I would suggest you to hook up an oscilloscope to check the bits output by the SPI pins to verify if the clock frequency is correct. I've personally encountered many problems with incorrect clock settings in my projects. Hope you solve the issue soon.

Cheers

@virat92
Copy link

virat92 commented Mar 30, 2018

sir i got something in display but not proper, see my modified code

#define PIN_CS LATBbits.LATB8
#define PIN_PD LATDbits.LATD12

#define PIN_CS_DIR TRISBbits.TRISB8
#define PIN_PD_DIR TRISDbits.TRISD12

#define FT800_StartTransmission() PIN_CS = 0
#define FT800_StopTransmission() PIN_CS = 1

int dumm;
#define tr8(value) SPI1BUF = value;while(!SPI1STATbits.SPITBE);dumm=SPI1BUF
#define tr16(value) tr8((value) & 0xFF);tr8(((value) >> 8) & 0xFF)
#define tr32(value) tr16((value) & 0xFFFF);tr16(((value) >> 16) & 0xFFFF)

`void ini_vGLCD_DRV(void)
{

SYSTEM_PRINTF(("SPI INIT START\n\r"));

 /*CLK1*/
ANSELDCLR=0x1;
ANSELDCLR = 0x0002;
TRISDbits.TRISD1 = 0;   //CLK as OUT

/*SDO1 MOSI*/
ANSELBbits.ANSB8=0;
PLIB_PORTS_RemapOutput(PORTS_ID_0, OUTPUT_FUNC_SDO1, OUTPUT_PIN_RPB3 );
 TRISBbits.TRISB3 = 0;  //SDO as out pin

// RPB3R=0X05;

/*SDI1 MISO*/
 ANSELFCLR=0x02;
PLIB_PORTS_RemapInput(PORTS_ID_0, INPUT_FUNC_SDI1, INPUT_PIN_RPF1 );
TRISFbits.TRISF1 = 1;   //SDI1 as in pin 
//    TRISFSET=0x02;  //as in pin

/*CS */
ANSELBCLR=0x100; //Disable analog
PIN_CS = 1;   // #define PIN_CS LATBbits.LATB8
PIN_CS_DIR=0;  //#define PIN_CS_DIR TRISBbits.TRISB8 

/*PD */
ANSELDCLR=0x1000;  //disable  analog
PIN_PD = 1;      //#define PIN_PD LATDbits.LATD12 
PIN_PD_DIR=0;    //#define PIN_PD_DIR TRISDbits.TRISD12 

SPI1CON=0;          //clear SPI1CON
SPI1STATCLR=0x40;   // clear the Overflow
SPI1CONbits.MCLKSEL=0;   //clock source PBCLK2

int rData;
rData = SPI1BUF;
SPI1BRG= 24;    //2 MHz

SPI1CON=0x8360; //8320
//SPI1CONbits.CKP=1;   // clock polarity: idle low   

/* SPI1CONbits.ON = 1;
SPI1CONbits.CKP=0; // clock polarity: idle low
SPI1CONbits.CKE=1; // clock phase: propagation on rising edge
SPI1CONbits.SMP=1;
SPI1CON2bits.AUDEN=0;
SPI1CONbits.MODE16=0;
SPI1CONbits.MODE32=0; */

SYSTEM_PRINTF(("SPI INIT COMPLETE\n\r"));

//PLIB_PORTS_RemapOutput(PORTS_ID_0, OUTPUT_FUNC_SDO1, OUTPUT_PIN_RPB3 );
FT800_Init();
int chipid;
chipid=rd32(ROM_CHIPID);

SYSTEM_PRINTF(("CHIP ID=%x\n\r",chipid));

}`
img_20180330_130108

@virat92
Copy link

virat92 commented Mar 30, 2018

Sir , i am able to print logo but it works sometimes and sometimes it is.'t give any responce ,. And it works only with SKP=1 and SKE=1
Why it is giving unpredictable responce. Did i mistake any where?

And sir in PIC32 spi there is no bit of write collision what you suggest for that?

img_20180330_171752
Thank you so much

@virat92
Copy link

virat92 commented Apr 4, 2018

@yamiyukiharu
sir i am working on PIC32Mz on 200Mhz frequency here eve is not giving proper response, what should i do ??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants