aboutsummaryrefslogtreecommitdiff
path: root/stm32/src/stm32f4_machdep.c
blob: 59f9ed75e200d0635225c97655ad90ea2f3a954f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

/*---------------------------------------------------------------------------*\

  FILE........: stm32f4_machdep.c
  AUTHOR......: David Rowe
  DATE CREATED: May 2 2013

  STM32F4 implementation of the machine dependent timer functions,
  e.g. profiling using a clock cycle counter..

\*---------------------------------------------------------------------------*/

/*
  Copyright (C) 2013 David Rowe

  All rights reserved.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License version 2.1, as
  published by the Free Software Foundation.  This program is
  distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include <string.h>
#include "machdep.h"

#ifdef SEMIHOST_USE_STDIO
#include "stdio.h"
#else
#include "gdb_stdio.h"
#define printf gdb_stdio_printf
#endif

volatile unsigned int *DWT_CYCCNT   = (volatile unsigned int *)0xE0001004;
volatile unsigned int *DWT_CONTROL  = (volatile unsigned int *)0xE0001000;
volatile unsigned int *SCB_DEMCR    = (volatile unsigned int *)0xE000EDFC;

#define CORE_CLOCK 168E6
#define BUF_SZ     4096

static char buf[BUF_SZ];

void machdep_profile_init(void)
{
    static int enabled = 0;

    if (!enabled) {
        *SCB_DEMCR = *SCB_DEMCR | 0x01000000;
        *DWT_CYCCNT = 0; // reset the counter
        *DWT_CONTROL = *DWT_CONTROL | 1 ; // enable the counter

        enabled = 1;
    }
    *buf = 0;
}

void machdep_profile_reset(void)
{
    *DWT_CYCCNT = 0; // reset the counter
}

unsigned int machdep_profile_sample(void) {
    return *DWT_CYCCNT;
}

/* log to a buffer, we only call printf after timing finished as it is slow */

unsigned int machdep_profile_sample_and_log(unsigned int start, char s[])
{
    char  tmp[80];
    float msec;

    unsigned int dwt = *DWT_CYCCNT - start;
    msec = 1000.0*(float)dwt/CORE_CLOCK;
    snprintf(tmp, sizeof(tmp), "%s %5.2f msecs\n",s,(double)msec);
    if ((strlen(buf) + strlen(tmp)) < BUF_SZ)
        strncat(buf, tmp, sizeof(buf)-1);
    return *DWT_CYCCNT;
}

void machdep_profile_print_logged_samples(void)
{
    printf("%s", buf);
    *buf = 0;
}