Skip to content

Commit 6b1cc41

Browse files
authored
Adds HardwareSerial Example (#8095)
New Example for using/testing Serial with any CPU/APB Frequency
1 parent e360c2e commit 6b1cc41

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed
+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
Simple Sketch for testing HardwareSerial with different CPU Frequencies
3+
Changing the CPU Frequency may affect peripherals and Wireless functionality
4+
In ESP32 Arduino, UART shall work correctly in order to let the user see DGB info
5+
and other application messages.
6+
7+
CPU Frequency is usually lowered in sleep modes
8+
and some other Low Power configurations
9+
10+
*/
11+
12+
int cpufreqs[6] = {240, 160, 80, 40, 20, 10};
13+
#define NUM_CPU_FREQS (sizeof(cpufreqs) / sizeof(int))
14+
15+
void setup() {
16+
17+
Serial.begin(115200);
18+
delay(1000);
19+
Serial.println("\n Starting...\n");
20+
Serial.flush();
21+
22+
// initial information
23+
uint32_t Freq = getCpuFrequencyMhz();
24+
Serial.print("CPU Freq = ");
25+
Serial.print(Freq);
26+
Serial.println(" MHz");
27+
Freq = getXtalFrequencyMhz();
28+
Serial.print("XTAL Freq = ");
29+
Serial.print(Freq);
30+
Serial.println(" MHz");
31+
Freq = getApbFrequency();
32+
Serial.print("APB Freq = ");
33+
Serial.print(Freq);
34+
Serial.println(" Hz");
35+
delay(500);
36+
37+
// ESP32-C3 and other RISC-V target may not support 240MHz
38+
#ifdef CONFIG_IDF_TARGET_ESP32C3
39+
uint8_t firstFreq = 1;
40+
#else
41+
uint8_t firstFreq = 0;
42+
#endif
43+
44+
// testing HardwareSerial for all possible CPU/APB Frequencies
45+
for (uint8_t i = firstFreq; i < NUM_CPU_FREQS; i++) {
46+
Serial.printf("\n------- Trying CPU Freq = %d ---------\n", cpufreqs[i]);
47+
Serial.flush(); // wait to empty the UART FIFO before changing the CPU Freq.
48+
setCpuFrequencyMhz(cpufreqs[i]);
49+
Serial.updateBaudRate(115200);
50+
51+
Freq = getCpuFrequencyMhz();
52+
Serial.print("CPU Freq = ");
53+
Serial.print(Freq);
54+
Serial.println(" MHz");
55+
Freq = getXtalFrequencyMhz();
56+
Serial.print("XTAL Freq = ");
57+
Serial.print(Freq);
58+
Serial.println(" MHz");
59+
Freq = getApbFrequency();
60+
Serial.print("APB Freq = ");
61+
Serial.print(Freq);
62+
Serial.println(" Hz");
63+
if (i < NUM_CPU_FREQS - 1) {
64+
Serial.println("Moving to the next frequency after a pause of 2 seconds.");
65+
delay(2000);
66+
}
67+
}
68+
Serial.println("\n-------------------\n");
69+
Serial.println("End of testing...");
70+
Serial.println("\n-------------------\n");
71+
}
72+
73+
void loop() {
74+
// Nothing here so far
75+
}

0 commit comments

Comments
 (0)