Page 1 of 1

passing xc pointer to c function

Posted: Wed Feb 01, 2017 3:46 pm
by shaileshwankhede
Hi,
I am trying to use some memory between two parallel tasks and I am ensuring that same memory regions are never used at same time in these two threads.
But xc compiler throws parallel usage rules error. After going through some post, I decided to add c functionality to write to and read from memory regions.
To do this I am trying to pass unsafe pointer from xc file to C function, but then I get function mismatch error because of xC pointer and C pointer.
How to fix this?

Below is my simple code base:
In xc file:

Code: Select all

unsafe void bufferData(mic_array_frame_time_domain * unsafe audio)
{
	static uint16_t wrIdx = 0;
	fillMicData((mic_array_frame_time_domain*)audio, wrIdx);	//ERROR here!!!
	if(wrIdx < BUFFER_SIZE-1)
        wrIdx++;
    else {
        wrIdx = 0;
	}
}
in .h file

Code: Select all

void fillMicData(mic_array_frame_time_domain* audio, uint16_t wrIdx);
Thanks,
Shailesh

Re: passing xc pointer to c function

Posted: Fri Feb 03, 2017 12:31 pm
by peter
Pointers in C are always unsafe pointer equivalents. You need to ensure that when you include a C-header from an XC file that the function prototypes will be correctly interpreted. The easiest way is to use the extern "C" in your header so that XC knows the function is a C function:

Code: Select all

#ifdef __XC__
extern "C" {
#endif

// Your C-function declarations
void fillMicData(mic_array_frame_time_domain* audio, uint16_t wrIdx);

#ifdef __XC__
} // extern "C"
#endif


Re: passing xc pointer to c function

Posted: Fri Feb 03, 2017 2:24 pm
by shaileshwankhede
Thanks Peter,

Its working like a charm!

(Is option to mark thread as solved removed in new UI? I can't see that.)

Regards,
Shailesh

Re: passing xc pointer to c function

Posted: Fri Feb 03, 2017 2:40 pm
by peter
Glad that helped. I think you can press the green tick (top right of my answer).

Peter

Re: passing xc pointer to c function

Posted: Fri Feb 03, 2017 2:48 pm
by shaileshwankhede
There is no green tick in new UI.
You can check the screenshot.

Thanks,
Shailesh

Re: passing xc pointer to c function

Posted: Fri Feb 03, 2017 4:14 pm
by migueljds
Hi there, Sorry, about that.
I found the issue. It was a configuration setting that wasn't correctly set. Before only moderators could mark a topic as answered.
I've updated the settings so both moderators and the topic author can mark a particular topic as answered.
Thanks,
Miguel