1 /**
2   *****************************************************************************************************
3   * @file    hk32f0xx_divsqrt.h
4   * @brief   hk32f0xx divsqrt  file.
5   *          The file is the unique include file that the application programmer
6     *          is using in the C source code.it is a patch file
7   *****************************************************************************************************
8 **/
9 
10 
11 /* Define to prevent recursive inclusion -------------------------------------*/
12 
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 /* Includes ------------------------------------------------------------------*/
18 
19 #include "hk32f0xx_divsqrt.h"
20 
21 /*******************************************************************************************************
22 * @function: HK_Dvsq_Init
23 * @brief:    使能DVSQ外设时钟
24 ********************************************************************************************************/
HK_Dvsq_Init(void)25 void HK_Dvsq_Init(void)
26 {
27     RCC_AHBPeriph_DVSQ_CLK_Enable();
28 }
29 
30 /*******************************************************************************************************
31 * @function: HK_Dvsq_Divsion
32 * @brief:    DVSQ除法运算
33 * @param:    u32Dividend: 32位无符号被除数
34 * @param:    u32Divisor: 除数
35 * @param:    u32pRemainder: 用于存放余数的数据指针
36 * @param:    u8IsUnsigned: 1-启用无符号除法运算模式
37                            0-启用带符号除法运算模式
38 * @param:    u8IsFastStart: 1-使用除法快速启动模式(硬件检测到除数写入后即刻开始运算)
39 *                           0-不使用除法快速启动模式(由软件启动除法运算)
40 *            FlagStatus定义在"hk32f0xx.h"里面
41 * @return:   商
42 * @note  :   u32开头的变量: 32位无符号型变量          e开头的变量: enum枚举类型
43 *******************************************************************************************************/
HK_Dvsq_Divsion(uint32_t u32Dividend,uint32_t u32Divisor,uint32_t * u32pRemainder,FlagStatus eIsUnsigned,FlagStatus eIsFastStart)44 uint32_t HK_Dvsq_Divsion(uint32_t u32Dividend, uint32_t u32Divisor, uint32_t *u32pRemainder,
45                          FlagStatus eIsUnsigned, FlagStatus eIsFastStart)
46 {
47     DVSQ_Wait();
48 
49     // 是否使用无符号模式进行除法运算
50     if (eIsUnsigned == SET)
51         DVSQ_ConfigDivUnsigned();
52     else
53         DVSQ_ConfigDivSigned();
54 
55     // 是否启用快速启动除法功能
56     if (eIsFastStart == SET)
57     {
58         DVSQ_EnableDivFastStart();
59 
60         DVSQ->DIVIDEND = u32Dividend;
61         DVSQ->DIVISOR  = u32Divisor;
62 
63         DVSQ_Wait();
64 
65         *u32pRemainder = DVSQ->REMAINDER;
66     }
67     else
68     {
69         DVSQ_DisableDivFastStart();
70 
71         DVSQ->DIVIDEND = u32Dividend;
72         DVSQ->DIVISOR  = u32Divisor;
73 
74         DVSQ_StartDivCalc();
75 
76         DVSQ_Wait();
77 
78         *u32pRemainder = DVSQ->REMAINDER;
79     }
80 
81     return DVSQ->RES;
82 }
83 
84 /*******************************************************************************************************
85 * @function: HK_Dvsq_Sqrt
86 * @brief:    DVSQ开方运算
87 * @param:    u32Radicand: 被开方数
88 * @param:    eIsHighPres: 1-启用高精度开方运算模式
89 *                         0-不启用高精度开方运算模式
90 *                         FlagStatus定义在"hk32f0xx.h"里面
91 * @return:   开方结果
92 * @note  :   u32开头的变量: 32位无符号型变量          e开头的变量: enum枚举类型
93 *******************************************************************************************************/
HK_Dvsq_Sqrt(uint32_t u32Radicand,FlagStatus eIsHighPres)94 uint32_t HK_Dvsq_Sqrt(uint32_t u32Radicand, FlagStatus eIsHighPres)
95 {
96     DVSQ_Wait();
97 
98     // 是否使能高精度开方模式
99     if (eIsHighPres == SET)
100         DVSQ_ConfigSqrtPresHigh();
101     else
102         DVSQ_ConfigSqrtPresNormal();
103 
104     DVSQ->RADICAND = u32Radicand;
105 
106     DVSQ_Wait();
107 
108     return DVSQ->RES;
109 }
110 
111