<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic RTOS locking in Sensors</title>
    <link>https://community.nxp.com/t5/Sensors/RTOS-locking/m-p/740363#M4752</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'll be using the sensor fusion library with an RTOS (uC/OS-II, not FreeRTOS, but I don't think that is relevant). The sensor fusion user's guide discusses using an RTOS and gives an example showing two tasks: one for reading data from the sensors (read_task) and another to process the data (fusion_task).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I don't see any locks (semaphore, mutex, etc) to serialize access to the raw sensor data. Depending what other tasks are running in the RTOS, I think it would be theoretically possible for the read_task to modify sensor data while the fusion_task is still processing it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I think it might be enough to put a lock around&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;sfg.readSensor()&lt;/P&gt;&lt;P&gt;in read_task and around&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;sfg.conditionSensorReadings()&lt;/P&gt;&lt;P&gt;in fusion_task. Is this correct? Is that sufficient locking to&amp;nbsp;serialize data shared between the two tasks?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;mike&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 27 Nov 2017 19:11:18 GMT</pubDate>
    <dc:creator>mike_durian</dc:creator>
    <dc:date>2017-11-27T19:11:18Z</dc:date>
    <item>
      <title>RTOS locking</title>
      <link>https://community.nxp.com/t5/Sensors/RTOS-locking/m-p/740363#M4752</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'll be using the sensor fusion library with an RTOS (uC/OS-II, not FreeRTOS, but I don't think that is relevant). The sensor fusion user's guide discusses using an RTOS and gives an example showing two tasks: one for reading data from the sensors (read_task) and another to process the data (fusion_task).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I don't see any locks (semaphore, mutex, etc) to serialize access to the raw sensor data. Depending what other tasks are running in the RTOS, I think it would be theoretically possible for the read_task to modify sensor data while the fusion_task is still processing it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I think it might be enough to put a lock around&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;sfg.readSensor()&lt;/P&gt;&lt;P&gt;in read_task and around&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;sfg.conditionSensorReadings()&lt;/P&gt;&lt;P&gt;in fusion_task. Is this correct? Is that sufficient locking to&amp;nbsp;serialize data shared between the two tasks?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;mike&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 27 Nov 2017 19:11:18 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Sensors/RTOS-locking/m-p/740363#M4752</guid>
      <dc:creator>mike_durian</dc:creator>
      <dc:date>2017-11-27T19:11:18Z</dc:date>
    </item>
    <item>
      <title>Re: RTOS locking</title>
      <link>https://community.nxp.com/t5/Sensors/RTOS-locking/m-p/740364#M4753</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Mike,&lt;/P&gt;&lt;P&gt;The FreeRTOS variant that is part of the SDK uses FreeRTOS events to coordinate between tasks.&amp;nbsp;Lower frequency tasks use xEventGroupWaitBits() to stall until&amp;nbsp; the appropriate control bit has been set by the next higher frequency task.&amp;nbsp; &amp;nbsp;So there's never a chance of lockup.&lt;/P&gt;&lt;P&gt;And I agree you will need to do something similar with whatever RTOS implementation you chose.&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Mike Stanley&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 28 Nov 2017 15:56:20 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Sensors/RTOS-locking/m-p/740364#M4753</guid>
      <dc:creator>michaelestanley</dc:creator>
      <dc:date>2017-11-28T15:56:20Z</dc:date>
    </item>
    <item>
      <title>Re: RTOS locking</title>
      <link>https://community.nxp.com/t5/Sensors/RTOS-locking/m-p/740365#M4754</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I don't think we can trust that the lower priority task, fusion_task, in this case, will complete its computation on the data before the higher priority task, read_task, needs to run again. This is especially true if this is a real-world application that has other non-sensor tasks that need to run at higher priorities. For example,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;read_task gets some data and sets a flag so fusion_task will run, then waits for the next sampling time&lt;/LI&gt;&lt;LI&gt;fusion_task starts running, but gets interrupted by a higher priority task(s)&lt;/LI&gt;&lt;LI&gt;higher priority task(s) run - taking longer than the read_task sampling interval - and then finish&lt;/LI&gt;&lt;LI&gt;the RTOS scheduler picks the next task to run, which is read_task since it's done with its wait period and has a higher priority than fusion_task&lt;/LI&gt;&lt;LI&gt;now read_task is modifying data that fusion_task hasn't finished processing&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This sort of thing might be unlikely to occur, but since it can occur, we need to protect against it. In addition to the mechanism that read_task uses to signal fusion_task to run (this would be a flag in uC/OS-II), there also needs to be a semaphore or mutex to serialize access to the data shared between the two tasks.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I think the only shared data between read_task and fusion_task is the raw sensor data and I think fusion_task is done with the raw data after its call to conditionSensorReadings(), but I don't know the code well enough to say for certain that that is true.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It would look something like&lt;/P&gt;&lt;P&gt;read_task:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;get lock&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;sfg.readSensors()&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;release lock&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;fusion_task:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;get lock&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;sfg.conditionSensorReadings()&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;release lock&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;mike&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 28 Nov 2017 16:25:06 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Sensors/RTOS-locking/m-p/740365#M4754</guid>
      <dc:creator>mike_durian</dc:creator>
      <dc:date>2017-11-28T16:25:06Z</dc:date>
    </item>
    <item>
      <title>Re: RTOS locking</title>
      <link>https://community.nxp.com/t5/Sensors/RTOS-locking/m-p/740366#M4755</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Good points and we always appreciate suggestions.&amp;nbsp; I double checked the code.&amp;nbsp; Accel and Mag raw data ARE consumed in conditionSensorReadings.&amp;nbsp; Gyro raw data is consumed both there as well as near the top of the Kalman filters.&amp;nbsp; We've never encountered problems because these operations typically occur well within our FAST_LOOP_HZ interval, which is fairly long relatively speaking.&amp;nbsp; If this wasn't sufficient, we would really need (in addition to your suggestions) to add extra FIFO buffers, increasing RAM utilization, as we wouldn't want to risk losing sensor samples.&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Mike&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 29 Nov 2017 16:51:45 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Sensors/RTOS-locking/m-p/740366#M4755</guid>
      <dc:creator>michaelestanley</dc:creator>
      <dc:date>2017-11-29T16:51:45Z</dc:date>
    </item>
    <item>
      <title>Re: RTOS locking</title>
      <link>https://community.nxp.com/t5/Sensors/RTOS-locking/m-p/740367#M4756</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Mike,&lt;/P&gt;&lt;P&gt;Thanks for checking on this.&amp;nbsp;Can you provide a little more details on where the raw gyro data is used in the Kalman filters? I might try to break it out so I can lock it without having to lock the entire Kalman computation, though I'll do that if it gets too messy.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've got another computation heavy task that needs to run at a higher priority than the sensor fusion stuff. I also have some high priority tasks that need to be low latency. I've found that even if I think something is unlikely, it I can imagine a failure scenario, it will come back to bite me, so I'm going to add the locking. I'll keep my eye on the FIFO usage and see if I need to enlarge that later.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm also changing the behavior of the read_task by slowing down how often it reads data. Instead of reading about once for every sensor sample, I'm going to wait for the smallest hardware FIFO to get&amp;nbsp;1/2 - 3/4 full before reading. I think fewer, larger sensor reads might help. I'm going to uncouple the fusion_task so it runs on its own schedule, instead of running after every read_task read. With proper locking, I don't think this will pose a problem. Ideally, the read_task would act in response to FIFO interrupts from the sensors, but my quick check didn't turn up a configurable watermark interrupt for the magnetometer part of the FXOS8700.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;One last thing. Unless there is good locking in the I2C driver, the calls to put the gyro in stand-by mode should probably happen in the read_task, not the fusion_task. That way all I2C access happens in the read_task. You wouldn't want both the fusion_task and read_task trying to use the I2C bus at the same time. The motion check call can still happen in the fusion_task - it just needs to signal the read_task about the stationary status.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;mike&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 29 Nov 2017 17:44:02 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Sensors/RTOS-locking/m-p/740367#M4756</guid>
      <dc:creator>mike_durian</dc:creator>
      <dc:date>2017-11-29T17:44:02Z</dc:date>
    </item>
  </channel>
</rss>

