Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added examples_v30x/pll_clock_test/HSI_PLL.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions examples_v30x/pll_clock_test/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
all : flash

TARGET:=pll_clock_test
TARGET_MCU:=CH32V303
TARGET_MCU_PACKAGE:=CH32V303

include ../../ch32fun/ch32fun.mk

flash : cv_flash
clean : cv_clean


22 changes: 22 additions & 0 deletions examples_v30x/pll_clock_test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# DMA ADC & op-amp example
Simple example that demonstrates how to configure different PLL multipliers
for the HSI clock source.

## In order to change the PLL multipler value
- first switch the peripheral clock to HSI.
- turn off PLL, set the PLL multiplier value, and turn on PLL
- finally, switch the peripheral clock back to use PLL

## Descriptions
This example cycles through different PLLMUL[3:0] values 0 to 15.
Depending on what chip you are using the PLLMUL value represents different multipliers.
For example, CH32V30x_D8 PLLMUL = 0 = PLL input clock * 2
whereas, CH32V30x_D8C PLLMUL = 0 = PLL input clock * 18
Since the PLL input clock changes without any adjustment to the Delay_Ms function
The delay time will change based on the PLL multiplier value.

The HSI clock source starts at 8MHz
min_multiplier 8MHz * 2 = 16MHz (uses about 2.5mA)
max_multiplier: 8Mhz * 18 = 144MHz (uses about 17.5mA)

![Screenshot](HSI_PLL.png)
13 changes: 13 additions & 0 deletions examples_v30x/pll_clock_test/funconfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef _FUNCONFIG_H
#define _FUNCONFIG_H

#define FUNCONF_SYSTICK_USE_HCLK 1
#define FUNCONF_USE_HSI 1
#define FUNCONF_USE_PLL 0
#define FUNCONF_PLL_MULTIPLIER 2

#define FUNCONF_USE_DEBUGPRINTF 1
#define FUNCONF_SYSTEM_CORE_CLOCK 8 * 1000 * 1000

#endif

46 changes: 46 additions & 0 deletions examples_v30x/pll_clock_test/pll_clock_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Simple example that demonstrates how to configure different PLL multipliers
// for the HSI clock source.

#include "ch32fun.h"
#include <stdio.h>

void PLL_configure(u8 pll_indexValue) {
if (pll_indexValue > 15) return;

// Switch to HSI first
RCC->CFGR0 &= ~RCC_SW; // Clear clock source bits
RCC->CFGR0 |= RCC_SW_HSI; // Switch to HSI clock source
while((RCC->CFGR0 & RCC_SWS) != RCC_SWS_HSI); // Wait for switch

// Turn off PLL and wait for it to stop
RCC->CTLR &= ~RCC_PLLON;
while(RCC->CTLR & RCC_PLLRDY); // Wait for PLL to fully stop

// Clear existing multiplier bits and set new one
RCC->CFGR0 &= ~RCC_PLLMULL; // Clear all PLLMUL bits
RCC->CFGR0 |= (pll_indexValue << 18); // Set new multiplier

// Turn on PLL and wait for it to lock
RCC->CTLR |= RCC_PLLON;
while(!(RCC->CTLR & RCC_PLLRDY)); // Wait for PLL to lock

// Switch to PLL
RCC->CFGR0 &= ~RCC_SW; // Clear clock source bits
RCC->CFGR0 |= RCC_SW_PLL; // Switch clock source to PLL
while((RCC->CFGR0 & RCC_SWS) != RCC_SWS_PLL); // Wait for switch
}

int main() {
SystemInit();
Delay_Ms(100);

printf("\n~ PLL Test ~\n");

while(1) {
for (int i = 0; i < 16; i++) {
printf("PLL multiplier index %d\n", i);
PLL_configure(i);
Delay_Ms(12000);
}
}
}