punith B

SPI example code for loop back is not working for TRK-KE64 board

Discussion created by punith B on Sep 29, 2015
Latest reply on Oct 19, 2015 by David Diaz Marin

void serial_flash_init(void)

{

 

 

  SIM->SCGC |= SIM_SCGC_SPI0_MASK;

    CONFIG_PIN_AS_GPIO(PTB,SPI0_CS,OUTPUT);

    CONFIG_PIN_AS_GPIO(PTB,SPI0_MOSI,OUTPUT);

    CONFIG_PIN_AS_GPIO(PTB,SPI0_SCK,OUTPUT);

    CONFIG_PIN_AS_GPIO(PTB,SPI0_MISO,INPUT);

    ENABLE_INPUT(PTB,SPI0_MISO);

 

 

    CONFIG_PIN_AS_GPIO(PTB,SPI0_CS,OUTPUT);

    OUTPUT_SET(PTB,SPI0_CS);

 

 

    SPI0_C1 |= SPI_C1_MSTR_MASK; // SPI0 master mode

 

 

    SPI0_BR = 0x00;//0x43;  //SPPR = 4, SPR = 3, bps div = (SPPR+1)*2^(SPR+1) = 80, baudrate= 24Mhz/80=300khz

    SPI0_C1 |= SPI_C1_SSOE_MASK;

    SPI0_C1 &= (~SPI_C1_CPHA_MASK);  // clock polarity

    SPI0_C1 &= (~SPI_C1_CPOL_MASK);  //clock phase

    SPI0_C1 &= (~SPI_C1_LSBFE_MASK);  // LSB:most significant

 

 

    SPI0_C1 &= (~SPI_C1_SPIE_MASK);                  //Disable RX interrrupt

    SPI0_C1 &= (~SPI_C1_SPTIE_MASK);         //Disable the transmit interrupt

    SPI0_C2 |= SPI_C2_MODFEN_MASK;

 

 

    SPI0_C1 |= SPI_C1_SPE_MASK;  // enable SPI module

}

 

 

uint8_t hal_spi_transfer_one_byte(uint8_t v)

{

   int dummy =0;

   char buff=0;

 

 

   while ((SPI0_S & SPI_S_SPTEF_MASK) == 0)  // wait for transmit buffer empty

   {

                dummy++;

   }

   dummy = SPI0_S;

   SPI0_D = v;   // send one byte to transmit buffer

 

 

   while ((SPI0_S & SPI_S_SPRF_MASK) == 0); // wait ready buffer full

   buff = SPI0_D;  // read one received byte

   return buff;         // return the received byte

 

 

}

 

 

void hal_spi_transfer_start()

{

  OUTPUT_CLEAR(PTB,SPI0_CS);

}

 

 

void hal_spi_transfer_stop()

{

  OUTPUT_SET(PTB,SPI0_CS);

}

 

int main()

{

serial_flash_init();

for(;;)

{

hal_spi_transfer_start();

  buff=hal_spi_transfer_one_byte(0xAA);

  if(buff==0xAA || buff==0x55)

  {

  LED3_ON;

  }

  hal_spi_transfer_stop();

  LED2_ON;

}

}

 

 

But LED3 is not on, i probed clock pin and mosi and miso , all are ok .

 

Could any one tell what may be the issue?.

Outcomes