Page 1 of 1

Modify clock associated to ports

Posted: Thu May 04, 2017 7:36 am
by cschopges
Hi all,

Here the code I am currently using:
stop_clock(p.cb);
set_clock_div(p.cb, CAN_CLOCK_DIVIDE);
set_port_clock(po, p.cb);
start_clock(p.cb);

I'd like to change the frequency of the clock associated to ports during execution of my code. Thus, I have a few question regarding the set_clock_div function (I can not find the detailled specification):

Does one need to stop the clock block (stop_clock) before changing the divider and to start the clock block (start_clock) again after it?

If I have a port linked to my clock block, then I change the divider of the clock block to have the clock running at another frequency, do I need to link my port with the clock block again (through set_port_clock)?

How much time does the function set_clock_div take (how much machine instructions)? Is it possible to write the same code in assembly in order for it to be faster?


Thanks

Re: Modify clock associated to ports

Posted: Tue May 09, 2017 9:47 am
by cschopges
Hey,

I would really appreciate an answer... :/

Re: Modify clock associated to ports

Posted: Tue May 09, 2017 2:23 pm
by infiniteimprobability
Does one need to stop the clock block (stop_clock) before changing the divider and to start the clock block (start_clock) again after it?
You will get an exception if you try to change the divider whilst the clock is started. This enforces safe behaviour.
If I have a port linked to my clock block, then I change the divider of the clock block to have the clock running at another frequency, do I need to link my port with the clock block again (through set_port_clock)?
No - it will retain it's binding. You just need to stop the clock first, change the divider and start it again. Note that the port timers are reset when you start the clock. This is a very useful feature when synchronising multiple ports (eg. I2S)
How much time does the function set_clock_div take (how much machine instructions)? Is it possible to write the same code in assembly in order for it to be faster?
one instruction - setd resource-id, source register. Two if you include loading a literal into the the source register (ldc dest register, constant)
The built-in function in xs1.h is optimal. Starting and stopping clocks, again, is a single instruction (setc resid, constant) and the xs1.h built-in is also optimal.

Re: Modify clock associated to ports

Posted: Fri May 12, 2017 6:59 am
by cschopges
Thanks for the reply :)

Re: Modify clock associated to ports

Posted: Mon May 15, 2017 11:58 am
by infiniteimprobability
Here's a diagram of the ports that may help..
Image

Re: Modify clock associated to ports

Posted: Wed May 17, 2017 7:33 am
by cschopges
Great! :)

Re: Modify clock associated to ports

Posted: Thu May 18, 2017 12:21 pm
by cschopges
I am trying to change the frequency dynamically (my code is written in assembly).
I therefore wrote a simple xc code and looked at the assembly of it since I can not find the assembly code of the function set_clock_div.

I found the following which I wrote in my file:
setc res[r10], 0x7
ldc r11, 0x0
setd res[r10], r11
setc res[r10], 0xf

However I get the following error:
xrun: Program received signal ET_ILLEGAL_RESOURCE, Resource exception.
[Switching to tile[0] core[1]]
ESI () at C:/Users/csc.DSA/workspace_appcanfd/module_can/src/can.S:264

263 setc res[r10], 0x7
Current language: auto; currently asm

Any idea why?
Or how to change the divider of a clock associated to a port in assembly?