1
2 /*---------------------------------------------------------------------------*\
3
4 FILE........: stm32f4_machdep.c
5 AUTHOR......: David Rowe
6 DATE CREATED: May 2 2013
7
8 STM32F4 implementation of the machine dependant timer functions,
9 e.g. profiling using a clock cycle counter..
10
11 \*---------------------------------------------------------------------------*/
12
13 /*
14 Copyright (C) 2013 David Rowe
15
16 All rights reserved.
17
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU Lesser General Public License version 2.1, as
20 published by the Free Software Foundation. This program is
21 distributed in the hope that it will be useful, but WITHOUT ANY
22 WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
24 License for more details.
25
26 You should have received a copy of the GNU Lesser General Public License
27 along with this program; if not, see <http://www.gnu.org/licenses/>.
28 */
29
30 #include <string.h>
31 #include "machdep.h"
32
33 #ifdef SEMIHOST_USE_STDIO
34 #include "stdio.h"
35 #else
36 #include "gdb_stdio.h"
37 #define printf gdb_stdio_printf
38 #endif
39
40 volatile unsigned int *DWT_CYCCNT = (volatile unsigned int *)0xE0001004;
41 volatile unsigned int *DWT_CONTROL = (volatile unsigned int *)0xE0001000;
42 volatile unsigned int *SCB_DEMCR = (volatile unsigned int *)0xE000EDFC;
43
44 #define CORE_CLOCK 168E6
45 #define BUF_SZ 4096
46
47 static char buf[BUF_SZ];
48
machdep_profile_init(void)49 void machdep_profile_init(void)
50 {
51 static int enabled = 0;
52
53 if (!enabled) {
54 *SCB_DEMCR = *SCB_DEMCR | 0x01000000;
55 *DWT_CYCCNT = 0; // reset the counter
56 *DWT_CONTROL = *DWT_CONTROL | 1 ; // enable the counter
57
58 enabled = 1;
59 }
60 *buf = 0;
61 }
62
machdep_profile_reset(void)63 void machdep_profile_reset(void)
64 {
65 *DWT_CYCCNT = 0; // reset the counter
66 }
67
machdep_profile_sample(void)68 unsigned int machdep_profile_sample(void) {
69 return *DWT_CYCCNT;
70 }
71
72 /* log to a buffer, we only call printf after timing finished as it is slow */
73
machdep_profile_sample_and_log(unsigned int start,char s[])74 unsigned int machdep_profile_sample_and_log(unsigned int start, char s[])
75 {
76 char tmp[80];
77 float msec;
78
79 unsigned int dwt = *DWT_CYCCNT - start;
80 msec = 1000.0*(float)dwt/CORE_CLOCK;
81 snprintf(tmp, sizeof(tmp), "%s %5.2f msecs\n",s,(double)msec);
82 if ((strlen(buf) + strlen(tmp)) < BUF_SZ)
83 strncat(buf, tmp, sizeof(buf)-1);
84 return *DWT_CYCCNT;
85 }
86
machdep_profile_print_logged_samples(void)87 void machdep_profile_print_logged_samples(void)
88 {
89 printf("%s", buf);
90 *buf = 0;
91 }
92
93