SPI issue on KL26Z when reading L3GD20

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

SPI issue on KL26Z when reading L3GD20

903 Views
omarvallecasas
Contributor III

Hello, we're working with KL26Z and trying to read Gyro L3GD20 by SPI protocol. When configuring as spi0_init(); L3GD20_init(); and reading with spi_read(L3G_OUT); signal observed in MISO doesn't change (See image from SCLK: orange and MISO: blue). That's why we think maybe we're missing something as a flag from KL26Z. Maybe someone can give us a clue.

Thanks in advance,

Omar

void spi0_init (void){

/*A comunicação SPI0 se dá pela PORTA C */

  /*

  *CHIP SELECT (SPIO_PCS0) : PTC4

  *CLOCK       (SPI0_SCK)  : PTC5

  *MOSI        (SPI0_MOSI) : PTC6

  *MISO        (SPI0_MISO) : PTC7       

  */

SIM_SCGC5 |= SIM_SCGC5_PORTC_MASK;

SIM_SCGC4 |= SIM_SCGC4_SPI0_MASK; 

  

  PORTC_PCR4 |= PORT_PCR_MUX(0x2);

  PORTC_PCR5 |= PORT_PCR_MUX(0x2);

  PORTC_PCR6 |= PORT_PCR_MUX(0x2);

  PORTC_PCR7 |= PORT_PCR_MUX(0x2);

    

  /* Inicialização da SPI em Master Mode 16-bit e FIFO = 0 */

    

  SPI0_C1 |= (SPI_C1_MSTR_MASK | SPI_C1_SSOE_MASK );//Set SPI0 to Master & SS pin to auto SS

  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  

      //Enable SPI0 

  SPI0_C2 |= (SPI_C2_MODFEN_MASK|SPI_C2_SPIMODE_MASK); //Master SS pin acts as slave select output 

  SPI0_C1 |= (SPI_C1_SPE_MASK);                           // Enable SPI module

}

void spi_write (unsigned char ADDR_REGISTER ,unsigned char DATA_REGISTER)

{

/*

* Fluxo : Envia o endereço do registrador; Envia o dado.

*/

  while (!(SPI0_S & SPI_S_SPTEF_MASK));

  SPI0_DH =  0x00| ADDR_REGISTER; // RW\ = 0; MS\ = 1

  SPI0_DL =  DATA_REGISTER;

  }

char spi_read (unsigned char L3G_OUT){

/*

* Fluxo : Envia o endereço do registrador a ser lido; Lê o dado (é enviado para o buffer SPI0_DL).

*/

  spi_write(0x80|L3G_OUT  /*RW\ = 1 MS\ = 0*/,  0xFF   /*dado falso (ignorado)*/ );

  while (!(SPI0_S & SPI_S_SPRF_MASK));

  return SPI0_DL;

  SPI0_S &= ~SPI_S_SPRF_MASK;

}

void L3GD20_init (void){

  spi_write(L3GX_CTRL_REG1, 0x0F);

  spi_write(L3GX_CTRL_REG2, 0x00);

  spi_write(L3GX_CTRL_REG4, 0x30);

}

Tags (1)
0 Kudos
1 Reply

508 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

Hi,

I would recommend to refer attached KL25 SPI example code.

KL26 is using the same SPI module with KL25.

Wish it helps.


Have a great day,
Ma Hui

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------