CRC16 routine problem
Damien Allen
Posts: 103
Hi, i'm new to the prop and programming in general and i've been trying to write a modbus slave object in spin and i'm having trouble generating a correct crc.
I've attached my routine with the test i've been using, it must be something simple. Please can someone point me in the right direction or if i'm re-inventing the wheel as regards the to CRC or modbus object please point me in the direction of that also.
Many thanks
Damo
I've attached my routine with the test i've been using, it must be something simple. Please can someone point me in the right direction or if i'm re-inventing the wheel as regards the to CRC or modbus object please point me in the direction of that also.
Many thanks
Damo
spin
1K
Comments
Edit: that worked!
Post Edited (Damien Allen) : 3/17/2007 4:18:10 PM GMT
I want the routine to be fast but I am even less proficient in ASM as I am in spin so that is a no-go for me.
This routine is for modbus messages so i hope it is fast enough.
I'm curious, there are a few different forms of CRC16... is this really CRC16-CCITT?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--Jeff Martin
· Sr. Software Engineer
· Parallax, Inc.
What I meant is that some of your statements could be combined and some of the temporary variables eliminated. Table lookup is also an option and can be done completely in Spin. The "programming guru's advice" is to make it work first, then make it work correctly, then worry about speed.
Jeff, I believe it is just CRC-16, the routine roughly stays the same its just the polynomial that changes
CRC-16 uses $A001 as the poly, whereas CRC16-CCITT uses $8404.
Does that satisfy your curiousity?
You'd call this with "c := crc16(@data,8)". If you wanted to start at the 6th byte, you'd call "c := crc16(@data+5,8)".
is there an easier way to reverse the byte order? the code i am using is below, again probably inelegant and slow but it works
temp2 := (result >> 8)
result := (result <<8) | temp2
My project requires me to have variables available across cogs as my modbus slave driver will reside in one cog and my main program which reads temperatures and stores them in the aforementioned variables in another.
My modbus slave must be able recieve a request and send the value of one of those variables back.
How or where do i declare these variables?? In the DAT section of my top level spin file?
Many thanks
Damo
There's really not a great answer for this. The variables probably belong in the top level spin file, but there's no clean way to pass knowledge of these variables lower down in the hierarchy. There's not even a clean way to define these variables in a object by themselves and pass that knowledge upwards since multiple uses of the same object create multiple instances of their variables.
Probably the best/easiest way to do this is to define all the variables in a "globalValues" object in a DAT section there. You would need a "set" and "get" method for each separate variable. The "set" and "get" method could take one or more subscripts as parameters if you are implementing an array. This object could be referenced in any object that needs access to the variables and, at least, only one copy of the code would be present (and only one copy of the DAT section). There are other ways, like allocating the variables yourself in upper memory and having constant declarations for each variable's address, then using byte[noparse]/noparse, word[noparse]/noparse, and long[noparse]/noparse to access the values. Download a copy of the Propeller OS for an example of this (in the OS_loader.spin object).
Thanks.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--Jeff Martin
· Sr. Software Engineer
· Parallax, Inc.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--Jeff Martin
· Sr. Software Engineer
· Parallax, Inc.