Subversion Repositories svn.mios32

Rev

Rev 1744 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
736 tk 1
// $Id: freertos_utils.c 2425 2016-11-03 00:44:22Z tk $
2
//! \defgroup FREERTOS_UTILS
3
//!
4
//! Utility functions for FreeRTOS
5
//!
6
//! Contains functions for performance stat measurements as desribed under:
7
//! http://www.freertos.org/index.html?http://www.freertos.org/rtos-run-time-stats.html
8
//!
9
//! In order to enable performance measurements, add following definitions to
10
//! your mios32_config.h file:
11
//! \code
12
//! #define configGENERATE_RUN_TIME_STATS           1
2425 tk 13
//! #if configGENERATE_RUN_TIME_STATS
736 tk 14
//! #define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS  FREERTOS_UTILS_PerfCounterInit
15
//! #define portGET_RUN_TIME_COUNTER_VALUE          FREERTOS_UTILS_PerfCounterGet
2425 tk 16
//! #endif
736 tk 17
//! \endcode
18
//!
19
//! Add following include statement to your Makefile:
20
//! \code
21
//! # For performance measurings
22
//! include $(MIOS32_PATH)/modules/freertos_utils/freertos_utils.mk
23
//! \endcode
24
//!
25
//! Performance stats can be sent to the MIOS Terminal via:
26
//! \code
27
//!   FREERTOS_UTILS_RunTimeStats();
28
//! \endcode
29
//! e.g. when a special button has been pushed, or a special MIDI event
30
//! has been received.
31
//!
32
//! Example for a stat table sent to the MIOS Terminal:
33
//! \code
34
//! 00000072321902 ms | Task                 Abs Time             % Time
35
//! 00000072321902 ms | ================================================
36
//! 00000072321902 ms | 
37
//! 00000072321904 ms | Period1mS            113775               5
38
//! 00000072321905 ms | IDLE                 836250               42
39
//! 00000072321905 ms | MSD                  19461                <1
40
//! 00000072321905 ms | Hooks                69434                3
41
//! 00000072321905 ms | Hooks                260256               13
42
//! 00000072321905 ms | Period1mS_LP         387894               19
43
//! 00000072321905 ms | Period1S             34842                1
44
//! 00000072321906 ms | MIDI                 199942               10
45
//! 00000072321906 ms | Pattern              26228                1
46
//! 00000072321906 ms | 
47
//! \endcode
48
//! 
49
//! Note that this performance measurement method is expensive!!!<BR>
50
//! It should only be used for debugging purposes!
51
//! 
52
//! \{
53
/* ==========================================================================
54
 *
55
 *  Copyright (C) 2009 Thorsten Klose (tk@midibox.org)
56
 *  Licensed for personal non-commercial use only.
57
 *  All other rights reserved.
58
 *
59
 * ==========================================================================
60
 */
61
 
62
/////////////////////////////////////////////////////////////////////////////
63
// Include files
64
/////////////////////////////////////////////////////////////////////////////
65
 
66
#include <mios32.h>
1744 tk 67
#include <FreeRTOS.h>
68
#include <portmacro.h>
69
#include <task.h>
736 tk 70
#include "freertos_utils.h"
71
 
72
 
73
/////////////////////////////////////////////////////////////////////////////
74
// Local functions
75
/////////////////////////////////////////////////////////////////////////////
76
 
2425 tk 77
#if configGENERATE_RUN_TIME_STATS
736 tk 78
static void PerfTimerIRQ(void);
2425 tk 79
static s32 FREERTOS_UTILS_PrintBuffer(char *buffer);
838 tk 80
#endif
736 tk 81
 
82
 
83
/////////////////////////////////////////////////////////////////////////////
84
// Local variables
85
/////////////////////////////////////////////////////////////////////////////
86
 
87
static u32 perf_counter;
88
 
89
 
90
/////////////////////////////////////////////////////////////////////////////
91
//! Add this function via 
92
//! \code
93
//! #define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS FREERTOS_UTILS_PerfCounterInit
94
//! \endcode
95
//! to your mios32_config.h file
96
//!
97
//! Optionally
98
//! \code
99
//! // which MIOS32 timer should be used for performance measurements (0..2)
100
//! #define FREERTOS_UTILS_PERF_TIMER 2
101
//! 
102
//! // at which interval should it be called (interval should be much less than 1 mS!)
103
//! #define FREERTOS_UTILS_PERF_TIMER_PERIOD 10 // uS
104
//! \endcode
105
//! \return < 0 if initialisation failed
106
/////////////////////////////////////////////////////////////////////////////
107
s32 FREERTOS_UTILS_PerfCounterInit(void)
108
{
109
  perf_counter = 0;
110
 
111
#if configGENERATE_RUN_TIME_STATS == 0
112
  return -1; // configGENERATE_RUN_TIME_STATS not activated
113
#else
114
  return MIOS32_TIMER_Init(FREERTOS_UTILS_PERF_TIMER, FREERTOS_UTILS_PERF_TIMER_PERIOD, PerfTimerIRQ, MIOS32_IRQ_PRIO_HIGHEST);
115
#endif
116
}
117
 
118
 
119
/////////////////////////////////////////////////////////////////////////////
120
//! Add this function via 
121
//! \code
122
//! #define portGET_RUN_TIME_COUNTER_VALUE FREERTOS_UTILS_PerfCounterGet
123
//! \endcode
124
//! to your mios32_config.h file
125
//! \return 32bit counter value
126
/////////////////////////////////////////////////////////////////////////////
127
u32 FREERTOS_UTILS_PerfCounterGet(void)
128
{
129
  return perf_counter;
130
}
131
 
132
 
133
/////////////////////////////////////////////////////////////////////////////
134
//! Use this function to send the performance stats to the MIOS terminal
135
//! via MIDI
136
//! \return < 0 if MIDI output failed
137
/////////////////////////////////////////////////////////////////////////////
138
s32 FREERTOS_UTILS_RunTimeStats(void)
139
{
1744 tk 140
#if configGENERATE_RUN_TIME_STATS == 0 && configUSE_TRACE_FACILITY == 0
141
  return MIOS32_MIDI_SendDebugString("configGENERATE_RUN_TIME_STATS and configUSE_TRACE_FACILITY not activated!\n");  
736 tk 142
#else
1744 tk 143
  signed char buffer[400];
736 tk 144
  s32 status = 0;
145
 
1744 tk 146
#if configGENERATE_RUN_TIME_STATS
147
  // print stats
148
  status |= MIOS32_MIDI_SendDebugString("================================================\n");
149
  status |= MIOS32_MIDI_SendDebugString("Task, Abs Time, %% Time\n");
736 tk 150
  vTaskGetRunTimeStats(buffer);
1744 tk 151
  status |= FREERTOS_UTILS_PrintBuffer((char *)buffer);
152
#endif
736 tk 153
 
1744 tk 154
#if configUSE_TRACE_FACILITY
155
  // print task list
156
  status |= MIOS32_MIDI_SendDebugString("================================================\n");
157
  status |= MIOS32_MIDI_SendDebugMessage("Task, Status, Priority, StackRemaining/%d, TCB Number\n", sizeof(portSTACK_TYPE));
158
  vTaskList(buffer);
159
  status |= FREERTOS_UTILS_PrintBuffer((char *)buffer);
160
#endif
736 tk 161
 
1744 tk 162
  status |= MIOS32_MIDI_SendDebugString("================================================\n");
163
 
164
  return status;
165
#endif
166
}
167
 
168
 
169
 
170
/////////////////////////////////////////////////////////////////////////////
171
// Sends individual SysEx strings to MIOS terminal which are separated by \n
172
/////////////////////////////////////////////////////////////////////////////
2425 tk 173
#if configGENERATE_RUN_TIME_STATS
1744 tk 174
static s32 FREERTOS_UTILS_PrintBuffer(char *buffer)
175
{
176
  s32 status = 0;
177
 
178
  // print buffer line by line
736 tk 179
  char *buffer_start = buffer;
180
  char *buffer_end = buffer;
181
  u8 end_of_string;
182
  do {
183
    // scan for end of line
184
    while( *buffer_end != '\n' && *buffer_end != 0 ) ++buffer_end;
185
    // end of string reached?
186
    end_of_string = *buffer_end == 0;
187
    // terminate line
188
    *buffer_end = 0;
189
    // print line
1744 tk 190
    status |= MIOS32_MIDI_SendDebugString(buffer_start);
736 tk 191
    // continue with next line
192
    buffer_start = ++buffer_end;
193
    // until 0 was read
194
  } while( status >= 0 && !end_of_string );
195
 
196
  return status;
197
}
2425 tk 198
#endif
736 tk 199
 
200
//! \}
201
 
202
 
838 tk 203
#if configGENERATE_RUN_TIME_STATS != 0
736 tk 204
// this interrupt increments the performance counter
205
static void PerfTimerIRQ(void)
206
{
207
  ++perf_counter;
208
}
838 tk 209
#endif