5.17 SQRT Guide
This chapter describes the file composition and the 8 public functions of the SQRT driver in the N32G033 standard peripheral library, together with the 1 example SQRT_AutoStart. Read the width limit, the calculation timing and the bus-hold principle of the square-root unit together with the “SQRT square-root unit” chapter of the user manual; for known silicon issues, refer to the errata sheet.
5.17.1 Related files
| Relative path | Purpose |
|---|---|
firmware/n32g033_std_periph_driver/inc/n32g033_sqrt.h |
Defines the macros such as the radicand width, and groups the 8 public functions into three declaration sections: module configuration, data read/write and start/enable |
firmware/n32g033_std_periph_driver/src/n32g033_sqrt.c |
Implements the 8 functions above: bus-hold and auto-start configuration, calculation status query, radicand and root read/write, single start and module enable |
projects/n32g033_EVAL/examples/SQRT/ |
SQRT example root directory, containing 1 example: SQRT_AutoStart |
projects/n32g033_EVAL/examples/SQRT/SQRT_AutoStart/ |
Example directory composition: MDK-ARM/, EWARM/, inc/, src/, readme.txt |
projects/n32g033_EVAL/bsp/inc/, projects/n32g033_EVAL/bsp/src/ |
Board-level common files; the example uses log.h for serial logging |
5.17.2 API reference
The 8 public functions below are grouped into 3 groups in the declaration order of n32g033_sqrt.h; each entry gives a function summary and the source comment verbatim above that function in n32g033_sqrt.c.
Module configuration and status
SQRT_ConfigBusHold, SQRT_ConfigAutoStart, SQRT_GetFlagStatus
Data read/write
SQRT_SetRADICAND_Data, SQRT_GetRADICAND_Data, SQRT_GetROOT_Data
Start and enable
SQRT_ConfigSingleStart, SQRT_Enable
void SQRT_ConfigBusHold(FunctionalState Cmd)Enable or disable the bus hold if read the result when calculating of SQRT module.
name SQRT_ConfigBusHold.
fun Enable or disable the bus hold if read the result when calculating of SQRT module.
param Cmd
- ENABLE
- DISABLE
return none
void SQRT_ConfigAutoStart(FunctionalState Cmd)Enable or disable the auto start of SQRT calculating.
name SQRT_ConfigAutoStart.
fun Enable or disable the auto start of SQRT calculating.
param Cmd
- ENABLE
- DISABLE
return none
FlagStatus SQRT_GetFlagStatus(void)Check whether the SQRT calculation is finished or not.
name SQRT_GetFlagStatus.
fun Check whether the SQRT calculation is finished or not.
param None
return The SQRT calculation status (SET or RESET).
void SQRT_SetRADICAND_Data(uint32_t data)Set the radicand of square root calculation.
name SQRT_SetRADICAND_Data.
fun Set the radicand of square root calculation.
param data
- 32 bit unsigned interger data
return none.
uint32_t SQRT_GetRADICAND_Data(void)Get the radicand of square root calculation.
name SQRT_GetRADICAND_Data.
fun Get the radicand of square root calculation.
param none
return 32 bit unsigned interger data.
uint32_t SQRT_GetROOT_Data(void)Get the square root of radicand.
name SQRT_GetROOT_Data.
fun Get the square root of radicand.
param none
return 32 bit unsigned interger data.
void SQRT_ConfigSingleStart(void)Start the SQRT Calculation.
name SQRT_Start.
fun Start the SQRT Calculation.
param none
return none.
Use note: This function is declared at line 71 of n32g033_sqrt.h; the name in the source comment reads SQRT_Start., which differs from the function name SQRT_ConfigSingleStart; the comment is copied verbatim.
void SQRT_Enable(FunctionalState Cmd)Enable or Disable the SQRT module.
name SQRT_Enable.
fun Enable or Disable the SQRT module.
param Cmd
- ENABLE
- DISABLE
return none.
5.17.3 Demo descriptions
This section describes the 1 SQRT example: SQRT_AutoStart enables auto start and writes the radicand 5, then reads the square root back and prints it on the serial port.
5.17.3.1 SQRT_AutoStart auto-start square root
Example directory: projects/n32g033_EVAL/examples/SQRT/SQRT_AutoStart/
Enable the AHB clock of the square-root module→enable the bus hold during calculation→enable auto start and turn on the module→write the radicand to trigger the calculation→read the root back and print it on the serial port
| Item | Source analysis |
|---|---|
| Function | Turn on the SQRT module and enable auto start; after writing the radicand 5 the square root is computed automatically, and the root 2 is read back and printed on the serial port. |
| Source files | src/main.c contains main;inc/main.h gives the peripheral headers;readme.txt gives the configuration and test steps;project files are in MDK-ARM/ and EWARM/. |
| Key configuration | Clock: system clock 64 MHz; bus hold: SQRT_ConfigBusHold(ENABLE);auto start: SQRT_ConfigAutoStart(ENABLE);log port: PA9, baud rate 115200. |
| Program and data path | main -> log_init -> RCC_EnableAHBPeriphClk enables the SQRT clock -> SQRT_ConfigBusHold enables the bus hold -> SQRT_ConfigAutoStart enables auto start -> SQRT_Enable turns on the module -> SQRT_SetRADICAND_Data writes 5 to trigger the calculation -> SQRT_GetROOT_Data reads the root back -> the serial port prints it. |
| Main APIs | SQRT_ConfigBusHold, SQRT_ConfigAutoStart, SQRT_Enable, SQRT_SetRADICAND_Data, SQRT_GetROOT_Data |
| Verification | After building, downloading and resetting, the serial port prints 5 square root is equal to 2. |
| Use note | The development environment is KEIL MDK-ARM V5.34.0.0 or IAR EWARM 8.50.1; with the bus hold enabled the root register can be read directly during the calculation without polling the done flag. |
Core-code walkthrough and porting
The example configures the bus hold and auto start, turns on the module, and writing the radicand triggers one square root whose root is read back and printed.
Data path: RCC_EnableAHBPeriphClk enables the SQRT clock -> SQRT_ConfigBusHold enables the bus hold -> SQRT_ConfigAutoStart enables auto start -> SQRT_Enable turns on the module -> SQRT_SetRADICAND_Data writes the radicand to trigger the calculation -> SQRT_GetROOT_Data reads the root -> log_info prints it
1. Declare the radicand and root variables. Use 32-bit integers to hold the radicand and the returned root.
int32_t radicand = 5;
int32_t root;
To use another radicand just change the initial value; the return value is still an unsigned integer.
2. Enable the AHB clock of the SQRT module. SQRT is on the AHB bus, so this clock must be enabled before accessing its registers.
/* ENABLE HDIV moudel clock */
RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_SQRT,ENABLE);
The comment says HDIV but the SQRT clock is actually enabled; when porting, follow the macro RCC_AHB_PERIPH_SQRT.
3. Configure the bus hold and auto start, then enable the module. Enable the bus hold during the calculation first, then auto start, and finally turn on the module.
/* Hold bus during computation */
SQRT_ConfigBusHold(ENABLE);
/* Enable auto start computation */
SQRT_ConfigAutoStart(ENABLE);
/* Enable moudel */
SQRT_Enable(ENABLE);
To switch to single-start mode, disable auto start and trigger the calculation with the single-start function instead.
4. Write the radicand and read the root back. With auto start enabled, writing the radicand starts the calculation, and with the bus hold the root can be read directly.
log_info("SQRT test start\r\n");
SQRT_SetRADICAND_Data(radicand);
root = SQRT_GetROOT_Data();
log_info("5 square root is equal to %d\r\n" ,root);
Without the bus hold, poll the calculation-done flag before reading the root; the radicand is a 32-bit unsigned number and the result is a 16-bit root.
Corresponding source files: src/main.c, firmware/n32g033_std_periph_driver/inc/n32g033_sqrt.h
5.17.4 Notes and troubleshooting
| Scenario | Check & action | Source |
|---|---|---|
| Calculation width | The SQRT chapter of the user manual states that the module supports 32-bit operation only, the input is a 32-bit unsigned radicand and the output is a 16-bit root; a radicand out of range gives a meaningless result. |
User manual |
| Calculation timing | The SQRT chapter of the user manual states that one unsigned integer square-root operation takes 8 clock cycles; for consecutive operations make sure the previous result has been read. |
User manual |
| Idle check before first use | The SQRT chapter of the user manual states that except for the first use the completion flag of the status register should be confirmed as 1, meaning the module is idle; otherwise no new calculation should be started. |
User manual |
| Bus hold and result read | The SQRT chapter of the user manual states that with the bus hold enabled the result can be read without polling the done flag, and without it the done flag must be polled; the result register can be read immediately. |
User manual |
| Auto start and single start | The SQRT chapter of the user manual states that with auto start enabled each write of the radicand starts one operation, otherwise the single-start bit triggers it; when the configuration changes restart from the init step, and when it stays the same just write the radicand. |
User manual |
Errata sheet V1.0.0 (2025.9.25) covers revision A silicon and lists no SQRT entries.