FRDM-KL25Z spi0 is not working

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

FRDM-KL25Z spi0 is not working

1,604 Views
rutvijtrivedi
Contributor III

Hi All,

I have made an application code from IAR for communicating to EEPROM CAT25256 using spi0 interface from port -C for FRDM-KL25Z EVM without processor expert.

Below is my code for the same.

@

#include "common.h"

/*============================= EEPROM command macros===================================*/

#define WR_EN 0x06

#define WR_DI 0x04

#define RD_SR 0x05

#define WR_SR 0x01

#define READ 0x03

#define WRITE 0x02

/*================================Function declaration =====================================*/

void spi_init(void);

void spi_send(char spiMsg);

void delay(void);

static void init_RGB(void);

void RGB(int ,int ,int);

char spi_read();

void eeprom_write(int, char);

char eeprom_read(int);

/*========================================MAIN===========================================*/

int main(void)

{

       init_RGB();

        RGB(0,0,1);

        delay();

        delay();

       spi_init();

     

        // Write only one character

  eeprom_write(0x0200, 'A');

  delay();

        // Read data from Same Address

  char read_data = eeprom_read(0x0200);

  delay();

        // Validation of data

  if(read_data == 'A')

  {

  RGB(1,0,0);             // If data is the same then Red LED is ON

  delay();

  }

  else

  {

  RGB(0,1,0);             // If data is not the same then Green LED is ON

                delay();

  }

  while(1);

}

/*========================================= SPI_INIT=========================================*/

void spi_init(void)

  SIM_SCGC5 |= SIM_SCGC5_PORTC_MASK;      //Turn on clock to C module

  SIM_SCGC4 |= SIM_SCGC4_SPI0_MASK;       //Enable SPI0 clock

  PORTC_PCR4 = PORT_PCR_MUX(0x1);    //Set PTC4 to mux 1 as a GPIO [SPI0_PCS0]

  FGPIOC_PDDR |= (1<<4);

  FGPIOC_PSOR |= (1 << 4);   // Disable the EEPROM here

  PORTC_PCR5 = PORT_PCR_MUX(0x2);    //Set PTC5 to mux 2 [SPI0_SCK]

  PORTC_PCR6 = PORT_PCR_MUX(0x2);    //Set PTC6 to mux 2 [SPI0_MOSI]

  PORTC_PCR7 = PORT_PCR_MUX(0x2);    //Set PTC7 to mux 2 [SPIO_MISO]

  // Set SPI0 to Master & SS pin to auto SS & spi mode in 0,0

  SPI0_C1 = SPI_C1_MSTR_MASK | SPI_C1_SSOE_MASK;

 

  // Configure SPI Register C2

  SPI0_C2 = 0x00;

 

  // Set baud rate pre scale divisor to 3 & set baud rate divisor to 64 for baud rate of 15625 hz

  SPI0_BR = (SPI_BR_SPPR(0x02) | SPI_BR_SPR(0x08));  

 

  // Enable SPI0

  SPI0_C1 |= SPI_C1_SPE_MASK;   

 

}

/*=============================================SENDING ON EEPROM/SPI===================================*/

void spi_send(char spiMsg) 

{    

  while(!(SPI_S_SPTEF_MASK & SPI0_S))     //While buffer is not empty do nothing

  {   

    asm("nop"); 

  }

  SPI0_D = spiMsg;    //Write char to SPI

}

/*=======================================READING FROM EEPROM/SPI====================================*/

char spi_read()

{

  char temp;

     

        // Wait for receive flag to set

  while(!(SPI0_S & SPI_S_SPRF_MASK)) 

  {

  asm("nop");

  }

  temp = SPI0_D;  //Read SPI data from slave

  return temp;

}

/*=========================================S\W DELAY==============================================*/

void my_delay(void)

{

    volatile uint32_t delayTicks = 2000;

 

    while(delayTicks--)

    {

        asm("nop");

    }

}

void delay(void)

{

    volatile uint32_t delayTicks = 2000000;

 

    while(delayTicks--)

    {

        asm("nop");

    }

}

/*=============================================RGB INIT==================================================*/

static void init_RGB(void)

    SIM_SCGC5 |= ( SIM_SCGC5_PORTA_MASK

                  | SIM_SCGC5_PORTB_MASK

                  | SIM_SCGC5_PORTC_MASK

                  | SIM_SCGC5_PORTD_MASK

                  | SIM_SCGC5_PORTE_MASK );

 

    SIM_SOPT2 |= SIM_SOPT2_PLLFLLSEL_MASK; // set PLLFLLSEL to select the PLL for this clock source

 

    PORTB_PCR18 = PORT_PCR_MUX(1);              // Set Pin B18 to GPIO function

    PORTB_PCR19 = PORT_PCR_MUX(1);              // Set Pin B19 to GPIO function

    PORTD_PCR1  =  PORT_PCR_MUX(1);

    FGPIOB_PDDR |= (1<<18);                         // Red LED, Negative Logic (0=on, 1=off)

    FGPIOB_PDDR |= (1<<19);                         // Green LED, Negative Logic (0=on, 1=off)

    FGPIOD_PDDR |= (1<<1);

 

}

