Exploring Edison - Almost Real Time Linux
Written by Harry Fairhead   
Tuesday, 20 October 2015
Article Index
Exploring Edison - Almost Real Time Linux
Scheduling Commands
Realtime FIFO Scheduling

Real Time FIFO Scheduling

Now we can try the same test but with FIFO real time scheduling selected.

To do this we need to use the sched_setscheduler function:

sched_setscheduler(pid,SCHED_FIFO,&priority);

where pid is the thread id - if zero then the calling thread is used.

The second parameter sets the type of scheduling used FIFO in this case and the final parameter is a pointer to a structure that specifies the priority.

The modified program is: 

#include <stdio.h>
#include <stdlib.h>
#include "mraa.h"
#include <sched.h>
int main() {
 const struct sched_param priority={1};
 sched_setscheduler(0,SCHED_FIFO,&priority);
 mraa_gpio_context pin = mraa_gpio_init(13);
 mraa_gpio_dir(pin, MRAA_GPIO_OUT);
 mraa_gpio_use_mmaped(pin,1);
 for (;;) {
  mraa_gpio_write(pin, 0);
  mraa_gpio_write(pin, 1);
 }
 return MRAA_SUCCESS;
}

 

If you run this program you will discover that the result are very different - no matter what the load on the CPU. 

Average 14.9
Maximum 67.38
Minimum 13.5625

 

and the frequency table is:

 

micro seconds  no of pulses
0 0
10 0
20 65700
30 57
40 1
50 49
60 46
70 3
80 0
90 0
100 0
1000 0
20000 0

 

You can see that now the only problem is that we have around 100 pulses around 50 microseconds. Looking at the logic analyser trace reveals that there is a 50 microsecond pulse about every 10 milliseconds. 

Notice that this result is independent of CPU loading, the data above is for a heavily loaded CPU. What happens is that the program is loaded into one of the cores and it stays there as there are no other priority one programs running. All of the priority zero programs are scheduled using the other core to run. 

You can also try locking the programs memory to stop the system from paging it if other applications need a lot of memory. In practice this isn't a common occurrence on a device like the Edison but if you do need to do it then all you need is a call to mlock in sys/mman.h and to unlock to unlock it. You can lock the current memory or future memory allocations. In most cases it is sufficient to lock current memory unless you are using dynamic memory allocation. 

mlockall(MCL_CURRENT);

In the case of our test program locking memory make no difference as there is plenty of real memory to go around. 

Practical FIFO Scheduling

Adding a simple statement makes your program hog one of the processor cores and removes the long interruptions that occur when other threads are scheduled to run. 

You might think at this point that the best thing to do is set a priority of 99 and use FIFO scheduling as soon as your program is loaded. However sharing a single core between all of the other processes on the Edison can have some undesirable effects. 

If you try this you will discover that ever so often - about every ten minutes - the WiFi link will fail. The reason is most likely that the WiFi thread doesn't get to run sufficiently often. There are likely to be other more subtle problems.

To avoid these it is a good idea to only enable FIFO scheduling when it is absolutely needed or to use the yield command at regular intervals.

For example if you are writing a program that has to decode an incoming pulse stream then FIFO scheduling it for the time it actually does the decoding is the best option. In this situation you would still have to cope with a possible 50 microsecond delay every 10 milliseconds. 

This raises the question of where the 50 microsecond delay originates?

Without more information it is difficult to be sure, but the most likely suspect is a System Management Interrupt, SMI. This is used by all Intel ICH chip sets which includes the Atom. An SMI is something that happens outside of the operating system and it is often necessary for the correct operation of the hardware. The bottom line is that SMIs cannot easily be turned off and this problem that effects all operating systems, including real time operating systems. 

At this point things get complicated and you have to start worrying about power management issues, DMA and many other things that a complex CPU and operating system does behind the scenes. 

If you can't work within the FIFO restrictions then you are probably better off finding a completely different solution - use an mcu, perhaps even the mcu on the Edison chip, or use dedicated hardware that carries out the operation away from the software like the UART or the PWM GPIO lines. More about this approach in the next chapter.  

More Information

The starting point for finding out about all Intel's Internet of Things resources, including Edison, is the Intel IoT Developer Zone.

 

Exploring Edison

ExploringEdison

 

Now On Sale!

You can now buy a print edition of Exploring Intel Edison.
You can buy it from:
 

