Page 1 of 1

[SOLVED]usbFunctionSetup to read/write memory

Posted: Sun Aug 09, 2009 12:17 am
by maxi
I have been trying to read the contents of a specific memory location on an active device using only usbFunctionSetup.
The address is specified by wIndex in usb_control_msg on a simple endpoint 0 in request (based on custom-class example).
All I want is a single uchar value returned in the buffer.

Code: Select all

usbMsgLen_t usbFunctionSetup(uchar data[8])
{
   usbRequest_t *rq = (void *)data;
   switch(rq->bRequest)
   {
   case GET_ADDRESS_VALUE:      
      usbMsgPtr = (uchar *)((unsigned int)rq->wIndex.word);
      return 1;
   }
   return 0;
}


All I get returned in my buffer is zero, even when I know the value is not. I'm sure this must be possible as the CDC-IO example has something like this via telnet.
It is most likely my mis-understaning of usbMsgPtr as my knowledge of C-style casting is a little vague..

Would someone please tell me if or how I could make this work, I'd also like to be able to set values if that's possible.

Thanks in advance.

Re: usbFunctionSetup to read memory location

Posted: Sun Aug 09, 2009 6:20 am
by spiky
I dont have an answer but i propose an alternative.

How about creating a global array ? Pass that array to usbMsgPtr. In your Main loop or elsewhere , you can modify that array. Is there any particular reason you wish to read from a specific location ?

Re: usbFunctionSetup to read memory location

Posted: Sun Aug 09, 2009 1:49 pm
by maxi
Basically what I am trying to implement is a means of reading/altering the contents of certain registers; timers, pre-scalers, adc status etc.
I realise that I could simply set up a switch case with a whole bunch of different request id's, however my idea was, if I could read/write the contents
of a specific location I could theoretically change the value of any register. Bitwise operations could be performed in a two-step process.

The idea came from the CDC-IO example which allows you to read/write the registers via Telnet. I have studied the source code and I think accessing the memory is not such a problem, I'm just not sure how to pass this correctly to usbMsgPtr.

Re: usbFunctionSetup to read memory location

Posted: Mon Aug 10, 2009 12:02 am
by maxi
Problem solved! once again just a silly oversight on my part. I have been using the IO addresses from the data sheet not realising that in memory they are offset by 0x20 because of the reigsters. The following code works if anyone is interested.

Code: Select all

usbMsgLen_t usbFunctionSetup(uchar data[8])
{
   usbRequest_t *rq = (void *)data;
   switch(rq->bRequest)
   {
   case GET_ADDRESS_VALUE:
      usbMsgPtr = (uchar *)((unsigned int)rq->wIndex.word);
      return 1;

   case SET_ADDRESS_VALUE:
      *(uchar *)((unsigned int)rq->wIndex.word) = rq->wValue.bytes[0];
      return 1;
   }
    return 0;
}