Hello,
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 |
WAIT1_Waitms(500);
------------------------------------------------------------
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 |
}
retour=SPI0_D;
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.
Michel
Hi
In order to send an address and then read a further byte you must send two bytes:
- the address
- a 'dummy' one (eg. 0xff) - this will generate the second 8 clocks
- they you will receive the result value back
Regards
Mark
Thank you Mark, you have solved a part of my problem.
Now the picture of analyzer seems to be correct, but the SPRF bit seems to be unused. I have add a 5ms wait before to read the answer, I should received 0xe5 but I allways get 0!!.
My read function looks like that:
------------------------------
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_SPTEF_MASK & SPI0_S))
{
//While buffer is not empty do nothing
}
SPI0_D =0x00; | //write 0x0 to generate 8 clock pulses |
while(!(SPI_S_SPRF_MASK & SPI0_S))
{
//While received buffer is not full do nothing |
}
WAIT1_Waitms(5);
retour=SPI0_D;
return retour;
----------------------
And the analyzer:
I think I am that with your help, I am very closed from the solution.
Thanks
Michel
Hi Michel Meunier,
Actually, to KL25Z, you don't need to delay after the SPRF is set, you can read the D register directly, and try again.
If you still not OK, please add the dummy read:
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_SPTEF_MASK & SPI0_S))
{
//While buffer is not empty do nothing
}
dummy = SPI0_S;
SPI0_D =0x00; | //write 0x0 to generate 8 clock pulses |
while(!(SPI_S_SPRF_MASK & SPI0_S))
{
//While received buffer is not full do nothing |
}
retour=SPI0_D;
return retour;
Wish it helps you!
If you still have question, please let me know!
Have a great day,
Jingjing
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Michel
After you configure the SPI interface add
(void)SPI0_S;
(void)SPI0_D;
This will ensure that the status flags start to work correctly.
Regards
Mark