GD32-CRC冗余校验即测试

1)GD32程序测试
注:如果测试成功,两个灯常亮。

#include "gd32f4xx.h"
#include "gd32f450i_eval.h"

uint32_t val = 0, valcrc = 0;
int main(void)
{
    val = (uint32_t)0xabcd1234;

    /* initialize the LED1 and LED2, turn off them */
    gd_eval_led_init(LED1);
    gd_eval_led_init(LED2);
    gd_eval_led_off(LED1);
    gd_eval_led_off(LED2);
    rcu_periph_clock_enable(RCU_CRC);

    /* reset the CRC data register and calculate the CRC of the value */
    crc_data_register_reset();
    valcrc = crc_single_data_calculate(val);

    if(0xf7018a40 == valcrc){
        gd_eval_led_on(LED1);
        gd_eval_led_on(LED2);
    }

    while (1){
    }
}

2)自写代码测试
注:单片机的固定的计算多项式:0x4C11DB7,这个要写对

#include "gd32f4xx.h"
#include "gd32f450i_eval.h"

uint32_t val = 0, valcrc = 0;

uint32_t cal_crc(uint32_t *ptr, int len)
{
    uint32_t  xbit;
    uint32_t	data;
    uint32_t	crcTmp = 0xFFFFFFFF;
    while (len--) 
		{
        xbit = (uint32_t)1 << 31;

        data = *ptr++;
        for (int bits = 0; bits < 32; bits++)
				{
            if (crcTmp & 0x80000000)
						{
                crcTmp <<= 1;
								crcTmp ^= 0x4C11DB7; //单片机的固定的计算多项式:0x4C11DB7,这个要写对
            }
            else
                crcTmp <<= 1;
            if (data & xbit)
                crcTmp ^= 0x4C11DB7;

            xbit >>= 1;
        }
    }
    return crcTmp;
}


int main(void)
{
    val = (uint32_t)0xabcd1234;
	
    gd_eval_led_init(LED1);
    gd_eval_led_init(LED2);
    gd_eval_led_off(LED1);
    gd_eval_led_off(LED2);
    rcu_periph_clock_enable(RCU_CRC);
		uint32_t tmp = cal_crc(&val,1);
    if(0xf7018a40 == tmp)
		{
        gd_eval_led_on(LED1);
        gd_eval_led_on(LED2);
    }

    while (1){
    }
}

总结:经测试,GD32-CRC32bit测试合格,数据一致。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值