Good morning
Is there a way to memcpy from SFR's in the C51?
I have tried to memcpy((char*)&buffer, (char*)&CAN0IF2DA1L, 8); but it doesn't work
Other things i tried
memcpy((char*)&buffer, CAN0IF2DA1L, 8); //error C214: illegal pointer conversion
memcpy((char*)&buffer, (char*)&CAN0IF2DA1L, 8); //error C189: '&' on bit/sfr illegal
Any ideas?
Richard
It's brilliant :)
Broeker said:Duff's Device
was about to say that - you beat me to it!
Broeker said:the only C code snippet infamous enough to carry a name
Algol has Jensen's Device
RichardT said:seems like the most efficient way of doing this is to copy all 8 bytes every time
Well, doing work that serves no purpose is very rarely the most efficient way of doing anything.
This also probably applies to the case at hand. Watch, be appalled, and learn: ;-)
// copy message into CAN0rx; switch(dlc) { case 8: m->Data[7] = CAN0IF2DB2H; case 7: m->Data[6] = CAN0IF2DB2L; case 6: m->Data[5] = CAN0IF2DB1H; case 5: m->Data[4] = CAN0IF2DB1L; case 4: m->Data[3] = CAN0IF2DA2H; case 3: m->Data[2] = CAN0IF2DA2L; case 2: m->Data[1] = CAN0IF2DA1H; case 1: m->Data[0] = CAN0IF2DA1L; case 0: }
This is one half of what may be the only C code snippet infamous enough to carry a name of its own: Duff's Device.
Andy Neil said:I tried googling the "CAN0IF2DA1L" from the OP, and found this: https://gist.github.com/petscare/3884991 - which does exactly that:
I have been using very similar code for a while now.
The can protocol gives the length of the packet (being 1 - 8 bytes) so I wanted to try copy the exact number of bytes into my rx buffer for processing.
No point passing the invalid characterst but it seems like the most efficient way of doing this is to copy all 8 bytes every time
Andy Neil said:eight direct assignments
I tried googling the "CAN0IF2DA1L" from the OP, and found this: https://gist.github.com/petscare/3884991 - which does exactly that:
// copy message into CAN0rx; m->Data[0] = CAN0IF2DA1L; m->Data[1] = CAN0IF2DA1H; m->Data[2] = CAN0IF2DA2L; m->Data[3] = CAN0IF2DA2H; m->Data[4] = CAN0IF2DB1L; m->Data[5] = CAN0IF2DB1H; m->Data[6] = CAN0IF2DB2L; m->Data[7] = CAN0IF2DB2H;
Andy Neil said:given that SFR space isn't indirectly accessible, I guess the whole question is moot?
Agreed
Andy Neil said:and, as we're only talking 8 bytes anyhow, eight direct assignments is almost certainly going to be the quickest solution.
Length is dynamic but in my case maximum of 8 bytes
Andy Neil said:Unless the chip has some DMA tricks ...
No tricks here
given that SFR space isn't indirectly accessible, I guess the whole question is moot?
RichardT said:memcpy((char*)&buffer, (char*)&CAN0IF2DA1L, 8 );
and, as we're only talking 8 bytes anyhow, eight direct assignments is almost certainly going to be the quickest solution.
Unless the chip has some DMA tricks ...
But RichardT seems to have lost interest.
Are the registers even contiguous?
*p++ = CAN0IF2DA1L;*p++ = CAN0IF2DA1H;
...
²erik malund - err ... I just posted those above!
You don't quite seem to have got the hang of this new forum, yet!
;-)
Note that you can edit your posts via the 'Actions' button:
and can now post pictures!
just made aware the links I previously used are dead, here are some others
community.nxp.com/.../DOC-334648 - 80C51 Family Architecture.pdf
community.nxp.com/.../DOC-334649 - 80C51 Family Hardware Description.pdf
community.nxp.com/.../DOC-334650 - 80C51 Family Programmer's Guide and Instruction Set.pdf
RichardT said:I am aware of the different address spaces but do not know how to create a pointer to the SFR address space
That statement is self-contradictory. Just about the single most important thing to learn about the different address spaces of an 8051 is that one cannot create a pointer to an SFR.
Andy Neil said:IIRC, SFR space is not indirectly addressable
It's been a long time since I last used an 8051 - but that recollection is correct:
From the Philips Semiconductors 80C51 Family Architecture document - linked earlier
²erik malund - Dan's site has been dead for some time now.
NXP do still seem to have archive copies of the so-called "bible" for the 8051:
https://community.nxp.com/docs/DOC-334648 - 80C51 Family Architecture.pdf
https://community.nxp.com/docs/DOC-334649 - 80C51 Family Hardware Description.pdf
https://community.nxp.com/docs/DOC-334650 - 80C51 Family Programmer's Guide and Instruction Set.pdf
Also on Dr. Linden McClure's page at the University of Colorado, Boulder:
http://ecee.colorado.edu/~mcclurel/
you need to familiarize yourself with the ;51
http://www.danlhenry.com/mfg/philips/docs/80C51_FAM_ARCH_1.pdf http://www.danlhenry.com/mfg/philips/docs/80C51_FAM_HARDWARE_1.pdf http://www.danlhenry.com/mfg/philips/docs/80C51_FAM_PROG_GUIDE_1.pdf
RichardT said:My Canbus receive routine needs to copy the incoming CAN message from SFR registers
Doesn't the chip maker give examples of doing that?
View all questions in Keil forum