<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Kinetis Microcontrollers中的主题 Re: kl25z spi basics</title>
    <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/kl25z-spi-basics/m-p/301102#M12622</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In the uTasker KL project the SPI status is cleared (ex. for SPI1) by using:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;(volatile unsigned char)SPI1_S;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;(volatile unsigned char)SPI1_DL;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is required only once after the interface has been configured and no delays should be necessary.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The other thing to be aware of is not to have an SPI peripheral view open when stepping code since it will be reading and clearing flags in the process, which can cause unexpected behaviour.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Mark&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 20 May 2014 18:51:16 GMT</pubDate>
    <dc:creator>mjbcswitzerland</dc:creator>
    <dc:date>2014-05-20T18:51:16Z</dc:date>
    <item>
      <title>kl25z spi basics</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/kl25z-spi-basics/m-p/301098#M12618</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am working with the SPI0 peripheral on the kl25z freedom board.&amp;nbsp; I was having some trouble getting the SPI receive buffer to give me the expected output. I used a loopback test on MOSI and MISO for self testing. I found that the problem was that I had to insert a manual delay in order for the proper data to be received from the buffer (see delay_nop(100) in code).&amp;nbsp; After inserting the delay the "r" receive buffer five the correct data of 0xa1- 0xa4.&amp;nbsp; Without the delay the buffer returns 0x03,0x04,0xa1,0xa2, which are the last 2 bytes from the transmit loop and the first 2 bytes of the receive loop.&amp;nbsp; You can see the attached image capture to see the sequence of events with and without the delay, with a test signal being triggered.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1) I thought that the "SPI_WAIT_RX_DATA" would throttle the SPI transaction without needing the manual delay.&amp;nbsp; I thought that the SPRF flag would wait upon each SPI send until it was received then proceed.&amp;nbsp; But, it seems to blow past the data actually being received?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It seems that there would be a more efficient way of writing the routines than using a manual delay that would have to be tuned depending on the clock rate.&amp;nbsp; But, I do not see any other flags that might help in the process.&amp;nbsp; Is a manual delay the only option?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="c++" __jive_macro_name="code" class="jive_text_macro _jivemacro_uid_14005438906891903 jive_macro_code" jivemacro_uid="_14005438906891903"&gt;