/*=============================================BLINK RGB========================================================*/

void RGB(int Red,int Green,int Blue)

{

    if (Red == 1)

        FGPIOB_PCOR |= (1<<18);

    else

        FGPIOB_PSOR |= (1<<18);

 

    if (Green == 1)

        FGPIOB_PCOR |= (1<<19);

    else

        FGPIOB_PSOR |= (1<<19);

 

     if (Blue == 1)

        FGPIOD_PCOR |= (1<<1);

    else

        FGPIOD_PSOR |= (1<<1);

}

/*=============================================EERPOME WRITE===============================================================*/

void eeprom_write(int address, char data)

{

  FGPIOC_PCOR |= (1 << 4);   // Enable the EEPROM here

        my_delay();

  spi_send(WR_EN);                // Send Write Enable Command

        my_delay();

  FGPIOC_PSOR |= (1 << 4);   // Disable the EEPROM here

     

        my_delay();

  FGPIOC_PCOR |= (1 << 4);   // Enable the EEPROM here

        my_delay();

  spi_send(WRITE); // Send writing command to EEPROM     

        my_delay();

  spi_send(address >> 8); // Send Writing address

        my_delay();

  spi_send(address);              // Send LSB Address

        my_delay();

  spi_send(data); // Send the actual data

        my_delay();

  FGPIOC_PSOR |= (1 << 4);   // Disable the EEPROM here

        delay();

        delay();

     

        FGPIOC_PCOR |= (1 << 4);   // Enable the EEPROM here

        my_delay();

  spi_send(WR_DI);                // Send Write Disable Command

        my_delay();

  FGPIOC_PSOR |= (1 << 4);   // Disable the EEPROM here

     

}

/*=============================================EEPROM READ===============================================================*/

char eeprom_read(int address)

{

  char data = 1;

  FGPIOC_PCOR |= (1 << 4);   // Enable the EEPROM here

        my_delay();

  spi_send(READ);                 // Send Read command

        my_delay();

  spi_send(address >> 8); // Send address from where to read form EEPROM

        my_delay();

  spi_send(address);              // Send LSB Address

        my_delay();

  spi_send(0xFF); // Send Dummy Data

        my_delay();

  data = spi_read(); // read character in variable

        my_delay();

    

  FGPIOC_PSOR |= (1 << 4);   // Disable the EEPROM here

  return data;

}

/*============================================================================================================*/

@

I m validating data read- write using RGB led.

Now my issue is it always Goes to the "else" condition of my code i.e as per debugging i m not getting data (may be i m not able to write even) from EEPROM.

Also i used oscilloscope for waveform checking but i m not getting sck pulse properly.

So what is here i m missing ?

Please tell me if you require any more detail .

Any help in this case would be great appreciable.

Thank you,

Regards,

Rutvij.

Tags (3)
0 Kudos
Reply
4 Replies

1,178 Views
EarlOrlando
Senior Contributor II

Hello Rutvij,

Please review this thread​ specially the post marked as correct answer, there is an example code that could be useful for you.

Regards,

Earl.

1,178 Views
rutvijtrivedi
Contributor III

Hi Earl,

Thank you for your quick response and I have gone through your suggested link but still it not working.

Please find my SPI configurations are as follow.

Mode of SPI - (0,0)

SPI clock - 15.625 KHz

CS pin      --> PTC4 --> GPIO configured

SPI Clock --> PTC5 --> SPI configured

MOSI       --> PTC6 --> SPI configured

MISO       --> PTC7 --> SPI configured

I have tested same code configuring with PORTD. Also looked into SPI waveforms in CRO but still not getting the SPI clock and also I have configured SPI clock as 1 MHz but still facing same issue.

I have interfaced CAT25256 EEPROM over SPI. Can you please tell me what is the minimum SPI clock frequency require to communicate with this EEPROM?

I really appreciate your help.

Regards,

Rutvij.

0 Kudos
Reply

1,178 Views
EarlOrlando
Senior Contributor II

Hello Rutvij,

The datasheet says that the clock frequency goes form 0 to 5 MHz.

pastedImage_0.png

When you make an EEPROM write does the other signals work fine? Is only the SCK signal missed?

Regards,

Earl.

0 Kudos
Reply

1,178 Views
EarlOrlando
Senior Contributor II

Hello,

I ran your project without modifications and I get the SCK signal OK. Please double check that you have that signal in the PTC5 pin.

Regards,

Earl.

0 Kudos
Reply