The purpose of this group is to foster exchange of information on the Texas Instruments MSP430 family of microcontrollers and related tools. Everyone welcome, all levels of familiarity/expertise.
CRC for RS232 communication, F149 and Excel-test - timokirschke - Sep 24 6:22:04 2008
Hello,
we're using the F149, programmed in C with Crossworks, communicating=20
via a 115kBd RS232 with the PC.
We implemented a very simple "XOR" CRC, which checks the ASCII=20
formatted data and is transmitted at the end of the data string, also=20
ASCII-formatted.
I tried to check, if the CRC calculation is correct, maybe, I did it=20
a bit stupid.
code (the ASCII formatting is correct, somebody will remember ;) :
u_SendBuf.SendBuf1[43] =3D 0x0D;
u_SendBuf.SendBuf1[42] =3D 0x0A;
u_SendBuf.SendBuf1_int[19] =3D ASCII_HEX_vector_int[SumRHist &=20
0xFF]; //look up table=20
u_SendBuf.SendBuf1_int[20] =3D ASCII_HEX_vector_int[(SumRHist >> 8) &=20
0xFF];
...
u_SendBuf.SendBuf1_int[1] =3D ASCII_HEX_vector_int[RTist_O2_left[k0]=20
& 0xFF];
u_SendBuf.SendBuf1_int[2] =3D ASCII_HEX_vector_int[(RTist_O2_left
[k0] >> 8) & 0xFF];
// bitwise EXOR for two byte operands
SendCRC =3D 0;
SendCRC =3D SendCRC ^ u_SendBuf.SendBuf1[43];
SendCRC =3D SendCRC ^ u_SendBuf.SendBuf1[42];
...
SendCRC =3D SendCRC ^ u_SendBuf.SendBuf1[2];
//save CRC via look up table=20
u_SendBuf.SendBuf1_int[0] =3D ASCII_HEX_vector_int[SendCRC];
SendBufCnt1 =3D 43; // counter for bytes to be sent
TXBUF1 =3D u_SendBuf.SendBuf1[SendBufCnt1]; // load first value into=20
send buffer
The CRC will become values of 0n and 7n with n=3D{0...F}.
The test in Excel produces values of 0n only, with n differing from=20
the CRC of the =B5C. This seems to be more correct, because the ASCII=20
data are bytes {0...F} only, the upper 4 bits remain 0 therefore.
The Excel routine is (sorry, in German):
=3DBININHEX(WECHSELN(HEXINBIN(value1;8)+HEXINBIN(value2;8);2;0))
Any hint is appreciated, also simplifications.
Thank you in advance!
Best regards, Timo
------------------------------------

(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )
Re: CRC for RS232 communication, F149 and Excel-test - Leon - Sep 24 7:08:20 2008
----- Original Message -----
From: "timokirschke"
To:
Sent: Wednesday, September 24, 2008 11:21 AM
Subject: [msp430] CRC for RS232 communication, F149 and Excel-test
Hello,
we're using the F149, programmed in C with Crossworks, communicating
via a 115kBd RS232 with the PC.
We implemented a very simple "XOR" CRC, which checks the ASCII
formatted data and is transmitted at the end of the data string, also
ASCII-formatted.
Here's a proper CRC that I've used with an ARM application:
//---------------------------- CRC
functions --------------------------------//
u_long crc32_table[256];
/* Initialized first time "crc32()" is called. If you prefer, you can
* statically initialize it at compile time. [Another exercise.]
*/
u_long crc32(u_char *buf, int len)
{
u_char *p;
u_long crc;
if (!crc32_table[1]) /* if not already done, */
init_crc32(); /* build table */
crc = 0xffffffff; /* preload shift register, per CRC-32 spec
*/
for (p = buf; len > 0; ++p, --len)
crc = (crc << 8) ^ crc32_table[(crc >> 24) ^ *p];
return ~crc; /* transmit complement, per CRC-32 spec */
}
/*
* Build auxiliary table for parallel byte-at-a-time CRC-32.
*/
#define CRC32_POLY 0x04c11db7 /* AUTODIN II, Ethernet, & FDDI */
init_crc32()
{
int i, j;
u_long c;
for (i = 0; i < 256; ++i) {
for (c = i << 24, j = 8; j > 0; --j)
c = c & 0x80000000 ? (c << 1) ^ CRC32_POLY : (c <<
1);
crc32_table[i] = c;
}
}
Leon
------------------------------------

