Number of clock cycles required for PWM functions

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Mrinal
Level 4
Level 4
First solution authored 50 replies posted 25 replies posted

Is there a way to know how many clock (HFCLK) cycles do the TCPWM API (for that matter any API) functions take to execute.

   

Take for example: TCPWM_WritePeriod(); How may colck cycles will it take to execute?

   

Also is there a way to directly write into registers using assembly command? How to nsert assembly commands into a C program?

   

Thanks

0 Likes
16 Replies
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Inline ASM - http://www.cypress.com/forum/psoc-4-architecture/inline-assembly-psoc4

   

 

   

To count clock cycles to execute an API look at the .lst file, find the API call, and count

   

your cycles from the asm code shown there. Use ARM M0 instruction table cycle count

   

to get the value for each ARM instruction.

   

 

   

Yes, you can write registers from C or from ASM. The C registers pneumonic name

   

is in the cyfitter.h file for the UDB object you placed. You can also look at the register

   

TRM for other registers. Be careful using direct reg writes, read carefully the notes in

   

the register TRM.

   

 

   

Regards, Dana.

0 Likes
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

MrinalMani,

   

You can yse SysTick counter to measure amount of CPU cycles taken by a function or operation as shown below. You will be limited by 2^24 CPU ticks only (about 0.25sec @24MHz CPU speed), as SysTick counter on Cortex M0 and M3 can not go beyond  SYSTICK_MAXVAL.

   

odissey1

   

 

   

 

   

#define SYSTICK_MAXVAL 0x00FFFFFF //max allowed SysTick counter value for 24bit

   

uint32 SysCntVal; // The value of SysTick counter you are trying to retrieve
 

   

       
      SysTick_Config(SYSTICK_MAXVAL); //reset counter set to max value, 1-time,  will not reload

   

      TCPWM_WritePeriod(); //do something.. 

   

      SysCntVal = SYSTICK_MAXVAL - (SysTick->VAL); //get elapsed ticks (min offset 3 ticks)
           
      sprintf(strMsg1, "%d, %f\r\n", SysCntVal, x); //report result
      UART_PutString(strMsg1);

0 Likes
Mrinal
Level 4
Level 4
First solution authored 50 replies posted 25 replies posted

Thanks you for the quick reply.

   

1. I am looking for a list of all API for PSOC4, cannot find it.

   

2. How to insert assembly codes into a C program?

   

Is there a function within which we may put all desired assembly codes and each time the function is called, the assembly codes will be executed.

   

eg

   

xyz()

   

{

   

CODE1

   

.

   

.

   

CODEn

   

}

   

Thank you

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Creator 3.3 (and former versions) are working with GCC which is the Gnu Compiler Collection. You will find explanations at gcc.gnu.org for all compiler and linker aspects.

   

You will not find a list of *ALL* APIs for a PSoC4, but a list of all APIs for a PSoC4 component. Since there could always a new component be built the former list is quite impossible. You may left-click on a component on your TopDesign sheet or on the name in the component catalog and select Open Datasheet to get a description and a list of *ALL* APIs the component supports.

   

Using ASM is not quite easy, but it works. I prefer

   

#define ASM(x)                __asm volatile(x)                                //    Shorthand, x is the string containing the assembly instruction

   

 

   

    ASM (
        "mrs    r0, psp\n\r"                            //    Get Process Stackpointer into R0
        "ldr    r3, xRunningTask\n\r"                   //    Get the pointer to the Task Descriptor
        "ldr    r2, [r3]\n\r"                            //    Address of Stackpointer Storage
        "sub    r0, r0, #32\n\r"                        //    Reserve space for the other registers to push
        "str    r0, [r2]\n\r"                            //    Save Process's Stackpointer value
        "stm    r0!, {r4-r7}\n\r"                        //    Like a push Instruction but using R0 as Stackpointer
        "mov    r4, r8\n\r"                                //    Move Registers R8 to R11

   

    );

   

This you may put into a function (not THAT text, but what YOU want to be executed). **** Watch **** Keep all assembly statements in ONE ASM(), otherwise the C-optimizer will take the right to re-arrange the instructions which usually will be disastrous.

   

 

   

Bob

0 Likes
Mrinal
Level 4
Level 4
First solution authored 50 replies posted 25 replies posted

Thanks Bob!

   

