Replies: 8 comments 5 replies
-
I found this, which might help... https://www.digi.com/resources/documentation/digidocs/90001537/references/r_python_crc16_modbus.htm along with a more detailed discussion of the process: https://stackoverflow.com/questions/55179867/crc16-modbus-computing-algorithm |
Beta Was this translation helpful? Give feedback.
-
My question was totally incomplete, I mean a native C function to replace the python one. |
Beta Was this translation helpful? Give feedback.
-
I've used viper to get near-c performance out of a python crc function previously, this is much easier to integrate / distribute than a c module typically. Here's an example (not crc16) that might be helpful: @micropython.viper()
def crc32_xfer(data: ptr8, size: int) -> uint:
"""
Returns the 32 bit CRC (XFER algorithm) of the provided bytes
Compatible with CRC-32/XFER from https://crccalc.com/?method=crc32
If the returned crc is appended to the original data and run back through
this function the result will be 0.
:param bytes,bytearray data: bytes to calculate crc on
:return int: crc of input data
"""
_poly:uint = 0x000000AF
crc = uint(0)
highbit = int(1 << (32 - 1))
mask = int((highbit - 1) << 1) | 0x1
poly = _poly
shift = 32 - 8
diff8 = -1 * shift
if diff8 > 0:
mask = 0xFF
crc <<= diff8
shift = 0
highbit = 0x80
poly = _poly << diff8
for i in range(size):
crc ^= data[i] << shift
for i in range(8):
if crc & highbit:
crc = (crc << 1) ^ poly
else:
crc = (crc << 1)
crc &= mask
if diff8 > 0:
crc >>= diff8
return crc |
Beta Was this translation helpful? Give feedback.
-
Last December I did a CRC library that contains viper, asm_thumb and asm_xtensa implementations. It has definitions for many 8, 16, 32 and 64 bit CRC variants and is as fast as a table-based implementation using these accelerators can be. There is also a benchmark. Anyone interested in working on adding the xtensawin variant of the xtensa assembler? Which would mainly be dealing with the complexities of the windowing in it, I suppose. |
Beta Was this translation helpful? Give feedback.
-
Lots of great resources out there. I've transcribed two from c, references included. Here is a crc16 (xmodem) version:
This version is crc16 (CCIT). I use this in AX.25 APRS Ham radio stuff
|
Beta Was this translation helpful? Give feedback.
-
@Tangerino seeing your question actually asked for C, I have that too for crc16 XMODEM.
|
Beta Was this translation helpful? Give feedback.
-
After some MP bugs here and there, now I can use the official MP binary releases. It would awesome if the CRC modules be native so avoiding to compile myself my modules. |
Beta Was this translation helpful? Give feedback.
-
I am surprised no one gave https://github.com/madler/crcany a try before ? I usually stick to this one when coding in C or mpy. It's tables covered all the three not so common CRCs I encountered so far. |
Beta Was this translation helpful? Give feedback.
-
Is there any crc16 function available to replace the CRC calculation in Python for ESP32?
Beta Was this translation helpful? Give feedback.
All reactions