Hello,
at the moment I'm facing a problem with SPI0. I want to establish a connection to a pressure sensor at this interface. The clock initialization looks like:
SIM_SCGC5 = 0x00
| SIM_SCGC5_PORTE_MASK
| SIM_SCGC5_PORTD_MASK
| SIM_SCGC5_PORTC_MASK
| SIM_SCGC5_PORTB_MASK
| SIM_SCGC5_PORTA_MASK;
SIM_SCGC6 = 0x00
| SIM_SCGC6_PIT_MASK
| SIM_SCGC6_SPI0_MASK;
After that, I initialized the SPI module, using the following code:
/* Function to initialize the SPI 0 interface at a frequency of 2.4MHz */
void init_spi_0(void)
{
/* Use alternative 2 on pin 0 of PORTD (CS) */
PORTD_PCR0 = 0x00
| PORT_PCR_MUX(0x02);
/* Use alternative 2 on pin 1 of PORTD (CLK) */
PORTD_PCR1 = 0x00
| PORT_PCR_MUX(0x02);
/* Use alternative 2 on pin 2 of PORTD (MOSI) */
PORTD_PCR2 = 0x00
| PORT_PCR_MUX(0x02);
/* Use alternative 2 on pin 3 of PORTD (MISO) */
PORTD_PCR3 = 0x00
| PORT_PCR_MUX(0x02)
| PORT_PCR_PE_MASK
| PORT_PCR_PS_MASK;
/* Use SPI0 as master, CS0 is low-active, clear TX FIFO and clear RX FIFO */
SPI0_MCR = 0x00
| SPI_MCR_MSTR_MASK
| SPI_MCR_PCSIS(0x01)
| SPI_MCR_DIS_TXF_MASK
| SPI_MCR_DIS_RXF_MASK;
/* Use in every frame 8 bits (0x08), baud rate prescaler is 3 (0x01) and the
baud rate scaler is 2 (0x00) creating a SPI frequency of 8.33MHz */
SPI0_CTAR0 = 0x00
| SPI_CTAR_FMSZ(0x08)
| SPI_CTAR_CPOL_MASK
| SPI_CTAR_PBR(0x1)
| SPI_CTAR_BR(0x00);
}
To write or read data I use the code:
/* Function to send or read a byte */
uint8_t write_read_spi_0(int8_t data, uint32_t continuous)
{
switch (continuous){
case 0:
/* Use a continuous chip select, assert PCS0 and write the byte to the register */
SPI0_PUSHR = 0x00
| SPI_PUSHR_CONT_MASK
| SPI_PUSHR_CTAS(0x00)
| SPI_PUSHR_PCS(0x01)
| SPI_PUSHR_TXDATA(data);
break;
case 1:
/* Assert PCS0 and write the byte to the register */
SPI0_PUSHR = 0x00
| SPI_PUSHR_CTAS(0x00)
| SPI_PUSHR_PCS(0x01)
| SPI_PUSHR_TXDATA(data);
break;
}
/* Wait for the byte to be sent */
while(!(SPI0_SR & SPI_SR_TCF_MASK));
/* Read the received byte */
data = SPI0_POPR;
/* Clear the flag (not self-clearing) */
SPI0_SR |= SPI_SR_TCF_MASK;
/* Return value */
return (data);
}
I checked the signals with a scope and the signal CLK and MOSI are as expected. Only MISO remains low all the time.
The next step I made was connecting MISO to the power supply. If I use MISO as a GPIO I'm able to read in the right logic levels. If I switch back to SPI the results are wrong again. I recognized that 0x01FF is shifted through registers SPI0_RXFR0 till SPI0_RXFR3 (I need four write/read cyles) and if it reached register SPI0_RXFR3 it is written into SPI0_POPR.
Does anyone know what I'm doing wrong? I thought I had switched of the FIFO registers.
Thank you for your help.
Regards,
Michael
Hi
I would recommend to refer SPI demo of Kinetis V2 product example software, which using SPI module read data from external SPI Flash.
Wish it helps.
Have a great day,
Ma Hui
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Hi
thank you for your reply. I tried the example and I solved the problem with the FIFOs. The only problem I still have is that the POPR register is always filled with 0x01FF regardless of setting MISO high or low. Do yo may have a suggestion what that can be?
Have a great day too,
Michael
Hi Michael,
The MISO pin is SPI master input pin.
From your code, the SPI0_CTAR0 register [FMSZ] bits with value 8, which means transfer 9 bits data frame during SPI communication.
That's why you will get 0x1FF at SPI0_POPR register.
SPI0_CTAR0 = 0x00
| SPI_CTAR_FMSZ(0x08)
| SPI_CTAR_CPOL_MASK
| SPI_CTAR_PBR(0x1)
| SPI_CTAR_BR(0x00);
Wish it helps.
Have a great day,
Ma Hui
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------