Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Logic Question

Status
Not open for further replies.

electroRF

Member
Hi,

I got an issue which I'd be happy to see how you'd approach it.

There's a certain IC which my uC controls.

There's also a global variables, which forms a counter
- When Counter = 0, the uC powers off the IC.
- When Counter = 1, the uC Powers on the IC.
- When Counter >1, the uC does keeps the IC as is (powered On).

Each task that runs on the uC increments the Counter when it needs to work with it, and decrements it when it doesn't need it anymore.

That way, the uC powers on the IC when the 1st task to use the IC increments the Counter (in that case, Counter = 1), and powers if off when the last task to use that IC decrements that counter (in that case, Counter = 0).

Now, I wanna use that IC as well.

However, I noticed that there're some cases in which some task decrements the counter more times than it increments it, which is illegal, and therefore the IC remains off, which blocks my usage of the IC.

It'd be complex and would take a while to figure out which task violates the rules, and until I figure it out, I wanna work'around it.

I thought of doing the following, what do you think?

C:
void Decrement_Counter ()
{
Counter--;
if (Counter == 0xFFFFFFFF)
{
     Counter = 0;
     TURN_ON_IC();
}
else if (Counter == 0)
{
    TURN_OFF_IC();
}
}

C:
void Increment_Counter ()
{
Counter++;
if (Counter == 1)
     TURN_ON_IC();
}
 
Last edited:
With your logic, it looks like if the "bad" task does as you described - increment once and decrement twice, the IC will be left On.

At the same time, if you increment it, but then the bad task does its thing, it'll turn it Off on you.
 
Hi NorthGuy,
Thank you.

Yes, you're write, if my task '+1' the Counter, and then the "bad" task '+1' it and '-2' it, the Counter would get to zero, and the uC will power off the IC.

I guess there's no way to overcome it, huh? (the Only way I think of is keeping it turned on for good, but it'd be power consuming)
 
I've heart Google's programmers modified Linux kernel and created a similar mess with turning power off in Android.

The clean way to do that is for a task to place a request to turn the device on (might be as easy as setting a bit in task's TCB), then revoke the request when the device is no longer needed. Of course, it cannot revoke requests placed by other tasks. As soon as at least one request is present, you keep the device on.

You can also monitor tasks, and if you think that a task keeps the resource on for too long, you always can kill it or force revocation of the request that the task keeps.
 
Would this work? Just don't turn it on if the counter = 0.

C:
void Decrement_Counter ()
{
Counter--;
if (Counter == 0xFFFFFFFF)
{
     Counter = 0;
   TURN_OFF_IC();
}
else if (Counter == 0)
{
    TURN_OFF_IC();
}
}

Then it is ready for you to turn on when you need it.
 
Last edited:
Hi Clyde,
Thanks for your comment.

Say that Task A turns it On, and then Task B turns it On once and Turns it off Twice, then Task C turns it On once and Turns it off Twice - i.e.:
A +1
B +1
B -1
B -1
C +1
C -1
C -1

then the Counter would eventually reach -1.
In that case, my Decrement function would switch it ON as it should be, when the C Task would double'ly '-1' it.

In your suggestion, the IC would remain OFF.
 
Last edited:
I wasn't thinking about counter being -2

If counter is a signed integer then

C:
void Decrement_Counter ()

{

Counter--;

if (Counter < 0x00000000)

{

    Counter = 0;

  TURN_OFF_IC();

}

else if (Counter == 0)

{

    TURN_OFF_IC();

}

}


Otherwise


C:
void Decrement_Counter ()

{

Counter--;

if (Counter > 0xFFFFFF00)

{

    Counter = 0;

  TURN_OFF_IC();

}

else if (Counter == 0)

{

    TURN_OFF_IC();

}

}
 
Status
Not open for further replies.
Back
Top