mbed library sources

Dependents:   Marvino mbot

Fork of mbed-src by mbed official

Committer:
jaerts
Date:
Tue Dec 22 13:22:16 2015 +0000
Revision:
637:ed69428d4850
Parent:
610:813dcc80987e
Add very shady LPC1768 CAN Filter implementation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 610:813dcc80987e 1 /**
mbed_official 610:813dcc80987e 2 ******************************************************************************
mbed_official 610:813dcc80987e 3 * @file stm32l4xx_hal_crc.c
mbed_official 610:813dcc80987e 4 * @author MCD Application Team
mbed_official 610:813dcc80987e 5 * @version V1.0.0
mbed_official 610:813dcc80987e 6 * @date 26-June-2015
mbed_official 610:813dcc80987e 7 * @brief CRC HAL module driver.
mbed_official 610:813dcc80987e 8 * This file provides firmware functions to manage the following
mbed_official 610:813dcc80987e 9 * functionalities of the CRC peripheral:
mbed_official 610:813dcc80987e 10 * + Initialization and de-initialization functions
mbed_official 610:813dcc80987e 11 * + Peripheral Control functions
mbed_official 610:813dcc80987e 12 * + Peripheral State functions
mbed_official 610:813dcc80987e 13 *
mbed_official 610:813dcc80987e 14 @verbatim
mbed_official 610:813dcc80987e 15 ===============================================================================
mbed_official 610:813dcc80987e 16 ##### How to use this driver #####
mbed_official 610:813dcc80987e 17 ===============================================================================
mbed_official 610:813dcc80987e 18 [..]
mbed_official 610:813dcc80987e 19 (+) Enable CRC AHB clock using __HAL_RCC_CRC_CLK_ENABLE();
mbed_official 610:813dcc80987e 20 (+) Initialize CRC calculator
mbed_official 610:813dcc80987e 21 (++) specify generating polynomial (IP default or non-default one)
mbed_official 610:813dcc80987e 22 (++) specify initialization value (IP default or non-default one)
mbed_official 610:813dcc80987e 23 (++) specify input data format
mbed_official 610:813dcc80987e 24 (++) specify input or output data inversion mode if any
mbed_official 610:813dcc80987e 25 (+) Use HAL_CRC_Accumulate() function to compute the CRC value of the
mbed_official 610:813dcc80987e 26 input data buffer starting with the previously computed CRC as
mbed_official 610:813dcc80987e 27 initialization value
mbed_official 610:813dcc80987e 28 (+) Use HAL_CRC_Calculate() function to compute the CRC value of the
mbed_official 610:813dcc80987e 29 input data buffer starting with the defined initialization value
mbed_official 610:813dcc80987e 30 (default or non-default) to initiate CRC calculation
mbed_official 610:813dcc80987e 31
mbed_official 610:813dcc80987e 32 @endverbatim
mbed_official 610:813dcc80987e 33 ******************************************************************************
mbed_official 610:813dcc80987e 34 * @attention
mbed_official 610:813dcc80987e 35 *
mbed_official 610:813dcc80987e 36 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
mbed_official 610:813dcc80987e 37 *
mbed_official 610:813dcc80987e 38 * Redistribution and use in source and binary forms, with or without modification,
mbed_official 610:813dcc80987e 39 * are permitted provided that the following conditions are met:
mbed_official 610:813dcc80987e 40 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 610:813dcc80987e 41 * this list of conditions and the following disclaimer.
mbed_official 610:813dcc80987e 42 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 610:813dcc80987e 43 * this list of conditions and the following disclaimer in the documentation
mbed_official 610:813dcc80987e 44 * and/or other materials provided with the distribution.
mbed_official 610:813dcc80987e 45 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 610:813dcc80987e 46 * may be used to endorse or promote products derived from this software
mbed_official 610:813dcc80987e 47 * without specific prior written permission.
mbed_official 610:813dcc80987e 48 *
mbed_official 610:813dcc80987e 49 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 610:813dcc80987e 50 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 610:813dcc80987e 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 610:813dcc80987e 52 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 610:813dcc80987e 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 610:813dcc80987e 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 610:813dcc80987e 55 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 610:813dcc80987e 56 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 610:813dcc80987e 57 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 610:813dcc80987e 58 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 610:813dcc80987e 59 *
mbed_official 610:813dcc80987e 60 ******************************************************************************
mbed_official 610:813dcc80987e 61 */
mbed_official 610:813dcc80987e 62
mbed_official 610:813dcc80987e 63 /* Includes ------------------------------------------------------------------*/
mbed_official 610:813dcc80987e 64 #include "stm32l4xx_hal.h"
mbed_official 610:813dcc80987e 65
mbed_official 610:813dcc80987e 66 /** @addtogroup STM32L4xx_HAL_Driver
mbed_official 610:813dcc80987e 67 * @{
mbed_official 610:813dcc80987e 68 */
mbed_official 610:813dcc80987e 69
mbed_official 610:813dcc80987e 70 /** @defgroup CRC CRC
mbed_official 610:813dcc80987e 71 * @brief CRC HAL module driver.
mbed_official 610:813dcc80987e 72 * @{
mbed_official 610:813dcc80987e 73 */
mbed_official 610:813dcc80987e 74
mbed_official 610:813dcc80987e 75 #ifdef HAL_CRC_MODULE_ENABLED
mbed_official 610:813dcc80987e 76
mbed_official 610:813dcc80987e 77 /* Private typedef -----------------------------------------------------------*/
mbed_official 610:813dcc80987e 78 /* Private define ------------------------------------------------------------*/
mbed_official 610:813dcc80987e 79 /* Private macro -------------------------------------------------------------*/
mbed_official 610:813dcc80987e 80 /* Private variables ---------------------------------------------------------*/
mbed_official 610:813dcc80987e 81 /* Private function prototypes -----------------------------------------------*/
mbed_official 610:813dcc80987e 82 /** @defgroup CRC_Private_Functions CRC Private Functions
mbed_official 610:813dcc80987e 83 * @{
mbed_official 610:813dcc80987e 84 */
mbed_official 610:813dcc80987e 85 static uint32_t CRC_Handle_8(CRC_HandleTypeDef *hcrc, uint8_t pBuffer[], uint32_t BufferLength);
mbed_official 610:813dcc80987e 86 static uint32_t CRC_Handle_16(CRC_HandleTypeDef *hcrc, uint16_t pBuffer[], uint32_t BufferLength);
mbed_official 610:813dcc80987e 87 /**
mbed_official 610:813dcc80987e 88 * @}
mbed_official 610:813dcc80987e 89 */
mbed_official 610:813dcc80987e 90
mbed_official 610:813dcc80987e 91 /* Exported functions --------------------------------------------------------*/
mbed_official 610:813dcc80987e 92
mbed_official 610:813dcc80987e 93 /** @defgroup CRC_Exported_Functions CRC Exported Functions
mbed_official 610:813dcc80987e 94 * @{
mbed_official 610:813dcc80987e 95 */
mbed_official 610:813dcc80987e 96
mbed_official 610:813dcc80987e 97 /** @defgroup CRC_Exported_Functions_Group1 Initialization and de-initialization functions
mbed_official 610:813dcc80987e 98 * @brief Initialization and Configuration functions.
mbed_official 610:813dcc80987e 99 *
mbed_official 610:813dcc80987e 100 @verbatim
mbed_official 610:813dcc80987e 101 ===============================================================================
mbed_official 610:813dcc80987e 102 ##### Initialization and de-initialization functions #####
mbed_official 610:813dcc80987e 103 ===============================================================================
mbed_official 610:813dcc80987e 104 [..] This section provides functions allowing to:
mbed_official 610:813dcc80987e 105 (+) Initialize the CRC according to the specified parameters
mbed_official 610:813dcc80987e 106 in the CRC_InitTypeDef and create the associated handle
mbed_official 610:813dcc80987e 107 (+) DeInitialize the CRC peripheral
mbed_official 610:813dcc80987e 108 (+) Initialize the CRC MSP (MCU Specific Package)
mbed_official 610:813dcc80987e 109 (+) DeInitialize the CRC MSP
mbed_official 610:813dcc80987e 110
mbed_official 610:813dcc80987e 111 @endverbatim
mbed_official 610:813dcc80987e 112 * @{
mbed_official 610:813dcc80987e 113 */
mbed_official 610:813dcc80987e 114
mbed_official 610:813dcc80987e 115 /**
mbed_official 610:813dcc80987e 116 * @brief Initializes the CRC according to the specified
mbed_official 610:813dcc80987e 117 * parameters in the CRC_InitTypeDef and initialize the associated handle.
mbed_official 610:813dcc80987e 118 * @param hcrc: CRC handle
mbed_official 610:813dcc80987e 119 * @retval HAL status
mbed_official 610:813dcc80987e 120 */
mbed_official 610:813dcc80987e 121 HAL_StatusTypeDef HAL_CRC_Init(CRC_HandleTypeDef *hcrc)
mbed_official 610:813dcc80987e 122 {
mbed_official 610:813dcc80987e 123 /* Check the CRC handle allocation */
mbed_official 610:813dcc80987e 124 if(hcrc == NULL)
mbed_official 610:813dcc80987e 125 {
mbed_official 610:813dcc80987e 126 return HAL_ERROR;
mbed_official 610:813dcc80987e 127 }
mbed_official 610:813dcc80987e 128
mbed_official 610:813dcc80987e 129 /* Check the parameters */
mbed_official 610:813dcc80987e 130 assert_param(IS_CRC_ALL_INSTANCE(hcrc->Instance));
mbed_official 610:813dcc80987e 131
mbed_official 610:813dcc80987e 132 if(hcrc->State == HAL_CRC_STATE_RESET)
mbed_official 610:813dcc80987e 133 {
mbed_official 610:813dcc80987e 134 /* Allocate lock resource and initialize it */
mbed_official 610:813dcc80987e 135 hcrc->Lock = HAL_UNLOCKED;
mbed_official 610:813dcc80987e 136
mbed_official 610:813dcc80987e 137 /* Init the low level hardware */
mbed_official 610:813dcc80987e 138 HAL_CRC_MspInit(hcrc);
mbed_official 610:813dcc80987e 139 }
mbed_official 610:813dcc80987e 140
mbed_official 610:813dcc80987e 141 hcrc->State = HAL_CRC_STATE_BUSY;
mbed_official 610:813dcc80987e 142
mbed_official 610:813dcc80987e 143 /* check whether or not non-default generating polynomial has been
mbed_official 610:813dcc80987e 144 * picked up by user */
mbed_official 610:813dcc80987e 145 assert_param(IS_DEFAULT_POLYNOMIAL(hcrc->Init.DefaultPolynomialUse));
mbed_official 610:813dcc80987e 146 if (hcrc->Init.DefaultPolynomialUse == DEFAULT_POLYNOMIAL_ENABLE)
mbed_official 610:813dcc80987e 147 {
mbed_official 610:813dcc80987e 148 /* initialize IP with default generating polynomial */
mbed_official 610:813dcc80987e 149 WRITE_REG(hcrc->Instance->POL, DEFAULT_CRC32_POLY);
mbed_official 610:813dcc80987e 150 MODIFY_REG(hcrc->Instance->CR, CRC_CR_POLYSIZE, CRC_POLYLENGTH_32B);
mbed_official 610:813dcc80987e 151 }
mbed_official 610:813dcc80987e 152 else
mbed_official 610:813dcc80987e 153 {
mbed_official 610:813dcc80987e 154 /* initialize CRC IP with generating polynomial defined by user */
mbed_official 610:813dcc80987e 155 if (HAL_CRCEx_Polynomial_Set(hcrc, hcrc->Init.GeneratingPolynomial, hcrc->Init.CRCLength) != HAL_OK)
mbed_official 610:813dcc80987e 156 {
mbed_official 610:813dcc80987e 157 return HAL_ERROR;
mbed_official 610:813dcc80987e 158 }
mbed_official 610:813dcc80987e 159 }
mbed_official 610:813dcc80987e 160
mbed_official 610:813dcc80987e 161 /* check whether or not non-default CRC initial value has been
mbed_official 610:813dcc80987e 162 * picked up by user */
mbed_official 610:813dcc80987e 163 assert_param(IS_DEFAULT_INIT_VALUE(hcrc->Init.DefaultInitValueUse));
mbed_official 610:813dcc80987e 164 if (hcrc->Init.DefaultInitValueUse == DEFAULT_INIT_VALUE_ENABLE)
mbed_official 610:813dcc80987e 165 {
mbed_official 610:813dcc80987e 166 WRITE_REG(hcrc->Instance->INIT, DEFAULT_CRC_INITVALUE);
mbed_official 610:813dcc80987e 167 }
mbed_official 610:813dcc80987e 168 else
mbed_official 610:813dcc80987e 169 {
mbed_official 610:813dcc80987e 170 WRITE_REG(hcrc->Instance->INIT, hcrc->Init.InitValue);
mbed_official 610:813dcc80987e 171 }
mbed_official 610:813dcc80987e 172
mbed_official 610:813dcc80987e 173
mbed_official 610:813dcc80987e 174 /* set input data inversion mode */
mbed_official 610:813dcc80987e 175 assert_param(IS_CRC_INPUTDATA_INVERSION_MODE(hcrc->Init.InputDataInversionMode));
mbed_official 610:813dcc80987e 176 MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_IN, hcrc->Init.InputDataInversionMode);
mbed_official 610:813dcc80987e 177
mbed_official 610:813dcc80987e 178 /* set output data inversion mode */
mbed_official 610:813dcc80987e 179 assert_param(IS_CRC_OUTPUTDATA_INVERSION_MODE(hcrc->Init.OutputDataInversionMode));
mbed_official 610:813dcc80987e 180 MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_OUT, hcrc->Init.OutputDataInversionMode);
mbed_official 610:813dcc80987e 181
mbed_official 610:813dcc80987e 182 /* makes sure the input data format (bytes, halfwords or words stream)
mbed_official 610:813dcc80987e 183 * is properly specified by user */
mbed_official 610:813dcc80987e 184 assert_param(IS_CRC_INPUTDATA_FORMAT(hcrc->InputDataFormat));
mbed_official 610:813dcc80987e 185
mbed_official 610:813dcc80987e 186 /* Change CRC peripheral state */
mbed_official 610:813dcc80987e 187 hcrc->State = HAL_CRC_STATE_READY;
mbed_official 610:813dcc80987e 188
mbed_official 610:813dcc80987e 189 /* Return function status */
mbed_official 610:813dcc80987e 190 return HAL_OK;
mbed_official 610:813dcc80987e 191 }
mbed_official 610:813dcc80987e 192
mbed_official 610:813dcc80987e 193 /**
mbed_official 610:813dcc80987e 194 * @brief DeInitialize the CRC peripheral.
mbed_official 610:813dcc80987e 195 * @param hcrc: CRC handle
mbed_official 610:813dcc80987e 196 * @retval HAL status
mbed_official 610:813dcc80987e 197 */
mbed_official 610:813dcc80987e 198 HAL_StatusTypeDef HAL_CRC_DeInit(CRC_HandleTypeDef *hcrc)
mbed_official 610:813dcc80987e 199 {
mbed_official 610:813dcc80987e 200 /* Check the CRC handle allocation */
mbed_official 610:813dcc80987e 201 if(hcrc == NULL)
mbed_official 610:813dcc80987e 202 {
mbed_official 610:813dcc80987e 203 return HAL_ERROR;
mbed_official 610:813dcc80987e 204 }
mbed_official 610:813dcc80987e 205
mbed_official 610:813dcc80987e 206 /* Check the parameters */
mbed_official 610:813dcc80987e 207 assert_param(IS_CRC_ALL_INSTANCE(hcrc->Instance));
mbed_official 610:813dcc80987e 208
mbed_official 610:813dcc80987e 209 /* Check the CRC peripheral state */
mbed_official 610:813dcc80987e 210 if(hcrc->State == HAL_CRC_STATE_BUSY)
mbed_official 610:813dcc80987e 211 {
mbed_official 610:813dcc80987e 212 return HAL_BUSY;
mbed_official 610:813dcc80987e 213 }
mbed_official 610:813dcc80987e 214
mbed_official 610:813dcc80987e 215 /* Change CRC peripheral state */
mbed_official 610:813dcc80987e 216 hcrc->State = HAL_CRC_STATE_BUSY;
mbed_official 610:813dcc80987e 217
mbed_official 610:813dcc80987e 218 /* Reset CRC calculation unit */
mbed_official 610:813dcc80987e 219 __HAL_CRC_DR_RESET(hcrc);
mbed_official 610:813dcc80987e 220
mbed_official 610:813dcc80987e 221 /* DeInit the low level hardware */
mbed_official 610:813dcc80987e 222 HAL_CRC_MspDeInit(hcrc);
mbed_official 610:813dcc80987e 223
mbed_official 610:813dcc80987e 224 /* Change CRC peripheral state */
mbed_official 610:813dcc80987e 225 hcrc->State = HAL_CRC_STATE_RESET;
mbed_official 610:813dcc80987e 226
mbed_official 610:813dcc80987e 227 /* Process unlocked */
mbed_official 610:813dcc80987e 228 __HAL_UNLOCK(hcrc);
mbed_official 610:813dcc80987e 229
mbed_official 610:813dcc80987e 230 /* Return function status */
mbed_official 610:813dcc80987e 231 return HAL_OK;
mbed_official 610:813dcc80987e 232 }
mbed_official 610:813dcc80987e 233
mbed_official 610:813dcc80987e 234 /**
mbed_official 610:813dcc80987e 235 * @brief Initializes the CRC MSP.
mbed_official 610:813dcc80987e 236 * @param hcrc: CRC handle
mbed_official 610:813dcc80987e 237 * @retval None
mbed_official 610:813dcc80987e 238 */
mbed_official 610:813dcc80987e 239 __weak void HAL_CRC_MspInit(CRC_HandleTypeDef *hcrc)
mbed_official 610:813dcc80987e 240 {
mbed_official 610:813dcc80987e 241 /* NOTE : This function should not be modified, when the callback is needed,
mbed_official 610:813dcc80987e 242 the HAL_CRC_MspInit can be implemented in the user file
mbed_official 610:813dcc80987e 243 */
mbed_official 610:813dcc80987e 244 }
mbed_official 610:813dcc80987e 245
mbed_official 610:813dcc80987e 246 /**
mbed_official 610:813dcc80987e 247 * @brief DeInitialize the CRC MSP.
mbed_official 610:813dcc80987e 248 * @param hcrc: CRC handle
mbed_official 610:813dcc80987e 249 * @retval None
mbed_official 610:813dcc80987e 250 */
mbed_official 610:813dcc80987e 251 __weak void HAL_CRC_MspDeInit(CRC_HandleTypeDef *hcrc)
mbed_official 610:813dcc80987e 252 {
mbed_official 610:813dcc80987e 253 /* NOTE : This function should not be modified, when the callback is needed,
mbed_official 610:813dcc80987e 254 the HAL_CRC_MspDeInit can be implemented in the user file
mbed_official 610:813dcc80987e 255 */
mbed_official 610:813dcc80987e 256 }
mbed_official 610:813dcc80987e 257
mbed_official 610:813dcc80987e 258 /**
mbed_official 610:813dcc80987e 259 * @}
mbed_official 610:813dcc80987e 260 */
mbed_official 610:813dcc80987e 261
mbed_official 610:813dcc80987e 262 /** @defgroup CRC_Exported_Functions_Group2 Peripheral Control functions
mbed_official 610:813dcc80987e 263 * @brief management functions.
mbed_official 610:813dcc80987e 264 *
mbed_official 610:813dcc80987e 265 @verbatim
mbed_official 610:813dcc80987e 266 ===============================================================================
mbed_official 610:813dcc80987e 267 ##### Peripheral Control functions #####
mbed_official 610:813dcc80987e 268 ===============================================================================
mbed_official 610:813dcc80987e 269 [..] This section provides functions allowing to:
mbed_official 610:813dcc80987e 270 (+) compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
mbed_official 610:813dcc80987e 271 using the combination of the previous CRC value and the new one
mbed_official 610:813dcc80987e 272
mbed_official 610:813dcc80987e 273 [..] or
mbed_official 610:813dcc80987e 274
mbed_official 610:813dcc80987e 275 (+) compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
mbed_official 610:813dcc80987e 276 independently of the previous CRC value.
mbed_official 610:813dcc80987e 277
mbed_official 610:813dcc80987e 278 @endverbatim
mbed_official 610:813dcc80987e 279 * @{
mbed_official 610:813dcc80987e 280 */
mbed_official 610:813dcc80987e 281
mbed_official 610:813dcc80987e 282 /**
mbed_official 610:813dcc80987e 283 * @brief Compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
mbed_official 610:813dcc80987e 284 * starting with the previously computed CRC as initialization value.
mbed_official 610:813dcc80987e 285 * @param hcrc: CRC handle
mbed_official 610:813dcc80987e 286 * @param pBuffer: pointer to the input data buffer, exact input data format is
mbed_official 610:813dcc80987e 287 * provided by hcrc->InputDataFormat.
mbed_official 610:813dcc80987e 288 * @param BufferLength: input data buffer length
mbed_official 610:813dcc80987e 289 * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
mbed_official 610:813dcc80987e 290 */
mbed_official 610:813dcc80987e 291 uint32_t HAL_CRC_Accumulate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
mbed_official 610:813dcc80987e 292 {
mbed_official 610:813dcc80987e 293 uint32_t index = 0; /* CRC input data buffer index */
mbed_official 610:813dcc80987e 294 uint32_t temp = 0; /* CRC output (read from hcrc->Instance->DR register) */
mbed_official 610:813dcc80987e 295
mbed_official 610:813dcc80987e 296 /* Process locked */
mbed_official 610:813dcc80987e 297 __HAL_LOCK(hcrc);
mbed_official 610:813dcc80987e 298
mbed_official 610:813dcc80987e 299 /* Change CRC peripheral state */
mbed_official 610:813dcc80987e 300 hcrc->State = HAL_CRC_STATE_BUSY;
mbed_official 610:813dcc80987e 301
mbed_official 610:813dcc80987e 302 switch (hcrc->InputDataFormat)
mbed_official 610:813dcc80987e 303 {
mbed_official 610:813dcc80987e 304 case CRC_INPUTDATA_FORMAT_WORDS:
mbed_official 610:813dcc80987e 305 /* Enter Data to the CRC calculator */
mbed_official 610:813dcc80987e 306 for(index = 0; index < BufferLength; index++)
mbed_official 610:813dcc80987e 307 {
mbed_official 610:813dcc80987e 308 hcrc->Instance->DR = pBuffer[index];
mbed_official 610:813dcc80987e 309 }
mbed_official 610:813dcc80987e 310 temp = hcrc->Instance->DR;
mbed_official 610:813dcc80987e 311 break;
mbed_official 610:813dcc80987e 312
mbed_official 610:813dcc80987e 313 case CRC_INPUTDATA_FORMAT_BYTES:
mbed_official 610:813dcc80987e 314 temp = CRC_Handle_8(hcrc, (uint8_t*)pBuffer, BufferLength);
mbed_official 610:813dcc80987e 315 break;
mbed_official 610:813dcc80987e 316
mbed_official 610:813dcc80987e 317 case CRC_INPUTDATA_FORMAT_HALFWORDS:
mbed_official 610:813dcc80987e 318 temp = CRC_Handle_16(hcrc, (uint16_t*)pBuffer, BufferLength);
mbed_official 610:813dcc80987e 319 break;
mbed_official 610:813dcc80987e 320
mbed_official 610:813dcc80987e 321 default:
mbed_official 610:813dcc80987e 322 break;
mbed_official 610:813dcc80987e 323 }
mbed_official 610:813dcc80987e 324
mbed_official 610:813dcc80987e 325 /* Change CRC peripheral state */
mbed_official 610:813dcc80987e 326 hcrc->State = HAL_CRC_STATE_READY;
mbed_official 610:813dcc80987e 327
mbed_official 610:813dcc80987e 328 /* Process unlocked */
mbed_official 610:813dcc80987e 329 __HAL_UNLOCK(hcrc);
mbed_official 610:813dcc80987e 330
mbed_official 610:813dcc80987e 331 /* Return the CRC computed value */
mbed_official 610:813dcc80987e 332 return temp;
mbed_official 610:813dcc80987e 333 }
mbed_official 610:813dcc80987e 334
mbed_official 610:813dcc80987e 335
mbed_official 610:813dcc80987e 336 /**
mbed_official 610:813dcc80987e 337 * @brief Compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
mbed_official 610:813dcc80987e 338 * starting with hcrc->Instance->INIT as initialization value.
mbed_official 610:813dcc80987e 339 * @param hcrc: CRC handle
mbed_official 610:813dcc80987e 340 * @param pBuffer: pointer to the input data buffer, exact input data format is
mbed_official 610:813dcc80987e 341 * provided by hcrc->InputDataFormat.
mbed_official 610:813dcc80987e 342 * @param BufferLength: input data buffer length
mbed_official 610:813dcc80987e 343 * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
mbed_official 610:813dcc80987e 344 */
mbed_official 610:813dcc80987e 345 uint32_t HAL_CRC_Calculate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
mbed_official 610:813dcc80987e 346 {
mbed_official 610:813dcc80987e 347 uint32_t index = 0; /* CRC input data buffer index */
mbed_official 610:813dcc80987e 348 uint32_t temp = 0; /* CRC output (read from hcrc->Instance->DR register) */
mbed_official 610:813dcc80987e 349
mbed_official 610:813dcc80987e 350 /* Process locked */
mbed_official 610:813dcc80987e 351 __HAL_LOCK(hcrc);
mbed_official 610:813dcc80987e 352
mbed_official 610:813dcc80987e 353 /* Change CRC peripheral state */
mbed_official 610:813dcc80987e 354 hcrc->State = HAL_CRC_STATE_BUSY;
mbed_official 610:813dcc80987e 355
mbed_official 610:813dcc80987e 356 /* Reset CRC Calculation Unit (hcrc->Instance->INIT is
mbed_official 610:813dcc80987e 357 * written in hcrc->Instance->DR) */
mbed_official 610:813dcc80987e 358 __HAL_CRC_DR_RESET(hcrc);
mbed_official 610:813dcc80987e 359
mbed_official 610:813dcc80987e 360 switch (hcrc->InputDataFormat)
mbed_official 610:813dcc80987e 361 {
mbed_official 610:813dcc80987e 362 case CRC_INPUTDATA_FORMAT_WORDS:
mbed_official 610:813dcc80987e 363 /* Enter 32-bit input data to the CRC calculator */
mbed_official 610:813dcc80987e 364 for(index = 0; index < BufferLength; index++)
mbed_official 610:813dcc80987e 365 {
mbed_official 610:813dcc80987e 366 hcrc->Instance->DR = pBuffer[index];
mbed_official 610:813dcc80987e 367 }
mbed_official 610:813dcc80987e 368 temp = hcrc->Instance->DR;
mbed_official 610:813dcc80987e 369 break;
mbed_official 610:813dcc80987e 370
mbed_official 610:813dcc80987e 371 case CRC_INPUTDATA_FORMAT_BYTES:
mbed_official 610:813dcc80987e 372 /* Specific 8-bit input data handling */
mbed_official 610:813dcc80987e 373 temp = CRC_Handle_8(hcrc, (uint8_t*)pBuffer, BufferLength);
mbed_official 610:813dcc80987e 374 break;
mbed_official 610:813dcc80987e 375
mbed_official 610:813dcc80987e 376 case CRC_INPUTDATA_FORMAT_HALFWORDS:
mbed_official 610:813dcc80987e 377 /* Specific 16-bit input data handling */
mbed_official 610:813dcc80987e 378 temp = CRC_Handle_16(hcrc, (uint16_t*)pBuffer, BufferLength);
mbed_official 610:813dcc80987e 379 break;
mbed_official 610:813dcc80987e 380
mbed_official 610:813dcc80987e 381 default:
mbed_official 610:813dcc80987e 382 break;
mbed_official 610:813dcc80987e 383 }
mbed_official 610:813dcc80987e 384
mbed_official 610:813dcc80987e 385 /* Change CRC peripheral state */
mbed_official 610:813dcc80987e 386 hcrc->State = HAL_CRC_STATE_READY;
mbed_official 610:813dcc80987e 387
mbed_official 610:813dcc80987e 388 /* Process unlocked */
mbed_official 610:813dcc80987e 389 __HAL_UNLOCK(hcrc);
mbed_official 610:813dcc80987e 390
mbed_official 610:813dcc80987e 391 /* Return the CRC computed value */
mbed_official 610:813dcc80987e 392 return temp;
mbed_official 610:813dcc80987e 393 }
mbed_official 610:813dcc80987e 394
mbed_official 610:813dcc80987e 395 /**
mbed_official 610:813dcc80987e 396 * @}
mbed_official 610:813dcc80987e 397 */
mbed_official 610:813dcc80987e 398
mbed_official 610:813dcc80987e 399 /** @defgroup CRC_Exported_Functions_Group3 Peripheral State functions
mbed_official 610:813dcc80987e 400 * @brief Peripheral State functions.
mbed_official 610:813dcc80987e 401 *
mbed_official 610:813dcc80987e 402 @verbatim
mbed_official 610:813dcc80987e 403 ===============================================================================
mbed_official 610:813dcc80987e 404 ##### Peripheral State functions #####
mbed_official 610:813dcc80987e 405 ===============================================================================
mbed_official 610:813dcc80987e 406 [..]
mbed_official 610:813dcc80987e 407 This subsection permits to get in run-time the status of the peripheral.
mbed_official 610:813dcc80987e 408
mbed_official 610:813dcc80987e 409 @endverbatim
mbed_official 610:813dcc80987e 410 * @{
mbed_official 610:813dcc80987e 411 */
mbed_official 610:813dcc80987e 412
mbed_official 610:813dcc80987e 413 /**
mbed_official 610:813dcc80987e 414 * @brief Returns the CRC handle state.
mbed_official 610:813dcc80987e 415 * @param hcrc: CRC handle
mbed_official 610:813dcc80987e 416 * @retval HAL state
mbed_official 610:813dcc80987e 417 */
mbed_official 610:813dcc80987e 418 HAL_CRC_StateTypeDef HAL_CRC_GetState(CRC_HandleTypeDef *hcrc)
mbed_official 610:813dcc80987e 419 {
mbed_official 610:813dcc80987e 420 /* Return CRC handle state */
mbed_official 610:813dcc80987e 421 return hcrc->State;
mbed_official 610:813dcc80987e 422 }
mbed_official 610:813dcc80987e 423
mbed_official 610:813dcc80987e 424 /**
mbed_official 610:813dcc80987e 425 * @}
mbed_official 610:813dcc80987e 426 */
mbed_official 610:813dcc80987e 427
mbed_official 610:813dcc80987e 428 /**
mbed_official 610:813dcc80987e 429 * @}
mbed_official 610:813dcc80987e 430 */
mbed_official 610:813dcc80987e 431
mbed_official 610:813dcc80987e 432 /** @defgroup CRC_Private_Functions CRC Private Functions
mbed_official 610:813dcc80987e 433 * @{
mbed_official 610:813dcc80987e 434 */
mbed_official 610:813dcc80987e 435
mbed_official 610:813dcc80987e 436 /**
mbed_official 610:813dcc80987e 437 * @brief Enter 8-bit input data to the CRC calculator.
mbed_official 610:813dcc80987e 438 * Specific data handling to optimize processing time.
mbed_official 610:813dcc80987e 439 * @param hcrc: CRC handle
mbed_official 610:813dcc80987e 440 * @param pBuffer: pointer to the input data buffer
mbed_official 610:813dcc80987e 441 * @param BufferLength: input data buffer length
mbed_official 610:813dcc80987e 442 * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
mbed_official 610:813dcc80987e 443 */
mbed_official 610:813dcc80987e 444 static uint32_t CRC_Handle_8(CRC_HandleTypeDef *hcrc, uint8_t pBuffer[], uint32_t BufferLength)
mbed_official 610:813dcc80987e 445 {
mbed_official 610:813dcc80987e 446 uint32_t i = 0; /* input data buffer index */
mbed_official 610:813dcc80987e 447
mbed_official 610:813dcc80987e 448 /* Processing time optimization: 4 bytes are entered in a row with a single word write,
mbed_official 610:813dcc80987e 449 * last bytes must be carefully fed to the CRC calculator to ensure a correct type
mbed_official 610:813dcc80987e 450 * handling by the IP */
mbed_official 610:813dcc80987e 451 for(i = 0; i < (BufferLength/4); i++)
mbed_official 610:813dcc80987e 452 {
mbed_official 610:813dcc80987e 453 hcrc->Instance->DR = ((uint32_t)pBuffer[4*i]<<24) | ((uint32_t)pBuffer[4*i+1]<<16) | ((uint32_t)pBuffer[4*i+2]<<8) | (uint32_t)pBuffer[4*i+3];
mbed_official 610:813dcc80987e 454 }
mbed_official 610:813dcc80987e 455 /* last bytes specific handling */
mbed_official 610:813dcc80987e 456 if ((BufferLength%4) != 0)
mbed_official 610:813dcc80987e 457 {
mbed_official 610:813dcc80987e 458 if (BufferLength%4 == 1)
mbed_official 610:813dcc80987e 459 {
mbed_official 610:813dcc80987e 460 *(uint8_t volatile*) (&hcrc->Instance->DR) = pBuffer[4*i];
mbed_official 610:813dcc80987e 461 }
mbed_official 610:813dcc80987e 462 if (BufferLength%4 == 2)
mbed_official 610:813dcc80987e 463 {
mbed_official 610:813dcc80987e 464 *(uint16_t volatile*) (&hcrc->Instance->DR) = ((uint32_t)pBuffer[4*i]<<8) | (uint32_t)pBuffer[4*i+1];
mbed_official 610:813dcc80987e 465 }
mbed_official 610:813dcc80987e 466 if (BufferLength%4 == 3)
mbed_official 610:813dcc80987e 467 {
mbed_official 610:813dcc80987e 468 *(uint16_t volatile*) (&hcrc->Instance->DR) = ((uint32_t)pBuffer[4*i]<<8) | (uint32_t)pBuffer[4*i+1];
mbed_official 610:813dcc80987e 469 *(uint8_t volatile*) (&hcrc->Instance->DR) = pBuffer[4*i+2];
mbed_official 610:813dcc80987e 470 }
mbed_official 610:813dcc80987e 471 }
mbed_official 610:813dcc80987e 472
mbed_official 610:813dcc80987e 473 /* Return the CRC computed value */
mbed_official 610:813dcc80987e 474 return hcrc->Instance->DR;
mbed_official 610:813dcc80987e 475 }
mbed_official 610:813dcc80987e 476
mbed_official 610:813dcc80987e 477
mbed_official 610:813dcc80987e 478
mbed_official 610:813dcc80987e 479 /**
mbed_official 610:813dcc80987e 480 * @brief Enter 16-bit input data to the CRC calculator.
mbed_official 610:813dcc80987e 481 * Specific data handling to optimize processing time.
mbed_official 610:813dcc80987e 482 * @param hcrc: CRC handle
mbed_official 610:813dcc80987e 483 * @param pBuffer: pointer to the input data buffer
mbed_official 610:813dcc80987e 484 * @param BufferLength: input data buffer length
mbed_official 610:813dcc80987e 485 * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
mbed_official 610:813dcc80987e 486 */
mbed_official 610:813dcc80987e 487 static uint32_t CRC_Handle_16(CRC_HandleTypeDef *hcrc, uint16_t pBuffer[], uint32_t BufferLength)
mbed_official 610:813dcc80987e 488 {
mbed_official 610:813dcc80987e 489 uint32_t i = 0; /* input data buffer index */
mbed_official 610:813dcc80987e 490
mbed_official 610:813dcc80987e 491 /* Processing time optimization: 2 HalfWords are entered in a row with a single word write,
mbed_official 610:813dcc80987e 492 * in case of odd length, last HalfWord must be carefully fed to the CRC calculator to ensure
mbed_official 610:813dcc80987e 493 * a correct type handling by the IP */
mbed_official 610:813dcc80987e 494 for(i = 0; i < (BufferLength/2); i++)
mbed_official 610:813dcc80987e 495 {
mbed_official 610:813dcc80987e 496 hcrc->Instance->DR = ((uint32_t)pBuffer[2*i]<<16) | (uint32_t)pBuffer[2*i+1];
mbed_official 610:813dcc80987e 497 }
mbed_official 610:813dcc80987e 498 if ((BufferLength%2) != 0)
mbed_official 610:813dcc80987e 499 {
mbed_official 610:813dcc80987e 500 *(uint16_t volatile*) (&hcrc->Instance->DR) = pBuffer[2*i];
mbed_official 610:813dcc80987e 501 }
mbed_official 610:813dcc80987e 502
mbed_official 610:813dcc80987e 503 /* Return the CRC computed value */
mbed_official 610:813dcc80987e 504 return hcrc->Instance->DR;
mbed_official 610:813dcc80987e 505 }
mbed_official 610:813dcc80987e 506
mbed_official 610:813dcc80987e 507 /**
mbed_official 610:813dcc80987e 508 * @}
mbed_official 610:813dcc80987e 509 */
mbed_official 610:813dcc80987e 510
mbed_official 610:813dcc80987e 511 #endif /* HAL_CRC_MODULE_ENABLED */
mbed_official 610:813dcc80987e 512 /**
mbed_official 610:813dcc80987e 513 * @}
mbed_official 610:813dcc80987e 514 */
mbed_official 610:813dcc80987e 515
mbed_official 610:813dcc80987e 516 /**
mbed_official 610:813dcc80987e 517 * @}
mbed_official 610:813dcc80987e 518 */
mbed_official 610:813dcc80987e 519
mbed_official 610:813dcc80987e 520 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/