The STM32G491 has an interesting mathematical acceleration subsystem called CORDIC (coordinate rotation digital computer). It implements the following mathematical operations in hardware: **cos**, **sin**, **phase**, **mod**, **atan**, **cosh**, **sinh**, **atanh**, **ln**, **sqrt**. It uses fixed point q31 or q15 calculations for all of these operations. These functions can be used to speed up DSP calculations while saving energy at the same time. I had an idea that they may be useful for **abc-dq0** and **dq0-abc** calculations in power electronics also but before I tried to tackle that I thought I should do some basic experiments. First on the list is a signal generator.

```
cordic Cordic;
int32_t args[2];
int32_t results[2];
int main()
{
char c;
int16_t s_int=0;
unsigned output = 0;
setup();
initDACS();
Serial.begin();
Cordic.begin();
Cordic.set_scale(0);
Cordic.set_precision(5);
Cordic.set_n_arg(2);
Cordic.set_n_res(2);
Cordic.set_arg_size(32);
Cordic.set_res_size(32);
Cordic.set_function(Cordic.sin);
args[0]=0;
args[1]=0x7fffff00;
enable_interrupts();
Serial.print("Cordic signal generator\r\n");
while(1)
{
s_int++;
output = (s_int-(int16_t)0x8000);
args[0]=output<<16;
Cordic.write_args(args);
Cordic.get_results(results);
DAC1->DAC_DHR12R1 = (results[0]+0x80000000)>>20;
DAC1->DAC_DHR12R2 = output>>4;
}
}
```

The code uses a counter (**s_int**) as an angle input to the CORDIC system which has been programmed to perform a sin calculation. The output from the CORDIC system is then passed to DAC1, channel1. The “angle” variable is passed to DAC1, channel2. The resultant analogue outputs are shown in the image below (green = “angle”, yellow = “sin”)

Some scaling and adjusting of values is necessary for this to work properly. First of all, the CORDIC output is 32 bits wide and so must be shifted right by 20 bits to suit the 12 bit DAC. Secondly, the zeros for the input and output to the CORDIC system must be shifted (with an overflow) to cater for the fact that the DAC is unsigned while the CORDIC system is signed.

Code is available over here on github.