Shresta
Posts: 31
Joined: Sun Jun 30, 2013 3:13 pm

RPi in Embedded System mode

Mon Sep 08, 2014 4:52 pm

Hi,
Is it possible to run RPi in Embedded System mode. I mean is it possible to configure RPi to start running a set program on Power-on and keep running till it is powered off. If it possible, what are the steps involved? Advise please.

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: RPi in Embedded System mode

Mon Sep 08, 2014 5:48 pm

One way to do that is to go really low-level and write your own kernel.img file. The Pi will load your file at boot time instead of a Linux kernel. You won't have any of the Linux drivers or libraries available, just what you build into your file. (See the Bare Metal forum for more information on that).

If you want to run with Linux, you could add "init=/path/to/myprog" to the end of the line in cmdline.txt and you will get a Linux kernel, root filesystem and your program running instead of the usual processes. You will have access to the libraries and all built-in drivers. If you mount the filesystems in read-only mode, then they will be safe against sudden power-off.

There are other ways of doing it, of course.

User avatar
lmarmisa
Posts: 1265
Joined: Thu Feb 14, 2013 2:22 am
Location: Jávea, Spain

Re: RPi in Embedded System mode

Mon Sep 08, 2014 5:52 pm

If you wish to run your program under the control of a Linux operating system, I recommend to install Raspbian. Then use the command "crontab -e" for launching your program at start-up.

Code: Select all

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
@reboot /home/pi/your_program_here
If you wish to develop a critical real-time system witouth an operating system (this task is recommended only for experts), take a look to these links related to bare metal programming:

http://www.valvers.com/embedded-linux/r ... ng-in-cpt1
http://www.raspberrypi.org/forums/viewforum.php?f=72

User avatar
socialdefect
Posts: 110
Joined: Mon Jun 25, 2012 9:02 pm
Location: Tilburg, the Netherlands
Contact: Website

Re: RPi in Embedded System mode

Mon Sep 08, 2014 11:57 pm

Why Crontab? Do you start your apps every 2 minutes and 7 seconds??

Depending on what your program needs to run (networking, devices, Xserver, etc) there are different ways. A simple low level program can run as explained in the first comment by setting init at boot.
If you need a Linux kernel and some system services you need to configure your OS (Raspbian, ArchLinuxArm) to start in headless mode and write a daemon script to start what you need (open a script in /etc/init.d/ directory for an example). If you only need to start one app without too much other configuration you can also use the script /etc/rc.local instead. rc.local is executed by init at boot (you might have to make it executable first).

When you need an xserver you need to configure the .xinitrc file in the user's home directory and start a window manager like matchbox or openbox and any other apps you might need.
== If it's not broke... I'm not done fixing it! ==

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: RPi in Embedded System mode

Tue Sep 09, 2014 5:37 am

socialdefect wrote:Why Crontab? Do you start your apps every 2 minutes and 7 seconds??
If you did start your app "every 2 minutes and 7 seconds", then crontab would be the wrong choice, as it only has a granularity of a minute.

However, there is nothing wrong with using the "@reboot" parameter in crontab to start a script shortly after boot up. It'll just take a little longer to start than any of the other methods of running something every time the system is booted.

User avatar
DougieLawson
Posts: 39126
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: RPi in Embedded System mode

Tue Sep 09, 2014 7:11 am

rpdom wrote:
However, there is nothing wrong with using the "@reboot" parameter in crontab to start a script shortly after boot up. It'll just take a little longer to start than any of the other methods of running something every time the system is booted.
There are a few ways to easily get things started at boot time.

1. crontab @reboot
2. /etc/rc.local
3. /etc/init.d script and symb links to it from /etc/rc2.d
4. /etc/inittab

Each has its own merits, so pick your poison.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Shresta
Posts: 31
Joined: Sun Jun 30, 2013 3:13 pm

Re: RPi in Embedded System mode

Tue Sep 16, 2014 2:28 am

How can I set the log rate to 10 Hz?

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: RPi in Embedded System mode

Tue Sep 16, 2014 6:10 am

Shresta wrote:How can I set the log rate to 10 Hz?
Have a loop in your program that checks the time.

Code: Select all

# Psuedo code. Not real code.
sample_time = time.msecs;
while true
    sample_time = sample_time + 100; # Set next sample to be in 0.1 seconds
    get_sample; # Get this sample, then wait
    while time.msecs < sample_time
        sleep(0.001) # sleep for 1000th second
    endwhile
endwhile
A bit crude, but you get the idea.

User avatar
lmarmisa
Posts: 1265
Joined: Thu Feb 14, 2013 2:22 am
Location: Jávea, Spain

Re: RPi in Embedded System mode

Tue Sep 16, 2014 7:48 am

This is an alternative pseudocode:

Code: Select all

time_next_iteration=time();      #  time() would be a system call for getting current time
while true
    time_current_iteration = time_next_iteration;
    your_periodic_function_here();  # Get a new sample or something else 
    time_next_iteration = time_current_iteration + 100msec;
    period_to_sleep = time_next_iteration - time();
    if period_to_sleep>0
       sleep(period_to_sleep) #sleep() would be a system call
    else
       print("Warning: the system is too slow for real-time requirements")
    endif
endwhile

Shresta
Posts: 31
Joined: Sun Jun 30, 2013 3:13 pm

Re: RPi in Embedded System mode

Thu Sep 25, 2014 4:22 pm

rpdom wrote:One way to do that is to go really low-level and write your own kernel.img file. The Pi will load your file at boot time instead of a Linux kernel. You won't have any of the Linux drivers or libraries available, just what you build into your file. (See the Bare Metal forum for more information on that).

If you want to run with Linux, you could add "init=/path/to/myprog" to the end of the line in cmdline.txt and you will get a Linux kernel, root filesystem and your program running instead of the usual processes. You will have access to the libraries and all built-in drivers. If you mount the filesystems in read-only mode, then they will be safe against sudden power-off.

There are other ways of doing it, of course.
I wanted to run it with Linux, I tried the method suggested in the para 2 above. It is not working. Am I missing something?

Shresta
Posts: 31
Joined: Sun Jun 30, 2013 3:13 pm

Re: RPi in Embedded System mode

Thu Sep 25, 2014 4:25 pm

DougieLawson wrote:
rpdom wrote:
However, there is nothing wrong with using the "@reboot" parameter in crontab to start a script shortly after boot up. It'll just take a little longer to start than any of the other methods of running something every time the system is booted.
There are a few ways to easily get things started at boot time.

1. crontab @reboot
2. /etc/rc.local
3. /etc/init.d script and symb links to it from /etc/rc2.d
4. /etc/inittab

Each has its own merits, so pick your poison.
Can you please give me a reference on how to implement the above methods. I mean does it need adding the file path to the files mentioned?

User avatar
DougieLawson
Posts: 39126
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: RPi in Embedded System mode

Thu Sep 25, 2014 4:58 pm

I'm 100% sure that using a Google search qualified with "site:raspberrypi.org" (to restrict it to this forum) will reveal countless posts on those subjects.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Return to “Beginners”