<?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>CodeWarrior for MCUのトピックCW10: __near doesn't force function to non-banked address</title>
    <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/CW10-near-doesn-t-force-function-to-non-banked-address/m-p/213072#M8381</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm trying to force a function to be non-banked so I can store its address in a (void *).&amp;nbsp; I thought that this syntax:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;int __near foo1( void){&amp;nbsp;&amp;nbsp;&amp;nbsp; return 42;}&lt;/PRE&gt;&lt;P&gt;would be sufficient, and it was enough to quiet the compiler warnings when trying to assign the address of foo to a void pointer.&amp;nbsp; But when I checked the MAP file, it had been compiled to banked memory.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I ended up using this syntax:&lt;/P&gt;&lt;PRE&gt;#pragma CODE_SEG __NEAR_SEG NON_BANKEDint foo2( void){&amp;nbsp;&amp;nbsp;&amp;nbsp; return 42;}#pragma CODE_SEG DEFAULT&lt;/PRE&gt;&lt;P&gt;Why doesn't the first version work?&amp;nbsp; In my test, foo1 was banked and foo2 was not.&amp;nbsp; The __near keyword is certainly cleaner, especially for multi-platform code (we can define a macro NEAR to be __near when necessary, or a null macro otherwise).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;-Tom&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 09 Jun 2011 00:33:37 GMT</pubDate>
    <dc:creator>tomlogic</dc:creator>
    <dc:date>2011-06-09T00:33:37Z</dc:date>
    <item>
      <title>CW10: __near doesn't force function to non-banked address</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/CW10-near-doesn-t-force-function-to-non-banked-address/m-p/213072#M8381</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm trying to force a function to be non-banked so I can store its address in a (void *).&amp;nbsp; I thought that this syntax:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;int __near foo1( void){&amp;nbsp;&amp;nbsp;&amp;nbsp; return 42;}&lt;/PRE&gt;&lt;P&gt;would be sufficient, and it was enough to quiet the compiler warnings when trying to assign the address of foo to a void pointer.&amp;nbsp; But when I checked the MAP file, it had been compiled to banked memory.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I ended up using this syntax:&lt;/P&gt;&lt;PRE&gt;#pragma CODE_SEG __NEAR_SEG NON_BANKEDint foo2( void){&amp;nbsp;&amp;nbsp;&amp;nbsp; return 42;}#pragma CODE_SEG DEFAULT&lt;/PRE&gt;&lt;P&gt;Why doesn't the first version work?&amp;nbsp; In my test, foo1 was banked and foo2 was not.&amp;nbsp; The __near keyword is certainly cleaner, especially for multi-platform code (we can define a macro NEAR to be __near when necessary, or a null macro otherwise).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;-Tom&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 09 Jun 2011 00:33:37 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/CW10-near-doesn-t-force-function-to-non-banked-address/m-p/213072#M8381</guid>
      <dc:creator>tomlogic</dc:creator>
      <dc:date>2011-06-09T00:33:37Z</dc:date>
    </item>
    <item>
      <title>Re: CW10: __near doesn't force function to non-banked address</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/CW10-near-doesn-t-force-function-to-non-banked-address/m-p/213073#M8382</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;As a followup, what's the correct syntax for a typedef of a near function pointer?&amp;nbsp; The help file entry for "The __near Keyword" is quite confusing, especially this entry which doesn't make sense (1 __far plus 2 __near = 1 __near plus 2 __far?):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1" cellpadding="2" cellspacing="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;DIV class="pTB_TableBody"&gt;&lt;SPAN&gt;void __far *&lt;SPAN&gt;&lt;SPAN&gt;__&lt;SPAN&gt;near&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt; (*&lt;SPAN&gt;&lt;SPAN&gt;__&lt;SPAN&gt;near&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt; f)(void)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV class="pTB_TableBody"&gt;OK&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV class="pTB_TableBody"&gt;&lt;SPAN&gt;&lt;SPAN&gt;&lt;SPAN&gt;__&lt;SPAN&gt;near&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt; pointer to __far function returning a __far pointer to void&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;﻿&lt;/P&gt;&lt;P&gt;For my example below, what does a function pointer to a near, non-banked function look like?&amp;nbsp; The compiler doesn't like any of the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;typedef int __near (* foo_fn)( void)&lt;/P&gt;&lt;P&gt;typedef int (* __near foo_fn)( void)&lt;/P&gt;&lt;P&gt;typedef __near int (* foo_fn)( void)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 09 Jun 2011 03:25:53 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/CW10-near-doesn-t-force-function-to-non-banked-address/m-p/213073#M8382</guid>
      <dc:creator>tomlogic</dc:creator>
      <dc:date>2011-06-09T03:25:53Z</dc:date>
    </item>
    <item>
      <title>Re: CW10: __near doesn't force function to non-banked address</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/CW10-near-doesn-t-force-function-to-non-banked-address/m-p/213074#M8383</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Use the pragma for defining/declaring near functions. As you noticed the __near keyword does not change where an object is allocated, only how it is accessed (or dereferenced for a pointer). So __near should almost eclusively be used for pointers.&lt;/P&gt;&lt;P&gt;For functions it rarely makes sence, one rare situtation would be to use near calling convention for a function which is allocated in a bank, but for which it is known that all callers are in the same bank.&lt;/P&gt;&lt;P&gt;For example when all code of a C file gets placed in a specially named section allocated on a particular bank, then all static functions could be __near qualified. In "normal" code: use the pragma.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;About the function pointer, the __near is always after the * for any kind of pointer, including function pointers.&lt;/P&gt;&lt;P&gt;I noticed that the ide did flag a warning for the __near function pointer below, but the compiler does fully accept it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;typedef int (* __near foo_fn)( void);#pragma push#pragma CODE_SEG __NEAR_SEG NON_BANKEDint fun_function(void){    return 42;}#pragma popfoo_fn funPtr = fun_function;void main(void) {  for(;;) {   (void)funPtr();  }}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Daniel&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 09 Jun 2011 13:12:19 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/CW10-near-doesn-t-force-function-to-non-banked-address/m-p/213074#M8383</guid>
      <dc:creator>CompilerGuru</dc:creator>
      <dc:date>2011-06-09T13:12:19Z</dc:date>
    </item>
    <item>
      <title>Re: CW10: __near doesn't force function to non-banked address</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/CW10-near-doesn-t-force-function-to-non-banked-address/m-p/213075#M8384</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Daniel,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In my case, I want to pass a function pointer in as the "void *context" parameter of an existing API.&amp;nbsp; In order for that to work, the function needs to be in non-banked memory so it fits in the 16-bit void pointer and I can cast it back out to the function pointer.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to say that it's inconvenient to require pragmas to force a function into non-banked memory, and it certainly makes writing code that compiles on multiple embedded platforms more difficult -- I need to wrap the pragmas in #if/#endif blocks so they only apply on the Freescale target.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regarding the function pointer declaration, after reading up on error C18005, I've learned that this is the correct syntax for a near pointer to a near function:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;typedef int __near (* __near foo_fn)( int param);&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;If the return type is a far pointer though, it gets ugly:&lt;/P&gt;&lt;PRE&gt;typedef void __near * __far (* __near bar_fn)( int param);&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Come on.&amp;nbsp; It's so confusing that the help file even gets it wrong on the page for "The __near keyword" (see my first message in this thread).&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Jun 2011 03:57:56 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/CW10-near-doesn-t-force-function-to-non-banked-address/m-p/213075#M8384</guid>
      <dc:creator>tomlogic</dc:creator>
      <dc:date>2011-06-10T03:57:56Z</dc:date>
    </item>
  </channel>
</rss>

