-
Notifications
You must be signed in to change notification settings - Fork 224
added Ch32v30x PVD test. #777
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| all : flash | ||
|
|
||
| TARGET:=pvd_voltageDetect_test | ||
| TARGET_MCU:=CH32V303 | ||
| TARGET_MCU_PACKAGE:=CH32V303 | ||
|
|
||
| include ../../ch32fun/ch32fun.mk | ||
|
|
||
| flash : cv_flash | ||
| clean : cv_clean | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
|
43 changes: 43 additions & 0 deletions
43
examples_v30x/pvd_voltageDetect_test/pvd_voltageDetect_test.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // Simple example that shows how to use the PVD voltage detector functions | ||
| // the datasheet shows 2 different PVD levels selection options one for VLEVEL 0 and one for VLEVEL 1 | ||
| // It's not mentioned anywhere (that I can find) what chip uses what PVD level, if you find it let me know | ||
| // When the input supply voltage drop bellow the threshold the PVD will trigger an alert | ||
|
|
||
| // Testing Methods: | ||
| // Method 1: Using a Potentiometer | ||
| // Connect a potentiometer across the power supply input and ground | ||
| // Adjust the potentiometer to vary the input voltage | ||
| // Observe the PVD status output as the voltage crosses the threshold | ||
|
|
||
| // Method 2: Using a Voltage Divider | ||
| // Add a series resistor to the power input to create a voltage drop | ||
| // Monitor the PVD output to detect when voltage falls below the set threshold | ||
|
|
||
| #define VLEVEL 1 | ||
| // #define VLEVEL 0 | ||
|
|
||
| #include "ch32fun.h" | ||
| #include <stdio.h> | ||
| #include "lib_pvd.h" | ||
|
|
||
| void PVD_statusPrint() { | ||
| const char *v1[] = { "2.29", "2.46", "2.55", "2.67", "2.78", "2.93", "3.06", "3.19" }; | ||
| const char *v0[] = { "2.13", "2.25", "2.32", "2.42", "2.51", "2.61", "2.69", "2.79" }; | ||
| const char *threshold_str = (VLEVEL == 0) ? v0[PVD_getThreshold()] : v1[PVD_getThreshold()]; | ||
|
|
||
| printf("\nThreshold: %sV", threshold_str); | ||
| printf("\nStatus: %s\n", PVD_getAlert() ? "BELOW THRESHOLD!" : "Normal"); | ||
| } | ||
|
|
||
| int main() { | ||
| SystemInit(); | ||
| Delay_Ms(100); | ||
|
|
||
| printf("\n~ PVD Voltage Detector Test ~\n"); | ||
| PVD_init(7); // select threshold 0-7 | ||
|
|
||
| while(1) { | ||
| PVD_statusPrint(); | ||
| Delay_Ms(1000); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Tested with CH32V002, CH32V006, and CH32V303 | ||
|
|
||
| #include "ch32fun.h" | ||
|
|
||
| void PVD_init(u8 threshold) { | ||
| if (threshold > PVD_MAX_THRESHOLD_LVL) threshold = PVD_MAX_THRESHOLD_LVL; | ||
|
|
||
| // Enable PWR clock | ||
| RCC->APB1PCENR |= RCC_APB1Periph_PWR; | ||
|
|
||
| // Enable PVD | ||
| PWR->CTLR |= PWR_CTLR_PVDE; | ||
|
|
||
| // Clear the existing PLS bits and set new threshold | ||
| PWR->CTLR = (PWR->CTLR & ~PWR_CTLR_PLS) | (threshold << 5); | ||
| } | ||
|
|
||
| // Get threshold setting: the PLS[1:0] bits | ||
| u8 PVD_getThreshold() { | ||
|
||
| return (PWR->CTLR & PWR_CTLR_PLS) >> 5; | ||
| } | ||
|
|
||
| // return PVD flag: 1 if VDD below threshold | ||
| u8 PVD_getAlert() { | ||
| return PWR->CSR & PWR_CSR_PVDO; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should be static.