Revision: 1757
Author: [email protected]
Date: Mon Mar 8 21:03:04 2010
Log: added crc library + sample
http://code.google.com/p/jallib/source/detail?r=1757
Added:
/trunk/include/jal/crc.jal
/trunk/sample/18f4620_crc.jal
=======================================
--- /dev/null
+++ /trunk/include/jal/crc.jal Mon Mar 8 21:03:04 2010
@@ -0,0 +1,154 @@
+-- Title: Library for CRC data error checking.
+-- Author: Matthew Schinkel - borntechi.com, copyright (c) 2009, all
rights reserved.
+-- Adapted-by:
+-- Compiler: >=2.4m
+--
+-- This file is part of jallib (http://jallib.googlecode.com)
+-- Released under the ZLIB license
(http://www.opensource.org/licenses/zlib-license.html)
+--
+-- Description: This library creates CRC data and also verifies CRC data
+--
+-- Notes:
+-- Currently only supports 3-Bit CRC. Please add others if needed.
+--
+-- Sources:
+-- http://www.ece.unb.ca/tervo/ee4253/crc.shtml
+-- http://en.wikipedia.org/wiki/Cyclic_redundancy_check
+--
+
+--------------------------------------------------------------------------------
+-- returns a 3-bit crc generated from a data byte
+-- insert a 4 bit polynomial, here are some polynomial examples:
+-- 1011 = x3+x+1
+-- 1101 = x3+x2+1
+--------------------------------------------------------------------------------
+function crc_3_byte_create(byte in polynomial, byte in data) return byte is
+ var word remainder = data
+ var word polynomial2 = polynomial
+
+ remainder = remainder << 3 -- add 3 zero's right of byte
+
+ polynomial2 = polynomial2 << 7 -- move the polynomial to left
+
+ -- make each data byte readable
+ var bit bit10 at remainder : 10
+ var bit bit9 at remainder : 9
+ var bit bit8 at remainder : 8
+ var bit bit7 at remainder : 7
+ var bit bit6 at remainder : 6
+ var bit bit5 at remainder : 5
+ var bit bit4 at remainder : 4
+ var bit bit3 at remainder : 3
+
+ if bit10 == 1 then
+ remainder = remainder ^ polynomial2 -- xor
+ end if
+ polynomial2 = polynomial2 >> 1 -- shift
+
+ if bit9 == 1 then
+ remainder = remainder ^ polynomial2
+ end if
+ polynomial2 = polynomial2 >> 1
+
+ if bit8 == 1 then
+ remainder = remainder ^ polynomial2
+ end if
+ polynomial2 = polynomial2 >> 1
+
+ if bit7 == 1 then
+ remainder = remainder ^ polynomial2
+ end if
+ polynomial2 = polynomial2 >> 1
+
+ if bit6 == 1 then
+ remainder = remainder ^ polynomial2
+ end if
+ polynomial2 = polynomial2 >> 1
+
+ if bit5 == 1 then
+ remainder = remainder ^ polynomial2
+ end if
+ polynomial2 = polynomial2 >> 1
+
+ if bit4 == 1 then
+ remainder = remainder ^ polynomial2
+ end if
+ polynomial2 = polynomial2 >> 1
+
+ if bit3 == 1 then
+ remainder = remainder ^ polynomial2
+ end if
+
+ return byte(remainder) -- return the crc data created
+end function
+
+--------------------------------------------------------------------------------
+-- returns true or false if 3-bit crc is correct
+-- insert same polynomial as create function
+--------------------------------------------------------------------------------
+function crc_3_byte_verify(byte in polynomial, byte in data, byte in crc)
return byte is
+ var word remainder = data
+ var word polynomial2 = polynomial
+
+ -- add the crc data to right side of byte
+ remainder = remainder << 3
+ remainder = remainder + crc
+
+ polynomial2 = polynomial2 << 7 -- move polynomial to left
+
+ -- make each data byte readable
+ var bit bit10 at remainder : 10
+ var bit bit9 at remainder : 9
+ var bit bit8 at remainder : 8
+ var bit bit7 at remainder : 7
+ var bit bit6 at remainder : 6
+ var bit bit5 at remainder : 5
+ var bit bit4 at remainder : 4
+ var bit bit3 at remainder : 3
+
+ if bit10 == 1 then
+ remainder = remainder ^ polynomial2 -- xor
+ end if
+ polynomial2 = polynomial2 >> 1 -- shift
+
+ if bit9 == 1 then
+ remainder = remainder ^ polynomial2
+ end if
+ polynomial2 = polynomial2 >> 1
+
+ if bit8 == 1 then
+ remainder = remainder ^ polynomial2
+ end if
+ polynomial2 = polynomial2 >> 1
+
+ if bit7 == 1 then
+ remainder = remainder ^ polynomial2
+ end if
+ polynomial2 = polynomial2 >> 1
+
+ if bit6 == 1 then
+ remainder = remainder ^ polynomial2
+ end if
+ polynomial2 = polynomial2 >> 1
+
+ if bit5 == 1 then
+ remainder = remainder ^ polynomial2
+ end if
+ polynomial2 = polynomial2 >> 1
+
+ if bit4 == 1 then
+ remainder = remainder ^ polynomial2
+ end if
+ polynomial2 = polynomial2 >> 1
+
+ if bit3 == 1 then
+ remainder = remainder ^ polynomial2
+ end if
+
+ -- return if data is good
+ if remainder == 0 then
+ return true
+ else
+ return false
+ end if
+end function
=======================================
--- /dev/null
+++ /trunk/sample/18f4620_crc.jal Mon Mar 8 21:03:04 2010
@@ -0,0 +1,61 @@
+-- Title: Sample file for CRC data error checking.
+-- Author: Matthew Schinkel - borntechi.com, copyright (c) 2009, all
rights reserved.
+-- Adapted-by:
+-- Compiler: >=2.4m
+--
+-- This file is part of jallib (http://jallib.googlecode.com)
+-- Released under the BSD license
(http://www.opensource.org/licenses/bsd-license.php)
+--
+-- Description: This sample shows usage of the CRC library which can be
used
+-- to create or verify crc data.
+--
+-- Notes:
+--
+-- Sources:
+-- http://www.ece.unb.ca/tervo/ee4253/crc.shtml
+-- http://en.wikipedia.org/wiki/Cyclic_redundancy_check
+--
+
+include 18f4620 -- target PICmicro
+--
+-- This program assumes a 20 MHz resonator or crystal
+-- is connected to pins OSC1 and OSC2.
+-- Unspecified configuration bits may cause a different frequency!
+pragma target clock 20_000_000 -- oscillator frequency
+-- configuration memory settings (fuses)
+pragma target OSC HS -- HS crystal or resonator
+pragma target WDT disabled -- no watchdog
+pragma target LVP disabled -- no Low Voltage Programming
+pragma target MCLR external -- reset externally
+
+-- setup uart for communication
+const serial_hw_baudrate = 115200 -- set the baudrate
+include serial_hardware
+serial_hw_init()
+
+include crc
+
+-- Imaginary master device code
+const byte poly = 0b1101 -- choose a polynomial
+var byte crc -- holds 3-bit crc data
+crc = crc_3_byte_create(poly, 0b110010) -- create the crc data
+
+-- Imaginary data transmission here
+
+-- Imaginary slave device code
+const byte poly2 = 0b1101 -- use same
polynomial
+var byte is_good_data -- holds true or
false
+is_good_data = crc_3_byte_verify(poly2, 0b110010, crc) -- verify crc data
+
+forever loop -- now do something after data has been recieved
+ _usec_delay (1_000_000)
+
+ -- sends 3-bit crc data
+ serial_hw_data = (crc)
+
+ -- You will receive TRUE, unless the data input for
+ -- create function is different then data input for
+ -- verify function
+ serial_hw_data = (is_good_data)
+end loop
+
--
You received this message because you are subscribed to the Google Groups
"jallib" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/jallib?hl=en.