Module gd32vf103_hal::crc[][src]

CRC calculation unit

The hardware cyclic redundancy check (CRC) unit on GD32VF103 has 32-bit data input and 32-bit data output. Calculation period is 4 AHB clock cycles for 32-bit input data size, from data entered to the calculation result available.

This unit uses fixed polynomial 0x4C11DB7, which is a common polynomial used in Ethernet.

Ref: Section 8, the User Manual; Firmware/Source/gd32vf103_crc.c

Usage

CRC calculation

To use this module, create a Crc wrapper using Crc::crc function. This function requires peripheral CRC and ownership of AHB peripheral bus; it turns on the CRC clock and creates an owned Crc wrapper. After the wrapper is created, you need to create a Digest struct. The function crc.new_digest() will clear the underlying CRC buffer and return an owned Digest struct to get ready for calculation. With the Digest struct, you may keep writing u32 values with function digest.write_u32(value). To read the digest value out, use digest.finish(). Further values can still be written after the digest value is read out. After all the processes, you may stop writing to digests and get the Crc wrapper back with function digest.free(). To release and turn off the clock to get CRC peripheral back, use crc.release().

Free data register fdata

The fdata register provides you with additional 8-bit global storage. You may read from or write to this register using function fdata_read and fdata_write.

Example

This example is tested on GD32VF103C-START board. It calculated the CRC value of 0xABCD1234. The desired result is 0xF7018A40; if the underlying hardware had calculated correctly, PA7 is set high to turn on the LED with anode connected to the MCU.

#![no_std]
#![no_main]

use gd32vf103_hal as hal;
use hal::{crc::Crc, pac, prelude::*};
use panic_halt as _;

#[riscv_rt::entry]
fn main() -> ! {
    let dp = pac::Peripherals::take().unwrap();
    let mut rcu = dp.RCU.constrain();
    let mut gpioa = dp.GPIOA.split(&mut rcu.apb2);
    let mut pa7 = gpioa.pa7.into_push_pull_output(&mut gpioa.ctl0);

    let src: u32 = 0xABCD1234;
    let crc = Crc::crc(dp.CRC, &mut rcu.ahb);
    let mut digest = crc.new_digest();
    digest.write_u32(src);

    if digest.finish() == 0xF7018A40 {
        pa7.set_high().unwrap();
    }

    loop {}
}

Structs

Crc

CRC module abstraction

Digest

Digest struct for CRC calculation.

Functions

fdata_read

Read the value of the free data register fdata.

fdata_write

Write data to the free data register fdata.