1 /*
2 * Copyright (c) 2021-2022 HPMicro
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8 #include "hpm_common.h"
9 #include "hpm_crc_drv.h"
10
crc_get_default_channel_config(crc_channel_config_t * cfg)11 void crc_get_default_channel_config(crc_channel_config_t *cfg)
12 {
13 cfg->preset = crc_preset_crc32;
14 cfg->poly = 0x4C11DB7ul;
15 cfg->init = 0xFFFFFFFul;
16 cfg->in_byte_order = crc_in_byte_order_lsb;
17 cfg->refout = crc_refout_true;
18 cfg->refin = crc_refin_true;
19 cfg->poly_width = CRC_POLY_WIDTH_32;
20 cfg->xorout = 0xFFFFFFF;
21 }
22
crc_setup_channel_config(CRC_Type * ptr,uint32_t ch_index,crc_channel_config_t * cfg)23 hpm_stat_t crc_setup_channel_config(CRC_Type *ptr, uint32_t ch_index,
24 crc_channel_config_t *cfg)
25 {
26 ptr->CHN[ch_index].CLR |= CRC_CHN_CLR_CLR_MASK;
27 ptr->CHN[ch_index].PRE_SET = cfg->preset;
28
29 if (!ptr->CHN[ch_index].PRE_SET) {
30 ptr->CHN[ch_index].POLY = cfg->poly;
31 ptr->CHN[ch_index].INIT_DATA = cfg->init;
32 ptr->CHN[ch_index].XOROUT = cfg->xorout;
33 ptr->CHN[ch_index].MISC_SETTING = CRC_CHN_MISC_SETTING_REV_OUT_SET(cfg->refout) |
34 CRC_CHN_MISC_SETTING_REV_IN_SET(cfg->refin) |
35 CRC_CHN_MISC_SETTING_POLY_WIDTH_SET(cfg->poly_width);
36 }
37
38 ptr->CHN[ch_index].MISC_SETTING |= CRC_CHN_MISC_SETTING_BYTE_REV_SET(cfg->in_byte_order);
39 return status_success;
40 }
41
crc_calc_block_bytes(CRC_Type * ptr,uint32_t ch_index,uint8_t * pbuffer,uint32_t length)42 void crc_calc_block_bytes(CRC_Type *ptr, uint32_t ch_index, uint8_t *pbuffer, uint32_t length)
43 {
44 uint32_t addr = (uint32_t)&ptr->CHN[ch_index].DATA;
45
46 for (uint32_t i = 0; i < length; i++) {
47 CRC_REG_WRITE8(addr, pbuffer[i]);
48 }
49 }
50
crc_calc_block_half_words(CRC_Type * ptr,uint32_t ch_index,uint16_t * pbuffer,uint32_t length)51 void crc_calc_block_half_words(CRC_Type *ptr, uint32_t ch_index, uint16_t *pbuffer, uint32_t length)
52 {
53 uint32_t addr = (uint32_t)&ptr->CHN[ch_index].DATA;
54
55 for (uint32_t i = 0; i < length; i++) {
56 CRC_REG_WRITE16(addr, pbuffer[i]);
57 }
58 }
59
crc_calc_block_words(CRC_Type * ptr,uint32_t ch_index,uint32_t * pbuffer,uint32_t length)60 void crc_calc_block_words(CRC_Type *ptr, uint32_t ch_index, uint32_t *pbuffer, uint32_t length)
61 {
62 uint32_t addr = (uint32_t)&ptr->CHN[ch_index].DATA;
63
64 for (uint32_t i = 0; i < length; i++) {
65 CRC_REG_WRITE32(addr, pbuffer[i]);
66 }
67 }
68
crc_calc_large_block_fast(CRC_Type * ptr,uint32_t ch_index,uint8_t * pbuffer,uint32_t length)69 void crc_calc_large_block_fast(CRC_Type *ptr, uint32_t ch_index, uint8_t *pbuffer, uint32_t length)
70 {
71 if (length < 16) {
72 crc_calc_block_bytes(ptr, ch_index, pbuffer, length);
73 return;
74 }
75
76 uint32_t addr = (uint32_t)&ptr->CHN[ch_index].DATA;
77 uint32_t start_byte_addr = (uint32_t)pbuffer;
78 uint32_t start_byte_len = 4ul - (start_byte_addr & 0x03ul);
79 uint32_t word_addr = start_byte_addr + start_byte_len;
80 uint32_t word_len = (length - start_byte_len) & (~0x03ul);
81 uint32_t end_byte_addr = word_addr + word_len;
82 uint32_t end_byte_len = (length - start_byte_len - word_len);
83
84 for (uint32_t i = 0; i < start_byte_len; i++) {
85 CRC_REG_WRITE8(addr, *(volatile uint8_t *)(start_byte_addr + i));
86 }
87
88 for (uint32_t i = 0; i < word_len; i += 4) {
89 CRC_REG_WRITE32(addr, *(volatile uint32_t *)(word_addr + i));
90 }
91
92 for (uint32_t i = 0; i < end_byte_len; i++) {
93 CRC_REG_WRITE8(addr, *(volatile uint8_t *)(end_byte_addr + i));
94 }
95 }
96
97