(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )Re: CRC for RS232 communication, F149 and Excel-test - "stefan.hauenstein" - Sep 24 9:41:50 2008
Hi,
I havn't really understood why do you need the look up table, but I=20
also used the XOR as a checksum in a project in the past.=20
For the checksum it doesn't matter wether the content is ASCII, Hex=20
or Octal. You're calculating the XOR sum over bytes.
To simplify it:
SendCRC =3D 0;
for( int i =3D 1; i < 44; i++)
{
SendCRC ^=3D u_SendBuf.SendBuf1[ i];
}=20
u_SendBuf.SendBuf1[0] =3D SendCRC;
In your example, the CRC is transmitted at the beginning of the=20
stream not at the end.
The receiver can simple do XOR over all byte and the sum must be=20
null, so that the received stream is correct.
Servas,
Stefan
--- In m...@yahoogroups.com, "timokirschke"
=20
wrote:
>
> Hello,
>=20
> we're using the F149, programmed in C with Crossworks,=20
communicating=20
> via a 115kBd RS232 with the PC.
> We implemented a very simple "XOR" CRC, which checks the ASCII=20
> formatted data and is transmitted at the end of the data string,=20
also=20
> ASCII-formatted.
>=20
> I tried to check, if the CRC calculation is correct, maybe, I did=20
it=20
> a bit stupid.
>=20
> code (the ASCII formatting is correct, somebody will remember ;) :
>=20
> u_SendBuf.SendBuf1[43] =3D 0x0D;
> u_SendBuf.SendBuf1[42] =3D 0x0A;
>=20
> u_SendBuf.SendBuf1_int[19] =3D ASCII_HEX_vector_int[SumRHist &=20
> 0xFF]; //look up table=20
> u_SendBuf.SendBuf1_int[20] =3D ASCII_HEX_vector_int[(SumRHist >> 8)=20
&=20
> 0xFF];
> ...
> u_SendBuf.SendBuf1_int[1] =3D ASCII_HEX_vector_int[RTist_O2_left
[k0]=20
> & 0xFF];
> u_SendBuf.SendBuf1_int[2] =3D ASCII_HEX_vector_int[(RTist_O2_left
> [k0] >> 8) & 0xFF];
>=20
> // bitwise EXOR for two byte operands
> SendCRC =3D 0;
> SendCRC =3D SendCRC ^ u_SendBuf.SendBuf1[43];
> SendCRC =3D SendCRC ^ u_SendBuf.SendBuf1[42];
> ...
> SendCRC =3D SendCRC ^ u_SendBuf.SendBuf1[2];
>=20
> //save CRC via look up table=20
> u_SendBuf.SendBuf1_int[0] =3D ASCII_HEX_vector_int[SendCRC];
> SendBufCnt1 =3D 43; // counter for bytes to be sent
> TXBUF1 =3D u_SendBuf.SendBuf1[SendBufCnt1]; // load first value=20
into=20
> send buffer
>=20
> The CRC will become values of 0n and 7n with n=3D{0...F}.
> The test in Excel produces values of 0n only, with n differing from=20
> the CRC of the =B5C. This seems to be more correct, because the ASCII=20
> data are bytes {0...F} only, the upper 4 bits remain 0 therefore.
> The Excel routine is (sorry, in German):
>=20
> =3DBININHEX(WECHSELN(HEXINBIN(value1;8)+HEXINBIN(value2;8);2;0))
>=20
> Any hint is appreciated, also simplifications.
> Thank you in advance!
> Best regards, Timo
>
------------------------------------

(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )Re: CRC for RS232 communication, F149 and Excel-test - antedeluvian51 - Sep 24 13:40:05 2008
Modbus uses a 16 bit CRC at the end of the message. It has quite good
documentation on generation of the CRC both through a software
implementation of the shift registers or as a look up table (with the
code in C)
Look from page 39 at
http://modbus.org/docs/Modbus_over_serial_line_V1_02.pdf
-Aubrey
------------------------------------

(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )
Re: Re: CRC for RS232 communication, F149 and Excel-test - Candemir Orsan - Sep 24 13:44:27 2008
And here is a good article about CRC that was in Embedded Programming back in 2000 with C
source code.
http://www.netrino.com/Embedded-Systems/How-To/CRC-Calculation-C-Code
C.
----- Original Message ----
From: antedeluvian51
To: m...@yahoogroups.com
Sent: Wednesday, September 24, 2008 1:39:50 PM
Subject: [msp430] Re: CRC for RS232 communication, F149 and Excel-test
Modbus uses a 16 bit CRC at the end of the message. It has quite good
documentation on generation of the CRC both through a software
implementation of the shift registers or as a look up table (with the
code in C)
Look from page 39 at
http://modbus. org/docs/ Modbus_over_ serial_line_ V1_02.pdf
-Aubrey
[Non-text portions of this message have been removed]
------------------------------------

(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )Re: CRC for RS232 communication, F149 and Excel-test - Stuart_Rubin - Sep 24 15:06:05 2008
IAR has a great feature (others may as well) where the flash memory
CRC can be calculated and stuffed into the codespace. At runtime, you
can calculate the CRC and make sure that nothing is corrupted. (The
linker calculates the CRC, so you don't need to do any kind of manual
calculations and filling-memory each build.)
To make things nicer, they give you source code for the CRC
calculations, both a "fast" (table) and "slow" (repeated calcs) way.
Take a look at their app note and code, "Technical Note 91733,
Checksum calculation with XLINK"
http://supp.iar.com/Support/?note=3D91733&from=3Dsearch+result
There are a lot of online CRC calculators, too. I like to use this one:
http://www.zorc.breitbandkatze.de/crc.html
as a code verification.
There has been some discussion about why bothering to use a table, and
not just calculate the constants on-the-fly. It may seem fast enough,
but you may calculating CRCs on real-time serial data. At a
reasonable bit/message rate, these times may become important.
Good luck.
Stuart
--- In m...@yahoogroups.com, "timokirschke"
wrote:
>
> Hello,
>=20
> we're using the F149, programmed in C with Crossworks, communicating=20
> via a 115kBd RS232 with the PC.
> We implemented a very simple "XOR" CRC, which checks the ASCII=20
> formatted data and is transmitted at the end of the data string, also=20
> ASCII-formatted.
>=20
> I tried to check, if the CRC calculation is correct, maybe, I did it=20
> a bit stupid.
>=20
> code (the ASCII formatting is correct, somebody will remember ;) :
>=20
> u_SendBuf.SendBuf1[43] =3D 0x0D;
> u_SendBuf.SendBuf1[42] =3D 0x0A;
>=20
> u_SendBuf.SendBuf1_int[19] =3D ASCII_HEX_vector_int[SumRHist &=20
> 0xFF]; //look up table=20
> u_SendBuf.SendBuf1_int[20] =3D ASCII_HEX_vector_int[(SumRHist >> 8) &=20
> 0xFF];
> ...
> u_SendBuf.SendBuf1_int[1] =3D ASCII_HEX_vector_int[RTist_O2_left[k0]=20
> & 0xFF];
> u_SendBuf.SendBuf1_int[2] =3D ASCII_HEX_vector_int[(RTist_O2_left
> [k0] >> 8) & 0xFF];
>=20
> // bitwise EXOR for two byte operands
> SendCRC =3D 0;
> SendCRC =3D SendCRC ^ u_SendBuf.SendBuf1[43];
> SendCRC =3D SendCRC ^ u_SendBuf.SendBuf1[42];
> ...
> SendCRC =3D SendCRC ^ u_SendBuf.SendBuf1[2];
>=20
> //save CRC via look up table=20
> u_SendBuf.SendBuf1_int[0] =3D ASCII_HEX_vector_int[SendCRC];
> SendBufCnt1 =3D 43; // counter for bytes to be sent
> TXBUF1 =3D u_SendBuf.SendBuf1[SendBufCnt1]; // load first value into=20
> send buffer
>=20
> The CRC will become values of 0n and 7n with n=3D{0...F}.
> The test in Excel produces values of 0n only, with n differing from=20
> the CRC of the =B5C. This seems to be more correct, because the ASCII=20
> data are bytes {0...F} only, the upper 4 bits remain 0 therefore.
> The Excel routine is (sorry, in German):
>=20
> =3DBININHEX(WECHSELN(HEXINBIN(value1;8)+HEXINBIN(value2;8);2;0))
>=20
> Any hint is appreciated, also simplifications.
> Thank you in advance!
> Best regards, Timo
>
------------------------------------

