[EE]:: Simple Task Scheduler written in C

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

[EE]:: Simple Task Scheduler written in C

RussellMc
Merit thereof unknown.
2012

https://www.cs.ucr.edu/~vahid/rios/

Code for pre-emptive version absent.

____________________________

RIOS is a task scheduler written entirely in C that:

   - Is simple and understandable for the beginning embedded programmer
   - Can provide basic non-preemptive or preemptive multitasking
   capabilities for cooperative tasks
   - Requires only a few dozen lines of C code. Reduces need for RTOS
   (real-time operating system).

VersionsRIOS comes in three versions. A non-preemptive version with either
simple or state-machine based tasks. Simple examples for each version is
available below for you to copy/paste. Each example targets the RIMS
software (Programming Embedded Systems
<http://www.programmingembeddedsystems.com/RITools>). By copying/pasting,
downloading, or using the code in any way, you are agreeing to the EULA
<https://www.cs.ucr.edu/~vahid/rios/eula.txt>.
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: [EE]:: Simple Task Scheduler written in C

Harold Hallikainen-3
Thanks for posting! I've really gotten use to just using state machines
with switch/case statements. Each "task" has an input FIFO that other
tasks can dump complete messages into. When the main loop calls a task, it
immediately returns if there is nothing in its input FIFO. If there is
something there, it acts on each message, generally outputting to other
task input FIFOs. Not preemptive, but simple! If needed, priority of a
task can be increased by putting it in the main loop more than once.

Harold


--
FCC Rules Updated Daily at http://www.hallikainen.com
Not sent from an iPhone.
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: [EE]:: Simple Task Scheduler written in C

sergio


On Tue, 16 Feb 2021, Harold Hallikainen wrote:

> Thanks for posting! I've really gotten use to just using state machines
> with switch/case statements. Each "task" has an input FIFO that other
> tasks can dump complete messages into. When the main loop calls a task, it
> immediately returns if there is nothing in its input FIFO. If there is
> something there, it acts on each message, generally outputting to other
> task input FIFOs. Not preemptive, but simple! If needed, priority of a
> task can be increased by putting it in the main loop more than once.

Hi Harold,

I respect your use of state machines (see my own use if you have the
slightest doubt :-)), however I think you might be interested in the
following. It is a simple implementation of co-routines (cooperative
multitasking) for the PIC16.

BEWARE you cannot call yield from inside a subroutine that has been called
via the hardware "call" instruction. BUT there is nothing to create your
own calling mechanism that circumvents the "call" instruction which is
itself similar to the "yield" macro.

I do something similar to this within the XCSB compiler.

Regards
Sergio Masci


--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: [EE]:: Simple Task Scheduler written in C

sergio
In reply to this post by Harold Hallikainen-3

oops! hit the wrong key :-)

here's the code:

  .data

alt_task_pc: .dsw 1
tmp_bl: .dsw 1

yield .macro arg

  movf arg+0,w
  movwf tmp_bl+0
  movf arg+1,w
  movwf tmp_bl+1

; <<<< this is the really really important trick!!!
; use the assembler to generate the return address
; not an instruction such as "call"

  movlw ($+8) & 0xff ; low (addr of "lab9")
  movwf arg+0
  movlw (($+8) >> 8) & 0xff ; high (addr of "lab9")
  movwf arg+1

  movf tmp_bl+1,w
  movwf PCLATH
  movf tmp_bl+0,w
  movwf PCL

; lab9

  .endm


new_task .macro arg1, arg2

  movlw (arg2) & 0xff ; low (addr of "task")
  movwf arg1+0
  movlw ((arg2) >> 8) & 0xff ; high (addr of "task")
  movwf arg1+1

  .endm

;======================

; example of use

  .code

main:
  new_task alt_task_pc, task2

lab1_1:

  ; do task1 stuff

  yield alt_task_pc

  ; do more task2 stuff

  yield alt_task_pc

  goto lab1

;======================

task2:

lab2_1
  ; do task2 stuff

  yield alt_task_pc

  ; do more task2 stuff

  yield alt_task_pc

  goto lab2_1


--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: [EE]:: Simple Task Scheduler written in C

Harold Hallikainen-3
Reminds me of something I wrote a LONG time ago!

http://www.piclist.com/techref/microchip/language/c/MultiTask.c.htm

Harold


