Advertisement
Guest User

main.c

a guest
Nov 23rd, 2014
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.15 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <time.h>
  6. #include <stdint.h>
  7.  
  8.  
  9. #define DEFAULT_DELAY   1
  10.  
  11. uint32_t m_nStart;               //DEBUG Stopwatch start cycle counter value
  12. uint32_t m_nStop;   //DEBUG Stopwatch stop cycle counter value            
  13. #define DEMCR_TRCENA    0x01000000
  14.  
  15. /* Core Debug registers */
  16. #define DEMCR           (*((volatile uint32_t *)0xE000EDFC))
  17. #define DWT_CTRL        (*(volatile uint32_t *)0xE0001000)
  18. #define CYCCNTENA       (1<<0)
  19. #define DWT_CYCCNT      ((volatile uint32_t *)0xE0001004)
  20. #define CPU_CYCLES      *DWT_CYCCNT
  21.  
  22.  
  23.  
  24. #define STOPWATCH_START { m_nStart = CPU_CYCLES; }/*DWT_CYCCNT;}*/
  25. #define STOPWATCH_STOP  { m_nStop = CPU_CYCLES; }
  26.  
  27.  
  28.  
  29.  
  30. static inline void stopwatch_reset(void)
  31. {
  32.     /* Enable DWT */
  33.     DEMCR |= DEMCR_TRCENA;
  34.     *DWT_CYCCNT = 0;            
  35.     /* Enable CPU cycle counter */
  36.     DWT_CTRL |= CYCCNTENA;
  37. }
  38.  
  39. static inline uint32_t stopwatch_getticks()
  40. {
  41.     return CPU_CYCLES;
  42. }
  43.  
  44. static inline void stopwatch_delay(uint32_t ticks)
  45. {
  46.     stopwatch_reset();
  47.     while(1)
  48.     {
  49.             if (stopwatch_getticks() >= ticks)
  50.                     break;
  51.     }
  52. }
  53.  
  54. uint32_t CalcNanosecondsFromStopwatch(uint32_t nStart, uint32_t nStop)
  55. {
  56.     uint32_t nTemp;
  57.     uint32_t n;
  58.     uint32_t SystemCoreClock = 180000000;
  59.     nTemp = nStop - nStart;
  60.  
  61.     nTemp *= 1000;                          // Scale cycles by 1000.
  62.     n = SystemCoreClock / 1000000;          // Convert Hz to MHz. SystemCoreClock = 168000000
  63.     nTemp = nTemp / n;                      // nanosec = (Cycles * 1000) / (Cycles/microsec)
  64.  
  65.     return nTemp;
  66. }
  67.  
  68.  
  69.  
  70. int main( int argc, char **argv )
  71. {
  72.         int     btn; //User Button status
  73.         int     ld = 1; /*LED value*/
  74.         long int diff;
  75.         int     delay = DEFAULT_DELAY;  // Initial value for the delay
  76.  
  77.         int timeDiff = 0;
  78.  
  79.  
  80.     STOPWATCH_START;
  81.     printf("Try...\n\n");
  82.     STOPWATCH_STOP;
  83.     timeDiff = CalcNanosecondsFromStopwatch(m_nStart, m_nStop);
  84.     printf("My function took %d nanoseconds\n", timeDiff);    
  85.  
  86.        
  87.         return( 0 );
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement