Micro Trace Buffer setup question...

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

File under "just curious" but...

 

In Atmel Microchip Studio 7, when you first enable Program Counter Trace, a helpful dialog pops up telling you how to enable the Micro Trace Buffer by inserting the following code in your main.c file:

#define TB_SIZE 1024
#define IS_MTB_ENABLED REG_MTB_MASTER &MTB_MASTER_EN
#define DISABLE_MTB REG_MTB_MASTER = REG_MTB_MASTER & ~MTB_MASTER_EN
#define ENABLE_MTB REG_MTB_MASTER = REG_MTB_MASTER | MTB_MASTER_EN

__attribute__((aligned(TB_SIZE))) volatile char __tracebuffer__[TB_SIZE];
volatile int __tracebuffersize__ = sizeof(__tracebuffer__);

void InitTraceBuffer() {
    int index = 0;
    uint32_t mtbEnabled = IS_MTB_ENABLED;
    DISABLE_MTB;
    for (index = 0; index < TB_SIZE; index++) {
        __tracebuffer__
; __tracebuffersize__; // <= WHAT IS THE EFFECT OF THIS LINE? } if (mtbEnabled) { ENABLE_MTB; } }

You can see the comment I've added ("WHAT IS THE EFFECT OF THIS LINE?").  It appears to simply expand to the size of the trace buffer, and seemingly would have no effect.  But the MTB machinery appears to notice, and when you comment that line out, it will pop up the dialog telling you how to enable Program Counter Trace.

 

So I'm curious: what's going on with that line?  What is its effect?  How does the MTB mechanism know if you've commented it out?  Etc...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey - not fair!  The forum software formatter mangled the source code.  The lines in question should read:

 

    for (index=0; index<TB_SIZE; index++) {

        __tracebuffer__<open_square_bracket>index<close_square_bracket];

        __tracebuffersize__;

    }

 

Last Edited: Wed. Feb 3, 2021 - 05:21 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

I have read somewhere, that to be able to use trace, you must have special hardware. Is that true, or is Atmel ICE (or other) capable of tracing?
Sometimes it would be beneficial to trace code lines that change the value of a certain variable. Is this possible?
It should be possible to turn trace on and off in the code by defines / preprocessor commands. Otherwise you surely get flooded with information.
What is your experience with the trace function so far?

Surprise: As soon as one's doing it correctly - it works!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

> What is your experience with the trace function so far?

 

First, it's really useful.  I've been using it with my unit tests to check code coverage.  I don't have any special hardware other than the Atmel ICE.

 

> Sometimes it would be beneficial to trace code lines that change the value of a certain variable. Is this possible?

 

Not sure I understand.  For detecting if a value changes, I'd just use a data breakpoint -- you don't need the MTB for that.

 

> It should be possible to turn trace on and off in the code by defines / preprocessor commands.

> Otherwise you surely get flooded with information.

 

Well, yes, that's one way to do it.  But you can also turn it on and off dynamically in the IDE.  Because the system runs very slowly while tracing, the approach I take is to set two breakpoints: one where you're entering the code you want to trace (or profile), a second where you leave the code.  When you hit the first breakpoint, turn on tracing.  When you hit the second, turn off tracing. 

 

As I mentioned, I'm just using this for code coverage testing.  But the MTB can be used as a forensic tool to answer the question "what was my code doing just before it got a fault?" 

 

Hope this helps.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you for your response and sharing your experience.

fearless_fool wrote:
Because the system runs very slowly while tracing

Ah OK, that I didn't know. This information is given only in "the fine print"?
I have wondered how the trace module manages to output such massive data volume.
But when the CPU runs in "slow motion" it is understandable.
Then for me, developing embedded control applications, the trace mode is practically of no use.
Because I need strictly timed response to internal peripherals or external signals.

 

Surprise: As soon as one's doing it correctly - it works!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It sounds like the MTB may be inappropriate for your use case.

 

If you need strict timing, it's not difficult to write a specialized trace function.  For example, you can allocate a couple of static variables and capture the system clock at certain points in your code and go back and look at them later.  A slightly fancier version would be to allocate a circular buffer and capture [timestamp, event_id] pairs at key places in your code.