2 ******************************************************************************
3 * @file stm32h7xx_hal_otfdec.c
4 * @author MCD Application Team
5 * @brief OTFDEC HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the On-The-Fly Decryption (OTFDEC) peripheral:
8 * + Initialization and de-initialization functions
9 * + Region setting/enable functions
10 * + Peripheral State functions
13 ==============================================================================
14 ##### How to use this driver #####
15 ==============================================================================
17 The OTFDEC HAL driver can be used as follows:
19 (#) Declare an OTFDEC_HandleTypeDef handle structure (eg. OTFDEC_HandleTypeDef hotfdec).
21 (#) Initialize the OTFDEC low level resources by implementing the HAL_OTFDEC_MspInit() API:
22 (++) Enable the OTFDEC interface clock.
23 (++) NVIC configuration if interrupts are used
24 (+++) Configure the OTFDEC interrupt priority.
25 (+++) Enable the NVIC OTFDEC IRQ handle.
27 (#) Initialize the OTFDEC peripheral by calling the HAL_OTFDEC_Init() API.
31 (++) Configure the region deciphering mode by calling the HAL_OTFDEC_RegionSetMode() API.
33 (++) Write the region Key by calling the HAL_OTFDEC_RegionSetKey() API. If desired,
34 read the key CRC by calling HAL_OTFDEC_RegionGetKeyCRC() API and compare the
35 result with the theoretically expected CRC.
37 (++) Initialize the OTFDEC region config structure with the Nonce, protected
38 region start and end addresses and firmware version, and wrap-up the region
39 configuration by calling HAL_OTFDEC_RegionConfig() API.
41 (#) At this point, the OTFDEC region configuration is done and the deciphering
42 is enabled. The region can be deciphered on the fly after having made sure
43 the OctoSPI is configured in memory-mapped mode.
46 (@) Warning: the OTFDEC deciphering is based on a different endianness compared
47 to the AES-CTR as implemented in the AES peripheral. E.g., if the OTFEC
48 resorts to the Key (B0, B1, B2, B3) where Bi are 32-bit longwords and B0
49 is the Least Significant Word, the AES has to be configured with the Key
50 (B3, B2, B1, B0) to report the same result (with the same swapping applied
51 to the Initialization Vector).
55 *** Callback registration ***
56 =============================================
59 The compilation flag USE_HAL_OTFDEC_REGISTER_CALLBACKS, when set to 1,
60 allows the user to configure dynamically the driver callbacks.
61 Use Functions @ref HAL_OTFDEC_RegisterCallback()
62 to register an interrupt callback.
65 Function @ref HAL_OTFDEC_RegisterCallback() allows to register following callbacks:
66 (+) ErrorCallback : OTFDEC error callback
67 (+) MspInitCallback : OTFDEC Msp Init callback
68 (+) MspDeInitCallback : OTFDEC Msp DeInit callback
69 This function takes as parameters the HAL peripheral handle, the Callback ID
70 and a pointer to the user callback function.
73 Use function @ref HAL_OTFDEC_UnRegisterCallback to reset a callback to the default
77 @ref HAL_OTFDEC_UnRegisterCallback takes as parameters the HAL peripheral handle,
79 This function allows to reset following callbacks:
80 (+) ErrorCallback : OTFDEC error callback
81 (+) MspInitCallback : OTFDEC Msp Init callback
82 (+) MspDeInitCallback : OTFDEC Msp DeInit callback
85 By default, after the @ref HAL_OTFDEC_Init() and when the state is @ref HAL_OTFDEC_STATE_RESET
86 all callbacks are set to the corresponding weak functions:
87 example @ref HAL_OTFDEC_ErrorCallback().
88 Exception done for MspInit and MspDeInit functions that are
89 reset to the legacy weak functions in the @ref HAL_OTFDEC_Init()/ @ref HAL_OTFDEC_DeInit() only when
90 these callbacks are null (not registered beforehand).
93 If MspInit or MspDeInit are not null, the @ref HAL_OTFDEC_Init()/ @ref HAL_OTFDEC_DeInit()
94 keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
97 Callbacks can be registered/unregistered in @ref HAL_OTFDEC_STATE_READY state only.
98 Exception done MspInit/MspDeInit functions that can be registered/unregistered
99 in @ref HAL_OTFDEC_STATE_READY or @ref HAL_OTFDEC_STATE_RESET state,
100 thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
103 Then, the user first registers the MspInit/MspDeInit user callbacks
104 using @ref HAL_OTFDEC_RegisterCallback() before calling @ref HAL_OTFDEC_DeInit()
105 or @ref HAL_OTFDEC_Init() function.
108 When the compilation flag USE_HAL_OTFDEC_REGISTER_CALLBACKS is set to 0 or
109 not defined, the callback registration feature is not available and all callbacks
110 are set to the corresponding weak functions.
113 ******************************************************************************
116 * <h2><center>© Copyright (c) 2018 STMicroelectronics.
117 * All rights reserved.</center></h2>
119 * This software component is licensed by ST under BSD 3-Clause license,
120 * the "License"; You may not use this file except in compliance with the
121 * License. You may obtain a copy of the License at:
122 * opensource.org/licenses/BSD-3-Clause
124 ******************************************************************************
127 /* Includes ------------------------------------------------------------------*/
128 #include "stm32h7xx_hal.h"
130 /** @addtogroup STM32H7xx_HAL_Driver
134 /** @defgroup OTFDEC OTFDEC
135 * @brief OTFDEC HAL module driver.
140 #ifdef HAL_OTFDEC_MODULE_ENABLED
144 /* Private typedef -----------------------------------------------------------*/
145 /* Private define ------------------------------------------------------------*/
146 /* Private macro -------------------------------------------------------------*/
147 /* Private variables ---------------------------------------------------------*/
148 /* Private function prototypes -----------------------------------------------*/
149 /* Private functions ---------------------------------------------------------*/
153 /* Exported functions --------------------------------------------------------*/
154 /** @addtogroup OTFDEC_Exported_Functions
158 /** @defgroup OTFDEC_Exported_Functions_Group1 Initialization and de-initialization functions
159 * @brief Initialization and Configuration functions.
162 ==============================================================================
163 ##### Initialization and de-initialization functions #####
164 ==============================================================================
171 * @brief Initialize the OTFDEC peripheral and create the associated handle.
172 * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
173 * the configuration information for OTFDEC module
176 HAL_StatusTypeDef
HAL_OTFDEC_Init(OTFDEC_HandleTypeDef
*hotfdec
)
178 /* Check the OTFDEC handle allocation */
184 /* Check the parameters */
185 assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec
->Instance
));
187 if(hotfdec
->State
== HAL_OTFDEC_STATE_RESET
)
189 /* Allocate lock resource and initialize it */
190 __HAL_UNLOCK(hotfdec
);
192 #if (USE_HAL_OTFDEC_REGISTER_CALLBACKS == 1)
193 /* Init the OTFDEC Callback settings */
194 hotfdec
->ErrorCallback
= HAL_OTFDEC_ErrorCallback
; /* Legacy weak callback */
196 if (hotfdec
->MspInitCallback
== NULL
)
198 hotfdec
->MspInitCallback
= HAL_OTFDEC_MspInit
; /* Legacy weak MspInit */
201 /* Init the low level hardware */
202 hotfdec
->MspInitCallback(hotfdec
);
204 /* Init the low level hardware */
205 HAL_OTFDEC_MspInit(hotfdec
);
206 #endif /* USE_HAL_OTFDEC_REGISTER_CALLBACKS */
209 /* Change the OTFDEC state */
210 hotfdec
->State
= HAL_OTFDEC_STATE_READY
;
212 /* Return function status */
217 * @brief DeInitialize the OTFDEC peripheral.
218 * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
219 * the configuration information for OTFDEC module
222 HAL_StatusTypeDef
HAL_OTFDEC_DeInit(OTFDEC_HandleTypeDef
*hotfdec
)
224 /* Check the OTFDEC handle allocation */
230 /* Check the parameters */
231 assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec
->Instance
));
233 /* Change the OTFDEC state */
234 hotfdec
->State
= HAL_OTFDEC_STATE_BUSY
;
236 #if (USE_HAL_OTFDEC_REGISTER_CALLBACKS == 1)
237 if (hotfdec
->MspDeInitCallback
== NULL
)
239 hotfdec
->MspDeInitCallback
= HAL_OTFDEC_MspDeInit
; /* Legacy weak MspDeInit */
242 /* DeInit the low level hardware: CLOCK, NVIC */
243 hotfdec
->MspDeInitCallback(hotfdec
);
245 /* DeInit the low level hardware: CLOCK, NVIC */
246 HAL_OTFDEC_MspDeInit(hotfdec
);
247 #endif /* USE_HAL_OTFDEC_REGISTER_CALLBACKS */
249 /* Change the OTFDEC state */
250 hotfdec
->State
= HAL_OTFDEC_STATE_RESET
;
252 /* Reset OTFDEC error status */
253 hotfdec
->ErrorCode
= HAL_OTFDEC_ERROR_NONE
;
256 __HAL_UNLOCK(hotfdec
);
258 /* Return function status */
263 * @brief Initialize the OTFDEC MSP.
264 * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
265 * the configuration information for OTFDEC module
268 __weak
void HAL_OTFDEC_MspInit(OTFDEC_HandleTypeDef
*hotfdec
)
270 /* Prevent unused argument(s) compilation warning */
273 /* NOTE : This function should not be modified; when the callback is needed,
274 the HAL_OTFDEC_MspInit can be implemented in the user file.
279 * @brief DeInitialize OTFDEC MSP.
280 * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
281 * the configuration information for OTFDEC module
284 __weak
void HAL_OTFDEC_MspDeInit(OTFDEC_HandleTypeDef
*hotfdec
)
286 /* Prevent unused argument(s) compilation warning */
289 /* NOTE : This function should not be modified; when the callback is needed,
290 the HAL_OTFDEC_MspDeInit can be implemented in the user file.
294 #if (USE_HAL_OTFDEC_REGISTER_CALLBACKS == 1)
296 * @brief Register a User OTFDEC Callback
297 * To be used instead of the weak predefined callback
298 * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
299 * the configuration information for OTFDEC module
300 * @param CallbackID ID of the callback to be registered
301 * This parameter can be one of the following values:
302 * @arg @ref HAL_OTFDEC_ERROR_CB_ID OTFDEC error callback ID
303 * @arg @ref HAL_OTFDEC_MSPINIT_CB_ID MspInit callback ID
304 * @arg @ref HAL_OTFDEC_MSPDEINIT_CB_ID MspDeInit callback ID
305 * @param pCallback pointer to the Callback function
308 HAL_StatusTypeDef
HAL_OTFDEC_RegisterCallback(OTFDEC_HandleTypeDef
*hotfdec
, HAL_OTFDEC_CallbackIDTypeDef CallbackID
, pOTFDEC_CallbackTypeDef pCallback
)
310 HAL_StatusTypeDef status
= HAL_OK
;
312 if (pCallback
== NULL
)
314 /* Update the error code */
315 hotfdec
->ErrorCode
|= HAL_OTFDEC_ERROR_INVALID_CALLBACK
;
320 if (hotfdec
->State
== HAL_OTFDEC_STATE_READY
)
324 case HAL_OTFDEC_ERROR_CB_ID
:
325 hotfdec
->ErrorCallback
= pCallback
;
328 case HAL_OTFDEC_MSPINIT_CB_ID
:
329 hotfdec
->MspInitCallback
= pCallback
;
332 case HAL_OTFDEC_MSPDEINIT_CB_ID
:
333 hotfdec
->MspDeInitCallback
= pCallback
;
337 /* Update the error code */
338 hotfdec
->ErrorCode
|= HAL_OTFDEC_ERROR_INVALID_CALLBACK
;
340 /* Return error status */
345 else if (HAL_OTFDEC_STATE_RESET
== hotfdec
->State
)
349 case HAL_OTFDEC_MSPINIT_CB_ID
:
350 hotfdec
->MspInitCallback
= pCallback
;
353 case HAL_OTFDEC_MSPDEINIT_CB_ID
:
354 hotfdec
->MspDeInitCallback
= pCallback
;
358 /* Update the error code */
359 hotfdec
->ErrorCode
|= HAL_OTFDEC_ERROR_INVALID_CALLBACK
;
361 /* Return error status */
368 /* Update the error code */
369 hotfdec
->ErrorCode
|= HAL_OTFDEC_ERROR_INVALID_CALLBACK
;
371 /* Return error status */
379 * @brief Unregister a OTFDEC Callback
380 * OTFDEC callback is redirected to the weak predefined callback
381 * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
382 * the configuration information for OTFDEC module
383 * @param CallbackID ID of the callback to be registered
384 * This parameter can be one of the following values:
385 * @arg @ref HAL_OTFDEC_ERROR_CB_ID OTFDEC error callback ID
386 * @arg @ref HAL_OTFDEC_MSPINIT_CB_ID MspInit callback ID
387 * @arg @ref HAL_OTFDEC_MSPDEINIT_CB_ID MspDeInit callback ID
390 HAL_StatusTypeDef
HAL_OTFDEC_UnRegisterCallback(OTFDEC_HandleTypeDef
*hotfdec
, HAL_OTFDEC_CallbackIDTypeDef CallbackID
)
392 HAL_StatusTypeDef status
= HAL_OK
;
394 if (hotfdec
->State
== HAL_OTFDEC_STATE_READY
)
398 case HAL_OTFDEC_ERROR_CB_ID
:
399 hotfdec
->ErrorCallback
= HAL_OTFDEC_ErrorCallback
;
402 case HAL_OTFDEC_MSPINIT_CB_ID
:
403 hotfdec
->MspInitCallback
= HAL_OTFDEC_MspInit
; /* Legacy weak MspInit */
406 case HAL_OTFDEC_MSPDEINIT_CB_ID
:
407 hotfdec
->MspDeInitCallback
= HAL_OTFDEC_MspDeInit
; /* Legacy weak MspDeInit */
411 /* Update the error code */
412 hotfdec
->ErrorCode
|= HAL_OTFDEC_ERROR_INVALID_CALLBACK
;
414 /* Return error status */
419 else if (HAL_OTFDEC_STATE_RESET
== hotfdec
->State
)
423 case HAL_OTFDEC_MSPINIT_CB_ID
:
424 hotfdec
->MspInitCallback
= HAL_OTFDEC_MspInit
; /* Legacy weak MspInit */
427 case HAL_OTFDEC_MSPDEINIT_CB_ID
:
428 hotfdec
->MspDeInitCallback
= HAL_OTFDEC_MspDeInit
; /* Legacy weak MspDeInit */
432 /* Update the error code */
433 hotfdec
->ErrorCode
|= HAL_OTFDEC_ERROR_INVALID_CALLBACK
;
435 /* Return error status */
442 /* Update the error code */
443 hotfdec
->ErrorCode
|= HAL_OTFDEC_ERROR_INVALID_CALLBACK
;
445 /* Return error status */
452 #endif /* USE_HAL_OTFDEC_REGISTER_CALLBACKS */
458 /** @defgroup OTFDEC_Exported_Functions_Group2 OTFDEC IRQ handler management
459 * @brief OTFDEC IRQ handler.
462 ==============================================================================
463 ##### OTFDEC IRQ handler management #####
464 ==============================================================================
465 [..] This section provides OTFDEC IRQ handler function.
472 * @brief Handle OTFDEC interrupt request.
473 * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
474 * the configuration information for OTFDEC module
477 void HAL_OTFDEC_IRQHandler(OTFDEC_HandleTypeDef
*hotfdec
)
481 isr_reg
= READ_REG(hotfdec
->Instance
->ISR
);
482 if ((isr_reg
& OTFDEC_ISR_SEIF
) == OTFDEC_ISR_SEIF
)
484 SET_BIT( hotfdec
->Instance
->ICR
, OTFDEC_ICR_SEIF
);
485 hotfdec
->ErrorCode
|= HAL_OTFDEC_SECURITY_ERROR
;
487 if ((isr_reg
& OTFDEC_ISR_XONEIF
) == OTFDEC_ISR_XONEIF
)
489 SET_BIT( hotfdec
->Instance
->ICR
, OTFDEC_ICR_XONEIF
);
490 hotfdec
->ErrorCode
|= HAL_OTFDEC_EXECUTE_ERROR
;
492 if ((isr_reg
& OTFDEC_ISR_KEIF
) == OTFDEC_ISR_KEIF
)
494 SET_BIT( hotfdec
->Instance
->ICR
, OTFDEC_ICR_KEIF
);
495 hotfdec
->ErrorCode
|= HAL_OTFDEC_KEY_ERROR
;
498 #if (USE_HAL_OTFDEC_REGISTER_CALLBACKS == 1)
499 hotfdec
->ErrorCallback(hotfdec
);
501 HAL_OTFDEC_ErrorCallback(hotfdec
);
502 #endif /* USE_HAL_OTFDEC_REGISTER_CALLBACKS */
506 * @brief OTFDEC error callback.
507 * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
508 * the configuration information for OTFDEC module
511 __weak
void HAL_OTFDEC_ErrorCallback(OTFDEC_HandleTypeDef
*hotfdec
)
513 /* Prevent unused argument(s) compilation warning */
516 /* NOTE : This function should not be modified; when the callback is needed,
517 the HAL_OTFDEC_ErrorCallback can be implemented in the user file.
528 /** @defgroup OTFDEC_Exported_Functions_Group3 Peripheral Control functions
529 * @brief Peripheral control functions.
532 ==============================================================================
533 ##### Peripheral Control functions #####
534 ==============================================================================
536 This subsection permits to configure the OTFDEC peripheral
543 * @brief Lock region keys.
544 * @note Writes to this region KEYRx registers are ignored until next OTFDEC reset.
545 * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
546 * the configuration information for OTFDEC module
547 * @param RegionIndex index of region the keys of which are locked
550 HAL_StatusTypeDef
HAL_OTFDEC_RegionKeyLock(OTFDEC_HandleTypeDef
*hotfdec
, uint32_t RegionIndex
)
552 OTFDEC_Region_TypeDef
* region
;
555 /* Check the parameters */
556 assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec
->Instance
));
557 assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex
));
562 address
= (uint32_t)(hotfdec
->Instance
) + 0x20U
+ (0x30U
* RegionIndex
);
563 region
= (OTFDEC_Region_TypeDef
*)address
;
565 SET_BIT( region
->REG_CONFIGR
, OTFDEC_REG_CONFIGR_KEYLOCK
);
568 __HAL_UNLOCK(hotfdec
);
575 * @brief Set region keys.
576 * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
577 * the configuration information for OTFDEC module
578 * @param RegionIndex index of region the keys of which are set
579 * @param pKey pointer at set of keys
580 * @note The API reads the key CRC computed by the peripheral and compares it with that
581 * theoretically expected. An error is reported if they are different.
584 HAL_StatusTypeDef
HAL_OTFDEC_RegionSetKey(OTFDEC_HandleTypeDef
*hotfdec
, uint32_t RegionIndex
, uint32_t *pKey
)
586 OTFDEC_Region_TypeDef
* region
;
589 /* Check the parameters */
590 assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec
->Instance
));
591 assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex
));
602 address
= (uint32_t)(hotfdec
->Instance
) + 0x20U
+ (0x30U
* RegionIndex
);
603 region
= (OTFDEC_Region_TypeDef
*)address
;
606 WRITE_REG( region
->REG_KEYR0
, pKey
[0]);
611 WRITE_REG( region
->REG_KEYR1
, pKey
[1]);
616 WRITE_REG( region
->REG_KEYR2
, pKey
[2]);
621 WRITE_REG( region
->REG_KEYR3
, pKey
[3]);
623 /* Compute theoretically expected CRC and compare it with that reported by the peripheral */
624 if (HAL_OTFDEC_KeyCRCComputation(pKey
) != HAL_OTFDEC_RegionGetKeyCRC(hotfdec
, RegionIndex
))
627 __HAL_UNLOCK(hotfdec
);
634 __HAL_UNLOCK(hotfdec
);
642 * @brief Set region mode.
643 * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
644 * the configuration information for OTFDEC module
645 * @param RegionIndex index of region the mode of which is set
646 * @param mode This parameter can be only:
647 * @arg @ref OTFDEC_REG_MODE_INSTRUCTION_ACCESSES_ONLY Only instruction accesses are decrypted
648 * @arg @ref OTFDEC_REG_MODE_DATA_ACCESSES_ONLY Only data accesses are decrypted
649 * @arg @ref OTFDEC_REG_MODE_INSTRUCTION_OR_DATA_ACCESSES All read accesses are decrypted (instruction or data)
650 * @arg @ref OTFDEC_REG_MODE_INSTRUCTION_ACCESSES_ONLY_WITH_CIPHER Only instruction accesses are decrypted with proprietary cipher activated
653 HAL_StatusTypeDef
HAL_OTFDEC_RegionSetMode(OTFDEC_HandleTypeDef
*hotfdec
, uint32_t RegionIndex
, uint32_t mode
)
655 OTFDEC_Region_TypeDef
* region
;
658 /* Check the parameters */
659 assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec
->Instance
));
660 assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex
));
661 assert_param(IS_OTFDEC_REGION_OPERATING_MODE(mode
));
666 address
= (uint32_t)(hotfdec
->Instance
) + 0x20U
+ (0x30U
* RegionIndex
);
667 region
= (OTFDEC_Region_TypeDef
*)address
;
670 MODIFY_REG(region
->REG_CONFIGR
, OTFDEC_REG_CONFIGR_MODE
, mode
);
673 __HAL_UNLOCK(hotfdec
);
680 * @brief Set region configuration.
681 * @note Region deciphering is enabled at the end of this function
682 * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
683 * the configuration information for OTFDEC module
684 * @param RegionIndex index of region that is configured
685 * @param Config pointer on structure containing the region configuration parameters
686 * @param lock configuration lock enable or disable parameter
687 * This parameter can be one of the following values:
688 * @arg @ref OTFDEC_REG_CONFIGR_LOCK_DISABLE OTFDEC region configuration is not locked
689 * @arg @ref OTFDEC_REG_CONFIGR_LOCK_ENABLE OTFDEC region configuration is locked
692 HAL_StatusTypeDef
HAL_OTFDEC_RegionConfig(OTFDEC_HandleTypeDef
*hotfdec
, uint32_t RegionIndex
, OTFDEC_RegionConfigTypeDef
*Config
, uint32_t lock
)
694 OTFDEC_Region_TypeDef
* region
;
697 /* Check the parameters */
698 assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec
->Instance
));
699 assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex
));
700 assert_param(IS_OTFDEC_REGION_CONFIG_LOCK(lock
));
712 address
= (uint32_t)(hotfdec
->Instance
) + 0x20U
+ (0x30U
* RegionIndex
);
713 region
= (OTFDEC_Region_TypeDef
*)address
;
716 WRITE_REG( region
->REG_NONCER0
, Config
->Nonce
[0]);
718 WRITE_REG( region
->REG_NONCER1
, Config
->Nonce
[1]);
720 /* Write region protected area start and end addresses */
721 WRITE_REG( region
->REG_START_ADDR
, Config
->StartAddress
);
723 WRITE_REG( region
->REG_END_ADDR
, Config
->EndAddress
);
726 MODIFY_REG( region
->REG_CONFIGR
, OTFDEC_REG_CONFIGR_VERSION
, (uint32_t)(Config
->Version
) << OTFDEC_REG_CONFIGR_VERSION_Pos
);
728 /* Enable region deciphering or enciphering (depending of OTFDEC_CR ENC bit setting) */
729 SET_BIT( region
->REG_CONFIGR
, OTFDEC_REG_CONFIGR_REG_ENABLE
);
731 /* Lock the region configuration according to lock parameter value */
732 if (lock
== OTFDEC_REG_CONFIGR_LOCK_ENABLE
)
734 SET_BIT( region
->REG_CONFIGR
, OTFDEC_REG_CONFIGR_LOCK_ENABLE
);
738 __HAL_UNLOCK(hotfdec
);
747 * @brief Compute Key CRC
748 * @param pKey pointer at set of keys
751 uint32_t HAL_OTFDEC_KeyCRCComputation(uint32_t *pKey
)
753 uint8_t crc7_poly
= 0x7;
754 uint32_t key_strobe
[4] = {0xAA55AA55U
, 0x3U
, 0x18U
, 0xC0U
};
757 uint32_t j
, keyval
, k
;
758 uint32_t * temp
= pKey
;
760 for (j
= 0U; j
< 4U; j
++)
766 keyval
^= key_strobe
[0];
770 keyval
^= (key_strobe
[j
] << 24) | ((uint32_t)crc
<< 16) | (key_strobe
[j
] << 8) | crc
;
774 for (i
= 0; i
< (uint8_t)32; i
++)
776 k
= ((((uint32_t)crc
>> 7) ^ ((keyval
>> ((uint8_t)31-i
))&((uint8_t)0xF)))) & 1U;
787 return (uint32_t) crc
;
792 * @brief Enable region deciphering.
793 * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
794 * the configuration information for OTFDEC module
795 * @param RegionIndex index of region the deciphering is enabled
796 * @note An error is reported when the configuration is locked.
799 HAL_StatusTypeDef
HAL_OTFDEC_RegionEnable(OTFDEC_HandleTypeDef
*hotfdec
, uint32_t RegionIndex
)
801 OTFDEC_Region_TypeDef
* region
;
804 /* Check the parameters */
805 assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec
->Instance
));
806 assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex
));
811 address
= (uint32_t)(hotfdec
->Instance
) + 0x20U
+ (0x30U
* RegionIndex
);
812 region
= (OTFDEC_Region_TypeDef
*)address
;
814 if (READ_BIT( region
->REG_CONFIGR
, OTFDEC_REG_CONFIGR_LOCK_ENABLE
) == OTFDEC_REG_CONFIGR_LOCK_ENABLE
)
816 /* Configuration is locked, REG_EN bit can't be modified */
817 __HAL_UNLOCK(hotfdec
);
822 /* Enable region processing */
823 SET_BIT( region
->REG_CONFIGR
, OTFDEC_REG_CONFIGR_REG_ENABLE
);
826 __HAL_UNLOCK(hotfdec
);
833 * @brief Disable region deciphering.
834 * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
835 * the configuration information for OTFDEC module
836 * @param RegionIndex index of region the deciphering is disabled
837 * @note An error is reported when the configuration is locked.
840 HAL_StatusTypeDef
HAL_OTFDEC_RegionDisable(OTFDEC_HandleTypeDef
*hotfdec
, uint32_t RegionIndex
)
842 OTFDEC_Region_TypeDef
* region
;
845 /* Check the parameters */
846 assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec
->Instance
));
847 assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex
));
852 address
= (uint32_t)(hotfdec
->Instance
) + 0x20U
+ (0x30U
* RegionIndex
);
853 region
= (OTFDEC_Region_TypeDef
*)address
;
855 if (READ_BIT( region
->REG_CONFIGR
, OTFDEC_REG_CONFIGR_LOCK_ENABLE
) == OTFDEC_REG_CONFIGR_LOCK_ENABLE
)
857 /* Configuration is locked, REG_EN bit can't be modified */
858 __HAL_UNLOCK(hotfdec
);
863 /* Disable region processing */
864 CLEAR_BIT( region
->REG_CONFIGR
, OTFDEC_REG_CONFIGR_REG_ENABLE
);
867 __HAL_UNLOCK(hotfdec
);
877 /** @defgroup OTFDEC_Exported_Functions_Group4 Peripheral State and Status functions
878 * @brief Peripheral State functions.
881 ==============================================================================
882 ##### Peripheral State functions #####
883 ==============================================================================
885 This subsection permits to get in run-time the status of the peripheral.
892 * @brief Return the OTFDEC state.
893 * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
894 * the configuration information for OTFDEC module
897 HAL_OTFDEC_StateTypeDef
HAL_OTFDEC_GetState(OTFDEC_HandleTypeDef
*hotfdec
)
899 return hotfdec
->State
;
904 * @brief Return region keys CRC.
905 * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
906 * the configuration information for OTFDEC module
907 * @param RegionIndex index of region the keys CRC of which is read
910 uint32_t HAL_OTFDEC_RegionGetKeyCRC(OTFDEC_HandleTypeDef
*hotfdec
, uint32_t RegionIndex
)
912 OTFDEC_Region_TypeDef
* region
;
916 /* Check the parameters */
917 assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec
->Instance
));
918 assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex
));
920 address
= (uint32_t)(hotfdec
->Instance
) + 0x20U
+ (0x30U
* RegionIndex
);
921 region
= (OTFDEC_Region_TypeDef
*)address
;
923 keycrc
= (READ_REG( region
->REG_CONFIGR
)) & OTFDEC_REG_CONFIGR_KEYCRC
;
925 keycrc
>>= OTFDEC_REG_CONFIGR_KEYCRC_Pos
;
931 * @brief Return region configuration parameters.
932 * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
933 * the configuration information for OTFDEC module
934 * @param RegionIndex index of region the configuration of which is read
935 * @param Config pointer on structure that will be filled up with the region configuration parameters
938 HAL_StatusTypeDef
HAL_OTFDEC_RegionGetConfig(OTFDEC_HandleTypeDef
*hotfdec
, uint32_t RegionIndex
, OTFDEC_RegionConfigTypeDef
*Config
)
940 OTFDEC_Region_TypeDef
* region
;
943 /* Check the parameters */
944 assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec
->Instance
));
945 assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex
));
956 address
= (uint32_t)(hotfdec
->Instance
) + 0x20U
+ (0x30U
* RegionIndex
);
957 region
= (OTFDEC_Region_TypeDef
*)address
;
960 Config
->Nonce
[0] = READ_REG(region
->REG_NONCER0
);
961 Config
->Nonce
[1] = READ_REG(region
->REG_NONCER1
);
964 Config
->StartAddress
= READ_REG(region
->REG_START_ADDR
);
965 Config
->EndAddress
= READ_REG(region
->REG_END_ADDR
);
968 Config
->Version
= (uint16_t)(READ_REG(region
->REG_CONFIGR
) & OTFDEC_REG_CONFIGR_VERSION
) >> OTFDEC_REG_CONFIGR_VERSION_Pos
;
971 __HAL_UNLOCK(hotfdec
);
989 #endif /* HAL_OTFDEC_MODULE_ENABLED */