1 |
198 |
zero_gravi |
// #################################################################################################
|
2 |
|
|
// # < neo430_crc.c - CRC module helper functions > #
|
3 |
|
|
// # ********************************************************************************************* #
|
4 |
|
|
// # BSD 3-Clause License #
|
5 |
|
|
// # #
|
6 |
|
|
// # Copyright (c) 2020, Stephan Nolting. All rights reserved. #
|
7 |
|
|
// # #
|
8 |
|
|
// # Redistribution and use in source and binary forms, with or without modification, are #
|
9 |
|
|
// # permitted provided that the following conditions are met: #
|
10 |
|
|
// # #
|
11 |
|
|
// # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
12 |
|
|
// # conditions and the following disclaimer. #
|
13 |
|
|
// # #
|
14 |
|
|
// # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
15 |
|
|
// # conditions and the following disclaimer in the documentation and/or other materials #
|
16 |
|
|
// # provided with the distribution. #
|
17 |
|
|
// # #
|
18 |
|
|
// # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
19 |
|
|
// # endorse or promote products derived from this software without specific prior written #
|
20 |
|
|
// # permission. #
|
21 |
|
|
// # #
|
22 |
|
|
// # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
23 |
|
|
// # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
24 |
|
|
// # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
25 |
|
|
// # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
26 |
|
|
// # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
27 |
|
|
// # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
28 |
|
|
// # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
29 |
|
|
// # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
30 |
|
|
// # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
31 |
|
|
// # ********************************************************************************************* #
|
32 |
|
|
// # The NEO430 Processor - https://github.com/stnolting/neo430 #
|
33 |
|
|
// #################################################################################################
|
34 |
|
|
|
35 |
|
|
#include "neo430.h"
|
36 |
|
|
#include "neo430_crc.h"
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
/* ------------------------------------------------------------
|
40 |
|
|
* INFO Compute CRC16 from buffer
|
41 |
|
|
* PARAM start_val: Start value for CRC shift register
|
42 |
|
|
* PARAM polynomial: 16-bit polynomial XOR mask
|
43 |
|
|
* PARAM data: Pointer to BYTE input data array
|
44 |
|
|
* PARAM length: Number of elements in input data array
|
45 |
|
|
* RETURN CRC16 result
|
46 |
|
|
* ------------------------------------------------------------ */
|
47 |
|
|
uint16_t neo430_crc16(uint16_t start_val, uint16_t polynomial, uint8_t *data, uint16_t length) {
|
48 |
|
|
|
49 |
|
|
CRC_POLY_LO = polynomial;
|
50 |
|
|
CRC_RESX = start_val;
|
51 |
|
|
|
52 |
|
|
while(length) {
|
53 |
|
|
CRC_CRC16IN = (uint16_t)(*data++); // no wait required here
|
54 |
|
|
length--;
|
55 |
|
|
}
|
56 |
|
|
|
57 |
|
|
return CRC_RESX;
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
|
61 |
|
|
/* ------------------------------------------------------------
|
62 |
|
|
* INFO Compute CRC32 from buffer
|
63 |
|
|
* PARAM start_val: Start value for CRC shift register
|
64 |
|
|
* PARAM polynomial: 32-bit polynomial XOR mask
|
65 |
|
|
* PARAM data: Pointer to BYTE input data array
|
66 |
|
|
* PARAM length: Number of elements in input data array
|
67 |
|
|
* RETURN CRC32 result
|
68 |
|
|
* ------------------------------------------------------------ */
|
69 |
|
|
uint32_t neo430_crc32(uint32_t start_val, uint32_t polynomial, uint8_t *data, uint16_t length) {
|
70 |
|
|
|
71 |
|
|
CRC_POLY32bit = polynomial;
|
72 |
|
|
CRC_R32bit = start_val;
|
73 |
|
|
|
74 |
|
|
while(length) {
|
75 |
|
|
CRC_CRC32IN = (uint16_t)(*data++); // no wait required here
|
76 |
|
|
length--;
|
77 |
|
|
}
|
78 |
|
|
|
79 |
|
|
return CRC_R32bit;
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
|
83 |
|
|
/* ------------------------------------------------------------
|
84 |
|
|
* INFO Initialize start value for CRC16
|
85 |
|
|
* PARAM 16-bit CRC shift reg start value
|
86 |
|
|
* ------------------------------------------------------------ */
|
87 |
|
|
void neo430_crc16_set_start_value(uint16_t start_val) {
|
88 |
|
|
|
89 |
|
|
CRC_RESX = start_val;
|
90 |
|
|
}
|
91 |
|
|
|
92 |
|
|
|
93 |
|
|
/* ------------------------------------------------------------
|
94 |
|
|
* INFO Initialize start value for CRC32
|
95 |
|
|
* PARAM 32-bit CRC shift reg start value
|
96 |
|
|
* ------------------------------------------------------------ */
|
97 |
|
|
void neo430_crc32_set_start_value(uint32_t start_val) {
|
98 |
|
|
|
99 |
|
|
CRC_R32bit = start_val;
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
|
103 |
|
|
/* ------------------------------------------------------------
|
104 |
|
|
* INFO Set polynomial mask for CRC16
|
105 |
|
|
* PARAM 16-bit CRC16 polynomial XOR mask
|
106 |
|
|
* ------------------------------------------------------------ */
|
107 |
|
|
void neo430_crc16_set_polynomial(uint16_t poly) {
|
108 |
|
|
|
109 |
|
|
CRC_POLY_LO = poly;
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
|
113 |
|
|
/* ------------------------------------------------------------
|
114 |
|
|
* INFO Set polynomial mask for CRC32
|
115 |
|
|
* PARAM 32-bit CRC16 polynomial XOR mask
|
116 |
|
|
* ------------------------------------------------------------ */
|
117 |
|
|
void neo430_crc32_set_polynomial(uint32_t poly) {
|
118 |
|
|
|
119 |
|
|
CRC_POLY32bit = poly;
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
|
123 |
|
|
/* ------------------------------------------------------------
|
124 |
|
|
* INFO Compute CRC16 for one new data byte
|
125 |
|
|
* PARAM 8-bit data input
|
126 |
|
|
* RETURN Current result of CRC16 shift reg
|
127 |
|
|
* ------------------------------------------------------------ */
|
128 |
|
|
uint16_t neo430_crc16_iterate(uint8_t data) {
|
129 |
|
|
|
130 |
|
|
CRC_CRC16IN = (uint16_t)data;
|
131 |
|
|
asm volatile ("nop");
|
132 |
|
|
return CRC_RESX;
|
133 |
|
|
}
|
134 |
|
|
|
135 |
|
|
|
136 |
|
|
/* ------------------------------------------------------------
|
137 |
|
|
* INFO Compute CRC32 for one new data byte
|
138 |
|
|
* PARAM 8-bit data input
|
139 |
|
|
* RETURN Current result of CRC32 shift reg
|
140 |
|
|
* ------------------------------------------------------------ */
|
141 |
|
|
uint32_t neo430_crc32_iterate(uint8_t data) {
|
142 |
|
|
|
143 |
|
|
CRC_CRC32IN = (uint16_t)data;
|
144 |
|
|
asm volatile ("nop");
|
145 |
|
|
return CRC_R32bit;
|
146 |
|
|
}
|