Function pointer / callback function serial communication

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Function pointer / callback function serial communication

Jump to solution
2,895 Views
Peverix
Contributor I

Hello everyone

 

Since a few weeks i start to do programming  for freescale MCU's ( i use MC9S08AW ) . 

About 15 years ago i did some C/C++ programming under a MS-DOS enviroment and then switch to windows programming using Delphi. I still know C but pointers stuff is very far in my head.

 

I want to make a general communication library that I can use in all my projects. The freescale specific stuff is working. I can send and receive data from the SCI but I can't manage to get working the callback function. The function is called but the parameters contain rubish data.

 

This is what I have done, sorry for the long post.

 

- define my callback ( comm-lib.c )

typedef void (*com_callback)(const unsigned int iReceived);

 

- declare internal callback ( comm-lib.c )

volatile static com_callback ReceivedData; 

 

- create function to assign call back  ( comm-lib.c )

void uart_setcallback( void (*my_callback )(const unsigned int iReceived) ); 

 

- declare my call back ( main.c )

com_callback com_binnen ( const unsigned int iReceived );

 

- and it implementation ( main.c )

com_callback com_binnen ( const unsigned int iReceived )

{

  if ( iReceived == 2 )

  {

 

  }

}

 

- assign the callback ( main.c )

uart_setcallback ( *com_binnen ); 

 

When compiling i get a warning "Indirection to different types .................." but its compiling.

 

Can any one help me with what i'm doing wrong

Thanks

Peter 

Labels (1)
Tags (1)
0 Kudos
1 Solution
1,036 Views
kef
Specialist I

- define my callback ( comm-lib.c )

typedef void (*com_callback)(const unsigned int iReceived);

const in parameter list is bit odd.

 

- declare internal callback ( comm-lib.c )

volatile static com_callback ReceivedData; 

To be more precise, you not declare callback ^^ here, but define variable ReceivedData, which is a pointer to your callback routine.

 

- create function to assign call back  ( comm-lib.c )

void uart_setcallback( void (*my_callback )(const unsigned int iReceived) ); 

 You typedefed com_callback. You may retype this ^^ line simpler

 

void uart_setcallback( com_callback my_callback); 

 

- declare my call back ( main.c )

com_callback com_binnen ( const unsigned int iReceived );

 

You declared ^^ here function com_binnen returning pointer to callback routine.

 

- and it implementation ( main.c )

com_callback com_binnen ( const unsigned int iReceived )

{

  if ( iReceived == 2 )

  {

 

  }

}

 

- assign the callback ( main.c )

uart_setcallback ( *com_binnen ); 

 

When compiling i get a warning "Indirection to different types .................." but its compiling.

 

Can any one help me with what i'm doing wrong

Thanks

Peter 


// define type com_callback, that will be a pointer to callback routine with following characteristics:

//  return type - void

//  arguments - unsigned int

typedef void (*com_callback)(unsigned int iReceived);

 

 

//Define callback function foo1

void foo1(unsigned int i)

{

}

 

//Define callback function foo2

void foo2(unsigned int i)

{

}

 

 

//Define pointer to callback routine, coult be static, volatile and combinations

com_callback Callback;

 

// routine to set Callback variable 

void SetCallback(com_callback cb)

{

   Callback = cb;

}

 

void test(void)

{

    // set Callback directly

    Callback = foo1;

 

   // call Callback

   Callback(123);

 

    // set Callback using SetCallback

    SetCallback(foo1);

 

    SetCallback(foo2);

 

   // call Callback

   Callback(123);

}

View solution in original post

0 Kudos
4 Replies
1,037 Views
kef
Specialist I

- define my callback ( comm-lib.c )

typedef void (*com_callback)(const unsigned int iReceived);

const in parameter list is bit odd.

 

- declare internal callback ( comm-lib.c )

volatile static com_callback ReceivedData; 

To be more precise, you not declare callback ^^ here, but define variable ReceivedData, which is a pointer to your callback routine.

 

- create function to assign call back  ( comm-lib.c )

void uart_setcallback( void (*my_callback )(const unsigned int iReceived) ); 

 You typedefed com_callback. You may retype this ^^ line simpler

 

void uart_setcallback( com_callback my_callback); 

 

- declare my call back ( main.c )

com_callback com_binnen ( const unsigned int iReceived );

 

You declared ^^ here function com_binnen returning pointer to callback routine.

 

- and it implementation ( main.c )

com_callback com_binnen ( const unsigned int iReceived )

{

  if ( iReceived == 2 )

  {

 

  }

}

 

- assign the callback ( main.c )

uart_setcallback ( *com_binnen ); 

 

When compiling i get a warning "Indirection to different types .................." but its compiling.

 

Can any one help me with what i'm doing wrong

Thanks

Peter 


// define type com_callback, that will be a pointer to callback routine with following characteristics:

//  return type - void

//  arguments - unsigned int

typedef void (*com_callback)(unsigned int iReceived);

 

 

//Define callback function foo1

void foo1(unsigned int i)

{

}

 

//Define callback function foo2

void foo2(unsigned int i)

{

}

 

 

//Define pointer to callback routine, coult be static, volatile and combinations

com_callback Callback;

 

// routine to set Callback variable 

void SetCallback(com_callback cb)

{

   Callback = cb;

}

 

void test(void)

{

    // set Callback directly

    Callback = foo1;

 

   // call Callback

   Callback(123);

 

    // set Callback using SetCallback

    SetCallback(foo1);

 

    SetCallback(foo2);

 

   // call Callback

   Callback(123);

}

0 Kudos
1,036 Views
Peverix
Contributor I

Thank you already for you clear explination about function pointers.

Now I start to understand how it is working again and why i was gettng the compiler warning.  

 

How ever even when i try you simple example , it is going wrong.

When debugging and calling CallBack(123) it jumps to the foo1 or foo2 function but the local variable i doesn't have the right value 123 but some rubush value. 

 

Maybe it have someting to do with the order the values or put on the stack.

I remember from before you have cdcecl and pascal ( but this is windows i don't know if this also exists with freescale ).

 

Peter 

 

 

0 Kudos
1,036 Views
kef
Specialist I

Do you mean debugger shows worng i value? Because I just tried my example. To check code is working, I defined static int varible and made callbacks writing their argument to this static variable. Code works properly, but while in callback, debugger shows odd argument values.

In CW for HCS08, functions receive single int argument in HX pair. Check if contents of HX pair is correct or not. It is the most easiest for debugger to inspect variables when variables are stored in RAM. In cases when store to RAM is optimized out, you may have problems inspecting your variables...

0 Kudos
1,036 Views
Peverix
Contributor I

Thanks again men, indeed it was only a debugger problem showing odd values for i.

 

When i declare static variable and fill in this it is working.

I was looking 1,5 day for a problem that wasn't one. But I learned much again those day .

 

 

 

0 Kudos