Hello, does someone have used SPI0 Master Mode of KL25z Board without Processor Expert and could post some code pls
Solved! Go to Solution.
Hi,
Here is a basic SPI0 Master send example.
#include "common.h"
void spi_init(void);
void spi_send(char spiMsg);
int main(void)
{
char ch;
spi_init(); //Init SPI0
while(1)
{
ch = in_char(); //Read char from terminal
spi_send(ch); //Send char over SPI
out_char(ch); //Echo char to terminal
}
}
void spi_init(void)
{
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; //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
}
void spi_send(char spiMsg)
{
while(!(SPI_S_SPTEF_MASK & SPI0_S))
{
asm("nop"); //While buffer is not empty do nothing
}
SPI0_D = spiMsg; //Write char to SPI
}
Best regards,
Martyn
Hi,
I'd be happy to help you fix this issue you are seeing.
Would it be possible for you to share your code? Which Kinetis device are you developing on?
Also, what device is the slave in this SPI configuration?
Best regards,
Martyn
Hello and thanks for your reply.I`m trying to read the x-channel of an inclinometer. I`m using the FRDM-kl25z from Freescale and the inclinometer is called SCA103T Differential Inclinometer. I`ll attach the datasheet. I fairly new to SPI so I have just used your code to write the read command to the SCA103T. I think the CS need to be low during the entire process of writing and reading. Another problem is that the answer is a 11 bit word. I`m not really sure how to read the data from the MISO and would really aprecciate your help.
Best RegardsPer Helgesen
The code:
#include "derivative.h" /* include peripheral declarations */
void spi_init(void); void spi_send(char spiMsg); int main(void) { spi_init(); //Init SPI0 spi_send(0x10); spi_send(0x00); return 0; } void spi_init(void) { 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 PORTD_PCR1 = PORT_PCR_MUX(0x2); //Set PTD1 to mux 2 PORTD_PCR2 = PORT_PCR_MUX(0x2); //Set PTD2 to mux 2 PORTD_PCR3 = PORT_PCR_MUX(0x2); //Set PTD3 to mux 2 SPI0_C1 = SPI_C1_MSTR_MASK | SPI_C1_SSOE_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 } void spi_send(char spiMsg) { while(!(SPI_S_SPTEF_MASK & SPI0_S)) { asm("nop"); //While buffer is not empty do nothing } SPI0_D = spiMsg; //Write char to SPI }
Date: Tue, 6 May 2014 07:59:17 -0700
From: admin@community.freescale.com
To: amardos@hotmail.com
Subject: Re: - SPI0 Freedom Board kl25z
SPI0 Freedom Board kl25z
reply from Martyn Hunt in Kinetis Microcontrollers - View the full discussion
Hi,
I'd be happy to help you fix this issue you are seeing.
Would it be possible for you to share your code? Which Kinetis device are you developing on?
Also, what device is the slave in this SPI configuration?
Best regards,
Martyn
Reply to this message by replying to this email, or go to the message on Freescale Community
Start a new discussion in Kinetis Microcontrollers by email or at Freescale Community
Following SPI0 Freedom Board kl25z in these streams:
Inbox
Based on the data sheet, you will need to do the following:
You will need to write the command byte, followed by 2 dummy bytes to read the 11-bit data.
Also, set SPIx_C1[CPHA] = 1, this will keep the CS line low when writing back to back bytes to the SPIx_D register.
I've added a spi_send_recv function that should work for you:
uint16_t spi_send_recv(char ch)
{
uint8_t bytes[3];
uint16_t temp;
uint8_t count;
count = 0;
while(count < 3)
{
while(!(SPI0_S & SPI_S_SPTEF_MASK))
{
__asm("NOP");
} // Wait for transmitter empty flag to set
if(!count)
{
SPI0_D = ch; //Write char to SPI
while(!(SPI0_S & SPI_S_SPRF_MASK))
{
__asm("NOP");
} // Wait for receive flag to set
bytes[count] = SPI0_D; //Read SPI data from slave
}
else
{
SPI0_D = 0xFFU;
while(!(SPI0_S & SPI_S_SPRF_MASK))
{
__asm("NOP");
} // Wait for receive flag to set
bytes[count] = SPI0_D; //Read SPI data from slave
}
count++;
}
temp = (bytes[1] << 8) | bytes[2];
return temp;
}
I`not able to keep the CS pin high during the entire process ?
From: amardos@hotmail.com
To: jive-1913929615-4s75-2-8lhx@freescale.hosted.jivesoftware.com
Subject: RE: - SPI0 Freedom Board kl25z
Date: Wed, 7 May 2014 11:15:05 +0200
The function gives me this ?: conflicting types for 'spi_send_recv'
From: amardos@hotmail.com
To: jive-1913929615-4s75-2-8lhx@freescale.hosted.jivesoftware.com
Subject: RE: - SPI0 Freedom Board kl25z
Date: Wed, 7 May 2014 07:01:48 +0200
Thanks. When using SPI0_C1[CPHA] = 1 i get the message that CHPA is undecleared. What I`m I missing ?
BestPer Helgesen
Date: Tue, 6 May 2014 10:40:19 -0700
From: admin@community.freescale.com
To: amardos@hotmail.com
Subject: Re: - SPI0 Freedom Board kl25z
SPI0 Freedom Board kl25z
reply from Martyn Hunt in Kinetis Microcontrollers - View the full discussion
Based on the data sheet, you will need to do the following:
You will need to write the command byte, followed by 2 dummy bytes to read the 11-bit data.
Also, set SPIx_C1[CPHA] = 1, this will keep the CS line low when writing back to back bytes to the SPIx_D register.
Reply to this message by replying to this email, or go to the message on Freescale Community
Start a new discussion in Kinetis Microcontrollers by email or at Freescale Community
Following SPI0 Freedom Board kl25z in these streams:
Inbox
To enable SPI0_C1[CPHA] = 1, you need to modify this line,
SPI0_C1 = SPI_C1_MSTR_MASK | SPI_C1_SSOE_MASK;
to this,
SPI0_C1 = SPI_C1_MSTR_MASK | SPI_C1_SSOE_MASK | SPI_C1_CPHA_MASK;
For the duration of the spi_send_recv function the CS should toggle low, which will signal the Inclinometer that it is being addressed by the KL25. Before and after that function is called it should remain high.
What compiler are you using for your project?
Thank you,
Martyn
By the way I`m using CodeWarrior for Microcontrollers v10.6CodeWarrior for Microcontrollers v10.6
From: amardos@hotmail.com
To: jive-824161160-4s75-2-8lok@freescale.hosted.jivesoftware.com
Subject: RE: - Re: SPI0 Freedom Board kl25z
Date: Thu, 8 May 2014 22:50:08 +0200
Hello. Now I`m facing the problem of reading the Y-channel. To do this I need to have a second CS and way to choose this with the same MISO, MOSI and CLK lines as for the X-channel read routine. Do you know how this could be done ?
From: amardos@hotmail.com
To: jive-824161160-4s75-2-8lok@freescale.hosted.jivesoftware.com
Subject: RE: - Re: SPI0 Freedom Board kl25z
Date: Thu, 8 May 2014 14:58:12 +0200
Thanks a lot. Now the code work like a charm.
Date: Wed, 7 May 2014 07:40:17 -0700
From: admin@community.freescale.com
To: amardos@hotmail.com
Subject: Re: - Re: SPI0 Freedom Board kl25z
Re: SPI0 Freedom Board kl25z
reply from Martyn Hunt in Kinetis Microcontrollers - View the full discussion
To enable SPI0_C1[CPHA] = 1, you need to modify this line,
SPI0_C1 = SPI_C1_MSTR_MASK | SPI_C1_SSOE_MASK;
to this,
SPI0_C1 = SPI_C1_MSTR_MASK | SPI_C1_SSOE_MASK | SPI_C1_CPHA_MASK;
For the duration of the spi_send_recv function the CS should toggle low, which will signal the Inclinometer that it is being addressed by the KL25. Before and after that function is called it should remain high.
What compiler are you using for your project?
Thank you,
Martyn
Reply to this message by replying to this email, or go to the message on Freescale Community
Start a new discussion in Kinetis Microcontrollers by email or at Freescale Community
Following Re: SPI0 Freedom Board kl25z in these streams:
Inbox
Hello. Now I`m facing the problem of reading the Y-channel. To do this I need to have a second CS and way to choose this with the same MISO, MOSI and CLK lines as for the X-channel read routine. Do you know how this could be done ?
From: amardos@hotmail.com
To: jive-824161160-4s75-2-8lok@freescale.hosted.jivesoftware.com
Subject: RE: - Re: SPI0 Freedom Board kl25z
Date: Thu, 8 May 2014 14:58:12 +0200
Thanks a lot. Now the code work like a charm.
Date: Wed, 7 May 2014 07:40:17 -0700
From: admin@community.freescale.com
Subject: Re: - Re: SPI0 Freedom Board kl25z
Re: SPI0 Freedom Board kl25z
reply from Martyn Hunt in Kinetis Microcontrollers - View the full discussion
To enable SPI0_C1[CPHA] = 1, you need to modify this line,
SPI0_C1 = SPI_C1_MSTR_MASK | SPI_C1_SSOE_MASK;
to this,
SPI0_C1 = SPI_C1_MSTR_MASK | SPI_C1_SSOE_MASK | SPI_C1_CPHA_MASK;
For the duration of the spi_send_recv function the CS should toggle low, which will signal the Inclinometer that it is being addressed by the KL25. Before and after that function is called it should remain high.
What compiler are you using for your project?
Thank you,
Martyn
Reply to this message by replying to this email, or go to the message on Freescale Community
Start a new discussion in Kinetis Microcontrollers by email or at Freescale Community
Following Re: SPI0 Freedom Board kl25z in these streams:
Inbox
You can use GPIO as CS. That way you can address a lot of devices on the same bus.
Thanks. Where do I actually find the commands and macros used to initialized SPI and the muxing of ports. I have the datasheet, but it describes no such thing ?
Date: Fri, 9 May 2014 12:38:20 -0700
From: admin@community.freescale.com
To: amardos@hotmail.com
Subject: Re: - SPI0 Freedom Board kl25z
SPI0 Freedom Board kl25z
reply from Martyn Hunt in Kinetis Microcontrollers - View the full discussion
You can use GPIO as CS. That way you can address a lot of devices on the same bus.
Reply to this message by replying to this email, or go to the message on Freescale Community
Start a new discussion in Kinetis Microcontrollers by email or at Freescale Community
Following SPI0 Freedom Board kl25z in these streams:
Inbox
Thanks a lot. Now the code work like a charm.
Date: Wed, 7 May 2014 07:40:17 -0700
From: admin@community.freescale.com
Subject: Re: - Re: SPI0 Freedom Board kl25z
Re: SPI0 Freedom Board kl25z
reply from Martyn Hunt in Kinetis Microcontrollers - View the full discussion
To enable SPI0_C1[CPHA] = 1, you need to modify this line,
SPI0_C1 = SPI_C1_MSTR_MASK | SPI_C1_SSOE_MASK;
to this,
SPI0_C1 = SPI_C1_MSTR_MASK | SPI_C1_SSOE_MASK | SPI_C1_CPHA_MASK;
For the duration of the spi_send_recv function the CS should toggle low, which will signal the Inclinometer that it is being addressed by the KL25. Before and after that function is called it should remain high.
What compiler are you using for your project?
Thank you,
Martyn
Reply to this message by replying to this email, or go to the message on Freescale Community
Start a new discussion in Kinetis Microcontrollers by email or at Freescale Community
Following Re: SPI0 Freedom Board kl25z in these streams:
Inbox
The function gives me this ?: conflicting types for 'spi_send_recv'
From: amardos@hotmail.com
To: jive-1913929615-4s75-2-8lhx@freescale.hosted.jivesoftware.com
Subject: RE: - SPI0 Freedom Board kl25z
Date: Wed, 7 May 2014 07:01:48 +0200
Thanks. When using SPI0_C1[CPHA] = 1 i get the message that CHPA is undecleared. What I`m I missing ?
BestPer Helgesen
Date: Tue, 6 May 2014 10:40:19 -0700
From: admin@community.freescale.com
To: amardos@hotmail.com
Subject: Re: - SPI0 Freedom Board kl25z
SPI0 Freedom Board kl25z
reply from Martyn Hunt in Kinetis Microcontrollers - View the full discussion
Based on the data sheet, you will need to do the following:
You will need to write the command byte, followed by 2 dummy bytes to read the 11-bit data.
Also, set SPIx_C1[CPHA] = 1, this will keep the CS line low when writing back to back bytes to the SPIx_D register.
Reply to this message by replying to this email, or go to the message on Freescale Community
Start a new discussion in Kinetis Microcontrollers by email or at Freescale Community
Following SPI0 Freedom Board kl25z in these streams:
Inbox
Thanks. When using SPI0_C1[CPHA] = 1 i get the message that CHPA is undecleared. What I`m I missing ?
BestPer Helgesen
Date: Tue, 6 May 2014 10:40:19 -0700
From: admin@community.freescale.com
To: amardos@hotmail.com
Subject: Re: - SPI0 Freedom Board kl25z
SPI0 Freedom Board kl25z
reply from Martyn Hunt in Kinetis Microcontrollers - View the full discussion
Based on the data sheet, you will need to do the following:
You will need to write the command byte, followed by 2 dummy bytes to read the 11-bit data.
Also, set SPIx_C1[CPHA] = 1, this will keep the CS line low when writing back to back bytes to the SPIx_D register.
Reply to this message by replying to this email, or go to the message on Freescale Community
Start a new discussion in Kinetis Microcontrollers by email or at Freescale Community
Following SPI0 Freedom Board kl25z in these streams:
Inbox
Hi asdasd
If you need more information about SPI you check this link http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=FRDM-KL25Z&fpsp=1&tab=Design_Tools_Ta... and download KL25_SC.
I hope this helps.
Hi,
Here is a basic SPI0 Master send example.
#include "common.h"
void spi_init(void);
void spi_send(char spiMsg);
int main(void)
{
char ch;
spi_init(); //Init SPI0
while(1)
{
ch = in_char(); //Read char from terminal
spi_send(ch); //Send char over SPI
out_char(ch); //Echo char to terminal
}
}
void spi_init(void)
{
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; //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
}
void spi_send(char spiMsg)
{
while(!(SPI_S_SPTEF_MASK & SPI0_S))
{
asm("nop"); //While buffer is not empty do nothing
}
SPI0_D = spiMsg; //Write char to SPI
}
Best regards,
Martyn
Its funny, after i initialise the SPI0 so the Blue LED is on. WHY?^^ BUT THE SPI WORKS
The Blue LED pin is the same as the SPI0 in the port D this pin is PTD13 remember that is multiplexed.
Oh yes of course didnt think about it. But Now i have the next Problem. When i send for example an 0x01 Hex Number to the SPI Port in a while(1) loop i can see the Bit on the Oscilloscope.
But if i try to send 0xFF i while(1) loop so there is nothing. Its Maybe Because the Data collides??. Then I tried to Send some Data to my LCD to turn it on. the lcd expects 16 bits. And when I send the following: so i dont see 00000110 and 00000001 on the Oscilloscope its seems to be wrong sometimes it matches but the most time its wrong.
void display_on (void)
{
FGPIOE_PCOR= (1UL<<1) // Pin LOW for Write Command to SPI
spi_send(0x06); // Command for LCD Register
FGPIOE_PSOR= (1UL<<1) // Pin HIGH for Write Command to SPI
spi_send(0x01); // Data LCD
}
But, when i do SOFTWARE SPI the Display works and the bits are right on the Oscilloskope too
void Write_command(unsigned char Data)
{
unsigned char i;
CS_OFF; // Chip Select LOW
DC_OFF; // Pin LOW for Write Command to SPI
for (i=0; i<8; i++)
{
SCLK_OFF; // SCLK LOW
FGPIOC_PDOR=(Data&0x80)>>7; // MSB FIRST
Data = Data << 1;
SCLK_ON; / SCLK HIGH
}
DC_ON; // Pin LOW for Write Command to SPI
CS_ON; // Chip Select HIGH
}
void Write_data(unsigned char Data)
{
unsigned char i;
CS_OFF; // Chip Select LOW
DC_ON; // Pin HIGH for Write Command to SPI
for (i=0; i<8; i++)
{
SCLK_OFF; // SCLK LOW
FGPIOC_PDOR=(Data&0x80)>>7; // MSB FIRST
Data = Data << 1;
SCLK_ON; / SCLK HIGH
}
DC_ON; // PinHIGH for Write Command to SPI
CS_ON; // Chip Select HIGH
}
if i write to display now it works and i see the right Bits on the oscilloscope
int main(void)
{
Write_command(0x06) // Command to LCD
Write_data(0x01); // Data LCD ON
}
Hi,
You might not be seeing any change in the MOSI line when writing 0xFF because it is high for the duration of the transfer. Could you post screen shots from your oscilloscope, please? Also, what type of LCD controller are you using? And, could you post a link to the datasheet?
Thank you,
Martyn
I can do the Screenshots Monday, The Controller is the SEPS525 the LCD is DD-160128FC-1A, http://www.techdesign.be/shop/datasheets/SEPS525[1].pdf , http://www.farnell.com/datasheets/302911.pdf
I initialise the SPI0 with CPHA=1 and CPOL=1 and i toggle the Chip Select like so
FGPIOE_PCOR= (1UL<<4) // Chip Select low
FGPIOE_PCOR= (1UL<<1) // Pin LOW for Write Command to SPI (RS PIN)
spi_send(0x06); // Command for LCD Register
FGPIOE_PSOR= (1UL<<1) // Pin HIGH for Write DATA to SPI (RS PIN)
FGPIOE_PSOR= (1UL<<4) // Chip Select HIGH
FGPIOE_PCOR= (1UL<<4) // Chip Select low
FGPIOE_PSOR= (1UL<<1) // Pin HIGH for Write DATA to SPI (RS PIN)
spi_send(0x01); // Data LCD
FGPIOE_PSOR= (1UL<<1) // Pin HIGH for Write DATA to SPI (RS PIN)
FGPIOE_PSOR= (1UL<<4) // Chip Select HIGH
I use the Pins PTD1 for SCK and PTD2 for MOSI. MISO is unused and i set SSOE and MODFEN=0, so Chip Select is unused too.
I also found a Lib for Arduino to handle the SEPS525 with SPI it look the same.
Thanks for help