1 /**
2 *********************************************************************************
3 *
4 * @file ald_calc.c
5 * @brief CALC module driver.
6 *
7 * @version V1.0
8 * @date 04 Dec 2017
9 * @author AE Team
10 * @note
11 *
12 * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
13 *
14 * SPDX-License-Identifier: Apache-2.0
15 *
16 * Licensed under the Apache License, Version 2.0 (the License); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
27 *
28 *********************************************************************************
29 */
30
31 #include "ald_calc.h"
32
33
34 /** @addtogroup ES32FXXX_ALD
35 * @{
36 */
37
38 /** @defgroup CALC CALC
39 * @brief CALC module driver
40 * @{
41 */
42 #ifdef ALD_CALC
43
44 /** @defgroup CALC_Public_Functions CALC Public Functions
45 * @brief Accelerating calculate functions
46 *
47 * @verbatim
48 ==============================================================================
49 ##### Accelerating calculate functions #####
50 ==============================================================================
51 [..] This section provides functions allowing to:
52 (+) Square root operation.
53 (+) Division.
54 (+) Get DZ flag.
55
56 @endverbatim
57 * @{
58 */
59
60 /**
61 * @brief Square root operation.
62 * @param data: The radicand
63 * @retval The value of square root.
64 */
ald_calc_sqrt(uint32_t data)65 uint32_t ald_calc_sqrt(uint32_t data)
66 {
67 WRITE_REG(CALC->RDCND, data);
68 while (READ_BIT(CALC->SQRTSR, CALC_SQRTSR_BUSY_MSK));
69
70 return READ_REG(CALC->SQRTRES);
71 }
72
73 /**
74 * @brief Calculating division.
75 * @param dividend: The value of the dividend.
76 * @param divisor: The value of the divisor.
77 * @param remainder: The value of the remainder.
78 * @retval The result of division.
79 */
ald_calc_div(uint32_t dividend,uint32_t divisor,uint32_t * remainder)80 uint32_t ald_calc_div(uint32_t dividend, uint32_t divisor, uint32_t *remainder)
81 {
82 CLEAR_BIT(CALC->DIVCSR, CALC_DIVCSR_SIGN_MSK);
83 SET_BIT(CALC->DIVCSR, CALC_DIVCSR_TRM_MSK);
84 WRITE_REG(CALC->DIVDR, dividend);
85 WRITE_REG(CALC->DIVSR, divisor);
86
87 while (READ_BIT(CALC->DIVCSR, CALC_DIVCSR_BUSY_MSK));
88
89 *remainder = READ_REG(CALC->DIVRR);
90 return READ_REG(CALC->DIVQR);
91 }
92
93 /**
94 * @brief Calculating division.
95 * @param dividend: The value of the dividend.
96 * @param divisor: The value of the divisor.
97 * @param remainder: The value of the remainder.
98 * @retval The result of division.
99 */
ald_calc_div_sign(int32_t dividend,int32_t divisor,int32_t * remainder)100 int32_t ald_calc_div_sign(int32_t dividend, int32_t divisor, int32_t *remainder)
101 {
102 SET_BIT(CALC->DIVCSR, CALC_DIVCSR_SIGN_MSK);
103 SET_BIT(CALC->DIVCSR, CALC_DIVCSR_TRM_MSK);
104 WRITE_REG(CALC->DIVDR, dividend);
105 WRITE_REG(CALC->DIVSR, divisor);
106
107 while (READ_BIT(CALC->DIVCSR, CALC_DIVCSR_BUSY_MSK));
108
109 *remainder = READ_REG(CALC->DIVRR);
110 return READ_REG(CALC->DIVQR);
111 }
112
113 /**
114 * @brief Get the flag of divisor is zero.
115 * @retval The status, SET/RESET.
116 */
ald_calc_get_dz_status(void)117 flag_status_t ald_calc_get_dz_status(void)
118 {
119 if (READ_BIT(CALC->DIVCSR, CALC_DIVCSR_DZ_MSK))
120 return SET;
121
122 return RESET;
123 }
124
125 /**
126 * @}
127 */
128 #endif /* ALD_CALC */
129 /**
130 * @}
131 */
132
133 /**
134 * @}
135 */
136