Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Why this gives "Illegal Pointer Conversion" error?

Status
Not open for further replies.

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,524
Helped
540
Reputation
1,078
Reaction score
524
Trophy points
1,393
Activity points
0
Why this gives "Illegal Pointer Conversion" error?

I am using mikroC PRO PIC 6 and I am getting error for my function.

The UART1_Init() and UART2_Init() library function(s) prototypes as mentioned in mikroC PRO PIC help is


Code C - [expand]
1
void UARTx_Init(const unsigned long baud_rate);




Code C - [expand]
1
2
3
4
5
6
void UARTx_Init(void(*fPtr)(const unsigned long), const unsigned long baud_rate) {
     (*fPtr)(baud_rate);
     Delay_ms(200);
}
 
UARTx_Init(&UART1_Init, 4800);

 

Re: Why this gives "Illegal Pointer Conversion" error?

Why this gives "Illegal Pointer Conversion" error?

I am using mikroC PRO PIC 6 and I am getting error for my function.

The UART1_Init() and UART2_Init() library function(s) prototypes as mentioned in mikroC PRO PIC help is


Code C - [expand]
1
void UARTx_Init(const unsigned long baud_rate);




Code C - [expand]
1
2
3
4
5
6
void UARTx_Init(void(*fPtr)(const unsigned long), const unsigned long baud_rate) {
     (*fPtr)(baud_rate);
     Delay_ms(200);
}
 
UARTx_Init(&UART1_Init, 4800);


Is the code you've posted yours or the examples from the mikroC Pro documentation?

If not your code, then please post it.

BigDog

- - - Updated - - -

The following function prototype:


Code C - [expand]
1
void UARTx_Init(const unsigned long baud_rate);



does not correspond to the following function definition:


Code C - [expand]
1
2
3
4
void UARTx_Init(void(*fPtr)(const unsigned long), const unsigned long baud_rate) {
     (*fPtr)(baud_rate);
     Delay_ms(200);
}



They do not refer to the same function and are examples of function/method overloading, which is permitted in C++, but not Standard/ISO C.

What version of mikroC Pro are you currently using?

Are you sure the following:


Code C - [expand]
1
void UARTx_Init(const unsigned long baud_rate);



Is not a generic function prototype for:


Code C - [expand]
1
2
3
4
5
UART1_Init(const unsigned long baud_rate);
 
UART2_Init(const unsigned long baud_rate);
 
...



Etc?

What are you attempting to accomplish?


BigDog
 
Re: Why this gives "Illegal Pointer Conversion" error?

This is the function prototype for UART1_Init() and UART2_Init mikroC PRO PIC library functions.


Code C - [expand]
1
void UARTx_Init(const unsigned long baud_rate);




This is the function I have created which uses function pointer. I am using UART1 and UART2 in my project and have to initialize both and so I created the below function so that I can pass the address of mikroC PRO PIC library function(s) UART1_Init and UART2_Init.


Code C - [expand]
1
2
3
4
5
6
void UARTx_Init(void(*fPtr)(const unsigned long), const unsigned long baud_rate) {
     (*fPtr)(baud_rate);
     Delay_ms(200);
}
 
UARTx_Init(&UART1_Init, 4800);




What are you attempting to accomplish?



The same can be accomplished using


Code C - [expand]
1
2
UART1_Init(4800);
UART2_Init(9615);



but want to do that using function pointer.
 

I just noticed you mentioned you are using the new version 6.0 of mikroC Pro, so I downloaded the "new" manual which as it turns out is the same manual as version 5.0. :shock:

Same page numbers, etc.

Most likely the issue is the lack of type declaration of your integer literals.

You might try the following:


Code C - [expand]
1
UARTx_Init(&UART1_Init, 4800UL);



Or


Code C - [expand]
1
UARTx_Init(&UART1_Init, (const unsigned long)4800);



Or mikroC Pro's equivalent.


Technically, the address of operator (&) is not required, as the function name alone returns its address or pointer to itself.

However, with mikroC Pro, you never know, as it is one of the least adherent to ANSI/ISO standards (C89, C99).

BigDog
 
I tried both methods mentioned by you but still it gives the same error. I have attached mikroC project files.

However, with mikroC Pro, you never know, as it is one of the least adherent to ANSI/ISO standards (C89, C99).

I have another function (shown below) where I have used function pointer and calling UART1_Write_Text and UART2_Write_Text mikroC library functions and it works.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
void SendConstATCmd(void(*fptr2UARTx_Write_Text)(char *), char *pPtr2gUartBuffer, const char *pAtCmd, const char *pAtResponse,
                       const char *pAtErr, char *pCopyConst2RamBuffer, unsigned int pTimeOutSec,
                       unsigned int *pPtr2gUartBufferIndex, unsigned char *pPtr2gAttemptFlag, unsigned char pClearBuffersFlag) {
 
      unsigned char lCounter = 0, lAttemptCounter = 0, lAttempt = 0, lClearBuffersFlag = 0;
 
      lClearBuffersFlag = pClearBuffersFlag;
      
      while(strstr(pPtr2gUartBuffer, CopyConst2Ram(pCopyConst2RamBuffer, pAtResponse)) == 0) {
 
                (*fptr2UARTx_Write_Text)(CopyConst2Ram(pCopyConst2RamBuffer, pAtCmd));
                
                while(lCounter < pTimeOutSec) {
                
                        Delay_ms(1000);
                        ++lCounter;
                }
 
                *pPtr2gUartBufferIndex = 0;
                lCounter = 0;
 
                if((++lAttemptCounter > 5) || (strstr(pPtr2gUartBuffer, CopyConst2Ram(pCopyConst2RamBuffer, pAtErr)) != 0)) {
                
                        GSM_RST = 0;
                        Delay_ms(500);
                        GSM_RST = 1;
                        
                        Delay_ms(4000);
                        lAttemptCounter = 0;
                        ++lAttempt;
 
                        Set_Buffer(pPtr2gUartBuffer, gNullChar);
                        Set_Buffer(pCopyConst2RamBuffer, gNullChar);
                }
 
                if(lAttempt == 2){ *pPtr2gAttemptFlag = 1; break; }
      }
 
      if(lClearBuffersFlag) {
      
             Set_Buffer(pPtr2gUartBuffer, gNullChar);
             Set_Buffer(pCopyConst2RamBuffer, gNullChar);
      }
 
      *pPtr2gUartBufferIndex = 0;
}

 

Attachments

  • VTS.rar
    24.1 KB · Views: 62

You might try something like:


Code C - [expand]
1
2
3
4
5
6
7
8
9
const unsigned long BAUD1 = 4800UL;
const unsigned long BAUD2 = 9600UL;
 
...
...
 
UARTx_Init(&UART1_Init, BAUD1);
 
UARTx_Init(&UART2_Init, BAUD2);



I'll take a look at your code.

BigDog
 
I've looked over your code and examined the compiler documentation.

I believe the clue to the problem is the following Note in the documentation:

Note : Calculation of the UART baud rate value is carried out by the compiler, as it would produce a relatively large code if performed on the library level.
Therefore, compiler needs to know the value of the parameter in the compile time. That is why this parameter needs to be a constant, and not a variable.

Which may indicate the following routines:

Code:
void UARTx_Init(const unsigned long baud_rate);

May in fact be a macro which is preprocessed by the compiler at compile time and can neither be calculated at runtime nor function pointer passed.

Unfortunately, just another hazard of stemming from the lack of source code.

You could always write your own UART routines, which could then be used to implement your requirements.


BigDog
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top