# Changing clock frequency using C

**URL:** <https://discuss.pynq.io/t/changing-clock-frequency-using-c/6146>\
**Category:** Support\
**Created:** [October 4, 2023, 12:11pm UTC](https://discuss.pynq.io/t/changing-clock-frequency-using-c/6146 "2023-10-04T12:11:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pranav\_Chaitanya](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/pranav_chaitanya/32/5193_2.png) [@Pranav\_Chaitanya](https://discuss.pynq.io/u/Pranav_Chaitanya)\
**Post date:** [October 4, 2023, 12:11pm UTC](https://discuss.pynq.io/t/changing-clock-frequency-using-c/6146/1 "2023-10-04T12:11:05Z")

</div>

Hey everyone,  
[Using: PYNQ Z1]  
I am trying to change the clock frequency of the PL clock (Clocks.fclk0\_mhz = 50 in python) using C. I guess we can write the frequency onto the memory address associated with the PL clock. I am using Vitis\_hls and Vivado 2020.2.

it would be of great help if any of you could tell me where to look for that address so that I can perform a DMA and write the frequency into it. I can also look int vitis\_hls or vivado if someone could tell me where the info related to clock address would be.

---

<div class="post-metadata">

**Author:** ![cathalmccabe](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/cathalmccabe/32/3348_2.png) [@cathalmccabe](https://discuss.pynq.io/u/cathalmccabe)\
**Post date:** [October 9, 2023, 7:55am UTC](https://discuss.pynq.io/t/changing-clock-frequency-using-c/6146/2 "2023-10-09T07:55:35Z")

</div>

The is the PYNQ forum. If you want to use Zynq with C you may be better posting on the [Xilinx forums](https://forums.xilinx.com/).  
You can find register info in the Zynq TRM.  
[https://docs.xilinx.com/r/en-US/ug585-zynq-7000-SoC-TRM/Register-slcr-FPGA0\_CLK\_CTRL](https://docs.xilinx.com/r/en-US/ug585-zynq-7000-SoC-TRM/Register-slcr-FPGA0_CLK_CTRL)

Cathal

---

<div class="post-metadata">

**Author:** ![Pranav\_Chaitanya](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/pranav_chaitanya/32/5193_2.png) [@Pranav\_Chaitanya](https://discuss.pynq.io/u/Pranav_Chaitanya)\
**Post date:** [October 9, 2023, 8:21am UTC](https://discuss.pynq.io/t/changing-clock-frequency-using-c/6146/3 "2023-10-09T08:21:30Z")

</div>

Thank you soo much.

---

<div class="post-metadata">

**Author:** ![Pranav\_Chaitanya](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/pranav_chaitanya/32/5193_2.png) [@Pranav\_Chaitanya](https://discuss.pynq.io/u/Pranav_Chaitanya)\
**Post date:** [November 2, 2023, 9:43am UTC](https://discuss.pynq.io/t/changing-clock-frequency-using-c/6146/4 "2023-11-02T09:43:11Z")

</div>

#include \<fcntl.h\>  
#include \<stdio.h\>  
#include \<sys/ioctl.h\>  
#include \<sys/mman.h\>  
#include \<unistd.h\>  
#include \<string.h\>  
#include \<math.h\>

int main() {  
int fd = open(“/dev/mem”, O\_RDWR | O\_SYNC);  
if (fd \< 0) {  
perror(“Failed to open /dev/mem”);  
return -1;  
}

```
void *map = mmap(NULL, 0x10000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0xF8000000);

if (map == MAP_FAILED) {
    perror("Failed to mmap");
    return -1;
}

// relavant bits
#define srcsel 0x30
#define divisor0 0x3F00
#define divisor1 0x3F00000

#define srcsel_val 0x0 
#define divisor0_val 0xA // First cascade divider value
#define divisor1_val 0x1 // Second cascade divider value

volatile int* clk_ctrl = (volatile int*)(map + 0x170); 

// clear 
*clk_ctrl &= ~srcsel;
*clk_ctrl &= ~divisor0;
*clk_ctrl &= ~divisor1;

//set
*clk_ctrl |= (srcsel_val << 4);
*clk_ctrl |= (divisor0_val << 8);
*clk_ctrl |= (divisor1_val << 20);

```

}

I could control the clock using this code.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/system/32/2_2.png) [@system](https://discuss.pynq.io/u/system)\
**Post date:** [November 5, 2023, 9:43am UTC](https://discuss.pynq.io/t/changing-clock-frequency-using-c/6146/5 "2023-11-05T09:43:32Z")

</div>

Closed on November 5, 2023, 9:43am UTC

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
