5.9 HDIV Guide
This chapter describes the file layout, the 18 public functions, and the single HDIV_AutoStart example of the hardware divider (HDIV) support in the N32G033 standard peripheral library. Read the HDIV chapter of the user manual for the division timing, divide-by-zero handling, and the done flag, and the errata sheet for known chip issues.
5.9.1 Related files
| Relative path | Purpose |
|---|---|
firmware/n32g033_std_periph_driver/inc/n32g033_hdiv.h |
Defines the HDIV flag and data-type enums, and declares the 18 configuration, data read/write, and result functions |
firmware/n32g033_std_periph_driver/src/n32g033_hdiv.c |
Implements those 18 functions: bus-hold, auto-start, and data-type configuration, signed and unsigned dividend, divisor, quotient, and remainder access, single-start, and module enable |
projects/n32g033_EVAL/examples/HDIV/ |
HDIV example root directory, with 1 example: HDIV_AutoStart |
projects/n32g033_EVAL/examples/HDIV/HDIV_AutoStart/ |
Project layout of this example: MDK-ARM/, EWARM/, inc/, src/, and readme.txt |
projects/n32g033_EVAL/bsp/ |
Board support files shared by the examples: the UART log interface in log.h |
5.9.2 API reference
The following 18 public functions are listed in the declaration order of n32g033_hdiv.h in 4 groups; each entry gives a summary and the original comment above the function in n32g033_hdiv.c.
Configuration and status
HDIV_ConfigBusHold, HDIV_ConfigAutoStart, HDIV_ConfigDataType, HDIV_GetFlagStatus
Unsigned data and results
HDIV_SetUnsignedDIVIDEND_Data, HDIV_GetUnsignedDIVIDEND_Data, HDIV_SetUnsignedDIVISOR_Data, HDIV_GetUnsignedDIVISOR_Data, HDIV_GetUnsignedQUOTIENT_Data, HDIV_GetUnsignedREMAINDER_Data
Start and enable
HDIV_ConfigSingleStart, HDIV_Enable
Signed data and results
HDIV_SetSignedDIVIDEND_Data, HDIV_SetSignedDIVISOR_Data, HDIV_GetSignedDIVIDEND_Data, HDIV_GetSignedDIVISOR_Data, HDIV_GetSignedQUOTIENT_Data, HDIV_GetSignedREMAINDER_Data
void HDIV_ConfigBusHold(FunctionalState Cmd)Enable or disable the bus hold if read the result when calculating.
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)Enable or disable the auto start of HDIV calculating.
name HDIV_ConfigAutoStart.
fun Enable or disable the auto start of HDIV calculating.
param Cmd
- ENABLE
- DISABLE
return none
void HDIV_ConfigDataType(FunctionalState Cmd)Is the data type of dividend defined as signed or unsigned.
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)Check whether the specified HDIV flag is set or not.
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)Set 32 bit unsigned interger dividend data through the HDIV peripheral.
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)Get 32 bit unsigned interger dividend data through the HDIV peripheral.
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)Set 32 bit unsigned interger divisor data through the HDIV peripheral.
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)Get 32 bit unsigned interger divisor data through the HDIV peripheral.
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)Get Unsigned Quotient calculated by the divider data through the HDIV peripheral.
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)Get Unsigned Remainder calculated by the divider data through the HDIV peripheral.
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)Start the HDIV Single Calculation.
name HDIV_Start.
fun Start the HDIV Single Calculation.
param none
return none
Use note: The name field in this comment is written as HDIV_Start., which does not match the function name HDIV_ConfigSingleStart; the declaration is on line 92 of n32g033_hdiv.h.
void HDIV_Enable(FunctionalState Cmd)Enable or Disable the HDIV module.
name HDIV_Enable.
fun Enable or Disable the HDIV module.
param Cmd
- ENABLE
- DISABLE
return none
void HDIV_SetSignedDIVIDEND_Data(int32_t data)Set 32 bit signed interger dividend data through the HDIV peripheral.
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)Set 32 bit signed interger divisor data through the HDIV peripheral.
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)Get 32 bit signed interger dividend data through the HDIV peripheral.
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)Get 32 bit Signed interger divisor data through the HDIV peripheral.
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)Get Signed Quotient calculated by the divider data through the HDIV peripheral.
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)Get Signed Remainder calculated by the divider data through the HDIV peripheral.
name HDIV_GetSignedQUOTIENT_Data.
fun Get Signed Remainder calculated by the divider data through the HDIV peripheral.
param None
return Signed Remainder
Use note: The name field in this comment is written as HDIV_GetSignedQUOTIENT_Data., which does not match the function name HDIV_GetSignedREMAINDER_Data; the declaration is on line 99 of n32g033_hdiv.h.
5.9.3 Demo descriptions
This section describes the single HDIV example: HDIV_AutoStart performs one signed division in auto-start mode and prints the quotient and remainder.
5.9.3.1 HDIV_AutoStart Auto-start division
Example directory: projects/n32g033_EVAL/examples/HDIV/HDIV_AutoStart/
Enable the HDIV clock→Turn on bus hold and auto-start→Select the signed data type and enable the module→Write the dividend and divisor to trigger the calculation→Read back the quotient and remainder and print them
| Item | Source analysis |
|---|---|
| Function | Makes HDIV perform one signed division in auto-start mode and prints the quotient and remainder over the UART. |
| Source files | src/main.c holds the division setup and result read in main;src/n32g033_it.c provides the Cortex-M0 exception handler skeletons;inc/main.h pulls in the peripheral and board headers;readme.txt gives the configuration and test steps;the project files are in MDK-ARM/ and EWARM/. |
| Key configuration | HDIV is clocked from the AHB; bus hold and auto-start are both enabled and the data type is signed; the dividend is -3 and the divisor is 2; UART: 115200 baud, TX PA9 and RX PA10. |
| Program and data path | main -> log_init -> RCC_EnableAHBPeriphClk opens the HDIV clock -> HDIV_ConfigBusHold, HDIV_ConfigAutoStart, HDIV_ConfigDataType, and HDIV_Enable configure the module -> HDIV_SetSignedDIVIDEND_Data and HDIV_SetSignedDIVISOR_Data write the operands -> HDIV_GetSignedQUOTIENT_Data and HDIV_GetSignedREMAINDER_Data read the result back and print it. |
| Main APIs | HDIV_ConfigBusHold, HDIV_ConfigAutoStart, HDIV_ConfigDataType, HDIV_Enable, HDIV_SetSignedDIVIDEND_Data, HDIV_SetSignedDIVISOR_Data, HDIV_GetSignedQUOTIENT_Data, HDIV_GetSignedREMAINDER_Data |
| Verification | Compile, download, and reset; the UART prints -3/2 = -1 ,remainder = -1. |
| Use note | The development environment is KEIL MDK-ARM V5.34.0.0 or IAR EWARM 8.50.1 on the N32G033K8Q7-1_STB V1.0 evaluation board; the system clock is 64 MHz. |
Core-code walkthrough and porting
The example performs one signed division in auto-start mode: it first sets bus hold, data type, and enable, then writes the operands to trigger the calculation and reads the result back.
Data path: RCC_EnableAHBPeriphClk opens the HDIV clock → HDIV_ConfigBusHold, HDIV_ConfigAutoStart, HDIV_ConfigDataType, and HDIV_Enable complete the setup → writing the dividend and divisor triggers the auto-start calculation → HDIV_GetSignedQUOTIENT_Data and HDIV_GetSignedREMAINDER_Data read back the quotient and remainder
1. Configure bus hold, auto-start, and the data type in turn. With auto-start enabled, writing the divisor triggers the calculation without a manual start.
/* 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);
With bus hold enabled, reading the result during a calculation does not interrupt the operation; without auto-start you must call HDIV_ConfigSingleStart to trigger the calculation manually.
2. Write the signed dividend and divisor to trigger the calculation. In auto-start mode the hardware starts the division as soon as the divisor is written.
HDIV_SetSignedDIVIDEND_Data(dividend);
HDIV_SetSignedDIVISOR_Data(divisor);
Both operands are 32-bit signed values, so keep them consistent with the type selected by HDIV_ConfigDataType when porting.
3. Read back the quotient and remainder and print them. The result is the quotient and remainder of -3 divided by 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);
Confirm the calculation has finished before reading the result; when porting you can poll the done flag through HDIV_GetFlagStatus or rely on bus hold to keep the read from interrupting the operation.
Corresponding source files: src/main.c, inc/main.h, firmware/n32g033_std_periph_driver/inc/n32g033_hdiv.h
5.9.4 Notes and troubleshooting
| Scenario | Check & action | Source |
|---|---|---|
| Enable order and the done flag | The HDIV chapter of the user manual states that you must set the enable bit before starting a calculation, and that from the second use on you must check the HDIVDF done flag; with bus hold enabled, reading the result during a calculation does not interrupt the operation. |
User manual |
| Handling a zero divisor | The HDIV chapter of the user manual states that a zero divisor sets the DIVBY0 flag and returns 0, and that the flag clears automatically on the next calculation; the application must check the divisor itself to avoid an invalid result. |
User manual |
| Single calculation and its cost | The HDIV chapter of the user manual states that one division takes 8 cycles and that auto-start triggers the calculation when the divisor is written; without auto-start you must start a single calculation explicitly. |
User manual |
| Signed and unsigned data | The HDIV chapter of the user manual states that the circuit supports 32-bit signed and unsigned division and that the data type is selected by the HDIV data-type configuration; keep the written and read data consistent with the selected type. |
User manual |
Errata sheet V1.0.0 (2025.9.25) covers revision A silicon and lists no HDIV entries.