&lt;P&gt;#define TEST_LOW GPIOD_PCOR |= GPIO_PCOR_PTCO(0x01)&amp;nbsp; //debug pin&lt;/P&gt;
&lt;P&gt;#define TEST_HIGH GPIOD_PSOR |= GPIO_PSOR_PTSO(0x01) //debug pin&lt;/P&gt;
&lt;P&gt;#define SPI_WAIT_RX_DATA while (!(SPI0_S &amp;amp; SPI_S_SPRF_MASK))&amp;nbsp; //waits for the rx data ready ready&lt;/P&gt;
&lt;P&gt;#define SPI_WAIT_TX_DATA while (!(SPI0_S &amp;amp; SPI_S_SPTEF_MASK)) //waits for the txef data ready ready&lt;/P&gt;
&lt;P&gt;#define SPI_RD_WR_REG SPI0_D&amp;nbsp; //kinetis read write register&lt;/P&gt;
&lt;P&gt;void spi_test_io( void){&lt;/P&gt;
&lt;P&gt;&amp;nbsp; uint8_t i=0,junk,status;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; uint8_t j = 0xA0;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; uint8_t r[4] = {0};&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; TEST_LOW;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; //clock out 1-8 on spi bus&lt;/P&gt;
&lt;P&gt;&amp;nbsp; for(i=0;i&amp;lt;4;i++){&lt;/P&gt;
&lt;P&gt;&amp;nbsp; SPI_WAIT_TX_DATA; //WAITS UNTIL THE TX EMPTY BUFFER FLAG GOES HIGH&lt;/P&gt;
&lt;P&gt;&amp;nbsp; SPI_RD_WR_REG = i+1; //OUTPUT A1 ON SPI&lt;/P&gt;
&lt;P&gt;&amp;nbsp; SPI_WAIT_RX_DATA; //THOUGHT THIS WOULD THROTTLE THE LOOP - WAIT FOR DATA TO BE TX (RECEIVED)&lt;/P&gt;
&lt;P&gt;&amp;nbsp; }&lt;/P&gt;
&lt;P&gt;&amp;nbsp; //delay_nop(100);&amp;nbsp; //ENOUGH TIME TO OUTPUT THE DATA....&lt;/P&gt;
&lt;P&gt;&amp;nbsp; TEST_HIGH;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; //TRYING TO CLEAR BUFFER SO THAT CAN READ CURRENT SPI DATA&lt;/P&gt;
&lt;P&gt;&amp;nbsp; //junk = SPI_STATUS; //read status&lt;/P&gt;
&lt;P&gt;&amp;nbsp; junk = SPI_RD_WR_REG; //MUST READ DATA IN ORDER FOR NEXT READS TO BE CORRECT&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; for(i=0;i&amp;lt;4;i++){&lt;/P&gt;
&lt;P&gt;&amp;nbsp; //output and read 0xa0 - 0xa4&lt;/P&gt;
&lt;P&gt;&amp;nbsp; SPI_WAIT_TX_DATA; //WAITS UNTIL THE TX EMPTY BUFFER FLAG GOES HIGH&lt;/P&gt;
&lt;P&gt;&amp;nbsp; SPI_RD_WR_REG = j++; //OUTPUT A1-a4 ON SPI&lt;/P&gt;
&lt;P&gt;&amp;nbsp; SPI_WAIT_RX_DATA; //wait for data to be tx&lt;/P&gt;
&lt;P&gt;&amp;nbsp; r[i] = SPI_RD_WR_REG; //READ REG TO UPDATE BUFFER&lt;/P&gt;
&lt;P&gt;&amp;nbsp; }&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; TEST_LOW;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; asm("nop");&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="font-size: 9pt; line-height: 12pt;"&gt;}&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;void spi_init( void){&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; //SPI0 module initialization&lt;/P&gt;
&lt;P&gt;&amp;nbsp; SIM_SCGC4 |= SIM_SCGC4_SPI0_MASK; // Turn on clock to SPI0 module&lt;/P&gt;
&lt;P&gt;&amp;nbsp; SIM_SCGC5 |= SIM_SCGC5_PORTD_MASK; // Turn on clock to Port D module&lt;/P&gt;
&lt;P&gt;&amp;nbsp; PORTD_PCR1 = PORT_PCR_MUX(0x02); // PTD1 pin is SPI0 CLK line&lt;/P&gt;
&lt;P&gt;&amp;nbsp; PORTD_PCR2 = PORT_PCR_MUX(0x02); // PTD2 pin is SPI0 MOSI line&lt;/P&gt;
&lt;P&gt;&amp;nbsp; PORTD_PCR3 = PORT_PCR_MUX(0x02); // PTD3 pin is SPI0 MISO line&lt;/P&gt;
&lt;P&gt;&amp;nbsp; PORTD_PCR0 = PORT_PCR_MUX(0x01); // PTD0 pin is configured as GPIO (CS line driven manually)&lt;/P&gt;
&lt;P&gt;&amp;nbsp; GPIOD_PSOR |= GPIO_PSOR_PTSO(0x01); // PTD0 = 1 (CS inactive)&lt;/P&gt;
&lt;P&gt;&amp;nbsp; GPIOD_PDDR |= GPIO_PDDR_PDD(0x01); // PTD0 pin is GPIO output&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; //SETUP SPI0 PERIPHERAL&lt;/P&gt;
&lt;P&gt;&amp;nbsp; SPI0_C1 = SPI_C1_SPE_MASK | SPI_C1_MSTR_MASK; // Enable SPI0 module, master mode //SETS SPI ENABLE AND SPI MASTER BITS&lt;/P&gt;
&lt;P&gt;&amp;nbsp; SPI0_BR = SPI_BR_SPPR(0x04) | SPI_BR_SPR(0x02) ; // BaudRate = BusClock / ((SPPR+1) * 2^(SPR+1)) = 20970000 / ((4+1) * 2^(2+1)) = 524.25 kHz&lt;/P&gt;
&lt;P&gt;&amp;nbsp; //SPI0_BR = SPI_BR_SPPR(0x00) | SPI_BR_SPR(0x00) ; // BaudRate = BusClock / ((SPPR+1) * 2^(SPR+1)) = 20970000 / ((4+1) * 2^(2+1)) = 5240.25 kHz&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;

&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 19 May 2014 23:51:22 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/kl25z-spi-basics/m-p/301098#M12618</guid>
      <dc:creator>mjg8t</dc:creator>
      <dc:date>2014-05-19T23:51:22Z</dc:date>
    </item>
    <item>
      <title>Re: kl25z spi basics</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/kl25z-spi-basics/m-p/301099#M12619</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;First of all, in actual spi transfer, I suggest you clear the GPIO pin which is connected to the /SS pin of slave spi when you transfer each byte. this is the procedure for master spi:&lt;/P&gt;&lt;P&gt;clear GPIO&lt;/P&gt;&lt;P&gt;write &lt;SPAN&gt;SPI_RD_WR_REG&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;wait SPRF bit is set&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;read &lt;/SPAN&gt;&lt;SPAN&gt;SPI_RD_WR_REG&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;set GPIO&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;delay some time&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;......&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;In the first loop code, you do not read the receiver register, how about reading the receiver register after the SPRF bit is set by the code I modified based on your original code, I suspect that the first loop receiver buffer is not emptied:&lt;/P&gt;&lt;OL class="dp-cpp" start="1"&gt;&lt;LI class="alt"&gt;&lt;SPAN&gt;&amp;nbsp; TEST_LOW;&amp;nbsp; &lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;&amp;nbsp; &lt;SPAN class="comment"&gt;//clock out 1-8 on spi bus&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI class="alt"&gt;&lt;SPAN&gt;&amp;nbsp; &lt;SPAN class="keyword"&gt;for&lt;/SPAN&gt;&lt;SPAN&gt;(i=&lt;/SPAN&gt;&lt;SPAN class="number"&gt;0&lt;/SPAN&gt;&lt;SPAN&gt;;i&amp;lt;&lt;/SPAN&gt;&lt;SPAN class="number"&gt;4&lt;/SPAN&gt;&lt;SPAN&gt;;i++){&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;&amp;nbsp; SPI_WAIT_TX_DATA; &lt;SPAN class="comment"&gt;//WAITS UNTIL THE TX EMPTY BUFFER FLAG GOES HIGH&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI class="alt"&gt;&lt;SPAN&gt;&amp;nbsp; SPI_RD_WR_REG = i+&lt;SPAN class="number"&gt;1&lt;/SPAN&gt;&lt;SPAN&gt;; &lt;/SPAN&gt;&lt;SPAN class="comment"&gt;//OUTPUT A1 ON SPI&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;&amp;nbsp; SPI_WAIT_RX_DATA; &lt;SPAN class="comment"&gt;//THOUGHT THIS WOULD THROTTLE THE LOOP - WAIT FOR DATA TO BE TX (RECEIVED)&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; r[i] = SPI_RD_WR_REG; &lt;SPAN class="comment"&gt;//READ REG TO UPDATE BUFFER&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; //Rong wrote&lt;BR /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI class="alt"&gt;&lt;SPAN&gt;&amp;nbsp; }&amp;nbsp; &lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;&amp;nbsp; &lt;SPAN class="comment"&gt;//delay_nop(100);&amp;nbsp; //ENOUGH TIME TO OUTPUT THE DATA....&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI class="alt"&gt;&lt;SPAN&gt;&amp;nbsp; TEST_HIGH;&amp;nbsp; &lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI class="alt"&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;&amp;nbsp; &lt;SPAN class="comment"&gt;//TRYING TO CLEAR BUFFER SO THAT CAN READ CURRENT SPI DATA&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI class="alt"&gt;&lt;SPAN&gt;&amp;nbsp; &lt;SPAN class="comment"&gt;//junk = SPI_STATUS; //read status&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;&amp;nbsp; junk = SPI_RD_WR_REG; &lt;SPAN class="comment"&gt;//MUST READ DATA IN ORDER FOR NEXT READS TO BE CORRECT&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI class="alt"&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;&amp;nbsp; &lt;SPAN class="keyword"&gt;for&lt;/SPAN&gt;&lt;SPAN&gt;(i=&lt;/SPAN&gt;&lt;SPAN class="number"&gt;0&lt;/SPAN&gt;&lt;SPAN&gt;;i&amp;lt;&lt;/SPAN&gt;&lt;SPAN class="number"&gt;4&lt;/SPAN&gt;&lt;SPAN&gt;;i++){&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI class="alt"&gt;&lt;SPAN&gt;&amp;nbsp; &lt;SPAN class="comment"&gt;//output and read 0xa0 - 0xa4&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;&amp;nbsp; SPI_WAIT_TX_DATA; &lt;SPAN class="comment"&gt;//WAITS UNTIL THE TX EMPTY BUFFER FLAG GOES HIGH&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI class="alt"&gt;&lt;SPAN&gt;&amp;nbsp; SPI_RD_WR_REG = j++; &lt;SPAN class="comment"&gt;//OUTPUT A1-a4 ON SPI&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;&amp;nbsp; SPI_WAIT_RX_DATA; &lt;SPAN class="comment"&gt;//wait for data to be tx&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI class="alt"&gt;&lt;SPAN&gt;&amp;nbsp; r[i] = SPI_RD_WR_REG; &lt;SPAN class="comment"&gt;//READ REG TO UPDATE BUFFER&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;&amp;nbsp; }&amp;nbsp; &lt;/SPAN&gt;&lt;/LI&gt;&lt;LI class="alt"&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;&amp;nbsp; TEST_LOW;&amp;nbsp; &lt;/SPAN&gt;&lt;/LI&gt;&lt;LI class="alt"&gt;&lt;SPAN&gt;&amp;nbsp; asm(&lt;SPAN class="string"&gt;"nop"&lt;/SPAN&gt;&lt;SPAN&gt;);&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;}&amp;nbsp; &lt;BR /&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 20 May 2014 06:52:45 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/kl25z-spi-basics/m-p/301099#M12619</guid>
      <dc:creator>xiangjun_rong</dc:creator>
      <dc:date>2014-05-20T06:52:45Z</dc:date>
    </item>
    <item>
      <title>Re: kl25z spi basics</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/kl25z-spi-basics/m-p/301100#M12620</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This document may help you out: &lt;A _jive_internal="true" href="https://community.nxp.com/docs/DOC-100784"&gt;L-Series SPI GPIO CS/SS&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Best regards,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Martyn&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;EDIT: Added updated source code for the document above.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 20 May 2014 16:23:34 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/kl25z-spi-basics/m-p/301100#M12620</guid>
      <dc:creator>martynhunt</dc:creator>
      <dc:date>2014-05-20T16:23:34Z</dc:date>
    </item>
    <item>
      <title>Re: Re: kl25z spi basics</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/kl25z-spi-basics/m-p/301101#M12621</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Martyn and Rong,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Rong you were correct that the buffer did need to be cleared.&amp;nbsp; Interestingly by adding the delay in my original version also fixed the issue I was having.&amp;nbsp; I am not sure why the delay would help in this case.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Martyn, thank for the very helpful document.&amp;nbsp; Wish I had seen this couple of days ago.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So, to ensure that the buffer is clear before running SPI routines I thought I would be safe put together a spi_clear_buf function as follows.&amp;nbsp; But, the buffer is only cleared when I use the delay routine.&amp;nbsp; Do you have why the delay would be required, is this expected?&amp;nbsp; Seems strange to me.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="c++" __jive_macro_name="code" class="jive_text_macro jive_macro_code _jivemacro_uid_14006067344496763" jivemacro_uid="_14006067344496763"&gt;
&lt;P&gt;void spi_clear_buf(){&lt;/P&gt;
&lt;P&gt;&amp;nbsp; uint8_t junk;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; while (SPI0_S &amp;amp; SPI_S_SPRF_MASK){&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //while the rx data is available flag is set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp; junk = SPI_RD_WR_REG;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; delay_nop(50);&amp;nbsp; // for some reason the delay is needed, why?&lt;/P&gt;
&lt;P&gt;&amp;nbsp; }&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 20 May 2014 17:27:00 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/kl25z-spi-basics/m-p/301101#M12621</guid>
      <dc:creator>mjg8t</dc:creator>
      <dc:date>2014-05-20T17:27:00Z</dc:date>
    </item>
    <item>
      <title>Re: kl25z spi basics</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/kl25z-spi-basics/m-p/301102#M12622</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In the uTasker KL project the SPI status is cleared (ex. for SPI1) by using:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;(volatile unsigned char)SPI1_S;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;(volatile unsigned char)SPI1_DL;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is required only once after the interface has been configured and no delays should be necessary.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The other thing to be aware of is not to have an SPI peripheral view open when stepping code since it will be reading and clearing flags in the process, which can cause unexpected behaviour.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Mark&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 20 May 2014 18:51:16 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/kl25z-spi-basics/m-p/301102#M12622</guid>
      <dc:creator>mjbcswitzerland</dc:creator>
      <dc:date>2014-05-20T18:51:16Z</dc:date>
    </item>
    <item>
      <title>Re: kl25z spi basics</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/kl25z-spi-basics/m-p/301103#M12623</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P style="font-weight: inherit; font-style: inherit; font-family: inherit;"&gt;Thanks Mark.&amp;nbsp; I have a couple follow up questions.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="font-weight: inherit; font-style: inherit; font-family: inherit;"&gt;&lt;SPAN style="color: #3d3d3d; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif;"&gt;"SPI status is cleared (ex. for SPI1) by using: &lt;/SPAN&gt;&lt;SPAN style="color: #3d3d3d; font-family: inherit; font-style: inherit;"&gt;&lt;STRONG&gt;(volatile unsigned char)SPI1_S;"&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-weight: inherit; font-style: inherit; font-family: inherit;"&gt;&lt;SPAN style="color: #3d3d3d; font-family: inherit; font-style: inherit;"&gt;&lt;STRONG&gt;&lt;BR /&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-weight: inherit; font-style: inherit; font-family: inherit;"&gt;1) Can you explain why ""clearing" the status allows the code to run without the need of the manual delay?&lt;/P&gt;&lt;P style="font-weight: inherit; font-style: inherit; font-family: inherit;"&gt;&lt;/P&gt;&lt;P style="font-weight: inherit; font-style: inherit; font-family: inherit;"&gt;2) How does declaring the &lt;SPAN style="color: #3d3d3d; font-family: inherit; font-style: inherit;"&gt;&lt;STRONG&gt;SPI_S &lt;/STRONG&gt;&lt;/SPAN&gt;register as volatile fix this scenario?&amp;nbsp; My understanding is that volatile ensures the compiler does not make any assumptions and optimizations for the declared variable.&amp;nbsp; Does this apply here?&lt;/P&gt;&lt;P style="font-weight: inherit; font-style: inherit; font-family: inherit;"&gt;&lt;/P&gt;&lt;P style="font-weight: inherit; font-style: inherit; font-family: inherit;"&gt;Thanks for helping me see the light here.&amp;nbsp; Hope to be a pro on the kinetis soon :smileyhappy:.&lt;/P&gt;&lt;P style="font-weight: inherit; font-style: inherit; font-family: inherit;"&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; color: #3d3d3d;"&gt;&lt;/P&gt;&lt;P style="font-weight: inherit; font-style: inherit; font-family: inherit;"&gt;&lt;/P&gt;&lt;P style="font-weight: inherit; font-style: inherit; font-family: inherit;"&gt;&lt;/P&gt;&lt;P class="jive-thread-reply-btn" style="margin: 26px 0 -10px; font-size: 0.9em; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; color: #3d3d3d;"&gt;&lt;/P&gt;&lt;DIV class="jive-thread-reply-btn-correct" style="font-weight: inherit; font-style: inherit; font-family: inherit;"&gt;&lt;A _jive_internal="true" href="https://community.nxp.com/thread/324070" style="margin: 0 20px 0 0; padding: 2px 14px 4px 28px; font-weight: bold; font-style: inherit; font-family: inherit; color: #6a737b; background-color: #f3f3f3;"&gt;kl25z spi basics&lt;/A&gt;&lt;P&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 21 May 2014 21:01:15 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/kl25z-spi-basics/m-p/301103#M12623</guid>
      <dc:creator>mjg8t</dc:creator>
      <dc:date>2014-05-21T21:01:15Z</dc:date>
    </item>
    <item>
      <title>Re: kl25z spi basics</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/kl25z-spi-basics/m-p/301104#M12624</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1. I can't explain why you needed a delay as I also can't actually explain why the status needs to be cleared since there is no reason why it should be set out of reset (it isn',t according to the user's manual and the bit may in fact actually be reading '0' - I don't remember the details) but practically it &lt;SPAN style="text-decoration: underline;"&gt;needs&lt;/SPAN&gt; to be done otherwise the first (ever) transfer is never correctly flagged as complete. Possibly it is an errata of sorts - but the workaround is harmless enough - and maybe it is by design (for some reason ?).&lt;/P&gt;&lt;P&gt;To clear the status, the SPI_S register needs to be read "with the flag set" (the first access) and then the data register needs to be read - therefore the two accesses.&lt;/P&gt;&lt;P&gt;Since I never needed a delay I can't explain why it also worked for you with one...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;2. SPI_S is already declared as volatile in the register definition but the codes help to make it clear that the access is a dummy read. The compiler may optimise the read since the result is not being used so volatile is certainly important. Sometimes it is written as &lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;dummy = SPI_S;&lt;/STRONG&gt; but there are often associated warnings that the result in &lt;STRONG&gt;dummy&lt;/STRONG&gt; is not used - a simple &lt;STRONG&gt;SPI_S;&lt;/STRONG&gt; should essentially be adequate (as long as the compiler understands that it may not optimise it away).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Mark&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 21 May 2014 21:36:50 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/kl25z-spi-basics/m-p/301104#M12624</guid>
      <dc:creator>mjbcswitzerland</dc:creator>
      <dc:date>2014-05-21T21:36:50Z</dc:date>
    </item>
    <item>
      <title>Re: kl25z spi basics</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/kl25z-spi-basics/m-p/301105#M12625</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks for the very insightful information!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Cheers.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 22 May 2014 16:11:26 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/kl25z-spi-basics/m-p/301105#M12625</guid>
      <dc:creator>mjg8t</dc:creator>
      <dc:date>2014-05-22T16:11:26Z</dc:date>
    </item>
  </channel>
</rss>

