STM32H735xx HAL User Manual
stm32h7xx_hal_otfdec.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32h7xx_hal_otfdec.c
00004   * @author  MCD Application Team
00005   * @brief   OTFDEC HAL module driver.
00006   *          This file provides firmware functions to manage the following
00007   *          functionalities of the On-The-Fly Decryption (OTFDEC) peripheral:
00008   *           + Initialization and de-initialization functions
00009   *           + Region setting/enable functions
00010   *           + Peripheral State functions
00011   *
00012   ******************************************************************************
00013   * @attention
00014   *
00015   * Copyright (c) 2018 STMicroelectronics.
00016   * All rights reserved.
00017   *
00018   * This software is licensed under terms that can be found in the LICENSE file
00019   * in the root directory of this software component.
00020   * If no LICENSE file comes with this software, it is provided AS-IS.
00021   *
00022   ******************************************************************************
00023   @verbatim
00024   ==============================================================================
00025                      ##### How to use this driver #####
00026   ==============================================================================
00027  [..]
00028     The OTFDEC HAL driver can be used as follows:
00029 
00030     (#) Declare an OTFDEC_HandleTypeDef handle structure (eg. OTFDEC_HandleTypeDef hotfdec).
00031 
00032     (#) Initialize the OTFDEC low level resources by implementing the HAL_OTFDEC_MspInit() API:
00033         (++) Enable the OTFDEC interface clock.
00034         (++) NVIC configuration if interrupts are used
00035             (+++) Configure the OTFDEC interrupt priority.
00036             (+++) Enable the NVIC OTFDEC IRQ handle.
00037 
00038     (#) Initialize the OTFDEC peripheral by calling the HAL_OTFDEC_Init() API.
00039 
00040     (#) For each region,
00041 
00042         (++) Configure the region deciphering mode by calling the HAL_OTFDEC_RegionSetMode() API.
00043 
00044         (++) Write the region Key by calling the HAL_OTFDEC_RegionSetKey() API. If desired,
00045         read the key CRC by calling HAL_OTFDEC_RegionGetKeyCRC() API and compare the
00046         result with the theoretically expected CRC.
00047 
00048         (++) Initialize the OTFDEC region config structure with the Nonce, protected
00049         region start and end addresses and firmware version, and wrap-up the region
00050         configuration by calling HAL_OTFDEC_RegionConfig() API.
00051 
00052     (#) At this point, the OTFDEC region configuration is done and the deciphering
00053         is enabled. The region can be deciphered on the fly after having made sure
00054         the OctoSPI is configured in memory-mapped mode.
00055 
00056     [..]
00057     (@) Warning: the OTFDEC deciphering is based on a different endianness compared
00058         to the AES-CTR as implemented in the AES peripheral. E.g., if the OTFEC
00059         resorts to the Key (B0, B1, B2, B3) where Bi are 32-bit longwords and B0
00060         is the Least Significant Word, the AES has to be configured with the Key
00061         (B3, B2, B1, B0) to report the same result (with the same swapping applied
00062         to the Initialization Vector).
00063 
00064     [..]
00065 
00066     *** Callback registration ***
00067     =============================================
00068     [..]
00069 
00070      The compilation flag USE_HAL_OTFDEC_REGISTER_CALLBACKS, when set to 1,
00071      allows the user to configure dynamically the driver callbacks.
00072      Use Functions HAL_OTFDEC_RegisterCallback()
00073      to register an interrupt callback.
00074     [..]
00075 
00076      Function HAL_OTFDEC_RegisterCallback() allows to register following callbacks:
00077        (+) ErrorCallback                  : OTFDEC error callback
00078        (+) MspInitCallback                : OTFDEC Msp Init callback
00079        (+) MspDeInitCallback              : OTFDEC Msp DeInit callback
00080      This function takes as parameters the HAL peripheral handle, the Callback ID
00081      and a pointer to the user callback function.
00082     [..]
00083 
00084      Use function HAL_OTFDEC_UnRegisterCallback to reset a callback to the default
00085      weak function.
00086     [..]
00087 
00088      HAL_OTFDEC_UnRegisterCallback takes as parameters the HAL peripheral handle,
00089      and the Callback ID.
00090      This function allows to reset following callbacks:
00091        (+) ErrorCallback                  : OTFDEC error callback
00092        (+) MspInitCallback                : OTFDEC Msp Init callback
00093        (+) MspDeInitCallback              : OTFDEC Msp DeInit callback
00094      [..]
00095 
00096      By default, after the HAL_OTFDEC_Init() and when the state is HAL_OTFDEC_STATE_RESET
00097      all callbacks are set to the corresponding weak functions:
00098      example HAL_OTFDEC_ErrorCallback().
00099      Exception done for MspInit and MspDeInit functions that are
00100      reset to the legacy weak functions in the HAL_OTFDEC_Init()/ HAL_OTFDEC_DeInit() only when
00101      these callbacks are null (not registered beforehand).
00102     [..]
00103 
00104      If MspInit or MspDeInit are not null, the HAL_OTFDEC_Init()/ HAL_OTFDEC_DeInit()
00105      keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
00106      [..]
00107 
00108      Callbacks can be registered/unregistered in HAL_OTFDEC_STATE_READY state only.
00109      Exception done MspInit/MspDeInit functions that can be registered/unregistered
00110      in HAL_OTFDEC_STATE_READY or HAL_OTFDEC_STATE_RESET state,
00111      thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
00112     [..]
00113 
00114      Then, the user first registers the MspInit/MspDeInit user callbacks
00115      using HAL_OTFDEC_RegisterCallback() before calling HAL_OTFDEC_DeInit()
00116      or HAL_OTFDEC_Init() function.
00117      [..]
00118 
00119      When the compilation flag USE_HAL_OTFDEC_REGISTER_CALLBACKS is set to 0 or
00120      not defined, the callback registration feature is not available and all callbacks
00121      are set to the corresponding weak functions.
00122 
00123   @endverbatim
00124   */
00125 
00126 /* Includes ------------------------------------------------------------------*/
00127 #include "stm32h7xx_hal.h"
00128 
00129 /** @addtogroup STM32H7xx_HAL_Driver
00130   * @{
00131   */
00132 
00133 /** @defgroup OTFDEC OTFDEC
00134   * @brief OTFDEC HAL module driver.
00135   * @{
00136   */
00137 
00138 
00139 #ifdef HAL_OTFDEC_MODULE_ENABLED
00140 
00141 #if defined(OTFDEC1)
00142 
00143 /* Private typedef -----------------------------------------------------------*/
00144 /* Private define ------------------------------------------------------------*/
00145 /* Private macro -------------------------------------------------------------*/
00146 /* Private variables ---------------------------------------------------------*/
00147 /* Private function prototypes -----------------------------------------------*/
00148 /* Private functions ---------------------------------------------------------*/
00149 
00150 
00151 
00152  /* Exported functions --------------------------------------------------------*/
00153 /** @addtogroup OTFDEC_Exported_Functions
00154   * @{
00155   */
00156 
00157 /** @defgroup OTFDEC_Exported_Functions_Group1 Initialization and de-initialization functions
00158  *  @brief    Initialization and Configuration functions.
00159  *
00160 @verbatim
00161   ==============================================================================
00162               ##### Initialization and de-initialization functions #####
00163   ==============================================================================
00164 
00165 @endverbatim
00166   * @{
00167   */
00168 
00169 /**
00170   * @brief  Initialize the OTFDEC peripheral and create the associated handle.
00171   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
00172   *         the configuration information for OTFDEC module
00173   * @retval HAL status
00174   */
00175 HAL_StatusTypeDef HAL_OTFDEC_Init(OTFDEC_HandleTypeDef *hotfdec)
00176 {
00177   /* Check the OTFDEC handle allocation */
00178   if(hotfdec == NULL)
00179   {
00180     return HAL_ERROR;
00181   }
00182 
00183   /* Check the parameters */
00184   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
00185 
00186   if(hotfdec->State == HAL_OTFDEC_STATE_RESET)
00187   {
00188     /* Allocate lock resource and initialize it */
00189     __HAL_UNLOCK(hotfdec);
00190 
00191 #if (USE_HAL_OTFDEC_REGISTER_CALLBACKS == 1)
00192     /* Init the OTFDEC Callback settings */
00193     hotfdec->ErrorCallback = HAL_OTFDEC_ErrorCallback; /* Legacy weak callback */
00194 
00195     if (hotfdec->MspInitCallback == NULL)
00196     {
00197       hotfdec->MspInitCallback = HAL_OTFDEC_MspInit; /* Legacy weak MspInit */
00198     }
00199 
00200     /* Init the low level hardware */
00201     hotfdec->MspInitCallback(hotfdec);
00202 #else
00203     /* Init the low level hardware */
00204     HAL_OTFDEC_MspInit(hotfdec);
00205 #endif /* USE_HAL_OTFDEC_REGISTER_CALLBACKS */
00206   }
00207 
00208   /* Change the OTFDEC state */
00209   hotfdec->State = HAL_OTFDEC_STATE_READY;
00210 
00211   /* Return function status */
00212   return HAL_OK;
00213 }
00214 
00215 /**
00216   * @brief  DeInitialize the OTFDEC peripheral.
00217   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
00218   *         the configuration information for OTFDEC module
00219   * @retval HAL status
00220   */
00221 HAL_StatusTypeDef HAL_OTFDEC_DeInit(OTFDEC_HandleTypeDef *hotfdec)
00222 {
00223   /* Check the OTFDEC handle allocation */
00224   if(hotfdec == NULL)
00225   {
00226     return HAL_ERROR;
00227   }
00228 
00229   /* Check the parameters */
00230   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
00231 
00232   /* Change the OTFDEC state */
00233   hotfdec->State = HAL_OTFDEC_STATE_BUSY;
00234 
00235 #if (USE_HAL_OTFDEC_REGISTER_CALLBACKS == 1)
00236   if (hotfdec->MspDeInitCallback == NULL)
00237   {
00238     hotfdec->MspDeInitCallback = HAL_OTFDEC_MspDeInit; /* Legacy weak MspDeInit */
00239   }
00240 
00241   /* DeInit the low level hardware: CLOCK, NVIC */
00242   hotfdec->MspDeInitCallback(hotfdec);
00243 #else
00244   /* DeInit the low level hardware: CLOCK, NVIC */
00245   HAL_OTFDEC_MspDeInit(hotfdec);
00246 #endif /* USE_HAL_OTFDEC_REGISTER_CALLBACKS */
00247 
00248   /* Change the OTFDEC state */
00249   hotfdec->State = HAL_OTFDEC_STATE_RESET;
00250 
00251   /* Reset OTFDEC error status */
00252   hotfdec->ErrorCode = HAL_OTFDEC_ERROR_NONE;
00253 
00254   /* Release Lock */
00255   __HAL_UNLOCK(hotfdec);
00256 
00257   /* Return function status */
00258   return HAL_OK;
00259 }
00260 
00261 /**
00262   * @brief  Initialize the OTFDEC MSP.
00263   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
00264   *         the configuration information for OTFDEC module
00265   * @retval None
00266   */
00267 __weak void HAL_OTFDEC_MspInit(OTFDEC_HandleTypeDef *hotfdec)
00268 {
00269   /* Prevent unused argument(s) compilation warning */
00270   UNUSED(hotfdec);
00271 
00272   /* NOTE : This function should not be modified; when the callback is needed,
00273             the HAL_OTFDEC_MspInit can be implemented in the user file.
00274    */
00275 }
00276 
00277 /**
00278   * @brief  DeInitialize OTFDEC MSP.
00279   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
00280   *         the configuration information for OTFDEC module
00281   * @retval None
00282   */
00283 __weak void HAL_OTFDEC_MspDeInit(OTFDEC_HandleTypeDef *hotfdec)
00284 {
00285   /* Prevent unused argument(s) compilation warning */
00286   UNUSED(hotfdec);
00287 
00288   /* NOTE : This function should not be modified; when the callback is needed,
00289             the HAL_OTFDEC_MspDeInit can be implemented in the user file.
00290    */
00291 }
00292 
00293 #if (USE_HAL_OTFDEC_REGISTER_CALLBACKS == 1)
00294 /**
00295   * @brief  Register a User OTFDEC Callback
00296   *         To be used instead of the weak predefined callback
00297   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
00298   *         the configuration information for OTFDEC module
00299   * @param  CallbackID ID of the callback to be registered
00300   *         This parameter can be one of the following values:
00301   *          @arg @ref HAL_OTFDEC_ERROR_CB_ID           OTFDEC error callback ID
00302   *          @arg @ref HAL_OTFDEC_MSPINIT_CB_ID         MspInit callback ID
00303   *          @arg @ref HAL_OTFDEC_MSPDEINIT_CB_ID       MspDeInit callback ID
00304   * @param  pCallback pointer to the Callback function
00305   * @retval HAL status
00306   */
00307 HAL_StatusTypeDef HAL_OTFDEC_RegisterCallback(OTFDEC_HandleTypeDef *hotfdec, HAL_OTFDEC_CallbackIDTypeDef CallbackID, 
00308                                               pOTFDEC_CallbackTypeDef pCallback)
00309 {
00310   HAL_StatusTypeDef status = HAL_OK;
00311 
00312   if (pCallback == NULL)
00313   {
00314     /* Update the error code */
00315     hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
00316 
00317     return HAL_ERROR;
00318   }
00319 
00320   if (hotfdec->State == HAL_OTFDEC_STATE_READY)
00321   {
00322     switch (CallbackID)
00323     {
00324       case HAL_OTFDEC_ERROR_CB_ID :
00325         hotfdec->ErrorCallback = pCallback;
00326         break;
00327 
00328       case HAL_OTFDEC_MSPINIT_CB_ID :
00329         hotfdec->MspInitCallback = pCallback;
00330         break;
00331 
00332       case HAL_OTFDEC_MSPDEINIT_CB_ID :
00333         hotfdec->MspDeInitCallback = pCallback;
00334         break;
00335 
00336       default :
00337         /* Update the error code */
00338         hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
00339 
00340         /* Return error status */
00341         status = HAL_ERROR;
00342         break;
00343     }
00344   }
00345   else if (HAL_OTFDEC_STATE_RESET == hotfdec->State)
00346   {
00347     switch (CallbackID)
00348     {
00349       case HAL_OTFDEC_MSPINIT_CB_ID :
00350         hotfdec->MspInitCallback = pCallback;
00351         break;
00352 
00353       case HAL_OTFDEC_MSPDEINIT_CB_ID :
00354         hotfdec->MspDeInitCallback = pCallback;
00355         break;
00356 
00357       default :
00358         /* Update the error code */
00359         hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
00360 
00361         /* Return error status */
00362         status = HAL_ERROR;
00363         break;
00364     }
00365   }
00366   else
00367   {
00368     /* Update the error code */
00369     hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
00370 
00371     /* Return error status */
00372     status =  HAL_ERROR;
00373   }
00374 
00375   return status;
00376 }
00377 
00378 /**
00379   * @brief  Unregister a OTFDEC Callback
00380   *         OTFDEC callback is redirected to the weak predefined callback
00381   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
00382   *         the configuration information for OTFDEC module
00383   * @param  CallbackID ID of the callback to be registered
00384   *         This parameter can be one of the following values:
00385   *          @arg @ref HAL_OTFDEC_ERROR_CB_ID           OTFDEC error callback ID
00386   *          @arg @ref HAL_OTFDEC_MSPINIT_CB_ID         MspInit callback ID
00387   *          @arg @ref HAL_OTFDEC_MSPDEINIT_CB_ID       MspDeInit callback ID
00388   * @retval HAL status
00389   */
00390 HAL_StatusTypeDef HAL_OTFDEC_UnRegisterCallback(OTFDEC_HandleTypeDef *hotfdec, HAL_OTFDEC_CallbackIDTypeDef CallbackID)
00391 {
00392   HAL_StatusTypeDef status = HAL_OK;
00393 
00394   if (hotfdec->State == HAL_OTFDEC_STATE_READY)
00395   {
00396     switch (CallbackID)
00397     {
00398       case HAL_OTFDEC_ERROR_CB_ID :
00399         hotfdec->ErrorCallback = HAL_OTFDEC_ErrorCallback;
00400         break;
00401 
00402       case HAL_OTFDEC_MSPINIT_CB_ID :
00403         hotfdec->MspInitCallback = HAL_OTFDEC_MspInit; /* Legacy weak MspInit */
00404         break;
00405 
00406       case HAL_OTFDEC_MSPDEINIT_CB_ID :
00407         hotfdec->MspDeInitCallback = HAL_OTFDEC_MspDeInit; /* Legacy weak MspDeInit */
00408         break;
00409 
00410       default :
00411         /* Update the error code */
00412         hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
00413 
00414         /* Return error status */
00415         status =  HAL_ERROR;
00416         break;
00417     }
00418   }
00419   else if (HAL_OTFDEC_STATE_RESET == hotfdec->State)
00420   {
00421     switch (CallbackID)
00422     {
00423       case HAL_OTFDEC_MSPINIT_CB_ID :
00424         hotfdec->MspInitCallback = HAL_OTFDEC_MspInit; /* Legacy weak MspInit */
00425         break;
00426 
00427       case HAL_OTFDEC_MSPDEINIT_CB_ID :
00428         hotfdec->MspDeInitCallback = HAL_OTFDEC_MspDeInit; /* Legacy weak MspDeInit */
00429         break;
00430 
00431       default :
00432         /* Update the error code */
00433         hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
00434 
00435         /* Return error status */
00436         status =  HAL_ERROR;
00437         break;
00438     }
00439   }
00440   else
00441   {
00442     /* Update the error code */
00443     hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
00444 
00445     /* Return error status */
00446     status =  HAL_ERROR;
00447   }
00448 
00449   return status;
00450 }
00451 
00452 #endif /* USE_HAL_OTFDEC_REGISTER_CALLBACKS */
00453 
00454 /**
00455   * @}
00456   */
00457 
00458 /** @defgroup OTFDEC_Exported_Functions_Group2  OTFDEC IRQ handler management
00459  *  @brief   OTFDEC IRQ handler.
00460  *
00461 @verbatim
00462   ==============================================================================
00463                 ##### OTFDEC IRQ handler management #####
00464   ==============================================================================
00465 [..]  This section provides OTFDEC IRQ handler function.
00466 
00467 @endverbatim
00468   * @{
00469   */
00470 
00471 /**
00472   * @brief  Handle OTFDEC interrupt request.
00473   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
00474   *         the configuration information for OTFDEC module
00475   * @retval None
00476   */
00477 void HAL_OTFDEC_IRQHandler(OTFDEC_HandleTypeDef *hotfdec)
00478 {
00479   uint32_t isr_reg;
00480 
00481   isr_reg = READ_REG(hotfdec->Instance->ISR);
00482   if ((isr_reg & OTFDEC_ISR_SEIF) == OTFDEC_ISR_SEIF)
00483   {
00484     SET_BIT( hotfdec->Instance->ICR, OTFDEC_ICR_SEIF );
00485     hotfdec->ErrorCode |= HAL_OTFDEC_SECURITY_ERROR;
00486   }
00487   if ((isr_reg & OTFDEC_ISR_XONEIF) == OTFDEC_ISR_XONEIF)
00488   {
00489     SET_BIT( hotfdec->Instance->ICR, OTFDEC_ICR_XONEIF );
00490     hotfdec->ErrorCode |= HAL_OTFDEC_EXECUTE_ERROR;
00491   }
00492   if ((isr_reg & OTFDEC_ISR_KEIF) == OTFDEC_ISR_KEIF)
00493   {
00494     SET_BIT( hotfdec->Instance->ICR, OTFDEC_ICR_KEIF );
00495     hotfdec->ErrorCode |= HAL_OTFDEC_KEY_ERROR;
00496   }
00497 
00498 #if (USE_HAL_OTFDEC_REGISTER_CALLBACKS == 1)
00499   hotfdec->ErrorCallback(hotfdec);
00500 #else
00501   HAL_OTFDEC_ErrorCallback(hotfdec);
00502 #endif /* USE_HAL_OTFDEC_REGISTER_CALLBACKS */
00503 }
00504 
00505 /**
00506   * @brief OTFDEC error callback.
00507   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
00508   *         the configuration information for OTFDEC module
00509   * @retval None
00510   */
00511 __weak void HAL_OTFDEC_ErrorCallback(OTFDEC_HandleTypeDef *hotfdec)
00512 {
00513   /* Prevent unused argument(s) compilation warning */
00514   UNUSED(hotfdec);
00515 
00516   /* NOTE : This function should not be modified; when the callback is needed,
00517             the HAL_OTFDEC_ErrorCallback can be implemented in the user file.
00518    */
00519 }
00520 
00521 /**
00522   * @}
00523   */
00524 
00525 
00526 
00527 
00528 /** @defgroup OTFDEC_Exported_Functions_Group3 Peripheral Control functions
00529  *  @brief   Peripheral control functions.
00530  *
00531 @verbatim
00532   ==============================================================================
00533                       ##### Peripheral Control functions #####
00534   ==============================================================================
00535     [..]
00536     This subsection permits to configure the OTFDEC peripheral
00537 
00538 @endverbatim
00539   * @{
00540   */
00541 
00542 /**
00543   * @brief  Lock region keys.
00544   * @note   Writes to this region KEYRx registers are ignored until next OTFDEC reset.
00545   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
00546   *         the configuration information for OTFDEC module
00547   * @param  RegionIndex index of region the keys of which are locked
00548   * @retval HAL state
00549   */
00550 HAL_StatusTypeDef HAL_OTFDEC_RegionKeyLock(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex)
00551 {
00552   OTFDEC_Region_TypeDef * region;
00553   uint32_t address;
00554 
00555   /* Check the parameters */
00556   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
00557   assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
00558 
00559   /* Take Lock */
00560   __HAL_LOCK(hotfdec);
00561 
00562   address =   (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
00563   region = (OTFDEC_Region_TypeDef *)address;
00564 
00565   SET_BIT( region->REG_CONFIGR, OTFDEC_REG_CONFIGR_KEYLOCK );
00566 
00567   /* Release Lock */
00568   __HAL_UNLOCK(hotfdec);
00569 
00570   /* Status is okay */
00571   return HAL_OK;
00572 }
00573 
00574 /**
00575   * @brief  Set region keys.
00576   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
00577   *         the configuration information for OTFDEC module
00578   * @param  RegionIndex index of region the keys of which are set
00579   * @param  pKey pointer at set of keys
00580   * @note   The API reads the key CRC computed by the peripheral and compares it with that
00581   *         theoretically expected. An error is reported if they are different.
00582   * @retval HAL state
00583   */
00584 HAL_StatusTypeDef HAL_OTFDEC_RegionSetKey(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex, uint32_t *pKey)
00585 {
00586   OTFDEC_Region_TypeDef * region;
00587   uint32_t address;
00588 
00589   /* Check the parameters */
00590   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
00591   assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
00592 
00593   if (pKey == NULL)
00594   {
00595     return HAL_ERROR;
00596   }
00597   else
00598   {
00599     /* Take Lock */
00600     __HAL_LOCK(hotfdec);
00601 
00602     address =   (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
00603     region = (OTFDEC_Region_TypeDef *)address;
00604 
00605     /* Set Key */
00606     WRITE_REG( region->REG_KEYR0, pKey[0]);
00607 
00608     __DSB();
00609     __ISB();
00610 
00611     WRITE_REG( region->REG_KEYR1, pKey[1]);
00612 
00613     __DSB();
00614     __ISB();
00615 
00616     WRITE_REG( region->REG_KEYR2, pKey[2]);
00617 
00618     __DSB();
00619     __ISB();
00620 
00621     WRITE_REG( region->REG_KEYR3, pKey[3]);
00622 
00623     /* Compute theoretically expected CRC and compare it with that reported by the peripheral */
00624     if (HAL_OTFDEC_KeyCRCComputation(pKey) != HAL_OTFDEC_RegionGetKeyCRC(hotfdec, RegionIndex))
00625     {
00626       /* Release Lock */
00627       __HAL_UNLOCK(hotfdec);
00628 
00629       /* Status is okay */
00630       return HAL_ERROR;
00631     }
00632 
00633     /* Release Lock */
00634     __HAL_UNLOCK(hotfdec);
00635 
00636     /* Status is okay */
00637     return HAL_OK;
00638   }
00639 }
00640 
00641 /**
00642   * @brief  Set region mode.
00643   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
00644   *         the configuration information for OTFDEC module
00645   * @param  RegionIndex index of region the mode of which is set
00646   * @param  mode This parameter can be only:
00647   *           @arg @ref OTFDEC_REG_MODE_INSTRUCTION_ACCESSES_ONLY            
00648                         Only instruction accesses are decrypted
00649   *           @arg @ref OTFDEC_REG_MODE_DATA_ACCESSES_ONLY                    
00650                         Only data accesses are decrypted
00651   *           @arg @ref OTFDEC_REG_MODE_INSTRUCTION_OR_DATA_ACCESSES         
00652                         All read accesses are decrypted (instruction or data)
00653   *           @arg @ref OTFDEC_REG_MODE_INSTRUCTION_ACCESSES_ONLY_WITH_CIPHER 
00654                         Only instruction accesses are decrypted with proprietary cipher activated
00655   * @retval HAL state
00656   */
00657 HAL_StatusTypeDef HAL_OTFDEC_RegionSetMode(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex, uint32_t mode)
00658 {
00659   OTFDEC_Region_TypeDef * region;
00660   uint32_t address;
00661 
00662   /* Check the parameters */
00663   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
00664   assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
00665   assert_param(IS_OTFDEC_REGION_OPERATING_MODE(mode));
00666 
00667   /* Take Lock */
00668   __HAL_LOCK(hotfdec);
00669 
00670   address =   (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
00671   region = (OTFDEC_Region_TypeDef *)address;
00672 
00673   /* Set mode */
00674   MODIFY_REG(region->REG_CONFIGR, OTFDEC_REG_CONFIGR_MODE, mode);
00675 
00676   /* Release Lock */
00677   __HAL_UNLOCK(hotfdec);
00678 
00679   /* Status is okay */
00680   return HAL_OK;
00681 }
00682 
00683 /**
00684   * @brief  Set region configuration.
00685   * @note  Region deciphering is enabled at the end of this function
00686   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
00687   *         the configuration information for OTFDEC module
00688   * @param  RegionIndex index of region that is configured
00689   * @param  Config pointer on structure containing the region configuration parameters
00690   * @param  lock configuration lock enable or disable parameter
00691   *         This parameter can be one of the following values:
00692   *          @arg @ref OTFDEC_REG_CONFIGR_LOCK_DISABLE      OTFDEC region configuration is not locked
00693   *          @arg @ref OTFDEC_REG_CONFIGR_LOCK_ENABLE       OTFDEC region configuration is locked
00694   * @retval HAL state
00695   */
00696 HAL_StatusTypeDef HAL_OTFDEC_RegionConfig(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex, 
00697                                           OTFDEC_RegionConfigTypeDef *Config, uint32_t lock)
00698 {
00699   OTFDEC_Region_TypeDef * region;
00700   uint32_t address;
00701 
00702   /* Check the parameters */
00703   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
00704   assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
00705   assert_param(IS_OTFDEC_REGION_CONFIG_LOCK(lock));
00706 
00707   if (Config == NULL)
00708   {
00709     return HAL_ERROR;
00710   }
00711   else
00712   {
00713 
00714     /* Take Lock */
00715     __HAL_LOCK(hotfdec);
00716 
00717     address =   (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
00718     region = (OTFDEC_Region_TypeDef *)address;
00719 
00720     /* Set Nonce */
00721     WRITE_REG( region->REG_NONCER0, Config->Nonce[0]);
00722 
00723     WRITE_REG( region->REG_NONCER1, Config->Nonce[1]);
00724 
00725     /* Write region protected area start and end addresses */
00726     WRITE_REG( region->REG_START_ADDR, Config->StartAddress);
00727 
00728     WRITE_REG( region->REG_END_ADDR, Config->EndAddress);
00729 
00730     /* Write Version */
00731     MODIFY_REG( region->REG_CONFIGR, OTFDEC_REG_CONFIGR_VERSION, 
00732                   (uint32_t)(Config->Version) << OTFDEC_REG_CONFIGR_VERSION_Pos );
00733 
00734     /* Enable region deciphering or enciphering (depending of OTFDEC_CR ENC bit setting) */
00735     SET_BIT( region->REG_CONFIGR, OTFDEC_REG_CONFIGR_REG_ENABLE);
00736 
00737     /* Lock the region configuration according to lock parameter value */
00738     if (lock == OTFDEC_REG_CONFIGR_LOCK_ENABLE)
00739     {
00740       SET_BIT( region->REG_CONFIGR, OTFDEC_REG_CONFIGR_LOCK_ENABLE);
00741     }
00742 
00743     /* Release Lock */
00744     __HAL_UNLOCK(hotfdec);
00745 
00746     /* Status is okay */
00747     return HAL_OK;
00748   }
00749 }
00750 
00751 
00752 /**
00753   * @brief  Compute Key CRC
00754   * @param  pKey pointer at set of keys
00755   * @retval CRC value
00756   */
00757 uint32_t HAL_OTFDEC_KeyCRCComputation(uint32_t *pKey)
00758 {
00759   uint8_t crc7_poly = 0x7;
00760   uint32_t key_strobe[4] = {0xAA55AA55U, 0x3U, 0x18U, 0xC0U};
00761   uint8_t  i;
00762   uint8_t crc = 0;
00763   uint32_t  j;
00764   uint32_t  keyval;
00765   uint32_t  k;
00766   uint32_t * temp = pKey;
00767 
00768   for (j = 0U; j < 4U; j++)
00769   {
00770     keyval = *temp;
00771     temp++;
00772     if (j == 0U)
00773     {
00774       keyval ^= key_strobe[0];
00775     }
00776     else
00777     {
00778       keyval ^= (key_strobe[j] << 24) | ((uint32_t)crc << 16) | (key_strobe[j] << 8) | crc;
00779     }
00780 
00781     crc = 0;
00782     for (i = 0; i < (uint8_t)32; i++)
00783     {
00784       k = ((((uint32_t)crc >> 7) ^ ((keyval >> ((uint8_t)31-i))&((uint8_t)0xF)))) & 1U;
00785       crc <<= 1;
00786       if (k != 0U)
00787       {
00788         crc ^= crc7_poly;
00789        }
00790     }
00791 
00792     crc^=(uint8_t)0x55;
00793   }
00794 
00795   return (uint32_t) crc;
00796 }
00797 
00798 
00799 /**
00800   * @brief  Enable region deciphering.
00801   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
00802   *         the configuration information for OTFDEC module
00803   * @param  RegionIndex index of region the deciphering is enabled
00804   * @note   An error is reported when the configuration is locked.
00805   * @retval HAL state
00806   */
00807 HAL_StatusTypeDef HAL_OTFDEC_RegionEnable(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex)
00808 {
00809   OTFDEC_Region_TypeDef * region;
00810   uint32_t address;
00811 
00812   /* Check the parameters */
00813   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
00814   assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
00815 
00816   /* Take Lock */
00817   __HAL_LOCK(hotfdec);
00818 
00819   address =   (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
00820   region = (OTFDEC_Region_TypeDef *)address;
00821 
00822   if (READ_BIT( region->REG_CONFIGR, OTFDEC_REG_CONFIGR_LOCK_ENABLE) == OTFDEC_REG_CONFIGR_LOCK_ENABLE)
00823   {
00824     /* Configuration is locked, REG_EN bit can't be modified */
00825     __HAL_UNLOCK(hotfdec);
00826 
00827      return HAL_ERROR;
00828   }
00829 
00830   /* Enable region processing */
00831   SET_BIT( region->REG_CONFIGR, OTFDEC_REG_CONFIGR_REG_ENABLE);
00832 
00833   /* Release Lock */
00834   __HAL_UNLOCK(hotfdec);
00835 
00836   /* Status is okay */
00837   return HAL_OK;
00838 }
00839 
00840 /**
00841   * @brief  Disable region deciphering.
00842   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
00843   *         the configuration information for OTFDEC module
00844   * @param  RegionIndex index of region the deciphering is disabled
00845   * @note   An error is reported when the configuration is locked.
00846   * @retval HAL state
00847   */
00848 HAL_StatusTypeDef HAL_OTFDEC_RegionDisable(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex)
00849 {
00850   OTFDEC_Region_TypeDef * region;
00851   uint32_t address;
00852 
00853   /* Check the parameters */
00854   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
00855   assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
00856 
00857   /* Take Lock */
00858   __HAL_LOCK(hotfdec);
00859 
00860   address =   (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
00861   region = (OTFDEC_Region_TypeDef *)address;
00862 
00863   if (READ_BIT( region->REG_CONFIGR, OTFDEC_REG_CONFIGR_LOCK_ENABLE) == OTFDEC_REG_CONFIGR_LOCK_ENABLE)
00864   {
00865     /* Configuration is locked, REG_EN bit can't be modified */
00866     __HAL_UNLOCK(hotfdec);
00867 
00868      return HAL_ERROR;
00869   }
00870 
00871   /* Disable region processing */
00872   CLEAR_BIT( region->REG_CONFIGR, OTFDEC_REG_CONFIGR_REG_ENABLE);
00873 
00874   /* Release Lock */
00875   __HAL_UNLOCK(hotfdec);
00876 
00877   /* Status is okay */
00878   return HAL_OK;
00879 }
00880 
00881 /**
00882   * @}
00883   */
00884 
00885 /** @defgroup OTFDEC_Exported_Functions_Group4 Peripheral State and Status functions
00886  *  @brief   Peripheral State functions.
00887  *
00888 @verbatim
00889   ==============================================================================
00890                       ##### Peripheral State functions #####
00891   ==============================================================================
00892     [..]
00893     This subsection permits to get in run-time the status of the peripheral.
00894 
00895 @endverbatim
00896   * @{
00897   */
00898 
00899 /**
00900   * @brief  Return the OTFDEC state.
00901   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
00902   *         the configuration information for OTFDEC module
00903   * @retval HAL state
00904   */
00905 HAL_OTFDEC_StateTypeDef HAL_OTFDEC_GetState(OTFDEC_HandleTypeDef *hotfdec)
00906 {
00907   return hotfdec->State;
00908 }
00909 
00910 
00911 /**
00912   * @brief  Return region keys CRC.
00913   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
00914   *         the configuration information for OTFDEC module
00915   * @param  RegionIndex index of region the keys CRC of which is read
00916   * @retval Key CRC
00917   */
00918 uint32_t HAL_OTFDEC_RegionGetKeyCRC(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex)
00919 {
00920   OTFDEC_Region_TypeDef * region;
00921   uint32_t address;
00922   uint32_t keycrc;
00923 
00924   /* Check the parameters */
00925   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
00926   assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
00927 
00928   address =   (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
00929   region = (OTFDEC_Region_TypeDef *)address;
00930 
00931   keycrc = (READ_REG( region->REG_CONFIGR )) & OTFDEC_REG_CONFIGR_KEYCRC;
00932 
00933   keycrc >>= OTFDEC_REG_CONFIGR_KEYCRC_Pos;
00934 
00935   return keycrc;
00936 }
00937 
00938 /**
00939   * @brief  Return region configuration parameters.
00940   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
00941   *         the configuration information for OTFDEC module
00942   * @param  RegionIndex index of region the configuration of which is read
00943   * @param  Config pointer on structure that will be filled up with the region configuration parameters
00944   * @retval HAL state
00945   */
00946 HAL_StatusTypeDef HAL_OTFDEC_RegionGetConfig(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex,
00947                                              OTFDEC_RegionConfigTypeDef *Config)
00948 {
00949   OTFDEC_Region_TypeDef * region;
00950   uint32_t address;
00951 
00952   /* Check the parameters */
00953   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
00954   assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
00955 
00956   if (Config == NULL)
00957   {
00958     return HAL_ERROR;
00959   }
00960   else
00961   {
00962     /* Take Lock */
00963     __HAL_LOCK(hotfdec);
00964 
00965     address =   (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
00966     region = (OTFDEC_Region_TypeDef *)address;
00967 
00968     /* Read Nonce */
00969     Config->Nonce[0] = READ_REG(region->REG_NONCER0);
00970     Config->Nonce[1] = READ_REG(region->REG_NONCER1);
00971 
00972     /* Read Addresses */
00973     Config->StartAddress = READ_REG(region->REG_START_ADDR);
00974     Config->EndAddress = READ_REG(region->REG_END_ADDR);
00975 
00976     /* Read Version */
00977     Config->Version = (uint16_t)(READ_REG(region->REG_CONFIGR) & 
00978                                      OTFDEC_REG_CONFIGR_VERSION) >> OTFDEC_REG_CONFIGR_VERSION_Pos;
00979 
00980     /* Release Lock */
00981     __HAL_UNLOCK(hotfdec);
00982 
00983     /* Status is okay */
00984     return HAL_OK;
00985   }
00986 }
00987 
00988 
00989 /**
00990   * @}
00991   */
00992 
00993 /**
00994   * @}
00995   */
00996 
00997 #endif /* OTFDEC1 */
00998 
00999 #endif /* HAL_OTFDEC_MODULE_ENABLED */
01000 
01001 
01002 /**
01003   * @}
01004   */
01005 
01006 /**
01007   * @}
01008   */