Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.arch.embedded > #29339
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | comp.arch.embedded |
| Subject | Re: Cortex-M: share an int between two tasks |
| Date | 2020-03-23 14:31 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <r5admv$9ho$1@dont-email.me> (permalink) |
| References | (7 earlier) <r4vhdd$d8d$1@dont-email.me> <r4vk2i$uas$1@dont-email.me> <r4vvnc$c9j$1@dont-email.me> <4H1eG.131641$xv4.107395@fx18.iad> <r5ab4n$jag$1@dont-email.me> |
On 23/03/2020 13:47, pozz wrote:
> Il 23/03/2020 13:10, Richard Damon ha scritto:
>> On 3/19/20 10:31 AM, pozz wrote:
>>> Il 19/03/2020 12:12, David Brown ha scritto:
>
>>>> One possible arrangement could be:
>>>>
>>>>
>>>> static volatile bool sensor_stopped;
>>>> static SemaphoreHandle_t sensor_semaphore;
>>>>
>>>> // TaskA
>>>> static const TickType_t delay_ticks = pdMS_TO_TICKS(10);
>>>>
>>>> while (true) {
>>>> // Get sample and process
>>>> if (sensor_stopped) {
>>>> // Wait for semaphore to be ready
>>>> xSemaphoreTake(sensor_semaphore, portMAX_DELAY);
>>>> // Then release it again
>>>> xSemaphoreGive(sensor_semaphore);
>>>> }
>>>> vTaskDelay(delay_ticks); // DelayUntil
>>>> }
>>>>
>>>>
>>>> // TaskB
>>>> static void turn_off_sensor(void) {
>>>> sensor_stopped = True;
>>>> xSemaphoreTake(sensor_semaphore, 0);
>>>> }
>>>>
>>>> static void turn_on_sensor(void) {
>>>> sensor_stopped = False;
>>>> xSemaphoreGive(sensor_semaphore);
>>>> }
>>>>
>>>> The "sensor_stopped" flag is really just an optimisation, so that
>>>> TaskA doesn't have to keep checking the semaphore. It can be dropped
>>>> if you want.
>>>
>>> I'm not sure about xSemaphoreTake(sensor_semaphore, 0) in
>>> turn_off_sensor(). You pass a zero timeout. Are you sure you will be
>>> able to take the semaphore in *every* situation? What happens if
>>> turn_off_sensor() is run immediately after xSemaphoreTake(...,
>>> portMAX_DELAY) returns?
>>> In this case, I think taskB wouldn't be able to take the semaphore and
>>> so taskA doesn't really block. You will have sensor_stopped true, but
>>> the semaphore released.
>>>
>> A Take with a zero timeout will lower the semaphore if it happens to be
>> raised, and continue if is was lowered. This will cause task A to go and
>> block on the Semaphore until task B gives it again. The take operation
>> returns a flag as to if it succeeded, and Task a should really use a:
>> if(xSemaphoreTake(sensor_semaphore, portMAX_DELAY)) {
>> xSemaphoreGive(sensor_semaphore);
>> }
>
> If portMAX_DELAY is used in xSemaphoreTake(), I think the function
> returns only if it was able to take the semaphore (if we don't use
> functions that abruptly let one task out of blocked state).
>
>> Otherwise if something causes the Take to abort, the the sampling will
>> start back up.
>>
>> I would probably do the take before setting the flag, so if TaskA runs
>> between the statements it doesn't do its take, and pass.
>
> Anyway my objection is still there. If turn_off_sensor() is executed
> immediately after xSemaphoreTake(sensor_semaphore, portMAX_DELAY)
> returns, taskB wouldn't be able to take the semaphore, so taskA will
> wrongly continue doing its work.
Yes, I think the code is not quite right. I am not happy about it
anyway - it doesn't feel good. But as I said before, I think you would
be better with a re-think of the design at a higher level.
>
> I think we can use portMAX_DELAY in turn_off_sensor() too. In the worst
> case, taskB would wait for a very short time to take the semaphore, but
> usually it takes the semaphore immediately.
>
That depends on the timing requirements, but could work.
Back to comp.arch.embedded | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Cortex-M: share an int between two tasks pozz <pozzugno@gmail.com> - 2020-03-13 15:38 +0100
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-13 17:49 +0100
Re: Cortex-M: share an int between two tasks pozz <pozzugno@gmail.com> - 2020-03-18 09:09 +0100
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-18 09:17 +0100
Re: Cortex-M: share an int between two tasks Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2020-03-18 17:37 +0100
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-18 19:23 +0100
Re: Cortex-M: share an int between two tasks Les Cargill <lcargill99@comcast.com> - 2020-03-22 13:42 -0500
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-22 21:45 +0100
Re: Cortex-M: share an int between two tasks Richard Damon <Richard@Damon-Family.org> - 2020-03-22 20:50 -0400
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-23 09:09 +0100
Re: Cortex-M: share an int between two tasks Richard Damon <Richard@Damon-Family.org> - 2020-03-23 08:00 -0400
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-23 14:21 +0100
Re: Cortex-M: share an int between two tasks Clifford Heath <no.spam@please.net> - 2020-03-25 09:55 +1100
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-30 16:07 +0200
Re: Cortex-M: share an int between two tasks Clifford Heath <no.spam@please.net> - 2020-03-31 09:02 +1100
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-31 10:57 +0200
Re: Cortex-M: share an int between two tasks Clifford Heath <no.spam@please.net> - 2020-04-01 12:05 +1100
Re: Cortex-M: share an int between two tasks Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2020-03-30 17:49 +0300
Re: Cortex-M: share an int between two tasks kegs@provalid.com (Kent Dickey) - 2020-05-19 00:34 -0500
Re: Cortex-M: share an int between two tasks Clifford Heath <no.spam@please.net> - 2020-05-19 17:40 +1000
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-05-19 12:00 +0200
Re: Cortex-M: share an int between two tasks Tom Gardner <spamjunk@blueyonder.co.uk> - 2020-05-19 13:48 +0100
Re: Cortex-M: share an int between two tasks pozz <pozzugno@gmail.com> - 2020-03-18 09:20 +0100
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-18 10:06 +0100
Re: Cortex-M: share an int between two tasks pozz <pozzugno@gmail.com> - 2020-03-18 10:59 +0100
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-18 11:48 +0100
Re: Cortex-M: share an int between two tasks upsidedown@downunder.com - 2020-03-18 20:37 +0200
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-18 21:24 +0100
Re: Cortex-M: share an int between two tasks Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2020-03-19 14:05 +0200
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-19 13:16 +0100
Re: Cortex-M: share an int between two tasks Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2020-03-19 14:27 +0200
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-19 14:13 +0100
Re: Cortex-M: share an int between two tasks pozz <pozzugno@gmail.com> - 2020-03-19 08:03 +0100
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-19 09:10 +0100
Re: Cortex-M: share an int between two tasks upsidedown@downunder.com - 2020-03-19 12:51 +0200
Re: Cortex-M: share an int between two tasks pozz <pozzugno@gmail.com> - 2020-03-19 07:40 +0100
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-19 09:38 +0100
Re: Cortex-M: share an int between two tasks pozz <pozzugno@gmail.com> - 2020-03-19 11:26 +0100
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-19 12:12 +0100
Re: Cortex-M: share an int between two tasks pozz <pozzugno@gmail.com> - 2020-03-19 15:31 +0100
Re: Cortex-M: share an int between two tasks pozz <pozzugno@gmail.com> - 2020-03-23 09:22 +0100
Re: Cortex-M: share an int between two tasks Richard Damon <Richard@Damon-Family.org> - 2020-03-23 08:10 -0400
Re: Cortex-M: share an int between two tasks pozz <pozzugno@gmail.com> - 2020-03-23 13:47 +0100
Re: Cortex-M: share an int between two tasks David Brown <david.brown@hesbynett.no> - 2020-03-23 14:31 +0100
Re: Cortex-M: share an int between two tasks Richard Damon <Richard@Damon-Family.org> - 2020-03-23 13:50 -0400
Re: Cortex-M: share an int between two tasks Niklas Holsti <niklas.holsti@tidorum.invalid> - 2020-03-19 12:27 +0200
Re: Cortex-M: share an int between two tasks pozz <pozzugno@gmail.com> - 2020-03-19 11:45 +0100
Re: Cortex-M: share an int between two tasks upsidedown@downunder.com - 2020-03-19 13:27 +0200
Re: Cortex-M: share an int between two tasks antispam@math.uni.wroc.pl - 2020-03-18 16:24 +0000
csiph-web