2 ******************************************************************************
3 * @file stm32f4xx_cryp.c
4 * @author MCD Application Team
7 * @brief This file provides firmware functions to manage the following
8 * functionalities of the Cryptographic processor (CRYP) peripheral:
9 * - Initialization and Configuration functions
10 * - Data treatment functions
11 * - Context swapping functions
12 * - DMA interface function
13 * - Interrupts and flags management
17 * ===================================================================
18 * How to use this driver
19 * ===================================================================
20 * 1. Enable the CRYP controller clock using
21 * RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_CRYP, ENABLE); function.
23 * 2. Initialise the CRYP using CRYP_Init(), CRYP_KeyInit() and if
24 * needed CRYP_IVInit().
26 * 3. Flush the IN and OUT FIFOs by using CRYP_FIFOFlush() function.
28 * 4. Enable the CRYP controller using the CRYP_Cmd() function.
30 * 5. If using DMA for Data input and output transfer,
31 * Activate the needed DMA Requests using CRYP_DMACmd() function
33 * 6. If DMA is not used for data transfer, use CRYP_DataIn() and
34 * CRYP_DataOut() functions to enter data to IN FIFO and get result
37 * 7. To control CRYP events you can use one of the following
39 * - Check on CRYP flags using the CRYP_GetFlagStatus() function.
40 * - Use CRYP interrupts through the function CRYP_ITConfig() at
41 * initialization phase and CRYP_GetITStatus() function into
42 * interrupt routines in processing phase.
44 * 8. Save and restore Cryptographic processor context using
45 * CRYP_SaveContext() and CRYP_RestoreContext() functions.
48 * ===================================================================
49 * Procedure to perform an encryption or a decryption
50 * ===================================================================
54 * 1. Initialize the peripheral using CRYP_Init(), CRYP_KeyInit() and
55 * CRYP_IVInit functions:
56 * - Configure the key size (128-, 192- or 256-bit, in the AES only)
57 * - Enter the symmetric key
58 * - Configure the data type
59 * - In case of decryption in AES-ECB or AES-CBC, you must prepare
60 * the key: configure the key preparation mode. Then Enable the CRYP
61 * peripheral using CRYP_Cmd() function: the BUSY flag is set.
62 * Wait until BUSY flag is reset : the key is prepared for decryption
63 * - Configure the algorithm and chaining (the DES/TDES in ECB/CBC, the
65 * - Configure the direction (encryption/decryption).
66 * - Write the initialization vectors (in CBC or CTR modes only)
68 * 2. Flush the IN and OUT FIFOs using the CRYP_FIFOFlush() function
71 * Basic Processing mode (polling mode)
72 * ====================================
73 * 1. Enable the cryptographic processor using CRYP_Cmd() function.
75 * 2. Write the first blocks in the input FIFO (2 to 8 words) using
76 * CRYP_DataIn() function.
78 * 3. Repeat the following sequence until the complete message has been
81 * a) Wait for flag CRYP_FLAG_OFNE occurs (using CRYP_GetFlagStatus()
82 * function), then read the OUT-FIFO using CRYP_DataOut() function
83 * (1 block or until the FIFO is empty)
85 * b) Wait for flag CRYP_FLAG_IFNF occurs, (using CRYP_GetFlagStatus()
86 * function then write the IN FIFO using CRYP_DataIn() function
87 * (1 block or until the FIFO is full)
89 * 4. At the end of the processing, CRYP_FLAG_BUSY flag will be reset and
90 * both FIFOs are empty (CRYP_FLAG_IFEM is set and CRYP_FLAG_OFNE is
91 * reset). You can disable the peripheral using CRYP_Cmd() function.
93 * Interrupts Processing mode
94 * ===========================
95 * In this mode, Processing is done when the data are transferred by the
96 * CPU during interrupts.
98 * 1. Enable the interrupts CRYP_IT_INI and CRYP_IT_OUTI using
99 * CRYP_ITConfig() function.
101 * 2. Enable the cryptographic processor using CRYP_Cmd() function.
103 * 3. In the CRYP_IT_INI interrupt handler : load the input message into the
104 * IN FIFO using CRYP_DataIn() function . You can load 2 or 4 words at a
105 * time, or load data until the IN FIFO is full. When the last word of
106 * the message has been entered into the IN FIFO, disable the CRYP_IT_INI
107 * interrupt (using CRYP_ITConfig() function).
109 * 4. In the CRYP_IT_OUTI interrupt handler : read the output message from
110 * the OUT FIFO using CRYP_DataOut() function. You can read 1 block (2 or
111 * 4 words) at a time or read data until the FIFO is empty.
112 * When the last word has been read, INIM=0, BUSY=0 and both FIFOs are
113 * empty (CRYP_FLAG_IFEM is set and CRYP_FLAG_OFNE is reset).
114 * You can disable the CRYP_IT_OUTI interrupt (using CRYP_ITConfig()
115 * function) and you can disable the peripheral using CRYP_Cmd() function.
117 * DMA Processing mode
118 * ====================
119 * In this mode, Processing is done when the DMA is used to transfer the
120 * data from/to the memory.
122 * 1. Configure the DMA controller to transfer the input data from the
123 * memory using DMA_Init() function.
124 * The transfer length is the length of the message.
125 * As message padding is not managed by the peripheral, the message
126 * length must be an entire number of blocks. The data are transferred
127 * in burst mode. The burst length is 4 words in the AES and 2 or 4
128 * words in the DES/TDES. The DMA should be configured to set an
129 * interrupt on transfer completion of the output data to indicate that
130 * the processing is finished.
131 * Refer to DMA peripheral driver for more details.
133 * 2. Enable the cryptographic processor using CRYP_Cmd() function.
134 * Enable the DMA requests CRYP_DMAReq_DataIN and CRYP_DMAReq_DataOUT
135 * using CRYP_DMACmd() function.
137 * 3. All the transfers and processing are managed by the DMA and the
138 * cryptographic processor. The DMA transfer complete interrupt indicates
139 * that the processing is complete. Both FIFOs are normally empty and
140 * CRYP_FLAG_BUSY flag is reset.
144 ******************************************************************************
147 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
148 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
149 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
150 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
151 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
152 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
154 * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
155 ******************************************************************************
158 /* Includes ------------------------------------------------------------------*/
159 #include "stm32f4xx_cryp.h"
160 #include "stm32f4xx_rcc.h"
162 /** @addtogroup STM32F4xx_StdPeriph_Driver
167 * @brief CRYP driver modules
171 /* Private typedef -----------------------------------------------------------*/
172 /* Private define ------------------------------------------------------------*/
173 #define FLAG_MASK ((uint8_t)0x20)
174 #define MAX_TIMEOUT ((uint16_t)0xFFFF)
176 /* Private macro -------------------------------------------------------------*/
177 /* Private variables ---------------------------------------------------------*/
178 /* Private function prototypes -----------------------------------------------*/
179 /* Private functions ---------------------------------------------------------*/
181 /** @defgroup CRYP_Private_Functions
185 /** @defgroup CRYP_Group1 Initialization and Configuration functions
186 * @brief Initialization and Configuration functions
189 ===============================================================================
190 Initialization and Configuration functions
191 ===============================================================================
192 This section provides functions allowing to
193 - Initialize the cryptographic Processor using CRYP_Init() function
195 - mode : TDES-ECB, TDES-CBC,
197 AES-ECB, AES-CBC, AES-CTR, AES-Key
198 - DataType : 32-bit data, 16-bit data, bit data or bit-string
199 - Key Size (only in AES modes)
200 - Configure the Encrypt or Decrypt Key using CRYP_KeyInit() function
201 - Configure the Initialization Vectors(IV) for CBC and CTR modes using
202 CRYP_IVInit() function.
203 - Flushes the IN and OUT FIFOs : using CRYP_FIFOFlush() function.
204 - Enable or disable the CRYP Processor using CRYP_Cmd() function
211 * @brief Deinitializes the CRYP peripheral registers to their default reset values
215 void CRYP_DeInit(void)
217 /* Enable CRYP reset state */
218 RCC_AHB2PeriphResetCmd(RCC_AHB2Periph_CRYP, ENABLE);
220 /* Release CRYP from reset state */
221 RCC_AHB2PeriphResetCmd(RCC_AHB2Periph_CRYP, DISABLE);
225 * @brief Initializes the CRYP peripheral according to the specified parameters
226 * in the CRYP_InitStruct.
227 * @param CRYP_InitStruct: pointer to a CRYP_InitTypeDef structure that contains
228 * the configuration information for the CRYP peripheral.
231 void CRYP_Init(CRYP_InitTypeDef* CRYP_InitStruct)
233 /* Check the parameters */
234 assert_param(IS_CRYP_ALGOMODE(CRYP_InitStruct->CRYP_AlgoMode));
235 assert_param(IS_CRYP_DATATYPE(CRYP_InitStruct->CRYP_DataType));
236 assert_param(IS_CRYP_ALGODIR(CRYP_InitStruct->CRYP_AlgoDir));
238 /* Select Algorithm mode*/
239 CRYP->CR &= ~CRYP_CR_ALGOMODE;
240 CRYP->CR |= CRYP_InitStruct->CRYP_AlgoMode;
242 /* Select dataType */
243 CRYP->CR &= ~CRYP_CR_DATATYPE;
244 CRYP->CR |= CRYP_InitStruct->CRYP_DataType;
246 /* select Key size (used only with AES algorithm) */
247 if ((CRYP_InitStruct->CRYP_AlgoMode == CRYP_AlgoMode_AES_ECB) ||
248 (CRYP_InitStruct->CRYP_AlgoMode == CRYP_AlgoMode_AES_CBC) ||
249 (CRYP_InitStruct->CRYP_AlgoMode == CRYP_AlgoMode_AES_CTR) ||
250 (CRYP_InitStruct->CRYP_AlgoMode == CRYP_AlgoMode_AES_Key))
252 assert_param(IS_CRYP_KEYSIZE(CRYP_InitStruct->CRYP_KeySize));
253 CRYP->CR &= ~CRYP_CR_KEYSIZE;
254 CRYP->CR |= CRYP_InitStruct->CRYP_KeySize; /* Key size and value must be
255 configured once the key has
259 /* Select data Direction */
260 CRYP->CR &= ~CRYP_CR_ALGODIR;
261 CRYP->CR |= CRYP_InitStruct->CRYP_AlgoDir;
265 * @brief Fills each CRYP_InitStruct member with its default value.
266 * @param CRYP_InitStruct: pointer to a CRYP_InitTypeDef structure which will
270 void CRYP_StructInit(CRYP_InitTypeDef* CRYP_InitStruct)
272 /* Initialize the CRYP_AlgoDir member */
273 CRYP_InitStruct->CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
275 /* initialize the CRYP_AlgoMode member */
276 CRYP_InitStruct->CRYP_AlgoMode = CRYP_AlgoMode_TDES_ECB;
278 /* initialize the CRYP_DataType member */
279 CRYP_InitStruct->CRYP_DataType = CRYP_DataType_32b;
281 /* Initialize the CRYP_KeySize member */
282 CRYP_InitStruct->CRYP_KeySize = CRYP_KeySize_128b;
286 * @brief Initializes the CRYP Keys according to the specified parameters in
287 * the CRYP_KeyInitStruct.
288 * @param CRYP_KeyInitStruct: pointer to a CRYP_KeyInitTypeDef structure that
289 * contains the configuration information for the CRYP Keys.
292 void CRYP_KeyInit(CRYP_KeyInitTypeDef* CRYP_KeyInitStruct)
294 /* Key Initialisation */
295 CRYP->K0LR = CRYP_KeyInitStruct->CRYP_Key0Left;
296 CRYP->K0RR = CRYP_KeyInitStruct->CRYP_Key0Right;
297 CRYP->K1LR = CRYP_KeyInitStruct->CRYP_Key1Left;
298 CRYP->K1RR = CRYP_KeyInitStruct->CRYP_Key1Right;
299 CRYP->K2LR = CRYP_KeyInitStruct->CRYP_Key2Left;
300 CRYP->K2RR = CRYP_KeyInitStruct->CRYP_Key2Right;
301 CRYP->K3LR = CRYP_KeyInitStruct->CRYP_Key3Left;
302 CRYP->K3RR = CRYP_KeyInitStruct->CRYP_Key3Right;
306 * @brief Fills each CRYP_KeyInitStruct member with its default value.
307 * @param CRYP_KeyInitStruct: pointer to a CRYP_KeyInitTypeDef structure
308 * which will be initialized.
311 void CRYP_KeyStructInit(CRYP_KeyInitTypeDef* CRYP_KeyInitStruct)
313 CRYP_KeyInitStruct->CRYP_Key0Left = 0;
314 CRYP_KeyInitStruct->CRYP_Key0Right = 0;
315 CRYP_KeyInitStruct->CRYP_Key1Left = 0;
316 CRYP_KeyInitStruct->CRYP_Key1Right = 0;
317 CRYP_KeyInitStruct->CRYP_Key2Left = 0;
318 CRYP_KeyInitStruct->CRYP_Key2Right = 0;
319 CRYP_KeyInitStruct->CRYP_Key3Left = 0;
320 CRYP_KeyInitStruct->CRYP_Key3Right = 0;
323 * @brief Initializes the CRYP Initialization Vectors(IV) according to the
324 * specified parameters in the CRYP_IVInitStruct.
325 * @param CRYP_IVInitStruct: pointer to a CRYP_IVInitTypeDef structure that contains
326 * the configuration information for the CRYP Initialization Vectors(IV).
329 void CRYP_IVInit(CRYP_IVInitTypeDef* CRYP_IVInitStruct)
331 CRYP->IV0LR = CRYP_IVInitStruct->CRYP_IV0Left;
332 CRYP->IV0RR = CRYP_IVInitStruct->CRYP_IV0Right;
333 CRYP->IV1LR = CRYP_IVInitStruct->CRYP_IV1Left;
334 CRYP->IV1RR = CRYP_IVInitStruct->CRYP_IV1Right;
338 * @brief Fills each CRYP_IVInitStruct member with its default value.
339 * @param CRYP_IVInitStruct: pointer to a CRYP_IVInitTypeDef Initialization
340 * Vectors(IV) structure which will be initialized.
343 void CRYP_IVStructInit(CRYP_IVInitTypeDef* CRYP_IVInitStruct)
345 CRYP_IVInitStruct->CRYP_IV0Left = 0;
346 CRYP_IVInitStruct->CRYP_IV0Right = 0;
347 CRYP_IVInitStruct->CRYP_IV1Left = 0;
348 CRYP_IVInitStruct->CRYP_IV1Right = 0;
352 * @brief Flushes the IN and OUT FIFOs (that is read and write pointers of the
354 * @note The FIFOs must be flushed only when BUSY flag is reset.
358 void CRYP_FIFOFlush(void)
360 /* Reset the read and write pointers of the FIFOs */
361 CRYP->CR |= CRYP_CR_FFLUSH;
365 * @brief Enables or disables the CRYP peripheral.
366 * @param NewState: new state of the CRYP peripheral.
367 * This parameter can be: ENABLE or DISABLE.
370 void CRYP_Cmd(FunctionalState NewState)
372 /* Check the parameters */
373 assert_param(IS_FUNCTIONAL_STATE(NewState));
375 if (NewState != DISABLE)
377 /* Enable the Cryptographic processor */
378 CRYP->CR |= CRYP_CR_CRYPEN;
382 /* Disable the Cryptographic processor */
383 CRYP->CR &= ~CRYP_CR_CRYPEN;
390 /** @defgroup CRYP_Group2 CRYP Data processing functions
391 * @brief CRYP Data processing functions
394 ===============================================================================
395 CRYP Data processing functions
396 ===============================================================================
397 This section provides functions allowing the encryption and decryption
399 - Enter data to be treated in the IN FIFO : using CRYP_DataIn() function.
400 - Get the data result from the OUT FIFO : using CRYP_DataOut() function.
407 * @brief Writes data in the Data Input register (DIN).
408 * @note After the DIN register has been read once or several times,
409 * the FIFO must be flushed (using CRYP_FIFOFlush() function).
410 * @param Data: data to write in Data Input register
413 void CRYP_DataIn(uint32_t Data)
419 * @brief Returns the last data entered into the output FIFO.
421 * @retval Last data entered into the output FIFO.
423 uint32_t CRYP_DataOut(void)
431 /** @defgroup CRYP_Group3 Context swapping functions
432 * @brief Context swapping functions
435 ===============================================================================
436 Context swapping functions
437 ===============================================================================
439 This section provides functions allowing to save and store CRYP Context
441 It is possible to interrupt an encryption/ decryption/ key generation process
442 to perform another processing with a higher priority, and to complete the
443 interrupted process later on, when the higher-priority task is complete. To do
444 so, the context of the interrupted task must be saved from the CRYP registers
445 to memory, and then be restored from memory to the CRYP registers.
447 1. To save the current context, use CRYP_SaveContext() function
448 2. To restore the saved context, use CRYP_RestoreContext() function
456 * @brief Saves the CRYP peripheral Context.
457 * @note This function stops DMA transfer before to save the context. After
458 * restoring the context, you have to enable the DMA again (if the DMA
459 * was previously used).
460 * @param CRYP_ContextSave: pointer to a CRYP_Context structure that contains
461 * the repository for current context.
462 * @param CRYP_KeyInitStruct: pointer to a CRYP_KeyInitTypeDef structure that
463 * contains the configuration information for the CRYP Keys.
466 ErrorStatus CRYP_SaveContext(CRYP_Context* CRYP_ContextSave,
467 CRYP_KeyInitTypeDef* CRYP_KeyInitStruct)
469 __IO uint32_t timeout = 0;
470 uint32_t ckeckmask = 0, bitstatus;
471 ErrorStatus status = ERROR;
473 /* Stop DMA transfers on the IN FIFO by clearing the DIEN bit in the CRYP_DMACR */
474 CRYP->DMACR &= ~(uint32_t)CRYP_DMACR_DIEN;
476 /* Wait until both the IN and OUT FIFOs are empty
477 (IFEM=1 and OFNE=0 in the CRYP_SR register) and the
478 BUSY bit is cleared. */
480 if ((CRYP->CR & (uint32_t)(CRYP_CR_ALGOMODE_TDES_ECB | CRYP_CR_ALGOMODE_TDES_CBC)) != (uint32_t)0 )/* TDES */
482 ckeckmask = CRYP_SR_IFEM | CRYP_SR_BUSY ;
484 else /* AES or DES */
486 ckeckmask = CRYP_SR_IFEM | CRYP_SR_BUSY | CRYP_SR_OFNE;
491 bitstatus = CRYP->SR & ckeckmask;
494 while ((timeout != MAX_TIMEOUT) && (bitstatus != CRYP_SR_IFEM));
496 if ((CRYP->SR & ckeckmask) != CRYP_SR_IFEM)
502 /* Stop DMA transfers on the OUT FIFO by
503 - writing the DOEN bit to 0 in the CRYP_DMACR register
504 - and clear the CRYPEN bit. */
506 CRYP->DMACR &= ~(uint32_t)CRYP_DMACR_DOEN;
507 CRYP->CR &= ~(uint32_t)CRYP_CR_CRYPEN;
509 /* Save the current configuration (bits [9:2] in the CRYP_CR register) */
510 CRYP_ContextSave->CR_bits9to2 = CRYP->CR & (CRYP_CR_KEYSIZE |
515 /* and, if not in ECB mode, the initialization vectors. */
516 CRYP_ContextSave->CRYP_IV0LR = CRYP->IV0LR;
517 CRYP_ContextSave->CRYP_IV0RR = CRYP->IV0RR;
518 CRYP_ContextSave->CRYP_IV1LR = CRYP->IV1LR;
519 CRYP_ContextSave->CRYP_IV1RR = CRYP->IV1RR;
521 /* save The key value */
522 CRYP_ContextSave->CRYP_K0LR = CRYP_KeyInitStruct->CRYP_Key0Left;
523 CRYP_ContextSave->CRYP_K0RR = CRYP_KeyInitStruct->CRYP_Key0Right;
524 CRYP_ContextSave->CRYP_K1LR = CRYP_KeyInitStruct->CRYP_Key1Left;
525 CRYP_ContextSave->CRYP_K1RR = CRYP_KeyInitStruct->CRYP_Key1Right;
526 CRYP_ContextSave->CRYP_K2LR = CRYP_KeyInitStruct->CRYP_Key2Left;
527 CRYP_ContextSave->CRYP_K2RR = CRYP_KeyInitStruct->CRYP_Key2Right;
528 CRYP_ContextSave->CRYP_K3LR = CRYP_KeyInitStruct->CRYP_Key3Left;
529 CRYP_ContextSave->CRYP_K3RR = CRYP_KeyInitStruct->CRYP_Key3Right;
531 /* When needed, save the DMA status (pointers for IN and OUT messages,
532 number of remaining bytes, etc.) */
541 * @brief Restores the CRYP peripheral Context.
542 * @note Since teh DMA transfer is stopped in CRYP_SaveContext() function,
543 * after restoring the context, you have to enable the DMA again (if the
544 * DMA was previously used).
545 * @param CRYP_ContextRestore: pointer to a CRYP_Context structure that contains
546 * the repository for saved context.
547 * @note The data that were saved during context saving must be rewrited into
551 void CRYP_RestoreContext(CRYP_Context* CRYP_ContextRestore)
554 /* Configure the processor with the saved configuration */
555 CRYP->CR = CRYP_ContextRestore->CR_bits9to2;
557 /* restore The key value */
558 CRYP->K0LR = CRYP_ContextRestore->CRYP_K0LR;
559 CRYP->K0RR = CRYP_ContextRestore->CRYP_K0RR;
560 CRYP->K1LR = CRYP_ContextRestore->CRYP_K1LR;
561 CRYP->K1RR = CRYP_ContextRestore->CRYP_K1RR;
562 CRYP->K2LR = CRYP_ContextRestore->CRYP_K2LR;
563 CRYP->K2RR = CRYP_ContextRestore->CRYP_K2RR;
564 CRYP->K3LR = CRYP_ContextRestore->CRYP_K3LR;
565 CRYP->K3RR = CRYP_ContextRestore->CRYP_K3RR;
567 /* and the initialization vectors. */
568 CRYP->IV0LR = CRYP_ContextRestore->CRYP_IV0LR;
569 CRYP->IV0RR = CRYP_ContextRestore->CRYP_IV0RR;
570 CRYP->IV1LR = CRYP_ContextRestore->CRYP_IV1LR;
571 CRYP->IV1RR = CRYP_ContextRestore->CRYP_IV1RR;
573 /* Enable the cryptographic processor */
574 CRYP->CR |= CRYP_CR_CRYPEN;
580 /** @defgroup CRYP_Group4 CRYP's DMA interface Configuration function
581 * @brief CRYP's DMA interface Configuration function
584 ===============================================================================
585 CRYP's DMA interface Configuration function
586 ===============================================================================
588 This section provides functions allowing to configure the DMA interface for
589 CRYP data input and output transfer.
591 When the DMA mode is enabled (using the CRYP_DMACmd() function), data can be
593 - From memory to the CRYP IN FIFO using the DMA peripheral by enabling
594 the CRYP_DMAReq_DataIN request.
595 - From the CRYP OUT FIFO to the memory using the DMA peripheral by enabling
596 the CRYP_DMAReq_DataOUT request.
603 * @brief Enables or disables the CRYP DMA interface.
604 * @param CRYP_DMAReq: specifies the CRYP DMA transfer request to be enabled or disabled.
605 * This parameter can be any combination of the following values:
606 * @arg CRYP_DMAReq_DataOUT: DMA for outgoing(Tx) data transfer
607 * @arg CRYP_DMAReq_DataIN: DMA for incoming(Rx) data transfer
608 * @param NewState: new state of the selected CRYP DMA transfer request.
609 * This parameter can be: ENABLE or DISABLE.
612 void CRYP_DMACmd(uint8_t CRYP_DMAReq, FunctionalState NewState)
614 /* Check the parameters */
615 assert_param(IS_CRYP_DMAREQ(CRYP_DMAReq));
616 assert_param(IS_FUNCTIONAL_STATE(NewState));
618 if (NewState != DISABLE)
620 /* Enable the selected CRYP DMA request */
621 CRYP->DMACR |= CRYP_DMAReq;
625 /* Disable the selected CRYP DMA request */
626 CRYP->DMACR &= (uint8_t)~CRYP_DMAReq;
633 /** @defgroup CRYP_Group5 Interrupts and flags management functions
634 * @brief Interrupts and flags management functions
637 ===============================================================================
638 Interrupts and flags management functions
639 ===============================================================================
641 This section provides functions allowing to configure the CRYP Interrupts and
642 to get the status and Interrupts pending bits.
644 The CRYP provides 2 Interrupts sources and 7 Flags:
649 1. CRYP_FLAG_IFEM : Set when Input FIFO is empty.
650 This Flag is cleared only by hardware.
652 2. CRYP_FLAG_IFNF : Set when Input FIFO is not full.
653 This Flag is cleared only by hardware.
656 3. CRYP_FLAG_INRIS : Set when Input FIFO Raw interrupt is pending
657 it gives the raw interrupt state prior to masking
658 of the input FIFO service interrupt.
659 This Flag is cleared only by hardware.
661 4. CRYP_FLAG_OFNE : Set when Output FIFO not empty.
662 This Flag is cleared only by hardware.
664 5. CRYP_FLAG_OFFU : Set when Output FIFO is full.
665 This Flag is cleared only by hardware.
667 6. CRYP_FLAG_OUTRIS : Set when Output FIFO Raw interrupt is pending
668 it gives the raw interrupt state prior to masking
669 of the output FIFO service interrupt.
670 This Flag is cleared only by hardware.
672 7. CRYP_FLAG_BUSY : Set when the CRYP core is currently processing a
673 block of data or a key preparation (for AES
675 This Flag is cleared only by hardware.
676 To clear it, the CRYP core must be disabled and the
677 last processing has completed.
682 1. CRYP_IT_INI : The input FIFO service interrupt is asserted when there
683 are less than 4 words in the input FIFO.
684 This interrupt is associated to CRYP_FLAG_INRIS flag.
686 @note This interrupt is cleared by performing write operations
687 to the input FIFO until it holds 4 or more words. The
688 input FIFO service interrupt INMIS is enabled with the
689 CRYP enable bit. Consequently, when CRYP is disabled, the
690 INMIS signal is low even if the input FIFO is empty.
694 2. CRYP_IT_OUTI : The output FIFO service interrupt is asserted when there
695 is one or more (32-bit word) data items in the output FIFO.
696 This interrupt is associated to CRYP_FLAG_OUTRIS flag.
698 @note This interrupt is cleared by reading data from the output
699 FIFO until there is no valid (32-bit) word left (that is,
700 the interrupt follows the state of the OFNE (output FIFO
704 Managing the CRYP controller events :
705 ------------------------------------
706 The user should identify which mode will be used in his application to manage
707 the CRYP controller events: Polling mode or Interrupt mode.
709 1. In the Polling Mode it is advised to use the following functions:
710 - CRYP_GetFlagStatus() : to check if flags events occur.
712 @note The CRYPT flags do not need to be cleared since they are cleared as
713 soon as the associated event are reset.
716 2. In the Interrupt Mode it is advised to use the following functions:
717 - CRYP_ITConfig() : to enable or disable the interrupt source.
718 - CRYP_GetITStatus() : to check if Interrupt occurs.
720 @note The CRYPT interrupts have no pending bits, the interrupt is cleared as
721 soon as the associated event is reset.
728 * @brief Enables or disables the specified CRYP interrupts.
729 * @param CRYP_IT: specifies the CRYP interrupt source to be enabled or disabled.
730 * This parameter can be any combination of the following values:
731 * @arg CRYP_IT_INI: Input FIFO interrupt
732 * @arg CRYP_IT_OUTI: Output FIFO interrupt
733 * @param NewState: new state of the specified CRYP interrupt.
734 * This parameter can be: ENABLE or DISABLE.
737 void CRYP_ITConfig(uint8_t CRYP_IT, FunctionalState NewState)
739 /* Check the parameters */
740 assert_param(IS_CRYP_CONFIG_IT(CRYP_IT));
741 assert_param(IS_FUNCTIONAL_STATE(NewState));
743 if (NewState != DISABLE)
745 /* Enable the selected CRYP interrupt */
746 CRYP->IMSCR |= CRYP_IT;
750 /* Disable the selected CRYP interrupt */
751 CRYP->IMSCR &= (uint8_t)~CRYP_IT;
756 * @brief Checks whether the specified CRYP interrupt has occurred or not.
757 * @note This function checks the status of the masked interrupt (i.e the
758 * interrupt should be previously enabled).
759 * @param CRYP_IT: specifies the CRYP (masked) interrupt source to check.
760 * This parameter can be one of the following values:
761 * @arg CRYP_IT_INI: Input FIFO interrupt
762 * @arg CRYP_IT_OUTI: Output FIFO interrupt
763 * @retval The new state of CRYP_IT (SET or RESET).
765 ITStatus CRYP_GetITStatus(uint8_t CRYP_IT)
767 ITStatus bitstatus = RESET;
768 /* Check the parameters */
769 assert_param(IS_CRYP_GET_IT(CRYP_IT));
771 /* Check the status of the specified CRYP interrupt */
772 if ((CRYP->MISR & CRYP_IT) != (uint8_t)RESET)
779 /* CRYP_IT is reset */
782 /* Return the CRYP_IT status */
787 * @brief Checks whether the specified CRYP flag is set or not.
788 * @param CRYP_FLAG: specifies the CRYP flag to check.
789 * This parameter can be one of the following values:
790 * @arg CRYP_FLAG_IFEM: Input FIFO Empty flag.
791 * @arg CRYP_FLAG_IFNF: Input FIFO Not Full flag.
792 * @arg CRYP_FLAG_OFNE: Output FIFO Not Empty flag.
793 * @arg CRYP_FLAG_OFFU: Output FIFO Full flag.
794 * @arg CRYP_FLAG_BUSY: Busy flag.
795 * @arg CRYP_FLAG_OUTRIS: Output FIFO raw interrupt flag.
796 * @arg CRYP_FLAG_INRIS: Input FIFO raw interrupt flag.
797 * @retval The new state of CRYP_FLAG (SET or RESET).
799 FlagStatus CRYP_GetFlagStatus(uint8_t CRYP_FLAG)
801 FlagStatus bitstatus = RESET;
802 uint32_t tempreg = 0;
804 /* Check the parameters */
805 assert_param(IS_CRYP_GET_FLAG(CRYP_FLAG));
807 /* check if the FLAG is in RISR register */
808 if ((CRYP_FLAG & FLAG_MASK) != 0x00)
810 tempreg = CRYP->RISR;
812 else /* The FLAG is in SR register */
818 /* Check the status of the specified CRYP flag */
819 if ((tempreg & CRYP_FLAG ) != (uint8_t)RESET)
821 /* CRYP_FLAG is set */
826 /* CRYP_FLAG is reset */
830 /* Return the CRYP_FLAG status */
850 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/