5.9 HDIV 使用说明
本章说明 N32G033 标准外设库中硬件除法器(HDIV)相关的文件组成、18 个公开函数,以及 HDIV_AutoStart 1 个示例。除法时序、除零处理与完成标志请配合用户手册的 HDIV 章节阅读,芯片已知问题以勘误手册为准。
5.9.1 相关文件
| 相对路径 | 作用 |
|---|---|
firmware/n32g033_std_periph_driver/inc/n32g033_hdiv.h |
定义 HDIV 标志与数据类型等枚举,并声明 18 个配置、数据读写与结果读取函数 |
firmware/n32g033_std_periph_driver/src/n32g033_hdiv.c |
实现上述 18 个函数:配置总线保持、自动启动与数据类型,读写有符号与无符号的被除数、除数、商与余数,启动单次计算与使能模块 |
projects/n32g033_EVAL/examples/HDIV/ |
HDIV 示例根目录,含 HDIV_AutoStart 1 个示例 |
projects/n32g033_EVAL/examples/HDIV/HDIV_AutoStart/ |
该示例的工程目录组成:MDK-ARM/、EWARM/、inc/、src/、readme.txt |
projects/n32g033_EVAL/bsp/ |
示例共用的板级支持文件:log.h 的串口日志接口 |
5.9.2 API 参考
以下 18 个公开函数按 n32g033_hdiv.h 的声明顺序分为 4 组;每个条目给出功能摘要与 n32g033_hdiv.c 中该函数正上方的注释原文。
配置与状态
HDIV_ConfigBusHold、HDIV_ConfigAutoStart、HDIV_ConfigDataType、HDIV_GetFlagStatus
无符号数据与结果
HDIV_SetUnsignedDIVIDEND_Data、HDIV_GetUnsignedDIVIDEND_Data、HDIV_SetUnsignedDIVISOR_Data、HDIV_GetUnsignedDIVISOR_Data、HDIV_GetUnsignedQUOTIENT_Data、HDIV_GetUnsignedREMAINDER_Data
启动与使能
HDIV_ConfigSingleStart、HDIV_Enable
有符号数据与结果
HDIV_SetSignedDIVIDEND_Data、HDIV_SetSignedDIVISOR_Data、HDIV_GetSignedDIVIDEND_Data、HDIV_GetSignedDIVISOR_Data、HDIV_GetSignedQUOTIENT_Data、HDIV_GetSignedREMAINDER_Data
void HDIV_ConfigBusHold(FunctionalState Cmd)在计算过程中读取结果时使能或关闭总线保持。
name HDIV_ConfigBusHold.
fun Enable or disable the bus hold if read the result when calculating.
param Cmd
- ENABLE
- DISABLE
return none
void HDIV_ConfigAutoStart(FunctionalState Cmd)使能或关闭 HDIV 计算的自动启动。
name HDIV_ConfigAutoStart.
fun Enable or disable the auto start of HDIV calculating.
param Cmd
- ENABLE
- DISABLE
return none
void HDIV_ConfigDataType(FunctionalState Cmd)把被除数的数据类型定义为有符号或无符号。
name HDIV_ConfigDataType.
fun Is the data type of dividend defined as signed or unsigned.
param Cmd
- ENABLE signed data
- DISABLE unsigned data
return none
FlagStatus HDIV_GetFlagStatus(uint32_t HDIV_FLAG)检查指定的 HDIV 标志是否置位。
name HDIV_GetFlagStatus.
fun Check whether the specified HDIV flag is set or not.
param HDIV_FLAG: (This parameter can be one of the following values)
- HDIV_FLAG_DF HDIV done flag
- HDIV_FLAG_DIVBY0 The divisor is 0 flag
return The SQRT calculation status (SET or RESET).
void HDIV_SetUnsignedDIVIDEND_Data(uint32_t data)通过 HDIV 外设设置 32 位无符号被除数。
name HDIV_SetUnsignedDIVIDEND_Data.
fun Set 32 bit unsigned interger dividend data through the HDIV peripheral.
param data
- 32 bit unsigned interger data
return none
uint32_t HDIV_GetUnsignedDIVIDEND_Data(void)通过 HDIV 外设获取 32 位无符号被除数。
name HDIV_GetUnsignedDIVIDEND_Data.
fun Get 32 bit unsigned interger dividend data through the HDIV peripheral.
param none
return 32 bit unsigned interger dividend data
void HDIV_SetUnsignedDIVISOR_Data(uint32_t data)通过 HDIV 外设设置 32 位无符号除数。
name HDIV_SetUnsignedDIVISOR_Data.
fun Set 32 bit unsigned interger divisor data through the HDIV peripheral.
param data
- 32 bit unsigned interger data
return none
uint32_t HDIV_GetUnsignedDIVISOR_Data(void)通过 HDIV 外设获取 32 位无符号除数。
name HDIV_GetUnsignedDIVISOR_Data.
fun Get 32 bit unsigned interger divisor data through the HDIV peripheral.
param none
return 32 bit unsigned interger divisor data
uint32_t HDIV_GetUnsignedQUOTIENT_Data(void)通过 HDIV 外设获取除法器计算出的无符号商。
name HDIV_GetUnsignedQUOTIENT_Data.
fun Get Unsigned Quotient calculated by the divider data through the HDIV peripheral.
param none
return Unsigned Quotient
uint32_t HDIV_GetUnsignedREMAINDER_Data(void)通过 HDIV 外设获取除法器计算出的无符号余数。
name HDIV_GetUnsignedREMAINDER_Data.
fun Get Unsigned Remainder calculated by the divider data through the HDIV peripheral.
param none
return Unsigned Remainder
void HDIV_ConfigSingleStart(void)启动 HDIV 单次计算。
name HDIV_Start.
fun Start the HDIV Single Calculation.
param none
return none
使用提示: 本条注释的 name 写成 HDIV_Start.,与函数名 HDIV_ConfigSingleStart 不一致,声明见 n32g033_hdiv.h 第 92 行。
void HDIV_Enable(FunctionalState Cmd)使能或关闭 HDIV 模块。
name HDIV_Enable.
fun Enable or Disable the HDIV module.
param Cmd
- ENABLE
- DISABLE
return none
void HDIV_SetSignedDIVIDEND_Data(int32_t data)通过 HDIV 外设设置 32 位有符号被除数。
name HDIV_SetSignedDIVIDEND_Data.
fun Set 32 bit signed interger dividend data through the HDIV peripheral.
param data
- 32 bit signed interger data
return none
void HDIV_SetSignedDIVISOR_Data(int32_t data)通过 HDIV 外设设置 32 位有符号除数。
name HDIV_SetSignedDIVISOR_Data.
fun Set 32 bit signed interger divisor data through the HDIV peripheral.
param data
- 32 bit signed interger data
return none
int32_t HDIV_GetSignedDIVIDEND_Data(void)通过 HDIV 外设获取 32 位有符号被除数。
name HDIV_GetSignedDIVIDEND_Data.
fun Get 32 bit signed interger dividend data through the HDIV peripheral.
param None
return 32 bit signed interger dividend data
int32_t HDIV_GetSignedDIVISOR_Data(void)通过 HDIV 外设获取 32 位有符号除数。
name HDIV_GetSignedDIVISOR_Data.
fun Get 32 bit Signed interger divisor data through the HDIV peripheral.
param None
return 32 bit signed interger divisor data
int32_t HDIV_GetSignedQUOTIENT_Data(void)通过 HDIV 外设获取除法器计算出的有符号商。
name HDIV_GetSignedQUOTIENT_Data.
fun Get Signed Quotient calculated by the divider data through the HDIV peripheral.
param None
return Signed Quotient
int32_t HDIV_GetSignedREMAINDER_Data(void)通过 HDIV 外设获取除法器计算出的有符号余数。
name HDIV_GetSignedQUOTIENT_Data.
fun Get Signed Remainder calculated by the divider data through the HDIV peripheral.
param None
return Signed Remainder
使用提示: 本条注释的 name 写成 HDIV_GetSignedQUOTIENT_Data.,与函数名 HDIV_GetSignedREMAINDER_Data 不一致,声明见 n32g033_hdiv.h 第 99 行。
5.9.3 Demo 示例说明
本节说明 HDIV 的 1 个示例:HDIV_AutoStart 以自动启动方式完成一次有符号除法并打印商与余数。
5.9.3.1 HDIV_AutoStart 自动启动除法
工程目录: projects/n32g033_EVAL/examples/HDIV/HDIV_AutoStart/
使能 HDIV 时钟→打开总线保持与自动启动→设为有符号数据类型并使能模块→写入被除数与除数触发计算→读回商和余数并打印
| 项目 | 源码解读 |
|---|---|
| 功能 | 让 HDIV 以自动启动方式完成一次有符号除法,并把商与余数通过串口打印。 |
| 源码组成 | src/main.c 含 main 中的除法配置与结果读取;src/n32g033_it.c 提供 Cortex-M0 异常处理函数骨架;inc/main.h 汇总外设与板级头文件;readme.txt 给出配置与测试步骤;工程文件在 MDK-ARM/ 与 EWARM/。 |
| 关键配置 | HDIV 时钟取 AHB; 总线保持与自动启动均使能,数据类型为有符号; 被除数 -3、除数 2; 串口 115200,TX PA9、RX PA10。 |
| 程序与数据路径 | main → log_init → RCC_EnableAHBPeriphClk 开 HDIV 时钟 → HDIV_ConfigBusHold、HDIV_ConfigAutoStart、HDIV_ConfigDataType、HDIV_Enable 逐项配置 → HDIV_SetSignedDIVIDEND_Data、HDIV_SetSignedDIVISOR_Data 写入操作数 → HDIV_GetSignedQUOTIENT_Data、HDIV_GetSignedREMAINDER_Data 读回结果并打印。 |
| 主要 API | HDIV_ConfigBusHold、HDIV_ConfigAutoStart、HDIV_ConfigDataType、HDIV_Enable、HDIV_SetSignedDIVIDEND_Data、HDIV_SetSignedDIVISOR_Data、HDIV_GetSignedQUOTIENT_Data、HDIV_GetSignedREMAINDER_Data |
| 验证方法 | 编译烧录并复位,串口打印 -3/2 = -1 ,remainder = -1。 |
| 使用提示 | 开发环境为 KEIL MDK-ARM V5.34.0.0 或 IAR EWARM 8.50.1,硬件基于评估板 N32G033K8Q7-1_STB V1.0; 系统时钟 64 MHz。 |
核心代码解读与移植
示例用自动启动方式算一次有符号除法,先配好总线保持、数据类型与使能,再写入操作数触发计算并读回结果。
数据路径: RCC_EnableAHBPeriphClk 开 HDIV 时钟 → HDIV_ConfigBusHold、HDIV_ConfigAutoStart、HDIV_ConfigDataType、HDIV_Enable 完成配置 → 写入被除数与除数触发自动计算 → HDIV_GetSignedQUOTIENT_Data、HDIV_GetSignedREMAINDER_Data 读回商与余数
1. 依次配置总线保持、自动启动与数据类型。 自动启动使能后,写入除数即触发计算,无需手动启动。
/* Hold bus during computation */
HDIV_ConfigBusHold(ENABLE);
/* Enable auto start computation */
HDIV_ConfigAutoStart(ENABLE);
/* Change the parameter to a signed type */
HDIV_ConfigDataType(ENABLE);
/* Enable moudel */
HDIV_Enable(ENABLE);
总线保持使能后,计算期间读结果不会打断运算;不使用自动启动时则要额外调用 HDIV_ConfigSingleStart 手动触发计算。
2. 写入有符号被除数与除数触发计算。 自动启动模式下,写入除数后硬件立即开始除法。
HDIV_SetSignedDIVIDEND_Data(dividend);
HDIV_SetSignedDIVISOR_Data(divisor);
被除数与除数都是 32 位有符号数,移植时要与 HDIV_ConfigDataType 的类型选择保持一致。
3. 读回商与余数并通过串口打印。 结果为 -3 除以 2 的商与余数。
log_info("HDIV test start\r\n");
quotient = HDIV_GetSignedQUOTIENT_Data();
remainder = HDIV_GetSignedREMAINDER_Data();
log_info("-3/2 = %d ,remainder = %d\r\n" ,quotient,remainder);
读取结果前要确认计算已完成,移植时可改查 HDIV_GetFlagStatus 的完成标志,或依赖总线保持避免读取打断运算。
对应源码: src/main.c、inc/main.h、firmware/n32g033_std_periph_driver/inc/n32g033_hdiv.h
5.9.4 注意与排障
| 场景 | 检查与建议 | 来源 |
|---|---|---|
| 使能顺序与完成标志 | 用户手册 HDIV 章写明必须先置位使能位再启动计算,从第二次使用起需查 HDIVDF 完成标志; 打开总线保持后,计算期间读结果不会打断运算。 |
用户手册 |
| 除数为 0 的处理 | 用户手册 HDIV 章写明除数为 0 时置位 DIVBY0 标志并返回 0,该标志在下一次计算时自动清除; 应用需自行判断除数,避免取到无效结果。 |
用户手册 |
| 单次计算与耗时 | 用户手册 HDIV 章写明一次除法耗时 8 个周期,自动启动模式下写入除数即触发计算; 非自动启动时需显式启动单次计算。 |
用户手册 |
| 有符号与无符号数据 | 用户手册 HDIV 章写明电路支持 32 位有符号与无符号除法,数据类型由 HDIV 的数据类型配置选择; 写入与读取数据时要与所选类型一致。 |
用户手册 |
勘误手册 V1.0.0(2025.9.25)针对 A 版芯片,未列出 HDIV 相关条目。