AnsweredAssumed Answered

SPI communication on KL25Z

Question asked by Michel Meunier on Dec 30, 2015
Latest reply on Jan 4, 2016 by Kerry Zhou


I tried to read an ADXL345 accelerometer, I have written my own SPI read and write because it doesn't work with processor expert.

The initialization of SPI is:


  SIM_SCGC5 |= SIM_SCGC5_PORTD_MASK;  //Turn on clock to D module
  SIM_SCGC4 |= SIM_SCGC4_SPI0_MASK;   //Enable SPI0 clock


  PORTD_PCR0 = PORT_PCR_MUX(0x2);//Set PTD0 to mux 2 [SPI0_PCS0]
  PORTD_PCR1 = PORT_PCR_MUX(0x2);//Set PTD1 to mux 2 [SPI0_SCK]
  PORTD_PCR2 = PORT_PCR_MUX(0x2);//Set PTD2 to mux 2 [SPI0_MOSI]
  PORTD_PCR3 = PORT_PCR_MUX(0x2);//Set PTD3 to mux 2 [SPIO_MISO]


  SPI0_C1 = SPI_C1_MSTR_MASK | SPI_C1_SSOE_MASK | SPI_C1_CPHA_MASK | SPI_C1_CPOL_MASK;   //Set SPI0 to Master & SS pin to auto SS

  SPI0_C2 = SPI_C2_MODFEN_MASK;   //Master SS pin acts as slave select output

  SPI0_BR = (SPI_BR_SPPR(0x02) | SPI_BR_SPR(0x08)); //Set baud rate prescale divisor to 3 & set baud rate divisor to 64 for baud rate of 15625 hz
  SPI0_C1 |= SPI_C1_SPE_MASK;//Enable SPI0



the read function is quite simple, I write the adress and next read the answer;


uint8_t retour=0,dummy;

bool fini=FALSE;

  while(!(SPI_S_SPTEF_MASK & SPI0_S))


      //While buffer is not empty do nothing



    SPI0_D =adresse+0x80;//write adress+0x80 (read mode)

    while(!(SPI_S_SPRF_MASK & SPI0_S))


      //While received buffer is not full do nothing



    return retour;



For the test I init the SPI and try to read adress 0x00.

As you can see on the analyzer image, the byte is written, but nothing is read, the KL25Z do nothing. I should have a second set of 8 clock pulses.