[分享] micropython在STM32H7/F7上快速CRC计算

dcexpert   2020-5-11 15:41 楼主

来自:https://forum.micropython.org/viewtopic.php?t=8316&p=47337#p47337

STM32H7/F7支持自定义的多项式,因此可以计算多种CRC,而STM32F4只能计算固定的CRC32。

 

    @micropython.viper
    def __stm_crc_16(self, data, size : int) -> int: # private
        ptr32(stm.CRC + stm.CRC_CR)[0] = (1 << 3) | 1 # Reset and put into 1 byte per write mode.
        crc8 = ptr8(stm.CRC + stm.CRC_DR)
        d = ptr8(data)
        for i in range(size):
            crc8[0] = d[i]
        return ptr32(stm.CRC + stm.CRC_DR)[0]

    if omv.board_type() == "H7":
        stm.mem32[stm.RCC + stm.RCC_AHB4ENR] = stm.mem32[stm.RCC + stm.RCC_AHB4ENR] | (1 << 19) # Enable CRC clock 
        stm.mem32[stm.CRC + stm.CRC_POL] = 0x1021
    elif omv.board_type() == "F7":
        stm.mem32[stm.RCC + stm.RCC_AHB1ENR] = stm.mem32[stm.RCC + stm.RCC_AHB1ENR] | (1 << 12) # Enable CRC clock 
        stm.mem32[stm.CRC + stm.CRC_POL] = 0x1021

 

回复评论 (2)

看不太懂啊

点赞  2020-5-11 17:30

多看几遍

 

点赞  2020-6-19 22:53
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复