(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )Re: CRC for RS232 communication, F149 and Excel-test - timokirschke - Sep 25 3:31:20 2008
Good Morning,
many thanks to all, what a lot of help, great! Both, the explanations and
the code examples are very welcome.
What I understood first, is the hidden message, that I did not do a
meaningful or safe CRC calculation. A hint for my laziness? ;)
What I forgot to say, the application is time critical: the MSP is busy
for 980+ us in a 1ms cycle while its control tasks. There is no time left
over. Therefore the CRC loop is splitted into separated commands, it saves
~20us. And yes I know, that this utilisation is too much for the
processor, normally. I have to check the non-overlapping cycles after
every code change using a scope.
That is the reason, why we use tables for ASCII-translation, Stefan. I
discussed it here some weeks ago.
For this reason it will be not possible in this time, to implement a full
CRC algorithm, whether using tables or not. Later we switch to the F2618,
this may allow for the better version.
In the mean time I checked the CRC-calculation of two data records by
hand, it seems to be correct. So the error should be on the PC side (what
the responsible guy does not want to hear).
Weird: in my setup I would not need any CRC, because the communication
between MSP and PC is flawless. Only the colleagues and especially the
partners, using the device hundreds of kilometers away, tell us of the
communication errors. Does anybody know this effect? ;)
Okay, I will check the cabling, especially the grounding.
Thank you again!
Best regards, Timo
------------------------------------

(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )
Re: Re: CRC for RS232 communication, F149 and Excel-test - Andreas =?ISO-8859-1?Q?K=F6pke?= - Sep 25 3:58:34 2008
There is one CRC implementation for the
ITU-T polynomial: G_16(x) = x^16 + x^12 + x^5 + 1
pointed out by Paul Curtis earlier on this mailing list:
uint16_t crcByte(uint16_t crc, uint8_t b) {
crc = (uint8_t)(crc >> 8) | (crc << 8);
crc ^= b;
crc ^= (uint8_t)(crc & 0xff) >> 4;
crc ^= crc << 12;
crc ^= (crc & 0xff) << 5;
return crc;
}
This takes only twice the time of the table implementation and is much
smaller in flash. The polynomial is also a pretty good one in terms of
error detection capabilities.
I recommend to always use a CRC for communication, this allows you to
recover (on the PC side) from synchronization problems like a reset of
the MSP in the middle of a message transmission. But in general you are
right: communication from MSP to PC is flawless.
Best, Andreas
Am Donnerstag, den 25.09.2008, 07:30 +0000 schrieb timokirschke:
> Good Morning,
>
> many thanks to all, what a lot of help, great! Both, the explanations
> and
> the code examples are very welcome.
>
> What I understood first, is the hidden message, that I did not do a
> meaningful or safe CRC calculation. A hint for my laziness? ;)
> What I forgot to say, the application is time critical: the MSP is
> busy
> for 980+ us in a 1ms cycle while its control tasks. There is no time
> left
> over. Therefore the CRC loop is splitted into separated commands, it
> saves
> ~20us. And yes I know, that this utilisation is too much for the
> processor, normally. I have to check the non-overlapping cycles after
> every code change using a scope.
> That is the reason, why we use tables for ASCII-translation, Stefan.
> I
> discussed it here some weeks ago.
>
> For this reason it will be not possible in this time, to implement a
> full
> CRC algorithm, whether using tables or not. Later we switch to the
> F2618,
> this may allow for the better version.
>
> In the mean time I checked the CRC-calculation of two data records by
> hand, it seems to be correct. So the error should be on the PC side
> (what
> the responsible guy does not want to hear).
>
> Weird: in my setup I would not need any CRC, because the
> communication
> between MSP and PC is flawless. Only the colleagues and especially
> the
> partners, using the device hundreds of kilometers away, tell us of
> the
> communication errors. Does anybody know this effect? ;)
> Okay, I will check the cabling, especially the grounding.
>
> Thank you again!
> Best regards, Timo
>
------------------------------------

(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )
Re: Re: CRC for RS232 communication, F149 and Excel-test - Ian Okey - Sep 25 4:14:47 2008
Look at using the F5x devices (TI have just released them). They are
cheaper than the F2618 and F149 and have a hardware CRC generator. You just
write the data to be CRC'd into the register and out comes the result. I
use it for Flash checksumming - no problems at all.
Ian
2008/9/25 timokirschke
> Good Morning,
>
> many thanks to all, what a lot of help, great! Both, the explanations and
> the code examples are very welcome.
>
> What I understood first, is the hidden message, that I did not do a
> meaningful or safe CRC calculation. A hint for my laziness? ;)
> What I forgot to say, the application is time critical: the MSP is busy
> for 980+ us in a 1ms cycle while its control tasks. There is no time left
> over. Therefore the CRC loop is splitted into separated commands, it saves
> ~20us. And yes I know, that this utilisation is too much for the
> processor, normally. I have to check the non-overlapping cycles after
> every code change using a scope.
> That is the reason, why we use tables for ASCII-translation, Stefan. I
> discussed it here some weeks ago.
>
> For this reason it will be not possible in this time, to implement a full
> CRC algorithm, whether using tables or not. Later we switch to the F2618,
> this may allow for the better version.
>
> In the mean time I checked the CRC-calculation of two data records by
> hand, it seems to be correct. So the error should be on the PC side (what
> the responsible guy does not want to hear).
>
> Weird: in my setup I would not need any CRC, because the communication
> between MSP and PC is flawless. Only the colleagues and especially the
> partners, using the device hundreds of kilometers away, tell us of the
> communication errors. Does anybody know this effect? ;)
> Okay, I will check the cabling, especially the grounding.
>
> Thank you again!
> Best regards, Timo
> ------------------------------------

