<?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>S32KのトピックRe: S32K144 problem reading pin state</title>
    <link>https://community.nxp.com/t5/S32K/S32K144-problem-reading-pin-state/m-p/1269617#M10711</link>
    <description>&lt;P&gt;Yes, I agree. I will ask for adding this to the documentation.&lt;/P&gt;</description>
    <pubDate>Wed, 28 Apr 2021 11:22:16 GMT</pubDate>
    <dc:creator>davidtosenovjan</dc:creator>
    <dc:date>2021-04-28T11:22:16Z</dc:date>
    <item>
      <title>S32K144 problem reading pin state</title>
      <link>https://community.nxp.com/t5/S32K/S32K144-problem-reading-pin-state/m-p/1263888#M10589</link>
      <description>&lt;P&gt;In S32K144 one can configure a pin as input and read its state as follows:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;bool read_input_pin(uint32_t port_offset, uint32_t pin_offset)
{
GPIO_Type* gpio[5] = GPIO_BASE_PTRS;
PORT_Type* port[5] = PORT_BASE_PTRS;

PCC-&amp;gt; PCCn[PCC_PORTA_INDEX + port_offset] = PCC_PCCn_CGC_MASK; // Enable clock to PORT X
gpio[port_offset]-&amp;gt;PDDR &amp;amp;= ~(1&amp;lt;&amp;lt;pin_offset); // Port pin: Data Direction= input
port[port_offset]-&amp;gt;PCR[pin_offset] = 0x00000110; // Port pin: MUX = GPIO, input filter enabled

uint32_t val = gpio[port_offset]-&amp;gt;PDIR &amp;amp; (1&amp;lt;&amp;lt;pin_offset);
return (0 != val);
}&lt;/LI-CODE&gt;&lt;P&gt;The above seems to work unless in the translated assembly code (with optimization O1)&amp;nbsp; the writing to PCR register is followed by immediate reading of PDIR register. The last part of the above function translates to the following assembly code:&lt;/P&gt;&lt;LI-CODE lang="c"&gt; 124 00be 5161     		str	r1, [r2, #20] &amp;lt;- Writes to PDDR
 125 00c0 55F8181C 		ldr	r1, [r5, #-24]
 126 00c4 4FF48870 		mov	r0, #272
 127 00c8 41F82400 		str	r0, [r1, r4, lsl #2] &amp;lt;- Writes to PCR
 128 00cc 1269     		ldr	r2, [r2, #16] &amp;lt;- Reads PDIR into r2
 129 00ce 1342     		tst	r3, r2 &amp;lt;- Checks pin bit with the value in r2 &lt;/LI-CODE&gt;&lt;P&gt;Even though a constant 3.3v is applied to the pin the test in the above assembly line 129 fails and the pin is assumed to have a low state. However, it is enough to either add a nop instruction between PCR write and PDIR read or to read PDIR register twice, the pin state starts to show up as 1. That is, any of the following workarounds help:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Workaround 1&lt;/STRONG&gt;. Adding NOP&lt;/P&gt;&lt;LI-CODE lang="c"&gt; 124 00be 4161     		str	r1, [r0, #20] &amp;lt;- Writes to PDDR as above
 125 00c0 53F8181C 		ldr	r1, [r3, #-24] 
 126 00c4 4FF48870 		mov	r0, #272
 127 00c8 41F82400 		str	r0, [r1, r4, lsl #2] &amp;lt;- Writes to PCR as above
 128              		.syntax unified
 129 00cc 00BF     		nop
 130              		.thumb
 131              		.syntax unified
 132 00ce 53F82C3C 		ldr	r3, [r3, #-44] &amp;lt;- Loads again GPIO_Type* into r3 again.
 133 00d2 1B69     		ldr	r3, [r3, #16] &amp;lt;- Reads PDIR into r3
 134 00d4 1A42     		tst	r2, r3 &amp;lt;- Test pin state of PDIR as above&lt;/LI-CODE&gt;&lt;P&gt;&lt;STRONG&gt;Workaround 2&lt;/STRONG&gt;. Reading PDIR twice&lt;/P&gt;&lt;LI-CODE lang="c"&gt; 124 00b8 5161     		str	r1, [r2, #20] &amp;lt;- Writes to PDDR as above
 125 00ba 55F8181C 		ldr	r1, [r5, #-24]
 126 00be 4FF48870 		mov	r0, #272
 127 00c2 41F82400 		str	r0, [r1, r4, lsl #2] &amp;lt;- Writes to PCR as above
 128 00c6 1169     		ldr	r1, [r2, #16] &amp;lt;- Reads PDIR
 129 00c8 1269     		ldr	r2, [r2, #16] &amp;lt;- !Reads PDIR again!
 130 00ca 1342     		tst	r3, r2 &amp;lt;- Same test as above&lt;/LI-CODE&gt;&lt;P&gt;So it seems that there should be some clock cycles between PCR write and PDIR read but I have not found any information about it in the manual.&lt;/P&gt;&lt;P&gt;Could people from NXP clarify this?&lt;/P&gt;&lt;P&gt;Thanks a lot in advance.&lt;/P&gt;</description>
      <pubDate>Sun, 18 Apr 2021 13:40:30 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S32K/S32K144-problem-reading-pin-state/m-p/1263888#M10589</guid>
      <dc:creator>davithakobyan</dc:creator>
      <dc:date>2021-04-18T13:40:30Z</dc:date>
    </item>
    <item>
      <title>Re: S32K144 problem reading pin state</title>
      <link>https://community.nxp.com/t5/S32K/S32K144-problem-reading-pin-state/m-p/1265256#M10615</link>
      <description>&lt;P&gt;I would say this delay is just caused by input buffer delay. It is needed to realize this are synchronous operation.&lt;BR /&gt;If you enable the input buffer, input may be sampled next clock and it sounds logically valid data will be available second clock after enabling.&lt;/P&gt;
&lt;P&gt;I guess you dont have enabled digital filter (PORT_Dxxx registers)?&lt;/P&gt;</description>
      <pubDate>Tue, 20 Apr 2021 15:32:00 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S32K/S32K144-problem-reading-pin-state/m-p/1265256#M10615</guid>
      <dc:creator>davidtosenovjan</dc:creator>
      <dc:date>2021-04-20T15:32:00Z</dc:date>
    </item>
    <item>
      <title>Re: S32K144 problem reading pin state</title>
      <link>https://community.nxp.com/t5/S32K/S32K144-problem-reading-pin-state/m-p/1265675#M10620</link>
      <description>&lt;P&gt;Thanks a lot for the feedback.&lt;/P&gt;&lt;P&gt;Yes, the digital filter was not enabled.&lt;/P&gt;&lt;P&gt;If i understood it correctly, when a pin is configured to be a digital input one needs to wait at least one clock cycle in order the input buffer becomes enabled, and this is the reason why reading the pin state via PDIR register is possible only on the second clock cycle?&lt;/P&gt;&lt;P&gt;It would be very helpful if the manual would indicate this.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Apr 2021 07:48:57 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S32K/S32K144-problem-reading-pin-state/m-p/1265675#M10620</guid>
      <dc:creator>davithakobyan</dc:creator>
      <dc:date>2021-04-21T07:48:57Z</dc:date>
    </item>
    <item>
      <title>Re: S32K144 problem reading pin state</title>
      <link>https://community.nxp.com/t5/S32K/S32K144-problem-reading-pin-state/m-p/1269617#M10711</link>
      <description>&lt;P&gt;Yes, I agree. I will ask for adding this to the documentation.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Apr 2021 11:22:16 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S32K/S32K144-problem-reading-pin-state/m-p/1269617#M10711</guid>
      <dc:creator>davidtosenovjan</dc:creator>
      <dc:date>2021-04-28T11:22:16Z</dc:date>
    </item>
  </channel>
</rss>