>
> oops! hit the wrong key :-)
>
> here's the code:
>
>   .data
>
> alt_task_pc: .dsw 1
> tmp_bl: .dsw 1
>
> yield .macro arg
>
>   movf arg+0,w
>   movwf tmp_bl+0
>   movf arg+1,w
>   movwf tmp_bl+1
>
> ; <<<< this is the really really important trick!!!
> ; use the assembler to generate the return address
> ; not an instruction such as "call"
>
>   movlw ($+8) & 0xff ; low (addr of "lab9")
>   movwf arg+0
>   movlw (($+8) >> 8) & 0xff ; high (addr of "lab9")
>   movwf arg+1
>
>   movf tmp_bl+1,w
>   movwf PCLATH
>   movf tmp_bl+0,w
>   movwf PCL
>
> ; lab9
>
>   .endm
>
>
> new_task .macro arg1, arg2
>
>   movlw (arg2) & 0xff ; low (addr of "task")
>   movwf arg1+0
>   movlw ((arg2) >> 8) & 0xff ; high (addr of "task")
>   movwf arg1+1
>
>   .endm
>
> ;======================
>
> ; example of use
>
>   .code
>
> main:
>   new_task alt_task_pc, task2
>
> lab1_1:
>
>   ; do task1 stuff
>
>   yield alt_task_pc
>
>   ; do more task2 stuff
>
>   yield alt_task_pc
>
>   goto lab1
>
> ;======================
>
> task2:
>
> lab2_1
>   ; do task2 stuff
>
>   yield alt_task_pc
>
>   ; do more task2 stuff
>
>   yield alt_task_pc
>
>   goto lab2_1
>
>
> --
> http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
> View/change your membership options at
> http://mailman.mit.edu/mailman/listinfo/piclist
>


--
FCC Rules Updated Daily at http://www.hallikainen.com
Not sent from an iPhone.
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: [EE]:: Simple Task Scheduler written in C

sergio


On Wed, 17 Feb 2021, Harold Hallikainen wrote:

> Reminds me of something I wrote a LONG time ago!
>
> http://www.piclist.com/techref/microchip/language/c/MultiTask.c.htm
>
> Harold
>

Hi Harold,

Thanks for the link.

However I think I need to emphasise that the code I presented does
***NOT*** use the hardware stack and is suitable for use on a low end
PIC16 which does not allow a running program to read from or write to the
hardware stack.

Friendly Regards
Sergio
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: [EE]:: Simple Task Scheduler written in C

Isaac Marino Bavaresco-2
In reply to this post by RussellMc
In my PICLIST page <http://www.piclist.com/Techref/member/IMB-yahoo-J86/index.htm> I have some related projects.

This one is a colaborative RTOS for PICs: < http://www.piclist.com/Techref/member/IMB-yahoo-J86/SimpleRTOS.htm>

It works, but I think only in a very specific version of Hi-Tech C.

This one is a Co-Routines framework that can work in any processor: <http://www.piclist.com/Techref/member/IMB-yahoo-J86/co-routines.htm>

This one is a real preemptive RTOS: <http://www.piclist.com/Techref/member/IMB-yahoo-J86/SimpleRTOS2.htm>

It was based on the structure of FreeRTOS, but with several changes.



Em 16 de fevereiro de 2021 07:09:04 BRT, RussellMc <[hidden email]> escreveu:

>Merit thereof unknown.
>2012
>
>https://www.cs.ucr.edu/~vahid/rios/
>
>Code for pre-emptive version absent.
>
>____________________________
>
>RIOS is a task scheduler written entirely in C that:
>
>   - Is simple and understandable for the beginning embedded programmer
>   - Can provide basic non-preemptive or preemptive multitasking
>   capabilities for cooperative tasks
>   - Requires only a few dozen lines of C code. Reduces need for RTOS
>   (real-time operating system).
>
>VersionsRIOS comes in three versions. A non-preemptive version with
>either
>simple or state-machine based tasks. Simple examples for each version
>is
>available below for you to copy/paste. Each example targets the RIMS
>software (Programming Embedded Systems
><http://www.programmingembeddedsystems.com/RITools>). By
>copying/pasting,
>downloading, or using the code in any way, you are agreeing to the EULA
><https://www.cs.ucr.edu/~vahid/rios/eula.txt>.
>--
>http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
>View/change your membership options at
>http://mailman.mit.edu/mailman/listinfo/piclist

--
Enviado de meu dispositivo Android com K-9 mail. Desculpe-me pela brevidade.
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist