This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

DK-TM4C129X: localtime does not work from within Clock SWI (or Idle task)

Part Number: DK-TM4C129X

Hi,

I'm trying to display the local time on the screen of my development kit.

I got the following code from one of the man pages for ti/sysbios/hal/Seconds

    

#include <ti/sysbios/hal/Seconds.h>
#include <time.h>

// ... time_t t; struct tm *ltm; char *curTime; t = time(NULL); ltm = localtime(&t); curTime = asctime(ltm); System_printf("Time(GMT): %s\n", curTime); System_flush();

which works just how I'd expect it to and puts the time in the console.

However, if I try and call the exact same code as above from within a clock function or an idle callback, I get

Unhandled ADP_Stopped exception 0x20023

when stepping over the localtime function call. I have tried to step in, and I get no file. Included, however, is the disassembly in case that helps.

localtime():
0000dd5c:   B510                push       {r4, lr}
0000dd5e:   4604                mov        r4, r0
57        
0000dd60:   F7F6F9B8            bl         #0x40d4
0000dd64:   F100017C            add.w      r1, r0, #0x7c
0000dd68:   4620                mov        r0, r4
58          _REENT_CHECK_TM(reent);
0000dd6a:   E8BD4010            pop.w      {r4, lr}
57        
0000dd6e:   F000B801            b.w        localtime_r
0000dd72:   BF00                nop        
17          return _mktm_r (tim_p, res, 0);

And, it dies on the first `bl` in particular.

I have tried digging deeper into that, and it is dying on

Assert_isTrue(((BIOS_getThreadType() != BIOS_ThreadType_Hwi) &&
                   (BIOS_getThreadType() != BIOS_ThreadType_Swi)),
                    ReentSupport_A_badThreadType);

But I can't imagine why localtime would be causing an issue here.

Any advice?

  • Hi David,

    localtime is a function defined in the GCC C runtime (libc) which requires a per thread re-entrancy structure. SYS/BIOS allocates this re-entrancy structure the very first time libc needs it. Since libc APIs needing a re-entrancy structure may result in dynamic memory allocation (malloc), they are not allowed to be called from within Interrupt (Hwi) or Swi context.

    Best,
    Ashish