|
| 1 | +/* |
| 2 | + Increasing core speed to 96MHz |
| 3 | + By: Nathan Seidle |
| 4 | + SparkFun Electronics |
| 5 | + Date: June 24th, 2019 |
| 6 | + License: This code is public domain but the prime number function |
| 7 | + comes from Ambiq/John Burkardt and has a GNU LGPL license. Additional |
| 8 | + code based on Ambiq's burst example. |
| 9 | +
|
| 10 | + SparkFun labored with love to create this code. Feel like supporting open source hardware? |
| 11 | + Buy a board from SparkFun! https://www.sparkfun.com/products/15376 |
| 12 | +
|
| 13 | + This example shows how to increase the core speed from 48MHz to 96MHz. |
| 14 | +
|
| 15 | + Increasing the core speed will increase current consumption. And increase in core |
| 16 | + speed is useful for calculations and minimizing code run time. Burst mode does not increase |
| 17 | + the speed at which GPIO can toggle. |
| 18 | +*/ |
| 19 | + |
| 20 | +long startTime; |
| 21 | +long endTime; |
| 22 | +int count = 0; |
| 23 | + |
| 24 | +//Make true if you want to print the prime numbers |
| 25 | +//Printing output will destroy the accuracy of the timing |
| 26 | +bool printThePrimes = false; |
| 27 | + |
| 28 | +void setup() { |
| 29 | + Serial.begin(9600); |
| 30 | + Serial.println("Calculating primes with burst mode"); |
| 31 | + |
| 32 | + startTime = millis(); |
| 33 | + count = numberOfPrimes(10000); //Find the number of primes in 10,000 |
| 34 | + endTime = millis(); |
| 35 | + |
| 36 | + Serial.printf("Total primes found (should be 1229): %d\n", count); |
| 37 | + Serial.printf("Time required at 48MHz: %dms\n", endTime - startTime); |
| 38 | + |
| 39 | + enableBurstMode(); //Go to 96MHz |
| 40 | + startTime = millis(); |
| 41 | + count = numberOfPrimes(10000); //Find the number of primes in 10,000 |
| 42 | + endTime = millis(); |
| 43 | + disableBurstMode(); //Return to 48MHz |
| 44 | + |
| 45 | + Serial.printf("Total primes found (should be 1229): %d\n", count); |
| 46 | + Serial.printf("Time required in burst mode: %dms\n", endTime - startTime); |
| 47 | +} |
| 48 | + |
| 49 | +void loop() { |
| 50 | + //Do nothing |
| 51 | +} |
| 52 | + |
| 53 | +// Returns the number of primes between 1 and N. |
| 54 | +// A naive algorithm is used. |
| 55 | +// Input, int N, the maximum number to check. |
| 56 | +// Output, int PRIME_NUMBER, the number of prime numbers up to N. |
| 57 | +int numberOfPrimes(int maxToSearch) |
| 58 | +{ |
| 59 | + uint32_t ui32Total, ui32Prime; |
| 60 | + int32_t ix, jx; |
| 61 | + |
| 62 | + ui32Total = 0; |
| 63 | + |
| 64 | + for ( ix = 2; ix <= maxToSearch; ix++ ) |
| 65 | + { |
| 66 | + ui32Prime = 1; |
| 67 | + for ( jx = 2; jx < ix; jx++ ) |
| 68 | + { |
| 69 | + if ( (ix % jx) == 0 ) |
| 70 | + { |
| 71 | + ui32Prime = 0; |
| 72 | + break; |
| 73 | + } |
| 74 | + } |
| 75 | + if (ui32Prime == 1 && printThePrimes == true) |
| 76 | + { |
| 77 | + Serial.printf("Prime found: %d\n", ix); |
| 78 | + } |
| 79 | + ui32Total += ui32Prime; |
| 80 | + } |
| 81 | + return ui32Total; |
| 82 | +} |
0 commit comments