(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )Re: CRC for RS232 communication, F149 and Excel-test - timokirschke - Sep 25 5:34:40 2008
--- In m...@yahoogroups.com, "Ian Okey"
wrote:
Hello,
>
> Look at using the F5x devices (TI have just released them). They are
> cheaper than the F2618 and F149 and have a hardware CRC generator. You
just
> write the data to be CRC'd into the register and out comes the result. I
> use it for Flash checksumming - no problems at all.
>
> Ian
interesting, I didn't get it from the last conference. It is definitely
worth to think about.
The F2618 'solution' is an intermediate workaround only. It does not need
any hardware modification, but allows lots of software improvements.
Thanks also to Andreas (and Paul) for the additional algorithm,
Timo
------------------------------------

(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )Re: CRC for RS232 communication, F149 and Excel-test - Stuart_Rubin - Sep 25 14:36:43 2008
If speed really is that much of a problem and you're "fairly"
confident with your data, than why not just use the UART's parity
check and do a simple checksum on your buffer.
The UART parity check is "free" and will catch any single-bit error.
For the checksum, add up every byte (throw-out the overflow bits) as
you get them. The last byte of your message is your checksum. When
you add it to your accumulated sum, you should get a 0x00 if your data
is good. The checksum is pre-calculated by the sender for this to
work-out. This method will catch 255/256 single-bit errors on top of
those caught by the parity check.
Sounds pretty good to me!
Stuart
--- In m...@yahoogroups.com, "timokirschke"
wrote:
>
> --- In m...@yahoogroups.com, "Ian Okey" wrote:
> Hello,
> >
> > Look at using the F5x devices (TI have just released them). They are
> > cheaper than the F2618 and F149 and have a hardware CRC generator.
You
> just
> > write the data to be CRC'd into the register and out comes the
result. I
> > use it for Flash checksumming - no problems at all.
> >
> > Ian
>
> interesting, I didn't get it from the last conference. It is definitely
> worth to think about.
> The F2618 'solution' is an intermediate workaround only. It does not
need
> any hardware modification, but allows lots of software improvements.
>
> Thanks also to Andreas (and Paul) for the additional algorithm,
> Timo
>
------------------------------------

(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )Re: Re: CRC for RS232 communication, F149 and Excel-test - Joe Radomski - Sep 25 21:04:00 2008
if you look at the samples directory within crossworks 1.4 there is a crc p=
roject, with a 16bit crc implementaion and how to use it.. even at 115k bau=
d a non table driven alg should work fine..=20
I remember doing an implementation on an Atari 800 computer (1.79mhz 65c02)=
in the late 80s and I was runnng 57.6k with lots of time to spare for othe=
r tasks... The MSP should easily handle it..
=A0
--- On Thu, 9/25/08, timokirschke
wro=
te:
From: timokirschke
Subject: [msp430] Re: CRC for RS232 communication, F149 and Excel-test
To: m...@yahoogroups.com
Date: Thursday, September 25, 2008, 5:34 AM
--- In msp430@yahoogroups. com, "Ian Okey" wrote:
Hello,
>
> Look at using the F5x devices (TI have just released them). They are
> cheaper than the F2618 and F149 and have a hardware CRC generator. You=20
just
> write the data to be CRC'd into the register and out comes the result. I
> use it for Flash checksumming - no problems at all.
>=20
> Ian
interesting, I didn't get it from the last conference. It is definitely=20
worth to think about.
The F2618 'solution' is an intermediate workaround only. It does not need=20
any hardware modification, but allows lots of software improvements.
Thanks also to Andreas (and Paul) for the additional algorithm,
Timo
=20
[Non-text portions of this message have been removed]
------------------------------------

(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )