Search code examples
ctimemicrocontrollerreal-time-clock

Why/how is time set for a microcontroller platform under Segger JTAG ICE?


On a SAM4C microcontroller platform the time() function is called within a library. I found out, that this gives the right time (1496321612) when I start from debugger. However, when I make a hardware reset on the board, and the flashed firmware executes without debugger, time() doesnt return and the firmware stucks at this point.

I wonder how time is set at all. I debug only via ATMEL JTAG ICE (Segger) and do not set the time explicitly. When I change date on my PC I get back a different time while debugging...Is there an automatic mechanism that sets time from my PC when a debug session gets started by the Segger ICE and why is my firmware crashing when time is not set?


Solution

  • This can be achieved using the mechanism called semi-hosting. Overall the mechanism is allowing the target to use some facilities available on the host computer (the one running the debugger HW/SW). The simplified mechanism is like the following:

    1. The target firmware is linked against a library supporting the semihosting.
    2. Each time the firmware is invoking the semihosted function, a debugger event is happening (It can be some SVC/SWI call, special breakpoint or something else, depending on the specific platform).
    3. The debugger is "catching" this event, figuring out what was requested (it can be some IO function, or a request for time as in your case) and passing this request to the host software.
    4. The host software is doing what was requested and passing the result to the debugger.
    5. The debugger is passing the result to the target FW.
    6. The target is resuming.