Semaphore doesn’t seem to work

Hi! In my application I create a semaphore to control access to a global structure which holds the acual time, which is beeing read out a RTC in a task. In another task I create log entrys, including the time. In both tasks, the access to the structure is protected by the semaphore. I initialize the "time reading task" before the log task, so the time should be read out before any log entry is created. Even so the first log entrys contain a wrong time. How is this possible. As long as the time reading task holds the semaphore the log task should not be able to access the time keeping structure. Unfortunately my debugger (using IAR Embedded Workbench) is not able to debug this, I get a message stating "simulating illegal code condition". Now I am wondering how this is possible. Preemption is enabled and both tasks have the same priority. But this should not be important as I am using the semaphore. Has anyone an idea on this? ThX!

Semaphore doesn’t seem to work

From the error it sounds like you are running it in the simulator.  Is this the case.  I don’t think the IAR simulator simulated peripherals.

Semaphore doesn’t seem to work

No it runs on hardware with a SAM-ICE connected. The error is not the problem. I figured out so much: The RTC Read task is started before the Log Task. It runs beyond the take semaphore command and starts updating the clock structure. I then have to let it run, else I encounter the prior mentioned error. The Log task is entered thereafter (I have a breakpoint here). If I debug it, sometimes the time is correctly set prior to the first log entry and sometimes not. Is it possible that the tasks start in a casual order, so sometimes the log task has first access and sometimes not? I don’t really think so but this would be an explanation. On the other hand: If the time is not correct, parts of the time are right, e.g. seconds and minutes, but not the hours, which is for me a hint that the RTC Task is being interrupted and the semaphore does not work. So far this only happens at startup. Before using a semaphore I had glitches in the displayed time several times, they are all gone with the introduction of the semaphore, only this startup problem is left. I have firgured out a workaround which is reading the clock directly before staring the scheduler, nonetheless I would like to find the problem there. I don’t think my explanation is very good, but I hope you can still get the problem.

Semaphore doesn’t seem to work

Update: I think I figured out the problem: It is not the semaphore it is the TWI which is use to read the RTC (I²C). Some or all Values seem to be read out wrong sometimes at startup, but not always. I thought the TWI routines work ok because they did till now. The errors seem only to occur at startup. So everything with the semaphores is fine!