diff --git a/include/libopencm3/stm32/common/crc_v2.h b/include/libopencm3/stm32/common/crc_v2.h new file mode 100644 index 00000000..1c60b38d --- /dev/null +++ b/include/libopencm3/stm32/common/crc_v2.h @@ -0,0 +1,110 @@ +/** @addtogroup crc_defines + + @author @htmlonly © @endhtmlonly 2016 Cem Basoglu + + */ + +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2016 Cem Basoglu + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* THIS FILE SHOULD NOT BE INCLUDED DIRECTLY, BUT ONLY VIA CRC.H + The order of header inclusion is important. crc.h includes the device + specific memorymap.h header before including this header file.*/ + +/** @cond */ +#ifdef LIBOPENCM3_CRC_H +/** @endcond */ +#ifndef LIBOPENCM3_CRC_V2_H +#define LIBOPENCM3_CRC_V2_H + +/**@{*/ + +#include + +/*****************************************************************************/ +/* Module definitions */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Register definitions */ +/*****************************************************************************/ + +/* Data register (CRC_DR) */ +#define CRC_DR8 MMIO8(CRC_BASE + 0x00) +#define CRC_DR16 MMIO16(CRC_BASE + 0x00) + +/* Initial CRC Value */ +#define CRC_INIT MMIO32(CRC_BASE + 0x10) + +/* CRC Polynomial */ +#define CRC_POL MMIO32(CRC_BASE + 0x14) + +/*****************************************************************************/ +/* Register values */ +/*****************************************************************************/ + +#define CRC_CR_REV_OUT (1 << 7) + +#define CRC_CR_REV_IN_SHIFT 5 +#define CRC_CR_REV_IN (3 << CRC_CR_REV_IN_SHIFT) +#define CRC_CR_REV_IN_NONE (0 << CRC_CR_REV_IN_SHIFT) +#define CRC_CR_REV_IN_BYTE (1 << CRC_CR_REV_IN_SHIFT) +#define CRC_CR_REV_IN_HALF (2 << CRC_CR_REV_IN_SHIFT) +#define CRC_CR_REV_IN_WORD (3 << CRC_CR_REV_IN_SHIFT) + +#define CRC_CR_POLYSIZE_SHIFT 3 +#define CRC_CR_POLYSIZE (3 << CRC_CR_POLYSIZE_SHIFT) +#define CRC_CR_POLYSIZE_32 (0 << CRC_CR_POLYSIZE_SHIFT) +#define CRC_CR_POLYSIZE_16 (1 << CRC_CR_POLYSIZE_SHIFT) +#define CRC_CR_POLYSIZE_8 (2 << CRC_CR_POLYSIZE_SHIFT) +#define CRC_CR_POLYSIZE_7 (3 << CRC_CR_POLYSIZE_SHIFT) + +/* Default polynomial */ +#define CRC_POL_DEFAULT 0x04C11DB7 + +/*****************************************************************************/ +/* API definitions */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* API Functions */ +/*****************************************************************************/ + +BEGIN_DECLS + +void crc_reverse_output_enable(void); +void crc_reverse_output_disable(void); + +void crc_set_reverse_input(uint32_t reverse_in); +void crc_set_polysize(uint32_t polysize); + +void crc_set_polynomial(uint32_t polynomial); +void crc_set_initial(uint32_t initial); + +END_DECLS + +/**@}*/ + +#endif +/** @cond */ +#else +#warning "crc_v2.h should not be included explicitly, only via crc.h" +#endif +/** @endcond */ + diff --git a/include/libopencm3/stm32/f0/crc.h b/include/libopencm3/stm32/f0/crc.h index 34d8a897..fb8ff8f2 100644 --- a/include/libopencm3/stm32/f0/crc.h +++ b/include/libopencm3/stm32/f0/crc.h @@ -32,58 +32,7 @@ #ifndef LIBOPENCM3_CRC_H #define LIBOPENCM3_CRC_H -/**@{*/ -#include - -/*****************************************************************************/ -/* Module definitions */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* Register definitions */ -/*****************************************************************************/ - -/* Initial CRC Value */ -#define CRC_INIT MMIO32(CRC_BASE + 0x10) - -/* CRC Polynomial */ -#define CRC_POL MMIO32(CRC_BASE + 0x14) - -/*****************************************************************************/ -/* Register values */ -/*****************************************************************************/ - -#define CRC_CR_REV_OUT (1 << 7) - -#define CRC_CR_REV_IN_SHIFT 5 -#define CRC_CR_REV_IN (3 << CRC_CR_REV_IN_SHIFT) -#define CRC_CR_REV_IN_NONE (0 << CRC_CR_REV_IN_SHIFT) -#define CRC_CR_REV_IN_BYTE (1 << CRC_CR_REV_IN_SHIFT) -#define CRC_CR_REV_IN_HALF (2 << CRC_CR_REV_IN_SHIFT) -#define CRC_CR_REV_IN_WORD (3 << CRC_CR_REV_IN_SHIFT) - -#define CRC_CR_POLYSIZE_SHIFT 3 -#define CRC_CR_POLYSIZE (3 << CRC_CR_POLYSIZE_SHIFT) -#define CRC_CR_POLYSIZE_32BIT (0 << CRC_CR_POLYSIZE_SHIFT) -#define CRC_CR_POLYSIZE_16BIT (1 << CRC_CR_POLYSIZE_SHIFT) -#define CRC_CR_POLYSIZE_8BIT (2 << CRC_CR_POLYSIZE_SHIFT) -#define CRC_CR_POLYSIZE_7BIT (3 << CRC_CR_POLYSIZE_SHIFT) - -/* Default polynomial */ -#define CRC_POL_DEFAULT 0x04C11DB7 - -/*****************************************************************************/ -/* API definitions */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* API Functions */ -/*****************************************************************************/ - -BEGIN_DECLS - -END_DECLS -/**@}*/ +#include #endif diff --git a/include/libopencm3/stm32/f3/crc.h b/include/libopencm3/stm32/f3/crc.h index 828832b0..469ffb7d 100644 --- a/include/libopencm3/stm32/f3/crc.h +++ b/include/libopencm3/stm32/f3/crc.h @@ -32,39 +32,6 @@ #ifndef LIBOPENCM3_CRC_H #define LIBOPENCM3_CRC_H -#include - -/* --- CRC registers ------------------------------------------------------- */ - -/* Initial CRC value (CRC_INIT) */ -#define CRC_INIT MMIO32(CRC_BASE + 0x10) - -/* CRC polynomial (CRC_POL) */ -#define CRC_POL MMIO32(CRC_BASE + 0x14) - -/* --- CRC_CR values ------------------------------------------------------- */ - -/* REV_OUT: Reverse output data */ -#define CRC_CR_REV_OUT (1 << 7) - -/* REV_IN[1:0]: Reverse input data */ -#define CRC_CR_REV_IN_NOT_AFFECTED (0x0 << 5) -#define CRC_CR_REV_IN_BYTE (0x1 << 5) -#define CRC_CR_REV_IN_HALF_WORD (0x2 << 5) -#define CRC_CR_REV_IN_WORD (0x3 << 5) - -/* POLYSIZE[1:0]: Polynomial size */ -#define CRC_CR_POLYSIZE_32 (0x0 << 3) -#define CRC_CR_POLYSIZE_16 (0x1 << 3) -#define CRC_CR_POLYSIZE_8 (0x2 << 3) -#define CRC_CR_POLYSIZE_7 (0x3 << 3) - -/* --- CRC_INIT values ----------------------------------------------------- */ - -/* Bits 31:0 CRC_INIT: Programmable initial CRC value */ - -/* --- CRC_POL values ------------------------------------------------------ */ - -/* Bits 31:0 POL[31:0]: Programmable polynomial */ +#include #endif diff --git a/lib/stm32/common/crc_v2.c b/lib/stm32/common/crc_v2.c new file mode 100644 index 00000000..5f25fe40 --- /dev/null +++ b/lib/stm32/common/crc_v2.c @@ -0,0 +1,115 @@ +/** @addtogroup crc_defines + + @author @htmlonly © @endhtmlonly 2016 Cem Basoglu + + */ + +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2016 Cem Basoglu + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + + +/**@{*/ + +#include + +/*---------------------------------------------------------------------------*/ +/** @brief Enable reverse output data. + + Enables the reversal of the bit order of the output data. + + */ +void crc_reverse_output_enable() +{ + CRC_CR |= CRC_CR_REV_OUT; +} + +/*---------------------------------------------------------------------------*/ +/** @brief Disable reverse output data. + + Disables the reversal of the bit order of the output data. + + */ +void crc_reverse_output_disable() +{ + CRC_CR &= ~CRC_CR_REV_OUT; +} + +/*---------------------------------------------------------------------------*/ +/** @brief Reverse input data. + + Controls the reversal of the bit order of the input data + + @param[in] reverse_in Unsigned int32. Reversal bit order @ref crc_rev_in. + */ +void crc_set_reverse_input(uint32_t reverse_in) +{ + uint32_t reg32 = CRC_CR; + reg32 = (reg32 & ~CRC_CR_REV_IN) | reverse_in; + CRC_CR = reg32; +} + +/*---------------------------------------------------------------------------*/ +/** @brief Polynomial size + + Set the size of the polynomial. + + @param[in] polysize Unsigned int32. Size of polynomial @ref crc_polysize. + */ +void crc_set_polysize(uint32_t polysize) +{ + uint32_t reg32 = CRC_CR; + reg32 = (reg32 & ~CRC_CR_POLYSIZE) | polysize; + CRC_CR = reg32; +} + +/*---------------------------------------------------------------------------*/ +/** @brief Polynomial coefficient + + Set the coefficients of the polynomial to be used for CRC calculation. + If the polynomial size is less than 32-bits, the least significant bits + have to be used to program the correct value. + + @note To obtain a reliable CRC calculation, any changes to the polynomial + value or size can not be performed during a CRC calculation. + As a result, if a CRC calculation is ongoing, the application must either + reset the crc unit it or perform a CRC_DR read before changing the polynomial. + + @note The default polynomial value is the CRC-32 (Ethernet) polynomial: 0x4C11DB7. + + @param[in] polynomial Unsigned int32. Polynomial coefficient. + */ +void crc_set_polynomial(uint32_t polynomial) +{ + CRC_POL = polynomial; +} + +/*---------------------------------------------------------------------------*/ +/** @brief CRC Initial value. + +Sets the crc initial value. + + @param[in] initial Unsigned int32. CRC initial value. + */ +void crc_set_initial(uint32_t initial) +{ + CRC_INIT = initial; +} + +/**@}*/ + diff --git a/lib/stm32/f0/Makefile b/lib/stm32/f0/Makefile index 59e26656..c98fc10c 100644 --- a/lib/stm32/f0/Makefile +++ b/lib/stm32/f0/Makefile @@ -39,7 +39,7 @@ ARFLAGS = rcs OBJS = can.o flash.o rcc.o usart.o dma.o rtc.o comparator.o crc.o \ dac.o iwdg.o pwr.o gpio.o timer.o adc.o desig.o -OBJS += gpio_common_all.o gpio_common_f0234.o crc_common_all.o \ +OBJS += gpio_common_all.o gpio_common_f0234.o crc_common_all.o crc_v2.o \ pwr_common_v1.o iwdg_common_all.o rtc_common_l1f024.o \ dma_common_l1f013.o exti_common_all.o spi_common_all.o \ spi_common_f03.o flash_common_f01.o dac_common_all.o \ diff --git a/lib/stm32/f3/Makefile b/lib/stm32/f3/Makefile index 68527a0b..a5f5ace4 100644 --- a/lib/stm32/f3/Makefile +++ b/lib/stm32/f3/Makefile @@ -40,7 +40,7 @@ ARFLAGS = rcs OBJS = rcc.o adc.o can.o pwr.o usart.o dma.o flash.o desig.o OBJS += gpio_common_all.o gpio_common_f0234.o \ - dac_common_all.o crc_common_all.o \ + dac_common_all.o crc_common_all.o crc_v2.o \ iwdg_common_all.o pwr_common_v1.o spi_common_all.o dma_common_l1f013.o\ timer_common_all.o timer_common_f0234.o flash_common_f234.o \ flash.o exti_common_all.o rcc_common_all.o spi_common_f03.o