AnsweredAssumed Answered

FRDM-KL25Z spi0 is not working

Question asked by Rutvij Trivedi on Jul 10, 2015
Latest reply on Jul 13, 2015 by Earl Orlando Ramirez Sanchez

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.

Outcomes