From 316c3b4825689afca170576e5a5681302f64fdfa Mon Sep 17 00:00:00 2001 From: Michael Spradling Date: Mon, 29 Jun 2015 21:54:13 -0400 Subject: Add CRC Driver This patch includes a high level and two low level drivers. The high level driver is enabled with flag HAL_USE_CRC The low level drivers include: * Hardware CRC for the STM32 cortex processor lines.(when supported) * Enabled with flag STM32_CRC_USE_CRC1 * DMA is enabled with CRC_USE_DMA * SYNC api will use DMA, but put calling thread to sleep * ASYNC api enabled. * DMA Disabled * SYNC api spin while calculating CRC * ASYNC api disabled * Software CRC (3 modes) * CRCSW_CRC32_TABLE - Enables crc32 with lookup table. * CRCSW_CRC16_TABLE - Enables crc16 with lookup tables. * CRCSW_PROGRAMMBLE - Enables any crc done with computation. * Can calculate any crc configuration. * CRC_USE_DMA obviously not support with software CRC --- os/hal/hal.mk | 3 +- os/hal/include/crc.h | 158 +++++++++++++++ os/hal/include/hal_community.h | 1 + os/hal/ports/STM32/LLD/CRCv1/crc_lld.c | 328 +++++++++++++++++++++++++++++++ os/hal/ports/STM32/LLD/CRCv1/crc_lld.h | 249 +++++++++++++++++++++++ os/hal/ports/STM32/STM32F0xx/platform.mk | 6 + os/hal/src/crc.c | 263 +++++++++++++++++++++++++ os/hal/src/hal_community.c | 4 + 8 files changed, 1011 insertions(+), 1 deletion(-) create mode 100644 os/hal/include/crc.h create mode 100644 os/hal/ports/STM32/LLD/CRCv1/crc_lld.c create mode 100644 os/hal/ports/STM32/LLD/CRCv1/crc_lld.h create mode 100644 os/hal/ports/STM32/STM32F0xx/platform.mk create mode 100644 os/hal/src/crc.c (limited to 'os/hal') diff --git a/os/hal/hal.mk b/os/hal/hal.mk index 51cda56..c193ef4 100644 --- a/os/hal/hal.mk +++ b/os/hal/hal.mk @@ -3,6 +3,7 @@ include ${CHIBIOS}/os/hal/hal.mk HALSRC += ${CHIBIOS}/community/os/hal/src/hal_community.c \ ${CHIBIOS}/community/os/hal/src/nand.c \ ${CHIBIOS}/community/os/hal/src/onewire.c \ - ${CHIBIOS}/community/os/hal/src/eicu.c + ${CHIBIOS}/community/os/hal/src/eicu.c \ + ${CHIBIOS}/community/os/hal/src/crc.c HALINC += ${CHIBIOS}/community/os/hal/include diff --git a/os/hal/include/crc.h b/os/hal/include/crc.h new file mode 100644 index 0000000..baa274b --- /dev/null +++ b/os/hal/include/crc.h @@ -0,0 +1,158 @@ +/* + ChibiOS - Copyright (C) 2015 Michael D. Spradling + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +#ifndef _CRC_H_ +#define _CRC_H_ + +#if HAL_USE_CRC || defined(__DOXYGEN__) + +/*===========================================================================*/ +/* Driver constants. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver pre-compile time settings. */ +/*===========================================================================*/ + +/** + * @name Configuration options + * @{ + */ + +/** + * @brief Enable DMA CRC + * @note Enables DMA when doing CRC calculations. This may be less + * efficient with smaller CRC calculations. + */ +#if !defined(CRC_USE_DMA) || defined(__DOXYGEN__) +#define CRC_USE_DMA FALSE +#endif + +/** + * @brief Enables the @p crcAcquireBus() and @p crcReleaseBus() APIs. + * @note Disabling this option saves both code and data space. + */ +#if !defined(CRC_USE_MUTUAL_EXCLUSION) || defined(__DOXYGEN__) +#define CRC_USE_MUTUAL_EXCLUSION TRUE +#endif + +/*===========================================================================*/ +/* Derived constants and error checks. */ +/*===========================================================================*/ + +#if STM32_CRC_USE_CRC1 != TRUE && CRCSW_USE_CRC1 != TRUE +#error "CRC requires at least one LLD driver." +#endif + +/*===========================================================================*/ +/* Driver data structures and types. */ +/*===========================================================================*/ + +/** + * @brief Driver state machine possible states. + */ +typedef enum { + CRC_UNINIT, /* Not initialized. */ + CRC_STOP, /* Stopped. */ + CRC_READY, /* Ready. */ + CRC_ACTIVE, /* Calculating CRC. */ + CRC_COMPLETE /* Asynchronous operation complete. */ +} crcstate_t; + +#include "crc_lld.h" /* Include software LL driver */ +#include "crcsw.h" + + +/*===========================================================================*/ +/* Driver macros. */ +/*===========================================================================*/ + +/** + * @name Low level driver helper macros + * @{ + */ + +/** + * @brief Wakes up the waiting thread. + * + * @param[in] crcp pointer to the @p CRCDriver object + * + * @notapi + */ +#define _crc_wakeup_isr(crcp) { \ + osalSysLockFromISR(); \ + osalThreadResumeI(&(crcp)->thread, MSG_OK); \ + osalSysUnlockFromISR(); \ +} + +/** + * @brief Common ISR code. + * @details This code handles the portable part of the ISR code: + * - Callback invocation. + * - Waiting thread wakeup, if any. + * - Driver state transitions. + * . + * @note This macro is meant to be used in the low level drivers + * implementation only. + * + * @param[in] crcp pointer to the @p CRCDriver object + * + * @notapi + */ +#define _crc_isr_code(crcp, crc) { \ + if ((crcp)->config->end_cb) { \ + (crcp)->state = CRC_COMPLETE; \ + (crcp)->config->end_cb(crcp, crc); \ + if ((crcp)->state == CRC_COMPLETE) \ + (crcp)->state = CRC_READY; \ + } \ + else \ + (crcp)->state = CRC_READY; \ + _crc_wakeup_isr(crcp); \ +} + +/*===========================================================================*/ +/* External declarations. */ +/*===========================================================================*/ + +#ifdef __cplusplus +extern "C" { +#endif + void crcInit(void); + void crcObjectInit(CRCDriver *crcp); + void crcStart(CRCDriver *crcp, const CRCConfig *config); + void crcStop(CRCDriver *crcp); + void crcReset(CRCDriver *crcp); + void crcResetI(CRCDriver *crcp); + uint32_t crcCalc(CRCDriver *crcp, size_t n, const void *buf); + uint32_t crcCalcI(CRCDriver *crcp, size_t n, const void *buf); +#if CRC_USE_DMA == TRUE + void crcStartCalc(CRCDriver *crcp, size_t n, const void *buf); + void crcStartCalcI(CRCDriver *crcp, size_t n, const void *buf); +#endif +#if CRC_USE_MUTUAL_EXCLUSION == TRUE + void crcAcquireUnit(CRCDriver *crcp); + void crcReleaseUnit(CRCDriver *crcp); +#endif +#ifdef __cplusplus +} +#endif + +#endif /* HAL_USE_CRC */ + +#endif /* _CRC_H_ */ + +/** @} */ diff --git a/os/hal/include/hal_community.h b/os/hal/include/hal_community.h index 6cd6b66..426fadd 100644 --- a/os/hal/include/hal_community.h +++ b/os/hal/include/hal_community.h @@ -35,6 +35,7 @@ /* Complex drivers.*/ #include "onewire.h" +#include "crc.h" /*===========================================================================*/ /* Driver constants. */ diff --git a/os/hal/ports/STM32/LLD/CRCv1/crc_lld.c b/os/hal/ports/STM32/LLD/CRCv1/crc_lld.c new file mode 100644 index 0000000..4bcb771 --- /dev/null +++ b/os/hal/ports/STM32/LLD/CRCv1/crc_lld.c @@ -0,0 +1,328 @@ +/* + ChibiOS - Copyright (C) 2015 Michael D. Spradling + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/** + * @file STM32/CRCv1/crc_lld.c + * @brief STM32 CRC subsystem low level driver source. + * + * @addtogroup CRC + * @{ + */ + +#include "hal.h" + +#if HAL_USE_CRC || defined(__DOXYGEN__) + +/** + * Allow CRC Software override for ST drivers. Some ST CRC implimentations + * have limited capabilities. + */ +#if CRCSW_USE_CRC1 != TRUE + +/*===========================================================================*/ +/* Driver local definitions. */ +/*===========================================================================*/ + +/** + * @brief CRC default configuration. + */ +static const CRCConfig default_config = { + .poly_size = 32, + .poly = 0x04C11DB7, + .initial_val = 0xFFFFFFFF, + .final_val = 0xFFFFFFFF, + .reflect_data = 1, + .reflect_remainder = 1 +}; + +/*===========================================================================*/ +/* Driver exported variables. */ +/*===========================================================================*/ + +/** @brief CRC1 driver identifier.*/ +#if STM32_CRC_USE_CRC1 || defined(__DOXYGEN__) +CRCDriver CRCD1; +#endif + +/*===========================================================================*/ +/* Driver local variables and types. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver local functions. */ +/*===========================================================================*/ + +void _crc_lld_calc_byte(CRCDriver *crcp, uint8_t data) { + __IO uint8_t *crc8 = (__IO uint8_t*)&(crcp->crc->DR); + *crc8 = data; +} + +/* + * @brief Returns calculated CRC from last reset + * + * @param[in] crcp pointer to the @p CRCDriver object + * @param[in] data data to be added to crc + * + * @notapi + */ +void _crc_lld_calc_halfword(CRCDriver *crcp, uint16_t data) { + __IO uint16_t *crc16 = (__IO uint16_t*)&(crcp->crc->DR); + *crc16 = data; +} + +/* + * @brief Returns calculated CRC from last reset + * + * @param[in] crcp pointer to the @p CRCDriver object + * @param[in] data data to be added to crc + * + * @notapi + */ +void _crc_lld_calc_word(CRCDriver *crcp, uint32_t data) { + crcp->crc->DR = data; +} + + +/*===========================================================================*/ +/* Driver interrupt handlers. */ +/*===========================================================================*/ + +/** + * @brief Shared end-of-rx service routine. + * + * @param[in] crcp pointer to the @p CRCDriver object + * @param[in] flags pre-shifted content of the ISR register + */ +#if CRC_USE_DMA == TRUE +static void crc_lld_serve_interrupt(CRCDriver *crcp, uint32_t flags) { + + /* DMA errors handling.*/ +#if defined(STM32_CRC_DMA_ERROR_HOOK) + if ((flags & (STM32_DMA_ISR_TEIF | STM32_DMA_ISR_DMEIF)) != 0) { + STM32_CRC_DMA_ERROR_HOOK(crcp); + } +#else + (void)flags; +#endif + + /* Stop everything.*/ + dmaStreamDisable(crcp->dma); + + /* Portable CRC ISR code defined in the high level driver, note, it is + a macro.*/ + _crc_isr_code(crcp, crcp->crc->DR ^ crcp->config->final_val); +} +#endif + +/*===========================================================================*/ +/* Driver exported functions. */ +/*===========================================================================*/ + +/** + * @brief Low level CRC driver initialization. + * + * @notapi + */ +void crc_lld_init(void) { + crcObjectInit(&CRCD1); + CRCD1.crc = CRC; +#if CRC_USE_DMA == TRUE + CRCD1.dma = STM32_CRC_CRC1_DMA_STREAM; +#endif +} + +/** + * @brief Configures and activates the CRC peripheral. + * + * @param[in] crcp pointer to the @p CRCDriver object + * + * @notapi + */ +void crc_lld_start(CRCDriver *crcp) { + if (crcp->config == NULL) + crcp->config = &default_config; + + rccEnableCRC(FALSE); + +#if STM32_CRC_PROGRAMMABLE == TRUE + crcp->crc->INIT = crcp->config->initial_val; + crcp->crc->POL = crcp->config->poly; + + crcp->crc->CR = 0; + switch(crcp->config->poly_size) { + case 32: + break; + case 16: + crcp->crc->CR |= CRC_CR_POLYSIZE_0; + break; + case 8: + crcp->crc->CR |= CRC_CR_POLYSIZE_1; + break; + case 7: + crcp->crc->CR |= CRC_CR_POLYSIZE_1 | CRC_CR_POLYSIZE_0; + break; + default: + osalDbgAssert(false, "hardware doesn't support polynomial size"); + break; + }; + if (crcp->config->reflect_data) { + crcp->crc->CR |= CRC_CR_REV_IN_1 | CRC_CR_REV_IN_0; + } + if (crcp->config->reflect_remainder) { + crcp->crc->CR |= CRC_CR_REV_OUT; + } +#else + osalDbgAssert(crcp->config->initial_val != default_config.initial_val, + "hardware doesn't support programmable initial value"); + osalDbgAssert(crcp->config->poly_size != default_config.poly_size, + "hardware doesn't support programmable polynomial size"); + osalDbgAssert(crcp->config->poly != default_config.poly, + "hardware doesn't support programmable polynomial"); + osalDbgAssert(crcp->config->reflect_data != default_config.reflect_data, + "hardware doesn't support reflect of input data"); + osalDbgAssert(crcp->config->reflect_remainder != default_config.reflect_remainder, + "hardware doesn't support reflect of output remainder"); +#endif + +#if CRC_USE_DMA == TRUE +#if STM32_CRC_PROGRAMMABLE == TRUE + crcp->dmamode = STM32_DMA_CR_DIR_M2M | STM32_DMA_CR_PINC | + STM32_DMA_CR_MSIZE_BYTE | STM32_DMA_CR_PSIZE_BYTE | + STM32_DMA_CR_TEIE | STM32_DMA_CR_TCIE | + STM32_DMA_CR_PL(STM32_CRC_CRC1_DMA_PRIORITY); +#else + crcp->dmamode = STM32_DMA_CR_DIR_M2M | STM32_DMA_CR_PINC | + STM32_DMA_CR_MSIZE_WORD | STM32_DMA_CR_PSIZE_WORD | + STM32_DMA_CR_TEIE | STM32_DMA_CR_TCIE | + STM32_DMA_CR_PL(STM32_CRC_CRC1_DMA_PRIORITY); +#endif + { + bool b; + b = dmaStreamAllocate(crcp->dma, + STM32_CRC_CRC1_DMA_IRQ_PRIORITY, + (stm32_dmaisr_t)crc_lld_serve_interrupt, + (void *)crcp); + osalDbgAssert(!b, "stream already allocated"); + } +#endif +} + + +/** + * @brief Deactivates the CRC peripheral. + * + * @param[in] crcp pointer to the @p CRCDriver object + * + * @notapi + */ +void crc_lld_stop(CRCDriver *crcp) { +#if CRC_USE_DMA == TRUE + dmaStreamRelease(crcp->dma); +#else + (void)crcp; +#endif + rccDisableCRC(FALSE); +} + +/** + * @brief Resets current CRC calculation. + * + * @param[in] crcp pointer to the @p CRCDriver object + * + * @notapi + */ +void crc_lld_reset(CRCDriver *crcp) { + crcp->crc->CR |= CRC_CR_RESET; +} + +/** + * @brief Returns calculated CRC from last reset + * + * @param[in] crcp pointer to the @p CRCDriver object + * @param[in] n size of buf in bytes + * @param[in] buf @p buffer location + * + * @notapi + */ +uint32_t crc_lld_calc(CRCDriver *crcp, size_t n, const void *buf) { +#if CRC_USE_DMA == TRUE + crc_lld_start_calc(crcp, n, buf); + (void) osalThreadSuspendS(&crcp->thread); +#else + /** + * BUG: Only peform byte writes to DR reg if reflect_data is disabled. + * The STM32 hardware unit seems to incorrectly calculate CRCs when all + * of the following is true: reflect_data(rev_in) is 0, dma is disable, and + * you are writing more than a byte into the DR register. + */ + if (crcp->config->reflect_data != 0) { + while(n > 3) { + _crc_lld_calc_word(crcp, *(uint32_t*)buf); + buf+=4; + n-=4; + } + } + +#if STM32_CRC_PROGRAMMABLE == TRUE + /* Programmable CRC units allow variable register width accesses.*/ + + /** + * BUG: Only peform byte writes to DR reg if reflect_data is disabled. + * The STM32 hardware unit seems to incorrectly calculate CRCs when all + * of the following is true: reflect_data(rev_in) is 0, dma is disable, and + * you are writing more than a byte into the DR register. + */ + if (crcp->config->reflect_data != 0) { + while(n > 1) { + _crc_lld_calc_halfword(crcp, *(uint16_t*)buf); + buf+=2; + n-=2; + } + } + + while(n > 0) { + _crc_lld_calc_byte(crcp, *(uint8_t*)buf); + buf++; + n--; + } +#else + osalDbgAssert(n != 0, "STM32 CRC Unit only supports WORD accesses"); +#endif + +#endif + return crcp->crc->DR ^ crcp->config->final_val; +} + +#if CRC_USE_DMA == TRUE +void crc_lld_start_calc(CRCDriver *crcp, size_t n, const void *buf) { + dmaStreamSetPeripheral(crcp->dma, buf); + dmaStreamSetMemory0(crcp->dma, &crcp->crc->DR); +#if STM32_CRC_PROGRAMMABLE == TRUE + dmaStreamSetTransactionSize(crcp->dma, n); +#else + dmaStreamSetTransactionSize(crcp->dma, (n / 4)); +#endif + dmaStreamSetMode(crcp->dma, crcp->dmamode); + + dmaStreamEnable(crcp->dma); +} +#endif + +#endif /* CRCSW_USE_CRC1 */ + +#endif /* HAL_USE_CRC */ + +/** @} */ diff --git a/os/hal/ports/STM32/LLD/CRCv1/crc_lld.h b/os/hal/ports/STM32/LLD/CRCv1/crc_lld.h new file mode 100644 index 0000000..6f70b42 --- /dev/null +++ b/os/hal/ports/STM32/LLD/CRCv1/crc_lld.h @@ -0,0 +1,249 @@ +/* + ChibiOS - Copyright (C) 2015 Michael D. Spradling + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/** + * @file STM32/CRCv1/crc_lld.h + * @brief STM32 CRC subsystem low level driver header. + * + * @addtogroup CRC + * @{ + */ + +#ifndef _CRC_LLD_H_ +#define _CRC_LLD_H_ + +#if HAL_USE_CRC || defined(__DOXYGEN__) + +/* + * This error check must occur outsite of CRCSW_USE_CRC1 to check if + * two LLD drivers are enabled at the same time + */ +#if STM32_CRC_USE_CRC1 == TRUE && \ + CRCSW_USE_CRC1 == TRUE +#error "Software CRC can't be enable with STM32_CRC_USE_CRC1" +#endif + +/** + * Allow CRC Software override for ST drivers. Some ST CRC implimentations + * have limited capabilities. + */ +#if CRCSW_USE_CRC1 != TRUE + +/*===========================================================================*/ +/* Driver constants. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver pre-compile time settings. */ +/*===========================================================================*/ + +/** + * @name Configuration options + * @{ + */ +/** + * @brief CRC1 driver enable switch. + * @details If set to @p TRUE the support for CRC1 is included. + * @note The default is @p FALSE. + */ +#if !defined(STM32_CRC_USE_CRC1) || defined(__DOXYGEN__) +#define STM32_CRC_USE_CRC1 FALSE +#endif + +/** + * @brief CRC1 DMA priority (0..3|lowest..highest). + * @note The priority level is for CRC DMA stream. + */ +#if !defined(STM32_CRC_CRC1_DMA_PRIORITY) || defined(__DOXYGEN__) +#define STM32_CRC_CRC1_DMA_PRIORITY 2 +#endif + +/** + * @brief CRC1 DMA interrupt priority level setting. + */ +#if !defined(STM32_CRC_CRC1_DMA_IRQ_PRIORITY) || defined(__DOXYGEN__) +#define STM32_CRC_CRC1_DMA_IRQ_PRIORITY 1 +#endif + +/** + * @brief CRC1 DMA STREAM to use when performing CRC calculation. + */ +#if !defined(STM32_CRC_CRC1_DMA_STREAM) || defined(__DOXYGEN__) +#define STM32_CRC_CRC1_DMA_STREAM STM32_DMA1_STREAM2 +#endif + +/** + * @brief CRC DMA error hook. + */ +#if !defined(STM32_CRC_DMA_ERROR_HOOK) || defined(__DOXYGEN__) +#define STM32_CRC_DMA_ERROR_HOOK(spip) osalSysHalt("DMA failure") +#endif + +/*===========================================================================*/ +/* Derived constants and error checks. */ +/*===========================================================================*/ + +#if STM32_CRC_USE_CRC1 && !STM32_HAS_CRC +#error "Hardware CRC not present in the selected device" +#error "Use CRCSW_USE_CRC1 for software implementation" +#endif + +#if CRC_USE_DMA +#if STM32_CRC_USE_CRC1 && \ + !OSAL_IRQ_IS_VALID_PRIORITY(STM32_CRC_CRC1_DMA_IRQ_PRIORITY) +#error "Invalid IRQ priority assigned to CRC1" +#endif + +#if STM32_CRC_USE_CRC1 && \ + !STM32_DMA_IS_VALID_PRIORITY(STM32_CRC_CRC1_DMA_PRIORITY) +#error "Invalid DMA priority assigned to CRC1" +#endif + +#if !defined(STM32_DMA_REQUIRED) +#define STM32_DMA_REQUIRED +#endif +#endif + +/*===========================================================================*/ +/* Driver data structures and types. */ +/*===========================================================================*/ + +/** + * @brief Type of a structure representing an CRC driver. + */ +typedef struct CRCDriver CRCDriver; + +/** + * @brief CRC notification callback type + * + * @param[in] crcp pointer to the @ CRCDriver object triggering the + * callback + */ +typedef void (*crccallback_t)(CRCDriver *crcp, uint32_t crc); + +/** + * @brief Driver configuration structure. + */ +typedef struct { + /** + * @brief The size of polynomial to be used for CRC. + */ + uint32_t poly_size; + /** + * @brief The coefficients of the polynomial to be used for CRC. + */ + uint32_t poly; + /** + * @brief The inital value + */ + uint32_t initial_val; + /** + * @brief The final XOR value + */ + uint32_t final_val; + /** + * @brief Reflect bit order data going into CRC + */ + bool reflect_data; + /** + * @brief Reflect bit order of final remainder + */ + bool reflect_remainder; + /* End of the mandatory fields.*/ + /** + * @brief Operation complete callback or @p NULL + */ + crccallback_t end_cb; +} CRCConfig; + + +/** + * @brief Structure representing an CRC driver. + */ +struct CRCDriver { + /** + * @brief Driver state. + */ + crcstate_t state; + /** + * @brief Current configuration data. + */ + const CRCConfig *config; +#if CRC_USE_MUTUAL_EXCLUSION || defined(__DOXYGEN__) + /** + * @brief Mutex protecting the peripheral. + */ + mutex_t mutex; +#endif /* CRC_USE_MUTUAL_EXCLUSION */ +#if defined(CRC_DRIVER_EXT_FIELDS) + CRC_DRIVER_EXT_FIELDS +#endif + /* End of the mandatory fields.*/ + /** + * @brief Pointer to the CRCx registers block. + */ + CRC_TypeDef *crc; + +#if CRC_USE_DMA == TRUE + /** + * @brief Waiting thread. + */ + thread_reference_t thread; + /** + * @brief CRC DMA stream + */ + const stm32_dma_stream_t *dma; + /** + * @brief DMA mode bit mask. + */ + uint32_t dmamode; +#endif +}; + +/*===========================================================================*/ +/* Driver macros. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* External declarations. */ +/*===========================================================================*/ + +#if STM32_CRC_USE_CRC1 && !defined(__DOXYGEN__) +extern CRCDriver CRCD1; +#endif /* STM32_CRC_USE_CRC1 */ + +#ifdef __cplusplus +extern "C" { +#endif + void crc_lld_init(void); + void crc_lld_start(CRCDriver *crcp); + void crc_lld_stop(CRCDriver *crcp); + void crc_lld_reset(CRCDriver *crcp); + uint32_t crc_lld_calc(CRCDriver *crcp, size_t n, const void *buf); +#if CRC_USE_DMA + void crc_lld_start_calc(CRCDriver *crcp, size_t n, const void *buf); +#endif +#ifdef __cplusplus +} +#endif + +#endif /* CRCSW_USE_CRC1 */ + +#endif /* HAL_USE_CRC */ + +#endif /* _CRC_LLD_H_ */ + +/** @} */ diff --git a/os/hal/ports/STM32/STM32F0xx/platform.mk b/os/hal/ports/STM32/STM32F0xx/platform.mk new file mode 100644 index 0000000..c1c0650 --- /dev/null +++ b/os/hal/ports/STM32/STM32F0xx/platform.mk @@ -0,0 +1,6 @@ +include ${CHIBIOS}/os/hal/ports/STM32/STM32F0xx/platform.mk + +PLATFORMSRC += ${CHIBIOS}/community/os/hal/ports/STM32/LLD/CRCv1/crc_lld.c + +PLATFORMINC += ${CHIBIOS}/community/os/hal/ports/STM32/LLD/CRCv1 \ + ${CHIBIOS}/community/os/hal/ports/STM32/LLD diff --git a/os/hal/src/crc.c b/os/hal/src/crc.c new file mode 100644 index 0000000..a238d91 --- /dev/null +++ b/os/hal/src/crc.c @@ -0,0 +1,263 @@ +/* + ChibiOS - Copyright (C) 2015 Michael D. Spradling + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/* + * Hardware Abstraction Layer for CRC Unit + */ +#include "hal.h" + +#if HAL_USE_CRC || defined(__DOXYGEN__) + +/*===========================================================================*/ +/* Driver local definitions. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver exported variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver local variables and types. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver local functions. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver exported functions. */ +/*===========================================================================*/ + +/** + * @brief CRC Driver initialization. + * + * @init + */ +void crcInit(void) { + crc_lld_init(); +} + +/** + * @brief Initializes the standard part of a @p CRCDriver structure. + * + * @param[out] crcp Pointer to the @p CRCDriver object + * + * @init + */ +void crcObjectInit(CRCDriver *crcp) { + crcp->state = CRC_STOP; + crcp->config = NULL; +#if CRC_USE_DMA == TRUE + crcp->thread = NULL; +#endif +#if CRC_USE_MUTUAL_EXCLUSION == TRUE + osalMutexObjectInit(&crcp->mutex); +#endif +#if defined(CRC_DRIVER_EXT_INIT_HOOK) + CRC_DRIVER_EXT_INIT_HOOK(crcp); +#endif +} + +/** + * @brief Configures and activates the CRC peripheral. + * + * @param[in] crcp Pointer to the @p CRCDriver object + * @param[in] config Pointer to the @p CRCConfig object + * @p NULL if the low level driver implementation + * supports a default configuration + * + * @api + */ +void crcStart(CRCDriver *crcp, const CRCConfig *config) { + osalDbgCheck(crcp != NULL); + + osalSysLock(); + osalDbgAssert((crcp->state == CRC_STOP) || (crcp->state == CRC_READY), + "invalid state"); + crcp->config = config; + crc_lld_start(crcp); + crcp->state = CRC_READY; + osalSysUnlock(); +} + +/** + * @brief Deactivates the CRC peripheral. + * + * @param[in] crcp Pointer to the @p CRCDriver object + * + * @api + */ +void crcStop(CRCDriver *crcp) { + osalDbgCheck(crcp != NULL); + + osalSysLock(); + osalDbgAssert((crcp->state == CRC_STOP) || (crcp->state == CRC_READY), + "invalid state"); + crc_lld_stop(crcp); + crcp->state = CRC_STOP; + osalSysUnlock(); +} + +/** + * @brief Resets the CRC calculation + * + * @param[in] crcp Pointer to the @p CRCDriver object + * + * @api + */ +void crcReset(CRCDriver *crcp) { + osalSysLock(); + crcResetI(crcp); + osalSysUnlock(); +} + +/** + * @brief Resets the current CRC calculation + * + * @param[in] crcp pointer to the @p CRCDriver object + * + * @iclass + */ +void crcResetI(CRCDriver *crcp) { + osalDbgCheck(crcp != NULL); + osalDbgAssert(crcp->state == CRC_READY, "Not ready"); + crc_lld_reset(crcp); +} + +/** + * @brief Performs a CRC calculation. + * @details This synchronous function performs a crc calculation operation. + * @pre In order to use this function the driver must have been configured + * without callbacks (@p end_cb = @p NULL). + * + * @param[in] crcp pointer to the @p CRCDriver object + * @param[in] n number of bytes to send + * @param[in] buf the pointer to the buffer + * + * @api + */ +uint32_t crcCalc(CRCDriver *crcp, size_t n, const void *buf) { + uint32_t crc; + osalSysLock(); + crc = crcCalcI(crcp, n, buf); + osalSysUnlock(); + return crc; +} + +/** + * @brief Performs a CRC calculation. + * @details This synchronous function performs a crc calcuation operation. + * @pre In order to use this function the driver must have been configured + * without callbacks (@p end_cb = @p NULL). + * @post At the end of the operation the configured callback is invoked. + * + * @param[in] crcp pointer to the @p CRCDriver object + * @param[in] n number of bytes to send + * @param[in] buf the pointer to the buffer + * + * @iclass + */ +uint32_t crcCalcI(CRCDriver *crcp, size_t n, const void *buf) { + osalDbgCheck((crcp != NULL) && (n > 0U) && (buf != NULL)); + osalDbgAssert(crcp->state == CRC_READY, "not ready"); +#if CRC_USE_DMA + osalDbgAssert(crcp->config->end_cb == NULL, "callback defined"); + (crcp)->state = CRC_ACTIVE; +#endif + return crc_lld_calc(crcp, n, buf); +} + +#if CRC_USE_DMA == TRUE +/** + * @brief Performs a CRC calculation. + * @details This asynchronous function starts a crc calcuation operation. + * @pre In order to use this function the driver must have been configured + * without callbacks (@p end_cb = @p NULL). + * @post At the end of the operation the configured callback is invoked. + * + * @param[in] crcp pointer to the @p CRCDriver object + * @param[in] n number of bytes to send + * @param[in] buf the pointer to the buffer + * + * @api + */ +void crcStartCalc(CRCDriver *crcp, size_t n, const void *buf) { + osalDbgCheck((crcp != NULL) && (n > 0U) && (buf != NULL)); + osalSysLock(); + osalDbgAssert(crcp->state == CRC_READY, "not ready"); + osalDbgAssert(crcp->config->end_cb != NULL, "callback not defined"); + crcStartCalcI(crcp, n, buf); + osalSysUnlock(); +} + +/** + * @brief Performs a CRC calculation. + * @details This asynchronous function starts a crc calcuation operation. + * @pre In order to use this function the driver must have been configured + * without callbacks (@p end_cb = @p NULL). + * @post At the end of the operation the configured callback is invoked. + * + * @param[in] crcp pointer to the @p CRCDriver object + * @param[in] n number of bytes to send + * @param[in] buf the pointer to the buffer + * + * + * @iclass + */ +void crcStartCalcI(CRCDriver *crcp, size_t n, const void *buf) { + osalDbgCheck((crcp != NULL) && (n > 0U) && (buf != NULL)); + osalDbgAssert(crcp->state == CRC_READY, "not ready"); + osalDbgAssert(crcp->config->end_cb != NULL, "callback defined"); + (crcp)->state = CRC_ACTIVE; + crc_lld_start_calc(crcp, n, buf); +} +#endif + +#if (CRC_USE_MUTUAL_EXCLUSION == TRUE) || defined(__DOXYGEN__) +/** + * @brief Gains exclusive access to the CRC unit. + * @details This function tries to gain ownership to the CRC, if the CRC is + * already being used then the invoking thread is queued. + * @pre In order to use this function the option @p CRC_USE_MUTUAL_EXCLUSION + * must be enabled. + * + * @param[in] crcp pointer to the @p CRCDriver object + * + * @api + */ +void crcAcquireUnit(CRCDriver *crcp) { + osalDbgCheck(crcp != NULL); + + osalMutexLock(&crcp->mutex); +} + +/** + * @brief Releases exclusive access to the CRC unit. + * @pre In order to use this function the option @p CRC_USE_MUTUAL_EXCLUSION + * must be enabled. + * + * @param[in] crcp pointer to the @p CRCDriver object + * + * @api + */ +void crcReleaseUnit(CRCDriver *crcp) { + osalDbgCheck(crcp != NULL); + + osalMutexUnlock(&crcp->mutex); +} +#endif /* CRC_USE_MUTUAL_EXCLUSION == TRUE */ + +#endif /* HAL_USE_CRC */ diff --git a/os/hal/src/hal_community.c b/os/hal/src/hal_community.c index 7c71e3f..b9700ba 100644 --- a/os/hal/src/hal_community.c +++ b/os/hal/src/hal_community.c @@ -60,6 +60,10 @@ void halCommunityInit(void) { #if HAL_USE_EICU || defined(__DOXYGEN__) eicuInit(); #endif + +#if HAL_USE_CRC || defined(__DOXYGEN__) + crcInit(); +#endif } #endif /* HAL_USE_COMMUNITY */ -- cgit v1.2.3 From fb8c390f06ee9466f498dc66a2bd955e5ba51b92 Mon Sep 17 00:00:00 2001 From: Michael Spradling Date: Sun, 16 Aug 2015 23:51:41 -0400 Subject: Update code from code feedback --- os/hal/include/crc.h | 4 ++-- os/hal/src/crc.c | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) (limited to 'os/hal') diff --git a/os/hal/include/crc.h b/os/hal/include/crc.h index baa274b..2f50f8f 100644 --- a/os/hal/include/crc.h +++ b/os/hal/include/crc.h @@ -72,8 +72,8 @@ typedef enum { CRC_COMPLETE /* Asynchronous operation complete. */ } crcstate_t; -#include "crc_lld.h" /* Include software LL driver */ -#include "crcsw.h" +#include "crc_lld.h" +#include "crcsw.h" /* Include software LL driver */ /*===========================================================================*/ diff --git a/os/hal/src/crc.c b/os/hal/src/crc.c index a238d91..e8c2d0b 100644 --- a/os/hal/src/crc.c +++ b/os/hal/src/crc.c @@ -151,9 +151,13 @@ void crcResetI(CRCDriver *crcp) { */ uint32_t crcCalc(CRCDriver *crcp, size_t n, const void *buf) { uint32_t crc; +#if CRC_USE_DMA osalSysLock(); +#endif crc = crcCalcI(crcp, n, buf); +#if CRC_USE_DMA osalSysUnlock(); +#endif return crc; } @@ -185,7 +189,7 @@ uint32_t crcCalcI(CRCDriver *crcp, size_t n, const void *buf) { * @brief Performs a CRC calculation. * @details This asynchronous function starts a crc calcuation operation. * @pre In order to use this function the driver must have been configured - * without callbacks (@p end_cb = @p NULL). + * with callbacks (@p end_cb != @p NULL). * @post At the end of the operation the configured callback is invoked. * * @param[in] crcp pointer to the @p CRCDriver object @@ -195,10 +199,7 @@ uint32_t crcCalcI(CRCDriver *crcp, size_t n, const void *buf) { * @api */ void crcStartCalc(CRCDriver *crcp, size_t n, const void *buf) { - osalDbgCheck((crcp != NULL) && (n > 0U) && (buf != NULL)); osalSysLock(); - osalDbgAssert(crcp->state == CRC_READY, "not ready"); - osalDbgAssert(crcp->config->end_cb != NULL, "callback not defined"); crcStartCalcI(crcp, n, buf); osalSysUnlock(); } @@ -220,7 +221,7 @@ void crcStartCalc(CRCDriver *crcp, size_t n, const void *buf) { void crcStartCalcI(CRCDriver *crcp, size_t n, const void *buf) { osalDbgCheck((crcp != NULL) && (n > 0U) && (buf != NULL)); osalDbgAssert(crcp->state == CRC_READY, "not ready"); - osalDbgAssert(crcp->config->end_cb != NULL, "callback defined"); + osalDbgAssert(crcp->config->end_cb != NULL, "callback not defined"); (crcp)->state = CRC_ACTIVE; crc_lld_start_calc(crcp, n, buf); } -- cgit v1.2.3