mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Wed Jul 01 09:45:11 2015 +0100
Revision:
579:53297373a894
Parent:
489:119543c9f674
Synchronized with git revision d5b4d2ab9c47edb4dc5776e7177b0c2263459081

Full URL: https://github.com/mbedmicro/mbed/commit/d5b4d2ab9c47edb4dc5776e7177b0c2263459081/

Initial version of drivers for SAMR21

Who changed what in which revision?

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