|
|
View previous topic :: View next topic |
Author |
Message |
adcor
Joined: 21 Feb 2008 Posts: 31
|
SOLVED: PIC24 pointer types do not match |
Posted: Mon Mar 12, 2018 6:19 pm |
|
|
Maybe is a trivial error I do not see, if someone can spot it for me. I am working on a project with PIC24FJ128GA006 and CCS 5.076 compiler.
The compiler complains of mismatching pointer types. Even a strlen can not work. On a PIC18F4550 platform without _attribute_((packed)) it works.
The code is quite huge but here are my defines:
Data definition:
Code: |
#define ALL_MENU 39 //total menus
typedef struct __attribute__((packed)){
unsigned int8 MenuID;
unsigned int8 SubMenu;
unsigned int8 MenuName[23];
} TypeMenu;
TypeMenu MenuPointer[3];
TypeMenu MenuGeneral[ALL_MENU]
={{_,_BEGIN,"OPTIONS"},
{_MAIN2,_BEGIN,"INFO LOGS"},
{_MAIN3,_BEGIN,"DATE / TIME CLOCK"},
{_SUBMENU_1A,_,"Heat Operating Mode"},
{_SUBMENU_1AA,_SUBMENU_1A,"Preheat Mode"},
{_SUBMENU_1AB,_SUBMENU_1A,"Hi Temp. Only Mode"},
{_SUBMENU_1AC,_SUBMENU_1A,"Economy Mode"},
{_SUBMENU_1B,_,"Hi Temp. Duration"},
{_SUBMENU_1C,_,"Max NonStop Steam"},
{_SUBMENU_1CA,_SUBMENU_1C,"Range"},
{_SUBMENU_1CB,_SUBMENU_1C,"Mode ON/OFF"},
{_SUBMENU_1D,_,"Stop Switch Override "}, //22
{_SUBMENU_1E,_,"Room Overheat Alarm"},
{_SUBMENU_1EA,_SUBMENU_1E,"Temp. Range"},
{_SUBMENU_1EB,_SUBMENU_1E,"Mode:Enable/Disable"},
{_SUBMENU_1EC,_SUBMENU_1E,"Unit of Measure"},
{_SUBMENU_1ED,_SUBMENU_1E,"Relay Switch NO/NC"},
{_SUBMENU_1F,_,"Aux. Relay Switch"},
{_SUBMENU_1FA,_SUBMENU_1F,"Normally Open/Close"},
{_SUBMENU_1FB,_SUBMENU_1F,"Mode: ON/OFF"},
{_SUBMENU_1G,_,"Water Fill Time"},
{_SUBMENU_1GA,_SUBMENU_1G,"Time Between Fills"},
{_SUBMENU_1GB,_SUBMENU_1G,"Fill Duration"},
{_SUBMENU_1H,_,"Flush Parameters"},
{_SUBMENU_1HA,_SUBMENU_1H,"Flush Duration"},
{_SUBMENU_1HB,_SUBMENU_1H,"Flush Wait Time"},
{_SUBMENU_1HC,_SUBMENU_1H,"Flush Quantity"},
{_SUBMENU_1I,_,"Min. Contactor Cycle "}, //22
{_SUBMENU_1J,_,"Scent Pump Override"},
{_SUBMENU_1K,_,"Keep Boiler Warm"},
{_SUBMENU_1KA,_SUBMENU_1K,"Heat ON Time"},
{_SUBMENU_1KB,_SUBMENU_1K,"Wait Time"},
{_SUBMENU_1KC,_SUBMENU_1K,"Mode: ON/OFF"},
{_SUBMENU_1L,_,"Save All?"},
{_SUBMENU_2A,_MAIN2,"Error Codes"},
{_SUBMENU_2B,_MAIN2,"Service Log"},
{_SUBMENU_2C,_MAIN2,"Enable logging"},
{_SUBMENU_3A,_MAIN3,"Time/Date Set"},
{_SUBMENU_3B,_MAIN3,"Enable DST"}
};
|
Quote: |
Here is the functions I call:
|
Code: |
printf(lcd_putc," %s",MenuGeneral[0].menuName); // no print out
void foo (void) {
unsigned int8 len_0, buff_0[20];
len_0=strlen(MenuGeneral[0].MenuName); // trim strings to 19
for(i=0;i<19;i++) buff_0[i]=MenuPointer[0].MenuName[i];
buff_0[19]='\0';
printf("\f%u %s",len_0,buff_0);} // prints 0 for len_0 and nothing for buff_0
|
Last edited by adcor on Tue Mar 13, 2018 5:02 pm; edited 1 time in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Mar 12, 2018 7:58 pm |
|
|
I made the test program shown below, and got the mismatched pointer
types error.
I was able to get to work by casting the strlen parameter, as shown in
bold below:
Quote: | #include <18F46K22.h>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOPBADEN
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)
#include <string.h>
#define ALL_MENU 39 //total menus
typedef struct __attribute__((packed)){
unsigned int8 MenuID;
unsigned int8 SubMenu;
unsigned int8 MenuName[23];
} TypeMenu;
TypeMenu MenuGeneral[3];
TypeMenu MenuPointer[3];
//-------------------------------
void foo(void)
{
unsigned int8 len_0, buff_0[20];
int8 i;
len_0 = strlen((unsigned int8 *)MenuGeneral[0].MenuName);
for(i=0; i<19; i++)
buff_0[i] = MenuPointer[0].MenuName[i];
buff_0[19]= '\0';
printf("\f%u %s", len_0, buff_0);
}
//=================================
void main(void)
{
foo();
while(TRUE);
}
|
|
|
|
adcor
Joined: 21 Feb 2008 Posts: 31
|
|
Posted: Mon Mar 12, 2018 9:56 pm |
|
|
Thank you, that cleared my errors but still my prints do not work, here what is happening. If I declare an array 3 elements of TypeMenu structure, (MenuPointer[3]) as in the code and have a const type as well: Code: |
TypeMenu MenuPointer[3];
const TypeMenu MenuGeneral[3]
={{_,_BEGIN,"OPTIONS"},
{_MAIN2,_BEGIN,"INFO LOGS"},
{_MAIN3,_BEGIN,"DATE / TIME CLOCK"};
|
and then:
Code: |
MenuPointer[0]=MenuGeneral[0];
printf("\f MP=%s MG=%s", MenuPointer[0].MenuName,MenuGeneral[0].MenuName);
|
There is no string printed for MenuPointer[0], only for MenuGeneral.
Now if I declare just one structure:
Code: |
TypeMenu MenuPointer0;
&MenuPointer0=MenuGeneral[0]; // use of &
printf("\f MP=%s MG=%s", MenuPointer0.MenuName,Menugeneral[0].MenuName);
|
it prints out for both strings. If I dereference the array as &MenuPointer[0] it does not work. Again this is happening with PIC24 only. I did not have to use attrib_packed or extra casting on a 18F4550. Thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Mar 12, 2018 10:31 pm |
|
|
I have a question about your use of 'const' for the MenuGeneral array.
Do you want to use 'const' in the CCS sense of "store the array in flash
memory", or do you want to use it in the ANSI sense of putting the
array in RAM, but make it be read-only ? |
|
|
adcor
Joined: 21 Feb 2008 Posts: 31
|
|
Posted: Mon Mar 12, 2018 10:47 pm |
|
|
Just in RAM, read only. Thanks. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Mar 12, 2018 11:17 pm |
|
|
Try adding this line after the #include line for your PIC.
I don't guarantee it will solve all your problems, but you should try it.
With the 18F46K22, I found that if I used that line, and removed the
packed attribute, then it worked. |
|
|
adcor
Joined: 21 Feb 2008 Posts: 31
|
|
Posted: Mon Mar 12, 2018 11:58 pm |
|
|
Thanks for all your help, but it did not work, it created other dozens of errors throughout the code. For now I'll stick with PCH/PIC18, it seems much more reliable. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19512
|
|
Posted: Tue Mar 13, 2018 4:14 am |
|
|
ANSI doesn't seem to force RAM for the CONST on PIC24's. Use:
#DEVICE CONST=READ_ONLY
Instead, which seems to work.
However there are some major caveats. When you use 'packed' things like INT16's, can be generated on byte boundaries. It will cause problems with all sorts of functions.
Does your menu really have to use a 23byte name?. If this was 22 or 24, then the whole structure would move back to being a word aligned variable, and you can get rid of packed..... |
|
|
adcor
Joined: 21 Feb 2008 Posts: 31
|
SOLVED : PIC24 pointer type do not match |
Posted: Tue Mar 13, 2018 5:01 pm |
|
|
yeap, you nailed it. The size of menu array was increased to 24 and packed attribute was removed. With the use of #DEVICE CONST=READ_ONLY I got it working. Good lesson to keep data word aligned for these beasts. Thank you guys for all your support! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19512
|
|
Posted: Wed Mar 14, 2018 2:11 am |
|
|
Yes. The only time you should really use 'packed', is if you are generating a structure that needs an alignment to work with something 'else' outside the chip. Then go back to word aligning for your own stuff inside....
Glad it is now working. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|