USA and World  Amazon.com
Canada              Amazon.ca
UK                      Amazon.co.uk
France                Amazon.fr
Germany            Amazon.de
Spain                  Amazon.es
Brazil                  Amazon.br
Italy                    Amazon.it
Japan                 Amazon.co.jp
Mexico               Amazon.com.mx 


Chapter List

  1. Meet Edison
    In this chapter we consider the Edison's pros and cons and get an overview of its structure and the ways in which you can make use of it. If you have ever wondered if you need an Edison or an Arduino or even a Raspberry Pi then this is the place to start. 

  2. First Contact
    When you are prototyping with the Edison you are going to need to use one of the two main breakout boards - the Arduino or the mini. This chapter explains how to set up the Edison for both configurations. 

  3. In C
    You can program the Edison in Python, JavaScript or C/C+ but there are big advantages in choosing C. It is fast, almost as easy as the other languages and gives you direct access to everything. It is worth the effort and in this chapter we show you how to set up the IDE and get coding. 

  4. Mraa GPIO
    Using the mraa library is the direct way to work with the GPIO lines and you have to master it. Output is easy but you do need to be aware of how long everything takes. Input is also easy but using it can be more difficult. You can use polling or the Edison interrupt system which might not work exactly as you would expect.

  5. Fast Memory Mapped I/O
    There is a faster way to work with GPIO lines - memory mapped I/O. Using this it is possible to generate pulses as short at 0.25 microsecond and read pulse widths of 5 microseconds. However getting things right can be tricky. We look at how to generate fast accurate pulses of a given width and how to measure pulse widths.

  6. Near Realtime Linux 
    You need to be aware how running your programs under a non-realtime operating system like Yocto Linux effects timings and how accurately you can create pulse trains and react to the outside world. In this chapter we look the realtime facilities in every version of Linux. 

  7. Sophisticated GPIO - Pulse Width Modulation 
    Using the PWM mode of the GPIO lines is often the best way of solving control problems. PWM means you can dim an LED or position a servo and all using mraa. 

  8. Sophisticated GPIO -  I2C 
    I2C is a simple communications bus that allows you to connect any of a very large range of sensors. 

  9. I2C - Measuring Temperature  
    After looking at the theory of using I2C here is a complete case study using the SparkFun HTU21D hardware and software. 
     
  10. Life At 1.8V
    How to convert a 1.8V input or output to work with 5V or 3.3V including how to deal with bidirectional pull-up buses.

  11. Using the DHT11/22 Temperature Humidity Sensor at 1.8V 
    In this chapter we make use of all of the ideas introduced in earlier chapters to create a raw interface with the low cost DHT11/22 temperature and humidity sensor. It is an exercise in interfacing two logic families and implementing a protocol directly in C. 

  12. The DS18B20 1-Wire Temperature 
    The Edison doesn't have built in support for the Maxim 1-Wire bus and this means you can't use the very popular DS18B20 temperature sensor. However with a little careful planning you can and you can do it from user rather than kernel space. 

  13. Using the SPI Bus 
    The SPI bus can be something of a problem because it doesn't have a well defined standard that every device conforms to. Even so, if you only want to work with one specific device it is usually easy to find a configuration that works - as long as you understand what the possibilities are. 

  14. SPI in Practice The MCP3008 AtoD 
    The SPI bus can be difficult to make work at first, but once you know what to look for about how the slave claims to work it gets easier. To demonstrate how its done let's add eight channels of 12-bit AtoD using the MCP3008.

  15. Beyond mraa - Controlling the features mraa doesn't. 
    There is a Linux-based approach to working with GPIO lines and serial buses that is worth knowing about because it provides an alternative to using the mraa library. Sometimes you need this because you are working in a language for which mraa isn't available. It also lets you access features that mraa doesn't make available. 
 

<ASIN:1871962447>

More Information

Edison Getting Started

Edison Software Downloads

Edison IDE Installers

Intel IoT Developer Kit

 

Related Articles

Powerful New Features For Edison 

Intel Announces IoT Platform       

 

 

To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter

 

graphics

 



 

Comments




or email your comment to: comments@i-programmer.info

 

 

<ASIN:B00ND1KH42@COM>

<ASIN:B00ND1KNXM>

<ASIN:B00ND1KH10>



Last Updated ( Tuesday, 14 March 2017 )