1 /* USER CODE BEGIN Header */
2 /**
3   ******************************************************************************
4   * @file    hw_pka.c
5   * @author  MCD Application Team
6   * @brief   This file contains the PKA driver for STM32WBA
7   ******************************************************************************
8   * @attention
9   *
10   * Copyright (c) 2024 STMicroelectronics.
11   * All rights reserved.
12   *
13   * This software is licensed under terms that can be found in the LICENSE file
14   * in the root directory of this software component.
15   * If no LICENSE file comes with this software, it is provided AS-IS.
16   *
17   ******************************************************************************
18   */
19 /* USER CODE END Header */
20 
21 #include "app_common.h"
22 #include "stm32wbaxx_ll_bus.h"
23 #include "stm32wbaxx_ll_pka.h"
24 
25 /*****************************************************************************/
26 
27 typedef struct
28 {
29   uint8_t  run;
30 } HW_PKA_VAR_T;
31 
32 /*****************************************************************************/
33 
34 static HW_PKA_VAR_T HW_PKA_var;
35 
36 /*****************************************************************************/
37 
HW_PKA_Enable(void)38 int HW_PKA_Enable( void )
39 {
40   HW_PKA_VAR_T* pv = &HW_PKA_var;
41 
42   /* Test if the driver is not already in use */
43 
44   if ( pv->run )
45   {
46     return FALSE;
47   }
48 
49   pv->run = TRUE;
50 
51   /* Enable the RNG clock as it is needed.
52    * See PKA chapter in IUM: the RNG peripheral must be clocked.
53    */
54   HW_RNG_EnableClock( 2 );
55 
56   UTILS_ENTER_CRITICAL_SECTION( );
57 
58   /* Enable the PKA clock */
59   LL_AHB2_GRP1_EnableClock( LL_AHB2_GRP1_PERIPH_PKA );
60 
61   UTILS_EXIT_CRITICAL_SECTION( );
62 
63   /* Enable the PKA block */
64   LL_PKA_Enable( PKA );
65 
66   /* Wait for PKA initialization OK */
67   while ( !(PKA->SR & PKA_SR_INITOK) );
68 
69   /* Reset any pending flag */
70   SET_BIT(PKA->CLRFR, (PKA_CLRFR_PROCENDFC | PKA_CLRFR_RAMERRFC |
71                        PKA_CLRFR_ADDRERRFC | PKA_CLRFR_OPERRFC));
72 
73   /* Disable the RNG clock as it is no more needed ???
74    */
75   HW_RNG_DisableClock( 2 );
76 
77   return TRUE;
78 }
79 
80 /*****************************************************************************/
81 
HW_PKA_WriteSingleInput(uint32_t index,uint32_t word)82 void HW_PKA_WriteSingleInput( uint32_t index, uint32_t word )
83 {
84   /* Write the single word into PKA RAM */
85 
86   PKA->RAM[index] = word;
87 }
88 
89 /*****************************************************************************/
90 
HW_PKA_WriteOperand(uint32_t index,int size,const uint32_t * in)91 void HW_PKA_WriteOperand( uint32_t index, int size, const uint32_t* in )
92 {
93   uint32_t* pka_ram = (uint32_t*)&PKA->RAM[index];
94 
95   /* Write the input data into PKA RAM */
96 
97   for ( ; size > 0; size-- )
98   {
99     *pka_ram++ = *in++;
100   }
101 
102   /* Write extra zeros into PKA RAM */
103 
104   *pka_ram = 0;
105 }
106 
107 /*****************************************************************************/
108 
HW_PKA_Start(uint32_t mode)109 void HW_PKA_Start( uint32_t mode )
110 {
111   /* Set the configuration */
112   LL_PKA_Config( PKA, mode );
113 
114   /* Start the PKA processing */
115   LL_PKA_ClearFlag_PROCEND( PKA );
116   LL_PKA_Start( PKA );
117 }
118 
119 /*****************************************************************************/
120 
HW_PKA_EndOfOperation(void)121 int HW_PKA_EndOfOperation( void )
122 {
123   /* Return 0 if the processing is still active */
124   return LL_PKA_IsActiveFlag_PROCEND( PKA );
125 }
126 
127 /*****************************************************************************/
128 
HW_PKA_ReadSingleOutput(uint32_t index)129 uint32_t HW_PKA_ReadSingleOutput( uint32_t index )
130 {
131   /* Read a single word from PKA RAM */
132 
133   return PKA->RAM[index];
134 }
135 
136 /*****************************************************************************/
137 
HW_PKA_ReadResult(uint32_t index,int size,uint32_t * out)138 void HW_PKA_ReadResult( uint32_t index, int size, uint32_t* out )
139 {
140   uint32_t* pka_ram = (uint32_t*)&PKA->RAM[index];
141 
142   /* Read from PKA RAM */
143 
144   for ( ; size > 0; size-- )
145   {
146     *out++ = *pka_ram++;
147   }
148 }
149 
150 /*****************************************************************************/
151 
HW_PKA_Disable(void)152 void HW_PKA_Disable( void )
153 {
154   HW_PKA_VAR_T* pv = &HW_PKA_var;
155 
156   if ( pv->run )
157   {
158     /* Disable the PKA block */
159     LL_PKA_Disable( PKA );
160 
161     UTILS_ENTER_CRITICAL_SECTION( );
162 
163     /* Disable the PKA clock */
164     LL_AHB2_GRP1_DisableClock( LL_AHB2_GRP1_PERIPH_PKA );
165 
166     UTILS_EXIT_CRITICAL_SECTION( );
167 
168     pv->run = FALSE;
169   }
170 }
171 
172 /*****************************************************************************/
173