<?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>S12 / MagniV MicrocontrollersのトピックPointers to Functions in a structure.</title>
    <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/Pointers-to-Functions-in-a-structure/m-p/180666#M6563</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm porting some code onto the 9S12 for handling CANOpen Object Dictionaries.&amp;nbsp; One of the fields is either a pointer to a rom constant, eerom location, a ram variable or a function.&amp;nbsp; Depending on one of the other fields the application either reads from the ram, rom or calls the eerom code with the eerom address or calls the function.&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm having trouble getting the compiler to generate what I want (rather than what I tell i.)&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt;// First the function that returns nothing with no parameters.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt;void MyFunc(void);&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt;typedef union _DICT_PTRS&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Pointers to objects */&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt;{&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt; unsigned char * pRam;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt; const unsigned char * pRom;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt; unsigned int EEAdr;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt; void __far * (* pFunc)(void);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt;}DICT_PTRS;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt;// Then the variable that will hold the data.&amp;nbsp; The initializer flags a compiler error &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt;// that the sizes don't match.&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt;DICT_OBJECT_TEMPLATE _db_device[3] = {&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt;&amp;nbsp; {0x1000,0x00,CONST,4,(unsigned char *)&amp;amp;rCO_DevType}, &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt;&amp;nbsp; {0x1001,0x00,RO,1,(unsigned char *)&amp;amp;uCO_DevErrReg},&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt; {0x1002,0x00,RO,4,(unsigned char *)&amp;amp;uCO_DevManufacturerStatReg}&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt;// {0x1005,0x00,FUNC | RW,6,(void __far&amp;nbsp; *)&amp;amp;_CO_COMM_SYNC_COBIDAccessEvent}&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt;};&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt;// So for now I try to load the variable directly.&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _db_device[0].p.pFunc = (void *)MyFunc;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (_db_device[0].p.pFunc)();&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&lt;SPAN class="mce_paste_marker"&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="mce_paste_marker"&gt;Breaking at the code where the function is assigned it's always the same.&amp;nbsp; The wrong value is stored in the stucture and the call picks yet another location relative to the PC.&amp;nbsp; I can work with the non function variables without any problems.&amp;nbsp; I've tried the various permuations of __far and * etc. but the code generated is the same every time and the funtion is not called because what's stored in the structure isn't correct.&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&lt;SPAN class="mce_paste_marker"&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 19 Nov 2012 17:03:19 GMT</pubDate>
    <dc:creator>jcdammeyer</dc:creator>
    <dc:date>2012-11-19T17:03:19Z</dc:date>
    <item>
      <title>Pointers to Functions in a structure.</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/Pointers-to-Functions-in-a-structure/m-p/180666#M6563</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm porting some code onto the 9S12 for handling CANOpen Object Dictionaries.&amp;nbsp; One of the fields is either a pointer to a rom constant, eerom location, a ram variable or a function.&amp;nbsp; Depending on one of the other fields the application either reads from the ram, rom or calls the eerom code with the eerom address or calls the function.&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm having trouble getting the compiler to generate what I want (rather than what I tell i.)&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt;// First the function that returns nothing with no parameters.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt;void MyFunc(void);&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt;typedef union _DICT_PTRS&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Pointers to objects */&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt;{&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt; unsigned char * pRam;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt; const unsigned char * pRom;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt; unsigned int EEAdr;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt; void __far * (* pFunc)(void);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt;}DICT_PTRS;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt;// Then the variable that will hold the data.&amp;nbsp; The initializer flags a compiler error &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt;// that the sizes don't match.&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt;DICT_OBJECT_TEMPLATE _db_device[3] = {&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt;&amp;nbsp; {0x1000,0x00,CONST,4,(unsigned char *)&amp;amp;rCO_DevType}, &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt;&amp;nbsp; {0x1001,0x00,RO,1,(unsigned char *)&amp;amp;uCO_DevErrReg},&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt; {0x1002,0x00,RO,4,(unsigned char *)&amp;amp;uCO_DevManufacturerStatReg}&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt;// {0x1005,0x00,FUNC | RW,6,(void __far&amp;nbsp; *)&amp;amp;_CO_COMM_SYNC_COBIDAccessEvent}&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt;};&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt;// So for now I try to load the variable directly.&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _db_device[0].p.pFunc = (void *)MyFunc;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier; font-size: 8pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (_db_device[0].p.pFunc)();&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&lt;SPAN class="mce_paste_marker"&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="mce_paste_marker"&gt;Breaking at the code where the function is assigned it's always the same.&amp;nbsp; The wrong value is stored in the stucture and the call picks yet another location relative to the PC.&amp;nbsp; I can work with the non function variables without any problems.&amp;nbsp; I've tried the various permuations of __far and * etc. but the code generated is the same every time and the funtion is not called because what's stored in the structure isn't correct.&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&lt;SPAN class="mce_paste_marker"&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 19 Nov 2012 17:03:19 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/Pointers-to-Functions-in-a-structure/m-p/180666#M6563</guid>
      <dc:creator>jcdammeyer</dc:creator>
      <dc:date>2012-11-19T17:03:19Z</dc:date>
    </item>
    <item>
      <title>Re: Pointers to Functions in a structure.</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/Pointers-to-Functions-in-a-structure/m-p/180667#M6564</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Initializer inits fisrt union member. It may be easier to use long int instead (long enough to hold largest union member), and convert data when writing/reading this field.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new, courier; font-size: 8pt;"&gt;What did you meant here:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new, courier; font-size: 8pt;"&gt;void __far * (* pFunc)(void);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new, courier; font-size: 8pt;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new, courier; font-size: 8pt;"&gt;Did you want to declare far pointer to void foo(void) or pointer to function, which returns far pointer to void? In Codewarrior we have this&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new, courier; font-size: 8pt;"&gt;void (*pFunc)(void); //pointer to void foo(void)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new, courier; font-size: 8pt;"&gt;void (* far pFunc)(void); // pointer to banked void foo(void). Far is not &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new, courier; font-size: 8pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //necessary in banked and large memory models&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new, courier; font-size: 8pt;"&gt;void * far (*pFunc)(void); // pointer to function, which returns void* to far data&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new, courier; font-size: 8pt;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 20 Nov 2012 05:47:38 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/Pointers-to-Functions-in-a-structure/m-p/180667#M6564</guid>
      <dc:creator>kef</dc:creator>
      <dc:date>2012-11-20T05:47:38Z</dc:date>
    </item>
    <item>
      <title>Re: Pointers to Functions in a structure.</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/Pointers-to-Functions-in-a-structure/m-p/180668#M6565</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I finally solved it in a manner that fits in with the processor architecture.&amp;nbsp; By making each a __far pointer the space is correctly reserved and the code generated sets up the page register correctly.&amp;nbsp; A lot of the other attempts compiled but would not set up the page register or program counter.&amp;nbsp; It pays here to understand the processor architecture.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Now with the two structures declared it's easy to create a ROM based table that has both values and or pointers to data. &lt;/P&gt;&lt;P&gt;Example below. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;DICT_OBJECT_TEMPLATE _db_pdo1_rx_comm[] = {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; {0x1400,0x00,CONST,1,(unsigned char *)&amp;amp;rMaxIndex1}, &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; {0x1400,0x01,RW | FUNC,4,(void __far *)&amp;amp;CO_COMM_RPDO1_COBIDAccessEvent} &lt;/P&gt;&lt;P&gt;};&lt;/P&gt;&lt;P&gt;&lt;SPAN class="mce_paste_marker"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The function is called like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _db_device[1].p.pFunc();&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Setting a breakpoint at the call shows how the processor does the load and indirect call to get to the actual function.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;void CO_COMM_RPDO1_COBIDAccessEvent(void) {&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for your feedback.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;John Dammeyer&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;typedef union _DICT_PTRS&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Pointers to objects */&lt;BR /&gt;{&lt;BR /&gt; const unsigned char&amp;nbsp; * __far pRom;&lt;BR /&gt; unsigned char * __far pRam;&lt;BR /&gt; unsigned int * __far EEAdr;&lt;BR /&gt; void (* pFunc)(void);&lt;BR /&gt;}DICT_PTRS;&lt;/P&gt;&lt;P&gt;// This is the scalar equivelant of a portion of the DICT_OBJ&lt;BR /&gt;typedef struct _DICTIONARY_OBJECT_TEMPLATE&lt;BR /&gt;{&lt;BR /&gt; unsigned int index;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR /&gt; unsigned char subindex;&lt;BR /&gt; unsigned char ctl;&lt;BR /&gt; unsigned int len;&lt;BR /&gt; DICT_PTRS p;&lt;BR /&gt;}DICT_OBJECT_TEMPLATE;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="mce_paste_marker"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 20 Nov 2012 06:17:00 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/Pointers-to-Functions-in-a-structure/m-p/180668#M6565</guid>
      <dc:creator>jcdammeyer</dc:creator>
      <dc:date>2012-11-20T06:17:00Z</dc:date>
    </item>
  </channel>
</rss>

