Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:08

0001 /**
0002   ******************************************************************************
0003   * @file    stm32h7xx_hal_otfdec.c
0004   * @author  MCD Application Team
0005   * @brief   OTFDEC HAL module driver.
0006   *          This file provides firmware functions to manage the following
0007   *          functionalities of the On-The-Fly Decryption (OTFDEC) peripheral:
0008   *           + Initialization and de-initialization functions
0009   *           + Region setting/enable functions
0010   *           + Peripheral State functions
0011   *
0012   ******************************************************************************
0013   * @attention
0014   *
0015   * Copyright (c) 2018 STMicroelectronics.
0016   * All rights reserved.
0017   *
0018   * This software is licensed under terms that can be found in the LICENSE file
0019   * in the root directory of this software component.
0020   * If no LICENSE file comes with this software, it is provided AS-IS.
0021   *
0022   ******************************************************************************
0023   @verbatim
0024   ==============================================================================
0025                      ##### How to use this driver #####
0026   ==============================================================================
0027  [..]
0028     The OTFDEC HAL driver can be used as follows:
0029 
0030     (#) Declare an OTFDEC_HandleTypeDef handle structure (eg. OTFDEC_HandleTypeDef hotfdec).
0031 
0032     (#) Initialize the OTFDEC low level resources by implementing the HAL_OTFDEC_MspInit() API:
0033         (++) Enable the OTFDEC interface clock.
0034         (++) NVIC configuration if interrupts are used
0035             (+++) Configure the OTFDEC interrupt priority.
0036             (+++) Enable the NVIC OTFDEC IRQ handle.
0037 
0038     (#) Initialize the OTFDEC peripheral by calling the HAL_OTFDEC_Init() API.
0039 
0040     (#) For each region,
0041 
0042         (++) Configure the region deciphering mode by calling the HAL_OTFDEC_RegionSetMode() API.
0043 
0044         (++) Write the region Key by calling the HAL_OTFDEC_RegionSetKey() API. If desired,
0045         read the key CRC by calling HAL_OTFDEC_RegionGetKeyCRC() API and compare the
0046         result with the theoretically expected CRC.
0047 
0048         (++) Initialize the OTFDEC region config structure with the Nonce, protected
0049         region start and end addresses and firmware version, and wrap-up the region
0050         configuration by calling HAL_OTFDEC_RegionConfig() API.
0051 
0052     (#) At this point, the OTFDEC region configuration is done and the deciphering
0053         is enabled. The region can be deciphered on the fly after having made sure
0054         the OctoSPI is configured in memory-mapped mode.
0055 
0056     [..]
0057     (@) Warning: the OTFDEC deciphering is based on a different endianness compared
0058         to the AES-CTR as implemented in the AES peripheral. E.g., if the OTFEC
0059         resorts to the Key (B0, B1, B2, B3) where Bi are 32-bit longwords and B0
0060         is the Least Significant Word, the AES has to be configured with the Key
0061         (B3, B2, B1, B0) to report the same result (with the same swapping applied
0062         to the Initialization Vector).
0063 
0064     [..]
0065 
0066     *** Callback registration ***
0067     =============================================
0068     [..]
0069 
0070      The compilation flag USE_HAL_OTFDEC_REGISTER_CALLBACKS, when set to 1,
0071      allows the user to configure dynamically the driver callbacks.
0072      Use Functions HAL_OTFDEC_RegisterCallback()
0073      to register an interrupt callback.
0074     [..]
0075 
0076      Function HAL_OTFDEC_RegisterCallback() allows to register following callbacks:
0077        (+) ErrorCallback                  : OTFDEC error callback
0078        (+) MspInitCallback                : OTFDEC Msp Init callback
0079        (+) MspDeInitCallback              : OTFDEC Msp DeInit callback
0080      This function takes as parameters the HAL peripheral handle, the Callback ID
0081      and a pointer to the user callback function.
0082     [..]
0083 
0084      Use function HAL_OTFDEC_UnRegisterCallback to reset a callback to the default
0085      weak function.
0086     [..]
0087 
0088     HAL_OTFDEC_UnRegisterCallback takes as parameters the HAL peripheral handle,
0089      and the Callback ID.
0090      This function allows to reset following callbacks:
0091        (+) ErrorCallback                  : OTFDEC error callback
0092        (+) MspInitCallback                : OTFDEC Msp Init callback
0093        (+) MspDeInitCallback              : OTFDEC Msp DeInit callback
0094      [..]
0095 
0096      By default, after the HAL_OTFDEC_Init() and when the state is HAL_OTFDEC_STATE_RESET
0097      all callbacks are set to the corresponding weak functions:
0098      example HAL_OTFDEC_ErrorCallback().
0099      Exception done for MspInit and MspDeInit functions that are
0100      reset to the legacy weak functions in the HAL_OTFDEC_Init()HAL_OTFDEC_DeInit() only when
0101      these callbacks are null (not registered beforehand).
0102     [..]
0103 
0104      If MspInit or MspDeInit are not null, the HAL_OTFDEC_Init()/HAL_OTFDEC_DeInit()
0105      keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
0106      [..]
0107 
0108      Callbacks can be registered/unregistered in HAL_OTFDEC_STATE_READY state only.
0109      Exception done MspInit/MspDeInit functions that can be registered/unregistered
0110      in HAL_OTFDEC_STATE_READY or HAL_OTFDEC_STATE_RESET state,
0111      thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
0112     [..]
0113 
0114      Then, the user first registers the MspInit/MspDeInit user callbacks
0115      using HAL_OTFDEC_RegisterCallback() before calling HAL_OTFDEC_DeInit()
0116      or HAL_OTFDEC_Init() function.
0117      [..]
0118 
0119      When the compilation flag USE_HAL_OTFDEC_REGISTER_CALLBACKS is set to 0 or
0120      not defined, the callback registration feature is not available and all callbacks
0121      are set to the corresponding weak functions.
0122 
0123   @endverbatim
0124   ******************************************************************************
0125   */
0126 
0127 /* Includes ------------------------------------------------------------------*/
0128 #include "stm32h7xx_hal.h"
0129 
0130 /** @addtogroup STM32H7xx_HAL_Driver
0131   * @{
0132   */
0133 
0134 /** @defgroup OTFDEC OTFDEC
0135   * @ingroup RTEMSBSPsARMSTM32H7
0136   * @brief OTFDEC HAL module driver.
0137   * @{
0138   */
0139 
0140 
0141 #ifdef HAL_OTFDEC_MODULE_ENABLED
0142 
0143 #if defined(OTFDEC1)
0144 
0145 /* Private typedef -----------------------------------------------------------*/
0146 /* Private define ------------------------------------------------------------*/
0147 /* Private macro -------------------------------------------------------------*/
0148 /* Private variables ---------------------------------------------------------*/
0149 /* Private function prototypes -----------------------------------------------*/
0150 /* Private functions ---------------------------------------------------------*/
0151 
0152 /* Exported functions --------------------------------------------------------*/
0153 /** @addtogroup OTFDEC_Exported_Functions
0154   * @{
0155   */
0156 
0157 /** @defgroup OTFDEC_Exported_Functions_Group1 Initialization and de-initialization functions
0158   * @ingroup RTEMSBSPsARMSTM32H7
0159   *  @brief    Initialization and Configuration functions.
0160   *
0161 @verbatim
0162   ==============================================================================
0163               ##### Initialization and de-initialization functions #####
0164   ==============================================================================
0165 
0166 @endverbatim
0167   * @{
0168   */
0169 
0170 /**
0171   * @brief  Initialize the OTFDEC peripheral and create the associated handle.
0172   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
0173   *         the configuration information for OTFDEC module
0174   * @retval HAL status
0175   */
0176 HAL_StatusTypeDef HAL_OTFDEC_Init(OTFDEC_HandleTypeDef *hotfdec)
0177 {
0178   /* Check the OTFDEC handle allocation */
0179   if (hotfdec == NULL)
0180   {
0181     return HAL_ERROR;
0182   }
0183 
0184   /* Check the parameters */
0185   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
0186 
0187   if (hotfdec->State == HAL_OTFDEC_STATE_RESET)
0188   {
0189     /* Allocate lock resource and initialize it */
0190     __HAL_UNLOCK(hotfdec);
0191 
0192 #if (USE_HAL_OTFDEC_REGISTER_CALLBACKS == 1)
0193     /* Init the OTFDEC Callback settings */
0194     hotfdec->ErrorCallback = HAL_OTFDEC_ErrorCallback; /* Legacy weak callback */
0195 
0196     if (hotfdec->MspInitCallback == NULL)
0197     {
0198       hotfdec->MspInitCallback = HAL_OTFDEC_MspInit; /* Legacy weak MspInit */
0199     }
0200 
0201     /* Init the low level hardware */
0202     hotfdec->MspInitCallback(hotfdec);
0203 #else
0204     /* Init the low level hardware */
0205     HAL_OTFDEC_MspInit(hotfdec);
0206 #endif /* USE_HAL_OTFDEC_REGISTER_CALLBACKS */
0207   }
0208 
0209   /* Change the OTFDEC state */
0210   hotfdec->State = HAL_OTFDEC_STATE_READY;
0211 
0212   /* Return function status */
0213   return HAL_OK;
0214 }
0215 
0216 /**
0217   * @brief  DeInitialize the OTFDEC peripheral.
0218   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
0219   *         the configuration information for OTFDEC module
0220   * @retval HAL status
0221   */
0222 HAL_StatusTypeDef HAL_OTFDEC_DeInit(OTFDEC_HandleTypeDef *hotfdec)
0223 {
0224   /* Check the OTFDEC handle allocation */
0225   if (hotfdec == NULL)
0226   {
0227     return HAL_ERROR;
0228   }
0229 
0230   /* Check the parameters */
0231   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
0232 
0233   /* Change the OTFDEC state */
0234   hotfdec->State = HAL_OTFDEC_STATE_BUSY;
0235 
0236 #if (USE_HAL_OTFDEC_REGISTER_CALLBACKS == 1)
0237   if (hotfdec->MspDeInitCallback == NULL)
0238   {
0239     hotfdec->MspDeInitCallback = HAL_OTFDEC_MspDeInit; /* Legacy weak MspDeInit */
0240   }
0241 
0242   /* DeInit the low level hardware: CLOCK, NVIC */
0243   hotfdec->MspDeInitCallback(hotfdec);
0244 #else
0245   /* DeInit the low level hardware: CLOCK, NVIC */
0246   HAL_OTFDEC_MspDeInit(hotfdec);
0247 #endif /* USE_HAL_OTFDEC_REGISTER_CALLBACKS */
0248 
0249   /* Change the OTFDEC state */
0250   hotfdec->State = HAL_OTFDEC_STATE_RESET;
0251 
0252   /* Reset OTFDEC error status */
0253   hotfdec->ErrorCode = HAL_OTFDEC_ERROR_NONE;
0254 
0255   /* Release Lock */
0256   __HAL_UNLOCK(hotfdec);
0257 
0258   /* Return function status */
0259   return HAL_OK;
0260 }
0261 
0262 /**
0263   * @brief  Initialize the OTFDEC MSP.
0264   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
0265   *         the configuration information for OTFDEC module
0266   * @retval None
0267   */
0268 __weak void HAL_OTFDEC_MspInit(OTFDEC_HandleTypeDef *hotfdec)
0269 {
0270   /* Prevent unused argument(s) compilation warning */
0271   UNUSED(hotfdec);
0272 
0273   /* NOTE : This function should not be modified; when the callback is needed,
0274             the HAL_OTFDEC_MspInit can be implemented in the user file.
0275    */
0276 }
0277 
0278 /**
0279   * @brief  DeInitialize OTFDEC MSP.
0280   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
0281   *         the configuration information for OTFDEC module
0282   * @retval None
0283   */
0284 __weak void HAL_OTFDEC_MspDeInit(OTFDEC_HandleTypeDef *hotfdec)
0285 {
0286   /* Prevent unused argument(s) compilation warning */
0287   UNUSED(hotfdec);
0288 
0289   /* NOTE : This function should not be modified; when the callback is needed,
0290             the HAL_OTFDEC_MspDeInit can be implemented in the user file.
0291    */
0292 }
0293 
0294 #if (USE_HAL_OTFDEC_REGISTER_CALLBACKS == 1)
0295 /**
0296   * @brief  Register a User OTFDEC Callback
0297   *         To be used instead of the weak predefined callback
0298   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
0299   *         the configuration information for OTFDEC module
0300   * @param  CallbackID ID of the callback to be registered
0301   *         This parameter can be one of the following values:
0302   *          @arg @ref HAL_OTFDEC_ERROR_CB_ID           OTFDEC error callback ID
0303   *          @arg @ref HAL_OTFDEC_MSPINIT_CB_ID         MspInit callback ID
0304   *          @arg @ref HAL_OTFDEC_MSPDEINIT_CB_ID       MspDeInit callback ID
0305   * @param  pCallback pointer to the Callback function
0306   * @retval HAL status
0307   */
0308 HAL_StatusTypeDef HAL_OTFDEC_RegisterCallback(OTFDEC_HandleTypeDef *hotfdec, HAL_OTFDEC_CallbackIDTypeDef CallbackID,
0309                                               pOTFDEC_CallbackTypeDef pCallback)
0310 {
0311   HAL_StatusTypeDef status = HAL_OK;
0312 
0313   if (pCallback == NULL)
0314   {
0315     /* Update the error code */
0316     hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
0317 
0318     return HAL_ERROR;
0319   }
0320 
0321   if (hotfdec->State == HAL_OTFDEC_STATE_READY)
0322   {
0323     switch (CallbackID)
0324     {
0325       case HAL_OTFDEC_ERROR_CB_ID :
0326         hotfdec->ErrorCallback = pCallback;
0327         break;
0328 
0329       case HAL_OTFDEC_MSPINIT_CB_ID :
0330         hotfdec->MspInitCallback = pCallback;
0331         break;
0332 
0333       case HAL_OTFDEC_MSPDEINIT_CB_ID :
0334         hotfdec->MspDeInitCallback = pCallback;
0335         break;
0336 
0337       default :
0338         /* Update the error code */
0339         hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
0340 
0341         /* Return error status */
0342         status = HAL_ERROR;
0343         break;
0344     }
0345   }
0346   else if (HAL_OTFDEC_STATE_RESET == hotfdec->State)
0347   {
0348     switch (CallbackID)
0349     {
0350       case HAL_OTFDEC_MSPINIT_CB_ID :
0351         hotfdec->MspInitCallback = pCallback;
0352         break;
0353 
0354       case HAL_OTFDEC_MSPDEINIT_CB_ID :
0355         hotfdec->MspDeInitCallback = pCallback;
0356         break;
0357 
0358       default :
0359         /* Update the error code */
0360         hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
0361 
0362         /* Return error status */
0363         status = HAL_ERROR;
0364         break;
0365     }
0366   }
0367   else
0368   {
0369     /* Update the error code */
0370     hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
0371 
0372     /* Return error status */
0373     status =  HAL_ERROR;
0374   }
0375 
0376   return status;
0377 }
0378 
0379 /**
0380   * @brief  Unregister a OTFDEC Callback
0381   *         OTFDEC callback is redirected to the weak predefined callback
0382   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
0383   *         the configuration information for OTFDEC module
0384   * @param  CallbackID ID of the callback to be registered
0385   *         This parameter can be one of the following values:
0386   *          @arg @ref HAL_OTFDEC_ERROR_CB_ID           OTFDEC error callback ID
0387   *          @arg @ref HAL_OTFDEC_MSPINIT_CB_ID         MspInit callback ID
0388   *          @arg @ref HAL_OTFDEC_MSPDEINIT_CB_ID       MspDeInit callback ID
0389   * @retval HAL status
0390   */
0391 HAL_StatusTypeDef HAL_OTFDEC_UnRegisterCallback(OTFDEC_HandleTypeDef *hotfdec, HAL_OTFDEC_CallbackIDTypeDef CallbackID)
0392 {
0393   HAL_StatusTypeDef status = HAL_OK;
0394 
0395   if (hotfdec->State == HAL_OTFDEC_STATE_READY)
0396   {
0397     switch (CallbackID)
0398     {
0399       case HAL_OTFDEC_ERROR_CB_ID :
0400         hotfdec->ErrorCallback = HAL_OTFDEC_ErrorCallback;
0401         break;
0402 
0403       case HAL_OTFDEC_MSPINIT_CB_ID :
0404         hotfdec->MspInitCallback = HAL_OTFDEC_MspInit; /* Legacy weak MspInit */
0405         break;
0406 
0407       case HAL_OTFDEC_MSPDEINIT_CB_ID :
0408         hotfdec->MspDeInitCallback = HAL_OTFDEC_MspDeInit; /* Legacy weak MspDeInit */
0409         break;
0410 
0411       default :
0412         /* Update the error code */
0413         hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
0414 
0415         /* Return error status */
0416         status =  HAL_ERROR;
0417         break;
0418     }
0419   }
0420   else if (HAL_OTFDEC_STATE_RESET == hotfdec->State)
0421   {
0422     switch (CallbackID)
0423     {
0424       case HAL_OTFDEC_MSPINIT_CB_ID :
0425         hotfdec->MspInitCallback = HAL_OTFDEC_MspInit; /* Legacy weak MspInit */
0426         break;
0427 
0428       case HAL_OTFDEC_MSPDEINIT_CB_ID :
0429         hotfdec->MspDeInitCallback = HAL_OTFDEC_MspDeInit; /* Legacy weak MspDeInit */
0430         break;
0431 
0432       default :
0433         /* Update the error code */
0434         hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
0435 
0436         /* Return error status */
0437         status =  HAL_ERROR;
0438         break;
0439     }
0440   }
0441   else
0442   {
0443     /* Update the error code */
0444     hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
0445 
0446     /* Return error status */
0447     status =  HAL_ERROR;
0448   }
0449 
0450   return status;
0451 }
0452 
0453 #endif /* USE_HAL_OTFDEC_REGISTER_CALLBACKS */
0454 
0455 /**
0456   * @}
0457   */
0458 
0459 /** @defgroup OTFDEC_Exported_Functions_Group2  OTFDEC IRQ handler management
0460   * @ingroup RTEMSBSPsARMSTM32H7
0461   *  @brief   OTFDEC IRQ handler.
0462   *
0463 @verbatim
0464   ==============================================================================
0465                 ##### OTFDEC IRQ handler management #####
0466   ==============================================================================
0467 [..]  This section provides OTFDEC IRQ handler function.
0468 
0469 @endverbatim
0470   * @{
0471   */
0472 
0473 /**
0474   * @brief  Handle OTFDEC interrupt request.
0475   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
0476   *         the configuration information for OTFDEC module
0477   * @retval None
0478   */
0479 void HAL_OTFDEC_IRQHandler(OTFDEC_HandleTypeDef *hotfdec)
0480 {
0481   uint32_t isr_reg;
0482 
0483   isr_reg = READ_REG(hotfdec->Instance->ISR);
0484   if ((isr_reg & OTFDEC_ISR_SEIF) == OTFDEC_ISR_SEIF)
0485   {
0486     SET_BIT(hotfdec->Instance->ICR, OTFDEC_ICR_SEIF);
0487     hotfdec->ErrorCode |= HAL_OTFDEC_SECURITY_ERROR;
0488   }
0489   if ((isr_reg & OTFDEC_ISR_XONEIF) == OTFDEC_ISR_XONEIF)
0490   {
0491     SET_BIT(hotfdec->Instance->ICR, OTFDEC_ICR_XONEIF);
0492     hotfdec->ErrorCode |= HAL_OTFDEC_EXECUTE_ERROR;
0493   }
0494   if ((isr_reg & OTFDEC_ISR_KEIF) == OTFDEC_ISR_KEIF)
0495   {
0496     SET_BIT(hotfdec->Instance->ICR, OTFDEC_ICR_KEIF);
0497     hotfdec->ErrorCode |= HAL_OTFDEC_KEY_ERROR;
0498   }
0499 
0500 #if (USE_HAL_OTFDEC_REGISTER_CALLBACKS == 1)
0501   hotfdec->ErrorCallback(hotfdec);
0502 #else
0503   HAL_OTFDEC_ErrorCallback(hotfdec);
0504 #endif /* USE_HAL_OTFDEC_REGISTER_CALLBACKS */
0505 }
0506 
0507 /**
0508   * @brief OTFDEC error callback.
0509   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
0510   *         the configuration information for OTFDEC module
0511   * @retval None
0512   */
0513 __weak void HAL_OTFDEC_ErrorCallback(OTFDEC_HandleTypeDef *hotfdec)
0514 {
0515   /* Prevent unused argument(s) compilation warning */
0516   UNUSED(hotfdec);
0517 
0518   /* NOTE : This function should not be modified; when the callback is needed,
0519             the HAL_OTFDEC_ErrorCallback can be implemented in the user file.
0520    */
0521 }
0522 
0523 /**
0524   * @}
0525   */
0526 
0527 
0528 
0529 
0530 /** @defgroup OTFDEC_Exported_Functions_Group3 Peripheral Control functions
0531   * @ingroup RTEMSBSPsARMSTM32H7
0532   *  @brief   Peripheral control functions.
0533   *
0534 @verbatim
0535   ==============================================================================
0536                       ##### Peripheral Control functions #####
0537   ==============================================================================
0538     [..]
0539     This subsection permits to configure the OTFDEC peripheral
0540 
0541 @endverbatim
0542   * @{
0543   */
0544 
0545 /**
0546   * @brief  Lock region keys.
0547   * @note   Writes to this region KEYRx registers are ignored until next OTFDEC reset.
0548   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
0549   *         the configuration information for OTFDEC module
0550   * @param  RegionIndex index of region the keys of which are locked
0551   * @retval HAL state
0552   */
0553 HAL_StatusTypeDef HAL_OTFDEC_RegionKeyLock(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex)
0554 {
0555   OTFDEC_Region_TypeDef *region;
0556   uint32_t address;
0557 
0558   /* Check the parameters */
0559   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
0560   assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
0561 
0562   /* Take Lock */
0563   __HAL_LOCK(hotfdec);
0564 
0565   address = (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
0566   region = (OTFDEC_Region_TypeDef *)address;
0567 
0568   SET_BIT(region->REG_CONFIGR, OTFDEC_REG_CONFIGR_KEYLOCK);
0569 
0570   /* Release Lock */
0571   __HAL_UNLOCK(hotfdec);
0572 
0573   /* Status is okay */
0574   return HAL_OK;
0575 }
0576 
0577 /**
0578   * @brief  Set region keys.
0579   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
0580   *         the configuration information for OTFDEC module
0581   * @param  RegionIndex index of region the keys of which are set
0582   * @param  pKey pointer at set of keys
0583   * @note   The API reads the key CRC computed by the peripheral and compares it with that
0584   *         theoretically expected. An error is reported if they are different.
0585   * @retval HAL state
0586   */
0587 HAL_StatusTypeDef HAL_OTFDEC_RegionSetKey(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex, uint32_t *pKey)
0588 {
0589   OTFDEC_Region_TypeDef *region;
0590   uint32_t address;
0591 
0592   /* Check the parameters */
0593   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
0594   assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
0595 
0596   if (pKey == NULL)
0597   {
0598     return HAL_ERROR;
0599   }
0600   else
0601   {
0602     /* Take Lock */
0603     __HAL_LOCK(hotfdec);
0604 
0605     address = (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
0606     region = (OTFDEC_Region_TypeDef *)address;
0607 
0608     /* Set Key */
0609     WRITE_REG(region->REG_KEYR0, pKey[0]);
0610 
0611     __DSB();
0612     __ISB();
0613 
0614     WRITE_REG(region->REG_KEYR1, pKey[1]);
0615 
0616     __DSB();
0617     __ISB();
0618 
0619     WRITE_REG(region->REG_KEYR2, pKey[2]);
0620 
0621     __DSB();
0622     __ISB();
0623 
0624     WRITE_REG(region->REG_KEYR3, pKey[3]);
0625 
0626     /* Compute theoretically expected CRC and compare it with that reported by the peripheral */
0627     if (HAL_OTFDEC_KeyCRCComputation(pKey) != HAL_OTFDEC_RegionGetKeyCRC(hotfdec, RegionIndex))
0628     {
0629       /* Release Lock */
0630       __HAL_UNLOCK(hotfdec);
0631 
0632       /* Status is okay */
0633       return HAL_ERROR;
0634     }
0635 
0636     /* Release Lock */
0637     __HAL_UNLOCK(hotfdec);
0638 
0639     /* Status is okay */
0640     return HAL_OK;
0641   }
0642 }
0643 
0644 /**
0645   * @brief  Set region mode.
0646   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
0647   *         the configuration information for OTFDEC module
0648   * @param  RegionIndex index of region the mode of which is set
0649   * @param  mode This parameter can be only:
0650   *           @arg @ref OTFDEC_REG_MODE_INSTRUCTION_ACCESSES_ONLY
0651                         Only instruction accesses are decrypted
0652   *           @arg @ref OTFDEC_REG_MODE_DATA_ACCESSES_ONLY
0653                         Only data accesses are decrypted
0654   *           @arg @ref OTFDEC_REG_MODE_INSTRUCTION_OR_DATA_ACCESSES
0655                         All read accesses are decrypted (instruction or data)
0656   *           @arg @ref OTFDEC_REG_MODE_INSTRUCTION_ACCESSES_ONLY_WITH_CIPHER
0657                         Only instruction accesses are decrypted with proprietary cipher activated
0658   * @retval HAL state
0659   */
0660 HAL_StatusTypeDef HAL_OTFDEC_RegionSetMode(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex, uint32_t mode)
0661 {
0662   OTFDEC_Region_TypeDef *region;
0663   uint32_t address;
0664 
0665   /* Check the parameters */
0666   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
0667   assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
0668   assert_param(IS_OTFDEC_REGION_OPERATING_MODE(mode));
0669 
0670   /* Take Lock */
0671   __HAL_LOCK(hotfdec);
0672 
0673   address = (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
0674   region = (OTFDEC_Region_TypeDef *)address;
0675 
0676   /* Set mode */
0677   MODIFY_REG(region->REG_CONFIGR, OTFDEC_REG_CONFIGR_MODE, mode);
0678 
0679   /* Release Lock */
0680   __HAL_UNLOCK(hotfdec);
0681 
0682   /* Status is okay */
0683   return HAL_OK;
0684 }
0685 
0686 /**
0687   * @brief  Set region configuration.
0688   * @note  Region deciphering is enabled at the end of this function
0689   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
0690   *         the configuration information for OTFDEC module
0691   * @param  RegionIndex index of region that is configured
0692   * @param  Config pointer on structure containing the region configuration parameters
0693   * @param  lock configuration lock enable or disable parameter
0694   *         This parameter can be one of the following values:
0695   *          @arg @ref OTFDEC_REG_CONFIGR_LOCK_DISABLE      OTFDEC region configuration is not locked
0696   *          @arg @ref OTFDEC_REG_CONFIGR_LOCK_ENABLE       OTFDEC region configuration is locked
0697   * @retval HAL state
0698   */
0699 HAL_StatusTypeDef HAL_OTFDEC_RegionConfig(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex,
0700                                           OTFDEC_RegionConfigTypeDef *Config, uint32_t lock)
0701 {
0702   OTFDEC_Region_TypeDef *region;
0703   uint32_t address;
0704 
0705   /* Check the parameters */
0706   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
0707   assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
0708   assert_param(IS_OTFDEC_REGION_CONFIG_LOCK(lock));
0709 
0710   if (Config == NULL)
0711   {
0712     return HAL_ERROR;
0713   }
0714   else
0715   {
0716 
0717     /* Take Lock */
0718     __HAL_LOCK(hotfdec);
0719 
0720     address = (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
0721     region = (OTFDEC_Region_TypeDef *)address;
0722 
0723     /* Set Nonce */
0724     WRITE_REG(region->REG_NONCER0, Config->Nonce[0]);
0725 
0726     WRITE_REG(region->REG_NONCER1, Config->Nonce[1]);
0727 
0728     /* Write region protected area start and end addresses */
0729     WRITE_REG(region->REG_START_ADDR, Config->StartAddress);
0730 
0731     WRITE_REG(region->REG_END_ADDR, Config->EndAddress);
0732 
0733     /* Write Version */
0734     MODIFY_REG(region->REG_CONFIGR, OTFDEC_REG_CONFIGR_VERSION,
0735                (uint32_t)(Config->Version) << OTFDEC_REG_CONFIGR_VERSION_Pos);
0736 
0737     /* Enable region deciphering or enciphering (depending of OTFDEC_CR ENC bit setting) */
0738     SET_BIT(region->REG_CONFIGR, OTFDEC_REG_CONFIGR_REG_ENABLE);
0739 
0740     /* Lock the region configuration according to lock parameter value */
0741     if (lock == OTFDEC_REG_CONFIGR_LOCK_ENABLE)
0742     {
0743       SET_BIT(region->REG_CONFIGR, OTFDEC_REG_CONFIGR_LOCK_ENABLE);
0744     }
0745 
0746     /* Release Lock */
0747     __HAL_UNLOCK(hotfdec);
0748 
0749     /* Status is okay */
0750     return HAL_OK;
0751   }
0752 }
0753 
0754 
0755 /**
0756   * @brief  Compute Key CRC
0757   * @param  pKey pointer at set of keys
0758   * @retval CRC value
0759   */
0760 uint32_t HAL_OTFDEC_KeyCRCComputation(uint32_t *pKey)
0761 {
0762   uint8_t crc7_poly = 0x7;
0763   uint32_t key_strobe[4] = {0xAA55AA55U, 0x3U, 0x18U, 0xC0U};
0764   uint8_t  i;
0765   uint8_t crc = 0;
0766   uint32_t  j;
0767   uint32_t  keyval;
0768   uint32_t  k;
0769   uint32_t *temp = pKey;
0770 
0771   for (j = 0U; j < 4U; j++)
0772   {
0773     keyval = *temp;
0774     temp++;
0775     if (j == 0U)
0776     {
0777       keyval ^= key_strobe[0];
0778     }
0779     else
0780     {
0781       keyval ^= (key_strobe[j] << 24) | ((uint32_t)crc << 16) | (key_strobe[j] << 8) | crc;
0782     }
0783 
0784     crc = 0;
0785     for (i = 0; i < (uint8_t)32; i++)
0786     {
0787       k = ((((uint32_t)crc >> 7) ^ ((keyval >> ((uint8_t)31 - i)) & ((uint8_t)0xF)))) & 1U;
0788       crc <<= 1;
0789       if (k != 0U)
0790       {
0791         crc ^= crc7_poly;
0792       }
0793     }
0794 
0795     crc ^= (uint8_t)0x55;
0796   }
0797 
0798   return (uint32_t) crc;
0799 }
0800 
0801 
0802 /**
0803   * @brief  Enable region deciphering.
0804   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
0805   *         the configuration information for OTFDEC module
0806   * @param  RegionIndex index of region the deciphering is enabled
0807   * @note   An error is reported when the configuration is locked.
0808   * @retval HAL state
0809   */
0810 HAL_StatusTypeDef HAL_OTFDEC_RegionEnable(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex)
0811 {
0812   OTFDEC_Region_TypeDef *region;
0813   uint32_t address;
0814 
0815   /* Check the parameters */
0816   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
0817   assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
0818 
0819   /* Take Lock */
0820   __HAL_LOCK(hotfdec);
0821 
0822   address = (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
0823   region = (OTFDEC_Region_TypeDef *)address;
0824 
0825   if (READ_BIT(region->REG_CONFIGR, OTFDEC_REG_CONFIGR_LOCK_ENABLE) == OTFDEC_REG_CONFIGR_LOCK_ENABLE)
0826   {
0827     /* Configuration is locked, REG_EN bit can't be modified */
0828     __HAL_UNLOCK(hotfdec);
0829 
0830     return HAL_ERROR;
0831   }
0832 
0833   /* Enable region processing */
0834   SET_BIT(region->REG_CONFIGR, OTFDEC_REG_CONFIGR_REG_ENABLE);
0835 
0836   /* Release Lock */
0837   __HAL_UNLOCK(hotfdec);
0838 
0839   /* Status is okay */
0840   return HAL_OK;
0841 }
0842 
0843 /**
0844   * @brief  Disable region deciphering.
0845   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
0846   *         the configuration information for OTFDEC module
0847   * @param  RegionIndex index of region the deciphering is disabled
0848   * @note   An error is reported when the configuration is locked.
0849   * @retval HAL state
0850   */
0851 HAL_StatusTypeDef HAL_OTFDEC_RegionDisable(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex)
0852 {
0853   OTFDEC_Region_TypeDef *region;
0854   uint32_t address;
0855 
0856   /* Check the parameters */
0857   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
0858   assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
0859 
0860   /* Take Lock */
0861   __HAL_LOCK(hotfdec);
0862 
0863   address = (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
0864   region = (OTFDEC_Region_TypeDef *)address;
0865 
0866   if (READ_BIT(region->REG_CONFIGR, OTFDEC_REG_CONFIGR_LOCK_ENABLE) == OTFDEC_REG_CONFIGR_LOCK_ENABLE)
0867   {
0868     /* Configuration is locked, REG_EN bit can't be modified */
0869     __HAL_UNLOCK(hotfdec);
0870 
0871     return HAL_ERROR;
0872   }
0873 
0874   /* Disable region processing */
0875   CLEAR_BIT(region->REG_CONFIGR, OTFDEC_REG_CONFIGR_REG_ENABLE);
0876 
0877   /* Release Lock */
0878   __HAL_UNLOCK(hotfdec);
0879 
0880   /* Status is okay */
0881   return HAL_OK;
0882 }
0883 
0884 /**
0885   * @}
0886   */
0887 
0888 /** @defgroup OTFDEC_Exported_Functions_Group4 Peripheral State and Status functions
0889   * @ingroup RTEMSBSPsARMSTM32H7
0890   *  @brief   Peripheral State functions.
0891   *
0892 @verbatim
0893   ==============================================================================
0894                       ##### Peripheral State functions #####
0895   ==============================================================================
0896     [..]
0897     This subsection permits to get in run-time the status of the peripheral.
0898 
0899 @endverbatim
0900   * @{
0901   */
0902 
0903 /**
0904   * @brief  Return the OTFDEC state.
0905   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
0906   *         the configuration information for OTFDEC module
0907   * @retval HAL state
0908   */
0909 HAL_OTFDEC_StateTypeDef HAL_OTFDEC_GetState(OTFDEC_HandleTypeDef *hotfdec)
0910 {
0911   return hotfdec->State;
0912 }
0913 
0914 
0915 /**
0916   * @brief  Return region keys CRC.
0917   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
0918   *         the configuration information for OTFDEC module
0919   * @param  RegionIndex index of region the keys CRC of which is read
0920   * @retval Key CRC
0921   */
0922 uint32_t HAL_OTFDEC_RegionGetKeyCRC(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex)
0923 {
0924   OTFDEC_Region_TypeDef *region;
0925   uint32_t address;
0926   uint32_t keycrc;
0927 
0928   /* Check the parameters */
0929   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
0930   assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
0931 
0932   address = (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
0933   region = (OTFDEC_Region_TypeDef *)address;
0934 
0935   keycrc = (READ_REG(region->REG_CONFIGR)) & OTFDEC_REG_CONFIGR_KEYCRC;
0936 
0937   keycrc >>= OTFDEC_REG_CONFIGR_KEYCRC_Pos;
0938 
0939   return keycrc;
0940 }
0941 
0942 /**
0943   * @brief  Return region configuration parameters.
0944   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
0945   *         the configuration information for OTFDEC module
0946   * @param  RegionIndex index of region the configuration of which is read
0947   * @param  Config pointer on structure that will be filled up with the region configuration parameters
0948   * @retval HAL state
0949   */
0950 HAL_StatusTypeDef HAL_OTFDEC_RegionGetConfig(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex,
0951                                              OTFDEC_RegionConfigTypeDef *Config)
0952 {
0953   OTFDEC_Region_TypeDef *region;
0954   uint32_t address;
0955 
0956   /* Check the parameters */
0957   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
0958   assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
0959 
0960   if (Config == NULL)
0961   {
0962     return HAL_ERROR;
0963   }
0964   else
0965   {
0966     /* Take Lock */
0967     __HAL_LOCK(hotfdec);
0968 
0969     address = (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
0970     region = (OTFDEC_Region_TypeDef *)address;
0971 
0972     /* Read Nonce */
0973     Config->Nonce[0] = READ_REG(region->REG_NONCER0);
0974     Config->Nonce[1] = READ_REG(region->REG_NONCER1);
0975 
0976     /* Read Addresses */
0977     Config->StartAddress = READ_REG(region->REG_START_ADDR);
0978     Config->EndAddress = READ_REG(region->REG_END_ADDR);
0979 
0980     /* Read Version */
0981     Config->Version = (uint16_t)(READ_REG(region->REG_CONFIGR) &
0982                                  OTFDEC_REG_CONFIGR_VERSION) >> OTFDEC_REG_CONFIGR_VERSION_Pos;
0983 
0984     /* Release Lock */
0985     __HAL_UNLOCK(hotfdec);
0986 
0987     /* Status is okay */
0988     return HAL_OK;
0989   }
0990 }
0991 
0992 
0993 /**
0994   * @}
0995   */
0996 
0997 /**
0998   * @}
0999   */
1000 
1001 #endif /* OTFDEC1 */
1002 
1003 #endif /* HAL_OTFDEC_MODULE_ENABLED */
1004 
1005 
1006 /**
1007   * @}
1008   */
1009 
1010 /**
1011   * @}
1012   */