Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include <mbed.h> 00002 00003 #if 1 00004 #define DWT_CONTROL ((volatile uint32_t *)0xE0001000) 00005 #define DWT_CYCCNT ((volatile uint32_t *)0xE0001004) 00006 #define SCB_DEMCR ((volatile uint32_t *)0xE000EDFC) 00007 00008 static inline uint32_t getDwtCyccnt(void) 00009 { 00010 return *DWT_CYCCNT; 00011 } 00012 00013 static inline void resetDwtCyccnt(void) 00014 { 00015 *DWT_CYCCNT = 0; // reset the counter 00016 } 00017 00018 static inline void enableDwtCyccnt(void) 00019 { 00020 *SCB_DEMCR = *SCB_DEMCR | (1u << 24); // TRCENA = 1 00021 *DWT_CONTROL = *DWT_CONTROL | 1 ; // enable the counter (CYCCNTENA = 1) 00022 *DWT_CYCCNT = 0; // reset the counter 00023 } 00024 00025 #endif 00026 00027 Serial pc(USBTX, USBRX); 00028 00029 static void benchmark_wait_us() { 00030 uint32_t count[11]; 00031 enableDwtCyccnt(); 00032 resetDwtCyccnt(); 00033 count[0] = getDwtCyccnt(); 00034 wait_us(1000); 00035 count[1] = getDwtCyccnt(); 00036 wait_us(1000); 00037 count[2] = getDwtCyccnt(); 00038 wait_us(1000); 00039 count[3] = getDwtCyccnt(); 00040 wait_us(1000); 00041 count[4] = getDwtCyccnt(); 00042 wait_us(1000); 00043 count[5] = getDwtCyccnt(); 00044 wait_us(1000); 00045 count[6] = getDwtCyccnt(); 00046 wait_us(1000); 00047 count[7] = getDwtCyccnt(); 00048 wait_us(1000); 00049 count[8] = getDwtCyccnt(); 00050 wait_us(1000); 00051 count[9] = getDwtCyccnt(); 00052 wait_us(1000); 00053 count[10] = getDwtCyccnt(); 00054 00055 for (size_t i = 0; i <= 10; i++) { 00056 const uint32_t diff = (i >= 1)?(count[i] - count[i - 1]):0; 00057 pc.printf("%d (%d)\r\n", count[i], diff); 00058 } 00059 } 00060 00061 static void benchmark_nop() { 00062 uint32_t count[11]; 00063 enableDwtCyccnt(); 00064 resetDwtCyccnt(); 00065 count[0] = getDwtCyccnt(); 00066 __nop(); 00067 count[1] = getDwtCyccnt(); 00068 __nop(); 00069 count[2] = getDwtCyccnt(); 00070 __nop(); 00071 count[3] = getDwtCyccnt(); 00072 __nop(); 00073 count[4] = getDwtCyccnt(); 00074 __nop(); 00075 count[5] = getDwtCyccnt(); 00076 __nop(); 00077 count[6] = getDwtCyccnt(); 00078 __nop(); 00079 count[7] = getDwtCyccnt(); 00080 __nop(); 00081 count[8] = getDwtCyccnt(); 00082 __nop(); 00083 count[9] = getDwtCyccnt(); 00084 __nop(); 00085 count[10] = getDwtCyccnt(); 00086 00087 for (size_t i = 0; i <= 10; i++) { 00088 const uint32_t diff = (i >= 1)?(count[i] - count[i - 1]):0; 00089 pc.printf("%d (%d)\r\n", count[i], diff); 00090 } 00091 } 00092 00093 int main() { 00094 pc.baud(115200); 00095 pc.printf("CPU SystemCoreClock is %.2f MHz\r\n", (float)SystemCoreClock/1000.0f/1000.0f); 00096 pc.printf("Nucleo compare DWT_CYCCNT to wait_us\r\n"); 00097 00098 // 00099 benchmark_wait_us(); 00100 benchmark_nop(); 00101 } 00102
Generated on Mon Jul 18 2022 23:36:05 by
1.7.2