Create release.yml
[betaflight.git] / lib / main / STM32H7 / Drivers / STM32H7xx_HAL_Driver / Src / stm32h7xx_hal_otfdec.c
bloba54882ece488c41f4ef0452640f3e1026b1d2bf2
1 /**
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
12 @verbatim
13 ==============================================================================
14 ##### How to use this driver #####
15 ==============================================================================
16 [..]
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.
29 (#) For each region,
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.
45 [..]
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).
53 [..]
55 *** Callback registration ***
56 =============================================
57 [..]
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.
63 [..]
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.
71 [..]
73 Use function @ref HAL_OTFDEC_UnRegisterCallback to reset a callback to the default
74 weak function.
75 [..]
77 @ref HAL_OTFDEC_UnRegisterCallback takes as parameters the HAL peripheral handle,
78 and the Callback ID.
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
83 [..]
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).
91 [..]
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.
95 [..]
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.
101 [..]
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.
106 [..]
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.
112 @endverbatim
113 ******************************************************************************
114 * @attention
116 * <h2><center>&copy; 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
131 * @{
134 /** @defgroup OTFDEC OTFDEC
135 * @brief OTFDEC HAL module driver.
136 * @{
140 #ifdef HAL_OTFDEC_MODULE_ENABLED
142 #if defined(OTFDEC1)
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
155 * @{
158 /** @defgroup OTFDEC_Exported_Functions_Group1 Initialization and de-initialization functions
159 * @brief Initialization and Configuration functions.
161 @verbatim
162 ==============================================================================
163 ##### Initialization and de-initialization functions #####
164 ==============================================================================
166 @endverbatim
167 * @{
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
174 * @retval HAL status
176 HAL_StatusTypeDef HAL_OTFDEC_Init(OTFDEC_HandleTypeDef *hotfdec)
178 /* Check the OTFDEC handle allocation */
179 if(hotfdec == NULL)
181 return HAL_ERROR;
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);
203 #else
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 */
213 return HAL_OK;
217 * @brief DeInitialize the OTFDEC peripheral.
218 * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
219 * the configuration information for OTFDEC module
220 * @retval HAL status
222 HAL_StatusTypeDef HAL_OTFDEC_DeInit(OTFDEC_HandleTypeDef *hotfdec)
224 /* Check the OTFDEC handle allocation */
225 if(hotfdec == NULL)
227 return HAL_ERROR;
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);
244 #else
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;
255 /* Release Lock */
256 __HAL_UNLOCK(hotfdec);
258 /* Return function status */
259 return HAL_OK;
263 * @brief Initialize the OTFDEC MSP.
264 * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
265 * the configuration information for OTFDEC module
266 * @retval None
268 __weak void HAL_OTFDEC_MspInit(OTFDEC_HandleTypeDef *hotfdec)
270 /* Prevent unused argument(s) compilation warning */
271 UNUSED(hotfdec);
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
282 * @retval None
284 __weak void HAL_OTFDEC_MspDeInit(OTFDEC_HandleTypeDef *hotfdec)
286 /* Prevent unused argument(s) compilation warning */
287 UNUSED(hotfdec);
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
306 * @retval HAL status
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;
317 return HAL_ERROR;
320 if (hotfdec->State == HAL_OTFDEC_STATE_READY)
322 switch (CallbackID)
324 case HAL_OTFDEC_ERROR_CB_ID :
325 hotfdec->ErrorCallback = pCallback;
326 break;
328 case HAL_OTFDEC_MSPINIT_CB_ID :
329 hotfdec->MspInitCallback = pCallback;
330 break;
332 case HAL_OTFDEC_MSPDEINIT_CB_ID :
333 hotfdec->MspDeInitCallback = pCallback;
334 break;
336 default :
337 /* Update the error code */
338 hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
340 /* Return error status */
341 status = HAL_ERROR;
342 break;
345 else if (HAL_OTFDEC_STATE_RESET == hotfdec->State)
347 switch (CallbackID)
349 case HAL_OTFDEC_MSPINIT_CB_ID :
350 hotfdec->MspInitCallback = pCallback;
351 break;
353 case HAL_OTFDEC_MSPDEINIT_CB_ID :
354 hotfdec->MspDeInitCallback = pCallback;
355 break;
357 default :
358 /* Update the error code */
359 hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
361 /* Return error status */
362 status = HAL_ERROR;
363 break;
366 else
368 /* Update the error code */
369 hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
371 /* Return error status */
372 status = HAL_ERROR;
375 return 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
388 * @retval HAL status
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)
396 switch (CallbackID)
398 case HAL_OTFDEC_ERROR_CB_ID :
399 hotfdec->ErrorCallback = HAL_OTFDEC_ErrorCallback;
400 break;
402 case HAL_OTFDEC_MSPINIT_CB_ID :
403 hotfdec->MspInitCallback = HAL_OTFDEC_MspInit; /* Legacy weak MspInit */
404 break;
406 case HAL_OTFDEC_MSPDEINIT_CB_ID :
407 hotfdec->MspDeInitCallback = HAL_OTFDEC_MspDeInit; /* Legacy weak MspDeInit */
408 break;
410 default :
411 /* Update the error code */
412 hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
414 /* Return error status */
415 status = HAL_ERROR;
416 break;
419 else if (HAL_OTFDEC_STATE_RESET == hotfdec->State)
421 switch (CallbackID)
423 case HAL_OTFDEC_MSPINIT_CB_ID :
424 hotfdec->MspInitCallback = HAL_OTFDEC_MspInit; /* Legacy weak MspInit */
425 break;
427 case HAL_OTFDEC_MSPDEINIT_CB_ID :
428 hotfdec->MspDeInitCallback = HAL_OTFDEC_MspDeInit; /* Legacy weak MspDeInit */
429 break;
431 default :
432 /* Update the error code */
433 hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
435 /* Return error status */
436 status = HAL_ERROR;
437 break;
440 else
442 /* Update the error code */
443 hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
445 /* Return error status */
446 status = HAL_ERROR;
449 return status;
452 #endif /* USE_HAL_OTFDEC_REGISTER_CALLBACKS */
455 * @}
458 /** @defgroup OTFDEC_Exported_Functions_Group2 OTFDEC IRQ handler management
459 * @brief OTFDEC IRQ handler.
461 @verbatim
462 ==============================================================================
463 ##### OTFDEC IRQ handler management #####
464 ==============================================================================
465 [..] This section provides OTFDEC IRQ handler function.
467 @endverbatim
468 * @{
472 * @brief Handle OTFDEC interrupt request.
473 * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
474 * the configuration information for OTFDEC module
475 * @retval None
477 void HAL_OTFDEC_IRQHandler(OTFDEC_HandleTypeDef *hotfdec)
479 uint32_t isr_reg;
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);
500 #else
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
509 * @retval None
511 __weak void HAL_OTFDEC_ErrorCallback(OTFDEC_HandleTypeDef *hotfdec)
513 /* Prevent unused argument(s) compilation warning */
514 UNUSED(hotfdec);
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.
522 * @}
528 /** @defgroup OTFDEC_Exported_Functions_Group3 Peripheral Control functions
529 * @brief Peripheral control functions.
531 @verbatim
532 ==============================================================================
533 ##### Peripheral Control functions #####
534 ==============================================================================
535 [..]
536 This subsection permits to configure the OTFDEC peripheral
538 @endverbatim
539 * @{
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
548 * @retval HAL state
550 HAL_StatusTypeDef HAL_OTFDEC_RegionKeyLock(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex)
552 OTFDEC_Region_TypeDef * region;
553 uint32_t address;
555 /* Check the parameters */
556 assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
557 assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
559 /* Take Lock */
560 __HAL_LOCK(hotfdec);
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 );
567 /* Release Lock */
568 __HAL_UNLOCK(hotfdec);
570 /* Status is okay */
571 return HAL_OK;
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.
582 * @retval HAL state
584 HAL_StatusTypeDef HAL_OTFDEC_RegionSetKey(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex, uint32_t *pKey)
586 OTFDEC_Region_TypeDef * region;
587 uint32_t address;
589 /* Check the parameters */
590 assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
591 assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
593 if (pKey == NULL)
595 return HAL_ERROR;
597 else
599 /* Take Lock */
600 __HAL_LOCK(hotfdec);
602 address = (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
603 region = (OTFDEC_Region_TypeDef *)address;
605 /* Set Key */
606 WRITE_REG( region->REG_KEYR0, pKey[0]);
608 __DSB();
609 __ISB();
611 WRITE_REG( region->REG_KEYR1, pKey[1]);
613 __DSB();
614 __ISB();
616 WRITE_REG( region->REG_KEYR2, pKey[2]);
618 __DSB();
619 __ISB();
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))
626 /* Release Lock */
627 __HAL_UNLOCK(hotfdec);
629 /* Status is okay */
630 return HAL_ERROR;
633 /* Release Lock */
634 __HAL_UNLOCK(hotfdec);
636 /* Status is okay */
637 return HAL_OK;
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
651 * @retval HAL state
653 HAL_StatusTypeDef HAL_OTFDEC_RegionSetMode(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex, uint32_t mode)
655 OTFDEC_Region_TypeDef * region;
656 uint32_t address;
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));
663 /* Take Lock */
664 __HAL_LOCK(hotfdec);
666 address = (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
667 region = (OTFDEC_Region_TypeDef *)address;
669 /* Set mode */
670 MODIFY_REG(region->REG_CONFIGR, OTFDEC_REG_CONFIGR_MODE, mode);
672 /* Release Lock */
673 __HAL_UNLOCK(hotfdec);
675 /* Status is okay */
676 return HAL_OK;
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
690 * @retval HAL state
692 HAL_StatusTypeDef HAL_OTFDEC_RegionConfig(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex, OTFDEC_RegionConfigTypeDef *Config, uint32_t lock)
694 OTFDEC_Region_TypeDef * region;
695 uint32_t address;
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));
702 if (Config == NULL)
704 return HAL_ERROR;
706 else
709 /* Take Lock */
710 __HAL_LOCK(hotfdec);
712 address = (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
713 region = (OTFDEC_Region_TypeDef *)address;
715 /* Set Nonce */
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);
725 /* Write Version */
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);
737 /* Release Lock */
738 __HAL_UNLOCK(hotfdec);
740 /* Status is okay */
741 return HAL_OK;
747 * @brief Compute Key CRC
748 * @param pKey pointer at set of keys
749 * @retval CRC value
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};
755 uint8_t i;
756 uint8_t crc = 0;
757 uint32_t j, keyval, k;
758 uint32_t * temp = pKey;
760 for (j = 0U; j < 4U; j++)
762 keyval = *temp;
763 temp++;
764 if (j == 0U)
766 keyval ^= key_strobe[0];
768 else
770 keyval ^= (key_strobe[j] << 24) | ((uint32_t)crc << 16) | (key_strobe[j] << 8) | crc;
773 crc = 0;
774 for (i = 0; i < (uint8_t)32; i++)
776 k = ((((uint32_t)crc >> 7) ^ ((keyval >> ((uint8_t)31-i))&((uint8_t)0xF)))) & 1U;
777 crc <<= 1;
778 if (k != 0U)
780 crc ^= crc7_poly;
784 crc^=(uint8_t)0x55;
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.
797 * @retval HAL state
799 HAL_StatusTypeDef HAL_OTFDEC_RegionEnable(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex)
801 OTFDEC_Region_TypeDef * region;
802 uint32_t address;
804 /* Check the parameters */
805 assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
806 assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
808 /* Take Lock */
809 __HAL_LOCK(hotfdec);
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);
819 return HAL_ERROR;
822 /* Enable region processing */
823 SET_BIT( region->REG_CONFIGR, OTFDEC_REG_CONFIGR_REG_ENABLE);
825 /* Release Lock */
826 __HAL_UNLOCK(hotfdec);
828 /* Status is okay */
829 return HAL_OK;
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.
838 * @retval HAL state
840 HAL_StatusTypeDef HAL_OTFDEC_RegionDisable(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex)
842 OTFDEC_Region_TypeDef * region;
843 uint32_t address;
845 /* Check the parameters */
846 assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
847 assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
849 /* Take Lock */
850 __HAL_LOCK(hotfdec);
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);
860 return HAL_ERROR;
863 /* Disable region processing */
864 CLEAR_BIT( region->REG_CONFIGR, OTFDEC_REG_CONFIGR_REG_ENABLE);
866 /* Release Lock */
867 __HAL_UNLOCK(hotfdec);
869 /* Status is okay */
870 return HAL_OK;
874 * @}
877 /** @defgroup OTFDEC_Exported_Functions_Group4 Peripheral State and Status functions
878 * @brief Peripheral State functions.
880 @verbatim
881 ==============================================================================
882 ##### Peripheral State functions #####
883 ==============================================================================
884 [..]
885 This subsection permits to get in run-time the status of the peripheral.
887 @endverbatim
888 * @{
892 * @brief Return the OTFDEC state.
893 * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
894 * the configuration information for OTFDEC module
895 * @retval HAL state
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
908 * @retval Key CRC
910 uint32_t HAL_OTFDEC_RegionGetKeyCRC(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex)
912 OTFDEC_Region_TypeDef * region;
913 uint32_t address;
914 uint32_t keycrc;
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;
927 return keycrc;
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
936 * @retval HAL state
938 HAL_StatusTypeDef HAL_OTFDEC_RegionGetConfig(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex, OTFDEC_RegionConfigTypeDef *Config)
940 OTFDEC_Region_TypeDef * region;
941 uint32_t address;
943 /* Check the parameters */
944 assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
945 assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
947 if (Config == NULL)
949 return HAL_ERROR;
951 else
953 /* Take Lock */
954 __HAL_LOCK(hotfdec);
956 address = (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
957 region = (OTFDEC_Region_TypeDef *)address;
959 /* Read Nonce */
960 Config->Nonce[0] = READ_REG(region->REG_NONCER0);
961 Config->Nonce[1] = READ_REG(region->REG_NONCER1);
963 /* Read Addresses */
964 Config->StartAddress = READ_REG(region->REG_START_ADDR);
965 Config->EndAddress = READ_REG(region->REG_END_ADDR);
967 /* Read Version */
968 Config->Version = (uint16_t)(READ_REG(region->REG_CONFIGR) & OTFDEC_REG_CONFIGR_VERSION) >> OTFDEC_REG_CONFIGR_VERSION_Pos;
970 /* Release Lock */
971 __HAL_UNLOCK(hotfdec);
973 /* Status is okay */
974 return HAL_OK;
980 * @}
984 * @}
987 #endif /* OTFDEC1 */
989 #endif /* HAL_OTFDEC_MODULE_ENABLED */
993 * @}
997 * @}