passing a char pointer to an xc function.

Technical questions regarding the XTC tools and programming with XMOS.
Matt
Active Member
Posts: 50
Joined: Sat Feb 13, 2010 12:04 pm

passing a char pointer to an xc function.

Post by Matt »

Hi,

I am trying to pass a char pointer from the parse_http_request function which points to the incoming data to an xc function which transmits the data down channel. The function uses an array index to count through the data. I have looked through other code and it seems to be common practice but for some reason it is giving me an exception.

Below is the xc function.

Code: Select all

signed int xc_write_data_flash(chanend server, unsigned int address, unsigned int len, char data[] ) {
	server <: 12;
	server <: address;
	server <: len;
	for(int i = 0; i < len; i++)
	  {
		server <: data[i];
	  }
}
this is the code that calls the function which is in c

Code: Select all

void parse_http_request(httpd_state_t *hs, char *data, int len,
                                chanend IRserve, chanend  flash_serve)
{
  int i;
  int cmd;
  int port;
  int chnl;
  int size;
  char tmp[7];

  if (hs->dptr != NULL)
    return;
  
  if (hs->receive_state.left < hs->receive_state.len)
  {
	  xc_write_data_flash(flash_serve, 393216, len, data);
	  
and finally the code that receives that data

Code: Select all

static void writeDataToFlash(chanend client) {
  int i;
  int j;
  unsigned int address;
  unsigned int size;
  unsigned char data[XTCP_CLIENT_BUF_SIZE];
  
  client :> address;
  client :> size;
  for (i = 0; i<size ; i++)
  {
	  client :> data[i];
  }
I am not sure how to turn the char pointer back to an array so I can use the index to step through the data.

The current code breaks when I send the array value down the channel.

Please help

Matt.


User avatar
paul
XCore Addict
Posts: 169
Joined: Fri Jan 08, 2010 12:13 am

Post by paul »

Hi Matt,

In the header file that you include in your XC file you can do the following which should let you pass a char array into that C function:

Code: Select all

#ifdef __XC__
void parse_http_request(httpd_state_t &hs, char data[], int len,
                                chanend IRserve, chanend  flash_serve)
#else
void parse_http_request(httpd_state_t *hs, char *data, int len,
                                chanend IRserve, chanend  flash_serve)
#endif
The arrays / structures will be passed by reference, rather than copying them.

Hope that helps,
Paul

On two occasions I have been asked, 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
Matt
Active Member
Posts: 50
Joined: Sat Feb 13, 2010 12:04 pm

Post by Matt »

Hi Paul,

I am not sure I follow you. The problem seems to be in the XC file, but when passing a pointer. I think it is due to the fact that both ends of the channel need to be the same data type.

So if what you are saying needs to be done does that mean I need a similar entry in the header of the file that has the xc_write_data_flash function?

Code: Select all

signed int xc_write_data_flash(chanend server, unsigned int address, unsigned int len, char data[] ) {
   server <: 12;
   server <: address;
   server <: len;
   for(int i = 0; i < len; i++)
     {
         server <: data[i]; 
     }
}
The "server <: data;" is the line that raises the exception.

Ill have a go and get back if there is any progress. Is there anywhere that I can read up on this?

Thanks for your time

Matt.
User avatar
paul
XCore Addict
Posts: 169
Joined: Fri Jan 08, 2010 12:13 am

Post by paul »

Hi Matt,

It looks like I completely misunderstood your question! Sorry about that, probably the result of answering questions on a friday evening!

Quick question before I try and answer what you were really asking: Can you tell me the exception you are getting. Is it memory related or something else?

Try casting the chars to unsigned values and sending them down the channel and reversing the process at the other end (unsigned to char).

Another thing - to speed up communication of large amounts of data over channels and reduce the overhead, it is worth investigating channel transactions (see section 3.4 of the Programming in XC document)

Sorry for the confusion - hope this is a bit more helpful!
Paul

On two occasions I have been asked, 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
Matt
Active Member
Posts: 50
Joined: Sat Feb 13, 2010 12:04 pm

Post by Matt »

Hi Paul,

I ended up making a c function that returned the value of a position in an array. I also made one to set a value.

Code: Select all

char getarrayval(int idx, char* array)
{
	return array[idx];
}

void putarrayval(int idx, char* array, char val)
{
	array[idx] = val;
}
I made the following header file after reading some posts on the xlinkers site.
char getarrayval(int idx,REFERENCE_PARAM( char, array));

void putarrayval(int idx,REFERENCE_PARAM( char, array), char val);
Seems to have sorted out the problems.

My latest problem is receiving bad data from the flash on an xc2. I have been writing a program that receives a file via ethernet and stores it to the on board flash. I am using the routines in the AtmelOps.xc to store and retrieve the data. When I try to retrieve the data all I get is a bunch of FF's?

Do you always have to erase flash before you write to it?

Thanks

Matt
User avatar
paul
XCore Addict
Posts: 169
Joined: Fri Jan 08, 2010 12:13 am

Post by paul »

Hmm, creating a setter and getter in C is an interesting solution to that issue - glad you got it working though.

I am not sure why you are getting FF's when you read back from your flash. I am not clued up with any of our flash libraries - so it is probably worth starting a separate thread on that topic.

Cheers,
Paul

On two occasions I have been asked, 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.