I just measured the number of machine cycles between the time that I enter the EMU_EnterEMU2 subroutine, and the time that the WFI instruction is executed. The time is 258 machine cycles or a little over 8uS at 32MHZ core clock ! Since I am burning full power during this additional 8uS, I am becoming concerned.
Single stepping thru the code, I see that the code is configuring/reconfiguring the DC-DC converter (again). What is going on here ? Is this code necessary ? Why ?
I am not doing the restore on exit from EM2 either since I am already using the HFRCO oscillator.
Is the DC-DC configured for something different after exit from EMU2 if I don't use the restore function ?
The DC-DC converter can be changed to different modes based on your power and noise requirements (which may be different in EM2). See AN948 for more information. Also see errata DCDC_E202 and DCDC_E203, the workaround is to set the DCDC to low noise mode when entering EM2 to reduce the output dip that occurs.
Keep in mind the emlib function is intended to cover all use cases. You can write your own function if you like, to reduce the amount of time it takes to enter EM2.
Also, are you compiling code in the Debug or Release configuration?
Debug includes a fair bit of overhead to permit stepping correspondence between C instructions and the underlying assembly language as well as constructs necessary for local variable visibility.
Try compiling your code in the Release configuration and seeing what happens to the EM2 entry time.