SPI CRC

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
hlovatt
Posts: 40
Joined: Thu Aug 15, 2019 3:52 am
Location: Sydney

SPI CRC

Post by hlovatt » Wed Jul 01, 2020 8:37 am

Hi,

Anyone know how to use the `crc` parameter on `pyb.SPI`?

Have googled but didn't come up with even an example!

Docs say:
``crc`` can be None for no CRC, or a polynomial specifier.
But what's a polynomial specifier?

Thanks for any help,

Howard.

User avatar
pythoncoder
Posts: 4268
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: SPI CRC

Post by pythoncoder » Thu Jul 02, 2020 5:52 am

Good question. This will probably add to your confusion :?

Looking at the source it seems to conflict with the docs (but I'm no expert). The docs indicate it's an integer, but the code suggests a .u_rom_obj rather than a .u_int. I suspect you may be the first to try to use this. What's your application?
Peter Hinch

User avatar
jimmo
Posts: 1710
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: SPI CRC

Post by jimmo » Thu Jul 02, 2020 7:00 am

hlovatt wrote:
Wed Jul 01, 2020 8:37 am
Have googled but didn't come up with even an example!
There's an example in the code using an integer:

Code: Select all

//     from pyb import SPI
//     spi = SPI(1, SPI.MASTER, baudrate=600000, polarity=1, phase=0, crc=0x7)
pythoncoder wrote:
Thu Jul 02, 2020 5:52 am
Looking at the source it seems to conflict with the docs (but I'm no expert). The docs indicate it's an integer, but the code suggests a .u_rom_obj rather than a .u_int. I suspect you may be the first to try to use this. What's your application?
This is (unfortunately) the (slightly strange) way of describing a function (via the mp_arg_parse_all helper) that takes an optional integer that can be, and defaults to, None.

What's going on here is that when using mp_arg_parse_all, if you say that a field is an int, then you have to specify that it's default value is an actual integer, and furthermore, mp_arg_parse_all will unconditionally try and convert the input to an integer.

In this case, we say the argument is just an object, defaulting to None, and the "is it non-None, covert to int" is handled later down in the function instead. On most ports the u_rom_obj vs u_obj distinction is irrelevant, but these are all members of a union, so that's why the code can access it with .u_obj later.

User avatar
pythoncoder
Posts: 4268
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: SPI CRC

Post by pythoncoder » Thu Jul 02, 2020 2:35 pm

OK, thanks. But I guess @hlovatt wants to know how the integer's value maps onto CRC algorithms.
Peter Hinch

User avatar
jimmo
Posts: 1710
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: SPI CRC

Post by jimmo » Thu Jul 02, 2020 11:25 pm

Oh, good point. The simple answer is that MicroPython just sets this value directly in the SPI_CRCPR register on the SPI peripheral.

It's the coefficients of the polynomial. So 0x07-->0b111 means that it's using x^8 + x^2 + x + 1 (there's always an implicit n+1'th bit set to 1)

hlovatt
Posts: 40
Joined: Thu Aug 15, 2019 3:52 am
Location: Sydney

Re: SPI CRC

Post by hlovatt » Thu Jul 02, 2020 11:34 pm

Thanks for your replies, my immediate concern is how to type the argument for a typeshed I'm writing. From Jimmo's comments I guessing that it should be `crc: Optional[int] = None` - correct?

A 2nd concern is that there are many ways of specifying the polynomial using an int, https://en.wikipedia.org/wiki/Cyclic_re ... cification. It would be good to know:
  1. The order of the polynomial.
  2. The default equation.
  3. Which specifying convention is used.

User avatar
pythoncoder
Posts: 4268
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: SPI CRC

Post by pythoncoder » Fri Jul 03, 2020 4:51 am

If you look at the chip datasheet it's as @jimmo says. It's a CRC8 polynomial, which means that x^8 is the MSB and is an implied 1. So the code is MSB first with the high order bit suppressed.
Peter Hinch

Post Reply