Take the 2-minute tour ×
Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It's 100% free, no registration required.

I have a project that I´ll be using RaspBerry Pi to make a controller for process control.

To do that I need to schedule processes periodically. Does anyone know how I could do that? So I have to compile kernel with my own modules and register them? Can I do it from user space? I don´t know where to start.

For example: every second the process should get data via the SPI and serial port, then it should make all calculations necessary and then send the answer via SPI.

PS: Does anyone know which bootloader the board uses?

share|improve this question
    
You don't need to write a kernel module, processes can schedule themselves with user space timers as Frepa suggests, which would be the normal and sane route. –  goldilocks Feb 16 '13 at 20:20
add comment

1 Answer 1

up vote 2 down vote accepted

A user-space program should take you a long way. But much depends on how critical the timing is in your application. If timing is critical, you might want to research real time operating systems (RTOS). There are also ways to make Linux more friendly towards real time applications. See this real time question.

If you don't have very strict timing demands, you could do something like this. This is pseudo-code, but I am vaguely thinking about Python.

while 1:
  read_SPI_input()
  calculate_something()
  write_SPI_output()
  sleep(1)             #pause 1 second

A important point is that sleep() might not pause for exactly the time you specify. On unixes, sleep is typically guaranteed to pause for at least the time given. Also note that the operating system may pause the execution of your program at any point. The more loaded the system is, the more likely this is to happen.

For slightly more precision, taking into account the time spent in the reading, calculating and writing steps:

nextTime = getCurrentTime() + 1
while 1:
  read_SPI_input()
  calculate_something()
  write_SPI_output()
  delta = nextTime - getCurrentTime()
  nextTime += 1
  sleep(delta)   #sleep so that the loop takes 1 s in total 

If delta < 0, one event has already been missed. This should be handled somehow.

Python's sleep accepts a floating point number. In C it demands an integer number of seconds. However, C has a usleep function for microseconds.

For the SPI part see here, and for GPIO, both are Python-oriented. Otherwise search this site, there have been several GPIO and SPI questions.

share|improve this answer
1  
FYI: sleep() actually is not standard C, so it varies from platform to platform, but on POSIX it does use seconds. usleep() parallels the usleep CLI utility; WRT C it is obsoleted in POSIX in favour of nanosleep(). A normal linux kernel has a latency of 10ms and your timing will be most accurate if you round off to that appropriately. If what you are doing is on the scale of seconds, it should be fine. –  goldilocks Feb 16 '13 at 20:17
    
@Frepa Thanks for your help. In my case I really need something reliable in terms o timing. I'll do what you told me. Thank you. –  morcillo Feb 16 '13 at 20:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.