Thanks for the detailed explanation.

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given
    This might be of use (discusses ASM as well as many other topics) -   
   

 

   
     http://www.cypress.com/documentation/application-notes/an89610-psoc-4-and-psoc-5lp-arm-cortex-code-o...     AN89610 - PSoC® 4 and PSoC 5LP ARM Cortex Code Optimization   
   

 

   

Regards, Dana.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

My experiences so far: You do not need to use assembly code for quite "normal" programs, even when time- or size-critical the optimization in GCC makes it difficult to beat that with own assembly code. You may specify "Release" settings instead of "Debug" when compiling your code, you may even request a different optimization level for a file and you could even set an optimization level for a function (will be flagged when editing, but compiles without error)

   

When you need to code something rather special (like teaching asm, task-switching, modifying sp, lr or so) you will indeed need assembly code.

   

When you tell us a bit more about what you'd like to perform we probably could help you a bit further on.

   

 

   

Bob

0 Likes
Mrinal
Level 4
Level 4
First solution authored 50 replies posted 25 replies posted

I want to implement a 100KHz 10bit SPWM signal. For this, the Period value of the TCPWM module must be updated every 10us.

   

Since the PWM must remain ON throughout, I was wondering whether I could use one line assembly code to access the new period value from flash and another one line instruction to update this value into the TCPWM period register. The whole thing would then be complete in 2 to 3 clock cycles, as opposed to more cycles (much more?) if I used API function calls to do the same.  I am not running out of space or time nor was I aware that inserting assembly is generally avoided. 

   

I generally have to deal with power electronics only. So I don't really have much of experience with MCUs and to add to this, PSOC had made it all the more easier. I really didn't need to program anything up till now, peripherals did it all! 

   

Is flash treated as a peripheral or a part of the MCU? If it is a peripheral then where can I find appropriate API related to flash?

   

Thank you.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Difficult to say where flash belongs to, there are APIs to program the flash, but code is executed usually from flash which would imply it belongs to the CPU. With your requirements I come to a PWM frequency of 100MHz which is quite more than a PSoC4 can swallow.

   

 

   

Bob

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

The issue is a presumption that you do not want to truncate a PWM cycle ?
 

   

That is do you want the PWM to issue its complete cycle before changing its

   

parameters ? And if so do you want zero latency from one period to the next, or

   

is latency allowed while doing the change ? If latter use HW on Tc output to stop

   

the PWM, do the update, then restart. If no latency I think a verilog solution might

   

be necessary.

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

your own pwm function in a pair of UDBs could work and be tied to a tight synchronization. you could load up 16 bit fifos with interrupt or dma.

   

 

   

-Ed

0 Likes
lock attach
Attachments are accessible only for community members.
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

MrinalMani, My understanding is that SPWM works like comparator between triangular ramp and external signal (e.g. sine wave). What is the source of this external signal in your case? Is this a software loop? What max frequency you expect for this external signal?

   

To clarify, I attached schematic of SPWM with signal input from ControlReg. In this case you would have to populate ControlReg by the CPU.

   

 

   

   

 

   

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

What mode are you trying to achieve ?

   

 

   

https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=7&ved=0CDQQFjAGahUKEwjNt4aT_pfIAhXH1R4...

   

 

   

If you google "spwm verilog" seems like there is a number of solutions out there.

   

 

   

http://stackoverflow.com/questions/22835525/sinusoidal-pulse-width-modulation-in-fpga-device-ok-in-s...

   

 

   

Regards, Dana.

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

This might be of interest, especially last entry in thread -

   

 

   

http://www.cypress.com/forum/psoc-5-device-programming/question-about-changing-pwm-compare-value

   

 

   

Regards, Dana.

0 Likes
Mrinal
Level 4
Level 4
First solution authored 50 replies posted 25 replies posted

Thanks everyone...

   

M trying out the suggestions.

0 Likes
Mrinal
Level 4
Level 4
First solution authored 50 replies posted 25 replies posted

Thanks everyone for the quick response.

   

1. There is no "EXTERNAL" signal to be compared to. Instead there is a look up table containing 200 discrete compare values.

   

2. After every 10us, the compare value of the PWM has to be updated with a new one.

   

3. Verilog solution? I am not into HDL. Will keep it as the last option. At this point I dont even know whether 32 macrocells will suffice

   

 

   

The problem is very simple:

   

Each time the PWM module flashes a TC interrupt, change the compare value.

   

Compare value can be changed through API, direct register update or through UDB. Any method will do. However the UDB method, if possible, will be cool!

0 Likes