aboutsummaryrefslogtreecommitdiff
path: root/src/crc32.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/crc32.c')
-rw-r--r--src/crc32.c42
1 files changed, 24 insertions, 18 deletions
diff --git a/src/crc32.c b/src/crc32.c
index d5c8732..6f1c0e3 100644
--- a/src/crc32.c
+++ b/src/crc32.c
@@ -18,7 +18,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#include "platform.h"
+#include "general.h"
#include "target.h"
#if !defined(STM32F1) && !defined(STM32F4)
@@ -94,14 +94,13 @@ uint32_t crc32_calc(uint32_t crc, uint8_t data)
return (crc << 8) ^ crc32_table[((crc >> 24) ^ data) & 255];
}
-uint32_t generic_crc32(struct target_s *target, uint32_t base, int len)
+uint32_t generic_crc32(target *t, uint32_t base, int len)
{
uint32_t crc = -1;
uint8_t byte;
while (len--) {
- if (target_mem_read_bytes(target, &byte, base, 1) != 0)
- return -1;
+ byte = target_mem_read8(t, base);
crc = crc32_calc(crc, byte);
base++;
@@ -110,29 +109,36 @@ uint32_t generic_crc32(struct target_s *target, uint32_t base, int len)
}
#else
#include <libopencm3/stm32/crc.h>
-uint32_t generic_crc32(struct target_s *target, uint32_t base, int len)
+uint32_t generic_crc32(target *t, uint32_t base, int len)
{
uint32_t data;
- uint8_t byte;
+ uint32_t crc;
+ size_t i;
- CRC_CR |= CRC_CR_RESET;
+ CRC_CR |= CRC_CR_RESET;
- while (len >3) {
- if (target_mem_read_words(target, &data, base, 1) != 0)
- return -1;
+ while (len > 3) {
+ data = target_mem_read32(t, base);
- CRC_DR = data;
- base+=4;
- len -= 4;
+ CRC_DR = __builtin_bswap32(data);
+ base += 4;
+ len -= 4;
}
+
+ crc = CRC_DR;
+
while (len--) {
- if (target_mem_read_bytes(target, &byte, base, 1) != 0)
- return -1;
+ data = target_mem_read8(t, base++);
- CRC_DR = byte;
- base++;
+ crc ^= data << 24;
+ for (i = 0; i < 8; i++) {
+ if (crc & 0x80000000)
+ crc = (crc << 1) ^ 0x4C11DB7;
+ else
+ crc <<= 1;
+ }
}
- return CRC_DR;
+ return crc;
}
#endif